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