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