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