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