KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_constraint_combinations.cpp
Go to the documentation of this file.
1/*
2 * A matrix of constraint combinations: each satisfiable case must diagnose clean, each contradiction
3 * must be flagged. Guards the over-constrained detection against regressions across many shapes.
4 */
5
6#include <algorithm>
7#include <cmath>
8#include <functional>
9#include <limits>
10#include <vector>
11
13
14#include <board.h>
15#include <pcb_shape.h>
18#include <core/mirror.h>
19#include <geometry/eda_angle.h>
20#include <geometry/seg.h>
21
23
24using namespace KI_TEST;
25
26namespace
27{
28void author( BOARD& aBoard, PCB_CONSTRAINT_TYPE aType, const std::vector<CONSTRAINT_MEMBER>& aMembers,
29 std::optional<double> aValue = std::nullopt )
30{
31 PCB_CONSTRAINT* c = addConstraint( aBoard, aType, aMembers, aValue );
32 std::vector<PCB_SHAPE*> mod;
33 ApplyConstraintImmediately( &aBoard, c, &mod,
34 []( BOARD_ITEM* )
35 {
36 } );
37}
38
40{
41 return { s->m_Uuid, CONSTRAINT_ANCHOR::WHOLE };
42}
43
44// Each scenario runs in this helper's frame, so only one BOARD is on the stack at a time.
45void run( const char* aName, bool aExpectConflict, const std::function<void( BOARD& )>& aBuild )
46{
47 BOARD board;
48 aBuild( board );
49
51
52 BOOST_CHECK_MESSAGE( ( !d.conflicting.empty() ) == aExpectConflict,
53 aName << ": expected " << ( aExpectConflict ? "conflict" : "clean" )
54 << " but conflicting=" << d.conflicting.size() );
55}
56} // namespace
57
58BOOST_AUTO_TEST_SUITE( ConstraintCombinations )
59
61{
62 using T = PCB_CONSTRAINT_TYPE;
63
64 // ---- Satisfiable (expect CLEAN) ----
65 run( "parallel(2 seg)", false,
66 []( BOARD& b )
67 {
68 auto* s1 = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
69 auto* s2 = addSegment( b, { 0, 5 * MM }, { 8 * MM, 3 * MM } );
70 author( b, T::PARALLEL, { whole( s1 ), whole( s2 ) } );
71 } );
72 run( "perpendicular(2 seg)", false,
73 []( BOARD& b )
74 {
75 auto* s1 = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
76 auto* s2 = addSegment( b, { 0, 5 * MM }, { 8 * MM, 3 * MM } );
77 author( b, T::PERPENDICULAR, { whole( s1 ), whole( s2 ) } );
78 } );
79 run( "horizontal(1 seg)", false,
80 []( BOARD& b )
81 {
82 auto* s = addSegment( b, { 0, 0 }, { 10 * MM, 2 * MM } );
83 author( b, T::HORIZONTAL, { whole( s ) } );
84 } );
85 run( "vertical(1 seg)", false,
86 []( BOARD& b )
87 {
88 auto* s = addSegment( b, { 0, 0 }, { 2 * MM, 10 * MM } );
89 author( b, T::VERTICAL, { whole( s ) } );
90 } );
91 run( "collinear(2 seg)", false,
92 []( BOARD& b )
93 {
94 auto* s1 = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
95 auto* s2 = addSegment( b, { 12 * MM, 1 * MM }, { 20 * MM, 2 * MM } );
96 author( b, T::COLLINEAR, { whole( s1 ), whole( s2 ) } );
97 } );
98 run( "equal_length(2 seg)", false,
99 []( BOARD& b )
100 {
101 auto* s1 = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
102 auto* s2 = addSegment( b, { 0, 5 * MM }, { 4 * MM, 5 * MM } );
103 author( b, T::EQUAL_LENGTH, { whole( s1 ), whole( s2 ) } );
104 } );
105 run( "fixed_length(1 seg)", false,
106 []( BOARD& b )
107 {
108 auto* s = addSegment( b, { 0, 0 }, { 4 * MM, 0 } );
109 author( b, T::FIXED_LENGTH, { whole( s ) }, 10.0 * MM );
110 } );
111 run( "angular(2 seg)", false,
112 []( BOARD& b )
113 {
114 auto* s1 = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
115 auto* s2 = addSegment( b, { 0, 0 }, { 0, 10 * MM } );
116 author( b, T::ANGULAR_DIMENSION, { whole( s1 ), whole( s2 ) }, 90.0 );
117 } );
118 run( "concentric(2 circ)", false,
119 []( BOARD& b )
120 {
121 auto* c1 = addCircle( b, { 0, 0 }, 5 * MM );
122 auto* c2 = addCircle( b, { 2 * MM, 1 * MM }, 3 * MM );
123 author( b, T::CONCENTRIC, { whole( c1 ), whole( c2 ) } );
124 } );
125 run( "equal_radius(2 circ)", false,
126 []( BOARD& b )
127 {
128 auto* c1 = addCircle( b, { 0, 0 }, 5 * MM );
129 auto* c2 = addCircle( b, { 20 * MM, 0 }, 3 * MM );
130 author( b, T::EQUAL_RADIUS, { whole( c1 ), whole( c2 ) } );
131 } );
132 run( "fixed_radius(1 circ)", false,
133 []( BOARD& b )
134 {
135 auto* c = addCircle( b, { 0, 0 }, 5 * MM );
136 author( b, T::FIXED_RADIUS, { whole( c ) }, 8.0 * MM );
137 } );
138 run( "tangent(line,circ)", false,
139 []( BOARD& b )
140 {
141 auto* s = addSegment( b, { 0, 0 }, { 20 * MM, 0 } );
142 auto* c = addCircle( b, { 10 * MM, 8 * MM }, 3 * MM );
143 author( b, T::TANGENT, { whole( s ), whole( c ) } );
144 } );
145 run( "tangent(2 circ)", false,
146 []( BOARD& b )
147 {
148 auto* c1 = addCircle( b, { 0, 0 }, 5 * MM );
149 auto* c2 = addCircle( b, { 15 * MM, 0 }, 4 * MM );
150 author( b, T::TANGENT, { whole( c1 ), whole( c2 ) } );
151 } );
152
153 // ---- Contradictions (expect CONFLICT) ----
154 run( "parallel+perpendicular", true,
155 []( BOARD& b )
156 {
157 auto* s1 = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
158 auto* s2 = addSegment( b, { 0, 5 * MM }, { 8 * MM, 3 * MM } );
159 author( b, T::PARALLEL, { whole( s1 ), whole( s2 ) } );
160 author( b, T::PERPENDICULAR, { whole( s1 ), whole( s2 ) } );
161 } );
162 run( "horizontal+vertical", true,
163 []( BOARD& b )
164 {
165 auto* s = addSegment( b, { 0, 0 }, { 10 * MM, 2 * MM } );
166 author( b, T::HORIZONTAL, { whole( s ) } );
167 author( b, T::VERTICAL, { whole( s ) } );
168 } );
169 run( "collinear+perpendicular", true,
170 []( BOARD& b )
171 {
172 auto* s1 = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
173 auto* s2 = addSegment( b, { 12 * MM, 1 * MM }, { 20 * MM, 2 * MM } );
174 author( b, T::COLLINEAR, { whole( s1 ), whole( s2 ) } );
175 author( b, T::PERPENDICULAR, { whole( s1 ), whole( s2 ) } );
176 } );
177 run( "equal_radius+two fixed_radius", true,
178 []( BOARD& b )
179 {
180 auto* c1 = addCircle( b, { 0, 0 }, 5 * MM );
181 auto* c2 = addCircle( b, { 20 * MM, 0 }, 3 * MM );
182 author( b, T::EQUAL_RADIUS, { whole( c1 ), whole( c2 ) } );
183 author( b, T::FIXED_RADIUS, { whole( c1 ) }, 5.0 * MM );
184 author( b, T::FIXED_RADIUS, { whole( c2 ) }, 9.0 * MM );
185 } );
186
187 // ---- Redundant (expect CLEAN, redundant>0) ----
188 run( "horiz+horiz+parallel(redundant)", false,
189 []( BOARD& b )
190 {
191 auto* s1 = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
192 auto* s2 = addSegment( b, { 0, 5 * MM }, { 10 * MM, 5 * MM } );
193 author( b, T::HORIZONTAL, { whole( s1 ) } );
194 author( b, T::HORIZONTAL, { whole( s2 ) } );
195 author( b, T::PARALLEL, { whole( s1 ), whole( s2 ) } );
196 } );
197
198 // ---- Locked variants ----
199 run( "perpendicular(locked+free)", false,
200 []( BOARD& b )
201 {
202 auto* lk = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
203 auto* fr = addSegment( b, { 0, 5 * MM }, { 8 * MM, 3 * MM } );
204 lk->SetLocked( true );
205 author( b, T::PERPENDICULAR, { whole( lk ), whole( fr ) } );
206 } );
207 run( "parallel+perp(locked+free)", true,
208 []( BOARD& b )
209 {
210 auto* lk = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
211 auto* fr = addSegment( b, { 0, 5 * MM }, { 8 * MM, 3 * MM } );
212 lk->SetLocked( true );
213 author( b, T::PARALLEL, { whole( lk ), whole( fr ) } );
214 author( b, T::PERPENDICULAR, { whole( lk ), whole( fr ) } );
215 } );
216}
217
218// Flipping a shape breaks its constraint, and the transform re-solve must restore it. Here a circle
219// tangent to a line is flipped away, then re-solved back to tangent.
220BOOST_AUTO_TEST_CASE( FlipRestoresTangent )
221{
222 BOARD b;
223 auto* line = addSegment( b, { 0, 0 }, { 20 * MM, 0 } );
224 auto* circ = addCircle( b, { 10 * MM, 5 * MM }, 5 * MM );
225 author( b, PCB_CONSTRAINT_TYPE::TANGENT, { whole( line ), whole( circ ) } );
226
227 auto tangentErr = [&]()
228 {
229 SEG seg( line->GetStart(), line->GetEnd() );
230 return std::abs( (double) seg.LineDistance( circ->GetCenter() ) - circ->GetRadius() );
231 };
232
233 BOOST_CHECK_LE( tangentErr(), 5000.0 ); // authored: tangent
234
235 circ->Flip( { 10 * MM, 30 * MM }, FLIP_DIRECTION::TOP_BOTTOM );
236 BOOST_CHECK_GT( tangentErr(), 1 * MM ); // flip broke tangency
237
238 std::vector<PCB_SHAPE*> shapes{ circ }, mod;
239 ReSolveShapeClusters( &b, shapes, &mod,
240 []( BOARD_ITEM* )
241 {
242 } );
243
244 BOOST_CHECK_LE( tangentErr(), 5000.0 ); // re-solve restored tangency
245 BOOST_CHECK( DiagnoseBoardConstraints( &b ).conflicting.empty() );
246}
247
248// Rotating a shape breaks its constraint, and the transform re-solve must restore it. Here one of
249// two parallel segments is rotated away, then re-solved back to parallel.
250BOOST_AUTO_TEST_CASE( RotateRestoresParallel )
251{
252 BOARD b;
253 auto* a = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
254 auto* c = addSegment( b, { 0, 5 * MM }, { 10 * MM, 5 * MM } );
255 author( b, PCB_CONSTRAINT_TYPE::PARALLEL, { whole( a ), whole( c ) } );
256
257 // |sin(angle between the two segment directions)|: zero when parallel.
258 auto parallelErr = [&]()
259 {
260 VECTOR2I da = a->GetEnd() - a->GetStart();
261 VECTOR2I dc = c->GetEnd() - c->GetStart();
262 double cross = (double) da.x * dc.y - (double) da.y * dc.x;
263 return std::abs( cross / ( da.EuclideanNorm() * dc.EuclideanNorm() ) );
264 };
265
266 BOOST_CHECK_LE( parallelErr(), 1e-3 ); // authored: parallel
267
268 c->Rotate( c->GetStart(), EDA_ANGLE( 30.0, DEGREES_T ) );
269 BOOST_CHECK_GT( parallelErr(), 0.1 ); // rotate broke parallelism
270
271 std::vector<PCB_SHAPE*> shapes{ c }, mod;
272 ReSolveShapeClusters( &b, shapes, &mod,
273 []( BOARD_ITEM* )
274 {
275 } );
276
277 // The re-solve pulls the pair back near parallel (approximate, not solver-exact) and the
278 // constraint stays satisfiable.
279 BOOST_CHECK_LE( parallelErr(), 0.05 );
280 BOOST_CHECK( DiagnoseBoardConstraints( &b ).conflicting.empty() );
281}
282
283// A reference constraint leaves no driving residual to catch a diverged solve, so the state itself must
284BOOST_AUTO_TEST_CASE( NonFiniteStateIsNeverSatisfied )
285{
286 BOARD b;
287 auto* s = addSegment( b, { 0, 0 }, { 10 * MM, 2 * MM } );
288 PCB_CONSTRAINT* reference = addConstraint( b, PCB_CONSTRAINT_TYPE::HORIZONTAL, { whole( s ) } );
289 reference->SetDriving( false );
290
292 BOOST_REQUIRE( adapter.Build( { s }, { reference } ) );
293
295 std::ranges::fill( state, std::numeric_limits<double>::quiet_NaN() );
296 BOOST_REQUIRE( adapter.Restore( state ) );
297
299}
300
301// A contradiction satisfied by collapsing a segment loses the length derivative, and the solve must stop there
302BOOST_AUTO_TEST_CASE( ContradictionLeavesFiniteGeometry )
303{
304 BOARD b;
305 auto* locked = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
306 auto* free = addSegment( b, { 0, 5 * MM }, { 8 * MM, 3 * MM } );
307 locked->SetLocked( true );
308 author( b, PCB_CONSTRAINT_TYPE::PARALLEL, { whole( locked ), whole( free ) } );
309 addConstraint( b, PCB_CONSTRAINT_TYPE::PERPENDICULAR, { whole( locked ), whole( free ) } );
310
311 // Authoring the second relation solves with the first member's start pinned where it is
312 std::vector<PCB_CONSTRAINT*> constraints( b.Constraints().begin(), b.Constraints().end() );
314 BOOST_REQUIRE( adapter.Build( { locked, free }, constraints ) );
315 adapter.Solve( { locked->m_Uuid, CONSTRAINT_ANCHOR::START }, locked->GetStart(), true );
316
317 for( double param : adapter.Snapshot() )
318 BOOST_CHECK( std::isfinite( param ) );
319}
320
321// The perpendicular contradicts the parallel, and the solve that meets a collapse must not move either segment
322BOOST_AUTO_TEST_CASE( ContradictoryAuthoringLeavesTheBoard )
323{
324 BOARD b;
325 auto* s1 = addSegment( b, { 0, 0 }, { 10 * MM, 0 } );
326 auto* s2 = addSegment( b, { 0, 5 * MM }, { 8 * MM, 3 * MM } );
327 author( b, PCB_CONSTRAINT_TYPE::PARALLEL, { whole( s1 ), whole( s2 ) } );
328
329 const VECTOR2I s1End = s1->GetEnd();
330 const VECTOR2I s2Start = s2->GetStart();
331 const VECTOR2I s2End = s2->GetEnd();
332
333 author( b, PCB_CONSTRAINT_TYPE::PERPENDICULAR, { whole( s1 ), whole( s2 ) } );
334
335 BOOST_CHECK_EQUAL( s1->GetEnd(), s1End );
336 BOOST_CHECK_EQUAL( s2->GetStart(), s2Start );
337 BOOST_CHECK_EQUAL( s2->GetEnd(), s2End );
338}
339
CONSTRAINT_DIAGNOSIS ApplyConstraintImmediately(BOARD *aBoard, const PCB_CONSTRAINT *aConstraint, std::vector< PCB_SHAPE * > *aModified, const std::function< void(BOARD_ITEM *)> &aBeforeModify, const std::set< KIID > &aFixedShapes)
Solve a just-created constraint's cluster so the geometry snaps to satisfy it (SolidWorks-style),...
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.
BOARD_CONSTRAINT_DIAGNOSTICS DiagnoseBoardConstraints(BOARD *aBoard)
Diagnose every constraint cluster on the board (validate only – geometry is not changed) and return t...
Translates KiCad board geometry to and from the planegcs solver (issue #2329).
bool Solve(const CONSTRAINT_MEMBER &aDragged, const VECTOR2I &aCursor, bool aStabilize=false, const std::set< KIID > &aEdited={}, const std::optional< std::pair< CONSTRAINT_MEMBER, VECTOR2I > > &aCoDragged=std::nullopt, bool aHoldDraggedRigid=false)
Solve the system, pinning a dragged anchor to a cursor position.
CONSTRAINT_SYSTEM_2D::SNAPSHOT SNAPSHOT
bool Restore(const SNAPSHOT &aSnapshot)
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.
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
const CONSTRAINTS & Constraints() const
Geometric constraints (#2329) owned by this board.
Definition board.h:522
const KIID m_Uuid
Definition eda_item.h:599
A geometric constraint between board items (issue #2329).
void SetDriving(bool aDriving)
Definition seg.h:38
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Return the closest Euclidean distance between point aP and the line defined by the ends of segment (t...
Definition seg.cpp:710
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:281
@ DEGREES_T
Definition eda_angle.h:30
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:28
Test utilities for timestamps.
PCB_SHAPE * addSegment(BOARD &aBoard, const VECTOR2I &aStart, const VECTOR2I &aEnd)
PCB_SHAPE * addCircle(BOARD &aBoard, const VECTOR2I &aCenter, int aRadius)
constexpr int MM
PCB_CONSTRAINT * addConstraint(BOARD &aBoard, PCB_CONSTRAINT_TYPE aType, const std::vector< CONSTRAINT_MEMBER > &aMembers, std::optional< double > aValue=std::nullopt)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
@ WHOLE
The item as a whole (a segment as a line, a circle).
@ START
First endpoint of a segment or arc.
PCB_CONSTRAINT_TYPE
The geometric relationship a PCB_CONSTRAINT enforces between its members.
@ TANGENT
A line and a curve, or two curves, touch tangentially.
@ PERPENDICULAR
Two segments are perpendicular.
@ HORIZONTAL
A segment (or two points) is horizontal.
@ PARALLEL
Two segments are parallel.
Board-wide diagnostics for the constraint overlay and info bar.
One participant in a constraint: a referenced board item plus the feature of that item that participa...
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(Sweep)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
DATABASE_CONNECTION dc(odbcBackend(cs))
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708