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 POLYGON_LINE_MODE aMode ) :
150 EDIT_CONSTRAINT<EDIT_LINE>( aLine ),
151 m_mode( aMode ),
152 m_colinearConstraint( nullptr ),
153 m_editPoints( aPoints ),
154 m_prevOrigin( aPoints.Previous( aLine.GetOrigin(), false ) ),
155 m_nextEnd( aPoints.Next( aLine.GetEnd(), false ) )
156{
157 EDIT_POINT& origin = aLine.GetOrigin();
158 EDIT_POINT& end = aLine.GetEnd();
159
160 // Constraints for segments adjacent to the dragged one
161 m_originSideConstraint = std::make_unique<EC_LINE>( origin, *m_prevOrigin );
162 m_endSideConstraint = std::make_unique<EC_LINE>( end, *m_nextEnd );
163
164 // Store the current vector and center of the line
165 m_draggedVector = end.GetPosition() - origin.GetPosition();
167
168 // Perpendicular direction for constraining movement
169 m_perpVector = m_draggedVector.Perpendicular();
170
171 // Half-length for fixed-length mode
172 m_halfLength = m_draggedVector.EuclideanNorm() / 2.0;
173
174 // Check for colinearity
175 SEG originSide( origin.GetPosition(), m_prevOrigin->GetPosition() );
176 SEG endSide( end.GetPosition(), m_nextEnd->GetPosition() );
177 SEG dragged( origin.GetPosition(), end.GetPosition() );
178
179 const int alignAngle = 10;
180
181 m_originCollinear = dragged.Angle( originSide ).AsDegrees() < alignAngle;
182 m_endCollinear = dragged.Angle( endSide ).AsDegrees() < alignAngle;
183
186 else if( m_endCollinear )
188
189 if( OPT_VECTOR2I intersect = originSide.IntersectLines( endSide ) )
191 else
193
195}
196
197
199{
200 // m_colinearConstraint should not be freed, it is a pointer to one of the above
201}
202
203
204void EC_CONVERGING::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
205{
206 VECTOR2I handlePos = aHandle.GetPosition();
207
208 // Project the handle position onto the perpendicular line through the original center.
209 // This ensures the line always moves perpendicular to itself.
211 SEG toHandle( handlePos, handlePos + m_draggedVector );
212 VECTOR2I newCenter;
213
214 if( OPT_VECTOR2I intersect = perpLine.IntersectLines( toHandle ) )
215 newCenter = *intersect;
216 else
217 newCenter = handlePos;
218
219 // In converging mode, don't allow movement past the convergence point
221 {
223 VECTOR2I centerToNew = newCenter - m_originalCenter;
224
225 // Check if we've crossed past the convergence point
226 if( centerToConv.Dot( m_perpVector ) != 0 )
227 {
228 double t = double( centerToNew.Dot( m_perpVector ) )
229 / double( centerToConv.Dot( m_perpVector ) );
230
231 if( t > 1.0 )
232 newCenter = m_convergencePoint;
233 }
234 }
235
236 aHandle.SetPosition( newCenter );
237
239 applyFixedLength( aHandle );
240 else
241 applyConverging( aHandle );
242}
243
244
246{
247 EDIT_POINT& origin = aHandle.GetOrigin();
248 EDIT_POINT& end = aHandle.GetEnd();
249
251 {
253 {
254 GRID_HELPER dummyGrid;
255 m_colinearConstraint->Apply( origin, dummyGrid );
256 m_colinearConstraint->Apply( end, dummyGrid );
257 }
258
259 return;
260 }
261
262 // The dragged segment at new position (parallel to original)
263 VECTOR2I newCenter = aHandle.GetPosition();
264 SEG dragged( newCenter - m_draggedVector / 2, newCenter + m_draggedVector / 2 );
265
266 // Get the fixed directions of adjacent segments from the stored EC_LINE constraints.
267 // These directions were captured at drag start and don't change.
268 EC_LINE* originLine = static_cast<EC_LINE*>( m_originSideConstraint.get() );
269 EC_LINE* endLine = static_cast<EC_LINE*>( m_endSideConstraint.get() );
270
271 VECTOR2I originDir = originLine->GetLineVector();
272 VECTOR2I endDir = endLine->GetLineVector();
273
274 // Adjacent segments use fixed directions from the anchor points
275 SEG originSide( m_prevOrigin->GetPosition(),
276 m_prevOrigin->GetPosition() + originDir );
277 SEG endSide( m_nextEnd->GetPosition(),
278 m_nextEnd->GetPosition() + endDir );
279
280 // Find intersection of dragged line with origin side
281 if( OPT_VECTOR2I originIntersect = dragged.IntersectLines( originSide ) )
282 origin.SetPosition( *originIntersect );
283
284 // Find intersection of dragged line with end side
285 if( OPT_VECTOR2I endIntersect = dragged.IntersectLines( endSide ) )
286 end.SetPosition( *endIntersect );
287
288 // Check if adjacent segments would intersect (self-intersecting polygon)
289 originSide = SEG( origin.GetPosition(), m_prevOrigin->GetPosition() );
290 endSide = SEG( end.GetPosition(), m_nextEnd->GetPosition() );
291
292 if( OPT_VECTOR2I originEndIntersect = endSide.Intersect( originSide ) )
293 {
294 if( m_editPoints.LinesSize() > 3 )
295 {
296 origin.SetPosition( *originEndIntersect );
297 end.SetPosition( *originEndIntersect );
298 }
299 }
300}
301
302
304{
305 EDIT_POINT& origin = aHandle.GetOrigin();
306 EDIT_POINT& end = aHandle.GetEnd();
307
308 VECTOR2I newCenter = aHandle.GetPosition();
309
310 // Keep the line at its original length, centered on the new position
311 VECTOR2D unitDir = VECTOR2D( m_draggedVector );
312
313 if( unitDir.EuclideanNorm() > 0 )
314 unitDir = unitDir / unitDir.EuclideanNorm();
315
316 VECTOR2I newOrigin = newCenter - KiROUND( unitDir * m_halfLength );
317 VECTOR2I newEnd = newCenter + KiROUND( unitDir * m_halfLength );
318
319 origin.SetPosition( newOrigin );
320 end.SetPosition( newEnd );
321}
322
323
325 EDIT_CONSTRAINT<EDIT_LINE>( aLine )
326{
327 m_mid = aLine.GetPosition();
328 m_line = ( aLine.GetEnd().GetPosition() - aLine.GetOrigin().GetPosition() ).Perpendicular();
329}
330
331
332void EC_PERPLINE::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
333{
334 SEG main( m_mid, m_mid + m_line );
335 SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
336
337 if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
338 aHandle.SetPosition( *intersect );
339
340 VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
341
342 aHandle.GetOrigin().SetPosition( aHandle.GetOrigin().GetPosition() );
343 aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
344}
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.
double m_halfLength
Original half-length of the line (for fixed-length mode)
void applyConverging(EDIT_LINE &aHandle)
Apply converging mode: find intersections with adjacent lines.
std::unique_ptr< EDIT_CONSTRAINT< EDIT_POINT > > m_originSideConstraint
Constraint for origin side segment.
POLYGON_LINE_MODE m_mode
Constraint mode.
EC_CONVERGING(EDIT_LINE &aLine, EDIT_POINTS &aPoints, POLYGON_LINE_MODE aMode=POLYGON_LINE_MODE::CONVERGING)
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.
VECTOR2I m_originalCenter
Original center position of the line.
bool m_originCollinear
Flags to indicate when dragged and neighbouring lines are (almost) collinear.
VECTOR2I m_perpVector
Perpendicular direction to the dragged segment (for constraining movement)
EDIT_POINTS & m_editPoints
EDIT_POINTS instance that stores currently modified lines.
void applyFixedLength(EDIT_LINE &aHandle)
Apply fixed-length mode: maintain line length, adjust adjacent line angles.
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.
EDIT_CONSTRAINT that imposes a constraint that a point has to lie on a line (determined by 2 points).
VECTOR2I GetLineVector() const
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: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:446
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:111
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
POLYGON_LINE_MODE
Mode for polygon line edge constraints.
@ CONVERGING
Adjacent lines converge/diverge, dragged line length changes.
@ FIXED_LENGTH
Dragged line maintains its length, adjacent lines adjust angles.
@ 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