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