KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_grid_helper.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#define BOOST_TEST_NO_MAIN
19#include <boost/test/unit_test.hpp>
20
21#include <tool/grid_helper.h>
22#include <tool/snap_frame.h>
23
25{
26 helper.clearAnchors();
27}
28
29
30namespace
31{
32class TEST_GRID_HELPER : public GRID_HELPER
33{
34public:
37
38 const std::vector<SEG>& DimensionBrackets() { return getSnapManager().GetViewItem().DimensionBrackets(); }
39
40 size_t AnchorCount() const { return m_anchors.size(); }
41
42 void RetainSnapForTest( const VECTOR2I& aPosition )
43 {
44 m_snapItem.emplace( aPosition, ANCHOR_FLAGS::SNAPPABLE, POINT_TYPE::PT_NONE, std::vector<EDA_ITEM*>{} );
45 }
46
48 std::vector<VECTOR2I> AnchorPositions() const
49 {
50 std::vector<VECTOR2I> positions;
51
52 for( const ANCHOR& anchor : m_anchors )
53 positions.push_back( anchor.pos );
54
55 return positions;
56 }
57};
58} // namespace
59
60
61BOOST_AUTO_TEST_SUITE( GridHelperTest )
62
63BOOST_AUTO_TEST_CASE( DefaultConstructor )
64{
65 GRID_HELPER helper;
66
67 // Test default state
68 BOOST_CHECK( helper.GetSnap() );
69 BOOST_CHECK( helper.GetUseGrid() );
70
71 // Test that manual setters work
72 helper.SetGridSize( VECTOR2D( 100, 100 ) );
73 helper.SetOrigin( VECTOR2I( 50, 50 ) );
74 helper.SetGridSnapping( true );
75
76 VECTOR2I grid = helper.GetGrid();
77 BOOST_CHECK_EQUAL( grid.x, 100 );
78 BOOST_CHECK_EQUAL( grid.y, 100 );
79
80 VECTOR2I origin = helper.GetOrigin();
81 BOOST_CHECK_EQUAL( origin.x, 50 );
82 BOOST_CHECK_EQUAL( origin.y, 50 );
83}
84
85
86BOOST_AUTO_TEST_CASE( SnapResultGuideUpdateClearsStaleDimensionBrackets )
87{
88 TEST_GRID_HELPER helper;
89 SNAP_RESULT equalGap;
90 equalGap.guides = { { { 0, 0 }, { 5, 0 }, SNAP_GUIDE_STYLE::DIMENSION_BRACKET },
91 { { 10, 0 }, { 15, 0 }, SNAP_GUIDE_STYLE::DIMENSION_BRACKET } };
92
93 helper.applySnapResultGuides( equalGap );
94 BOOST_REQUIRE_EQUAL( helper.DimensionBrackets().size(), 2 );
95
96 SNAP_RESULT alignment;
97 alignment.guides = { { { 0, 0 }, { 0, 10 }, SNAP_GUIDE_STYLE::SNAP_LINE } };
98 helper.applySnapResultGuides( alignment );
99 BOOST_CHECK( helper.DimensionBrackets().empty() );
100}
101
102
103BOOST_AUTO_TEST_CASE( SnapFrameSelectsPayloadForAcceptedId )
104{
105 const SNAP_STABLE_ID rejectedId = MakePointSnapId( SNAP_ID_KIND::INTRINSIC_ANCHOR, { 10, 10 }, 1 );
106 const SNAP_STABLE_ID acceptedId = MakePointSnapId( SNAP_ID_KIND::INTRINSIC_ANCHOR, { 10, 10 }, 2 );
108 input.context.sourcePoint = { 9, 9 };
109 input.candidates.push_back( SNAP_CANDIDATE::Point( rejectedId, SNAP_PRIORITY_TIER::OBJECT,
110 SNAP_CANDIDATE_SUBTYPE::INTRINSIC_ANCHOR, { 10, 10 }, 0.1 ) );
111 input.candidates.push_back( SNAP_CANDIDATE::Point( acceptedId, SNAP_PRIORITY_TIER::OBJECT,
112 SNAP_CANDIDATE_SUBTYPE::INTRINSIC_ANCHOR, { 10, 10 }, 0.1 ) );
113 input.presentation.emplace( rejectedId, 1 );
114 input.presentation.emplace( acceptedId, 2 );
115 input.feasibility =
116 [rejectedId]( const SNAP_SOURCE_CONTEXT& aContext, const std::vector<SNAP_CANDIDATE>& aCandidates )
117 {
119 result.position = aContext.sourcePoint;
120
121 if( aCandidates.empty() )
122 return result;
123
124 if( aCandidates.back().id == rejectedId )
125 {
127 return result;
128 }
129
130 result.position = { 10, 10 };
131 result.remainingDof = 0;
132 return result;
133 };
134
135 SNAP_FRAME_OUTPUT<int> output = ResolveSnapFrame( std::move( input ) );
136
137 BOOST_REQUIRE_EQUAL( output.result.accepted.size(), 1 );
138 BOOST_CHECK( output.result.accepted.front() == acceptedId );
139 BOOST_REQUIRE( output.presentation );
140 BOOST_CHECK( output.presentation->id == acceptedId );
141 BOOST_CHECK_EQUAL( output.presentation->payload, 2 );
142}
143
144
146{
147 GRID_HELPER helper;
148 helper.SetGridSize( VECTOR2D( 100, 100 ) );
149 helper.SetOrigin( VECTOR2I( 0, 0 ) );
150 helper.SetGridSnapping( true );
151
152 // Test basic alignment - should round to nearest grid point
153 VECTOR2I aligned = helper.Align( VECTOR2I( 149, 251 ) );
154 BOOST_CHECK_EQUAL( aligned.x, 100 );
155 BOOST_CHECK_EQUAL( aligned.y, 300 );
156
157 // Test exact grid points
158 aligned = helper.Align( VECTOR2I( 200, 300 ) );
159 BOOST_CHECK_EQUAL( aligned.x, 200 );
160 BOOST_CHECK_EQUAL( aligned.y, 300 );
161
162 // Test negative coordinates
163 aligned = helper.Align( VECTOR2I( -149, -251 ) );
164 BOOST_CHECK_EQUAL( aligned.x, -100 );
165 BOOST_CHECK_EQUAL( aligned.y, -300 );
166}
167
168BOOST_AUTO_TEST_CASE( AlignGridWithCustomGrid )
169{
170 GRID_HELPER helper;
171 helper.SetGridSize( VECTOR2D( 50, 50 ) );
172 helper.SetOrigin( VECTOR2I( 0, 0 ) );
173 helper.SetGridSnapping( true );
174
175 VECTOR2I aligned = helper.AlignGrid( VECTOR2I( 26, 74 ) );
176 BOOST_CHECK_EQUAL( aligned.x, 50 );
177 BOOST_CHECK_EQUAL( aligned.y, 50 );
178
179 // Test AlignGrid with specific grid parameter
180 aligned = helper.AlignGrid( VECTOR2I( 26, 74 ), VECTOR2D( 25, 25 ), VECTOR2D( 0, 0 ) );
181 BOOST_CHECK_EQUAL( aligned.x, 25 );
182 BOOST_CHECK_EQUAL( aligned.y, 75 );
183}
184
185BOOST_AUTO_TEST_CASE( AlignWithOriginOffset )
186{
187 GRID_HELPER helper;
188 helper.SetGridSize( VECTOR2D( 100, 100 ) );
189 helper.SetOrigin( VECTOR2I( 25, 25 ) );
190 helper.SetGridSnapping( true );
191
192 // When grid has an origin offset, alignment should work from the new reference point
193 VECTOR2I aligned = helper.AlignGrid( VECTOR2I( 149, 251 ) );
194 BOOST_CHECK_EQUAL( aligned.x, 125 );
195 BOOST_CHECK_EQUAL( aligned.y, 225 );
196}
197
198BOOST_AUTO_TEST_CASE( AlignWithAuxiliaryAxes )
199{
200 GRID_HELPER helper;
201 helper.SetGridSize( VECTOR2D( 100, 100 ) );
202 helper.SetOrigin( VECTOR2I( 0, 0 ) );
203 helper.SetGridSnapping( true );
204
205 // Set auxiliary axis at (75, 75)
206 helper.SetAuxAxes( true, VECTOR2I( 75, 75 ) );
207
208 // Point closer to aux axis than grid should snap to aux axis
209 VECTOR2I aligned = helper.Align( VECTOR2I( 80, 80 ) );
210 BOOST_CHECK_EQUAL( aligned.x, 75 ); // Closer to aux axis X
211 BOOST_CHECK_EQUAL( aligned.y, 75 ); // Closer to aux axis Y
212
213 // Point closer to grid than aux axis should snap to grid
214 aligned = helper.Align( VECTOR2I( 95, 95 ) );
215 BOOST_CHECK_EQUAL( aligned.x, 100 ); // Closer to grid
216 BOOST_CHECK_EQUAL( aligned.y, 100 ); // Closer to grid
217
218 // Disable aux axes
219 helper.SetAuxAxes( false );
220 aligned = helper.Align( VECTOR2I( 80, 80 ) );
221 BOOST_CHECK_EQUAL( aligned.x, 100 ); // Should snap to grid only
222 BOOST_CHECK_EQUAL( aligned.y, 100 );
223}
224
225BOOST_AUTO_TEST_CASE( GridSnappingDisabled )
226{
227 GRID_HELPER helper;
228 helper.SetGridSize( VECTOR2D( 100, 100 ) );
229 helper.SetOrigin( VECTOR2I( 0, 0 ) );
230 helper.SetGridSnapping( false ); // Disable grid snapping
231
232 // When grid snapping is disabled, Align should return original point
233 VECTOR2I original( 149, 251 );
234 VECTOR2I aligned = helper.Align( original );
235 BOOST_CHECK_EQUAL( aligned.x, original.x );
236 BOOST_CHECK_EQUAL( aligned.y, original.y );
237
238 // AlignGrid should still work regardless of grid snapping setting
239 aligned = helper.AlignGrid( original );
240 BOOST_CHECK_EQUAL( aligned.x, 100 );
241 BOOST_CHECK_EQUAL( aligned.y, 300 );
242}
243
244BOOST_AUTO_TEST_CASE( UseGridDisabled )
245{
246 GRID_HELPER helper;
247 helper.SetGridSize( VECTOR2D( 100, 100 ) );
248 helper.SetOrigin( VECTOR2I( 0, 0 ) );
249 helper.SetGridSnapping( true );
250 helper.SetUseGrid( false ); // Disable grid usage
251
252 // When grid usage is disabled, Align should return original point
253 VECTOR2I original( 149, 251 );
254 VECTOR2I aligned = helper.Align( original );
255 BOOST_CHECK_EQUAL( aligned.x, original.x );
256 BOOST_CHECK_EQUAL( aligned.y, original.y );
257}
258
259BOOST_AUTO_TEST_CASE( AsymmetricGrid )
260{
261 GRID_HELPER helper;
262 helper.SetGridSize( VECTOR2D( 25, 75 ) ); // Different X and Y grid sizes
263 helper.SetOrigin( VECTOR2I( 0, 0 ) );
264 helper.SetGridSnapping( true );
265
266 VECTOR2I aligned = helper.Align( VECTOR2I( 30, 100 ) );
267 BOOST_CHECK_EQUAL( aligned.x, 25 ); // Nearest 25-unit boundary
268 BOOST_CHECK_EQUAL( aligned.y, 75 ); // Nearest 75-unit boundary
269
270 aligned = helper.Align( VECTOR2I( 40, 120 ) );
271 BOOST_CHECK_EQUAL( aligned.x, 50 ); // Next 25-unit boundary
272 BOOST_CHECK_EQUAL( aligned.y, 150 ); // Next 75-unit boundary
273}
274
276{
277 GRID_HELPER helper;
278
279 // Test snap flag getters/setters
280 BOOST_CHECK( helper.GetSnap() ); // Default should be true
281
282 helper.SetSnap( false );
283 BOOST_CHECK( !helper.GetSnap() );
284
285 helper.SetSnap( true );
286 BOOST_CHECK( helper.GetSnap() );
287
288 // Test grid usage flag
289 BOOST_CHECK( helper.GetUseGrid() ); // Default should be true
290
291 helper.SetUseGrid( false );
292 BOOST_CHECK( !helper.GetUseGrid() );
293
294 helper.SetUseGrid( true );
295 BOOST_CHECK( helper.GetUseGrid() );
296}
297
298BOOST_AUTO_TEST_CASE( MaskOperations )
299{
300 TEST_GRID_HELPER helper;
301
302 // An anchor is admitted only when the mask allows every one of its flags
303 helper.SetMask( GRID_HELPER::CORNER | GRID_HELPER::OUTLINE );
304
305 helper.addAnchor( VECTOR2I( 10, 10 ), GRID_HELPER::CORNER, nullptr );
306 BOOST_CHECK_EQUAL( helper.AnchorCount(), 1 );
307
308 helper.addAnchor( VECTOR2I( 20, 20 ), GRID_HELPER::SNAPPABLE, nullptr );
309 BOOST_CHECK_EQUAL( helper.AnchorCount(), 1 );
310
311 // A partially allowed anchor is rejected as a whole
312 helper.addAnchor( VECTOR2I( 30, 30 ), GRID_HELPER::CORNER | GRID_HELPER::SNAPPABLE, nullptr );
313 BOOST_CHECK_EQUAL( helper.AnchorCount(), 1 );
314
315 helper.SetMask( GRID_HELPER::ALL );
316 helper.addAnchor( VECTOR2I( 40, 40 ), GRID_HELPER::SNAPPABLE, nullptr );
317 BOOST_REQUIRE_EQUAL( helper.AnchorCount(), 2 );
318
319 helper.ClearMaskFlag( GRID_HELPER::SNAPPABLE );
320 helper.addAnchor( VECTOR2I( 50, 50 ), GRID_HELPER::SNAPPABLE, nullptr );
321 BOOST_CHECK_EQUAL( helper.AnchorCount(), 2 );
322
323 const std::vector<VECTOR2I> admitted = helper.AnchorPositions();
324 BOOST_CHECK( admitted == std::vector<VECTOR2I>( { VECTOR2I( 10, 10 ), VECTOR2I( 40, 40 ) } ) );
325}
326
328{
329 GRID_HELPER helper;
330 helper.SetGridSize( VECTOR2D( 100, 100 ) );
331 helper.SetOrigin( VECTOR2I( 0, 0 ) );
332 helper.SetGridSnapping( true );
333 helper.ClearSkipPoint();
334
335 // A horizontal construction line snaps to the grid column nearest the cursor, held at the
336 // line's own height
337 const VECTOR2D grid( 100, 100 );
338 const VECTOR2I cursor( 720, 510 );
339
340 helper.SetSnapLineOrigin( VECTOR2I( 500, 500 ) );
341 helper.SetSnapLineDirections( { VECTOR2I( 1, 0 ) } );
342
343 const VECTOR2I nearestGrid = helper.AlignGrid( cursor );
344
345 std::optional<VECTOR2I> snap = helper.SnapToConstructionLines( cursor, nearestGrid, grid, 50.0 );
346
347 BOOST_REQUIRE( snap.has_value() );
348 BOOST_CHECK_EQUAL( snap->x, 700 );
349 BOOST_CHECK_EQUAL( snap->y, 500 );
350
351 helper.SetSkipPoint( *snap );
352 BOOST_CHECK( !helper.SnapToConstructionLines( cursor, nearestGrid, grid, 50.0 ).has_value() );
353
354 // A skip point elsewhere leaves the candidate alone
355 helper.SetSkipPoint( VECTOR2I( 800, 500 ) );
356 BOOST_CHECK( helper.SnapToConstructionLines( cursor, nearestGrid, grid, 50.0 ) == snap );
357
358 helper.ClearSkipPoint();
359 BOOST_CHECK( helper.SnapToConstructionLines( cursor, nearestGrid, grid, 50.0 ) == snap );
360}
361
362BOOST_AUTO_TEST_CASE( SkipPointDefaultsToUnreachable )
363{
364 GRID_HELPER helper;
365 helper.SetGridSize( VECTOR2D( 100, 100 ) );
366 helper.SetOrigin( VECTOR2I( 0, 0 ) );
367 helper.SetGridSnapping( true );
368
369 // No ClearSkipPoint() here: a helper nobody has configured must still snap to the origin
370 helper.SetSnapLineOrigin( VECTOR2I( 0, 0 ) );
371 helper.SetSnapLineDirections( { VECTOR2I( 1, 0 ) } );
372
373 const VECTOR2I cursor( 10, 5 );
374 std::optional<VECTOR2I> snap =
375 helper.SnapToConstructionLines( cursor, helper.AlignGrid( cursor ), VECTOR2D( 100, 100 ), 50.0 );
376
377 BOOST_REQUIRE( snap.has_value() );
378 BOOST_CHECK_EQUAL( snap->x, 0 );
379 BOOST_CHECK_EQUAL( snap->y, 0 );
380}
381
382BOOST_AUTO_TEST_CASE( GridTypeAlignment )
383{
384 GRID_HELPER helper;
385 helper.SetGridSize( VECTOR2D( 100, 100 ) );
386 helper.SetOrigin( VECTOR2I( 0, 0 ) );
387 helper.SetGridSnapping( true );
388
389 // Test alignment with specific grid type
390 VECTOR2I aligned = helper.Align( VECTOR2I( 149, 251 ), GRID_CURRENT );
391 BOOST_CHECK_EQUAL( aligned.x, 100 );
392 BOOST_CHECK_EQUAL( aligned.y, 300 );
393
394 aligned = helper.AlignGrid( VECTOR2I( 149, 251 ), GRID_CURRENT );
395 BOOST_CHECK_EQUAL( aligned.x, 100 );
396 BOOST_CHECK_EQUAL( aligned.y, 300 );
397}
398
400{
401 GRID_HELPER helper;
402 helper.SetGridSize( VECTOR2D( 1, 1 ) ); // Very small grid
403 helper.SetOrigin( VECTOR2I( 0, 0 ) );
404 helper.SetGridSnapping( true );
405
406 // Test with very small grid
407 VECTOR2I aligned = helper.Align( VECTOR2I( 5, 5 ) );
408 BOOST_CHECK_EQUAL( aligned.x, 5 );
409 BOOST_CHECK_EQUAL( aligned.y, 5 );
410
411 // Test with zero point
412 aligned = helper.Align( VECTOR2I( 0, 0 ) );
413 BOOST_CHECK_EQUAL( aligned.x, 0 );
414 BOOST_CHECK_EQUAL( aligned.y, 0 );
415
416 // Test with large grid
417 helper.SetGridSize( VECTOR2D( 10000, 10000 ) );
418 aligned = helper.Align( VECTOR2I( 3000, 7000 ) );
419 BOOST_CHECK_EQUAL( aligned.x, 0 ); // Closer to 0 than 10000
420 BOOST_CHECK_EQUAL( aligned.y, 10000 ); // Closer to 10000 than 0
421}
422
424{
425 GRID_HELPER helper;
426
427 // Test GetGridSize with different grid types
428 helper.SetGridSize( VECTOR2D( 50, 75 ) );
429
430 VECTOR2D gridSize = helper.GetGridSize( GRID_CURRENT );
431 BOOST_CHECK_EQUAL( gridSize.x, 50 );
432 BOOST_CHECK_EQUAL( gridSize.y, 75 );
433
434 // Other grid types should return the same in the base implementation
435 gridSize = helper.GetGridSize( GRID_CONNECTABLE );
436 BOOST_CHECK_EQUAL( gridSize.x, 50 );
437 BOOST_CHECK_EQUAL( gridSize.y, 75 );
438}
439
441{
442 GRID_HELPER helper;
443 helper.SetVisibleGridSize( VECTOR2D( 25, 35 ) );
444
445 VECTOR2D visibleGrid = helper.GetVisibleGrid();
446 BOOST_CHECK_EQUAL( visibleGrid.x, 25 );
447 BOOST_CHECK_EQUAL( visibleGrid.y, 35 );
448}
449
450BOOST_AUTO_TEST_CASE( SnapPointManagement )
451{
452 TEST_GRID_HELPER helper;
453
454 // Initially should have no snapped point
455 auto snappedPoint = helper.GetSnappedPoint();
456 BOOST_CHECK( !snappedPoint.has_value() );
457
458 // After clearing anchors, still no snapped point
459 TEST_CLEAR_ANCHORS( helper );
460 snappedPoint = helper.GetSnappedPoint();
461 BOOST_CHECK( !snappedPoint.has_value() );
462
463 helper.RetainSnapForTest( { 100, 200 } );
464 BOOST_REQUIRE( helper.GetSnappedPoint().has_value() );
465
466 helper.FullReset();
467 BOOST_CHECK( !helper.GetSnappedPoint().has_value() );
468}
469
470
471BOOST_AUTO_TEST_CASE( AlignGridWithNonPageOrigin )
472{
473 // Issue #21800: Grid snapping should produce positions that are exact multiples
474 // of the grid size relative to the grid origin, regardless of display origin setting.
475 // When grid sizes go through VECTOR2D, floating-point imprecision in the
476 // VECTOR2D -> VECTOR2I truncation could produce incorrect grid positions.
477
478 GRID_HELPER helper;
479 helper.SetGridSnapping( true );
480
481 // PCB IU_PER_MM = 1000000 (nanometers)
482 constexpr int IU_PER_MM = 1000000;
483
484 struct TestCase
485 {
486 const char* name;
487 double gridSizeMM;
488 int gridOriginX;
489 int gridOriginY;
490 int pointX;
491 int pointY;
492 int expectedX;
493 int expectedY;
494 };
495
496 // Test various grid sizes, including ones that don't convert exactly from mm to nm
497 std::vector<TestCase> cases = {
498 // 1mm grid with non-zero origin
499 { "1mm grid, origin at 47.3mm",
500 1.0, 47300000, 25300000,
501 127400000, 95400000,
502 0, 0 },
503
504 // 0.5mm grid
505 { "0.5mm grid, origin at 47.3mm",
506 0.5, 47300000, 25300000,
507 127400000, 95400000,
508 0, 0 },
509
510 // 0.1mm grid (0.1 is NOT exact in IEEE 754 double)
511 { "0.1mm grid, origin at 47.3mm",
512 0.1, 47300000, 25300000,
513 127340000, 95340000,
514 0, 0 },
515
516 // 25 mil = 0.635mm (0.635 is NOT exact in IEEE 754 double)
517 { "25mil grid, origin at 47.625mm",
518 0.635, 47625000, 25400000,
519 127960000, 95250000,
520 0, 0 },
521
522 // 10 mil = 0.254mm (0.254 is NOT exact in IEEE 754 double)
523 { "10mil grid, origin at 47.752mm",
524 0.254, 47752000, 25400000,
525 127960000, 95250000,
526 0, 0 },
527
528 // 50 mil = 1.27mm
529 { "50mil grid, origin at 47.625mm",
530 1.27, 47625000, 25400000,
531 127960000, 95250000,
532 0, 0 },
533 };
534
535 // Compute all expected values using integer grid size (the ground truth)
536 for( auto& tc : cases )
537 {
538 int gridSizeIU = KiROUND( tc.gridSizeMM * IU_PER_MM );
539
540 tc.expectedX = KiROUND( double( tc.pointX - tc.gridOriginX ) / gridSizeIU )
541 * gridSizeIU + tc.gridOriginX;
542 tc.expectedY = KiROUND( double( tc.pointY - tc.gridOriginY ) / gridSizeIU )
543 * gridSizeIU + tc.gridOriginY;
544 }
545
546 for( const auto& tc : cases )
547 {
548 BOOST_TEST_CONTEXT( tc.name )
549 {
550 int gridSizeIU = KiROUND( tc.gridSizeMM * IU_PER_MM );
551
552 // Simulate the grid size coming through VECTOR2D (as it does from GAL)
553 VECTOR2D gridD( tc.gridSizeMM * IU_PER_MM, tc.gridSizeMM * IU_PER_MM );
554 VECTOR2D offsetD( tc.gridOriginX, tc.gridOriginY );
555
556 helper.SetGridSize( gridD );
557 helper.SetOrigin( VECTOR2I( tc.gridOriginX, tc.gridOriginY ) );
558
559 VECTOR2I point( tc.pointX, tc.pointY );
560
561 // Test AlignGrid with VECTOR2D parameters (the path that goes through
562 // implicit VECTOR2D -> VECTOR2I truncation in computeNearest)
563 VECTOR2I resultD = helper.AlignGrid( point, gridD, offsetD );
564
565 // Test AlignGrid with no parameters (uses GetGrid() which rounds properly)
566 VECTOR2I resultI = helper.AlignGrid( point );
567
568 BOOST_CHECK_EQUAL( resultD.x, tc.expectedX );
569 BOOST_CHECK_EQUAL( resultD.y, tc.expectedY );
570 BOOST_CHECK_EQUAL( resultI.x, tc.expectedX );
571 BOOST_CHECK_EQUAL( resultI.y, tc.expectedY );
572
573 // Verify that the result is on-grid
574 BOOST_CHECK_EQUAL( ( resultD.x - tc.gridOriginX ) % gridSizeIU, 0 );
575 BOOST_CHECK_EQUAL( ( resultD.y - tc.gridOriginY ) % gridSizeIU, 0 );
576 }
577 }
578}
579
580
581BOOST_AUTO_TEST_CASE( MovementFromOffGridAnchor )
582{
583 // Issue #23308 / #21800: When moving a footprint by its anchor, the reference point
584 // should be the actual anchor (not grid-snapped). The final position must still land
585 // exactly on-grid because AlignGrid now rounds VECTOR2D grid parameters correctly.
586
587 GRID_HELPER helper;
588 helper.SetGridSnapping( true );
589
590 constexpr int IU_PER_MM = 1000000;
591
592 struct TestCase
593 {
594 const char* name;
595 double gridSizeMM;
596 int anchorX;
597 int anchorY;
598 int mouseTargetX;
599 int mouseTargetY;
600 };
601
602 std::vector<TestCase> cases = {
603 // Anchor at 0.05mm on a 0.1mm grid
604 { "0.1mm grid, anchor at half-grid",
605 0.1, 50000, 50000, 300000, 300000 },
606
607 // Anchor at 0.127mm on a 0.254mm (10 mil) grid
608 { "10mil grid, anchor at half-grid",
609 0.254, 127000, 127000, 762000, 508000 },
610
611 // Anchor at 0.3175mm on a 0.635mm (25 mil) grid
612 { "25mil grid, anchor at half-grid",
613 0.635, 317500, 317500, 1905000, 1270000 },
614 };
615
616 for( const auto& tc : cases )
617 {
618 BOOST_TEST_CONTEXT( tc.name )
619 {
620 int gridSizeIU = KiROUND( tc.gridSizeMM * IU_PER_MM );
621
622 VECTOR2D gridD( tc.gridSizeMM * IU_PER_MM, tc.gridSizeMM * IU_PER_MM );
623 VECTOR2D offsetD( 0, 0 );
624
625 // Simulate the cursor snapping to grid at the target mouse position. This is
626 // what ResolveSnap does during the move loop.
627 VECTOR2I snappedTarget = helper.AlignGrid( VECTOR2I( tc.mouseTargetX, tc.mouseTargetY ), gridD, offsetD );
628
629 // The movement delta starts from the actual anchor (off-grid), not a grid-snapped ref
630 VECTOR2I anchor( tc.anchorX, tc.anchorY );
631 VECTOR2I movement = snappedTarget - anchor;
632 VECTOR2I finalPos = anchor + movement;
633
634 // Final position must be exactly on-grid
635 BOOST_CHECK_EQUAL( finalPos.x % gridSizeIU, 0 );
636 BOOST_CHECK_EQUAL( finalPos.y % gridSizeIU, 0 );
637
638 // And it must equal the snapped target exactly
639 BOOST_CHECK_EQUAL( finalPos.x, snappedTarget.x );
640 BOOST_CHECK_EQUAL( finalPos.y, snappedTarget.y );
641 }
642 }
643}
644
645
const char * name
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
KIGFX::CONSTRUCTION_GEOM & GetViewItem()
std::optional< VECTOR2I > SnapToConstructionLines(const VECTOR2I &aPoint, const VECTOR2I &aNearestGrid, const VECTOR2D &aGrid, double aSnapRange) const
void SetSnapLineDirections(const std::vector< VECTOR2I > &aDirections)
void addAnchor(const VECTOR2I &aPos, int aFlags, EDA_ITEM *aItem, int aPointTypes=POINT_TYPE::PT_NONE)
bool GetSnap() const
void SetSnap(bool aSnap)
void SetSkipPoint(const VECTOR2I &aPoint)
bool GetUseGrid() const
void SetOrigin(const VECTOR2I &aOrigin)
Definition grid_helper.h:97
void SetSnapLineOrigin(const VECTOR2I &aOrigin)
void SetVisibleGridSize(const VECTOR2D &aGrid)
Definition grid_helper.h:96
void ClearSkipPoint()
Clear the skip point by setting it to an unreachable position, thereby preventing matching.
void SetGridSnapping(bool aEnable)
Definition grid_helper.h:98
void SetUseGrid(bool aSnapToGrid)
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
VECTOR2D GetVisibleGrid() const
virtual VECTOR2D GetGridSize(GRID_HELPER_GRIDS aGrid) const
Return the size of the specified grid.
VECTOR2I GetGrid() const
VECTOR2I GetOrigin() const
void SetGridSize(const VECTOR2D &aGrid)
Definition grid_helper.h:95
void clearAnchors()
std::optional< ANCHOR > m_snapItem
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
void applySnapResultGuides(const SNAP_RESULT &aResult)
std::vector< ANCHOR > m_anchors
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
const std::vector< SEG > & DimensionBrackets() const
@ GRID_CURRENT
Definition grid_helper.h:57
@ GRID_CONNECTABLE
Definition grid_helper.h:59
@ PT_NONE
No specific point type.
Definition point_types.h:38
SNAP_FRAME_OUTPUT< Payload > ResolveSnapFrame(SNAP_FRAME_INPUT< Payload > aInput)
Definition snap_frame.h:60
SNAP_STABLE_ID MakePointSnapId(SNAP_ID_KIND aKind, const VECTOR2I &aPoint, int aFeatureIndex=0)
static SNAP_CANDIDATE Point(SNAP_STABLE_ID aId, SNAP_PRIORITY_TIER aPriority, SNAP_CANDIDATE_SUBTYPE aSubtype, const VECTOR2I &aPoint, double aResidual)
SNAP_SOURCE_CONTEXT context
Definition snap_frame.h:32
std::optional< SNAP_FRAME_PRESENTATION< Payload > > presentation
Definition snap_frame.h:55
SNAP_RESULT result
Definition snap_frame.h:54
std::vector< SNAP_GUIDE > guides
std::vector< SNAP_STABLE_ID > accepted
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
void TEST_CLEAR_ANCHORS(GRID_HELPER &helper)
BOOST_AUTO_TEST_CASE(DefaultConstructor)
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_CONTEXT("Test Clearance")
static constexpr double IU_PER_MM
Mock up a conversion function.
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707