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