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, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef EDIT_CONSTRAINTS_H_
28#define EDIT_CONSTRAINTS_H_
29
30#include <math/vector2d.h>
31#include <tool/grid_helper.h>
32#include <functional>
33
34class EDIT_POINT;
35class EDIT_LINE;
36class EDIT_POINTS;
37
38
48
49
51{
54 SNAP_BY_GRID // Keep it on grid if it started on grid (treat x and y independently)
55};
56
57
64
65
72template<class EDIT_TYPE>
74{
75public:
79 EDIT_CONSTRAINT( EDIT_TYPE& aConstrained ) :
80 m_constrained( aConstrained )
81 {};
82
83 virtual ~EDIT_CONSTRAINT() {};
84
88 virtual void Apply( EDIT_TYPE& aHandle, const GRID_HELPER& aGrid ) = 0;
89
93 void Apply( const GRID_HELPER& aGrid )
94 {
95 Apply( m_constrained, aGrid );
96 }
97
98protected:
99 EDIT_TYPE& m_constrained;
100};
101
102
106class EC_VERTICAL : public EDIT_CONSTRAINT<EDIT_POINT>
107{
108public:
113 EC_VERTICAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
114 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
115 m_constrainer( aConstrainer )
116 {}
117
119 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
120
121private:
123};
124
125
129class EC_HORIZONTAL : public EDIT_CONSTRAINT<EDIT_POINT>
130{
131public:
136 EC_HORIZONTAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
137 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
138 m_constrainer( aConstrainer )
139 {}
140
142 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
143
144private:
146};
147
148
153class EC_45DEGREE : public EDIT_CONSTRAINT<EDIT_POINT>
154{
155public:
160 EC_45DEGREE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
161 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
162 m_constrainer( aConstrainer )
163 {}
164
166 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
167
168private:
170};
171
172
177class EC_90DEGREE : public EDIT_CONSTRAINT<EDIT_POINT>
178{
179public:
184 EC_90DEGREE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
185 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
186 m_constrainer( aConstrainer )
187 {};
188
190 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
191
192private:
194};
195
200class EC_LINE : public EDIT_CONSTRAINT<EDIT_POINT>
201{
202public:
203 EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer );
204
206 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
207
208 const EDIT_POINT& GetConstrainer() const { return m_constrainer; }
209 VECTOR2I GetLineVector() const { return m_line; }
210
211private:
214};
215
216
220class EC_CIRCLE : public EDIT_CONSTRAINT<EDIT_POINT>
221{
222public:
228 EC_CIRCLE( EDIT_POINT& aConstrained, const EDIT_POINT& aCenter, const EDIT_POINT& aEnd ) :
229 EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
230 m_center( aCenter ),
231 m_end( aEnd )
232 {}
233
235 virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
236
237private:
240
243};
244
245
252class EC_CONVERGING : public EDIT_CONSTRAINT<EDIT_LINE>
253{
254public:
255 EC_CONVERGING( EDIT_LINE& aLine, EDIT_POINTS& aPoints,
257
258 virtual ~EC_CONVERGING();
259
261 virtual void Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid ) override;
262
264 POLYGON_LINE_MODE GetMode() const { return m_mode; }
265
267 void SetMode( POLYGON_LINE_MODE aMode ) { m_mode = aMode; }
268
269private:
271 void applyConverging( EDIT_LINE& aHandle );
272
274 void applyFixedLength( EDIT_LINE& aHandle );
275
278
280 std::unique_ptr<EDIT_CONSTRAINT<EDIT_POINT>> m_originSideConstraint;
281
283 std::unique_ptr<EDIT_CONSTRAINT<EDIT_POINT>> m_endSideConstraint;
284
288
291
294
297
300
303
307
311
314
317};
318
319
324class EC_PERPLINE : public EDIT_CONSTRAINT<EDIT_LINE>
325{
326public:
327
328 EC_PERPLINE( EDIT_LINE& aLine );
329
330 virtual ~EC_PERPLINE()
331 {}
332
334 virtual void Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid ) override;
335
336private:
339};
340
341#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.
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.
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:48
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:695