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