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