Blender V5.3
curve.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cmath> /* floor */
11#include <cstdlib>
12#include <cstring>
13#include <optional>
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_ghash.hh"
18#include "BLI_index_range.hh"
19#include "BLI_listbase.hh"
20#include "BLI_math_base_safe.hh"
21#include "BLI_math_geom_c.hh"
22#include "BLI_math_matrix_c.hh"
24#include "BLI_math_solvers.hh"
25#include "BLI_math_vector_c.hh"
27#include "BLI_string.hh"
28#include "BLI_string_utf8.hh"
29#include "BLI_utildefines.hh"
30#include "BLT_translation.hh"
31
32/* Allow using deprecated functionality for .blend file I/O. */
33#define DNA_DEPRECATED_ALLOW
34
35#include "DNA_anim_types.h"
36#include "DNA_curve_types.h"
37#include "DNA_material_types.h"
38
39/* For dereferencing pointers. */
40#include "DNA_key_types.h"
41#include "DNA_object_types.h"
42#include "DNA_vfont_types.h"
43
44#include "BKE_curve.hh"
45#include "BKE_curveprofile.h"
46#include "BKE_idtype.hh"
47#include "BKE_key.hh"
48#include "BKE_lib_id.hh"
49#include "BKE_lib_query.hh"
50#include "BKE_object_types.hh"
51#include "BKE_vfont.hh"
52
53#include "DEG_depsgraph.hh"
55
56#include "BLO_read_write.hh"
57
58namespace blender {
59
60/* globals */
61
62/* local */
63// static CLG_LogRef LOG = {"geom.curve"};
64
72
73static void curve_init_data(ID *id)
74{
75 Curve *curve = id_cast<Curve *>(id);
76
78}
79
80static void curve_copy_data(Main *bmain,
81 std::optional<Library *> owner_library,
82 ID *id_dst,
83 const ID *id_src,
84 const int flag)
85{
86 Curve *curve_dst = id_cast<Curve *>(id_dst);
87 const Curve *curve_src = id_cast<const Curve *>(id_src);
88
89 curve_dst->nurb.clear_no_delete();
90 BKE_nurbList_duplicate(&(curve_dst->nurb), &(curve_src->nurb));
91
92 curve_dst->mat = MEM_dupalloc(curve_src->mat);
93
94 curve_dst->str = MEM_dupalloc(curve_src->str);
95 curve_dst->strinfo = MEM_dupalloc(curve_src->strinfo);
96 curve_dst->tb = MEM_dupalloc(curve_src->tb);
97 curve_dst->batch_cache = nullptr;
98
99 curve_dst->bevel_profile = BKE_curveprofile_copy(curve_src->bevel_profile);
100
101 if (curve_src->key && (flag & LIB_ID_COPY_SHAPEKEY)) {
102 BKE_id_copy_in_lib(bmain,
103 owner_library,
104 &curve_src->key->id,
105 &curve_dst->id,
106 reinterpret_cast<ID **>(&curve_dst->key),
107 flag);
108 /* It has one user, but its owner reference (added in #id_copy_libmanagement_cb)
109 * is the real owner, remove the reference here, see: #159691. */
110 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
111 id_us_min(&curve_dst->key->id);
112 }
113 }
114
115 curve_dst->editnurb = nullptr;
116 curve_dst->editfont = nullptr;
117}
118
119static void curve_free_data(ID *id)
120{
121 Curve *curve = id_cast<Curve *>(id);
122
124
125 BKE_nurbList_free(&curve->nurb);
126
127 if (!curve->edit_data_from_original) {
129
131 }
132
134
135 MEM_SAFE_DELETE(curve->mat);
136 MEM_SAFE_DELETE(curve->str);
137 MEM_SAFE_DELETE(curve->strinfo);
138 MEM_SAFE_DELETE(curve->tb);
139}
140
157
158static void curve_blend_write(BlendWriter *writer, ID *id, const void *id_address)
159{
160 Curve *cu = id_cast<Curve *>(id);
161
162 /* Clean up, important in undo case to reduce false detection of changed datablocks. */
163 cu->editnurb = nullptr;
164 cu->editfont = nullptr;
165 cu->batch_cache = nullptr;
166
167 /* write LibData */
168 writer->write_id_struct(id_address, cu);
169 BKE_id_blend_write(writer, &cu->id);
170
171 /* direct data */
172 writer->write_pointer_array(cu->totcol, cu->mat);
173
174 if (cu->ob_type == OB_FONT) {
175 writer->write_string(cu->str);
176 writer->write_struct_array(cu->len_char32 + 1, cu->strinfo);
177 writer->write_struct_array(cu->totbox, cu->tb);
178 }
179 else {
180 /* is also the order of reading */
181 for (Nurb &nu : cu->nurb) {
182 writer->write_struct(&nu);
183 }
184 for (Nurb &nu : cu->nurb) {
185 if (nu.type == CU_BEZIER) {
186 writer->write_struct_array(nu.pntsu, nu.bezt);
187 }
188 else {
189 writer->write_struct_array(nu.pntsu * nu.pntsv, nu.bp);
190 if (nu.knotsu) {
191 writer->write_float_array(KNOTSU(&nu), nu.knotsu);
192 }
193 if (nu.knotsv) {
194 writer->write_float_array(KNOTSV(&nu), nu.knotsv);
195 }
196 }
197 }
198 }
199
200 if (cu->bevel_profile != nullptr) {
202 }
203}
204
205static void curve_blend_read_data(BlendDataReader *reader, ID *id)
206{
207 Curve *cu = id_cast<Curve *>(id);
208
209 BLO_read_string(reader, &cu->str);
210
211 /* Old files don't have `cu->len_char32` set. */
212 if ((cu->len_char32 == 0) && (cu->str != nullptr) && (cu->str[0] != '\0')) {
213 size_t len_bytes;
214 size_t len_char32 = BLI_strlen_utf8_ex(cu->str, &len_bytes);
215 cu->len_char32 = len_char32;
216 }
217 /* Protect against integer overflow vulnerability. */
218 CLAMP(cu->len_char32, 0, INT_MAX - 4);
219
221
222 if (!BLO_read_array(reader, &cu->strinfo, cu->len_char32 + 1)) {
223 cu->len_char32 = 0;
224 }
225 BLO_read_array_and_validate_size(reader, &cu->tb, &cu->totbox);
226
227 /* WARNING: for old files `cu->ob_type` won't be initialized,
228 * versioning detects fonts based on `cu->vfont` (which won't have run yet)
229 * so do the same here. */
230 const bool is_font = cu->ob_type ? (cu->ob_type == OB_FONT) : (cu->vfont != nullptr);
231
232 if (is_font == false) {
233 BLO_read_struct_list(reader, Nurb, &(cu->nurb));
234 }
235 else {
236 cu->nurb.first_ = cu->nurb.last_ = nullptr;
237
238 if (cu->str == nullptr) [[unlikely]] {
239 cu->len_char32 = 0;
240 cu->str = MEM_new_array_zeroed<char>(cu->len_char32 + 1, "str new");
241 }
242 if (cu->strinfo == nullptr) [[unlikely]] {
243 cu->strinfo = MEM_new_array<CharInfo>(cu->len_char32 + 1, "strinfo new");
244 }
245
246 TextBox *tb = MEM_new_array<TextBox>(MAXTEXTBOX, "TextBoxread");
247 if (cu->tb) {
248 memcpy(tb, cu->tb, cu->totbox * sizeof(TextBox));
249 MEM_delete(cu->tb);
250 cu->tb = tb;
251 }
252 else {
253 cu->totbox = 1;
254 cu->actbox = 1;
255 cu->tb = tb;
256 cu->tb[0].w = cu->linewidth;
257 }
258 if (cu->wordspace == 0.0f) {
259 cu->wordspace = 1.0f;
260 }
261 }
262
263 cu->editnurb = nullptr;
264 cu->editfont = nullptr;
265 cu->batch_cache = nullptr;
266
267 for (Nurb &nu : cu->nurb) {
268 /* Only read the arrays that were written, so we don't get errors due
269 * to mismatched size. Checking nu.type to find the right arrays to read
270 * here is tricky as it is affected by versioning. */
271 bool ok = true;
272 if (nu.bezt) {
273 ok &= BLO_read_array(reader, &nu.bezt, nu.pntsu);
274 }
275 if (nu.bp) {
276 ok &= BLO_read_array(reader, &nu.bp, nu.pntsu, nu.pntsv);
277 }
278 if (nu.knotsu) {
279 ok &= BLO_read_array(reader, &nu.knotsu, KNOTSU(&nu));
280 }
281 if (nu.knotsv) {
282 ok &= BLO_read_array(reader, &nu.knotsv, KNOTSV(&nu));
283 }
284 if (!ok) {
285 nu.pntsu = 0;
286 nu.pntsv = 0;
287 }
288 if (is_font == false) {
289 nu.charidx = 0;
290 }
291 }
293
295 if (cu->bevel_profile != nullptr) {
297 }
298}
299
301 .id_code = Curve::id_type,
302 .id_filter = FILTER_ID_CU_LEGACY,
303 .dependencies_id_types = FILTER_ID_OB | FILTER_ID_MA | FILTER_ID_VF | FILTER_ID_KE,
304 .main_listbase_index = INDEX_ID_CU_LEGACY,
305 .struct_size = sizeof(Curve),
306 .name = "Curve",
307 .name_plural = N_("curves"),
308 .translation_context = BLT_I18NCONTEXT_ID_CURVE_LEGACY,
310 .asset_type_info = nullptr,
311
312 .init_data = curve_init_data,
313 .copy_data = curve_copy_data,
314 .free_data = curve_free_data,
315 .make_local = nullptr,
316 .foreach_id = curve_foreach_id,
317 .foreach_cache = nullptr,
318 .foreach_path = nullptr,
319 .foreach_working_space_color = nullptr,
320 .foreach_asset_weak_reference = nullptr,
321 .owner_pointer_get = nullptr,
322
323 .blend_write = curve_blend_write,
324 .blend_read_data = curve_blend_read_data,
325 .blend_read_after_liblink = nullptr,
326
327 .blend_read_undo_preserve = nullptr,
328
329 .lib_override_apply_post = nullptr,
330};
331
333{
334 if (cu->editfont) {
335 EditFont *ef = cu->editfont;
336
337 if (ef->textbuf) {
338 MEM_delete(ef->textbuf);
339 }
340 if (ef->textbufinfo) {
341 MEM_delete(ef->textbufinfo);
342 }
343 if (ef->selboxes) {
344 MEM_delete(ef->selboxes);
345 }
346
347 MEM_delete(ef);
348 cu->editfont = nullptr;
349 }
350}
351
353{
354 MEM_delete_void(index->orig_cv);
355 MEM_delete(index);
356}
357
359{
360 BLI_assert(keyindex != nullptr);
361 if (CVKeyIndex *index = keyindex->pop_default(cv, nullptr)) {
363 }
364}
365
367{
368 if (!(*keyindex)) {
369 return;
370 }
371 for (CVKeyIndex *index : (*keyindex)->values()) {
373 }
374 MEM_delete(*keyindex);
375 *keyindex = nullptr;
376}
377
379{
380 if (cu->editnurb) {
383 MEM_delete(cu->editnurb);
384 cu->editnurb = nullptr;
385 }
386}
387
388void BKE_curve_init(Curve *cu, const ObjectType curve_type)
389{
390 curve_init_data(&cu->id);
391
392 cu->ob_type = curve_type;
393
394 if (cu->ob_type == OB_FONT) {
395 cu->flag |= CU_FRONT | CU_BACK;
398 cu->vfont = cu->vfontb = cu->vfonti = cu->vfontbi = BKE_vfont_builtin_ensure();
399 cu->vfont->id.us += 4;
400
401 const char *str = DATA_("Text");
402 size_t len_bytes;
403 size_t len_char32 = BLI_strlen_utf8_ex(str, &len_bytes);
404
405 cu->str = MEM_new_array_uninitialized<char>(len_bytes + 1, "str");
406 memcpy(cu->str, str, len_bytes + 1);
407 BLI_assert(cu->str[len_bytes] == '\0');
408
409 cu->len = len_bytes;
410 cu->len_char32 = cu->pos = len_char32;
411
412 cu->strinfo = MEM_new_array<CharInfo>(len_char32 + 1, "strinfo new");
413
414 cu->totbox = cu->actbox = 1;
415 cu->tb = MEM_new_array<TextBox>(MAXTEXTBOX, "textbox");
416 cu->tb[0].w = cu->tb[0].h = 0.0;
417 }
418 else if (cu->ob_type == OB_SURF) {
419 cu->flag |= CU_3D;
420 cu->resolu = 4;
421 cu->resolv = 4;
422 }
423 cu->bevel_profile = nullptr;
424 /* Initialize the offset to 1.0, to compensate for it being set to -1.0
425 * in the property getter. */
426 cu->offset = 1.0f;
427}
428
429Curve *BKE_curve_add(Main *bmain, const char *name, ObjectType type)
430{
431 Curve *cu;
432
433 /* We cannot use #BKE_id_new here as we need some custom initialization code. */
434 cu = static_cast<Curve *>(BKE_libblock_alloc(bmain, ID_CU_LEGACY, name, 0));
435
436 BKE_curve_init(cu, type);
437
438 return cu;
439}
440
441ListBaseT<Nurb> *BKE_curve_editNurbs_get(Curve *cu)
442{
443 if (cu->editnurb) {
444 return &cu->editnurb->nurbs;
445 }
446
447 return nullptr;
448}
449
450const ListBaseT<Nurb> *BKE_curve_editNurbs_get_for_read(const Curve *cu)
451{
452 if (cu->editnurb) {
453 return &cu->editnurb->nurbs;
454 }
455
456 return nullptr;
457}
458
460{
461 ListBaseT<Nurb> *nurbs = BKE_curve_nurbs_get(cu);
462 bool is_2d = CU_IS_2D(cu);
463
464 for (Nurb &nu : *nurbs) {
465 if (is_2d) {
467 }
468
469 /* since the handles are moved they need to be auto-located again */
470 if (nu.type == CU_BEZIER) {
472 }
473 }
474}
475
476void BKE_curve_type_test(Object *ob, const bool dimension_update)
477{
478 Curve *cu = id_cast<Curve *>(ob->data);
479 ob->type = cu->ob_type;
480
481 if (dimension_update) {
482 if (ob->type == OB_CURVES_LEGACY) {
483 if (CU_IS_2D(cu)) {
485 }
486 }
487 }
488}
489
491{
493 std::optional<Bounds<float3>> bounds = BKE_curve_minmax(cu, true);
494 if (!bounds) {
496 }
497
498 float texspace_location[3], texspace_size[3];
499 mid_v3_v3v3(texspace_location, bounds->min, bounds->max);
500
501 texspace_size[0] = (bounds->max[0] - bounds->min[0]) / 2.0f;
502 texspace_size[1] = (bounds->max[1] - bounds->min[1]) / 2.0f;
503 texspace_size[2] = (bounds->max[2] - bounds->min[2]) / 2.0f;
504
505 for (int a = 0; a < 3; a++) {
506 if (texspace_size[a] == 0.0f) {
507 texspace_size[a] = 1.0f;
508 }
509 else if (texspace_size[a] > 0.0f && texspace_size[a] < 0.00001f) {
510 texspace_size[a] = 0.00001f;
511 }
512 else if (texspace_size[a] < 0.0f && texspace_size[a] > -0.00001f) {
513 texspace_size[a] = -0.00001f;
514 }
515 }
516
517 copy_v3_v3(cu->texspace_location, texspace_location);
518 copy_v3_v3(cu->texspace_size, texspace_size);
519
521 }
522}
523
532
533bool BKE_nurbList_index_get_co(ListBaseT<Nurb> *nurb, const int index, float r_co[3])
534{
535 int tot = 0;
536
537 for (Nurb &nu : *nurb) {
538 int tot_nu;
539 if (nu.type == CU_BEZIER) {
540 tot_nu = nu.pntsu;
541 if (index - tot < tot_nu) {
542 copy_v3_v3(r_co, nu.bezt[index - tot].vec[1]);
543 return true;
544 }
545 }
546 else {
547 tot_nu = nu.pntsu * nu.pntsv;
548 if (index - tot < tot_nu) {
549 copy_v3_v3(r_co, nu.bp[index - tot].vec);
550 return true;
551 }
552 }
553 tot += tot_nu;
554 }
555
556 return false;
557}
558
559int BKE_nurbList_verts_count(const ListBaseT<Nurb> *nurb)
560{
561 int tot = 0;
562
563 for (const Nurb &nu : *nurb) {
564 if (nu.bezt) {
565 tot += 3 * nu.pntsu;
566 }
567 else if (nu.bp) {
568 tot += nu.pntsu * nu.pntsv;
569 }
570 }
571
572 return tot;
573}
574
575int BKE_nurbList_verts_count_without_handles(const ListBaseT<Nurb> *nurb)
576{
577 int tot = 0;
578
579 for (Nurb &nu : *nurb) {
580 if (nu.bezt) {
581 tot += nu.pntsu;
582 }
583 else if (nu.bp) {
584 tot += nu.pntsu * nu.pntsv;
585 }
586 }
587
588 return tot;
589}
590
591/* **************** NURBS ROUTINES ******************** */
592
594{
595 if (nu == nullptr) {
596 return;
597 }
598
599 if (nu->bezt) {
600 MEM_delete(nu->bezt);
601 }
602 nu->bezt = nullptr;
603 if (nu->bp) {
604 MEM_delete(nu->bp);
605 }
606 nu->bp = nullptr;
607 if (nu->knotsu) {
608 MEM_delete(nu->knotsu);
609 }
610 nu->knotsu = nullptr;
611 if (nu->knotsv) {
612 MEM_delete(nu->knotsv);
613 }
614 nu->knotsv = nullptr;
615
616 MEM_delete(nu);
617}
618
619void BKE_nurbList_free(ListBaseT<Nurb> *lb)
620{
621 if (lb == nullptr) {
622 return;
623 }
624
625 for (Nurb &nu : lb->items_mutable()) {
626 BKE_nurb_free(&nu);
627 }
628 lb->clear_no_delete();
629}
630
632{
633 Nurb *newnu;
634 int len;
635
636 newnu = MEM_new<Nurb>("duplicateNurb");
637 if (newnu == nullptr) {
638 return nullptr;
639 }
640 *newnu = dna::shallow_copy(*nu);
641
642 if (nu->bezt) {
643 newnu->bezt = MEM_new_array_uninitialized<BezTriple>(size_t(nu->pntsu), "duplicateNurb2");
644 memcpy(newnu->bezt, nu->bezt, nu->pntsu * sizeof(BezTriple));
645 }
646 else {
647 len = nu->pntsu * nu->pntsv;
648 newnu->bp = MEM_new_array_uninitialized<BPoint>(size_t(len), "duplicateNurb3");
649 memcpy(newnu->bp, nu->bp, len * sizeof(BPoint));
650
651 newnu->knotsu = newnu->knotsv = nullptr;
652
653 if (nu->knotsu) {
654 len = KNOTSU(nu);
655 if (len) {
656 newnu->knotsu = MEM_new_array_uninitialized<float>(size_t(len), "duplicateNurb4");
657 memcpy(newnu->knotsu, nu->knotsu, sizeof(float) * len);
658 }
659 }
660 if (nu->pntsv > 1 && nu->knotsv) {
661 len = KNOTSV(nu);
662 if (len) {
663 newnu->knotsv = MEM_new_array_uninitialized<float>(size_t(len), "duplicateNurb5");
664 memcpy(newnu->knotsv, nu->knotsv, sizeof(float) * len);
665 }
666 }
667 }
668 return newnu;
669}
670
671Nurb *BKE_nurb_copy(Nurb *src, int pntsu, int pntsv)
672{
673 Nurb *newnu = MEM_new<Nurb>("copyNurb");
674 *newnu = dna::shallow_copy(*src);
675
676 if (pntsu == 1) {
677 std::swap(pntsu, pntsv);
678 }
679 newnu->pntsu = pntsu;
680 newnu->pntsv = pntsv;
681
682 /* caller can manually handle these arrays */
683 newnu->knotsu = nullptr;
684 newnu->knotsv = nullptr;
685
686 if (src->bezt) {
687 newnu->bezt = MEM_new_array_uninitialized<BezTriple>(size_t(pntsu) * size_t(pntsv),
688 "copyNurb2");
689 }
690 else {
691 newnu->bp = MEM_new_array_uninitialized<BPoint>(size_t(pntsu) * size_t(pntsv), "copyNurb3");
692 }
693
694 return newnu;
695}
696
697void BKE_nurbList_duplicate(ListBaseT<Nurb> *lb1, const ListBaseT<Nurb> *lb2)
698{
700
701 for (const Nurb &nu : *lb2) {
702 Nurb *nurb_new = BKE_nurb_duplicate(&nu);
703 BLI_addtail(lb1, nurb_new);
704 }
705}
706
708{
709 BezTriple *bezt;
710 BPoint *bp;
711 int a;
712
713 if (nu->type == CU_BEZIER) {
714 a = nu->pntsu;
715 bezt = nu->bezt;
716 while (a--) {
717 bezt->vec[0][2] = 0.0;
718 bezt->vec[1][2] = 0.0;
719 bezt->vec[2][2] = 0.0;
720 bezt++;
721 }
722 }
723 else {
724 a = nu->pntsu * nu->pntsv;
725 bp = nu->bp;
726 while (a--) {
727 bp->vec[2] = 0.0;
728 bp++;
729 }
730 }
731}
732
733static void calc_nurb_minmax(const Nurb *nu, bool use_radius, float min[3], float max[3])
734{
735 BezTriple *bezt;
736 BPoint *bp;
737 int a;
738 float point[3];
739
740 if (nu->type == CU_BEZIER) {
741 a = nu->pntsu;
742 bezt = nu->bezt;
743 while (a--) {
744 if (use_radius) {
745 float radius_vector[3];
746 radius_vector[0] = radius_vector[1] = radius_vector[2] = bezt->radius;
747
748 add_v3_v3v3(point, bezt->vec[1], radius_vector);
749 minmax_v3v3_v3(min, max, point);
750
751 sub_v3_v3v3(point, bezt->vec[1], radius_vector);
752 minmax_v3v3_v3(min, max, point);
753 }
754 else {
755 minmax_v3v3_v3(min, max, bezt->vec[1]);
756 }
757 minmax_v3v3_v3(min, max, bezt->vec[0]);
758 minmax_v3v3_v3(min, max, bezt->vec[2]);
759 bezt++;
760 }
761 }
762 else {
763 a = nu->pntsu * nu->pntsv;
764 bp = nu->bp;
765 while (a--) {
766 if (nu->pntsv == 1 && use_radius) {
767 float radius_vector[3];
768 radius_vector[0] = radius_vector[1] = radius_vector[2] = bp->radius;
769
770 add_v3_v3v3(point, bp->vec, radius_vector);
771 minmax_v3v3_v3(min, max, point);
772
773 sub_v3_v3v3(point, bp->vec, radius_vector);
774 minmax_v3v3_v3(min, max, point);
775 }
776 else {
777 /* Surfaces doesn't use bevel, so no need to take radius into account. */
778 minmax_v3v3_v3(min, max, bp->vec);
779 }
780 bp++;
781 }
782 }
783}
784
785float BKE_nurb_calc_length(const Nurb *nu, int resolution)
786{
787 BezTriple *bezt, *prevbezt;
788 BPoint *bp, *prevbp;
789 int a, b;
790 float length = 0.0f;
791 int resolu = resolution ? resolution : nu->resolu;
792 int pntsu = nu->pntsu;
793 float *points, *pntsit, *prevpntsit;
794
795 if (nu->type == CU_POLY) {
796 a = nu->pntsu - 1;
797 bp = nu->bp;
798 if (nu->flagu & CU_NURB_CYCLIC) {
799 a++;
800 prevbp = nu->bp + (nu->pntsu - 1);
801 }
802 else {
803 prevbp = bp;
804 bp++;
805 }
806
807 while (a--) {
808 length += len_v3v3(prevbp->vec, bp->vec);
809 prevbp = bp;
810 bp++;
811 }
812 }
813 else if (nu->type == CU_BEZIER) {
814 points = MEM_new_array_uninitialized<float>(3 * (size_t(resolu) + 1), "getLength_bezier");
815 a = nu->pntsu - 1;
816 bezt = nu->bezt;
817 if (nu->flagu & CU_NURB_CYCLIC) {
818 a++;
819 prevbezt = nu->bezt + (nu->pntsu - 1);
820 }
821 else {
822 prevbezt = bezt;
823 bezt++;
824 }
825
826 while (a--) {
827 if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
828 length += len_v3v3(prevbezt->vec[1], bezt->vec[1]);
829 }
830 else {
831 for (int j = 0; j < 3; j++) {
832 BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],
833 prevbezt->vec[2][j],
834 bezt->vec[0][j],
835 bezt->vec[1][j],
836 points + j,
837 resolu,
838 sizeof(float[3]));
839 }
840
841 prevpntsit = pntsit = points;
842 b = resolu;
843 while (b--) {
844 pntsit += 3;
845 length += len_v3v3(prevpntsit, pntsit);
846 prevpntsit = pntsit;
847 }
848 }
849 prevbezt = bezt;
850 bezt++;
851 }
852
853 MEM_delete(points);
854 }
855 else if (nu->type == CU_NURBS) {
856 if (nu->pntsv == 1) {
857 /* important to zero for BKE_nurb_makeCurve. */
858 points = MEM_new_array_zeroed<float>(3 * size_t(pntsu) * size_t(resolu), "getLength_nurbs");
859
860 BKE_nurb_makeCurve(nu, points, nullptr, nullptr, nullptr, resolu, sizeof(float[3]));
861
862 if (nu->flagu & CU_NURB_CYCLIC) {
863 b = pntsu * resolu + 1;
864 prevpntsit = points + 3 * (pntsu * resolu - 1);
865 pntsit = points;
866 }
867 else {
868 b = (pntsu - 1) * resolu;
869 prevpntsit = points;
870 pntsit = points + 3;
871 }
872
873 while (--b > 0) {
874 length += len_v3v3(prevpntsit, pntsit);
875 prevpntsit = pntsit;
876 pntsit += 3;
877 }
878
879 MEM_delete(points);
880 }
881 }
882
883 return length;
884}
885
886void BKE_nurb_points_add(Nurb *nu, int number)
887{
888 nu->bp = static_cast<BPoint *>(
889 MEM_realloc_zeroed(nu->bp, (nu->pntsu + number) * sizeof(BPoint)));
890
891 BPoint *bp;
892 int i;
893 for (i = 0, bp = &nu->bp[nu->pntsu]; i < number; i++, bp++) {
894 bp->radius = 1.0f;
895 }
896
897 nu->pntsu += number;
898}
899
900void BKE_nurb_bezierPoints_add(Nurb *nu, int number)
901{
902 BezTriple *bezt;
903 int i;
904
905 nu->bezt = static_cast<BezTriple *>(
906 MEM_realloc_zeroed(nu->bezt, (nu->pntsu + number) * sizeof(BezTriple)));
907
908 for (i = 0, bezt = &nu->bezt[nu->pntsu]; i < number; i++, bezt++) {
909 bezt->radius = 1.0f;
910 }
911
912 nu->pntsu += number;
913}
914
915int BKE_nurb_index_from_uv(Nurb *nu, int u, int v)
916{
917 const int totu = nu->pntsu;
918 const int totv = nu->pntsv;
919
920 if (nu->flagu & CU_NURB_CYCLIC) {
921 u = mod_i(u, totu);
922 }
923 else if (u < 0 || u >= totu) {
924 return -1;
925 }
926
927 if (nu->flagv & CU_NURB_CYCLIC) {
928 v = mod_i(v, totv);
929 }
930 else if (v < 0 || v >= totv) {
931 return -1;
932 }
933
934 return (v * totu) + u;
935}
936
937void BKE_nurb_index_to_uv(Nurb *nu, int index, int *r_u, int *r_v)
938{
939 const int totu = nu->pntsu;
940 const int totv = nu->pntsv;
941 BLI_assert(index >= 0 && index < (nu->pntsu * nu->pntsv));
942 *r_u = (index % totu);
943 *r_v = (index / totu) % totv;
944}
945
947{
948 BezTriple *bezt_next;
949
950 BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
951
952 if (bezt == &nu->bezt[nu->pntsu - 1]) {
953 if (nu->flagu & CU_NURB_CYCLIC) {
954 bezt_next = nu->bezt;
955 }
956 else {
957 bezt_next = nullptr;
958 }
959 }
960 else {
961 bezt_next = bezt + 1;
962 }
963
964 return bezt_next;
965}
966
968{
969 BPoint *bp_next;
970
971 BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
972
973 if (bp == &nu->bp[nu->pntsu - 1]) {
974 if (nu->flagu & CU_NURB_CYCLIC) {
975 bp_next = nu->bp;
976 }
977 else {
978 bp_next = nullptr;
979 }
980 }
981 else {
982 bp_next = bp + 1;
983 }
984
985 return bp_next;
986}
987
989{
990 BezTriple *bezt_prev;
991
992 BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
993 BLI_assert(nu->pntsv <= 1);
994
995 if (bezt == nu->bezt) {
996 if (nu->flagu & CU_NURB_CYCLIC) {
997 bezt_prev = &nu->bezt[nu->pntsu - 1];
998 }
999 else {
1000 bezt_prev = nullptr;
1001 }
1002 }
1003 else {
1004 bezt_prev = bezt - 1;
1005 }
1006
1007 return bezt_prev;
1008}
1009
1011{
1012 BPoint *bp_prev;
1013
1014 BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
1015 BLI_assert(nu->pntsv == 1);
1016
1017 if (bp == nu->bp) {
1018 if (nu->flagu & CU_NURB_CYCLIC) {
1019 bp_prev = &nu->bp[nu->pntsu - 1];
1020 }
1021 else {
1022 bp_prev = nullptr;
1023 }
1024 }
1025 else {
1026 bp_prev = bp - 1;
1027 }
1028
1029 return bp_prev;
1030}
1031
1032void BKE_nurb_bezt_calc_normal(Nurb * /*nu*/, BezTriple *bezt, float r_normal[3])
1033{
1034 /* calculate the axis matrix from the spline */
1035 float dir_prev[3], dir_next[3];
1036
1037 sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
1038 sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
1039
1040 normalize_v3(dir_prev);
1041 normalize_v3(dir_next);
1042
1043 add_v3_v3v3(r_normal, dir_prev, dir_next);
1044 normalize_v3(r_normal);
1045}
1046
1047void BKE_nurb_bezt_calc_plane(Nurb *nu, BezTriple *bezt, float r_plane[3])
1048{
1049 float dir_prev[3], dir_next[3];
1050
1051 sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
1052 sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
1053
1054 normalize_v3(dir_prev);
1055 normalize_v3(dir_next);
1056
1057 cross_v3_v3v3(r_plane, dir_prev, dir_next);
1058 if (normalize_v3(r_plane) < FLT_EPSILON) {
1059 BezTriple *bezt_prev = BKE_nurb_bezt_get_prev(nu, bezt);
1060 BezTriple *bezt_next = BKE_nurb_bezt_get_next(nu, bezt);
1061
1062 if (bezt_prev) {
1063 sub_v3_v3v3(dir_prev, bezt_prev->vec[1], bezt->vec[1]);
1064 normalize_v3(dir_prev);
1065 }
1066 if (bezt_next) {
1067 sub_v3_v3v3(dir_next, bezt->vec[1], bezt_next->vec[1]);
1068 normalize_v3(dir_next);
1069 }
1070 cross_v3_v3v3(r_plane, dir_prev, dir_next);
1071 }
1072
1073 /* matches with bones more closely */
1074 {
1075 float dir_mid[3], tvec[3];
1076 add_v3_v3v3(dir_mid, dir_prev, dir_next);
1077 cross_v3_v3v3(tvec, r_plane, dir_mid);
1078 copy_v3_v3(r_plane, tvec);
1079 }
1080
1081 normalize_v3(r_plane);
1082}
1083
1084void BKE_nurb_bpoint_calc_normal(Nurb *nu, BPoint *bp, float r_normal[3])
1085{
1086 BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp);
1087 BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp);
1088
1089 zero_v3(r_normal);
1090
1091 if (bp_prev) {
1092 float dir_prev[3];
1093 sub_v3_v3v3(dir_prev, bp_prev->vec, bp->vec);
1094 normalize_v3(dir_prev);
1095 add_v3_v3(r_normal, dir_prev);
1096 }
1097 if (bp_next) {
1098 float dir_next[3];
1099 sub_v3_v3v3(dir_next, bp->vec, bp_next->vec);
1100 normalize_v3(dir_next);
1101 add_v3_v3(r_normal, dir_next);
1102 }
1103
1104 normalize_v3(r_normal);
1105}
1106
1107void BKE_nurb_bpoint_calc_plane(Nurb *nu, BPoint *bp, float r_plane[3])
1108{
1109 BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp);
1110 BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp);
1111
1112 float dir_prev[3] = {0.0f}, dir_next[3] = {0.0f};
1113
1114 if (bp_prev) {
1115 sub_v3_v3v3(dir_prev, bp_prev->vec, bp->vec);
1116 normalize_v3(dir_prev);
1117 }
1118 if (bp_next) {
1119 sub_v3_v3v3(dir_next, bp->vec, bp_next->vec);
1120 normalize_v3(dir_next);
1121 }
1122 cross_v3_v3v3(r_plane, dir_prev, dir_next);
1123
1124 /* matches with bones more closely */
1125 {
1126 float dir_mid[3], tvec[3];
1127 add_v3_v3v3(dir_mid, dir_prev, dir_next);
1128 cross_v3_v3v3(tvec, r_plane, dir_mid);
1129 copy_v3_v3(r_plane, tvec);
1130 }
1131
1132 normalize_v3(r_plane);
1133}
1134
1135/* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
1136
1137static void calcknots(float *knots, const int pnts, const short order, const short flag)
1138{
1139 const bool is_cyclic = flag & CU_NURB_CYCLIC;
1140 const bool is_bezier = flag & CU_NURB_BEZIER;
1141 const bool is_end_point = flag & CU_NURB_ENDPOINT;
1142 /* Inner knots are always repeated once except on Bezier case. */
1143 const int repeat_inner = is_bezier ? order - 1 : 1;
1144 /* How many times to repeat 0.0 at the beginning of knot. */
1145 const int head = is_end_point ? (order - (is_cyclic ? 1 : 0)) :
1146 (is_bezier ? min_ii(2, repeat_inner) : 1);
1147 /* Number of knots replicating widths of the starting knots.
1148 * Covers both Cyclic and EndPoint cases. */
1149 const int tail = is_cyclic ? 2 * order - 1 : (is_end_point ? order : 0);
1150
1151 const int knot_count = pnts + order + (is_cyclic ? order - 1 : 0);
1152
1153 int r = head;
1154 float current = 0.0f;
1155
1156 const int offset = is_end_point && is_cyclic ? 1 : 0;
1157 if (offset) {
1158 knots[0] = current;
1159 current += 1.0f;
1160 }
1161
1162 for (const int i : IndexRange(offset, knot_count - offset - tail)) {
1163 knots[i] = current;
1164 r--;
1165 if (r == 0) {
1166 current += 1.0f;
1167 r = repeat_inner;
1168 }
1169 }
1170
1171 const int tail_index = knot_count - tail;
1172 for (const int i : IndexRange(tail)) {
1173 knots[tail_index + i] = current + (knots[i] - knots[0]);
1174 }
1175}
1176
1177static void makeknots(Nurb *nu, short uv)
1178{
1179 if (nu->type == CU_NURBS) {
1180 if (uv == 1) {
1181 if (nu->knotsu) {
1182 MEM_delete(nu->knotsu);
1183 }
1184 if (BKE_nurb_check_valid_u(nu)) {
1185 nu->knotsu = MEM_new_array_zeroed<float>(size_t(KNOTSU(nu)) + 1, "makeknots");
1186 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
1187 nu->flagu &= ~CU_NURB_CUSTOM;
1188 }
1189 else {
1190 nu->knotsu = nullptr;
1191 }
1192 }
1193 else if (uv == 2) {
1194 if (nu->knotsv) {
1195 MEM_delete(nu->knotsv);
1196 }
1197 if (BKE_nurb_check_valid_v(nu)) {
1198 nu->knotsv = MEM_new_array_zeroed<float>(size_t(KNOTSV(nu)) + 1, "makeknots");
1199 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
1200 nu->flagv &= ~CU_NURB_CUSTOM;
1201 }
1202 else {
1203 nu->knotsv = nullptr;
1204 }
1205 }
1206 }
1207}
1208
1210{
1211 nu->knotsu = MEM_new_array_zeroed<float>(KNOTSU(nu) + 1, __func__);
1212}
1213
1215{
1216 makeknots(nu, 1);
1217}
1218
1220{
1221 makeknots(nu, 2);
1222}
1223
1224static void basisNurb(
1225 float t, short order, int pnts, const float *knots, float *basis, int *start, int *end)
1226{
1227 float d, e;
1228 int i, i1 = 0, i2 = 0, j, orderpluspnts, opp2, o2;
1229
1230 orderpluspnts = order + pnts;
1231 opp2 = orderpluspnts - 1;
1232
1233 /* this is for float inaccuracy */
1234 if (t < knots[0]) {
1235 t = knots[0];
1236 }
1237 else if (t > knots[opp2]) {
1238 t = knots[opp2];
1239 }
1240
1241 /* this part is order '1' */
1242 o2 = order + 1;
1243 for (i = 0; i < opp2; i++) {
1244 if (knots[i] != knots[i + 1] && t >= knots[i] && t <= knots[i + 1]) {
1245 basis[i] = 1.0;
1246 i1 = i - o2;
1247 i1 = std::max(i1, 0);
1248 i2 = i;
1249 i++;
1250 while (i < opp2) {
1251 basis[i] = 0.0;
1252 i++;
1253 }
1254 break;
1255 }
1256
1257 basis[i] = 0.0;
1258 }
1259 basis[i] = 0.0;
1260
1261 /* this is order 2, 3, ... */
1262 for (j = 2; j <= order; j++) {
1263
1264 if (i2 + j >= orderpluspnts) {
1265 i2 = opp2 - j;
1266 }
1267
1268 for (i = i1; i <= i2; i++) {
1269 if (basis[i] != 0.0f) {
1270 d = ((t - knots[i]) * basis[i]) / (knots[i + j - 1] - knots[i]);
1271 }
1272 else {
1273 d = 0.0f;
1274 }
1275
1276 if (basis[i + 1] != 0.0f) {
1277 e = ((knots[i + j] - t) * basis[i + 1]) / (knots[i + j] - knots[i + 1]);
1278 }
1279 else {
1280 e = 0.0;
1281 }
1282
1283 basis[i] = d + e;
1284 }
1285 }
1286
1287 *start = 1000;
1288 *end = 0;
1289
1290 for (i = i1; i <= i2; i++) {
1291 if (basis[i] > 0.0f) {
1292 *end = i;
1293 if (*start == 1000) {
1294 *start = i;
1295 }
1296 }
1297 }
1298}
1299
1300void BKE_nurb_makeFaces(const Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
1301{
1302 BPoint *bp;
1303 float *basisu, *basis, *basisv, *sum, *fp, *in;
1304 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
1305 int i, j, iofs, jofs, cycl, len, curu, curv;
1306 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
1307
1308 int totu = nu->pntsu * resolu, totv = nu->pntsv * resolv;
1309
1310 if (nu->knotsu == nullptr || nu->knotsv == nullptr) {
1311 return;
1312 }
1313 if (nu->orderu > nu->pntsu) {
1314 return;
1315 }
1316 if (nu->orderv > nu->pntsv) {
1317 return;
1318 }
1319 if (coord_array == nullptr) {
1320 return;
1321 }
1322
1323 /* allocate and initialize */
1324 len = totu * totv;
1325 if (len == 0) {
1326 return;
1327 }
1328
1329 sum = MEM_new_array_zeroed<float>(len, "makeNurbfaces1");
1330
1331 bp = nu->bp;
1332 i = nu->pntsu * nu->pntsv;
1333 ratcomp = 0;
1334 while (i--) {
1335 if (bp->vec[3] != 1.0f) {
1336 ratcomp = 1;
1337 break;
1338 }
1339 bp++;
1340 }
1341
1342 fp = nu->knotsu;
1343 ustart = fp[nu->orderu - 1];
1344 if (nu->flagu & CU_NURB_CYCLIC) {
1345 uend = fp[nu->pntsu + nu->orderu - 1];
1346 }
1347 else {
1348 uend = fp[nu->pntsu];
1349 }
1350 ustep = (uend - ustart) / ((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
1351
1352 basisu = MEM_new_array_uninitialized<float>(size_t(KNOTSU(nu)), "makeNurbfaces3");
1353
1354 fp = nu->knotsv;
1355 vstart = fp[nu->orderv - 1];
1356
1357 if (nu->flagv & CU_NURB_CYCLIC) {
1358 vend = fp[nu->pntsv + nu->orderv - 1];
1359 }
1360 else {
1361 vend = fp[nu->pntsv];
1362 }
1363 vstep = (vend - vstart) / ((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
1364
1365 len = KNOTSV(nu);
1366 basisv = MEM_new_array_uninitialized<float>(size_t(len) * size_t(totv), "makeNurbfaces3");
1367 jstart = MEM_new_array_uninitialized<int>(size_t(totv), "makeNurbfaces4");
1368 jend = MEM_new_array_uninitialized<int>(size_t(totv), "makeNurbfaces5");
1369
1370 /* Pre-calculation of `basisv` and `jstart`, `jend`. */
1371 if (nu->flagv & CU_NURB_CYCLIC) {
1372 cycl = nu->orderv - 1;
1373 }
1374 else {
1375 cycl = 0;
1376 }
1377 v = vstart;
1378 basis = basisv;
1379 curv = totv;
1380 while (curv--) {
1381 basisNurb(v, nu->orderv, nu->pntsv + cycl, nu->knotsv, basis, jstart + curv, jend + curv);
1382 basis += KNOTSV(nu);
1383 v += vstep;
1384 }
1385
1386 if (nu->flagu & CU_NURB_CYCLIC) {
1387 cycl = nu->orderu - 1;
1388 }
1389 else {
1390 cycl = 0;
1391 }
1392 in = coord_array;
1393 u = ustart;
1394 curu = totu;
1395 while (curu--) {
1396 basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
1397
1398 basis = basisv;
1399 curv = totv;
1400 while (curv--) {
1401 jsta = jstart[curv];
1402 jen = jend[curv];
1403
1404 /* calculate sum */
1405 sumdiv = 0.0;
1406 fp = sum;
1407
1408 for (j = jsta; j <= jen; j++) {
1409
1410 if (j >= nu->pntsv) {
1411 jofs = (j - nu->pntsv);
1412 }
1413 else {
1414 jofs = j;
1415 }
1416 bp = nu->bp + nu->pntsu * jofs + istart - 1;
1417
1418 for (i = istart; i <= iend; i++, fp++) {
1419 if (i >= nu->pntsu) {
1420 iofs = i - nu->pntsu;
1421 bp = nu->bp + nu->pntsu * jofs + iofs;
1422 }
1423 else {
1424 bp++;
1425 }
1426
1427 if (ratcomp) {
1428 *fp = basisu[i] * basis[j] * bp->vec[3];
1429 sumdiv += *fp;
1430 }
1431 else {
1432 *fp = basisu[i] * basis[j];
1433 }
1434 }
1435 }
1436
1437 if (ratcomp) {
1438 fp = sum;
1439 for (j = jsta; j <= jen; j++) {
1440 for (i = istart; i <= iend; i++, fp++) {
1441 *fp /= sumdiv;
1442 }
1443 }
1444 }
1445
1446 zero_v3(in);
1447
1448 /* one! (1.0) real point now */
1449 fp = sum;
1450 for (j = jsta; j <= jen; j++) {
1451
1452 if (j >= nu->pntsv) {
1453 jofs = (j - nu->pntsv);
1454 }
1455 else {
1456 jofs = j;
1457 }
1458 bp = nu->bp + nu->pntsu * jofs + istart - 1;
1459
1460 for (i = istart; i <= iend; i++, fp++) {
1461 if (i >= nu->pntsu) {
1462 iofs = i - nu->pntsu;
1463 bp = nu->bp + nu->pntsu * jofs + iofs;
1464 }
1465 else {
1466 bp++;
1467 }
1468
1469 if (*fp != 0.0f) {
1470 madd_v3_v3fl(in, bp->vec, *fp);
1471 }
1472 }
1473 }
1474
1475 in += 3;
1476 basis += KNOTSV(nu);
1477 }
1478 u += ustep;
1479 if (rowstride != 0) {
1480 in = reinterpret_cast<float *>((reinterpret_cast<uchar *>(in)) +
1481 (rowstride - 3 * totv * sizeof(*in)));
1482 }
1483 }
1484
1485 /* free */
1486 MEM_delete(sum);
1487 MEM_delete(basisu);
1488 MEM_delete(basisv);
1489 MEM_delete(jstart);
1490 MEM_delete(jend);
1491}
1492
1494 float *coord_array,
1495 float *tilt_array,
1496 float *radius_array,
1497 float *weight_array,
1498 int resolu,
1499 int stride)
1500{
1501 const float eps = 1e-6f;
1502 BPoint *bp;
1503 float u, ustart, uend, ustep, sumdiv;
1504 float *basisu, *sum, *fp;
1505 float *coord_fp = coord_array, *tilt_fp = tilt_array, *radius_fp = radius_array,
1506 *weight_fp = weight_array;
1507 int i, len, istart, iend, cycl;
1508
1509 if (nu->knotsu == nullptr) {
1510 return;
1511 }
1512 if (nu->orderu > nu->pntsu) {
1513 return;
1514 }
1515 if (coord_array == nullptr) {
1516 return;
1517 }
1518
1519 /* allocate and initialize */
1520 len = nu->pntsu;
1521 if (len == 0) {
1522 return;
1523 }
1524 sum = MEM_new_array_zeroed<float>(len, "makeNurbcurve1");
1525
1526 resolu = (resolu * SEGMENTSU(nu));
1527
1528 if (resolu == 0) {
1529 MEM_delete(sum);
1530 return;
1531 }
1532
1533 fp = nu->knotsu;
1534 ustart = fp[nu->orderu - 1];
1535 if (nu->flagu & CU_NURB_CYCLIC) {
1536 uend = fp[nu->pntsu + nu->orderu - 1];
1537 }
1538 else {
1539 uend = fp[nu->pntsu];
1540 }
1541 ustep = (uend - ustart) / (resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
1542
1543 basisu = MEM_new_array_uninitialized<float>(size_t(KNOTSU(nu)), "makeNurbcurve3");
1544
1545 if (nu->flagu & CU_NURB_CYCLIC) {
1546 cycl = nu->orderu - 1;
1547 }
1548 else {
1549 cycl = 0;
1550 }
1551
1552 u = ustart;
1553 while (resolu--) {
1554 basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
1555
1556 /* calc sum */
1557 sumdiv = 0.0;
1558 fp = sum;
1559 bp = nu->bp + istart - 1;
1560 for (i = istart; i <= iend; i++, fp++) {
1561 if (i >= nu->pntsu) {
1562 bp = nu->bp + (i - nu->pntsu);
1563 }
1564 else {
1565 bp++;
1566 }
1567
1568 *fp = basisu[i] * bp->vec[3];
1569 sumdiv += *fp;
1570 }
1571 if ((sumdiv != 0.0f) && (sumdiv < 1.0f - eps || sumdiv > 1.0f + eps)) {
1572 /* is normalizing needed? */
1573 fp = sum;
1574 for (i = istart; i <= iend; i++, fp++) {
1575 *fp /= sumdiv;
1576 }
1577 }
1578
1579 zero_v3(coord_fp);
1580
1581 /* one! (1.0) real point */
1582 fp = sum;
1583 bp = nu->bp + istart - 1;
1584 for (i = istart; i <= iend; i++, fp++) {
1585 if (i >= nu->pntsu) {
1586 bp = nu->bp + (i - nu->pntsu);
1587 }
1588 else {
1589 bp++;
1590 }
1591
1592 if (*fp != 0.0f) {
1593 madd_v3_v3fl(coord_fp, bp->vec, *fp);
1594
1595 if (tilt_fp) {
1596 (*tilt_fp) += (*fp) * bp->tilt;
1597 }
1598
1599 if (radius_fp) {
1600 (*radius_fp) += (*fp) * bp->radius;
1601 }
1602
1603 if (weight_fp) {
1604 (*weight_fp) += (*fp) * bp->weight;
1605 }
1606 }
1607 }
1608
1609 coord_fp = static_cast<float *> POINTER_OFFSET(coord_fp, stride);
1610
1611 if (tilt_fp) {
1612 tilt_fp = static_cast<float *> POINTER_OFFSET(tilt_fp, stride);
1613 }
1614 if (radius_fp) {
1615 radius_fp = static_cast<float *> POINTER_OFFSET(radius_fp, stride);
1616 }
1617 if (weight_fp) {
1618 weight_fp = static_cast<float *> POINTER_OFFSET(weight_fp, stride);
1619 }
1620
1621 u += ustep;
1622 }
1623
1624 /* free */
1625 MEM_delete(sum);
1626 MEM_delete(basisu);
1627}
1628
1630 const uint resolu,
1631 const bool is_cyclic,
1632 const bool use_cyclic_duplicate_endpoint)
1633{
1634 const uint segments = bezt_array_len - (is_cyclic ? 0 : 1);
1635 const uint points_len = (segments * resolu) + (is_cyclic ? (use_cyclic_duplicate_endpoint) : 1);
1636 return points_len;
1637}
1638
1640 const uint bezt_array_len,
1641 const uint resolu,
1642 const bool is_cyclic,
1643 const bool use_cyclic_duplicate_endpoint,
1644 /* Array parameters. */
1645 const uint axis,
1646 const uint stride,
1647 float *r_points)
1648{
1649 const uint points_len = BKE_curve_calc_coords_axis_len(
1650 bezt_array_len, resolu, is_cyclic, use_cyclic_duplicate_endpoint);
1651 float *r_points_offset = r_points;
1652
1653 const uint resolu_stride = resolu * stride;
1654 const uint bezt_array_last = bezt_array_len - 1;
1655
1656 for (uint i = 0; i < bezt_array_last; i++) {
1657 const BezTriple *bezt_curr = &bezt_array[i];
1658 const BezTriple *bezt_next = &bezt_array[i + 1];
1659 BKE_curve_forward_diff_bezier(bezt_curr->vec[1][axis],
1660 bezt_curr->vec[2][axis],
1661 bezt_next->vec[0][axis],
1662 bezt_next->vec[1][axis],
1663 r_points_offset,
1664 int(resolu),
1665 stride);
1666 r_points_offset = static_cast<float *> POINTER_OFFSET(r_points_offset, resolu_stride);
1667 }
1668
1669 if (is_cyclic) {
1670 const BezTriple *bezt_curr = &bezt_array[bezt_array_last];
1671 const BezTriple *bezt_next = &bezt_array[0];
1672 BKE_curve_forward_diff_bezier(bezt_curr->vec[1][axis],
1673 bezt_curr->vec[2][axis],
1674 bezt_next->vec[0][axis],
1675 bezt_next->vec[1][axis],
1676 r_points_offset,
1677 int(resolu),
1678 stride);
1679 r_points_offset = static_cast<float *> POINTER_OFFSET(r_points_offset, resolu_stride);
1680 if (use_cyclic_duplicate_endpoint) {
1681 *r_points_offset = *r_points;
1682 r_points_offset = static_cast<float *> POINTER_OFFSET(r_points_offset, stride);
1683 }
1684 }
1685 else {
1686 float *r_points_last = static_cast<float *> POINTER_OFFSET(r_points,
1687 bezt_array_last * resolu_stride);
1688 *r_points_last = bezt_array[bezt_array_last].vec[1][axis];
1689 r_points_offset = static_cast<float *> POINTER_OFFSET(r_points_offset, stride);
1690 }
1691
1692 BLI_assert((float *)POINTER_OFFSET(r_points, points_len * stride) == r_points_offset);
1693 UNUSED_VARS_NDEBUG(points_len);
1694}
1695
1697 float q0, float q1, float q2, float q3, float *p, int it, int stride)
1698{
1699 float rt0, rt1, rt2, rt3, f;
1700 int a;
1701
1702 f = float(it);
1703 rt0 = q0;
1704 rt1 = 3.0f * (q1 - q0) / f;
1705 f *= f;
1706 rt2 = 3.0f * (q0 - 2.0f * q1 + q2) / f;
1707 f *= it;
1708 rt3 = (q3 - q0 + 3.0f * (q1 - q2)) / f;
1709
1710 q0 = rt0;
1711 q1 = rt1 + rt2 + rt3;
1712 q2 = 2 * rt2 + 6 * rt3;
1713 q3 = 6 * rt3;
1714
1715 for (a = 0; a <= it; a++) {
1716 *p = q0;
1717 p = static_cast<float *> POINTER_OFFSET(p, stride);
1718 q0 += q1;
1719 q1 += q2;
1720 q2 += q3;
1721 }
1722}
1723
1725 float q0, float q1, float q2, float q3, float *p, int it, int stride)
1726{
1727 float rt0, rt1, rt2, f;
1728 int a;
1729
1730 f = 1.0f / float(it);
1731
1732 rt0 = 3.0f * (q1 - q0);
1733 rt1 = f * (3.0f * (q3 - q0) + 9.0f * (q1 - q2));
1734 rt2 = 6.0f * (q0 + q2) - 12.0f * q1;
1735
1736 q0 = rt0;
1737 q1 = f * (rt1 + rt2);
1738 q2 = 2.0f * f * rt1;
1739
1740 for (a = 0; a <= it; a++) {
1741 *p = q0;
1742 p = static_cast<float *> POINTER_OFFSET(p, stride);
1743 q0 += q1;
1744 q1 += q2;
1745 }
1746}
1747
1748static void forward_diff_bezier_cotangent(const float p0[3],
1749 const float p1[3],
1750 const float p2[3],
1751 const float p3[3],
1752 float p[3],
1753 int it,
1754 int stride)
1755{
1756 /* note that these are not perpendicular to the curve
1757 * they need to be rotated for this,
1758 *
1759 * This could also be optimized like BKE_curve_forward_diff_bezier */
1760 for (int a = 0; a <= it; a++) {
1761 float t = float(a) / float(it);
1762
1763 for (int i = 0; i < 3; i++) {
1764 p[i] = (-6.0f * t + 6.0f) * p0[i] + (18.0f * t - 12.0f) * p1[i] +
1765 (-18.0f * t + 6.0f) * p2[i] + (6.0f * t) * p3[i];
1766 }
1767 normalize_v3(p);
1768 p = static_cast<float *> POINTER_OFFSET(p, stride);
1769 }
1770}
1771
1772static int cu_isectLL(const float v1[3],
1773 const float v2[3],
1774 const float v3[3],
1775 const float v4[3],
1776 short cox,
1777 short coy,
1778 float *lambda,
1779 float *mu,
1780 float vec[3])
1781{
1782 /* return:
1783 * -1: collinear
1784 * 0: no intersection of segments
1785 * 1: exact intersection of segments
1786 * 2: cross-intersection of segments
1787 */
1788 float deler;
1789
1790 deler = (v1[cox] - v2[cox]) * (v3[coy] - v4[coy]) - (v3[cox] - v4[cox]) * (v1[coy] - v2[coy]);
1791 if (deler == 0.0f) {
1792 return -1;
1793 }
1794
1795 *lambda = (v1[coy] - v3[coy]) * (v3[cox] - v4[cox]) - (v1[cox] - v3[cox]) * (v3[coy] - v4[coy]);
1796 *lambda = -(*lambda / deler);
1797
1798 deler = v3[coy] - v4[coy];
1799 if (deler == 0) {
1800 deler = v3[cox] - v4[cox];
1801 *mu = -(*lambda * (v2[cox] - v1[cox]) + v1[cox] - v3[cox]) / deler;
1802 }
1803 else {
1804 *mu = -(*lambda * (v2[coy] - v1[coy]) + v1[coy] - v3[coy]) / deler;
1805 }
1806 vec[cox] = *lambda * (v2[cox] - v1[cox]) + v1[cox];
1807 vec[coy] = *lambda * (v2[coy] - v1[coy]) + v1[coy];
1808
1809 if (*lambda >= 0.0f && *lambda <= 1.0f && *mu >= 0.0f && *mu <= 1.0f) {
1810 if (*lambda == 0.0f || *lambda == 1.0f || *mu == 0.0f || *mu == 1.0f) {
1811 return 1;
1812 }
1813 return 2;
1814 }
1815 return 0;
1816}
1817
1818static bool bevelinside(const BevList *bl1, const BevList *bl2)
1819{
1820 /* is bl2 INSIDE bl1 ? with left-right method and "lambda's" */
1821 /* returns '1' if correct hole. */
1822 BevPoint *bevp, *prevbevp;
1823 float min, max, vec[3], hvec1[3], hvec2[3], lab, mu;
1824 int nr, links = 0, rechts = 0, mode;
1825
1826 /* take first vertex of possible hole */
1827
1828 bevp = bl2->bevpoints;
1829 hvec1[0] = bevp->vec[0];
1830 hvec1[1] = bevp->vec[1];
1831 hvec1[2] = 0.0;
1832 copy_v3_v3(hvec2, hvec1);
1833 hvec2[0] += 1000;
1834
1835 /* test it with all edges of potential surrounding poly */
1836 /* count number of transitions left-right. */
1837
1838 bevp = bl1->bevpoints;
1839 nr = bl1->nr;
1840 prevbevp = bevp + (nr - 1);
1841
1842 while (nr--) {
1843 min = prevbevp->vec[1];
1844 max = bevp->vec[1];
1845 if (max < min) {
1846 min = max;
1847 max = prevbevp->vec[1];
1848 }
1849 if (min != max) {
1850 if (min <= hvec1[1] && max >= hvec1[1]) {
1851 /* there's a transition, calc intersection point */
1852 mode = cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1853 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1854 * only allow for one situation: we choose lab= 1.0
1855 */
1856 if (mode >= 0 && lab != 0.0f) {
1857 if (vec[0] < hvec1[0]) {
1858 links++;
1859 }
1860 else {
1861 rechts++;
1862 }
1863 }
1864 }
1865 }
1866 prevbevp = bevp;
1867 bevp++;
1868 }
1869
1870 return (links & 1) && (rechts & 1);
1871}
1872
1875 float left;
1876 int dir;
1877};
1878
1879static int vergxcobev(const void *a1, const void *a2)
1880{
1881 const BevelSort *x1 = static_cast<BevelSort *>(const_cast<void *>(a1)),
1882 *x2 = static_cast<BevelSort *>(const_cast<void *>(a2));
1883
1884 if (x1->left > x2->left) {
1885 return 1;
1886 }
1887 if (x1->left < x2->left) {
1888 return -1;
1889 }
1890 return 0;
1891}
1892
1893/* this function cannot be replaced with atan2, but why? */
1894
1896 float x1, float y1, float x2, float y2, float *r_sina, float *r_cosa)
1897{
1898 float t01, t02, x3, y3;
1899
1900 t01 = sqrtf(x1 * x1 + y1 * y1);
1901 t02 = sqrtf(x2 * x2 + y2 * y2);
1902 if (t01 == 0.0f) {
1903 t01 = 1.0f;
1904 }
1905 if (t02 == 0.0f) {
1906 t02 = 1.0f;
1907 }
1908
1909 x1 /= t01;
1910 y1 /= t01;
1911 x2 /= t02;
1912 y2 /= t02;
1913
1914 t02 = x1 * x2 + y1 * y2;
1915 if (fabsf(t02) >= 1.0f) {
1916 t02 = M_PI_2;
1917 }
1918 else {
1919 t02 = safe_acosf(t02) / 2.0f;
1920 }
1921
1922 t02 = sinf(t02);
1923 if (t02 == 0.0f) {
1924 t02 = 1.0f;
1925 }
1926
1927 x3 = x1 - x2;
1928 y3 = y1 - y2;
1929 if (x3 == 0 && y3 == 0) {
1930 x3 = y1;
1931 y3 = -x1;
1932 }
1933 else {
1934 t01 = sqrtf(x3 * x3 + y3 * y3);
1935 x3 /= t01;
1936 y3 /= t01;
1937 }
1938
1939 *r_sina = -y3 / t02;
1940 *r_cosa = x3 / t02;
1941}
1942
1943static void tilt_bezpart(const BezTriple *prevbezt,
1944 const BezTriple *bezt,
1945 const Nurb *nu,
1946 float *tilt_array,
1947 float *radius_array,
1948 float *weight_array,
1949 int resolu,
1950 int stride)
1951{
1952 const BezTriple *pprev, *next, *last;
1953 float fac, dfac, t[4];
1954 int a;
1955
1956 if (tilt_array == nullptr && radius_array == nullptr) {
1957 return;
1958 }
1959
1960 last = nu->bezt + (nu->pntsu - 1);
1961
1962 /* returns a point */
1963 if (prevbezt == nu->bezt) {
1964 if (nu->flagu & CU_NURB_CYCLIC) {
1965 pprev = last;
1966 }
1967 else {
1968 pprev = prevbezt;
1969 }
1970 }
1971 else {
1972 pprev = prevbezt - 1;
1973 }
1974
1975 /* next point */
1976 if (bezt == last) {
1977 if (nu->flagu & CU_NURB_CYCLIC) {
1978 next = nu->bezt;
1979 }
1980 else {
1981 next = bezt;
1982 }
1983 }
1984 else {
1985 next = bezt + 1;
1986 }
1987
1988 fac = 0.0;
1989 dfac = 1.0f / float(resolu);
1990
1991 for (a = 0; a < resolu; a++, fac += dfac) {
1992 if (tilt_array) {
1993 if (nu->tilt_interp == KEY_CU_EASE) {
1994 /* May as well support for tilt also 2.47 ease interp. */
1995 *tilt_array = prevbezt->tilt +
1996 (bezt->tilt - prevbezt->tilt) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
1997 }
1998 else {
2000 *tilt_array = t[0] * pprev->tilt + t[1] * prevbezt->tilt + t[2] * bezt->tilt +
2001 t[3] * next->tilt;
2002 }
2003
2004 tilt_array = static_cast<float *> POINTER_OFFSET(tilt_array, stride);
2005 }
2006
2007 if (radius_array) {
2008 if (nu->radius_interp == KEY_CU_EASE) {
2009 /* Support 2.47 ease interp
2010 * NOTE: this only takes the 2 points into account,
2011 * giving much more localized results to changes in radius, sometimes you want that. */
2012 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius) *
2013 (3.0f * fac * fac - 2.0f * fac * fac * fac);
2014 }
2015 else {
2016
2017 /* reuse interpolation from tilt if we can */
2018 if (tilt_array == nullptr || nu->tilt_interp != nu->radius_interp) {
2020 }
2021 *radius_array = t[0] * pprev->radius + t[1] * prevbezt->radius + t[2] * bezt->radius +
2022 t[3] * next->radius;
2023 }
2024
2025 radius_array = static_cast<float *> POINTER_OFFSET(radius_array, stride);
2026 }
2027
2028 if (weight_array) {
2029 /* Basic interpolation for now, could copy tilt interp too. */
2030 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight) *
2031 (3.0f * fac * fac - 2.0f * fac * fac * fac);
2032
2033 weight_array = static_cast<float *> POINTER_OFFSET(weight_array, stride);
2034 }
2035 }
2036}
2037
2038/* `make_bevel_list_3D_*` functions, at a minimum these must
2039 * fill in the #BevPoint.quat and #BevPoint.dir values. */
2040
2043{
2044 BevPoint *bevp2, *bevp1, *bevp0;
2045 int nr;
2046 const bool is_cyclic = bl->poly != -1;
2047
2048 if (is_cyclic) {
2049 bevp2 = bl->bevpoints;
2050 bevp1 = bevp2 + (bl->nr - 1);
2051 bevp0 = bevp1 - 1;
2052 nr = bl->nr;
2053 }
2054 else {
2055 /* If spline is not cyclic, direction of first and
2056 * last bevel points matches direction of CV handle.
2057 *
2058 * This is getting calculated earlier when we know
2059 * CV's handles and here we might simply skip evaluation
2060 * of direction for this guys.
2061 */
2062
2063 bevp0 = bl->bevpoints;
2064 bevp1 = bevp0 + 1;
2065 bevp2 = bevp1 + 1;
2066
2067 nr = bl->nr - 2;
2068 }
2069
2070 while (nr--) {
2071 /* totally simple */
2072 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2073
2074 bevp0 = bevp1;
2075 bevp1 = bevp2;
2076 bevp2++;
2077 }
2078
2079 /* In the unlikely situation that handles define a zeroed direction,
2080 * calculate it from the adjacent points, see #80742.
2081 *
2082 * Only do this as a fallback since we typically want the end-point directions
2083 * to be exactly aligned with the handles at the end-point, see #83117. */
2084 if (is_cyclic == false) {
2085 bevp0 = &bl->bevpoints[0];
2086 bevp1 = &bl->bevpoints[1];
2087 if (is_zero_v3(bevp0->dir)) [[unlikely]] {
2088 sub_v3_v3v3(bevp0->dir, bevp1->vec, bevp0->vec);
2089 if (normalize_v3(bevp0->dir) == 0.0f) {
2090 copy_v3_v3(bevp0->dir, bevp1->dir);
2091 }
2092 }
2093
2094 bevp0 = &bl->bevpoints[bl->nr - 2];
2095 bevp1 = &bl->bevpoints[bl->nr - 1];
2096 if (is_zero_v3(bevp1->dir)) [[unlikely]] {
2097 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp0->vec);
2098 if (normalize_v3(bevp1->dir) == 0.0f) {
2099 copy_v3_v3(bevp1->dir, bevp0->dir);
2100 }
2101 }
2102 }
2103}
2105{
2106 BevPoint *bevp2, *bevp1, *bevp0;
2107 int nr;
2108
2109 bevp2 = bl->bevpoints;
2110 bevp1 = bevp2 + (bl->nr - 1);
2111 bevp0 = bevp1 - 1;
2112
2113 nr = bl->nr;
2114 while (nr--) {
2115 if (angle_normalized_v3v3(bevp0->tan, bevp1->tan) > DEG2RADF(90.0f)) {
2116 negate_v3(bevp1->tan);
2117 }
2118
2119 bevp0 = bevp1;
2120 bevp1 = bevp2;
2121 bevp2++;
2122 }
2123}
2124/* apply user tilt */
2126{
2127 BevPoint *bevp2, *bevp1;
2128 int nr;
2129 float q[4];
2130
2131 bevp2 = bl->bevpoints;
2132 bevp1 = bevp2 + (bl->nr - 1);
2133
2134 nr = bl->nr;
2135 while (nr--) {
2136 axis_angle_to_quat(q, bevp1->dir, bevp1->tilt);
2137 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2138 normalize_qt(bevp1->quat);
2139
2140 bevp1 = bevp2;
2141 bevp2++;
2142 }
2143}
2144
2148static void bevel_list_smooth(BevList *bl, int smooth_iter)
2149{
2150 BevPoint *bevp2, *bevp1, *bevp0;
2151 int nr;
2152
2153 float q[4];
2154 float bevp0_quat[4];
2155 int a;
2156
2157 for (a = 0; a < smooth_iter; a++) {
2158 bevp2 = bl->bevpoints;
2159 bevp1 = bevp2 + (bl->nr - 1);
2160 bevp0 = bevp1 - 1;
2161
2162 nr = bl->nr;
2163
2164 if (bl->poly == -1) { /* check its not cyclic */
2165 /* Skip the first point. */
2166 // bevp0 = bevp1;
2167 bevp1 = bevp2;
2168 bevp2++;
2169 nr--;
2170
2171 bevp0 = bevp1;
2172 bevp1 = bevp2;
2173 bevp2++;
2174 nr--;
2175 }
2176
2177 copy_qt_qt(bevp0_quat, bevp0->quat);
2178
2179 while (nr--) {
2180 /* Interpolate quaternions. */
2181 float zaxis[3] = {0, 0, 1}, cross[3], q2[4];
2182 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
2183 normalize_qt(q);
2184
2185 mul_qt_v3(q, zaxis);
2186 cross_v3_v3v3(cross, zaxis, bevp1->dir);
2188 normalize_qt(q2);
2189
2190 copy_qt_qt(bevp0_quat, bevp1->quat);
2191 mul_qt_qtqt(q, q2, q);
2192 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
2193 normalize_qt(bevp1->quat);
2194
2195 // bevp0 = bevp1; /* UNUSED */
2196 bevp1 = bevp2;
2197 bevp2++;
2198 }
2199 }
2200}
2201
2203{
2204 BevPoint *bevp = bl->bevpoints;
2205 int nr = bl->nr;
2206
2208
2209 while (nr--) {
2210 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2211 bevp++;
2212 }
2213}
2214
2215static void minimum_twist_between_two_points(BevPoint *bevp_curr, const BevPoint *bevp_prev)
2216{
2217 float angle = angle_normalized_v3v3(bevp_prev->dir, bevp_curr->dir);
2218
2219 if (angle > 0.0f) { /* otherwise we can keep as is */
2220 float q[4];
2221 float cross_tmp[3];
2222 cross_v3_v3v3(cross_tmp, bevp_prev->dir, bevp_curr->dir);
2223 axis_angle_to_quat(q, cross_tmp, angle);
2224 mul_qt_qtqt(bevp_curr->quat, q, bevp_prev->quat);
2225 }
2226 else {
2227 copy_qt_qt(bevp_curr->quat, bevp_prev->quat);
2228 }
2229}
2230
2232{
2233 BevPoint *bevp2, *bevp1, *bevp0; /* Standard for all make_bevel_list_3D_* functions. */
2234 int nr;
2235 float q[4];
2236 const bool is_cyclic = bl->poly != -1;
2237 /* NOTE(@ideasman42): For non-cyclic curves only initialize the first direction
2238 * (via `vec_to_quat`), necessary for symmetry, see #71137.
2239 * Otherwise initialize the first and second points before propagating rotation forward.
2240 * This is historical as changing this can cause significantly different output.
2241 * Specifically: `deform_modifiers` test: (`CurveMeshDeform`).
2242 *
2243 * While it would seem correct to only use the first point for non-cyclic curves as well
2244 * the end-points direction is initialized from the input handles (instead of the directions
2245 * between points), there is often a bigger difference in the first and second directions
2246 * than you'd otherwise expect. So using only the first direction breaks compatibility
2247 * enough it's best to leave it as-is. */
2248 const int nr_init = bl->nr - (is_cyclic ? 1 : 2);
2249
2251
2252 bevp2 = bl->bevpoints;
2253 bevp1 = bevp2 + (bl->nr - 1);
2254 bevp0 = bevp1 - 1;
2255
2256 nr = bl->nr;
2257 while (nr--) {
2258
2259 if (nr >= nr_init) {
2260 /* Initialize the rotation, otherwise propagate the previous rotation forward. */
2261 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2262 }
2263 else {
2265 }
2266
2267 bevp0 = bevp1;
2268 bevp1 = bevp2;
2269 bevp2++;
2270 }
2271
2272 if (bl->poly != -1) { /* check for cyclic */
2273
2274 /* Need to correct for the start/end points not matching
2275 * do this by calculating the tilt angle difference, then apply
2276 * the rotation gradually over the entire curve.
2277 *
2278 * Note that the split is between last and second last, rather than first/last as you'd expect.
2279 *
2280 * real order is like this
2281 * 0,1,2,3,4 --> 1,2,3,4,0
2282 *
2283 * This is why we compare last with second last.
2284 */
2285 float vec_1[3] = {0, 1, 0}, vec_2[3] = {0, 1, 0}, angle, ang_fac, cross_tmp[3];
2286
2287 BevPoint *bevp_first;
2288 BevPoint *bevp_last;
2289
2290 bevp_first = bl->bevpoints;
2291 bevp_first += bl->nr - 1;
2292 bevp_last = bevp_first;
2293 bevp_last--;
2294
2295 /* Quaternions and vectors are normalized, should not need to re-normalize. */
2296 mul_qt_v3(bevp_first->quat, vec_1);
2297 mul_qt_v3(bevp_last->quat, vec_2);
2298 normalize_v3(vec_1);
2299 normalize_v3(vec_2);
2300
2301 /* align the vector, can avoid this and it looks 98% OK but
2302 * better to align the angle quat roll's before comparing */
2303 {
2304 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
2305 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
2306 axis_angle_to_quat(q, cross_tmp, angle);
2307 mul_qt_v3(q, vec_2);
2308 }
2309
2310 angle = angle_normalized_v3v3(vec_1, vec_2);
2311
2312 /* flip rotation if needs be */
2313 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
2314 normalize_v3(cross_tmp);
2315 if (angle_normalized_v3v3(bevp_first->dir, cross_tmp) < DEG2RADF(90.0f)) {
2316 angle = -angle;
2317 }
2318
2319 bevp2 = bl->bevpoints;
2320 bevp1 = bevp2 + (bl->nr - 1);
2321 bevp0 = bevp1 - 1;
2322
2323 nr = bl->nr;
2324 while (nr--) {
2325 ang_fac = angle * (1.0f - (float(nr) / bl->nr)); /* also works */
2326
2327 axis_angle_to_quat(q, bevp1->dir, ang_fac);
2328 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2329
2330 bevp0 = bevp1;
2331 bevp1 = bevp2;
2332 bevp2++;
2333 }
2334 }
2335 else {
2336 /* Need to correct quat for the first/last point,
2337 * this is so because previously it was only calculated
2338 * using its own direction, which might not correspond
2339 * the twist of neighbor point.
2340 */
2341 bevp1 = bl->bevpoints;
2342 bevp0 = bevp1 + 1;
2344
2345 bevp2 = bl->bevpoints;
2346 bevp1 = bevp2 + (bl->nr - 1);
2347 bevp0 = bevp1 - 1;
2349 }
2350}
2351
2353{
2354 BevPoint *bevp2, *bevp1, *bevp0; /* Standard for all make_bevel_list_3D_* functions. */
2355 int nr;
2356
2357 float bevp0_tan[3];
2358
2361
2362 /* correct the tangents */
2363 bevp2 = bl->bevpoints;
2364 bevp1 = bevp2 + (bl->nr - 1);
2365 bevp0 = bevp1 - 1;
2366
2367 nr = bl->nr;
2368 while (nr--) {
2369 float cross_tmp[3];
2370 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2371 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
2372 normalize_v3(bevp1->tan);
2373
2374 bevp0 = bevp1;
2375 bevp1 = bevp2;
2376 bevp2++;
2377 }
2378
2379 /* now for the real twist calc */
2380 bevp2 = bl->bevpoints;
2381 bevp1 = bevp2 + (bl->nr - 1);
2382 bevp0 = bevp1 - 1;
2383
2384 copy_v3_v3(bevp0_tan, bevp0->tan);
2385
2386 nr = bl->nr;
2387 while (nr--) {
2388 /* make perpendicular, modify tan in place, is ok */
2389 float cross_tmp[3];
2390 const float zero[3] = {0, 0, 0};
2391
2392 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2393 normalize_v3(cross_tmp);
2394 tri_to_quat(bevp1->quat, zero, cross_tmp, bevp1->tan); /* XXX: could be faster. */
2395
2396 // bevp0 = bevp1; /* UNUSED */
2397 bevp1 = bevp2;
2398 bevp2++;
2399 }
2400}
2401
2402static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
2403{
2404 switch (twist_mode) {
2405 case CU_TWIST_TANGENT:
2407 break;
2408 case CU_TWIST_MINIMUM:
2410 break;
2411 default: /* CU_TWIST_Z_UP default, pre 2.49c */
2413 break;
2414 }
2415
2416 if (smooth_iter) {
2417 bevel_list_smooth(bl, smooth_iter);
2418 }
2419
2421}
2422
2423/* only for 2 points */
2425{
2426 float q[4];
2427
2428 BevPoint *bevp2 = bl->bevpoints;
2429 BevPoint *bevp1 = bevp2 + 1;
2430
2431 /* simple quat/dir */
2432 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2433 normalize_v3(bevp1->dir);
2434
2435 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2436 axis_angle_to_quat(q, bevp1->dir, bevp1->tilt);
2437 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2438 normalize_qt(bevp1->quat);
2439
2440 copy_v3_v3(bevp2->dir, bevp1->dir);
2441 vec_to_quat(bevp2->quat, bevp2->dir, 5, 1);
2442 axis_angle_to_quat(q, bevp2->dir, bevp2->tilt);
2443 mul_qt_qtqt(bevp2->quat, q, bevp2->quat);
2444 normalize_qt(bevp2->quat);
2445}
2446
2447/* only for 2 points */
2449{
2450 BevPoint *bevp2 = bl->bevpoints;
2451 BevPoint *bevp1 = bevp2 + 1;
2452
2453 const float x1 = bevp1->vec[0] - bevp2->vec[0];
2454 const float y1 = bevp1->vec[1] - bevp2->vec[1];
2455
2456 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2457 bevp2->sina = bevp1->sina;
2458 bevp2->cosa = bevp1->cosa;
2459
2460 /* fill in dir & quat */
2462}
2463
2465{
2466 /* NOTE(@ideasman42): `bevp->dir` and `bevp->quat` are not needed for beveling but are
2467 * used when making a path from a 2D curve, therefore they need to be set. */
2468
2469 BevPoint *bevp0, *bevp1, *bevp2;
2470 int nr;
2471
2472 if (bl->poly != -1) {
2473 bevp2 = bl->bevpoints;
2474 bevp1 = bevp2 + (bl->nr - 1);
2475 bevp0 = bevp1 - 1;
2476 nr = bl->nr;
2477 }
2478 else {
2479 bevp0 = bl->bevpoints;
2480 bevp1 = bevp0 + 1;
2481 bevp2 = bevp1 + 1;
2482
2483 nr = bl->nr - 2;
2484 }
2485
2486 while (nr--) {
2487 const float x1 = bevp1->vec[0] - bevp0->vec[0];
2488 const float x2 = bevp1->vec[0] - bevp2->vec[0];
2489 const float y1 = bevp1->vec[1] - bevp0->vec[1];
2490 const float y2 = bevp1->vec[1] - bevp2->vec[1];
2491
2492 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2493
2494 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2495 * no need for tricky tilt calculation as with 3D curves */
2496 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2497 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2498 /* done with inline make_bevel_list_3D_zup */
2499
2500 bevp0 = bevp1;
2501 bevp1 = bevp2;
2502 bevp2++;
2503 }
2504
2505 /* correct non-cyclic cases */
2506 if (bl->poly == -1) {
2507 BevPoint *bevp;
2508 float angle;
2509
2510 /* first */
2511 bevp = bl->bevpoints;
2512 angle = atan2f(bevp->dir[0], bevp->dir[1]) - float(M_PI_2);
2513 bevp->sina = sinf(angle);
2514 bevp->cosa = cosf(angle);
2515 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2516
2517 /* last */
2518 bevp = bl->bevpoints;
2519 bevp += (bl->nr - 1);
2520 angle = atan2f(bevp->dir[0], bevp->dir[1]) - float(M_PI_2);
2521 bevp->sina = sinf(angle);
2522 bevp->cosa = cosf(angle);
2523 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2524 }
2525}
2526
2528{
2529 if (nu->pntsu > 1) {
2530 BPoint *first_bp = nu->bp, *last_bp = nu->bp + (nu->pntsu - 1);
2531 BevPoint *first_bevp, *last_bevp;
2532
2533 first_bevp = bl->bevpoints;
2534 last_bevp = first_bevp + (bl->nr - 1);
2535
2536 sub_v3_v3v3(first_bevp->dir, (first_bp + 1)->vec, first_bp->vec);
2537 normalize_v3(first_bevp->dir);
2538
2539 sub_v3_v3v3(last_bevp->dir, last_bp->vec, (last_bp - 1)->vec);
2540 normalize_v3(last_bevp->dir);
2541 }
2542}
2543
2544void BKE_curve_bevelList_free(ListBaseT<BevList> *bev)
2545{
2546 for (BevList &bl : bev->items_mutable()) {
2547 if (bl.seglen != nullptr) {
2548 MEM_delete(bl.seglen);
2549 }
2550 if (bl.segbevcount != nullptr) {
2551 MEM_delete(bl.segbevcount);
2552 }
2553 if (bl.bevpoints != nullptr) {
2554 MEM_delete(bl.bevpoints);
2555 }
2556 MEM_delete(&bl);
2557 }
2558
2559 bev->clear_no_delete();
2560}
2561
2562void BKE_curve_bevelList_make(Object *ob, const ListBaseT<Nurb> *nurbs, const bool for_render)
2563{
2564 /* - Convert all curves to polys, with indication of resolution and flags for double-vertices.
2565 * - Possibly; do a smart vertex removal (in case #Nurb).
2566 * - Separate in individual blocks with #BoundBox.
2567 * - Auto-hole detection.
2568 */
2569
2570 /* This function needs an object, because of `tflag` and `upflag`. */
2571 Curve *cu = id_cast<Curve *>(ob->data);
2572 BezTriple *bezt, *prevbezt;
2573 BPoint *bp;
2574 BevList *blnew;
2575 BevPoint *bevp2, *bevp1 = nullptr, *bevp0;
2576 const float threshold = 0.00001f;
2577 float min, inp;
2578 float *seglen = nullptr;
2579 BevelSort *sortdata, *sd, *sd1;
2580 int a, b, nr, poly, resolu = 0, len = 0, segcount;
2581 int *segbevcount;
2582 bool do_tilt, do_radius, do_weight;
2583 bool is_editmode = false;
2584 ListBaseT<BevList> *bev;
2585
2586 /* segbevcount also requires seglen. */
2587 const bool need_seglen = ELEM(
2590
2591 bev = &ob->runtime->curve_cache->bev;
2592
2593#if 0
2594 /* do we need to calculate the radius for each point? */
2595 do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 :
2596 1;
2597#endif
2598
2599 /* STEP 1: MAKE POLYS */
2600
2602 if (cu->editnurb && ob->type != OB_FONT) {
2603 is_editmode = true;
2604 }
2605
2606 for (const Nurb &nu : *nurbs) {
2607 if (nu.hide && is_editmode) {
2608 continue;
2609 }
2610
2611 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2612 * enforced in the UI but can go wrong possibly */
2613 if (!BKE_nurb_check_valid_u(&nu)) {
2614 BevList *bl = MEM_new<BevList>("makeBevelList1");
2615 bl->bevpoints = MEM_new_array_zeroed<BevPoint>(1, "makeBevelPoints1");
2616 BLI_addtail(bev, bl);
2617 bl->nr = 0;
2618 bl->charidx = nu.charidx;
2619 continue;
2620 }
2621
2622 /* Tilt, as the rotation angle of curve control points, is only calculated for 3D curves,
2623 * (since this transformation affects the 3D space). */
2624 do_tilt = (cu->flag & CU_3D) != 0;
2625
2626 /* Normal display uses the radius, better just to calculate them. */
2627 do_radius = CU_DO_RADIUS(cu, nu);
2628
2629 do_weight = true;
2630
2631 BevPoint *bevp;
2632
2633 if (for_render && cu->resolu_ren != 0) {
2634 resolu = cu->resolu_ren;
2635 }
2636 else {
2637 resolu = nu.resolu;
2638 }
2639
2640 segcount = SEGMENTSU(&nu);
2641
2642 if (nu.type == CU_POLY) {
2643 len = nu.pntsu;
2644 BevList *bl = MEM_new<BevList>(__func__);
2646 if (need_seglen && (nu.flagu & CU_NURB_CYCLIC) == 0) {
2647 bl->seglen = MEM_new_array_uninitialized<float>(size_t(segcount), __func__);
2648 bl->segbevcount = MEM_new_array_uninitialized<int>(size_t(segcount), __func__);
2649 }
2650 BLI_addtail(bev, bl);
2651
2652 bl->poly = (nu.flagu & CU_NURB_CYCLIC) ? 0 : -1;
2653 bl->nr = len;
2654 bl->dupe_nr = 0;
2655 bl->charidx = nu.charidx;
2656 bevp = bl->bevpoints;
2657 bevp->offset = 0;
2658 bp = nu.bp;
2659 seglen = bl->seglen;
2660 segbevcount = bl->segbevcount;
2661
2662 while (len--) {
2663 copy_v3_v3(bevp->vec, bp->vec);
2664 bevp->tilt = bp->tilt;
2665 bevp->radius = bp->radius;
2666 bevp->weight = bp->weight;
2667 bp++;
2668 if (seglen != nullptr && len != 0) {
2669 *seglen = len_v3v3(bevp->vec, bp->vec);
2670 bevp++;
2671 bevp->offset = *seglen;
2672 if (*seglen > threshold) {
2673 *segbevcount = 1;
2674 }
2675 else {
2676 *segbevcount = 0;
2677 }
2678 seglen++;
2679 segbevcount++;
2680 }
2681 else {
2682 bevp++;
2683 }
2684 }
2685
2686 if ((nu.flagu & CU_NURB_CYCLIC) == 0) {
2688 }
2689 }
2690 else if (nu.type == CU_BEZIER) {
2691 /* in case last point is not cyclic */
2692 len = segcount * resolu + 1;
2693
2694 BevList *bl = MEM_new<BevList>(__func__);
2696 if (need_seglen && (nu.flagu & CU_NURB_CYCLIC) == 0) {
2697 bl->seglen = MEM_new_array_uninitialized<float>(size_t(segcount), __func__);
2698 bl->segbevcount = MEM_new_array_uninitialized<int>(size_t(segcount), __func__);
2699 }
2700 BLI_addtail(bev, bl);
2701
2702 bl->poly = (nu.flagu & CU_NURB_CYCLIC) ? 0 : -1;
2703 bl->charidx = nu.charidx;
2704
2705 bevp = bl->bevpoints;
2706 seglen = bl->seglen;
2707 segbevcount = bl->segbevcount;
2708
2709 bevp->offset = 0;
2710 if (seglen != nullptr) {
2711 *seglen = 0;
2712 *segbevcount = 0;
2713 }
2714
2715 a = nu.pntsu - 1;
2716 bezt = nu.bezt;
2717 if (nu.flagu & CU_NURB_CYCLIC) {
2718 a++;
2719 prevbezt = nu.bezt + (nu.pntsu - 1);
2720 }
2721 else {
2722 prevbezt = bezt;
2723 bezt++;
2724 }
2725
2726 sub_v3_v3v3(bevp->dir, prevbezt->vec[2], prevbezt->vec[1]);
2727 normalize_v3(bevp->dir);
2728
2729 BLI_assert(segcount >= a);
2730
2731 while (a--) {
2732 if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
2733
2734 copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2735 bevp->tilt = prevbezt->tilt;
2736 bevp->radius = prevbezt->radius;
2737 bevp->weight = prevbezt->weight;
2738 bevp->dupe_tag = false;
2739 bevp++;
2740 bl->nr++;
2741 bl->dupe_nr = 1;
2742 if (seglen != nullptr) {
2743 *seglen = len_v3v3(prevbezt->vec[1], bezt->vec[1]);
2744 bevp->offset = *seglen;
2745 seglen++;
2746 /* match segbevcount to the cleaned up bevel lists (see STEP 2) */
2747 if (bevp->offset > threshold) {
2748 *segbevcount = 1;
2749 }
2750 segbevcount++;
2751 }
2752 }
2753 else {
2754 /* Always do all three, to prevent data hanging around. */
2755 int j;
2756
2757 /* #BevPoint must stay aligned to 4 so `sizeof(BevPoint) / sizeof(float)` works. */
2758 for (j = 0; j < 3; j++) {
2759 BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],
2760 prevbezt->vec[2][j],
2761 bezt->vec[0][j],
2762 bezt->vec[1][j],
2763 &(bevp->vec[j]),
2764 resolu,
2765 sizeof(BevPoint));
2766 }
2767
2768 /* If both arrays are `nullptr` do nothing. */
2769 tilt_bezpart(prevbezt,
2770 bezt,
2771 &nu,
2772 do_tilt ? &bevp->tilt : nullptr,
2773 do_radius ? &bevp->radius : nullptr,
2774 do_weight ? &bevp->weight : nullptr,
2775 resolu,
2776 sizeof(BevPoint));
2777
2778 if (cu->twist_mode == CU_TWIST_TANGENT) {
2780 prevbezt->vec[2],
2781 bezt->vec[0],
2782 bezt->vec[1],
2783 bevp->tan,
2784 resolu,
2785 sizeof(BevPoint));
2786 }
2787
2788 /* `seglen`. */
2789 if (seglen != nullptr) {
2790 *seglen = 0;
2791 *segbevcount = 0;
2792 for (j = 0; j < resolu; j++) {
2793 bevp0 = bevp;
2794 bevp++;
2795 bevp->offset = len_v3v3(bevp0->vec, bevp->vec);
2796 /* Match `seglen` and `segbevcount` to the cleaned up bevel lists (see STEP 2). */
2797 if (bevp->offset > threshold) {
2798 *seglen += bevp->offset;
2799 *segbevcount += 1;
2800 }
2801 }
2802 seglen++;
2803 segbevcount++;
2804 }
2805 else {
2806 bevp += resolu;
2807 }
2808 bl->nr += resolu;
2809 }
2810 prevbezt = bezt;
2811 bezt++;
2812 }
2813
2814 if ((nu.flagu & CU_NURB_CYCLIC) == 0) { /* not cyclic: endpoint */
2815 copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2816 bevp->tilt = prevbezt->tilt;
2817 bevp->radius = prevbezt->radius;
2818 bevp->weight = prevbezt->weight;
2819
2820 sub_v3_v3v3(bevp->dir, prevbezt->vec[1], prevbezt->vec[0]);
2821 normalize_v3(bevp->dir);
2822
2823 bl->nr++;
2824 }
2825 }
2826 else if (nu.type == CU_NURBS) {
2827 if (nu.pntsv == 1) {
2828 len = (resolu * segcount);
2829
2830 BevList *bl = MEM_new<BevList>(__func__);
2832 if (need_seglen && (nu.flagu & CU_NURB_CYCLIC) == 0) {
2833 bl->seglen = MEM_new_array_uninitialized<float>(size_t(segcount), __func__);
2834 bl->segbevcount = MEM_new_array_uninitialized<int>(size_t(segcount), __func__);
2835 }
2836 BLI_addtail(bev, bl);
2837 bl->nr = len;
2838 bl->dupe_nr = 0;
2839 bl->poly = (nu.flagu & CU_NURB_CYCLIC) ? 0 : -1;
2840 bl->charidx = nu.charidx;
2841
2842 bevp = bl->bevpoints;
2843 seglen = bl->seglen;
2844 segbevcount = bl->segbevcount;
2845
2847 &bevp->vec[0],
2848 do_tilt ? &bevp->tilt : nullptr,
2849 do_radius ? &bevp->radius : nullptr,
2850 do_weight ? &bevp->weight : nullptr,
2851 resolu,
2852 sizeof(BevPoint));
2853
2854 /* match seglen and segbevcount to the cleaned up bevel lists (see STEP 2) */
2855 if (seglen != nullptr) {
2856 nr = segcount;
2857 bevp0 = bevp;
2858 bevp++;
2859 while (nr) {
2860 int j;
2861 *seglen = 0;
2862 *segbevcount = 0;
2863 /* We keep last bevel segment zero-length. */
2864 for (j = 0; j < ((nr == 1) ? (resolu - 1) : resolu); j++) {
2865 bevp->offset = len_v3v3(bevp0->vec, bevp->vec);
2866 if (bevp->offset > threshold) {
2867 *seglen += bevp->offset;
2868 *segbevcount += 1;
2869 }
2870 bevp0 = bevp;
2871 bevp++;
2872 }
2873 seglen++;
2874 segbevcount++;
2875 nr--;
2876 }
2877 }
2878
2879 if ((nu.flagu & CU_NURB_CYCLIC) == 0) {
2881 }
2882 }
2883 }
2884 }
2885
2886 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2887 for (BevList &bl : *bev) {
2888 if (bl.nr == 0) { /* null bevel items come from single points */
2889 continue;
2890 }
2891
2892 /* Scale the threshold so high resolution shapes don't get over reduced, see: #49850. */
2893 const float threshold_resolu = 0.00001f / resolu;
2894 const bool is_cyclic = bl.poly != -1;
2895 nr = bl.nr;
2896 if (is_cyclic) {
2897 bevp1 = bl.bevpoints;
2898 bevp0 = bevp1 + (nr - 1);
2899 }
2900 else {
2901 bevp0 = bl.bevpoints;
2902 bevp0->offset = 0;
2903 bevp1 = bevp0 + 1;
2904 }
2905 nr--;
2906 while (nr--) {
2907 if (seglen != nullptr) {
2908 if (fabsf(bevp1->offset) < threshold) {
2909 bevp0->dupe_tag = true;
2910 bl.dupe_nr++;
2911 }
2912 }
2913 else {
2914 if (compare_v3v3(bevp0->vec, bevp1->vec, threshold_resolu)) {
2915 bevp0->dupe_tag = true;
2916 bl.dupe_nr++;
2917 }
2918 }
2919 bevp0 = bevp1;
2920 bevp1++;
2921 }
2922 }
2923
2924 for (BevList &bl : bev->items_mutable()) {
2925 if (bl.nr == 0 || bl.dupe_nr == 0) {
2926 continue;
2927 }
2928
2929 nr = bl.nr - bl.dupe_nr + 1; /* +1 because vector-bezier sets flag too. */
2930 blnew = MEM_new<BevList>("makeBevelList4", bl);
2931 blnew->bevpoints = MEM_new_array_zeroed<BevPoint>(nr, "makeBevelPoints4");
2932 if (!blnew->bevpoints) {
2933 MEM_delete(blnew);
2934 break;
2935 }
2936 blnew->segbevcount = bl.segbevcount;
2937 blnew->seglen = bl.seglen;
2938 blnew->nr = 0;
2939 BLI_remlink(bev, &bl);
2940 BLI_insertlinkbefore(bev, bl.next, blnew); /* Ensure `bevlist` is tuned with `nurblist`. */
2941 bevp0 = bl.bevpoints;
2942 bevp1 = blnew->bevpoints;
2943 nr = bl.nr;
2944 while (nr--) {
2945 if (bevp0->dupe_tag == 0) {
2946 memcpy(bevp1, bevp0, sizeof(BevPoint));
2947 bevp1++;
2948 blnew->nr++;
2949 }
2950 bevp0++;
2951 }
2952 if (bl.bevpoints != nullptr) {
2953 MEM_delete(bl.bevpoints);
2954 }
2955 MEM_delete(&bl);
2956 blnew->dupe_nr = 0;
2957 }
2958
2959 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2960 poly = 0;
2961 for (BevList &bl : *bev) {
2962 if (bl.nr && bl.poly >= 0) {
2963 poly++;
2964 bl.poly = poly;
2965 bl.hole = 0;
2966 }
2967 }
2968
2969 /* find extreme left points, also test (turning) direction */
2970 if (poly > 0) {
2971 sd = sortdata = MEM_new_array_uninitialized<BevelSort>(size_t(poly), __func__);
2972 for (BevList &bl : *bev) {
2973 if (bl.poly > 0) {
2974 BevPoint *bevp;
2975
2976 bevp = bl.bevpoints;
2977 bevp1 = bl.bevpoints;
2978 min = bevp1->vec[0];
2979 nr = bl.nr;
2980 while (nr--) {
2981 if (min > bevp->vec[0]) {
2982 min = bevp->vec[0];
2983 bevp1 = bevp;
2984 }
2985 bevp++;
2986 }
2987 sd->bl = &bl;
2988 sd->left = min;
2989
2990 bevp = bl.bevpoints;
2991 if (bevp1 == bevp) {
2992 bevp0 = bevp + (bl.nr - 1);
2993 }
2994 else {
2995 bevp0 = bevp1 - 1;
2996 }
2997 bevp = bevp + (bl.nr - 1);
2998 if (bevp1 == bevp) {
2999 bevp2 = bl.bevpoints;
3000 }
3001 else {
3002 bevp2 = bevp1 + 1;
3003 }
3004
3005 inp = ((bevp1->vec[0] - bevp0->vec[0]) * (bevp0->vec[1] - bevp2->vec[1]) +
3006 (bevp0->vec[1] - bevp1->vec[1]) * (bevp0->vec[0] - bevp2->vec[0]));
3007
3008 if (inp > 0.0f) {
3009 sd->dir = 1;
3010 }
3011 else {
3012 sd->dir = 0;
3013 }
3014
3015 sd++;
3016 }
3017 }
3018 qsort(sortdata, poly, sizeof(BevelSort), vergxcobev);
3019
3020 sd = sortdata + 1;
3021 for (a = 1; a < poly; a++, sd++) {
3022 BevList *bl = sd->bl; /* is bl a hole? */
3023 sd1 = sortdata + (a - 1);
3024 for (b = a - 1; b >= 0; b--, sd1--) { /* all polys to the left */
3025 if (sd1->bl->charidx == bl->charidx) { /* for text, only check matching char */
3026 if (bevelinside(sd1->bl, bl)) {
3027 bl->hole = 1 - sd1->bl->hole;
3028 break;
3029 }
3030 }
3031 }
3032 }
3033
3034 /* turning direction */
3035 if (CU_IS_2D(cu)) {
3036 sd = sortdata;
3037 for (a = 0; a < poly; a++, sd++) {
3038 BLI_assert(sd->bl->reversed == false);
3039 if (sd->bl->hole == sd->dir) {
3040 sd->bl->reversed = true;
3041 BevList *bl = sd->bl;
3042 bevp1 = bl->bevpoints;
3043 bevp2 = bevp1 + (bl->nr - 1);
3044 nr = bl->nr / 2;
3045 while (nr--) {
3046 std::swap(*bevp1, *bevp2);
3047 bevp1++;
3048 bevp2--;
3049 }
3050 }
3051 }
3052 }
3053 MEM_delete(sortdata);
3054 }
3055
3056 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
3057 if (CU_IS_2D(cu)) {
3058 /* 2D Curves */
3059 for (BevList &bl : *bev) {
3060 if (bl.nr < 2) {
3061 BevPoint *bevp = bl.bevpoints;
3062 unit_qt(bevp->quat);
3063 }
3064 else if (bl.nr == 2) { /* 2 points, treat separately. */
3066 }
3067 else {
3068 make_bevel_list_2D(&bl);
3069 }
3070 }
3071 }
3072 else {
3073 /* 3D Curves */
3074 for (BevList &bl : *bev) {
3075 if (bl.nr < 2) {
3076 BevPoint *bevp = bl.bevpoints;
3077 unit_qt(bevp->quat);
3078 }
3079 else if (bl.nr == 2) { /* 2 points, treat separately. */
3081 }
3082 else {
3083 make_bevel_list_3D(&bl, int(resolu * cu->twist_smooth), cu->twist_mode);
3084 }
3085 }
3086 }
3087}
3088
3089/* ****************** HANDLES ************** */
3090
3092 const BezTriple *prev,
3093 const BezTriple *next,
3094 eBezTriple_Flag handle_sel_flag,
3095 bool is_fcurve,
3096 bool skip_align,
3097 char fcurve_smoothing)
3098{
3099 /* defines to avoid confusion */
3100#define p2_h1 ((p2) - 3)
3101#define p2_h2 ((p2) + 3)
3102
3103 const float *p1, *p3;
3104 float *p2;
3105 float pt[3];
3106 float dvec_a[3], dvec_b[3];
3107 float len, len_a, len_b;
3108 const float eps = 1e-5;
3109
3110 /* assume normal handle until we check */
3112
3113 if (bezt->h1 == 0 && bezt->h2 == 0) {
3114 return;
3115 }
3116
3117 if (prev == nullptr && next == nullptr) {
3118 /* Single point, no need to calculate handles. */
3119 return;
3120 }
3121
3122 p2 = bezt->vec[1];
3123
3124 if (prev == nullptr) {
3125 p3 = next->vec[1];
3126 pt[0] = 2.0f * p2[0] - p3[0];
3127 pt[1] = 2.0f * p2[1] - p3[1];
3128 pt[2] = 2.0f * p2[2] - p3[2];
3129 p1 = pt;
3130 }
3131 else {
3132 p1 = prev->vec[1];
3133 }
3134
3135 if (next == nullptr) {
3136 pt[0] = 2.0f * p2[0] - p1[0];
3137 pt[1] = 2.0f * p2[1] - p1[1];
3138 pt[2] = 2.0f * p2[2] - p1[2];
3139 p3 = pt;
3140 }
3141 else {
3142 p3 = next->vec[1];
3143 }
3144
3145 sub_v3_v3v3(dvec_a, p2, p1);
3146 sub_v3_v3v3(dvec_b, p3, p2);
3147
3148 if (is_fcurve) {
3149 len_a = dvec_a[0];
3150 len_b = dvec_b[0];
3151 }
3152 else {
3153 len_a = len_v3(dvec_a);
3154 len_b = len_v3(dvec_b);
3155 }
3156
3157 if (len_a == 0.0f) {
3158 len_a = 1.0f;
3159 }
3160 if (len_b == 0.0f) {
3161 len_b = 1.0f;
3162 }
3163
3164 if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM) || ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) { /* auto */
3165 float tvec[3];
3166 tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
3167 tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
3168 tvec[2] = dvec_b[2] / len_b + dvec_a[2] / len_a;
3169
3170 if (is_fcurve) {
3171 if (fcurve_smoothing != FCURVE_SMOOTH_NONE) {
3172 /* force the horizontal handle size to be 1/3 of the key interval so that
3173 * the X component of the parametric bezier curve is a linear spline */
3174 len = 6.0f / 2.5614f;
3175 }
3176 else {
3177 len = tvec[0];
3178 }
3179 }
3180 else {
3181 len = len_v3(tvec);
3182 }
3183 len *= 2.5614f;
3184
3185 if (len != 0.0f) {
3186 /* Only for F-Curves. */
3187 bool leftviolate = false, rightviolate = false;
3188
3189 if (!is_fcurve || fcurve_smoothing == FCURVE_SMOOTH_NONE) {
3190 len_a = std::min(len_a, 5.0f * len_b);
3191 len_b = std::min(len_b, 5.0f * len_a);
3192 }
3193
3194 if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) {
3195 len_a /= len;
3196 madd_v3_v3v3fl(p2_h1, p2, tvec, -len_a);
3197
3198 if ((bezt->h1 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
3199 float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
3200 float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
3201 if ((ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f)) {
3202 bezt->vec[0][1] = bezt->vec[1][1];
3204 }
3205 else { /* handles should not be beyond y coord of two others */
3206 if (ydiff1 <= 0.0f) {
3207 if (prev->vec[1][1] > bezt->vec[0][1]) {
3208 bezt->vec[0][1] = prev->vec[1][1];
3209 leftviolate = true;
3210 }
3211 }
3212 else {
3213 if (prev->vec[1][1] < bezt->vec[0][1]) {
3214 bezt->vec[0][1] = prev->vec[1][1];
3215 leftviolate = true;
3216 }
3217 }
3218 }
3219 }
3220 }
3221 if (ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {
3222 len_b /= len;
3223 madd_v3_v3v3fl(p2_h2, p2, tvec, len_b);
3224
3225 if ((bezt->h2 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
3226 float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
3227 float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
3228 if ((ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f)) {
3229 bezt->vec[2][1] = bezt->vec[1][1];
3231 }
3232 else { /* handles should not be beyond y coord of two others */
3233 if (ydiff1 <= 0.0f) {
3234 if (next->vec[1][1] < bezt->vec[2][1]) {
3235 bezt->vec[2][1] = next->vec[1][1];
3236 rightviolate = true;
3237 }
3238 }
3239 else {
3240 if (next->vec[1][1] > bezt->vec[2][1]) {
3241 bezt->vec[2][1] = next->vec[1][1];
3242 rightviolate = true;
3243 }
3244 }
3245 }
3246 }
3247 }
3248 if (leftviolate || rightviolate) { /* align left handle */
3249 BLI_assert(is_fcurve);
3250 /* simple 2d calculation */
3251 float h1_x = p2_h1[0] - p2[0];
3252 float h2_x = p2[0] - p2_h2[0];
3253
3254 if (leftviolate) {
3255 p2_h2[1] = p2[1] + ((p2[1] - p2_h1[1]) / h1_x) * h2_x;
3256 }
3257 else {
3258 p2_h1[1] = p2[1] + ((p2[1] - p2_h2[1]) / h2_x) * h1_x;
3259 }
3260 }
3261 }
3262 }
3263
3264 if (bezt->h1 == HD_VECT) { /* vector */
3265 madd_v3_v3v3fl(p2_h1, p2, dvec_a, -1.0f / 3.0f);
3266 }
3267 if (bezt->h2 == HD_VECT) {
3268 madd_v3_v3v3fl(p2_h2, p2, dvec_b, 1.0f / 3.0f);
3269 }
3270
3271 if (skip_align ||
3272 /* When one handle is free, aligning makes no sense, see: #35952 */
3273 ELEM(HD_FREE, bezt->h1, bezt->h2) ||
3274 /* Also when no handles are aligned, skip this step. */
3275 (!ELEM(HD_ALIGN, bezt->h1, bezt->h2) && !ELEM(HD_ALIGN_DOUBLESIDE, bezt->h1, bezt->h2)))
3276 {
3277 /* Handles need to be updated during animation and applying stuff like hooks,
3278 * but in such situations it's quite difficult to distinguish in which order
3279 * align handles should be aligned so skip them for now. */
3280 return;
3281 }
3282
3283 len_a = len_v3v3(p2, p2_h1);
3284 len_b = len_v3v3(p2, p2_h2);
3285
3286 if (len_a == 0.0f) {
3287 len_a = 1.0f;
3288 }
3289 if (len_b == 0.0f) {
3290 len_b = 1.0f;
3291 }
3292
3293 const float len_ratio = len_a / len_b;
3294
3295 if (bezt->f1 & handle_sel_flag) { /* order of calculation */
3296 if (ELEM(bezt->h2, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) { /* aligned */
3297 if (len_a > eps) {
3298 len = 1.0f / len_ratio;
3299 p2_h2[0] = p2[0] + len * (p2[0] - p2_h1[0]);
3300 p2_h2[1] = p2[1] + len * (p2[1] - p2_h1[1]);
3301 p2_h2[2] = p2[2] + len * (p2[2] - p2_h1[2]);
3302 }
3303 }
3304 if (ELEM(bezt->h1, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) {
3305 if (len_b > eps) {
3306 len = len_ratio;
3307 p2_h1[0] = p2[0] + len * (p2[0] - p2_h2[0]);
3308 p2_h1[1] = p2[1] + len * (p2[1] - p2_h2[1]);
3309 p2_h1[2] = p2[2] + len * (p2[2] - p2_h2[2]);
3310 }
3311 }
3312 }
3313 else {
3314 if (ELEM(bezt->h1, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) {
3315 if (len_b > eps) {
3316 len = len_ratio;
3317 p2_h1[0] = p2[0] + len * (p2[0] - p2_h2[0]);
3318 p2_h1[1] = p2[1] + len * (p2[1] - p2_h2[1]);
3319 p2_h1[2] = p2[2] + len * (p2[2] - p2_h2[2]);
3320 }
3321 }
3322 if (ELEM(bezt->h2, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) { /* aligned */
3323 if (len_a > eps) {
3324 len = 1.0f / len_ratio;
3325 p2_h2[0] = p2[0] + len * (p2[0] - p2_h1[0]);
3326 p2_h2[1] = p2[1] + len * (p2[1] - p2_h1[1]);
3327 p2_h2[2] = p2[2] + len * (p2[2] - p2_h1[2]);
3328 }
3329 }
3330 }
3331
3332#undef p2_h1
3333#undef p2_h2
3334}
3335
3336static void calchandlesNurb_intern(Nurb *nu, eBezTriple_Flag handle_sel_flag, bool skip_align)
3337{
3338 BezTriple *bezt, *prev, *next;
3339 int a;
3340
3341 if (nu->type != CU_BEZIER) {
3342 return;
3343 }
3344 if (nu->pntsu < 2) {
3345 return;
3346 }
3347
3348 a = nu->pntsu;
3349 bezt = nu->bezt;
3350 if (nu->flagu & CU_NURB_CYCLIC) {
3351 prev = bezt + (a - 1);
3352 }
3353 else {
3354 prev = nullptr;
3355 }
3356 next = bezt + 1;
3357
3358 while (a--) {
3359 calchandleNurb_intern(bezt, prev, next, handle_sel_flag, false, skip_align, 0);
3360 prev = bezt;
3361 if (a == 1) {
3362 if (nu->flagu & CU_NURB_CYCLIC) {
3363 next = nu->bezt;
3364 }
3365 else {
3366 next = nullptr;
3367 }
3368 }
3369 else if (next != nullptr) {
3370 next++;
3371 }
3372
3373 bezt++;
3374 }
3375}
3376
3387static void *allocate_arrays(int count, float ***floats, char ***chars, const char *name)
3388{
3389 size_t num_floats = 0, num_chars = 0;
3390
3391 while (floats && floats[num_floats]) {
3392 num_floats++;
3393 }
3394
3395 while (chars && chars[num_chars]) {
3396 num_chars++;
3397 }
3398
3399 void *buffer = MEM_new_array_uninitialized(
3400 count, (sizeof(float) * num_floats + num_chars), name);
3401
3402 if (!buffer) {
3403 return nullptr;
3404 }
3405
3406 float *fptr = static_cast<float *>(buffer);
3407
3408 for (int i = 0; i < num_floats; i++, fptr += count) {
3409 *floats[i] = fptr;
3410 }
3411
3412 char *cptr = reinterpret_cast<char *>(fptr);
3413
3414 for (int i = 0; i < num_chars; i++, cptr += count) {
3415 *chars[i] = cptr;
3416 }
3417
3418 return buffer;
3419}
3420
3421static void free_arrays(void *buffer)
3422{
3423 MEM_delete_void(buffer);
3424}
3425
3426/* computes in which direction to change h[i] to satisfy conditions better */
3427static float bezier_relax_direction(const float *a,
3428 const float *b,
3429 const float *c,
3430 const float *d,
3431 const float *h,
3432 int i,
3433 int count)
3434{
3435 /* current deviation between sides of the equation */
3436 float state = a[i] * h[(i + count - 1) % count] + b[i] * h[i] + c[i] * h[(i + 1) % count] - d[i];
3437
3438 /* only the sign is meaningful */
3439 return -state * b[i];
3440}
3441
3442static void bezier_lock_unknown(float *a, float *b, float *c, float *d, int i, float value)
3443{
3444 a[i] = c[i] = 0.0f;
3445 b[i] = 1.0f;
3446 d[i] = value;
3447}
3448
3449static void bezier_restore_equation(float *a,
3450 float *b,
3451 float *c,
3452 float *d,
3453 const float *a0,
3454 const float *b0,
3455 const float *c0,
3456 const float *d0,
3457 int i)
3458{
3459 a[i] = a0[i];
3460 b[i] = b0[i];
3461 c[i] = c0[i];
3462 d[i] = d0[i];
3463}
3464
3466 float *b,
3467 float *c,
3468 float *d,
3469 float *h,
3470 const float *hmin,
3471 const float *hmax,
3472 int solve_count)
3473{
3474 float *a0, *b0, *c0, *d0;
3475 float **arrays[] = {&a0, &b0, &c0, &d0, nullptr};
3476 char *is_locked, *num_unlocks;
3477 char **flagarrays[] = {&is_locked, &num_unlocks, nullptr};
3478
3479 void *tmps = allocate_arrays(solve_count, arrays, flagarrays, "tridiagonal_solve_with_limits");
3480 if (!tmps) {
3481 return false;
3482 }
3483
3484 memcpy(a0, a, sizeof(float) * solve_count);
3485 memcpy(b0, b, sizeof(float) * solve_count);
3486 memcpy(c0, c, sizeof(float) * solve_count);
3487 memcpy(d0, d, sizeof(float) * solve_count);
3488
3489 memset(is_locked, 0, solve_count);
3490 memset(num_unlocks, 0, solve_count);
3491
3492 bool overshoot, unlocked;
3493
3494 do {
3495 if (!BLI_tridiagonal_solve_cyclic(a, b, c, d, h, solve_count)) {
3496 free_arrays(tmps);
3497 return false;
3498 }
3499
3500 /* first check if any handles overshoot the limits, and lock them */
3501 bool all = false, locked = false;
3502
3503 overshoot = unlocked = false;
3504
3505 do {
3506 for (int i = 0; i < solve_count; i++) {
3507 if (h[i] >= hmin[i] && h[i] <= hmax[i]) {
3508 continue;
3509 }
3510
3511 overshoot = true;
3512
3513 float target = h[i] > hmax[i] ? hmax[i] : hmin[i];
3514
3515 /* heuristically only lock handles that go in the right direction if there are such ones */
3516 if (target != 0.0f || all) {
3517 /* mark item locked */
3518 is_locked[i] = 1;
3519
3520 bezier_lock_unknown(a, b, c, d, i, target);
3521 locked = true;
3522 }
3523 }
3524
3525 all = true;
3526 } while (overshoot && !locked);
3527
3528 /* If no handles overshot and were locked,
3529 * see if it may be a good idea to unlock some handles. */
3530 if (!locked) {
3531 for (int i = 0; i < solve_count; i++) {
3532 /* to definitely avoid infinite loops limit this to 2 times */
3533 if (!is_locked[i] || num_unlocks[i] >= 2) {
3534 continue;
3535 }
3536
3537 /* if the handle wants to move in allowable direction, release it */
3538 float relax = bezier_relax_direction(a0, b0, c0, d0, h, i, solve_count);
3539
3540 if ((relax > 0 && h[i] < hmax[i]) || (relax < 0 && h[i] > hmin[i])) {
3541 bezier_restore_equation(a, b, c, d, a0, b0, c0, d0, i);
3542
3543 is_locked[i] = 0;
3544 num_unlocks[i]++;
3545 unlocked = true;
3546 }
3547 }
3548 }
3549 } while (overshoot || unlocked);
3550
3551 free_arrays(tmps);
3552 return true;
3553}
3554
3555/* Keep ASCII art. */
3556/* clang-format off */
3557/*
3558 * This function computes the handles of a series of auto bezier points
3559 * on the basis of 'no acceleration discontinuities' at the points.
3560 * The first and last bezier points are considered 'fixed' (their handles are not touched)
3561 * The result is the smoothest possible trajectory going through intermediate points.
3562 * The difficulty is that the handles depends on their neighbors.
3563 *
3564 * The exact solution is found by solving a tridiagonal matrix equation formed
3565 * by the continuity and boundary conditions. Although theoretically handle position
3566 * is affected by all other points of the curve segment, in practice the influence
3567 * decreases exponentially with distance.
3568 *
3569 * NOTE: this algorithm assumes that the handle horizontal size is always 1/3 of the
3570 * of the interval to the next point. This rule ensures linear interpolation of time.
3571 *
3572 * ^ height (co 1)
3573 * | yN
3574 * | yN-1 |
3575 * | y2 | |
3576 * | y1 | | |
3577 * | y0 | | | |
3578 * | | | | | |
3579 * | | | | | |
3580 * | | | | | |
3581 * |------dx1--------dx2--------- ~ -------dxN-------------------> time (co 0)
3582 *
3583 * Notation:
3584 *
3585 * x[i], y[i] - keyframe coordinates
3586 * h[i] - right handle y offset from y[i]
3587 *
3588 * dx[i] = x[i] - x[i-1]
3589 * dy[i] = y[i] - y[i-1]
3590 *
3591 * Mathematical basis:
3592 *
3593 * 1. Handle lengths on either side of each point are connected by a factor
3594 * ensuring continuity of the first derivative:
3595 *
3596 * l[i] = dx[i+1]/dx[i]
3597 *
3598 * 2. The tridiagonal system is formed by the following equation, which is derived
3599 * by differentiating the bezier curve and specifies second derivative continuity
3600 * at every point:
3601 *
3602 * l[i]^2 * h[i-1] + (2*l[i]+2) * h[i] + 1/l[i+1] * h[i+1] = dy[i]*l[i]^2 + dy[i+1]
3603 *
3604 * 3. If this point is adjacent to a manually set handle with X size not equal to 1/3
3605 * of the horizontal interval, this equation becomes slightly more complex:
3606 *
3607 * l[i]^2 * h[i-1] + (3*(1-R[i-1])*l[i] + 3*(1-L[i+1])) * h[i] + 1/l[i+1] * h[i+1] = dy[i]*l[i]^2 + dy[i+1]
3608 *
3609 * The difference between equations amounts to this, and it's obvious that when R[i-1]
3610 * and L[i+1] are both 1/3, it becomes zero:
3611 *
3612 * ( (1-3*R[i-1])*l[i] + (1-3*L[i+1]) ) * h[i]
3613 *
3614 * 4. The equations for zero acceleration border conditions are basically the above
3615 * equation with parts omitted, so the handle size correction also applies.
3616 *
3617 * 5. The fully cyclic curve case is handled by eliminating one of the end points,
3618 * and instead of border conditions connecting the curve via a set of equations:
3619 *
3620 * l[0] = l[N] = dx[1] / dx[N]
3621 * dy[0] = dy[N]
3622 * Continuity equation (item 2) for i = 0.
3623 * Substitute h[0] for h[N] and h[N-1] for h[-1]
3624 */
3625/* clang-format on */
3626
3628 float *a, float *b, float *c, float *d, const float *dy, const float *l, int i)
3629{
3630 a[i] = l[i] * l[i];
3631 b[i] = 2.0f * (l[i] + 1);
3632 c[i] = 1.0f / l[i + 1];
3633 d[i] = dy[i] * l[i] * l[i] + dy[i + 1];
3634}
3635
3637 float *a, float *b, float *c, float *d, const float *dy, const float *l, int i)
3638{
3639 a[i] = 0.0f;
3640 b[i] = 2.0f;
3641 c[i] = 1.0f / l[i + 1];
3642 d[i] = dy[i + 1];
3643}
3644
3646 float *a, float *b, float *c, float *d, const float *dy, const float *l, int i)
3647{
3648 a[i] = l[i] * l[i];
3649 b[i] = 2.0f * l[i];
3650 c[i] = 0.0f;
3651 d[i] = dy[i] * l[i] * l[i];
3652}
3653
3654/* auto clamp prevents its own point going the wrong way, and adjacent handles overshooting */
3655static void bezier_clamp(
3656 float *hmax, float *hmin, int i, float dy, bool no_reverse, bool no_overshoot)
3657{
3658 if (dy > 0) {
3659 if (no_overshoot) {
3660 hmax[i] = min_ff(hmax[i], dy);
3661 }
3662 if (no_reverse) {
3663 hmin[i] = 0.0f;
3664 }
3665 }
3666 else if (dy < 0) {
3667 if (no_reverse) {
3668 hmax[i] = 0.0f;
3669 }
3670 if (no_overshoot) {
3671 hmin[i] = max_ff(hmin[i], dy);
3672 }
3673 }
3674 else if (no_reverse || no_overshoot) {
3675 hmax[i] = hmin[i] = 0.0f;
3676 }
3677}
3678
3679/* write changes to a bezier handle */
3681 bool right,
3682 const float newval[3],
3683 bool endpoint)
3684{
3685 float tmp[3];
3686
3687 int idx = right ? 2 : 0;
3688 char hr = right ? bezt->h2 : bezt->h1;
3689 char hm = right ? bezt->h1 : bezt->h2;
3690
3691 /* only assign Auto/Vector handles */
3692 if (!ELEM(hr, HD_AUTO, HD_AUTO_ANIM, HD_VECT)) {
3693 return;
3694 }
3695
3696 copy_v3_v3(bezt->vec[idx], newval);
3697
3698 /* fix up the Align handle if any */
3699 if (ELEM(hm, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) {
3700 float hlen = len_v3v3(bezt->vec[1], bezt->vec[2 - idx]);
3701 float h2len = len_v3v3(bezt->vec[1], bezt->vec[idx]);
3702
3703 sub_v3_v3v3(tmp, bezt->vec[1], bezt->vec[idx]);
3704 madd_v3_v3v3fl(bezt->vec[2 - idx], bezt->vec[1], tmp, hlen / h2len);
3705 }
3706 /* at end points of the curve, mirror handle to the other side */
3707 else if (endpoint && ELEM(hm, HD_AUTO, HD_AUTO_ANIM, HD_VECT)) {
3708 sub_v3_v3v3(tmp, bezt->vec[1], bezt->vec[idx]);
3709 add_v3_v3v3(bezt->vec[2 - idx], bezt->vec[1], tmp);
3710 }
3711}
3712
3713static void bezier_output_handle(BezTriple *bezt, bool right, float dy, bool endpoint)
3714{
3715 float tmp[3];
3716
3717 copy_v3_v3(tmp, bezt->vec[right ? 2 : 0]);
3718
3719 tmp[1] = bezt->vec[1][1] + dy;
3720
3721 bezier_output_handle_inner(bezt, right, tmp, endpoint);
3722}
3723
3724static bool bezier_check_solve_end_handle(BezTriple *bezt, char htype, bool end)
3725{
3726 return (htype == HD_VECT) || (end && ELEM(htype, HD_AUTO, HD_AUTO_ANIM) &&
3728}
3729
3730static float bezier_calc_handle_adj(float hsize[2], float dx)
3731{
3732 /* if handles intersect in x direction, they are scaled to fit */
3733 float fac = dx / (hsize[0] + dx / 3.0f);
3734 if (fac < 1.0f) {
3735 mul_v2_fl(hsize, fac);
3736 }
3737 return 1.0f - 3.0f * hsize[0] / dx;
3738}
3739
3741 BezTriple *bezt, int total, int start, int count, bool cycle)
3742{
3743 float *dx, *dy, *l, *a, *b, *c, *d, *h, *hmax, *hmin;
3744 float **arrays[] = {&dx, &dy, &l, &a, &b, &c, &d, &h, &hmax, &hmin, nullptr};
3745
3746 int solve_count = count;
3747
3748 /* verify index ranges */
3749
3750 if (count < 2) {
3751 return;
3752 }
3753
3754 BLI_assert(start < total - 1 && count <= total);
3755 BLI_assert(start + count <= total || cycle);
3756
3757 bool full_cycle = (start == 0 && count == total && cycle);
3758
3759 BezTriple *bezt_first = &bezt[start];
3760 BezTriple *bezt_last =
3761 &bezt[(start + count > total) ? start + count - total : start + count - 1];
3762
3763 bool solve_first = bezier_check_solve_end_handle(bezt_first, bezt_first->h2, start == 0);
3764 bool solve_last = bezier_check_solve_end_handle(
3765 bezt_last, bezt_last->h1, start + count == total);
3766
3767 if (count == 2 && !full_cycle && solve_first == solve_last) {
3768 return;
3769 }
3770
3771 /* allocate all */
3772
3773 void *tmp_buffer = allocate_arrays(count, arrays, nullptr, "bezier_calc_smooth_tmp");
3774 if (!tmp_buffer) {
3775 return;
3776 }
3777
3778 /* point locations */
3779
3780 dx[0] = dy[0] = NAN_FLT;
3781
3782 for (int i = 1, j = start + 1; i < count; i++, j++) {
3783 dx[i] = bezt[j].vec[1][0] - bezt[j - 1].vec[1][0];
3784 dy[i] = bezt[j].vec[1][1] - bezt[j - 1].vec[1][1];
3785
3786 /* when cyclic, jump from last point to first */
3787 if (cycle && j == total - 1) {
3788 j = 0;
3789 }
3790 }
3791
3792 /* ratio of x intervals */
3793
3794 if (full_cycle) {
3795 dx[0] = dx[count - 1];
3796 dy[0] = dy[count - 1];
3797
3798 l[0] = l[count - 1] = dx[1] / dx[0];
3799 }
3800 else {
3801 l[0] = l[count - 1] = 1.0f;
3802 }
3803
3804 for (int i = 1; i < count - 1; i++) {
3805 l[i] = dx[i + 1] / dx[i];
3806 }
3807
3808 /* compute handle clamp ranges */
3809
3810 bool clamped_prev = false, clamped_cur = ELEM(HD_AUTO_ANIM, bezt_first->h1, bezt_first->h2);
3811
3812 for (int i = 0; i < count; i++) {
3813 hmax[i] = FLT_MAX;
3814 hmin[i] = -FLT_MAX;
3815 }
3816
3817 for (int i = 1, j = start + 1; i < count; i++, j++) {
3818 clamped_prev = clamped_cur;
3819 clamped_cur = ELEM(HD_AUTO_ANIM, bezt[j].h1, bezt[j].h2);
3820
3821 if (cycle && j == total - 1) {
3822 j = 0;
3823 clamped_cur = clamped_cur || ELEM(HD_AUTO_ANIM, bezt[j].h1, bezt[j].h2);
3824 }
3825
3826 bezier_clamp(hmax, hmin, i - 1, dy[i], clamped_prev, clamped_prev);
3827 bezier_clamp(hmax, hmin, i, dy[i] * l[i], clamped_cur, clamped_cur);
3828 }
3829
3830 /* full cycle merges first and last points into continuous loop */
3831
3832 float first_handle_adj = 0.0f, last_handle_adj = 0.0f;
3833
3834 if (full_cycle) {
3835 /* reduce the number of unknowns by one */
3836 int i = solve_count = count - 1;
3837
3838 hmin[0] = max_ff(hmin[0], hmin[i]);
3839 hmax[0] = min_ff(hmax[0], hmax[i]);
3840
3841 solve_first = solve_last = true;
3842
3843 bezier_eq_continuous(a, b, c, d, dy, l, 0);
3844 }
3845 else {
3846 float tmp[2];
3847
3848 /* boundary condition: fixed handles or zero curvature */
3849 if (!solve_first) {
3850 sub_v2_v2v2(tmp, bezt_first->vec[2], bezt_first->vec[1]);
3851 first_handle_adj = bezier_calc_handle_adj(tmp, dx[1]);
3852
3853 bezier_lock_unknown(a, b, c, d, 0, tmp[1]);
3854 }
3855 else {
3856 bezier_eq_noaccel_right(a, b, c, d, dy, l, 0);
3857 }
3858
3859 if (!solve_last) {
3860 sub_v2_v2v2(tmp, bezt_last->vec[1], bezt_last->vec[0]);
3861 last_handle_adj = bezier_calc_handle_adj(tmp, dx[count - 1]);
3862
3863 bezier_lock_unknown(a, b, c, d, count - 1, tmp[1]);
3864 }
3865 else {
3866 bezier_eq_noaccel_left(a, b, c, d, dy, l, count - 1);
3867 }
3868 }
3869
3870 /* main tridiagonal system of equations */
3871
3872 for (int i = 1; i < count - 1; i++) {
3873 bezier_eq_continuous(a, b, c, d, dy, l, i);
3874 }
3875
3876 /* apply correction for user-defined handles with nonstandard x positions */
3877
3878 if (!full_cycle) {
3879 if (count > 2 || solve_last) {
3880 b[1] += l[1] * first_handle_adj;
3881 }
3882
3883 if (count > 2 || solve_first) {
3884 b[count - 2] += last_handle_adj;
3885 }
3886 }
3887
3888 /* solve and output results */
3889
3890 if (tridiagonal_solve_with_limits(a, b, c, d, h, hmin, hmax, solve_count)) {
3891 if (full_cycle) {
3892 h[count - 1] = h[0];
3893 }
3894
3895 for (int i = 1, j = start + 1; i < count - 1; i++, j++) {
3896 bool end = (j == total - 1);
3897
3898 bezier_output_handle(&bezt[j], false, -h[i] / l[i], end);
3899
3900 if (end) {
3901 j = 0;
3902 }
3903
3904 bezier_output_handle(&bezt[j], true, h[i], end);
3905 }
3906
3907 if (solve_first) {
3908 bezier_output_handle(bezt_first, true, h[0], start == 0);
3909 }
3910
3911 if (solve_last) {
3912 bezier_output_handle(bezt_last, false, -h[count - 1] / l[count - 1], start + count == total);
3913 }
3914 }
3915
3916 /* free all */
3917
3918 free_arrays(tmp_buffer);
3919}
3920
3922{
3923 return BEZT_IS_AUTOH(bezt) && bezt->auto_handle_type == HD_AUTOTYPE_NORMAL;
3924}
3925
3926void BKE_nurb_handle_smooth_fcurve(BezTriple *bezt, int total, bool cyclic)
3927{
3928 /* ignore cyclic extrapolation if end points are locked */
3929 cyclic = cyclic && is_free_auto_point(&bezt[0]) && is_free_auto_point(&bezt[total - 1]);
3930
3931 /* if cyclic, try to find a sequence break point */
3932 int search_base = 0;
3933
3934 if (cyclic) {
3935 for (int i = 1; i < total - 1; i++) {
3936 if (!is_free_auto_point(&bezt[i])) {
3937 search_base = i;
3938 break;
3939 }
3940 }
3941
3942 /* all points of the curve are freely changeable auto handles - solve as full cycle */
3943 if (search_base == 0) {
3944 bezier_handle_calc_smooth_fcurve(bezt, total, 0, total, cyclic);
3945 return;
3946 }
3947 }
3948
3949 /* Find continuous sub-sequences of free auto handles and smooth them, starting at search_base.
3950 * In cyclic mode these sub-sequences can span the cycle boundary. */
3951 int start = search_base, count = 1;
3952
3953 for (int i = 1, j = start + 1; i < total; i++, j++) {
3954 /* in cyclic mode: jump from last to first point when necessary */
3955 if (j == total - 1 && cyclic) {
3956 j = 0;
3957 }
3958
3959 /* non auto handle closes the list (we come here at least for the last handle, see above) */
3960 if (!is_free_auto_point(&bezt[j])) {
3961 bezier_handle_calc_smooth_fcurve(bezt, total, start, count + 1, cyclic);
3962 start = j;
3963 count = 1;
3964 }
3965 else {
3966 count++;
3967 }
3968 }
3969
3970 if (count > 1) {
3971 bezier_handle_calc_smooth_fcurve(bezt, total, start, count, cyclic);
3972 }
3973}
3974
3976 BezTriple *bezt, BezTriple *prev, BezTriple *next, const bool is_fcurve, const char smoothing)
3977{
3978 calchandleNurb_intern(bezt, prev, next, BEZT_FLAG_SELECT, is_fcurve, false, smoothing);
3979}
3980
3982 BezTriple *prev,
3983 BezTriple *next,
3984 const eBezTriple_Flag__Alias handle_sel_flag,
3985 const bool is_fcurve,
3986 const char smoothing)
3987{
3989 bezt, prev, next, eBezTriple_Flag(handle_sel_flag), is_fcurve, false, smoothing);
3990}
3991
3992void BKE_nurb_handles_calc(Nurb *nu) /* first, if needed, set handle flags */
3993{
3995}
3996
4002{
4003 BezTriple *bezt;
4004 int i;
4005
4006 for (i = nu->pntsu, bezt = nu->bezt; i--; bezt++) {
4007 if ((bezt->f1 & SELECT) != (bezt->f3 & SELECT)) {
4008 bezt->f1 ^= BEZT_FLAG_SELECT;
4009 bezt->f3 ^= BEZT_FLAG_SELECT;
4010 }
4011 }
4012}
4013
4014/* internal use only (weak) */
4021
4023{
4024 if (nu->pntsu > 1) {
4025 BezTriple *prev = BKE_nurb_bezt_get_prev(nu, bezt);
4026 BezTriple *next = BKE_nurb_bezt_get_next(nu, bezt);
4027 BKE_nurb_handle_calc(bezt, prev, next, false, 0);
4028 }
4029}
4030
4032{
4033 if (nu->pntsu > 1) {
4034 const eBezTriple_Handle h1_back = bezt->h1, h2_back = bezt->h2;
4035
4036 bezt->h1 = bezt->h2 = HD_AUTO;
4037
4038 /* Override handle types to HD_AUTO and recalculate */
4040
4041 bezt->h1 = h1_back;
4042 bezt->h2 = h2_back;
4043 }
4044}
4045
4046#define SEL_F1 (1 << 0)
4047#define SEL_F2 (1 << 1)
4048#define SEL_F3 (1 << 2)
4049
4051 const eBezTriple_Flag__Alias sel_flag,
4052 const eNurbHandleTest_Mode handle_mode)
4053{
4054 short flag = 0;
4055
4056 switch (handle_mode) {
4058 flag = (bezt->f2 & sel_flag) ? (SEL_F1 | SEL_F2 | SEL_F3) : 0;
4059 break;
4060 }
4062 if (bezt->f2 & sel_flag) {
4063 flag = (bezt->f2 & sel_flag) ? (SEL_F1 | SEL_F2 | SEL_F3) : 0;
4064 break;
4065 }
4066 [[fallthrough]];
4067 case NURB_HANDLE_TEST_EACH: {
4068 if (bezt->f1 & sel_flag) {
4069 flag |= SEL_F1;
4070 }
4071 if (bezt->f2 & sel_flag) {
4072 flag |= SEL_F2;
4073 }
4074 if (bezt->f3 & sel_flag) {
4075 flag |= SEL_F3;
4076 }
4077 break;
4078 }
4079 }
4080 return flag;
4081}
4082
4084 const eBezTriple_Flag__Alias sel_flag,
4085 const eNurbHandleTest_Mode handle_mode,
4086 const bool use_around_local)
4087{
4088 short flag = BKE_nurb_bezt_handle_test_calc_flag(bezt, sel_flag, handle_mode);
4089 if (use_around_local) {
4090 flag &= ~SEL_F2;
4091 }
4092
4093 /* check for partial selection */
4094 if (!ELEM(flag, 0, SEL_F1 | SEL_F2 | SEL_F3)) {
4095 if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) {
4096 bezt->h1 = HD_ALIGN;
4097 }
4098 if (ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {
4099 bezt->h2 = HD_ALIGN;
4100 }
4101
4102 if (bezt->h1 == HD_VECT) {
4103 if (!(flag & SEL_F1) != !(flag & SEL_F2)) {
4104 bezt->h1 = HD_FREE;
4105 }
4106 }
4107 if (bezt->h2 == HD_VECT) {
4108 if (!(flag & SEL_F3) != !(flag & SEL_F2)) {
4109 bezt->h2 = HD_FREE;
4110 }
4111 }
4112 }
4113}
4114
4115#undef SEL_F1
4116#undef SEL_F2
4117#undef SEL_F3
4118
4120 const eNurbHandleTest_Mode handle_mode,
4121 const bool use_around_local)
4122{
4123 BezTriple *bezt;
4124 int a;
4125
4126 if (nu->type != CU_BEZIER) {
4127 return;
4128 }
4129
4130 bezt = nu->bezt;
4131 a = nu->pntsu;
4132 while (a--) {
4133 BKE_nurb_bezt_handle_test(bezt, SELECT, handle_mode, use_around_local);
4134 bezt++;
4135 }
4136
4138}
4139
4141{
4142 /* checks handle coordinates and calculates type */
4143 const float eps = 0.0001f;
4144 const float eps_sq = eps * eps;
4145
4146 if (nu == nullptr || nu->bezt == nullptr) {
4147 return;
4148 }
4149
4150 BezTriple *bezt2 = nu->bezt;
4151 BezTriple *bezt1 = bezt2 + (nu->pntsu - 1);
4152 BezTriple *bezt0 = bezt1 - 1;
4153 int i = nu->pntsu;
4154
4155 while (i--) {
4156 bool align = false, leftsmall = false, rightsmall = false;
4157
4158 /* left handle: */
4159 if (flag == 0 || (bezt1->f1 & flag)) {
4160 bezt1->h1 = HD_FREE;
4161 /* Distance too short: vector-handle. */
4162 if (len_squared_v3v3(bezt1->vec[1], bezt0->vec[1]) < eps_sq) {
4163 bezt1->h1 = HD_VECT;
4164 leftsmall = true;
4165 }
4166 else {
4167 /* Aligned handle? */
4168 if (dist_squared_to_line_v3(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < eps_sq) {
4169 align = true;
4170 bezt1->h1 = HD_ALIGN;
4171 }
4172 /* or vector handle? */
4173 if (dist_squared_to_line_v3(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < eps_sq) {
4174 bezt1->h1 = HD_VECT;
4175 }
4176 }
4177 }
4178 /* right handle: */
4179 if (flag == 0 || (bezt1->f3 & flag)) {
4180 bezt1->h2 = HD_FREE;
4181 /* Distance too short: vector-handle. */
4182 if (len_squared_v3v3(bezt1->vec[1], bezt2->vec[1]) < eps_sq) {
4183 bezt1->h2 = HD_VECT;
4184 rightsmall = true;
4185 }
4186 else {
4187 /* Aligned handle? */
4188 if (align) {
4189 bezt1->h2 = HD_ALIGN;
4190 }
4191
4192 /* or vector handle? */
4193 if (dist_squared_to_line_v3(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < eps_sq) {
4194 bezt1->h2 = HD_VECT;
4195 }
4196 }
4197 }
4198 if (leftsmall && bezt1->h2 == HD_ALIGN) {
4199 bezt1->h2 = HD_FREE;
4200 }
4201 if (rightsmall && bezt1->h1 == HD_ALIGN) {
4202 bezt1->h1 = HD_FREE;
4203 }
4204
4205 /* undesired combination: */
4206 if (bezt1->h1 == HD_ALIGN && bezt1->h2 == HD_VECT) {
4207 bezt1->h1 = HD_FREE;
4208 }
4209 if (bezt1->h2 == HD_ALIGN && bezt1->h1 == HD_VECT) {
4210 bezt1->h2 = HD_FREE;
4211 }
4212
4213 bezt0 = bezt1;
4214 bezt1 = bezt2;
4215 bezt2++;
4216 }
4217
4219}
4220
4221void BKE_nurbList_handles_autocalc(ListBaseT<Nurb> *editnurb, eBezTriple_Flag flag)
4222{
4223 for (Nurb &nu : *editnurb) {
4224 BKE_nurb_handles_autocalc(&nu, flag);
4225 }
4226}
4227
4228void BKE_nurbList_handles_set(ListBaseT<Nurb> *editnurb,
4229 eNurbHandleTest_Mode handle_mode,
4230 eBezTriple_Handle code)
4231{
4232 BezTriple *bezt;
4233 int a;
4234
4235 if (ELEM(code, HD_AUTO, HD_VECT)) {
4236 for (Nurb &nu : *editnurb) {
4237 if (nu.type == CU_BEZIER) {
4238 bezt = nu.bezt;
4239 a = nu.pntsu;
4240 while (a--) {
4241 const short flag = BKE_nurb_bezt_handle_test_calc_flag(bezt, SELECT, handle_mode);
4242 if ((flag & (1 << 0)) || (flag & (1 << 2))) {
4243 if (flag & (1 << 0)) {
4244 bezt->h1 = code;
4245 }
4246 if (flag & (1 << 2)) {
4247 bezt->h2 = code;
4248 }
4249 if (bezt->h1 != bezt->h2) {
4250 if (ELEM(bezt->h1, HD_ALIGN, HD_AUTO)) {
4251 bezt->h1 = HD_FREE;
4252 }
4253 if (ELEM(bezt->h2, HD_ALIGN, HD_AUTO)) {
4254 bezt->h2 = HD_FREE;
4255 }
4256 }
4257 }
4258 bezt++;
4259 }
4260
4261 /* like BKE_nurb_handles_calc but moves selected */
4263 }
4264 }
4265 }
4266 else {
4267 eBezTriple_Handle h_new = HD_FREE;
4268
4269 /* There is 1 handle not FREE: FREE it all, else make ALIGNED. */
4270 if (code == 5) {
4271 h_new = HD_ALIGN;
4272 }
4273 else if (code == 6) {
4274 h_new = HD_FREE;
4275 }
4276 else {
4277 /* Toggle */
4278 for (Nurb &nu : *editnurb) {
4279 if (nu.type == CU_BEZIER) {
4280 bezt = nu.bezt;
4281 a = nu.pntsu;
4282 while (a--) {
4283 const short flag = BKE_nurb_bezt_handle_test_calc_flag(bezt, SELECT, handle_mode);
4284 if (((flag & (1 << 0)) && bezt->h1 != HD_FREE) ||
4285 ((flag & (1 << 2)) && bezt->h2 != HD_FREE))
4286 {
4287 h_new = HD_AUTO;
4288 break;
4289 }
4290 bezt++;
4291 }
4292 }
4293 }
4294 h_new = (h_new == HD_FREE) ? HD_ALIGN : HD_FREE;
4295 }
4296 for (Nurb &nu : *editnurb) {
4297 if (nu.type == CU_BEZIER) {
4298 bezt = nu.bezt;
4299 a = nu.pntsu;
4300 while (a--) {
4301 const short flag = BKE_nurb_bezt_handle_test_calc_flag(bezt, SELECT, handle_mode);
4302 if (flag & (1 << 0)) {
4303 bezt->h1 = h_new;
4304 }
4305 if (flag & (1 << 2)) {
4306 bezt->h2 = h_new;
4307 }
4308
4309 bezt++;
4310 }
4311
4312 /* like BKE_nurb_handles_calc but moves selected */
4314 }
4315 }
4316 }
4317}
4318
4319void BKE_nurbList_handles_recalculate(ListBaseT<Nurb> *editnurb,
4320 const bool calc_length,
4321 const eBezTriple_Flag flag)
4322{
4323 BezTriple *bezt;
4324 int a;
4325
4326 for (Nurb &nu : *editnurb) {
4327 if (nu.type != CU_BEZIER) {
4328 continue;
4329 }
4330
4331 bool changed = false;
4332
4333 for (a = nu.pntsu, bezt = nu.bezt; a--; bezt++) {
4334
4335 const bool h1_select = (bezt->f1 & flag) == flag;
4336 const bool h2_select = (bezt->f3 & flag) == flag;
4337
4338 if (h1_select || h2_select) {
4339
4340 float co1_back[3], co2_back[3];
4341
4342 copy_v3_v3(co1_back, bezt->vec[0]);
4343 copy_v3_v3(co2_back, bezt->vec[2]);
4344
4346
4347 if (h1_select) {
4348 if (!calc_length) {
4349 dist_ensure_v3_v3fl(bezt->vec[0], bezt->vec[1], len_v3v3(co1_back, bezt->vec[1]));
4350 }
4351 }
4352 else {
4353 copy_v3_v3(bezt->vec[0], co1_back);
4354 }
4355
4356 if (h2_select) {
4357 if (!calc_length) {
4358 dist_ensure_v3_v3fl(bezt->vec[2], bezt->vec[1], len_v3v3(co2_back, bezt->vec[1]));
4359 }
4360 }
4361 else {
4362 copy_v3_v3(bezt->vec[2], co2_back);
4363 }
4364
4365 changed = true;
4366 }
4367 }
4368
4369 if (changed) {
4370 /* Recalculate the whole curve */
4372 }
4373 }
4374}
4375
4376void BKE_nurbList_flag_set(ListBaseT<Nurb> *editnurb, eBezTriple_Flag flag, bool set)
4377{
4378 BezTriple *bezt;
4379 BPoint *bp;
4380 int a;
4381
4382 for (Nurb &nu : *editnurb) {
4383 if (nu.type == CU_BEZIER) {
4384 a = nu.pntsu;
4385 bezt = nu.bezt;
4386 while (a--) {
4387 if (set) {
4388 bezt->f1 = eBezTriple_Flag(bezt->f1 | flag);
4389 bezt->f2 = eBezTriple_Flag(bezt->f2 | flag);
4390 bezt->f3 = eBezTriple_Flag(bezt->f3 | flag);
4391 }
4392 else {
4393 bezt->f1 &= ~flag;
4394 bezt->f2 &= ~flag;
4395 bezt->f3 &= ~flag;
4396 }
4397 bezt++;
4398 }
4399 }
4400 else {
4401 a = nu.pntsu * nu.pntsv;
4402 bp = nu.bp;
4403 while (a--) {
4404 SET_FLAG_FROM_TEST(bp->f1, set, flag);
4405 bp++;
4406 }
4407 }
4408 }
4409}
4410
4411bool BKE_nurbList_flag_set_from_flag(ListBaseT<Nurb> *editnurb,
4412 eBezTriple_Flag from_flag,
4413 eBezTriple_Flag flag)
4414{
4415 bool changed = false;
4416
4417 for (Nurb &nu : *editnurb) {
4418 if (nu.type == CU_BEZIER) {
4419 for (int i = 0; i < nu.pntsu; i++) {
4420 BezTriple *bezt = &nu.bezt[i];
4421 eBezTriple_Flag old_f1 = bezt->f1, old_f2 = bezt->f2, old_f3 = bezt->f3;
4422
4423 SET_FLAG_FROM_TEST(bezt->f1, bezt->f1 & from_flag, flag);
4424 SET_FLAG_FROM_TEST(bezt->f2, bezt->f2 & from_flag, flag);
4425 SET_FLAG_FROM_TEST(bezt->f3, bezt->f3 & from_flag, flag);
4426
4427 changed |= (old_f1 != bezt->f1) || (old_f2 != bezt->f2) || (old_f3 != bezt->f3);
4428 }
4429 }
4430 else {
4431 for (int i = 0; i < nu.pntsu * nu.pntsv; i++) {
4432 BPoint *bp = &nu.bp[i];
4433 uint8_t old_f1 = bp->f1;
4434
4435 SET_FLAG_FROM_TEST(bp->f1, bp->f1 & from_flag, flag);
4436 changed |= (old_f1 != bp->f1);
4437 }
4438 }
4439 }
4440
4441 return changed;
4442}
4443
4445{
4446 BezTriple *bezt1, *bezt2;
4447 BPoint *bp1, *bp2;
4448 float *fp1, *fp2, *tempf;
4449 int a, b;
4450
4451 if (nu->pntsu == 1 && nu->pntsv == 1) {
4452 return;
4453 }
4454
4455 if (nu->type == CU_BEZIER) {
4456 a = nu->pntsu;
4457 bezt1 = nu->bezt;
4458 bezt2 = bezt1 + (a - 1);
4459 if (a & 1) {
4460 a += 1; /* if odd, also swap middle content */
4461 }
4462 a /= 2;
4463 while (a > 0) {
4464 if (bezt1 != bezt2) {
4465 std::swap(*bezt1, *bezt2);
4466 }
4467
4468 swap_v3_v3(bezt1->vec[0], bezt1->vec[2]);
4469
4470 if (bezt1 != bezt2) {
4471 swap_v3_v3(bezt2->vec[0], bezt2->vec[2]);
4472 }
4473
4474 std::swap(bezt1->h1, bezt1->h2);
4475 std::swap(bezt1->f1, bezt1->f3);
4476
4477 if (bezt1 != bezt2) {
4478 std::swap(bezt2->h1, bezt2->h2);
4479 std::swap(bezt2->f1, bezt2->f3);
4480 bezt1->tilt = -bezt1->tilt;
4481 bezt2->tilt = -bezt2->tilt;
4482 }
4483 else {
4484 bezt1->tilt = -bezt1->tilt;
4485 }
4486 a--;
4487 bezt1++;
4488 bezt2--;
4489 }
4490 }
4491 else if (nu->pntsv == 1) {
4492 a = nu->pntsu;
4493 bp1 = nu->bp;
4494 bp2 = bp1 + (a - 1);
4495 a /= 2;
4496 while (bp1 != bp2 && a > 0) {
4497 std::swap(*bp1, *bp2);
4498 a--;
4499 bp1->tilt = -bp1->tilt;
4500 bp2->tilt = -bp2->tilt;
4501 bp1++;
4502 bp2--;
4503 }
4504 /* If there are odd number of points no need to touch coord of middle one,
4505 * but still need to change its tilt.
4506 */
4507 if (nu->pntsu & 1) {
4508 bp1->tilt = -bp1->tilt;
4509 }
4510 if (nu->type == CU_NURBS) {
4511 /* no knots for too short paths */
4512 if (nu->knotsu) {
4513 /* inverse knots */
4514 a = KNOTSU(nu);
4515 fp1 = nu->knotsu;
4516 fp2 = fp1 + (a - 1);
4517 a /= 2;
4518 while (fp1 != fp2 && a > 0) {
4519 std::swap(*fp1, *fp2);
4520 a--;
4521 fp1++;
4522 fp2--;
4523 }
4524 /* and make in increasing order again */
4525 a = KNOTSU(nu);
4526 fp1 = nu->knotsu;
4527 fp2 = tempf = MEM_new_array_uninitialized<float>(size_t(a), "switchdirect");
4528 a--;
4529 fp2[a] = fp1[a];
4530 while (a--) {
4531 fp2[0] = fabsf(fp1[1] - fp1[0]);
4532 fp1++;
4533 fp2++;
4534 }
4535
4536 a = KNOTSU(nu) - 1;
4537 fp1 = nu->knotsu;
4538 fp2 = tempf;
4539 fp1[0] = 0.0;
4540 fp1++;
4541 while (a--) {
4542 fp1[0] = fp1[-1] + fp2[0];
4543 fp1++;
4544 fp2++;
4545 }
4546 MEM_delete(tempf);
4547 }
4548 }
4549 }
4550 else {
4551 for (b = 0; b < nu->pntsv; b++) {
4552 bp1 = nu->bp + b * nu->pntsu;
4553 a = nu->pntsu;
4554 bp2 = bp1 + (a - 1);
4555 a /= 2;
4556
4557 while (bp1 != bp2 && a > 0) {
4558 std::swap(*bp1, *bp2);
4559 a--;
4560 bp1++;
4561 bp2--;
4562 }
4563 }
4564 }
4565}
4566
4567void BKE_curve_nurbs_vert_coords_get(const ListBaseT<Nurb> *lb, MutableSpan<float3> vert_coords)
4568{
4569 int index = 0;
4570 for (const Nurb &nu : *lb) {
4571 if (nu.type == CU_BEZIER) {
4572 const BezTriple *bezt = nu.bezt;
4573 for (int i = 0; i < nu.pntsu; i++, bezt++) {
4574 vert_coords[index] = bezt->vec[0];
4575 index++;
4576 vert_coords[index] = bezt->vec[1];
4577 index++;
4578 vert_coords[index] = bezt->vec[2];
4579 index++;
4580 }
4581 }
4582 else {
4583 const BPoint *bp = nu.bp;
4584 for (int i = 0; i < nu.pntsu * nu.pntsv; i++, bp++) {
4585 vert_coords[index] = bp->vec;
4586 index++;
4587 }
4588 }
4589 }
4590}
4591
4593{
4594 Array<float3> vert_coords(BKE_nurbList_verts_count(lb));
4595 BKE_curve_nurbs_vert_coords_get(lb, vert_coords);
4596 return vert_coords;
4597}
4598
4600 const Span<float3> vert_coords,
4601 const float4x4 &transform,
4602 const bool constrain_2d)
4603{
4604 int index = 0;
4605 for (Nurb &nu : *lb) {
4606 if (nu.type == CU_BEZIER) {
4607 BezTriple *bezt = nu.bezt;
4608
4609 for (int i = 0; i < nu.pntsu; i++, bezt++) {
4610 mul_v3_m4v3(bezt->vec[0], transform.ptr(), vert_coords[index]);
4611 index++;
4612 mul_v3_m4v3(bezt->vec[1], transform.ptr(), vert_coords[index]);
4613 index++;
4614 mul_v3_m4v3(bezt->vec[2], transform.ptr(), vert_coords[index]);
4615 index++;
4616 }
4617 }
4618 else {
4619 BPoint *bp = nu.bp;
4620
4621 for (int i = 0; i < nu.pntsu * nu.pntsv; i++, bp++) {
4622 mul_v3_m4v3(bp->vec, transform.ptr(), vert_coords[index]);
4623 index++;
4624 }
4625 }
4626
4627 if (constrain_2d) {
4629 }
4630
4632 }
4633}
4634
4635void BKE_curve_nurbs_vert_coords_apply(ListBaseT<Nurb> *lb,
4636 const Span<float3> vert_coords,
4637 const bool constrain_2d)
4638{
4639 int index = 0;
4640 for (Nurb &nu : *lb) {
4641 if (nu.type == CU_BEZIER) {
4642 BezTriple *bezt = nu.bezt;
4643
4644 for (int i = 0; i < nu.pntsu; i++, bezt++) {
4645 copy_v3_v3(bezt->vec[0], vert_coords[index]);
4646 index++;
4647 copy_v3_v3(bezt->vec[1], vert_coords[index]);
4648 index++;
4649 copy_v3_v3(bezt->vec[2], vert_coords[index]);
4650 index++;
4651 }
4652 }
4653 else {
4654 BPoint *bp = nu.bp;
4655
4656 for (int i = 0; i < nu.pntsu * nu.pntsv; i++, bp++) {
4657 copy_v3_v3(bp->vec, vert_coords[index]);
4658 index++;
4659 }
4660 }
4661
4662 if (constrain_2d) {
4664 }
4665
4667 }
4668}
4669
4670Array<float3> BKE_curve_nurbs_key_vert_coords_alloc(const ListBaseT<Nurb> *lb, const float *key)
4671{
4672 Array<float3> vert_coords(BKE_nurbList_verts_count(lb));
4673
4674 int index = 0;
4675 for (const Nurb &nu : *lb) {
4676 if (nu.type == CU_BEZIER) {
4677
4678 for (int i = 0; i < nu.pntsu; i++) {
4679 vert_coords[index] = &key[0];
4680 index++;
4681 vert_coords[index] = &key[3];
4682 index++;
4683 vert_coords[index] = &key[6];
4684 index++;
4686 }
4687 }
4688 else {
4689 for (int i = 0; i < nu.pntsu * nu.pntsv; i++) {
4690 vert_coords[index] = key;
4691 index++;
4693 }
4694 }
4695 }
4696 return vert_coords;
4697}
4698
4699void BKE_curve_nurbs_key_vert_tilts_apply(ListBaseT<Nurb> *lb, const float *key)
4700{
4701 for (Nurb &nu : *lb) {
4702 if (nu.type == CU_BEZIER) {
4703 BezTriple *bezt = nu.bezt;
4704
4705 for (int i = 0; i < nu.pntsu; i++, bezt++) {
4706 bezt->tilt = key[9];
4707 bezt->radius = key[10];
4709 }
4710 }
4711 else {
4712 BPoint *bp = nu.bp;
4713
4714 for (int i = 0; i < nu.pntsu * nu.pntsv; i++, bp++) {
4715 bp->tilt = key[3];
4716 bp->radius = key[4];
4718 }
4719 }
4720 }
4721}
4722
4724 const short order,
4725 const short flag,
4726 const short type,
4727 const bool is_surf,
4728 int *r_points_needed)
4729{
4730 if (pnts <= 1) {
4732 }
4733 if (type == CU_NURBS) {
4734 if (pnts < order) {
4736 }
4737 if (flag & CU_NURB_BEZIER) {
4738 int points_needed = 0;
4739 if (flag & CU_NURB_CYCLIC) {
4740 const int remainder = pnts % (order - 1);
4741 points_needed = remainder > 0 ? order - 1 - remainder : 0;
4742 }
4743 else if (((flag & CU_NURB_ENDPOINT) == 0) && pnts <= order) {
4744 points_needed = order + 1 - pnts;
4745 }
4746 if (points_needed) {
4747 *r_points_needed = points_needed;
4750 }
4751 }
4752 }
4754}
4755
4756bool BKE_nurb_valid_message(const int pnts,
4757 const short order,
4758 const short flag,
4759 const short type,
4760 const bool is_surf,
4761 const int dir,
4762 char *message_dst,
4763 const size_t maxncpy)
4764{
4765 int points_needed;
4767 pnts, order, flag, type, is_surf, &points_needed);
4768
4769 switch (status) {
4771 message_dst[0] = 0;
4772 return false;
4774 if (dir == 1) {
4775 /* Exception made for curves as their pntsv == 1. */
4776 message_dst[0] = 0;
4777 return false;
4778 }
4779 BLI_strncpy(message_dst, RPT_("At least two points required"), maxncpy);
4780 break;
4782 BLI_strncpy(message_dst, RPT_("Must have more control points than Order"), maxncpy);
4783 break;
4785 BLI_snprintf(message_dst,
4786 maxncpy,
4787 RPT_("%d more %s row(s) needed for Bézier"),
4788 points_needed,
4789 dir == 0 ? "U" : "V");
4790 break;
4793 message_dst, maxncpy, RPT_("%d more point(s) needed for Bézier"), points_needed);
4794 break;
4795 }
4796
4797 return true;
4798}
4799
4801{
4802 int points_needed;
4805 nu->pntsu, nu->orderu, nu->flagu, nu->type, nu->pntsv > 1, &points_needed);
4806}
4807
4809{
4810 int points_needed;
4813 nu->pntsv, nu->orderv, nu->flagv, nu->type, nu->pntsv > 1, &points_needed);
4814}
4815
4817{
4818 if (!BKE_nurb_check_valid_u(nu)) {
4819 return false;
4820 }
4821 if ((nu->pntsv > 1) && !BKE_nurb_check_valid_v(nu)) {
4822 return false;
4823 }
4824
4825 return true;
4826}
4827
4829{
4830 bool changed = false;
4831 if (nu->pntsu < nu->orderu) {
4832 nu->orderu = max_ii(2, nu->pntsu);
4833 changed = true;
4834 }
4835 return changed;
4836}
4837
4839{
4840 bool changed = false;
4841 if (nu->pntsv < nu->orderv) {
4842 nu->orderv = max_ii(2, nu->pntsv);
4843 changed = true;
4844 }
4845 return changed;
4846}
4847
4849 const eNurbType type,
4850 const bool use_handles,
4851 const char **r_err_msg)
4852{
4853 BezTriple *bezt;
4854 BPoint *bp;
4855 int a, c, nr;
4856
4857 if (nu->type == CU_POLY) {
4858 if (type == CU_BEZIER) { /* To Bezier with vector-handles. */
4859 nr = nu->pntsu;
4860 bezt = MEM_new_array_zeroed<BezTriple>(nr, "setsplinetype2");
4861 nu->bezt = bezt;
4862 a = nr;
4863 bp = nu->bp;
4864 while (a--) {
4865 copy_v3_v3(bezt->vec[1], bp->vec);
4866 bezt->f1 = bezt->f2 = bezt->f3 = eBezTriple_Flag(bp->f1);
4867 bezt->h1 = bezt->h2 = HD_VECT;
4868 bezt->weight = bp->weight;
4869 bezt->radius = bp->radius;
4870 bp++;
4871 bezt++;
4872 }
4873 MEM_delete(nu->bp);
4874 nu->bp = nullptr;
4875 nu->pntsu = nr;
4876 nu->pntsv = 0;
4877 nu->type = CU_BEZIER;
4879 }
4880 else if (type == CU_NURBS) {
4881 nu->type = CU_NURBS;
4882 nu->orderu = 4;
4883 nu->flagu &= CU_NURB_CYCLIC; /* disable all flags except for cyclic */
4885 a = nu->pntsu * nu->pntsv;
4886 bp = nu->bp;
4887 while (a--) {
4888 bp->vec[3] = 1.0;
4889 bp++;
4890 }
4891 }
4892 }
4893 else if (nu->type == CU_BEZIER) { /* Bezier */
4894 if (ELEM(type, CU_POLY, CU_NURBS)) {
4895 nr = use_handles ? (3 * nu->pntsu) : nu->pntsu;
4896 nu->bp = MEM_new_array_zeroed<BPoint>(nr, "setsplinetype");
4897 a = nu->pntsu;
4898 bezt = nu->bezt;
4899 bp = nu->bp;
4900 while (a--) {
4901 if ((type == CU_POLY && bezt->h1 == HD_VECT && bezt->h2 == HD_VECT) ||
4902 (use_handles == false))
4903 {
4904 /* vector handle becomes one poly vertex */
4905 copy_v3_v3(bp->vec, bezt->vec[1]);
4906 bp->vec[3] = 1.0;
4907 bp->f1 = bezt->f2;
4908 if (use_handles) {
4909 nr -= 2;
4910 }
4911 bp->radius = bezt->radius;
4912 bp->weight = bezt->weight;
4913 bp++;
4914 }
4915 else {
4916 const eBezTriple_Flag *f = &bezt->f1;
4917 for (c = 0; c < 3; c++, f++) {
4918 copy_v3_v3(bp->vec, bezt->vec[c]);
4919 bp->vec[3] = 1.0;
4920 bp->f1 = *f;
4921 bp->radius = bezt->radius;
4922 bp->weight = bezt->weight;
4923 bp++;
4924 }
4925 }
4926 bezt++;
4927 }
4928 MEM_delete(nu->bezt);
4929 nu->bezt = nullptr;
4930 nu->pntsu = nr;
4931 nu->pntsv = 1;
4932 nu->orderu = 4;
4933 nu->orderv = 1;
4934 nu->type = type;
4935
4936 if (type == CU_NURBS) {
4937 nu->flagu &= CU_NURB_CYCLIC; /* disable all flags except for cyclic */
4938 nu->flagu |= CU_NURB_BEZIER;
4940 }
4941 }
4942 }
4943 else if (nu->type == CU_NURBS) {
4944 if (type == CU_POLY) {
4945 nu->type = CU_POLY;
4946 if (nu->knotsu) {
4947 MEM_delete(nu->knotsu); /* python created nurbs have a knotsu of zero */
4948 }
4949 nu->knotsu = nullptr;
4950 MEM_SAFE_DELETE(nu->knotsv);
4951 }
4952 else if (type == CU_BEZIER) { /* to Bezier */
4953 nr = nu->pntsu / 3;
4954
4955 if (nr < 2) {
4956 if (r_err_msg != nullptr) {
4957 *r_err_msg = "At least 6 points required for conversion";
4958 }
4959 return false; /* conversion impossible */
4960 }
4961
4962 bezt = MEM_new_array_zeroed<BezTriple>(nr, "setsplinetype2");
4963 nu->bezt = bezt;
4964 a = nr;
4965 bp = nu->bp;
4966 while (a--) {
4967 copy_v3_v3(bezt->vec[0], bp->vec);
4968 bezt->f1 = eBezTriple_Flag(bp->f1);
4969 bp++;
4970 copy_v3_v3(bezt->vec[1], bp->vec);
4971 bezt->f2 = eBezTriple_Flag(bp->f1);
4972 bp++;
4973 copy_v3_v3(bezt->vec[2], bp->vec);
4974 bezt->f3 = eBezTriple_Flag(bp->f1);
4975 bezt->radius = bp->radius;
4976 bezt->weight = bp->weight;
4977 bp++;
4978 bezt++;
4979 }
4980 MEM_delete(nu->bp);
4981 nu->bp = nullptr;
4982 MEM_delete(nu->knotsu);
4983 nu->knotsu = nullptr;
4984 nu->pntsu = nr;
4985 nu->type = CU_BEZIER;
4986 }
4987 }
4988
4989 return true;
4990}
4991
4992ListBaseT<Nurb> *BKE_curve_nurbs_get(Curve *cu)
4993{
4994 if (cu->editnurb) {
4995 return BKE_curve_editNurbs_get(cu);
4996 }
4997
4998 return &cu->nurb;
4999}
5000
5001const ListBaseT<Nurb> *BKE_curve_nurbs_get_for_read(const Curve *cu)
5002{
5003 if (cu->editnurb) {
5005 }
5006
5007 return &cu->nurb;
5008}
5009
5011{
5012 if (nu == nullptr) {
5013 cu->actnu = CU_ACT_NONE;
5014 }
5015 else {
5016 BLI_assert(!nu->hide);
5017 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
5018 cu->actnu = BLI_findindex(nurbs, nu);
5019 }
5020}
5021
5023{
5024 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
5025 return static_cast<Nurb *>(BLI_findlink(nurbs, cu->actnu));
5026}
5027
5029{
5030 Nurb *nu = nullptr;
5031 void *vert = nullptr;
5032
5033 BKE_curve_nurb_vert_active_get(cu, &nu, &vert);
5034 return vert;
5035}
5036
5037int BKE_curve_nurb_vert_index_get(const Nurb *nu, const void *vert)
5038{
5039 if (nu->type == CU_BEZIER) {
5040 BLI_assert(ARRAY_HAS_ITEM((BezTriple *)vert, nu->bezt, nu->pntsu));
5041 return static_cast<BezTriple *>(const_cast<void *>(vert)) - nu->bezt;
5042 }
5043
5044 BLI_assert(ARRAY_HAS_ITEM((BPoint *)vert, nu->bp, nu->pntsu * nu->pntsv));
5045 return static_cast<BPoint *>(const_cast<void *>(vert)) - nu->bp;
5046}
5047
5048void BKE_curve_nurb_vert_active_set(Curve *cu, const Nurb *nu, const void *vert)
5049{
5050 if (nu) {
5052
5053 if (vert) {
5054 cu->actvert = BKE_curve_nurb_vert_index_get(nu, vert);
5055 }
5056 else {
5057 cu->actvert = CU_ACT_NONE;
5058 }
5059 }
5060 else {
5061 cu->actnu = cu->actvert = CU_ACT_NONE;
5062 }
5063}
5064
5065bool BKE_curve_nurb_vert_active_get(Curve *cu, Nurb **r_nu, void **r_vert)
5066{
5067 Nurb *nu = nullptr;
5068 void *vert = nullptr;
5069
5070 if (cu->actvert != CU_ACT_NONE) {
5071 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
5072 nu = static_cast<Nurb *>(BLI_findlink(nurbs, cu->actnu));
5073
5074 if (nu) {
5075 if (nu->type == CU_BEZIER) {
5076 BLI_assert(nu->pntsu > cu->actvert);
5077 vert = &nu->bezt[cu->actvert];
5078 }
5079 else {
5080 BLI_assert((nu->pntsu * nu->pntsv) > cu->actvert);
5081 vert = &nu->bp[cu->actvert];
5082 }
5083 }
5084 }
5085
5086 *r_nu = nu;
5087 *r_vert = vert;
5088
5089 return (*r_vert != nullptr);
5090}
5091
5093{
5094 Nurb *nu;
5095 void *vert;
5096
5097 if (BKE_curve_nurb_vert_active_get(cu, &nu, &vert)) {
5098 if (nu->type == CU_BEZIER) {
5099 BezTriple *bezt = static_cast<BezTriple *>(vert);
5100 if (BEZT_ISSEL_ANY(bezt) == 0) {
5101 cu->actvert = CU_ACT_NONE;
5102 }
5103 }
5104 else {
5105 BPoint *bp = static_cast<BPoint *>(vert);
5106 if ((bp->f1 & SELECT) == 0) {
5107 cu->actvert = CU_ACT_NONE;
5108 }
5109 }
5110
5111 if (nu->hide) {
5112 cu->actnu = CU_ACT_NONE;
5113 }
5114 }
5115}
5116
5117static std::optional<Bounds<float3>> calc_nurblist_bounds(const ListBaseT<Nurb> *nurbs,
5118 const bool use_radius)
5119{
5120 if (nurbs->is_empty()) {
5121 return std::nullopt;
5122 }
5123 float3 min(std::numeric_limits<float>::max());
5124 float3 max(std::numeric_limits<float>::lowest());
5125 for (const Nurb &nu : *nurbs) {
5126 calc_nurb_minmax(&nu, use_radius, min, max);
5127 }
5128 return Bounds<float3>{min, max};
5129}
5130
5131std::optional<Bounds<float3>> BKE_curve_minmax(const Curve *cu, bool use_radius)
5132{
5133 const ListBaseT<Nurb> *nurb_lb = BKE_curve_nurbs_get_for_read(cu);
5134 const bool is_font = nurb_lb->is_empty() && (cu->len != 0);
5135 /* For font curves we generate temp list of splines.
5136 *
5137 * This is likely to be fine, this function is not supposed to be called
5138 * often, and it's the only way to get meaningful bounds for fonts.
5139 */
5140 if (is_font) {
5141 ListBaseT<Nurb> temp_nurb_lb{};
5143 nullptr, *cu, FO_EDIT, &temp_nurb_lb, nullptr, nullptr, nullptr, nullptr, nullptr);
5144 BLI_SCOPED_DEFER([&]() { BKE_nurbList_free(&temp_nurb_lb); });
5145 return calc_nurblist_bounds(&temp_nurb_lb, false);
5146 }
5147
5148 return calc_nurblist_bounds(nurb_lb, use_radius);
5149}
5150
5151bool BKE_curve_center_median(Curve *cu, float cent[3])
5152{
5153 ListBaseT<Nurb> *nurb_lb = BKE_curve_nurbs_get(cu);
5154 int total = 0;
5155
5156 zero_v3(cent);
5157
5158 for (Nurb &nu : *nurb_lb) {
5159 int i;
5160
5161 if (nu.type == CU_BEZIER) {
5162 BezTriple *bezt;
5163 i = nu.pntsu;
5164 total += i * 3;
5165 for (bezt = nu.bezt; i--; bezt++) {
5166 add_v3_v3(cent, bezt->vec[0]);
5167 add_v3_v3(cent, bezt->vec[1]);
5168 add_v3_v3(cent, bezt->vec[2]);
5169 }
5170 }
5171 else {
5172 BPoint *bp;
5173 i = nu.pntsu * nu.pntsv;
5174 total += i;
5175 for (bp = nu.bp; i--; bp++) {
5176 add_v3_v3(cent, bp->vec);
5177 }
5178 }
5179 }
5180
5181 if (total) {
5182 mul_v3_fl(cent, 1.0f / float(total));
5183 }
5184
5185 return (total != 0);
5186}
5187
5189 const float mat[4][4],
5190 const bool do_keys,
5191 const bool do_props,
5192 const float unit_scale)
5193{
5194 BPoint *bp;
5195 BezTriple *bezt;
5196 int i;
5197
5198 const bool is_uniform_scaled = is_uniform_scaled_m4(mat);
5199
5200 for (Nurb &nu : cu->nurb) {
5201 if (nu.type == CU_BEZIER) {
5202 i = nu.pntsu;
5203 for (bezt = nu.bezt; i--; bezt++) {
5204 mul_m4_v3(mat, bezt->vec[0]);
5205 mul_m4_v3(mat, bezt->vec[1]);
5206 mul_m4_v3(mat, bezt->vec[2]);
5207 if (do_props) {
5208 bezt->radius *= unit_scale;
5209 }
5210 if (!is_uniform_scaled) {
5211 if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM) || ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {
5212 bezt->h1 = bezt->h2 = HD_ALIGN;
5213 }
5214 }
5215 }
5217 }
5218 else {
5219 i = nu.pntsu * nu.pntsv;
5220 for (bp = nu.bp; i--; bp++) {
5221 mul_m4_v3(mat, bp->vec);
5222 if (do_props) {
5223 bp->radius *= unit_scale;
5224 }
5225 }
5226 }
5227 }
5228
5229 if (do_keys && cu->key) {
5230 for (KeyBlock &kb : cu->key->block) {
5231 float *fp = static_cast<float *>(kb.data);
5232 int n = kb.totelem;
5233
5234 for (Nurb &nu : cu->nurb) {
5235 if (nu.type == CU_BEZIER) {
5236 for (i = nu.pntsu; i && (n -= KEYELEM_ELEM_LEN_BEZTRIPLE) >= 0; i--) {
5237 mul_m4_v3(mat, &fp[0]);
5238 mul_m4_v3(mat, &fp[3]);
5239 mul_m4_v3(mat, &fp[6]);
5240 if (do_props) {
5241 fp[10] *= unit_scale; /* radius */
5242 }
5244 }
5245 }
5246 else {
5247 for (i = nu.pntsu * nu.pntsv; i && (n -= KEYELEM_ELEM_LEN_BPOINT) >= 0; i--) {
5248 mul_m4_v3(mat, fp);
5249 if (do_props) {
5250 fp[4] *= unit_scale; /* radius */
5251 }
5253 }
5254 }
5255 }
5256 }
5257 }
5258}
5259
5260void BKE_curve_transform(Curve *cu, const float mat[4][4], const bool do_keys, const bool do_props)
5261{
5262 float unit_scale = mat4_to_scale(mat);
5263 BKE_curve_transform_ex(cu, mat, do_keys, do_props, unit_scale);
5264}
5265
5266void BKE_curve_translate(Curve *cu, const float offset[3], const bool do_keys)
5267{
5268 ListBaseT<Nurb> *nurb_lb = BKE_curve_nurbs_get(cu);
5269
5270 for (Nurb &nu : *nurb_lb) {
5271 if (nu.type == CU_BEZIER) {
5272 int i = nu.pntsu;
5273 for (BezTriple *bezt = nu.bezt; i--; bezt++) {
5274 add_v3_v3(bezt->vec[0], offset);
5275 add_v3_v3(bezt->vec[1], offset);
5276 add_v3_v3(bezt->vec[2], offset);
5277 }
5278 }
5279 else {
5280 int i = nu.pntsu * nu.pntsv;
5281 for (BPoint *bp = nu.bp; i--; bp++) {
5282 add_v3_v3(bp->vec, offset);
5283 }
5284 }
5285 }
5286
5287 if (do_keys && cu->key) {
5288 for (KeyBlock &kb : cu->key->block) {
5289 float *fp = static_cast<float *>(kb.data);
5290 int n = kb.totelem;
5291
5292 for (Nurb &nu : cu->nurb) {
5293 if (nu.type == CU_BEZIER) {
5294 for (int i = nu.pntsu; i && (n -= KEYELEM_ELEM_LEN_BEZTRIPLE) >= 0; i--) {
5295 add_v3_v3(&fp[0], offset);
5296 add_v3_v3(&fp[3], offset);
5297 add_v3_v3(&fp[6], offset);
5299 }
5300 }
5301 else {
5302 for (int i = nu.pntsu * nu.pntsv; i && (n -= KEYELEM_ELEM_LEN_BPOINT) >= 0; i--) {
5303 add_v3_v3(fp, offset);
5305 }
5306 }
5307 }
5308 }
5309 }
5310}
5311
5313{
5314 if (cu->ob_type == OB_FONT) {
5315 CharInfo *info = cu->strinfo;
5316 for (int i = cu->len_char32 - 1; i >= 0; i--, info++) {
5317 if (info->mat_nr && info->mat_nr >= index) {
5318 info->mat_nr--;
5319 }
5320 }
5321 }
5322 else {
5323 for (Nurb &nu : cu->nurb) {
5324 if (nu.mat_nr && nu.mat_nr >= index) {
5325 nu.mat_nr--;
5326 }
5327 }
5328 }
5329}
5330
5332{
5333 if (cu->ob_type == OB_FONT) {
5334 const CharInfo *info = cu->strinfo;
5335 for (int i = cu->len_char32 - 1; i >= 0; i--, info++) {
5336 if (info->mat_nr == index) {
5337 return true;
5338 }
5339 }
5340 }
5341 else {
5342 for (const Nurb &nu : cu->nurb) {
5343 if (nu.mat_nr == index) {
5344 return true;
5345 }
5346 }
5347 }
5348
5349 return false;
5350}
5351
5353{
5354 if (cu->ob_type == OB_FONT) {
5355 CharInfo *info = cu->strinfo;
5356 for (int i = cu->len_char32 - 1; i >= 0; i--, info++) {
5357 info->mat_nr = 0;
5358 }
5359 }
5360 else {
5361 for (Nurb &nu : cu->nurb) {
5362 nu.mat_nr = 0;
5363 }
5364 }
5365}
5366
5368{
5369 bool is_valid = true;
5370
5371 if (cu->ob_type == OB_FONT) {
5372 CharInfo *info = cu->strinfo;
5373 const int max_idx = max_ii(0, cu->totcol); /* OB_FONT use 1 as first mat index, not 0!!! */
5374 int i;
5375 for (i = cu->len_char32 - 1; i >= 0; i--, info++) {
5376 if (info->mat_nr > max_idx) {
5377 info->mat_nr = 0;
5378 is_valid = false;
5379 }
5380 }
5381 }
5382 else {
5383 const int max_idx = max_ii(0, cu->totcol - 1);
5384 for (Nurb &nu : cu->nurb) {
5385 if (nu.mat_nr > max_idx) {
5386 nu.mat_nr = 0;
5387 is_valid = false;
5388 }
5389 }
5390 }
5391
5392 if (!is_valid) {
5394 return true;
5395 }
5396 return false;
5397}
5398
5399void BKE_curve_material_remap(Curve *cu, const uint *remap, uint remap_len)
5400{
5401 const short remap_len_short = short(remap_len);
5402
5403#define MAT_NR_REMAP(n) \
5404 if (n < remap_len_short) { \
5405 BLI_assert(n >= 0 && remap[n] < remap_len_short); \
5406 n = remap[n]; \
5407 } \
5408 ((void)0)
5409
5410 if (cu->ob_type == OB_FONT) {
5411 CharInfo *strinfo;
5412 int charinfo_len, i;
5413
5414 if (cu->editfont) {
5415 EditFont *ef = cu->editfont;
5416 strinfo = ef->textbufinfo;
5417 charinfo_len = ef->len;
5418 }
5419 else {
5420 strinfo = cu->strinfo;
5421 charinfo_len = cu->len_char32;
5422 }
5423
5424 for (i = 0; i <= charinfo_len; i++) {
5425 MAT_NR_REMAP(strinfo[i].mat_nr);
5426 }
5427 }
5428 else {
5429 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
5430
5431 if (nurbs) {
5432 for (Nurb &nu : *nurbs) {
5433 MAT_NR_REMAP(nu.mat_nr);
5434 }
5435 }
5436 }
5437
5438#undef MAT_NR_REMAP
5439}
5440
5441void BKE_curve_smooth_flag_set(Curve *cu, const bool use_smooth)
5442{
5443 if (use_smooth) {
5444 for (Nurb &nu : cu->nurb) {
5445 nu.flag |= CU_SMOOTH;
5446 }
5447 }
5448 else {
5449 for (Nurb &nu : cu->nurb) {
5450 nu.flag &= ~CU_SMOOTH;
5451 }
5452 }
5453}
5454
5455void BKE_curve_rect_from_textbox(const Curve *cu, const TextBox *tb, rctf *r_rect)
5456{
5457 r_rect->xmin = cu->xof + tb->x;
5458 r_rect->ymax = cu->yof + tb->y + cu->fsize;
5459
5460 r_rect->xmax = r_rect->xmin + tb->w;
5461 r_rect->ymin = r_rect->ymax - tb->h;
5462}
5463
5464void BKE_curve_correct_bezpart(const float v1[2], float v2[2], float v3[2], const float v4[2])
5465{
5466 float h1[2], h2[2], len1, len2, len, fac;
5467
5468 /* Calculate handle deltas. */
5469 h1[0] = v1[0] - v2[0];
5470 h1[1] = v1[1] - v2[1];
5471
5472 h2[0] = v4[0] - v3[0];
5473 h2[1] = v4[1] - v3[1];
5474
5475 /* Calculate distances:
5476 * - len = span of time between keyframes
5477 * - len1 = length of handle of start key
5478 * - len2 = length of handle of end key
5479 */
5480 len = v4[0] - v1[0];
5481 len1 = fabsf(h1[0]);
5482 len2 = fabsf(h2[0]);
5483
5484 /* If the handles have no length, no need to do any corrections. */
5485 if ((len1 + len2) == 0.0f) {
5486 return;
5487 }
5488
5489 /* the two handles cross over each other, so force them
5490 * apart using the proportion they overlap
5491 */
5492 if ((len1 + len2) > len) {
5493 fac = len / (len1 + len2);
5494
5495 v2[0] = (v1[0] - fac * h1[0]);
5496 v2[1] = (v1[1] - fac * h1[1]);
5497
5498 v3[0] = (v4[0] - fac * h2[0]);
5499 v3[1] = (v4[1] - fac * h2[1]);
5500 }
5501}
5502
5503std::optional<int> Curve::material_index_max() const
5504{
5505 if (this->nurb.is_empty()) {
5506 return std::nullopt;
5507 }
5508 int max_index = 0;
5509 for (const Nurb &nurb : this->nurb) {
5510 max_index = std::max<int>(max_index, nurb.mat_nr);
5511 }
5512 return clamp_i(max_index, 0, MAXMAT);
5513}
5514
5515/* **** Depsgraph evaluation **** */
5516
5517void BKE_curve_eval_geometry(Depsgraph *depsgraph, Curve *curve)
5518{
5519 DEG_debug_print_eval(depsgraph, __func__, curve->id.name, curve);
5521 if (DEG_is_active(depsgraph)) {
5522 Curve *curve_orig = DEG_get_original(curve);
5525 copy_v3_v3(curve_orig->texspace_location, curve->texspace_location);
5526 copy_v3_v3(curve_orig->texspace_size, curve->texspace_size);
5527 }
5528 }
5529}
5530
5531/* Draw Engine */
5532
5533void (*BKE_curve_batch_cache_dirty_tag_cb)(Curve *cu, int mode) = nullptr;
5535
5537{
5538 if (cu->batch_cache) {
5540 }
5541}
5543{
5544 if (cu->batch_cache) {
5546 }
5547}
5548
5549} // namespace blender
#define KNOTSU(nu)
Definition BKE_curve.hh:78
#define CU_DO_RADIUS(cu, nu)
Definition BKE_curve.hh:87
#define CU_IS_2D(cu)
Definition BKE_curve.hh:93
#define KNOTSV(nu)
Definition BKE_curve.hh:80
#define SEGMENTSU(nu)
Definition BKE_curve.hh:84
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
#define SELECT
Definition BKE_mask.hh:19
#define NAN_FLT
#define DEG2RADF(_deg)
#define M_PI_2
#define BLI_SCOPED_DEFER(function_to_defer)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define RPT_(msgid)
#define BLT_I18NCONTEXT_ID_CURVE_LEGACY
#define DATA_(msgid)
#define FILTER_ID_OB
Definition DNA_ID.h:1216
#define FILTER_ID_MA
Definition DNA_ID.h:1210
#define FILTER_ID_CU_LEGACY
Definition DNA_ID.h:1203
#define FILTER_ID_VF
Definition DNA_ID.h:1224
#define FILTER_ID_KE
Definition DNA_ID.h:1234
#define MAXTEXTBOX
#define CU_ACT_NONE
#define KEY_CU_EASE
#define BEZT_IS_AUTOH(bezt)
#define BEZT_ISSEL_ANY(bezt)
#define KEYELEM_ELEM_LEN_BPOINT
#define KEYELEM_FLOAT_LEN_BEZTRIPLE
#define KEYELEM_FLOAT_LEN_BPOINT
#define KEYELEM_ELEM_LEN_BEZTRIPLE
#define MAXMAT
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define ARRAY_HAS_ITEM(arr_item, arr_start, arr_len)
#define UNUSED_VARS_NDEBUG(...)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
#define MEM_realloc_zeroed(vmemh, len)
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr IndexRange()=default
Value pop_default(const Key &key, const Value &default_value)
Definition BLI_map.hh:439
#define p2_h1
#define p2_h2
#define MAT_NR_REMAP(n)
#define SEL_F1
Definition curve.cc:4046
#define SEL_F2
Definition curve.cc:4047
#define SEL_F3
Definition curve.cc:4048
#define str(s)
#define FLT_MAX
#define INT_MAX
#define FLT_EPSILON
bool all(VecOp< bool, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float length(VecOp< float, D >) RET
const int state
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
BMesh Iterators.
bool BLI_tridiagonal_solve_cyclic(const float *a, const float *b, const float *c, const float *d, float *r_x, int count)
Solve a possibly cyclic tridiagonal system using the Sherman-Morrison formula.
MINLINE float min_ff(float a, float b)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void BKE_curve_nurb_vert_active_set(Curve *cu, const Nurb *nu, const void *vert)
Definition curve.cc:5048
static void bevel_list_smooth(BevList *bl, int smooth_iter)
Definition curve.cc:2148
IDTypeInfo IDType_ID_CU_LEGACY
Definition curve.cc:300
void BKE_curve_editNurb_free(Curve *cu)
Definition curve.cc:378
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
bool is_uniform_scaled_m4(const float m[4][4])
int BKE_curve_nurb_vert_index_get(const Nurb *nu, const void *vert)
Definition curve.cc:5037
MINLINE int mod_i(int i, int n)
Nurb * BKE_curve_nurb_active_get(Curve *cu)
Definition curve.cc:5022
void BKE_curve_eval_geometry(Depsgraph *depsgraph, Curve *curve)
Definition curve.cc:5517
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void BKE_nurb_points_add(Nurb *nu, int number)
Definition curve.cc:886
static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *r_sina, float *r_cosa)
Definition curve.cc:1895
static void bezier_handle_calc_smooth_fcurve(BezTriple *bezt, int total, int start, int count, bool cycle)
Definition curve.cc:3740
static std::optional< Bounds< float3 > > calc_nurblist_bounds(const ListBaseT< Nurb > *nurbs, const bool use_radius)
Definition curve.cc:5117
static void nurb_handles_calc__align_selected(Nurb *nu)
Definition curve.cc:4015
static void curve_blend_read_data(BlendDataReader *reader, ID *id)
Definition curve.cc:205
static void nurbList_handles_swap_select(Nurb *nu)
Definition curve.cc:4001
@ HD_AUTOTYPE_LOCKED_FINAL
static bool bezier_check_solve_end_handle(BezTriple *bezt, char htype, bool end)
Definition curve.cc:3724
void BKE_curve_forward_diff_tangent_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1724
static void calchandleNurb_intern(BezTriple *bezt, const BezTriple *prev, const BezTriple *next, eBezTriple_Flag handle_sel_flag, bool is_fcurve, bool skip_align, char fcurve_smoothing)
Definition curve.cc:3091
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_curve_translate(Curve *cu, const float offset[3], bool do_keys)
Definition curve.cc:5266
const ListBaseT< Nurb > * BKE_curve_nurbs_get_for_read(const Curve *cu)
Definition curve.cc:5001
static bool bevelinside(const BevList *bl1, const BevList *bl2)
Definition curve.cc:1818
void BKE_curve_init(Curve *cu, ObjectType curve_type)
Definition curve.cc:388
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:324
static void bezier_eq_noaccel_left(float *a, float *b, float *c, float *d, const float *dy, const float *l, int i)
Definition curve.cc:3645
void BKE_nurb_bezt_handle_test(BezTriple *bezt, eBezTriple_Flag__Alias sel_flag, const eNurbHandleTest_Mode handle_mode, bool use_around_local)
Definition curve.cc:4083
void BLO_read_pointer_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static void calchandlesNurb_intern(Nurb *nu, eBezTriple_Flag handle_sel_flag, bool skip_align)
Definition curve.cc:3336
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void vec_to_quat(float q[4], const float vec[3], short axis, short upflag)
MINLINE void mul_v2_fl(float r[2], float f)
ListBaseT< Nurb > * BKE_curve_nurbs_get(Curve *cu)
Definition curve.cc:4992
void BKE_curve_type_test(Object *ob, bool dimension_update)
Definition curve.cc:476
float mat4_to_scale(const float mat[4][4])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
static void make_bevel_list_2D(BevList *bl)
Definition curve.cc:2464
void BKE_nurbList_free(ListBaseT< Nurb > *lb)
Definition curve.cc:619
float tri_to_quat(float q[4], const float a[3], const float b[3], const float c[3])
static void calcknots(float *knots, const int pnts, const short order, const short flag)
Definition curve.cc:1137
float normalize_qt(float q[4])
MINLINE float safe_acosf(float a)
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
bool BKE_nurb_order_clamp_u(Nurb *nu)
Definition curve.cc:4828
BezTriple * BKE_nurb_bezt_get_prev(Nurb *nu, BezTriple *bezt)
Definition curve.cc:988
static void make_bevel_list_3D_minimum_twist(BevList *bl)
Definition curve.cc:2231
static void calc_nurb_minmax(const Nurb *nu, bool use_radius, float min[3], float max[3])
Definition curve.cc:733
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
void BKE_curve_transform_ex(Curve *cu, const float mat[4][4], bool do_keys, bool do_props, float unit_scale)
Definition curve.cc:5188
void BKE_nurb_bezt_calc_normal(Nurb *nu, BezTriple *bezt, float r_normal[3])
Definition curve.cc:1032
Curve * BKE_curve_add(Main *bmain, const char *name, ObjectType type)
Definition curve.cc:429
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
void BKE_curve_batch_cache_dirty_tag(Curve *cu, int mode)
Definition curve.cc:5536
MINLINE int max_ii(int a, int b)
void mul_qt_qtqt(float q[4], const float a[4], const float b[4])
ATTR_WARN_UNUSED_RESULT const BMVert * v
eNurbHandleTest_Mode
Definition BKE_curve.hh:63
@ NURB_HANDLE_TEST_KNOT_OR_EACH
Definition BKE_curve.hh:70
@ NURB_HANDLE_TEST_EACH
Definition BKE_curve.hh:65
@ NURB_HANDLE_TEST_KNOT_ONLY
Definition BKE_curve.hh:75
void BKE_nurbList_flag_set(ListBaseT< Nurb > *editnurb, eBezTriple_Flag flag, bool set)
Definition curve.cc:4376
MatBase< float, 4, 4 > float4x4
static void bevel_list_apply_tilt(BevList *bl)
Definition curve.cc:2125
void BKE_curve_material_remap(Curve *cu, const unsigned int *remap, unsigned int remap_len)
Definition curve.cc:5399
void BKE_curve_batch_cache_free(Curve *cu)
Definition curve.cc:5542
void BKE_nurb_project_2d(Nurb *nu)
Definition curve.cc:707
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
void(*) BKE_curve_batch_cache_free_cb(Curve *cu)
Definition curve.cc:5534
void BKE_nurbList_handles_set(ListBaseT< Nurb > *editnurb, eNurbHandleTest_Mode handle_mode, eBezTriple_Handle code)
Definition curve.cc:4228
void BKE_nurb_knot_alloc_u(Nurb *nu)
Definition curve.cc:1209
void MEM_delete_void(void *vmemh)
void BKE_curve_texspace_ensure(Curve *cu)
Definition curve.cc:524
void BKE_nurb_handle_calc(BezTriple *bezt, BezTriple *prev, BezTriple *next, bool is_fcurve, char smoothing)
Definition curve.cc:3975
bool BKE_curve_material_index_validate(Curve *cu)
Definition curve.cc:5367
static void curve_copy_data(Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, const int flag)
Definition curve.cc:80
void BKE_nurbList_handles_autocalc(ListBaseT< Nurb > *editnurb, eBezTriple_Flag flag)
Definition curve.cc:4221
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_SHAPEKEY
void BKE_nurb_index_to_uv(Nurb *nu, int index, int *r_u, int *r_v)
Definition curve.cc:937
void BKE_curve_nurb_vert_active_validate(Curve *cu)
Definition curve.cc:5092
MINLINE void mul_v3_fl(float r[3], float f)
static void * allocate_arrays(int count, float ***floats, char ***chars, const char *name)
Definition curve.cc:3387
void BKE_curve_nurb_active_set(Curve *cu, const Nurb *nu)
Definition curve.cc:5010
float dist_squared_to_line_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:549
unsigned int BKE_curve_calc_coords_axis_len(unsigned int bezt_array_len, unsigned int resolu, bool is_cyclic, bool use_cyclic_duplicate_endpoint)
Definition curve.cc:1629
static void forward_diff_bezier_cotangent(const float p0[3], const float p1[3], const float p2[3], const float p3[3], float p[3], int it, int stride)
Definition curve.cc:1748
bool BKE_vfont_to_curve_ex(Object *ob, const Curve &cu, eEditFontMode mode, ListBaseT< Nurb > *r_nubase, const char32_t **r_text, int *r_text_len, bool *r_text_free, CharTrans **r_chartransdata, float *r_font_size_eval)
void BKE_nurb_knot_calc_v(Nurb *nu)
Definition curve.cc:1219
const ListBaseT< Nurb > * BKE_curve_editNurbs_get_for_read(const Curve *cu)
Definition curve.cc:450
void mul_qt_v3(const float q[4], float r[3])
void BKE_curve_nurbs_key_vert_tilts_apply(ListBaseT< Nurb > *lb, const float *key)
Definition curve.cc:4699
void BKE_nurb_bezierPoints_add(Nurb *nu, int number)
Definition curve.cc:900
static void make_bevel_list_segment_3D(BevList *bl)
Definition curve.cc:2424
void mul_m4_v3(const float M[4][4], float r[3])
void unit_qt(float q[4])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
static void curve_init_data(ID *id)
Definition curve.cc:73
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
static int vergxcobev(const void *a1, const void *a2)
Definition curve.cc:1879
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
Definition curve.cc:2402
void BKE_curve_transform(Curve *cu, const float mat[4][4], bool do_keys, bool do_props)
Definition curve.cc:5260
unsigned char uchar
Definition BKE_text.h:58
void dist_ensure_v3_v3fl(float v1[3], const float v2[3], float dist)
static void tilt_bezpart(const BezTriple *prevbezt, const BezTriple *bezt, const Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
Definition curve.cc:1943
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
void BKE_nurb_handles_calc(Nurb *nu)
Definition curve.cc:3992
static void bevlist_firstlast_direction_calc_from_bpoint(const Nurb *nu, BevList *bl)
Definition curve.cc:2527
bool BKE_nurbList_index_get_co(ListBaseT< Nurb > *nurb, int index, float r_co[3])
Definition curve.cc:533
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void copy_qt_qt(float q[4], const float a[4])
static void make_bevel_list_segment_2D(BevList *bl)
Definition curve.cc:2448
void DEG_id_tag_update(ID *id, unsigned int flags)
MINLINE void swap_v3_v3(float a[3], float b[3])
BMesh const char void * data
void interp_qt_qtqt(float q[4], const float a[4], const float b[4], float t)
void BKE_curve_editNurb_keyIndex_delCV(CVKeyIndexMap *keyindex, const void *cv)
Definition curve.cc:358
void BKE_nurb_makeCurve(const Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
Definition curve.cc:1493
void BKE_nurb_handle_calc_ex(BezTriple *bezt, BezTriple *prev, BezTriple *next, eBezTriple_Flag__Alias handle_sel_flag, bool is_fcurve, char smoothing)
Definition curve.cc:3981
void BKE_curve_nurbs_vert_coords_apply(ListBaseT< Nurb > *lb, const Span< float3 > vert_coords, bool constrain_2d)
Definition curve.cc:4635
unsigned char uint8_t
Definition BKE_text.h:79
bool BKE_nurbList_flag_set_from_flag(ListBaseT< Nurb > *editnurb, eBezTriple_Flag from_flag, eBezTriple_Flag flag)
Definition curve.cc:4411
static void free_arrays(void *buffer)
Definition curve.cc:3421
bool BKE_curve_material_index_used(const Curve *cu, int index)
Definition curve.cc:5331
bool BKE_nurb_valid_message(int pnts, short order, short flag, short type, bool is_surf, int dir, char *message_dst, size_t maxncpy)
Definition curve.cc:4756
static void make_bevel_list_3D_zup(BevList *bl)
Definition curve.cc:2202
static void bezier_output_handle(BezTriple *bezt, bool right, float dy, bool endpoint)
Definition curve.cc:3713
static int cu_isectLL(const float v1[3], const float v2[3], const float v3[3], const float v4[3], short cox, short coy, float *lambda, float *mu, float vec[3])
Definition curve.cc:1772
MINLINE void negate_v3(float r[3])
static void curve_free_data(ID *id)
Definition curve.cc:119
void BKE_curve_correct_bezpart(const float v1[2], float v2[2], float v3[2], const float v4[2])
Definition curve.cc:5464
static void makeknots(Nurb *nu, short uv)
Definition curve.cc:1177
BezTriple * BKE_nurb_bezt_get_next(Nurb *nu, BezTriple *bezt)
Definition curve.cc:946
void BKE_curve_nurbs_vert_coords_apply_with_mat4(ListBaseT< Nurb > *lb, const Span< float3 >, const float4x4 &transform, bool constrain_2d)
Definition curve.cc:4599
void BKE_curve_smooth_flag_set(Curve *cu, bool use_smooth)
Definition curve.cc:5441
Array< float3 > BKE_curve_nurbs_key_vert_coords_alloc(const ListBaseT< Nurb > *lb, const float *key)
Definition curve.cc:4670
static bool tridiagonal_solve_with_limits(float *a, float *b, float *c, float *d, float *h, const float *hmin, const float *hmax, int solve_count)
Definition curve.cc:3465
void BKE_curve_material_index_remove(Curve *cu, int index)
Definition curve.cc:5312
static void bezier_clamp(float *hmax, float *hmin, int i, float dy, bool no_reverse, bool no_overshoot)
Definition curve.cc:3655
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
bool BKE_nurb_check_valid_v(const Nurb *nu)
Definition curve.cc:4808
static void curve_foreach_id(ID *id, LibraryForeachIDData *data)
Definition curve.cc:141
static void bezier_restore_equation(float *a, float *b, float *c, float *d, const float *a0, const float *b0, const float *c0, const float *d0, int i)
Definition curve.cc:3449
MINLINE void zero_v3(float r[3])
int eBezTriple_Flag__Alias
Definition BKE_curve.hh:41
static void basisNurb(float t, short order, int pnts, const float *knots, float *basis, int *start, int *end)
Definition curve.cc:1224
void BKE_curve_nurbs_vert_coords_get(const ListBaseT< Nurb > *lb, MutableSpan< float3 > vert_coords)
Definition curve.cc:4567
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
@ CU_BEVFAC_MAP_SEGMENT
@ CU_BEVFAC_MAP_SPLINE
void BKE_curveprofile_blend_write(struct BlendWriter *writer, const struct CurveProfile *profile)
bool BLO_read_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size, const int elems=1)
Nurb * BKE_nurb_duplicate(const Nurb *nu)
Definition curve.cc:631
void BKE_nurb_handle_smooth_fcurve(BezTriple *bezt, int total, bool cyclic)
Definition curve.cc:3926
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
void BKE_curve_rect_from_textbox(const Curve *cu, const TextBox *tb, rctf *r_rect)
Definition curve.cc:5455
static void curve_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition curve.cc:158
static void bevel_list_calc_bisect(BevList *bl)
Definition curve.cc:2042
int BKE_nurb_index_from_uv(Nurb *nu, int u, int v)
Definition curve.cc:915
void BKE_nurb_handles_autocalc(Nurb *nu, eBezTriple_Flag flag)
Definition curve.cc:4140
void BKE_nurbList_handles_recalculate(ListBaseT< Nurb > *editnurb, bool calc_length, eBezTriple_Flag flag)
Definition curve.cc:4319
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
BPoint * BKE_nurb_bpoint_get_prev(Nurb *nu, BPoint *bp)
Definition curve.cc:1010
Array< float3 > BKE_curve_nurbs_vert_coords_alloc(const ListBaseT< Nurb > *lb)
Definition curve.cc:4592
void BKE_curveprofile_free(struct CurveProfile *profile)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
void BKE_nurbList_duplicate(ListBaseT< Nurb > *lb1, const ListBaseT< Nurb > *lb2)
Definition curve.cc:697
bool BKE_nurb_check_valid_u(const Nurb *nu)
Definition curve.cc:4800
MINLINE bool compare_v3v3(const float v1[3], const float v2[3], float limit) ATTR_WARN_UNUSED_RESULT
T * DEG_get_original(T *id)
bool BKE_curve_center_median(Curve *cu, float cent[3])
Definition curve.cc:5151
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
ATTR_WARN_UNUSED_RESULT const BMVert * v2
static void minimum_twist_between_two_points(BevPoint *bevp_curr, const BevPoint *bevp_prev)
Definition curve.cc:2215
void BKE_curve_editNurb_keyIndex_free(CVKeyIndexMap **keyindex)
Definition curve.cc:366
@ INDEX_ID_CU_LEGACY
Definition DNA_ID.h:1328
bool BKE_curve_nurb_vert_active_get(Curve *cu, Nurb **r_nu, void **r_vert)
Definition curve.cc:5065
void BKE_nurb_handles_test(Nurb *nu, eNurbHandleTest_Mode handle_mode, bool use_around_local)
Definition curve.cc:4119
void BKE_nurb_handle_calc_simple_auto(Nurb *nu, BezTriple *bezt)
Definition curve.cc:4031
MINLINE float max_ff(float a, float b)
void BKE_nurb_bpoint_calc_plane(Nurb *nu, BPoint *bp, float r_plane[3])
Definition curve.cc:1107
void bisect_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float c[3])
void(*) BKE_curve_batch_cache_dirty_tag_cb(Curve *cu, int mode)
Definition curve.cc:5533
static void bezier_eq_noaccel_right(float *a, float *b, float *c, float *d, const float *dy, const float *l, int i)
Definition curve.cc:3636
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
VFont * BKE_vfont_builtin_ensure()
Definition vfont.cc:387
void BKE_nurb_bpoint_calc_normal(Nurb *nu, BPoint *bp, float r_normal[3])
Definition curve.cc:1084
static void bezier_eq_continuous(float *a, float *b, float *c, float *d, const float *dy, const float *l, int i)
Definition curve.cc:3627
static void make_bevel_list_3D_tangent(BevList *bl)
Definition curve.cc:2352
bool BKE_nurb_type_convert(Nurb *nu, eNurbType type, bool use_handles, const char **r_err_msg)
Definition curve.cc:4848
static void bezier_lock_unknown(float *a, float *b, float *c, float *d, int i, float value)
Definition curve.cc:3442
@ CU_TEXSPACE_FLAG_AUTO_EVALUATED
@ CU_TEXSPACE_FLAG_AUTO
short BKE_nurb_bezt_handle_test_calc_flag(const BezTriple *bezt, const eBezTriple_Flag__Alias sel_flag, const eNurbHandleTest_Mode handle_mode)
Definition curve.cc:4050
bool BKE_nurb_check_valid_uv(const Nurb *nu)
Definition curve.cc:4816
ListBaseT< Nurb > * BKE_curve_editNurbs_get(Curve *cu)
Definition curve.cc:441
void BKE_nurb_handle_calc_simple(Nurb *nu, BezTriple *bezt)
Definition curve.cc:4022
BPoint * BKE_nurb_bpoint_get_next(Nurb *nu, BPoint *bp)
Definition curve.cc:967
size_t BLI_strlen_utf8_ex(const char *strc, size_t *r_len_bytes) ATTR_NONNULL(1
struct CurveProfile * BKE_curveprofile_copy(const struct CurveProfile *profile)
void key_curve_position_weights(float t, float data[4], KeyInterpolationType type)
Definition key.cc:310
std::optional< Bounds< float3 > > BKE_curve_minmax(const Curve *cu, bool use_radius)
Definition curve.cc:5131
static bool is_free_auto_point(BezTriple *bezt)
Definition curve.cc:3921
VecBase< float, 3 > float3
void * BKE_curve_vert_active_get(Curve *cu)
Definition curve.cc:5028
void BKE_curve_bevelList_make(Object *ob, const ListBaseT< Nurb > *nurbs, bool for_render)
Definition curve.cc:2562
void BKE_nurb_knot_calc_u(Nurb *nu)
Definition curve.cc:1214
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void bezier_output_handle_inner(BezTriple *bezt, bool right, const float newval[3], bool endpoint)
Definition curve.cc:3680
void BKE_curveprofile_blend_read(struct BlendDataReader *reader, struct CurveProfile *profile)
void BKE_nurb_bezt_calc_plane(Nurb *nu, BezTriple *bezt, float r_plane[3])
Definition curve.cc:1047
Nurb * BKE_nurb_copy(Nurb *src, int pntsu, int pntsv)
Definition curve.cc:671
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1696
MINLINE int min_ii(int a, int b)
MINLINE int clamp_i(int value, int min, int max)
void BKE_curve_calc_coords_axis(const BezTriple *bezt_array, unsigned int bezt_array_len, unsigned int resolu, bool is_cyclic, bool use_cyclic_duplicate_endpoint, unsigned int axis, unsigned int stride, float *r_points)
Definition curve.cc:1639
void BKE_curve_texspace_calc(Curve *cu)
Definition curve.cc:490
void id_us_min(ID *id)
Definition lib_id.cc:441
void BKE_curve_material_index_clear(Curve *cu)
Definition curve.cc:5352
NURBSValidationStatus
Definition curve.cc:65
void BKE_nurb_makeFaces(const Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
Definition curve.cc:1300
float BKE_nurb_calc_length(const Nurb *nu, int resolution)
Definition curve.cc:785
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1545
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, std::optional< const ID * > new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:749
static float bezier_calc_handle_adj(float hsize[2], float dx)
Definition curve.cc:3730
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
Map< const void *, struct CVKeyIndex * > CVKeyIndexMap
int BKE_nurbList_verts_count(const ListBaseT< Nurb > *nurb)
Definition curve.cc:559
void BKE_curve_editfont_free(Curve *cu)
Definition curve.cc:332
int BKE_nurbList_verts_count_without_handles(const ListBaseT< Nurb > *nurb)
Definition curve.cc:575
static NURBSValidationStatus nurb_check_valid(const int pnts, const short order, const short flag, const short type, const bool is_surf, int *r_points_needed)
Definition curve.cc:4723
void BKE_curve_bevelList_free(ListBaseT< BevList > *bev)
Definition curve.cc:2544
static void bevel_list_flip_tangents(BevList *bl)
Definition curve.cc:2104
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BKE_nurb_order_clamp_v(Nurb *nu)
Definition curve.cc:4838
void BKE_nurb_direction_switch(Nurb *nu)
Definition curve.cc:4444
void BKE_nurb_free(Nurb *nu)
Definition curve.cc:593
static float bezier_relax_direction(const float *a, const float *b, const float *c, const float *d, const float *h, int i, int count)
Definition curve.cc:3427
void BKE_curve_dimension_update(Curve *cu)
Definition curve.cc:459
static void curve_editNurb_keyIndex_cv_free_cb(CVKeyIndex *index)
Definition curve.cc:352
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
const btScalar eps
Definition poly34.cpp:11
#define fabsf
#define sqrtf
#define sinf
#define cosf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
eBezTriple_Flag f2
eBezTriple_Handle h2
eBezTriple_Flag f3
eBezTriple_Flag f1
eBezTriple_Auto_Type auto_handle_type
eBezTriple_Handle h1
void write_pointer_array(int64_t num, const void *data)
void write_string(const char *data)
void write_float_array(int64_t num, const float *data)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
ListBaseT< BevList > bev
Definition BKE_curve.hh:45
draw::CurveBatchCache * batch_cache
struct VFont * vfonti
eCurveTwistMode twist_mode
struct VFont * vfont
ListBaseT< Nurb > nurb
EditNurb * editnurb
struct TextBox * tb
float texspace_location[3]
eCurveBevfacMapping bevfac2_mapping
struct EditFont * editfont
CurveFillSolverType fill_solver
struct Object * textoncurve
struct CurveProfile * bevel_profile
struct Object * bevobj
struct Material ** mat
struct Object * taperobj
CurveFillRuleType fill_rule
eCurveBevfacMapping bevfac1_mapping
struct CharInfo * strinfo
struct Key * key
struct VFont * vfontb
struct VFont * vfontbi
CharInfo * textbufinfo
Definition BKE_vfont.hh:48
EditFontSelBox * selboxes
Definition BKE_vfont.hh:55
char32_t * textbuf
Definition BKE_vfont.hh:46
CVKeyIndexMap * keyindex
ListBaseT< Nurb > nurbs
char name[258]
Definition DNA_ID.h:433
ListBaseT< KeyBlock > block
BezTriple * bezt
eNurbKnotFlag flagu
eNurbKnotFlag flagv
bke::ObjectRuntime * runtime
#define N_(msgid)