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