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