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