Blender V5.3
grease_pencil_trim_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "BKE_attribute.hh"
8#include "BKE_curves.hh"
10#include "BKE_gtest_base.hh"
11#include "BKE_idtype.hh"
12#include "BKE_lib_id.hh"
13#include "BKE_main.hh"
14
15#include "BLI_array.hh"
16#include "BLI_bounds.hh"
17#include "BLI_lasso_2d.hh"
18#include "BLI_vector.hh"
19#include "BLI_virtual_array.hh"
20
21#include "ED_grease_pencil.hh"
22#include "ED_view3d.hh"
23
25
27 const Span<float2> positions_2d,
28 const Span<bool> cyclic)
29{
30 BLI_assert(!offsets.is_empty());
31 const int curves_num = offsets.size() - 1;
32 BLI_assert(cyclic.size() == curves_num);
33 const int points_num = offsets.last();
34
35 bke::CurvesGeometry curves(points_num, curves_num);
36 curves.offsets_for_write().copy_from(offsets);
37 curves.cyclic_for_write().copy_from(cyclic);
38
39 MutableSpan<float3> positions = curves.positions_for_write();
40 for (const int i : curves.points_range()) {
41 positions[i] = float3(positions_2d[i], 0.0f);
42 }
43
44 return curves;
45}
46
47static void expect_near_positions(const Span<float3> actual, const Span<float2> expected)
48{
49 EXPECT_EQ(expected.size(), actual.size());
50 if (expected.size() != actual.size()) {
51 return;
52 }
53
54 for (const int64_t i : expected.index_range()) {
55 EXPECT_NEAR(expected[i].x, actual[i].x, 1e-4) << "X mismatch at index " << i;
56 EXPECT_NEAR(expected[i].y, actual[i].y, 1e-4) << "Y mismatch at index " << i;
57 }
58}
59
61
63{
64 using namespace bke::greasepencil;
65 using namespace bke;
66
67 const Array<int2> mcoords = {{-10, 50}, {10, 50}, {10, 10}, {50, 10}, {50, -10}, {-10, -10}};
68 const Array<int> src_offsets = {0, 2, 4, 6, 8};
69 const Array<bool> src_cyclic = {false, false, false, false};
70 const Array<float2> screen_space_positions = {{20.0f, 0.0f},
71 {20.0f, 60.0f},
72 {40.0f, 0.0f},
73 {40.0f, 60.0f},
74 {0.0f, 20.0f},
75 {60.0f, 20.0f},
76 {0.0f, 40.0f},
77 {60.0f, 40.0f}};
79 src_offsets, screen_space_positions, src_cyclic);
81 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
82
83 const Array<float2> expected_positions = {{20.0f, 20.0f},
84 {20.0f, 60.0f},
85 {40.0f, 20.0f},
86 {40.0f, 60.0f},
87 {20.0f, 20.0f},
88 {60.0f, 20.0f},
89 {20.0f, 40.0f},
90 {60.0f, 40.0f}};
91 expect_near_positions(dst.positions(), expected_positions);
92}
93
95{
96 using namespace bke::greasepencil;
97 using namespace bke;
98
99 const Array<int2> mcoords = {{10, 35}, {50, 35}, {50, 25}, {10, 25}};
100 const Array<int> src_offsets = {0, 2, 4, 6, 8};
101 const Array<bool> src_cyclic = {false, false, false, false};
102 const Array<float2> screen_space_positions = {{20.0f, 0.0f},
103 {20.0f, 60.0f},
104 {40.0f, 0.0f},
105 {40.0f, 60.0f},
106 {0.0f, 20.0f},
107 {60.0f, 20.0f},
108 {0.0f, 40.0f},
109 {60.0f, 40.0f}};
111 src_offsets, screen_space_positions, src_cyclic);
113 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
114
115 const Array<float2> expected_positions = {{20.0f, 0.0f},
116 {20.0f, 20.0f},
117 {20.0f, 40.0f},
118 {20.0f, 60.0f},
119 {40.0f, 0.0f},
120 {40.0f, 20.0f},
121 {40.0f, 40.0f},
122 {40.0f, 60.0f},
123 {0.0f, 20.0f},
124 {60.0f, 20.0f},
125 {0.0f, 40.0f},
126 {60.0f, 40.0f}};
127 expect_near_positions(dst.positions(), expected_positions);
128}
129
130TEST_F(GreasePencilTrimTest, trim_plus_intersection)
131{
132 using namespace bke::greasepencil;
133 using namespace bke;
134
135 const Array<int2> mcoords = {{20, -10}, {20, 10}, {40, 10}, {40, -10}};
136 const Array<int> src_offsets = {0, 4, 8};
137 const Array<bool> src_cyclic = {false, false};
138 const Array<float2> screen_space_positions = {{30.0f, 0.0f},
139 {30.0f, 20.0f},
140 {30.0f, 40.0f},
141 {30.0f, 60.0f},
142 {0.0f, 30.0f},
143 {20.0f, 30.0f},
144 {40.0f, 30.0f},
145 {60.0f, 30.0f}};
147 src_offsets, screen_space_positions, src_cyclic);
149 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
150
151 const Array<float2> expected_positions = {{30.0f, 30.0f},
152 {30.0f, 40.0f},
153 {30.0f, 60.0f},
154 {0.0f, 30.0f},
155 {20.0f, 30.0f},
156 {40.0f, 30.0f},
157 {60.0f, 30.0f}};
158 expect_near_positions(dst.positions(), expected_positions);
159}
160
161TEST_F(GreasePencilTrimTest, trim_t_intersection_to_corner)
162{
163 using namespace bke::greasepencil;
164 using namespace bke;
165
166 const Array<int2> mcoords = {{-10, 20}, {10, 20}, {10, 40}, {-10, 40}};
167 const Array<int> src_offsets = {0, 3, 7};
168 const Array<bool> src_cyclic = {false, false};
169 const Array<float2> screen_space_positions = {{30.0f, 0.0f},
170 {30.0f, 20.0f},
171 {30.0f, 30.0f},
172 {0.0f, 30.0f},
173 {20.0f, 30.0f},
174 {40.0f, 30.0f},
175 {60.0f, 30.0f}};
177 src_offsets, screen_space_positions, src_cyclic);
179 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
180
181 const Array<float2> expected_positions = {{30.0f, 0.0f},
182 {30.0f, 20.0f},
183 {30.0f, 30.0f},
184 {30.0f, 30.0f},
185 {40.0f, 30.0f},
186 {60.0f, 30.0f}};
187 expect_near_positions(dst.positions(), expected_positions);
188}
189
190TEST_F(GreasePencilTrimTest, trim_t_intersection_line)
191{
192 using namespace bke::greasepencil;
193 using namespace bke;
194 const Array<int2> mcoords = {{20, -10}, {20, 10}, {40, 10}, {40, -10}};
195 const Array<bool> src_cyclic = {false, false};
196
197 /* Intersection at the start. */
198 {
199 const Array<int> src_offsets = {0, 2, 4};
200 const Array<float2> screen_space_positions = {
201 {30.0f, 30.0f}, {30.0f, 0.0f}, {0.0f, 30.0f}, {60.0f, 30.0f}};
203 src_offsets, screen_space_positions, src_cyclic);
205 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
206
207 const Array<float2> expected_positions = {{0.0f, 30.0f}, {60.0f, 30.0f}};
208 expect_near_positions(dst.positions(), expected_positions);
209 }
210
211 /* Intersection at the end. */
212 {
213 const Array<int> src_offsets = {0, 2, 4};
214 const Array<float2> screen_space_positions = {
215 {30.0f, 0.0f}, {30.0f, 30.0f}, {0.0f, 30.0f}, {60.0f, 30.0f}};
217 src_offsets, screen_space_positions, src_cyclic);
219 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
220
221 const Array<float2> expected_positions = {{0.0f, 30.0f}, {60.0f, 30.0f}};
222 expect_near_positions(dst.positions(), expected_positions);
223 }
224}
225
226TEST_F(GreasePencilTrimTest, trim_figure_eight)
227{
228 using namespace bke::greasepencil;
229 using namespace bke;
230
231 const Array<int2> mcoords = {{40, 20}, {40, 40}, {60, 40}, {60, 20}};
232 const Array<int> src_offsets = {0, 8};
233 const Array<bool> src_cyclic = {true};
234 const Array<float2> screen_space_positions = {{0.0f, 10.0f},
235 {0.0f, 30.0f},
236 {20.0f, 30.0f},
237 {30.0f, 10.0f},
238 {50.0f, 10.0f},
239 {50.0f, 30.0f},
240 {30.0f, 30.0f},
241 {20.0f, 10.0f}};
243 src_offsets, screen_space_positions, src_cyclic);
245 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
246
247 const Array<float2> expected_positions = {{25.0f, 20.0f},
248 {20.0f, 10.0f},
249 {0.0f, 10.0f},
250 {0.0f, 30.0f},
251 {20.0f, 30.0f},
252 {25.0f, 20.0f}};
253 expect_near_positions(dst.positions(), expected_positions);
254}
255
256TEST_F(GreasePencilTrimTest, trim_no_geometry)
257{
258 using namespace bke::greasepencil;
259 using namespace bke;
260
261 const Array<int2> mcoords = {{0, 0}, {0, 5}, {5, 5}, {5, 0}};
262 const Array<int> src_offsets = {0, 2, 4};
263 const Array<bool> src_cyclic = {false, false};
264 const Array<float2> screen_space_positions = {
265 {10.0f, 10.0f}, {50.0f, 10.0f}, {10.0f, 50.0f}, {50.0f, 50.0f}};
267 src_offsets, screen_space_positions, src_cyclic);
269 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
270
271 const Array<float2> expected_positions = {
272 {10.0f, 10.0f}, {50.0f, 10.0f}, {10.0f, 50.0f}, {50.0f, 50.0f}};
273 expect_near_positions(dst.positions(), expected_positions);
274}
275
276TEST_F(GreasePencilTrimTest, trim_no_geometry_loop)
277{
278 using namespace bke::greasepencil;
279 using namespace bke;
280
281 const Array<int2> mcoords = {{0, 0}, {0, 5}, {5, 5}, {5, 0}};
282 const Array<int> src_offsets = {0, 3};
283 const Array<bool> src_cyclic = {true};
284 const Array<float2> screen_space_positions = {{10.0f, 10.0f}, {50.0f, 10.0f}, {10.0f, 50.0f}};
286 src_offsets, screen_space_positions, src_cyclic);
288 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
289
290 const Array<float2> expected_positions = {{10.0f, 10.0f}, {50.0f, 10.0f}, {10.0f, 50.0f}};
291 expect_near_positions(dst.positions(), expected_positions);
292 EXPECT_EQ(dst.cyclic()[0], true);
293}
294
295TEST_F(GreasePencilTrimTest, trim_cyclical_corner)
296{
297 using namespace bke::greasepencil;
298 using namespace bke;
299
300 const Array<int2> mcoords = {{20, 20}, {40, 20}, {20, 40}};
301 const Array<int> src_offsets = {0, 3, 6};
302 const Array<bool> src_cyclic = {false, true};
303 const Array<float2> screen_space_positions = {{40.0f, 10.0f},
304 {10.0f, 10.0f},
305 {10.0f, 40.0f},
306 {0.0f, 30.0f},
307 {30.0f, 30.0f},
308 {30.0f, 0.0f}};
310 src_offsets, screen_space_positions, src_cyclic);
312 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
313
314 const Array<float2> expected_positions = {{40.0f, 10.0f},
315 {10.0f, 10.0f},
316 {10.0f, 40.0f},
317 {30.0f, 10.0f},
318 {30.0f, 0.0f},
319 {0.0f, 30.0f},
320 {10.0f, 30.0f}};
321 expect_near_positions(dst.positions(), expected_positions);
322}
323
324TEST_F(GreasePencilTrimTest, trim_no_geometry_edge_end_intersection)
325{
326 using namespace bke::greasepencil;
327 using namespace bke;
328
329 const Array<int2> mcoords = {{20, 10}, {20, 20}, {30, 20}, {30, 10}};
330 const Array<int> src_offsets = {0, 2, 5};
331 const Array<bool> src_cyclic = {false, false};
332 const Array<float2> screen_space_positions = {
333 {0.0f, 0.0f}, {20.0f, 0.0f}, {0.0f, 10.0f}, {10.0f, 20.0f}, {10.0f, 0.0f}};
335 src_offsets, screen_space_positions, src_cyclic);
337 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
338
339 const Array<float2> expected_positions = {
340 {0.0f, 0.0f}, {20.0f, 0.0f}, {0.0f, 10.0f}, {10.0f, 20.0f}, {10.0f, 0.0f}};
341 expect_near_positions(dst.positions(), expected_positions);
342 EXPECT_EQ(dst.cyclic()[0], false);
343 EXPECT_EQ(dst.cyclic()[1], false);
344}
345
346TEST_F(GreasePencilTrimTest, trim_no_geometry_cyclical_loop)
347{
348 using namespace bke::greasepencil;
349 using namespace bke;
350
351 const Array<int2> mcoords = {{40, 50}, {40, 40}, {50, 40}, {50, 50}};
352 const Array<int> src_offsets = {0, 4, 6};
353 const Array<bool> src_cyclic = {true, false};
354 const Array<float2> screen_space_positions = {
355 {0.0f, 0.0f}, {20.0f, 0.0f}, {20.0f, 20.0f}, {0.0f, 20.0f}, {10.0f, 10.0f}, {30.0f, 0.0f}};
357 src_offsets, screen_space_positions, src_cyclic);
359 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
360
361 const Array<float2> expected_positions = {
362 {0.0f, 0.0f}, {20.0f, 0.0f}, {20.0f, 20.0f}, {0.0f, 20.0f}, {10.0f, 10.0f}, {30.0f, 0.0f}};
363 expect_near_positions(dst.positions(), expected_positions);
364 EXPECT_EQ(dst.cyclic()[0], true);
365 EXPECT_EQ(dst.cyclic()[1], false);
366}
367
368TEST_F(GreasePencilTrimTest, trim_no_geometry_point_intersection)
369{
370 using namespace bke::greasepencil;
371 using namespace bke;
372
373 const Array<int2> mcoords = {{40, 50}, {40, 40}, {50, 40}, {50, 50}};
374 const Array<int> src_offsets = {0, 3, 6};
375 const Array<bool> src_cyclic = {false, false};
376 const Array<float2> screen_space_positions = {
377 {20.0f, 20.0f}, {10.0f, 10.0f}, {20.0f, 0.0f}, {0.0f, 20.0f}, {10.0f, 10.0f}, {0.0f, 0.0f}};
379 src_offsets, screen_space_positions, src_cyclic);
381 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
382
383 const Array<float2> expected_positions = {
384 {20.0f, 20.0f}, {10.0f, 10.0f}, {20.0f, 0.0f}, {0.0f, 20.0f}, {10.0f, 10.0f}, {0.0f, 0.0f}};
385 expect_near_positions(dst.positions(), expected_positions);
386 EXPECT_EQ(dst.cyclic()[0], false);
387 EXPECT_EQ(dst.cyclic()[1], false);
388}
389
390TEST_F(GreasePencilTrimTest, trim_no_geometry_self_intersection_degeneracy)
391{
392 using namespace bke::greasepencil;
393 using namespace bke;
394
395 const Array<int2> mcoords = {{40, 50}, {40, 40}, {50, 40}, {50, 50}};
396 const Array<int> src_offsets = {0, 6};
397 const Array<bool> src_cyclic = {true};
398 const Array<float2> screen_space_positions = {
399 {0.0f, 20.0f}, {10.0f, 10.0f}, {0.0f, 0.0f}, {0.0f, 20.0f}, {10.0f, 10.0f}, {0.0f, 0.0f}};
401 src_offsets, screen_space_positions, src_cyclic);
403 src, screen_space_positions, mcoords, src.curves_range(), src.curves_range(), true);
404
405 const Array<float2> expected_positions = {
406 {0.0f, 20.0f}, {10.0f, 10.0f}, {0.0f, 0.0f}, {0.0f, 20.0f}, {10.0f, 10.0f}, {0.0f, 0.0f}};
407 expect_near_positions(dst.positions(), expected_positions);
408 EXPECT_EQ(dst.cyclic()[0], true);
409}
410
411} // namespace blender::ed::greasepencil::tests
Low-level operations for curves.
Low-level operations for grease pencil.
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
VArray< bool > cyclic() const
Span< float3 > positions() const
IndexRange curves_range() const
#define EXPECT_NEAR(result, expect, threshold)
TEST_F(GreasePencilBooleanTest, Squares)
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 expect_near_positions(const Span< float3 > actual, const Span< float2 > expected)
bke::CurvesGeometry trim_curve_segments(const bke::CurvesGeometry &src, const Span< float2 > screen_space_positions, const Span< int2 > mcoords, const IndexMask &editable_curves, const IndexMask &visible_curves, const bool keep_caps)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
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