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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include "tool/edit_constraints.h"
26 #include "tool/edit_points.h"
27 
28 #include <geometry/seg.h>
29 #include <math/util.h> // for KiROUND
30 
31 #include <common.h>
32 
33 #include <utility>
34 
36 {
37  VECTOR2I point = aHandle.GetPosition();
38  point.x = m_constrainer.GetPosition().x;
39  aHandle.SetPosition( point );
40 }
41 
42 
44 {
45  VECTOR2I point = aHandle.GetPosition();
46  point.y = m_constrainer.GetPosition().y;
47  aHandle.SetPosition( point );
48 }
49 
50 
52 {
53  // Current line vector
54  VECTOR2I lineVector( aHandle.GetPosition() - m_constrainer.GetPosition() );
55  double angle = lineVector.Angle();
56 
57  // Find the closest angle, which is a multiple of 45 degrees
58  double newAngle = KiROUND( angle / ( M_PI / 4.0 ) ) * M_PI / 4.0;
59  VECTOR2I newLineVector = lineVector.Rotate( newAngle - angle );
60 
61  aHandle.SetPosition( m_constrainer.GetPosition() + newLineVector );
62 }
63 
64 
65 EC_LINE::EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
66  EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
67 {
69 }
70 
71 
72 void EC_LINE::Apply( EDIT_POINT& aHandle )
73 {
75  SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
76 
77  if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
78  aHandle.SetPosition( *intersect );
79 }
80 
81 
82 void EC_CIRCLE::Apply( EDIT_POINT& aHandle )
83 {
84  VECTOR2I centerToEnd = m_end.GetPosition() - m_center.GetPosition();
85  VECTOR2I centerToPoint = aHandle.GetPosition() - m_center.GetPosition();
86 
87  int radius = centerToEnd.EuclideanNorm();
88  double angle = centerToPoint.Angle();
89 
90  VECTOR2I newLine( radius, 0 );
91  newLine = newLine.Rotate( angle );
92 
93  aHandle.SetPosition( m_center.GetPosition() + newLine );
94 }
95 
96 
98  EDIT_CONSTRAINT<EDIT_LINE>( aLine ),
99  m_colinearConstraint( NULL ), m_editPoints( aPoints )
100 {
101  // Dragged segment endings
102  EDIT_POINT& origin = aLine.GetOrigin();
103  EDIT_POINT& end = aLine.GetEnd();
104 
105  // Previous and next points, to make constraining lines (adjacent to the dragged line)
106  EDIT_POINT& prevOrigin = *aPoints.Previous( origin, false );
107  EDIT_POINT& nextEnd = *aPoints.Next( end, false );
108 
109  // Constraints for segments adjacent to the dragged one
110  m_originSideConstraint = new EC_LINE( origin, prevOrigin );
111  m_endSideConstraint = new EC_LINE( end, nextEnd );
112 
113  // Store the current vector of the line
114  m_draggedVector = end.GetPosition() - origin.GetPosition();
115 
116  // Check for colinearity
117  SEG originSide( origin.GetPosition(), prevOrigin.GetPosition() );
118  SEG endSide( end.GetPosition(), nextEnd.GetPosition() );
119  SEG dragged( origin.GetPosition(), end.GetPosition() );
120 
121  if( dragged.Collinear( originSide ) )
123  else if( dragged.Collinear( endSide ) )
125 }
126 
127 
129 {
130  delete m_originSideConstraint;
131  delete m_endSideConstraint;
132  // m_colinearConstraint should not be freed, it is a pointer to one of the above
133 }
134 
135 
137 {
138  // The dragged segment endpoints
139  EDIT_POINT& origin = aHandle.GetOrigin();
140  EDIT_POINT& end = aHandle.GetEnd();
141 
143  {
144  m_colinearConstraint->Apply( origin );
145  m_colinearConstraint->Apply( end );
146  }
147 
148  // The dragged segment
149  SEG dragged( origin.GetPosition(), origin.GetPosition() + m_draggedVector );
150 
151  // Do not allow points on the adjacent segments move freely
154 
155  EDIT_POINT& prevOrigin = *m_editPoints.Previous( origin, false );
156  EDIT_POINT& nextEnd = *m_editPoints.Next( end, false );
157 
158  // Two segments adjacent to the dragged segment
159  SEG originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
160  SEG endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
161 
162  // First intersection point (dragged segment against origin side)
163  if( OPT_VECTOR2I originIntersect = dragged.IntersectLines( originSide ) )
164  origin.SetPosition( *originIntersect );
165 
166  // Second intersection point (dragged segment against end side)
167  if( OPT_VECTOR2I endIntersect = dragged.IntersectLines( endSide ) )
168  end.SetPosition( *endIntersect );
169 
170  // Check if adjacent segments intersect (did we dragged the line to the point that it may
171  // create a selfintersecting polygon?)
172  originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
173  endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
174 
175  if( OPT_VECTOR2I originEndIntersect = endSide.Intersect( originSide ) )
176  {
177  // Triangle intersect by definition
178  if( m_editPoints.LinesSize() > 3 )
179  {
180  origin.SetPosition( *originEndIntersect );
181  end.SetPosition( *originEndIntersect );
182  }
183  }
184 }
185 
186 
188  EDIT_CONSTRAINT<EDIT_LINE>( aLine ), m_snapFun( std::move(aSnapFun) )
189 {}
190 
191 
193 {
194  VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
195 
196  aHandle.GetOrigin().SetPosition( m_snapFun( aHandle.GetOrigin().GetPosition() ) );
197  aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
198 }
void Apply()
Function Apply()
VECTOR2I m_line
Vector representing the constraining line.
virtual ~EC_CONVERGING()
main()
VECTOR2< T > Perpendicular() const
Function Perpendicular computes the perpendicular vector.
Definition: vector2d.h:320
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:93
const EDIT_POINT & m_center
Point that imposes the constraint (center of the circle).
EDIT_LINE.
Definition: edit_points.h:221
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
EDIT_POINT * Next(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Function Next()
EDIT_CONSTRAINT< EDIT_POINT > * m_colinearConstraint
Additional constriant, applied when at least two points are collinear.
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:56
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
EC_LINE(EDIT_POINT &aConstrained, const EDIT_POINT &aConstrainer)
EC_LINE.
EDIT_POINT & GetOrigin()
Function GetOrigin()
Definition: edit_points.h:288
EC_SNAPLINE(EDIT_LINE &aLine, V2D_TRANSFORM_FUN aSnapFun)
EDIT_POINT & GetEnd()
Function GetEnd()
Definition: edit_points.h:303
virtual VECTOR2I GetPosition() const
Function GetPosition()
Definition: edit_points.h:69
#define NULL
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:37
const EDIT_POINT & m_end
Point that imposes the constraint (decides on the radius of the circle).
boost::function< VECTOR2D(const VECTOR2D &)> V2D_TRANSFORM_FUN
Typedef for a function that determines snapping point.
unsigned int LinesSize() const
Function LinesSize()
Definition: edit_points.h:522
EDIT_CONSTRAINT< EDIT_POINT > * m_endSideConstraint
Constraint for end side segment.
static bool intersect(const SEGMENT_WITH_NORMALS &aSeg, const SFVEC2F &aStart, const SFVEC2F &aEnd)
Definition: cpolygon2d.cpp:291
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:313
V2D_TRANSFORM_FUN m_snapFun
Function that determines snapping point.
EDIT_POINTS.
Definition: edit_points.h:337
EDIT_CONSTRAINT< EDIT_POINT > * m_originSideConstraint
Constraint for origin side segment.
Definition: seg.h:39
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377
VECTOR2I m_draggedVector
Vector that represents the initial direction of the dragged segment.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
virtual void Apply(EDIT_TYPE &aHandle)=0
Function Apply()
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
The common library.
EDIT_POINTS & m_editPoints
EDIT_POINTS instance that stores currently modified lines.
EC_CONVERGING(EDIT_LINE &aLine, EDIT_POINTS &aPoints)
EDIT_POINT & m_constrained
Point that is constrained by rules implemented by Apply()
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:299
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Function Previous()
EDIT_POINT.
Definition: edit_points.h:46
EDIT_CONSTRAINT.
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
virtual void SetPosition(const VECTOR2I &aPosition)
Function SetPosition()
Definition: edit_points.h:106