Blender V5.3
mask_evaluate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <algorithm> /* For `min/max`. */
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_listbase.hh"
17#include "BLI_math_geom_c.hh"
18#include "BLI_math_matrix_c.hh"
19#include "BLI_math_vector_c.hh"
20#include "BLI_utildefines.hh"
21
22#include "DNA_mask_types.h"
23
24#include "BKE_curve.hh"
25#include "BKE_mask.hh"
26
27#include "DEG_depsgraph.hh"
29
30namespace blender {
31
32static constexpr int MASK_RESOL_MAX = 128;
33
34int BKE_mask_spline_resolution(MaskSpline *spline, int width, int height)
35{
36 float max_segment = 0.01f;
37 int i, resol = 1;
38
39 if (width != 0 && height != 0) {
40 max_segment = 1.0f / float(max_ii(width, height));
41 }
42
43 for (i = 0; i < spline->tot_point; i++) {
44 MaskSplinePoint *point = &spline->points[i];
45 BezTriple *bezt_curr, *bezt_next;
46 float a, b, c, len;
47 int cur_resol;
48
49 bezt_curr = &point->bezt;
50 bezt_next = BKE_mask_spline_point_next_bezt(spline, spline->points, point);
51
52 if (bezt_next == nullptr) {
53 break;
54 }
55
56 a = len_v3v3(bezt_curr->vec[1], bezt_curr->vec[2]);
57 b = len_v3v3(bezt_curr->vec[2], bezt_next->vec[0]);
58 c = len_v3v3(bezt_next->vec[0], bezt_next->vec[1]);
59
60 len = a + b + c;
61 cur_resol = len / max_segment;
62
63 resol = std::max(resol, cur_resol);
64
65 if (resol >= MASK_RESOL_MAX) {
66 break;
67 }
68 }
69
70 return std::clamp(resol, 1, MASK_RESOL_MAX);
71}
72
73uint BKE_mask_spline_feather_resolution(MaskSpline *spline, int width, int height)
74{
75 const float max_segment = 0.005;
76 int resol = BKE_mask_spline_resolution(spline, width, height);
77 float max_jump = 0.0f;
78
79 /* Avoid checking the feather if we already hit the maximum value. */
80 if (resol >= MASK_RESOL_MAX) {
81 return MASK_RESOL_MAX;
82 }
83
84 for (int i = 0; i < spline->tot_point; i++) {
85 MaskSplinePoint *point = &spline->points[i];
86
87 float prev_u = 0.0f;
88 float prev_w = point->bezt.weight;
89
90 for (int j = 0; j < point->tot_uw; j++) {
91 const float w_diff = (point->uw[j].w - prev_w);
92 const float u_diff = (point->uw[j].u - prev_u);
93
94 /* avoid divide by zero and very high values,
95 * though these get clamped eventually */
96 if (u_diff > FLT_EPSILON) {
97 float jump = fabsf(w_diff / u_diff);
98
99 max_jump = max_ff(max_jump, jump);
100 }
101
102 prev_u = point->uw[j].u;
103 prev_w = point->uw[j].w;
104 }
105 }
106
107 resol += max_jump / max_segment;
108
109 return std::clamp(resol, 1, MASK_RESOL_MAX);
110}
111
113{
114 if (spline->flag & MASK_SPLINE_CYCLIC) {
115 return spline->tot_point * resol;
116 }
117
118 return ((spline->tot_point - 1) * resol) + 1;
119}
120
122 const uint resol,
123 uint *r_tot_diff_point))[2]
124{
125 MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
126
127 MaskSplinePoint *point_curr, *point_prev;
128 float (*diff_points)[2], (*fp)[2];
129 const int tot = BKE_mask_spline_differentiate_calc_total(spline, resol);
130 int a;
131
132 if (spline->tot_point <= 1) {
133 /* nothing to differentiate */
134 *r_tot_diff_point = 0;
135 return nullptr;
136 }
137
138 /* len+1 because of 'forward_diff_bezier' function */
139 *r_tot_diff_point = tot;
140 diff_points = fp = MEM_new_array_zeroed<float[2]>(tot + 1, "mask spline vets");
141
142 a = spline->tot_point - 1;
143 if (spline->flag & MASK_SPLINE_CYCLIC) {
144 a++;
145 }
146
147 point_prev = points_array;
148 point_curr = point_prev + 1;
149
150 while (a--) {
151 BezTriple *bezt_prev;
152 BezTriple *bezt_curr;
153 int j;
154
155 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC)) {
156 point_curr = points_array;
157 }
158
159 bezt_prev = &point_prev->bezt;
160 bezt_curr = &point_curr->bezt;
161
162 for (j = 0; j < 2; j++) {
163 BKE_curve_forward_diff_bezier(bezt_prev->vec[1][j],
164 bezt_prev->vec[2][j],
165 bezt_curr->vec[0][j],
166 bezt_curr->vec[1][j],
167 &(*fp)[j],
168 resol,
169 sizeof(float[2]));
170 }
171
172 fp += resol;
173
174 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC) == 0) {
175 copy_v2_v2(*fp, bezt_curr->vec[1]);
176 }
177
178 point_prev = point_curr;
179 point_curr++;
180 }
181
182 return diff_points;
183}
184
186 MaskSpline *spline, int width, int height, uint *r_tot_diff_point))[2]
187{
188 int resol = BKE_mask_spline_resolution(spline, width, height);
189
190 return BKE_mask_spline_differentiate_with_resolution(spline, resol, r_tot_diff_point);
191}
192
193/* ** feather points self-intersection collapse routine ** */
194
200
201static void feather_bucket_add_edge(FeatherEdgesBucket *bucket, int start, int end)
202{
203 const int alloc_delta = 256;
204
205 if (bucket->tot_segment >= bucket->alloc_segment) {
206 if (!bucket->segments) {
207 bucket->segments = MEM_new_array_zeroed<int[2]>(alloc_delta, "feather bucket segments");
208 }
209 else {
210 bucket->segments = static_cast<int (*)[2]>(MEM_realloc_uninitialized(
211 bucket->segments, (alloc_delta + bucket->tot_segment) * sizeof(*bucket->segments)));
212 }
213
214 bucket->alloc_segment += alloc_delta;
215 }
216
217 bucket->segments[bucket->tot_segment][0] = start;
218 bucket->segments[bucket->tot_segment][1] = end;
219
220 bucket->tot_segment++;
221}
222
223static void feather_bucket_check_intersect(float (*feather_points)[2],
224 int tot_feather_point,
225 FeatherEdgesBucket *bucket,
226 int cur_a,
227 int cur_b)
228{
229 const float *v1 = static_cast<float *>(feather_points[cur_a]);
230 const float *v2 = static_cast<float *>(feather_points[cur_b]);
231
232 for (int i = 0; i < bucket->tot_segment; i++) {
233 int check_a = bucket->segments[i][0];
234 int check_b = bucket->segments[i][1];
235
236 const float *v3 = static_cast<float *>(feather_points[check_a]);
237 const float *v4 = static_cast<float *>(feather_points[check_b]);
238
239 if (check_a >= cur_a - 1 || cur_b == check_a) {
240 continue;
241 }
242
243 if (isect_seg_seg_v2_simple(v1, v2, v3, v4)) {
244 int k;
245 float p[2];
246 float min_a[2], max_a[2];
247 float min_b[2], max_b[2];
248
249 isect_seg_seg_v2_point(v1, v2, v3, v4, p);
250
251 INIT_MINMAX2(min_a, max_a);
252 INIT_MINMAX2(min_b, max_b);
253
254 /* collapse loop with smaller AABB */
255 for (k = 0; k < tot_feather_point; k++) {
256 if (k >= check_b && k <= cur_a) {
257 minmax_v2v2_v2(min_a, max_a, feather_points[k]);
258 }
259 else {
260 minmax_v2v2_v2(min_b, max_b, feather_points[k]);
261 }
262 }
263
264 if (max_a[0] - min_a[0] < max_b[0] - min_b[0] || max_a[1] - min_a[1] < max_b[1] - min_b[1]) {
265 for (k = check_b; k <= cur_a; k++) {
266 copy_v2_v2(feather_points[k], p);
267 }
268 }
269 else {
270 for (k = 0; k <= check_a; k++) {
271 copy_v2_v2(feather_points[k], p);
272 }
273
274 if (cur_b != 0) {
275 for (k = cur_b; k < tot_feather_point; k++) {
276 copy_v2_v2(feather_points[k], p);
277 }
278 }
279 }
280 }
281 }
282}
283
284static int feather_bucket_index_from_coord(const float co[2],
285 const float min[2],
286 const float bucket_scale[2],
287 const int buckets_per_side)
288{
289 int x = int((co[0] - min[0]) * bucket_scale[0]);
290 int y = int((co[1] - min[1]) * bucket_scale[1]);
291
292 if (x == buckets_per_side) {
293 x--;
294 }
295
296 if (y == buckets_per_side) {
297 y--;
298 }
299
300 return y * buckets_per_side + x;
301}
302
304 int start_bucket_index,
305 int end_bucket_index,
306 int buckets_per_side,
307 FeatherEdgesBucket **r_diagonal_bucket_a,
308 FeatherEdgesBucket **r_diagonal_bucket_b)
309{
310 int start_bucket_x = start_bucket_index % buckets_per_side;
311 int start_bucket_y = start_bucket_index / buckets_per_side;
312
313 int end_bucket_x = end_bucket_index % buckets_per_side;
314 int end_bucket_y = end_bucket_index / buckets_per_side;
315
316 int diagonal_bucket_a_index = start_bucket_y * buckets_per_side + end_bucket_x;
317 int diagonal_bucket_b_index = end_bucket_y * buckets_per_side + start_bucket_x;
318
319 *r_diagonal_bucket_a = &buckets[diagonal_bucket_a_index];
320 *r_diagonal_bucket_b = &buckets[diagonal_bucket_b_index];
321}
322
324 float (*feather_points)[2],
325 const uint tot_feather_point)
326{
327#define BUCKET_INDEX(co) feather_bucket_index_from_coord(co, min, bucket_scale, buckets_per_side)
328
329 int buckets_per_side, tot_bucket;
330 float bucket_size, bucket_scale[2];
331
332 FeatherEdgesBucket *buckets;
333
334 float min[2], max[2];
335 float max_delta_x = -1.0f, max_delta_y = -1.0f, max_delta;
336
337 if (tot_feather_point < 4) {
338 /* self-intersection works only for quads at least,
339 * in other cases polygon can't be self-intersecting anyway
340 */
341
342 return;
343 }
344
345 /* find min/max corners of mask to build buckets in that space */
347
348 for (uint i = 0; i < tot_feather_point; i++) {
349 uint next = i + 1;
350 float delta;
351
352 minmax_v2v2_v2(min, max, feather_points[i]);
353
354 if (next == tot_feather_point) {
355 if (spline->flag & MASK_SPLINE_CYCLIC) {
356 next = 0;
357 }
358 else {
359 break;
360 }
361 }
362
363 delta = fabsf(feather_points[i][0] - feather_points[next][0]);
364 max_delta_x = std::max(delta, max_delta_x);
365
366 delta = fabsf(feather_points[i][1] - feather_points[next][1]);
367 max_delta_y = std::max(delta, max_delta_y);
368 }
369
370 /* Prevent divisions by zero by ensuring bounding box is not collapsed. */
371 if (max[0] - min[0] < FLT_EPSILON) {
372 max[0] += 0.01f;
373 min[0] -= 0.01f;
374 }
375
376 if (max[1] - min[1] < FLT_EPSILON) {
377 max[1] += 0.01f;
378 min[1] -= 0.01f;
379 }
380
381 /* use dynamically calculated buckets per side, so we likely wouldn't
382 * run into a situation when segment doesn't fit two buckets which is
383 * pain collecting candidates for intersection
384 */
385
386 max_delta_x /= max[0] - min[0];
387 max_delta_y /= max[1] - min[1];
388
389 max_delta = std::max(max_delta_x, max_delta_y);
390
391 buckets_per_side = min_ii(512, 0.9f / max_delta);
392
393 if (buckets_per_side == 0) {
394 /* happens when some segment fills the whole bounding box across some of dimension */
395
396 buckets_per_side = 1;
397 }
398
399 tot_bucket = buckets_per_side * buckets_per_side;
400 bucket_size = 1.0f / buckets_per_side;
401
402 /* pre-compute multipliers, to save mathematical operations in loops */
403 bucket_scale[0] = 1.0f / ((max[0] - min[0]) * bucket_size);
404 bucket_scale[1] = 1.0f / ((max[1] - min[1]) * bucket_size);
405
406 /* fill in buckets' edges */
407 buckets = MEM_new_array_zeroed<FeatherEdgesBucket>(tot_bucket, "feather buckets");
408
409 for (int i = 0; i < tot_feather_point; i++) {
410 int start = i, end = i + 1;
411 int start_bucket_index, end_bucket_index;
412
413 if (end == tot_feather_point) {
414 if (spline->flag & MASK_SPLINE_CYCLIC) {
415 end = 0;
416 }
417 else {
418 break;
419 }
420 }
421
422 start_bucket_index = BUCKET_INDEX(feather_points[start]);
423 end_bucket_index = BUCKET_INDEX(feather_points[end]);
424
425 feather_bucket_add_edge(&buckets[start_bucket_index], start, end);
426
427 if (start_bucket_index != end_bucket_index) {
428 FeatherEdgesBucket *end_bucket = &buckets[end_bucket_index];
429 FeatherEdgesBucket *diagonal_bucket_a, *diagonal_bucket_b;
430
432 start_bucket_index,
433 end_bucket_index,
434 buckets_per_side,
435 &diagonal_bucket_a,
436 &diagonal_bucket_b);
437
438 feather_bucket_add_edge(end_bucket, start, end);
439 feather_bucket_add_edge(diagonal_bucket_a, start, end);
440 feather_bucket_add_edge(diagonal_bucket_a, start, end);
441 }
442 }
443
444 /* check all edges for intersection with edges from their buckets */
445 for (int i = 0; i < tot_feather_point; i++) {
446 int cur_a = i, cur_b = i + 1;
447 int start_bucket_index, end_bucket_index;
448
449 FeatherEdgesBucket *start_bucket;
450
451 if (cur_b == tot_feather_point) {
452 cur_b = 0;
453 }
454
455 start_bucket_index = BUCKET_INDEX(feather_points[cur_a]);
456 end_bucket_index = BUCKET_INDEX(feather_points[cur_b]);
457
458 start_bucket = &buckets[start_bucket_index];
459
460 feather_bucket_check_intersect(feather_points, tot_feather_point, start_bucket, cur_a, cur_b);
461
462 if (start_bucket_index != end_bucket_index) {
463 FeatherEdgesBucket *end_bucket = &buckets[end_bucket_index];
464 FeatherEdgesBucket *diagonal_bucket_a, *diagonal_bucket_b;
465
467 start_bucket_index,
468 end_bucket_index,
469 buckets_per_side,
470 &diagonal_bucket_a,
471 &diagonal_bucket_b);
472
473 feather_bucket_check_intersect(feather_points, tot_feather_point, end_bucket, cur_a, cur_b);
475 feather_points, tot_feather_point, diagonal_bucket_a, cur_a, cur_b);
477 feather_points, tot_feather_point, diagonal_bucket_b, cur_a, cur_b);
478 }
479 }
480
481 /* free buckets */
482 for (int i = 0; i < tot_bucket; i++) {
483 if (buckets[i].segments) {
484 MEM_delete(buckets[i].segments);
485 }
486 }
487
488 MEM_delete(buckets);
489
490#undef BUCKET_INDEX
491}
492
494static float (
496 const uint resol,
497 const bool do_feather_isect,
498 uint *r_tot_feather_point))[2]
499{
500 MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
501 MaskSplinePoint *point_curr, *point_prev;
502 float (*feather)[2], (*fp)[2];
503
504 const int tot = BKE_mask_spline_differentiate_calc_total(spline, resol);
505 int a;
506
507 /* tot+1 because of 'forward_diff_bezier' function */
508 feather = fp = MEM_new_array_zeroed<float[2]>(tot + 1, "mask spline feather diff points");
509
510 a = spline->tot_point - 1;
511 if (spline->flag & MASK_SPLINE_CYCLIC) {
512 a++;
513 }
514
515 point_prev = points_array;
516 point_curr = point_prev + 1;
517
518 while (a--) {
519 // BezTriple *bezt_prev; /* UNUSED */
520 // BezTriple *bezt_curr; /* UNUSED */
521 int j;
522
523 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC)) {
524 point_curr = points_array;
525 }
526
527 // bezt_prev = &point_prev->bezt;
528 // bezt_curr = &point_curr->bezt;
529
530 for (j = 0; j < resol; j++, fp++) {
531 float u = float(j) / resol, weight;
532 float co[2], n[2];
533
534 /* TODO: these calls all calculate similar things
535 * could be unified for some speed */
536 BKE_mask_point_segment_co(spline, point_prev, u, co);
537 BKE_mask_point_normal(spline, point_prev, u, n);
538 weight = BKE_mask_point_weight(spline, point_prev, u);
539
540 madd_v2_v2v2fl(*fp, co, n, weight);
541 }
542
543 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC) == 0) {
544 float u = 1.0f, weight;
545 float co[2], n[2];
546
547 BKE_mask_point_segment_co(spline, point_prev, u, co);
548 BKE_mask_point_normal(spline, point_prev, u, n);
549 weight = BKE_mask_point_weight(spline, point_prev, u);
550
551 madd_v2_v2v2fl(*fp, co, n, weight);
552 }
553
554 point_prev = point_curr;
555 point_curr++;
556 }
557
558 *r_tot_feather_point = tot;
559
560 if ((spline->flag & MASK_SPLINE_NOINTERSECT) && do_feather_isect) {
562 }
563
564 return feather;
565}
566
569 MaskSpline *spline,
570 const uint resol,
571 const bool do_feather_isect,
572 uint *r_tot_feather_point))[2]
573{
574 MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
575
576 MaskSplinePoint *point_curr, *point_prev;
577 float (*feather)[2], (*fp)[2];
578 const int tot = BKE_mask_spline_differentiate_calc_total(spline, resol);
579 int a;
580
581 if (spline->tot_point <= 1) {
582 /* nothing to differentiate */
583 *r_tot_feather_point = 0;
584 return nullptr;
585 }
586
587 /* len+1 because of 'forward_diff_bezier' function */
588 *r_tot_feather_point = tot;
589 feather = fp = MEM_new_array_zeroed<float[2]>(tot + 1, "mask spline vets");
590
591 a = spline->tot_point - 1;
592 if (spline->flag & MASK_SPLINE_CYCLIC) {
593 a++;
594 }
595
596 point_prev = points_array;
597 point_curr = point_prev + 1;
598
599 while (a--) {
600 BezTriple local_prevbezt;
601 BezTriple local_bezt;
602 float point_prev_n[2], point_curr_n[2], tvec[2];
603 float weight_prev, weight_curr;
604 float len_base, len_feather, len_scalar;
605
606 BezTriple *bezt_prev;
607 BezTriple *bezt_curr;
608 int j;
609
610 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC)) {
611 point_curr = points_array;
612 }
613
614 bezt_prev = &point_prev->bezt;
615 bezt_curr = &point_curr->bezt;
616
617 /* modified copy for feather */
618 local_prevbezt = *bezt_prev;
619 local_bezt = *bezt_curr;
620
621 bezt_prev = &local_prevbezt;
622 bezt_curr = &local_bezt;
623
624 /* calc the normals */
625 sub_v2_v2v2(tvec, bezt_prev->vec[1], bezt_prev->vec[0]);
626 normalize_v2(tvec);
627 point_prev_n[0] = -tvec[1];
628 point_prev_n[1] = tvec[0];
629
630 sub_v2_v2v2(tvec, bezt_curr->vec[1], bezt_curr->vec[0]);
631 normalize_v2(tvec);
632 point_curr_n[0] = -tvec[1];
633 point_curr_n[1] = tvec[0];
634
635 weight_prev = bezt_prev->weight;
636 weight_curr = bezt_curr->weight;
637
638 mul_v2_fl(point_prev_n, weight_prev);
639 mul_v2_fl(point_curr_n, weight_curr);
640
641 /* before we transform verts */
642 len_base = len_v2v2(bezt_prev->vec[1], bezt_curr->vec[1]);
643
644 // add_v2_v2(bezt_prev->vec[0], point_prev_n); /* Not needed. */
645 add_v2_v2(bezt_prev->vec[1], point_prev_n);
646 add_v2_v2(bezt_prev->vec[2], point_prev_n);
647
648 add_v2_v2(bezt_curr->vec[0], point_curr_n);
649 add_v2_v2(bezt_curr->vec[1], point_curr_n);
650 // add_v2_v2(bezt_curr->vec[2], point_curr_n); /* Not needed. */
651
652 len_feather = len_v2v2(bezt_prev->vec[1], bezt_curr->vec[1]);
653
654 /* scale by change in length */
655 len_scalar = len_feather / len_base;
656 dist_ensure_v2_v2fl(bezt_prev->vec[2],
657 bezt_prev->vec[1],
658 len_scalar * len_v2v2(bezt_prev->vec[2], bezt_prev->vec[1]));
659 dist_ensure_v2_v2fl(bezt_curr->vec[0],
660 bezt_curr->vec[1],
661 len_scalar * len_v2v2(bezt_curr->vec[0], bezt_curr->vec[1]));
662
663 for (j = 0; j < 2; j++) {
664 BKE_curve_forward_diff_bezier(bezt_prev->vec[1][j],
665 bezt_prev->vec[2][j],
666 bezt_curr->vec[0][j],
667 bezt_curr->vec[1][j],
668 &(*fp)[j],
669 resol,
670 sizeof(float[2]));
671 }
672
673 /* scale by the uw's */
674 if (point_prev->tot_uw) {
675 for (j = 0; j < resol; j++, fp++) {
676 float u = float(j) / resol;
677 float weight_uw, weight_scalar;
678 float co[2];
679
680 /* TODO: these calls all calculate similar things
681 * could be unified for some speed */
682 BKE_mask_point_segment_co(spline, point_prev, u, co);
683
684 weight_uw = BKE_mask_point_weight(spline, point_prev, u);
685 weight_scalar = BKE_mask_point_weight_scalar(spline, point_prev, u);
686
687 dist_ensure_v2_v2fl(*fp, co, len_v2v2(*fp, co) * (weight_uw / weight_scalar));
688 }
689 }
690 else {
691 fp += resol;
692 }
693
694 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC) == 0) {
695 copy_v2_v2(*fp, bezt_curr->vec[1]);
696 }
697
698 point_prev = point_curr;
699 point_curr++;
700 }
701
702 if ((spline->flag & MASK_SPLINE_NOINTERSECT) && do_feather_isect) {
704 }
705
706 return feather;
707}
708
711 const uint resol,
712 const bool do_feather_isect,
713 uint *r_tot_feather_point))[2]
714{
715 switch (spline->offset_mode) {
718 spline, resol, do_feather_isect, r_tot_feather_point);
720 default:
722 spline, resol, do_feather_isect, r_tot_feather_point);
723 }
724}
725
726float (*BKE_mask_spline_feather_points(MaskSpline *spline, int *r_tot_feather_point))[2]
727{
728 MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
729
730 int i, tot = 0;
731 float (*feather)[2], (*fp)[2];
732
733 /* count */
734 for (i = 0; i < spline->tot_point; i++) {
735 MaskSplinePoint *point = &points_array[i];
736
737 tot += point->tot_uw + 1;
738 }
739
740 /* create data */
741 feather = fp = MEM_new_array_zeroed<float[2]>(tot, "mask spline feather points");
742
743 for (i = 0; i < spline->tot_point; i++) {
744 MaskSplinePoint *point = &points_array[i];
745 BezTriple *bezt = &point->bezt;
746 float weight, n[2];
747 int j;
748
749 BKE_mask_point_normal(spline, point, 0.0f, n);
750 weight = BKE_mask_point_weight(spline, point, 0.0f);
751
752 madd_v2_v2v2fl(*fp, bezt->vec[1], n, weight);
753 fp++;
754
755 for (j = 0; j < point->tot_uw; j++) {
756 float u = point->uw[j].u;
757 float co[2];
758
759 BKE_mask_point_segment_co(spline, point, u, co);
760 BKE_mask_point_normal(spline, point, u, n);
761 weight = BKE_mask_point_weight(spline, point, u);
762
763 madd_v2_v2v2fl(*fp, co, n, weight);
764 fp++;
765 }
766 }
767
768 *r_tot_feather_point = tot;
769
770 return feather;
771}
772
774 MaskSpline *spline, MaskSplinePoint *point, int width, int height, uint *r_tot_feather_point)
775{
776 float *feather, *fp;
777 uint resol = BKE_mask_spline_feather_resolution(spline, width, height);
778
779 feather = fp = MEM_new_array_zeroed<float>(2 * resol, "mask point spline feather diff points");
780
781 for (uint i = 0; i < resol; i++, fp += 2) {
782 float u = float(i % resol) / resol, weight;
783 float co[2], n[2];
784
785 BKE_mask_point_segment_co(spline, point, u, co);
786 BKE_mask_point_normal(spline, point, u, n);
787 weight = BKE_mask_point_weight(spline, point, u);
788
789 fp[0] = co[0] + n[0] * weight;
790 fp[1] = co[1] + n[1] * weight;
791 }
792
793 *r_tot_feather_point = resol;
794
795 return feather;
796}
797
799 MaskSpline *spline, MaskSplinePoint *point, int width, int height, uint *r_tot_diff_point)
800{
801 MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
802
803 BezTriple *bezt, *bezt_next;
804 float *diff_points, *fp;
805 int j, resol = BKE_mask_spline_resolution(spline, width, height);
806
807 bezt = &point->bezt;
808 bezt_next = BKE_mask_spline_point_next_bezt(spline, points_array, point);
809
810 if (!bezt_next) {
811 return nullptr;
812 }
813
814 /* resol+1 because of 'forward_diff_bezier' function */
815 *r_tot_diff_point = resol + 1;
816 diff_points = fp = MEM_new_array_zeroed<float>(2 * (resol + 1), "mask segment vets");
817
818 for (j = 0; j < 2; j++) {
820 bezt->vec[2][j],
821 bezt_next->vec[0][j],
822 bezt_next->vec[1][j],
823 fp + j,
824 resol,
825 sizeof(float[2]));
826 }
827
828 copy_v2_v2(fp + 2 * resol, bezt_next->vec[1]);
829
830 return diff_points;
831}
832
833static void mask_evaluate_apply_point_parent(MaskSplinePoint *point, float ctime)
834{
835 float parent_matrix[3][3];
836 BKE_mask_point_parent_matrix_get(point, ctime, parent_matrix);
837 mul_m3_v2(parent_matrix, point->bezt.vec[0]);
838 mul_m3_v2(parent_matrix, point->bezt.vec[1]);
839 mul_m3_v2(parent_matrix, point->bezt.vec[2]);
840}
841
842void BKE_mask_layer_evaluate_animation(MaskLayer *masklay, const float ctime)
843{
844 /* animation if available */
845 MaskLayerShape *masklay_shape_a;
846 MaskLayerShape *masklay_shape_b;
848 masklay, ctime, &masklay_shape_a, &masklay_shape_b);
849 if (found) {
850 if (found == 1) {
851#if 0
852 printf("%s: exact %d %d (%d)\n",
853 __func__,
854 int(ctime),
855 masklay->splines_shapes.count(),
856 masklay_shape_a->frame);
857#endif
858 BKE_mask_layer_shape_to_mask(masklay, masklay_shape_a);
859 }
860 else if (found == 2) {
861 float w = masklay_shape_b->frame - masklay_shape_a->frame;
862#if 0
863 printf("%s: tween %d %d (%d %d)\n",
864 __func__,
865 int(ctime),
866 masklay->splines_shapes.count(),
867 masklay_shape_a->frame,
868 masklay_shape_b->frame);
869#endif
871 masklay, masklay_shape_a, masklay_shape_b, (ctime - masklay_shape_a->frame) / w);
872 }
873 else {
874 /* always fail, should never happen */
875 BLI_assert(found == 2);
876 }
877 }
878}
879
880void BKE_mask_layer_evaluate_deform(MaskLayer *masklay, const float ctime)
881{
883 for (MaskSpline &spline : masklay->splines) {
884 bool need_handle_recalc = false;
886 for (int i = 0; i < spline.tot_point; i++) {
887 MaskSplinePoint *point = &spline.points[i];
888 MaskSplinePoint *point_deform = &spline.runtime->points_deform[i];
889 BKE_mask_point_free(point_deform);
890 *point_deform = *point;
891 point_deform->uw = point->uw ? MEM_dupalloc(point->uw) : nullptr;
892 mask_evaluate_apply_point_parent(point_deform, ctime);
893 if (ELEM(point->bezt.h1, HD_AUTO, HD_VECT)) {
894 need_handle_recalc = true;
895 }
896 }
897 /* if the spline has auto or vector handles, these need to be
898 * recalculated after deformation.
899 */
900 if (need_handle_recalc) {
901 for (int i = 0; i < spline.tot_point; i++) {
902 MaskSplinePoint *point_deform = &spline.runtime->points_deform[i];
903 if (ELEM(point_deform->bezt.h1, HD_AUTO, HD_VECT)) {
904 BKE_mask_calc_handle_point(&spline, point_deform);
905 }
906 }
907 }
908 /* end extra calc handles loop */
909 }
910}
911
912void BKE_mask_eval_animation(Depsgraph *depsgraph, Mask *mask)
913{
914 float ctime = DEG_get_ctime(depsgraph);
915 DEG_debug_print_eval(depsgraph, __func__, mask->id.name, mask);
916 for (MaskLayer &mask_layer : mask->masklayers) {
917 BKE_mask_layer_evaluate_animation(&mask_layer, ctime);
918 }
919 mask->runtime.last_update = DEG_get_update_count(depsgraph);
920}
921
922void BKE_mask_eval_update(Depsgraph *depsgraph, Mask *mask)
923{
924 const bool is_depsgraph_active = DEG_is_active(depsgraph);
925 float ctime = DEG_get_ctime(depsgraph);
926 DEG_debug_print_eval(depsgraph, __func__, mask->id.name, mask);
927 for (MaskLayer &mask_layer : mask->masklayers) {
928 BKE_mask_layer_evaluate_deform(&mask_layer, ctime);
929 }
930
931 if (is_depsgraph_active) {
932 Mask *mask_orig = DEG_get_original(mask);
933 for (MaskLayer *masklay_orig = mask_orig->masklayers.first(),
934 *masklay_eval = mask->masklayers.first();
935 masklay_orig != nullptr;
936 masklay_orig = masklay_orig->next, masklay_eval = masklay_eval->next)
937 {
938 for (MaskSpline *spline_orig = masklay_orig->splines.first(),
939 *spline_eval = masklay_eval->splines.first();
940 spline_orig != nullptr;
941 spline_orig = spline_orig->next, spline_eval = spline_eval->next)
942 {
943 for (int i = 0; i < spline_eval->tot_point; i++) {
944 MaskSplinePoint *point_eval = &spline_eval->points[i];
945 MaskSplinePoint *point_orig = &spline_orig->points[i];
946 point_orig->bezt = point_eval->bezt;
947 }
948 }
949 }
950 }
951 mask->runtime.last_update = DEG_get_update_count(depsgraph);
952}
953
954} // namespace blender
Read Blender memory allocation and freeing.
#define INIT_MINMAX2(min, max)
#define ELEM(...)
#define MEM_realloc_uninitialized(vmemh, len)
btScalar max
void jump(const btVector3 &v=btVector3(0, 0, 0))
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define FLT_EPSILON
#define BUCKET_INDEX(co)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void feather_bucket_add_edge(FeatherEdgesBucket *bucket, int start, int end)
unsigned int uint
Definition BKE_text.h:55
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:324
void BKE_mask_layer_evaluate_deform(MaskLayer *masklay, float ctime)
MINLINE void mul_v2_fl(float r[2], float f)
uint64_t DEG_get_update_count(const Depsgraph *depsgraph)
Definition depsgraph.cc:356
MINLINE void copy_v2_v2(float r[2], const float a[2])
MaskSplinePoint * BKE_mask_spline_point_array_from_point(MaskSpline *spline, const MaskSplinePoint *point_ref)
void BKE_mask_eval_update(Depsgraph *depsgraph, Mask *mask)
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
int isect_seg_seg_v2_point(const float v0[2], const float v1[2], const float v2[2], const float v3[2], float r_vi[2])
static float(* mask_spline_feather_differentiated_points_with_resolution__even(MaskSpline *spline, const uint resol, const bool do_feather_isect, uint *r_tot_feather_point))[2]
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
void BKE_mask_point_parent_matrix_get(MaskSplinePoint *point, float ctime, float parent_matrix[3][3])
MINLINE float normalize_v2(float n[2])
MINLINE int max_ii(int a, int b)
void mul_m3_v2(const float m[3][3], float r[2])
void BKE_mask_layer_shape_to_mask(MaskLayer *masklay, MaskLayerShape *masklay_shape)
int BKE_mask_layer_shape_find_frame_range(MaskLayer *masklay, float frame, MaskLayerShape **r_masklay_shape_a, MaskLayerShape **r_masklay_shape_b)
MINLINE void add_v2_v2(float r[2], const float a[2])
void minmax_v2v2_v2(float min[2], float max[2], const float vec[2])
BezTriple * BKE_mask_spline_point_next_bezt(MaskSpline *spline, MaskSplinePoint *points_array, MaskSplinePoint *point)
int BKE_mask_spline_resolution(MaskSpline *spline, int width, int height)
float BKE_mask_point_weight_scalar(MaskSpline *spline, MaskSplinePoint *point, float u)
float(* BKE_mask_spline_feather_differentiated_points_with_resolution(MaskSpline *spline, unsigned int resol, bool do_feather_isect, unsigned int *r_tot_feather_point))[2]
void BKE_mask_spline_feather_collapse_inner_loops(MaskSpline *spline, float(*feather_points)[2], unsigned int tot_feather_point)
void BKE_mask_spline_ensure_deform(MaskSpline *spline)
float(* BKE_mask_spline_feather_points(MaskSpline *spline, int *tot_feather_point))[2]
void BKE_mask_layer_calc_handles(MaskLayer *masklay)
@ MASK_SPLINE_CYCLIC
@ MASK_SPLINE_NOINTERSECT
void BKE_mask_point_normal(MaskSpline *spline, MaskSplinePoint *point, float u, float n[2])
float * BKE_mask_point_segment_feather_diff(MaskSpline *spline, MaskSplinePoint *point, int width, int height, unsigned int *r_tot_feather_point)
int BKE_mask_spline_differentiate_calc_total(const MaskSpline *spline, unsigned int resol)
static float(* mask_spline_feather_differentiated_points_with_resolution__double(MaskSpline *spline, const uint resol, const bool do_feather_isect, uint *r_tot_feather_point))[2]
void dist_ensure_v2_v2fl(float v1[2], const float v2[2], float dist)
bool isect_seg_seg_v2_simple(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
void BKE_mask_layer_evaluate_animation(MaskLayer *masklay, float ctime)
static void feather_bucket_check_intersect(float(*feather_points)[2], int tot_feather_point, FeatherEdgesBucket *bucket, int cur_a, int cur_b)
float BKE_mask_point_weight(MaskSpline *spline, MaskSplinePoint *point, float u)
static void feather_bucket_get_diagonal(FeatherEdgesBucket *buckets, int start_bucket_index, int end_bucket_index, int buckets_per_side, FeatherEdgesBucket **r_diagonal_bucket_a, FeatherEdgesBucket **r_diagonal_bucket_b)
T * DEG_get_original(T *id)
static void mask_evaluate_apply_point_parent(MaskSplinePoint *point, float ctime)
void BKE_mask_calc_handle_point(MaskSpline *spline, MaskSplinePoint *point)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
unsigned int BKE_mask_spline_feather_resolution(MaskSpline *spline, int width, int height)
MINLINE float max_ff(float a, float b)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
static constexpr int MASK_RESOL_MAX
void BKE_mask_point_free(MaskSplinePoint *point)
static int feather_bucket_index_from_coord(const float co[2], const float min[2], const float bucket_scale[2], const int buckets_per_side)
void BKE_mask_eval_animation(Depsgraph *depsgraph, Mask *mask)
MINLINE float len_v3v3(const float a[3], const float b[3]) 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
MaskSplinePoint * BKE_mask_spline_point_array(MaskSpline *spline)
void BKE_mask_layer_shape_to_mask_interp(MaskLayer *masklay, MaskLayerShape *masklay_shape_a, MaskLayerShape *masklay_shape_b, float fac)
MINLINE int min_ii(int a, int b)
float DEG_get_ctime(const Depsgraph *graph)
void BKE_mask_point_segment_co(MaskSpline *spline, MaskSplinePoint *point, float u, float co[2])
@ MASK_SPLINE_OFFSET_EVEN
@ MASK_SPLINE_OFFSET_SMOOTH
float * BKE_mask_point_segment_diff(MaskSpline *spline, MaskSplinePoint *point, int width, int height, unsigned int *r_tot_diff_point)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE void madd_v2_v2v2fl(float r[2], const float a[2], const float b[2], float f)
float(* BKE_mask_spline_differentiate(MaskSpline *spline, int width, int height, unsigned int *r_tot_diff_point))[2]
float(* BKE_mask_spline_differentiate_with_resolution(MaskSpline *spline, unsigned int resol, unsigned int *r_tot_diff_point))[2]
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
eBezTriple_Handle h1
ListBaseT< MaskLayerShape > splines_shapes
ListBaseT< MaskSpline > splines
MaskSplinePointUW * uw
MaskSplinePoint * points
MaskSplineRuntime * runtime
struct MaskSpline * next
MaskSplineFlag flag
MaskSplineOffset offset_mode
ListBaseT< struct MaskLayer > masklayers
Array< MaskSplinePoint > points_deform
Definition BKE_mask.hh:38