Blender V5.3
curveprofile.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "MEM_guardedalloc.h"
12
13#include "DNA_curve_types.h"
15
16#include "BLI_math_geom_c.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_rect.hh"
19#include "BLI_utildefines.hh"
20
21#include "BKE_curve.hh"
22#include "BKE_curveprofile.h"
23
24#include "BLO_read_write.hh"
25
26namespace blender {
27
29#define PROF_TABLE_MAX 512
30
31/* -------------------------------------------------------------------- */
34
36{
37 CurveProfile *profile = MEM_new<CurveProfile>(__func__);
38
40 profile->preset = preset;
42 BKE_curveprofile_update(profile, 0);
43
44 return profile;
45}
46
48{
49 MEM_SAFE_DELETE(profile->path);
50 MEM_SAFE_DELETE(profile->table);
51 MEM_SAFE_DELETE(profile->segments);
52}
53
55{
56 if (profile) {
58 MEM_delete(profile);
59 }
60}
61
63{
64 *target = *profile;
65
66 target->path = MEM_dupalloc(profile->path);
67 target->table = MEM_dupalloc(profile->table);
68 target->segments = MEM_dupalloc(profile->segments);
69
70 /* Update the reference the points have to the profile. */
71 for (int i = 0; i < target->path_len; i++) {
72 target->path[i].profile = target;
73 }
74}
75
77{
78 if (profile) {
79 CurveProfile *new_prdgt = MEM_dupalloc(profile);
80 BKE_curveprofile_copy_data(new_prdgt, profile);
81 return new_prdgt;
82 }
83 return nullptr;
84}
85
87{
88 writer->write_struct(profile, [&](BlendStructWriter<CurveProfile> &struct_writer) {
89 if (!writer->is_undo()) {
90 struct_writer.shallow_data.changed_timestamp = 0;
91 }
92 });
93 writer->write_struct_array(profile->path_len, profile->path);
94}
95
97{
98 BLO_read_array_and_validate_size(reader, &profile->path, &profile->path_len);
99 profile->table = nullptr;
100 profile->segments = nullptr;
101
102 /* Reset the points' pointers to the profile. */
103 for (int i = 0; i < profile->path_len; i++) {
104 profile->path[i].profile = profile;
105 }
106
107 BKE_curveprofile_init(profile, profile->segments_len);
108}
109
111
112/* -------------------------------------------------------------------- */
115
117 const bool handle_1,
118 const bool snap,
119 const float delta[2])
120{
121 short handle_type = (handle_1) ? point->h1 : point->h2;
122 float *handle_location = (handle_1) ? &point->h1_loc[0] : &point->h2_loc[0];
123
124 float start_position[2];
125 copy_v2_v2(start_position, handle_location);
126
127 /* Don't move the handle if it's not a free handle type. */
128 if (!ELEM(handle_type, HD_FREE, HD_ALIGN)) {
129 return false;
130 }
131
132 /* Move the handle. */
133 handle_location[0] += delta ? delta[0] : 0.0f;
134 handle_location[1] += delta ? delta[1] : 0.0f;
135 if (snap) {
136 handle_location[0] = 0.125f * roundf(8.0f * handle_location[0]);
137 handle_location[1] = 0.125f * roundf(8.0f * handle_location[1]);
138 }
139
140 /* Move the other handle if they are aligned. */
141 if (handle_type == HD_ALIGN) {
142 short other_handle_type = (handle_1) ? point->h2 : point->h1;
143 if (other_handle_type == HD_ALIGN) {
144 float *other_handle_location = (handle_1) ? &point->h2_loc[0] : &point->h1_loc[0];
145 other_handle_location[0] = 2.0f * point->x - handle_location[0];
146 other_handle_location[1] = 2.0f * point->y - handle_location[1];
147 }
148 }
149
150 if (!equals_v2v2(handle_location, start_position)) {
151 return true;
152 }
153 return false;
154}
155
157 CurveProfilePoint *point,
158 const bool snap,
159 const float delta[2])
160{
161 /* Don't move the final point. */
162 if (point == &profile->path[profile->path_len - 1]) {
163 return false;
164 }
165 /* Don't move the first point. */
166 if (point == profile->path) {
167 return false;
168 }
169 float origx = point->x;
170 float origy = point->y;
171
172 point->x += delta[0];
173 point->y += delta[1];
174 if (snap) {
175 point->x = 0.125f * roundf(8.0f * point->x);
176 point->y = 0.125f * roundf(8.0f * point->y);
177 }
178
179 /* Clip here instead to test clipping here to stop handles from moving too. */
180 if (profile->flag & PROF_USE_CLIP) {
181 point->x = max_ff(point->x, profile->clip_rect.xmin);
182 point->x = min_ff(point->x, profile->clip_rect.xmax);
183 point->y = max_ff(point->y, profile->clip_rect.ymin);
184 point->y = min_ff(point->y, profile->clip_rect.ymax);
185 }
186
187 /* Also move free handles even when they aren't selected. */
188 if (ELEM(point->h1, HD_FREE, HD_ALIGN)) {
189 point->h1_loc[0] += point->x - origx;
190 point->h1_loc[1] += point->y - origy;
191 }
192 if (ELEM(point->h2, HD_FREE, HD_ALIGN)) {
193 point->h2_loc[0] += point->x - origx;
194 point->h2_loc[1] += point->y - origy;
195 }
196
197 if (point->x != origx || point->y != origy) {
198 return true;
199 }
200 return false;
201}
202
204{
205 for (int i = 0; i < profile->path_len; i++) {
206 CurveProfilePoint *pt = &profile->path[i];
207 float delta[2] = {offset.x, offset.y};
208
209 /* The main point is selected or all handles are aligned and selected. */
210 if ((pt->flag & PROF_SELECT) || ((pt->flag & PROF_H1_SELECT && pt->h1 == HD_ALIGN) &&
211 (pt->flag & PROF_H2_SELECT && pt->h2 == HD_ALIGN)))
212 {
213 BKE_curveprofile_move_point(profile, pt, false, delta);
214 }
215 else {
216 /* Otherwise, move only handles that are selected. */
217 if (pt->flag & PROF_H1_SELECT) {
218 BKE_curveprofile_move_handle(pt, true, false, delta);
219 }
220 if (pt->flag & PROF_H2_SELECT) {
221 BKE_curveprofile_move_handle(pt, false, false, delta);
222 }
223 }
224 }
225}
226
228{
229 /* Must have 2 points minimum. */
230 if (profile->path_len <= 2) {
231 return false;
232 }
233
234 /* Input point must be within the array. */
235 if (!(point > profile->path && point < profile->path + profile->path_len)) {
236 return false;
237 }
238
239 CurveProfilePoint *new_path = MEM_new_array<CurveProfilePoint>(size_t(profile->path_len),
240 __func__);
241
242 int i_delete = int(point - profile->path);
243 BLI_assert(i_delete > 0);
244
245 /* Copy the before and after the deleted point. */
246 memcpy(new_path, profile->path, sizeof(CurveProfilePoint) * i_delete);
247 memcpy(new_path + i_delete,
248 profile->path + i_delete + 1,
249 sizeof(CurveProfilePoint) * (profile->path_len - i_delete - 1));
250
251 MEM_delete(profile->path);
252 profile->path = new_path;
253 profile->path_len -= 1;
254 return true;
255}
256
257void BKE_curveprofile_remove_by_flag(CurveProfile *profile, const short flag)
258{
259 /* Copy every point without the flag into the new path. */
260 CurveProfilePoint *new_path = MEM_new_array<CurveProfilePoint>(size_t(profile->path_len),
261 __func__);
262
263 /* Build the new list without any of the points with the flag. Keep the first and last points. */
264 int i_new = 1;
265 int i_old = 1;
266 int n_removed = 0;
267 new_path[0] = profile->path[0];
268 for (; i_old < profile->path_len - 1; i_old++) {
269 if (!(profile->path[i_old].flag & flag)) {
270 new_path[i_new] = profile->path[i_old];
271 i_new++;
272 }
273 else {
274 n_removed++;
275 }
276 }
277 new_path[i_new] = profile->path[i_old];
278
279 MEM_delete(profile->path);
280 profile->path = new_path;
281 profile->path_len -= n_removed;
282}
283
287static void point_init(
288 CurveProfilePoint *point, float x, float y, eCurveProfilePoint_Flag flag, char h1, char h2)
289{
290 point->x = x;
291 point->y = y;
292 point->flag = flag;
293 point->h1 = h1;
294 point->h2 = h2;
295}
296
298{
299 const float new_loc[2] = {x, y};
300
301 /* Don't add more control points than the maximum size of the higher resolution table. */
302 if (profile->path_len == PROF_TABLE_MAX - 1) {
303 return nullptr;
304 }
305
306 /* Find the index at the line segment that's closest to the new position. */
307 float min_distance = FLT_MAX;
308 int i_insert = 0;
309 for (int i = 0; i < profile->path_len - 1; i++) {
310 const float loc1[2] = {profile->path[i].x, profile->path[i].y};
311 const float loc2[2] = {profile->path[i + 1].x, profile->path[i + 1].y};
312
313 float distance = dist_squared_to_line_segment_v2(new_loc, loc1, loc2);
314 if (distance < min_distance) {
315 min_distance = distance;
316 i_insert = i + 1;
317 }
318 }
319
320 /* Insert the new point at the location we found and copy all of the old points in as well. */
321 profile->path_len++;
322 CurveProfilePoint *new_path = MEM_new_array<CurveProfilePoint>(size_t(profile->path_len),
323 __func__);
324 CurveProfilePoint *new_pt = nullptr;
325 for (int i_new = 0, i_old = 0; i_new < profile->path_len; i_new++) {
326 if (i_new != i_insert) {
327 /* Insert old points. */
328 new_path[i_new] = profile->path[i_old];
329 new_path[i_new].flag &= ~(PROF_SELECT | PROF_H1_SELECT | PROF_H2_SELECT | PROF_ACTIVE |
330 PROF_H1_ACTIVE | PROF_H2_ACTIVE); /* Deselect old points. */
331 i_old++;
332 }
333 else {
334 /* Insert new point. */
335 /* Set handles of new point based on its neighbors. */
336 const char new_handle_type = (new_path[i_new - 1].h2 == HD_VECT &&
337 profile->path[i_insert].h1 == HD_VECT) ?
338 HD_VECT :
339 HD_AUTO;
340 point_init(&new_path[i_new], x, y, PROF_SELECT, new_handle_type, new_handle_type);
341 new_pt = &new_path[i_new];
342 /* Give new point a reference to the profile. */
343 new_pt->profile = profile;
344 }
345 }
346
347 /* Free the old path and use the new one. */
348 MEM_delete(profile->path);
349 profile->path = new_path;
350 return new_pt;
351}
352
353void BKE_curveprofile_selected_handle_set(CurveProfile *profile, int type_1, int type_2)
354{
355 for (int i = 0; i < profile->path_len; i++) {
356 CurveProfilePoint *point = &profile->path[i];
357 if (point->flag & (PROF_SELECT | PROF_H1_SELECT | PROF_H2_SELECT)) {
358 point->h1 = type_1;
359 point->h2 = type_2;
360
361 if (type_1 == HD_ALIGN && type_2 == HD_ALIGN) {
362 /* Align the handles. */
363 BKE_curveprofile_move_handle(point, true, false, nullptr);
364 }
365
366 if (ELEM(type_1, HD_AUTO, HD_VECT)) {
367 /* Auto and Vector handles can't be selected, so pass their selection on to the point. */
368 if (point->flag & (PROF_H1_ACTIVE | PROF_H2_ACTIVE)) {
369 point->flag |= PROF_ACTIVE;
370 }
371 point->flag |= PROF_SELECT;
373 }
374 }
375 }
376}
377
379{
380 CurveProfilePoint new_point = *point;
381 point_init(&new_point, point->y, point->x, point->flag, point->h2, point->h1);
382 return new_point;
383}
384
386{
387 /* When there are only two points, reversing shouldn't do anything. */
388 if (profile->path_len == 2) {
389 return;
390 }
391 CurveProfilePoint *new_path = MEM_new_array<CurveProfilePoint>(size_t(profile->path_len),
392 __func__);
393 /* Mirror the new points across the y = x line */
394 for (int i = 0; i < profile->path_len; i++) {
395 int i_reversed = profile->path_len - i - 1;
396 BLI_assert(i_reversed >= 0);
397 new_path[i_reversed] = mirror_point(&profile->path[i]);
398 new_path[i_reversed].profile = profile;
399
400 /* Mirror free handles, they can't be recalculated. */
401 if (ELEM(profile->path[i].h1, HD_FREE, HD_ALIGN)) {
402 new_path[i_reversed].h1_loc[0] = profile->path[i].h2_loc[1];
403 new_path[i_reversed].h1_loc[1] = profile->path[i].h2_loc[0];
404 }
405 if (ELEM(profile->path[i].h2, HD_FREE, HD_ALIGN)) {
406 new_path[i_reversed].h2_loc[0] = profile->path[i].h1_loc[1];
407 new_path[i_reversed].h2_loc[1] = profile->path[i].h1_loc[0];
408 }
409 }
410
411 /* Free the old points and use the new ones */
412 MEM_delete(profile->path);
413 profile->path = new_path;
414}
415
420{
421 int n = profile->path_len;
422
423 point_init(&profile->path[0], 1.0f, 0.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
424 point_init(&profile->path[1], 1.0f, 0.5f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
425 for (int i = 1; i < n - 2; i++) {
426 const float x = 1.0f - (0.5f * (1.0f - cosf(float(i / float(n - 3)) * M_PI_2)));
427 const float y = 0.5f + 0.5f * sinf(float((i / float(n - 3)) * M_PI_2));
429 }
430 point_init(&profile->path[n - 2], 0.5f, 1.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
431 point_init(&profile->path[n - 1], 0.0f, 1.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
432}
433
439{
440 int n = profile->path_len;
441
442 /* Special case for two points to avoid dividing by zero later. */
443 if (n == 2) {
444 point_init(&profile->path[0], 1.0f, 0.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
445 point_init(&profile->path[0], 0.0f, 1.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
446 return;
447 }
448
449 float n_steps_x = (n % 2 == 0) ? n : (n - 1);
450 float n_steps_y = (n % 2 == 0) ? (n - 2) : (n - 1);
451
452 for (int i = 0; i < n; i++) {
453 int step_x = (i + 1) / 2;
454 int step_y = i / 2;
455 const float x = 1.0f - (float(2 * step_x) / n_steps_x);
456 const float y = float(2 * step_y) / n_steps_y;
458 }
459}
460
462{
463 profile->view_rect = profile->clip_rect;
464}
465
467{
468 MEM_SAFE_DELETE(profile->path);
469
470 eCurveProfilePresets preset = static_cast<eCurveProfilePresets>(profile->preset);
471 switch (preset) {
472 case PROF_PRESET_LINE:
473 profile->path_len = 2;
474 break;
476 /* Use a dynamic number of control points for the widget's profile. */
477 if (profile->segments_len < 4) {
478 /* But always use enough points to at least build the support points. */
479 profile->path_len = 5;
480 }
481 else {
482 profile->path_len = profile->segments_len + 1;
483 }
484 break;
486 profile->path_len = 13;
487 break;
489 profile->path_len = 11;
490 break;
492 /* Also use dynamic number of control points based on the set number of segments. */
493 if (profile->segments_len == 0) {
494 /* totsegments hasn't been set-- use the number of control points for 8 steps. */
495 profile->path_len = 17;
496 }
497 else {
498 profile->path_len = profile->segments_len + 1;
499 }
500 break;
501 }
502
503 profile->path = MEM_new_array<CurveProfilePoint>(profile->path_len, __func__);
504
505 switch (preset) {
506 case PROF_PRESET_LINE:
507 point_init(&profile->path[0], 1.0f, 0.0f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
508 point_init(&profile->path[1], 0.0f, 1.0f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
509 break;
512 break;
514 point_init(&profile->path[0], 1.0f, 0.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
515 point_init(&profile->path[1], 1.0f, 0.125f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
516 point_init(&profile->path[2], 0.92f, 0.16f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
517 point_init(&profile->path[3], 0.875f, 0.25f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
518 point_init(&profile->path[4], 0.8f, 0.25f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
519 point_init(&profile->path[5], 0.733f, 0.433f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
520 point_init(&profile->path[6], 0.582f, 0.522f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
521 point_init(&profile->path[7], 0.4f, 0.6f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
522 point_init(&profile->path[8], 0.289f, 0.727f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
523 point_init(&profile->path[9], 0.25f, 0.925f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
524 point_init(&profile->path[10], 0.175f, 0.925f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
525 point_init(&profile->path[11], 0.175f, 1.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
526 point_init(&profile->path[12], 0.0f, 1.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
527 break;
529 point_init(&profile->path[0], 1.0f, 0.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
530 point_init(&profile->path[1], 1.0f, 0.25f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
531 point_init(&profile->path[2], 0.75f, 0.25f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
532 point_init(&profile->path[3], 0.75f, 0.325f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
533 point_init(&profile->path[4], 0.925f, 0.4f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
534 point_init(&profile->path[5], 0.975f, 0.5f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
535 point_init(&profile->path[6], 0.94f, 0.65f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
536 point_init(&profile->path[7], 0.85f, 0.75f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
537 point_init(&profile->path[8], 0.75f, 0.875f, eCurveProfilePoint_Flag{}, HD_AUTO, HD_AUTO);
538 point_init(&profile->path[9], 0.7f, 1.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
539 point_init(&profile->path[10], 0.0f, 1.0f, eCurveProfilePoint_Flag{}, HD_VECT, HD_VECT);
540 break;
543 break;
544 }
545
546 profile->flag &= ~PROF_DIRTY_PRESET;
547
548 /* Ensure each point has a reference to the profile. */
549 for (int i = 0; i < profile->path_len; i++) {
550 profile->path[i].profile = profile;
551 }
552
553 MEM_SAFE_DELETE(profile->table);
554 profile->table = nullptr;
555}
556
558{
559 CurveProfilePoint *pts = profile->path;
560 for (int i = 1;; i++) {
561 int k = (i + 1) / 2;
562 int idx = (i & 1) ? (i_last - k) : (i_last + k);
563
564 if (idx < 0 || idx >= profile->path_len) {
565 if (i_last - k < 0 && i_last + k >= profile->path_len) {
566 return;
567 }
568 continue;
569 }
570
571 if (pts[idx].flag & PROF_SELECT) {
572 pts[idx].flag |= PROF_ACTIVE;
573 return;
574 }
575 if (pts[idx].flag & PROF_H1_SELECT) {
576 pts[idx].flag |= PROF_H1_ACTIVE;
577 return;
578 }
579 if (pts[idx].flag & PROF_H2_SELECT) {
580 pts[idx].flag |= PROF_H2_ACTIVE;
581 return;
582 }
583 }
584}
585
587
588/* -------------------------------------------------------------------- */
591
593{
595 const int resolution = 16;
596
597 /* Make sure there is always one sample, even if there are no control points. */
598 return std::clamp((profile->path_len - 1) * resolution + 1, 1, PROF_TABLE_MAX);
599}
600
605static bool is_curved_edge(CurveProfilePoint *path, int i)
606{
607 return (path[i].h2 != HD_VECT || path[i + 1].h1 != HD_VECT);
608}
609
615 const CurveProfilePoint *prev,
616 const CurveProfilePoint *next)
617{
618 if (point->h1 == HD_FREE && point->h2 == HD_FREE) {
619 return;
620 }
621
622 float *point_loc = &point->x;
623
624 float pt[2];
625 const float *prev_loc, *next_loc;
626 if (prev == nullptr) {
627 next_loc = &next->x;
628 pt[0] = 2.0f * point_loc[0] - next_loc[0];
629 pt[1] = 2.0f * point_loc[1] - next_loc[1];
630 prev_loc = pt;
631 }
632 else {
633 prev_loc = &prev->x;
634 }
635
636 if (next == nullptr) {
637 prev_loc = &prev->x;
638 pt[0] = 2.0f * point_loc[0] - prev_loc[0];
639 pt[1] = 2.0f * point_loc[1] - prev_loc[1];
640 next_loc = pt;
641 }
642 else {
643 next_loc = &next->x;
644 }
645
646 float dvec_a[2], dvec_b[2];
647 sub_v2_v2v2(dvec_a, point_loc, prev_loc);
648 sub_v2_v2v2(dvec_b, next_loc, point_loc);
649
650 float len_a = len_v2(dvec_a);
651 float len_b = len_v2(dvec_b);
652 if (len_a == 0.0f) {
653 len_a = 1.0f;
654 }
655 if (len_b == 0.0f) {
656 len_b = 1.0f;
657 }
658
659 if (point->h1 == HD_AUTO || point->h2 == HD_AUTO) {
660 float tvec[2];
661 tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
662 tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
663
664 float len = len_v2(tvec) * 2.5614f;
665 if (len != 0.0f) {
666 if (point->h1 == HD_AUTO) {
667 len_a /= len;
668 madd_v2_v2v2fl(point->h1_loc, point_loc, tvec, -len_a);
669 }
670 if (point->h2 == HD_AUTO) {
671 len_b /= len;
672 madd_v2_v2v2fl(point->h2_loc, point_loc, tvec, len_b);
673 }
674 }
675 }
676
677 if (point->h1 == HD_VECT) {
678 madd_v2_v2v2fl(point->h1_loc, point_loc, dvec_a, -1.0f / 3.0f);
679 }
680 if (point->h2 == HD_VECT) {
681 madd_v2_v2v2fl(point->h2_loc, point_loc, dvec_b, 1.0f / 3.0f);
682 }
683}
684
685static void calculate_path_handles(CurveProfilePoint *path, int path_len)
686{
687 point_calculate_handle(&path[0], nullptr, &path[1]);
688 for (int i = 1; i < path_len - 1; i++) {
689 point_calculate_handle(&path[i], &path[i - 1], &path[i + 1]);
690 }
691 point_calculate_handle(&path[path_len - 1], &path[path_len - 2], nullptr);
692}
693
700static float bezt_edge_handle_angle(const CurveProfilePoint *path, int i_edge)
701{
702 /* Find the direction of the handles that define this edge along the direction of the path. */
703 float start_handle_direction[2], end_handle_direction[2];
704 /* Handle 2 - point location. */
705 sub_v2_v2v2(start_handle_direction, path[i_edge].h2_loc, &path[i_edge].x);
706 /* Point location - handle 1. */
707 sub_v2_v2v2(end_handle_direction, &path[i_edge + 1].x, path[i_edge + 1].h1_loc);
708
709 return angle_v2v2(start_handle_direction, end_handle_direction);
710}
711
719
723static int sort_points_curvature(const void *in_a, const void *in_b)
724{
725 const CurvatureSortPoint *a = static_cast<const CurvatureSortPoint *>(in_a);
726 const CurvatureSortPoint *b = static_cast<const CurvatureSortPoint *>(in_b);
727
728 if (a->point_curvature > b->point_curvature) {
729 return 0;
730 }
731
732 return 1;
733}
734
748static void create_samples(CurveProfile *profile,
749 int n_segments,
750 bool sample_straight_edges,
751 CurveProfilePoint *r_samples)
752{
753 CurveProfilePoint *path = profile->path;
754 int totpoints = profile->path_len;
755 BLI_assert(n_segments > 0);
756
757 int totedges = totpoints - 1;
758
759 calculate_path_handles(path, totpoints);
760
761 /* Create a list of edge indices with the most curved at the start, least curved at the end. */
762 CurvatureSortPoint *curve_sorted = MEM_new_array_zeroed<CurvatureSortPoint>(totedges, __func__);
763 for (int i = 0; i < totedges; i++) {
764 curve_sorted[i].point_index = i;
765 /* Calculate the curvature of each edge once for use when sorting for curvature. */
766 curve_sorted[i].point_curvature = bezt_edge_handle_angle(path, i);
767 }
768 qsort(curve_sorted, totedges, sizeof(CurvatureSortPoint), sort_points_curvature);
769
770 /* Assign the number of sampled points for each edge. */
771 int16_t *n_samples = MEM_new_array_zeroed<int16_t>(totedges, "samples numbers");
772 int n_added = 0;
773 int n_left;
774 if (n_segments >= totedges) {
775 if (sample_straight_edges) {
776 /* Assign an even number to each edge if it's possible, then add the remainder of sampled
777 * points starting with the most curved edges. */
778 int n_common = n_segments / totedges;
779 n_left = n_segments % totedges;
780
781 /* Assign the points that fill fit evenly to the edges. */
782 if (n_common > 0) {
783 BLI_assert(n_common < INT16_MAX);
784 for (int i = 0; i < totedges; i++) {
785 n_samples[i] = n_common;
786 n_added += n_common;
787 }
788 }
789 }
790 else {
791 /* Count the number of curved edges */
792 int n_curved_edges = 0;
793 for (int i = 0; i < totedges; i++) {
794 if (is_curved_edge(path, i)) {
795 n_curved_edges++;
796 }
797 }
798 /* Just sample all of the edges if there are no curved edges. */
799 n_curved_edges = (n_curved_edges == 0) ? totedges : n_curved_edges;
800
801 /* Give all of the curved edges the same number of points and straight edges one point. */
802 n_left = n_segments - (totedges - n_curved_edges); /* Left after 1 for each straight edge. */
803 int n_common = n_left / n_curved_edges; /* Number assigned to all curved edges */
804 if (n_common > 0) {
805 for (int i = 0; i < totedges; i++) {
806 /* Add the common number if it's a curved edge or if edges are curved. */
807 if (is_curved_edge(path, i) || n_curved_edges == totedges) {
808 BLI_assert(n_common + n_samples[i] < INT16_MAX);
809 n_samples[i] += n_common;
810 n_added += n_common;
811 }
812 else {
813 n_samples[i] = 1;
814 n_added++;
815 }
816 }
817 }
818 n_left -= n_common * n_curved_edges;
819 }
820 }
821 else {
822 /* Not enough segments to give one to each edge, so just give them to the most curved edges. */
823 n_left = n_segments;
824 }
825 /* Assign the remainder of the points that couldn't be spread out evenly. */
826 BLI_assert(n_left < totedges);
827 for (int i = 0; i < n_left; i++) {
828 BLI_assert(n_samples[curve_sorted[i].point_index] < INT16_MAX);
829 n_samples[curve_sorted[i].point_index]++;
830 n_added++;
831 }
832
833 BLI_assert(n_added == n_segments); /* n_added is just used for this assert, could remove it. */
834 UNUSED_VARS_NDEBUG(n_added);
835
836 /* Sample the points and add them to the locations table. */
837 for (int i_sample = 0, i = 0; i < totedges; i++) {
838 if (n_samples[i] > 0) {
839 /* Carry over the handle types from the control point to its first corresponding sample. */
840 r_samples[i_sample].h1 = path[i].h1;
841 r_samples[i_sample].h2 = path[i].h2;
842 /* All extra sample points for this control point get "auto" handles. */
843 for (int j = i_sample + 1; j < i_sample + n_samples[i]; j++) {
844 r_samples[j].flag = eCurveProfilePoint_Flag{};
845 r_samples[j].h1 = HD_AUTO;
846 r_samples[j].h2 = HD_AUTO;
847 BLI_assert(j < n_segments);
848 }
849
850 /* Sample from the bezier points. X then Y values. */
852 path[i].h2_loc[0],
853 path[i + 1].h1_loc[0],
854 path[i + 1].x,
855 &r_samples[i_sample].x,
856 n_samples[i],
857 sizeof(CurveProfilePoint));
859 path[i].h2_loc[1],
860 path[i + 1].h1_loc[1],
861 path[i + 1].y,
862 &r_samples[i_sample].y,
863 n_samples[i],
864 sizeof(CurveProfilePoint));
865 }
866 i_sample += n_samples[i]; /* Add the next set of points after the ones we just added. */
867 BLI_assert(i_sample <= n_segments);
868 }
869
870 MEM_delete(curve_sorted);
871 MEM_delete(n_samples);
872}
873
875{
876 profile->flag = PROF_USE_CLIP;
877
878 BLI_rctf_init(&profile->view_rect, 0.0f, 1.0f, 0.0f, 1.0f);
879 profile->clip_rect = profile->view_rect;
880
881 profile->path_len = 2;
882 profile->path = MEM_new_array<CurveProfilePoint>(2, __func__);
883
884 profile->path[0].x = 1.0f;
885 profile->path[0].y = 0.0f;
886 profile->path[0].profile = profile;
887 profile->path[1].x = 1.0f;
888 profile->path[1].y = 1.0f;
889 profile->path[1].profile = profile;
890
891 profile->changed_timestamp = 0;
892}
893
894void BKE_curveprofile_init(CurveProfile *profile, short segments_len)
895{
896 if (segments_len != profile->segments_len) {
897 profile->flag |= PROF_DIRTY_PRESET;
898 }
899 profile->segments_len = segments_len;
900
901 /* Calculate the higher resolution / segments tables for display and evaluation. */
903}
904
912{
914
915 return len_v2v2(&profile->table[i].x, &profile->table[i + 1].x);
916}
917
923static float curveprofile_total_length(const CurveProfile *profile)
924{
925 float total_length = 0;
926 for (int i = 0; i < BKE_curveprofile_table_size(profile) - 1; i++) {
927 total_length += len_v2v2(&profile->table[i].x, &profile->table[i + 1].x);
928 }
929 return total_length;
930}
931
941 int n_segments,
942 CurveProfilePoint *r_samples)
943{
944 const float total_length = curveprofile_total_length(profile);
945 const float segment_length = total_length / n_segments;
946 float distance_to_next_table_point = curveprofile_distance_to_next_table_point(profile, 0);
947 float distance_to_previous_table_point = 0.0f;
948 int i_table = 0;
949
950 /* Set the location for the first point. */
951 r_samples[0].x = profile->table[0].x;
952 r_samples[0].y = profile->table[0].y;
953
954 /* Travel along the path, recording the locations of segments as we pass them. */
955 float segment_left = segment_length;
956 for (int i = 1; i < n_segments; i++) {
957 /* Travel over all of the points that fit inside this segment. */
958 while (distance_to_next_table_point < segment_left) {
959 segment_left -= distance_to_next_table_point;
960 i_table++;
961 distance_to_next_table_point = curveprofile_distance_to_next_table_point(profile, i_table);
962 distance_to_previous_table_point = 0.0f;
963 }
964 /* We're at the last table point that fits inside the current segment, use interpolation. */
965 float factor = (distance_to_previous_table_point + segment_left) /
966 (distance_to_previous_table_point + distance_to_next_table_point);
967 r_samples[i].x = interpf(profile->table[i_table + 1].x, profile->table[i_table].x, factor);
968 r_samples[i].y = interpf(profile->table[i_table + 1].y, profile->table[i_table].y, factor);
969 BLI_assert(factor <= 1.0f && factor >= 0.0f);
970#ifdef DEBUG_CURVEPROFILE_EVALUATE
971 printf("segment_left: %.3f\n", segment_left);
972 printf("i_table: %d\n", i_table);
973 printf("distance_to_previous_table_point: %.3f\n", distance_to_previous_table_point);
974 printf("distance_to_next_table_point: %.3f\n", distance_to_next_table_point);
975 printf("Interpolating with factor %.3f from (%.3f, %.3f) to (%.3f, %.3f)\n\n",
976 factor,
977 profile->table[i_table].x,
978 profile->table[i_table].y,
979 profile->table[i_table + 1].x,
980 profile->table[i_table + 1].y);
981#endif
982
983 /* We sampled in between this table point and the next, so the next travel step is smaller. */
984 distance_to_next_table_point -= segment_left;
985 distance_to_previous_table_point += segment_left;
986 segment_left = segment_length;
987 }
988}
989
995{
996 int n_samples = BKE_curveprofile_table_size(profile);
997 CurveProfilePoint *new_table = MEM_new_array<CurveProfilePoint>((n_samples + 1), __func__);
998
999 if (n_samples > 1) {
1000 create_samples(profile, n_samples - 1, false, new_table);
1001 }
1002
1003 /* Manually add last point at the end of the profile */
1004 new_table[n_samples - 1].x = 0.0f;
1005 new_table[n_samples - 1].y = 1.0f;
1006
1007 MEM_SAFE_DELETE(profile->table);
1008 profile->table = new_table;
1009}
1010
1016{
1017 int n_samples = profile->segments_len;
1018 if (n_samples <= 0) {
1019 return;
1020 }
1021 CurveProfilePoint *new_table = MEM_new_array<CurveProfilePoint>((n_samples + 1), __func__);
1022
1023 if (profile->flag & PROF_SAMPLE_EVEN_LENGTHS) {
1024 /* Even length sampling incompatible with only straight edge sampling for now. */
1025 create_samples_even_spacing(profile, n_samples, new_table);
1026 }
1027 else {
1028 create_samples(profile, n_samples, profile->flag & PROF_SAMPLE_STRAIGHT_EDGES, new_table);
1029 }
1030
1031 MEM_SAFE_DELETE(profile->segments);
1032 profile->segments = new_table;
1033}
1034
1035void BKE_curveprofile_update(CurveProfile *profile, const int update_flags)
1036{
1037 CurveProfilePoint *points = profile->path;
1038 const rctf *clipr = &profile->clip_rect;
1039
1040 profile->changed_timestamp++;
1041
1042 /* Clamp with the clipping rect in case something got past. */
1043 if (profile->flag & PROF_USE_CLIP) {
1044 /* Move points inside the clip rectangle. */
1045 if (update_flags & PROF_UPDATE_CLIP) {
1046 for (int i = 0; i < profile->path_len; i++) {
1047 points[i].x = clamp_f(points[i].x, clipr->xmin, clipr->xmax);
1048 points[i].y = clamp_f(points[i].y, clipr->ymin, clipr->ymax);
1049
1050 /* Extra sanity assert to make sure the points have the right profile pointer. */
1051 BLI_assert(points[i].profile == profile);
1052 }
1053 }
1054 /* Ensure zoom-level respects clipping. */
1055 if (BLI_rctf_size_x(&profile->view_rect) > BLI_rctf_size_x(&profile->clip_rect)) {
1056 profile->view_rect.xmin = profile->clip_rect.xmin;
1057 profile->view_rect.xmax = profile->clip_rect.xmax;
1058 }
1059 if (BLI_rctf_size_y(&profile->view_rect) > BLI_rctf_size_y(&profile->clip_rect)) {
1060 profile->view_rect.ymin = profile->clip_rect.ymin;
1061 profile->view_rect.ymax = profile->clip_rect.ymax;
1062 }
1063 }
1064
1065 /* Remove doubles with a threshold set at 1% of default range. */
1066 float thresh = pow2f(0.01f * BLI_rctf_size_x(clipr));
1067 if (update_flags & PROF_UPDATE_REMOVE_DOUBLES && profile->path_len > 2) {
1068 for (int i = 0; i < profile->path_len - 1; i++) {
1069 if (len_squared_v2v2(&points[i].x, &points[i + 1].x) < thresh) {
1070 if (i == 0) {
1071 points[0].flag |= (points[1].flag & (PROF_SELECT | PROF_ACTIVE));
1072 BKE_curveprofile_remove_point(profile, &points[1]);
1073 }
1074 else {
1075 points[i + 1].flag |= (points[i].flag & (PROF_SELECT | PROF_ACTIVE));
1076 BKE_curveprofile_remove_point(profile, &points[i]);
1077 }
1078 break; /* Assumes 1 deletion per update call is ok. */
1079 }
1080 }
1081 }
1082
1083 /* Create the high resolution table for drawing and some evaluation functions. */
1084 curveprofile_make_table(profile);
1085
1086 /* Store a table of samples for the segment locations for a preview and the table's user. */
1087 if (profile->segments_len > 0) {
1089 }
1090}
1091
1093{
1094 CurveProfilePoint *active_pt = nullptr;
1095 for (int i = 0; i < profile->path_len; i++) {
1096 CurveProfilePoint *pt = &profile->path[i];
1098 active_pt = pt;
1100 break;
1101 }
1102 }
1103 }
1104 return active_pt;
1105}
1106
1108{
1109 if (point->flag & PROF_ACTIVE) {
1110 return &point->x;
1111 }
1112 if (point->flag & PROF_H1_ACTIVE) {
1113 return &point->h1_loc[0];
1114 }
1115 if (point->flag & PROF_H2_ACTIVE) {
1116 return &point->h2_loc[0];
1117 }
1118 /* If no active point or handles, return the selected location. */
1119 if (point->flag & PROF_SELECT) {
1120 return &point->x;
1121 }
1122 if (point->flag & PROF_H1_SELECT) {
1123 return &point->h1_loc[0];
1124 }
1125 if (point->flag & PROF_H2_SELECT) {
1126 return &point->h2_loc[0];
1127 }
1128
1129 /* Either the input point itself or its handle should be labeled as active. */
1131 return nullptr;
1132}
1133
1135 float length_portion,
1136 float *x_out,
1137 float *y_out)
1138{
1139 const float total_length = curveprofile_total_length(profile);
1140 const float requested_length = length_portion * total_length;
1141
1142 /* Find the last point along the path with a lower length portion than the input. */
1143 int i = 0;
1144 float length_traveled = 0.0f;
1145 while (length_traveled < requested_length) {
1146 /* Check if we reached the last point before the final one. */
1147 if (i == BKE_curveprofile_table_size(profile) - 2) {
1148 break;
1149 }
1150 float new_length = curveprofile_distance_to_next_table_point(profile, i);
1151 if (length_traveled + new_length >= requested_length) {
1152 break;
1153 }
1154 length_traveled += new_length;
1155 i++;
1156 }
1157
1158 /* Now travel the remaining distance of length portion down the path to the next point and
1159 * find the location where we stop. */
1160 float distance_to_next_point = curveprofile_distance_to_next_table_point(profile, i);
1161 float lerp_factor = (requested_length - length_traveled) / distance_to_next_point;
1162
1163#ifdef DEBUG_CURVEPROFILE_EVALUATE
1164 printf("CURVEPROFILE EVALUATE\n");
1165 printf(" length portion input: %f\n", double(length_portion));
1166 printf(" requested path length: %f\n", double(requested_length));
1167 printf(" distance to next point: %f\n", double(distance_to_next_point));
1168 printf(" length traveled: %f\n", double(length_traveled));
1169 printf(" lerp-factor: %f\n", double(lerp_factor));
1170 printf(" ith point (%f, %f)\n", double(profile->path[i].x), double(profile->path[i].y));
1171 printf(" next point(%f, %f)\n", double(profile->path[i + 1].x), double(profile->path[i + 1].y));
1172#endif
1173
1174 *x_out = interpf(profile->table[i].x, profile->table[i + 1].x, lerp_factor);
1175 *y_out = interpf(profile->table[i].y, profile->table[i + 1].y, lerp_factor);
1176}
1177
1179
1180} // namespace blender
#define INT16_MAX
#define M_PI_2
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define PROF_TABLE_MAX
#define roundf(x)
#define FLT_MAX
float distance(float, float) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
VecBase< float, 2 > float2
BMesh Iterators.
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_curveprofile_init(struct CurveProfile *profile, short segments_len)
static void curveprofile_make_table(CurveProfile *profile)
void BKE_curveprofile_translate_selection(struct CurveProfile *profile, const blender::float2 &offset)
MINLINE void copy_v2_v2(float r[2], const float a[2])
static void point_init(CurveProfilePoint *point, float x, float y, eCurveProfilePoint_Flag flag, char h1, char h2)
static float curveprofile_distance_to_next_table_point(const CurveProfile *profile, int i)
void BKE_curveprofile_evaluate_length_portion(const struct CurveProfile *profile, float length_portion, float *x_out, float *y_out)
static void create_samples(CurveProfile *profile, int n_segments, bool sample_straight_edges, CurveProfilePoint *r_samples)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
static bool is_curved_edge(CurveProfilePoint *path, int i)
static float curveprofile_total_length(const CurveProfile *profile)
void BKE_curveprofile_remove_by_flag(struct CurveProfile *profile, short flag)
void BKE_curveprofile_copy_data(struct CurveProfile *target, const struct CurveProfile *profile)
MINLINE float pow2f(float x)
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:307
static CurveProfilePoint mirror_point(const CurveProfilePoint *point)
void BKE_curveprofile_reset(struct CurveProfile *profile)
static void curveprofile_build_steps(CurveProfile *profile)
bool BKE_curveprofile_move_handle(struct CurveProfilePoint *point, bool handle_1, bool snap, const float delta[2])
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
static void create_samples_even_spacing(CurveProfile *profile, int n_segments, CurveProfilePoint *r_samples)
void BKE_curveprofile_reset_view(struct CurveProfile *profile)
@ PROF_UPDATE_REMOVE_DOUBLES
static void calculate_path_handles(CurveProfilePoint *path, int path_len)
void BKE_curveprofile_blend_write(struct BlendWriter *writer, const struct CurveProfile *profile)
MINLINE bool equals_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
struct CurveProfilePoint * BKE_curveprofile_insert(struct CurveProfile *profile, float x, float y)
int BKE_curveprofile_table_size(const struct CurveProfile *profile)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
void BKE_curveprofile_free(struct CurveProfile *profile)
static void curveprofile_make_segments_table(CurveProfile *profile)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
MINLINE float max_ff(float a, float b)
static float bezt_edge_handle_angle(const CurveProfilePoint *path, int i_edge)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
float * BKE_curveprofile_active_location_get(struct CurveProfilePoint *point)
struct CurveProfile * BKE_curveprofile_add(eCurveProfilePresets preset)
float angle_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float interpf(float target, float origin, float t)
void BKE_curveprofile_selected_handle_set(struct CurveProfile *profile, int type_1, int type_2)
bool BKE_curveprofile_move_point(struct CurveProfile *profile, struct CurveProfilePoint *point, bool snap, const float delta[2])
static void curveprofile_build_supports(CurveProfile *profile)
struct CurveProfile * BKE_curveprofile_copy(const struct CurveProfile *profile)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
static void point_calculate_handle(CurveProfilePoint *point, const CurveProfilePoint *prev, const CurveProfilePoint *next)
void BKE_curveprofile_blend_read(struct BlendDataReader *reader, struct CurveProfile *profile)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1696
void BKE_curveprofile_update(struct CurveProfile *profile, int update_flags)
CurveProfilePoint * BKE_curveprofile_active_get(CurveProfile *profile)
void BKE_curveprofile_free_data(struct CurveProfile *profile)
void BKE_curveprofile_activate_nearest_point(struct CurveProfile *profile, const int i_last)
bool BKE_curveprofile_remove_point(struct CurveProfile *profile, struct CurveProfilePoint *point)
void BKE_curveprofile_reverse(struct CurveProfile *profile)
static int sort_points_curvature(const void *in_a, const void *in_b)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
Definition rct.cc:418
signed short int16_t
Definition BKE_text.h:77
MINLINE void madd_v2_v2v2fl(float r[2], const float a[2], const float b[2], float f)
MINLINE float clamp_f(float value, float min, float max)
void BKE_curveprofile_set_defaults(struct CurveProfile *profile)
vector snap(vector a, vector b)
Definition node_math.h:147
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
CurveProfilePoint * segments
eCurveProfilePresets preset