Blender V5.3
grease_pencil_boolean_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "testing/testing.h"
6
7#include "MEM_guardedalloc.h"
8
9#include "BLI_array.hh"
10#include "BLI_array_utils.hh"
11#include "BLI_bounds.hh"
12#include "BLI_offset_indices.hh"
13
14#include "BKE_attribute.hh"
15#include "BKE_curves.hh"
17#include "BKE_gtest_base.hh"
18
19#include "ED_grease_pencil.hh"
20
21#include <fstream>
22#include <iostream>
23#include <sstream>
24#include <type_traits>
25
26/* Should tests draw their output to an HTML file? */
27#define DO_DRAW 0
28
30
32
33static void CSS_setup_style(std::ofstream &f)
34{
35 constexpr int border_width = 5;
36 constexpr int stroke_width = 3;
37 constexpr int stroke_dasharray = 15;
38
39 f << ".ui-group {\n"
40 " border: "
41 << border_width << "px solid black;\n"
42 << " text-align: center;\n"
43 "}\n"
44 "\n";
45
46 f << ".ui-list {\n"
47 " align-items: center;\n"
48 " display: inline-flex;\n"
49 "}\n"
50 "\n";
51
52 f << ".polygon-A {\n"
53 " fill: blue;\n"
54 " fill-opacity: 0.25;\n"
55 " stroke: blue;\n"
56 " stroke-width: "
57 << stroke_width
58 << "px;\n"
59 " stroke-dasharray: "
60 << stroke_dasharray
61 << "px;\n"
62 "}\n";
63 f << ".polygon-B {\n"
64 " fill: red;\n"
65 " fill-opacity: 0.25;\n"
66 " stroke: red;\n"
67 " stroke-width: "
68 << stroke_width
69 << "px;\n"
70 " stroke-dasharray: "
71 << stroke_dasharray
72 << "px;\n"
73 "}\n";
74 f << ".polygon-C {\n"
75 " fill: green;\n"
76 " stroke: black;\n"
77 " stroke-width: "
78 << stroke_width + 1
79 << "px;\n"
80 " fill-opacity: 0.75;\n"
81 "}\n"
82 "\n";
83
84 f << ".cut-A {\n"
85 " fill: none;\n"
86 " stroke: blue;\n"
87 " stroke-width: "
88 << stroke_width
89 << "px;\n"
90 " stroke-dasharray: "
91 << stroke_dasharray
92 << "px;\n"
93 "}\n";
94 f << ".cut-B {\n"
95 " fill: red;\n"
96 " stroke: red;\n"
97 " fill-opacity: 0.25;\n"
98 " stroke-width: "
99 << stroke_width
100 << "px;\n"
101 " stroke-dasharray: "
102 << stroke_dasharray
103 << "px;\n"
104 "}\n";
105 f << ".cut-C {\n"
106 " fill: none;\n"
107 " stroke: black;\n"
108 " stroke-width: "
109 << stroke_width + 1
110 << "px;\n"
111 "}\n";
112}
113
115 public:
117 float scale;
120
121 float SX(const float x) const
122 {
123 return ((x - topleft[0]) * scale);
124 }
125
126 float SY(const float y) const
127 {
128 return ((topleft[1] - y) * scale);
129 }
130
132 {
133 constexpr int max_draw_width = 600;
134 constexpr int max_draw_height = 400;
135
136 const float draw_margin = (bounds.size().x + bounds.size().y) * 0.05;
137
138 Bounds<float2> bounds_padded = bounds;
139 bounds_padded.pad(draw_margin);
140
141 topleft = float2(bounds_padded.min.x, bounds_padded.max.y);
142 const float width = bounds_padded.size().x;
143 const float height = bounds_padded.size().y;
144 const float aspect = height / width;
145 view_width = max_draw_width;
146 view_height = int(view_width * aspect);
147 if (view_height > max_draw_height) {
148 view_height = max_draw_height;
149 view_width = int(view_height / aspect);
150 }
151 scale = view_width / width;
152 }
153};
154
155static void SVG_add_path(std::ofstream &f,
156 const std::string &class_name,
157 const Span<float2> &positions,
158 const GroupedSpan<int> shapes,
159 const IndexMask &shape_mask,
160 const OffsetIndices<int> points_by_curve,
161 const VArraySpan<bool> &cyclic,
162 const SVGMapping &mapping)
163{
164 shape_mask.foreach_index([&](const int64_t shape_i) {
165 f << "<path class = \"" << class_name << "\" d = \"";
166
167 const Span<int> shape = shapes[shape_i];
168
169 for (const int pos : shape.index_range()) {
170 const int curve_i = shape[pos];
171
172 if (pos != 0) {
173 f << " ";
174 }
175
176 f << "M ";
177 const IndexRange points = points_by_curve[curve_i];
178 for (const int i : points.index_range()) {
179 const float2 &pos = positions[points[i]];
180
181 if (i == 1) {
182 f << " L ";
183 }
184 else if (i != 0) {
185 f << ", ";
186 }
187 f << mapping.SX(pos[0]) << "," << mapping.SY(pos[1]);
188 }
189 if (cyclic[curve_i]) {
190 f << " Z";
191 }
192 }
193
194 f << "\"";
195 f << " fill-rule=\"evenodd\"";
196 f << "/>\n";
197 });
198}
199
200static bool draw_append = false; /* Will be set to true after first call. */
201
202static std::ofstream get_file_stream()
203{
204 constexpr const char *drawfile = "./boolean_curves_test_draw.html";
205
206 std::ofstream f;
207 if (draw_append) {
208 f.open(drawfile, std::ios_base::app);
209 }
210 else {
211 f.open(drawfile);
212 }
213 if (!f) {
214 std::cout << "Could not open file " << drawfile << "\n";
215 return f;
216 }
217
218 if (!draw_append) {
219 f << "<!DOCTYPE html>\n";
220
221 f << "<style>\n";
222
224
225 f << "</style>\n";
226 }
227
228 draw_append = true;
229
230 return f;
231}
232
233static void draw_divider_start(const std::string &label)
234{
235 if (!DO_DRAW) {
236 return;
237 }
238
239 std::ofstream f = get_file_stream();
240 if (!f) {
241 return;
242 }
243
244 f << "<div class=\"ui-group\">\n";
245 f << "<h1>" << label << "</h1>\n";
246 f << "<div class=\"ui-list\">\n";
247}
248
249static void draw_divider_end()
250{
251 if (!DO_DRAW) {
252 return;
253 }
254
255 std::ofstream f = get_file_stream();
256 if (!f) {
257 return;
258 }
259
260 /* Exit `ui-list` */
261 f << "</div>\n";
262 /* Exit `ui-group` */
263 f << "</div>\n";
264}
265
267{
268 return float2(position.x, position.y);
269};
270
271static void draw_results(const std::string &label,
272 const std::string &type,
273 const bke::CurvesGeometry &src_curves,
274 const bke::CurvesGeometry &dst_curves,
275 const IndexMask &clipping_shapes)
276{
277 using namespace bke::greasepencil;
278
279 if (!DO_DRAW) {
280 return;
281 }
282
283 std::ofstream f = get_file_stream();
284 if (!f) {
285 return;
286 }
287
288 const OffsetIndices<int> src_points_by_curve = src_curves.points_by_curve();
289 const OffsetIndices<int> dst_points_by_curve = dst_curves.points_by_curve();
290 const VArraySpan<bool> src_cyclic = src_curves.cyclic();
291 const VArraySpan<bool> dst_cyclic = dst_curves.cyclic();
292
293 const bke::AttributeAccessor src_attributes = src_curves.attributes();
294 const bke::AttributeAccessor dst_attributes = dst_curves.attributes();
295
296 Array<float2> src_positions_2d(src_curves.points_num());
297 const Span<float3> src_positions = src_curves.positions();
298 for (const int i : src_curves.points_range()) {
299 src_positions_2d[i] = project_fn(src_positions[i]);
300 }
301
302 Array<float2> dst_positions_2d(dst_curves.points_num());
303 const Span<float3> dst_positions = dst_curves.positions();
304 for (const int i : dst_curves.points_range()) {
305 dst_positions_2d[i] = project_fn(dst_positions[i]);
306 }
307
308 const VArray<int> src_fill_ids = *src_attributes.lookup<int>("fill_id", bke::AttrDomain::Curve);
309 const VArray<int> dst_fill_ids = *dst_attributes.lookup<int>("fill_id", bke::AttrDomain::Curve);
310
311 const ShapeData src_shape_data = shapes_from_fill_ids(src_fill_ids, src_curves.curves_num());
312 const ShapeData dst_shape_data = shapes_from_fill_ids(dst_fill_ids, dst_curves.curves_num());
313
314 const GroupedSpan<int> src_shapes = src_shape_data.shapes();
315 const GroupedSpan<int> dst_shapes = dst_shape_data.shapes();
316
317 IndexMaskMemory memory;
318 const IndexMask subject_shapes = clipping_shapes.complement(src_shapes.index_range(), memory);
319
320 const SVGMapping mapping = SVGMapping(*bounds::min_max(src_positions_2d.as_span()));
321
322 f << "<div>\n";
323 f << "<svg width=\"" << mapping.view_width << "\" height=\"" << mapping.view_height << "\">\n";
324
325 SVG_add_path(f,
326 type + "-A",
327 src_positions_2d,
328 src_shapes,
329 subject_shapes,
330 src_points_by_curve,
331 src_cyclic,
332 mapping);
333 SVG_add_path(f,
334 type + "-B",
335 src_positions_2d,
336 src_shapes,
337 clipping_shapes,
338 src_points_by_curve,
339 src_cyclic,
340 mapping);
341
342 SVG_add_path(f,
343 type + "-C",
344 dst_positions_2d,
345 dst_shapes,
346 dst_shapes.index_range(),
347 dst_points_by_curve,
348 dst_cyclic,
349 mapping);
350
351 f << "</svg>\n";
352 f << "<h2>" << label << "</h2>\n";
353 f << "</div>\n";
354}
355
357 const Span<float2> points,
358 const Span<int> fill_ids,
359 const Span<bool> cyclic)
360{
361 BLI_assert(!offsets.is_empty());
362 const int curves_num = offsets.size() - 1;
363 const int points_num = offsets.last();
364 BLI_assert(cyclic.size() == curves_num);
365 BLI_assert(fill_ids.size() == curves_num);
366
367 bke::CurvesGeometry curves(points_num, curves_num);
368 curves.offsets_for_write().copy_from(offsets);
369 curves.cyclic_for_write().copy_from(cyclic);
370
371 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
372
373 bke::SpanAttributeWriter<int> fill_id_writer = attributes.lookup_or_add_for_write_span<int>(
374 "fill_id", bke::AttrDomain::Curve);
375 fill_id_writer.span.copy_from(fill_ids);
376 fill_id_writer.finish();
377
378 MutableSpan<float3> positions = curves.positions_for_write();
379 for (const int i : curves.points_range()) {
380 positions[i] = float3(points[i], 0.0f);
381 }
382
383 return curves;
384}
385
387 const Array<Vector<float2>> &expected_points)
388{
389 Array<float2> dst_positions_2d(dst_curves.points_num());
390 const Span<float3> dst_positions = dst_curves.positions();
391 for (const int i : dst_curves.points_range()) {
392 dst_positions_2d[i] = project_fn(dst_positions[i]);
393 }
394
395 const OffsetIndices<int> points_by_curve = dst_curves.points_by_curve();
396
397 EXPECT_EQ(dst_curves.curves_num(), expected_points.size());
398 if (dst_curves.curves_num() != expected_points.size()) {
399 return;
400 }
401
402 int total_size = 0;
403 for (const int i : expected_points.index_range()) {
404 total_size += expected_points[i].size();
405 }
406
407 EXPECT_EQ(dst_positions_2d.size(), total_size);
408 if (dst_positions_2d.size() != total_size) {
409 return;
410 }
411
412 for (const int curve_i : points_by_curve.index_range()) {
413 const IndexRange points = points_by_curve[curve_i];
414
415 const Span<float2> expected_sub_points = expected_points[curve_i];
416
417 EXPECT_EQ(expected_sub_points.size(), points.size());
418 if (expected_sub_points.size() != points.size()) {
419 return;
420 }
421
422 for (const int i : points.index_range()) {
423 const float2 &point = dst_positions_2d[points[i]];
424 const float2 &expected_point = expected_sub_points[i];
425
426 EXPECT_NEAR(point.x, expected_point.x, 1e-4);
427 EXPECT_NEAR(point.y, expected_point.y, 1e-4);
428 }
429 }
430}
431
433 const bke::CurvesGeometry &src_curves,
434 const Span<int> fill_ids,
435 const IndexMask &clipping_shapes)
436{
437 using namespace bke::greasepencil;
438 CurveBooleanOpParameters op_params;
439 op_params.boolean_mode = opt;
440 op_params.keep_caps = true;
441 op_params.skip_clipping_attributes = false;
442 op_params.separate_islands = false;
443
444 const ShapeData shapes_data = shapes_from_fill_ids(VArray<int>::from_span(fill_ids),
445 src_curves.curves_num());
446 const GroupedSpan<int> shapes = shapes_data.shapes();
447
448 return curve_boolean(
449 op_params, src_curves, project_fn, shapes, shapes.index_range(), clipping_shapes);
450}
451
453
455{
456 draw_divider_start("Squares");
457
458 const Array<float2> points = {{0, 0}, {2, 0}, {2, 2}, {0, 2}, {1, 1}, {3, 1}, {3, 3}, {1, 3}};
459 const Array<int> points_by_curve = {0, 4, 8};
460 const Array<int> fill_ids = {1, 2};
461 const Array<bool> is_cyclic = {true, true};
462 const IndexRange clipping_shapes = IndexRange(1, 1);
463
464 const bke::CurvesGeometry src_curves = create_test_curves(
465 points_by_curve, points, fill_ids, is_cyclic);
466
467 {
468 const bke::CurvesGeometry dst_curves = test_curve_boolean(
469 Operation::Intersect, src_curves, fill_ids, clipping_shapes);
470
471 const Array<Vector<float2>> expected_points = {{{2, 1}, {2, 2}, {1, 2}, {1, 1}}};
472 expect_boolean_result_coord(dst_curves, expected_points);
473
474 draw_results("Intersection", "polygon", src_curves, dst_curves, clipping_shapes);
475 }
476 {
477 const bke::CurvesGeometry dst_curves = test_curve_boolean(
478 Operation::Union, src_curves, fill_ids, clipping_shapes);
479
480 const Array<Vector<float2>> expected_points = {
481 {{1, 2}, {0, 2}, {0, 0}, {2, 0}, {2, 1}, {3, 1}, {3, 3}, {1, 3}}};
482 expect_boolean_result_coord(dst_curves, expected_points);
483
484 draw_results("Union", "polygon", src_curves, dst_curves, clipping_shapes);
485 }
486 {
487 const bke::CurvesGeometry dst_curves = test_curve_boolean(
488 Operation::Difference, src_curves, fill_ids, clipping_shapes);
489
490 const Array<Vector<float2>> expected_points = {
491 {{1, 2}, {0, 2}, {0, 0}, {2, 0}, {2, 1}, {1, 1}}};
492 expect_boolean_result_coord(dst_curves, expected_points);
493
494 draw_results("Difference", "polygon", src_curves, dst_curves, clipping_shapes);
495 }
496
498}
499
501{
502 draw_divider_start("Simple");
503
509 const Array<float2> points = {
510 {0, 6}, {8, 6}, {8, 3}, {0, 3}, {6, 0}, {6, 4}, {4, 2}, {2, 4}, {2, 0}};
511 const Array<int> points_by_curve = {0, 4, 9};
512 const Array<bool> is_cyclic = {true, true};
513 const Array<int> fill_ids = {1, 2};
514 const IndexRange clipping_shapes = IndexRange(1, 1);
515
516 const bke::CurvesGeometry src_curves = create_test_curves(
517 points_by_curve, points, fill_ids, is_cyclic);
518
519 {
520 const bke::CurvesGeometry dst_curves = test_curve_boolean(
521 Operation::Intersect, src_curves, fill_ids, clipping_shapes);
522
523 const Array<Vector<float2>> expected_points = {{{6, 3}, {5, 3}, {6, 4}},
524 {{3, 3}, {2, 3}, {2, 4}}};
525 expect_boolean_result_coord(dst_curves, expected_points);
526
527 draw_results("Intersection", "polygon", src_curves, dst_curves, clipping_shapes);
528 }
529 {
530 const bke::CurvesGeometry dst_curves = test_curve_boolean(
531 Operation::Union, src_curves, fill_ids, clipping_shapes);
532
533 const Array<Vector<float2>> expected_points = {
534 {{2, 3}, {0, 3}, {0, 6}, {8, 6}, {8, 3}, {6, 3}, {6, 0}, {2, 0}},
535 {{5, 3}, {3, 3}, {4, 2}}};
536 expect_boolean_result_coord(dst_curves, expected_points);
537
538 draw_results("Union", "polygon", src_curves, dst_curves, clipping_shapes);
539 }
540 {
541 const bke::CurvesGeometry dst_curves = test_curve_boolean(
542 Operation::Difference, src_curves, fill_ids, clipping_shapes);
543
544 const Array<Vector<float2>> expected_points = {
545 {{2, 3}, {0, 3}, {0, 6}, {8, 6}, {8, 3}, {6, 3}, {6, 4}, {5, 3}, {3, 3}, {2, 4}}};
546 expect_boolean_result_coord(dst_curves, expected_points);
547
548 draw_results("Difference", "polygon", src_curves, dst_curves, clipping_shapes);
549 }
550
552}
553
555{
556 draw_divider_start("Complex");
557
563 const Array<float2> points = {
564 {14, 1}, {0, 5}, {14, 10}, {5, 6}, {14, 6}, {5, 5}, {9, 13}, {13, 0}, {9, 9}, {6, 0}};
565 const Array<int> points_by_curve = {0, 6, 10};
566 const Array<bool> is_cyclic = {true, true};
567 const Array<int> fill_ids = {1, 2};
568 const IndexRange clipping_shapes = IndexRange(1, 1);
569
570 const bke::CurvesGeometry src_curves = create_test_curves(
571 points_by_curve, points, fill_ids, is_cyclic);
572
573 {
574 const bke::CurvesGeometry dst_curves = test_curve_boolean(
575 Operation::Intersect, src_curves, fill_ids, clipping_shapes);
576
577 const Array<Vector<float2>> expected_points = {
578 {{12.5662, 1.4096}, {12.3454, 1.4727}, {12.2, 1.8}, {12.4851, 1.6732}},
579 {{7, 3}, {6.7113, 3.0824}, {6.9534, 4.1317}, {7.3225, 3.9677}},
580 {{7.7964, 7.7844}, {8.7027, 8.1081}, {8.5217, 7.5652}, {7.6571, 7.1809}},
581 {{9.3013, 8.3219}, {10.3267, 8.6881}, {10.4135, 8.4060}, {9.4536, 7.9793}},
582 {{7.3846, 6}, {8, 6}, {7.7692, 5.3076}, {7.2105, 5.2456}},
583 {{10.3333, 6}, {11.1538, 6}, {11.2479, 5.6942}, {10.5058, 5.6117}}};
584 expect_boolean_result_coord(dst_curves, expected_points);
585
586 draw_results("Intersection", "polygon", src_curves, dst_curves, clipping_shapes);
587 }
588 {
589 const bke::CurvesGeometry dst_curves = test_curve_boolean(
590 Operation::Union, src_curves, fill_ids, clipping_shapes);
591
592 const Array<Vector<float2>> expected_points = {
593 {{12.4851, 1.6732},
594 {14, 1},
595 {12.5662, 1.4096},
596 {13, 0},
597 {12.3454, 1.4727},
598 {7, 3},
599 {6, 0},
600 {6.7113, 3.0824},
601 {0, 5},
602 {7.7964, 7.7844},
603 {9, 13},
604 {10.3267, 8.6881},
605 {14, 10},
606 {10.4135, 8.4060},
607 {11.1538, 6},
608 {14, 6},
609 {11.2479, 5.6942}},
610 {{8.7027, 8.1081}, {9.3013, 8.3219}, {9, 9}},
611 {{9.4536, 7.9793}, {8.5217, 7.5652}, {8, 6}, {10.3333, 6}},
612 {{7.6571, 7.1809}, {5, 6}, {7.3846, 6}},
613 {{10.5058, 5.6117}, {7.7692, 5.3076}, {7.3225, 3.9677}, {12.2, 1.8}},
614 {{7.2105, 5.2456}, {5, 5}, {6.9534, 4.1317}}};
615 expect_boolean_result_coord(dst_curves, expected_points);
616
617 draw_results("Union", "polygon", src_curves, dst_curves, clipping_shapes);
618 }
619 {
620 const bke::CurvesGeometry dst_curves = test_curve_boolean(
621 Operation::Difference, src_curves, fill_ids, clipping_shapes);
622
623 const Array<Vector<float2>> expected_points = {
624 {{12.4851, 1.6732}, {14, 1}, {12.5662, 1.4096}},
625 {{12.3454, 1.4727}, {7, 3}, {7.3225, 3.9677}, {12.2, 1.8}},
626 {{6.7113, 3.0824},
627 {0, 5},
628 {7.7964, 7.7844},
629 {7.6571, 7.1809},
630 {5, 6},
631 {7.3846, 6},
632 {7.2105, 5.2456},
633 {5, 5},
634 {6.9534, 4.1317}},
635 {{8.7027, 8.1081}, {9.3013, 8.3219}, {9.4536, 7.9793}, {8.5217, 7.5652}},
636 {{10.3267, 8.6881}, {14, 10}, {10.4135, 8.4060}},
637 {{8, 6}, {10.3333, 6}, {10.5058, 5.6117}, {7.7692, 5.3076}},
638 {{11.1538, 6}, {14, 6}, {11.2479, 5.6942}}};
639 expect_boolean_result_coord(dst_curves, expected_points);
640
641 draw_results("Difference", "polygon", src_curves, dst_curves, clipping_shapes);
642 }
643
645}
646
648{
649 draw_divider_start("Last Edge Loop");
650
658 const Array<float2> points = {
659 {0, 5}, {0, 0}, {7, 0}, {7, 5}, {2, 3}, {0, 7}, {3, 7}, {6, 3}, {7, 6}, {3, 3}, {2, 6}};
660 const Array<int> points_by_curve = {0, 4, 11};
661 const Array<bool> is_cyclic = {true, true};
662 const Array<int> fill_ids = {1, 2};
663 const IndexRange clipping_shapes = IndexRange(1, 1);
664
665 const bke::CurvesGeometry src_curves = create_test_curves(
666 points_by_curve, points, fill_ids, is_cyclic);
667
668 {
669 const bke::CurvesGeometry dst_curves = test_curve_boolean(
670 Operation::Intersect, src_curves, fill_ids, clipping_shapes);
671
672 const Array<Vector<float2>> expected_points = {
673 {{6.6666, 5}, {5.6666, 5}, {3, 3}, {2.3333, 5}, {4.5000, 5}, {6, 3}},
674 {{2, 5}, {1, 5}, {2, 3}}};
675 expect_boolean_result_coord(dst_curves, expected_points);
676
677 draw_results("Intersection", "polygon", src_curves, dst_curves, clipping_shapes);
678 }
679 {
680 const bke::CurvesGeometry dst_curves = test_curve_boolean(
681 Operation::Union, src_curves, fill_ids, clipping_shapes);
682
683 const Array<Vector<float2>> expected_points = {{{1, 5},
684 {0, 5},
685 {0, 0},
686 {7, 0},
687 {7, 5},
688 {6.6666, 5},
689 {7, 6},
690 {5.6666, 5},
691 {4.5, 5},
692 {3, 7},
693 {0, 7}},
694 {{2.3333, 5}, {2, 5}, {2, 6}}};
695 expect_boolean_result_coord(dst_curves, expected_points);
696
697 draw_results("Union", "polygon", src_curves, dst_curves, clipping_shapes);
698 }
699 {
700 const bke::CurvesGeometry dst_curves = test_curve_boolean(
701 Operation::Difference, src_curves, fill_ids, clipping_shapes);
702
703 const Array<Vector<float2>> expected_points = {{{1, 5},
704 {0, 5},
705 {0, 0},
706 {7, 0},
707 {7, 5},
708 {6.6666, 5},
709 {6, 3},
710 {4.5, 5},
711 {5.6666, 5},
712 {3, 3},
713 {2.3333, 5},
714 {2, 5},
715 {2, 3}}};
716 expect_boolean_result_coord(dst_curves, expected_points);
717
718 draw_results("Difference", "polygon", src_curves, dst_curves, clipping_shapes);
719 }
720
722}
723
725{
726 draw_divider_start("Cuts");
727
728 {
729 const Array<float2> points = {
730 {5, 7}, {3, 6}, {0, 2}, {0, 0}, {1, 6}, {3, 4}, {3, 1}, {0, 4}, {2, 3}};
731 const Array<int> points_by_curve = {0, 4, 9};
732 const Array<bool> is_cyclic = {false, true};
733 const Array<int> fill_ids = {0, 1};
734 const IndexRange clipping_shapes = IndexRange(1, 1);
735
736 const bke::CurvesGeometry src_curves = create_test_curves(
737 points_by_curve, points, fill_ids, is_cyclic);
738
739 const bke::CurvesGeometry dst_curves = test_curve_boolean(
740 Operation::Difference, src_curves, fill_ids, clipping_shapes);
741
742 const Array<Vector<float2>> expected_points = {{{5, 7}, {3, 6}, {2.14286, 4.85714}},
743 {{1.61538, 4.15385}, {1.09091, 3.45455}},
744 {{0.857143, 3.14286}, {0, 2}, {0, 0}}};
745 expect_boolean_result_coord(dst_curves, expected_points);
746
747 draw_results("Simple Cut 1", "cut", src_curves, dst_curves, clipping_shapes);
748 }
749 {
750 const Array<float2> points = {{5, 5}, {3, 5}, {1, 3}, {1, 1}, {5, 6}, {6, 5}, {1, 0}, {0, 1}};
751 const Array<int> points_by_curve = {0, 4, 8};
752 const Array<bool> is_cyclic = {false, true};
753 const Array<int> fill_ids = {0, 1};
754 const IndexRange clipping_shapes = IndexRange(1, 1);
755
756 const bke::CurvesGeometry src_curves = create_test_curves(
757 points_by_curve, points, fill_ids, is_cyclic);
758
759 const bke::CurvesGeometry dst_curves = test_curve_boolean(
760 Operation::Difference, src_curves, fill_ids, clipping_shapes);
761
762 const Array<Vector<float2>> expected_points = {{{4, 5}, {3, 5}, {1, 3}, {1, 2}}};
763 expect_boolean_result_coord(dst_curves, expected_points);
764
765 draw_results("Simple Cut 2", "cut", src_curves, dst_curves, clipping_shapes);
766 }
767 {
768 const Array<float2> points = {{6, 8},
769 {4, 7},
770 {1, 3},
771 {1, 1},
772 {3, 7},
773 {5, 5},
774 {1, 0},
775 {0, 4},
776 {2, 3},
777 {1, 5},
778 {3, 4},
779 {2, 6},
780 {4, 5}};
781 const Array<int> points_by_curve = {0, 4, 13};
782 const Array<bool> is_cyclic = {false, true};
783 const Array<int> fill_ids = {0, 1};
784 const IndexRange clipping_shapes = IndexRange(1, 1);
785
786 const bke::CurvesGeometry src_curves = create_test_curves(
787 points_by_curve, points, fill_ids, is_cyclic);
788
789 const bke::CurvesGeometry dst_curves = test_curve_boolean(
790 Operation::Difference, src_curves, fill_ids, clipping_shapes);
791
792 const Array<Vector<float2>> expected_points = {{{6, 8}, {4, 7}, {3.57143, 6.42857}},
793 {{3.4, 6.2}, {2.90909, 5.54545}},
794 {{2.5, 5}, {2.09091, 4.45455}},
795 {{1.6, 3.8}, {1.27273, 3.36364}}};
796 expect_boolean_result_coord(dst_curves, expected_points);
797
798 draw_results("Simple Cut 3", "cut", src_curves, dst_curves, clipping_shapes);
799 }
800 {
801 const Array<float2> points = {{6, 7},
802 {4, 6},
803 {1, 2},
804 {1, 0},
805 {0, 4},
806 {2, 2},
807 {7, 8},
808 {3, 7},
809 {4, 5},
810 {2, 6},
811 {3, 4},
812 {1, 5},
813 {2, 3}};
814 const Array<int> points_by_curve = {0, 4, 13};
815 const Array<bool> is_cyclic = {false, true};
816 const Array<int> fill_ids = {0, 1};
817 const IndexRange clipping_shapes = IndexRange(1, 1);
818
819 const bke::CurvesGeometry src_curves = create_test_curves(
820 points_by_curve, points, fill_ids, is_cyclic);
821
822 const bke::CurvesGeometry dst_curves = test_curve_boolean(
823 Operation::Difference, src_curves, fill_ids, clipping_shapes);
824
825 const Array<Vector<float2>> expected_points = {{{3.7, 5.6}, {3.45455, 5.27273}},
826 {{2.8, 4.4}, {2.63636, 4.18182}},
827 {{1.9, 3.2}, {1.81818, 3.09091}},
828 {{1.42857, 2.57143}, {1, 2}, {1, 0}}};
829 expect_boolean_result_coord(dst_curves, expected_points);
830
831 draw_results("Simple Cut 4", "cut", src_curves, dst_curves, clipping_shapes);
832 }
833 {
834 const Array<float2> points = {
835 {6, 5}, {4, 5}, {1, 2}, {1, 0}, {1, 4}, {3, 1}, {5, 3}, {2, 5}, {3, 3}};
836 const Array<int> points_by_curve = {0, 4, 9};
837 const Array<bool> is_cyclic = {true, true};
838 const Array<int> fill_ids = {0, 1};
839 const IndexRange clipping_shapes = IndexRange(1, 1);
840
841 const bke::CurvesGeometry src_curves = create_test_curves(
842 points_by_curve, points, fill_ids, is_cyclic);
843
844 const bke::CurvesGeometry dst_curves = test_curve_boolean(
845 Operation::Difference, src_curves, fill_ids, clipping_shapes);
846
847 const Array<Vector<float2>> expected_points = {{{4.4, 3.4}, {6, 5}, {4, 5}, {3.2, 4.2}},
848 {{2.66667, 3.66667}, {2.33333, 3.33333}},
849 {{1.8, 2.8}, {1, 2}, {1, 0}, {2.6, 1.6}}};
850 expect_boolean_result_coord(dst_curves, expected_points);
851
852 draw_results("Cyclical Cut", "cut", src_curves, dst_curves, clipping_shapes);
853 }
855}
856
858{
859 draw_divider_start("Square With Hole");
860
861 const Array<float2> points = {{0, 0},
862 {0, 5},
863 {5, 5},
864 {5, 0},
865
866 {1, 1},
867 {1, 4},
868 {4, 4},
869 {4, 1},
870
871 {2, 2},
872 {2, 7},
873 {7, 7},
874 {7, 2}};
875 const Array<int> points_by_curve = {0, 4, 8, 12};
876 const Array<bool> is_cyclic = {true, true, true};
877 const Array<int> fill_ids = {1, 1, 2};
878 const IndexRange clipping_shapes = IndexRange::from_begin_end(1, 2);
879
880 const bke::CurvesGeometry src_curves = create_test_curves(
881 points_by_curve, points, fill_ids, is_cyclic);
882
883 {
884 const bke::CurvesGeometry dst_curves = test_curve_boolean(
885 Operation::Difference, src_curves, fill_ids, clipping_shapes);
886
887 const Array<Vector<float2>> expected_points = {
888 {{5, 2}, {5, 0}, {0, 0}, {0, 5}, {2, 5}, {2, 4}, {1, 4}, {1, 1}, {4, 1}, {4, 2}}};
889 expect_boolean_result_coord(dst_curves, expected_points);
890
891 draw_results("Difference", "polygon", src_curves, dst_curves, clipping_shapes);
892 }
894}
895
896TEST_F(GreasePencilBooleanTest, Squares_With_Holes)
897{
898 draw_divider_start("Squares With Holes");
899
900 const Array<float2> points = {{0, 0},
901 {0, 5},
902 {5, 5},
903 {5, 0},
904
905 {1, 1},
906 {1, 4},
907 {4, 4},
908 {4, 1},
909
910 {2, 2},
911 {2, 7},
912 {7, 7},
913 {7, 2},
914
915 {3, 3},
916 {3, 6},
917 {6, 6},
918 {6, 3}};
919 const Array<int> points_by_curve = {0, 4, 8, 12, 16};
920 const Array<bool> is_cyclic = {true, true, true, true};
921 const Array<int> fill_ids = {1, 1, 2, 2};
922 const IndexRange clipping_shapes = IndexRange::from_begin_end(1, 2);
923
924 const bke::CurvesGeometry src_curves = create_test_curves(
925 points_by_curve, points, fill_ids, is_cyclic);
926
927 {
928 const bke::CurvesGeometry dst_curves = test_curve_boolean(
929 Operation::Difference, src_curves, fill_ids, clipping_shapes);
930
931 const Array<Vector<float2>> expected_points = {
932 {{5, 2}, {5, 0}, {0, 0}, {0, 5}, {2, 5}, {2, 4}, {1, 4}, {1, 1}, {4, 1}, {4, 2}},
933 {{3, 5}, {5, 5}, {5, 3}, {4, 3}, {4, 4}, {3, 4}}};
934 expect_boolean_result_coord(dst_curves, expected_points);
935
936 draw_results("Difference", "polygon", src_curves, dst_curves, clipping_shapes);
937 }
939}
940
942{
943 draw_divider_start("Multiple Shapes");
944
945 const Array<float2> points = {{0, 2},
946 {0, 7},
947 {5, 7},
948 {5, 2},
949
950 {2, 0},
951 {2, 5},
952 {7, 5},
953 {7, 0},
954
955 {3, 3},
956 {3, 8},
957 {8, 8},
958 {8, 3}};
959 const Array<int> points_by_curve = {0, 4, 8, 12};
960 const Array<bool> is_cyclic = {true, true, true};
961 const Array<int> fill_ids = {1, 2, 3};
962
963 const bke::CurvesGeometry src_curves = create_test_curves(
964 points_by_curve, points, fill_ids, is_cyclic);
965
970 {
971 const IndexRange clipping_shapes = IndexRange::from_begin_end(2, 3);
972 const bke::CurvesGeometry dst_curves = test_curve_boolean(
973 Operation::Intersect, src_curves, fill_ids, clipping_shapes);
974
975 const Array<Vector<float2>> expected_points = {{{3, 7}, {5, 7}, {5, 3}, {3, 3}},
976 {{3, 5}, {7, 5}, {7, 3}, {3, 3}}};
977 expect_boolean_result_coord(dst_curves, expected_points);
978
979 draw_results("2 Subjects Intersection", "polygon", src_curves, dst_curves, clipping_shapes);
980 }
981 {
982 const IndexRange clipping_shapes = IndexRange::from_begin_end(2, 3);
983 const bke::CurvesGeometry dst_curves = test_curve_boolean(
984 Operation::Difference, src_curves, fill_ids, clipping_shapes);
985
986 const Array<Vector<float2>> expected_points = {
987 {{5, 3}, {5, 2}, {0, 2}, {0, 7}, {3, 7}, {3, 3}},
988 {{7, 3}, {7, 0}, {2, 0}, {2, 5}, {3, 5}, {3, 3}}};
989 expect_boolean_result_coord(dst_curves, expected_points);
990
991 draw_results("2 Subjects Difference", "polygon", src_curves, dst_curves, clipping_shapes);
992 }
994}
995
996} // namespace blender::ed::greasepencil::tests
Low-level operations for curves.
Read Blender memory allocation and freeing.
int64_t size() const
Definition BLI_array.hh:267
Span< T > as_span() const
Definition BLI_array.hh:254
constexpr int64_t size() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr IndexRange()=default
constexpr IndexRange index_range() const
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
static VArray from_span(Span< T > values)
GAttributeReader lookup(const StringRef name) const
VArray< bool > cyclic() const
AttributeAccessor attributes() const
Span< float3 > positions() const
IndexRange points_range() const
OffsetIndices< int > points_by_curve() const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
#define position
#define EXPECT_NEAR(result, expect, threshold)
#define DO_DRAW
ShapeData shapes_from_fill_ids(const VArray< int > &fill_ids, int curves_num)
std::optional< Bounds< T > > min_max(const std::optional< Bounds< T > > &a, const T &b)
Definition BLI_bounds.hh:56
bke::CurvesGeometry curve_boolean(const CurveBooleanOpParameters op_params, const bke::CurvesGeometry &curves, const ProjectionFunc project_fn, const GroupedSpan< int > shapes, const IndexMask &editable_shapes, const IndexMask &clipping_shapes)
TEST_F(GreasePencilBooleanTest, Squares)
static void SVG_add_path(std::ofstream &f, const std::string &class_name, const Span< float2 > &positions, const GroupedSpan< int > shapes, const IndexMask &shape_mask, const OffsetIndices< int > points_by_curve, const VArraySpan< bool > &cyclic, const SVGMapping &mapping)
static bke::CurvesGeometry test_curve_boolean(const Operation opt, const bke::CurvesGeometry &src_curves, const Span< int > fill_ids, const IndexMask &clipping_shapes)
static void CSS_setup_style(std::ofstream &f)
static bke::CurvesGeometry create_test_curves(const Span< int > offsets, const Span< float2 > points, const Span< int > fill_ids, const Span< bool > cyclic)
static void draw_results(const std::string &label, const std::string &type, const bke::CurvesGeometry &src_curves, const bke::CurvesGeometry &dst_curves, const IndexMask &clipping_shapes)
static void draw_divider_start(const std::string &label)
static float2 project_fn(const float3 position)
static void expect_boolean_result_coord(const bke::CurvesGeometry &dst_curves, const Array< Vector< float2 > > &expected_points)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
VecBase< float, 2 > float2
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
VecBase< float, 3 > float3
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void pad(const PaddingT &padding)
const GroupedSpan< int > shapes() const