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