KiCad PCB EDA Suite
Loading...
Searching...
No Matches
edit_constraints.h
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 *
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef EDIT_CONSTRAINTS_H_
24#define EDIT_CONSTRAINTS_H_
25
26#include <math/vector2d.h>
27#include <tool/grid_helper.h>
28#include <functional>
29
30class EDIT_POINT;
31class EDIT_LINE;
32class EDIT_POINTS;
33
34
44
45
47{
50 SNAP_BY_GRID // Keep it on grid if it started on grid (treat x and y independently)
51};
52
53
60
61
68template<class EDIT_TYPE>
70{
71public:
75 EDIT_CONSTRAINT( EDIT_TYPE& aConstrained ) :
76 m_constrained( aConstrained )
77 {};
78
79 virtual ~EDIT_CONSTRAINT() {};
80
84 virtual void Apply( EDIT_TYPE& aHandle, const GRID_HELPER& aGrid ) = 0;
85
89 void Apply( const GRID_HELPER& aGrid )
90 {
91 Apply( m_constrained, aGrid );
92 }
93
94protected:
95 EDIT_TYPE& m_constrained;
96};
97
98
102class EC_VERTICAL : public EDIT_CONSTRAINT<EDIT_POINT>
103{
104public:
109 EC_VERTICAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
110 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
111 m_constrainer( aConstrainer )
112 {}
113
115 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
116
117private:
119};
120
121
125class EC_HORIZONTAL : public EDIT_CONSTRAINT<EDIT_POINT>
126{
127public:
132 EC_HORIZONTAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
133 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
134 m_constrainer( aConstrainer )
135 {}
136
138 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
139
140private:
142};
143
144
149class EC_45DEGREE : public EDIT_CONSTRAINT<EDIT_POINT>
150{
151public:
156 EC_45DEGREE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
157 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
158 m_constrainer( aConstrainer )
159 {}
160
162 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
163
164private:
166};
167
168
173class EC_90DEGREE : public EDIT_CONSTRAINT<EDIT_POINT>
174{
175public:
180 EC_90DEGREE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
181 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
182 m_constrainer( aConstrainer )
183 {};
184
186 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
187
188private:
190};
191
196class EC_LINE : public EDIT_CONSTRAINT<EDIT_POINT>
197{
198public:
199 EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer );
200
202 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
203
204 const EDIT_POINT& GetConstrainer() const { return m_constrainer; }
205 VECTOR2I GetLineVector() const { return m_line; }
206
207private:
210};
211
212
216class EC_CIRCLE : public EDIT_CONSTRAINT<EDIT_POINT>
217{
218public:
224 EC_CIRCLE( EDIT_POINT& aConstrained, const EDIT_POINT& aCenter, const EDIT_POINT& aEnd ) :
225 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
226 m_center( aCenter ),
227 m_end( aEnd )
228 {}
229
231 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
232
233private:
236
239};
240
241
248class EC_CONVERGING : public EDIT_CONSTRAINT<EDIT_LINE>
249{
250public:
251 EC_CONVERGING( EDIT_LINE& aLine, EDIT_POINTS& aPoints,
253
254 virtual ~EC_CONVERGING();
255
257 virtual void Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid ) override;
258
260 POLYGON_LINE_MODE GetMode() const { return m_mode; }
261
263 void SetMode( POLYGON_LINE_MODE aMode ) { m_mode = aMode; }
264
266 const VECTOR2I& GetOriginalCenter() const { return m_originalCenter; }
267
270 const VECTOR2I& GetPerpVector() const { return m_perpVector; }
271
272private:
274 void applyConverging( EDIT_LINE& aHandle );
275
277 void applyFixedLength( EDIT_LINE& aHandle );
278
281
283 std::unique_ptr<EDIT_CONSTRAINT<EDIT_POINT>> m_originSideConstraint;
284
286 std::unique_ptr<EDIT_CONSTRAINT<EDIT_POINT>> m_endSideConstraint;
287
291
294
297
300
303
306
310
314
317
320};
321
322
327class EC_PERPLINE : public EDIT_CONSTRAINT<EDIT_LINE>
328{
329public:
330
331 EC_PERPLINE( EDIT_LINE& aLine );
332
333 virtual ~EC_PERPLINE()
334 {}
335
337 virtual void Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid ) override;
338
339private:
342};
343
344#endif /* EDIT_CONSTRAINTS_H_ */
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.
EC_45DEGREE(EDIT_POINT &aConstrained, const EDIT_POINT &aConstrainer)
EC_90DEGREE(EDIT_POINT &aConstrained, const EDIT_POINT &aConstrainer)
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).
EC_CIRCLE(EDIT_POINT &aConstrained, const EDIT_POINT &aCenter, const EDIT_POINT &aEnd)
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.
const VECTOR2I & GetPerpVector() const
Perpendicular direction of motion for the dragged line, captured at drag start.
POLYGON_LINE_MODE GetMode() const
Get the current constraint mode.
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.
const VECTOR2I & GetOriginalCenter() const
Original center of the dragged line, captured at drag start.
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.
void SetMode(POLYGON_LINE_MODE aMode)
Set the constraint mode (allows switching between converging and fixed-length)
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.
EC_HORIZONTAL(EDIT_POINT &aConstrained, const EDIT_POINT &aConstrainer)
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.
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.
const EDIT_POINT & GetConstrainer() const
EC_LINE(EDIT_POINT &aConstrained, const EDIT_POINT &aConstrainer)
virtual ~EC_PERPLINE()
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.
EC_VERTICAL(EDIT_POINT &aConstrained, const EDIT_POINT &aConstrainer)
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
void Apply(const GRID_HELPER &aGrid)
Correct coordinates of the constrained edit handle.
EDIT_TYPE & m_constrained
Point that is constrained by rules implemented by Apply().
EDIT_CONSTRAINT(EDIT_TYPE &aConstrained)
virtual void Apply(EDIT_TYPE &aHandle, const GRID_HELPER &aGrid)=0
Correct coordinates of the constrained edit handle.
virtual ~EDIT_CONSTRAINT()
Represent a line connecting two EDIT_POINTs.
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:44
SNAP_CONSTRAINT_TYPE
@ ALL_LAYERS
@ OBJECT_LAYERS
@ IGNORE_SNAPS
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.
GRID_CONSTRAINT_TYPE
@ IGNORE_GRID
@ SNAP_BY_GRID
@ SNAP_TO_GRID
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683