KiCad PCB EDA Suite
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 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 
26 #include "tool/edit_constraints.h"
27 #include "tool/edit_points.h"
28 
29 #include <geometry/seg.h>
30 #include <math/util.h> // for KiROUND
31 
32 #include <common.h>
33 
34 #include <utility>
36 
37 
39 {
40  VECTOR2I point = aHandle.GetPosition();
41  point.x = m_constrainer.GetPosition().x;
42  aHandle.SetPosition( point );
43 }
44 
45 
47 {
48  VECTOR2I point = aHandle.GetPosition();
49  point.y = m_constrainer.GetPosition().y;
50  aHandle.SetPosition( point );
51 }
52 
53 
55 {
56  VECTOR2I lineVector( aHandle.GetPosition() - m_constrainer.GetPosition() );
57  VECTOR2I newLineVector = GetVectorSnapped45( lineVector );
58 
59  aHandle.SetPosition( m_constrainer.GetPosition() + newLineVector );
60 }
61 
62 
63 EC_LINE::EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
64  EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
65 {
67 }
68 
69 
70 void EC_LINE::Apply( EDIT_POINT& aHandle )
71 {
73  SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
74 
75  if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
76  aHandle.SetPosition( *intersect );
77 }
78 
79 
80 void EC_CIRCLE::Apply( EDIT_POINT& aHandle )
81 {
82  VECTOR2I centerToEnd = m_end.GetPosition() - m_center.GetPosition();
83  VECTOR2I centerToPoint = aHandle.GetPosition() - m_center.GetPosition();
84 
85  int radius = centerToEnd.EuclideanNorm();
86  double angle = centerToPoint.Angle();
87 
88  VECTOR2I newLine( radius, 0 );
89  newLine = newLine.Rotate( angle );
90 
91  aHandle.SetPosition( m_center.GetPosition() + newLine );
92 }
93 
94 
96  EDIT_CONSTRAINT<EDIT_LINE>( aLine ),
97  m_colinearConstraint( nullptr ), m_editPoints( aPoints )
98 {
99  // Dragged segment endings
100  EDIT_POINT& origin = aLine.GetOrigin();
101  EDIT_POINT& end = aLine.GetEnd();
102 
103  // Previous and next points, to make constraining lines (adjacent to the dragged line)
104  EDIT_POINT& prevOrigin = *aPoints.Previous( origin, false );
105  EDIT_POINT& nextEnd = *aPoints.Next( end, false );
106 
107  // Constraints for segments adjacent to the dragged one
108  m_originSideConstraint = new EC_LINE( origin, prevOrigin );
109  m_endSideConstraint = new EC_LINE( end, nextEnd );
110 
111  // Store the current vector of the line
112  m_draggedVector = end.GetPosition() - origin.GetPosition();
113 
114  // Check for colinearity
115  SEG originSide( origin.GetPosition(), prevOrigin.GetPosition() );
116  SEG endSide( end.GetPosition(), nextEnd.GetPosition() );
117  SEG dragged( origin.GetPosition(), end.GetPosition() );
118 
119  if( dragged.Collinear( originSide ) )
121  else if( dragged.Collinear( endSide ) )
123 }
124 
125 
127 {
128  delete m_originSideConstraint;
129  delete m_endSideConstraint;
130  // m_colinearConstraint should not be freed, it is a pointer to one of the above
131 }
132 
133 
135 {
136  // The dragged segment endpoints
137  EDIT_POINT& origin = aHandle.GetOrigin();
138  EDIT_POINT& end = aHandle.GetEnd();
139 
141  {
142  m_colinearConstraint->Apply( origin );
143  m_colinearConstraint->Apply( end );
144  }
145 
146  // The dragged segment
147  SEG dragged( origin.GetPosition(), origin.GetPosition() + m_draggedVector );
148 
149  // Do not allow points on the adjacent segments move freely
152 
153  EDIT_POINT& prevOrigin = *m_editPoints.Previous( origin, false );
154  EDIT_POINT& nextEnd = *m_editPoints.Next( end, false );
155 
156  // Two segments adjacent to the dragged segment
157  SEG originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
158  SEG endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
159 
160  // First intersection point (dragged segment against origin side)
161  if( OPT_VECTOR2I originIntersect = dragged.IntersectLines( originSide ) )
162  origin.SetPosition( *originIntersect );
163 
164  // Second intersection point (dragged segment against end side)
165  if( OPT_VECTOR2I endIntersect = dragged.IntersectLines( endSide ) )
166  end.SetPosition( *endIntersect );
167 
168  // Check if adjacent segments intersect (did we dragged the line to the point that it may
169  // create a selfintersecting polygon?)
170  originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
171  endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
172 
173  if( OPT_VECTOR2I originEndIntersect = endSide.Intersect( originSide ) )
174  {
175  // Triangle intersect by definition
176  if( m_editPoints.LinesSize() > 3 )
177  {
178  origin.SetPosition( *originEndIntersect );
179  end.SetPosition( *originEndIntersect );
180  }
181  }
182 }
183 
184 
186  EDIT_CONSTRAINT<EDIT_LINE>( aLine ), m_snapFun( std::move(aSnapFun) )
187 {}
188 
189 
191 {
192  VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
193 
194  aHandle.GetOrigin().SetPosition( m_snapFun( aHandle.GetOrigin().GetPosition() ) );
195  aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
196 }
197 
198 
200  EDIT_CONSTRAINT<EDIT_LINE>( aLine )
201 {
202  m_mid = aLine.GetPosition();
203  m_line = ( aLine.GetEnd().GetPosition() - aLine.GetOrigin().GetPosition() ).Perpendicular();
204 }
205 
206 
208 {
209  SEG main( m_mid, m_mid + m_line );
210  SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
211 
212  if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
213  aHandle.SetPosition( *intersect );
214 
215  VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
216 
217  aHandle.GetOrigin().SetPosition( aHandle.GetOrigin().GetPosition() );
218  aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
219 }
void Apply()
Correct coordinates of the constrained edit handle.
VECTOR2I m_line
Vector representing the constraining line.
VECTOR2< T > GetVectorSnapped45(const VECTOR2< T > &aVec, bool only45=false)
Snap a vector onto the nearest 0, 45 or 90 degree line.
virtual ~EC_CONVERGING()
Correct coordinates of the constrained edit handle.
main()
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:314
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:154
const EDIT_POINT & m_center
< Point that imposes the constraint (center of the circle).
Represent a line connecting two EDIT_POINTs.
Definition: edit_points.h:215
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
EDIT_POINT * Next(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is before the given point in the list.
EDIT_CONSTRAINT< EDIT_POINT > * m_colinearConstraint
EDIT_POINTS instance that stores currently modified lines.
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:231
Definition: bitmap.cpp:64
static bool intersect(const SEGMENT_WITH_NORMALS &aSeg, const SFVEC2F &aStart, const SFVEC2F &aEnd)
Definition: polygon_2d.cpp:273
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
EC_LINE(EDIT_POINT &aConstrained, const EDIT_POINT &aConstrainer)
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).
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
Definition: edit_points.h:277
EC_SNAPLINE(EDIT_LINE &aLine, V2D_TRANSFORM_FUN aSnapFun)
std::function< VECTOR2D(const VECTOR2D &)> V2D_TRANSFORM_FUN
< Typedef for a function that determines snapping point.
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
Definition: edit_points.h:290
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:70
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:38
const EDIT_POINT & m_end
unsigned int LinesSize() const
Return number of stored EDIT_LINEs.
Definition: edit_points.h:486
EDIT_CONSTRAINT< EDIT_POINT > * m_endSideConstraint
Additional constraint, applied when at least two points are collinear.
virtual void SetPosition(const VECTOR2I &aPosition) override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:237
a few functions useful in geometry calculations.
double Angle() const
Compute the angle of the vector.
Definition: vector2d.h:307
V2D_TRANSFORM_FUN m_snapFun
< Function that determines snapping point.
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
Definition: edit_points.h:322
EDIT_CONSTRAINT< EDIT_POINT > * m_originSideConstraint
< Constraint for origin side segment.
Definition: seg.h:40
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition: vector2d.h:371
VECTOR2I m_draggedVector
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
virtual void Apply(EDIT_TYPE &aHandle)=0
Correct coordinates of the constrained edit handle.
The common library.
EDIT_POINTS & m_editPoints
Vector that represents the initial direction of the dragged segment.
EC_CONVERGING(EDIT_LINE &aLine, EDIT_POINTS &aPoints)
EDIT_POINT & m_constrained
Point that is constrained by rules implemented by Apply().
constexpr int delta
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is after the given point in the list.
Represent a single point that can be used for modifying items.
Definition: edit_points.h:47
Describe constraints between two edit handles.
EC_PERPLINE(EDIT_LINE &aLine)
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
virtual void SetPosition(const VECTOR2I &aPosition)
Set new coordinates for an EDIT_POINT.
Definition: edit_points.h:106