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