KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 // Used to align lines that are almost collinear
152 const int alignAngle = 10;
153
154 m_originCollinear = dragged.Angle( originSide ).AsDegrees() < alignAngle;
155 m_endCollinear = dragged.Angle( endSide ).AsDegrees() < alignAngle;
156
159 else if( m_endCollinear )
161}
162
163
165{
167 delete m_endSideConstraint;
168 // m_colinearConstraint should not be freed, it is a pointer to one of the above
169}
170
171
172void EC_CONVERGING::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
173{
174 // The dragged segment endpoints
175 EDIT_POINT& origin = aHandle.GetOrigin();
176 EDIT_POINT& end = aHandle.GetEnd();
177
179 {
180 m_colinearConstraint->Apply( origin, aGrid );
181 m_colinearConstraint->Apply( end, aGrid );
182 }
183
185 return;
186
187 // The dragged segment
188 SEG dragged( origin.GetPosition(), origin.GetPosition() + m_draggedVector );
189
190 // Do not allow points on the adjacent segments move freely
192 m_endSideConstraint->Apply( aGrid );
193
194 EDIT_POINT& prevOrigin = *m_editPoints.Previous( origin, false );
195 EDIT_POINT& nextEnd = *m_editPoints.Next( end, false );
196
197 // Two segments adjacent to the dragged segment
198 SEG originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
199 SEG endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
200
201 // First intersection point (dragged segment against origin side)
202 if( OPT_VECTOR2I originIntersect = dragged.IntersectLines( originSide ) )
203 origin.SetPosition( *originIntersect );
204
205 // Second intersection point (dragged segment against end side)
206 if( OPT_VECTOR2I endIntersect = dragged.IntersectLines( endSide ) )
207 end.SetPosition( *endIntersect );
208
209 // Check if adjacent segments intersect (did we dragged the line to the point that it may
210 // create a selfintersecting polygon?)
211 originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
212 endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
213
214 if( OPT_VECTOR2I originEndIntersect = endSide.Intersect( originSide ) )
215 {
216 // Triangle intersect by definition
217 if( m_editPoints.LinesSize() > 3 )
218 {
219 origin.SetPosition( *originEndIntersect );
220 end.SetPosition( *originEndIntersect );
221 }
222 }
223}
224
225
227 EDIT_CONSTRAINT<EDIT_LINE>( aLine )
228{
229 m_mid = aLine.GetPosition();
230 m_line = ( aLine.GetEnd().GetPosition() - aLine.GetOrigin().GetPosition() ).Perpendicular();
231}
232
233
234void EC_PERPLINE::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
235{
236 SEG main( m_mid, m_mid + m_line );
237 SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
238
239 if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
240 aHandle.SetPosition( *intersect );
241
242 VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
243
244 aHandle.GetOrigin().SetPosition( aHandle.GetOrigin().GetPosition() );
245 aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
246}
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
Flags to indicate when dragged and neighbouring lines are (almost) collinear.
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.
double AsDegrees() const
Definition: eda_angle.h:113
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:223
virtual void SetPosition(const VECTOR2I &aPosition) override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:244
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
Definition: edit_points.h:297
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
Definition: edit_points.h:284
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:238
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
Definition: edit_points.h:353
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:532
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
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:70
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:254
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Compute the intersection point of lines passing through ends of (this) and aSeg.
Definition: seg.h:220
EDA_ANGLE Angle(const SEG &aOther) const
Determine the smallest angle between two segments.
Definition: seg.cpp:97
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:283
constexpr VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:314
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 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)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:229
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691