KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_block_drag_validation.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#include <tool/tool_manager.h>
23
24#include <board.h>
25#include <board_commit.h>
27#include <pcb_shape.h>
28#include <zone.h>
30
31
32namespace
33{
34
35struct BLOCK_DRAG_VALIDATION_FIXTURE
36{
37 BOARD board;
38 TOOL_MANAGER mgr;
39 KI_TEST::DUMMY_TOOL* tool;
40
41 BLOCK_DRAG_VALIDATION_FIXTURE() :
42 tool( new KI_TEST::DUMMY_TOOL() )
43 {
44 mgr.SetEnvironment( &board, nullptr, nullptr, nullptr, nullptr );
45 mgr.RegisterTool( tool );
46 }
47
48 ZONE* makeSquareZone()
49 {
50 ZONE* zone = new ZONE( &board );
51 zone->Outline()->NewOutline();
52 zone->Outline()->Append( 0, 0 );
53 zone->Outline()->Append( 100000, 0 );
54 zone->Outline()->Append( 100000, 100000 );
55 zone->Outline()->Append( 0, 100000 );
56 board.Add( zone );
57 return zone;
58 }
59
61 ZONE* makeHoledZone()
62 {
63 ZONE* zone = makeSquareZone();
64
65 zone->Outline()->NewHole( 0 );
66 zone->Outline()->Append( 30000, 30000, 0, 0 );
67 zone->Outline()->Append( 70000, 30000, 0, 0 );
68 zone->Outline()->Append( 70000, 70000, 0, 0 );
69 zone->Outline()->Append( 30000, 70000, 0, 0 );
70 return zone;
71 }
72
73 PCB_SHAPE* makeEdgeSegment( const VECTOR2I& aStart, const VECTOR2I& aEnd )
74 {
75 PCB_SHAPE* segment = new PCB_SHAPE( &board, SHAPE_T::SEGMENT );
76 segment->SetStart( aStart );
77 segment->SetEnd( aEnd );
78 segment->SetLayer( Edge_Cuts );
79 board.Add( segment );
80 return segment;
81 }
82
83 PCB_SHAPE* makeClosedOutline()
84 {
85 PCB_SHAPE* first = makeEdgeSegment( VECTOR2I( 0, 0 ), VECTOR2I( 1000000, 0 ) );
86 makeEdgeSegment( VECTOR2I( 1000000, 0 ), VECTOR2I( 1000000, 1000000 ) );
87 makeEdgeSegment( VECTOR2I( 1000000, 1000000 ), VECTOR2I( 0, 1000000 ) );
88 makeEdgeSegment( VECTOR2I( 0, 1000000 ), VECTOR2I( 0, 0 ) );
89 return first;
90 }
91};
92
93} // namespace
94
95
96BOOST_FIXTURE_TEST_SUITE( BlockDragValidationTests, BLOCK_DRAG_VALIDATION_FIXTURE )
97
98
99BOOST_AUTO_TEST_CASE( InvalidZoneRestoresStagedGeometry )
100{
101 ZONE* zone = makeSquareZone();
102
103 std::vector<VECTOR2I> original;
104
105 for( int ii = 0; ii < zone->Outline()->TotalVertices(); ++ii )
106 original.push_back( zone->Outline()->CVertex( ii ) );
107
108 BLOCK_DRAG_VALIDATION validation( board );
109 validation.CaptureBaseline( { zone } );
110
111 BOARD_COMMIT commit( tool );
112 commit.Modify( zone );
113
114 // Swap two corners into a bow tie, which crosses itself
115 zone->Outline()->SetVertex( 2, VECTOR2I( 0, 100000 ) );
116 zone->Outline()->SetVertex( 3, VECTOR2I( 100000, 100000 ) );
117
120
121 commit.Revert();
122
123 BOOST_REQUIRE_EQUAL( zone->Outline()->TotalVertices(), (int) original.size() );
124
125 for( int ii = 0; ii < zone->Outline()->TotalVertices(); ++ii )
126 BOOST_CHECK( zone->Outline()->CVertex( ii ) == original[ii] );
127
130}
131
132
133// A zone with a hole has to count as valid at capture time, or the baseline comparison
134// accepts every later state including a broken one
135BOOST_AUTO_TEST_CASE( HoledZoneMovePasses )
136{
137 ZONE* zone = makeHoledZone();
138
139 BLOCK_DRAG_VALIDATION validation( board );
140 validation.CaptureBaseline( { zone } );
141
142 zone->Outline()->Move( VECTOR2I( 5000, 5000 ) );
143
145}
146
147
148BOOST_AUTO_TEST_CASE( OutlineDraggedAcrossItsHoleRejects )
149{
150 ZONE* zone = makeHoledZone();
151
152 BLOCK_DRAG_VALIDATION validation( board );
153 validation.CaptureBaseline( { zone } );
154
155 // Pull the far corner of the outline into the middle of the hole
156 zone->Outline()->Outline( 0 ).SetPoint( 2, VECTOR2I( 40000, 40000 ) );
157
159}
160
161
162BOOST_AUTO_TEST_CASE( HoleDraggedOutOfItsOutlineRejects )
163{
164 ZONE* zone = makeHoledZone();
165
166 BLOCK_DRAG_VALIDATION validation( board );
167 validation.CaptureBaseline( { zone } );
168
169 // Clear of the outline altogether, so nothing crosses and only containment catches it
170 zone->Outline()->Hole( 0, 0 ).Move( VECTOR2I( 200000, 0 ) );
171
173}
174
175
176BOOST_AUTO_TEST_CASE( ValidZoneMovePasses )
177{
178 ZONE* zone = makeSquareZone();
179
180 BLOCK_DRAG_VALIDATION validation( board );
181 validation.CaptureBaseline( { zone } );
182
183 for( int ii = 0; ii < zone->Outline()->TotalVertices(); ++ii )
184 zone->Outline()->SetVertex( ii, zone->Outline()->CVertex( ii ) + VECTOR2I( 5000, 5000 ) );
185
187}
188
189
190// RemoveNullSegments can leave a zone with two corners. It no longer encloses
191// anything, but it does not cross itself either, so it needs its own check
192BOOST_AUTO_TEST_CASE( CollapsedOutlineRejects )
193{
194 ZONE* zone = makeSquareZone();
195
196 BLOCK_DRAG_VALIDATION validation( board );
197 validation.CaptureBaseline( { zone } );
198
199 zone->Outline()->Outline( 0 ).Remove( 2, 3 );
200
202}
203
204
205BOOST_AUTO_TEST_CASE( BrokenOutlineRejectsAndRestores )
206{
207 PCB_SHAPE* moved = makeClosedOutline();
208
209 BLOCK_DRAG_VALIDATION validation( board );
210 validation.CaptureBaseline( { moved } );
211
212 BOARD_COMMIT commit( tool );
213 commit.Modify( moved );
214
215 moved->SetStart( VECTOR2I( 500000, 500000 ) );
216
218
219 commit.Revert();
220
222}
223
224
225// A drag that starts from an outline that is already open is not refused, or a broken
226// outline could never be dragged back into shape. The move has to leave it open as well,
227// otherwise the result is OK whether or not the baseline is consulted
228BOOST_AUTO_TEST_CASE( PreBrokenOutlineDoesNotReject )
229{
230 // Three segments only, so the outline is already open before the drag
231 makeEdgeSegment( VECTOR2I( 0, 0 ), VECTOR2I( 1000000, 0 ) );
232 makeEdgeSegment( VECTOR2I( 1000000, 0 ), VECTOR2I( 1000000, 1000000 ) );
233 PCB_SHAPE* moved = makeEdgeSegment( VECTOR2I( 1000000, 1000000 ), VECTOR2I( 0, 1000000 ) );
234
235 SHAPE_POLY_SET outlines;
236 BOOST_REQUIRE( !board.GetBoardPolygonOutlines( outlines, false ) );
237
238 BLOCK_DRAG_VALIDATION validation( board );
239 validation.CaptureBaseline( { moved } );
240
241 moved->SetEnd( VECTOR2I( 400000, 700000 ) );
242
243 BOOST_REQUIRE( !board.GetBoardPolygonOutlines( outlines, false ) );
244
246}
247
248
249BOOST_AUTO_TEST_CASE( PreInvalidZoneDoesNotReject )
250{
251 ZONE* zone = new ZONE( &board );
252 zone->Outline()->NewOutline();
253 zone->Outline()->Append( 0, 0 );
254 zone->Outline()->Append( 100000, 0 );
255 zone->Outline()->Append( 0, 100000 );
256 zone->Outline()->Append( 100000, 100000 );
257 board.Add( zone );
258
260
261 BLOCK_DRAG_VALIDATION validation( board );
262 validation.CaptureBaseline( { zone } );
263
264 for( int ii = 0; ii < zone->Outline()->TotalVertices(); ++ii )
265 zone->Outline()->SetVertex( ii, zone->Outline()->CVertex( ii ) + VECTOR2I( 5000, 5000 ) );
266
268}
269
270
271// The carried items decide whether the outline is checked at all, so a break in
272// geometry the drag never touched does not stop it
273BOOST_AUTO_TEST_CASE( UncarriedOutlineDamageDoesNotReject )
274{
275 PCB_SHAPE* untouched = makeClosedOutline();
276 ZONE* zone = makeSquareZone();
277
278 BLOCK_DRAG_VALIDATION validation( board );
279 validation.CaptureBaseline( { zone } );
280
281 untouched->SetStart( VECTOR2I( 500000, 500000 ) );
282
284}
285
286
287BOOST_AUTO_TEST_CASE( FootprintHolderOutlineIsNotProtected )
288{
290
291 PCB_SHAPE* moved = makeClosedOutline();
292
293 BLOCK_DRAG_VALIDATION validation( board );
294 validation.CaptureBaseline( { moved } );
295
296 moved->SetStart( VECTOR2I( 500000, 500000 ) );
297
299}
300
301
@ INVALID_OUTLINE
The board outline was closed before the drag and the moved Edge.Cuts broke it.
@ INVALID_ZONE
A zone that came along lost its valid outline.
@ FPHOLDER
Definition board.h:403
Checks a Block Drag's result at release and says whether it may be kept.
void CaptureBaseline(const std::vector< BOARD_ITEM * > &aCarriedItems)
Note whether the zones coming along, and the board outline, were valid to start with.
BLOCK_DRAG_VALIDATION_RESULT Validate() const
void Revert() override
Revert the commit by restoring the modified items state.
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 SetBoardUse(BOARD_USE aUse)
Set what the board is going to be used for.
Definition board.h:423
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, bool aInferOutlineIfNecessary, OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr, bool aAllowUseArcsInPolygons=false, bool aIncludeNPTHAsOutlines=false)
Extract the board outlines and build a closed polygon from lines, arcs and circle items on edge cut l...
Definition board.cpp:3651
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
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 Move(const VECTOR2I &aVector) override
void SetPoint(int aIndex, const VECTOR2I &aPos)
Move a point to a specific location.
void Remove(int aStartIndex, int aEndIndex)
Remove the range of points [start_index, end_index] from the line chain.
Represent a set of closed polygons.
void SetVertex(const VERTEX_INDEX &aIndex, const VECTOR2I &aPos)
Accessor function to set the position of a specific point.
int TotalVertices() const
Return total number of vertices stored in the set.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
SHAPE_LINE_CHAIN & Hole(int aOutline, int aHole)
Return the reference to aHole-th hole in the aIndex-th outline.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
int NewHole(int aOutline=-1)
Creates a new hole in a given outline.
const VECTOR2I & CVertex(int aIndex, int aOutline, int aHole) const
Return the index-th vertex in a given hole outline within a given outline.
void Move(const VECTOR2I &aVector) override
bool IsSelfIntersecting() const
Check whether any of the polygons in the set is self intersecting.
Handle a list of polygons defining a copper zone.
Definition zone.h:70
SHAPE_POLY_SET * Outline()
Definition zone.h:421
@ SEGMENT
Definition eda_shape.h:57
@ Edge_Cuts
Definition layer_ids.h:108
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(InvalidZoneRestoresStagedGeometry)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
bool moved
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708