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