Merge branch 'blender2.7'
[blender.git] / source / blender / blenkernel / intern / displist.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/displist.c
29  *  \ingroup bke
30  */
31
32
33 #include <math.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_curve_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_vfont_types.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_memarena.h"
47 #include "BLI_math.h"
48 #include "BLI_scanfill.h"
49 #include "BLI_utildefines.h"
50
51 #include "BKE_displist.h"
52 #include "BKE_cdderivedmesh.h"
53 #include "BKE_object.h"
54 #include "BKE_library.h"
55 #include "BKE_mball.h"
56 #include "BKE_mball_tessellate.h"
57 #include "BKE_mesh.h"
58 #include "BKE_curve.h"
59 #include "BKE_key.h"
60 #include "BKE_anim.h"
61 #include "BKE_font.h"
62 #include "BKE_lattice.h"
63 #include "BKE_modifier.h"
64
65 #include "BLI_sys_types.h" // for intptr_t support
66
67 #include "DEG_depsgraph.h"
68 #include "DEG_depsgraph_query.h"
69
70 static void boundbox_displist_object(Object *ob);
71
72 void BKE_displist_elem_free(DispList *dl)
73 {
74         if (dl) {
75                 if (dl->verts) MEM_freeN(dl->verts);
76                 if (dl->nors) MEM_freeN(dl->nors);
77                 if (dl->index) MEM_freeN(dl->index);
78                 if (dl->bevel_split) MEM_freeN(dl->bevel_split);
79                 MEM_freeN(dl);
80         }
81 }
82
83 void BKE_displist_free(ListBase *lb)
84 {
85         DispList *dl;
86
87         while ((dl = BLI_pophead(lb))) {
88                 BKE_displist_elem_free(dl);
89         }
90 }
91
92 DispList *BKE_displist_find_or_create(ListBase *lb, int type)
93 {
94         DispList *dl;
95
96         dl = lb->first;
97         while (dl) {
98                 if (dl->type == type)
99                         return dl;
100                 dl = dl->next;
101         }
102
103         dl = MEM_callocN(sizeof(DispList), "find_disp");
104         dl->type = type;
105         BLI_addtail(lb, dl);
106
107         return dl;
108 }
109
110 DispList *BKE_displist_find(ListBase *lb, int type)
111 {
112         DispList *dl;
113
114         dl = lb->first;
115         while (dl) {
116                 if (dl->type == type)
117                         return dl;
118                 dl = dl->next;
119         }
120
121         return NULL;
122 }
123
124 bool BKE_displist_has_faces(ListBase *lb)
125 {
126         DispList *dl;
127
128         for (dl = lb->first; dl; dl = dl->next) {
129                 if (ELEM(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)) {
130                         return true;
131                 }
132         }
133
134         return false;
135 }
136
137 void BKE_displist_copy(ListBase *lbn, ListBase *lb)
138 {
139         DispList *dln, *dl;
140
141         BKE_displist_free(lbn);
142
143         dl = lb->first;
144         while (dl) {
145                 dln = MEM_dupallocN(dl);
146                 BLI_addtail(lbn, dln);
147                 dln->verts = MEM_dupallocN(dl->verts);
148                 dln->nors = MEM_dupallocN(dl->nors);
149                 dln->index = MEM_dupallocN(dl->index);
150
151                 if (dl->bevel_split) {
152                         dln->bevel_split = MEM_dupallocN(dl->bevel_split);
153                 }
154
155                 dl = dl->next;
156         }
157 }
158
159 void BKE_displist_normals_add(ListBase *lb)
160 {
161         DispList *dl = NULL;
162         float *vdata, *ndata, nor[3];
163         float *v1, *v2, *v3, *v4;
164         float *n1, *n2, *n3, *n4;
165         int a, b, p1, p2, p3, p4;
166
167         dl = lb->first;
168
169         while (dl) {
170                 if (dl->type == DL_INDEX3) {
171                         if (dl->nors == NULL) {
172                                 dl->nors = MEM_callocN(sizeof(float) * 3, "dlnors");
173
174                                 if (dl->flag & DL_BACK_CURVE) {
175                                         dl->nors[2] = -1.0f;
176                                 }
177                                 else {
178                                         dl->nors[2] = 1.0f;
179                                 }
180                         }
181                 }
182                 else if (dl->type == DL_SURF) {
183                         if (dl->nors == NULL) {
184                                 dl->nors = MEM_callocN(sizeof(float) * 3 * dl->nr * dl->parts, "dlnors");
185
186                                 vdata = dl->verts;
187                                 ndata = dl->nors;
188
189                                 for (a = 0; a < dl->parts; a++) {
190
191                                         if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
192                                                 break;
193
194                                         v1 = vdata + 3 * p1;
195                                         n1 = ndata + 3 * p1;
196                                         v2 = vdata + 3 * p2;
197                                         n2 = ndata + 3 * p2;
198                                         v3 = vdata + 3 * p3;
199                                         n3 = ndata + 3 * p3;
200                                         v4 = vdata + 3 * p4;
201                                         n4 = ndata + 3 * p4;
202
203                                         for (; b < dl->nr; b++) {
204                                                 normal_quad_v3(nor, v1, v3, v4, v2);
205
206                                                 add_v3_v3(n1, nor);
207                                                 add_v3_v3(n2, nor);
208                                                 add_v3_v3(n3, nor);
209                                                 add_v3_v3(n4, nor);
210
211                                                 v2 = v1; v1 += 3;
212                                                 v4 = v3; v3 += 3;
213                                                 n2 = n1; n1 += 3;
214                                                 n4 = n3; n3 += 3;
215                                         }
216                                 }
217                                 a = dl->parts * dl->nr;
218                                 v1 = ndata;
219                                 while (a--) {
220                                         normalize_v3(v1);
221                                         v1 += 3;
222                                 }
223                         }
224                 }
225                 dl = dl->next;
226         }
227 }
228
229 void BKE_displist_count(ListBase *lb, int *totvert, int *totface, int *tottri)
230 {
231         DispList *dl;
232
233         for (dl = lb->first; dl; dl = dl->next) {
234                 int vert_tot = 0;
235                 int face_tot = 0;
236                 int tri_tot = 0;
237
238                 switch (dl->type) {
239                         case DL_SURF:
240                         {
241                                 vert_tot = dl->nr * dl->parts;
242                                 face_tot = (dl->nr - 1) * (dl->parts - 1);
243                                 tri_tot  = face_tot * 2;
244                                 break;
245                         }
246                         case DL_INDEX3:
247                         {
248                                 vert_tot = dl->nr;
249                                 face_tot = dl->parts;
250                                 tri_tot  = face_tot;
251                                 break;
252                         }
253                         case DL_INDEX4:
254                         {
255                                 vert_tot = dl->nr;
256                                 face_tot = dl->parts;
257                                 tri_tot  = face_tot * 2;
258                                 break;
259                         }
260                         case DL_POLY:
261                         case DL_SEGM:
262                         {
263                                 vert_tot = dl->nr * dl->parts;
264                                 break;
265                         }
266                 }
267
268                 *totvert += vert_tot;
269                 *totface += face_tot;
270                 *tottri  += tri_tot;
271         }
272 }
273
274 bool BKE_displist_surfindex_get(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4)
275 {
276         if ((dl->flag & DL_CYCL_V) == 0 && a == (dl->parts) - 1) {
277                 return false;
278         }
279
280         if (dl->flag & DL_CYCL_U) {
281                 (*p1) = dl->nr * a;
282                 (*p2) = (*p1) + dl->nr - 1;
283                 (*p3) = (*p1) + dl->nr;
284                 (*p4) = (*p2) + dl->nr;
285                 (*b) = 0;
286         }
287         else {
288                 (*p2) = dl->nr * a;
289                 (*p1) = (*p2) + 1;
290                 (*p4) = (*p2) + dl->nr;
291                 (*p3) = (*p1) + dl->nr;
292                 (*b) = 1;
293         }
294
295         if ((dl->flag & DL_CYCL_V) && a == dl->parts - 1) {
296                 (*p3) -= dl->nr * dl->parts;
297                 (*p4) -= dl->nr * dl->parts;
298         }
299
300         return true;
301 }
302
303 /* ****************** make displists ********************* */
304 #ifdef __INTEL_COMPILER
305 /* ICC with the optimization -02 causes crashes. */
306 #   pragma intel optimization_level 1
307 #endif
308 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase,
309                               const bool for_render, const bool use_render_resolution)
310 {
311         Nurb *nu;
312         DispList *dl;
313         BezTriple *bezt, *prevbezt;
314         BPoint *bp;
315         float *data;
316         int a, len, resolu;
317         const bool editmode = (!for_render && (cu->editnurb || cu->editfont));
318
319         nu = nubase->first;
320         while (nu) {
321                 if (nu->hide == 0 || editmode == false) {
322                         if (use_render_resolution && cu->resolu_ren != 0)
323                                 resolu = cu->resolu_ren;
324                         else
325                                 resolu = nu->resolu;
326
327                         if (!BKE_nurb_check_valid_u(nu)) {
328                                 /* pass */
329                         }
330                         else if (nu->type == CU_BEZIER) {
331                                 /* count */
332                                 len = 0;
333                                 a = nu->pntsu - 1;
334                                 if (nu->flagu & CU_NURB_CYCLIC) a++;
335
336                                 prevbezt = nu->bezt;
337                                 bezt = prevbezt + 1;
338                                 while (a--) {
339                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC))
340                                                 bezt = nu->bezt;
341
342                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT)
343                                                 len++;
344                                         else
345                                                 len += resolu;
346
347                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC) == 0)
348                                                 len++;
349
350                                         prevbezt = bezt;
351                                         bezt++;
352                                 }
353
354                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbez");
355                                 /* len+1 because of 'forward_diff_bezier' function */
356                                 dl->verts = MEM_mallocN((len + 1) * sizeof(float[3]), "dlverts");
357                                 BLI_addtail(dispbase, dl);
358                                 dl->parts = 1;
359                                 dl->nr = len;
360                                 dl->col = nu->mat_nr;
361                                 dl->charidx = nu->charidx;
362
363                                 data = dl->verts;
364
365                                 /* check that (len != 2) so we don't immediately loop back on ourselves */
366                                 if (nu->flagu & CU_NURB_CYCLIC && (dl->nr != 2)) {
367                                         dl->type = DL_POLY;
368                                         a = nu->pntsu;
369                                 }
370                                 else {
371                                         dl->type = DL_SEGM;
372                                         a = nu->pntsu - 1;
373                                 }
374
375                                 prevbezt = nu->bezt;
376                                 bezt = prevbezt + 1;
377
378                                 while (a--) {
379                                         if (a == 0 && dl->type == DL_POLY)
380                                                 bezt = nu->bezt;
381
382                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
383                                                 copy_v3_v3(data, prevbezt->vec[1]);
384                                                 data += 3;
385                                         }
386                                         else {
387                                                 int j;
388                                                 for (j = 0; j < 3; j++) {
389                                                         BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],
390                                                                                       prevbezt->vec[2][j],
391                                                                                       bezt->vec[0][j],
392                                                                                       bezt->vec[1][j],
393                                                                                       data + j, resolu, 3 * sizeof(float));
394                                                 }
395
396                                                 data += 3 * resolu;
397                                         }
398
399                                         if (a == 0 && dl->type == DL_SEGM) {
400                                                 copy_v3_v3(data, bezt->vec[1]);
401                                         }
402
403                                         prevbezt = bezt;
404                                         bezt++;
405                                 }
406                         }
407                         else if (nu->type == CU_NURBS) {
408                                 len = (resolu * SEGMENTSU(nu));
409
410                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
411                                 dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
412                                 BLI_addtail(dispbase, dl);
413                                 dl->parts = 1;
414
415                                 dl->nr = len;
416                                 dl->col = nu->mat_nr;
417                                 dl->charidx = nu->charidx;
418
419                                 data = dl->verts;
420                                 if (nu->flagu & CU_NURB_CYCLIC)
421                                         dl->type = DL_POLY;
422                                 else dl->type = DL_SEGM;
423                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
424                         }
425                         else if (nu->type == CU_POLY) {
426                                 len = nu->pntsu;
427                                 dl = MEM_callocN(sizeof(DispList), "makeDispListpoly");
428                                 dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
429                                 BLI_addtail(dispbase, dl);
430                                 dl->parts = 1;
431                                 dl->nr = len;
432                                 dl->col = nu->mat_nr;
433                                 dl->charidx = nu->charidx;
434
435                                 data = dl->verts;
436                                 if ((nu->flagu & CU_NURB_CYCLIC) && (dl->nr != 2)) {
437                                         dl->type = DL_POLY;
438                                 }
439                                 else {
440                                         dl->type = DL_SEGM;
441                                 }
442
443                                 a = len;
444                                 bp = nu->bp;
445                                 while (a--) {
446                                         copy_v3_v3(data, bp->vec);
447                                         bp++;
448                                         data += 3;
449                                 }
450                         }
451                 }
452                 nu = nu->next;
453         }
454 }
455
456 /**
457  * \param normal_proj: Optional normal thats used to project the scanfill verts into 2d coords.
458  * Pass this along if known since it saves time calculating the normal.
459  * \param flipnormal: Flip the normal (same as passing \a normal_proj negated)
460  */
461 void BKE_displist_fill(ListBase *dispbase, ListBase *to, const float normal_proj[3], const bool flipnormal)
462 {
463         ScanFillContext sf_ctx;
464         ScanFillVert *sf_vert, *sf_vert_new, *sf_vert_last;
465         ScanFillFace *sf_tri;
466         MemArena *sf_arena;
467         DispList *dlnew = NULL, *dl;
468         float *f1;
469         int colnr = 0, charidx = 0, cont = 1, tot, a, *index, nextcol = 0;
470         int totvert;
471         const int scanfill_flag = BLI_SCANFILL_CALC_REMOVE_DOUBLES | BLI_SCANFILL_CALC_POLYS | BLI_SCANFILL_CALC_HOLES;
472
473         if (dispbase == NULL)
474                 return;
475         if (BLI_listbase_is_empty(dispbase))
476                 return;
477
478         sf_arena = BLI_memarena_new(BLI_SCANFILL_ARENA_SIZE, __func__);
479
480         while (cont) {
481                 int dl_flag_accum = 0;
482                 cont = 0;
483                 totvert = 0;
484                 nextcol = 0;
485
486                 BLI_scanfill_begin_arena(&sf_ctx, sf_arena);
487
488                 dl = dispbase->first;
489                 while (dl) {
490                         if (dl->type == DL_POLY) {
491                                 if (charidx < dl->charidx)
492                                         cont = 1;
493                                 else if (charidx == dl->charidx) { /* character with needed index */
494                                         if (colnr == dl->col) {
495
496                                                 sf_ctx.poly_nr++;
497
498                                                 /* make editverts and edges */
499                                                 f1 = dl->verts;
500                                                 a = dl->nr;
501                                                 sf_vert = sf_vert_new = NULL;
502
503                                                 while (a--) {
504                                                         sf_vert_last = sf_vert;
505
506                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, f1);
507                                                         totvert++;
508
509                                                         if (sf_vert_last == NULL)
510                                                                 sf_vert_new = sf_vert;
511                                                         else {
512                                                                 BLI_scanfill_edge_add(&sf_ctx, sf_vert_last, sf_vert);
513                                                         }
514                                                         f1 += 3;
515                                                 }
516
517                                                 if (sf_vert != NULL && sf_vert_new != NULL) {
518                                                         BLI_scanfill_edge_add(&sf_ctx, sf_vert, sf_vert_new);
519                                                 }
520                                         }
521                                         else if (colnr < dl->col) {
522                                                 /* got poly with next material at current char */
523                                                 cont = 1;
524                                                 nextcol = 1;
525                                         }
526                                 }
527                                 dl_flag_accum |= dl->flag;
528                         }
529                         dl = dl->next;
530                 }
531
532                 /* XXX (obedit && obedit->actcol) ? (obedit->actcol - 1) : 0)) { */
533                 if (totvert && (tot = BLI_scanfill_calc_ex(&sf_ctx,
534                                                            scanfill_flag,
535                                                            normal_proj)))
536                 {
537                         if (tot) {
538                                 dlnew = MEM_callocN(sizeof(DispList), "filldisplist");
539                                 dlnew->type = DL_INDEX3;
540                                 dlnew->flag = (dl_flag_accum & (DL_BACK_CURVE | DL_FRONT_CURVE));
541                                 dlnew->col = colnr;
542                                 dlnew->nr = totvert;
543                                 dlnew->parts = tot;
544
545                                 dlnew->index = MEM_mallocN(tot * 3 * sizeof(int), "dlindex");
546                                 dlnew->verts = MEM_mallocN(totvert * 3 * sizeof(float), "dlverts");
547
548                                 /* vert data */
549                                 f1 = dlnew->verts;
550                                 totvert = 0;
551
552                                 for (sf_vert = sf_ctx.fillvertbase.first; sf_vert; sf_vert = sf_vert->next) {
553                                         copy_v3_v3(f1, sf_vert->co);
554                                         f1 += 3;
555
556                                         /* index number */
557                                         sf_vert->tmp.i = totvert;
558                                         totvert++;
559                                 }
560
561                                 /* index data */
562
563                                 index = dlnew->index;
564                                 for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
565                                         index[0] = sf_tri->v1->tmp.i;
566                                         index[1] = sf_tri->v2->tmp.i;
567                                         index[2] = sf_tri->v3->tmp.i;
568
569                                         if (flipnormal)
570                                                 SWAP(int, index[0], index[2]);
571
572                                         index += 3;
573                                 }
574                         }
575
576                         BLI_addhead(to, dlnew);
577                 }
578                 BLI_scanfill_end_arena(&sf_ctx, sf_arena);
579
580                 if (nextcol) {
581                         /* stay at current char but fill polys with next material */
582                         colnr++;
583                 }
584                 else {
585                         /* switch to next char and start filling from first material */
586                         charidx++;
587                         colnr = 0;
588                 }
589         }
590
591         BLI_memarena_free(sf_arena);
592
593         /* do not free polys, needed for wireframe display */
594 }
595
596 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
597 {
598         const float z_up[3] = {0.0f, 0.0f, 1.0f};
599         ListBase front, back;
600         DispList *dl, *dlnew;
601         float *fp, *fp1;
602         int a, dpoly;
603
604         BLI_listbase_clear(&front);
605         BLI_listbase_clear(&back);
606
607         dl = dispbase->first;
608         while (dl) {
609                 if (dl->type == DL_SURF) {
610                         if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U) == 0) {
611                                 if ((cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE)) {
612                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
613                                         BLI_addtail(&front, dlnew);
614                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
615                                         dlnew->nr = dl->parts;
616                                         dlnew->parts = 1;
617                                         dlnew->type = DL_POLY;
618                                         dlnew->flag = DL_BACK_CURVE;
619                                         dlnew->col = dl->col;
620                                         dlnew->charidx = dl->charidx;
621
622                                         fp = dl->verts;
623                                         dpoly = 3 * dl->nr;
624
625                                         a = dl->parts;
626                                         while (a--) {
627                                                 copy_v3_v3(fp1, fp);
628                                                 fp1 += 3;
629                                                 fp += dpoly;
630                                         }
631                                 }
632                                 if ((cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE)) {
633                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
634                                         BLI_addtail(&back, dlnew);
635                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
636                                         dlnew->nr = dl->parts;
637                                         dlnew->parts = 1;
638                                         dlnew->type = DL_POLY;
639                                         dlnew->flag = DL_FRONT_CURVE;
640                                         dlnew->col = dl->col;
641                                         dlnew->charidx = dl->charidx;
642
643                                         fp = dl->verts + 3 * (dl->nr - 1);
644                                         dpoly = 3 * dl->nr;
645
646                                         a = dl->parts;
647                                         while (a--) {
648                                                 copy_v3_v3(fp1, fp);
649                                                 fp1 += 3;
650                                                 fp += dpoly;
651                                         }
652                                 }
653                         }
654                 }
655                 dl = dl->next;
656         }
657
658         BKE_displist_fill(&front, dispbase, z_up, true);
659         BKE_displist_fill(&back, dispbase, z_up, false);
660
661         BKE_displist_free(&front);
662         BKE_displist_free(&back);
663
664         BKE_displist_fill(dispbase, dispbase, z_up, false);
665 }
666
667 static void curve_to_filledpoly(Curve *cu, ListBase *UNUSED(nurb), ListBase *dispbase)
668 {
669         if (!CU_DO_2DFILL(cu))
670                 return;
671
672         if (dispbase->first && ((DispList *) dispbase->first)->type == DL_SURF) {
673                 bevels_to_filledpoly(cu, dispbase);
674         }
675         else {
676                 const float z_up[3] = {0.0f, 0.0f, 1.0f};
677                 BKE_displist_fill(dispbase, dispbase, z_up, false);
678         }
679 }
680
681 /* taper rules:
682  * - only 1 curve
683  * - first point left, last point right
684  * - based on subdivided points in original curve, not on points in taper curve (still)
685  */
686 static float displist_calc_taper(Depsgraph *depsgraph, Scene *scene, Object *taperobj, float fac)
687 {
688         DispList *dl;
689
690         if (taperobj == NULL || taperobj->type != OB_CURVE)
691                 return 1.0;
692
693         dl = taperobj->runtime.curve_cache ? taperobj->runtime.curve_cache->disp.first : NULL;
694         if (dl == NULL) {
695                 BKE_displist_make_curveTypes(depsgraph, scene, taperobj, false, false);
696                 dl = taperobj->runtime.curve_cache->disp.first;
697         }
698         if (dl) {
699                 float minx, dx, *fp;
700                 int a;
701
702                 /* horizontal size */
703                 minx = dl->verts[0];
704                 dx = dl->verts[3 * (dl->nr - 1)] - minx;
705                 if (dx > 0.0f) {
706                         fp = dl->verts;
707                         for (a = 0; a < dl->nr; a++, fp += 3) {
708                                 if ((fp[0] - minx) / dx >= fac) {
709                                         /* interpolate with prev */
710                                         if (a > 0) {
711                                                 float fac1 = (fp[-3] - minx) / dx;
712                                                 float fac2 = (fp[0] - minx) / dx;
713                                                 if (fac1 != fac2)
714                                                         return fp[1] * (fac1 - fac) / (fac1 - fac2) + fp[-2] * (fac - fac2) / (fac1 - fac2);
715                                         }
716                                         return fp[1];
717                                 }
718                         }
719                         return fp[-2];  // last y coord
720                 }
721         }
722
723         return 1.0;
724 }
725
726 float BKE_displist_calc_taper(Depsgraph *depsgraph, Scene *scene, Object *taperobj, int cur, int tot)
727 {
728         float fac = ((float)cur) / (float)(tot - 1);
729
730         return displist_calc_taper(depsgraph, scene, taperobj, fac);
731 }
732
733 void BKE_displist_make_mball(Depsgraph *depsgraph, Scene *scene, Object *ob)
734 {
735         if (!ob || ob->type != OB_MBALL)
736                 return;
737
738         if (ob == BKE_mball_basis_find(scene, ob)) {
739                 if (ob->runtime.curve_cache) {
740                         BKE_displist_free(&(ob->runtime.curve_cache->disp));
741                 }
742                 else {
743                         ob->runtime.curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for MBall");
744                 }
745
746                 BKE_mball_polygonize(depsgraph, scene, ob, &ob->runtime.curve_cache->disp);
747                 BKE_mball_texspace_calc(ob);
748
749                 object_deform_mball(ob, &ob->runtime.curve_cache->disp);
750
751                 /* NOP for MBALLs anyway... */
752                 boundbox_displist_object(ob);
753         }
754 }
755
756 void BKE_displist_make_mball_forRender(Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase)
757 {
758         BKE_mball_polygonize(depsgraph, scene, ob, dispbase);
759         BKE_mball_texspace_calc(ob);
760
761         object_deform_mball(ob, dispbase);
762 }
763
764 static ModifierData *curve_get_tessellate_point(Scene *scene, Object *ob,
765                                                 const bool use_render_resolution, const bool editmode)
766 {
767         VirtualModifierData virtualModifierData;
768         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
769         ModifierData *pretessellatePoint;
770         int required_mode;
771
772         if (use_render_resolution)
773                 required_mode = eModifierMode_Render;
774         else
775                 required_mode = eModifierMode_Realtime;
776
777         if (editmode)
778                 required_mode |= eModifierMode_Editmode;
779
780         pretessellatePoint = NULL;
781         for (; md; md = md->next) {
782                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
783
784                 if (!modifier_isEnabled(scene, md, required_mode))
785                         continue;
786                 if (mti->type == eModifierTypeType_Constructive)
787                         return pretessellatePoint;
788
789                 if (ELEM(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
790                         pretessellatePoint = md;
791
792                         /* this modifiers are moving point of tessellation automatically
793                          * (some of them even can't be applied on tessellated curve), set flag
794                          * for information button in modifier's header
795                          */
796                         md->mode |= eModifierMode_ApplyOnSpline;
797                 }
798                 else if (md->mode & eModifierMode_ApplyOnSpline) {
799                         pretessellatePoint = md;
800                 }
801         }
802
803         return pretessellatePoint;
804 }
805
806 static void curve_calc_modifiers_pre(
807         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *nurb,
808         const bool for_render, const bool use_render_resolution)
809 {
810         VirtualModifierData virtualModifierData;
811         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
812         ModifierData *pretessellatePoint;
813         Curve *cu = ob->data;
814         int numElems = 0, numVerts = 0;
815         const bool editmode = (!for_render && (cu->editnurb || cu->editfont));
816         ModifierApplyFlag app_flag = 0;
817         float (*deformedVerts)[3] = NULL;
818         float *keyVerts = NULL;
819         int required_mode;
820
821         modifiers_clearErrors(ob);
822
823         if (editmode)
824                 app_flag |= MOD_APPLY_USECACHE;
825         if (use_render_resolution) {
826                 app_flag |= MOD_APPLY_RENDER;
827                 required_mode = eModifierMode_Render;
828         }
829         else
830                 required_mode = eModifierMode_Realtime;
831
832         const ModifierEvalContext mectx = {depsgraph, ob, app_flag};
833
834         pretessellatePoint = curve_get_tessellate_point(scene, ob, use_render_resolution, editmode);
835
836         if (editmode)
837                 required_mode |= eModifierMode_Editmode;
838
839         if (!editmode) {
840                 keyVerts = BKE_key_evaluate_object(ob, &numElems);
841
842                 if (keyVerts) {
843                         BLI_assert(BKE_keyblock_curve_element_count(nurb) == numElems);
844
845                         /* split coords from key data, the latter also includes
846                          * tilts, which is passed through in the modifier stack.
847                          * this is also the reason curves do not use a virtual
848                          * shape key modifier yet. */
849                         deformedVerts = BKE_curve_nurbs_keyVertexCos_get(nurb, keyVerts);
850                         numVerts = BKE_nurbList_verts_count(nurb);
851                 }
852         }
853
854         if (pretessellatePoint) {
855                 for (; md; md = md->next) {
856                         const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
857
858                         if (!modifier_isEnabled(scene, md, required_mode))
859                                 continue;
860                         if (mti->type != eModifierTypeType_OnlyDeform)
861                                 continue;
862
863                         if (!deformedVerts) {
864                                 deformedVerts = BKE_curve_nurbs_vertexCos_get(nurb, &numVerts);
865                         }
866
867                         mti->deformVerts(md, &mectx, NULL, deformedVerts, numVerts);
868
869                         if (md == pretessellatePoint)
870                                 break;
871                 }
872         }
873
874         if (deformedVerts) {
875                 BK_curve_nurbs_vertexCos_apply(nurb, deformedVerts);
876                 MEM_freeN(deformedVerts);
877         }
878         if (keyVerts) /* these are not passed through modifier stack */
879                 BKE_curve_nurbs_keyVertexTilts_apply(nurb, keyVerts);
880
881         if (keyVerts)
882                 MEM_freeN(keyVerts);
883 }
884
885 static float (*displist_get_allverts(ListBase *dispbase, int *totvert))[3]
886 {
887         DispList *dl;
888         float (*allverts)[3], *fp;
889
890         *totvert = 0;
891
892         for (dl = dispbase->first; dl; dl = dl->next)
893                 *totvert += (dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr;
894
895         allverts = MEM_mallocN((*totvert) * sizeof(float) * 3, "displist_get_allverts allverts");
896         fp = (float *)allverts;
897         for (dl = dispbase->first; dl; dl = dl->next) {
898                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
899                 memcpy(fp, dl->verts, sizeof(float) * offs);
900                 fp += offs;
901         }
902
903         return allverts;
904 }
905
906 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
907 {
908         DispList *dl;
909         const float *fp;
910
911         fp = (float *)allverts;
912         for (dl = dispbase->first; dl; dl = dl->next) {
913                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
914                 memcpy(dl->verts, fp, sizeof(float) * offs);
915                 fp += offs;
916         }
917 }
918
919 static void curve_calc_modifiers_post(
920         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *nurb,
921         ListBase *dispbase, Mesh **r_final,
922         const bool for_render, const bool use_render_resolution)
923 {
924         VirtualModifierData virtualModifierData;
925         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
926         ModifierData *pretessellatePoint;
927         Curve *cu = ob->data;
928         int required_mode = 0, totvert = 0;
929         const bool editmode = (!for_render && (cu->editnurb || cu->editfont));
930         Mesh *modified = NULL, *mesh_applied;
931         float (*vertCos)[3] = NULL;
932         int useCache = !for_render;
933         ModifierApplyFlag app_flag = 0;
934
935         if (use_render_resolution) {
936                 app_flag |= MOD_APPLY_RENDER;
937                 required_mode = eModifierMode_Render;
938         }
939         else
940                 required_mode = eModifierMode_Realtime;
941
942         const ModifierEvalContext mectx_deform = {depsgraph, ob,
943                                                   editmode ? app_flag | MOD_APPLY_USECACHE : app_flag};
944         const ModifierEvalContext mectx_apply = {depsgraph, ob,
945                                                  useCache ? app_flag | MOD_APPLY_USECACHE : app_flag};
946
947         pretessellatePoint = curve_get_tessellate_point(scene, ob, use_render_resolution, editmode);
948
949         if (editmode)
950                 required_mode |= eModifierMode_Editmode;
951
952         if (pretessellatePoint) {
953                 md = pretessellatePoint->next;
954         }
955
956         if (r_final && *r_final) {
957                 BKE_id_free(NULL, r_final);
958         }
959
960         for (; md; md = md->next) {
961                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
962
963                 if (!modifier_isEnabled(scene, md, required_mode))
964                         continue;
965
966                 /* If we need normals, no choice, have to convert to mesh now. */
967                 if (mti->dependsOnNormals != NULL && mti->dependsOnNormals(md) && modified == NULL) {
968                         if (vertCos != NULL) {
969                                 displist_apply_allverts(dispbase, vertCos);
970                         }
971
972                         if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
973                                 curve_to_filledpoly(cu, nurb, dispbase);
974                         }
975
976                         modified = BKE_mesh_new_nomain_from_curve_displist(ob, dispbase);
977                 }
978
979                 if (mti->type == eModifierTypeType_OnlyDeform ||
980                     (mti->type == eModifierTypeType_DeformOrConstruct && !modified))
981                 {
982                         if (modified) {
983                                 if (!vertCos) {
984                                         vertCos = BKE_mesh_vertexCos_get(modified, &totvert);
985                                 }
986                                 if (mti->dependsOnNormals != NULL && mti->dependsOnNormals(md)) {
987                                         BKE_mesh_ensure_normals(modified);
988                                 }
989                                 mti->deformVerts(md, &mectx_deform, modified, vertCos, totvert);
990                         }
991                         else {
992                                 if (!vertCos) {
993                                         vertCos = displist_get_allverts(dispbase, &totvert);
994                                 }
995                                 mti->deformVerts(md, &mectx_deform, NULL, vertCos, totvert);
996                         }
997                 }
998                 else {
999                         if (!r_final) {
1000                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh
1001                                  * is totally unnecessary, so we could stop modifiers applying
1002                                  * when we found constructive modifier but derived mesh is unwanted result
1003                                  */
1004                                 break;
1005                         }
1006
1007                         if (modified) {
1008                                 if (vertCos) {
1009                                         Mesh *temp_mesh;
1010                                         BKE_id_copy_ex(NULL, &modified->id, (ID **)&temp_mesh,
1011                                                        LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_USER_REFCOUNT |
1012                                                        LIB_ID_CREATE_NO_DEG_TAG | LIB_ID_COPY_NO_PREVIEW,
1013                                                        false);
1014                                         BKE_id_free(NULL, modified);
1015                                         modified = temp_mesh;
1016
1017                                         BKE_mesh_apply_vert_coords(modified, vertCos);
1018                                 }
1019                         }
1020                         else {
1021                                 if (vertCos) {
1022                                         displist_apply_allverts(dispbase, vertCos);
1023                                 }
1024
1025                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
1026                                         curve_to_filledpoly(cu, nurb, dispbase);
1027                                 }
1028
1029                                 modified = BKE_mesh_new_nomain_from_curve_displist(ob, dispbase);
1030                         }
1031
1032                         if (vertCos) {
1033                                 /* Vertex coordinates were applied to necessary data, could free it */
1034                                 MEM_freeN(vertCos);
1035                                 vertCos = NULL;
1036                         }
1037
1038                         if (mti->dependsOnNormals != NULL && mti->dependsOnNormals(md)) {
1039                                 BKE_mesh_ensure_normals(modified);
1040                         }
1041                         mesh_applied = mti->applyModifier(md, &mectx_apply, modified);
1042
1043                         if (mesh_applied) {
1044                                 /* Modifier returned a new derived mesh */
1045
1046                                 if (modified && modified != mesh_applied) /* Modifier  */
1047                                         BKE_id_free(NULL, modified);
1048                                 modified = mesh_applied;
1049                         }
1050                 }
1051         }
1052
1053         if (vertCos) {
1054                 if (modified) {
1055                         Mesh *temp_mesh;
1056                         BKE_id_copy_ex(NULL, &modified->id, (ID **)&temp_mesh,
1057                                        LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_USER_REFCOUNT |
1058                                        LIB_ID_CREATE_NO_DEG_TAG | LIB_ID_COPY_NO_PREVIEW,
1059                                        false);
1060                         BKE_id_free(NULL, modified);
1061                         modified = temp_mesh;
1062
1063                         BKE_mesh_apply_vert_coords(modified, vertCos);
1064                         BKE_mesh_calc_normals_mapping_simple(modified);
1065
1066                         MEM_freeN(vertCos);
1067                 }
1068                 else {
1069                         displist_apply_allverts(dispbase, vertCos);
1070                         MEM_freeN(vertCos);
1071                         vertCos = NULL;
1072                 }
1073         }
1074
1075         if (r_final) {
1076                 if (modified) {
1077                         /* see: mesh_calc_modifiers */
1078                         if (modified->totface == 0) {
1079                                 BKE_mesh_tessface_calc(modified);
1080                         }
1081                         /* Even if tessellation is not needed, some modifiers might have modified CD layers
1082                          * (like mloopcol or mloopuv), hence we have to update those. */
1083                         else if (modified->runtime.cd_dirty_vert & CD_MASK_TESSLOOPNORMAL) {
1084                                 BKE_mesh_tessface_calc(modified);
1085                         }
1086
1087                         /* XXX2.8(Sybren): make sure the face normals are recalculated as well */
1088                         BKE_mesh_ensure_normals(modified);
1089
1090                         /* Special tweaks, needed since neither BKE_mesh_new_nomain_from_template() nor
1091                          * BKE_mesh_new_nomain_from_curve_displist() properly duplicate mat info...
1092                          */
1093                         BLI_strncpy(modified->id.name, cu->id.name, sizeof(modified->id.name));
1094                         *((short *)modified->id.name) = ID_ME;
1095                         MEM_SAFE_FREE(modified->mat);
1096                         /* Set flag which makes it easier to see what's going on in a debugger. */
1097                         modified->id.tag |= LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT;
1098                         modified->mat = MEM_dupallocN(cu->mat);
1099                         modified->totcol = cu->totcol;
1100
1101                         (*r_final) = modified;
1102                 }
1103                 else {
1104                         (*r_final) = NULL;
1105                 }
1106         }
1107 }
1108
1109 static void displist_surf_indices(DispList *dl)
1110 {
1111         int a, b, p1, p2, p3, p4;
1112         int *index;
1113
1114         dl->totindex = 0;
1115
1116         index = dl->index = MEM_mallocN(4 * sizeof(int) * (dl->parts + 1) * (dl->nr + 1), "index array nurbs");
1117
1118         for (a = 0; a < dl->parts; a++) {
1119
1120                 if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
1121                         break;
1122
1123                 for (; b < dl->nr; b++, index += 4) {
1124                         index[0] = p1;
1125                         index[1] = p2;
1126                         index[2] = p4;
1127                         index[3] = p3;
1128
1129                         dl->totindex++;
1130
1131                         p2 = p1; p1++;
1132                         p4 = p3; p3++;
1133                 }
1134         }
1135 }
1136
1137 /* XXX2.8(Sybren): unused function; impossible to test after porting to Mesh */
1138 #ifdef WITH_DERIVEDMESH_DEPRECATED_FUNCS
1139 static DerivedMesh *create_orco_dm(Depsgraph *depsgraph, Scene *scene, Object *ob)
1140 {
1141         DerivedMesh *dm;
1142         ListBase disp = {NULL, NULL};
1143
1144         /* OrcoDM should be created from underformed disp lists */
1145         BKE_displist_make_curveTypes_forOrco(depsgraph, scene, ob, &disp);
1146         dm = CDDM_from_curve_displist(ob, &disp);
1147
1148         BKE_displist_free(&disp);
1149
1150         return dm;
1151 }
1152
1153 static void add_orco_dm(Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1154 {
1155         float (*orco)[3], (*layerorco)[3];
1156         int totvert, a;
1157         Curve *cu = ob->data;
1158
1159         totvert = dm->getNumVerts(dm);
1160
1161         orco = MEM_callocN(sizeof(float) * 3 * totvert, "dm orco");
1162
1163         if (orcodm->getNumVerts(orcodm) == totvert)
1164                 orcodm->getVertCos(orcodm, orco);
1165         else
1166                 dm->getVertCos(dm, orco);
1167
1168         for (a = 0; a < totvert; a++) {
1169                 float *co = orco[a];
1170                 co[0] = (co[0] - cu->loc[0]) / cu->size[0];
1171                 co[1] = (co[1] - cu->loc[1]) / cu->size[1];
1172                 co[2] = (co[2] - cu->loc[2]) / cu->size[2];
1173         }
1174
1175         if ((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1176                 memcpy(layerorco, orco, sizeof(float) * totvert);
1177                 MEM_freeN(orco);
1178         }
1179         else
1180                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1181 }
1182 #endif
1183
1184 /* XXX2.8(Sybren): unused function; impossible to test after porting to Mesh */
1185 #ifdef WITH_DERIVEDMESH_DEPRECATED_FUNCS
1186 static void curve_calc_orcodm(
1187         Depsgraph *depsgraph, Scene *scene, Object *ob, DerivedMesh *dm_final,
1188         const bool for_render, const bool use_render_resolution)
1189 {
1190         /* this function represents logic of mesh's orcodm calculation
1191          * for displist-based objects
1192          */
1193         VirtualModifierData virtualModifierData;
1194         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
1195         ModifierData *pretessellatePoint;
1196         Curve *cu = ob->data;
1197         int required_mode;
1198         const bool editmode = (!for_render && (cu->editnurb || cu->editfont));
1199         DerivedMesh *ndm, *orcodm = NULL;
1200         ModifierApplyFlag app_flag = MOD_APPLY_ORCO;
1201
1202         if (use_render_resolution) {
1203                 app_flag |= MOD_APPLY_RENDER;
1204                 required_mode = eModifierMode_Render;
1205         }
1206         else
1207                 required_mode = eModifierMode_Realtime;
1208
1209         const ModifierEvalContext mectx = {depsgraph, ob, app_flag};
1210
1211         pretessellatePoint = curve_get_tessellate_point(scene, ob, use_render_resolution, editmode);
1212
1213         if (editmode)
1214                 required_mode |= eModifierMode_Editmode;
1215
1216         if (pretessellatePoint) {
1217                 md = pretessellatePoint->next;
1218         }
1219
1220         /* If modifiers are disabled, we wouldn't be here because
1221          * this function is only called if there're enabled constructive
1222          * modifiers applied on the curve.
1223          *
1224          * This means we can create ORCO DM in advance and assume it's
1225          * never NULL.
1226          */
1227         orcodm = create_orco_dm(depsgraph, scene, ob);
1228
1229         for (; md; md = md->next) {
1230                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1231
1232                 md->scene = scene;
1233
1234                 if (!modifier_isEnabled(scene, md, required_mode))
1235                         continue;
1236                 if (mti->type != eModifierTypeType_Constructive)
1237                         continue;
1238
1239                 ndm = modwrap_applyModifier(md, &mectx, orcodm);
1240
1241                 if (ndm) {
1242                         /* if the modifier returned a new dm, release the old one */
1243                         if (orcodm && orcodm != ndm) {
1244                                 orcodm->release(orcodm);
1245                         }
1246                         orcodm = ndm;
1247                 }
1248         }
1249
1250         /* add an orco layer if needed */
1251         add_orco_dm(ob, dm_final, orcodm);
1252
1253         orcodm->release(orcodm);
1254 }
1255 #endif
1256
1257 void BKE_displist_make_surf(
1258         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase,
1259         Mesh **r_final,
1260         const bool for_render, const bool for_orco, const bool use_render_resolution)
1261 {
1262         ListBase nubase = {NULL, NULL};
1263         Nurb *nu;
1264         Curve *cu = ob->data;
1265         DispList *dl;
1266         float *data;
1267         int len;
1268
1269         if (!for_render && cu->editnurb) {
1270                 BKE_nurbList_duplicate(&nubase, BKE_curve_editNurbs_get(cu));
1271         }
1272         else {
1273                 BKE_nurbList_duplicate(&nubase, &cu->nurb);
1274         }
1275
1276         if (!for_orco)
1277                 curve_calc_modifiers_pre(depsgraph, scene, ob, &nubase, for_render, use_render_resolution);
1278
1279         for (nu = nubase.first; nu; nu = nu->next) {
1280                 if ((for_render || nu->hide == 0) && BKE_nurb_check_valid_uv(nu)) {
1281                         int resolu = nu->resolu, resolv = nu->resolv;
1282
1283                         if (use_render_resolution) {
1284                                 if (cu->resolu_ren)
1285                                         resolu = cu->resolu_ren;
1286                                 if (cu->resolv_ren)
1287                                         resolv = cu->resolv_ren;
1288                         }
1289
1290                         if (nu->pntsv == 1) {
1291                                 len = SEGMENTSU(nu) * resolu;
1292
1293                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1294                                 dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
1295
1296                                 BLI_addtail(dispbase, dl);
1297                                 dl->parts = 1;
1298                                 dl->nr = len;
1299                                 dl->col = nu->mat_nr;
1300                                 dl->charidx = nu->charidx;
1301
1302                                 /* dl->rt will be used as flag for render face and */
1303                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1304                                 dl->rt = nu->flag & ~CU_2D;
1305
1306                                 data = dl->verts;
1307                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
1308                                 else dl->type = DL_SEGM;
1309
1310                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
1311                         }
1312                         else {
1313                                 len = (nu->pntsu * resolu) * (nu->pntsv * resolv);
1314
1315                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1316                                 dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
1317                                 BLI_addtail(dispbase, dl);
1318
1319                                 dl->col = nu->mat_nr;
1320                                 dl->charidx = nu->charidx;
1321
1322                                 /* dl->rt will be used as flag for render face and */
1323                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1324                                 dl->rt = nu->flag & ~CU_2D;
1325
1326                                 data = dl->verts;
1327                                 dl->type = DL_SURF;
1328
1329                                 dl->parts = (nu->pntsu * resolu);  /* in reverse, because makeNurbfaces works that way */
1330                                 dl->nr = (nu->pntsv * resolv);
1331                                 if (nu->flagv & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_U;  /* reverse too! */
1332                                 if (nu->flagu & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_V;
1333
1334                                 BKE_nurb_makeFaces(nu, data, 0, resolu, resolv);
1335
1336                                 /* gl array drawing: using indices */
1337                                 displist_surf_indices(dl);
1338                         }
1339                 }
1340         }
1341
1342         if (!for_orco) {
1343                 BKE_nurbList_duplicate(&ob->runtime.curve_cache->deformed_nurbs, &nubase);
1344                 curve_calc_modifiers_post(depsgraph, scene, ob, &nubase, dispbase, r_final,
1345                                           for_render, use_render_resolution);
1346         }
1347
1348         BKE_nurbList_free(&nubase);
1349 }
1350
1351 static void rotateBevelPiece(Curve *cu, BevPoint *bevp, BevPoint *nbevp, DispList *dlb, float bev_blend, float widfac, float fac, float **r_data)
1352 {
1353         float *fp, *data = *r_data;
1354         int b;
1355
1356         fp = dlb->verts;
1357         for (b = 0; b < dlb->nr; b++, fp += 3, data += 3) {
1358                 if (cu->flag & CU_3D) {
1359                         float vec[3], quat[4];
1360
1361                         vec[0] = fp[1] + widfac;
1362                         vec[1] = fp[2];
1363                         vec[2] = 0.0;
1364
1365                         if (nbevp == NULL) {
1366                                 copy_v3_v3(data, bevp->vec);
1367                                 copy_qt_qt(quat, bevp->quat);
1368                         }
1369                         else {
1370                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1371                                 interp_qt_qtqt(quat, bevp->quat, nbevp->quat, bev_blend);
1372                         }
1373
1374                         mul_qt_v3(quat, vec);
1375
1376                         data[0] += fac * vec[0];
1377                         data[1] += fac * vec[1];
1378                         data[2] += fac * vec[2];
1379                 }
1380                 else {
1381                         float sina, cosa;
1382
1383                         if (nbevp == NULL) {
1384                                 copy_v3_v3(data, bevp->vec);
1385                                 sina = bevp->sina;
1386                                 cosa = bevp->cosa;
1387                         }
1388                         else {
1389                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1390
1391                                 /* perhaps we need to interpolate angles instead. but the thing is
1392                                  * cosa and sina are not actually sine and cosine
1393                                  */
1394                                 sina = nbevp->sina * bev_blend + bevp->sina * (1.0f - bev_blend);
1395                                 cosa = nbevp->cosa * bev_blend + bevp->cosa * (1.0f - bev_blend);
1396                         }
1397
1398                         data[0] += fac * (widfac + fp[1]) * sina;
1399                         data[1] += fac * (widfac + fp[1]) * cosa;
1400                         data[2] += fac * fp[2];
1401                 }
1402         }
1403
1404         *r_data = data;
1405 }
1406
1407 static void fillBevelCap(Nurb *nu, DispList *dlb, float *prev_fp, ListBase *dispbase)
1408 {
1409         DispList *dl;
1410
1411         dl = MEM_callocN(sizeof(DispList), "makeDispListbev2");
1412         dl->verts = MEM_mallocN(sizeof(float[3]) * dlb->nr, "dlverts");
1413         memcpy(dl->verts, prev_fp, 3 * sizeof(float) * dlb->nr);
1414
1415         dl->type = DL_POLY;
1416
1417         dl->parts = 1;
1418         dl->nr = dlb->nr;
1419         dl->col = nu->mat_nr;
1420         dl->charidx = nu->charidx;
1421
1422         /* dl->rt will be used as flag for render face and */
1423         /* CU_2D conflicts with R_NOPUNOFLIP */
1424         dl->rt = nu->flag & ~CU_2D;
1425
1426         BLI_addtail(dispbase, dl);
1427 }
1428
1429 static void calc_bevfac_segment_mapping(BevList *bl, float bevfac, float spline_length, int *r_bev, float *r_blend)
1430 {
1431         float normlen, normsum = 0.0f;
1432         float *seglen = bl->seglen;
1433         int *segbevcount = bl->segbevcount;
1434         int bevcount = 0, nr = bl->nr;
1435
1436         float bev_fl = bevfac * (bl->nr - 1);
1437         *r_bev = (int)bev_fl;
1438
1439         while (bevcount < nr - 1) {
1440                 normlen = *seglen / spline_length;
1441                 if (normsum + normlen > bevfac) {
1442                         bev_fl = bevcount + (bevfac - normsum) / normlen * *segbevcount;
1443                         *r_bev = (int) bev_fl;
1444                         *r_blend = bev_fl - *r_bev;
1445                         break;
1446                 }
1447                 normsum += normlen;
1448                 bevcount += *segbevcount;
1449                 segbevcount++;
1450                 seglen++;
1451         }
1452 }
1453
1454 static void calc_bevfac_spline_mapping(BevList *bl, float bevfac,
1455                                        float spline_length,
1456                                        int *r_bev, float *r_blend)
1457 {
1458         const float len_target = bevfac * spline_length;
1459         BevPoint *bevp = bl->bevpoints;
1460         float len_next = 0.0f, len = 0.0f;
1461         int i = 0, nr = bl->nr;
1462
1463         while (nr--) {
1464                 bevp++;
1465                 len_next = len + bevp->offset;
1466                 if (len_next > len_target) {
1467                         break;
1468                 }
1469                 len = len_next;
1470                 i++;
1471         }
1472
1473         *r_bev = i;
1474         *r_blend = (len_target - len) / bevp->offset;
1475 }
1476
1477 static void calc_bevfac_mapping_default(BevList *bl,
1478                                         int *r_start, float *r_firstblend,
1479                                         int *r_steps, float *r_lastblend)
1480 {
1481         *r_start = 0;
1482         *r_steps = bl->nr;
1483         *r_firstblend = 1.0f;
1484         *r_lastblend = 1.0f;
1485 }
1486
1487 static void calc_bevfac_mapping(Curve *cu, BevList *bl, Nurb *nu,
1488         int *r_start, float *r_firstblend, int *r_steps, float *r_lastblend)
1489 {
1490         float tmpf, total_length = 0.0f;
1491         int end = 0, i;
1492
1493         if ((BKE_nurb_check_valid_u(nu) == false) ||
1494             /* not essential, but skips unnecessary calculation */
1495             (min_ff(cu->bevfac1, cu->bevfac2) == 0.0f &&
1496              max_ff(cu->bevfac1, cu->bevfac2) == 1.0f))
1497         {
1498                 calc_bevfac_mapping_default(bl, r_start, r_firstblend, r_steps, r_lastblend);
1499                 return;
1500         }
1501
1502         if (ELEM(cu->bevfac1_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE) ||
1503             ELEM(cu->bevfac2_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE))
1504         {
1505                 for (i = 0; i < SEGMENTSU(nu); i++) {
1506                         total_length += bl->seglen[i];
1507                 }
1508         }
1509
1510         switch (cu->bevfac1_mapping) {
1511                 case CU_BEVFAC_MAP_RESOLU:
1512                 {
1513                         const float start_fl = cu->bevfac1 * (bl->nr - 1);
1514                         *r_start = (int)start_fl;
1515                         *r_firstblend = 1.0f - (start_fl - (*r_start));
1516                         break;
1517                 }
1518                 case CU_BEVFAC_MAP_SEGMENT:
1519                 {
1520                         calc_bevfac_segment_mapping(bl, cu->bevfac1, total_length, r_start, r_firstblend);
1521                         *r_firstblend = 1.0f - *r_firstblend;
1522                         break;
1523                 }
1524                 case CU_BEVFAC_MAP_SPLINE:
1525                 {
1526                         calc_bevfac_spline_mapping(bl, cu->bevfac1, total_length, r_start, r_firstblend);
1527                         *r_firstblend = 1.0f - *r_firstblend;
1528                         break;
1529                 }
1530         }
1531
1532         switch (cu->bevfac2_mapping) {
1533                 case CU_BEVFAC_MAP_RESOLU:
1534                 {
1535                         const float end_fl = cu->bevfac2 * (bl->nr - 1);
1536                         end = (int)end_fl;
1537
1538                         *r_steps = 2 + end - *r_start;
1539                         *r_lastblend = end_fl - end;
1540                         break;
1541                 }
1542                 case CU_BEVFAC_MAP_SEGMENT:
1543                 {
1544                         calc_bevfac_segment_mapping(bl, cu->bevfac2, total_length, &end, r_lastblend);
1545                         *r_steps = end - *r_start + 2;
1546                         break;
1547                 }
1548                 case CU_BEVFAC_MAP_SPLINE:
1549                 {
1550                         calc_bevfac_spline_mapping(bl, cu->bevfac2, total_length, &end, r_lastblend);
1551                         *r_steps = end - *r_start + 2;
1552                         break;
1553                 }
1554         }
1555
1556         if (end < *r_start || (end == *r_start && *r_lastblend < 1.0f - *r_firstblend )) {
1557                 SWAP(int, *r_start, end);
1558                 tmpf = *r_lastblend;
1559                 *r_lastblend = 1.0f - *r_firstblend;
1560                 *r_firstblend = 1.0f - tmpf;
1561                 *r_steps = end - *r_start + 2;
1562         }
1563
1564         if (*r_start + *r_steps > bl->nr) {
1565                 *r_steps = bl->nr - *r_start;
1566                 *r_lastblend = 1.0f;
1567         }
1568 }
1569
1570 static void do_makeDispListCurveTypes(
1571         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase,
1572         Mesh **r_final,
1573         const bool for_render, const bool for_orco, const bool use_render_resolution)
1574 {
1575         Curve *cu = ob->data;
1576
1577         /* we do allow duplis... this is only displist on curve level */
1578         if (!ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1579
1580         if (ob->type == OB_SURF) {
1581                 BKE_displist_make_surf(depsgraph, scene, ob, dispbase, r_final, for_render, for_orco, use_render_resolution);
1582         }
1583         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1584                 ListBase dlbev;
1585                 ListBase nubase = {NULL, NULL};
1586
1587                 BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
1588
1589                 /* We only re-evaluate path if evaluation is not happening for orco.
1590                  * If the calculation happens for orco, we should never free data which
1591                  * was needed before and only not needed for orco calculation.
1592                  */
1593                 if (!for_orco) {
1594                         if (ob->runtime.curve_cache->path) free_path(ob->runtime.curve_cache->path);
1595                         ob->runtime.curve_cache->path = NULL;
1596                 }
1597
1598                 if (ob->type == OB_FONT) {
1599                         BKE_vfont_to_curve_nubase(ob, FO_EDIT, &nubase);
1600                 }
1601                 else {
1602                         BKE_nurbList_duplicate(&nubase, BKE_curve_nurbs_get(cu));
1603                 }
1604
1605                 if (!for_orco)
1606                         curve_calc_modifiers_pre(depsgraph, scene, ob, &nubase, for_render, use_render_resolution);
1607
1608                 BKE_curve_bevelList_make(ob, &nubase, use_render_resolution);
1609
1610                 /* If curve has no bevel will return nothing */
1611                 BKE_curve_bevel_make(depsgraph, scene, ob, &dlbev, for_render, use_render_resolution);
1612
1613                 /* no bevel or extrude, and no width correction? */
1614                 if (!dlbev.first && cu->width == 1.0f) {
1615                         curve_to_displist(cu, &nubase, dispbase, for_render, use_render_resolution);
1616                 }
1617                 else {
1618                         float widfac = cu->width - 1.0f;
1619                         BevList *bl = ob->runtime.curve_cache->bev.first;
1620                         Nurb *nu = nubase.first;
1621
1622                         for (; bl && nu; bl = bl->next, nu = nu->next) {
1623                                 DispList *dl;
1624                                 float *data;
1625                                 int a;
1626
1627                                 if (bl->nr) { /* blank bevel lists can happen */
1628
1629                                         /* exception handling; curve without bevel or extrude, with width correction */
1630                                         if (BLI_listbase_is_empty(&dlbev)) {
1631                                                 BevPoint *bevp;
1632                                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbev");
1633                                                 dl->verts = MEM_mallocN(sizeof(float[3]) * bl->nr, "dlverts");
1634                                                 BLI_addtail(dispbase, dl);
1635
1636                                                 if (bl->poly != -1) dl->type = DL_POLY;
1637                                                 else dl->type = DL_SEGM;
1638
1639                                                 if (dl->type == DL_SEGM) dl->flag = (DL_FRONT_CURVE | DL_BACK_CURVE);
1640
1641                                                 dl->parts = 1;
1642                                                 dl->nr = bl->nr;
1643                                                 dl->col = nu->mat_nr;
1644                                                 dl->charidx = nu->charidx;
1645
1646                                                 /* dl->rt will be used as flag for render face and */
1647                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1648                                                 dl->rt = nu->flag & ~CU_2D;
1649
1650                                                 a = dl->nr;
1651                                                 bevp = bl->bevpoints;
1652                                                 data = dl->verts;
1653                                                 while (a--) {
1654                                                         data[0] = bevp->vec[0] + widfac * bevp->sina;
1655                                                         data[1] = bevp->vec[1] + widfac * bevp->cosa;
1656                                                         data[2] = bevp->vec[2];
1657                                                         bevp++;
1658                                                         data += 3;
1659                                                 }
1660                                         }
1661                                         else {
1662                                                 DispList *dlb;
1663                                                 ListBase bottom_capbase = {NULL, NULL};
1664                                                 ListBase top_capbase = {NULL, NULL};
1665                                                 float bottom_no[3] = {0.0f};
1666                                                 float top_no[3] = {0.0f};
1667                                                 float firstblend = 0.0f, lastblend = 0.0f;
1668                                                 int i, start, steps = 0;
1669
1670                                                 if (nu->flagu & CU_NURB_CYCLIC) {
1671                                                         calc_bevfac_mapping_default(bl,
1672                                                                                     &start, &firstblend, &steps, &lastblend);
1673                                                 }
1674                                                 else {
1675                                                         if (fabsf(cu->bevfac2 - cu->bevfac1) < FLT_EPSILON) {
1676                                                                 continue;
1677                                                         }
1678
1679                                                         calc_bevfac_mapping(cu, bl, nu, &start, &firstblend, &steps, &lastblend);
1680                                                 }
1681
1682                                                 for (dlb = dlbev.first; dlb; dlb = dlb->next) {
1683                                                         BevPoint *bevp_first, *bevp_last;
1684                                                         BevPoint *bevp;
1685
1686                                                         /* for each part of the bevel use a separate displblock */
1687                                                         dl = MEM_callocN(sizeof(DispList), "makeDispListbev1");
1688                                                         dl->verts = data = MEM_mallocN(sizeof(float[3]) * dlb->nr * steps, "dlverts");
1689                                                         BLI_addtail(dispbase, dl);
1690
1691                                                         dl->type = DL_SURF;
1692
1693                                                         dl->flag = dlb->flag & (DL_FRONT_CURVE | DL_BACK_CURVE);
1694                                                         if (dlb->type == DL_POLY) {
1695                                                                 dl->flag |= DL_CYCL_U;
1696                                                         }
1697                                                         if ((bl->poly >= 0) && (steps > 2)) {
1698                                                                 dl->flag |= DL_CYCL_V;
1699                                                         }
1700
1701                                                         dl->parts = steps;
1702                                                         dl->nr = dlb->nr;
1703                                                         dl->col = nu->mat_nr;
1704                                                         dl->charidx = nu->charidx;
1705
1706                                                         /* dl->rt will be used as flag for render face and */
1707                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1708                                                         dl->rt = nu->flag & ~CU_2D;
1709
1710                                                         dl->bevel_split = BLI_BITMAP_NEW(steps, "bevel_split");
1711
1712                                                         /* for each point of poly make a bevel piece */
1713                                                         bevp_first =  bl->bevpoints;
1714                                                         bevp_last  = &bl->bevpoints[bl->nr - 1];
1715                                                         bevp       = &bl->bevpoints[start];
1716                                                         for (i = start, a = 0; a < steps; i++, bevp++, a++) {
1717                                                                 float fac = 1.0;
1718                                                                 float *cur_data = data;
1719
1720                                                                 if (cu->taperobj == NULL) {
1721                                                                         fac = bevp->radius;
1722                                                                 }
1723                                                                 else {
1724                                                                         float len, taper_fac;
1725
1726                                                                         if (cu->flag & CU_MAP_TAPER) {
1727                                                                                 len = (steps - 3) + firstblend + lastblend;
1728
1729                                                                                 if (a == 0)
1730                                                                                         taper_fac = 0.0f;
1731                                                                                 else if (a == steps - 1)
1732                                                                                         taper_fac = 1.0f;
1733                                                                                 else
1734                                                                                         taper_fac = ((float) a - (1.0f - firstblend)) / len;
1735                                                                         }
1736                                                                         else {
1737                                                                                 len = bl->nr - 1;
1738                                                                                 taper_fac = (float) i / len;
1739
1740                                                                                 if (a == 0)
1741                                                                                         taper_fac += (1.0f - firstblend) / len;
1742                                                                                 else if (a == steps - 1)
1743                                                                                         taper_fac -= (1.0f - lastblend) / len;
1744                                                                         }
1745
1746                                                                         fac = displist_calc_taper(depsgraph, scene, cu->taperobj, taper_fac);
1747                                                                 }
1748
1749                                                                 if (bevp->split_tag) {
1750                                                                         BLI_BITMAP_ENABLE(dl->bevel_split, a);
1751                                                                 }
1752
1753                                                                 /* rotate bevel piece and write in data */
1754                                                                 if ((a == 0) && (bevp != bevp_last)) {
1755                                                                         rotateBevelPiece(cu, bevp, bevp + 1, dlb, 1.0f - firstblend, widfac, fac, &data);
1756                                                                 }
1757                                                                 else if ((a == steps - 1) && (bevp != bevp_first) ) {
1758                                                                         rotateBevelPiece(cu, bevp, bevp - 1, dlb, 1.0f - lastblend, widfac, fac, &data);
1759                                                                 }
1760                                                                 else {
1761                                                                         rotateBevelPiece(cu, bevp, NULL, dlb, 0.0f, widfac, fac, &data);
1762                                                                 }
1763
1764                                                                 if (cu->bevobj && (cu->flag & CU_FILL_CAPS) && !(nu->flagu & CU_NURB_CYCLIC)) {
1765                                                                         if (a == 1) {
1766                                                                                 fillBevelCap(nu, dlb, cur_data - 3 * dlb->nr, &bottom_capbase);
1767                                                                                 negate_v3_v3(bottom_no, bevp->dir);
1768                                                                         }
1769                                                                         if (a == steps - 1) {
1770                                                                                 fillBevelCap(nu, dlb, cur_data, &top_capbase);
1771                                                                                 copy_v3_v3(top_no, bevp->dir);
1772                                                                         }
1773                                                                 }
1774                                                         }
1775
1776                                                         /* gl array drawing: using indices */
1777                                                         displist_surf_indices(dl);
1778                                                 }
1779
1780                                                 if (bottom_capbase.first) {
1781                                                         BKE_displist_fill(&bottom_capbase, dispbase, bottom_no, false);
1782                                                         BKE_displist_fill(&top_capbase, dispbase, top_no, false);
1783                                                         BKE_displist_free(&bottom_capbase);
1784                                                         BKE_displist_free(&top_capbase);
1785                                                 }
1786                                         }
1787                                 }
1788
1789                         }
1790                         BKE_displist_free(&dlbev);
1791                 }
1792
1793                 if (!(cu->flag & CU_DEFORM_FILL)) {
1794                         curve_to_filledpoly(cu, &nubase, dispbase);
1795                 }
1796
1797                 if (!for_orco) {
1798                         if ((cu->flag & CU_PATH) ||
1799                             DEG_get_eval_flags_for_id(depsgraph, &ob->id) & DAG_EVAL_NEED_CURVE_PATH)
1800                         {
1801                                 calc_curvepath(ob, &nubase);
1802                         }
1803                 }
1804
1805                 if (!for_orco) {
1806                         BKE_nurbList_duplicate(&ob->runtime.curve_cache->deformed_nurbs, &nubase);
1807                         curve_calc_modifiers_post(depsgraph, scene, ob, &nubase, dispbase, r_final, for_render, use_render_resolution);
1808                 }
1809
1810                 if (cu->flag & CU_DEFORM_FILL && !ob->runtime.mesh_eval) {
1811                         curve_to_filledpoly(cu, &nubase, dispbase);
1812                 }
1813
1814                 BKE_nurbList_free(&nubase);
1815         }
1816 }
1817
1818 void BKE_displist_make_curveTypes(
1819         Depsgraph *depsgraph, Scene *scene, Object *ob, const bool for_render, const bool for_orco)
1820 {
1821         ListBase *dispbase;
1822
1823         /* The same check for duplis as in do_makeDispListCurveTypes.
1824          * Happens when curve used for constraint/bevel was converted to mesh.
1825          * check there is still needed for render displist and orco displists. */
1826         if (!ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT))
1827                 return;
1828
1829         BKE_object_free_derived_caches(ob);
1830
1831         if (!ob->runtime.curve_cache) {
1832                 ob->runtime.curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for curve types");
1833         }
1834
1835         dispbase = &(ob->runtime.curve_cache->disp);
1836
1837         do_makeDispListCurveTypes(depsgraph, scene, ob, dispbase, &ob->runtime.mesh_eval, for_render, for_orco, false);
1838
1839         boundbox_displist_object(ob);
1840 }
1841
1842 void BKE_displist_make_curveTypes_forRender(
1843         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase,
1844         Mesh **r_final, const bool for_orco,
1845         const bool use_render_resolution)
1846 {
1847         if (ob->runtime.curve_cache == NULL) {
1848                 ob->runtime.curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for Curve");
1849         }
1850
1851         do_makeDispListCurveTypes(depsgraph, scene, ob, dispbase, r_final, true, for_orco, use_render_resolution);
1852 }
1853
1854 void BKE_displist_make_curveTypes_forOrco(
1855         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase)
1856 {
1857         if (ob->runtime.curve_cache == NULL) {
1858                 ob->runtime.curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for Curve");
1859         }
1860
1861         do_makeDispListCurveTypes(depsgraph, scene, ob, dispbase, NULL, 1, 1, 1);
1862 }
1863
1864 void BKE_displist_minmax(ListBase *dispbase, float min[3], float max[3])
1865 {
1866         DispList *dl;
1867         const float *vert;
1868         int a, tot = 0;
1869         int doit = 0;
1870
1871         for (dl = dispbase->first; dl; dl = dl->next) {
1872                 tot = (dl->type == DL_INDEX3) ? dl->nr : dl->nr * dl->parts;
1873                 vert = dl->verts;
1874                 for (a = 0; a < tot; a++, vert += 3) {
1875                         minmax_v3v3_v3(min, max, vert);
1876                 }
1877                 doit |= (tot != 0);
1878         }
1879
1880         if (!doit) {
1881                 /* there's no geometry in displist, use zero-sized boundbox */
1882                 zero_v3(min);
1883                 zero_v3(max);
1884         }
1885 }
1886
1887 /* this is confusing, there's also min_max_object, appplying the obmat... */
1888 static void boundbox_displist_object(Object *ob)
1889 {
1890         if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1891                 /* Curve's BB is already calculated as a part of modifier stack,
1892                  * here we only calculate object BB based on final display list.
1893                  */
1894
1895                 /* object's BB is calculated from final displist */
1896                 if (ob->bb == NULL)
1897                         ob->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1898
1899                 if (ob->runtime.mesh_eval) {
1900                         BKE_object_boundbox_calc_from_mesh(ob, ob->runtime.mesh_eval);
1901                 }
1902                 else {
1903                         float min[3], max[3];
1904
1905                         INIT_MINMAX(min, max);
1906                         BKE_displist_minmax(&ob->runtime.curve_cache->disp, min, max);
1907                         BKE_boundbox_init_from_minmax(ob->bb, min, max);
1908
1909                         ob->bb->flag &= ~BOUNDBOX_DIRTY;
1910                 }
1911         }
1912 }