KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_block_drag_dependents.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
21
22#include <base_units.h>
23#include <board.h>
24#include <footprint.h>
25#include <pcb_shape.h>
27
28
29namespace
30{
31
32const int TOL = pcbIUScale.mmToIU( 0.001 );
33
34PCB_SHAPE* makeSegment( BOARD& aBoard, const VECTOR2I& aStart, const VECTOR2I& aEnd, PCB_LAYER_ID aLayer = Edge_Cuts )
35{
36 PCB_SHAPE* segment = new PCB_SHAPE( &aBoard, SHAPE_T::SEGMENT );
37 segment->SetStart( aStart );
38 segment->SetEnd( aEnd );
39 segment->SetLayer( aLayer );
40 aBoard.Add( segment );
41 return segment;
42}
43
44BLOCK_DRAG_ENDPOINT movedStart( PCB_SHAPE* aShape )
45{
46 return { aShape, true, aShape->GetStart() };
47}
48
49PCB_SHAPE* makeArc( BOARD& aBoard, const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd,
50 PCB_LAYER_ID aLayer = Edge_Cuts )
51{
52 PCB_SHAPE* arc = new PCB_SHAPE( &aBoard, SHAPE_T::ARC );
53 arc->SetArcGeometry( aStart, aMid, aEnd );
54 arc->SetLayer( aLayer );
55 aBoard.Add( arc );
56 return arc;
57}
58
59const BLOCK_DRAG_ENDPOINT* find( const std::vector<BLOCK_DRAG_ENDPOINT>& aDependents, const PCB_SHAPE* aShape )
60{
61 for( const BLOCK_DRAG_ENDPOINT& dep : aDependents )
62 {
63 if( dep.shape == aShape )
64 return &dep;
65 }
66
67 return nullptr;
68}
69
70} // namespace
71
72
73BOOST_AUTO_TEST_SUITE( BlockDragDependentsTests )
74
75
76BOOST_AUTO_TEST_CASE( ExactAndToleranceMatches )
77{
78 BOARD board;
79 board.SetOutlinesChainingEpsilon( TOL );
80
81 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
82 PCB_SHAPE* exact = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 0, 100000 ) );
83 PCB_SHAPE* nearSeg = makeSegment( board, VECTOR2I( TOL, 0 ), VECTOR2I( 50000, 100000 ) );
84 PCB_SHAPE* farSeg = makeSegment( board, VECTOR2I( TOL + 1, TOL + 1 ), VECTOR2I( 90000, 90000 ) );
85
86 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
87
89 BlockDragFindDependents( { exact, nearSeg, farSeg }, { movedStart( moved ) }, false, dependents );
90
92 BOOST_REQUIRE_EQUAL( dependents.size(), 2 );
93
94 int exactCount = 0;
95 int nearCount = 0;
96
97 for( const BLOCK_DRAG_ENDPOINT& dep : dependents )
98 {
99 exactCount += dep.shape == exact ? 1 : 0;
100 nearCount += dep.shape == nearSeg ? 1 : 0;
101
102 BOOST_CHECK( dep.shape != farSeg );
103 BOOST_CHECK( dep.isStart );
104 }
105
106 BOOST_CHECK_EQUAL( exactCount, 1 );
107 BOOST_CHECK_EQUAL( nearCount, 1 );
108}
109
110
111// A candidate close enough on each axis on its own but too far away as the crow
112// flies has to be rejected, because the tolerance is a real distance
113BOOST_AUTO_TEST_CASE( ToleranceIsEuclidean )
114{
115 BOARD board;
116 board.SetOutlinesChainingEpsilon( TOL );
117
118 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
119 PCB_SHAPE* diagonal = makeSegment( board, VECTOR2I( TOL, TOL ), VECTOR2I( 90000, 90000 ) );
120
121 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
122
124 BlockDragFindDependents( { diagonal }, { movedStart( moved ) }, false, dependents );
125
127 BOOST_CHECK( dependents.empty() );
128}
129
130
131BOOST_AUTO_TEST_CASE( ArcCandidateFollowsWhole )
132{
133 BOARD board;
134
135 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
136 PCB_SHAPE* arc = makeArc( board, VECTOR2I( 0, 0 ), VECTOR2I( 50000, 50000 ), VECTOR2I( 100000, 0 ) );
137
138 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
139
140 BLOCK_DRAG_DEPENDENT_RESULT result = BlockDragFindDependents( { arc }, { movedStart( moved ) }, false, dependents );
141
143 BOOST_REQUIRE_EQUAL( dependents.size(), 1 );
144 BOOST_CHECK_EQUAL( dependents[0].shape, arc );
145 BOOST_CHECK( dependents[0].follow == BLOCK_DRAG_FOLLOW::WHOLE );
146}
147
148
149BOOST_AUTO_TEST_CASE( FilletedCornerCarriesArcAndStretchesNextSegment )
150{
151 // A filleted corner: a straight edge, a corner arc, then the next edge. Dragging
152 // the first edge's joint carries the arc whole and moves the end of the far
153 // segment that touches it, while that segment's other end holds.
154 BOARD board;
155
156 // Quarter arc centred on (0, 100000), from (0,0) round to (100000,100000)
157 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( -100000, 0 ), VECTOR2I( 0, 0 ) );
158 PCB_SHAPE* arc = makeArc( board, VECTOR2I( 0, 0 ), VECTOR2I( 70711, 29289 ), VECTOR2I( 100000, 100000 ) );
159 PCB_SHAPE* next = makeSegment( board, VECTOR2I( 100000, 100000 ), VECTOR2I( 100000, 400000 ) );
160
161 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
162
164 BlockDragFindDependents( { arc, next }, { { moved, false, moved->GetEnd() } }, false, dependents );
165
167 BOOST_REQUIRE_EQUAL( dependents.size(), 2 );
168
169 const BLOCK_DRAG_ENDPOINT* arcDep = find( dependents, arc );
170 const BLOCK_DRAG_ENDPOINT* nextDep = find( dependents, next );
171
172 BOOST_REQUIRE( arcDep && nextDep );
174 BOOST_CHECK( arcDep->isStart );
175
176 // The walk stops at the segment, which stretches, so its far end holds
178 BOOST_CHECK( nextDep->isStart );
179}
180
181
182BOOST_AUTO_TEST_CASE( ClosedChainDoesNotCascadeIntoItself )
183{
184 // Four arcs joined end to end with no segment anywhere. The walk visits each one
185 // once and stops, so it cannot loop back to the arc it started from.
186 BOARD board;
187
188 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( -100000, 0 ), VECTOR2I( 0, 0 ) );
189
190 // A full circle centred on (0, 150000), cut into four quarters, closing on (0,0)
191 PCB_SHAPE* a1 = makeArc( board, VECTOR2I( 0, 0 ), VECTOR2I( 106066, 43934 ), VECTOR2I( 150000, 150000 ) );
192 PCB_SHAPE* a2 = makeArc( board, VECTOR2I( 150000, 150000 ), VECTOR2I( 106066, 256066 ), VECTOR2I( 0, 300000 ) );
193 PCB_SHAPE* a3 = makeArc( board, VECTOR2I( 0, 300000 ), VECTOR2I( -106066, 256066 ), VECTOR2I( -150000, 150000 ) );
194 PCB_SHAPE* a4 = makeArc( board, VECTOR2I( -150000, 150000 ), VECTOR2I( -106066, 43934 ), VECTOR2I( 0, 0 ) );
195
196 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
197
199 BlockDragFindDependents( { a1, a2, a3, a4 }, { { moved, false, moved->GetEnd() } }, false, dependents );
200
202
203 // Every arc follows, and each one exactly once. Checking for "at most once" would
204 // also pass if the walk found nothing at all
205 BOOST_REQUIRE_EQUAL( dependents.size(), 4 );
206
207 for( const PCB_SHAPE* arc : { a1, a2, a3, a4 } )
208 {
209 int seen = 0;
210
211 for( const BLOCK_DRAG_ENDPOINT& dep : dependents )
212 {
213 if( dep.shape == arc )
214 {
215 seen++;
216 BOOST_CHECK( dep.follow == BLOCK_DRAG_FOLLOW::WHOLE );
217 }
218 }
219
220 BOOST_CHECK_EQUAL( seen, 1 );
221 }
222}
223
224
225BOOST_AUTO_TEST_CASE( LockedArcInTheChainRefuses )
226{
227 BOARD board;
228
229 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( -100000, 0 ), VECTOR2I( 0, 0 ) );
230 PCB_SHAPE* arc = makeArc( board, VECTOR2I( 0, 0 ), VECTOR2I( 70711, 29289 ), VECTOR2I( 100000, 100000 ) );
231
232 arc->SetLocked( true );
233
234 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
235
237 BlockDragFindDependents( { arc }, { { moved, false, moved->GetEnd() } }, false, dependents );
238
240 BOOST_CHECK( dependents.empty() );
241}
242
243
244BOOST_AUTO_TEST_CASE( CircleCandidateIsStillSkipped )
245{
246 BOARD board;
247
248 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
249
250 PCB_SHAPE* circle = new PCB_SHAPE( &board, SHAPE_T::CIRCLE );
251 circle->SetStart( VECTOR2I( 0, 0 ) );
252 circle->SetEnd( VECTOR2I( 50000, 0 ) );
253 circle->SetLayer( moved->GetLayer() );
254 board.Add( circle );
255
256 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
257
259 BlockDragFindDependents( { circle }, { movedStart( moved ) }, false, dependents );
260
262 BOOST_CHECK( dependents.empty() );
263}
264
265
266BOOST_AUTO_TEST_CASE( DifferentLayerDoesNotMatch )
267{
268 BOARD board;
269
270 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ), Edge_Cuts );
271 makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 0, 100000 ), F_SilkS );
272
273 std::vector<PCB_SHAPE*> candidates;
274
275 for( BOARD_ITEM* drawing : board.Drawings() )
276 {
277 if( drawing != moved )
278 candidates.push_back( static_cast<PCB_SHAPE*>( drawing ) );
279 }
280
281 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
282
283 BOOST_REQUIRE( BlockDragFindDependents( candidates, { movedStart( moved ) }, false, dependents )
285 BOOST_CHECK( dependents.empty() );
286}
287
288
289BOOST_AUTO_TEST_CASE( AmbiguousCandidatesAreNotGuessed )
290{
291 BOARD board;
292 board.SetOutlinesChainingEpsilon( TOL );
293
294 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
295
296 // Both close enough to the moved endpoint, but not to each other
297 PCB_SHAPE* left = makeSegment( board, VECTOR2I( -TOL, 0 ), VECTOR2I( -50000, 50000 ) );
298 PCB_SHAPE* right = makeSegment( board, VECTOR2I( TOL, 0 ), VECTOR2I( 50000, 50000 ) );
299
300 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
301
302 BOOST_REQUIRE( BlockDragFindDependents( { left, right }, { movedStart( moved ) }, false, dependents )
304 BOOST_CHECK( dependents.empty() );
305}
306
307
308BOOST_AUTO_TEST_CASE( MutuallyCoincidentClusterFollowsTogether )
309{
310 BOARD board;
311
312 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
313 PCB_SHAPE* first = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( -50000, 50000 ) );
314 PCB_SHAPE* second = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 50000, 50000 ) );
315
316 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
317
318 BOOST_REQUIRE( BlockDragFindDependents( { first, second }, { movedStart( moved ) }, false, dependents )
320 BOOST_REQUIRE_EQUAL( dependents.size(), 2 );
321
322 int firstCount = 0;
323 int secondCount = 0;
324
325 for( const BLOCK_DRAG_ENDPOINT& dep : dependents )
326 {
327 firstCount += dep.shape == first ? 1 : 0;
328 secondCount += dep.shape == second ? 1 : 0;
329 }
330
331 BOOST_CHECK_EQUAL( firstCount, 1 );
332 BOOST_CHECK_EQUAL( secondCount, 1 );
333}
334
335
336BOOST_AUTO_TEST_CASE( MovedEndpointsAreNotDependents )
337{
338 BOARD board;
339
340 // Both segments' starts are picked and sit on each other, so neither can follow
341 PCB_SHAPE* movedA = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
342 PCB_SHAPE* movedB = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 0, 100000 ) );
343
344 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
345
346 BOOST_REQUIRE( BlockDragFindDependents( { movedA, movedB }, { movedStart( movedA ), movedStart( movedB ) }, false,
347 dependents )
349 BOOST_CHECK( dependents.empty() );
350}
351
352
353BOOST_AUTO_TEST_CASE( DependentAppearsOnce )
354{
355 BOARD board;
356
357 // Two picked endpoints sit on the same neighbour endpoint
358 PCB_SHAPE* movedA = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
359 PCB_SHAPE* movedB = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 0, 100000 ) );
360 PCB_SHAPE* neighbor = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( -50000, -50000 ) );
361
362 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
363
365 BlockDragFindDependents( { neighbor }, { movedStart( movedA ), movedStart( movedB ) }, false, dependents )
367 BOOST_REQUIRE_EQUAL( dependents.size(), 1 );
368 BOOST_CHECK( dependents.front().shape == neighbor );
369}
370
371
372BOOST_AUTO_TEST_CASE( LockedDependentRejectsUnlessOverridden )
373{
374 BOARD board;
375
376 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
377 PCB_SHAPE* locked = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 0, 100000 ) );
378 locked->SetLocked( true );
379
380 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
381
382 BOOST_CHECK( BlockDragFindDependents( { locked }, { movedStart( moved ) }, false, dependents )
384 BOOST_CHECK( dependents.empty() );
385
386 BOOST_CHECK( BlockDragFindDependents( { locked }, { movedStart( moved ) }, true, dependents )
388 BOOST_REQUIRE_EQUAL( dependents.size(), 1 );
389}
390
391
392BOOST_AUTO_TEST_CASE( LockedParentOfDependentRejects )
393{
394 BOARD board;
395
396 PCB_SHAPE* moved = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
397
398 // The candidate is not locked itself, but it sits inside a locked footprint
399 FOOTPRINT* footprint = new FOOTPRINT( &board );
400 footprint->SetLocked( true );
401
402 PCB_SHAPE* child = new PCB_SHAPE( footprint, SHAPE_T::SEGMENT );
403 child->SetStart( VECTOR2I( 0, 0 ) );
404 child->SetEnd( VECTOR2I( 0, 100000 ) );
405 child->SetLayer( Edge_Cuts );
406 footprint->Add( child );
407 board.Add( footprint );
408
409 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
410
411 BOOST_CHECK( BlockDragFindDependents( { child }, { movedStart( moved ) }, false, dependents )
413 BOOST_CHECK( dependents.empty() );
414
415 BOOST_CHECK( BlockDragFindDependents( { child }, { movedStart( moved ) }, true, dependents )
417 BOOST_REQUIRE_EQUAL( dependents.size(), 1 );
418}
419
420
421BOOST_AUTO_TEST_CASE( BothEndsOfOneNeighborCanFollow )
422{
423 BOARD board;
424
425 PCB_SHAPE* movedA = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 100000, 0 ) );
426 PCB_SHAPE* movedB = makeSegment( board, VECTOR2I( 0, 50000 ), VECTOR2I( 100000, 50000 ) );
427 PCB_SHAPE* bridge = makeSegment( board, VECTOR2I( 0, 0 ), VECTOR2I( 0, 50000 ) );
428
429 std::vector<BLOCK_DRAG_ENDPOINT> dependents;
430
432 BlockDragFindDependents( { bridge }, { movedStart( movedA ), movedStart( movedB ) }, false, dependents )
434 BOOST_REQUIRE_EQUAL( dependents.size(), 2 );
435 BOOST_CHECK( dependents[0].shape == bridge );
436 BOOST_CHECK( dependents[1].shape == bridge );
437 BOOST_CHECK( dependents[0].isStart != dependents[1].isStart );
438}
439
440
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
BLOCK_DRAG_DEPENDENT_RESULT BlockDragFindDependents(const std::vector< PCB_SHAPE * > &aCandidates, const std::vector< BLOCK_DRAG_ENDPOINT > &aMovedEndpoints, bool aOverrideLocks, std::vector< BLOCK_DRAG_ENDPOINT > &aDependents)
Find the joined endpoints that have to follow the ones the drag moves.
BLOCK_DRAG_DEPENDENT_RESULT
@ LOCKED
A shape that would have to follow is locked and Override Locks is off, so the drag has to stop.
@ STRETCH
Only this end moves, so the shape stretches.
@ WHOLE
The whole shape moves, for a shape that moving one end alone would distort.
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
void SetOutlinesChainingEpsilon(int aValue)
Definition board.h:1082
const DRAWINGS & Drawings() const
Definition board.h:467
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:284
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Adds an item to the container.
void SetEnd(const VECTOR2I &aEnd) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
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.
@ SEGMENT
Definition eda_shape.h:57
static PCB_TRACK * makeSegment(BOARD *aBoard, const VECTOR2I &aStart, const VECTOR2I &aEnd)
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ Edge_Cuts
Definition layer_ids.h:108
@ F_SilkS
Definition layer_ids.h:96
CITER next(CITER it)
Definition ptree.cpp:120
One end of a graphic shape, used both for the ends that moved and for the ones found to follow them.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(ExactAndToleranceMatches)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
static PCB_SHAPE makeArc(const VECTOR2I &aCenter, const VECTOR2I &aStart, double aAngle, PCB_LAYER_ID aLayer=Dwgs_User)
BOOST_AUTO_TEST_SUITE_END()
bool moved
static std::vector< int > candidates(const SEGMENT_INDEX &aIndex, const SEG &aQuery, int aPadding)
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708