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