Merge branch 'blender2.7'
[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                         /* Special tweaks, needed since neither BKE_mesh_new_nomain_from_template() nor
1093                          * BKE_mesh_new_nomain_from_curve_displist() properly duplicate mat info...
1094                          */
1095                         BLI_strncpy(modified->id.name, cu->id.name, sizeof(modified->id.name));
1096                         *((short *)modified->id.name) = ID_ME;
1097                         MEM_SAFE_FREE(modified->mat);
1098                         /* Set flag which makes it easier to see what's going on in a debugger. */
1099                         modified->id.tag |= LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT;
1100                         modified->mat = MEM_dupallocN(cu->mat);
1101                         modified->totcol = cu->totcol;
1102
1103                         (*r_final) = modified;
1104                 }
1105                 else {
1106                         (*r_final) = NULL;
1107                 }
1108         }
1109 }
1110
1111 static void displist_surf_indices(DispList *dl)
1112 {
1113         int a, b, p1, p2, p3, p4;
1114         int *index;
1115
1116         dl->totindex = 0;
1117
1118         index = dl->index = MEM_mallocN(4 * sizeof(int) * (dl->parts + 1) * (dl->nr + 1), "index array nurbs");
1119
1120         for (a = 0; a < dl->parts; a++) {
1121
1122                 if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
1123                         break;
1124
1125                 for (; b < dl->nr; b++, index += 4) {
1126                         index[0] = p1;
1127                         index[1] = p2;
1128                         index[2] = p4;
1129                         index[3] = p3;
1130
1131                         dl->totindex++;
1132
1133                         p2 = p1; p1++;
1134                         p4 = p3; p3++;
1135                 }
1136         }
1137 }
1138
1139 /* XXX2.8(Sybren): unused function; impossible to test after porting to Mesh */
1140 #ifdef WITH_DERIVEDMESH_DEPRECATED_FUNCS
1141 static DerivedMesh *create_orco_dm(Depsgraph *depsgraph, Scene *scene, Object *ob)
1142 {
1143         DerivedMesh *dm;
1144         ListBase disp = {NULL, NULL};
1145
1146         /* OrcoDM should be created from underformed disp lists */
1147         BKE_displist_make_curveTypes_forOrco(depsgraph, scene, ob, &disp);
1148         dm = CDDM_from_curve_displist(ob, &disp);
1149
1150         BKE_displist_free(&disp);
1151
1152         return dm;
1153 }
1154
1155 static void add_orco_dm(Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1156 {
1157         float (*orco)[3], (*layerorco)[3];
1158         int totvert, a;
1159         Curve *cu = ob->data;
1160
1161         totvert = dm->getNumVerts(dm);
1162
1163         orco = MEM_callocN(sizeof(float) * 3 * totvert, "dm orco");
1164
1165         if (orcodm->getNumVerts(orcodm) == totvert)
1166                 orcodm->getVertCos(orcodm, orco);
1167         else
1168                 dm->getVertCos(dm, orco);
1169
1170         for (a = 0; a < totvert; a++) {
1171                 float *co = orco[a];
1172                 co[0] = (co[0] - cu->loc[0]) / cu->size[0];
1173                 co[1] = (co[1] - cu->loc[1]) / cu->size[1];
1174                 co[2] = (co[2] - cu->loc[2]) / cu->size[2];
1175         }
1176
1177         if ((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1178                 memcpy(layerorco, orco, sizeof(float) * totvert);
1179                 MEM_freeN(orco);
1180         }
1181         else
1182                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1183 }
1184 #endif
1185
1186 /* XXX2.8(Sybren): unused function; impossible to test after porting to Mesh */
1187 #ifdef WITH_DERIVEDMESH_DEPRECATED_FUNCS
1188 static void curve_calc_orcodm(
1189         Depsgraph *depsgraph, Scene *scene, Object *ob, DerivedMesh *dm_final,
1190         const bool for_render, const bool use_render_resolution)
1191 {
1192         /* this function represents logic of mesh's orcodm calculation
1193          * for displist-based objects
1194          */
1195         VirtualModifierData virtualModifierData;
1196         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
1197         ModifierData *pretessellatePoint;
1198         Curve *cu = ob->data;
1199         int required_mode;
1200         const bool editmode = (!for_render && (cu->editnurb || cu->editfont));
1201         DerivedMesh *ndm, *orcodm = NULL;
1202         ModifierApplyFlag app_flag = MOD_APPLY_ORCO;
1203
1204         if (use_render_resolution) {
1205                 app_flag |= MOD_APPLY_RENDER;
1206                 required_mode = eModifierMode_Render;
1207         }
1208         else
1209                 required_mode = eModifierMode_Realtime;
1210
1211         const ModifierEvalContext mectx = {depsgraph, ob, app_flag};
1212
1213         pretessellatePoint = curve_get_tessellate_point(scene, ob, use_render_resolution, editmode);
1214
1215         if (editmode)
1216                 required_mode |= eModifierMode_Editmode;
1217
1218         if (pretessellatePoint) {
1219                 md = pretessellatePoint->next;
1220         }
1221
1222         /* If modifiers are disabled, we wouldn't be here because
1223          * this function is only called if there're enabled constructive
1224          * modifiers applied on the curve.
1225          *
1226          * This means we can create ORCO DM in advance and assume it's
1227          * never NULL.
1228          */
1229         orcodm = create_orco_dm(depsgraph, scene, ob);
1230
1231         for (; md; md = md->next) {
1232                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1233
1234                 md->scene = scene;
1235
1236                 if (!modifier_isEnabled(scene, md, required_mode))
1237                         continue;
1238                 if (mti->type != eModifierTypeType_Constructive)
1239                         continue;
1240
1241                 ndm = modwrap_applyModifier(md, &mectx, orcodm);
1242
1243                 if (ndm) {
1244                         /* if the modifier returned a new dm, release the old one */
1245                         if (orcodm && orcodm != ndm) {
1246                                 orcodm->release(orcodm);
1247                         }
1248                         orcodm = ndm;
1249                 }
1250         }
1251
1252         /* add an orco layer if needed */
1253         add_orco_dm(ob, dm_final, orcodm);
1254
1255         orcodm->release(orcodm);
1256 }
1257 #endif
1258
1259 void BKE_displist_make_surf(
1260         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase,
1261         Mesh **r_final,
1262         const bool for_render, const bool for_orco, const bool use_render_resolution)
1263 {
1264         ListBase nubase = {NULL, NULL};
1265         Nurb *nu;
1266         Curve *cu = ob->data;
1267         DispList *dl;
1268         float *data;
1269         int len;
1270
1271         if (!for_render && cu->editnurb) {
1272                 BKE_nurbList_duplicate(&nubase, BKE_curve_editNurbs_get(cu));
1273         }
1274         else {
1275                 BKE_nurbList_duplicate(&nubase, &cu->nurb);
1276         }
1277
1278         if (!for_orco)
1279                 curve_calc_modifiers_pre(depsgraph, scene, ob, &nubase, for_render, use_render_resolution);
1280
1281         for (nu = nubase.first; nu; nu = nu->next) {
1282                 if ((for_render || nu->hide == 0) && BKE_nurb_check_valid_uv(nu)) {
1283                         int resolu = nu->resolu, resolv = nu->resolv;
1284
1285                         if (use_render_resolution) {
1286                                 if (cu->resolu_ren)
1287                                         resolu = cu->resolu_ren;
1288                                 if (cu->resolv_ren)
1289                                         resolv = cu->resolv_ren;
1290                         }
1291
1292                         if (nu->pntsv == 1) {
1293                                 len = SEGMENTSU(nu) * resolu;
1294
1295                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1296                                 dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
1297
1298                                 BLI_addtail(dispbase, dl);
1299                                 dl->parts = 1;
1300                                 dl->nr = len;
1301                                 dl->col = nu->mat_nr;
1302                                 dl->charidx = nu->charidx;
1303
1304                                 /* dl->rt will be used as flag for render face and */
1305                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1306                                 dl->rt = nu->flag & ~CU_2D;
1307
1308                                 data = dl->verts;
1309                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
1310                                 else dl->type = DL_SEGM;
1311
1312                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
1313                         }
1314                         else {
1315                                 len = (nu->pntsu * resolu) * (nu->pntsv * resolv);
1316
1317                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1318                                 dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
1319                                 BLI_addtail(dispbase, dl);
1320
1321                                 dl->col = nu->mat_nr;
1322                                 dl->charidx = nu->charidx;
1323
1324                                 /* dl->rt will be used as flag for render face and */
1325                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1326                                 dl->rt = nu->flag & ~CU_2D;
1327
1328                                 data = dl->verts;
1329                                 dl->type = DL_SURF;
1330
1331                                 dl->parts = (nu->pntsu * resolu);  /* in reverse, because makeNurbfaces works that way */
1332                                 dl->nr = (nu->pntsv * resolv);
1333                                 if (nu->flagv & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_U;  /* reverse too! */
1334                                 if (nu->flagu & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_V;
1335
1336                                 BKE_nurb_makeFaces(nu, data, 0, resolu, resolv);
1337
1338                                 /* gl array drawing: using indices */
1339                                 displist_surf_indices(dl);
1340                         }
1341                 }
1342         }
1343
1344         if (!for_orco) {
1345                 BKE_nurbList_duplicate(&ob->runtime.curve_cache->deformed_nurbs, &nubase);
1346                 curve_calc_modifiers_post(depsgraph, scene, ob, &nubase, dispbase, r_final,
1347                                           for_render, use_render_resolution);
1348         }
1349
1350         BKE_nurbList_free(&nubase);
1351 }
1352
1353 static void rotateBevelPiece(Curve *cu, BevPoint *bevp, BevPoint *nbevp, DispList *dlb, float bev_blend, float widfac, float fac, float **r_data)
1354 {
1355         float *fp, *data = *r_data;
1356         int b;
1357
1358         fp = dlb->verts;
1359         for (b = 0; b < dlb->nr; b++, fp += 3, data += 3) {
1360                 if (cu->flag & CU_3D) {
1361                         float vec[3], quat[4];
1362
1363                         vec[0] = fp[1] + widfac;
1364                         vec[1] = fp[2];
1365                         vec[2] = 0.0;
1366
1367                         if (nbevp == NULL) {
1368                                 copy_v3_v3(data, bevp->vec);
1369                                 copy_qt_qt(quat, bevp->quat);
1370                         }
1371                         else {
1372                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1373                                 interp_qt_qtqt(quat, bevp->quat, nbevp->quat, bev_blend);
1374                         }
1375
1376                         mul_qt_v3(quat, vec);
1377
1378                         data[0] += fac * vec[0];
1379                         data[1] += fac * vec[1];
1380                         data[2] += fac * vec[2];
1381                 }
1382                 else {
1383                         float sina, cosa;
1384
1385                         if (nbevp == NULL) {
1386                                 copy_v3_v3(data, bevp->vec);
1387                                 sina = bevp->sina;
1388                                 cosa = bevp->cosa;
1389                         }
1390                         else {
1391                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1392
1393                                 /* perhaps we need to interpolate angles instead. but the thing is
1394                                  * cosa and sina are not actually sine and cosine
1395                                  */
1396                                 sina = nbevp->sina * bev_blend + bevp->sina * (1.0f - bev_blend);
1397                                 cosa = nbevp->cosa * bev_blend + bevp->cosa * (1.0f - bev_blend);
1398                         }
1399
1400                         data[0] += fac * (widfac + fp[1]) * sina;
1401                         data[1] += fac * (widfac + fp[1]) * cosa;
1402                         data[2] += fac * fp[2];
1403                 }
1404         }
1405
1406         *r_data = data;
1407 }
1408
1409 static void fillBevelCap(Nurb *nu, DispList *dlb, float *prev_fp, ListBase *dispbase)
1410 {
1411         DispList *dl;
1412
1413         dl = MEM_callocN(sizeof(DispList), "makeDispListbev2");
1414         dl->verts = MEM_mallocN(sizeof(float[3]) * dlb->nr, "dlverts");
1415         memcpy(dl->verts, prev_fp, 3 * sizeof(float) * dlb->nr);
1416
1417         dl->type = DL_POLY;
1418
1419         dl->parts = 1;
1420         dl->nr = dlb->nr;
1421         dl->col = nu->mat_nr;
1422         dl->charidx = nu->charidx;
1423
1424         /* dl->rt will be used as flag for render face and */
1425         /* CU_2D conflicts with R_NOPUNOFLIP */
1426         dl->rt = nu->flag & ~CU_2D;
1427
1428         BLI_addtail(dispbase, dl);
1429 }
1430
1431 static void calc_bevfac_segment_mapping(BevList *bl, float bevfac, float spline_length, int *r_bev, float *r_blend)
1432 {
1433         float normlen, normsum = 0.0f;
1434         float *seglen = bl->seglen;
1435         int *segbevcount = bl->segbevcount;
1436         int bevcount = 0, nr = bl->nr;
1437
1438         float bev_fl = bevfac * (bl->nr - 1);
1439         *r_bev = (int)bev_fl;
1440
1441         while (bevcount < nr - 1) {
1442                 normlen = *seglen / spline_length;
1443                 if (normsum + normlen > bevfac) {
1444                         bev_fl = bevcount + (bevfac - normsum) / normlen * *segbevcount;
1445                         *r_bev = (int) bev_fl;
1446                         *r_blend = bev_fl - *r_bev;
1447                         break;
1448                 }
1449                 normsum += normlen;
1450                 bevcount += *segbevcount;
1451                 segbevcount++;
1452                 seglen++;
1453         }
1454 }
1455
1456 static void calc_bevfac_spline_mapping(BevList *bl, float bevfac,
1457                                        float spline_length,
1458                                        int *r_bev, float *r_blend)
1459 {
1460         const float len_target = bevfac * spline_length;
1461         BevPoint *bevp = bl->bevpoints;
1462         float len_next = 0.0f, len = 0.0f;
1463         int i = 0, nr = bl->nr;
1464
1465         while (nr--) {
1466                 bevp++;
1467                 len_next = len + bevp->offset;
1468                 if (len_next > len_target) {
1469                         break;
1470                 }
1471                 len = len_next;
1472                 i++;
1473         }
1474
1475         *r_bev = i;
1476         *r_blend = (len_target - len) / bevp->offset;
1477 }
1478
1479 static void calc_bevfac_mapping_default(BevList *bl,
1480                                         int *r_start, float *r_firstblend,
1481                                         int *r_steps, float *r_lastblend)
1482 {
1483         *r_start = 0;
1484         *r_steps = bl->nr;
1485         *r_firstblend = 1.0f;
1486         *r_lastblend = 1.0f;
1487 }
1488
1489 static void calc_bevfac_mapping(Curve *cu, BevList *bl, Nurb *nu,
1490         int *r_start, float *r_firstblend, int *r_steps, float *r_lastblend)
1491 {
1492         float tmpf, total_length = 0.0f;
1493         int end = 0, i;
1494
1495         if ((BKE_nurb_check_valid_u(nu) == false) ||
1496             /* not essential, but skips unnecessary calculation */
1497             (min_ff(cu->bevfac1, cu->bevfac2) == 0.0f &&
1498              max_ff(cu->bevfac1, cu->bevfac2) == 1.0f))
1499         {
1500                 calc_bevfac_mapping_default(bl, r_start, r_firstblend, r_steps, r_lastblend);
1501                 return;
1502         }
1503
1504         if (ELEM(cu->bevfac1_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE) ||
1505             ELEM(cu->bevfac2_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE))
1506         {
1507                 for (i = 0; i < SEGMENTSU(nu); i++) {
1508                         total_length += bl->seglen[i];
1509                 }
1510         }
1511
1512         switch (cu->bevfac1_mapping) {
1513                 case CU_BEVFAC_MAP_RESOLU:
1514                 {
1515                         const float start_fl = cu->bevfac1 * (bl->nr - 1);
1516                         *r_start = (int)start_fl;
1517                         *r_firstblend = 1.0f - (start_fl - (*r_start));
1518                         break;
1519                 }
1520                 case CU_BEVFAC_MAP_SEGMENT:
1521                 {
1522                         calc_bevfac_segment_mapping(bl, cu->bevfac1, total_length, r_start, r_firstblend);
1523                         *r_firstblend = 1.0f - *r_firstblend;
1524                         break;
1525                 }
1526                 case CU_BEVFAC_MAP_SPLINE:
1527                 {
1528                         calc_bevfac_spline_mapping(bl, cu->bevfac1, total_length, r_start, r_firstblend);
1529                         *r_firstblend = 1.0f - *r_firstblend;
1530                         break;
1531                 }
1532         }
1533
1534         switch (cu->bevfac2_mapping) {
1535                 case CU_BEVFAC_MAP_RESOLU:
1536                 {
1537                         const float end_fl = cu->bevfac2 * (bl->nr - 1);
1538                         end = (int)end_fl;
1539
1540                         *r_steps = 2 + end - *r_start;
1541                         *r_lastblend = end_fl - end;
1542                         break;
1543                 }
1544                 case CU_BEVFAC_MAP_SEGMENT:
1545                 {
1546                         calc_bevfac_segment_mapping(bl, cu->bevfac2, total_length, &end, r_lastblend);
1547                         *r_steps = end - *r_start + 2;
1548                         break;
1549                 }
1550                 case CU_BEVFAC_MAP_SPLINE:
1551                 {
1552                         calc_bevfac_spline_mapping(bl, cu->bevfac2, total_length, &end, r_lastblend);
1553                         *r_steps = end - *r_start + 2;
1554                         break;
1555                 }
1556         }
1557
1558         if (end < *r_start || (end == *r_start && *r_lastblend < 1.0f - *r_firstblend )) {
1559                 SWAP(int, *r_start, end);
1560                 tmpf = *r_lastblend;
1561                 *r_lastblend = 1.0f - *r_firstblend;
1562                 *r_firstblend = 1.0f - tmpf;
1563                 *r_steps = end - *r_start + 2;
1564         }
1565
1566         if (*r_start + *r_steps > bl->nr) {
1567                 *r_steps = bl->nr - *r_start;
1568                 *r_lastblend = 1.0f;
1569         }
1570 }
1571
1572 static void do_makeDispListCurveTypes(
1573         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase,
1574         Mesh **r_final,
1575         const bool for_render, const bool for_orco, const bool use_render_resolution)
1576 {
1577         Curve *cu = ob->data;
1578
1579         /* we do allow duplis... this is only displist on curve level */
1580         if (!ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1581
1582         if (ob->type == OB_SURF) {
1583                 BKE_displist_make_surf(depsgraph, scene, ob, dispbase, r_final, for_render, for_orco, use_render_resolution);
1584         }
1585         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1586                 ListBase dlbev;
1587                 ListBase nubase = {NULL, NULL};
1588
1589                 BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
1590
1591                 /* We only re-evaluate path if evaluation is not happening for orco.
1592                  * If the calculation happens for orco, we should never free data which
1593                  * was needed before and only not needed for orco calculation.
1594                  */
1595                 if (!for_orco) {
1596                         if (ob->runtime.curve_cache->path) free_path(ob->runtime.curve_cache->path);
1597                         ob->runtime.curve_cache->path = NULL;
1598                 }
1599
1600                 if (ob->type == OB_FONT) {
1601                         BKE_vfont_to_curve_nubase(ob, FO_EDIT, &nubase);
1602                 }
1603                 else {
1604                         BKE_nurbList_duplicate(&nubase, BKE_curve_nurbs_get(cu));
1605                 }
1606
1607                 if (!for_orco)
1608                         curve_calc_modifiers_pre(depsgraph, scene, ob, &nubase, for_render, use_render_resolution);
1609
1610                 BKE_curve_bevelList_make(ob, &nubase, use_render_resolution);
1611
1612                 /* If curve has no bevel will return nothing */
1613                 BKE_curve_bevel_make(depsgraph, scene, ob, &dlbev, for_render, use_render_resolution);
1614
1615                 /* no bevel or extrude, and no width correction? */
1616                 if (!dlbev.first && cu->width == 1.0f) {
1617                         curve_to_displist(cu, &nubase, dispbase, for_render, use_render_resolution);
1618                 }
1619                 else {
1620                         float widfac = cu->width - 1.0f;
1621                         BevList *bl = ob->runtime.curve_cache->bev.first;
1622                         Nurb *nu = nubase.first;
1623
1624                         for (; bl && nu; bl = bl->next, nu = nu->next) {
1625                                 DispList *dl;
1626                                 float *data;
1627                                 int a;
1628
1629                                 if (bl->nr) { /* blank bevel lists can happen */
1630
1631                                         /* exception handling; curve without bevel or extrude, with width correction */
1632                                         if (BLI_listbase_is_empty(&dlbev)) {
1633                                                 BevPoint *bevp;
1634                                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbev");
1635                                                 dl->verts = MEM_mallocN(sizeof(float[3]) * bl->nr, "dlverts");
1636                                                 BLI_addtail(dispbase, dl);
1637
1638                                                 if (bl->poly != -1) dl->type = DL_POLY;
1639                                                 else dl->type = DL_SEGM;
1640
1641                                                 if (dl->type == DL_SEGM) dl->flag = (DL_FRONT_CURVE | DL_BACK_CURVE);
1642
1643                                                 dl->parts = 1;
1644                                                 dl->nr = bl->nr;
1645                                                 dl->col = nu->mat_nr;
1646                                                 dl->charidx = nu->charidx;
1647
1648                                                 /* dl->rt will be used as flag for render face and */
1649                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1650                                                 dl->rt = nu->flag & ~CU_2D;
1651
1652                                                 a = dl->nr;
1653                                                 bevp = bl->bevpoints;
1654                                                 data = dl->verts;
1655                                                 while (a--) {
1656                                                         data[0] = bevp->vec[0] + widfac * bevp->sina;
1657                                                         data[1] = bevp->vec[1] + widfac * bevp->cosa;
1658                                                         data[2] = bevp->vec[2];
1659                                                         bevp++;
1660                                                         data += 3;
1661                                                 }
1662                                         }
1663                                         else {
1664                                                 DispList *dlb;
1665                                                 ListBase bottom_capbase = {NULL, NULL};
1666                                                 ListBase top_capbase = {NULL, NULL};
1667                                                 float bottom_no[3] = {0.0f};
1668                                                 float top_no[3] = {0.0f};
1669                                                 float firstblend = 0.0f, lastblend = 0.0f;
1670                                                 int i, start, steps = 0;
1671
1672                                                 if (nu->flagu & CU_NURB_CYCLIC) {
1673                                                         calc_bevfac_mapping_default(bl,
1674                                                                                     &start, &firstblend, &steps, &lastblend);
1675                                                 }
1676                                                 else {
1677                                                         if (fabsf(cu->bevfac2 - cu->bevfac1) < FLT_EPSILON) {
1678                                                                 continue;
1679                                                         }
1680
1681                                                         calc_bevfac_mapping(cu, bl, nu, &start, &firstblend, &steps, &lastblend);
1682                                                 }
1683
1684                                                 for (dlb = dlbev.first; dlb; dlb = dlb->next) {
1685                                                         BevPoint *bevp_first, *bevp_last;
1686                                                         BevPoint *bevp;
1687
1688                                                         /* for each part of the bevel use a separate displblock */
1689                                                         dl = MEM_callocN(sizeof(DispList), "makeDispListbev1");
1690                                                         dl->verts = data = MEM_mallocN(sizeof(float[3]) * dlb->nr * steps, "dlverts");
1691                                                         BLI_addtail(dispbase, dl);
1692
1693                                                         dl->type = DL_SURF;
1694
1695                                                         dl->flag = dlb->flag & (DL_FRONT_CURVE | DL_BACK_CURVE);
1696                                                         if (dlb->type == DL_POLY) {
1697                                                                 dl->flag |= DL_CYCL_U;
1698                                                         }
1699                                                         if ((bl->poly >= 0) && (steps > 2)) {
1700                                                                 dl->flag |= DL_CYCL_V;
1701                                                         }
1702
1703                                                         dl->parts = steps;
1704                                                         dl->nr = dlb->nr;
1705                                                         dl->col = nu->mat_nr;
1706                                                         dl->charidx = nu->charidx;
1707
1708                                                         /* dl->rt will be used as flag for render face and */
1709                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1710                                                         dl->rt = nu->flag & ~CU_2D;
1711
1712                                                         dl->bevel_split = BLI_BITMAP_NEW(steps, "bevel_split");
1713
1714                                                         /* for each point of poly make a bevel piece */
1715                                                         bevp_first =  bl->bevpoints;
1716                                                         bevp_last  = &bl->bevpoints[bl->nr - 1];
1717                                                         bevp       = &bl->bevpoints[start];
1718                                                         for (i = start, a = 0; a < steps; i++, bevp++, a++) {
1719                                                                 float fac = 1.0;
1720                                                                 float *cur_data = data;
1721
1722                                                                 if (cu->taperobj == NULL) {
1723                                                                         fac = bevp->radius;
1724                                                                 }
1725                                                                 else {
1726                                                                         float len, taper_fac;
1727
1728                                                                         if (cu->flag & CU_MAP_TAPER) {
1729                                                                                 len = (steps - 3) + firstblend + lastblend;
1730
1731                                                                                 if (a == 0)
1732                                                                                         taper_fac = 0.0f;
1733                                                                                 else if (a == steps - 1)
1734                                                                                         taper_fac = 1.0f;
1735                                                                                 else
1736                                                                                         taper_fac = ((float) a - (1.0f - firstblend)) / len;
1737                                                                         }
1738                                                                         else {
1739                                                                                 len = bl->nr - 1;
1740                                                                                 taper_fac = (float) i / len;
1741
1742                                                                                 if (a == 0)
1743                                                                                         taper_fac += (1.0f - firstblend) / len;
1744                                                                                 else if (a == steps - 1)
1745                                                                                         taper_fac -= (1.0f - lastblend) / len;
1746                                                                         }
1747
1748                                                                         fac = displist_calc_taper(depsgraph, scene, cu->taperobj, taper_fac);
1749                                                                 }
1750
1751                                                                 if (bevp->split_tag) {
1752                                                                         BLI_BITMAP_ENABLE(dl->bevel_split, a);
1753                                                                 }
1754
1755                                                                 /* rotate bevel piece and write in data */
1756                                                                 if ((a == 0) && (bevp != bevp_last)) {
1757                                                                         rotateBevelPiece(cu, bevp, bevp + 1, dlb, 1.0f - firstblend, widfac, fac, &data);
1758                                                                 }
1759                                                                 else if ((a == steps - 1) && (bevp != bevp_first) ) {
1760                                                                         rotateBevelPiece(cu, bevp, bevp - 1, dlb, 1.0f - lastblend, widfac, fac, &data);
1761                                                                 }
1762                                                                 else {
1763                                                                         rotateBevelPiece(cu, bevp, NULL, dlb, 0.0f, widfac, fac, &data);
1764                                                                 }
1765
1766                                                                 if (cu->bevobj && (cu->flag & CU_FILL_CAPS) && !(nu->flagu & CU_NURB_CYCLIC)) {
1767                                                                         if (a == 1) {
1768                                                                                 fillBevelCap(nu, dlb, cur_data - 3 * dlb->nr, &bottom_capbase);
1769                                                                                 negate_v3_v3(bottom_no, bevp->dir);
1770                                                                         }
1771                                                                         if (a == steps - 1) {
1772                                                                                 fillBevelCap(nu, dlb, cur_data, &top_capbase);
1773                                                                                 copy_v3_v3(top_no, bevp->dir);
1774                                                                         }
1775                                                                 }
1776                                                         }
1777
1778                                                         /* gl array drawing: using indices */
1779                                                         displist_surf_indices(dl);
1780                                                 }
1781
1782                                                 if (bottom_capbase.first) {
1783                                                         BKE_displist_fill(&bottom_capbase, dispbase, bottom_no, false);
1784                                                         BKE_displist_fill(&top_capbase, dispbase, top_no, false);
1785                                                         BKE_displist_free(&bottom_capbase);
1786                                                         BKE_displist_free(&top_capbase);
1787                                                 }
1788                                         }
1789                                 }
1790
1791                         }
1792                         BKE_displist_free(&dlbev);
1793                 }
1794
1795                 if (!(cu->flag & CU_DEFORM_FILL)) {
1796                         curve_to_filledpoly(cu, &nubase, dispbase);
1797                 }
1798
1799                 if (!for_orco) {
1800                         if ((cu->flag & CU_PATH) ||
1801                             DEG_get_eval_flags_for_id(depsgraph, &ob->id) & DAG_EVAL_NEED_CURVE_PATH)
1802                         {
1803                                 calc_curvepath(ob, &nubase);
1804                         }
1805                 }
1806
1807                 if (!for_orco) {
1808                         BKE_nurbList_duplicate(&ob->runtime.curve_cache->deformed_nurbs, &nubase);
1809                         curve_calc_modifiers_post(depsgraph, scene, ob, &nubase, dispbase, r_final, for_render, use_render_resolution);
1810                 }
1811
1812                 if (cu->flag & CU_DEFORM_FILL && !ob->runtime.mesh_eval) {
1813                         curve_to_filledpoly(cu, &nubase, dispbase);
1814                 }
1815
1816                 BKE_nurbList_free(&nubase);
1817         }
1818 }
1819
1820 void BKE_displist_make_curveTypes(
1821         Depsgraph *depsgraph, Scene *scene, Object *ob, const bool for_render, const bool for_orco)
1822 {
1823         ListBase *dispbase;
1824
1825         /* The same check for duplis as in do_makeDispListCurveTypes.
1826          * Happens when curve used for constraint/bevel was converted to mesh.
1827          * check there is still needed for render displist and orco displists. */
1828         if (!ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT))
1829                 return;
1830
1831         BKE_object_free_derived_caches(ob);
1832
1833         if (!ob->runtime.curve_cache) {
1834                 ob->runtime.curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for curve types");
1835         }
1836
1837         dispbase = &(ob->runtime.curve_cache->disp);
1838
1839         do_makeDispListCurveTypes(depsgraph, scene, ob, dispbase, &ob->runtime.mesh_eval, for_render, for_orco, false);
1840
1841         boundbox_displist_object(ob);
1842 }
1843
1844 void BKE_displist_make_curveTypes_forRender(
1845         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase,
1846         Mesh **r_final, const bool for_orco,
1847         const bool use_render_resolution)
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, r_final, true, for_orco, use_render_resolution);
1854 }
1855
1856 void BKE_displist_make_curveTypes_forOrco(
1857         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase)
1858 {
1859         if (ob->runtime.curve_cache == NULL) {
1860                 ob->runtime.curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for Curve");
1861         }
1862
1863         do_makeDispListCurveTypes(depsgraph, scene, ob, dispbase, NULL, 1, 1, 1);
1864 }
1865
1866 void BKE_displist_minmax(ListBase *dispbase, float min[3], float max[3])
1867 {
1868         DispList *dl;
1869         const float *vert;
1870         int a, tot = 0;
1871         int doit = 0;
1872
1873         for (dl = dispbase->first; dl; dl = dl->next) {
1874                 tot = (dl->type == DL_INDEX3) ? dl->nr : dl->nr * dl->parts;
1875                 vert = dl->verts;
1876                 for (a = 0; a < tot; a++, vert += 3) {
1877                         minmax_v3v3_v3(min, max, vert);
1878                 }
1879                 doit |= (tot != 0);
1880         }
1881
1882         if (!doit) {
1883                 /* there's no geometry in displist, use zero-sized boundbox */
1884                 zero_v3(min);
1885                 zero_v3(max);
1886         }
1887 }
1888
1889 /* this is confusing, there's also min_max_object, appplying the obmat... */
1890 static void boundbox_displist_object(Object *ob)
1891 {
1892         if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1893                 /* Curve's BB is already calculated as a part of modifier stack,
1894                  * here we only calculate object BB based on final display list.
1895                  */
1896
1897                 /* object's BB is calculated from final displist */
1898                 if (ob->bb == NULL)
1899                         ob->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1900
1901                 if (ob->runtime.mesh_eval) {
1902                         BKE_object_boundbox_calc_from_mesh(ob, ob->runtime.mesh_eval);
1903                 }
1904                 else {
1905                         float min[3], max[3];
1906
1907                         INIT_MINMAX(min, max);
1908                         BKE_displist_minmax(&ob->runtime.curve_cache->disp, min, max);
1909                         BKE_boundbox_init_from_minmax(ob->bb, min, max);
1910
1911                         ob->bb->flag &= ~BOUNDBOX_DIRTY;
1912                 }
1913         }
1914 }