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