Blender V5.3
mask_rasterize.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
49
50#include <algorithm>
51#include <numeric> /* For `std::iota`. */
52
53#include "CLG_log.h"
54
55#include "MEM_guardedalloc.h"
56
57#include "DNA_mask_types.h"
58#include "DNA_scene_types.h"
59#include "DNA_vec_types.h"
60
61#include "BLI_array.hh"
62#include "BLI_delaunay_2d.hh"
63#include "BLI_math_geom_c.hh"
64#include "BLI_math_vector_c.hh"
66#include "BLI_memarena.hh"
67#include "BLI_scanfill.hh"
68#include "BLI_utildefines.hh"
69#include "BLI_vector.hh"
70
71#include "BLI_linklist.hh"
72#include "BLI_listbase.hh"
73#include "BLI_rect.hh"
74#include "BLI_task_c.hh"
75
76#include "BKE_mask.hh"
77
78#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
79
80namespace blender {
81
82/* this is rather and annoying hack, use define to isolate it.
83 * problem is caused by scanfill removing edges on us. */
84#define USE_SCANFILL_EDGE_WORKAROUND
85
86#define SPLINE_RESOL_CAP_PER_PIXEL 2
87#define SPLINE_RESOL_CAP_MIN 8
88#define SPLINE_RESOL_CAP_MAX 64
89
90/* Bucket size in pixels: smaller values are more granular (less work for raster loop),
91 * but more memory and more work in single threaded initialization time. Found that 8
92 * seems to be optimal for both simple (150 faces) & complex (16000 faces) masks at
93 * HD / 4K resolutions. */
94static constexpr int BUCKET_PIXELS_PER_CELL = 8;
95
96#define SF_EDGE_IS_BOUNDARY 0xff
97#define SF_KEYINDEX_TEMP_ID uint(-1)
98
99#define TRI_TERMINATOR_ID uint(-1)
100#define TRI_VERT uint(-1)
101
102/* for debugging add... */
103#ifndef NDEBUG
104// printf("%u %u %u %u\n", _t[0], _t[1], _t[2], _t[3]);
105# define FACE_ASSERT(face, vert_max) \
106 { \
107 uint *_t = face; \
108 BLI_assert(_t[0] < vert_max); \
109 BLI_assert(_t[1] < vert_max); \
110 BLI_assert(_t[2] < vert_max); \
111 BLI_assert(_t[3] < vert_max || _t[3] == TRI_VERT); \
112 UNUSED_VARS_NDEBUG(vert_max); \
113 } \
114 (void)0
115#else
116/* do nothing */
117# define FACE_ASSERT(face, vert_max)
118#endif
119
120static CLG_LogRef LOG = {"mask.rasterize"};
121
122static void rotate_point_v2(
123 float r_p[2], const float p[2], const float cent[2], const float angle, const float asp[2])
124{
125 const float s = sinf(angle);
126 const float c = cosf(angle);
127 float p_new[2];
128
129 /* translate point back to origin */
130 r_p[0] = (p[0] - cent[0]) / asp[0];
131 r_p[1] = (p[1] - cent[1]) / asp[1];
132
133 /* rotate point */
134 p_new[0] = ((r_p[0] * c) - (r_p[1] * s)) * asp[0];
135 p_new[1] = ((r_p[0] * s) + (r_p[1] * c)) * asp[1];
136
137 /* translate point back */
138 r_p[0] = p_new[0] + cent[0];
139 r_p[1] = p_new[1] + cent[1];
140}
141
143{
144 return v < min ? min : (v > max ? max : v);
145}
146
148 const float co_xy[2],
149 const float co_z)
150{
151 const float co[3] = {co_xy[0], co_xy[1], co_z};
152 return BLI_scanfill_vert_add(sf_ctx, co);
153}
154
155/* --------------------------------------------------------------------- */
156/* local structs for mask rasterizing */
157/* --------------------------------------------------------------------- */
158
168 /* geometry */
170 uint (*face_array)[4]; /* access coords tri/quad */
171 float (*face_coords)[3]; /* xy, z 0-1 (1.0 == filled) */
172
173 /* 2d bounds (to quickly skip bucket lookup) */
175
176 /* buckets */
178 /* cache divide and subtract */
179 float buckets_xy_scalar[2]; /* (1.0 / (buckets_width + FLT_EPSILON)) * buckets_x */
182
183 /* copied direct from #MaskLayer.--- */
184 /* blending options */
185 float alpha;
186 char blend;
189};
190
192 /* body of the spline */
195
196 /* capping for non-filled, non cyclic splines */
199
201};
202
209
210 /* 2d bounds (to quickly skip bucket lookup) */
212};
213
220 const MaskRasterSplineInfo *open_spline_ranges,
221 uint open_spline_index,
222 uint *face,
223 const uint vert_num)
224{
225 UNUSED_VARS_NDEBUG(vert_num);
226 uint quad_num = 0;
227 while (open_spline_index-- > 0) {
228 const uint vertex_offset = open_spline_ranges[open_spline_index].vertex_offset;
229 uint vertex_total = open_spline_ranges[open_spline_index].vertex_total;
230 uint vertex_total_cap_head = open_spline_ranges[open_spline_index].vertex_total_cap_head;
231 uint vertex_total_cap_tail = open_spline_ranges[open_spline_index].vertex_total_cap_tail;
232 uint k, j;
233
234 j = vertex_offset;
235
236 /* subtract one since we reference next vertex triple */
237 for (k = 0; k < vertex_total - 1; k++, j += 3) {
238
239 BLI_assert(j == vertex_offset + (k * 3));
240
241 *(face++) = j + 3; /* next span */ /* z 1 */
242 *(face++) = j + 0; /* z 1 */
243 *(face++) = j + 1; /* z 0 */
244 *(face++) = j + 4; /* next span */ /* z 0 */
245 quad_num++;
246 FACE_ASSERT(face - 4, vert_num);
247
248 *(face++) = j + 0; /* z 1 */
249 *(face++) = j + 3; /* next span */ /* z 1 */
250 *(face++) = j + 5; /* next span */ /* z 0 */
251 *(face++) = j + 2; /* z 0 */
252 quad_num++;
253 FACE_ASSERT(face - 4, vert_num);
254 }
255
256 if (open_spline_ranges[open_spline_index].is_cyclic) {
257 *(face++) = vertex_offset + 0; /* next span */ /* z 1 */
258 *(face++) = j + 0; /* z 1 */
259 *(face++) = j + 1; /* z 0 */
260 *(face++) = vertex_offset + 1; /* next span */ /* z 0 */
261 quad_num++;
262 FACE_ASSERT(face - 4, vert_num);
263
264 *(face++) = j + 0; /* z 1 */
265 *(face++) = vertex_offset + 0; /* next span */ /* z 1 */
266 *(face++) = vertex_offset + 2; /* next span */ /* z 0 */
267 *(face++) = j + 2; /* z 0 */
268 quad_num++;
269 FACE_ASSERT(face - 4, vert_num);
270 }
271 else {
272 uint midvidx = vertex_offset;
273
274 /***************
275 * cap end 'a' */
276 j = midvidx + (vertex_total * 3);
277
278 for (k = 0; k < vertex_total_cap_head - 2; k++, j++) {
279 *(face++) = midvidx + 0; /* z 1 */
280 *(face++) = midvidx + 0; /* z 1 */
281 *(face++) = j + 0; /* z 0 */
282 *(face++) = j + 1; /* z 0 */
283 quad_num++;
284 FACE_ASSERT(face - 4, vert_num);
285 }
286
287 j = vertex_offset + (vertex_total * 3);
288
289 /* 2 tris that join the original */
290 *(face++) = midvidx + 0; /* z 1 */
291 *(face++) = midvidx + 0; /* z 1 */
292 *(face++) = midvidx + 1; /* z 0 */
293 *(face++) = j + 0; /* z 0 */
294 quad_num++;
295 FACE_ASSERT(face - 4, vert_num);
296
297 *(face++) = midvidx + 0; /* z 1 */
298 *(face++) = midvidx + 0; /* z 1 */
299 *(face++) = j + vertex_total_cap_head - 2; /* z 0 */
300 *(face++) = midvidx + 2; /* z 0 */
301 quad_num++;
302 FACE_ASSERT(face - 4, vert_num);
303
304 /***************
305 * cap end 'b' */
306 /* ... same as previous but v 2-3 flipped, and different initial offsets */
307
308 j = vertex_offset + (vertex_total * 3) + (vertex_total_cap_head - 1);
309
310 midvidx = vertex_offset + (vertex_total * 3) - 3;
311
312 for (k = 0; k < vertex_total_cap_tail - 2; k++, j++) {
313 *(face++) = midvidx; /* z 1 */
314 *(face++) = midvidx; /* z 1 */
315 *(face++) = j + 1; /* z 0 */
316 *(face++) = j + 0; /* z 0 */
317 quad_num++;
318 FACE_ASSERT(face - 4, vert_num);
319 }
320
321 j = vertex_offset + (vertex_total * 3) + (vertex_total_cap_head - 1);
322
323 /* 2 tris that join the original */
324 *(face++) = midvidx + 0; /* z 1 */
325 *(face++) = midvidx + 0; /* z 1 */
326 *(face++) = j + 0; /* z 0 */
327 *(face++) = midvidx + 1; /* z 0 */
328 quad_num++;
329 FACE_ASSERT(face - 4, vert_num);
330
331 *(face++) = midvidx + 0; /* z 1 */
332 *(face++) = midvidx + 0; /* z 1 */
333 *(face++) = midvidx + 2; /* z 0 */
334 *(face++) = j + vertex_total_cap_tail - 2; /* z 0 */
335 quad_num++;
336 FACE_ASSERT(face - 4, vert_num);
337 }
338 }
339 return quad_num;
340}
341
342/* --------------------------------------------------------------------- */
343/* alloc / free functions */
344/* --------------------------------------------------------------------- */
345
347{
348 MaskRasterHandle *mr_handle;
349
350 mr_handle = MEM_new<MaskRasterHandle>("MaskRasterHandle");
351
352 return mr_handle;
353}
354
356{
357 const uint layers_tot = mr_handle->layers_tot;
358 MaskRasterLayer *layer = mr_handle->layers;
359
360 for (uint i = 0; i < layers_tot; i++, layer++) {
361
362 if (layer->face_array) {
363 MEM_delete(layer->face_array);
364 }
365
366 if (layer->face_coords) {
367 MEM_delete(layer->face_coords);
368 }
369
370 if (layer->buckets_face) {
371 const uint bucket_tot = layer->buckets_x * layer->buckets_y;
372 uint bucket_index;
373 for (bucket_index = 0; bucket_index < bucket_tot; bucket_index++) {
374 uint *face_index = layer->buckets_face[bucket_index];
375 if (face_index) {
376 MEM_delete(face_index);
377 }
378 }
379
380 MEM_delete(layer->buckets_face);
381 }
382 }
383
384 MEM_delete(mr_handle->layers);
385 MEM_delete(mr_handle);
386}
387
388static void maskrasterize_spline_differentiate_point_outset(float (*diff_feather_points)[2],
389 float (*diff_points)[2],
390 const uint tot_diff_point,
391 const float ofs,
392 const bool do_test)
393{
394 uint k_prev = tot_diff_point - 2;
395 uint k_curr = tot_diff_point - 1;
396 uint k_next = 0;
397
398 uint k;
399
400 float d_prev[2];
401 float d_next[2];
402 float d[2];
403
404 const float *co_prev;
405 const float *co_curr;
406 const float *co_next;
407
408 const float ofs_squared = ofs * ofs;
409
410 co_prev = diff_points[k_prev];
411 co_curr = diff_points[k_curr];
412 co_next = diff_points[k_next];
413
414 /* precalc */
415 sub_v2_v2v2(d_prev, co_prev, co_curr);
416 normalize_v2(d_prev);
417
418 for (k = 0; k < tot_diff_point; k++) {
419
420 // co_prev = diff_points[k_prev]; /* Precalculate. */
421 co_curr = diff_points[k_curr];
422 co_next = diff_points[k_next];
423
424 // sub_v2_v2v2(d_prev, co_prev, co_curr); /* Precalculate. */
425 sub_v2_v2v2(d_next, co_curr, co_next);
426
427 // normalize_v2(d_prev); /* precalc */
428 normalize_v2(d_next);
429
430 if ((do_test == false) ||
431 (len_squared_v2v2(diff_feather_points[k], diff_points[k]) < ofs_squared))
432 {
433
434 add_v2_v2v2(d, d_prev, d_next);
435
436 normalize_v2(d);
437
438 diff_feather_points[k][0] = diff_points[k][0] + (d[1] * ofs);
439 diff_feather_points[k][1] = diff_points[k][1] + (-d[0] * ofs);
440 }
441
442 /* use next iter */
443 copy_v2_v2(d_prev, d_next);
444
445 // k_prev = k_curr; /* Precalculate. */
446 k_curr = k_next;
447 k_next++;
448 }
449}
450
451/* Check if triangle overlaps a bucket cell. This is based on Separating Axis Theorem,
452 * note that it only checks triangle edges against the cell (not cell edges against triangle -
453 * the calling code already only visits cells that are within triangle bounding box). */
454static bool isect_aabb_tri_v2(const float xmin,
455 const float ymin,
456 const float xmax,
457 const float ymax,
458 const float v1[2],
459 const float v2[2],
460 const float v3[2])
461{
462 /* Test each triangle edge. */
463 const float *va[3] = {v1, v2, v3};
464 for (int i = 0; i < 3; i++) {
465 const float *a = va[i];
466 const float *b = va[(i + 1) % 3];
467 const float *c = va[(i + 2) % 3];
468 /* Edge normal. */
469 float nx = -(b[1] - a[1]);
470 float ny = b[0] - a[0];
471 /* Project the opposite vertex along the normal. */
472 float d_c = nx * (c[0] - a[0]) + ny * (c[1] - a[1]);
473 /* Project the cell box using the p-vertex (max along normal) and n-vertex (min along normal);
474 * pick the p/n vertices based on normal component signs. */
475 float box_max = nx * (nx >= 0.0f ? xmax : xmin) - nx * a[0] + ny * (ny >= 0.0f ? ymax : ymin) -
476 ny * a[1];
477 float box_min = nx * (nx >= 0.0f ? xmin : xmax) - nx * a[0] + ny * (ny >= 0.0f ? ymin : ymax) -
478 ny * a[1];
479 /* No intersection when intervals do not overlap. */
480 if (box_max < fminf(0.0f, d_c) || box_min > fmaxf(0.0f, d_c)) {
481 return false;
482 }
483 }
484 return true;
485}
486
487/* Check if face overlaps the bucket cell. */
489 uint face_index,
490 const uint bucket_x,
491 const uint bucket_y,
492 const float bucket_size_x,
493 const float bucket_size_y)
494{
495 const uint *face = layer->face_array[face_index];
496 const float (*cos)[3] = layer->face_coords;
497
498 const float xmin = layer->bounds.xmin + (bucket_size_x * float(bucket_x));
499 const float ymin = layer->bounds.ymin + (bucket_size_y * float(bucket_y));
500 const float xmax = xmin + bucket_size_x;
501 const float ymax = ymin + bucket_size_y;
502
503 const float *v1 = cos[face[0]];
504 const float *v2 = cos[face[1]];
505 const float *v3 = cos[face[2]];
506 if (isect_aabb_tri_v2(xmin, ymin, xmax, ymax, v1, v2, v3)) {
507 return true;
508 }
509 if (face[3] != TRI_VERT) {
510 const float *v4 = cos[face[3]];
511 if (isect_aabb_tri_v2(xmin, ymin, xmax, ymax, v1, v3, v4)) {
512 return true;
513 }
514 }
515 return false;
516}
517
519 const MaskLayer *masklay)
520{
521 layer->alpha = masklay->alpha;
522 layer->blend = masklay->blend;
523 layer->blend_flag = masklay->blend_flag;
524 layer->falloff = masklay->falloff;
525}
526
528{
529 layer->face_tot = 0;
530 layer->face_coords = nullptr;
531 layer->face_array = nullptr;
532
533 layer->buckets_x = 0;
534 layer->buckets_y = 0;
535
536 layer->buckets_xy_scalar[0] = 0.0f;
537 layer->buckets_xy_scalar[1] = 0.0f;
538
539 layer->buckets_face = nullptr;
540
541 BLI_rctf_init(&layer->bounds, -1.0f, -1.0f, -1.0f, -1.0f);
542}
543
544static void layer_bucket_init(MaskRasterLayer *layer, const float pixel_size)
545{
546 MemArena *arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), __func__);
547
548 const float bucket_dim_x = BLI_rctf_size_x(&layer->bounds);
549 const float bucket_dim_y = BLI_rctf_size_y(&layer->bounds);
550
551 layer->buckets_x = uint((bucket_dim_x / pixel_size) / float(BUCKET_PIXELS_PER_CELL));
552 layer->buckets_y = uint((bucket_dim_y / pixel_size) / float(BUCKET_PIXELS_PER_CELL));
553
554 // printf("bucket size %ux%u\n", layer->buckets_x, layer->buckets_y);
555
556 CLAMP(layer->buckets_x, 8, 512);
557 CLAMP(layer->buckets_y, 8, 512);
558
559 layer->buckets_xy_scalar[0] = (1.0f / (bucket_dim_x + FLT_EPSILON)) * float(layer->buckets_x);
560 layer->buckets_xy_scalar[1] = (1.0f / (bucket_dim_y + FLT_EPSILON)) * float(layer->buckets_y);
561
562 {
563 /* width and height of each bucket */
564 const float bucket_size_x = (bucket_dim_x + FLT_EPSILON) / float(layer->buckets_x);
565 const float bucket_size_y = (bucket_dim_y + FLT_EPSILON) / float(layer->buckets_y);
566
567 uint *face = &layer->face_array[0][0];
568 float (*cos)[3] = layer->face_coords;
569
570 const uint bucket_tot = layer->buckets_x * layer->buckets_y;
571 LinkNode **bucketstore = MEM_new_array_zeroed<LinkNode *>(bucket_tot, __func__);
572 uint *bucketstore_tot = MEM_new_array_zeroed<uint>(bucket_tot, __func__);
573
574 uint face_index;
575
576 for (face_index = 0; face_index < layer->face_tot; face_index++, face += 4) {
577 float xmin;
578 float xmax;
579 float ymin;
580 float ymax;
581
582 if (face[3] == TRI_VERT) {
583 const float *v1 = cos[face[0]];
584 const float *v2 = cos[face[1]];
585 const float *v3 = cos[face[2]];
586
587 xmin = min_ff(v1[0], min_ff(v2[0], v3[0]));
588 xmax = max_ff(v1[0], max_ff(v2[0], v3[0]));
589 ymin = min_ff(v1[1], min_ff(v2[1], v3[1]));
590 ymax = max_ff(v1[1], max_ff(v2[1], v3[1]));
591 }
592 else {
593 const float *v1 = cos[face[0]];
594 const float *v2 = cos[face[1]];
595 const float *v3 = cos[face[2]];
596 const float *v4 = cos[face[3]];
597
598 xmin = min_ff(v1[0], min_ff(v2[0], min_ff(v3[0], v4[0])));
599 xmax = max_ff(v1[0], max_ff(v2[0], max_ff(v3[0], v4[0])));
600 ymin = min_ff(v1[1], min_ff(v2[1], min_ff(v3[1], v4[1])));
601 ymax = max_ff(v1[1], max_ff(v2[1], max_ff(v3[1], v4[1])));
602 }
603
604 /* not essential but may as will skip any faces outside the view */
605 if (!((xmax < 0.0f) || (ymax < 0.0f) || (xmin > 1.0f) || (ymin > 1.0f))) {
606
607 CLAMP(xmin, 0.0f, 1.0f);
608 CLAMP(ymin, 0.0f, 1.0f);
609 CLAMP(xmax, 0.0f, 1.0f);
610 CLAMP(ymax, 0.0f, 1.0f);
611
612 {
613 uint xi_min = uint((xmin - layer->bounds.xmin) * layer->buckets_xy_scalar[0]);
614 uint xi_max = uint((xmax - layer->bounds.xmin) * layer->buckets_xy_scalar[0]);
615 uint yi_min = uint((ymin - layer->bounds.ymin) * layer->buckets_xy_scalar[1]);
616 uint yi_max = uint((ymax - layer->bounds.ymin) * layer->buckets_xy_scalar[1]);
617 void *face_index_void = POINTER_FROM_UINT(face_index);
618
619 uint xi, yi;
620
621 /* this should _almost_ never happen but since it can in extreme cases,
622 * we have to clamp the values or we overrun the buffer and crash */
623 if (xi_min >= layer->buckets_x) {
624 xi_min = layer->buckets_x - 1;
625 }
626 if (xi_max >= layer->buckets_x) {
627 xi_max = layer->buckets_x - 1;
628 }
629 if (yi_min >= layer->buckets_y) {
630 yi_min = layer->buckets_y - 1;
631 }
632 if (yi_max >= layer->buckets_y) {
633 yi_max = layer->buckets_y - 1;
634 }
635
636 for (yi = yi_min; yi <= yi_max; yi++) {
637 uint bucket_index = (layer->buckets_x * yi) + xi_min;
638 for (xi = xi_min; xi <= xi_max; xi++, bucket_index++) {
639
640 BLI_assert(xi < layer->buckets_x);
641 BLI_assert(yi < layer->buckets_y);
642 BLI_assert(bucket_index < bucket_tot);
643
644 /* Check if the bucket intersects with the face. */
645 if (layer_bucket_isect_test(layer, face_index, xi, yi, bucket_size_x, bucket_size_y))
646 {
647 BLI_linklist_prepend_arena(&bucketstore[bucket_index], face_index_void, arena);
648 bucketstore_tot[bucket_index]++;
649 }
650 }
651 }
652 }
653 }
654 }
655
656 if (true) {
657 /* Now convert link-nodes into arrays for faster per pixel access. */
658 uint **buckets_face = MEM_new_array_zeroed<uint *>(bucket_tot, __func__);
659 uint bucket_index;
660
661 for (bucket_index = 0; bucket_index < bucket_tot; bucket_index++) {
662 if (bucketstore_tot[bucket_index]) {
663 uint *bucket = MEM_new_array_zeroed<uint>((bucketstore_tot[bucket_index] + 1), __func__);
664 LinkNode *bucket_node;
665
666 buckets_face[bucket_index] = bucket;
667
668 for (bucket_node = bucketstore[bucket_index]; bucket_node;
669 bucket_node = bucket_node->next)
670 {
671 *bucket = POINTER_AS_UINT(bucket_node->link);
672 bucket++;
673 }
674 *bucket = TRI_TERMINATOR_ID;
675 }
676 else {
677 buckets_face[bucket_index] = nullptr;
678 }
679 }
680
681 layer->buckets_face = buckets_face;
682 }
683
684 MEM_delete(bucketstore);
685 MEM_delete(bucketstore_tot);
686 }
687
688 BLI_memarena_free(arena);
689}
690
692 MaskLayer *masklay,
693 uint masklay_index,
694 const rctf &default_bounds,
695 const float pixel_size,
696 const float asp_xy[2],
697 const int width,
698 const int height,
699 const bool do_aspect_correct,
700 const bool do_mask_aa,
701 const bool do_feather,
702 MemArena *sf_arena)
703{
704 const float zvec[3] = {0.0f, 0.0f, -1.0f};
705
706 /* we need to store vertex ranges for open splines for filling */
707 uint tot_splines;
708 MaskRasterSplineInfo *open_spline_ranges;
709 uint open_spline_index = 0;
710
711 /* scanfill */
712 ScanFillContext sf_ctx;
713 ScanFillVert *sf_vert = nullptr;
714 ScanFillVert *sf_vert_next = nullptr;
715 ScanFillFace *sf_tri;
716
717 uint vert_num = 0;
718 uint tot_feather_quads = 0;
719
720#ifdef USE_SCANFILL_EDGE_WORKAROUND
721 uint tot_boundary_used = 0;
722 uint tot_boundary_found = 0;
723#endif
724
725 tot_splines = uint(masklay->splines.count());
726 open_spline_ranges = MEM_new_array<MaskRasterSplineInfo>(tot_splines, __func__);
727
728 BLI_scanfill_begin_arena(&sf_ctx, sf_arena);
729
730 for (MaskSpline &spline : masklay->splines) {
731 const bool is_cyclic = (spline.flag & MASK_SPLINE_CYCLIC) != 0;
732 const bool is_fill = (spline.flag & MASK_SPLINE_NOFILL) == 0;
733
734 float (*diff_points)[2];
735 uint tot_diff_point;
736
737 float (*diff_feather_points)[2];
738 float (*diff_feather_points_flip)[2];
739 uint tot_diff_feather_points;
740
741 const uint resol_a = uint(BKE_mask_spline_resolution(&spline, width, height) / 4);
742 const uint resol_b = BKE_mask_spline_feather_resolution(&spline, width, height) / 4;
743 const uint resol = std::clamp(std::max(resol_a, resol_b), 4u, 512u);
744
745 diff_points = BKE_mask_spline_differentiate_with_resolution(&spline, resol, &tot_diff_point);
746
747 if (do_feather) {
749 &spline, resol, false, &tot_diff_feather_points);
750 BLI_assert(diff_feather_points);
751 }
752 else {
753 tot_diff_feather_points = 0;
754 diff_feather_points = nullptr;
755 }
756
757 if (tot_diff_point > 3) {
758 ScanFillVert *sf_vert_prev;
759 uint j;
760
761 sf_ctx.poly_nr++;
762
763 if (do_aspect_correct) {
764 if (width != height) {
765 float *fp;
766 float *ffp;
767 float asp;
768
769 if (width < height) {
770 fp = &diff_points[0][0];
771 ffp = tot_diff_feather_points ? &diff_feather_points[0][0] : nullptr;
772 asp = float(width) / float(height);
773 }
774 else {
775 fp = &diff_points[0][1];
776 ffp = tot_diff_feather_points ? &diff_feather_points[0][1] : nullptr;
777 asp = float(height) / float(width);
778 }
779
780 for (uint i = 0; i < tot_diff_point; i++, fp += 2) {
781 (*fp) = (((*fp) - 0.5f) / asp) + 0.5f;
782 }
783
784 if (tot_diff_feather_points) {
785 for (uint i = 0; i < tot_diff_feather_points; i++, ffp += 2) {
786 (*ffp) = (((*ffp) - 0.5f) / asp) + 0.5f;
787 }
788 }
789 }
790 }
791
792 /* fake aa, using small feather */
793 if (do_mask_aa == true) {
794 if (do_feather == false) {
795 tot_diff_feather_points = tot_diff_point;
796 diff_feather_points = MEM_new_array_zeroed<float[2]>(tot_diff_feather_points, __func__);
797 /* add single pixel feather */
799 diff_feather_points, diff_points, tot_diff_point, pixel_size, false);
800 }
801 else {
802 /* ensure single pixel feather, on any zero feather areas */
804 diff_feather_points, diff_points, tot_diff_point, pixel_size, true);
805 }
806 }
807
808 if (is_fill) {
809 /* Apply intersections depending on fill settings. */
810 if (spline.flag & MASK_SPLINE_NOINTERSECT) {
812 &spline, diff_feather_points, tot_diff_feather_points);
813 }
814
815 sf_vert_prev = scanfill_vert_add_v2_with_depth(&sf_ctx, diff_points[0], 0.0f);
816 sf_vert_prev->tmp.u = vert_num;
817
818 /* Absolute index of feather vert. */
819 sf_vert_prev->keyindex = vert_num + tot_diff_point;
820
821 vert_num++;
822
823 for (j = 1; j < tot_diff_point; j++) {
824 sf_vert = scanfill_vert_add_v2_with_depth(&sf_ctx, diff_points[j], 0.0f);
825 sf_vert->tmp.u = vert_num;
826 sf_vert->keyindex = vert_num + tot_diff_point; /* absolute index of feather vert */
827 vert_num++;
828 }
829
830 sf_vert = sf_vert_prev;
831 sf_vert_prev = sf_ctx.fillvertbase.last();
832
833 for (j = 0; j < tot_diff_point; j++) {
834 ScanFillEdge *sf_edge = BLI_scanfill_edge_add(&sf_ctx, sf_vert_prev, sf_vert);
835
836#ifdef USE_SCANFILL_EDGE_WORKAROUND
837 if (diff_feather_points) {
838 sf_edge->tmp.c = SF_EDGE_IS_BOUNDARY;
839 tot_boundary_used++;
840 }
841#else
842 (void)sf_edge;
843#endif
844 sf_vert_prev = sf_vert;
845 sf_vert = sf_vert->next;
846 }
847
848 if (diff_feather_points) {
849 BLI_assert(tot_diff_feather_points == tot_diff_point);
850
851 /* NOTE: only added for convenience, we don't in fact use these to scan-fill,
852 * only to create feather faces after scan-fill. */
853 for (j = 0; j < tot_diff_feather_points; j++) {
854 sf_vert = scanfill_vert_add_v2_with_depth(&sf_ctx, diff_feather_points[j], 1.0f);
855 sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
856 vert_num++;
857 }
858
859 tot_feather_quads += tot_diff_point;
860 }
861 }
862 else {
863 /* unfilled spline */
864 if (diff_feather_points) {
865
866 if (spline.flag & MASK_SPLINE_NOINTERSECT) {
867 diff_feather_points_flip = MEM_new_array_zeroed<float[2]>(tot_diff_feather_points,
868 "diff_feather_points_flip");
869
870 float co_diff[2];
871 for (j = 0; j < tot_diff_point; j++) {
872 sub_v2_v2v2(co_diff, diff_points[j], diff_feather_points[j]);
873 add_v2_v2v2(diff_feather_points_flip[j], diff_points[j], co_diff);
874 }
875
877 &spline, diff_feather_points, tot_diff_feather_points);
879 &spline, diff_feather_points_flip, tot_diff_feather_points);
880 }
881 else {
882 diff_feather_points_flip = nullptr;
883 }
884
885 open_spline_ranges[open_spline_index].vertex_offset = vert_num;
886 open_spline_ranges[open_spline_index].vertex_total = tot_diff_point;
887
888 /* TODO: an alternate functions so we can avoid double vector copy! */
889 for (j = 0; j < tot_diff_point; j++) {
890
891 /* center vert */
892 sf_vert = scanfill_vert_add_v2_with_depth(&sf_ctx, diff_points[j], 0.0f);
893 sf_vert->tmp.u = vert_num;
894 sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
895 vert_num++;
896
897 /* feather vert A */
898 sf_vert = scanfill_vert_add_v2_with_depth(&sf_ctx, diff_feather_points[j], 1.0f);
899 sf_vert->tmp.u = vert_num;
900 sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
901 vert_num++;
902
903 /* feather vert B */
904 if (diff_feather_points_flip) {
906 &sf_ctx, diff_feather_points_flip[j], 1.0f);
907 }
908 else {
909 float co_diff[2];
910 sub_v2_v2v2(co_diff, diff_points[j], diff_feather_points[j]);
911 add_v2_v2v2(co_diff, diff_points[j], co_diff);
912 sf_vert = scanfill_vert_add_v2_with_depth(&sf_ctx, co_diff, 1.0f);
913 }
914
915 sf_vert->tmp.u = vert_num;
916 sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
917 vert_num++;
918
919 tot_feather_quads += 2;
920 }
921
922 if (!is_cyclic) {
923 tot_feather_quads -= 2;
924 }
925
926 if (diff_feather_points_flip) {
927 MEM_delete(diff_feather_points_flip);
928 diff_feather_points_flip = nullptr;
929 }
930
931 /* cap ends */
932
933 /* dummy init value */
934 open_spline_ranges[open_spline_index].vertex_total_cap_head = 0;
935 open_spline_ranges[open_spline_index].vertex_total_cap_tail = 0;
936
937 if (!is_cyclic) {
938 const float *fp_cent;
939 const float *fp_turn;
940
941 uint k;
942
943 fp_cent = diff_points[0];
944 fp_turn = diff_feather_points[0];
945
946#define CALC_CAP_RESOL \
947 clampis_uint(uint(len_v2v2(fp_cent, fp_turn) / (pixel_size * SPLINE_RESOL_CAP_PER_PIXEL)), \
948 SPLINE_RESOL_CAP_MIN, \
949 SPLINE_RESOL_CAP_MAX)
950
951 {
952 const uint vertex_total_cap = CALC_CAP_RESOL;
953
954 for (k = 1; k < vertex_total_cap; k++) {
955 const float angle = float(k) * (1.0f / float(vertex_total_cap)) * float(M_PI);
956 float co_feather[2];
957 rotate_point_v2(co_feather, fp_turn, fp_cent, angle, asp_xy);
958
959 sf_vert = scanfill_vert_add_v2_with_depth(&sf_ctx, co_feather, 1.0f);
960 sf_vert->tmp.u = vert_num;
961 sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
962 vert_num++;
963 }
964 tot_feather_quads += vertex_total_cap;
965
966 open_spline_ranges[open_spline_index].vertex_total_cap_head = vertex_total_cap;
967 }
968
969 fp_cent = diff_points[tot_diff_point - 1];
970 fp_turn = diff_feather_points[tot_diff_point - 1];
971
972 {
973 const uint vertex_total_cap = CALC_CAP_RESOL;
974
975 for (k = 1; k < vertex_total_cap; k++) {
976 const float angle = float(k) * (1.0f / float(vertex_total_cap)) * float(M_PI);
977 float co_feather[2];
978 rotate_point_v2(co_feather, fp_turn, fp_cent, -angle, asp_xy);
979
980 sf_vert = scanfill_vert_add_v2_with_depth(&sf_ctx, co_feather, 1.0f);
981 sf_vert->tmp.u = vert_num;
982 sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
983 vert_num++;
984 }
985 tot_feather_quads += vertex_total_cap;
986
987 open_spline_ranges[open_spline_index].vertex_total_cap_tail = vertex_total_cap;
988 }
989 }
990
991 open_spline_ranges[open_spline_index].is_cyclic = is_cyclic;
992 open_spline_index++;
993
994#undef CALC_CAP_RESOL
995 /* end capping */
996 }
997 }
998 }
999
1000 if (diff_points) {
1001 MEM_delete(diff_points);
1002 }
1003
1004 if (diff_feather_points) {
1005 MEM_delete(diff_feather_points);
1006 }
1007 }
1008
1009 {
1010 uint(*face_array)[4], *face; /* access coords */
1011 float (*face_coords)[3], *cos; /* xy, z 0-1 (1.0 == filled) */
1012 uint sf_tri_tot;
1013 rctf bounds;
1014 uint face_index;
1015 int scanfill_flag = 0;
1016
1017 bool is_isect = false;
1018 ListBaseT<ScanFillVert> isect_remvertbase = {nullptr, nullptr};
1019 ListBaseT<ScanFillEdge> isect_remedgebase = {nullptr, nullptr};
1020
1021 /* now we have all the splines */
1022 face_coords = MEM_new_array_zeroed<float[3]>(vert_num, "maskrast_face_coords");
1023
1024 /* init bounds */
1026
1027 /* coords */
1028 cos = reinterpret_cast<float *>(face_coords);
1029 for (sf_vert = sf_ctx.fillvertbase.first(); sf_vert; sf_vert = sf_vert_next) {
1030 sf_vert_next = sf_vert->next;
1031 copy_v3_v3(cos, sf_vert->co);
1032
1033 /* remove so as not to interfere with fill (called after) */
1034 if (sf_vert->keyindex == SF_KEYINDEX_TEMP_ID) {
1035 BLI_remlink(&sf_ctx.fillvertbase, sf_vert);
1036 }
1037
1038 /* bounds */
1040
1041 cos += 3;
1042 }
1043
1044 /* --- inefficient self-intersect case --- */
1045 /* if self intersections are found, its too tricky to attempt to map vertices
1046 * so just realloc and add entirely new vertices - the result of the self-intersect check.
1047 */
1048 if ((masklay->flag & MASK_LAYERFLAG_FILL_OVERLAP) &&
1049 (is_isect = BLI_scanfill_calc_self_isect(&sf_ctx, &isect_remvertbase, &isect_remedgebase)))
1050 {
1051 uint sf_vert_tot_isect = uint(sf_ctx.fillvertbase.count());
1052 uint i = vert_num;
1053
1054 face_coords = static_cast<float (*)[3]>(MEM_realloc_uninitialized(
1055 face_coords, sizeof(float[3]) * (vert_num + sf_vert_tot_isect)));
1056
1057 cos = (&face_coords[vert_num][0]);
1058
1059 for (sf_vert = sf_ctx.fillvertbase.first(); sf_vert; sf_vert = sf_vert->next) {
1060 copy_v3_v3(cos, sf_vert->co);
1061 sf_vert->tmp.u = i++;
1062 cos += 3;
1063 }
1064
1065 vert_num += sf_vert_tot_isect;
1066
1067 /* we need to calc polys after self intersect */
1068 scanfill_flag |= BLI_SCANFILL_CALC_POLYS;
1069 }
1070 /* --- end inefficient code --- */
1071
1072 /* main scan-fill */
1073 if ((masklay->flag & MASK_LAYERFLAG_FILL_DISCRETE) == 0) {
1074 scanfill_flag |= BLI_SCANFILL_CALC_HOLES;
1075 }
1076
1077 /* Store an array of edges from `sf_ctx.filledgebase`
1078 * because filling may remove edges, see: #127692. */
1079 ScanFillEdge **sf_edge_array = nullptr;
1080 uint sf_edge_array_num = 0;
1081 if (tot_feather_quads) {
1082 const ListBaseT<ScanFillEdge> *lb_array[] = {&sf_ctx.filledgebase, &isect_remedgebase};
1083 for (int pass = 0; pass < 2; pass++) {
1084 for (ScanFillEdge &sf_edge : *lb_array[pass]) {
1085 if (sf_edge.tmp.c == SF_EDGE_IS_BOUNDARY) {
1086 sf_edge_array_num += 1;
1087 }
1088 }
1089 }
1090
1091 if (sf_edge_array_num > 0) {
1092 sf_edge_array = MEM_new_array_uninitialized<ScanFillEdge *>(size_t(sf_edge_array_num),
1093 __func__);
1094 uint edge_index = 0;
1095 for (int pass = 0; pass < 2; pass++) {
1096 for (ScanFillEdge &sf_edge : *lb_array[pass]) {
1097 if (sf_edge.tmp.c == SF_EDGE_IS_BOUNDARY) {
1098 sf_edge_array[edge_index++] = &sf_edge;
1099 }
1100 }
1101 }
1102 BLI_assert(edge_index == sf_edge_array_num);
1103 }
1104 }
1105
1106 sf_tri_tot = uint(BLI_scanfill_calc_ex(&sf_ctx, scanfill_flag, zvec));
1107
1108 if (is_isect) {
1109 /* add removed data back, we only need edges for feather,
1110 * but add verts back so they get freed along with others */
1111 BLI_movelisttolist(&sf_ctx.fillvertbase, &isect_remvertbase);
1112 BLI_movelisttolist(&sf_ctx.filledgebase, &isect_remedgebase);
1113 }
1114
1116 size_t(sf_tri_tot) + size_t(tot_feather_quads), "maskrast_face_index");
1117 face_index = 0;
1118
1119 /* faces */
1120 face = reinterpret_cast<uint *>(face_array);
1121 for (sf_tri = sf_ctx.fillfacebase.first(); sf_tri; sf_tri = sf_tri->next) {
1122 *(face++) = sf_tri->v3->tmp.u;
1123 *(face++) = sf_tri->v2->tmp.u;
1124 *(face++) = sf_tri->v1->tmp.u;
1125 *(face++) = TRI_VERT;
1126 face_index++;
1127 FACE_ASSERT(face - 4, vert_num);
1128 }
1129
1130 /* start of feather faces... if we have this set,
1131 * 'face_index' is kept from loop above */
1132
1133 BLI_assert(face_index == sf_tri_tot);
1134 UNUSED_VARS_NDEBUG(face_index);
1135
1136 if (sf_edge_array) {
1137 BLI_assert(tot_feather_quads);
1138 for (uint i = 0; i < sf_edge_array_num; i++) {
1139 ScanFillEdge *sf_edge = sf_edge_array[i];
1140 BLI_assert(sf_edge->tmp.c == SF_EDGE_IS_BOUNDARY);
1141 *(face++) = sf_edge->v1->tmp.u;
1142 *(face++) = sf_edge->v2->tmp.u;
1143 *(face++) = sf_edge->v2->keyindex;
1144 *(face++) = sf_edge->v1->keyindex;
1145 face_index++;
1146 FACE_ASSERT(face - 4, vert_num);
1147
1148#ifdef USE_SCANFILL_EDGE_WORKAROUND
1149 tot_boundary_found++;
1150#endif
1151 }
1152 MEM_delete(sf_edge_array);
1153 }
1154
1155#ifdef USE_SCANFILL_EDGE_WORKAROUND
1156 if (tot_boundary_found != tot_boundary_used) {
1157 BLI_assert(tot_boundary_found < tot_boundary_used);
1158 }
1159#endif
1160
1161 {
1163 open_spline_ranges, open_spline_index, face, vert_num);
1164 face += open_quad_num * 4;
1165 face_index += open_quad_num;
1166 }
1167
1168 MEM_delete(open_spline_ranges);
1169
1170#if 0
1171 fprintf(stderr,
1172 "%u %u (%u %u), %u\n",
1173 face_index,
1174 sf_tri_tot + tot_feather_quads,
1175 sf_tri_tot,
1176 tot_feather_quads,
1177 tot_boundary_used - tot_boundary_found);
1178#endif
1179
1180#ifdef USE_SCANFILL_EDGE_WORKAROUND
1181 BLI_assert(face_index + (tot_boundary_used - tot_boundary_found) ==
1182 sf_tri_tot + tot_feather_quads);
1183#else
1184 BLI_assert(face_index == sf_tri_tot + tot_feather_quads);
1185#endif
1186 {
1187 MaskRasterLayer *layer = &mr_handle->layers[masklay_index];
1189
1190 if (BLI_rctf_isect(&default_bounds, &bounds, &bounds)) {
1191#ifdef USE_SCANFILL_EDGE_WORKAROUND
1192 layer->face_tot = (sf_tri_tot + tot_feather_quads) -
1193 (tot_boundary_used - tot_boundary_found);
1194#else
1195 layer->face_tot = (sf_tri_tot + tot_feather_quads);
1196#endif
1197 layer->face_coords = face_coords;
1198 layer->face_array = face_array;
1199 layer->bounds = bounds;
1200
1201 layer_bucket_init(layer, pixel_size);
1202
1203 BLI_rctf_union(&mr_handle->bounds, &bounds);
1204 }
1205 else {
1206 MEM_delete(face_coords);
1207 MEM_delete(face_array);
1208
1210 }
1211 }
1212
1213 // printf("tris %d, feather tris %d\n", sf_tri_tot, tot_feather_quads);
1214 }
1215
1216 /* Add triangles. */
1217 BLI_scanfill_end_arena(&sf_ctx, sf_arena);
1218}
1219
1221 MaskLayer *masklay,
1222 uint masklay_index,
1223 const rctf &default_bounds,
1224 const float pixel_size,
1225 const float asp_xy[2],
1226 const int width,
1227 const int height,
1228 const bool do_aspect_correct,
1229 const bool do_mask_aa,
1230 const bool do_feather)
1231{
1233 struct FeatherRange {
1234 int vert_start;
1235 int vert_count;
1236 int feather_offset;
1237 };
1238
1239 /* we need to store vertex ranges for open splines for filling */
1240 uint tot_splines;
1241 MaskRasterSplineInfo *open_spline_ranges;
1242 uint open_spline_index = 0;
1243
1244 uint tot_feather_quads = 0;
1245
1246 tot_splines = uint(masklay->splines.count());
1247 open_spline_ranges = MEM_new_array<MaskRasterSplineInfo>(tot_splines, __func__);
1248
1249 /* CDT input buffers. */
1250 Vector<double2> cdt_verts;
1251 Vector<int> cdt_face_offsets;
1252 Vector<int> cdt_face_vert_indices;
1253 Vector<float2> feather_coords;
1254 Vector<FeatherRange> feather_ranges;
1255
1256 /* Open-spline vert buffer (body + feather_a + feather_b interleaved, plus caps). */
1257 Vector<float3> open_spline_verts;
1258
1259 /* First: collect CDT input + open-spline verts. */
1260
1261 for (MaskSpline &spline : masklay->splines) {
1262 const bool is_cyclic = (spline.flag & MASK_SPLINE_CYCLIC) != 0;
1263 const bool is_fill = (spline.flag & MASK_SPLINE_NOFILL) == 0;
1264
1265 float (*diff_points)[2];
1266 uint tot_diff_point;
1267
1268 float (*diff_feather_points)[2];
1269 float (*diff_feather_points_flip)[2];
1270 uint tot_diff_feather_points;
1271
1272 const uint resol_a = uint(BKE_mask_spline_resolution(&spline, width, height) / 4);
1273 const uint resol_b = BKE_mask_spline_feather_resolution(&spline, width, height) / 4;
1274 const uint resol = std::clamp(std::max(resol_a, resol_b), 4u, 512u);
1275
1276 diff_points = BKE_mask_spline_differentiate_with_resolution(&spline, resol, &tot_diff_point);
1277
1278 if (do_feather) {
1280 &spline, resol, false, &tot_diff_feather_points);
1281 BLI_assert(diff_feather_points);
1282 }
1283 else {
1284 tot_diff_feather_points = 0;
1285 diff_feather_points = nullptr;
1286 }
1287
1288 if (tot_diff_point > 3) {
1289 uint j;
1290
1291 if (do_aspect_correct) {
1292 if (width != height) {
1293 float *fp;
1294 float *ffp;
1295 float asp;
1296
1297 if (width < height) {
1298 fp = &diff_points[0][0];
1299 ffp = tot_diff_feather_points ? &diff_feather_points[0][0] : nullptr;
1300 asp = float(width) / float(height);
1301 }
1302 else {
1303 fp = &diff_points[0][1];
1304 ffp = tot_diff_feather_points ? &diff_feather_points[0][1] : nullptr;
1305 asp = float(height) / float(width);
1306 }
1307
1308 for (uint i = 0; i < tot_diff_point; i++, fp += 2) {
1309 (*fp) = (((*fp) - 0.5f) / asp) + 0.5f;
1310 }
1311
1312 if (tot_diff_feather_points) {
1313 for (uint i = 0; i < tot_diff_feather_points; i++, ffp += 2) {
1314 (*ffp) = (((*ffp) - 0.5f) / asp) + 0.5f;
1315 }
1316 }
1317 }
1318 }
1319
1320 /* fake aa, using small feather */
1321 if (do_mask_aa == true) {
1322 if (do_feather == false) {
1323 tot_diff_feather_points = tot_diff_point;
1324 diff_feather_points = MEM_new_array_zeroed<float[2]>(tot_diff_feather_points, __func__);
1325 /* add single pixel feather */
1327 diff_feather_points, diff_points, tot_diff_point, pixel_size, false);
1328 }
1329 else {
1330 /* ensure single pixel feather, on any zero feather areas */
1332 diff_feather_points, diff_points, tot_diff_point, pixel_size, true);
1333 }
1334 }
1335
1336 if (is_fill) {
1337 /* Apply intersections depending on fill settings. */
1338 if (spline.flag & MASK_SPLINE_NOINTERSECT) {
1340 &spline, diff_feather_points, tot_diff_feather_points);
1341 }
1342
1343 /* Append body verts to CDT input. */
1344 int spline_vert_start = int(cdt_verts.size());
1345 for (j = 0; j < tot_diff_point; j++) {
1346 cdt_verts.append(double2(diff_points[j][0], diff_points[j][1]));
1347 }
1348
1349 /* Polygon for CDT. */
1350 const int poly_num = int(tot_diff_point);
1351 cdt_face_offsets.append(int(cdt_face_vert_indices.size()));
1352 for (const int64_t i : IndexRange(int64_t(poly_num))) {
1353 cdt_face_vert_indices.append(int(spline_vert_start + i));
1354 }
1355
1356 /* Feather: only when feather points exist. */
1357 if (diff_feather_points) {
1358 BLI_assert(tot_diff_feather_points == tot_diff_point);
1359 int feather_offset = int(feather_coords.size());
1360 for (j = 0; j < tot_diff_feather_points; j++) {
1361 feather_coords.append(float2(diff_feather_points[j][0], diff_feather_points[j][1]));
1362 }
1363 feather_ranges.append({spline_vert_start, int(tot_diff_point), feather_offset});
1364 tot_feather_quads += tot_diff_point;
1365 }
1366 }
1367 else {
1368 /* unfilled spline */
1369 if (diff_feather_points) {
1370
1371 if (spline.flag & MASK_SPLINE_NOINTERSECT) {
1372 diff_feather_points_flip = MEM_new_array_zeroed<float[2]>(tot_diff_feather_points,
1373 "diff_feather_points_flip");
1374
1375 float co_diff[2];
1376 for (j = 0; j < tot_diff_point; j++) {
1377 sub_v2_v2v2(co_diff, diff_points[j], diff_feather_points[j]);
1378 add_v2_v2v2(diff_feather_points_flip[j], diff_points[j], co_diff);
1379 }
1380
1382 &spline, diff_feather_points, tot_diff_feather_points);
1384 &spline, diff_feather_points_flip, tot_diff_feather_points);
1385 }
1386 else {
1387 diff_feather_points_flip = nullptr;
1388 }
1389
1390 uint open_spline_vert_start = uint(open_spline_verts.size());
1391 open_spline_ranges[open_spline_index].vertex_offset = open_spline_vert_start;
1392 open_spline_ranges[open_spline_index].vertex_total = tot_diff_point;
1393
1394 for (j = 0; j < tot_diff_point; j++) {
1395 /* center vert */
1396 open_spline_verts.append(float3(diff_points[j][0], diff_points[j][1], 0.0f));
1397
1398 /* feather vert A */
1399 open_spline_verts.append(
1400 float3(diff_feather_points[j][0], diff_feather_points[j][1], 1.0f));
1401
1402 /* feather vert B */
1403 if (diff_feather_points_flip) {
1404 open_spline_verts.append(
1405 float3(diff_feather_points_flip[j][0], diff_feather_points_flip[j][1], 1.0f));
1406 }
1407 else {
1408 float co_diff[2];
1409 sub_v2_v2v2(co_diff, diff_points[j], diff_feather_points[j]);
1410 add_v2_v2v2(co_diff, diff_points[j], co_diff);
1411 open_spline_verts.append(float3(co_diff[0], co_diff[1], 1.0f));
1412 }
1413
1414 tot_feather_quads += 2;
1415 }
1416
1417 if (!is_cyclic) {
1418 tot_feather_quads -= 2;
1419 }
1420
1421 if (diff_feather_points_flip) {
1422 MEM_delete(diff_feather_points_flip);
1423 diff_feather_points_flip = nullptr;
1424 }
1425
1426 /* cap ends */
1427
1428 /* dummy init value */
1429 open_spline_ranges[open_spline_index].vertex_total_cap_head = 0;
1430 open_spline_ranges[open_spline_index].vertex_total_cap_tail = 0;
1431
1432 if (!is_cyclic) {
1433 const float *fp_cent;
1434 const float *fp_turn;
1435
1436 uint k;
1437
1438 fp_cent = diff_points[0];
1439 fp_turn = diff_feather_points[0];
1440
1441#define CALC_CAP_RESOL \
1442 clampis_uint(uint(len_v2v2(fp_cent, fp_turn) / (pixel_size * SPLINE_RESOL_CAP_PER_PIXEL)), \
1443 SPLINE_RESOL_CAP_MIN, \
1444 SPLINE_RESOL_CAP_MAX)
1445
1446 {
1447 const uint vertex_total_cap = CALC_CAP_RESOL;
1448
1449 for (k = 1; k < vertex_total_cap; k++) {
1450 const float angle = float(k) * (1.0f / float(vertex_total_cap)) * float(M_PI);
1451 float co_feather[2];
1452 rotate_point_v2(co_feather, fp_turn, fp_cent, angle, asp_xy);
1453
1454 open_spline_verts.append(float3(co_feather[0], co_feather[1], 1.0f));
1455 }
1456 tot_feather_quads += vertex_total_cap;
1457
1458 open_spline_ranges[open_spline_index].vertex_total_cap_head = vertex_total_cap;
1459 }
1460
1461 fp_cent = diff_points[tot_diff_point - 1];
1462 fp_turn = diff_feather_points[tot_diff_point - 1];
1463
1464 {
1465 const uint vertex_total_cap = CALC_CAP_RESOL;
1466
1467 for (k = 1; k < vertex_total_cap; k++) {
1468 const float angle = float(k) * (1.0f / float(vertex_total_cap)) * float(M_PI);
1469 float co_feather[2];
1470 rotate_point_v2(co_feather, fp_turn, fp_cent, -angle, asp_xy);
1471
1472 open_spline_verts.append(float3(co_feather[0], co_feather[1], 1.0f));
1473 }
1474 tot_feather_quads += vertex_total_cap;
1475
1476 open_spline_ranges[open_spline_index].vertex_total_cap_tail = vertex_total_cap;
1477 }
1478 }
1479
1480 open_spline_ranges[open_spline_index].is_cyclic = is_cyclic;
1481 open_spline_index++;
1482
1483#undef CALC_CAP_RESOL
1484 /* end capping */
1485 }
1486 }
1487 }
1488
1489 if (diff_points) {
1490 MEM_delete(diff_points);
1491 }
1492
1493 if (diff_feather_points) {
1494 MEM_delete(diff_feather_points);
1495 }
1496 }
1497
1498 cdt_face_offsets.append(int(cdt_face_vert_indices.size()));
1499
1500 /* CDT filling call (if there is anything to fill). */
1501
1502 uint cdt_tri_num = 0;
1503 const uint feather_vert_num = uint(feather_coords.size());
1504 const uint open_spline_vert_num = uint(open_spline_verts.size());
1505 uint cdt_vert_num = 0;
1506
1508
1509 if (!cdt_face_vert_indices.is_empty()) {
1510 const bool has_feather = !feather_ranges.is_empty();
1511
1513 cdt_in.vert = cdt_verts.as_span();
1514 cdt_in.face_offsets = cdt_face_offsets.as_span();
1515 cdt_in.face_vert_indices = cdt_face_vert_indices.as_span();
1516 cdt_in.epsilon = 1e-8;
1517 cdt_in.needed_ids = has_feather ? CDT_ORIG_VERTS : CDT_NO_ORIG_IDS;
1518
1519 CDT_output_type cdt_type = (masklay->flag & MASK_LAYERFLAG_FILL_DISCRETE) ?
1520 CDT_INSIDE :
1522
1523 result = meshintersect::delaunay_2d_calc(cdt_in, cdt_type);
1524 cdt_vert_num = uint(result.vert.size());
1525 cdt_tri_num = uint(result.face.size());
1526 }
1527
1528 /* Second: build face_coords + face_array. */
1529
1530 const uint face_num = cdt_tri_num + tot_feather_quads;
1531 const uint vert_num = cdt_vert_num + feather_vert_num + open_spline_vert_num;
1532
1533 MaskRasterLayer *layer = &mr_handle->layers[masklay_index];
1535
1536 if (face_num == 0) {
1537 MEM_delete(open_spline_ranges);
1539 }
1540 else {
1541 uint(*face_array)[4], *face;
1542 float (*face_coords)[3];
1543 rctf bounds;
1544 uint face_index;
1545
1546 face_coords = MEM_new_array_zeroed<float[3]>(vert_num, "maskrast_face_coords");
1547
1548 /* CDT output verts `[0 .. cdt_vert_num - 1]`, all Z=0. */
1549 for (uint i = 0; i < cdt_vert_num; i++) {
1550 copy_v3_v3(face_coords[i],
1551 float3(float(result.vert[int(i)][0]), float(result.vert[int(i)][1]), 0.0f));
1552 }
1553
1554 /* Feather verts `[cdt_vert_num .. cdt_vert_num + feather_vert_num - 1]`, all Z=1. */
1555 for (uint k = 0; k < feather_vert_num; k++) {
1556 copy_v3_v3(face_coords[cdt_vert_num + k],
1557 float3(feather_coords[int(k)][0], feather_coords[int(k)][1], 1.0f));
1558 }
1559
1560 /* Open-spline verts `[cdt_vert_num + feather_vert_num .. vert_num - 1]`. */
1561 for (uint i = 0; i < open_spline_vert_num; i++) {
1562 copy_v3_v3(face_coords[cdt_vert_num + feather_vert_num + i], open_spline_verts[int(i)]);
1563 }
1564
1565 /* Adjust open-spline offsets. */
1566 for (uint i = 0; i < open_spline_index; i++) {
1567 open_spline_ranges[i].vertex_offset += (cdt_vert_num + feather_vert_num);
1568 }
1569
1570 /* Bounds. */
1572 for (uint i = 0; i < vert_num; i++) {
1573 BLI_rctf_do_minmax_v(&bounds, face_coords[i]);
1574 }
1575
1576 /* Allocate face array. */
1577 face_array = MEM_new_array_uninitialized<uint[4]>(size_t(face_num), "maskrast_face_index");
1578 face_index = 0;
1579 face = reinterpret_cast<uint *>(face_array);
1580
1581 /* CDT triangles (CCW output, used directly without reversal). */
1582 for (uint i = 0; i < cdt_tri_num; i++) {
1583 const Vector<int> &tri = result.face[int(i)];
1584 BLI_assert(tri.size() == 3);
1585 *(face++) = uint(tri[0]);
1586 *(face++) = uint(tri[1]);
1587 *(face++) = uint(tri[2]);
1588 *(face++) = TRI_VERT;
1589 face_index++;
1590 FACE_ASSERT(face - 4, vert_num);
1591 }
1592
1593 /* Feather quads from feather_ranges. */
1594 if (!feather_ranges.is_empty()) {
1595 /* Build input-to-output vert map. */
1596 Array<int> input_to_output(int(cdt_verts.size()), -1);
1597 for (int i = 0; i < int(result.vert.size()); i++) {
1598 for (uint32_t orig : result.vert_orig[i]) {
1599 if (orig < uint32_t(cdt_verts.size())) {
1600 input_to_output[int(orig)] = i;
1601 }
1602 }
1603 }
1604
1605 for (const FeatherRange &range : feather_ranges) {
1606 for (int j = 0; j < range.vert_count; j++) {
1607 int j_next = (j + 1) % range.vert_count;
1608 int out_a = input_to_output[range.vert_start + j];
1609 int out_b = input_to_output[range.vert_start + j_next];
1610
1611 BLI_assert(out_a != -1 && out_b != -1);
1612
1613 *(face++) = uint(out_a);
1614 *(face++) = uint(out_b);
1615 *(face++) = cdt_vert_num + uint(range.feather_offset + j_next);
1616 *(face++) = cdt_vert_num + uint(range.feather_offset + j);
1617 face_index++;
1618 FACE_ASSERT(face - 4, vert_num);
1619 }
1620 }
1621 }
1622
1623 {
1625 open_spline_ranges, open_spline_index, face, vert_num);
1626 face += open_quad_num * 4;
1627 face_index += open_quad_num;
1628 }
1629
1630 MEM_delete(open_spline_ranges);
1631
1632 BLI_assert(face_index == face_num);
1633 UNUSED_VARS_NDEBUG(face_index);
1634
1635 if (BLI_rctf_isect(&default_bounds, &bounds, &bounds)) {
1636 layer->face_tot = face_num;
1637 layer->face_coords = face_coords;
1638 layer->face_array = face_array;
1639 layer->bounds = bounds;
1640
1641 layer_bucket_init(layer, pixel_size);
1642
1643 BLI_rctf_union(&mr_handle->bounds, &bounds);
1644 }
1645 else {
1646 MEM_delete(face_coords);
1647 MEM_delete(face_array);
1648
1650 }
1651 }
1652}
1653
1655 Mask *mask,
1656 const int width,
1657 const int height,
1658 const bool do_aspect_correct,
1659 const bool do_mask_aa,
1660 const bool do_feather)
1661{
1662 const rctf default_bounds = {0.0f, 1.0f, 0.0f, 1.0f};
1663 const float pixel_size = 1.0f / float(min_ii(width, height));
1664 const float asp_xy[2] = {
1665 (do_aspect_correct && width > height) ? float(height) / float(width) : 1.0f,
1666 (do_aspect_correct && width < height) ? float(width) / float(height) : 1.0f};
1667
1668 mr_handle->layers_tot = uint(mask->masklayers.count());
1669 mr_handle->layers = MEM_new_array<MaskRasterLayer>(mr_handle->layers_tot, "MaskRasterLayer");
1670 BLI_rctf_init_minmax(&mr_handle->bounds);
1671
1672 /* Arena shared across scan-fill layers for efficiency. */
1673 MemArena *sf_arena = nullptr;
1674
1675 uint masklay_index = 0;
1676 for (MaskLayer &masklay_iter : mask->masklayers) {
1677 MaskLayer *masklay = &masklay_iter;
1678 if (masklay->visibility_flag & MASK_HIDE_RENDER) {
1679 mr_handle->layers_tot--;
1680 continue;
1681 }
1682
1683 if (masklay->fill_solver == MASK_FILL_SOLVER_CDT) {
1685 masklay,
1686 masklay_index,
1687 default_bounds,
1688 pixel_size,
1689 asp_xy,
1690 width,
1691 height,
1692 do_aspect_correct,
1693 do_mask_aa,
1694 do_feather);
1695 }
1696 else {
1697 if (sf_arena == nullptr) {
1698 sf_arena = BLI_memarena_new(BLI_SCANFILL_ARENA_SIZE, __func__);
1699 }
1701 masklay,
1702 masklay_index,
1703 default_bounds,
1704 pixel_size,
1705 asp_xy,
1706 width,
1707 height,
1708 do_aspect_correct,
1709 do_mask_aa,
1710 do_feather,
1711 sf_arena);
1712 }
1713
1714 masklay_index++;
1715 }
1716
1717 if (sf_arena) {
1718 BLI_memarena_free(sf_arena);
1719 }
1720}
1721
1722/* --------------------------------------------------------------------- */
1723/* functions that run inside the sampling thread (keep fast!) */
1724/* --------------------------------------------------------------------- */
1725
1726/* 2D ray test */
1727#if 0
1728static float maskrasterize_layer_z_depth_tri(const float pt[2],
1729 const float v1[3],
1730 const float v2[3],
1731 const float v3[3])
1732{
1733 float w[3];
1734 barycentric_weights_v2(v1, v2, v3, pt, w);
1735 return (v1[2] * w[0]) + (v2[2] * w[1]) + (v3[2] * w[2]);
1736}
1737#endif
1738
1740 const float pt[2], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
1741{
1742 float w[4];
1743 barycentric_weights_v2_quad(v1, v2, v3, v4, pt, w);
1744 // return (v1[2] * w[0]) + (v2[2] * w[1]) + (v3[2] * w[2]) + (v4[2] * w[3]);
1745 return w[2] + w[3]; /* we can make this assumption for small speedup */
1746}
1747
1748static float maskrasterize_layer_isect(const uint *face,
1749 float (*cos)[3],
1750 const float dist_orig,
1751 const float xy[2])
1752{
1753 /* we always cast from same place only need xy */
1754 if (face[3] == TRI_VERT) {
1755 /* --- tri --- */
1756
1757#if 0
1758 /* not essential but avoids unneeded extra lookups */
1759 if ((cos[0][2] < dist_orig) || (cos[1][2] < dist_orig) || (cos[2][2] < dist_orig)) {
1760 if (isect_point_tri_v2_cw(xy, cos[face[0]], cos[face[1]], cos[face[2]])) {
1761 /* we know all tris are close for now */
1762 return maskrasterize_layer_z_depth_tri(xy, cos[face[0]], cos[face[1]], cos[face[2]]);
1763 }
1764 }
1765#else
1766 /* we know all tris are close for now */
1767 if (isect_point_tri_v2_cw(xy, cos[face[0]], cos[face[1]], cos[face[2]])) {
1768 return 0.0f;
1769 }
1770#endif
1771 }
1772 else {
1773 /* --- quad --- */
1774
1775 /* not essential but avoids unneeded extra lookups */
1776 if ((cos[0][2] < dist_orig) || (cos[1][2] < dist_orig) || (cos[2][2] < dist_orig) ||
1777 (cos[3][2] < dist_orig))
1778 {
1779
1780 /* needs work */
1781#if 1
1782 /* quad check fails for bow-tie, so keep using 2 tri checks */
1783 // if (isect_point_quad_v2(xy, cos[face[0]], cos[face[1]], cos[face[2]], cos[face[3]]))
1784 if (isect_point_tri_v2(xy, cos[face[0]], cos[face[1]], cos[face[2]]) ||
1785 isect_point_tri_v2(xy, cos[face[0]], cos[face[2]], cos[face[3]]))
1786 {
1788 xy, cos[face[0]], cos[face[1]], cos[face[2]], cos[face[3]]);
1789 }
1790#elif 1
1791 /* don't use isect_point_tri_v2_cw because we could have bow-tie quads */
1792
1793 if (isect_point_tri_v2(xy, cos[face[0]], cos[face[1]], cos[face[2]])) {
1794 return maskrasterize_layer_z_depth_tri(xy, cos[face[0]], cos[face[1]], cos[face[2]]);
1795 }
1796 else if (isect_point_tri_v2(xy, cos[face[0]], cos[face[2]], cos[face[3]])) {
1797 return maskrasterize_layer_z_depth_tri(xy, cos[face[0]], cos[face[2]], cos[face[3]]);
1798 }
1799#else
1800 /* cheat - we know first 2 verts are z0.0f and second 2 are z 1.0f */
1801 /* ... worth looking into */
1802#endif
1803 }
1804 }
1805
1806 return 1.0f;
1807}
1808
1810{
1811 BLI_assert(BLI_rctf_isect_pt_v(&layer->bounds, xy));
1812
1813 return uint((xy[0] - layer->bounds.xmin) * layer->buckets_xy_scalar[0]) +
1814 (uint((xy[1] - layer->bounds.ymin) * layer->buckets_xy_scalar[1]) * layer->buckets_x);
1815}
1816
1817static float layer_bucket_depth_from_xy(MaskRasterLayer *layer, const float xy[2])
1818{
1820 uint *face_index = layer->buckets_face[index];
1821
1822 if (face_index) {
1823 uint(*face_array)[4] = layer->face_array;
1824 float (*cos)[3] = layer->face_coords;
1825 float best_dist = 1.0f;
1826 while (*face_index != TRI_TERMINATOR_ID) {
1827 const float test_dist = maskrasterize_layer_isect(
1828 face_array[*face_index], cos, best_dist, xy);
1829 if (test_dist < best_dist) {
1830 best_dist = test_dist;
1831 /* comparing with 0.0f is OK here because triangles are always zero depth */
1832 if (best_dist == 0.0f) {
1833 /* bail early, we're as close as possible */
1834 return 0.0f;
1835 }
1836 }
1837 face_index++;
1838 }
1839 return best_dist;
1840 }
1841
1842 return 1.0f;
1843}
1844
1845float BKE_maskrasterize_handle_sample(MaskRasterHandle *mr_handle, const float xy[2])
1846{
1847 /* can't do this because some layers may invert */
1848 /* if (BLI_rctf_isect_pt_v(&mr_handle->bounds, xy)) */
1849
1850 const uint layers_tot = mr_handle->layers_tot;
1851 MaskRasterLayer *layer = mr_handle->layers;
1852
1853 /* return value */
1854 float value = 0.0f;
1855
1856 for (uint i = 0; i < layers_tot; i++, layer++) {
1857 float value_layer;
1858
1859 /* also used as signal for unused layer (when render is disabled) */
1860 if (layer->alpha != 0.0f && BLI_rctf_isect_pt_v(&layer->bounds, xy)) {
1861 value_layer = 1.0f - layer_bucket_depth_from_xy(layer, xy);
1862
1863 switch (layer->falloff) {
1864 case PROP_SMOOTH:
1865 /* ease - gives less hard lines for dilate/erode feather */
1866 value_layer = (3.0f * value_layer * value_layer -
1867 2.0f * value_layer * value_layer * value_layer);
1868 break;
1869 case PROP_SPHERE:
1870 value_layer = sqrtf(2.0f * value_layer - value_layer * value_layer);
1871 break;
1872 case PROP_ROOT:
1873 value_layer = sqrtf(value_layer);
1874 break;
1875 case PROP_SHARP:
1876 value_layer = value_layer * value_layer;
1877 break;
1878 case PROP_INVSQUARE:
1879 value_layer = value_layer * (2.0f - value_layer);
1880 break;
1881 case PROP_LIN:
1882 default:
1883 /* nothing */
1884 break;
1885 }
1886
1887 if (layer->blend != MASK_BLEND_REPLACE) {
1888 value_layer *= layer->alpha;
1889 }
1890 }
1891 else {
1892 value_layer = 0.0f;
1893 }
1894
1895 if (layer->blend_flag & MASK_BLENDFLAG_INVERT) {
1896 value_layer = 1.0f - value_layer;
1897 }
1898
1899 switch (layer->blend) {
1901 value += value_layer * (1.0f - value);
1902 break;
1904 value -= value_layer * value;
1905 break;
1906 case MASK_BLEND_ADD:
1907 value += value_layer;
1908 break;
1910 value -= value_layer;
1911 break;
1912 case MASK_BLEND_LIGHTEN:
1913 value = max_ff(value, value_layer);
1914 break;
1915 case MASK_BLEND_DARKEN:
1916 value = min_ff(value, value_layer);
1917 break;
1918 case MASK_BLEND_MUL:
1919 value *= value_layer;
1920 break;
1921 case MASK_BLEND_REPLACE:
1922 value = (value * (1.0f - layer->alpha)) + (value_layer * layer->alpha);
1923 break;
1925 value = fabsf(value - value_layer);
1926 break;
1927 default: /* same as add */
1928 CLOG_ERROR(&LOG, "unhandled blend type: %d", layer->blend);
1929 BLI_assert(0);
1930 value += value_layer;
1931 break;
1932 }
1933
1934 /* clamp after applying each layer so we don't get
1935 * issues subtracting after accumulating over 1.0f */
1936 CLAMP(value, 0.0f, 1.0f);
1937 }
1938
1939 return value;
1940}
1941
1950
1951static void maskrasterize_buffer_cb(void *__restrict userdata,
1952 const int y,
1953 const TaskParallelTLS *__restrict /*tls*/)
1954{
1955 MaskRasterizeBufferData *data = static_cast<MaskRasterizeBufferData *>(userdata);
1956
1957 MaskRasterHandle *mr_handle = data->mr_handle;
1958 float *buffer = data->buffer;
1959
1960 const uint width = data->width;
1961 const float x_inv = data->x_inv;
1962 const float x_px_ofs = data->x_px_ofs;
1963
1964 uint i = uint(y) * width;
1965 float xy[2];
1966 xy[1] = (float(y) * data->y_inv) + data->y_px_ofs;
1967 for (uint x = 0; x < width; x++, i++) {
1968 xy[0] = (float(x) * x_inv) + x_px_ofs;
1969
1970 buffer[i] = BKE_maskrasterize_handle_sample(mr_handle, xy);
1971 }
1972}
1973
1975 const uint width,
1976 const uint height,
1977 /* Cannot be const, because it is assigned to non-const variable.
1978 * NOLINTNEXTLINE: readability-non-const-parameter. */
1979 float *buffer)
1980{
1981 const float x_inv = 1.0f / float(width);
1982 const float y_inv = 1.0f / float(height);
1983
1985 data.mr_handle = mr_handle;
1986 data.x_inv = x_inv;
1987 data.y_inv = y_inv;
1988 data.x_px_ofs = x_inv * 0.5f;
1989 data.y_px_ofs = y_inv * 0.5f;
1990 data.width = width;
1991 data.buffer = buffer;
1992 TaskParallelSettings settings;
1994 settings.use_threading = (size_t(height) * width > 10000);
1995 BLI_task_parallel_range(0, int(height), &data, maskrasterize_buffer_cb, &settings);
1996}
1997
1998} // namespace blender
#define M_PI
#define BLI_SCANFILL_ARENA_SIZE
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define POINTER_AS_UINT(i)
#define UNUSED_VARS_NDEBUG(...)
#define BLI_INLINE
#define POINTER_FROM_UINT(i)
#define MEM_SIZE_OPTIMAL(size)
#define MEM_realloc_uninitialized(vmemh, len)
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr IndexRange()=default
int64_t size() const
void append(const T &value)
bool is_empty() const
Span< T > as_span() const
#define FLT_EPSILON
#define layer
#define cos
#define SF_EDGE_IS_BOUNDARY
#define FACE_ASSERT(face, vert_max)
#define TRI_VERT
#define SF_KEYINDEX_TEMP_ID
#define TRI_TERMINATOR_ID
#define CALC_CAP_RESOL
CDT_result< T > delaunay_2d_calc(const CDT_input< T > &input, CDT_output_type output_type)
BMesh Iterators.
MINLINE float min_ff(float a, float b)
BLI_INLINE uint clampis_uint(const uint v, const uint min, const uint max)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
bool BLI_rctf_isect_pt_v(const struct rctf *rect, const float xy[2])
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
MaskRasterHandle * BKE_maskrasterize_handle_new()
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static uint maskrasterize_layer_fill_open_spline_feather_quads(const MaskRasterSplineInfo *open_spline_ranges, uint open_spline_index, uint *face, const uint vert_num)
static void layer_bucket_init(MaskRasterLayer *layer, const float pixel_size)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
static void layer_bucket_init_dummy(MaskRasterLayer *layer)
bool BLI_scanfill_calc_self_isect(ScanFillContext *sf_ctx, ListBaseT< ScanFillVert > *remvertbase, ListBaseT< ScanFillEdge > *remedgebase)
static void maskrasterize_layer_init_scanfill(MaskRasterHandle *mr_handle, MaskLayer *masklay, uint masklay_index, const rctf &default_bounds, const float pixel_size, const float asp_xy[2], const int width, const int height, const bool do_aspect_correct, const bool do_mask_aa, const bool do_feather, MemArena *sf_arena)
MINLINE void copy_v2_v2(float r[2], const float a[2])
static bool isect_aabb_tri_v2(const float xmin, const float ymin, const float xmax, const float ymax, const float v1[2], const float v2[2], const float v3[2])
float BKE_maskrasterize_handle_sample(MaskRasterHandle *mr_handle, const float xy[2])
@ CDT_INSIDE_WITH_HOLES_NONZERO
VecBase< double, 2 > double2
@ BLI_SCANFILL_CALC_POLYS
@ BLI_SCANFILL_CALC_HOLES
void BLI_linklist_prepend_arena(LinkNode **listp, void *ptr, struct MemArena *ma) ATTR_NONNULL(1
void BLI_rctf_do_minmax_v(struct rctf *rect, const float xy[2])
Definition rct.cc:524
void void void BLI_movelisttolist(ListBase *dst, ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:32
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE float normalize_v2(float n[2])
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static bool layer_bucket_isect_test(const MaskRasterLayer *layer, uint face_index, const uint bucket_x, const uint bucket_y, const float bucket_size_x, const float bucket_size_y)
static float maskrasterize_layer_isect(const uint *face, float(*cos)[3], const float dist_orig, const float xy[2])
int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
unsigned int uint32_t
Definition BKE_text.h:81
BLI_INLINE uint layer_bucket_index_from_xy(MaskRasterLayer *layer, const float xy[2])
VecBase< float, 2 > float2
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
int BKE_mask_spline_resolution(MaskSpline *spline, int width, int height)
BMesh const char void * data
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 BLI_scanfill_begin_arena(ScanFillContext *sf_ctx, struct MemArena *arena)
Definition scanfill.cc:791
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
static void maskrasterize_layer_init_from_mask_layer(MaskRasterLayer *layer, const MaskLayer *masklay)
struct ScanFillVert * BLI_scanfill_vert_add(ScanFillContext *sf_ctx, const float vec[3])
Definition scanfill.cc:105
static ScanFillVert * scanfill_vert_add_v2_with_depth(ScanFillContext *sf_ctx, const float co_xy[2], const float co_z)
void BKE_mask_spline_feather_collapse_inner_loops(MaskSpline *spline, float(*feather_points)[2], unsigned int tot_feather_point)
void BKE_maskrasterize_handle_init(MaskRasterHandle *mr_handle, Mask *mask, int width, int height, bool do_aspect_correct, bool do_mask_aa, bool do_feather)
void barycentric_weights_v2_quad(const float v1[2], const float v2[2], const float v3[2], const float v4[2], const float co[2], float w[4])
@ MASK_BLEND_DIFFERENCE
@ MASK_BLEND_REPLACE
@ MASK_BLEND_MERGE_ADD
@ MASK_BLEND_LIGHTEN
@ MASK_BLEND_MERGE_SUBTRACT
@ MASK_BLEND_SUBTRACT
@ MASK_BLEND_DARKEN
void BKE_maskrasterize_handle_free(MaskRasterHandle *mr_handle)
@ MASK_SPLINE_CYCLIC
@ MASK_SPLINE_NOFILL
@ MASK_SPLINE_NOINTERSECT
static float maskrasterize_layer_z_depth_quad(const float pt[2], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
void BLI_rctf_init_minmax(struct rctf *rect)
Definition rct.cc:494
@ MASK_FILL_SOLVER_CDT
static void maskrasterize_buffer_cb(void *__restrict userdata, const int y, const TaskParallelTLS *__restrict)
static float layer_bucket_depth_from_xy(MaskRasterLayer *layer, const float xy[2])
void BLI_rctf_union(struct rctf *rct_a, const struct rctf *rct_b)
struct ScanFillEdge * BLI_scanfill_edge_add(ScanFillContext *sf_ctx, struct ScanFillVert *v1, struct ScanFillVert *v2)
Definition scanfill.cc:127
bool isect_point_tri_v2_cw(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
void BLI_scanfill_end_arena(ScanFillContext *sf_ctx, struct MemArena *arena)
Definition scanfill.cc:808
ATTR_WARN_UNUSED_RESULT const BMVert * v2
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
static void maskrasterize_layer_init_cdt(MaskRasterHandle *mr_handle, MaskLayer *masklay, uint masklay_index, const rctf &default_bounds, const float pixel_size, const float asp_xy[2], const int width, const int height, const bool do_aspect_correct, const bool do_mask_aa, const bool do_feather)
unsigned int BKE_mask_spline_feather_resolution(MaskSpline *spline, int width, int height)
MINLINE float max_ff(float a, float b)
bool BLI_rctf_isect(const struct rctf *src1, const struct rctf *src2, struct rctf *dest)
@ MASK_BLENDFLAG_INVERT
@ MASK_LAYERFLAG_FILL_DISCRETE
@ MASK_LAYERFLAG_FILL_OVERLAP
static void maskrasterize_spline_differentiate_point_outset(float(*diff_feather_points)[2], float(*diff_points)[2], const uint tot_diff_point, const float ofs, const bool do_test)
static CLG_LogRef LOG
Definition logging.cpp:12
unsigned int BLI_scanfill_calc_ex(ScanFillContext *sf_ctx, int flag, const float nor_proj[3])
Definition scanfill.cc:818
void BKE_maskrasterize_buffer(MaskRasterHandle *mr_handle, unsigned int width, unsigned int height, float *buffer)
Rasterize a buffer from a single mask (threaded execution).
VecBase< float, 3 > float3
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
MINLINE int min_ii(int a, int b)
static void rotate_point_v2(float r_p[2], const float p[2], const float cent[2], const float angle, const float asp[2])
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
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
static constexpr int BUCKET_PIXELS_PER_CELL
float(* BKE_mask_spline_differentiate_with_resolution(MaskSpline *spline, unsigned int resol, unsigned int *r_tot_diff_point))[2]
#define fabsf
#define sqrtf
#define fmaxf
#define sinf
#define fminf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
struct LinkNode * next
MaskLayerBlendFlag blend_flag
MaskLayerBlend blend
MaskLayerVisibility visibility_flag
ListBaseT< MaskSpline > splines
MaskLayerFillSolverType fill_solver
MaskSplineFlag flag
ListBaseT< ScanFillFace > fillfacebase
ListBaseT< ScanFillEdge > filledgebase
ListBaseT< ScanFillVert > fillvertbase
union blender::ScanFillEdge::@262251053107234334274134243151221246013035300046 tmp
struct ScanFillVert * v2
struct ScanFillVert * v1
struct ScanFillFace * next
struct ScanFillVert * v2
struct ScanFillVert * v1
struct ScanFillVert * v3
struct ScanFillVert * next
union blender::ScanFillVert::@124355022001164321030006076112165043115032300246 tmp