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