Curves: save active point to file
[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         cun->nurb.first = cun->nurb.last = NULL;
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         lb->first = lb->last = NULL;
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, int forRender, int renderResolution)
1624 {
1625         DispList *dl, *dlnew;
1626         Curve *bevcu, *cu;
1627         float *fp, facx, facy, angle, dangle;
1628         int nr, a;
1629
1630         cu = ob->data;
1631         disp->first = disp->last = NULL;
1632
1633         /* if a font object is being edited, then do nothing */
1634 // XXX  if ( ob == obedit && ob->type == OB_FONT ) return;
1635
1636         if (cu->bevobj) {
1637                 if (cu->bevobj->type != OB_CURVE)
1638                         return;
1639
1640                 bevcu = cu->bevobj->data;
1641                 if (bevcu->ext1 == 0.0f && bevcu->ext2 == 0.0f) {
1642                         ListBase bevdisp = {NULL, NULL};
1643                         facx = cu->bevobj->size[0];
1644                         facy = cu->bevobj->size[1];
1645
1646                         if (forRender) {
1647                                 BKE_displist_make_curveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0, renderResolution);
1648                                 dl = bevdisp.first;
1649                         }
1650                         else {
1651                                 BLI_assert(cu->bevobj->curve_cache != NULL);
1652                                 dl = cu->bevobj->curve_cache->disp.first;
1653                         }
1654
1655                         while (dl) {
1656                                 if (ELEM(dl->type, DL_POLY, DL_SEGM)) {
1657                                         dlnew = MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1658                                         *dlnew = *dl;
1659                                         dlnew->verts = MEM_mallocN(3 * sizeof(float) * dl->parts * dl->nr, "makebevelcurve1");
1660                                         memcpy(dlnew->verts, dl->verts, 3 * sizeof(float) * dl->parts * dl->nr);
1661
1662                                         if (dlnew->type == DL_SEGM)
1663                                                 dlnew->flag |= (DL_FRONT_CURVE | DL_BACK_CURVE);
1664
1665                                         BLI_addtail(disp, dlnew);
1666                                         fp = dlnew->verts;
1667                                         nr = dlnew->parts * dlnew->nr;
1668                                         while (nr--) {
1669                                                 fp[2] = fp[1] * facy;
1670                                                 fp[1] = -fp[0] * facx;
1671                                                 fp[0] = 0.0;
1672                                                 fp += 3;
1673                                         }
1674                                 }
1675                                 dl = dl->next;
1676                         }
1677
1678                         BKE_displist_free(&bevdisp);
1679                 }
1680         }
1681         else if (cu->ext1 == 0.0f && cu->ext2 == 0.0f) {
1682                 /* pass */
1683         }
1684         else if (cu->ext2 == 0.0f) {
1685                 dl = MEM_callocN(sizeof(DispList), "makebevelcurve2");
1686                 dl->verts = MEM_mallocN(2 * 3 * sizeof(float), "makebevelcurve2");
1687                 BLI_addtail(disp, dl);
1688                 dl->type = DL_SEGM;
1689                 dl->parts = 1;
1690                 dl->flag = DL_FRONT_CURVE | DL_BACK_CURVE;
1691                 dl->nr = 2;
1692
1693                 fp = dl->verts;
1694                 fp[0] = fp[1] = 0.0;
1695                 fp[2] = -cu->ext1;
1696                 fp[3] = fp[4] = 0.0;
1697                 fp[5] = cu->ext1;
1698         }
1699         else if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0 && cu->ext1 == 0.0f) { // we make a full round bevel in that case
1700                 nr = 4 + 2 * cu->bevresol;
1701
1702                 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1703                 dl->verts = MEM_mallocN(nr * 3 * sizeof(float), "makebevelcurve p1");
1704                 BLI_addtail(disp, dl);
1705                 dl->type = DL_POLY;
1706                 dl->parts = 1;
1707                 dl->flag = DL_BACK_CURVE;
1708                 dl->nr = nr;
1709
1710                 /* a circle */
1711                 fp = dl->verts;
1712                 dangle = (2.0f * (float)M_PI / (nr));
1713                 angle = -(nr - 1) * dangle;
1714
1715                 for (a = 0; a < nr; a++) {
1716                         fp[0] = 0.0;
1717                         fp[1] = (cosf(angle) * (cu->ext2));
1718                         fp[2] = (sinf(angle) * (cu->ext2)) - cu->ext1;
1719                         angle += dangle;
1720                         fp += 3;
1721                 }
1722         }
1723         else {
1724                 short dnr;
1725
1726                 /* bevel now in three parts, for proper vertex normals */
1727                 /* part 1, back */
1728
1729                 if ((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1730                         dnr = nr = 2 + cu->bevresol;
1731                         if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0)
1732                                 nr = 3 + 2 * cu->bevresol;
1733
1734                         dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1735                         dl->verts = MEM_mallocN(nr * 3 * sizeof(float), "makebevelcurve p1");
1736                         BLI_addtail(disp, dl);
1737                         dl->type = DL_SEGM;
1738                         dl->parts = 1;
1739                         dl->flag = DL_BACK_CURVE;
1740                         dl->nr = nr;
1741
1742                         /* half a circle */
1743                         fp = dl->verts;
1744                         dangle = (0.5 * M_PI / (dnr - 1));
1745                         angle = -(nr - 1) * dangle;
1746
1747                         for (a = 0; a < nr; a++) {
1748                                 fp[0] = 0.0;
1749                                 fp[1] = (float)(cosf(angle) * (cu->ext2));
1750                                 fp[2] = (float)(sinf(angle) * (cu->ext2)) - cu->ext1;
1751                                 angle += dangle;
1752                                 fp += 3;
1753                         }
1754                 }
1755
1756                 /* part 2, sidefaces */
1757                 if (cu->ext1 != 0.0f) {
1758                         nr = 2;
1759
1760                         dl = MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1761                         dl->verts = MEM_callocN(nr * 3 * sizeof(float), "makebevelcurve p2");
1762                         BLI_addtail(disp, dl);
1763                         dl->type = DL_SEGM;
1764                         dl->parts = 1;
1765                         dl->nr = nr;
1766
1767                         fp = dl->verts;
1768                         fp[1] = cu->ext2;
1769                         fp[2] = -cu->ext1;
1770                         fp[4] = cu->ext2;
1771                         fp[5] = cu->ext1;
1772
1773                         if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0) {
1774                                 dl = MEM_dupallocN(dl);
1775                                 dl->verts = MEM_dupallocN(dl->verts);
1776                                 BLI_addtail(disp, dl);
1777
1778                                 fp = dl->verts;
1779                                 fp[1] = -fp[1];
1780                                 fp[2] = -fp[2];
1781                                 fp[4] = -fp[4];
1782                                 fp[5] = -fp[5];
1783                         }
1784                 }
1785
1786                 /* part 3, front */
1787                 if ((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1788                         dnr = nr = 2 + cu->bevresol;
1789                         if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0)
1790                                 nr = 3 + 2 * cu->bevresol;
1791
1792                         dl = MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1793                         dl->verts = MEM_mallocN(nr * 3 * sizeof(float), "makebevelcurve p3");
1794                         BLI_addtail(disp, dl);
1795                         dl->type = DL_SEGM;
1796                         dl->flag = DL_FRONT_CURVE;
1797                         dl->parts = 1;
1798                         dl->nr = nr;
1799
1800                         /* half a circle */
1801                         fp = dl->verts;
1802                         angle = 0.0;
1803                         dangle = (0.5 * M_PI / (dnr - 1));
1804
1805                         for (a = 0; a < nr; a++) {
1806                                 fp[0] = 0.0;
1807                                 fp[1] = (float)(cosf(angle) * (cu->ext2));
1808                                 fp[2] = (float)(sinf(angle) * (cu->ext2)) + cu->ext1;
1809                                 angle += dangle;
1810                                 fp += 3;
1811                         }
1812                 }
1813         }
1814 }
1815
1816 static int cu_isectLL(const float v1[3], const float v2[3], const float v3[3], const float v4[3],
1817                       short cox, short coy,
1818                       float *lambda, float *mu, float vec[3])
1819 {
1820         /* return:
1821          * -1: collinear
1822          *  0: no intersection of segments
1823          *  1: exact intersection of segments
1824          *  2: cross-intersection of segments
1825          */
1826         float deler;
1827
1828         deler = (v1[cox] - v2[cox]) * (v3[coy] - v4[coy]) - (v3[cox] - v4[cox]) * (v1[coy] - v2[coy]);
1829         if (deler == 0.0f)
1830                 return -1;
1831
1832         *lambda = (v1[coy] - v3[coy]) * (v3[cox] - v4[cox]) - (v1[cox] - v3[cox]) * (v3[coy] - v4[coy]);
1833         *lambda = -(*lambda / deler);
1834
1835         deler = v3[coy] - v4[coy];
1836         if (deler == 0) {
1837                 deler = v3[cox] - v4[cox];
1838                 *mu = -(*lambda * (v2[cox] - v1[cox]) + v1[cox] - v3[cox]) / deler;
1839         }
1840         else {
1841                 *mu = -(*lambda * (v2[coy] - v1[coy]) + v1[coy] - v3[coy]) / deler;
1842         }
1843         vec[cox] = *lambda * (v2[cox] - v1[cox]) + v1[cox];
1844         vec[coy] = *lambda * (v2[coy] - v1[coy]) + v1[coy];
1845
1846         if (*lambda >= 0.0f && *lambda <= 1.0f && *mu >= 0.0f && *mu <= 1.0f) {
1847                 if (*lambda == 0.0f || *lambda == 1.0f || *mu == 0.0f || *mu == 1.0f)
1848                         return 1;
1849                 return 2;
1850         }
1851         return 0;
1852 }
1853
1854
1855 static bool bevelinside(BevList *bl1, BevList *bl2)
1856 {
1857         /* is bl2 INSIDE bl1 ? with left-right method and "lambda's" */
1858         /* returns '1' if correct hole  */
1859         BevPoint *bevp, *prevbevp;
1860         float min, max, vec[3], hvec1[3], hvec2[3], lab, mu;
1861         int nr, links = 0, rechts = 0, mode;
1862
1863         /* take first vertex of possible hole */
1864
1865         bevp = (BevPoint *)(bl2 + 1);
1866         hvec1[0] = bevp->vec[0];
1867         hvec1[1] = bevp->vec[1];
1868         hvec1[2] = 0.0;
1869         copy_v3_v3(hvec2, hvec1);
1870         hvec2[0] += 1000;
1871
1872         /* test it with all edges of potential surounding poly */
1873         /* count number of transitions left-right  */
1874
1875         bevp = (BevPoint *)(bl1 + 1);
1876         nr = bl1->nr;
1877         prevbevp = bevp + (nr - 1);
1878
1879         while (nr--) {
1880                 min = prevbevp->vec[1];
1881                 max = bevp->vec[1];
1882                 if (max < min) {
1883                         min = max;
1884                         max = prevbevp->vec[1];
1885                 }
1886                 if (min != max) {
1887                         if (min <= hvec1[1] && max >= hvec1[1]) {
1888                                 /* there's a transition, calc intersection point */
1889                                 mode = cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1890                                 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1891                                  * only allow for one situation: we choose lab= 1.0
1892                                  */
1893                                 if (mode >= 0 && lab != 0.0f) {
1894                                         if (vec[0] < hvec1[0]) links++;
1895                                         else rechts++;
1896                                 }
1897                         }
1898                 }
1899                 prevbevp = bevp;
1900                 bevp++;
1901         }
1902
1903         return (links & 1) && (rechts & 1);
1904 }
1905
1906
1907 struct BevelSort {
1908         BevList *bl;
1909         float left;
1910         int dir;
1911 };
1912
1913 static int vergxcobev(const void *a1, const void *a2)
1914 {
1915         const struct BevelSort *x1 = a1, *x2 = a2;
1916
1917         if (x1->left > x2->left)
1918                 return 1;
1919         else if (x1->left < x2->left)
1920                 return -1;
1921         return 0;
1922 }
1923
1924 /* this function cannot be replaced with atan2, but why? */
1925
1926 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1927 {
1928         float t01, t02, x3, y3;
1929
1930         t01 = (float)sqrt(x1 * x1 + y1 * y1);
1931         t02 = (float)sqrt(x2 * x2 + y2 * y2);
1932         if (t01 == 0.0f)
1933                 t01 = 1.0f;
1934         if (t02 == 0.0f)
1935                 t02 = 1.0f;
1936
1937         x1 /= t01;
1938         y1 /= t01;
1939         x2 /= t02;
1940         y2 /= t02;
1941
1942         t02 = x1 * x2 + y1 * y2;
1943         if (fabsf(t02) >= 1.0f)
1944                 t02 = 0.5 * M_PI;
1945         else
1946                 t02 = (saacos(t02)) / 2.0f;
1947
1948         t02 = sinf(t02);
1949         if (t02 == 0.0f)
1950                 t02 = 1.0f;
1951
1952         x3 = x1 - x2;
1953         y3 = y1 - y2;
1954         if (x3 == 0 && y3 == 0) {
1955                 x3 = y1;
1956                 y3 = -x1;
1957         }
1958         else {
1959                 t01 = (float)sqrt(x3 * x3 + y3 * y3);
1960                 x3 /= t01;
1961                 y3 /= t01;
1962         }
1963
1964         *sina = -y3 / t02;
1965         *cosa = x3 / t02;
1966
1967 }
1968
1969 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array,
1970                          float *weight_array, int resolu, int stride)
1971 {
1972         BezTriple *pprev, *next, *last;
1973         float fac, dfac, t[4];
1974         int a;
1975
1976         if (tilt_array == NULL && radius_array == NULL)
1977                 return;
1978
1979         last = nu->bezt + (nu->pntsu - 1);
1980
1981         /* returns a point */
1982         if (prevbezt == nu->bezt) {
1983                 if (nu->flagu & CU_NURB_CYCLIC)
1984                         pprev = last;
1985                 else
1986                         pprev = prevbezt;
1987         }
1988         else
1989                 pprev = prevbezt - 1;
1990
1991         /* next point */
1992         if (bezt == last) {
1993                 if (nu->flagu & CU_NURB_CYCLIC)
1994                         next = nu->bezt;
1995                 else
1996                         next = bezt;
1997         }
1998         else
1999                 next = bezt + 1;
2000
2001         fac = 0.0;
2002         dfac = 1.0f / (float)resolu;
2003
2004         for (a = 0; a < resolu; a++, fac += dfac) {
2005                 if (tilt_array) {
2006                         if (nu->tilt_interp == KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
2007                                 *tilt_array = prevbezt->alfa +
2008                                         (bezt->alfa - prevbezt->alfa) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2009                         }
2010                         else {
2011                                 key_curve_position_weights(fac, t, nu->tilt_interp);
2012                                 *tilt_array = t[0] * pprev->alfa + t[1] * prevbezt->alfa + t[2] * bezt->alfa + t[3] * next->alfa;
2013                         }
2014
2015                         tilt_array = (float *)(((char *)tilt_array) + stride);
2016                 }
2017
2018                 if (radius_array) {
2019                         if (nu->radius_interp == KEY_CU_EASE) {
2020                                 /* Support 2.47 ease interp
2021                                  * Note! - this only takes the 2 points into account,
2022                                  * giving much more localized results to changes in radius, sometimes you want that */
2023                                 *radius_array = prevbezt->radius +
2024                                         (bezt->radius - prevbezt->radius) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2025                         }
2026                         else {
2027
2028                                 /* reuse interpolation from tilt if we can */
2029                                 if (tilt_array == NULL || nu->tilt_interp != nu->radius_interp) {
2030                                         key_curve_position_weights(fac, t, nu->radius_interp);
2031                                 }
2032                                 *radius_array = t[0] * pprev->radius + t[1] * prevbezt->radius +
2033                                         t[2] * bezt->radius + t[3] * next->radius;
2034                         }
2035
2036                         radius_array = (float *)(((char *)radius_array) + stride);
2037                 }
2038
2039                 if (weight_array) {
2040                         /* basic interpolation for now, could copy tilt interp too  */
2041                         *weight_array = prevbezt->weight +
2042                                 (bezt->weight - prevbezt->weight) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2043
2044                         weight_array = (float *)(((char *)weight_array) + stride);
2045                 }
2046         }
2047 }
2048
2049 /* make_bevel_list_3D_* funcs, at a minimum these must
2050  * fill in the bezp->quat and bezp->dir values */
2051
2052 /* utility for make_bevel_list_3D_* funcs */
2053 static void bevel_list_calc_bisect(BevList *bl)
2054 {
2055         BevPoint *bevp2, *bevp1, *bevp0;
2056         int nr;
2057         bool is_cyclic = bl->poly != -1;
2058
2059         if (is_cyclic) {
2060                 bevp2 = (BevPoint *)(bl + 1);
2061                 bevp1 = bevp2 + (bl->nr - 1);
2062                 bevp0 = bevp1 - 1;
2063                 nr = bl->nr;
2064         }
2065         else {
2066                 /* If spline is not cyclic, direction of first and
2067                  * last bevel points matches direction of CV handle.
2068                  *
2069                  * This is getting calculated earlier when we know
2070                  * CV's handles and here we might simply skip evaluation
2071                  * of direction for this guys.
2072                  */
2073
2074                 bevp0 = (BevPoint *)(bl + 1);
2075                 bevp1 = bevp0 + 1;
2076                 bevp2 = bevp1 + 1;
2077
2078                 nr = bl->nr - 2;
2079         }
2080
2081         while (nr--) {
2082                 /* totally simple */
2083                 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2084
2085                 bevp0 = bevp1;
2086                 bevp1 = bevp2;
2087                 bevp2++;
2088         }
2089 }
2090 static void bevel_list_flip_tangents(BevList *bl)
2091 {
2092         BevPoint *bevp2, *bevp1, *bevp0;
2093         int nr;
2094
2095         bevp2 = (BevPoint *)(bl + 1);
2096         bevp1 = bevp2 + (bl->nr - 1);
2097         bevp0 = bevp1 - 1;
2098
2099         nr = bl->nr;
2100         while (nr--) {
2101                 if (angle_normalized_v3v3(bevp0->tan, bevp1->tan) > DEG2RADF(90.0f))
2102                         negate_v3(bevp1->tan);
2103
2104                 bevp0 = bevp1;
2105                 bevp1 = bevp2;
2106                 bevp2++;
2107         }
2108 }
2109 /* apply user tilt */
2110 static void bevel_list_apply_tilt(BevList *bl)
2111 {
2112         BevPoint *bevp2, *bevp1;
2113         int nr;
2114         float q[4];
2115
2116         bevp2 = (BevPoint *)(bl + 1);
2117         bevp1 = bevp2 + (bl->nr - 1);
2118
2119         nr = bl->nr;
2120         while (nr--) {
2121                 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2122                 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2123                 normalize_qt(bevp1->quat);
2124
2125                 bevp1 = bevp2;
2126                 bevp2++;
2127         }
2128 }
2129 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
2130 static void bevel_list_smooth(BevList *bl, int smooth_iter)
2131 {
2132         BevPoint *bevp2, *bevp1, *bevp0;
2133         int nr;
2134
2135         float q[4];
2136         float bevp0_quat[4];
2137         int a;
2138
2139         for (a = 0; a < smooth_iter; a++) {
2140                 bevp2 = (BevPoint *)(bl + 1);
2141                 bevp1 = bevp2 + (bl->nr - 1);
2142                 bevp0 = bevp1 - 1;
2143
2144                 nr = bl->nr;
2145
2146                 if (bl->poly == -1) { /* check its not cyclic */
2147                         /* skip the first point */
2148                         /* bevp0 = bevp1; */
2149                         bevp1 = bevp2;
2150                         bevp2++;
2151                         nr--;
2152
2153                         bevp0 = bevp1;
2154                         bevp1 = bevp2;
2155                         bevp2++;
2156                         nr--;
2157                 }
2158
2159                 copy_qt_qt(bevp0_quat, bevp0->quat);
2160
2161                 while (nr--) {
2162                         /* interpolate quats */
2163                         float zaxis[3] = {0, 0, 1}, cross[3], q2[4];
2164                         interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
2165                         normalize_qt(q);
2166
2167                         mul_qt_v3(q, zaxis);
2168                         cross_v3_v3v3(cross, zaxis, bevp1->dir);
2169                         axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
2170                         normalize_qt(q2);
2171
2172                         copy_qt_qt(bevp0_quat, bevp1->quat);
2173                         mul_qt_qtqt(q, q2, q);
2174                         interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
2175                         normalize_qt(bevp1->quat);
2176
2177                         /* bevp0 = bevp1; */ /* UNUSED */
2178                         bevp1 = bevp2;
2179                         bevp2++;
2180                 }
2181         }
2182 }
2183
2184 static void make_bevel_list_3D_zup(BevList *bl)
2185 {
2186         BevPoint *bevp = (BevPoint *)(bl + 1);
2187         int nr = bl->nr;
2188
2189         bevel_list_calc_bisect(bl);
2190
2191         while (nr--) {
2192                 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2193                 bevp++;
2194         }
2195 }
2196
2197 static void minimum_twist_between_two_points(BevPoint *current_point, BevPoint *previous_point)
2198 {
2199         float angle = angle_normalized_v3v3(previous_point->dir, current_point->dir);
2200         float q[4];
2201
2202         if (angle > 0.0f) { /* otherwise we can keep as is */
2203                 float cross_tmp[3];
2204                 cross_v3_v3v3(cross_tmp, previous_point->dir, current_point->dir);
2205                 axis_angle_to_quat(q, cross_tmp, angle);
2206                 mul_qt_qtqt(current_point->quat, q, previous_point->quat);
2207         }
2208         else {
2209                 copy_qt_qt(current_point->quat, previous_point->quat);
2210         }
2211 }
2212
2213 static void make_bevel_list_3D_minimum_twist(BevList *bl)
2214 {
2215         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
2216         int nr;
2217         float q[4];
2218
2219         bevel_list_calc_bisect(bl);
2220
2221         bevp2 = (BevPoint *)(bl + 1);
2222         bevp1 = bevp2 + (bl->nr - 1);
2223         bevp0 = bevp1 - 1;
2224
2225         nr = bl->nr;
2226         while (nr--) {
2227
2228                 if (nr + 4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
2229                         vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2230                 }
2231                 else {
2232                         minimum_twist_between_two_points(bevp1, bevp0);
2233                 }
2234
2235                 bevp0 = bevp1;
2236                 bevp1 = bevp2;
2237                 bevp2++;
2238         }
2239
2240         if (bl->poly != -1) { /* check for cyclic */
2241
2242                 /* Need to correct for the start/end points not matching
2243                  * do this by calculating the tilt angle difference, then apply
2244                  * the rotation gradually over the entire curve
2245                  *
2246                  * note that the split is between last and second last, rather than first/last as youd expect.
2247                  *
2248                  * real order is like this
2249                  * 0,1,2,3,4 --> 1,2,3,4,0
2250                  *
2251                  * this is why we compare last with second last
2252                  * */
2253                 float vec_1[3] = {0, 1, 0}, vec_2[3] = {0, 1, 0}, angle, ang_fac, cross_tmp[3];
2254
2255                 BevPoint *bevp_first;
2256                 BevPoint *bevp_last;
2257
2258
2259                 bevp_first = (BevPoint *)(bl + 1);
2260                 bevp_first += bl->nr - 1;
2261                 bevp_last = bevp_first;
2262                 bevp_last--;
2263
2264                 /* quats and vec's are normalized, should not need to re-normalize */
2265                 mul_qt_v3(bevp_first->quat, vec_1);
2266                 mul_qt_v3(bevp_last->quat, vec_2);
2267                 normalize_v3(vec_1);
2268                 normalize_v3(vec_2);
2269
2270                 /* align the vector, can avoid this and it looks 98% OK but
2271                  * better to align the angle quat roll's before comparing */
2272                 {
2273                         cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
2274                         angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
2275                         axis_angle_to_quat(q, cross_tmp, angle);
2276                         mul_qt_v3(q, vec_2);
2277                 }
2278
2279                 angle = angle_normalized_v3v3(vec_1, vec_2);
2280
2281                 /* flip rotation if needs be */
2282                 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
2283                 normalize_v3(cross_tmp);
2284                 if (angle_normalized_v3v3(bevp_first->dir, cross_tmp) < DEG2RADF(90.0f))
2285                         angle = -angle;
2286
2287                 bevp2 = (BevPoint *)(bl + 1);
2288                 bevp1 = bevp2 + (bl->nr - 1);
2289                 bevp0 = bevp1 - 1;
2290
2291                 nr = bl->nr;
2292                 while (nr--) {
2293                         ang_fac = angle * (1.0f - ((float)nr / bl->nr)); /* also works */
2294
2295                         axis_angle_to_quat(q, bevp1->dir, ang_fac);
2296                         mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2297
2298                         bevp0 = bevp1;
2299                         bevp1 = bevp2;
2300                         bevp2++;
2301                 }
2302         }
2303         else {
2304                 /* Need to correct quat for the first/last point,
2305                  * this is so because previously it was only calculated
2306                  * using it's own direction, which might not correspond
2307                  * the twist of neighbor point.
2308                  */
2309                 bevp1 = (BevPoint *)(bl + 1);
2310                 bevp0 = bevp1 + 1;
2311                 minimum_twist_between_two_points(bevp1, bevp0);
2312
2313                 bevp2 = (BevPoint *)(bl + 1);
2314                 bevp1 = bevp2 + (bl->nr - 1);
2315                 bevp0 = bevp1 - 1;
2316                 minimum_twist_between_two_points(bevp1, bevp0);
2317         }
2318 }
2319
2320 static void make_bevel_list_3D_tangent(BevList *bl)
2321 {
2322         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
2323         int nr;
2324
2325         float bevp0_tan[3];
2326
2327         bevel_list_calc_bisect(bl);
2328         bevel_list_flip_tangents(bl);
2329
2330         /* correct the tangents */
2331         bevp2 = (BevPoint *)(bl + 1);
2332         bevp1 = bevp2 + (bl->nr - 1);
2333         bevp0 = bevp1 - 1;
2334
2335         nr = bl->nr;
2336         while (nr--) {
2337                 float cross_tmp[3];
2338                 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2339                 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
2340                 normalize_v3(bevp1->tan);
2341
2342                 bevp0 = bevp1;
2343                 bevp1 = bevp2;
2344                 bevp2++;
2345         }
2346
2347
2348         /* now for the real twist calc */
2349         bevp2 = (BevPoint *)(bl + 1);
2350         bevp1 = bevp2 + (bl->nr - 1);
2351         bevp0 = bevp1 - 1;
2352
2353         copy_v3_v3(bevp0_tan, bevp0->tan);
2354
2355         nr = bl->nr;
2356         while (nr--) {
2357                 /* make perpendicular, modify tan in place, is ok */
2358                 float cross_tmp[3];
2359                 float zero[3] = {0, 0, 0};
2360
2361                 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2362                 normalize_v3(cross_tmp);
2363                 tri_to_quat(bevp1->quat, zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
2364
2365                 /* bevp0 = bevp1; */ /* UNUSED */
2366                 bevp1 = bevp2;
2367                 bevp2++;
2368         }
2369 }
2370
2371 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
2372 {
2373         switch (twist_mode) {
2374                 case CU_TWIST_TANGENT:
2375                         make_bevel_list_3D_tangent(bl);
2376                         break;
2377                 case CU_TWIST_MINIMUM:
2378                         make_bevel_list_3D_minimum_twist(bl);
2379                         break;
2380                 default: /* CU_TWIST_Z_UP default, pre 2.49c */
2381                         make_bevel_list_3D_zup(bl);
2382                         break;
2383         }
2384
2385         if (smooth_iter)
2386                 bevel_list_smooth(bl, smooth_iter);
2387
2388         bevel_list_apply_tilt(bl);
2389 }
2390
2391 /* only for 2 points */
2392 static void make_bevel_list_segment_3D(BevList *bl)
2393 {
2394         float q[4];
2395
2396         BevPoint *bevp2 = (BevPoint *)(bl + 1);
2397         BevPoint *bevp1 = bevp2 + 1;
2398
2399         /* simple quat/dir */
2400         sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2401         normalize_v3(bevp1->dir);
2402
2403         vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2404
2405         axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2406         mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2407         normalize_qt(bevp1->quat);
2408         copy_v3_v3(bevp2->dir, bevp1->dir);
2409         copy_qt_qt(bevp2->quat, bevp1->quat);
2410 }
2411
2412 /* only for 2 points */
2413 static void make_bevel_list_segment_2D(BevList *bl)
2414 {
2415         BevPoint *bevp2 = (BevPoint *)(bl + 1);
2416         BevPoint *bevp1 = bevp2 + 1;
2417
2418         const float x1 = bevp1->vec[0] - bevp2->vec[0];
2419         const float y1 = bevp1->vec[1] - bevp2->vec[1];
2420
2421         calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2422         bevp2->sina = bevp1->sina;
2423         bevp2->cosa = bevp1->cosa;
2424
2425         /* fill in dir & quat */
2426         make_bevel_list_segment_3D(bl);
2427 }
2428
2429 static void make_bevel_list_2D(BevList *bl)
2430 {
2431         /* note: bevp->dir and bevp->quat are not needed for beveling but are
2432          * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2433
2434         BevPoint *bevp0, *bevp1, *bevp2;
2435         int nr;
2436
2437         if (bl->poly != -1) {
2438                 bevp2 = (BevPoint *)(bl + 1);
2439                 bevp1 = bevp2 + (bl->nr - 1);
2440                 bevp0 = bevp1 - 1;
2441                 nr = bl->nr;
2442         }
2443         else {
2444                 bevp0 = (BevPoint *)(bl + 1);
2445                 bevp1 = bevp0 + 1;
2446                 bevp2 = bevp1 + 1;
2447
2448                 nr = bl->nr - 2;
2449         }
2450
2451         while (nr--) {
2452                 const float x1 = bevp1->vec[0] - bevp0->vec[0];
2453                 const float x2 = bevp1->vec[0] - bevp2->vec[0];
2454                 const float y1 = bevp1->vec[1] - bevp0->vec[1];
2455                 const float y2 = bevp1->vec[1] - bevp2->vec[1];
2456
2457                 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2458
2459                 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2460                  * no need for tricky tilt calculation as with 3D curves */
2461                 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2462                 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2463                 /* done with inline make_bevel_list_3D_zup */
2464
2465                 bevp0 = bevp1;
2466                 bevp1 = bevp2;
2467                 bevp2++;
2468         }
2469
2470         /* correct non-cyclic cases */
2471         if (bl->poly == -1) {
2472                 BevPoint *bevp;
2473                 float angle;
2474
2475                 /* first */
2476                 bevp = (BevPoint *)(bl + 1);
2477                 angle = atan2(bevp->dir[0], bevp->dir[1]) - M_PI / 2.0;
2478                 bevp->sina = sinf(angle);
2479                 bevp->cosa = cosf(angle);
2480                 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2481
2482                 /* last */
2483                 bevp = (BevPoint *)(bl + 1);
2484                 bevp += (bl->nr - 1);
2485                 angle = atan2(bevp->dir[0], bevp->dir[1]) - M_PI / 2.0;
2486                 bevp->sina = sinf(angle);
2487                 bevp->cosa = cosf(angle);
2488                 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2489         }
2490 }
2491
2492 static void bevlist_firstlast_direction_calc_from_bpoint(Nurb *nu, BevList *bl)
2493 {
2494         if (nu->pntsu > 1) {
2495                 BPoint *first_bp = nu->bp, *last_bp = nu->bp + (nu->pntsu - 1);
2496                 BevPoint *first_bevp, *last_bevp;
2497
2498                 first_bevp = (BevPoint *)(bl + 1);
2499                 last_bevp = first_bevp + (bl->nr - 1);
2500
2501                 sub_v3_v3v3(first_bevp->dir, (first_bp + 1)->vec, first_bp->vec);
2502                 normalize_v3(first_bevp->dir);
2503
2504                 sub_v3_v3v3(last_bevp->dir, last_bp->vec, (last_bp - 1)->vec);
2505                 normalize_v3(last_bevp->dir);
2506         }
2507 }
2508
2509 void BKE_curve_bevelList_make(Object *ob, ListBase *nurbs, bool for_render)
2510 {
2511         /*
2512          * - convert all curves to polys, with indication of resol and flags for double-vertices
2513          * - possibly; do a smart vertice removal (in case Nurb)
2514          * - separate in individual blicks with BoundBox
2515          * - AutoHole detection
2516          */
2517         Curve *cu;
2518         Nurb *nu;
2519         BezTriple *bezt, *prevbezt;
2520         BPoint *bp;
2521         BevList *bl, *blnew, *blnext;
2522         BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2523         float min, inp;
2524         struct BevelSort *sortdata, *sd, *sd1;
2525         int a, b, nr, poly, resolu = 0, len = 0;
2526         int do_tilt, do_radius, do_weight;
2527         int is_editmode = 0;
2528         ListBase *bev;
2529
2530         /* this function needs an object, because of tflag and upflag */
2531         cu = ob->data;
2532
2533         bev = &ob->curve_cache->bev;
2534
2535         /* do we need to calculate the radius for each point? */
2536         /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2537
2538         /* STEP 1: MAKE POLYS  */
2539
2540         BLI_freelistN(&(ob->curve_cache->bev));
2541         nu = nurbs->first;
2542         if (cu->editnurb && ob->type != OB_FONT) {
2543                 is_editmode = 1;
2544         }
2545
2546         for (; nu; nu = nu->next) {
2547                 
2548                 if (nu->hide && is_editmode)
2549                         continue;
2550                 
2551                 /* check if we will calculate tilt data */
2552                 do_tilt = CU_DO_TILT(cu, nu);
2553                 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2554                 do_weight = TRUE;
2555
2556                 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2557                  * enforced in the UI but can go wrong possibly */
2558                 if (!BKE_nurb_check_valid_u(nu)) {
2559                         bl = MEM_callocN(sizeof(BevList) + 1 * sizeof(BevPoint), "makeBevelList1");
2560                         BLI_addtail(bev, bl);
2561                         bl->nr = 0;
2562                         bl->charidx = nu->charidx;
2563                 }
2564                 else {
2565                         if (for_render && cu->resolu_ren != 0)
2566                                 resolu = cu->resolu_ren;
2567                         else
2568                                 resolu = nu->resolu;
2569
2570                         if (nu->type == CU_POLY) {
2571                                 len = nu->pntsu;
2572                                 bl = MEM_callocN(sizeof(BevList) + len * sizeof(BevPoint), "makeBevelList2");
2573                                 BLI_addtail(bev, bl);
2574
2575                                 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2576                                 bl->nr = len;
2577                                 bl->dupe_nr = 0;
2578                                 bl->charidx = nu->charidx;
2579                                 bevp = (BevPoint *)(bl + 1);
2580                                 bp = nu->bp;
2581
2582                                 while (len--) {
2583                                         copy_v3_v3(bevp->vec, bp->vec);
2584                                         bevp->alfa = bp->alfa;
2585                                         bevp->radius = bp->radius;
2586                                         bevp->weight = bp->weight;
2587                                         bevp->split_tag = TRUE;
2588                                         bevp++;
2589                                         bp++;
2590                                 }
2591
2592                                 if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
2593                                         bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
2594                                 }
2595                         }
2596                         else if (nu->type == CU_BEZIER) {
2597                                 /* in case last point is not cyclic */
2598                                 len = resolu * (nu->pntsu + (nu->flagu & CU_NURB_CYCLIC) - 1) + 1;
2599                                 bl = MEM_callocN(sizeof(BevList) + len * sizeof(BevPoint), "makeBevelBPoints");
2600                                 BLI_addtail(bev, bl);
2601
2602                                 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2603                                 bl->charidx = nu->charidx;
2604                                 bevp = (BevPoint *)(bl + 1);
2605
2606                                 a = nu->pntsu - 1;
2607                                 bezt = nu->bezt;
2608                                 if (nu->flagu & CU_NURB_CYCLIC) {
2609                                         a++;
2610                                         prevbezt = nu->bezt + (nu->pntsu - 1);
2611                                 }
2612                                 else {
2613                                         prevbezt = bezt;
2614                                         bezt++;
2615                                 }
2616
2617                                 sub_v3_v3v3(bevp->dir, prevbezt->vec[2], prevbezt->vec[1]);
2618                                 normalize_v3(bevp->dir);
2619
2620                                 while (a--) {
2621                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
2622
2623                                                 copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2624                                                 bevp->alfa = prevbezt->alfa;
2625                                                 bevp->radius = prevbezt->radius;
2626                                                 bevp->weight = prevbezt->weight;
2627                                                 bevp->split_tag = TRUE;
2628                                                 bevp->dupe_tag = FALSE;
2629                                                 bevp++;
2630                                                 bl->nr++;
2631                                                 bl->dupe_nr = 1;
2632                                         }
2633                                         else {
2634                                                 /* always do all three, to prevent data hanging around */
2635                                                 int j;
2636
2637                                                 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2638                                                 for (j = 0; j < 3; j++) {
2639                                                         BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],  prevbezt->vec[2][j],
2640                                                                                       bezt->vec[0][j],      bezt->vec[1][j],
2641                                                                                       &(bevp->vec[j]), resolu, sizeof(BevPoint));
2642                                                 }
2643
2644                                                 /* if both arrays are NULL do nothiong */
2645                                                 alfa_bezpart(prevbezt, bezt, nu,
2646                                                              do_tilt    ? &bevp->alfa : NULL,
2647                                                              do_radius  ? &bevp->radius : NULL,
2648                                                              do_weight  ? &bevp->weight : NULL,
2649                                                              resolu, sizeof(BevPoint));
2650
2651
2652                                                 if (cu->twist_mode == CU_TWIST_TANGENT) {
2653                                                         forward_diff_bezier_cotangent(prevbezt->vec[1], prevbezt->vec[2],
2654                                                                                       bezt->vec[0],     bezt->vec[1],
2655                                                                                       bevp->tan, resolu, sizeof(BevPoint));
2656                                                 }
2657
2658                                                 /* indicate with handlecodes double points */
2659                                                 if (prevbezt->h1 == prevbezt->h2) {
2660                                                         if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
2661                                                                 bevp->split_tag = TRUE;
2662                                                 }
2663                                                 else {
2664                                                         if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
2665                                                                 bevp->split_tag = TRUE;
2666                                                         else if (prevbezt->h2 == 0 || prevbezt->h2 == HD_VECT)
2667                                                                 bevp->split_tag = TRUE;
2668                                                 }
2669                                                 bl->nr += resolu;
2670                                                 bevp += resolu;
2671                                         }
2672                                         prevbezt = bezt;
2673                                         bezt++;
2674                                 }
2675
2676                                 if ((nu->flagu & CU_NURB_CYCLIC) == 0) {      /* not cyclic: endpoint */
2677                                         copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2678                                         bevp->alfa = prevbezt->alfa;
2679                                         bevp->radius = prevbezt->radius;
2680                                         bevp->weight = prevbezt->weight;
2681
2682                                         sub_v3_v3v3(bevp->dir, prevbezt->vec[1], prevbezt->vec[0]);
2683                                         normalize_v3(bevp->dir);
2684
2685                                         bl->nr++;
2686                                 }
2687                         }
2688                         else if (nu->type == CU_NURBS) {
2689                                 if (nu->pntsv == 1) {
2690                                         len = (resolu * SEGMENTSU(nu));
2691
2692                                         bl = MEM_callocN(sizeof(BevList) + len * sizeof(BevPoint), "makeBevelList3");
2693                                         BLI_addtail(bev, bl);
2694                                         bl->nr = len;
2695                                         bl->dupe_nr = 0;
2696                                         bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2697                                         bl->charidx = nu->charidx;
2698                                         bevp = (BevPoint *)(bl + 1);
2699
2700                                         BKE_nurb_makeCurve(nu, &bevp->vec[0],
2701                                                            do_tilt      ? &bevp->alfa : NULL,
2702                                                            do_radius    ? &bevp->radius : NULL,
2703                                                            do_weight    ? &bevp->weight : NULL,
2704                                                            resolu, sizeof(BevPoint));
2705
2706                                         if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
2707                                                 bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
2708                                         }
2709                                 }
2710                         }
2711                 }
2712         }
2713
2714         /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2715         bl = bev->first;
2716         while (bl) {
2717                 if (bl->nr) { /* null bevel items come from single points */
2718                         bool is_cyclic = bl->poly != -1;
2719                         nr = bl->nr;
2720                         if (is_cyclic) {
2721                                 bevp1 = (BevPoint *)(bl + 1);
2722                                 bevp0 = bevp1 + (nr - 1);
2723                         }
2724                         else {
2725                                 bevp0 = (BevPoint *)(bl + 1);
2726                                 bevp1 = bevp0 + 1;
2727                         }
2728                         nr--;
2729                         while (nr--) {
2730                                 if (fabsf(bevp0->vec[0] - bevp1->vec[0]) < 0.00001f) {
2731                                         if (fabsf(bevp0->vec[1] - bevp1->vec[1]) < 0.00001f) {
2732                                                 if (fabsf(bevp0->vec[2] - bevp1->vec[2]) < 0.00001f) {
2733                                                         bevp0->dupe_tag = TRUE;
2734                                                         bl->dupe_nr++;
2735                                                 }
2736                                         }
2737                                 }
2738                                 bevp0 = bevp1;
2739                                 bevp1++;
2740                         }
2741                 }
2742                 bl = bl->next;
2743         }
2744         bl = bev->first;
2745         while (bl) {
2746                 blnext = bl->next;
2747                 if (bl->nr && bl->dupe_nr) {
2748                         nr = bl->nr - bl->dupe_nr + 1;  /* +1 because vectorbezier sets flag too */
2749                         blnew = MEM_mallocN(sizeof(BevList) + nr * sizeof(BevPoint), "makeBevelList4");
2750                         memcpy(blnew, bl, sizeof(BevList));
2751                         blnew->nr = 0;
2752                         BLI_remlink(bev, bl);
2753                         BLI_insertlinkbefore(bev, blnext, blnew);    /* to make sure bevlijst is tuned with nurblist */
2754                         bevp0 = (BevPoint *)(bl + 1);
2755                         bevp1 = (BevPoint *)(blnew + 1);
2756                         nr = bl->nr;
2757                         while (nr--) {
2758                                 if (bevp0->dupe_tag == 0) {
2759                                         memcpy(bevp1, bevp0, sizeof(BevPoint));
2760                                         bevp1++;
2761                                         blnew->nr++;
2762                                 }
2763                                 bevp0++;
2764                         }
2765                         MEM_freeN(bl);
2766                         blnew->dupe_nr = 0;
2767                 }
2768                 bl = blnext;
2769         }
2770
2771         /* STEP 3: POLYS COUNT AND AUTOHOLE */
2772         bl = bev->first;
2773         poly = 0;
2774         while (bl) {
2775                 if (bl->nr && bl->poly >= 0) {
2776                         poly++;
2777                         bl->poly = poly;
2778                         bl->hole = 0;
2779                 }
2780                 bl = bl->next;
2781         }
2782
2783         /* find extreme left points, also test (turning) direction */
2784         if (poly > 0) {
2785                 sd = sortdata = MEM_mallocN(sizeof(struct BevelSort) * poly, "makeBevelList5");
2786                 bl = bev->first;
2787                 while (bl) {
2788                         if (bl->poly > 0) {
2789
2790                                 min = 300000.0;
2791                                 bevp = (BevPoint *)(bl + 1);
2792                                 nr = bl->nr;
2793                                 while (nr--) {
2794                                         if (min > bevp->vec[0]) {
2795                                                 min = bevp->vec[0];
2796                                                 bevp1 = bevp;
2797                                         }
2798                                         bevp++;
2799                                 }
2800                                 sd->bl = bl;
2801                                 sd->left = min;
2802
2803                                 bevp = (BevPoint *)(bl + 1);
2804                                 if (bevp1 == bevp)
2805                                         bevp0 = bevp + (bl->nr - 1);
2806                                 else
2807                                         bevp0 = bevp1 - 1;
2808                                 bevp = bevp + (bl->nr - 1);
2809                                 if (bevp1 == bevp)
2810                                         bevp2 = (BevPoint *)(bl + 1);
2811                                 else
2812                                         bevp2 = bevp1 + 1;
2813
2814                                 inp = ((bevp1->vec[0] - bevp0->vec[0]) * (bevp0->vec[1] - bevp2->vec[1]) +
2815                                        (bevp0->vec[1] - bevp1->vec[1]) * (bevp0->vec[0] - bevp2->vec[0]));
2816
2817                                 if (inp > 0.0f)
2818                                         sd->dir = 1;
2819                                 else
2820                                         sd->dir = 0;
2821
2822                                 sd++;
2823                         }
2824
2825                         bl = bl->next;
2826                 }
2827                 qsort(sortdata, poly, sizeof(struct BevelSort), vergxcobev);
2828
2829                 sd = sortdata + 1;
2830                 for (a = 1; a < poly; a++, sd++) {
2831                         bl = sd->bl;     /* is bl a hole? */
2832                         sd1 = sortdata + (a - 1);
2833                         for (b = a - 1; b >= 0; b--, sd1--) { /* all polys to the left */
2834                                 if (sd1->bl->charidx == bl->charidx) { /* for text, only check matching char */
2835                                         if (bevelinside(sd1->bl, bl)) {
2836                                                 bl->hole = 1 - sd1->bl->hole;
2837                                                 break;
2838                                         }
2839                                 }
2840                         }
2841                 }
2842
2843                 /* turning direction */
2844                 if ((cu->flag & CU_3D) == 0) {
2845                         sd = sortdata;
2846                         for (a = 0; a < poly; a++, sd++) {
2847                                 if (sd->bl->hole == sd->dir) {
2848                                         bl = sd->bl;
2849                                         bevp1 = (BevPoint *)(bl + 1);
2850                                         bevp2 = bevp1 + (bl->nr - 1);
2851                                         nr = bl->nr / 2;
2852                                         while (nr--) {
2853                                                 SWAP(BevPoint, *bevp1, *bevp2);
2854                                                 bevp1++;
2855                                                 bevp2--;
2856                                         }
2857                                 }
2858                         }
2859                 }
2860                 MEM_freeN(sortdata);
2861         }
2862
2863         /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2864         if ((cu->flag & CU_3D) == 0) {
2865                 /* 2D Curves */
2866                 for (bl = bev->first; bl; bl = bl->next) {
2867                         if (bl->nr < 2) {
2868                                 /* do nothing */
2869                         }
2870                         else if (bl->nr == 2) {   /* 2 pnt, treat separate */
2871                                 make_bevel_list_segment_2D(bl);
2872                         }
2873                         else {
2874                                 make_bevel_list_2D(bl);
2875                         }
2876                 }
2877         }
2878         else {
2879                 /* 3D Curves */
2880                 for (bl = bev->first; bl; bl = bl->next) {
2881                         if (bl->nr < 2) {
2882                                 /* do nothing */
2883                         }
2884                         else if (bl->nr == 2) {   /* 2 pnt, treat separate */
2885                                 make_bevel_list_segment_3D(bl);
2886                         }
2887                         else {
2888                                 make_bevel_list_3D(bl, (int)(resolu * cu->twist_smooth), cu->twist_mode);
2889                         }
2890                 }
2891         }
2892 }
2893
2894 /* ****************** HANDLES ************** */
2895
2896 /*
2897  *   handlecodes:
2898  *              0: nothing,  1:auto,  2:vector,  3:aligned
2899  */
2900
2901 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2902 static void calchandleNurb_intern(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode, int skip_align)
2903 {
2904         float *p1, *p2, *p3, pt[3];
2905         float dvec_a[3], dvec_b[3];
2906         float len, len_a, len_b;
2907         const float eps = 1e-5;
2908
2909         if (bezt->h1 == 0 && bezt->h2 == 0) {
2910                 return;
2911         }
2912
2913         p2 = bezt->vec[1];
2914
2915         if (prev == NULL) {
2916                 p3 = next->vec[1];
2917                 pt[0] = 2.0f * p2[0] - p3[0];
2918                 pt[1] = 2.0f * p2[1] - p3[1];
2919                 pt[2] = 2.0f * p2[2] - p3[2];
2920                 p1 = pt;
2921         }
2922         else {
2923                 p1 = prev->vec[1];
2924         }
2925
2926         if (next == NULL) {
2927                 pt[0] = 2.0f * p2[0] - p1[0];
2928                 pt[1] = 2.0f * p2[1] - p1[1];
2929                 pt[2] = 2.0f * p2[2] - p1[2];
2930                 p3 = pt;
2931         }
2932         else {
2933                 p3 = next->vec[1];
2934         }
2935
2936         sub_v3_v3v3(dvec_a, p2, p1);
2937         sub_v3_v3v3(dvec_b, p3, p2);
2938
2939         if (mode != 0) {
2940                 len_a = dvec_a[0];
2941                 len_b = dvec_b[0];
2942         }
2943         else {
2944                 len_a = len_v3(dvec_a);
2945                 len_b = len_v3(dvec_b);
2946         }
2947
2948         if (len_a == 0.0f) len_a = 1.0f;
2949         if (len_b == 0.0f) len_b = 1.0f;
2950
2951
2952         if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM) || ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {    /* auto */
2953                 float tvec[3];
2954                 tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
2955                 tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
2956                 tvec[2] = dvec_b[2] / len_b + dvec_a[2] / len_a;
2957                 len = len_v3(tvec) * 2.5614f;
2958
2959                 if (len != 0.0f) {
2960                         int leftviolate = 0, rightviolate = 0;  /* for mode==2 */
2961
2962                         if (len_a > 5.0f * len_b)
2963                                 len_a = 5.0f * len_b;
2964                         if (len_b > 5.0f * len_a)
2965                                 len_b = 5.0f * len_a;
2966
2967                         if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) {
2968                                 len_a /= len;
2969                                 madd_v3_v3v3fl(p2 - 3, p2, tvec, -len_a);
2970
2971                                 if ((bezt->h1 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
2972                                         float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
2973                                         float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
2974                                         if ((ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f)) {
2975                                                 bezt->vec[0][1] = bezt->vec[1][1];
2976                                         }
2977                                         else { /* handles should not be beyond y coord of two others */
2978                                                 if (ydiff1 <= 0.0f) {
2979                                                         if (prev->vec[1][1] > bezt->vec[0][1]) {
2980                                                                 bezt->vec[0][1] = prev->vec[1][1];
2981                                                                 leftviolate = 1;
2982                                                         }
2983                                                 }
2984                                                 else {
2985                                                         if (prev->vec[1][1] < bezt->vec[0][1]) {
2986                                                                 bezt->vec[0][1] = prev->vec[1][1];
2987                                                                 leftviolate = 1;
2988                                                         }
2989                                                 }
2990                                         }
2991                                 }
2992                         }
2993                         if (ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {
2994                                 len_b /= len;
2995                                 madd_v3_v3v3fl(p2 + 3, p2, tvec,  len_b);
2996
2997                                 if ((bezt->h2 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
2998                                         float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
2999                                         float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
3000                                         if ( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
3001                                                 bezt->vec[2][1] = bezt->vec[1][1];
3002                                         }
3003                                         else { /* andles should not be beyond y coord of two others */
3004                                                 if (ydiff1 <= 0.0f) {
3005                                                         if (next->vec[1][1] < bezt->vec[2][1]) {
3006                                                                 bezt->vec[2][1] = next->vec[1][1];
3007                                                                 rightviolate = 1;
3008                                                         }
3009                                                 }
3010                                                 else {
3011                                                         if (next->vec[1][1] > bezt->vec[2][1]) {
3012                                                                 bezt->vec[2][1] = next->vec[1][1];
3013                                                                 rightviolate = 1;
3014                                                         }
3015                                                 }
3016                                         }
3017                                 }
3018                         }
3019                         if (leftviolate || rightviolate) { /* align left handle */
3020                                 float h1[3], h2[3];
3021                                 float dot;
3022
3023                                 sub_v3_v3v3(h1, p2 - 3, p2);
3024                                 sub_v3_v3v3(h2, p2, p2 + 3);
3025
3026                                 len_a = normalize_v3(h1);
3027                                 len_b = normalize_v3(h2);
3028
3029                                 dot = dot_v3v3(h1, h2);
3030
3031                                 if (leftviolate) {
3032                                         mul_v3_fl(h1, dot * len_b);
3033                                         sub_v3_v3v3(p2 + 3, p2, h1);
3034                                 }
3035                                 else {
3036                                         mul_v3_fl(h2, dot * len_a);
3037                                         add_v3_v3v3(p2 - 3, p2, h2);
3038                                 }
3039                         }
3040                 }
3041         }
3042
3043         if (bezt->h1 == HD_VECT) {    /* vector */
3044                 madd_v3_v3v3fl(p2 - 3, p2, dvec_a, -1.0f / 3.0f);
3045         }
3046         if (bezt->h2 == HD_VECT) {
3047                 madd_v3_v3v3fl(p2 + 3, p2, dvec_b,  1.0f / 3.0f);
3048         }
3049
3050         if (skip_align) {
3051                 /* handles need to be updated during animation and applying stuff like hooks,
3052                  * but in such situations it's quite difficult to distinguish in which order
3053                  * align handles should be aligned so skip them for now */
3054                 return;
3055         }
3056
3057         len_b = len_v3v3(p2, p2 + 3);
3058         len_a = len_v3v3(p2, p2 - 3);
3059         if (len_a == 0.0f)
3060                 len_a = 1.0f;
3061         if (len_b == 0.0f)
3062                 len_b = 1.0f;
3063
3064         if (bezt->f1 & SELECT) { /* order of calculation */
3065                 if (bezt->h2 == HD_ALIGN) { /* aligned */
3066                         if (len_a > eps) {
3067                                 len = len_b / len_a;
3068                                 p2[3] = p2[0] + len * (p2[0] - p2[-3]);