KiCad PCB EDA Suite
edit_constraints.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 (C) 2014 CERN
5 * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27#include "tool/edit_points.h"
28
29#include <geometry/seg.h>
30#include <trigo.h>
31
33
34
35void EC_VERTICAL::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
36{
37 VECTOR2I point = aHandle.GetPosition();
38
39 if( aHandle.GetGridConstraint() == SNAP_TO_GRID )
40 point = aGrid.AlignGrid( point );
41
42 point.x = m_constrainer.GetPosition().x;
43 aHandle.SetPosition( point );
44}
45
46
47void EC_HORIZONTAL::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
48{
49 VECTOR2I point = aHandle.GetPosition();
50
51 if( aHandle.GetGridConstraint() == SNAP_TO_GRID )
52 point = aGrid.AlignGrid( point );
53
54 point.y = m_constrainer.GetPosition().y;
55 aHandle.SetPosition( point );
56}
57
58
59void EC_45DEGREE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
60{
61 VECTOR2I lineVector( aHandle.GetPosition() - m_constrainer.GetPosition() );
62 VECTOR2I newLineVector = GetVectorSnapped45( lineVector );
63
64 if( aHandle.GetGridConstraint() == SNAP_TO_GRID
65 && ( newLineVector.x == 0 || newLineVector.y == 0 ) )
66 {
67 VECTOR2I snap = aGrid.AlignGrid( m_constrainer.GetPosition() + newLineVector );
68
69 if( newLineVector.x == 0 )
70 aHandle.SetPosition( VECTOR2I( m_constrainer.GetPosition().x, snap.y ) );
71 else
72 aHandle.SetPosition( VECTOR2I( snap.x, m_constrainer.GetPosition().y ) );
73 }
74 else
75 {
76 aHandle.SetPosition( m_constrainer.GetPosition() + newLineVector );
77 }
78}
79
80
81EC_LINE::EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
82 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
83 m_constrainer( aConstrainer )
84{
86}
87
88
89void EC_LINE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
90{
92
93 if( aHandle.GetGridConstraint() == SNAP_TO_GRID
94 && ( m_line.x == 0 || m_line.y == 0 ) )
95 {
96 VECTOR2I snappedHandle = aGrid.AlignGrid( aHandle.GetPosition() );
97
98 if( m_line.x == 0 )
99 aHandle.SetPosition( VECTOR2I( aHandle.GetPosition().x, snappedHandle.y ) );
100 else
101 aHandle.SetPosition( VECTOR2I( snappedHandle.x, aHandle.GetPosition().y ) );
102 }
103
104 SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
105
106 if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
107 aHandle.SetPosition( *intersect );
108}
109
110
111void EC_CIRCLE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
112{
113 VECTOR2I centerToEnd = m_end.GetPosition() - m_center.GetPosition();
114 VECTOR2I centerToPoint = aHandle.GetPosition() - m_center.GetPosition();
115
116 int radius = centerToEnd.EuclideanNorm();
117 EDA_ANGLE angle( centerToPoint );
118
119 VECTOR2I newLine( radius, 0 );
120 RotatePoint( newLine, -angle );
121
122 aHandle.SetPosition( m_center.GetPosition() + newLine );
123}
124
125
127 EDIT_CONSTRAINT<EDIT_LINE>( aLine ),
128 m_colinearConstraint( nullptr ),
129 m_editPoints( aPoints )
130{
131 // Dragged segment endings
132 EDIT_POINT& origin = aLine.GetOrigin();
133 EDIT_POINT& end = aLine.GetEnd();
134
135 // Previous and next points, to make constraining lines (adjacent to the dragged line)
136 EDIT_POINT& prevOrigin = *aPoints.Previous( origin, false );
137 EDIT_POINT& nextEnd = *aPoints.Next( end, false );
138
139 // Constraints for segments adjacent to the dragged one
140 m_originSideConstraint = new EC_LINE( origin, prevOrigin );
141 m_endSideConstraint = new EC_LINE( end, nextEnd );
142
143 // Store the current vector of the line
144 m_draggedVector = end.GetPosition() - origin.GetPosition();
145
146 // Check for colinearity
147 SEG originSide( origin.GetPosition(), prevOrigin.GetPosition() );
148 SEG endSide( end.GetPosition(), nextEnd.GetPosition() );
149 SEG dragged( origin.GetPosition(), end.GetPosition() );
150
151 if( dragged.Collinear( originSide ) )
153 else if( dragged.Collinear( endSide ) )
155}
156
157
159{
161 delete m_endSideConstraint;
162 // m_colinearConstraint should not be freed, it is a pointer to one of the above
163}
164
165
166void EC_CONVERGING::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
167{
168 // The dragged segment endpoints
169 EDIT_POINT& origin = aHandle.GetOrigin();
170 EDIT_POINT& end = aHandle.GetEnd();
171
173 {
174 m_colinearConstraint->Apply( origin, aGrid );
175 m_colinearConstraint->Apply( end, aGrid );
176 }
177
178 // The dragged segment
179 SEG dragged( origin.GetPosition(), origin.GetPosition() + m_draggedVector );
180
181 // Do not allow points on the adjacent segments move freely
183 m_endSideConstraint->Apply( aGrid );
184
185 EDIT_POINT& prevOrigin = *m_editPoints.Previous( origin, false );
186 EDIT_POINT& nextEnd = *m_editPoints.Next( end, false );
187
188 // Two segments adjacent to the dragged segment
189 SEG originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
190 SEG endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
191
192 // First intersection point (dragged segment against origin side)
193 if( OPT_VECTOR2I originIntersect = dragged.IntersectLines( originSide ) )
194 origin.SetPosition( *originIntersect );
195
196 // Second intersection point (dragged segment against end side)
197 if( OPT_VECTOR2I endIntersect = dragged.IntersectLines( endSide ) )
198 end.SetPosition( *endIntersect );
199
200 // Check if adjacent segments intersect (did we dragged the line to the point that it may
201 // create a selfintersecting polygon?)
202 originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
203 endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
204
205 if( OPT_VECTOR2I originEndIntersect = endSide.Intersect( originSide ) )
206 {
207 // Triangle intersect by definition
208 if( m_editPoints.LinesSize() > 3 )
209 {
210 origin.SetPosition( *originEndIntersect );
211 end.SetPosition( *originEndIntersect );
212 }
213 }
214}
215
216
218 EDIT_CONSTRAINT<EDIT_LINE>( aLine )
219{
220 m_mid = aLine.GetPosition();
221 m_line = ( aLine.GetEnd().GetPosition() - aLine.GetOrigin().GetPosition() ).Perpendicular();
222}
223
224
225void EC_PERPLINE::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
226{
227 SEG main( m_mid, m_mid + m_line );
228 SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
229
230 if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
231 aHandle.SetPosition( *intersect );
232
233 VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
234
235 aHandle.GetOrigin().SetPosition( aHandle.GetOrigin().GetPosition() );
236 aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
237}
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
virtual void Apply(EDIT_POINT &aHandle, const GRID_HELPER &aGrid) override
Correct coordinates of the constrained edit handle.
const EDIT_POINT & m_end
const EDIT_POINT & m_center
< Point that imposes the constraint (center of the circle).
virtual void Apply(EDIT_POINT &aHandle, const GRID_HELPER &aGrid) override
Correct coordinates of the constrained edit handle.
EDIT_CONSTRAINT< EDIT_POINT > * m_endSideConstraint
Additional constraint, applied when at least two points are collinear.
EC_CONVERGING(EDIT_LINE &aLine, EDIT_POINTS &aPoints)
virtual ~EC_CONVERGING()
Correct coordinates of the constrained edit handle.
EDIT_CONSTRAINT< EDIT_POINT > * m_colinearConstraint
EDIT_POINTS instance that stores currently modified lines.
VECTOR2I m_draggedVector
EDIT_CONSTRAINT< EDIT_POINT > * m_originSideConstraint
< Constraint for origin side segment.
EDIT_POINTS & m_editPoints
Vector that represents the initial direction of the dragged segment.
virtual void Apply(EDIT_LINE &aHandle, const GRID_HELPER &aGrid) override
Correct coordinates of the constrained edit handle.
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
virtual void Apply(EDIT_POINT &aHandle, const GRID_HELPER &aGrid) override
Correct coordinates of the constrained edit handle.
EDIT_CONSTRAINT that imposes a constraint that a point has to lie on a line (determined by 2 points).
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
VECTOR2I m_line
Vector representing the constraining line.
virtual void Apply(EDIT_POINT &aHandle, const GRID_HELPER &aGrid) override
Correct coordinates of the constrained edit handle.
EC_LINE(EDIT_POINT &aConstrained, const EDIT_POINT &aConstrainer)
Correct coordinates of the constrained edit handle.
EC_PERPLINE(EDIT_LINE &aLine)
virtual void Apply(EDIT_LINE &aHandle, const GRID_HELPER &aGrid) override
Correct coordinates of the constrained edit handle.
virtual void Apply(EDIT_POINT &aHandle, const GRID_HELPER &aGrid) override
Correct coordinates of the constrained edit handle.
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
Describe constraints between two edit handles.
EDIT_POINT & m_constrained
Point that is constrained by rules implemented by Apply().
virtual void Apply(EDIT_TYPE &aHandle, const GRID_HELPER &aGrid)=0
Correct coordinates of the constrained edit handle.
Represent a line connecting two EDIT_POINTs.
Definition: edit_points.h:221
virtual void SetPosition(const VECTOR2I &aPosition) override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:242
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
Definition: edit_points.h:295
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
Definition: edit_points.h:282
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:236
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
Definition: edit_points.h:328
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is after the given point in the list.
EDIT_POINT * Next(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is before the given point in the list.
unsigned int LinesSize() const
Return number of stored EDIT_LINEs.
Definition: edit_points.h:491
Represent a single point that can be used for modifying items.
Definition: edit_points.h:48
virtual void SetPosition(const VECTOR2I &aPosition)
Set new coordinates for an EDIT_POINT.
Definition: edit_points.h:107
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:71
GRID_CONSTRAINT_TYPE GetGridConstraint() const
Definition: edit_points.h:178
VECTOR2I AlignGrid(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:91
Definition: seg.h:42
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition: seg.cpp:196
bool Collinear(const SEG &aSeg) const
Check if segment aSeg lies on the same line as (this).
Definition: seg.h:269
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Compute the intersection point of lines passing through ends of (this) and aSeg.
Definition: seg.h:210
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:265
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:279
main()
@ SNAP_TO_GRID
a few functions useful in geometry calculations.
VECTOR2< T > GetVectorSnapped45(const VECTOR2< T > &aVec, bool only45=false)
Snap a vector onto the nearest 0, 45 or 90 degree line.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
static bool intersect(const SEGMENT_WITH_NORMALS &aSeg, const SFVEC2F &aStart, const SFVEC2F &aEnd)
Definition: polygon_2d.cpp:273
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39
constexpr int delta
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590