KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_block_drag_displacement.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 <board.h>
23#include <pcb_shape.h>
25#include <zone.h>
26#include <tool/edit_points.h>
27#include <tool/grid_helper.h>
30
31
32namespace
33{
34
35std::shared_ptr<EDIT_POINTS> makeTriangle( EDA_ITEM* aParent, int aOffset = 0 )
36{
37 std::shared_ptr<EDIT_POINTS> points = std::make_shared<EDIT_POINTS>( aParent );
38
39 points->AddPoint( VECTOR2I( aOffset, 0 ) );
40 points->AddPoint( VECTOR2I( aOffset + 1000, 0 ) );
41 points->AddPoint( VECTOR2I( aOffset + 1000, 1000 ) );
42 points->AddLine( points->Point( 0 ), points->Point( 1 ) );
43 points->AddLine( points->Point( 1 ), points->Point( 2 ) );
44
45 return points;
46}
47
48} // namespace
49
50
51BOOST_AUTO_TEST_SUITE( BlockDragDisplacementTests )
52
53
54// Two items, one drag. Every picked handle moves by the same offset, and no point
55// is snapped again against its own item's grid
56BOOST_AUTO_TEST_CASE( OneDisplacementReachesEveryPickedHandle )
57{
58 // These handles have no drag rule, so the grid is here only to be passed along
59 GRID_HELPER testGrid;
60 BOARD board;
61
62 ZONE* zone = new ZONE( &board );
63 PCB_SHAPE* poly = new PCB_SHAPE( &board, SHAPE_T::POLY );
64
65 board.Add( zone );
66 board.Add( poly );
67
68 std::shared_ptr<EDIT_POINTS> zonePoints = makeTriangle( zone );
69 std::shared_ptr<EDIT_POINTS> polyPoints = makeTriangle( poly, 5000 );
70
71 std::set<PCB_HANDLE_KEY> picked;
72 picked.insert( *PCB_HANDLE_KEY::Make( *zonePoints, &zonePoints->Point( 0 ) ) );
73 picked.insert( *PCB_HANDLE_KEY::Make( *zonePoints, &zonePoints->Point( 2 ) ) );
74 picked.insert( *PCB_HANDLE_KEY::Make( *polyPoints, &polyPoints->Point( 1 ) ) );
75
76 std::vector<EDIT_POINTS*> collections{ zonePoints.get(), polyPoints.get() };
77
78 std::map<PCB_HANDLE_KEY, VECTOR2I> origins = BlockDragMoveOrigins( collections, picked );
79
80 BOOST_REQUIRE_EQUAL( origins.size(), 3 );
81
82 // An offset that lines up with no grid, as if it had been snapped against the
83 // grabbed item alone. Every point lands at its start plus exactly this offset.
84 const VECTOR2I delta( 333, -77 );
85
86 BOOST_REQUIRE( BlockDragApplyDisplacement( collections, origins, delta, testGrid ) );
87
88 BOOST_CHECK( zonePoints->Point( 0 ).GetPosition() == VECTOR2I( 333, -77 ) );
89 BOOST_CHECK( zonePoints->Point( 2 ).GetPosition() == VECTOR2I( 1333, 923 ) );
90 BOOST_CHECK( polyPoints->Point( 1 ).GetPosition() == VECTOR2I( 6333, -77 ) );
91
92 BOOST_CHECK( zonePoints->Point( 1 ).GetPosition() == VECTOR2I( 1000, 0 ) );
93 BOOST_CHECK( polyPoints->Point( 0 ).GetPosition() == VECTOR2I( 5000, 0 ) );
94
95 // Positions come from the starting points, so a second call with a new offset
96 // does not add to the first
97 const VECTOR2I delta2( 10, 10 );
98
99 BOOST_REQUIRE( BlockDragApplyDisplacement( collections, origins, delta2, testGrid ) );
100 BOOST_CHECK( zonePoints->Point( 0 ).GetPosition() == VECTOR2I( 10, 10 ) );
101}
102
103
104// An odd offset with even endpoints. The midpoint of two integers rounds, so setting the
105// edge's own position would shift both of its ends a nanometre off target. The handles
106// here are built the way the editor builds them, so every line carries the same drag rule
107// a real picked edge has.
108BOOST_AUTO_TEST_CASE( PickedEdgeWithItsPolicyMovesPerpendicular )
109{
110 GRID_HELPER testGrid;
111 BOARD board;
112 ZONE* zone = new ZONE( &board );
113 board.Add( zone );
114
115 SHAPE_POLY_SET& outline = *zone->Outline();
116 outline.NewOutline();
117 outline.Append( 0, 0 );
118 outline.Append( 100000, 0 );
119 outline.Append( 100000, 100000 );
120 outline.Append( 0, 100000 );
121
122 std::shared_ptr<EDIT_POINTS> points = std::make_shared<EDIT_POINTS>( zone );
124
125 BOOST_REQUIRE( points->LinesSize() >= 1 );
126 BOOST_REQUIRE_MESSAGE( points->Line( 0 ).GetDragPolicy() != nullptr, "every edge should come with a drag rule" );
127
128 // The first edge runs between corners 0 and 1, the horizontal bottom edge
129 std::set<PCB_HANDLE_KEY> picked;
130 picked.insert( *PCB_HANDLE_KEY::Make( *points, &points->Line( 0 ) ) );
131
132 std::vector<EDIT_POINTS*> collections{ points.get() };
133 std::map<PCB_HANDLE_KEY, VECTOR2I> origins = BlockDragMoveOrigins( collections, picked );
134
135 // one edge key plus its two corners
136 BOOST_REQUIRE_EQUAL( origins.size(), 3 );
137
138 const VECTOR2I delta( 7000, 5000 );
139
140 BOOST_REQUIRE( BlockDragApplyDisplacement( collections, origins, delta, testGrid ) );
141
142 // The drag rule only lets the edge move at right angles to itself, so the part of the
143 // offset along the edge is dropped and the two corners keep their x
144 BOOST_CHECK_EQUAL( points->Point( 0 ).GetPosition().x, 0 );
145 BOOST_CHECK_EQUAL( points->Point( 1 ).GetPosition().x, 100000 );
146 BOOST_CHECK_EQUAL( points->Point( 0 ).GetPosition().y, 5000 );
147 BOOST_CHECK_EQUAL( points->Point( 1 ).GetPosition().y, 5000 );
148
149 // The corners this edge does not touch stay where they were
150 BOOST_CHECK( points->Point( 2 ).GetPosition() == VECTOR2I( 100000, 100000 ) );
151 BOOST_CHECK( points->Point( 3 ).GetPosition() == VECTOR2I( 0, 100000 ) );
152}
153
154
155// Only where the drag ends should matter. A picked edge's drag rule is rebuilt from the
156// current geometry every frame, so a cursor that wanders off and comes back is the case that
157// would show it drifting. The outline is compared, not the handle positions, because the
158// handles are written straight from origin plus delta and so cannot drift by construction.
159BOOST_AUTO_TEST_CASE( PickedEdgeIsIndependentOfThePathTaken )
160{
161 GRID_HELPER testGrid;
162
163 auto run = [&]( const std::vector<VECTOR2I>& aPath )
164 {
165 BOARD board;
166 ZONE* zone = new ZONE( &board );
167 board.Add( zone );
168
169 SHAPE_POLY_SET& outline = *zone->Outline();
170 outline.NewOutline();
171 outline.Append( 0, 0 );
172 outline.Append( 100000, 0 );
173 outline.Append( 100000, 100000 );
174 outline.Append( 0, 100000 );
175
176 std::shared_ptr<EDIT_POINTS> points = std::make_shared<EDIT_POINTS>( zone );
178
179 std::set<PCB_HANDLE_KEY> picked;
180 picked.insert( *PCB_HANDLE_KEY::Make( *points, &points->Line( 0 ) ) );
181
182 std::vector<EDIT_POINTS*> collections{ points.get() };
183 std::map<PCB_HANDLE_KEY, VECTOR2I> origins = BlockDragMoveOrigins( collections, picked );
184
185 // One frame is a displacement followed by the outline write, and the write is what
186 // reseeds each unpicked edge's drag rule from the geometry as it now stands. Leaving
187 // the write out would test a rule that is never rebuilt, which cannot drift.
188 for( const VECTOR2I& step : aPath )
189 {
190 BlockDragApplyDisplacement( collections, origins, step, testGrid );
191 POLYGON_POINT_EDIT_BEHAVIOR::UpdateOutlineFromPoints( outline, points->Line( 0 ), *points );
192 }
193
194 std::vector<VECTOR2I> result;
195
196 for( int ii = 0; ii < outline.TotalVertices(); ++ii )
197 result.push_back( outline.CVertex( ii ) );
198
199 return result;
200 };
201
202 const VECTOR2I target( 7000, 5000 );
203
204 std::vector<VECTOR2I> direct = run( { target } );
205 std::vector<VECTOR2I> wandered = run( { VECTOR2I( 30000, -20000 ), VECTOR2I( -5000, 40000 ), target } );
206
207 BOOST_REQUIRE_EQUAL( direct.size(), wandered.size() );
208
209 for( size_t ii = 0; ii < direct.size(); ++ii )
210 {
211 BOOST_CHECK_MESSAGE( direct[ii] == wandered[ii],
212 "corner " << ii << " landed at " << wandered[ii].x << "," << wandered[ii].y
213 << " by one path and " << direct[ii].x << "," << direct[ii].y << " by another" );
214 }
215}
216
217
218BOOST_AUTO_TEST_CASE( PickedLineLandsExactlyOnAnOddDelta )
219{
220 // These handles have no drag rule, so the grid is here only to be passed along
221 GRID_HELPER testGrid;
222 BOARD board;
223 ZONE* zone = new ZONE( &board );
224 board.Add( zone );
225
226 std::shared_ptr<EDIT_POINTS> points = makeTriangle( zone );
227
228 std::set<PCB_HANDLE_KEY> picked;
229 picked.insert( *PCB_HANDLE_KEY::Make( *points, &points->Line( 0 ) ) );
230
231 std::vector<EDIT_POINTS*> collections{ points.get() };
232
233 std::map<PCB_HANDLE_KEY, VECTOR2I> origins = BlockDragMoveOrigins( collections, picked );
234
235 const VECTOR2I delta( 501, 500 );
236
237 BOOST_REQUIRE( BlockDragApplyDisplacement( collections, origins, delta, testGrid ) );
238
239 BOOST_CHECK( points->Point( 0 ).GetPosition() == VECTOR2I( 501, 500 ) );
240 BOOST_CHECK( points->Point( 1 ).GetPosition() == VECTOR2I( 1501, 500 ) );
241}
242
243
244BOOST_AUTO_TEST_CASE( PickedLineExpandsWithoutDoubleMove )
245{
246 // These handles have no drag rule, so the grid is here only to be passed along
247 GRID_HELPER testGrid;
248 BOARD board;
249 ZONE* zone = new ZONE( &board );
250 board.Add( zone );
251
252 std::shared_ptr<EDIT_POINTS> points = makeTriangle( zone );
253
254 // Line 0 runs between points 0 and 1, and point 1 is picked on its own too
255 std::set<PCB_HANDLE_KEY> picked;
256 picked.insert( *PCB_HANDLE_KEY::Make( *points, &points->Line( 0 ) ) );
257 picked.insert( *PCB_HANDLE_KEY::Make( *points, &points->Point( 1 ) ) );
258
259 std::vector<EDIT_POINTS*> collections{ points.get() };
260
261 std::map<PCB_HANDLE_KEY, VECTOR2I> origins = BlockDragMoveOrigins( collections, picked );
262
263 // Corners 0 and 1, plus the edge itself, which the frame drives through its drag
264 // rule once they are placed
265 BOOST_REQUIRE_EQUAL( origins.size(), 3 );
266
267 int lineKeys = 0;
268
269 for( const auto& [key, origin] : origins )
270 lineKeys += key.IsLine() ? 1 : 0;
271
272 BOOST_CHECK_EQUAL( lineKeys, 1 );
273
274 const VECTOR2I delta( 500, 500 );
275
276 BOOST_REQUIRE( BlockDragApplyDisplacement( collections, origins, delta, testGrid ) );
277
278 BOOST_CHECK( points->Point( 0 ).GetPosition() == VECTOR2I( 500, 500 ) );
279 BOOST_CHECK( points->Point( 1 ).GetPosition() == VECTOR2I( 1500, 500 ) );
280 BOOST_CHECK( points->Point( 2 ).GetPosition() == VECTOR2I( 1000, 1000 ) );
281}
282
283
284BOOST_AUTO_TEST_CASE( MissingIdentityInvalidatesGesture )
285{
286 // These handles have no drag rule, so the grid is here only to be passed along
287 GRID_HELPER testGrid;
288 BOARD board;
289 ZONE* zone = new ZONE( &board );
290 board.Add( zone );
291
292 std::shared_ptr<EDIT_POINTS> before = makeTriangle( zone );
293
294 std::set<PCB_HANDLE_KEY> picked;
295 picked.insert( *PCB_HANDLE_KEY::Make( *before, &before->Point( 2 ) ) );
296
297 std::vector<EDIT_POINTS*> collections{ before.get() };
298
299 std::map<PCB_HANDLE_KEY, VECTOR2I> origins = BlockDragMoveOrigins( collections, picked );
300 BOOST_REQUIRE_EQUAL( origins.size(), 1 );
301
302 // The item loses a corner mid drag, so the collection is rebuilt smaller
303 std::shared_ptr<EDIT_POINTS> after = std::make_shared<EDIT_POINTS>( zone );
304 after->AddPoint( VECTOR2I( 0, 0 ) );
305 after->AddPoint( VECTOR2I( 1000, 0 ) );
306
307 std::vector<EDIT_POINTS*> rebuilt{ after.get() };
308
309 BOOST_CHECK( !BlockDragApplyDisplacement( rebuilt, origins, VECTOR2I( 10, 10 ), testGrid ) );
310
311 // Capture also refuses to start when a picked key has gone stale
312 BOOST_CHECK( BlockDragMoveOrigins( rebuilt, picked ).empty() );
313}
314
315
bool BlockDragApplyDisplacement(const std::vector< EDIT_POINTS * > &aCollections, const std::map< PCB_HANDLE_KEY, VECTOR2I > &aOrigins, const VECTOR2I &aDelta, const GRID_HELPER &aGrid)
Put every handle at its starting position plus the drag's offset.
std::map< PCB_HANDLE_KEY, VECTOR2I > BlockDragMoveOrigins(const std::vector< EDIT_POINTS * > &aCollections, const std::set< PCB_HANDLE_KEY > &aPicked)
Note every handle the drag will move, and where each one started.
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
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
static std::optional< PCB_HANDLE_KEY > Make(const EDIT_POINTS &aPoints, const EDIT_POINT *aPoint)
Build the key for a handle in a collection.
static void UpdateOutlineFromPoints(SHAPE_POLY_SET &aOutline, const EDIT_POINT &aEditedPoint, EDIT_POINTS &aPoints)
Update the polygon outline with the new positions of the edit points.
static void BuildForPolyOutline(EDIT_POINTS &aPoints, const SHAPE_POLY_SET &aOutline)
Build the edit points for the given polygon outline.
Represent a set of closed polygons.
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)
int NewOutline()
Creates a new empty polygon in the set and returns its index.
const VECTOR2I & CVertex(int aIndex, int aOutline, int aHole) const
Return the index-th vertex in a given hole outline within a given outline.
Handle a list of polygons defining a copper zone.
Definition zone.h:70
SHAPE_POLY_SET * Outline()
Definition zone.h:421
static bool empty(const wxTextEntryBase *aCtrl)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(OneDisplacementReachesEveryPickedHandle)
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_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
wxString result
Test unit parsing edge cases and error handling.
int delta
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708