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