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