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