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