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>
25
26
27BOOST_AUTO_TEST_SUITE( EditRelations )
28
29
30BOOST_AUTO_TEST_CASE( PointRelationsExposeTypedSemantics )
31{
32 EDIT_POINT constrained( VECTOR2I( 30, 40 ) );
33 EDIT_POINT reference( VECTOR2I( 10, 20 ) );
34
35 constrained.SetRelation( EDIT_RELATION::SameX( reference ) );
36
37 BOOST_REQUIRE( constrained.GetRelation() );
38 BOOST_CHECK( constrained.GetRelation()->Kind() == EDIT_RELATION_KIND::SAME_X );
39
41 constrained.ApplyRelation( grid );
42 BOOST_CHECK_EQUAL( constrained.GetPosition(), VECTOR2I( 10, 40 ) );
43}
44
45
46BOOST_AUTO_TEST_CASE( PerpendicularLinePreservesDirection )
47{
48 EDIT_POINT origin( VECTOR2I( 0, 0 ) );
49 EDIT_POINT end( VECTOR2I( 20, 0 ) );
50 EDIT_LINE line( origin, end );
51
53 line.SetPosition( VECTOR2I( 17, 9 ) );
54
56 line.ApplyRelation( grid );
57
58 BOOST_CHECK_EQUAL( end.GetPosition() - origin.GetPosition(), VECTOR2I( 20, 0 ) );
59 BOOST_CHECK_EQUAL( origin.GetY(), end.GetY() );
60 BOOST_CHECK_LE( std::abs( line.GetX() - 10 ), 1 );
61}
62
63
64BOOST_AUTO_TEST_CASE( PolygonFixedLengthPreservesEndpoints )
65{
66 EDIT_POINTS points( nullptr );
67 points.AddPoint( VECTOR2I( 0, 10 ) );
68 points.AddPoint( VECTOR2I( 0, 0 ) );
69 points.AddPoint( VECTOR2I( 20, 0 ) );
70 points.AddPoint( VECTOR2I( 20, 10 ) );
71 points.AddLine( points.Point( 0 ), points.Point( 1 ) );
72 points.AddLine( points.Point( 1 ), points.Point( 2 ) );
73 points.AddLine( points.Point( 2 ), points.Point( 3 ) );
74 points.AddLine( points.Point( 3 ), points.Point( 0 ) );
75
76 EDIT_LINE& line = points.Line( 1 );
77 line.SetDragPolicy( std::make_unique<POLYGON_EDGE_DRAG_POLICY>( line, points, POLYGON_LINE_MODE::FIXED_LENGTH ) );
78 line.SetPosition( VECTOR2I( 10, 5 ) );
79
81 line.ApplyRelation( grid );
82
84 BOOST_CHECK( line.GetDragPolicy()->GetMode() == POLYGON_LINE_MODE::FIXED_LENGTH );
85 BOOST_CHECK_EQUAL( line.GetEnd().GetPosition() - line.GetOrigin().GetPosition(), VECTOR2I( 20, 0 ) );
86 BOOST_CHECK_EQUAL( line.GetOrigin().GetY(), line.GetEnd().GetY() );
87}
88
89
90BOOST_AUTO_TEST_CASE( PolygonConvergenceClampsAtAdjacentIntersection )
91{
92 EDIT_POINTS points( nullptr );
93 points.AddPoint( VECTOR2I( 10, 10 ) );
94 points.AddPoint( VECTOR2I( 0, 0 ) );
95 points.AddPoint( VECTOR2I( 20, 0 ) );
96 points.AddPoint( VECTOR2I( 10, 10 ) );
97 points.AddLine( points.Point( 0 ), points.Point( 1 ) );
98 points.AddLine( points.Point( 1 ), points.Point( 2 ) );
99 points.AddLine( points.Point( 2 ), points.Point( 3 ) );
100 points.AddLine( points.Point( 3 ), points.Point( 0 ) );
101
102 EDIT_LINE& line = points.Line( 1 );
103 line.SetDragPolicy( std::make_unique<POLYGON_EDGE_DRAG_POLICY>( line, points ) );
104 line.SetPosition( VECTOR2I( 10, 20 ) );
105
107 line.ApplyRelation( grid );
108
109 BOOST_CHECK_EQUAL( line.GetOrigin().GetPosition(), VECTOR2I( 10, 10 ) );
110 BOOST_CHECK_EQUAL( line.GetEnd().GetPosition(), VECTOR2I( 10, 10 ) );
111}
112
113
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:92
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:68
int GetX() const
Return X coordinate of an EDIT_POINT.
Definition edit_points.h:84
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
POLYGON_LINE_MODE GetMode() const
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
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:683