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