413 CLAMP(layer->buckets_x, 8, 512);
414 CLAMP(layer->buckets_y, 8, 512);
416 layer->buckets_xy_scalar[0] = (1.0f / (bucket_dim_x + FLT_EPSILON)) *
float(layer->buckets_x);
417 layer->buckets_xy_scalar[1] = (1.0f / (bucket_dim_y + FLT_EPSILON)) *
float(layer->buckets_y);
421 const float bucket_size_x = (bucket_dim_x + FLT_EPSILON) /
float(layer->buckets_x);
422 const float bucket_size_y = (bucket_dim_y + FLT_EPSILON) /
float(layer->buckets_y);
423 const float bucket_max_rad = (
max_ff(bucket_size_x, bucket_size_y) *
float(
M_SQRT2)) +
425 const float bucket_max_rad_squared = bucket_max_rad * bucket_max_rad;
427 uint *face = &layer->face_array[0][0];
428 float(*
cos)[3] = layer->face_coords;
430 const uint bucket_tot = layer->buckets_x * layer->buckets_y;
431 LinkNode **bucketstore = MEM_cnew_array<LinkNode *>(bucket_tot, __func__);
432 uint *bucketstore_tot = MEM_cnew_array<uint>(bucket_tot, __func__);
436 for (face_index = 0; face_index < layer->face_tot; face_index++, face += 4) {
443 const float *v1 =
cos[face[0]];
444 const float *
v2 =
cos[face[1]];
445 const float *v3 =
cos[face[2]];
453 const float *v1 =
cos[face[0]];
454 const float *
v2 =
cos[face[1]];
455 const float *v3 =
cos[face[2]];
456 const float *v4 =
cos[face[3]];
465 if (!((xmax < 0.0f) || (ymax < 0.0f) || (xmin > 1.0f) || (ymin > 1.0f))) {
467 CLAMP(xmin, 0.0f, 1.0f);
468 CLAMP(ymin, 0.0f, 1.0f);
469 CLAMP(xmax, 0.0f, 1.0f);
470 CLAMP(ymax, 0.0f, 1.0f);
473 uint xi_min =
uint((xmin - layer->bounds.xmin) * layer->buckets_xy_scalar[0]);
474 uint xi_max =
uint((xmax - layer->bounds.xmin) * layer->buckets_xy_scalar[0]);
475 uint yi_min =
uint((ymin - layer->bounds.ymin) * layer->buckets_xy_scalar[1]);
476 uint yi_max =
uint((ymax - layer->bounds.ymin) * layer->buckets_xy_scalar[1]);
483 if (xi_min >= layer->buckets_x) {
484 xi_min = layer->buckets_x - 1;
486 if (xi_max >= layer->buckets_x) {
487 xi_max = layer->buckets_x - 1;
489 if (yi_min >= layer->buckets_y) {
490 yi_min = layer->buckets_y - 1;
492 if (yi_max >= layer->buckets_y) {
493 yi_max = layer->buckets_y - 1;
496 for (yi = yi_min; yi <= yi_max; yi++) {
497 uint bucket_index = (layer->buckets_x * yi) + xi_min;
498 for (xi = xi_min; xi <= xi_max; xi++, bucket_index++) {
517 bucket_max_rad_squared))
520 bucketstore_tot[bucket_index]++;
530 uint **buckets_face = MEM_cnew_array<uint *>(bucket_tot, __func__);
533 for (bucket_index = 0; bucket_index < bucket_tot; bucket_index++) {
534 if (bucketstore_tot[bucket_index]) {
535 uint *bucket = MEM_cnew_array<uint>((bucketstore_tot[bucket_index] + 1), __func__);
538 buckets_face[bucket_index] = bucket;
540 for (bucket_node = bucketstore[bucket_index]; bucket_node;
541 bucket_node = bucket_node->
next)
549 buckets_face[bucket_index] =
nullptr;
553 layer->buckets_face = buckets_face;
567 const bool do_aspect_correct,
568 const bool do_mask_aa,
569 const bool do_feather)
571 const rctf default_bounds = {0.0f, 1.0f, 0.0f, 1.0f};
572 const float pixel_size = 1.0f /
float(
min_ii(width, height));
573 const float asp_xy[2] = {
574 (do_aspect_correct && width > height) ?
float(height) /
float(width) : 1.0f,
575 (do_aspect_correct && width < height) ?
float(width) /
float(height) : 1.0f};
577 const float zvec[3] = {0.0f, 0.0f, -1.0f};
583 mr_handle->
layers = MEM_cnew_array<MaskRasterLayer>(mr_handle->
layers_tot,
"MaskRasterLayer");
588 for (masklay =
static_cast<MaskLayer *
>(mask->masklayers.first), masklay_index = 0; masklay;
589 masklay = masklay->
next, masklay_index++)
594 uint open_spline_index = 0;
602 uint sf_vert_tot = 0;
603 uint tot_feather_quads = 0;
605#ifdef USE_SCANFILL_EDGE_WORKAROUND
606 uint tot_boundary_used = 0;
607 uint tot_boundary_found = 0;
618 open_spline_ranges = MEM_cnew_array<MaskRasterSplineInfo>(tot_splines, __func__);
626 float(*diff_points)[2];
629 float(*diff_feather_points)[2];
630 float(*diff_feather_points_flip)[2];
631 uint tot_diff_feather_points;
635 const uint resol = std::clamp(std::max(resol_a, resol_b), 4u, 512u);
641 spline, resol,
false, &tot_diff_feather_points);
645 tot_diff_feather_points = 0;
646 diff_feather_points =
nullptr;
649 if (tot_diff_point > 3) {
655 if (do_aspect_correct) {
656 if (width != height) {
661 if (width < height) {
662 fp = &diff_points[0][0];
663 ffp = tot_diff_feather_points ? &diff_feather_points[0][0] :
nullptr;
667 fp = &diff_points[0][1];
668 ffp = tot_diff_feather_points ? &diff_feather_points[0][1] :
nullptr;
672 for (
uint i = 0; i < tot_diff_point; i++, fp += 2) {
673 (*fp) = (((*fp) - 0.5f) / asp) + 0.5f;
676 if (tot_diff_feather_points) {
677 for (
uint i = 0; i < tot_diff_feather_points; i++, ffp += 2) {
678 (*ffp) = (((*ffp) - 0.5f) / asp) + 0.5f;
685 if (do_mask_aa ==
true) {
686 if (do_feather ==
false) {
687 tot_diff_feather_points = tot_diff_point;
688 diff_feather_points = MEM_cnew_array<float[2]>(
size_t(tot_diff_feather_points),
692 diff_feather_points, diff_points, tot_diff_point, pixel_size,
false);
697 diff_feather_points, diff_points, tot_diff_point, pixel_size,
true);
705 spline, diff_feather_points, tot_diff_feather_points);
709 sf_vert_prev->
tmp.
u = sf_vert_tot;
712 sf_vert_prev->
keyindex = sf_vert_tot + tot_diff_point;
716 for (j = 1; j < tot_diff_point; j++) {
718 sf_vert->
tmp.
u = sf_vert_tot;
719 sf_vert->
keyindex = sf_vert_tot + tot_diff_point;
723 sf_vert = sf_vert_prev;
726 for (j = 0; j < tot_diff_point; j++) {
729#ifdef USE_SCANFILL_EDGE_WORKAROUND
730 if (diff_feather_points) {
737 sf_vert_prev = sf_vert;
738 sf_vert = sf_vert->
next;
741 if (diff_feather_points) {
742 BLI_assert(tot_diff_feather_points == tot_diff_point);
746 for (j = 0; j < tot_diff_feather_points; j++) {
752 tot_feather_quads += tot_diff_point;
757 if (diff_feather_points) {
760 diff_feather_points_flip = MEM_cnew_array<float[2]>(tot_diff_feather_points,
761 "diff_feather_points_flip");
764 for (j = 0; j < tot_diff_point; j++) {
765 sub_v2_v2v2(co_diff, diff_points[j], diff_feather_points[j]);
766 add_v2_v2v2(diff_feather_points_flip[j], diff_points[j], co_diff);
770 spline, diff_feather_points, tot_diff_feather_points);
772 spline, diff_feather_points_flip, tot_diff_feather_points);
775 diff_feather_points_flip =
nullptr;
778 open_spline_ranges[open_spline_index].
vertex_offset = sf_vert_tot;
779 open_spline_ranges[open_spline_index].
vertex_total = tot_diff_point;
782 for (j = 0; j < tot_diff_point; j++) {
786 sf_vert->
tmp.
u = sf_vert_tot;
792 sf_vert->
tmp.
u = sf_vert_tot;
797 if (diff_feather_points_flip) {
799 &sf_ctx, diff_feather_points_flip[j], 1.0f);
803 sub_v2_v2v2(co_diff, diff_points[j], diff_feather_points[j]);
808 sf_vert->
tmp.
u = sf_vert_tot;
812 tot_feather_quads += 2;
816 tot_feather_quads -= 2;
819 if (diff_feather_points_flip) {
821 diff_feather_points_flip =
nullptr;
831 const float *fp_cent;
832 const float *fp_turn;
836 fp_cent = diff_points[0];
837 fp_turn = diff_feather_points[0];
839#define CALC_CAP_RESOL \
840 clampis_uint(uint(len_v2v2(fp_cent, fp_turn) / (pixel_size * SPLINE_RESOL_CAP_PER_PIXEL)), \
841 SPLINE_RESOL_CAP_MIN, \
842 SPLINE_RESOL_CAP_MAX)
847 for (k = 1; k < vertex_total_cap; k++) {
848 const float angle =
float(k) * (1.0f /
float(vertex_total_cap)) *
float(
M_PI);
853 sf_vert->
tmp.
u = sf_vert_tot;
857 tot_feather_quads += vertex_total_cap;
862 fp_cent = diff_points[tot_diff_point - 1];
863 fp_turn = diff_feather_points[tot_diff_point - 1];
868 for (k = 1; k < vertex_total_cap; k++) {
869 const float angle =
float(k) * (1.0f /
float(vertex_total_cap)) *
float(
M_PI);
874 sf_vert->
tmp.
u = sf_vert_tot;
878 tot_feather_quads += vertex_total_cap;
897 if (diff_feather_points) {
903 uint(*face_array)[4], *face;
908 int scanfill_flag = 0;
910 bool is_isect =
false;
911 ListBase isect_remvertbase = {
nullptr,
nullptr};
912 ListBase isect_remedgebase = {
nullptr,
nullptr};
915 face_coords = MEM_cnew_array<float[3]>(sf_vert_tot,
"maskrast_face_coords");
921 cos = (
float *)face_coords;
923 sf_vert = sf_vert_next)
925 sf_vert_next = sf_vert->
next;
945 &sf_ctx, &isect_remvertbase, &isect_remedgebase)))
948 uint i = sf_vert_tot;
950 face_coords =
static_cast<float(*)[3]
>(
951 MEM_reallocN(face_coords,
sizeof(
float[3]) * (sf_vert_tot + sf_vert_tot_isect)));
953 cos = (
float *)&face_coords[sf_vert_tot][0];
956 sf_vert = sf_vert->
next)
959 sf_vert->
tmp.
u = i++;
963 sf_vert_tot += sf_vert_tot_isect;
978 uint sf_edge_array_num = 0;
979 if (tot_feather_quads) {
981 for (
int pass = 0; pass < 2; pass++) {
984 sf_edge_array_num += 1;
989 if (sf_edge_array_num > 0) {
993 for (
int pass = 0; pass < 2; pass++) {
996 sf_edge_array[edge_index++] = sf_edge;
1013 face_array =
static_cast<uint(*)[4]
>(
1014 MEM_mallocN(
sizeof(*face_array) * (
size_t(sf_tri_tot) +
size_t(tot_feather_quads)),
1015 "maskrast_face_index"));
1019 face = (
uint *)face_array;
1021 sf_tri = sf_tri->
next)
1023 *(face++) = sf_tri->
v3->
tmp.
u;
1024 *(face++) = sf_tri->
v2->
tmp.
u;
1025 *(face++) = sf_tri->
v1->
tmp.
u;
1037 if (sf_edge_array) {
1039 for (
uint i = 0; i < sf_edge_array_num; i++) {
1042 *(face++) = sf_edge->
v1->
tmp.
u;
1043 *(face++) = sf_edge->
v2->
tmp.
u;
1049#ifdef USE_SCANFILL_EDGE_WORKAROUND
1050 tot_boundary_found++;
1056#ifdef USE_SCANFILL_EDGE_WORKAROUND
1057 if (tot_boundary_found != tot_boundary_used) {
1058 BLI_assert(tot_boundary_found < tot_boundary_used);
1063 while (open_spline_index > 0) {
1064 const uint vertex_offset = open_spline_ranges[--open_spline_index].
vertex_offset;
1073 for (k = 0; k < vertex_total - 1; k++, j += 3) {
1092 if (open_spline_ranges[open_spline_index].
is_cyclic) {
1093 *(face++) = vertex_offset + 0;
1096 *(face++) = vertex_offset + 1;
1101 *(face++) = vertex_offset + 0;
1102 *(face++) = vertex_offset + 2;
1108 uint midvidx = vertex_offset;
1112 j = midvidx + (vertex_total * 3);
1114 for (k = 0; k < vertex_total_cap_head - 2; k++, j++) {
1115 *(face++) = midvidx + 0;
1116 *(face++) = midvidx + 0;
1123 j = vertex_offset + (vertex_total * 3);
1126 *(face++) = midvidx + 0;
1127 *(face++) = midvidx + 0;
1128 *(face++) = midvidx + 1;
1133 *(face++) = midvidx + 0;
1134 *(face++) = midvidx + 0;
1135 *(face++) = j + vertex_total_cap_head - 2;
1136 *(face++) = midvidx + 2;
1144 j = vertex_offset + (vertex_total * 3) + (vertex_total_cap_head - 1);
1146 midvidx = vertex_offset + (vertex_total * 3) - 3;
1148 for (k = 0; k < vertex_total_cap_tail - 2; k++, j++) {
1149 *(face++) = midvidx;
1150 *(face++) = midvidx;
1157 j = vertex_offset + (vertex_total * 3) + (vertex_total_cap_head - 1);
1160 *(face++) = midvidx + 0;
1161 *(face++) = midvidx + 0;
1163 *(face++) = midvidx + 1;
1167 *(face++) = midvidx + 0;
1168 *(face++) = midvidx + 0;
1169 *(face++) = midvidx + 2;
1170 *(face++) = j + vertex_total_cap_tail - 2;
1180 "%u %u (%u %u), %u\n",
1182 sf_tri_tot + tot_feather_quads,
1185 tot_boundary_used - tot_boundary_found);
1188#ifdef USE_SCANFILL_EDGE_WORKAROUND
1189 BLI_assert(face_index + (tot_boundary_used - tot_boundary_found) ==
1190 sf_tri_tot + tot_feather_quads);
1192 BLI_assert(face_index == sf_tri_tot + tot_feather_quads);
1198#ifdef USE_SCANFILL_EDGE_WORKAROUND
1199 layer->
face_tot = (sf_tri_tot + tot_feather_quads) -
1200 (tot_boundary_used - tot_boundary_found);
1202 layer->face_tot = (sf_tri_tot + tot_feather_quads);
1204 layer->face_coords = face_coords;
1205 layer->face_array = face_array;
1220 layer->alpha = masklay->
alpha;
1221 layer->blend = masklay->
blend;
1223 layer->falloff = masklay->
falloff;