KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_constraint_drag.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 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <algorithm>
21#include <array>
22#include <cmath>
23#include <vector>
24
26#include <tool/tool_manager.h>
28
29#include <board.h>
30#include <board_commit.h>
31#include <pcb_dimension.h>
32#include <pcb_shape.h>
33#include <geometry/shape_arc.h>
37
38
39BOOST_AUTO_TEST_SUITE( ConstraintSolverDrag )
40
41
42namespace
43{
44constexpr int MM = 1000000;
45
46
47SNAP_TARGET_ID targetId( const char* aName )
48{
49 return KIID::FromName( aName ).AsBytes();
50}
51
52
53SNAP_STABLE_ID testSnapId( const char* aName, SNAP_ID_KIND aKind = SNAP_ID_KIND::ITEM_GEOMETRY )
54{
55 return { aKind, targetId( aName ) };
56}
57
58
59struct DRAG_FIXTURE
60{
61 BOARD board;
62 TOOL_MANAGER mgr;
63 KI_TEST::DUMMY_TOOL* tool;
64
65 DRAG_FIXTURE() :
66 tool( new KI_TEST::DUMMY_TOOL() )
67 {
68 mgr.SetEnvironment( &board, nullptr, nullptr, nullptr, nullptr );
69 mgr.RegisterTool( tool );
70 }
71
72 PCB_SHAPE* addSegment( const VECTOR2I& aStart, const VECTOR2I& aEnd )
73 {
74 PCB_SHAPE* seg = new PCB_SHAPE( &board, SHAPE_T::SEGMENT );
75 seg->SetStart( aStart );
76 seg->SetEnd( aEnd );
77 board.Add( seg );
78 return seg;
79 }
80
81 PCB_SHAPE* addCircle( const VECTOR2I& aCenter, int aRadius )
82 {
83 PCB_SHAPE* circle = new PCB_SHAPE( &board, SHAPE_T::CIRCLE );
84 circle->SetCenter( aCenter );
85 circle->SetRadius( aRadius );
86 board.Add( circle );
87 return circle;
88 }
89
90 PCB_SHAPE* addArc( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd )
91 {
92 PCB_SHAPE* arc = new PCB_SHAPE( &board, SHAPE_T::ARC );
93 arc->SetArcGeometry( aStart, aMid, aEnd );
94 board.Add( arc );
95 return arc;
96 }
97
98 PCB_SHAPE* addRect( const VECTOR2I& aStart, const VECTOR2I& aEnd )
99 {
100 PCB_SHAPE* rect = new PCB_SHAPE( &board, SHAPE_T::RECTANGLE );
101 rect->SetStart( aStart );
102 rect->SetEnd( aEnd );
103 board.Add( rect );
104 return rect;
105 }
106
107 PCB_SHAPE* addPoly( const std::vector<VECTOR2I>& aPoints )
108 {
109 PCB_SHAPE* poly = new PCB_SHAPE( &board, SHAPE_T::POLY );
110 poly->SetPolyPoints( aPoints );
111 board.Add( poly );
112 return poly;
113 }
114
115 PCB_CONSTRAINT* addHorizontal( PCB_SHAPE* aShape )
116 {
117 PCB_CONSTRAINT* c = new PCB_CONSTRAINT( &board, PCB_CONSTRAINT_TYPE::HORIZONTAL );
119 board.Add( c );
120 return c;
121 }
122
123 PCB_CONSTRAINT* addFixedPosition( PCB_SHAPE* aShape, CONSTRAINT_ANCHOR aAnchor )
124 {
125 PCB_CONSTRAINT* c = new PCB_CONSTRAINT( &board, PCB_CONSTRAINT_TYPE::FIXED_POSITION );
126 c->AddMember( aShape->m_Uuid, aAnchor );
127 board.Add( c );
128 return c;
129 }
130
131 PCB_CONSTRAINT* addCoincident( PCB_SHAPE* aA, CONSTRAINT_ANCHOR aAnchorA, PCB_SHAPE* aB,
132 CONSTRAINT_ANCHOR aAnchorB )
133 {
134 PCB_CONSTRAINT* c = new PCB_CONSTRAINT( &board, PCB_CONSTRAINT_TYPE::COINCIDENT );
135 c->AddMember( aA->m_Uuid, aAnchorA );
136 c->AddMember( aB->m_Uuid, aAnchorB );
137 board.Add( c );
138 return c;
139 }
140};
141
142
143// Mirrors PCB_POINT_EDITOR updateItem staging dragged shape moving it then rederiving constrained cluster
144// staging each neighbor before it changes
145void simulateDrag( BOARD_COMMIT& aCommit, BOARD* aBoard, PCB_SHAPE* aShape,
146 CONSTRAINT_ANCHOR aAnchor, const VECTOR2I& aCursor,
147 std::vector<PCB_SHAPE*>* aModified )
148{
149 aCommit.Modify( aShape );
150
151 if( aAnchor == CONSTRAINT_ANCHOR::START )
152 aShape->SetStart( aCursor );
153 else if( aAnchor == CONSTRAINT_ANCHOR::END )
154 aShape->SetEnd( aCursor );
155 else if( aAnchor == CONSTRAINT_ANCHOR::CENTER )
156 aShape->SetCenter( aCursor );
157
158 SolveCluster( aBoard, { aShape->m_Uuid, aAnchor }, aCursor, aModified,
159 [&]( BOARD_ITEM* aNeighbor )
160 {
161 aCommit.Modify( aNeighbor );
162 } );
163}
164} // namespace
165
166
167// Dragging one end of a corner re-derives the coincident neighbor; reverting the same commit
168// restores both shapes (one undoable transaction).
169BOOST_FIXTURE_TEST_CASE( DragReDerivesNeighborRevertRestores, DRAG_FIXTURE )
170{
171 PCB_SHAPE* a = addSegment( { 0, 0 }, { 10 * MM, 0 } );
172 PCB_SHAPE* b = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
173
175 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::END );
176 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::START );
177 board.Add( c );
178
179 const VECTOR2I aEnd0 = a->GetEnd();
180 const VECTOR2I bStart0 = b->GetStart();
181
182 std::vector<PCB_SHAPE*> modified;
183
184 BOARD_COMMIT commit( tool );
185 simulateDrag( commit, &board, a, CONSTRAINT_ANCHOR::END, { 12 * MM, 3 * MM }, &modified );
186
187 // The neighbor followed the dragged corner and is reported as modified.
188 BOOST_CHECK_LE( ( a->GetEnd() - b->GetStart() ).EuclideanNorm(), 100 );
189 BOOST_CHECK( std::find( modified.begin(), modified.end(), b ) != modified.end() );
190 BOOST_CHECK( a->GetEnd() != aEnd0 );
191
192 // Revert restores every shape the transaction touched.
193 commit.Revert();
194 BOOST_CHECK_EQUAL( a->GetEnd(), aEnd0 );
195 BOOST_CHECK_EQUAL( b->GetStart(), bStart0 );
196}
197
198
199BOOST_FIXTURE_TEST_CASE( PersistentDragSessionWarmStartsSuccessivePreviewFrames, DRAG_FIXTURE )
200{
201 PCB_SHAPE* a = addSegment( { 0, 0 }, { 10 * MM, 0 } );
202 PCB_SHAPE* b = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
203 addCoincident( a, CONSTRAINT_ANCHOR::END, b, CONSTRAINT_ANCHOR::START );
204
207 std::vector<PCB_SHAPE*> modified;
208 BOARD_COMMIT commit( tool );
209 const VECTOR2I originalAEnd = a->GetEnd();
210 const VECTOR2I originalBStart = b->GetStart();
211
212 BOOST_REQUIRE( session.Build( &board, dragged ) );
213 BOOST_CHECK( session.Matches( dragged ) );
214 commit.Modify( a );
215
216 CONSTRAINT_DIAGNOSIS first = session.Solve(
217 { 12 * MM, 2 * MM }, &modified,
218 [&]( BOARD_ITEM* aItem )
219 {
220 commit.Modify( aItem );
221 },
222 false, false );
223 BOOST_REQUIRE( first.solved );
224 BOOST_CHECK_LE( ( a->GetEnd() - b->GetStart() ).EuclideanNorm(), 100 );
225
226 CONSTRAINT_DIAGNOSIS second = session.Solve(
227 { 14 * MM, 4 * MM }, &modified,
228 [&]( BOARD_ITEM* aItem )
229 {
230 commit.Modify( aItem );
231 },
232 false, false );
233 BOOST_REQUIRE( second.solved );
234 BOOST_CHECK_LE( ( a->GetEnd() - b->GetStart() ).EuclideanNorm(), 100 );
235 BOOST_CHECK_LE( ( a->GetEnd() - VECTOR2I( 14 * MM, 4 * MM ) ).EuclideanNorm(), 100 );
236
237 commit.Revert();
238 BOOST_CHECK_EQUAL( a->GetEnd(), originalAEnd );
239 BOOST_CHECK_EQUAL( b->GetStart(), originalBStart );
240}
241
242
243BOOST_FIXTURE_TEST_CASE( PersistentDragSessionRejectsInexactSnapTarget, DRAG_FIXTURE )
244{
245 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 10 * MM, 0 } );
246
248 length->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
249 length->SetValue( 10.0 * MM );
250 board.Add( length );
251
254 BOOST_CHECK( session.IsExactFeasible( { 10 * MM, 0 } ) );
255 BOOST_CHECK( session.IsExactFeasible( { 10 * MM, 0 } ) );
256 BOOST_CHECK( !session.IsExactFeasible( { 20 * MM, 0 } ) );
257 BOOST_CHECK_EQUAL( seg->GetEnd(), VECTOR2I( 10 * MM, 0 ) );
258}
259
260
261BOOST_FIXTURE_TEST_CASE( PersistentDragSessionExactifiesEqualGapTarget, DRAG_FIXTURE )
262{
263 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 10 * MM, 0 } );
264
266 length->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
267 length->SetValue( 10.0 * MM );
268 board.Add( length );
269
271 horizontal->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
272 board.Add( horizontal );
273
276
277 SNAP_SOURCE_CONTEXT context;
278 context.sourcePoint = { 10 * MM, 0 };
279 SNAP_CANDIDATE gap =
283
284 SNAP_RESULT result = session.ResolveCandidates( context, { gap } );
286 BOOST_REQUIRE_EQUAL( result.quantizedResiduals.size(), 1 );
287 BOOST_CHECK_EQUAL( result.quantizedResiduals.front(), 0.0 );
288 BOOST_CHECK_EQUAL( seg->GetEnd(), VECTOR2I( 10 * MM, 0 ) );
289}
290
291
292BOOST_FIXTURE_TEST_CASE( PersistentMoveSessionArbitratesBeforeApplying, DRAG_FIXTURE )
293{
294 PCB_SHAPE* moved = addSegment( { 0, 0 }, { 10 * MM, 0 } );
295 PCB_SHAPE* neighbor = addSegment( { 10 * MM, 0 }, { 20 * MM, 0 } );
296 addCoincident( moved, CONSTRAINT_ANCHOR::END, neighbor, CONSTRAINT_ANCHOR::START );
297
299 BOOST_REQUIRE( session.Build( &board, { moved }, { 0, 0 } ) );
300
301 SNAP_SOURCE_CONTEXT context;
302 context.sourcePoint = { 5 * MM, 0 };
303 SNAP_CANDIDATE target = SNAP_CANDIDATE::Point( testSnapId( "target" ), SNAP_PRIORITY_TIER::OBJECT,
305
306 // A feasibility callback reports the residual and the freedom consumed; SNAP_RESOLVER owns
307 // the accepted list, so honouring a two-degree candidate shows up as zero freedom left.
308 SNAP_RESULT result = session.ResolveCandidates( context, { target } );
310 BOOST_CHECK_EQUAL( result.position, context.sourcePoint );
311 BOOST_REQUIRE_EQUAL( result.quantizedResiduals.size(), 1 );
312 BOOST_CHECK_EQUAL( result.quantizedResiduals.front(), 0.0 );
313 BOOST_CHECK_EQUAL( result.remainingDof, 0 );
314 BOOST_CHECK_EQUAL( moved->GetStart(), VECTOR2I( 0, 0 ) );
315 BOOST_CHECK_EQUAL( neighbor->GetStart(), VECTOR2I( 10 * MM, 0 ) );
316
317 std::vector<PCB_SHAPE*> modified;
318 BOOST_REQUIRE( session.Solve( result.position, &modified, {} ) );
319 BOOST_CHECK_EQUAL( moved->GetStart(), VECTOR2I( 5 * MM, 0 ) );
320 BOOST_CHECK_EQUAL( moved->GetEnd(), VECTOR2I( 15 * MM, 0 ) );
321 BOOST_CHECK_EQUAL( neighbor->GetStart(), VECTOR2I( 15 * MM, 0 ) );
322}
323
324
325BOOST_FIXTURE_TEST_CASE( RigidTranslationPreservesRoundShapeRadii, DRAG_FIXTURE )
326{
327 PCB_SHAPE* circle = addCircle( { 0, 0 }, 4 * MM );
328 PCB_SHAPE* arc = addArc( { 20 * MM, 0 }, { 17 * MM, 3 * MM }, { 14 * MM, 0 } );
329 PCB_SHAPE* ellipse = new PCB_SHAPE( &board, SHAPE_T::ELLIPSE );
330 ellipse->SetEllipseCenter( { 30 * MM, 0 } );
331 ellipse->SetEllipseMajorRadius( 6 * MM );
332 ellipse->SetEllipseMinorRadius( 3 * MM );
333 board.Add( ellipse );
334
335 const int circleRadius = circle->GetRadius();
336 const int arcRadius = arc->GetRadius();
337 const int ellipseMinorRadius = ellipse->GetEllipseMinorRadius();
338
340 BOOST_REQUIRE( adapter.Build( { circle, arc, ellipse }, {} ) );
342 adapter.SolveRigidTranslation( { circle->m_Uuid, arc->m_Uuid, ellipse->m_Uuid }, { 3 * MM, 2 * MM } ) );
343 adapter.Apply();
344
345 BOOST_CHECK_EQUAL( circle->GetRadius(), circleRadius );
346 BOOST_CHECK_EQUAL( arc->GetRadius(), arcRadius );
347 BOOST_CHECK_EQUAL( ellipse->GetEllipseMinorRadius(), ellipseMinorRadius );
348}
349
350
351BOOST_AUTO_TEST_CASE( UnbuiltMoveSessionRejectsCandidates )
352{
354 SNAP_SOURCE_CONTEXT context;
355 context.sourcePoint = { 10, 20 };
356 SNAP_CANDIDATE candidate = SNAP_CANDIDATE::Point( testSnapId( "point" ), SNAP_PRIORITY_TIER::OBJECT,
358
359 SNAP_RESULT result = session.ResolveCandidates( context, { candidate } );
360
362}
363
364
365BOOST_FIXTURE_TEST_CASE( PersistentMoveSessionRejectsIncompatibleTarget, DRAG_FIXTURE )
366{
367 PCB_SHAPE* moved = addSegment( { 0, 0 }, { 10 * MM, 0 } );
368 PCB_SHAPE* locked = addSegment( { 10 * MM, 0 }, { 20 * MM, 0 } );
369 locked->SetLocked( true );
371
373 BOOST_REQUIRE( session.Build( &board, { moved }, { 0, 0 } ) );
374
375 SNAP_SOURCE_CONTEXT context;
376 context.sourcePoint = { 5 * MM, 0 };
377 SNAP_CANDIDATE target = SNAP_CANDIDATE::Point( testSnapId( "blocked" ), SNAP_PRIORITY_TIER::OBJECT,
379
380 BOOST_CHECK( session.ResolveCandidates( context, { target } ).status == SNAP_RESULT_STATUS::INCOMPATIBLE );
381 BOOST_CHECK_EQUAL( moved->GetStart(), VECTOR2I( 0, 0 ) );
382 BOOST_CHECK_EQUAL( locked->GetStart(), VECTOR2I( 10 * MM, 0 ) );
383}
384
385
386BOOST_FIXTURE_TEST_CASE( PersistentMoveSessionFallsBackAfterRejectedObject, DRAG_FIXTURE )
387{
388 PCB_SHAPE* moved = addSegment( { 0, 0 }, { 10 * MM, 0 } );
389 PCB_SHAPE* locked = addSegment( { 10 * MM, 0 }, { 20 * MM, 0 } );
390 locked->SetLocked( true );
392
393 auto session = std::make_shared<BOARD_CONSTRAINT_MOVE_SESSION>();
394 BOOST_REQUIRE( session->Build( &board, { moved }, { 0, 0 } ) );
395
396 SNAP_SOURCE_CONTEXT context;
397 context.sourcePoint = { 5 * MM, 0 };
398 SNAP_STABLE_ID objectId = testSnapId( "blocked" );
399 SNAP_STABLE_ID gridId = testSnapId( "grid-x", SNAP_ID_KIND::GRID_X );
400 SNAP_RESOLVER resolver;
401
402 // The rejected object solve can outlast the frame budget in a debug build, which would skip
403 // the grid fallback this test is about
404 resolver.SetClock(
405 []
406 {
407 return SNAP_RESOLVER::CLOCK::time_point();
408 } );
409 resolver.SetFeasibilityCallback(
410 [session]( const SNAP_SOURCE_CONTEXT& aContext, const std::vector<SNAP_CANDIDATE>& aCandidates )
411 {
412 return session->ResolveCandidates( aContext, aCandidates );
413 } );
416 0.0 ) );
417 resolver.AddCandidate(
419
420 SNAP_RESULT result = resolver.Resolve( context );
422 BOOST_CHECK( !result.Accepted( objectId ) );
423 BOOST_CHECK( result.Accepted( gridId ) );
424 BOOST_CHECK_EQUAL( result.position, VECTOR2I( 0, 0 ) );
425}
426
427
428BOOST_FIXTURE_TEST_CASE( PersistentMoveSessionReportsEachAcceptedSnapOnce, DRAG_FIXTURE )
429{
430 PCB_SHAPE* moved = addSegment( { 0, 0 }, { 10 * MM, 0 } );
431 PCB_SHAPE* neighbor = addSegment( { 10 * MM, 0 }, { 20 * MM, 0 } );
432 addCoincident( moved, CONSTRAINT_ANCHOR::END, neighbor, CONSTRAINT_ANCHOR::START );
433
434 auto session = std::make_shared<BOARD_CONSTRAINT_MOVE_SESSION>();
435 BOOST_REQUIRE( session->Build( &board, { moved }, { 0, 0 } ) );
436
437 SNAP_SOURCE_CONTEXT context;
438 context.sourcePoint = { 5 * MM, 0 };
439 SNAP_STABLE_ID targetId = testSnapId( "target" );
442 target.guides.push_back( { { 0, 0 }, context.sourcePoint } );
443
444 SNAP_RESOLVER resolver;
445 resolver.SetFeasibilityCallback(
446 [session]( const SNAP_SOURCE_CONTEXT& aContext, const std::vector<SNAP_CANDIDATE>& aCandidates )
447 {
448 return session->ResolveCandidates( aContext, aCandidates );
449 } );
450 resolver.AddCandidate( target );
451
452 // The resolver appends the id and the guides itself. A session that reported them too would
453 // enter the snap in the sticky set twice and draw its guide twice every frame.
454 SNAP_RESULT result = resolver.Resolve( context );
456 BOOST_CHECK_EQUAL( std::count( result.accepted.begin(), result.accepted.end(), targetId ), 1 );
457 BOOST_CHECK_EQUAL( result.guides.size(), 1 );
458}
459
460
461BOOST_FIXTURE_TEST_CASE( PersistentMoveSessionJointlySolvesAuthoredAndObjectManifolds, DRAG_FIXTURE )
462{
463 PCB_SHAPE* moved = addSegment( { 0, 0 }, { 10 * MM, 0 } );
464 PCB_SHAPE* guide = addSegment( { -20 * MM, 0 }, { 20 * MM, 0 } );
465 guide->SetLocked( true );
466
468 onLine->AddMember( moved->m_Uuid, CONSTRAINT_ANCHOR::START );
469 onLine->AddMember( guide->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
470 board.Add( onLine );
471
473 BOOST_REQUIRE( session.Build( &board, { moved }, { 0, 0 } ) );
474
475 SNAP_SOURCE_CONTEXT context;
476 context.sourcePoint = { 5 * MM, 3 * MM };
477 SNAP_CANDIDATE vertical = SNAP_CANDIDATE::Line( testSnapId( "vertical" ), SNAP_PRIORITY_TIER::OBJECT,
479 { 0.0, 40.0 * MM }, 0.0 );
480
481 SNAP_RESULT base = session.ResolveCandidates( context, {} );
483 BOOST_CHECK_EQUAL( base.position, VECTOR2I( 5 * MM, 0 ) );
484
485 SNAP_RESULT result = session.ResolveCandidates( context, { vertical } );
487 BOOST_REQUIRE_EQUAL( result.quantizedResiduals.size(), 1 );
488 BOOST_CHECK_EQUAL( result.quantizedResiduals.front(), 0.0 );
489 BOOST_CHECK_EQUAL( result.remainingDof, 1 );
490 BOOST_CHECK_EQUAL( result.position, VECTOR2I( 5 * MM, 0 ) );
491}
492
493
494BOOST_FIXTURE_TEST_CASE( PersistentMoveSessionJointlySolvesDisconnectedClusters, DRAG_FIXTURE )
495{
496 PCB_SHAPE* first = addSegment( { 0, 0 }, { 10 * MM, 0 } );
497 PCB_SHAPE* firstGuide = addSegment( { -100 * MM, 0 }, { 100 * MM, 0 } );
498 PCB_SHAPE* second = addSegment( { 0, 10 * MM }, { 10 * MM, 10 * MM } );
499 PCB_SHAPE* secondGuide = addSegment( { -100 * MM, 9 * MM }, { 100 * MM, 11 * MM } );
500 firstGuide->SetLocked( true );
501 secondGuide->SetLocked( true );
502
504 firstOnLine->AddMember( first->m_Uuid, CONSTRAINT_ANCHOR::START );
505 firstOnLine->AddMember( firstGuide->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
506 board.Add( firstOnLine );
507
509 secondOnLine->AddMember( second->m_Uuid, CONSTRAINT_ANCHOR::START );
510 secondOnLine->AddMember( secondGuide->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
511 board.Add( secondOnLine );
512
514 BOOST_REQUIRE( session.Build( &board, { first, second }, { 0, 0 } ) );
515
516 SNAP_SOURCE_CONTEXT context;
517 context.sourcePoint = { 20 * MM, 5 * MM };
518 SNAP_RESULT result = session.ResolveCandidates( context, {} );
519
521 BOOST_CHECK_EQUAL( result.position, VECTOR2I( 0, 0 ) );
522}
523
524
525BOOST_FIXTURE_TEST_CASE( PersistentDragSessionFindsJointlyFeasibleManifoldPoint, DRAG_FIXTURE )
526{
527 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 10 * MM, 0 } );
528
530 horizontal->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
531 board.Add( horizontal );
532
535
536 SNAP_SOURCE_CONTEXT context;
537 context.sourcePoint = { 6 * MM, 7 * MM };
538 SNAP_CANDIDATE manifold = SNAP_CANDIDATE::Line( testSnapId( "vertical" ), SNAP_PRIORITY_TIER::OBJECT,
540 { 0.0, 40.0 * MM }, 0.0 );
541
542 SNAP_RESULT result = session.ResolveCandidates( context, { manifold } );
543
545 BOOST_CHECK_EQUAL( result.position.x, 6 * MM );
546 BOOST_CHECK_LE( std::abs( result.position.y ), 1000 );
547 BOOST_CHECK_EQUAL( seg->GetEnd(), VECTOR2I( 10 * MM, 0 ) );
548
549 manifold.origin = { -20 * MM, 20 * MM };
550 manifold.direction = { 40.0 * MM, 0.0 };
551 BOOST_CHECK( session.ResolveCandidates( context, { manifold } ).status == SNAP_RESULT_STATUS::INCOMPATIBLE );
552}
553
554
555BOOST_FIXTURE_TEST_CASE( PersistentDragSessionPreservesGeometryOnBaseConflict, DRAG_FIXTURE )
556{
557 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 12 * MM, 0 } );
558
560 length->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
561 length->SetValue( 10.0 * MM );
562 board.Add( length );
563
566
567 seg->SetEnd( { 20 * MM, 0 } );
568 CONSTRAINT_DIAGNOSIS diagnosis = session.Solve( { 20 * MM, 0 }, nullptr, {}, false, false );
569
570 BOOST_CHECK( !diagnosis.solved );
571 BOOST_CHECK_EQUAL( seg->GetEnd(), VECTOR2I( 12 * MM, 0 ) );
572 BOOST_CHECK( !session.IsExactFeasible( { 12 * MM, 0 } ) );
573}
574
575
576// A whole cluster inside the moved selection travels rigidly, so a constraint set it already
577// could not satisfy must not block the move.
578BOOST_FIXTURE_TEST_CASE( WholeClusterMoveSurvivesPreExistingConflict, DRAG_FIXTURE )
579{
580 // Two sides of a closed outline, corner tied, both marked horizontal. Adjacent sides cannot
581 // both be horizontal, which is the state this board arrived in
582 PCB_SHAPE* bottom = addSegment( { 0, 0 }, { 10 * MM, 0 } );
583 PCB_SHAPE* right = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
584
586 corner->AddMember( bottom->m_Uuid, CONSTRAINT_ANCHOR::END );
587 corner->AddMember( right->m_Uuid, CONSTRAINT_ANCHOR::START );
588 board.Add( corner );
589
590 for( PCB_SHAPE* seg : { bottom, right } )
591 {
593 horizontal->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
594 board.Add( horizontal );
595 }
596
598 BOOST_REQUIRE( session.Build( &board, { bottom, right }, { 0, 0 } ) );
599
600 std::vector<PCB_SHAPE*> modified;
601 BOOST_REQUIRE( session.Solve( { 5 * MM, 5 * MM }, &modified, {} ) );
602
603 BOOST_CHECK_EQUAL( bottom->GetStart(), VECTOR2I( 5 * MM, 5 * MM ) );
604 BOOST_CHECK_EQUAL( right->GetEnd(), VECTOR2I( 15 * MM, 15 * MM ) );
605}
606
607
608// A pinned point cannot travel, so a whole-cluster move that would carry it off is still refused
609// and the geometry is left where it was.
610BOOST_FIXTURE_TEST_CASE( WholeClusterMoveWithFixedPoint, DRAG_FIXTURE )
611{
612 PCB_SHAPE* bottom = addSegment( { 0, 0 }, { 10 * MM, 0 } );
613 PCB_SHAPE* right = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
614
616 corner->AddMember( bottom->m_Uuid, CONSTRAINT_ANCHOR::END );
617 corner->AddMember( right->m_Uuid, CONSTRAINT_ANCHOR::START );
618 board.Add( corner );
619
621 pinned->AddMember( bottom->m_Uuid, CONSTRAINT_ANCHOR::START );
622 board.Add( pinned );
623
625 BOOST_REQUIRE( session.Build( &board, { bottom, right }, { 0, 0 } ) );
626
627 std::vector<PCB_SHAPE*> modified;
628
629 BOOST_CHECK( !session.Solve( { 5 * MM, 5 * MM }, &modified, {} ) );
630 BOOST_CHECK_EQUAL( bottom->GetStart(), VECTOR2I( 0, 0 ) );
631 BOOST_CHECK_EQUAL( right->GetEnd(), VECTOR2I( 10 * MM, 10 * MM ) );
632}
633
634
635// A pin grounds its own point and nothing else, so a free corner in the same cluster still drags.
636BOOST_FIXTURE_TEST_CASE( DragFreeCornerBesideFixedPoint, DRAG_FIXTURE )
637{
638 PCB_SHAPE* bottom = addSegment( { 0, 0 }, { 10 * MM, 0 } );
639 PCB_SHAPE* right = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
640
641 addCoincident( bottom, CONSTRAINT_ANCHOR::END, right, CONSTRAINT_ANCHOR::START );
642
644 pinned->AddMember( bottom->m_Uuid, CONSTRAINT_ANCHOR::START );
645 board.Add( pinned );
646
648 BOOST_REQUIRE( session.Build( &board, CONSTRAINT_MEMBER( right->m_Uuid, CONSTRAINT_ANCHOR::START ) ) );
649 BOOST_REQUIRE( !session.HasBaseConflict() );
650
651 right->SetStart( { 12 * MM, 3 * MM } );
652 CONSTRAINT_DIAGNOSIS diagnosis = session.Solve( { 12 * MM, 3 * MM }, nullptr, {}, false, false );
653
654 BOOST_CHECK( diagnosis.solved );
655 BOOST_CHECK( diagnosis.conflicting.empty() );
656 BOOST_CHECK_LE( ( right->GetStart() - VECTOR2I( 12 * MM, 3 * MM ) ).EuclideanNorm(), 1000 );
657 BOOST_CHECK_LE( ( bottom->GetEnd() - right->GetStart() ).EuclideanNorm(), 1000 );
658 BOOST_CHECK_EQUAL( bottom->GetStart(), VECTOR2I( 0, 0 ) );
659}
660
661
662// Standing still is always feasible, so a pinned cluster keeps its snapping instead of reporting
663// itself unusable on every frame.
664BOOST_FIXTURE_TEST_CASE( PinnedClusterStaysSnappableAtRest, DRAG_FIXTURE )
665{
666 PCB_SHAPE* bottom = addSegment( { 0, 0 }, { 10 * MM, 0 } );
667 PCB_SHAPE* right = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
668
669 addCoincident( bottom, CONSTRAINT_ANCHOR::END, right, CONSTRAINT_ANCHOR::START );
670
672 pinned->AddMember( bottom->m_Uuid, CONSTRAINT_ANCHOR::START );
673 board.Add( pinned );
674
676 BOOST_REQUIRE( session.Build( &board, { bottom, right }, { 0, 0 } ) );
677
678 SNAP_SOURCE_CONTEXT context;
679 context.sourcePoint = { 0, 0 };
680 SNAP_RESULT result = session.ResolveCandidates( context, {} );
681
683 BOOST_CHECK_EQUAL( result.position, VECTOR2I( 0, 0 ) );
684}
685
686
687// Moving part of a cluster still works when the pin sits on a shape left behind.
688BOOST_FIXTURE_TEST_CASE( PartialClusterMoveBesideFixedPoint, DRAG_FIXTURE )
689{
690 PCB_SHAPE* bottom = addSegment( { 0, 0 }, { 10 * MM, 0 } );
691 PCB_SHAPE* right = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
692
693 addCoincident( bottom, CONSTRAINT_ANCHOR::END, right, CONSTRAINT_ANCHOR::START );
694
696 pinned->AddMember( bottom->m_Uuid, CONSTRAINT_ANCHOR::START );
697 board.Add( pinned );
698
700 BOOST_REQUIRE( session.Build( &board, { right }, { 10 * MM, 0 } ) );
701
702 std::vector<PCB_SHAPE*> moved;
703 BOOST_REQUIRE( session.Solve( { 13 * MM, 4 * MM }, &moved, {} ) );
704
705 BOOST_CHECK_EQUAL( right->GetStart(), VECTOR2I( 13 * MM, 4 * MM ) );
706 BOOST_CHECK_EQUAL( bottom->GetStart(), VECTOR2I( 0, 0 ) );
707 BOOST_CHECK_LE( ( bottom->GetEnd() - right->GetStart() ).EuclideanNorm(), 1000 );
708}
709
710
711// Dragging one circle's centre re-derives a concentric neighbor (centre-anchor drag on a
712// non-segment shape).
713BOOST_FIXTURE_TEST_CASE( DragCircleCentreMovesConcentricNeighbor, DRAG_FIXTURE )
714{
715 PCB_SHAPE* a = addCircle( { 0, 0 }, 5 * MM );
716 PCB_SHAPE* b = addCircle( { 0, 0 }, 8 * MM );
717
719 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
720 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
721 board.Add( c );
722
723 std::vector<PCB_SHAPE*> modified;
724
725 BOARD_COMMIT commit( tool );
726 simulateDrag( commit, &board, a, CONSTRAINT_ANCHOR::CENTER, { 4 * MM, 3 * MM }, &modified );
727
728 // The concentric neighbor's centre followed the dragged centre.
729 BOOST_CHECK_LE( ( a->GetCenter() - b->GetCenter() ).EuclideanNorm(), 2000 );
730 BOOST_CHECK( std::find( modified.begin(), modified.end(), b ) != modified.end() );
731
732 commit.Revert();
733}
734
735
736// When the cluster cannot be solved (here, an unmapped constraint family), neighbors are left
737// untouched -- nothing is half-moved or staged.
738BOOST_FIXTURE_TEST_CASE( UnsolvableClusterLeavesNeighborUntouched, DRAG_FIXTURE )
739{
740 PCB_SHAPE* a = addSegment( { 0, 0 }, { 10 * MM, 0 } );
741 PCB_SHAPE* b = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
742
743 // Concentric needs circles; given two segments the adapter cannot map it, so Build() leaves it
744 // unenforced and the solve is skipped, leaving the neighbor where it was.
746 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
747 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
748 board.Add( c );
749
750 const VECTOR2I bStart0 = b->GetStart();
751 const VECTOR2I bEnd0 = b->GetEnd();
752
753 std::vector<PCB_SHAPE*> modified;
754
755 BOARD_COMMIT commit( tool );
756 simulateDrag( commit, &board, a, CONSTRAINT_ANCHOR::END, { 12 * MM, 3 * MM }, &modified );
757
758 BOOST_CHECK( modified.empty() );
759 BOOST_CHECK_EQUAL( b->GetStart(), bStart0 );
760 BOOST_CHECK_EQUAL( b->GetEnd(), bEnd0 );
761
762 commit.Revert();
763}
764
765
766// Deleting a referenced shape leaves the constraint in place (in an error state), it is not
767// cascade-deleted (Zulip "Geometry Constraint Solver", 2026-06-18: deleting an object should put
768// the constraint in an error state, not delete it).
769BOOST_FIXTURE_TEST_CASE( DeleteShapeLeavesConstraintInErrorState, DRAG_FIXTURE )
770{
771 PCB_SHAPE* a = addSegment( { 0, 0 }, { 10 * MM, 0 } );
772 PCB_SHAPE* b = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
773
774 KIID aId = a->m_Uuid;
775
778 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::START );
779 board.Add( c );
780
781 BOOST_REQUIRE_EQUAL( board.Constraints().size(), 1 );
782
783 BOARD_COMMIT commit( tool );
784 commit.Remove( a );
785 commit.Push( wxT( "delete segment" ) );
786
787 // The constraint survives and still holds the now-dangling reference to the deleted shape.
788 BOOST_REQUIRE_EQUAL( board.Constraints().size(), 1 );
789 PCB_CONSTRAINT* survivor = board.Constraints().front();
790 BOOST_REQUIRE_EQUAL( survivor->GetMembers().size(), 2 );
791
792 bool referencesDeleted = false;
793
794 for( const CONSTRAINT_MEMBER& m : survivor->GetMembers() )
795 {
796 if( m.m_item == aId )
797 referencesDeleted = true;
798 }
799
800 BOOST_CHECK( referencesDeleted );
801 BOOST_CHECK( board.ResolveItem( aId, true ) == nullptr ); // the shape is gone
802}
803
804
805// A whole-shape move breaks a point-on-line relation. ReSolveShapeClusters restores it with the
806// moved shape pinned where it was dropped.
807BOOST_FIXTURE_TEST_CASE( MoveReSolvesCluster, DRAG_FIXTURE )
808{
809 PCB_SHAPE* line = addSegment( { 0, 0 }, { 20 * MM, 0 } );
810 PCB_SHAPE* seg = addSegment( { 2 * MM, 5 * MM }, { 5 * MM, 0 } );
811
813 c->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::END );
814 c->AddMember( line->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
815 board.Add( c );
816
819 board.Add( f1 );
820
823 board.Add( f2 );
824
825 seg->Move( { 0, 3 * MM } );
826 BOOST_CHECK_EQUAL( seg->GetEnd().y, 3 * MM );
827
828 std::vector<PCB_SHAPE*> modified;
829 ReSolveShapeClusters( &board, { seg }, &modified );
830
831 // The end is back on the line, the dropped start held, and the fixed line did not move.
832 BOOST_CHECK_LE( std::abs( seg->GetEnd().y ), 5000 );
833 BOOST_CHECK_LE( ( seg->GetStart() - VECTOR2I( 2 * MM, 8 * MM ) ).EuclideanNorm(), 5000.0 );
834 BOOST_CHECK_EQUAL( line->GetStart(), VECTOR2I( 0, 0 ) );
835 BOOST_CHECK_EQUAL( line->GetEnd(), VECTOR2I( 20 * MM, 0 ) );
836 BOOST_CHECK( std::find( modified.begin(), modified.end(), seg ) != modified.end() );
837}
838
839
840// The live move drag stages every neighbor it pulls along into the drag commit, so cancelling the
841// move (localCommit.Revert) must restore both the moved shape and its solved neighbor.
842BOOST_FIXTURE_TEST_CASE( MoveReSolveStagesNeighborsRevertRestores, DRAG_FIXTURE )
843{
844 PCB_SHAPE* a = addSegment( { 0, 0 }, { 10 * MM, 0 } );
845 PCB_SHAPE* b = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
846
847 addCoincident( a, CONSTRAINT_ANCHOR::END, b, CONSTRAINT_ANCHOR::START );
848
849 const VECTOR2I aStart0 = a->GetStart();
850 const VECTOR2I aEnd0 = a->GetEnd();
851 const VECTOR2I bStart0 = b->GetStart();
852
853 // Mirrors drag loop staging moved shape translating it then resolving cluster into same commit
854 // staging each neighbor the solver touches
855 BOARD_COMMIT commit( tool );
856 std::vector<PCB_SHAPE*> modified;
857
858 commit.Modify( a );
859 a->Move( { 0, 4 * MM } );
860
861 ReSolveShapeClusters( &board, { a }, &modified,
862 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
863
864 // The neighbor followed the moved corner.
865 BOOST_CHECK_LE( ( a->GetEnd() - b->GetStart() ).EuclideanNorm(), 100 );
866 BOOST_CHECK( b->GetStart() != bStart0 );
867
868 // Cancelling the move restores every shape the drag commit staged.
869 commit.Revert();
870 BOOST_CHECK_EQUAL( a->GetStart(), aStart0 );
871 BOOST_CHECK_EQUAL( a->GetEnd(), aEnd0 );
872 BOOST_CHECK_EQUAL( b->GetStart(), bStart0 );
873}
874
875
876// Mouse-up pushes the last painted preview without another solve. The pushed transaction must
877// therefore preserve every coordinate that was visible in the final preview frame.
878BOOST_FIXTURE_TEST_CASE( MoveCommitEqualsLastPaintedPreview, DRAG_FIXTURE )
879{
880 PCB_SHAPE* a = addSegment( { 0, 0 }, { 10 * MM, 0 } );
881 PCB_SHAPE* b = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
882
883 addCoincident( a, CONSTRAINT_ANCHOR::END, b, CONSTRAINT_ANCHOR::START );
884
885 BOARD_COMMIT commit( tool );
886 std::vector<PCB_SHAPE*> modified;
887
888 commit.Modify( a );
889 a->Move( { 2 * MM, 4 * MM } );
890
891 ReSolveShapeClusters( &board, { a }, &modified,
892 [&]( BOARD_ITEM* aItem )
893 {
894 commit.Modify( aItem );
895 } );
896
897 const std::array<VECTOR2I, 4> painted = { a->GetStart(), a->GetEnd(), b->GetStart(), b->GetEnd() };
898
899 commit.Push( wxT( "Move" ) );
900
901 const std::array<VECTOR2I, 4> committed = { a->GetStart(), a->GetEnd(), b->GetStart(), b->GetEnd() };
902
903 BOOST_CHECK_EQUAL_COLLECTIONS( committed.begin(), committed.end(), painted.begin(), painted.end() );
904}
905
906
907// The caller-owned, Pack and Duplicate move paths pass no constraint shapes, so the drag collects
908// none and the settle-solve runs over an empty set. That must stage nothing and touch no neighbor,
909// mirroring the guard that keeps those commits free of constraint side effects.
910BOOST_FIXTURE_TEST_CASE( MoveEmptyShapesStagesNoNeighbor, DRAG_FIXTURE )
911{
912 PCB_SHAPE* a = addSegment( { 0, 0 }, { 10 * MM, 0 } );
913 PCB_SHAPE* b = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
914
915 addCoincident( a, CONSTRAINT_ANCHOR::END, b, CONSTRAINT_ANCHOR::START );
916
917 const VECTOR2I bStart0 = b->GetStart();
918
919 BOARD_COMMIT commit( tool );
920 std::vector<PCB_SHAPE*> modified;
921 int staged = 0;
922
923 ReSolveShapeClusters( &board, {}, &modified,
924 [&]( BOARD_ITEM* ) { staged++; } );
925
926 BOOST_CHECK_EQUAL( staged, 0 );
927 BOOST_CHECK( modified.empty() );
928 BOOST_CHECK_EQUAL( b->GetStart(), bStart0 );
929 BOOST_CHECK( commit.Empty() );
930}
931
932
933// A fixed-length segment dragged by one endpoint rotates about the other end, which stays put.
934// Without pinning the far end the solver is free to translate the whole segment (Zulip "Constraint
935// solver", 2026-07-10: "we need to pin the other end of the line when moving, not only the length").
936BOOST_FIXTURE_TEST_CASE( FixedLengthDragPinsFarEnd, DRAG_FIXTURE )
937{
938 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 10 * MM, 0 } );
939
941 len->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
942 len->SetValue( 10.0 * MM );
943 board.Add( len );
944
945 const VECTOR2I start0 = seg->GetStart();
946
947 std::vector<PCB_SHAPE*> modified;
948
949 BOARD_COMMIT commit( tool );
950
951 // Drag END to a 6-8-10 point, so the held 10 mm length lands it exactly on the cursor.
952 simulateDrag( commit, &board, seg, CONSTRAINT_ANCHOR::END, { 6 * MM, 8 * MM }, &modified );
953
954 // The far (start) end held where it was, and the length is unchanged.
955 BOOST_CHECK_LE( ( seg->GetStart() - start0 ).EuclideanNorm(), 5000.0 );
956 BOOST_CHECK_LE( std::abs( ( seg->GetEnd() - seg->GetStart() ).EuclideanNorm() - 10.0 * MM ), 5000.0 );
957
958 // The dragged end reached the cursor, which sat on the length circle.
959 BOOST_CHECK_LE( ( seg->GetEnd() - VECTOR2I( 6 * MM, 8 * MM ) ).EuclideanNorm(), 20000.0 );
960
961 commit.Revert();
962}
963
964
965// A fixed-length segment dragged by one end holds the far end even when the cursor is off the length
966// circle. The far end used to drift to split the pin error between the two ends.
967BOOST_FIXTURE_TEST_CASE( FixedLengthDragOffCircleHoldsFarEnd, DRAG_FIXTURE )
968{
969 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 10 * MM, 0 } );
970
972 len->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
973 len->SetValue( 10.0 * MM );
974 board.Add( len );
975
976 const VECTOR2I start0 = seg->GetStart();
977
978 std::vector<PCB_SHAPE*> modified;
979 BOARD_COMMIT commit( tool );
980
981 // Cursor 20 mm out on +x, off the 10 mm circle. Far end holds, dragged end lands at {10 mm, 0}.
982 simulateDrag( commit, &board, seg, CONSTRAINT_ANCHOR::END, { 20 * MM, 0 }, &modified );
983
984 BOOST_CHECK_LE( ( seg->GetStart() - start0 ).EuclideanNorm(), 5000.0 );
985 BOOST_CHECK_LE( std::abs( ( seg->GetEnd() - seg->GetStart() ).EuclideanNorm() - 10.0 * MM ), 5000.0 );
986 BOOST_CHECK_LE( ( seg->GetEnd() - VECTOR2I( 10 * MM, 0 ) ).EuclideanNorm(), 20000.0 );
987
988 commit.Revert();
989}
990
991
992// Dragging one endpoint of a constrained arc holds the circle (centre + radius) and the far
993// endpoint, so only the dragged endpoint sweeps -- the arc does not drift or balloon.
994BOOST_FIXTURE_TEST_CASE( ArcEndpointDragHoldsCircleAndFarEnd, DRAG_FIXTURE )
995{
996 PCB_SHAPE* arc = addArc( { 10 * MM, 0 }, { 7071068, 7071068 }, { 0, 10 * MM } ); // 90 deg, r 10
997 PCB_SHAPE* seg = addSegment( { 0, 10 * MM }, { 5 * MM, 15 * MM } );
998 addCoincident( arc, CONSTRAINT_ANCHOR::END, seg, CONSTRAINT_ANCHOR::START );
999
1000 const VECTOR2I center0 = arc->GetCenter();
1001 const int radius0 = arc->GetRadius();
1002 const VECTOR2I end0 = arc->GetEnd();
1003
1004 // Even an off-circle target is projected onto the held circle inside the adapter, so the centre,
1005 // radius and far end stay put and only the dragged endpoint sweeps.
1006 std::vector<PCB_SHAPE*> modified;
1007 BOARD_COMMIT commit( tool );
1008 SolveCluster( &board, { arc->m_Uuid, CONSTRAINT_ANCHOR::START }, { 12 * MM, 3 * MM }, &modified,
1009 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
1010
1011 BOOST_CHECK_LE( ( arc->GetCenter() - center0 ).EuclideanNorm(), 20000.0 );
1012 BOOST_CHECK_LE( std::abs( arc->GetRadius() - radius0 ), 20000 );
1013 BOOST_CHECK_LE( ( arc->GetEnd() - end0 ).EuclideanNorm(), 20000.0 );
1014 BOOST_CHECK( ( arc->GetStart() - VECTOR2I( 10 * MM, 0 ) ).EuclideanNorm() > 20000.0 );
1015}
1016
1017
1018// Real FIXED_RADIUS on arc overrides temporary radius hold
1019// Dragging endpoint keeps driven radius so far end moves off old spot to stay on the now larger circle
1020BOOST_FIXTURE_TEST_CASE( ArcEndpointDragYieldsToFixedRadius, DRAG_FIXTURE )
1021{
1022 PCB_SHAPE* arc = addArc( { 10 * MM, 0 }, { 7071068, 7071068 }, { 0, 10 * MM } ); // r 10
1023
1025 r->AddMember( arc->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
1026 r->SetValue( 12.0 * MM ); // drive the radius larger than the current 10 mm
1027 board.Add( r );
1028
1029 std::vector<PCB_SHAPE*> modified;
1030 BOARD_COMMIT commit( tool );
1031 SolveCluster( &board, { arc->m_Uuid, CONSTRAINT_ANCHOR::START }, { 12 * MM, 0 }, &modified,
1032 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
1033
1034 // The driving radius wins over the temporary hold.
1035 BOOST_CHECK_LE( std::abs( arc->GetRadius() - 12 * MM ), 20000 );
1036}
1037
1038
1039// Dragging a rectangle corner resizes about the diagonally opposite corner, which
1040// pinDraggedShapeRest holds. The rect stores its corners swapped (start is the bottom-right), so a
1041// broken canonical corner-role mapping would drive or hold the wrong corner and fail both checks.
1042BOOST_FIXTURE_TEST_CASE( RectCornerDragHoldsOppositeCorner, DRAG_FIXTURE )
1043{
1044 PCB_SHAPE* rect = addRect( { 10 * MM, 10 * MM }, { 0, 0 } ); // swapped storage
1045 PCB_SHAPE* seg = addSegment( { 10 * MM, 0 }, { 15 * MM, -5 * MM } );
1046
1047 // Tie the dragged corner (canonical index 1, the corner at (10mm, 0)) to a segment end so the
1048 // cluster contains a mappable constraint and the solve runs.
1050 c->AddMember( rect->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 1 );
1051 c->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::START );
1052 board.Add( c );
1053
1054 const VECTOR2I cursor( 12 * MM, -2 * MM );
1055
1056 std::vector<PCB_SHAPE*> modified;
1057 BOARD_COMMIT commit( tool );
1058 SolveCluster( &board, { rect->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 1 }, cursor, &modified,
1059 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
1060
1061 VECTOR2I tl( std::min( rect->GetStart().x, rect->GetEnd().x ),
1062 std::min( rect->GetStart().y, rect->GetEnd().y ) );
1063 VECTOR2I br( std::max( rect->GetStart().x, rect->GetEnd().x ),
1064 std::max( rect->GetStart().y, rect->GetEnd().y ) );
1065
1066 // The dragged top-right corner landed on the cursor and pulled the coincident segment along.
1067 BOOST_CHECK_LE( ( VECTOR2I( br.x, tl.y ) - cursor ).EuclideanNorm(), 5000.0 );
1068 BOOST_CHECK_LE( ( seg->GetStart() - cursor ).EuclideanNorm(), 5000.0 );
1069
1070 // The opposite (bottom-left) corner held its position.
1071 BOOST_CHECK_LE( ( VECTOR2I( tl.x, br.y ) - VECTOR2I( 0, 10 * MM ) ).EuclideanNorm(), 5000.0 );
1072}
1073
1074
1075// Dragging one polygon vertex moves only that vertex; pinDraggedShapeRest holds every other vertex
1076// so the rest of the outline does not drift.
1077BOOST_FIXTURE_TEST_CASE( PolyVertexDragHoldsOtherVertices, DRAG_FIXTURE )
1078{
1079 const std::vector<VECTOR2I> points{ { 0, 0 },
1080 { 10 * MM, 0 },
1081 { 13 * MM, 8 * MM },
1082 { 5 * MM, 14 * MM },
1083 { -3 * MM, 8 * MM } };
1084
1085 PCB_SHAPE* poly = addPoly( points );
1086 PCB_SHAPE* seg = addSegment( { 0, 0 }, { -5 * MM, -5 * MM } );
1087
1088 // Tie one vertex to a segment start so the cluster contains a mappable constraint.
1090 c->AddMember( poly->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 0 );
1091 c->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::START );
1092 board.Add( c );
1093
1094 const VECTOR2I cursor( 16 * MM, 9 * MM );
1095
1096 std::vector<PCB_SHAPE*> modified;
1097 BOARD_COMMIT commit( tool );
1098 SolveCluster( &board, { poly->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 2 }, cursor, &modified,
1099 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
1100
1101 const SHAPE_LINE_CHAIN& outline = poly->GetPolyShape().COutline( 0 );
1102
1103 BOOST_REQUIRE_EQUAL( outline.PointCount(), 5 );
1104 BOOST_CHECK_LE( ( outline.CPoint( 2 ) - cursor ).EuclideanNorm(), 5000.0 );
1105
1106 for( int i : { 0, 1, 3, 4 } )
1107 BOOST_CHECK_LE( ( outline.CPoint( i ) - points[i] ).EuclideanNorm(), 5000.0 );
1108}
1109
1110
1111// The point-editor bridge maps a dragged rectangle corner handle to its canonical min/max corner,
1112// independent of which diagonal the shape stores; ordinals past the corners map to nothing, so the
1113// centre and radius handles fall through to the authoritative-shape re-solve instead.
1114BOOST_FIXTURE_TEST_CASE( VertexForRectCornerMapsCanonicalIndex, DRAG_FIXTURE )
1115{
1116 PCB_SHAPE* rect = addRect( { 0, 0 }, { 10 * MM, 10 * MM } );
1117 PCB_SHAPE* swapped = addRect( { 10 * MM, 10 * MM }, { 0, 0 } ); // swapped storage
1118
1119 for( PCB_SHAPE* shape : { rect, swapped } )
1120 {
1121 const std::vector<VECTOR2I> corners{ { 0, 0 },
1122 { 10 * MM, 0 },
1123 { 10 * MM, 10 * MM },
1124 { 0, 10 * MM } };
1125
1126 for( size_t i = 0; i < corners.size(); ++i )
1127 {
1128 std::optional<CONSTRAINT_ANCHOR_POINT> vertex = ConstraintShapeVertex( shape, (int) i );
1129
1130 BOOST_REQUIRE( vertex.has_value() );
1131 BOOST_CHECK( vertex->anchor == CONSTRAINT_ANCHOR::VERTEX );
1132 BOOST_CHECK_EQUAL( vertex->index, (int) i );
1133 BOOST_CHECK( vertex->pos == corners[i] );
1134 }
1135 }
1136
1139
1140 // A segment exposes endpoint anchors, not vertices, so an ordinal on it maps to nothing.
1141 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1142 BOOST_CHECK( !ConstraintShapeVertex( seg, 0 ) );
1143}
1144
1145
1146// The point-editor bridge maps a dragged polygon vertex handle to its outline ordinal; an ordinal
1147// past the outline, or any ordinal on an arc-bearing polygon, maps to nothing.
1148BOOST_FIXTURE_TEST_CASE( VertexForPolyMapsOrdinal, DRAG_FIXTURE )
1149{
1150 const std::vector<VECTOR2I> points{ { 0, 0 },
1151 { 10 * MM, 0 },
1152 { 13 * MM, 8 * MM },
1153 { 5 * MM, 14 * MM },
1154 { -3 * MM, 8 * MM } };
1155
1156 PCB_SHAPE* poly = addPoly( points );
1157
1158 for( size_t i = 0; i < points.size(); ++i )
1159 {
1160 std::optional<CONSTRAINT_ANCHOR_POINT> vertex = ConstraintShapeVertex( poly, (int) i );
1161
1162 BOOST_REQUIRE( vertex.has_value() );
1163 BOOST_CHECK( vertex->anchor == CONSTRAINT_ANCHOR::VERTEX );
1164 BOOST_CHECK_EQUAL( vertex->index, (int) i );
1165 BOOST_CHECK( vertex->pos == points[i] );
1166 }
1167
1168 BOOST_CHECK( !ConstraintShapeVertex( poly, (int) points.size() ) );
1169
1170 PCB_SHAPE* arcPoly = new PCB_SHAPE( &board, SHAPE_T::POLY );
1171
1173 chain.Append( VECTOR2I( 10 * MM, 10 * MM ) );
1174 chain.Append( VECTOR2I( 50 * MM, 10 * MM ) );
1175 chain.Append( SHAPE_ARC( { 50 * MM, 10 * MM }, { 55 * MM, 25 * MM }, { 50 * MM, 40 * MM }, 0 ) );
1176 chain.Append( VECTOR2I( 10 * MM, 40 * MM ) );
1177 chain.SetClosed( true );
1178
1179 arcPoly->GetPolyShape().AddOutline( chain );
1180 board.Add( arcPoly );
1181
1182 BOOST_REQUIRE_GT( arcPoly->GetPolyShape().COutline( 0 ).ArcCount(), 0 );
1183
1184 // The solver never ingests an arc-bearing outline, so its vertices must not map to members.
1185 BOOST_CHECK( !ConstraintShapeVertex( arcPoly, 0 ) );
1186}
1187
1188
1189// Mimics point editor end to end for rectangle corner
1190// Behavior moves corner first bridge maps dragged handle ordinal to canonical corner and solve pulls coincident segment along
1191BOOST_FIXTURE_TEST_CASE( RectCornerDragMapsThenSolves, DRAG_FIXTURE )
1192{
1193 PCB_SHAPE* rect = addRect( { 0, 0 }, { 10 * MM, 10 * MM } );
1194 PCB_SHAPE* seg = addSegment( { 10 * MM, 0 }, { 15 * MM, -5 * MM } );
1195
1197 c->AddMember( rect->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 1 );
1198 c->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::START );
1199 board.Add( c );
1200
1201 // Drag the top-right corner as RECTANGLE_POINT_EDIT_BEHAVIOR::UpdateItem would.
1202 const VECTOR2I cursor( 12 * MM, -2 * MM );
1203 rect->SetTop( cursor.y );
1204 rect->SetRight( cursor.x );
1205
1206 std::optional<CONSTRAINT_ANCHOR_POINT> vertex = ConstraintShapeVertex( rect, 1 );
1207
1208 BOOST_REQUIRE( vertex.has_value() );
1209 BOOST_CHECK( vertex->pos == cursor );
1210
1211 std::vector<PCB_SHAPE*> modified;
1212 BOARD_COMMIT commit( tool );
1213 SolveCluster( &board, { rect->m_Uuid, vertex->anchor, vertex->index }, vertex->pos, &modified,
1214 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
1215
1216 BOOST_CHECK_LE( ( seg->GetStart() - cursor ).EuclideanNorm(), 5000.0 );
1217}
1218
1219
1220// PinEditedCorner clamps a corner drag at minimum size so shape holds clamped corner while edit point keeps raw cursor
1221// Mapping by handle ordinal and solving toward post clamp corner keeps neighbor riding its real position
1222BOOST_FIXTURE_TEST_CASE( ClampedRectCornerDragSolvesToClampedCorner, DRAG_FIXTURE )
1223{
1224 PCB_SHAPE* rect = addRect( { 0, 0 }, { 10 * MM, 10 * MM } );
1225 PCB_SHAPE* seg = addSegment( { 10 * MM, 0 }, { 15 * MM, -5 * MM } );
1226
1228 c->AddMember( rect->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 1 );
1229 c->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::START );
1230 board.Add( c );
1231
1232 // Drag the top-right corner far past the left edge; the behavior clamps x to the minimum
1233 // width while y follows the cursor, so the shape corner and the raw cursor diverge.
1234 const VECTOR2I rawCursor( -5 * MM, -2 * MM );
1235 const int minWidth = 25400; // 1 mil floor applied by PinEditedCorner
1236 const VECTOR2I clamped( minWidth, rawCursor.y );
1237
1238 rect->SetTop( clamped.y );
1239 rect->SetRight( clamped.x );
1240
1241 std::optional<CONSTRAINT_ANCHOR_POINT> vertex = ConstraintShapeVertex( rect, 1 );
1242
1243 BOOST_REQUIRE( vertex.has_value() );
1244 BOOST_CHECK( vertex->pos == clamped );
1245 BOOST_CHECK( vertex->pos != rawCursor );
1246
1247 std::vector<PCB_SHAPE*> modified;
1248 BOARD_COMMIT commit( tool );
1249 SolveCluster( &board, { rect->m_Uuid, vertex->anchor, vertex->index }, vertex->pos, &modified,
1250 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
1251
1252 BOOST_CHECK_LE( ( seg->GetStart() - clamped ).EuclideanNorm(), 5000.0 );
1253}
1254
1255
1256namespace
1257{
1258VECTOR2I dragRectTopLeft( const PCB_SHAPE* aRect )
1259{
1260 return VECTOR2I( std::min( aRect->GetStart().x, aRect->GetEnd().x ),
1261 std::min( aRect->GetStart().y, aRect->GetEnd().y ) );
1262}
1263
1264
1265VECTOR2I dragRectBotRight( const PCB_SHAPE* aRect )
1266{
1267 return VECTOR2I( std::max( aRect->GetStart().x, aRect->GetEnd().x ),
1268 std::max( aRect->GetStart().y, aRect->GetEnd().y ) );
1269}
1270
1271
1272PCB_CONSTRAINT* addDrivingVertexLength( BOARD& aBoard, PCB_SHAPE* aShape, int aIndexA, int aIndexB,
1273 double aLengthIU )
1274{
1276 c->AddMember( aShape->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, aIndexA );
1277 c->AddMember( aShape->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, aIndexB );
1278 c->SetValue( aLengthIU );
1279 c->SetDriving( true );
1280 aBoard.Add( c );
1281 return c;
1282}
1283} // namespace
1284
1285
1286// Driving width on top side must survive adjacent side drag where side handles used to bypass solver and violate length
1287// Pinning side canonical corner routes it through same drag solve as corner drag so hard length wins and rectangle translates
1288BOOST_FIXTURE_TEST_CASE( RectSideDragEnforcesDrivingWidth, DRAG_FIXTURE )
1289{
1290 PCB_SHAPE* rect = addRect( { 0, 0 }, { 10 * MM, 10 * MM } );
1291
1292 addDrivingVertexLength( board, rect, 0, 1, 10.0 * MM ); // TL-TR width
1293
1294 // Drag the right side 5 mm out, as RECTANGLE_POINT_EDIT_BEHAVIOR::UpdateItem would.
1295 rect->SetRight( 15 * MM );
1296
1297 // Side RECT_RIGHT (1) maps to canonical corner 1 (TR), read back post-move.
1298 std::optional<CONSTRAINT_ANCHOR_POINT> corner = ConstraintShapeVertex( rect, 1 );
1299
1300 BOOST_REQUIRE( corner.has_value() );
1301 BOOST_CHECK( corner->pos == VECTOR2I( 15 * MM, 0 ) );
1302
1303 std::vector<PCB_SHAPE*> modified;
1304 BOARD_COMMIT commit( tool );
1305 SolveCluster( &board, { rect->m_Uuid, corner->anchor, corner->index }, corner->pos, &modified,
1306 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
1307
1308 const VECTOR2I tl = dragRectTopLeft( rect );
1309 const VECTOR2I br = dragRectBotRight( rect );
1310
1311 // The driving width held against the drag and the height stayed untouched.
1312 BOOST_CHECK_LE( std::abs( ( br.x - tl.x ) - 10 * MM ), 20000 );
1313 BOOST_CHECK_LE( std::abs( ( br.y - tl.y ) - 10 * MM ), 20000 );
1314
1315 // Drag was not simply refused rect moved toward cursor strictly between original and requested span
1316 BOOST_CHECK_GT( tl.x, 1 * MM );
1317 BOOST_CHECK_LT( br.x, 14 * MM );
1318
1319 BOOST_TEST_MESSAGE( "side drag under driving width settled TL at " << tl.x << "," << tl.y );
1320
1321 // The four enumerated corners agree with the solved extremes, so the rect stayed axis-aligned.
1322 std::vector<CONSTRAINT_ANCHOR_POINT> corners = ConstraintShapeAnchors( rect );
1323
1324 BOOST_REQUIRE_EQUAL( corners.size(), 4 );
1325 BOOST_CHECK_EQUAL( corners[0].pos, tl );
1326 BOOST_CHECK_EQUAL( corners[2].pos, br );
1327}
1328
1329
1330// Dragging bottom side leaves top side width constraint satisfiable so resize applies exactly
1331// Height follows handle while held top left corner and driven width stay intact
1332BOOST_FIXTURE_TEST_CASE( RectSideDragOffConstrainedAxisResizes, DRAG_FIXTURE )
1333{
1334 PCB_SHAPE* rect = addRect( { 0, 0 }, { 10 * MM, 10 * MM } );
1335
1336 addDrivingVertexLength( board, rect, 0, 1, 10.0 * MM ); // TL-TR width
1337
1338 // Drag the bottom side 5 mm down; side RECT_BOT (2) maps to canonical corner 2 (BR).
1339 rect->SetBottom( 15 * MM );
1340
1341 std::optional<CONSTRAINT_ANCHOR_POINT> corner = ConstraintShapeVertex( rect, 2 );
1342
1343 BOOST_REQUIRE( corner.has_value() );
1344 BOOST_CHECK( corner->pos == VECTOR2I( 10 * MM, 15 * MM ) );
1345
1346 std::vector<PCB_SHAPE*> modified;
1347 BOARD_COMMIT commit( tool );
1348 SolveCluster( &board, { rect->m_Uuid, corner->anchor, corner->index }, corner->pos, &modified,
1349 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
1350
1351 const VECTOR2I tl = dragRectTopLeft( rect );
1352 const VECTOR2I br = dragRectBotRight( rect );
1353
1354 BOOST_CHECK_LE( tl.EuclideanNorm(), 5000.0 );
1355 BOOST_CHECK_LE( std::abs( ( br.x - tl.x ) - 10 * MM ), 5000 );
1356 BOOST_CHECK_LE( std::abs( ( br.y - tl.y ) - 15 * MM ), 5000 );
1357}
1358
1359
1360// A side drag on a rectangle whose own dimensions are unconstrained resizes exactly as the handle
1361// placed it; the coincident neighbor on an unmoved corner stays put.
1362BOOST_FIXTURE_TEST_CASE( UnconstrainedRectSideDragResizesExactly, DRAG_FIXTURE )
1363{
1364 PCB_SHAPE* rect = addRect( { 0, 0 }, { 10 * MM, 10 * MM } );
1365 PCB_SHAPE* seg = addSegment( { 0, 0 }, { -5 * MM, -5 * MM } );
1366
1368 c->AddMember( rect->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 0 );
1369 c->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::START );
1370 board.Add( c );
1371
1372 rect->SetRight( 15 * MM );
1373
1374 std::optional<CONSTRAINT_ANCHOR_POINT> corner = ConstraintShapeVertex( rect, 1 );
1375
1376 BOOST_REQUIRE( corner.has_value() );
1377
1378 std::vector<PCB_SHAPE*> modified;
1379 BOARD_COMMIT commit( tool );
1380 SolveCluster( &board, { rect->m_Uuid, corner->anchor, corner->index }, corner->pos, &modified,
1381 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); } );
1382
1383 BOOST_CHECK_LE( dragRectTopLeft( rect ).EuclideanNorm(), 5000.0 );
1384 BOOST_CHECK_LE( ( dragRectBotRight( rect ) - VECTOR2I( 15 * MM, 10 * MM ) ).EuclideanNorm(), 5000.0 );
1385 BOOST_CHECK_LE( seg->GetStart().EuclideanNorm(), 5000.0 );
1386}
1387
1388
1389// Driving length on polygon edge must survive a drag of that same edge
1390// Both endpoints ride co dragged pins that yield to hard length so edge lands at driven length and unbound vertices hold
1391BOOST_FIXTURE_TEST_CASE( PolyEdgeDragEnforcesDrivingLength, DRAG_FIXTURE )
1392{
1393 const std::vector<VECTOR2I> points{ { 0, 0 },
1394 { 10 * MM, 0 },
1395 { 13 * MM, 8 * MM },
1396 { 5 * MM, 14 * MM },
1397 { -3 * MM, 8 * MM } };
1398
1399 PCB_SHAPE* poly = addPoly( points );
1400 const double edgeLen = std::hypot( 3.0, 8.0 ) * MM; // the 1-2 edge's current length
1401
1402 addDrivingVertexLength( board, poly, 1, 2, edgeLen );
1403
1404 // Drag the 1-2 edge so both vertices move and its length would stretch.
1405 std::vector<VECTOR2I> dragged = points;
1406 dragged[1] = { 8 * MM, -2 * MM };
1407 dragged[2] = { 16 * MM, 9 * MM };
1408 poly->SetPolyPoints( dragged );
1409
1410 std::vector<PCB_SHAPE*> modified;
1411 BOARD_COMMIT commit( tool );
1412 SolveCluster( &board, { poly->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 1 }, dragged[1], &modified,
1413 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); },
1414 /* aIncludeDragged */ false, /* aStabilize */ false, {},
1415 std::pair{ CONSTRAINT_MEMBER( poly->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 2 ), dragged[2] } );
1416
1417 const SHAPE_LINE_CHAIN& outline = poly->GetPolyShape().COutline( 0 );
1418
1419 double solvedLen = ( outline.CPoint( 2 ) - outline.CPoint( 1 ) ).EuclideanNorm();
1420
1421 BOOST_CHECK_LE( std::abs( solvedLen - edgeLen ), 20000.0 );
1422
1423 // Drag was not simply refused edge midpoint moved out of solver noise toward requested midpoint
1424 // Length constrains only vertex separation so midpoint is free to follow landing closer than start
1425 const VECTOR2I midBefore = ( points[1] + points[2] ) / 2;
1426 const VECTOR2I midTarget = ( dragged[1] + dragged[2] ) / 2;
1427 const VECTOR2I midSolved = ( outline.CPoint( 1 ) + outline.CPoint( 2 ) ) / 2;
1428
1429 BOOST_CHECK_LT( ( midSolved - midTarget ).EuclideanNorm(), ( midBefore - midTarget ).EuclideanNorm() );
1430 BOOST_CHECK_GT( ( midSolved - midBefore ).EuclideanNorm(), 250000.0 );
1431
1432 for( int i : { 0, 3, 4 } )
1433 BOOST_CHECK_LE( ( outline.CPoint( i ) - points[i] ).EuclideanNorm(), 5000.0 );
1434}
1435
1436
1437// An unconstrained polygon edge drag lands both vertices exactly where the handle placed them; the
1438// vertex-bound segment follows and the unbound vertices hold.
1439BOOST_FIXTURE_TEST_CASE( UnconstrainedPolyEdgeDragMovesBothVertices, DRAG_FIXTURE )
1440{
1441 const std::vector<VECTOR2I> points{ { 0, 0 },
1442 { 10 * MM, 0 },
1443 { 13 * MM, 8 * MM },
1444 { 5 * MM, 14 * MM },
1445 { -3 * MM, 8 * MM } };
1446
1447 PCB_SHAPE* poly = addPoly( points );
1448 PCB_SHAPE* seg = addSegment( points[1], { 25 * MM, -5 * MM } );
1449
1451 c->AddMember( poly->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 1 );
1452 c->AddMember( seg->m_Uuid, CONSTRAINT_ANCHOR::START );
1453 board.Add( c );
1454
1455 // Translate the 1-2 edge 3 mm right, as the edge handle would.
1456 std::vector<VECTOR2I> dragged = points;
1457 dragged[1] += VECTOR2I( 3 * MM, 0 );
1458 dragged[2] += VECTOR2I( 3 * MM, 0 );
1459 poly->SetPolyPoints( dragged );
1460
1461 std::vector<PCB_SHAPE*> modified;
1462 BOARD_COMMIT commit( tool );
1463 SolveCluster( &board, { poly->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 1 }, dragged[1], &modified,
1464 [&]( BOARD_ITEM* aItem ) { commit.Modify( aItem ); },
1465 /* aIncludeDragged */ false, /* aStabilize */ false, {},
1466 std::pair{ CONSTRAINT_MEMBER( poly->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 2 ), dragged[2] } );
1467
1468 const SHAPE_LINE_CHAIN& outline = poly->GetPolyShape().COutline( 0 );
1469
1470 BOOST_CHECK_LE( ( outline.CPoint( 1 ) - dragged[1] ).EuclideanNorm(), 5000.0 );
1471 BOOST_CHECK_LE( ( outline.CPoint( 2 ) - dragged[2] ).EuclideanNorm(), 5000.0 );
1472 BOOST_CHECK_LE( ( seg->GetStart() - dragged[1] ).EuclideanNorm(), 5000.0 );
1473 BOOST_CHECK( std::find( modified.begin(), modified.end(), seg ) != modified.end() );
1474
1475 for( int i : { 0, 3, 4 } )
1476 BOOST_CHECK_LE( ( outline.CPoint( i ) - points[i] ).EuclideanNorm(), 5000.0 );
1477}
1478
1479
1480// Dragging an arc's centre holds both endpoints, so the centre moves and the radius adapts.
1481BOOST_FIXTURE_TEST_CASE( ArcCentreDragHoldsEndpoints, DRAG_FIXTURE )
1482{
1483 PCB_SHAPE* arc = addArc( { 10 * MM, 0 }, { 7071068, 7071068 }, { 0, 10 * MM } );
1484 PCB_SHAPE* seg = addSegment( { 0, 10 * MM }, { 5 * MM, 15 * MM } );
1485 addCoincident( arc, CONSTRAINT_ANCHOR::END, seg, CONSTRAINT_ANCHOR::START );
1486
1487 const VECTOR2I start0 = arc->GetStart();
1488 const VECTOR2I end0 = arc->GetEnd();
1489 const int radius0 = arc->GetRadius();
1490
1491 std::vector<PCB_SHAPE*> modified;
1492 BOARD_COMMIT commit( tool );
1493 SolveCluster( &board, { arc->m_Uuid, CONSTRAINT_ANCHOR::CENTER }, { 1 * MM, 1 * MM }, &modified,
1494 [&]( BOARD_ITEM* aItem )
1495 {
1496 commit.Modify( aItem );
1497 } );
1498
1499 BOOST_CHECK_LE( ( arc->GetStart() - start0 ).EuclideanNorm(), 20000.0 );
1500 BOOST_CHECK_LE( ( arc->GetEnd() - end0 ).EuclideanNorm(), 20000.0 );
1501 BOOST_CHECK( ( arc->GetCenter() - VECTOR2I( 0, 0 ) ).EuclideanNorm() > 100000.0 );
1502 BOOST_CHECK_GT( std::abs( arc->GetRadius() - radius0 ), 100000 );
1503}
1504
1505
1506// Picking the very point a FIXED_POSITION constraint nails down. The drag writes the cursor
1507// into the shape before the solve, so the solve has to put that point back.
1508BOOST_FIXTURE_TEST_CASE( BlockEditHoldsAFixedPositionAnchor, DRAG_FIXTURE )
1509{
1510 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1511 addFixedPosition( seg, CONSTRAINT_ANCHOR::START );
1512
1514 BOOST_REQUIRE( session.Build( &board, { seg } ) );
1515
1516 const VECTOR2I delta( 2 * MM, 3 * MM );
1517 BOARD_COMMIT commit( tool );
1518
1519 commit.Modify( seg );
1520 seg->SetStart( seg->GetStart() + delta );
1521
1522 std::vector<PCB_SHAPE*> moved;
1524 session.Solve( { { CONSTRAINT_MEMBER( seg->m_Uuid, CONSTRAINT_ANCHOR::START ), delta } }, {}, delta, &moved,
1525 [&]( BOARD_ITEM* aItem )
1526 {
1527 commit.Modify( aItem );
1528 } );
1529
1531 BOOST_CHECK_EQUAL( seg->GetStart(), VECTOR2I( 0, 0 ) );
1532 BOOST_CHECK_EQUAL( seg->GetEnd(), VECTOR2I( 10 * MM, 0 ) );
1533
1534 commit.Revert();
1535}
1536
1537
1538// The constraint holds one end, it does not freeze the shape. Picking both ends has to leave
1539// the free one exactly where the user put it.
1540BOOST_FIXTURE_TEST_CASE( BlockEditMovesAFreeEndWhileHoldingAFixedStart, DRAG_FIXTURE )
1541{
1542 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1543 addFixedPosition( seg, CONSTRAINT_ANCHOR::START );
1544
1546 BOOST_REQUIRE( session.Build( &board, { seg } ) );
1547
1548 const VECTOR2I delta( 2 * MM, 3 * MM );
1549 BOARD_COMMIT commit( tool );
1550
1551 commit.Modify( seg );
1552 seg->SetStart( seg->GetStart() + delta );
1553 seg->SetEnd( seg->GetEnd() + delta );
1554
1555 std::vector<PCB_SHAPE*> moved;
1558 { CONSTRAINT_MEMBER( seg->m_Uuid, CONSTRAINT_ANCHOR::END ), VECTOR2I( 10 * MM, 0 ) + delta } },
1559 {}, delta, &moved,
1560 [&]( BOARD_ITEM* aItem )
1561 {
1562 commit.Modify( aItem );
1563 } );
1564
1566 BOOST_CHECK_EQUAL( seg->GetStart(), VECTOR2I( 0, 0 ) );
1567 BOOST_CHECK_EQUAL( seg->GetEnd(), VECTOR2I( 12 * MM, 3 * MM ) );
1568
1569 commit.Revert();
1570}
1571
1572
1573// A fixed point on one shape must not stop its coincident neighbour being dragged by a point
1574// of its own that nothing pins.
1575BOOST_FIXTURE_TEST_CASE( BlockEditStillMovesAFreeNeighborOfAFixedPoint, DRAG_FIXTURE )
1576{
1577 PCB_SHAPE* pinned = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1578 PCB_SHAPE* free = addSegment( { 10 * MM, 0 }, { 20 * MM, 0 } );
1579 addFixedPosition( pinned, CONSTRAINT_ANCHOR::START );
1580 addCoincident( pinned, CONSTRAINT_ANCHOR::END, free, CONSTRAINT_ANCHOR::START );
1581
1583 BOOST_REQUIRE( session.Build( &board, { free } ) );
1584
1585 const VECTOR2I target( 22 * MM, 4 * MM );
1586 BOARD_COMMIT commit( tool );
1587
1588 commit.Modify( free );
1589 free->SetEnd( target );
1590
1591 std::vector<PCB_SHAPE*> moved;
1593 session.Solve( { { CONSTRAINT_MEMBER( free->m_Uuid, CONSTRAINT_ANCHOR::END ), target } }, {},
1594 VECTOR2I( 2 * MM, 4 * MM ), &moved,
1595 [&]( BOARD_ITEM* aItem )
1596 {
1597 commit.Modify( aItem );
1598 } );
1599
1601 BOOST_CHECK_EQUAL( free->GetEnd(), target );
1602 BOOST_CHECK_EQUAL( pinned->GetStart(), VECTOR2I( 0, 0 ) );
1603
1604 commit.Revert();
1605}
1606
1607
1608// A shape that travels whole cannot take a pinned point along, so the gesture is refused
1609// rather than quietly dragging the point off its constraint. SolveRigidTranslation already
1610// refuses the same move for the stock drag.
1611BOOST_FIXTURE_TEST_CASE( BlockEditRefusesCarryingAFixedPoint, DRAG_FIXTURE )
1612{
1613 PCB_SHAPE* seg = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1614 addFixedPosition( seg, CONSTRAINT_ANCHOR::START );
1615
1617 BOOST_REQUIRE( session.Build( &board, { seg } ) );
1618
1619 const VECTOR2I delta( 2 * MM, 3 * MM );
1620 BOARD_COMMIT commit( tool );
1621
1622 commit.Modify( seg );
1623 seg->Move( delta );
1624
1625 std::vector<PCB_SHAPE*> moved;
1626 CONSTRAINT_BLOCK_RESULT result = session.Solve( {}, { seg->m_Uuid }, delta, &moved,
1627 [&]( BOARD_ITEM* aItem )
1628 {
1629 commit.Modify( aItem );
1630 } );
1631
1633
1634 commit.Revert();
1635}
1636
1637
1638// One relation the geometry already fails used to mark the whole cluster conflicted, and a
1639// conflicted cluster was not solved at all, so the relations beside it stopped holding too.
1640// Here the stale horizontal belongs to the second segment and the first one's has to survive.
1641BOOST_FIXTURE_TEST_CASE( BlockEditKeepsASatisfiedRelationBesideAStaleOne, DRAG_FIXTURE )
1642{
1643 PCB_SHAPE* holds = addSegment( { 0, 0 }, { 40 * MM, 0 } );
1644 PCB_SHAPE* stale = addSegment( { 40 * MM, 0 }, { 80 * MM, 10 * MM } );
1645
1646 addCoincident( holds, CONSTRAINT_ANCHOR::END, stale, CONSTRAINT_ANCHOR::START );
1647 addHorizontal( holds );
1648 addHorizontal( stale ); // not satisfied, the segment rises 10 mm
1649
1651 BOOST_REQUIRE( session.Build( &board, { holds, stale } ) );
1652 BOOST_REQUIRE( !session.HasBaseConflict() );
1653
1654 const VECTOR2I target( 40 * MM, 8 * MM );
1655 BOARD_COMMIT commit( tool );
1656
1657 commit.Modify( holds );
1658 holds->SetEnd( target );
1659
1660 std::vector<PCB_SHAPE*> moved;
1662 session.Solve( { { CONSTRAINT_MEMBER( holds->m_Uuid, CONSTRAINT_ANCHOR::END ), target } }, {},
1663 VECTOR2I( 0, 8 * MM ), &moved,
1664 [&]( BOARD_ITEM* aItem )
1665 {
1666 commit.Modify( aItem );
1667 } );
1668
1669 // The cluster is modelled now rather than skipped
1671
1672 // The surviving horizontal held, so the unpicked start came along
1673 BOOST_CHECK_EQUAL( holds->GetStart().y, holds->GetEnd().y );
1674
1675 commit.Revert();
1676}
1677
1678
1679// The stale relation is out of the system, so it must not refuse the drag either.
1680BOOST_FIXTURE_TEST_CASE( BlockEditDoesNotEnforceAStaleRelation, DRAG_FIXTURE )
1681{
1682 PCB_SHAPE* stale = addSegment( { 0, 0 }, { 40 * MM, 10 * MM } );
1683 addHorizontal( stale );
1684
1686 BOOST_REQUIRE( session.Build( &board, { stale } ) );
1687
1688 const VECTOR2I target( 40 * MM, 18 * MM );
1689 BOARD_COMMIT commit( tool );
1690
1691 commit.Modify( stale );
1692 stale->SetEnd( target );
1693
1694 std::vector<PCB_SHAPE*> moved;
1696 session.Solve( { { CONSTRAINT_MEMBER( stale->m_Uuid, CONSTRAINT_ANCHOR::END ), target } }, {},
1697 VECTOR2I( 0, 8 * MM ), &moved,
1698 [&]( BOARD_ITEM* aItem )
1699 {
1700 commit.Modify( aItem );
1701 } );
1702
1704 BOOST_CHECK_EQUAL( stale->GetEnd(), target );
1705
1706 commit.Revert();
1707}
1708
1709
1710// A block drag reshapes the shape whose anchor was picked and carries the rest whole, which
1711// leaves the joint between them broken until the solver tidies up what the drag left free.
1712// Here only the loose end of the reshaped segment can move, and it has to follow the carried one.
1713BOOST_FIXTURE_TEST_CASE( BlockEditRelaxesUncommandedAnchorOfDeformedShape, DRAG_FIXTURE )
1714{
1715 PCB_SHAPE* deformed = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1716 PCB_SHAPE* carried = addSegment( { 10 * MM, 0 }, { 20 * MM, 0 } );
1717 addCoincident( deformed, CONSTRAINT_ANCHOR::END, carried, CONSTRAINT_ANCHOR::START );
1718
1720 BOOST_REQUIRE( session.Build( &board, { deformed, carried } ) );
1721
1722 const VECTOR2I delta( 2 * MM, 3 * MM );
1723 BOARD_COMMIT commit( tool );
1724
1725 // What the drag writes before the solve. The picked anchor sits on its target, the carried
1726 // segment has moved whole, and the unpicked end has not moved
1727 commit.Modify( deformed );
1728 commit.Modify( carried );
1729 deformed->SetStart( deformed->GetStart() + delta );
1730 carried->Move( delta );
1731
1732 std::vector<PCB_SHAPE*> moved;
1734 session.Solve( { { CONSTRAINT_MEMBER( deformed->m_Uuid, CONSTRAINT_ANCHOR::START ), delta } },
1735 { carried->m_Uuid }, delta, &moved,
1736 [&]( BOARD_ITEM* aItem )
1737 {
1738 commit.Modify( aItem );
1739 } );
1740
1742
1743 // Both placed points landed exactly where they were put
1744 BOOST_CHECK_EQUAL( deformed->GetStart(), VECTOR2I( 2 * MM, 3 * MM ) );
1745 BOOST_CHECK_EQUAL( carried->GetStart(), VECTOR2I( 12 * MM, 3 * MM ) );
1746 BOOST_CHECK_EQUAL( carried->GetEnd(), VECTOR2I( 22 * MM, 3 * MM ) );
1747
1748 // The end nobody placed moved to the carried joint instead of staying where the reshape
1749 // left it, so the constraint holds and the reshaped segment ends up moved whole
1750 BOOST_CHECK_LE( ( deformed->GetEnd() - carried->GetStart() ).EuclideanNorm(), 100 );
1751 BOOST_CHECK( deformed->GetEnd() != VECTOR2I( 10 * MM, 0 ) );
1752 BOOST_CHECK( std::find( moved.begin(), moved.end(), deformed ) != moved.end() );
1753
1754 commit.Revert();
1755}
1756
1757
1758// The common case. One picked anchor reshapes a shape whose constrained partner is not in the
1759// block at all, so the solver brings the partner along and reports it for the caller to stage.
1760BOOST_FIXTURE_TEST_CASE( BlockEditPullsAnUnselectedNeighborAlong, DRAG_FIXTURE )
1761{
1762 PCB_SHAPE* deformed = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1763 PCB_SHAPE* neighbor = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
1764 addCoincident( deformed, CONSTRAINT_ANCHOR::END, neighbor, CONSTRAINT_ANCHOR::START );
1765
1767 BOOST_REQUIRE( session.Build( &board, { deformed } ) );
1768
1769 const VECTOR2I target( 13 * MM, 4 * MM );
1770 const VECTOR2I neighborEnd0 = neighbor->GetEnd();
1771 BOARD_COMMIT commit( tool );
1772
1773 commit.Modify( deformed );
1774 deformed->SetEnd( target );
1775
1776 std::vector<PCB_SHAPE*> moved;
1777 std::vector<BOARD_ITEM*> staged;
1779 session.Solve( { { CONSTRAINT_MEMBER( deformed->m_Uuid, CONSTRAINT_ANCHOR::END ), target } }, {},
1780 target - VECTOR2I( 10 * MM, 0 ), &moved,
1781 [&]( BOARD_ITEM* aItem )
1782 {
1783 staged.push_back( aItem );
1784 commit.Modify( aItem );
1785 } );
1786
1788 BOOST_CHECK_EQUAL( deformed->GetEnd(), target );
1789 BOOST_CHECK_LE( ( neighbor->GetStart() - target ).EuclideanNorm(), 100 );
1790
1791 // Only the constrained end moved, because the weak pins hold the far end, and the caller
1792 // heard about the shape before it changed
1793 BOOST_CHECK( neighbor->GetStart() != VECTOR2I( 10 * MM, 0 ) );
1794 BOOST_CHECK_EQUAL( neighbor->GetEnd(), neighborEnd0 );
1795 BOOST_CHECK( std::find( moved.begin(), moved.end(), neighbor ) != moved.end() );
1796 BOOST_CHECK( std::find( staged.begin(), staged.end(), neighbor ) != staged.end() );
1797
1798 commit.Revert();
1799}
1800
1801
1802// Both anchors of one segment picked while its constrained partner travels whole. Everything
1803// moves by the same offset, so the constraint moves with the block and the drag goes through.
1804BOOST_FIXTURE_TEST_CASE( BlockEditCarriesSpannedClusterWhole, DRAG_FIXTURE )
1805{
1806 PCB_SHAPE* picked = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1807 PCB_SHAPE* carried = addSegment( { 10 * MM, 0 }, { 20 * MM, 0 } );
1808 addCoincident( picked, CONSTRAINT_ANCHOR::END, carried, CONSTRAINT_ANCHOR::START );
1809
1811 BOOST_REQUIRE( session.Build( &board, { picked, carried } ) );
1812
1813 const VECTOR2I delta( -4 * MM, 1 * MM );
1814 BOARD_COMMIT commit( tool );
1815
1816 commit.Modify( picked );
1817 commit.Modify( carried );
1818 picked->Move( delta );
1819 carried->Move( delta );
1820
1821 std::vector<PCB_SHAPE*> moved;
1824 { CONSTRAINT_MEMBER( picked->m_Uuid, CONSTRAINT_ANCHOR::END ), VECTOR2I( 10 * MM, 0 ) + delta } },
1825 { carried->m_Uuid }, delta, &moved,
1826 [&]( BOARD_ITEM* aItem )
1827 {
1828 commit.Modify( aItem );
1829 } );
1830
1832 BOOST_CHECK_EQUAL( picked->GetStart(), VECTOR2I( -4 * MM, 1 * MM ) );
1833 BOOST_CHECK_EQUAL( picked->GetEnd(), VECTOR2I( 6 * MM, 1 * MM ) );
1834 BOOST_CHECK_EQUAL( carried->GetStart(), VECTOR2I( 6 * MM, 1 * MM ) );
1835
1836 commit.Revert();
1837}
1838
1839
1840// A placement the constraints forbid is refused and the geometry is left as it was. Here the
1841// carried segment's far end is tied to a locked neighbour the solver cannot move.
1842BOOST_FIXTURE_TEST_CASE( BlockEditRefusesCommandTiedToLockedNeighbor, DRAG_FIXTURE )
1843{
1844 PCB_SHAPE* carried = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1845 PCB_SHAPE* anchored = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
1846 anchored->SetLocked( true );
1847 addCoincident( carried, CONSTRAINT_ANCHOR::END, anchored, CONSTRAINT_ANCHOR::START );
1848
1850 BOOST_REQUIRE( session.Build( &board, { carried } ) );
1851
1852 const VECTOR2I delta( 3 * MM, 0 );
1853 const VECTOR2I anchoredStart0 = anchored->GetStart();
1854 BOARD_COMMIT commit( tool );
1855
1856 commit.Modify( carried );
1857 carried->Move( delta );
1858
1859 std::vector<PCB_SHAPE*> moved;
1860 CONSTRAINT_BLOCK_RESULT result = session.Solve( {}, { carried->m_Uuid }, delta, &moved,
1861 [&]( BOARD_ITEM* aItem )
1862 {
1863 commit.Modify( aItem );
1864 } );
1865
1867 BOOST_CHECK( moved.empty() );
1868
1869 // The solve wrote nothing, so the locked neighbour is untouched and the carried geometry is
1870 // still exactly what the drag put on the board for the caller to undo
1871 BOOST_CHECK_EQUAL( anchored->GetStart(), anchoredStart0 );
1872 BOOST_CHECK_EQUAL( carried->GetStart(), VECTOR2I( 3 * MM, 0 ) );
1873
1874 commit.Revert();
1875}
1876
1877
1878// The solver normally freezes a locked shape. With the lock override the drag carries it
1879// anyway, so its placed coordinates stand and the free neighbour follows them.
1880BOOST_FIXTURE_TEST_CASE( BlockEditCarriesALockedShapeUnderTheOverride, DRAG_FIXTURE )
1881{
1882 PCB_SHAPE* locked = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1883 PCB_SHAPE* neighbor = addSegment( { 10 * MM, 0 }, { 10 * MM, 10 * MM } );
1884 locked->SetLocked( true );
1885 addCoincident( locked, CONSTRAINT_ANCHOR::END, neighbor, CONSTRAINT_ANCHOR::START );
1886
1888 BOOST_REQUIRE( session.Build( &board, { locked } ) );
1889
1890 const VECTOR2I delta( 3 * MM, 0 );
1891 BOARD_COMMIT commit( tool );
1892
1893 commit.Modify( locked );
1894 locked->Move( delta );
1895
1896 std::vector<PCB_SHAPE*> moved;
1897 CONSTRAINT_BLOCK_RESULT result = session.Solve( {}, { locked->m_Uuid }, delta, &moved,
1898 [&]( BOARD_ITEM* aItem )
1899 {
1900 commit.Modify( aItem );
1901 } );
1902
1904 BOOST_CHECK_EQUAL( locked->GetEnd(), VECTOR2I( 13 * MM, 0 ) );
1905 BOOST_CHECK_LE( ( neighbor->GetStart() - locked->GetEnd() ).EuclideanNorm(), 100 );
1906
1907 commit.Revert();
1908}
1909
1910
1911// A dimension has no pickable handle, so a block carries it whole, and the solver freezes the
1912// dimension point no constraint binds. The carried offset still has to apply to both points, so
1913// the frame goes through and no point is left behind.
1914BOOST_FIXTURE_TEST_CASE( BlockEditCarriesABoundDimension, DRAG_FIXTURE )
1915{
1916 PCB_SHAPE* deformed = addSegment( { 0, 0 }, { 10 * MM, 0 } );
1917
1919 dim->SetStart( { 10 * MM, 0 } );
1920 dim->SetEnd( { 10 * MM, 5 * MM } );
1921 dim->Update();
1922 board.Add( dim );
1923
1925 joint->AddMember( deformed->m_Uuid, CONSTRAINT_ANCHOR::END );
1926 joint->AddMember( dim->m_Uuid, CONSTRAINT_ANCHOR::START );
1927 board.Add( joint );
1928
1930 BOOST_REQUIRE( session.Build( &board, { deformed } ) );
1931
1932 const VECTOR2I delta( 2 * MM, 1 * MM );
1933 BOARD_COMMIT commit( tool );
1934
1935 commit.Modify( deformed );
1936 commit.Modify( dim );
1937 deformed->SetEnd( VECTOR2I( 10 * MM, 0 ) + delta );
1938 dim->Move( delta );
1939
1941 { { CONSTRAINT_MEMBER( deformed->m_Uuid, CONSTRAINT_ANCHOR::END ), VECTOR2I( 10 * MM, 0 ) + delta } },
1942 { dim->m_Uuid }, delta, nullptr,
1943 [&]( BOARD_ITEM* aItem )
1944 {
1945 commit.Modify( aItem );
1946 } );
1947
1949 BOOST_CHECK_EQUAL( dim->GetStart(), VECTOR2I( 12 * MM, 1 * MM ) );
1950 BOOST_CHECK_EQUAL( dim->GetEnd(), VECTOR2I( 12 * MM, 6 * MM ) );
1951
1952 commit.Revert();
1953}
1954
1955
1956// One picked polygon corner. The constrained corner follows the carried segment, the picked one
1957// lands exactly where it was put, and the corners nothing binds barely move.
1958BOOST_FIXTURE_TEST_CASE( BlockEditRelaxesOnlyBoundPolygonVertices, DRAG_FIXTURE )
1959{
1960 const std::vector<VECTOR2I> points{ { 0, 0 }, { 10 * MM, 0 }, { 10 * MM, 10 * MM }, { 0, 10 * MM } };
1961
1962 PCB_SHAPE* poly = addPoly( points );
1963 PCB_SHAPE* carried = addSegment( { 0, 0 }, { 0, -10 * MM } );
1964
1966 joint->AddMember( poly->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 0 );
1967 joint->AddMember( carried->m_Uuid, CONSTRAINT_ANCHOR::START );
1968 board.Add( joint );
1969
1971 BOOST_REQUIRE( session.Build( &board, { poly, carried } ) );
1972
1973 const VECTOR2I delta( 2 * MM, 0 );
1974 BOARD_COMMIT commit( tool );
1975
1976 commit.Modify( poly );
1977 commit.Modify( carried );
1978
1979 std::vector<VECTOR2I> deformed = points;
1980 deformed[2] += delta;
1981 poly->SetPolyPoints( deformed );
1982 carried->Move( delta );
1983
1984 std::vector<PCB_SHAPE*> moved;
1986 session.Solve( { { CONSTRAINT_MEMBER( poly->m_Uuid, CONSTRAINT_ANCHOR::VERTEX, 2 ), points[2] + delta } },
1987 { carried->m_Uuid }, delta, &moved,
1988 [&]( BOARD_ITEM* aItem )
1989 {
1990 commit.Modify( aItem );
1991 } );
1992
1994
1995 const SHAPE_LINE_CHAIN& outline = poly->GetPolyShape().COutline( 0 );
1996 BOOST_REQUIRE_EQUAL( outline.PointCount(), 4 );
1997
1998 BOOST_CHECK_EQUAL( outline.CPoint( 2 ), VECTOR2I( 12 * MM, 10 * MM ) );
1999 BOOST_CHECK_LE( ( outline.CPoint( 0 ) - carried->GetStart() ).EuclideanNorm(), 100 );
2000 BOOST_CHECK_LE( ( outline.CPoint( 0 ) - VECTOR2I( 2 * MM, 0 ) ).EuclideanNorm(), 100 );
2001
2002 for( int i : { 1, 3 } )
2003 BOOST_CHECK_LE( ( outline.CPoint( i ) - points[i] ).EuclideanNorm(), 100 );
2004
2005 commit.Revert();
2006}
2007
2008
2009// The session solves every frame from the geometry it was built on, so a cluster with room to
2010// move ends up the same whatever path the cursor took to get there.
2011BOOST_FIXTURE_TEST_CASE( BlockEditResultDoesNotDependOnThePath, DRAG_FIXTURE )
2012{
2013 const std::vector<VECTOR2I> path{ { 0, 2 * MM }, { 0, 5 * MM } };
2014
2015 const auto run = [&]( const std::vector<VECTOR2I>& aDeltas ) -> std::pair<VECTOR2I, VECTOR2I>
2016 {
2017 BOARD runBoard;
2018 PCB_SHAPE* deformed = new PCB_SHAPE( &runBoard, SHAPE_T::SEGMENT );
2019 deformed->SetStart( { 0, 0 } );
2020 deformed->SetEnd( { 10 * MM, 0 } );
2021 runBoard.Add( deformed );
2022
2023 PCB_SHAPE* neighbor = new PCB_SHAPE( &runBoard, SHAPE_T::SEGMENT );
2024 neighbor->SetStart( { 20 * MM, 0 } );
2025 neighbor->SetEnd( { 20 * MM, 10 * MM } );
2026 runBoard.Add( neighbor );
2027
2029 square->AddMember( deformed->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
2030 square->AddMember( neighbor->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
2031 runBoard.Add( square );
2032
2034 BOOST_REQUIRE( session.Build( &runBoard, { deformed } ) );
2035
2036 for( const VECTOR2I& delta : aDeltas )
2037 {
2038 deformed->SetEnd( VECTOR2I( 10 * MM, 0 ) + delta );
2039
2041 session.Solve( { { CONSTRAINT_MEMBER( deformed->m_Uuid, CONSTRAINT_ANCHOR::END ),
2042 VECTOR2I( 10 * MM, 0 ) + delta } },
2043 {}, delta, nullptr, {} );
2044
2046 }
2047
2048 return { neighbor->GetStart(), neighbor->GetEnd() };
2049 };
2050
2051 const std::pair<VECTOR2I, VECTOR2I> wandered = run( path );
2052 const std::pair<VECTOR2I, VECTOR2I> direct = run( { path.back() } );
2053
2054 BOOST_CHECK_EQUAL( wandered.first, direct.first );
2055 BOOST_CHECK_EQUAL( wandered.second, direct.second );
2056}
2057
2058
2059// A relation that was already broken is left alone. The block drag did not break it, so the
2060// drag neither repairs it nor is refused over it, and the neighbour it named stays put. The
2061// relation is dropped at build time, so the cluster itself is no longer counted as conflicted.
2062BOOST_FIXTURE_TEST_CASE( BlockEditLeavesAPreBrokenClusterAlone, DRAG_FIXTURE )
2063{
2064 PCB_SHAPE* carried = addSegment( { 0, 0 }, { 10 * MM, 0 } );
2065 PCB_SHAPE* neighbor = addSegment( { 12 * MM, 5 * MM }, { 20 * MM, 5 * MM } );
2066 addCoincident( carried, CONSTRAINT_ANCHOR::END, neighbor, CONSTRAINT_ANCHOR::START );
2067
2069 BOOST_REQUIRE( session.Build( &board, { carried } ) );
2070 BOOST_REQUIRE( !session.HasBaseConflict() );
2071
2072 const VECTOR2I neighborStart0 = neighbor->GetStart();
2073
2074 std::vector<PCB_SHAPE*> moved;
2075 CONSTRAINT_BLOCK_RESULT result = session.Solve( {}, { carried->m_Uuid }, VECTOR2I( 3 * MM, 0 ), &moved, {} );
2076
2078 BOOST_CHECK( std::find( moved.begin(), moved.end(), neighbor ) == moved.end() );
2079 BOOST_CHECK_EQUAL( neighbor->GetStart(), neighborStart0 );
2080}
2081
2082
2083// A block that touches a cluster but places none of its coordinates solves nothing and writes
2084// nothing, so picking a handle the solver cannot model still lets the drag run.
2085BOOST_FIXTURE_TEST_CASE( BlockEditWithNoModelledCommandWritesNothing, DRAG_FIXTURE )
2086{
2087 PCB_SHAPE* a = addSegment( { 0, 0 }, { 10 * MM, 0 } );
2088 PCB_SHAPE* b = addSegment( { 10 * MM, 0 }, { 20 * MM, 0 } );
2089 PCB_CONSTRAINT* joint = addCoincident( a, CONSTRAINT_ANCHOR::END, b, CONSTRAINT_ANCHOR::START );
2090
2092 BOOST_REQUIRE( adapter.Build( { a, b }, { joint } ) );
2093
2094 const VECTOR2I aEnd0 = a->GetEnd();
2095 const VECTOR2I bStart0 = b->GetStart();
2096
2097 // A centre anchor on a segment maps to no parameter, so the cluster gets nothing to solve
2098 // even though the block carried one of its shapes
2100 { { CONSTRAINT_MEMBER( a->m_Uuid, CONSTRAINT_ANCHOR::CENTER ), VECTOR2I( 5 * MM, 5 * MM ) } }, {},
2101 VECTOR2I( 0, 0 ) );
2102
2104 BOOST_CHECK( adapter.Apply().empty() );
2105 BOOST_CHECK_EQUAL( a->GetEnd(), aEnd0 );
2106 BOOST_CHECK_EQUAL( b->GetStart(), bStart0 );
2107}
2108
2109
2110// The drag reads each pin's target from the shape through ConstraintShapeAnchors, so the index
2111// that enumeration hands back has to be the index the solver's parameter mapping uses. A
2112// rectangle's four corners share two stored points and a polygon's corners are outline indices,
2113// so a mismatch here would move a different corner than the one the user dragged.
2114BOOST_AUTO_TEST_CASE( BlockEditHonoursEveryEnumeratedAnchor )
2115{
2116 const std::vector<VECTOR2I> polyPoints{ { 0, 0 }, { 10 * MM, 0 }, { 13 * MM, 8 * MM }, { -3 * MM, 8 * MM } };
2117 const VECTOR2I nudge( 3 * MM, 2 * MM );
2118
2119 const auto makeProbe = [&]( bool aRectangle, std::unique_ptr<BOARD>& aBoard ) -> PCB_SHAPE*
2120 {
2121 aBoard = std::make_unique<BOARD>();
2122
2123 PCB_SHAPE* shape = new PCB_SHAPE( aBoard.get(), aRectangle ? SHAPE_T::RECTANGLE : SHAPE_T::POLY );
2124
2125 if( aRectangle )
2126 {
2127 shape->SetStart( { 0, 0 } );
2128 shape->SetEnd( { 10 * MM, 8 * MM } );
2129 }
2130 else
2131 {
2132 shape->SetPolyPoints( polyPoints );
2133 }
2134
2135 aBoard->Add( shape );
2136 return shape;
2137 };
2138
2139 for( bool rectangle : { true, false } )
2140 {
2141 std::unique_ptr<BOARD> sizingBoard;
2142 const size_t anchorCount = ConstraintShapeAnchors( makeProbe( rectangle, sizingBoard ) ).size();
2143
2144 BOOST_REQUIRE_EQUAL( anchorCount, 4 );
2145
2146 for( size_t ii = 0; ii < anchorCount; ++ii )
2147 {
2148 std::unique_ptr<BOARD> probeBoard;
2149 PCB_SHAPE* probe = makeProbe( rectangle, probeBoard );
2150
2151 std::vector<CONSTRAINT_ANCHOR_POINT> before = ConstraintShapeAnchors( probe );
2152 BOOST_REQUIRE_EQUAL( before.size(), anchorCount );
2153
2154 const VECTOR2I target = before[ii].pos + nudge;
2155
2157 BOOST_REQUIRE( adapter.Build( { probe }, {} ) );
2158
2160 { { CONSTRAINT_MEMBER( probe->m_Uuid, before[ii].anchor, before[ii].index ), target } }, {},
2161 VECTOR2I( 0, 0 ) );
2162
2164 adapter.Apply();
2165
2166 std::vector<CONSTRAINT_ANCHOR_POINT> after = ConstraintShapeAnchors( probe );
2167 BOOST_REQUIRE_EQUAL( after.size(), anchorCount );
2168
2169 // The anchor that was placed is the one that landed on the target
2170 BOOST_CHECK_EQUAL( after[ii].pos, target );
2171
2172 for( size_t jj = 0; jj < anchorCount; ++jj )
2173 {
2174 if( jj != ii && rectangle )
2175 continue; // a rect corner shares each of its coordinates with a neighbour
2176
2177 if( jj != ii )
2178 BOOST_CHECK_EQUAL( after[jj].pos, before[jj].pos );
2179 }
2180 }
2181 }
2182}
2183
2184
static double square(double x)
CONSTRAINT_DIAGNOSIS SolveCluster(BOARD *aBoard, const CONSTRAINT_MEMBER &aDragged, const VECTOR2I &aCursor, std::vector< PCB_SHAPE * > *aModified, const std::function< void(BOARD_ITEM *)> &aBeforeModify, bool aIncludeDragged, bool aStabilize, const std::set< KIID > &aEdited, const std::optional< std::pair< CONSTRAINT_MEMBER, VECTOR2I > > &aCoDragged, const std::set< KIID > &aFixedShapes, bool aHoldDraggedRigid)
Gather the cluster of shapes transitively constrained with the dragged shape, solve with the dragged ...
void ReSolveShapeClusters(BOARD *aBoard, const std::vector< PCB_SHAPE * > &aShapes, std::vector< PCB_SHAPE * > *aModified, const std::function< void(BOARD_ITEM *)> &aBeforeModify)
Re-solve the clusters of shapes edited outside the solver, e.g.
CONSTRAINT_BLOCK_RESULT
How a block edit solve turned out.
@ REFUSED
No arrangement fits the pinned points, and the geometry is untouched.
@ SOLVED
The constraints hold and every pinned point is exactly where it was put.
@ NOT_MODELED
Nothing the caller pinned belongs to this cluster.
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
void Revert() override
Revert the commit by restoring the modified items state.
Translates KiCad board geometry to and from the planegcs solver (issue #2329).
bool SolveRigidTranslation(const std::set< KIID > &aEditedShapes, const VECTOR2I &aTranslation)
std::vector< PCB_SHAPE * > Apply(const std::function< void(BOARD_ITEM *)> &aBeforeWrite={})
Write the solved coordinates back into the shapes, de-normalized to IU.
bool Build(const std::vector< PCB_SHAPE * > &aShapes, const std::vector< PCB_CONSTRAINT * > &aConstraints, const std::set< KIID > *aFixedShapes=nullptr, const std::vector< PCB_DIMENSION_BASE * > &aDimensions={}, const std::vector< PAD * > &aPads={})
Translate a cluster into a planegcs system.
CONSTRAINT_BLOCK_RESULT SolveBlockEdit(const std::vector< std::pair< CONSTRAINT_MEMBER, VECTOR2I > > &aPinnedAnchors, const std::set< KIID > &aTranslatedShapes, const VECTOR2I &aTranslation)
Solve a block edit whose geometry the caller has already written.
One block edit session, holding several anchors and one travel offset at once.
bool Build(BOARD *aBoard, const std::vector< BOARD_ITEM * > &aBlockItems)
Build one adapter over the clusters holding aBlockItems.
CONSTRAINT_BLOCK_RESULT Solve(const std::vector< std::pair< CONSTRAINT_MEMBER, VECTOR2I > > &aPinnedAnchors, const std::set< KIID > &aTranslatedShapes, const VECTOR2I &aTranslation, std::vector< PCB_SHAPE * > *aModified, const std::function< void(BOARD_ITEM *)> &aBeforeModify)
Solve one frame of the block edit and apply it.
bool IsExactFeasible(const VECTOR2I &aTarget)
bool Build(BOARD *aBoard, const CONSTRAINT_MEMBER &aDragged)
CONSTRAINT_DIAGNOSIS Solve(const VECTOR2I &aCursor, std::vector< PCB_SHAPE * > *aModified, const std::function< void(BOARD_ITEM *)> &aBeforeModify, bool aIncludeDragged, bool aStabilize, const std::set< KIID > &aEdited={}, const std::optional< std::pair< CONSTRAINT_MEMBER, VECTOR2I > > &aCoDragged=std::nullopt)
SNAP_RESULT ResolveCandidates(const SNAP_SOURCE_CONTEXT &aContext, const std::vector< SNAP_CANDIDATE > &aCandidates)
bool Matches(const CONSTRAINT_MEMBER &aDragged) const
bool Solve(const VECTOR2I &aTarget, std::vector< PCB_SHAPE * > *aModified, const std::function< void(BOARD_ITEM *)> &aBeforeModify)
bool Build(BOARD *aBoard, const std::vector< PCB_SHAPE * > &aEditedShapes, const VECTOR2I &aReference)
SNAP_RESULT ResolveCandidates(const SNAP_SOURCE_CONTEXT &aContext, const std::vector< SNAP_CANDIDATE > &aCandidates)
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
void SetLocked(bool aLocked) override
Definition board_item.h:441
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1548
const CONSTRAINTS & Constraints() const
Geometric constraints (#2329) owned by this board.
Definition board.h:522
BOARD_ITEM * ResolveItem(const KIID &aID, bool aAllowNullptrReturn=false) const
Definition board.cpp:2176
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition commit.h:86
bool Empty() const
Definition commit.h:145
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
const KIID m_Uuid
Definition eda_item.h:599
SHAPE_POLY_SET & GetPolyShape()
int GetRadius() const
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:334
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:284
void SetPolyPoints(const std::vector< VECTOR2I > &aPoints)
Definition kiid.h:46
std::array< uint8_t, 16 > AsBytes() const
Definition kiid.cpp:276
static KIID FromName(const std::string &aName)
Return a KIID derived from a name, the same name always gives the same KIID.
Definition kiid.cpp:237
A geometric constraint between board items (issue #2329).
const std::vector< CONSTRAINT_MEMBER > & GetMembers() const
void AddMember(const KIID &aItem, CONSTRAINT_ANCHOR aAnchor=CONSTRAINT_ANCHOR::WHOLE, int aIndex=-1)
void SetValue(std::optional< double > aValue)
void SetDriving(bool aDriving)
For better understanding of the points that make a dimension:
void SetCenter(const VECTOR2I &aCenter) override
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pcb_shape.h:80
void SetEnd(const VECTOR2I &aEnd) override
void Move(const VECTOR2I &aMoveVector) override
Move this object.
void SetStart(const VECTOR2I &aStart) override
void SetArcGeometry(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd) override
Set the three controlling points for an arc.
void SetRadius(int aRadius)
void SetCenter(const VECTOR2I &aCenter)
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
size_t ArcCount() const
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
SNAP_RESULT Resolve(const SNAP_SOURCE_CONTEXT &aContext) const
void SetFeasibilityCallback(FEASIBILITY_CALLBACK aCallback)
void SetClock(CLOCK_CALLBACK aClock)
void AddCandidate(SNAP_CANDIDATE aCandidate)
std::optional< CONSTRAINT_ANCHOR_POINT > ConstraintShapeVertex(const PCB_SHAPE *aShape, int aIndex)
VERTEX anchor at ordinal aIndex of a rectangle or eligible polygon or std::nullopt if the shape has n...
std::vector< CONSTRAINT_ANCHOR_POINT > ConstraintShapeAnchors(const PCB_SHAPE *aShape)
Enumerate a shape constraint anchors with positions segment and arc endpoints arc centre circle centr...
@ ELLIPSE
Definition eda_shape.h:63
@ SEGMENT
Definition eda_shape.h:57
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:58
PCB_SHAPE * addPoly(BOARD &aBoard, const std::vector< VECTOR2I > &aPoints)
PCB_SHAPE * addArc(BOARD &aBoard, const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
PCB_SHAPE * addCircle(BOARD &aBoard, const VECTOR2I &aCenter, int aRadius)
PCB_SHAPE * addRect(BOARD &aBoard, const VECTOR2I &aStart, const VECTOR2I &aEnd)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
CONSTRAINT_ANCHOR
Which feature of a referenced board item participates in a constraint.
@ VERTEX
An indexed rectangle corner or polygon outline vertex; pairs with CONSTRAINT_MEMBER::m_index.
@ WHOLE
The item as a whole (a segment as a line, a circle).
@ START
First endpoint of a segment or arc.
@ END
Second endpoint of a segment or arc.
@ CENTER
Center of an arc or circle.
@ CONCENTRIC
Two arcs/circles share a center.
@ FIXED_POSITION
A point is locked at its current location.
@ COINCIDENT
Two points are made to coincide.
@ PERPENDICULAR
Two segments are perpendicular.
@ FIXED_RADIUS
An arc/circle has a driving radius value.
@ HORIZONTAL
A segment (or two points) is horizontal.
@ POINT_ON_LINE
A point lies on a segment's supporting line.
@ FIXED_LENGTH
A segment has a driving length value.
static bool addSegment(VRML_LAYER &model, IDF_SEGMENT *seg, int icont, int iseg)
SNAP_ID_KIND
std::array< uint8_t, 16 > SNAP_TARGET_ID
The outcome of a constraint solve, in plain data so callers need not know planegcs.
bool solved
Solver reached Success or Converged.
std::vector< KIID > conflicting
Constraints the solver reports as over-constraining.
One participant in a constraint: a referenced board item plus the feature of that item that participa...
static SNAP_CANDIDATE Point(SNAP_STABLE_ID aId, SNAP_PRIORITY_TIER aPriority, SNAP_CANDIDATE_SUBTYPE aSubtype, const VECTOR2I &aPoint, double aResidual)
static SNAP_CANDIDATE Line(SNAP_STABLE_ID aId, SNAP_PRIORITY_TIER aPriority, SNAP_CANDIDATE_SUBTYPE aSubtype, const VECTOR2I &aOrigin, const VECTOR2D &aDirection, double aResidual)
static SNAP_CANDIDATE AxisX(SNAP_STABLE_ID aId, SNAP_PRIORITY_TIER aPriority, SNAP_CANDIDATE_SUBTYPE aSubtype, int aCoordinate, double aResidual)
std::vector< SNAP_GUIDE > guides
SNAP_RELATION relation
SNAP_RESULT_STATUS status
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_FIXTURE_TEST_CASE(DragReDerivesNeighborRevertRestores, DRAG_FIXTURE)
BOOST_AUTO_TEST_CASE(UnbuiltMoveSessionRejectsCandidates)
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)
BOOST_CHECK_EQUAL_COLLECTIONS(mixed.begin(), mixed.end(), expMixed.begin(), expMixed.end())
BOOST_AUTO_TEST_SUITE_END()
bool moved
std::string path
static const long long MM
const SHAPE_LINE_CHAIN chain
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
wxString result
Test unit parsing edge cases and error handling.
int delta
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:94
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
static PIECE rect(double aLeft, double aTop, double aRight, double aBottom, bool aMixed=false)