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