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