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