Logging: Use CLOG for blenkernel
[blender.git] / source / blender / blenkernel / intern / curve.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/curve.c
29  *  \ingroup bke
30  */
31
32
33 #include <math.h>  // floor
34 #include <string.h>
35 #include <stdlib.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_ghash.h"
43
44 #include "DNA_anim_types.h"
45 #include "DNA_curve_types.h"
46 #include "DNA_material_types.h"
47
48 /* for dereferencing pointers */
49 #include "DNA_key_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_vfont_types.h"
52 #include "DNA_object_types.h"
53
54 #include "BKE_animsys.h"
55 #include "BKE_curve.h"
56 #include "BKE_displist.h"
57 #include "BKE_font.h"
58 #include "BKE_key.h"
59 #include "BKE_library.h"
60 #include "BKE_main.h"
61 #include "BKE_object.h"
62 #include "BKE_material.h"
63
64 #include "DEG_depsgraph.h"
65
66 #include "CLG_log.h"
67
68 /* globals */
69
70 /* local */
71 static CLG_LogRef LOG = {"bke.curve"};
72
73 static int cu_isectLL(const float v1[3], const float v2[3], const float v3[3], const float v4[3],
74                       short cox, short coy,
75                       float *lambda, float *mu, float vec[3]);
76
77 /* frees editcurve entirely */
78 void BKE_curve_editfont_free(Curve *cu)
79 {
80         if (cu->editfont) {
81                 EditFont *ef = cu->editfont;
82
83                 if (ef->textbuf)
84                         MEM_freeN(ef->textbuf);
85                 if (ef->textbufinfo)
86                         MEM_freeN(ef->textbufinfo);
87                 if (ef->selboxes)
88                         MEM_freeN(ef->selboxes);
89
90                 MEM_freeN(ef);
91                 cu->editfont = NULL;
92         }
93 }
94
95 static void curve_editNurb_keyIndex_cv_free_cb(void *val)
96 {
97         CVKeyIndex *index = val;
98         MEM_freeN(index->orig_cv);
99         MEM_freeN(val);
100 }
101
102 void BKE_curve_editNurb_keyIndex_delCV(GHash *keyindex, const void *cv)
103 {
104         BLI_assert(keyindex != NULL);
105         BLI_ghash_remove(keyindex, cv, NULL, curve_editNurb_keyIndex_cv_free_cb);
106 }
107
108 void BKE_curve_editNurb_keyIndex_free(GHash **keyindex)
109 {
110         if (!(*keyindex)) {
111                 return;
112         }
113         BLI_ghash_free(*keyindex, NULL, curve_editNurb_keyIndex_cv_free_cb);
114         *keyindex = NULL;
115 }
116
117 void BKE_curve_editNurb_free(Curve *cu)
118 {
119         if (cu->editnurb) {
120                 BKE_nurbList_free(&cu->editnurb->nurbs);
121                 BKE_curve_editNurb_keyIndex_free(&cu->editnurb->keyindex);
122                 MEM_freeN(cu->editnurb);
123                 cu->editnurb = NULL;
124         }
125 }
126
127 /** Free (or release) any data used by this curve (does not free the curve itself). */
128 void BKE_curve_free(Curve *cu)
129 {
130         BKE_animdata_free((ID *)cu, false);
131
132         BKE_curve_batch_cache_free(cu);
133
134         BKE_nurbList_free(&cu->nurb);
135         BKE_curve_editfont_free(cu);
136
137         BKE_curve_editNurb_free(cu);
138
139         MEM_SAFE_FREE(cu->mat);
140         MEM_SAFE_FREE(cu->str);
141         MEM_SAFE_FREE(cu->strinfo);
142         MEM_SAFE_FREE(cu->bb);
143         MEM_SAFE_FREE(cu->tb);
144 }
145
146 void BKE_curve_init(Curve *cu)
147 {
148         /* BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(cu, id)); */  /* cu->type is already initialized... */
149
150         copy_v3_fl(cu->size, 1.0f);
151         cu->flag = CU_FRONT | CU_BACK | CU_DEFORM_BOUNDS_OFF | CU_PATH_RADIUS;
152         cu->pathlen = 100;
153         cu->resolu = cu->resolv = (cu->type == OB_SURF) ? 4 : 12;
154         cu->width = 1.0;
155         cu->wordspace = 1.0;
156         cu->spacing = cu->linedist = 1.0;
157         cu->fsize = 1.0;
158         cu->ulheight = 0.05;
159         cu->texflag = CU_AUTOSPACE;
160         cu->smallcaps_scale = 0.75f;
161         /* XXX: this one seems to be the best one in most cases, at least for curve deform... */
162         cu->twist_mode = CU_TWIST_MINIMUM;
163         cu->bevfac1 = 0.0f;
164         cu->bevfac2 = 1.0f;
165         cu->bevfac1_mapping = CU_BEVFAC_MAP_RESOLU;
166         cu->bevfac2_mapping = CU_BEVFAC_MAP_RESOLU;
167
168         cu->bb = BKE_boundbox_alloc_unit();
169
170         if (cu->type == OB_FONT) {
171                 cu->vfont = cu->vfontb = cu->vfonti = cu->vfontbi = BKE_vfont_builtin_get();
172                 cu->vfont->id.us += 4;
173                 cu->str = MEM_malloc_arrayN(12, sizeof(unsigned char), "str");
174                 BLI_strncpy(cu->str, "Text", 12);
175                 cu->len = cu->len_wchar = cu->pos = 4;
176                 cu->strinfo = MEM_calloc_arrayN(12, sizeof(CharInfo), "strinfo new");
177                 cu->totbox = cu->actbox = 1;
178                 cu->tb = MEM_calloc_arrayN(MAXTEXTBOX, sizeof(TextBox), "textbox");
179                 cu->tb[0].w = cu->tb[0].h = 0.0;
180         }
181 }
182
183 Curve *BKE_curve_add(Main *bmain, const char *name, int type)
184 {
185         Curve *cu;
186
187         cu = BKE_libblock_alloc(bmain, ID_CU, name, 0);
188         cu->type = type;
189
190         BKE_curve_init(cu);
191
192         return cu;
193 }
194
195 /**
196  * Only copy internal data of Curve ID from source to already allocated/initialized destination.
197  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
198  *
199  * WARNING! This function will not handle ID user count!
200  *
201  * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
202  */
203 void BKE_curve_copy_data(Main *bmain, Curve *cu_dst, const Curve *cu_src, const int flag)
204 {
205         BLI_listbase_clear(&cu_dst->nurb);
206         BKE_nurbList_duplicate(&(cu_dst->nurb), &(cu_src->nurb));
207
208         cu_dst->mat = MEM_dupallocN(cu_src->mat);
209
210         cu_dst->str = MEM_dupallocN(cu_src->str);
211         cu_dst->strinfo = MEM_dupallocN(cu_src->strinfo);
212         cu_dst->tb = MEM_dupallocN(cu_src->tb);
213         cu_dst->bb = MEM_dupallocN(cu_src->bb);
214         cu_dst->batch_cache = NULL;
215
216         if (cu_src->key && (flag & LIB_ID_COPY_SHAPEKEY)) {
217                 BKE_id_copy_ex(bmain, &cu_src->key->id, (ID **)&cu_dst->key, flag, false);
218         }
219
220         cu_dst->editnurb = NULL;
221         cu_dst->editfont = NULL;
222 }
223
224 Curve *BKE_curve_copy(Main *bmain, const Curve *cu)
225 {
226         Curve *cu_copy;
227         BKE_id_copy_ex(bmain, &cu->id, (ID **)&cu_copy, LIB_ID_COPY_SHAPEKEY, false);
228         return cu_copy;
229 }
230
231 void BKE_curve_make_local(Main *bmain, Curve *cu, const bool lib_local)
232 {
233         BKE_id_make_local_generic(bmain, &cu->id, true, lib_local);
234 }
235
236 /* Get list of nurbs from editnurbs structure */
237 ListBase *BKE_curve_editNurbs_get(Curve *cu)
238 {
239         if (cu->editnurb) {
240                 return &cu->editnurb->nurbs;
241         }
242
243         return NULL;
244 }
245
246 short BKE_curve_type_get(Curve *cu)
247 {
248         Nurb *nu;
249         int type = cu->type;
250
251         if (cu->vfont) {
252                 return OB_FONT;
253         }
254
255         if (!cu->type) {
256                 type = OB_CURVE;
257
258                 for (nu = cu->nurb.first; nu; nu = nu->next) {
259                         if (nu->pntsv > 1) {
260                                 type = OB_SURF;
261                         }
262                 }
263         }
264
265         return type;
266 }
267
268 void BKE_curve_curve_dimension_update(Curve *cu)
269 {
270         ListBase *nurbs = BKE_curve_nurbs_get(cu);
271         Nurb *nu = nurbs->first;
272
273         if (cu->flag & CU_3D) {
274                 for (; nu; nu = nu->next) {
275                         nu->flag &= ~CU_2D;
276                 }
277         }
278         else {
279                 for (; nu; nu = nu->next) {
280                         nu->flag |= CU_2D;
281                         BKE_nurb_test_2d(nu);
282
283                         /* since the handles are moved they need to be auto-located again */
284                         if (nu->type == CU_BEZIER)
285                                 BKE_nurb_handles_calc(nu);
286                 }
287         }
288 }
289
290 void BKE_curve_type_test(Object *ob)
291 {
292         ob->type = BKE_curve_type_get(ob->data);
293
294         if (ob->type == OB_CURVE)
295                 BKE_curve_curve_dimension_update((Curve *)ob->data);
296 }
297
298 void BKE_curve_boundbox_calc(Curve *cu, float r_loc[3], float r_size[3])
299 {
300         BoundBox *bb;
301         float min[3], max[3];
302         float mloc[3], msize[3];
303
304         if (cu->bb == NULL) cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
305         bb = cu->bb;
306
307         if (!r_loc) r_loc = mloc;
308         if (!r_size) r_size = msize;
309
310         INIT_MINMAX(min, max);
311         if (!BKE_curve_minmax(cu, true, min, max)) {
312                 min[0] = min[1] = min[2] = -1.0f;
313                 max[0] = max[1] = max[2] = 1.0f;
314         }
315
316         mid_v3_v3v3(r_loc, min, max);
317
318         r_size[0] = (max[0] - min[0]) / 2.0f;
319         r_size[1] = (max[1] - min[1]) / 2.0f;
320         r_size[2] = (max[2] - min[2]) / 2.0f;
321
322         BKE_boundbox_init_from_minmax(bb, min, max);
323
324         bb->flag &= ~BOUNDBOX_DIRTY;
325 }
326
327 BoundBox *BKE_curve_boundbox_get(Object *ob)
328 {
329         /* This is Object-level data access, DO NOT touch to Mesh's bb, would be totally thread-unsafe. */
330         if (ob->bb == NULL || ob->bb->flag & BOUNDBOX_DIRTY) {
331                 Curve *cu = ob->data;
332                 float min[3], max[3];
333
334                 INIT_MINMAX(min, max);
335                 BKE_curve_minmax(cu, true, min, max);
336
337                 if (ob->bb == NULL) {
338                         ob->bb = MEM_mallocN(sizeof(*ob->bb), __func__);
339                 }
340                 BKE_boundbox_init_from_minmax(ob->bb, min, max);
341                 ob->bb->flag &= ~BOUNDBOX_DIRTY;
342         }
343
344         return ob->bb;
345 }
346
347 void BKE_curve_texspace_calc(Curve *cu)
348 {
349         float loc[3], size[3];
350         int a;
351
352         BKE_curve_boundbox_calc(cu, loc, size);
353
354         if (cu->texflag & CU_AUTOSPACE) {
355                 for (a = 0; a < 3; a++) {
356                         if (size[a] == 0.0f) size[a] = 1.0f;
357                         else if (size[a] > 0.0f && size[a] < 0.00001f) size[a] = 0.00001f;
358                         else if (size[a] < 0.0f && size[a] > -0.00001f) size[a] = -0.00001f;
359                 }
360
361                 copy_v3_v3(cu->loc, loc);
362                 copy_v3_v3(cu->size, size);
363                 zero_v3(cu->rot);
364         }
365 }
366
367 BoundBox *BKE_curve_texspace_get(Curve *cu, float r_loc[3], float r_rot[3], float r_size[3])
368 {
369         if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
370                 BKE_curve_texspace_calc(cu);
371         }
372
373         if (r_loc) copy_v3_v3(r_loc,  cu->loc);
374         if (r_rot) copy_v3_v3(r_rot,  cu->rot);
375         if (r_size) copy_v3_v3(r_size, cu->size);
376
377         return cu->bb;
378 }
379
380 bool BKE_nurbList_index_get_co(ListBase *nurb, const int index, float r_co[3])
381 {
382         Nurb *nu;
383         int tot = 0;
384
385         for (nu = nurb->first; nu; nu = nu->next) {
386                 int tot_nu;
387                 if (nu->type == CU_BEZIER) {
388                         tot_nu = nu->pntsu;
389                         if (index - tot < tot_nu) {
390                                 copy_v3_v3(r_co, nu->bezt[index - tot].vec[1]);
391                                 return true;
392                         }
393                 }
394                 else {
395                         tot_nu = nu->pntsu * nu->pntsv;
396                         if (index - tot < tot_nu) {
397                                 copy_v3_v3(r_co, nu->bp[index - tot].vec);
398                                 return true;
399                         }
400                 }
401                 tot += tot_nu;
402         }
403
404         return false;
405 }
406
407 int BKE_nurbList_verts_count(ListBase *nurb)
408 {
409         Nurb *nu;
410         int tot = 0;
411
412         nu = nurb->first;
413         while (nu) {
414                 if (nu->bezt)
415                         tot += 3 * nu->pntsu;
416                 else if (nu->bp)
417                         tot += nu->pntsu * nu->pntsv;
418
419                 nu = nu->next;
420         }
421         return tot;
422 }
423
424 int BKE_nurbList_verts_count_without_handles(ListBase *nurb)
425 {
426         Nurb *nu;
427         int tot = 0;
428
429         nu = nurb->first;
430         while (nu) {
431                 if (nu->bezt)
432                         tot += nu->pntsu;
433                 else if (nu->bp)
434                         tot += nu->pntsu * nu->pntsv;
435
436                 nu = nu->next;
437         }
438         return tot;
439 }
440
441 /* **************** NURBS ROUTINES ******************** */
442
443 void BKE_nurb_free(Nurb *nu)
444 {
445
446         if (nu == NULL) return;
447
448         if (nu->bezt)
449                 MEM_freeN(nu->bezt);
450         nu->bezt = NULL;
451         if (nu->bp)
452                 MEM_freeN(nu->bp);
453         nu->bp = NULL;
454         if (nu->knotsu)
455                 MEM_freeN(nu->knotsu);
456         nu->knotsu = NULL;
457         if (nu->knotsv)
458                 MEM_freeN(nu->knotsv);
459         nu->knotsv = NULL;
460         /* if (nu->trim.first) freeNurblist(&(nu->trim)); */
461
462         MEM_freeN(nu);
463
464 }
465
466
467 void BKE_nurbList_free(ListBase *lb)
468 {
469         Nurb *nu, *next;
470
471         if (lb == NULL) return;
472
473         nu = lb->first;
474         while (nu) {
475                 next = nu->next;
476                 BKE_nurb_free(nu);
477                 nu = next;
478         }
479         BLI_listbase_clear(lb);
480 }
481
482 Nurb *BKE_nurb_duplicate(const Nurb *nu)
483 {
484         Nurb *newnu;
485         int len;
486
487         newnu = (Nurb *)MEM_mallocN(sizeof(Nurb), "duplicateNurb");
488         if (newnu == NULL) return NULL;
489         memcpy(newnu, nu, sizeof(Nurb));
490
491         if (nu->bezt) {
492                 newnu->bezt =
493                     (BezTriple *)MEM_malloc_arrayN(nu->pntsu, sizeof(BezTriple), "duplicateNurb2");
494                 memcpy(newnu->bezt, nu->bezt, nu->pntsu * sizeof(BezTriple));
495         }
496         else {
497                 len = nu->pntsu * nu->pntsv;
498                 newnu->bp =
499                     (BPoint *)MEM_malloc_arrayN(len, sizeof(BPoint), "duplicateNurb3");
500                 memcpy(newnu->bp, nu->bp, len * sizeof(BPoint));
501
502                 newnu->knotsu = newnu->knotsv = NULL;
503
504                 if (nu->knotsu) {
505                         len = KNOTSU(nu);
506                         if (len) {
507                                 newnu->knotsu = MEM_malloc_arrayN(len, sizeof(float), "duplicateNurb4");
508                                 memcpy(newnu->knotsu, nu->knotsu, sizeof(float) * len);
509                         }
510                 }
511                 if (nu->pntsv > 1 && nu->knotsv) {
512                         len = KNOTSV(nu);
513                         if (len) {
514                                 newnu->knotsv = MEM_malloc_arrayN(len, sizeof(float), "duplicateNurb5");
515                                 memcpy(newnu->knotsv, nu->knotsv, sizeof(float) * len);
516                         }
517                 }
518         }
519         return newnu;
520 }
521
522 /* copy the nurb but allow for different number of points (to be copied after this) */
523 Nurb *BKE_nurb_copy(Nurb *src, int pntsu, int pntsv)
524 {
525         Nurb *newnu = (Nurb *)MEM_mallocN(sizeof(Nurb), "copyNurb");
526         memcpy(newnu, src, sizeof(Nurb));
527
528         if (pntsu == 1) SWAP(int, pntsu, pntsv);
529         newnu->pntsu = pntsu;
530         newnu->pntsv = pntsv;
531
532         /* caller can manually handle these arrays */
533         newnu->knotsu = NULL;
534         newnu->knotsv = NULL;
535
536         if (src->bezt) {
537                 newnu->bezt = (BezTriple *)MEM_malloc_arrayN(pntsu * pntsv, sizeof(BezTriple), "copyNurb2");
538         }
539         else {
540                 newnu->bp = (BPoint *)MEM_malloc_arrayN(pntsu * pntsv, sizeof(BPoint), "copyNurb3");
541         }
542
543         return newnu;
544 }
545
546 void BKE_nurbList_duplicate(ListBase *lb1, const ListBase *lb2)
547 {
548         Nurb *nu, *nun;
549
550         BKE_nurbList_free(lb1);
551
552         nu = lb2->first;
553         while (nu) {
554                 nun = BKE_nurb_duplicate(nu);
555                 BLI_addtail(lb1, nun);
556
557                 nu = nu->next;
558         }
559 }
560
561 void BKE_nurb_test_2d(Nurb *nu)
562 {
563         BezTriple *bezt;
564         BPoint *bp;
565         int a;
566
567         if ((nu->flag & CU_2D) == 0)
568                 return;
569
570         if (nu->type == CU_BEZIER) {
571                 a = nu->pntsu;
572                 bezt = nu->bezt;
573                 while (a--) {
574                         bezt->vec[0][2] = 0.0;
575                         bezt->vec[1][2] = 0.0;
576                         bezt->vec[2][2] = 0.0;
577                         bezt++;
578                 }
579         }
580         else {
581                 a = nu->pntsu * nu->pntsv;
582                 bp = nu->bp;
583                 while (a--) {
584                         bp->vec[2] = 0.0;
585                         bp++;
586                 }
587         }
588 }
589
590 /**
591  * if use_radius is truth, minmax will take points' radius into account,
592  * which will make boundbox closer to beveled curve.
593  */
594 void BKE_nurb_minmax(Nurb *nu, bool use_radius, float min[3], float max[3])
595 {
596         BezTriple *bezt;
597         BPoint *bp;
598         int a;
599         float point[3];
600
601         if (nu->type == CU_BEZIER) {
602                 a = nu->pntsu;
603                 bezt = nu->bezt;
604                 while (a--) {
605                         if (use_radius) {
606                                 float radius_vector[3];
607                                 radius_vector[0] = radius_vector[1] = radius_vector[2] = bezt->radius;
608
609                                 add_v3_v3v3(point, bezt->vec[1], radius_vector);
610                                 minmax_v3v3_v3(min, max, point);
611
612                                 sub_v3_v3v3(point, bezt->vec[1], radius_vector);
613                                 minmax_v3v3_v3(min, max, point);
614                         }
615                         else {
616                                 minmax_v3v3_v3(min, max, bezt->vec[1]);
617                         }
618                         minmax_v3v3_v3(min, max, bezt->vec[0]);
619                         minmax_v3v3_v3(min, max, bezt->vec[2]);
620                         bezt++;
621                 }
622         }
623         else {
624                 a = nu->pntsu * nu->pntsv;
625                 bp = nu->bp;
626                 while (a--) {
627                         if (nu->pntsv == 1 && use_radius) {
628                                 float radius_vector[3];
629                                 radius_vector[0] = radius_vector[1] = radius_vector[2] = bp->radius;
630
631                                 add_v3_v3v3(point, bp->vec, radius_vector);
632                                 minmax_v3v3_v3(min, max, point);
633
634                                 sub_v3_v3v3(point, bp->vec, radius_vector);
635                                 minmax_v3v3_v3(min, max, point);
636                         }
637                         else {
638                                 /* Surfaces doesn't use bevel, so no need to take radius into account. */
639                                 minmax_v3v3_v3(min, max, bp->vec);
640                         }
641                         bp++;
642                 }
643         }
644 }
645
646 float BKE_nurb_calc_length(const Nurb *nu, int resolution)
647 {
648         BezTriple *bezt, *prevbezt;
649         BPoint *bp, *prevbp;
650         int a, b;
651         float length = 0.0f;
652         int resolu = resolution ? resolution : nu->resolu;
653         int pntsu = nu->pntsu;
654         float *points, *pntsit, *prevpntsit;
655
656         if (nu->type == CU_POLY) {
657                 a = nu->pntsu - 1;
658                 bp = nu->bp;
659                 if (nu->flagu & CU_NURB_CYCLIC) {
660                         ++a;
661                         prevbp = nu->bp + (nu->pntsu - 1);
662                 }
663                 else {
664                         prevbp = bp;
665                         bp++;
666                 }
667
668                 while (a--) {
669                         length += len_v3v3(prevbp->vec, bp->vec);
670                         prevbp = bp;
671                         ++bp;
672                 }
673         }
674         else if (nu->type == CU_BEZIER) {
675                 points = MEM_mallocN(sizeof(float[3]) * (resolu + 1), "getLength_bezier");
676                 a = nu->pntsu - 1;
677                 bezt = nu->bezt;
678                 if (nu->flagu & CU_NURB_CYCLIC) {
679                         ++a;
680                         prevbezt = nu->bezt + (nu->pntsu - 1);
681                 }
682                 else {
683                         prevbezt = bezt;
684                         ++bezt;
685                 }
686
687                 while (a--) {
688                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
689                                 length += len_v3v3(prevbezt->vec[1], bezt->vec[1]);
690                         }
691                         else {
692                                 for (int j = 0; j < 3; j++) {
693                                         BKE_curve_forward_diff_bezier(
694                                                 prevbezt->vec[1][j], prevbezt->vec[2][j],
695                                                 bezt->vec[0][j], bezt->vec[1][j],
696                                                 points + j, resolu, 3 * sizeof(float));
697                                 }
698
699                                 prevpntsit = pntsit = points;
700                                 b = resolu;
701                                 while (b--) {
702                                         pntsit += 3;
703                                         length += len_v3v3(prevpntsit, pntsit);
704                                         prevpntsit = pntsit;
705                                 }
706                         }
707                         prevbezt = bezt;
708                         ++bezt;
709                 }
710
711                 MEM_freeN(points);
712         }
713         else if (nu->type == CU_NURBS) {
714                 if (nu->pntsv == 1) {
715                         /* important to zero for BKE_nurb_makeCurve. */
716                         points = MEM_callocN(sizeof(float[3]) * pntsu * resolu, "getLength_nurbs");
717
718                         BKE_nurb_makeCurve(
719                                 nu, points,
720                                 NULL, NULL, NULL,
721                                 resolu, sizeof(float[3]));
722
723                         if (nu->flagu & CU_NURB_CYCLIC) {
724                                 b = pntsu * resolu + 1;
725                                 prevpntsit = points + 3 * (pntsu * resolu - 1);
726                                 pntsit = points;
727                         }
728                         else {
729                                 b = (pntsu - 1) * resolu;
730                                 prevpntsit = points;
731                                 pntsit = points + 3;
732                         }
733
734                         while (--b) {
735                                 length += len_v3v3(prevpntsit, pntsit);
736                                 prevpntsit = pntsit;
737                                 pntsit += 3;
738                         }
739
740                         MEM_freeN(points);
741                 }
742         }
743
744         return length;
745 }
746
747 /* be sure to call makeknots after this */
748 void BKE_nurb_points_add(Nurb *nu, int number)
749 {
750         BPoint *bp;
751         int i;
752
753         nu->bp = MEM_recallocN(nu->bp, (nu->pntsu + number) * sizeof(BPoint));
754
755         for (i = 0, bp = &nu->bp[nu->pntsu]; i < number; i++, bp++) {
756                 bp->radius = 1.0f;
757         }
758
759         nu->pntsu += number;
760 }
761
762 void BKE_nurb_bezierPoints_add(Nurb *nu, int number)
763 {
764         BezTriple *bezt;
765         int i;
766
767         nu->bezt = MEM_recallocN(nu->bezt, (nu->pntsu + number) * sizeof(BezTriple));
768
769         for (i = 0, bezt = &nu->bezt[nu->pntsu]; i < number; i++, bezt++) {
770                 bezt->radius = 1.0f;
771         }
772
773         nu->pntsu += number;
774 }
775
776
777 int BKE_nurb_index_from_uv(
778         Nurb *nu,
779         int u, int v)
780 {
781         const int totu = nu->pntsu;
782         const int totv = nu->pntsv;
783
784         if (nu->flagu & CU_NURB_CYCLIC) {
785                 u = mod_i(u, totu);
786         }
787         else if (u < 0 || u >= totu) {
788                 return -1;
789         }
790
791         if (nu->flagv & CU_NURB_CYCLIC) {
792                 v = mod_i(v, totv);
793         }
794         else if (v < 0 || v >= totv) {
795                 return -1;
796         }
797
798         return (v * totu) + u;
799 }
800
801 void BKE_nurb_index_to_uv(
802         Nurb *nu, int index,
803         int *r_u, int *r_v)
804 {
805         const int totu = nu->pntsu;
806         const int totv = nu->pntsv;
807         BLI_assert(index >= 0 && index < (nu->pntsu * nu->pntsv));
808         *r_u = (index % totu);
809         *r_v = (index / totu) % totv;
810 }
811
812 BezTriple *BKE_nurb_bezt_get_next(Nurb *nu, BezTriple *bezt)
813 {
814         BezTriple *bezt_next;
815
816         BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
817
818         if (bezt == &nu->bezt[nu->pntsu - 1]) {
819                 if (nu->flagu & CU_NURB_CYCLIC) {
820                         bezt_next = nu->bezt;
821                 }
822                 else {
823                         bezt_next = NULL;
824                 }
825         }
826         else {
827                 bezt_next = bezt + 1;
828         }
829
830         return bezt_next;
831 }
832
833 BPoint *BKE_nurb_bpoint_get_next(Nurb *nu, BPoint *bp)
834 {
835         BPoint *bp_next;
836
837         BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
838
839         if (bp == &nu->bp[nu->pntsu - 1]) {
840                 if (nu->flagu & CU_NURB_CYCLIC) {
841                         bp_next = nu->bp;
842                 }
843                 else {
844                         bp_next = NULL;
845                 }
846         }
847         else {
848                 bp_next = bp + 1;
849         }
850
851         return bp_next;
852 }
853
854 BezTriple *BKE_nurb_bezt_get_prev(Nurb *nu, BezTriple *bezt)
855 {
856         BezTriple *bezt_prev;
857
858         BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
859         BLI_assert(nu->pntsv <= 1);
860
861         if (bezt == nu->bezt) {
862                 if (nu->flagu & CU_NURB_CYCLIC) {
863                         bezt_prev = &nu->bezt[nu->pntsu - 1];
864                 }
865                 else {
866                         bezt_prev = NULL;
867                 }
868         }
869         else {
870                 bezt_prev = bezt - 1;
871         }
872
873         return bezt_prev;
874 }
875
876 BPoint *BKE_nurb_bpoint_get_prev(Nurb *nu, BPoint *bp)
877 {
878         BPoint *bp_prev;
879
880         BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
881         BLI_assert(nu->pntsv == 1);
882
883         if (bp == nu->bp) {
884                 if (nu->flagu & CU_NURB_CYCLIC) {
885                         bp_prev = &nu->bp[nu->pntsu - 1];
886                 }
887                 else {
888                         bp_prev = NULL;
889                 }
890         }
891         else {
892                 bp_prev = bp - 1;
893         }
894
895         return bp_prev;
896 }
897
898 void BKE_nurb_bezt_calc_normal(struct Nurb *UNUSED(nu), BezTriple *bezt, float r_normal[3])
899 {
900         /* calculate the axis matrix from the spline */
901         float dir_prev[3], dir_next[3];
902
903         sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
904         sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
905
906         normalize_v3(dir_prev);
907         normalize_v3(dir_next);
908
909         add_v3_v3v3(r_normal, dir_prev, dir_next);
910         normalize_v3(r_normal);
911 }
912
913 void BKE_nurb_bezt_calc_plane(struct Nurb *nu, BezTriple *bezt, float r_plane[3])
914 {
915         float dir_prev[3], dir_next[3];
916
917         sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
918         sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
919
920         normalize_v3(dir_prev);
921         normalize_v3(dir_next);
922
923         cross_v3_v3v3(r_plane, dir_prev, dir_next);
924         if (normalize_v3(r_plane) < FLT_EPSILON) {
925                 BezTriple *bezt_prev = BKE_nurb_bezt_get_prev(nu, bezt);
926                 BezTriple *bezt_next = BKE_nurb_bezt_get_next(nu, bezt);
927
928                 if (bezt_prev) {
929                         sub_v3_v3v3(dir_prev, bezt_prev->vec[1], bezt->vec[1]);
930                         normalize_v3(dir_prev);
931                 }
932                 if (bezt_next) {
933                         sub_v3_v3v3(dir_next, bezt->vec[1], bezt_next->vec[1]);
934                         normalize_v3(dir_next);
935                 }
936                 cross_v3_v3v3(r_plane, dir_prev, dir_next);
937         }
938
939         /* matches with bones more closely */
940         {
941                 float dir_mid[3], tvec[3];
942                 add_v3_v3v3(dir_mid, dir_prev, dir_next);
943                 cross_v3_v3v3(tvec, r_plane, dir_mid);
944                 copy_v3_v3(r_plane, tvec);
945         }
946
947         normalize_v3(r_plane);
948 }
949
950 void BKE_nurb_bpoint_calc_normal(struct Nurb *nu, BPoint *bp, float r_normal[3])
951 {
952         BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp);
953         BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp);
954
955         zero_v3(r_normal);
956
957         if (bp_prev) {
958                 float dir_prev[3];
959                 sub_v3_v3v3(dir_prev, bp_prev->vec, bp->vec);
960                 normalize_v3(dir_prev);
961                 add_v3_v3(r_normal, dir_prev);
962         }
963         if (bp_next) {
964                 float dir_next[3];
965                 sub_v3_v3v3(dir_next, bp->vec, bp_next->vec);
966                 normalize_v3(dir_next);
967                 add_v3_v3(r_normal, dir_next);
968         }
969
970         normalize_v3(r_normal);
971 }
972
973 void BKE_nurb_bpoint_calc_plane(struct Nurb *nu, BPoint *bp, float r_plane[3])
974 {
975         BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp);
976         BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp);
977
978         float dir_prev[3] = {0.0f}, dir_next[3] = {0.0f};
979
980         if (bp_prev) {
981                 sub_v3_v3v3(dir_prev, bp_prev->vec, bp->vec);
982                 normalize_v3(dir_prev);
983         }
984         if (bp_next) {
985                 sub_v3_v3v3(dir_next, bp->vec, bp_next->vec);
986                 normalize_v3(dir_next);
987         }
988         cross_v3_v3v3(r_plane, dir_prev, dir_next);
989
990         /* matches with bones more closely */
991         {
992                 float dir_mid[3], tvec[3];
993                 add_v3_v3v3(dir_mid, dir_prev, dir_next);
994                 cross_v3_v3v3(tvec, r_plane, dir_mid);
995                 copy_v3_v3(r_plane, tvec);
996         }
997
998         normalize_v3(r_plane);
999 }
1000
1001 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
1002
1003
1004 static void calcknots(float *knots, const int pnts, const short order, const short flag)
1005 {
1006         /* knots: number of pnts NOT corrected for cyclic */
1007         const int pnts_order = pnts + order;
1008         float k;
1009         int a;
1010
1011         switch (flag & (CU_NURB_ENDPOINT | CU_NURB_BEZIER)) {
1012                 case CU_NURB_ENDPOINT:
1013                         k = 0.0;
1014                         for (a = 1; a <= pnts_order; a++) {
1015                                 knots[a - 1] = k;
1016                                 if (a >= order && a <= pnts)
1017                                         k += 1.0f;
1018                         }
1019                         break;
1020                 case CU_NURB_BEZIER:
1021                         /* Warning, the order MUST be 2 or 4,
1022                          * if this is not enforced, the displist will be corrupt */
1023                         if (order == 4) {
1024                                 k = 0.34;
1025                                 for (a = 0; a < pnts_order; a++) {
1026                                         knots[a] = floorf(k);
1027                                         k += (1.0f / 3.0f);
1028                                 }
1029                         }
1030                         else if (order == 3) {
1031                                 k = 0.6f;
1032                                 for (a = 0; a < pnts_order; a++) {
1033                                         if (a >= order && a <= pnts)
1034                                                 k += 0.5f;
1035                                         knots[a] = floorf(k);
1036                                 }
1037                         }
1038                         else {
1039                                 CLOG_ERROR(&LOG, "bez nurb curve order is not 3 or 4, should never happen");
1040                         }
1041                         break;
1042                 default:
1043                         for (a = 0; a < pnts_order; a++) {
1044                                 knots[a] = (float)a;
1045                         }
1046                         break;
1047         }
1048 }
1049
1050 static void makecyclicknots(float *knots, int pnts, short order)
1051 /* pnts, order: number of pnts NOT corrected for cyclic */
1052 {
1053         int a, b, order2, c;
1054
1055         if (knots == NULL)
1056                 return;
1057
1058         order2 = order - 1;
1059
1060         /* do first long rows (order -1), remove identical knots at endpoints */
1061         if (order > 2) {
1062                 b = pnts + order2;
1063                 for (a = 1; a < order2; a++) {
1064                         if (knots[b] != knots[b - a])
1065                                 break;
1066                 }
1067                 if (a == order2)
1068                         knots[pnts + order - 2] += 1.0f;
1069         }
1070
1071         b = order;
1072         c = pnts + order + order2;
1073         for (a = pnts + order2; a < c; a++) {
1074                 knots[a] = knots[a - 1] + (knots[b] - knots[b - 1]);
1075                 b--;
1076         }
1077 }
1078
1079
1080
1081 static void makeknots(Nurb *nu, short uv)
1082 {
1083         if (nu->type == CU_NURBS) {
1084                 if (uv == 1) {
1085                         if (nu->knotsu)
1086                                 MEM_freeN(nu->knotsu);
1087                         if (BKE_nurb_check_valid_u(nu)) {
1088                                 nu->knotsu = MEM_calloc_arrayN(KNOTSU(nu) + 1, sizeof(float), "makeknots");
1089                                 if (nu->flagu & CU_NURB_CYCLIC) {
1090                                         calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0);  /* cyclic should be uniform */
1091                                         makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
1092                                 }
1093                                 else {
1094                                         calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
1095                                 }
1096                         }
1097                         else
1098                                 nu->knotsu = NULL;
1099                 }
1100                 else if (uv == 2) {
1101                         if (nu->knotsv)
1102                                 MEM_freeN(nu->knotsv);
1103                         if (BKE_nurb_check_valid_v(nu)) {
1104                                 nu->knotsv = MEM_calloc_arrayN(KNOTSV(nu) + 1, sizeof(float), "makeknots");
1105                                 if (nu->flagv & CU_NURB_CYCLIC) {
1106                                         calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0);  /* cyclic should be uniform */
1107                                         makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
1108                                 }
1109                                 else {
1110                                         calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
1111                                 }
1112                         }
1113                         else {
1114                                 nu->knotsv = NULL;
1115                         }
1116                 }
1117         }
1118 }
1119
1120 void BKE_nurb_knot_calc_u(Nurb *nu)
1121 {
1122         makeknots(nu, 1);
1123 }
1124
1125 void BKE_nurb_knot_calc_v(Nurb *nu)
1126 {
1127         makeknots(nu, 2);
1128 }
1129
1130 static void basisNurb(float t, short order, int pnts, float *knots, float *basis, int *start, int *end)
1131 {
1132         float d, e;
1133         int i, i1 = 0, i2 = 0, j, orderpluspnts, opp2, o2;
1134
1135         orderpluspnts = order + pnts;
1136         opp2 = orderpluspnts - 1;
1137
1138         /* this is for float inaccuracy */
1139         if (t < knots[0])
1140                 t = knots[0];
1141         else if (t > knots[opp2])
1142                 t = knots[opp2];
1143
1144         /* this part is order '1' */
1145         o2 = order + 1;
1146         for (i = 0; i < opp2; i++) {
1147                 if (knots[i] != knots[i + 1] && t >= knots[i] && t <= knots[i + 1]) {
1148                         basis[i] = 1.0;
1149                         i1 = i - o2;
1150                         if (i1 < 0) i1 = 0;
1151                         i2 = i;
1152                         i++;
1153                         while (i < opp2) {
1154                                 basis[i] = 0.0;
1155                                 i++;
1156                         }
1157                         break;
1158                 }
1159                 else
1160                         basis[i] = 0.0;
1161         }
1162         basis[i] = 0.0;
1163
1164         /* this is order 2, 3, ... */
1165         for (j = 2; j <= order; j++) {
1166
1167                 if (i2 + j >= orderpluspnts) i2 = opp2 - j;
1168
1169                 for (i = i1; i <= i2; i++) {
1170                         if (basis[i] != 0.0f)
1171                                 d = ((t - knots[i]) * basis[i]) / (knots[i + j - 1] - knots[i]);
1172                         else
1173                                 d = 0.0f;
1174
1175                         if (basis[i + 1] != 0.0f)
1176                                 e = ((knots[i + j] - t) * basis[i + 1]) / (knots[i + j] - knots[i + 1]);
1177                         else
1178                                 e = 0.0;
1179
1180                         basis[i] = d + e;
1181                 }
1182         }
1183
1184         *start = 1000;
1185         *end = 0;
1186
1187         for (i = i1; i <= i2; i++) {
1188                 if (basis[i] > 0.0f) {
1189                         *end = i;
1190                         if (*start == 1000) *start = i;
1191                 }
1192         }
1193 }
1194
1195 /**
1196  * \param coord_array: has to be (3 * 4 * resolu * resolv) in size, and zero-ed.
1197  */
1198 void BKE_nurb_makeFaces(const Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
1199 {
1200         BPoint *bp;
1201         float *basisu, *basis, *basisv, *sum, *fp, *in;
1202         float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
1203         int i, j, iofs, jofs, cycl, len, curu, curv;
1204         int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
1205
1206         int totu = nu->pntsu * resolu, totv = nu->pntsv * resolv;
1207
1208         if (nu->knotsu == NULL || nu->knotsv == NULL)
1209                 return;
1210         if (nu->orderu > nu->pntsu)
1211                 return;
1212         if (nu->orderv > nu->pntsv)
1213                 return;
1214         if (coord_array == NULL)
1215                 return;
1216
1217         /* allocate and initialize */
1218         len = totu * totv;
1219         if (len == 0)
1220                 return;
1221
1222         sum = (float *)MEM_calloc_arrayN(len, sizeof(float), "makeNurbfaces1");
1223
1224         bp = nu->bp;
1225         i = nu->pntsu * nu->pntsv;
1226         ratcomp = 0;
1227         while (i--) {
1228                 if (bp->vec[3] != 1.0f) {
1229                         ratcomp = 1;
1230                         break;
1231                 }
1232                 bp++;
1233         }
1234
1235         fp = nu->knotsu;
1236         ustart = fp[nu->orderu - 1];
1237         if (nu->flagu & CU_NURB_CYCLIC)
1238                 uend = fp[nu->pntsu + nu->orderu - 1];
1239         else
1240                 uend = fp[nu->pntsu];
1241         ustep = (uend - ustart) / ((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
1242
1243         basisu = (float *)MEM_malloc_arrayN(KNOTSU(nu), sizeof(float), "makeNurbfaces3");
1244
1245         fp = nu->knotsv;
1246         vstart = fp[nu->orderv - 1];
1247
1248         if (nu->flagv & CU_NURB_CYCLIC)
1249                 vend = fp[nu->pntsv + nu->orderv - 1];
1250         else
1251                 vend = fp[nu->pntsv];
1252         vstep = (vend - vstart) / ((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
1253
1254         len = KNOTSV(nu);
1255         basisv = (float *)MEM_malloc_arrayN(len * totv, sizeof(float), "makeNurbfaces3");
1256         jstart = (int *)MEM_malloc_arrayN(totv, sizeof(float), "makeNurbfaces4");
1257         jend = (int *)MEM_malloc_arrayN(totv, sizeof(float), "makeNurbfaces5");
1258
1259         /* precalculation of basisv and jstart, jend */
1260         if (nu->flagv & CU_NURB_CYCLIC)
1261                 cycl = nu->orderv - 1;
1262         else cycl = 0;
1263         v = vstart;
1264         basis = basisv;
1265         curv = totv;
1266         while (curv--) {
1267                 basisNurb(v, nu->orderv, nu->pntsv + cycl, nu->knotsv, basis, jstart + curv, jend + curv);
1268                 basis += KNOTSV(nu);
1269                 v += vstep;
1270         }
1271
1272         if (nu->flagu & CU_NURB_CYCLIC)
1273                 cycl = nu->orderu - 1;
1274         else
1275                 cycl = 0;
1276         in = coord_array;
1277         u = ustart;
1278         curu = totu;
1279         while (curu--) {
1280                 basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
1281
1282                 basis = basisv;
1283                 curv = totv;
1284                 while (curv--) {
1285                         jsta = jstart[curv];
1286                         jen = jend[curv];
1287
1288                         /* calculate sum */
1289                         sumdiv = 0.0;
1290                         fp = sum;
1291
1292                         for (j = jsta; j <= jen; j++) {
1293
1294                                 if (j >= nu->pntsv)
1295                                         jofs = (j - nu->pntsv);
1296                                 else
1297                                         jofs = j;
1298                                 bp = nu->bp + nu->pntsu * jofs + istart - 1;
1299
1300                                 for (i = istart; i <= iend; i++, fp++) {
1301                                         if (i >= nu->pntsu) {
1302                                                 iofs = i - nu->pntsu;
1303                                                 bp = nu->bp + nu->pntsu * jofs + iofs;
1304                                         }
1305                                         else
1306                                                 bp++;
1307
1308                                         if (ratcomp) {
1309                                                 *fp = basisu[i] * basis[j] * bp->vec[3];
1310                                                 sumdiv += *fp;
1311                                         }
1312                                         else
1313                                                 *fp = basisu[i] * basis[j];
1314                                 }
1315                         }
1316
1317                         if (ratcomp) {
1318                                 fp = sum;
1319                                 for (j = jsta; j <= jen; j++) {
1320                                         for (i = istart; i <= iend; i++, fp++) {
1321                                                 *fp /= sumdiv;
1322                                         }
1323                                 }
1324                         }
1325
1326                         zero_v3(in);
1327
1328                         /* one! (1.0) real point now */
1329                         fp = sum;
1330                         for (j = jsta; j <= jen; j++) {
1331
1332                                 if (j >= nu->pntsv)
1333                                         jofs = (j - nu->pntsv);
1334                                 else
1335                                         jofs = j;
1336                                 bp = nu->bp + nu->pntsu * jofs + istart - 1;
1337
1338                                 for (i = istart; i <= iend; i++, fp++) {
1339                                         if (i >= nu->pntsu) {
1340                                                 iofs = i - nu->pntsu;
1341                                                 bp = nu->bp + nu->pntsu * jofs + iofs;
1342                                         }
1343                                         else
1344                                                 bp++;
1345
1346                                         if (*fp != 0.0f) {
1347                                                 madd_v3_v3fl(in, bp->vec, *fp);
1348                                         }
1349                                 }
1350                         }
1351
1352                         in += 3;
1353                         basis += KNOTSV(nu);
1354                 }
1355                 u += ustep;
1356                 if (rowstride != 0) {
1357                         in = (float *)(((unsigned char *)in) + (rowstride - 3 * totv * sizeof(*in)));
1358                 }
1359         }
1360
1361         /* free */
1362         MEM_freeN(sum);
1363         MEM_freeN(basisu);
1364         MEM_freeN(basisv);
1365         MEM_freeN(jstart);
1366         MEM_freeN(jend);
1367 }
1368
1369 /**
1370  * \param coord_array: Has to be 3 * 4 * pntsu * resolu in size and zero-ed
1371  * \param tilt_array: set when non-NULL
1372  * \param radius_array: set when non-NULL
1373  */
1374 void BKE_nurb_makeCurve(
1375         const Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array,
1376         int resolu, int stride)
1377 {
1378         const float eps = 1e-6f;
1379         BPoint *bp;
1380         float u, ustart, uend, ustep, sumdiv;
1381         float *basisu, *sum, *fp;
1382         float *coord_fp = coord_array, *tilt_fp = tilt_array, *radius_fp = radius_array, *weight_fp = weight_array;
1383         int i, len, istart, iend, cycl;
1384
1385         if (nu->knotsu == NULL)
1386                 return;
1387         if (nu->orderu > nu->pntsu)
1388                 return;
1389         if (coord_array == NULL)
1390                 return;
1391
1392         /* allocate and initialize */
1393         len = nu->pntsu;
1394         if (len == 0)
1395                 return;
1396         sum = (float *)MEM_calloc_arrayN(len, sizeof(float), "makeNurbcurve1");
1397
1398         resolu = (resolu * SEGMENTSU(nu));
1399
1400         if (resolu == 0) {
1401                 MEM_freeN(sum);
1402                 return;
1403         }
1404
1405         fp = nu->knotsu;
1406         ustart = fp[nu->orderu - 1];
1407         if (nu->flagu & CU_NURB_CYCLIC)
1408                 uend = fp[nu->pntsu + nu->orderu - 1];
1409         else
1410                 uend = fp[nu->pntsu];
1411         ustep = (uend - ustart) / (resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
1412
1413         basisu = (float *)MEM_malloc_arrayN(KNOTSU(nu), sizeof(float), "makeNurbcurve3");
1414
1415         if (nu->flagu & CU_NURB_CYCLIC)
1416                 cycl = nu->orderu - 1;
1417         else
1418                 cycl = 0;
1419
1420         u = ustart;
1421         while (resolu--) {
1422                 basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
1423
1424                 /* calc sum */
1425                 sumdiv = 0.0;
1426                 fp = sum;
1427                 bp = nu->bp + istart - 1;
1428                 for (i = istart; i <= iend; i++, fp++) {
1429                         if (i >= nu->pntsu)
1430                                 bp = nu->bp + (i - nu->pntsu);
1431                         else
1432                                 bp++;
1433
1434                         *fp = basisu[i] * bp->vec[3];
1435                         sumdiv += *fp;
1436                 }
1437                 if ((sumdiv != 0.0f) && (sumdiv < 1.0f - eps || sumdiv > 1.0f + eps)) {
1438                         /* is normalizing needed? */
1439                         fp = sum;
1440                         for (i = istart; i <= iend; i++, fp++) {
1441                                 *fp /= sumdiv;
1442                         }
1443                 }
1444
1445                 zero_v3(coord_fp);
1446
1447                 /* one! (1.0) real point */
1448                 fp = sum;
1449                 bp = nu->bp + istart - 1;
1450                 for (i = istart; i <= iend; i++, fp++) {
1451                         if (i >= nu->pntsu)
1452                                 bp = nu->bp + (i - nu->pntsu);
1453                         else
1454                                 bp++;
1455
1456                         if (*fp != 0.0f) {
1457                                 madd_v3_v3fl(coord_fp, bp->vec, *fp);
1458
1459                                 if (tilt_fp)
1460                                         (*tilt_fp) += (*fp) * bp->alfa;
1461
1462                                 if (radius_fp)
1463                                         (*radius_fp) += (*fp) * bp->radius;
1464
1465                                 if (weight_fp)
1466                                         (*weight_fp) += (*fp) * bp->weight;
1467                         }
1468                 }
1469
1470                 coord_fp = POINTER_OFFSET(coord_fp, stride);
1471
1472                 if (tilt_fp)
1473                         tilt_fp = POINTER_OFFSET(tilt_fp, stride);
1474                 if (radius_fp)
1475                         radius_fp = POINTER_OFFSET(radius_fp, stride);
1476                 if (weight_fp)
1477                         weight_fp = POINTER_OFFSET(weight_fp, stride);
1478
1479                 u += ustep;
1480         }
1481
1482         /* free */
1483         MEM_freeN(sum);
1484         MEM_freeN(basisu);
1485 }
1486
1487 /**
1488  * Calculate the length for arrays filled in by #BKE_curve_calc_coords_axis.
1489  */
1490 unsigned int BKE_curve_calc_coords_axis_len(
1491         const unsigned int bezt_array_len, const unsigned int resolu,
1492         const bool is_cyclic, const bool use_cyclic_duplicate_endpoint)
1493 {
1494         const unsigned int segments = bezt_array_len - (is_cyclic ?  0 : 1);
1495         const unsigned int points_len = (segments * resolu) + (is_cyclic ? (use_cyclic_duplicate_endpoint) : 1);
1496         return points_len;
1497 }
1498
1499 /**
1500  * Calculate an array for the entire curve (cyclic or non-cyclic).
1501  * \note Call for each axis.
1502  *
1503  * \param use_cyclic_duplicate_endpoint: Duplicate values at the beginning & end of the array.
1504  */
1505 void BKE_curve_calc_coords_axis(
1506         const BezTriple *bezt_array, const unsigned int bezt_array_len, const unsigned int resolu,
1507         const bool is_cyclic, const bool use_cyclic_duplicate_endpoint,
1508         /* array params */
1509         const unsigned int axis, const unsigned int stride,
1510         float *r_points)
1511 {
1512         const unsigned int points_len = BKE_curve_calc_coords_axis_len(
1513                 bezt_array_len, resolu, is_cyclic, use_cyclic_duplicate_endpoint);
1514         float *r_points_offset = r_points;
1515
1516         const unsigned int resolu_stride = resolu * stride;
1517         const unsigned int bezt_array_last = bezt_array_len - 1;
1518
1519         for (unsigned int i = 0; i < bezt_array_last; i++) {
1520                 const BezTriple *bezt_curr = &bezt_array[i];
1521                 const BezTriple *bezt_next = &bezt_array[i + 1];
1522                 BKE_curve_forward_diff_bezier(
1523                         bezt_curr->vec[1][axis], bezt_curr->vec[2][axis],
1524                         bezt_next->vec[0][axis], bezt_next->vec[1][axis],
1525                         r_points_offset, (int)resolu, stride);
1526                 r_points_offset = POINTER_OFFSET(r_points_offset, resolu_stride);
1527         }
1528
1529         if (is_cyclic) {
1530                 const BezTriple *bezt_curr = &bezt_array[bezt_array_last];
1531                 const BezTriple *bezt_next = &bezt_array[0];
1532                 BKE_curve_forward_diff_bezier(
1533                         bezt_curr->vec[1][axis], bezt_curr->vec[2][axis],
1534                         bezt_next->vec[0][axis], bezt_next->vec[1][axis],
1535                         r_points_offset, (int)resolu, stride);
1536                 r_points_offset = POINTER_OFFSET(r_points_offset, resolu_stride);
1537                 if (use_cyclic_duplicate_endpoint) {
1538                         *r_points_offset = *r_points;
1539                         r_points_offset = POINTER_OFFSET(r_points_offset, stride);
1540                 }
1541         }
1542         else {
1543                 float *r_points_last = POINTER_OFFSET(r_points, bezt_array_last * resolu_stride);
1544                 *r_points_last = bezt_array[bezt_array_last].vec[1][axis];
1545                 r_points_offset = POINTER_OFFSET(r_points_offset, stride);
1546         }
1547
1548         BLI_assert(POINTER_OFFSET(r_points, points_len * stride) == r_points_offset);
1549         UNUSED_VARS_NDEBUG(points_len);
1550 }
1551
1552 /* forward differencing method for bezier curve */
1553 void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1554 {
1555         float rt0, rt1, rt2, rt3, f;
1556         int a;
1557
1558         f = (float)it;
1559         rt0 = q0;
1560         rt1 = 3.0f * (q1 - q0) / f;
1561         f *= f;
1562         rt2 = 3.0f * (q0 - 2.0f * q1 + q2) / f;
1563         f *= it;
1564         rt3 = (q3 - q0 + 3.0f * (q1 - q2)) / f;
1565
1566         q0 = rt0;
1567         q1 = rt1 + rt2 + rt3;
1568         q2 = 2 * rt2 + 6 * rt3;
1569         q3 = 6 * rt3;
1570
1571         for (a = 0; a <= it; a++) {
1572                 *p = q0;
1573                 p = POINTER_OFFSET(p, stride);
1574                 q0 += q1;
1575                 q1 += q2;
1576                 q2 += q3;
1577         }
1578 }
1579
1580 /* forward differencing method for first derivative of cubic bezier curve */
1581 void BKE_curve_forward_diff_tangent_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1582 {
1583         float rt0, rt1, rt2, f;
1584         int a;
1585
1586         f = 1.0f / (float)it;
1587
1588         rt0 = 3.0f * (q1 - q0);
1589         rt1 = f * (3.0f * (q3 - q0) + 9.0f * (q1 - q2));
1590         rt2 = 6.0f * (q0 + q2) - 12.0f * q1;
1591
1592         q0 = rt0;
1593         q1 = f * (rt1 + rt2);
1594         q2 = 2.0f * f * rt1;
1595
1596         for (a = 0; a <= it; a++) {
1597                 *p = q0;
1598                 p = POINTER_OFFSET(p, stride);
1599                 q0 += q1;
1600                 q1 += q2;
1601         }
1602 }
1603
1604 static void forward_diff_bezier_cotangent(const float p0[3], const float p1[3], const float p2[3], const float p3[3],
1605                                           float p[3], int it, int stride)
1606 {
1607         /* note that these are not perpendicular to the curve
1608          * they need to be rotated for this,
1609          *
1610          * This could also be optimized like BKE_curve_forward_diff_bezier */
1611         int a;
1612         for (a = 0; a <= it; a++) {
1613                 float t = (float)a / (float)it;
1614
1615                 int i;
1616                 for (i = 0; i < 3; i++) {
1617                         p[i] = (-6.0f  * t +  6.0f) * p0[i] +
1618                                ( 18.0f * t - 12.0f) * p1[i] +
1619                                (-18.0f * t +  6.0f) * p2[i] +
1620                                ( 6.0f  * t)         * p3[i];
1621                 }
1622                 normalize_v3(p);
1623                 p = POINTER_OFFSET(p, stride);
1624         }
1625 }
1626
1627 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1628
1629 float *BKE_curve_surf_make_orco(Object *ob)
1630 {
1631         /* Note: this function is used in convertblender only atm, so
1632          * suppose nonzero curve's render resolution should always be used */
1633         Curve *cu = ob->data;
1634         Nurb *nu;
1635         int a, b, tot = 0;
1636         int sizeu, sizev;
1637         int resolu, resolv;
1638         float *fp, *coord_array;
1639
1640         /* first calculate the size of the datablock */
1641         nu = cu->nurb.first;
1642         while (nu) {
1643                 /* as we want to avoid the seam in a cyclic nurbs
1644                  * texture wrapping, reserve extra orco data space to save these extra needed
1645                  * vertex based UV coordinates for the meridian vertices.
1646                  * Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1647                  * the renderface/vert construction.
1648                  *
1649                  * See also convertblender.c: init_render_surf()
1650                  */
1651
1652                 resolu = cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1653                 resolv = cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1654
1655                 sizeu = nu->pntsu * resolu;
1656                 sizev = nu->pntsv * resolv;
1657                 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1658                 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1659                 if (nu->pntsv > 1) tot += sizeu * sizev;
1660
1661                 nu = nu->next;
1662         }
1663         /* makeNurbfaces wants zeros */
1664         fp = coord_array = MEM_calloc_arrayN(tot, 3 * sizeof(float), "make_orco");
1665
1666         nu = cu->nurb.first;
1667         while (nu) {
1668                 resolu = cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1669                 resolv = cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1670
1671                 if (nu->pntsv > 1) {
1672                         sizeu = nu->pntsu * resolu;
1673                         sizev = nu->pntsv * resolv;
1674
1675                         if (nu->flagu & CU_NURB_CYCLIC)
1676                                 sizeu++;
1677                         if (nu->flagv & CU_NURB_CYCLIC)
1678                                 sizev++;
1679
1680                         if (cu->flag & CU_UV_ORCO) {
1681                                 for (b = 0; b < sizeu; b++) {
1682                                         for (a = 0; a < sizev; a++) {
1683
1684                                                 if (sizev < 2)
1685                                                         fp[0] = 0.0f;
1686                                                 else
1687                                                         fp[0] = -1.0f + 2.0f * ((float)a) / (sizev - 1);
1688
1689                                                 if (sizeu < 2)
1690                                                         fp[1] = 0.0f;
1691                                                 else
1692                                                         fp[1] = -1.0f + 2.0f * ((float)b) / (sizeu - 1);
1693
1694                                                 fp[2] = 0.0;
1695
1696                                                 fp += 3;
1697                                         }
1698                                 }
1699                         }
1700                         else {
1701                                 int size = (nu->pntsu * resolu) * (nu->pntsv * resolv) * 3 * sizeof(float);
1702                                 float *_tdata = MEM_mallocN(size, "temp data");
1703                                 float *tdata = _tdata;
1704
1705                                 BKE_nurb_makeFaces(nu, tdata, 0, resolu, resolv);
1706
1707                                 for (b = 0; b < sizeu; b++) {
1708                                         int use_b = b;
1709                                         if (b == sizeu - 1 && (nu->flagu & CU_NURB_CYCLIC))
1710                                                 use_b = false;
1711
1712                                         for (a = 0; a < sizev; a++) {
1713                                                 int use_a = a;
1714                                                 if (a == sizev - 1 && (nu->flagv & CU_NURB_CYCLIC))
1715                                                         use_a = false;
1716
1717                                                 tdata = _tdata + 3 * (use_b * (nu->pntsv * resolv) + use_a);
1718
1719                                                 fp[0] = (tdata[0] - cu->loc[0]) / cu->size[0];
1720                                                 fp[1] = (tdata[1] - cu->loc[1]) / cu->size[1];
1721                                                 fp[2] = (tdata[2] - cu->loc[2]) / cu->size[2];
1722                                                 fp += 3;
1723                                         }
1724                                 }
1725
1726                                 MEM_freeN(_tdata);
1727                         }
1728                 }
1729                 nu = nu->next;
1730         }
1731
1732         return coord_array;
1733 }
1734
1735
1736 /* NOTE: This routine is tied to the order of vertex
1737  * built by displist and as passed to the renderer.
1738  */
1739 float *BKE_curve_make_orco(Depsgraph *depsgraph, Scene *scene, Object *ob, int *r_numVerts)
1740 {
1741         Curve *cu = ob->data;
1742         DispList *dl;
1743         int u, v, numVerts;
1744         float *fp, *coord_array;
1745         ListBase disp = {NULL, NULL};
1746
1747         BKE_displist_make_curveTypes_forOrco(depsgraph, scene, ob, &disp);
1748
1749         numVerts = 0;
1750         for (dl = disp.first; dl; dl = dl->next) {
1751                 if (dl->type == DL_INDEX3) {
1752                         numVerts += dl->nr;
1753                 }
1754                 else if (dl->type == DL_SURF) {
1755                         /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only
1756                          * (closed circle beveling)
1757                          */
1758                         if (dl->flag & DL_CYCL_U) {
1759                                 if (dl->flag & DL_CYCL_V)
1760                                         numVerts += (dl->parts + 1) * (dl->nr + 1);
1761                                 else
1762                                         numVerts += dl->parts * (dl->nr + 1);
1763                         }
1764                         else if (dl->flag & DL_CYCL_V) {
1765                                 numVerts += (dl->parts + 1) * dl->nr;
1766                         }
1767                         else
1768                                 numVerts += dl->parts * dl->nr;
1769                 }
1770         }
1771
1772         if (r_numVerts)
1773                 *r_numVerts = numVerts;
1774
1775         fp = coord_array = MEM_malloc_arrayN(numVerts, 3 * sizeof(float), "cu_orco");
1776         for (dl = disp.first; dl; dl = dl->next) {
1777                 if (dl->type == DL_INDEX3) {
1778                         for (u = 0; u < dl->nr; u++, fp += 3) {
1779                                 if (cu->flag & CU_UV_ORCO) {
1780                                         fp[0] = 2.0f * u / (dl->nr - 1) - 1.0f;
1781                                         fp[1] = 0.0;
1782                                         fp[2] = 0.0;
1783                                 }
1784                                 else {
1785                                         copy_v3_v3(fp, &dl->verts[u * 3]);
1786
1787                                         fp[0] = (fp[0] - cu->loc[0]) / cu->size[0];
1788                                         fp[1] = (fp[1] - cu->loc[1]) / cu->size[1];
1789                                         fp[2] = (fp[2] - cu->loc[2]) / cu->size[2];
1790                                 }
1791                         }
1792                 }
1793                 else if (dl->type == DL_SURF) {
1794                         int sizeu = dl->nr, sizev = dl->parts;
1795
1796                         /* exception as handled in convertblender.c too */
1797                         if (dl->flag & DL_CYCL_U) {
1798                                 sizeu++;
1799                                 if (dl->flag & DL_CYCL_V)
1800                                         sizev++;
1801                         }
1802                         else if (dl->flag & DL_CYCL_V) {
1803                                 sizev++;
1804                         }
1805
1806                         for (u = 0; u < sizev; u++) {
1807                                 for (v = 0; v < sizeu; v++, fp += 3) {
1808                                         if (cu->flag & CU_UV_ORCO) {
1809                                                 fp[0] = 2.0f * u / (sizev - 1) - 1.0f;
1810                                                 fp[1] = 2.0f * v / (sizeu - 1) - 1.0f;
1811                                                 fp[2] = 0.0;
1812                                         }
1813                                         else {
1814                                                 const float *vert;
1815                                                 int realv = v % dl->nr;
1816                                                 int realu = u % dl->parts;
1817
1818                                                 vert = dl->verts + 3 * (dl->nr * realu + realv);
1819                                                 copy_v3_v3(fp, vert);
1820
1821                                                 fp[0] = (fp[0] - cu->loc[0]) / cu->size[0];
1822                                                 fp[1] = (fp[1] - cu->loc[1]) / cu->size[1];
1823                                                 fp[2] = (fp[2] - cu->loc[2]) / cu->size[2];
1824                                         }
1825                                 }
1826                         }
1827                 }
1828         }
1829
1830         BKE_displist_free(&disp);
1831
1832         return coord_array;
1833 }
1834
1835
1836 /* ***************** BEVEL ****************** */
1837
1838 void BKE_curve_bevel_make(
1839         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *disp,
1840         const bool for_render, const bool use_render_resolution)
1841 {
1842         DispList *dl, *dlnew;
1843         Curve *bevcu, *cu;
1844         float *fp, facx, facy, angle, dangle;
1845         int nr, a;
1846
1847         cu = ob->data;
1848         BLI_listbase_clear(disp);
1849
1850         /* if a font object is being edited, then do nothing */
1851 // XXX  if ( ob == obedit && ob->type == OB_FONT ) return;
1852
1853         if (cu->bevobj) {
1854                 if (cu->bevobj->type != OB_CURVE)
1855                         return;
1856
1857                 bevcu = cu->bevobj->data;
1858                 if (bevcu->ext1 == 0.0f && bevcu->ext2 == 0.0f) {
1859                         ListBase bevdisp = {NULL, NULL};
1860                         facx = cu->bevobj->size[0];
1861                         facy = cu->bevobj->size[1];
1862
1863                         if (for_render) {
1864                                 BKE_displist_make_curveTypes_forRender(depsgraph, scene, cu->bevobj, &bevdisp, NULL, false, use_render_resolution);
1865                                 dl = bevdisp.first;
1866                         }
1867                         else if (cu->bevobj->runtime.curve_cache) {
1868                                 dl = cu->bevobj->runtime.curve_cache->disp.first;
1869                         }
1870                         else {
1871                                 BLI_assert(cu->bevobj->runtime.curve_cache != NULL);
1872                                 dl = NULL;
1873                         }
1874
1875                         while (dl) {
1876                                 if (ELEM(dl->type, DL_POLY, DL_SEGM)) {
1877                                         dlnew = MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1878                                         *dlnew = *dl;
1879                                         dlnew->verts = MEM_malloc_arrayN(dl->parts * dl->nr, 3 * sizeof(float), "makebevelcurve1");
1880                                         memcpy(dlnew->verts, dl->verts, 3 * sizeof(float) * dl->parts * dl->nr);
1881
1882                                         if (dlnew->type == DL_SEGM)
1883                                                 dlnew->flag |= (DL_FRONT_CURVE | DL_BACK_CURVE);
1884
1885                                         BLI_addtail(disp, dlnew);
1886                                         fp = dlnew->verts;
1887                                         nr = dlnew->parts * dlnew->nr;
1888                                         while (nr--) {
1889                                                 fp[2] = fp[1] * facy;
1890                                                 fp[1] = -fp[0] * facx;
1891                                                 fp[0] = 0.0;
1892                                                 fp += 3;
1893                                         }
1894                                 }
1895                                 dl = dl->next;
1896                         }
1897
1898                         BKE_displist_free(&bevdisp);
1899                 }
1900         }
1901         else if (cu->ext1 == 0.0f && cu->ext2 == 0.0f) {
1902                 /* pass */
1903         }
1904         else if (cu->ext2 == 0.0f) {
1905                 dl = MEM_callocN(sizeof(DispList), "makebevelcurve2");
1906                 dl->verts = MEM_malloc_arrayN(2, sizeof(float[3]), "makebevelcurve2");
1907                 BLI_addtail(disp, dl);
1908                 dl->type = DL_SEGM;
1909                 dl->parts = 1;
1910                 dl->flag = DL_FRONT_CURVE | DL_BACK_CURVE;
1911                 dl->nr = 2;
1912
1913                 fp = dl->verts;
1914                 fp[0] = fp[1] = 0.0;
1915                 fp[2] = -cu->ext1;
1916                 fp[3] = fp[4] = 0.0;
1917                 fp[5] = cu->ext1;
1918         }
1919         else if ((cu->flag & (CU_FRONT | CU_BACK)) == 0 && cu->ext1 == 0.0f) {  /* we make a full round bevel in that case */
1920                 nr = 4 + 2 * cu->bevresol;
1921
1922                 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1923                 dl->verts = MEM_malloc_arrayN(nr, sizeof(float[3]), "makebevelcurve p1");
1924                 BLI_addtail(disp, dl);
1925                 dl->type = DL_POLY;
1926                 dl->parts = 1;
1927                 dl->flag = DL_BACK_CURVE;
1928                 dl->nr = nr;
1929
1930                 /* a circle */
1931                 fp = dl->verts;
1932                 dangle = (2.0f * (float)M_PI / (nr));
1933                 angle = -(nr - 1) * dangle;
1934
1935                 for (a = 0; a < nr; a++) {
1936                         fp[0] = 0.0;
1937                         fp[1] = (cosf(angle) * (cu->ext2));
1938                         fp[2] = (sinf(angle) * (cu->ext2)) - cu->ext1;
1939                         angle += dangle;
1940                         fp += 3;
1941                 }
1942         }
1943         else {
1944                 short dnr;
1945
1946                 /* bevel now in three parts, for proper vertex normals */
1947                 /* part 1, back */
1948
1949                 if ((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1950                         dnr = nr = 2 + cu->bevresol;
1951                         if ((cu->flag & (CU_FRONT | CU_BACK)) == 0) {
1952                                 nr = 3 + 2 * cu->bevresol;
1953                         }
1954                         dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1955                         dl->verts = MEM_malloc_arrayN(nr, sizeof(float[3]), "makebevelcurve p1");
1956                         BLI_addtail(disp, dl);
1957                         dl->type = DL_SEGM;
1958                         dl->parts = 1;
1959                         dl->flag = DL_BACK_CURVE;
1960                         dl->nr = nr;
1961
1962                         /* half a circle */
1963                         fp = dl->verts;
1964                         dangle = ((float)M_PI_2 / (dnr - 1));
1965                         angle = -(nr - 1) * dangle;
1966
1967                         for (a = 0; a < nr; a++) {
1968                                 fp[0] = 0.0;
1969                                 fp[1] = (float)(cosf(angle) * (cu->ext2));
1970                                 fp[2] = (float)(sinf(angle) * (cu->ext2)) - cu->ext1;
1971                                 angle += dangle;
1972                                 fp += 3;
1973                         }
1974                 }
1975
1976                 /* part 2, sidefaces */
1977                 if (cu->ext1 != 0.0f) {
1978                         nr = 2;
1979
1980                         dl = MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1981                         dl->verts = MEM_malloc_arrayN(nr, sizeof(float[3]), "makebevelcurve p2");
1982                         BLI_addtail(disp, dl);
1983                         dl->type = DL_SEGM;
1984                         dl->parts = 1;
1985                         dl->nr = nr;
1986
1987                         fp = dl->verts;
1988                         fp[1] = cu->ext2;
1989                         fp[2] = -cu->ext1;
1990                         fp[4] = cu->ext2;
1991                         fp[5] = cu->ext1;
1992
1993                         if ((cu->flag & (CU_FRONT | CU_BACK)) == 0) {
1994                                 dl = MEM_dupallocN(dl);
1995                                 dl->verts = MEM_dupallocN(dl->verts);
1996                                 BLI_addtail(disp, dl);
1997
1998                                 fp = dl->verts;
1999                                 fp[1] = -fp[1];
2000                                 fp[2] = -fp[2];
2001                                 fp[4] = -fp[4];
2002                                 fp[5] = -fp[5];
2003                         }
2004                 }
2005
2006                 /* part 3, front */
2007                 if ((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
2008                         dnr = nr = 2 + cu->bevresol;
2009                         if ((cu->flag & (CU_FRONT | CU_BACK)) == 0) {
2010                                 nr = 3 + 2 * cu->bevresol;
2011                         }
2012                         dl = MEM_callocN(sizeof(DispList), "makebevelcurve p3");
2013                         dl->verts = MEM_malloc_arrayN(nr, sizeof(float[3]), "makebevelcurve p3");
2014                         BLI_addtail(disp, dl);
2015                         dl->type = DL_SEGM;
2016                         dl->flag = DL_FRONT_CURVE;
2017                         dl->parts = 1;
2018                         dl->nr = nr;
2019
2020                         /* half a circle */
2021                         fp = dl->verts;
2022                         angle = 0.0;
2023                         dangle = ((float)M_PI_2 / (dnr - 1));
2024
2025                         for (a = 0; a < nr; a++) {
2026                                 fp[0] = 0.0;
2027                                 fp[1] = (float)(cosf(angle) * (cu->ext2));
2028                                 fp[2] = (float)(sinf(angle) * (cu->ext2)) + cu->ext1;
2029                                 angle += dangle;
2030                                 fp += 3;
2031                         }
2032                 }
2033         }
2034 }
2035
2036 static int cu_isectLL(const float v1[3], const float v2[3], const float v3[3], const float v4[3],
2037                       short cox, short coy,
2038                       float *lambda, float *mu, float vec[3])
2039 {
2040         /* return:
2041          * -1: collinear
2042          *  0: no intersection of segments
2043          *  1: exact intersection of segments
2044          *  2: cross-intersection of segments
2045          */
2046         float deler;
2047
2048         deler = (v1[cox] - v2[cox]) * (v3[coy] - v4[coy]) - (v3[cox] - v4[cox]) * (v1[coy] - v2[coy]);
2049         if (deler == 0.0f)
2050                 return -1;
2051
2052         *lambda = (v1[coy] - v3[coy]) * (v3[cox] - v4[cox]) - (v1[cox] - v3[cox]) * (v3[coy] - v4[coy]);
2053         *lambda = -(*lambda / deler);
2054
2055         deler = v3[coy] - v4[coy];
2056         if (deler == 0) {
2057                 deler = v3[cox] - v4[cox];
2058                 *mu = -(*lambda * (v2[cox] - v1[cox]) + v1[cox] - v3[cox]) / deler;
2059         }
2060         else {
2061                 *mu = -(*lambda * (v2[coy] - v1[coy]) + v1[coy] - v3[coy]) / deler;
2062         }
2063         vec[cox] = *lambda * (v2[cox] - v1[cox]) + v1[cox];
2064         vec[coy] = *lambda * (v2[coy] - v1[coy]) + v1[coy];
2065
2066         if (*lambda >= 0.0f && *lambda <= 1.0f && *mu >= 0.0f && *mu <= 1.0f) {
2067                 if (*lambda == 0.0f || *lambda == 1.0f || *mu == 0.0f || *mu == 1.0f)
2068                         return 1;
2069                 return 2;
2070         }
2071         return 0;
2072 }
2073
2074
2075 static bool bevelinside(BevList *bl1, BevList *bl2)
2076 {
2077         /* is bl2 INSIDE bl1 ? with left-right method and "lambda's" */
2078         /* returns '1' if correct hole  */
2079         BevPoint *bevp, *prevbevp;
2080         float min, max, vec[3], hvec1[3], hvec2[3], lab, mu;
2081         int nr, links = 0, rechts = 0, mode;
2082
2083         /* take first vertex of possible hole */
2084
2085         bevp = bl2->bevpoints;
2086         hvec1[0] = bevp->vec[0];
2087         hvec1[1] = bevp->vec[1];
2088         hvec1[2] = 0.0;
2089         copy_v3_v3(hvec2, hvec1);
2090         hvec2[0] += 1000;
2091
2092         /* test it with all edges of potential surrounding poly */
2093         /* count number of transitions left-right  */
2094
2095         bevp = bl1->bevpoints;
2096         nr = bl1->nr;
2097         prevbevp = bevp + (nr - 1);
2098
2099         while (nr--) {
2100                 min = prevbevp->vec[1];
2101                 max = bevp->vec[1];
2102                 if (max < min) {
2103                         min = max;
2104                         max = prevbevp->vec[1];
2105                 }
2106                 if (min != max) {
2107                         if (min <= hvec1[1] && max >= hvec1[1]) {
2108                                 /* there's a transition, calc intersection point */
2109                                 mode = cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
2110                                 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
2111                                  * only allow for one situation: we choose lab= 1.0
2112                                  */
2113                                 if (mode >= 0 && lab != 0.0f) {
2114                                         if (vec[0] < hvec1[0]) links++;
2115                                         else rechts++;
2116                                 }
2117                         }
2118                 }
2119                 prevbevp = bevp;
2120                 bevp++;
2121         }
2122
2123         return (links & 1) && (rechts & 1);
2124 }
2125
2126
2127 struct BevelSort {
2128         BevList *bl;
2129         float left;
2130         int dir;
2131 };
2132
2133 static int vergxcobev(const void *a1, const void *a2)
2134 {
2135         const struct BevelSort *x1 = a1, *x2 = a2;
2136
2137         if (x1->left > x2->left)
2138                 return 1;
2139         else if (x1->left < x2->left)
2140                 return -1;
2141         return 0;
2142 }
2143
2144 /* this function cannot be replaced with atan2, but why? */
2145
2146 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2,
2147                                float *r_sina, float *r_cosa)
2148 {
2149         float t01, t02, x3, y3;
2150
2151         t01 = sqrtf(x1 * x1 + y1 * y1);
2152         t02 = sqrtf(x2 * x2 + y2 * y2);
2153         if (t01 == 0.0f)
2154                 t01 = 1.0f;
2155         if (t02 == 0.0f)
2156                 t02 = 1.0f;
2157
2158         x1 /= t01;
2159         y1 /= t01;
2160         x2 /= t02;
2161         y2 /= t02;
2162
2163         t02 = x1 * x2 + y1 * y2;
2164         if (fabsf(t02) >= 1.0f)
2165                 t02 = M_PI_2;
2166         else
2167                 t02 = (saacos(t02)) / 2.0f;
2168
2169         t02 = sinf(t02);
2170         if (t02 == 0.0f)
2171                 t02 = 1.0f;
2172
2173         x3 = x1 - x2;
2174         y3 = y1 - y2;
2175         if (x3 == 0 && y3 == 0) {
2176                 x3 = y1;
2177                 y3 = -x1;
2178         }
2179         else {
2180                 t01 = sqrtf(x3 * x3 + y3 * y3);
2181                 x3 /= t01;
2182                 y3 /= t01;
2183         }
2184
2185         *r_sina = -y3 / t02;
2186         *r_cosa =  x3 / t02;
2187
2188 }
2189
2190 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array,
2191                          float *weight_array, int resolu, int stride)
2192 {
2193         BezTriple *pprev, *next, *last;
2194         float fac, dfac, t[4];
2195         int a;
2196
2197         if (tilt_array == NULL && radius_array == NULL)
2198                 return;
2199
2200         last = nu->bezt + (nu->pntsu - 1);
2201
2202         /* returns a point */
2203         if (prevbezt == nu->bezt) {
2204                 if (nu->flagu & CU_NURB_CYCLIC)
2205                         pprev = last;
2206                 else
2207                         pprev = prevbezt;
2208         }
2209         else
2210                 pprev = prevbezt - 1;
2211
2212         /* next point */
2213         if (bezt == last) {
2214                 if (nu->flagu & CU_NURB_CYCLIC)
2215                         next = nu->bezt;
2216                 else
2217                         next = bezt;
2218         }
2219         else
2220                 next = bezt + 1;
2221
2222         fac = 0.0;
2223         dfac = 1.0f / (float)resolu;
2224
2225         for (a = 0; a < resolu; a++, fac += dfac) {
2226                 if (tilt_array) {
2227                         if (nu->tilt_interp == KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
2228                                 *tilt_array = prevbezt->alfa +
2229                                         (bezt->alfa - prevbezt->alfa) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2230                         }
2231                         else {
2232                                 key_curve_position_weights(fac, t, nu->tilt_interp);
2233                                 *tilt_array = t[0] * pprev->alfa + t[1] * prevbezt->alfa + t[2] * bezt->alfa + t[3] * next->alfa;
2234                         }
2235
2236                         tilt_array = POINTER_OFFSET(tilt_array, stride);
2237                 }
2238
2239                 if (radius_array) {
2240                         if (nu->radius_interp == KEY_CU_EASE) {
2241                                 /* Support 2.47 ease interp
2242                                  * Note! - this only takes the 2 points into account,
2243                                  * giving much more localized results to changes in radius, sometimes you want that */
2244                                 *radius_array = prevbezt->radius +
2245                                         (bezt->radius - prevbezt->radius) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2246                         }
2247                         else {
2248
2249                                 /* reuse interpolation from tilt if we can */
2250                                 if (tilt_array == NULL || nu->tilt_interp != nu->radius_interp) {
2251                                         key_curve_position_weights(fac, t, nu->radius_interp);
2252                                 }
2253                                 *radius_array =
2254                                         t[0] * pprev->radius + t[1] * prevbezt->radius +
2255                                         t[2] * bezt->radius + t[3] * next->radius;
2256                         }
2257
2258                         radius_array = POINTER_OFFSET(radius_array, stride);
2259                 }
2260
2261                 if (weight_array) {
2262                         /* basic interpolation for now, could copy tilt interp too  */
2263                         *weight_array =
2264                                 prevbezt->weight +
2265                                 (bezt->weight - prevbezt->weight) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2266
2267                         weight_array = POINTER_OFFSET(weight_array, stride);
2268                 }
2269         }
2270 }
2271
2272 /* make_bevel_list_3D_* funcs, at a minimum these must
2273  * fill in the bezp->quat and bezp->dir values */
2274
2275 /* utility for make_bevel_list_3D_* funcs */
2276 static void bevel_list_calc_bisect(BevList *bl)
2277 {
2278         BevPoint *bevp2, *bevp1, *bevp0;
2279         int nr;
2280         bool is_cyclic = bl->poly != -1;
2281
2282         if (is_cyclic) {
2283                 bevp2 = bl->bevpoints;
2284                 bevp1 = bevp2 + (bl->nr - 1);
2285                 bevp0 = bevp1 - 1;
2286                 nr = bl->nr;
2287         }
2288         else {
2289                 /* If spline is not cyclic, direction of first and
2290                  * last bevel points matches direction of CV handle.
2291                  *
2292                  * This is getting calculated earlier when we know
2293                  * CV's handles and here we might simply skip evaluation
2294                  * of direction for this guys.
2295                  */
2296
2297                 bevp0 = bl->bevpoints;
2298                 bevp1 = bevp0 + 1;
2299                 bevp2 = bevp1 + 1;
2300
2301                 nr = bl->nr - 2;
2302         }
2303
2304         while (nr--) {
2305                 /* totally simple */
2306                 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2307
2308                 bevp0 = bevp1;
2309                 bevp1 = bevp2;
2310                 bevp2++;
2311         }
2312 }
2313 static void bevel_list_flip_tangents(BevList *bl)
2314 {
2315         BevPoint *bevp2, *bevp1, *bevp0;
2316         int nr;
2317
2318         bevp2 = bl->bevpoints;
2319         bevp1 = bevp2 + (bl->nr - 1);
2320         bevp0 = bevp1 - 1;
2321
2322         nr = bl->nr;
2323         while (nr--) {
2324                 if (angle_normalized_v3v3(bevp0->tan, bevp1->tan) > DEG2RADF(90.0f))
2325                         negate_v3(bevp1->tan);
2326
2327                 bevp0 = bevp1;
2328                 bevp1 = bevp2;
2329                 bevp2++;
2330         }
2331 }
2332 /* apply user tilt */
2333 static void bevel_list_apply_tilt(BevList *bl)
2334 {
2335         BevPoint *bevp2, *bevp1;
2336         int nr;
2337         float q[4];
2338
2339         bevp2 = bl->bevpoints;
2340         bevp1 = bevp2 + (bl->nr - 1);
2341
2342         nr = bl->nr;
2343         while (nr--) {
2344                 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2345                 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2346                 normalize_qt(bevp1->quat);
2347
2348                 bevp1 = bevp2;
2349                 bevp2++;
2350         }
2351 }
2352 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
2353 static void bevel_list_smooth(BevList *bl, int smooth_iter)
2354 {
2355         BevPoint *bevp2, *bevp1, *bevp0;
2356         int nr;
2357
2358         float q[4];
2359         float bevp0_quat[4];
2360         int a;
2361
2362         for (a = 0; a < smooth_iter; a++) {
2363                 bevp2 = bl->bevpoints;
2364                 bevp1 = bevp2 + (bl->nr - 1);
2365                 bevp0 = bevp1 - 1;
2366
2367                 nr = bl->nr;
2368
2369                 if (bl->poly == -1) { /* check its not cyclic */
2370                         /* skip the first point */
2371                         /* bevp0 = bevp1; */
2372                         bevp1 = bevp2;
2373                         bevp2++;
2374                         nr--;
2375
2376                         bevp0 = bevp1;
2377                         bevp1 = bevp2;
2378                         bevp2++;
2379                         nr--;
2380                 }
2381
2382                 copy_qt_qt(bevp0_quat, bevp0->quat);
2383
2384                 while (nr--) {
2385                         /* interpolate quats */
2386                         float zaxis[3] = {0, 0, 1}, cross[3], q2[4];
2387                         interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
2388                         normalize_qt(q);
2389
2390                         mul_qt_v3(q, zaxis);
2391                         cross_v3_v3v3(cross, zaxis, bevp1->dir);
2392                         axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
2393                         normalize_qt(q2);
2394
2395                         copy_qt_qt(bevp0_quat, bevp1->quat);
2396                         mul_qt_qtqt(q, q2, q);
2397                         interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
2398                         normalize_qt(bevp1->quat);
2399
2400                         /* bevp0 = bevp1; */ /* UNUSED */
2401                         bevp1 = bevp2;
2402                         bevp2++;
2403                 }
2404         }
2405 }
2406
2407 static void make_bevel_list_3D_zup(BevList *bl)
2408 {
2409         BevPoint *bevp = bl->bevpoints;
2410         int nr = bl->nr;
2411
2412         bevel_list_calc_bisect(bl);
2413
2414         while (nr--) {
2415                 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2416                 bevp++;
2417         }
2418 }
2419
2420 static void minimum_twist_between_two_points(BevPoint *current_point, BevPoint *previous_point)
2421 {
2422         float angle = angle_normalized_v3v3(previous_point->dir, current_point->dir);
2423         float q[4];
2424
2425         if (angle > 0.0f) { /* otherwise we can keep as is */
2426                 float cross_tmp[3];
2427                 cross_v3_v3v3(cross_tmp, previous_point->dir, current_point->dir);
2428                 axis_angle_to_quat(q, cross_tmp, angle);
2429                 mul_qt_qtqt(current_point->quat, q, previous_point->quat);
2430         }
2431         else {
2432                 copy_qt_qt(current_point->quat, previous_point->quat);
2433         }
2434 }
2435
2436 static void make_bevel_list_3D_minimum_twist(BevList *bl)
2437 {
2438         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
2439         int nr;
2440         float q[4];
2441
2442         bevel_list_calc_bisect(bl);
2443
2444         bevp2 = bl->bevpoints;
2445         bevp1 = bevp2 + (bl->nr - 1);
2446         bevp0 = bevp1 - 1;
2447
2448         nr = bl->nr;
2449         while (nr--) {
2450
2451                 if (nr + 4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
2452                         vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2453                 }
2454                 else {
2455                         minimum_twist_between_two_points(bevp1, bevp0);
2456                 }
2457
2458                 bevp0 = bevp1;
2459                 bevp1 = bevp2;
2460                 bevp2++;
2461         }
2462
2463         if (bl->poly != -1) { /* check for cyclic */
2464
2465                 /* Need to correct for the start/end points not matching
2466                  * do this by calculating the tilt angle difference, then apply
2467                  * the rotation gradually over the entire curve
2468                  *
2469                  * note that the split is between last and second last, rather than first/last as youd expect.
2470                  *
2471                  * real order is like this
2472                  * 0,1,2,3,4 --> 1,2,3,4,0
2473                  *
2474                  * this is why we compare last with second last
2475                  * */
2476                 float vec_1[3] = {0, 1, 0}, vec_2[3] = {0, 1, 0}, angle, ang_fac, cross_tmp[3];
2477
2478                 BevPoint *bevp_first;
2479                 BevPoint *bevp_last;
2480
2481
2482                 bevp_first = bl->bevpoints;
2483                 bevp_first += bl->nr - 1;
2484                 bevp_last = bevp_first;
2485                 bevp_last--;
2486
2487                 /* quats and vec's are normalized, should not need to re-normalize */
2488                 mul_qt_v3(bevp_first->quat, vec_1);
2489                 mul_qt_v3(bevp_last->quat, vec_2);
2490                 normalize_v3(vec_1);
2491                 normalize_v3(vec_2);
2492
2493                 /* align the vector, can avoid this and it looks 98% OK but
2494                  * better to align the angle quat roll's before comparing */
2495                 {
2496                         cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
2497                         angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
2498                         axis_angle_to_quat(q, cross_tmp, angle);
2499                         mul_qt_v3(q, vec_2);
2500                 }
2501
2502                 angle = angle_normalized_v3v3(vec_1, vec_2);
2503
2504                 /* flip rotation if needs be */
2505                 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
2506                 normalize_v3(cross_tmp);
2507                 if (angle_normalized_v3v3(bevp_first->dir, cross_tmp) < DEG2RADF(90.0f))
2508                         angle = -angle;
2509
2510                 bevp2 = bl->bevpoints;
2511                 bevp1 = bevp2 + (bl->nr - 1);
2512                 bevp0 = bevp1 - 1;
2513
2514                 nr = bl->nr;
2515                 while (nr--) {
2516                         ang_fac = angle * (1.0f - ((float)nr / bl->nr)); /* also works */
2517
2518                         axis_angle_to_quat(q, bevp1->dir, ang_fac);
2519                         mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2520
2521                         bevp0 = bevp1;
2522                         bevp1 = bevp2;
2523                         bevp2++;
2524                 }
2525         }
2526         else {
2527                 /* Need to correct quat for the first/last point,
2528                  * this is so because previously it was only calculated
2529                  * using it's own direction, which might not correspond
2530                  * the twist of neighbor point.
2531                  */
2532                 bevp1 = bl->bevpoints;
2533                 bevp0 = bevp1 + 1;
2534                 minimum_twist_between_two_points(bevp1, bevp0);
2535
2536                 bevp2 = bl->bevpoints;
2537                 bevp1 = bevp2 + (bl->nr - 1);
2538                 bevp0 = bevp1 - 1;
2539                 minimum_twist_between_two_points(bevp1, bevp0);
2540         }
2541 }
2542
2543 static void make_bevel_list_3D_tangent(BevList *bl)
2544 {
2545         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
2546         int nr;
2547
2548         float bevp0_tan[3];
2549
2550         bevel_list_calc_bisect(bl);
2551         bevel_list_flip_tangents(bl);
2552
2553         /* correct the tangents */
2554         bevp2 = bl->bevpoints;
2555         bevp1 = bevp2 + (bl->nr - 1);
2556         bevp0 = bevp1 - 1;
2557
2558         nr = bl->nr;
2559         while (nr--) {
2560                 float cross_tmp[3];
2561                 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2562                 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
2563                 normalize_v3(bevp1->tan);
2564
2565                 bevp0 = bevp1;
2566                 bevp1 = bevp2;
2567                 bevp2++;
2568         }
2569
2570
2571         /* now for the real twist calc */
2572         bevp2 = bl->bevpoints;
2573         bevp1 = bevp2 + (bl->nr - 1);
2574         bevp0 = bevp1 - 1;
2575
2576         copy_v3_v3(bevp0_tan, bevp0->tan);
2577
2578         nr = bl->nr;
2579         while (nr--) {
2580                 /* make perpendicular, modify tan in place, is ok */
2581                 float cross_tmp[3];
2582                 float zero[3] = {0, 0, 0};
2583
2584                 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2585                 normalize_v3(cross_tmp);
2586                 tri_to_quat(bevp1->quat, zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
2587
2588                 /* bevp0 = bevp1; */ /* UNUSED */
2589                 bevp1 = bevp2;
2590                 bevp2++;
2591         }
2592 }
2593
2594 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
2595 {
2596         switch (twist_mode) {
2597                 case CU_TWIST_TANGENT:
2598                         make_bevel_list_3D_tangent(bl);
2599                         break;
2600                 case CU_TWIST_MINIMUM:
2601                         make_bevel_list_3D_minimum_twist(bl);
2602                         break;
2603                 default: /* CU_TWIST_Z_UP default, pre 2.49c */
2604                         make_bevel_list_3D_zup(bl);
2605                         break;
2606         }
2607
2608         if (smooth_iter)
2609                 bevel_list_smooth(bl, smooth_iter);
2610
2611         bevel_list_apply_tilt(bl);
2612 }
2613
2614 /* only for 2 points */
2615 static void make_bevel_list_segment_3D(BevList *bl)
2616 {
2617         float q[4];
2618
2619         BevPoint *bevp2 = bl->bevpoints;
2620         BevPoint *bevp1 = bevp2 + 1;
2621
2622         /* simple quat/dir */
2623         sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2624         normalize_v3(bevp1->dir);
2625
2626         vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2627
2628         axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2629         mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2630         normalize_qt(bevp1->quat);
2631         copy_v3_v3(bevp2->dir, bevp1->dir);
2632         copy_qt_qt(bevp2->quat, bevp1->quat);
2633 }
2634
2635 /* only for 2 points */
2636 static void make_bevel_list_segment_2D(BevList *bl)
2637 {
2638         BevPoint *bevp2 = bl->bevpoints;
2639         BevPoint *bevp1 = bevp2 + 1;
2640
2641         const float x1 = bevp1->vec[0] - bevp2->vec[0];
2642         const float y1 = bevp1->vec[1] - bevp2->vec[1];
2643
2644         calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2645         bevp2->sina = bevp1->sina;
2646         bevp2->cosa = bevp1->cosa;
2647
2648         /* fill in dir & quat */
2649         make_bevel_list_segment_3D(bl);
2650 }
2651
2652 static void make_bevel_list_2D(BevList *bl)
2653 {
2654         /* note: bevp->dir and bevp->quat are not needed for beveling but are
2655          * used when making a path from a 2D curve, therefore they need to be set - Campbell */
2656
2657         BevPoint *bevp0, *bevp1, *bevp2;
2658         int nr;
2659
2660         if (bl->poly != -1) {
2661                 bevp2 = bl->bevpoints;
2662                 bevp1 = bevp2 + (bl->nr - 1);
2663                 bevp0 = bevp1 - 1;
2664                 nr = bl->nr;
2665         }
2666         else {
2667                 bevp0 = bl->bevpoints;
2668                 bevp1 = bevp0 + 1;
2669                 bevp2 = bevp1 + 1;
2670
2671                 nr = bl->nr - 2;
2672         }
2673
2674         while (nr--) {
2675                 const float x1 = bevp1->vec[0] - bevp0->vec[0];
2676                 const float x2 = bevp1->vec[0] - bevp2->vec[0];
2677                 const float y1 = bevp1->vec[1] - bevp0->vec[1];
2678                 const float y2 = bevp1->vec[1] - bevp2->vec[1];
2679
2680                 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2681
2682                 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2683                  * no need for tricky tilt calculation as with 3D curves */
2684                 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2685                 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2686                 /* done with inline make_bevel_list_3D_zup */
2687
2688                 bevp0 = bevp1;
2689                 bevp1 = bevp2;
2690                 bevp2++;
2691         }
2692
2693         /* correct non-cyclic cases */
2694         if (bl->poly == -1) {
2695                 BevPoint *bevp;
2696                 float angle;
2697
2698                 /* first */
2699                 bevp = bl->bevpoints;
2700                 angle = atan2f(bevp->dir[0], bevp->dir[1]) - (float)M_PI_2;
2701                 bevp->sina = sinf(angle);
2702                 bevp->cosa = cosf(angle);
2703                 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2704
2705                 /* last */
2706                 bevp = bl->bevpoints;
2707                 bevp += (bl->nr - 1);
2708                 angle = atan2f(bevp->dir[0], bevp->dir[1]) - (float)M_PI_2;
2709                 bevp->sina = sinf(angle);
2710                 bevp->cosa = cosf(angle);
2711                 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2712         }
2713 }
2714
2715 static void bevlist_firstlast_direction_calc_from_bpoint(Nurb *nu, BevList *bl)
2716 {
2717         if (nu->pntsu > 1) {
2718                 BPoint *first_bp = nu->bp, *last_bp = nu->bp + (nu->pntsu - 1);
2719                 BevPoint *first_bevp, *last_bevp;
2720
2721                 first_bevp = bl->bevpoints;
2722                 last_bevp = first_bevp + (bl->nr - 1);
2723
2724                 sub_v3_v3v3(first_bevp->dir, (first_bp + 1)->vec, first_bp->vec);
2725                 normalize_v3(first_bevp->dir);
2726
2727                 sub_v3_v3v3(last_bevp->dir, last_bp->vec, (last_bp - 1)->vec);
2728                 normalize_v3(last_bevp->dir);
2729         }
2730 }
2731
2732 void BKE_curve_bevelList_free(ListBase *bev)
2733 {
2734         BevList *bl, *blnext;
2735         for (bl = bev->first; bl != NULL; bl = blnext) {
2736                 blnext = bl->next;
2737                 if (bl->seglen != NULL) {
2738                         MEM_freeN(bl->seglen);
2739                 }
2740                 if (bl->segbevcount != NULL) {
2741                         MEM_freeN(bl->segbevcount);
2742                 }
2743                 if (bl->bevpoints != NULL) {
2744                         MEM_freeN(bl->bevpoints);
2745                 }
2746                 MEM_freeN(bl);
2747         }
2748
2749         BLI_listbase_clear(bev);
2750 }
2751
2752 void BKE_curve_bevelList_make(Object *ob, ListBase *nurbs, bool for_render)
2753 {
2754         /*
2755          * - convert all curves to polys, with indication of resol and flags for double-vertices
2756          * - possibly; do a smart vertice removal (in case Nurb)
2757          * - separate in individual blocks with BoundBox
2758          * - AutoHole detection
2759          */
2760
2761         /* this function needs an object, because of tflag and upflag */
2762         Curve *cu = ob->data;
2763         Nurb *nu;
2764         BezTriple *bezt, *prevbezt;
2765         BPoint *bp;
2766         BevList *bl, *blnew, *blnext;
2767         BevPoint *bevp2, *bevp1 = NULL, *bevp0;
2768         const float treshold = 0.00001f;
2769         float min, inp;
2770         float *seglen = NULL;
2771         struct BevelSort *sortdata, *sd, *sd1;
2772         int a, b, nr, poly, resolu = 0, len = 0, segcount;
2773         int *segbevcount;
2774         bool do_tilt, do_radius, do_weight;
2775         bool is_editmode = false;
2776         ListBase *bev;
2777
2778         /* segbevcount alsp requires seglen. */
2779         const bool need_seglen =
2780                 ELEM(cu->bevfac1_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE) ||
2781                 ELEM(cu->bevfac2_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE);
2782
2783
2784         bev = &ob->runtime.curve_cache->bev;
2785
2786         /* do we need to calculate the radius for each point? */
2787         /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2788
2789         /* STEP 1: MAKE POLYS  */
2790
2791         BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
2792         nu = nurbs->first;
2793         if (cu->editnurb && ob->type != OB_FONT) {
2794                 is_editmode = 1;
2795         }
2796
2797         for (; nu; nu = nu->next) {
2798
2799                 if (nu->hide && is_editmode)
2800                         continue;
2801
2802                 /* check if we will calculate tilt data */
2803                 do_tilt = CU_DO_TILT(cu, nu);
2804                 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2805                 do_weight = true;
2806
2807                 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2808                  * enforced in the UI but can go wrong possibly */
2809                 if (!BKE_nurb_check_valid_u(nu)) {
2810                         bl = MEM_callocN(sizeof(BevList), "makeBevelList1");
2811                         bl->bevpoints = MEM_calloc_arrayN(1, sizeof(BevPoint), "makeBevelPoints1");
2812                         BLI_addtail(bev, bl);
2813                         bl->nr = 0;
2814                         bl->charidx = nu->charidx;
2815                 }
2816                 else {
2817                         BevPoint *bevp;
2818
2819                         if (for_render && cu->resolu_ren != 0)
2820                                 resolu = cu->resolu_ren;
2821                         else
2822                                 resolu = nu->resolu;
2823
2824                         segcount = SEGMENTSU(nu);
2825
2826                         if (nu->type == CU_POLY) {
2827                                 len = nu->pntsu;
2828                                 bl = MEM_callocN(sizeof(BevList), "makeBevelList2");
2829                                 bl->bevpoints = MEM_calloc_arrayN(len, sizeof(BevPoint), "makeBevelPoints2");
2830                                 if (need_seglen && (nu->flagu & CU_NURB_CYCLIC) == 0) {
2831                                         bl->seglen = MEM_malloc_arrayN(segcount, sizeof(float), "makeBevelList2_seglen");
2832                                         bl->segbevcount = MEM_malloc_arrayN(segcount, sizeof(int), "makeBevelList2_segbevcount");
2833                                 }
2834                                 BLI_addtail(bev, bl);
2835
2836                                 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2837                                 bl->nr = len;
2838                                 bl->dupe_nr = 0;
2839                                 bl->charidx = nu->charidx;
2840                                 bevp = bl->bevpoints;
2841                                 bevp->offset = 0;
2842                                 bp = nu->bp;
2843                                 seglen = bl->seglen;
2844                                 segbevcount = bl->segbevcount;
2845
2846                                 while (len--) {
2847                                         copy_v3_v3(bevp->vec, bp->vec);
2848                                         bevp->alfa = bp->alfa;
2849                                         bevp->radius = bp->radius;
2850                                         bevp->weight = bp->weight;
2851                                         bevp->split_tag = true;
2852                                         bp++;
2853                                         if (seglen != NULL && len != 0) {
2854                                                 *seglen = len_v3v3(bevp->vec, bp->vec);
2855                                                 bevp++;
2856                                                 bevp->offset = *seglen;
2857                                                 if (*seglen > treshold) *segbevcount = 1;
2858                                                 else *segbevcount = 0;
2859                                                 seglen++;
2860                                                 segbevcount++;
2861                                         }
2862                                         else {
2863                                                 bevp++;
2864                                         }
2865                                 }
2866
2867                                 if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
2868                                         bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
2869                                 }
2870                         }
2871                         else if (nu->type == CU_BEZIER) {
2872                                 /* in case last point is not cyclic */
2873                                 len = segcount * resolu + 1;
2874
2875                                 bl = MEM_callocN(sizeof(BevList), "makeBevelBPoints");
2876                                 bl->bevpoints = MEM_calloc_arrayN(len, sizeof(BevPoint), "makeBevelBPointsPoints");
2877                                 if (need_seglen && (nu->flagu & CU_NURB_CYCLIC) == 0) {
2878                                         bl->seglen = MEM_malloc_arrayN(segcount, sizeof(float), "makeBevelBPoints_seglen");
2879                                         bl->segbevcount = MEM_malloc_arrayN(segcount, sizeof(int), "makeBevelBPoints_segbevcount");
2880                                 }
2881                                 BLI_addtail(bev, bl);
2882
2883                                 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2884                                 bl->charidx = nu->charidx;
2885
2886                                 bevp = bl->bevpoints;
2887                                 seglen = bl->seglen;
2888                                 segbevcount = bl->segbevcount;
2889
2890                                 bevp->offset = 0;
2891                                 if (seglen != NULL) {
2892                                         *seglen = 0;
2893                                         *segbevcount = 0;
2894                                 }
2895
2896                                 a = nu->pntsu - 1;
2897                                 bezt = nu->bezt;
2898                                 if (nu->flagu & CU_NURB_CYCLIC) {
2899                                         a++;
2900                                         prevbezt = nu->bezt + (nu->pntsu - 1);
2901                                 }
2902                                 else {
2903                                         prevbezt = bezt;
2904                                         bezt++;
2905                                 }
2906
2907                                 sub_v3_v3v3(bevp->dir, prevbezt->vec[2], prevbezt->vec[1]);
2908                                 normalize_v3(bevp->dir);
2909
2910                                 BLI_assert(segcount >= a);
2911
2912                                 while (a--) {
2913                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
2914
2915                                                 copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2916                                                 bevp->alfa = prevbezt->alfa;
2917                                                 bevp->radius = prevbezt->radius;
2918                                                 bevp->weight = prevbezt->weight;
2919                                                 bevp->split_tag = true;
2920                                                 bevp->dupe_tag = false;
2921                                                 bevp++;
2922                                                 bl->nr++;
2923                                                 bl->dupe_nr = 1;
2924                                                 if (seglen != NULL) {
2925                                                         *seglen = len_v3v3(prevbezt->vec[1], bezt->vec[1]);
2926                                                         bevp->offset = *seglen;
2927                                                         seglen++;
2928                                                         /* match segbevcount to the cleaned up bevel lists (see STEP 2) */
2929                                                         if (bevp->offset > treshold) *segbevcount = 1;
2930                                                         segbevcount++;
2931                                                 }
2932                                         }
2933                                         else {
2934                                                 /* always do all three, to prevent data hanging around */
2935                                                 int j;
2936
2937                                                 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2938                                                 for (j = 0; j < 3; j++) {
2939                                                         BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],  prevbezt->vec[2][j],
2940                                                                                       bezt->vec[0][j],      bezt->vec[1][j],
2941                                                                                       &(bevp->vec[j]), resolu, sizeof(BevPoint));
2942                                                 }
2943
2944                                                 /* if both arrays are NULL do nothiong */
2945                                                 alfa_bezpart(prevbezt, bezt, nu,
2946                                                              do_tilt    ? &bevp->alfa : NULL,
2947                                                              do_radius  ? &bevp->radius : NULL,
2948                                                              do_weight  ? &bevp->weight : NULL,
2949                                                              resolu, sizeof(BevPoint));
2950
2951
2952                                                 if (cu->twist_mode == CU_TWIST_TANGENT) {
2953                                                         forward_diff_bezier_cotangent(prevbezt->vec[1], prevbezt->vec[2],
2954                                                                                       bezt->vec[0],     bezt->vec[1],
2955                                                                                       bevp->tan, resolu, sizeof(BevPoint));
2956                                                 }
2957
2958                                                 /* indicate with handlecodes double points */
2959                                                 if (prevbezt->h1 == prevbezt->h2) {
2960                                                         if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
2961                                                                 bevp->split_tag = true;
2962                                                 }
2963                                                 else {
2964                                                         if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
2965                                                                 bevp->split_tag = true;
2966                                                         else if (prevbezt->h2 == 0 || prevbezt->h2 == HD_VECT)
2967                                                                 bevp->split_tag = true;
2968                                                 }
2969
2970                                                 /* seglen */
2971                                                 if (seglen != NULL) {
2972                                                         *seglen = 0;
2973                                                         *segbevcount = 0;
2974                                                         for (j = 0; j < resolu; j++) {
2975                                                                 bevp0 = bevp;
2976                                                                 bevp++;
2977                                                                 bevp->offset = len_v3v3(bevp0->vec, bevp->vec);
2978                                                                 /* match seglen and segbevcount to the cleaned up bevel lists (see STEP 2) */
2979                                                                 if (bevp->offset > treshold) {
2980                                                                         *seglen += bevp->offset;
2981                                                                         *segbevcount += 1;
2982                                                                 }
2983                                                         }
2984                                                         seglen++;
2985                                                         segbevcount++;
2986                                                 }
2987                                                 else {
2988                                                         bevp += resolu;
2989                                                 }
2990                                                 bl->nr += resolu;
2991                                         }
2992                                         prevbezt = bezt;
2993                                         bezt++;
2994                                 }
2995
2996                                 if ((nu->flagu & CU_NURB_CYCLIC) == 0) {      /* not cyclic: endpoint */
2997                                         copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2998                                         bevp->alfa = prevbezt->alfa;
2999                                         bevp->radius = prevbezt->radius;
3000                                         bevp->weight = prevbezt->weight;
3001
3002                                         sub_v3_v3v3(bevp->dir, prevbezt->vec[1], prevbezt->vec[0]);
3003                                         normalize_v3(bevp->dir);
3004
3005                                         bl->nr++;
3006                                 }
3007                         }
3008                         else if (nu->type == CU_NURBS) {
3009                                 if (nu->pntsv == 1) {
3010                                         len = (resolu * segcount);
3011
3012                                         bl = MEM_callocN(sizeof(BevList), "makeBevelList3");
3013                                         bl->bevpoints = MEM_calloc_arrayN(len, sizeof(BevPoint), "makeBevelPoints3");
3014                                         if (need_seglen && (nu->flagu & CU_NURB_CYCLIC) == 0) {
3015                                                 bl->seglen = MEM_malloc_arrayN(segcount, sizeof(float), "makeBevelList3_seglen");
3016                                                 bl->segbevcount = MEM_malloc_arrayN(segcount, sizeof(int), "makeBevelList3_segbevcount");
3017                                         }
3018                                         BLI_addtail(bev, bl);
3019                                         bl->nr = len;
3020                                         bl->dupe_nr = 0;
3021                                         bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
3022                                         bl->charidx = nu->charidx;
3023
3024                                         bevp = bl->bevpoints;
3025                                         seglen = bl->seglen;
3026                                         segbevcount = bl->segbevcount;
3027
3028                                         BKE_nurb_makeCurve(nu, &bevp->vec[0],
3029                                                            do_tilt      ? &bevp->alfa : NULL,
3030                                                            do_radius    ? &bevp->radius : NULL,
3031                                                            do_weight    ? &bevp->weight : NULL,
3032                                                            resolu, sizeof(BevPoint));
3033
3034                                         /* match seglen and segbevcount to the cleaned up bevel lists (see STEP 2) */
3035                                         if (seglen != NULL) {
3036                                                 nr = segcount;
3037                                                 bevp0 = bevp;
3038                                                 bevp++;
3039                                                 while (nr) {
3040                                                         int j;
3041                                                         *seglen = 0;
3042                                                         *segbevcount = 0;
3043                                                         /* We keep last bevel segment zero-length. */
3044                                                         for (j = 0; j < ((nr == 1) ? (resolu - 1) : resolu); j++) {
3045                                                                 bevp->offset = len_v3v3(bevp0->vec, bevp->vec);
3046                                                                 if (bevp->offset > treshold) {
3047                                                                         *seglen += bevp->offset;
3048                                                                         *segbevcount += 1;
3049                                                                 }
3050                                                                 bevp0 = bevp;
3051                                                                 bevp++;
3052                                                         }
3053                                                         seglen++;
3054                                                         segbevcount++;
3055                                                         nr--;
3056                                                 }
3057                                         }
3058
3059                                         if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
3060                                                 bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
3061                                         }
3062                                 }
3063                         }
3064                 }
3065         }
3066
3067         /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
3068         bl = bev->first;
3069         while (bl) {
3070                 if (bl->nr) { /* null bevel items come from single points */
3071                         bool is_cyclic = bl->poly != -1;
3072                         nr = bl->nr;
3073                         if (is_cyclic) {
3074                                 bevp1 = bl->bevpoints;
3075                                 bevp0 = bevp1 + (nr - 1);
3076                         }
3077                         else {
3078                                 bevp0 = bl->bevpoints;
3079                                 bevp0->offset = 0;
3080                                 bevp1 = bevp0 + 1;
3081                         }