KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_edit_relations.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 3
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 <boost/test/unit_test.hpp>
21
22#include <tool/edit_points.h>
23#include <tool/edit_relations.h>
24#include <tool/grid_helper.h>
26#include <commit.h>
27#include <eda_shape.h>
29
30
31BOOST_AUTO_TEST_SUITE( EditRelations )
32
33
34BOOST_AUTO_TEST_CASE( PointRelationsExposeTypedSemantics )
35{
36 EDIT_POINT constrained( VECTOR2I( 30, 40 ) );
37 EDIT_POINT reference( VECTOR2I( 10, 20 ) );
38
39 constrained.SetRelation( EDIT_RELATION::SameX( reference ) );
40
41 BOOST_REQUIRE( constrained.GetRelation() );
42 BOOST_CHECK( constrained.GetRelation()->Kind() == EDIT_RELATION_KIND::SAME_X );
43
45 constrained.ApplyRelation( grid );
46 BOOST_CHECK_EQUAL( constrained.GetPosition(), VECTOR2I( 10, 40 ) );
47}
48
49
50BOOST_AUTO_TEST_CASE( PerpendicularLinePreservesDirection )
51{
52 EDIT_POINT origin( VECTOR2I( 0, 0 ) );
53 EDIT_POINT end( VECTOR2I( 20, 0 ) );
54 EDIT_LINE line( origin, end );
55
57 line.SetPosition( VECTOR2I( 17, 9 ) );
58
60 line.ApplyRelation( grid );
61
62 BOOST_CHECK_EQUAL( end.GetPosition() - origin.GetPosition(), VECTOR2I( 20, 0 ) );
63 BOOST_CHECK_EQUAL( origin.GetY(), end.GetY() );
64 BOOST_CHECK_LE( std::abs( line.GetX() - 10 ), 1 );
65}
66
67
68BOOST_AUTO_TEST_CASE( PolygonFixedLengthPreservesEndpoints )
69{
70 EDIT_POINTS points( nullptr );
71 points.AddPoint( VECTOR2I( 0, 10 ) );
72 points.AddPoint( VECTOR2I( 0, 0 ) );
73 points.AddPoint( VECTOR2I( 20, 0 ) );
74 points.AddPoint( VECTOR2I( 20, 10 ) );
75 points.AddLine( points.Point( 0 ), points.Point( 1 ) );
76 points.AddLine( points.Point( 1 ), points.Point( 2 ) );
77 points.AddLine( points.Point( 2 ), points.Point( 3 ) );
78 points.AddLine( points.Point( 3 ), points.Point( 0 ) );
79
80 EDIT_LINE& line = points.Line( 1 );
81 line.SetDragPolicy( std::make_unique<POLYGON_EDGE_DRAG_POLICY>( line, points, POLYGON_LINE_MODE::FIXED_LENGTH ) );
82 line.SetPosition( VECTOR2I( 10, 5 ) );
83
85 line.ApplyRelation( grid );
86
88 BOOST_CHECK( line.GetDragPolicy()->GetMode() == POLYGON_LINE_MODE::FIXED_LENGTH );
89 BOOST_CHECK_EQUAL( line.GetEnd().GetPosition() - line.GetOrigin().GetPosition(), VECTOR2I( 20, 0 ) );
90 BOOST_CHECK_EQUAL( line.GetOrigin().GetY(), line.GetEnd().GetY() );
91}
92
93
94BOOST_AUTO_TEST_CASE( PolygonConvergenceClampsAtAdjacentIntersection )
95{
96 EDIT_POINTS points( nullptr );
97 points.AddPoint( VECTOR2I( 10, 10 ) );
98 points.AddPoint( VECTOR2I( 0, 0 ) );
99 points.AddPoint( VECTOR2I( 20, 0 ) );
100 points.AddPoint( VECTOR2I( 10, 10 ) );
101 points.AddLine( points.Point( 0 ), points.Point( 1 ) );
102 points.AddLine( points.Point( 1 ), points.Point( 2 ) );
103 points.AddLine( points.Point( 2 ), points.Point( 3 ) );
104 points.AddLine( points.Point( 3 ), points.Point( 0 ) );
105
106 EDIT_LINE& line = points.Line( 1 );
107 line.SetDragPolicy( std::make_unique<POLYGON_EDGE_DRAG_POLICY>( line, points ) );
108 line.SetPosition( VECTOR2I( 10, 20 ) );
109
111 line.ApplyRelation( grid );
112
113 BOOST_CHECK_EQUAL( line.GetOrigin().GetPosition(), VECTOR2I( 10, 10 ) );
114 BOOST_CHECK_EQUAL( line.GetEnd().GetPosition(), VECTOR2I( 10, 10 ) );
115}
116
117
118BOOST_AUTO_TEST_CASE( TriangleEdgeDragPassesThroughTheCollapsePoint )
119{
120 EDIT_POINTS points( nullptr );
121 points.AddPoint( VECTOR2I( 0, 0 ) );
122 points.AddPoint( VECTOR2I( 40, 0 ) );
123 points.AddPoint( VECTOR2I( 20, 30 ) );
124 points.AddLine( points.Point( 0 ), points.Point( 1 ) );
125 points.AddLine( points.Point( 1 ), points.Point( 2 ) );
126 points.AddLine( points.Point( 2 ), points.Point( 0 ) );
127
128 EDIT_LINE& line = points.Line( 0 );
129 line.SetDragPolicy( std::make_unique<POLYGON_EDGE_DRAG_POLICY>( line, points ) );
130 line.SetPosition( VECTOR2I( 20, 60 ) );
131
133 line.ApplyRelation( grid );
134
135 BOOST_CHECK_EQUAL( line.GetOrigin().GetPosition(), VECTOR2I( 40, 60 ) );
136 BOOST_CHECK_EQUAL( line.GetEnd().GetPosition(), VECTOR2I( 0, 60 ) );
137}
138
139
140BOOST_AUTO_TEST_CASE( TriangularHoleEdgeDragKeepsItsCorners )
141{
142 SHAPE_POLY_SET poly;
143
144 poly.NewOutline();
145 poly.Append( VECTOR2I( 0, 0 ) );
146 poly.Append( VECTOR2I( 60, 0 ) );
147 poly.Append( VECTOR2I( 60, 60 ) );
148 poly.Append( VECTOR2I( 0, 60 ) );
149 poly.NewHole();
150 poly.Append( VECTOR2I( 20, 20 ), 0, 0 );
151 poly.Append( VECTOR2I( 40, 20 ), 0, 0 );
152 poly.Append( VECTOR2I( 30, 40 ), 0, 0 );
153
154 EDIT_POINTS points( nullptr );
156
157 EDIT_LINE& line = points.Line( 4 );
158 BOOST_REQUIRE_EQUAL( line.GetOrigin().GetPosition(), VECTOR2I( 20, 20 ) );
159 BOOST_REQUIRE_EQUAL( line.GetEnd().GetPosition(), VECTOR2I( 40, 20 ) );
160
161 line.SetDragPolicy( std::make_unique<POLYGON_EDGE_DRAG_POLICY>( line, points ) );
162 line.SetPosition( VECTOR2I( 30, 30 ) );
163
165 line.ApplyRelation( grid );
166
167 BOOST_CHECK_EQUAL( line.GetOrigin().GetPosition(), VECTOR2I( 25, 30 ) );
168 BOOST_CHECK_EQUAL( line.GetEnd().GetPosition(), VECTOR2I( 35, 30 ) );
169}
170
171
173{
174public:
177};
178
179
181{
182public:
183 void Push( const wxString&, int ) override {}
184 void Revert() override {}
185 void RevertToCheckpoint( int aCheckpoint ) override {}
186 EDA_ITEM* ResolveItem( KIID& aID ) override { return nullptr; }
187
188protected:
189 EDA_ITEM* undoLevelItem( EDA_ITEM* aItem ) const override { return aItem; }
190 EDA_ITEM* makeImage( EDA_ITEM* aItem ) const override { return aItem->Clone(); }
191};
192
193
194BOOST_AUTO_TEST_CASE( CollapsedPolygonKeepsItsCorners )
195{
196 SHAPE_POLY_SET poly;
197
198 poly.NewOutline();
199 poly.Append( VECTOR2I( 20, 30 ) );
200 poly.Append( VECTOR2I( 50, 30 ) );
201 poly.Append( VECTOR2I( 35, 55 ) );
202
203 for( int ii = 0; ii < poly.TotalVertices(); ++ii )
204 poly.SetVertex( ii, VECTOR2I( 35, 55 ) );
205
206 POLY_SHAPE_MOCK shape;
207 shape.SetPolyShape( poly );
208
209 EDIT_POINTS points( nullptr );
210 EDA_POLYGON_POINT_EDIT_BEHAVIOR behavior( shape );
211 POLY_EDIT_COMMIT commit;
212
213 behavior.FinalizeItem( points, commit );
214
215 BOOST_CHECK_MESSAGE( shape.GetPolyShape().TotalVertices() == 3,
216 "finalizing a collapsed polygon stripped every corner" );
217}
218
219
COMMIT()
Definition commit.cpp:30
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition eda_item.cpp:278
"Standard" polygon editing behavior for EDA_SHAPE polygons.
void FinalizeItem(EDIT_POINTS &aPoints, COMMIT &aCommit) override
Finalize the edit operation.
SHAPE_POLY_SET & GetPolyShape()
EDA_SHAPE(SHAPE_T aType, int aLineWidth, FILL_T aFill)
Definition eda_shape.cpp:56
virtual void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition eda_shape.h:528
Represent a line connecting two EDIT_POINTs.
virtual void SetPosition(const VECTOR2I &aPosition) override
Return coordinates of an EDIT_POINT.
POLYGON_EDGE_DRAG_POLICY * GetDragPolicy() const
void SetRelation(std::unique_ptr< EDIT_RELATION > aRelation)
Set a relation for an EDIT_LINE.
virtual void ApplyRelation(const GRID_HELPER &aGrid) override
Correct coordinates of an EDIT_POINT by applying its relation.
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
void SetDragPolicy(std::unique_ptr< POLYGON_EDGE_DRAG_POLICY > aPolicy)
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
void AddPoint(const EDIT_POINT &aPoint)
Add an EDIT_POINT.
EDIT_LINE & Line(unsigned int aIndex)
EDIT_POINT & Point(unsigned int aIndex)
void AddLine(const EDIT_LINE &aLine)
Adds an EDIT_LINE.
Represent a single point that can be used for modifying items.
Definition edit_points.h:44
int GetY() const
Return Y coordinate of an EDIT_POINT.
Definition edit_points.h:93
EDIT_RELATION * GetRelation() const
Return the relation imposed on an EDIT_POINT.
virtual void ApplyRelation(const GRID_HELPER &aGrid)
Correct coordinates of an EDIT_POINT by applying its relation.
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition edit_points.h:69
int GetX() const
Return X coordinate of an EDIT_POINT.
Definition edit_points.h:85
void SetRelation(std::unique_ptr< EDIT_RELATION > aRelation)
Set a relation for an EDIT_POINT.
static std::unique_ptr< EDIT_RELATION > PerpendicularTranslation(const EDIT_LINE &aLine)
static std::unique_ptr< EDIT_RELATION > SameX(const EDIT_POINT &aReference)
EDIT_RELATION_KIND Kind() const
Definition kiid.h:46
POLYGON_LINE_MODE GetMode() const
static void BuildForPolyOutline(EDIT_POINTS &aPoints, const SHAPE_POLY_SET &aOutline)
Build the edit points for the given polygon outline.
void Push(const wxString &, int) override
Execute the changes.
void Revert() override
Revert the commit by restoring the modified items state.
void RevertToCheckpoint(int aCheckpoint) override
EDA_ITEM * undoLevelItem(EDA_ITEM *aItem) const override
EDA_ITEM * ResolveItem(KIID &aID) override
Search for an item in this commit that matches the provided KIID.
EDA_ITEM * makeImage(EDA_ITEM *aItem) const override
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)
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.
FILL_T
Definition eda_fill.h:29
@ NO_FILL
Definition eda_fill.h:30
SHAPE_T
Definition eda_shape.h:54
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:437
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(PointRelationsExposeTypedSemantics)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
VECTOR2I end
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708