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 The 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#include <math/vector2d.h>
34#include <math/util.h>
35
36
37void EC_VERTICAL::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
38{
39 VECTOR2I point = aHandle.GetPosition();
40
41 if( aHandle.GetGridConstraint() == SNAP_TO_GRID )
42 point = aGrid.AlignGrid( point );
43
44 point.x = m_constrainer.GetPosition().x;
45 aHandle.SetPosition( point );
46}
47
48
49void EC_HORIZONTAL::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
50{
51 VECTOR2I point = aHandle.GetPosition();
52
53 if( aHandle.GetGridConstraint() == SNAP_TO_GRID )
54 point = aGrid.AlignGrid( point );
55
56 point.y = m_constrainer.GetPosition().y;
57 aHandle.SetPosition( point );
58}
59
60
61void EC_45DEGREE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
62{
63 VECTOR2I lineVector( aHandle.GetPosition() - m_constrainer.GetPosition() );
64 VECTOR2I newLineVector = GetVectorSnapped45( lineVector );
65
66 if( aHandle.GetGridConstraint() == SNAP_TO_GRID
67 && ( newLineVector.x == 0 || newLineVector.y == 0 ) )
68 {
69 VECTOR2I snap = aGrid.AlignGrid( m_constrainer.GetPosition() + newLineVector );
70
71 if( newLineVector.x == 0 )
72 aHandle.SetPosition( VECTOR2I( m_constrainer.GetPosition().x, snap.y ) );
73 else
74 aHandle.SetPosition( VECTOR2I( snap.x, m_constrainer.GetPosition().y ) );
75 }
76 else
77 {
78 aHandle.SetPosition( m_constrainer.GetPosition() + newLineVector );
79 }
80}
81
82
83void EC_90DEGREE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
84{
85 VECTOR2I lineVector( aHandle.GetPosition() - m_constrainer.GetPosition() );
86 VECTOR2I newLineVector = GetVectorSnapped90( lineVector );
87
88 if( aHandle.GetGridConstraint() == SNAP_TO_GRID )
89 {
90 VECTOR2I snap = aGrid.AlignGrid( m_constrainer.GetPosition() + newLineVector );
91
92 if( newLineVector.x == 0 )
93 aHandle.SetPosition( VECTOR2I( m_constrainer.GetPosition().x, snap.y ) );
94 else
95 aHandle.SetPosition( VECTOR2I( snap.x, m_constrainer.GetPosition().y ) );
96 }
97 else
98 {
99 aHandle.SetPosition( m_constrainer.GetPosition() + newLineVector );
100 }
101}
102
103EC_LINE::EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
104 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
105 m_constrainer( aConstrainer )
106{
107 m_line = m_constrained.GetPosition() - m_constrainer.GetPosition();
108}
109
110
111void EC_LINE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
112{
113 SEG main( m_constrainer.GetPosition(), m_constrainer.GetPosition() + m_line );
114
115 if( aHandle.GetGridConstraint() == SNAP_TO_GRID
116 && ( m_line.x == 0 || m_line.y == 0 ) )
117 {
118 VECTOR2I snappedHandle = aGrid.AlignGrid( aHandle.GetPosition() );
119
120 if( m_line.x == 0 )
121 aHandle.SetPosition( VECTOR2I( aHandle.GetPosition().x, snappedHandle.y ) );
122 else
123 aHandle.SetPosition( VECTOR2I( snappedHandle.x, aHandle.GetPosition().y ) );
124 }
125
126 SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
127
128 if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
129 aHandle.SetPosition( *intersect );
130}
131
132
133void EC_CIRCLE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
134{
135 VECTOR2I centerToEnd = m_end.GetPosition() - m_center.GetPosition();
136 VECTOR2I centerToPoint = aHandle.GetPosition() - m_center.GetPosition();
137
138 int radius = centerToEnd.EuclideanNorm();
139 EDA_ANGLE angle( centerToPoint );
140
141 VECTOR2I newLine( radius, 0 );
142 RotatePoint( newLine, -angle );
143
144 aHandle.SetPosition( m_center.GetPosition() + newLine );
145}
146
147
149 EDIT_CONSTRAINT<EDIT_LINE>( aLine ),
150 m_colinearConstraint( nullptr ),
151 m_editPoints( aPoints ),
152 m_prevOrigin( aPoints.Previous( aLine.GetOrigin(), false ) ),
153 m_nextEnd( aPoints.Next( aLine.GetEnd(), false ) )
154{
155 // Dragged segment endings
156 EDIT_POINT& origin = aLine.GetOrigin();
157 EDIT_POINT& end = aLine.GetEnd();
158
159 // Constraints for segments adjacent to the dragged one
160 m_originSideConstraint = std::make_unique<EC_LINE>( origin, *m_prevOrigin );
161 m_endSideConstraint = std::make_unique<EC_LINE>( end, *m_nextEnd );
162
163 // Store the current vector of the line
164 m_draggedVector = end.GetPosition() - origin.GetPosition();
165
166 // Check for colinearity
167 SEG originSide( origin.GetPosition(), m_prevOrigin->GetPosition() );
168 SEG endSide( end.GetPosition(), m_nextEnd->GetPosition() );
169 SEG dragged( origin.GetPosition(), end.GetPosition() );
170
171 // Used to align lines that are almost collinear
172 const int alignAngle = 10;
173
174 m_originCollinear = dragged.Angle( originSide ).AsDegrees() < alignAngle;
175 m_endCollinear = dragged.Angle( endSide ).AsDegrees() < alignAngle;
176
179 else if( m_endCollinear )
181
182 if( OPT_VECTOR2I intersect = originSide.IntersectLines( endSide ) )
184 else
186
188}
189
190
192{
193 // m_colinearConstraint should not be freed, it is a pointer to one of the above
194}
195
196
197void EC_CONVERGING::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
198{
199 VECTOR2I handlePos = aHandle.GetPosition();
200 VECTOR2I convToHandle = handlePos - m_convergencePoint;
201 double t = 0.0;
202
203 if( m_midVector.SquaredEuclideanNorm() )
204 t = double( convToHandle.Dot( m_midVector ) )
205 / double( m_midVector.SquaredEuclideanNorm() );
206
207 if( t < 0.0 )
208 t = 0.0;
209
211 VECTOR2I newCenter( KiROUND( newCenterD.x ), KiROUND( newCenterD.y ) );
212 aHandle.SetPosition( newCenter );
213
214 // The dragged segment endpoints
215 EDIT_POINT& origin = aHandle.GetOrigin();
216 EDIT_POINT& end = aHandle.GetEnd();
217
219 {
220 m_colinearConstraint->Apply( origin, aGrid );
221 m_colinearConstraint->Apply( end, aGrid );
222 }
223
225 return;
226
227 // The dragged segment
228 SEG dragged( origin.GetPosition(), origin.GetPosition() + m_draggedVector );
229
230 // Do not allow points on the adjacent segments move freely
231 m_originSideConstraint->Apply( aGrid );
232 m_endSideConstraint->Apply( aGrid );
233
234 // Two segments adjacent to the dragged segment
235 SEG originSide = SEG( origin.GetPosition(), m_prevOrigin->GetPosition() );
236 SEG endSide = SEG( end.GetPosition(), m_nextEnd->GetPosition() );
237
238 // First intersection point (dragged segment against origin side)
239 if( OPT_VECTOR2I originIntersect = dragged.IntersectLines( originSide ) )
240 origin.SetPosition( *originIntersect );
241
242 // Second intersection point (dragged segment against end side)
243 if( OPT_VECTOR2I endIntersect = dragged.IntersectLines( endSide ) )
244 end.SetPosition( *endIntersect );
245
246 // Check if adjacent segments intersect (did we dragged the line to the point that it may
247 // create a selfintersecting polygon?)
248 originSide = SEG( origin.GetPosition(), m_prevOrigin->GetPosition() );
249 endSide = SEG( end.GetPosition(), m_nextEnd->GetPosition() );
250
251 if( OPT_VECTOR2I originEndIntersect = endSide.Intersect( originSide ) )
252 {
253 // Triangle intersect by definition
254 if( m_editPoints.LinesSize() > 3 )
255 {
256 origin.SetPosition( *originEndIntersect );
257 end.SetPosition( *originEndIntersect );
258 }
259 }
260}
261
262
264 EDIT_CONSTRAINT<EDIT_LINE>( aLine )
265{
266 m_mid = aLine.GetPosition();
267 m_line = ( aLine.GetEnd().GetPosition() - aLine.GetOrigin().GetPosition() ).Perpendicular();
268}
269
270
271void EC_PERPLINE::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
272{
273 SEG main( m_mid, m_mid + m_line );
274 SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
275
276 if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
277 aHandle.SetPosition( *intersect );
278
279 VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
280
281 aHandle.GetOrigin().SetPosition( aHandle.GetOrigin().GetPosition() );
282 aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
283}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
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_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
Point that imposes the constraint (decides on the radius of the circle).
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.
std::unique_ptr< EDIT_CONSTRAINT< EDIT_POINT > > m_endSideConstraint
Constraint for end side segment.
EC_CONVERGING(EDIT_LINE &aLine, EDIT_POINTS &aPoints)
std::unique_ptr< EDIT_CONSTRAINT< EDIT_POINT > > m_originSideConstraint
Constraint for origin side segment.
EDIT_CONSTRAINT< EDIT_POINT > * m_colinearConstraint
Additional constraint, applied when at least two points are collinear.
VECTOR2I m_midVector
Vector from the convergence point to the mid-line point.
VECTOR2I m_draggedVector
Vector that represents the initial direction of the dragged segment.
EDIT_POINT * m_nextEnd
EDIT_POINT * m_prevOrigin
Previous and next points to keep drag endpoints fixed.
bool m_originCollinear
Flags to indicate when dragged and neighbouring lines are (almost) collinear.
EDIT_POINTS & m_editPoints
EDIT_POINTS instance that stores currently modified lines.
virtual void Apply(EDIT_LINE &aHandle, const GRID_HELPER &aGrid) override
Correct coordinates of the constrained edit handle.
VECTOR2I m_convergencePoint
Original convergence point of adjacent segments.
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_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)
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:116
EDIT_CONSTRAINT(EDIT_POINT &aConstrained)
Represent a line connecting two EDIT_POINTs.
virtual void SetPosition(const VECTOR2I &aPosition) override
Return coordinates of an EDIT_POINT.
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
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.
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition edit_points.h:72
GRID_CONSTRAINT_TYPE GetGridConstraint() const
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition grid_helper.h:79
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:437
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:102
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:283
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition vector2d.h:554
@ 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.
VECTOR2< T > GetVectorSnapped90(const VECTOR2< T > &aVec)
Snap a vector onto the nearest horizontal or vertical line.
static bool intersect(const SEGMENT_WITH_NORMALS &aSeg, const SFVEC2F &aStart, const SFVEC2F &aEnd)
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:39
int radius
VECTOR2I end
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:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694