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 "edit_constraints.h"
26 #include "edit_points.h"
27 
28 #include <geometry/seg.h>
29 
30 #include <common.h>
31 
32 #include <utility>
33 
35 {
36  VECTOR2I point = aHandle.GetPosition();
37  point.x = m_constrainer.GetPosition().x;
38  aHandle.SetPosition( point );
39 }
40 
41 
43 {
44  VECTOR2I point = aHandle.GetPosition();
45  point.y = m_constrainer.GetPosition().y;
46  aHandle.SetPosition( point );
47 }
48 
49 
51 {
52  // Current line vector
53  VECTOR2I lineVector( aHandle.GetPosition() - m_constrainer.GetPosition() );
54  double angle = lineVector.Angle();
55 
56  // Find the closest angle, which is a multiple of 45 degrees
57  double newAngle = KiROUND( angle / ( M_PI / 4.0 ) ) * M_PI / 4.0;
58  VECTOR2I newLineVector = lineVector.Rotate( newAngle - angle );
59 
60  aHandle.SetPosition( m_constrainer.GetPosition() + newLineVector );
61 }
62 
63 
64 EC_LINE::EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
65  EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
66 {
68 }
69 
70 
71 void EC_LINE::Apply( EDIT_POINT& aHandle )
72 {
74  SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
75 
76  if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
77  aHandle.SetPosition( *intersect );
78 }
79 
80 
81 void EC_CIRCLE::Apply( EDIT_POINT& aHandle )
82 {
83  VECTOR2I centerToEnd = m_end.GetPosition() - m_center.GetPosition();
84  VECTOR2I centerToPoint = aHandle.GetPosition() - m_center.GetPosition();
85 
86  int radius = centerToEnd.EuclideanNorm();
87  double angle = centerToPoint.Angle();
88 
89  VECTOR2I newLine( radius, 0 );
90  newLine = newLine.Rotate( angle );
91 
92  aHandle.SetPosition( m_center.GetPosition() + newLine );
93 }
94 
95 
97  EDIT_CONSTRAINT<EDIT_LINE>( aLine ),
98  m_colinearConstraint( NULL ), m_editPoints( aPoints )
99 {
100  // Dragged segment endings
101  EDIT_POINT& origin = aLine.GetOrigin();
102  EDIT_POINT& end = aLine.GetEnd();
103 
104  // Previous and next points, to make constraining lines (adjacent to the dragged line)
105  EDIT_POINT& prevOrigin = *aPoints.Previous( origin, false );
106  EDIT_POINT& nextEnd = *aPoints.Next( end, false );
107 
108  // Constraints for segments adjacent to the dragged one
109  m_originSideConstraint = new EC_LINE( origin, prevOrigin );
110  m_endSideConstraint = new EC_LINE( end, nextEnd );
111 
112  // Store the current vector of the line
113  m_draggedVector = end.GetPosition() - origin.GetPosition();
114 
115  // Check for colinearity
116  SEG originSide( origin.GetPosition(), prevOrigin.GetPosition() );
117  SEG endSide( end.GetPosition(), nextEnd.GetPosition() );
118  SEG dragged( origin.GetPosition(), end.GetPosition() );
119 
120  if( dragged.Collinear( originSide ) )
122  else if( dragged.Collinear( endSide ) )
124 }
125 
126 
128 {
129  delete m_originSideConstraint;
130  delete m_endSideConstraint;
131  // m_colinearConstraint should not be freed, it is a pointer to one of the above
132 }
133 
134 
136 {
137  // The dragged segment endpoints
138  EDIT_POINT& origin = aHandle.GetOrigin();
139  EDIT_POINT& end = aHandle.GetEnd();
140 
142  {
143  m_colinearConstraint->Apply( origin );
144  m_colinearConstraint->Apply( end );
145  }
146 
147  // The dragged segment
148  SEG dragged( origin.GetPosition(), origin.GetPosition() + m_draggedVector );
149 
150  // Do not allow points on the adjacent segments move freely
153 
154  EDIT_POINT& prevOrigin = *m_editPoints.Previous( origin, false );
155  EDIT_POINT& nextEnd = *m_editPoints.Next( end, false );
156 
157  // Two segments adjacent to the dragged segment
158  SEG originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
159  SEG endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
160 
161  // First intersection point (dragged segment against origin side)
162  if( OPT_VECTOR2I originIntersect = dragged.IntersectLines( originSide ) )
163  origin.SetPosition( *originIntersect );
164 
165  // Second intersection point (dragged segment against end side)
166  if( OPT_VECTOR2I endIntersect = dragged.IntersectLines( endSide ) )
167  end.SetPosition( *endIntersect );
168 
169  // Check if adjacent segments intersect (did we dragged the line to the point that it may
170  // create a selfintersecting polygon?)
171  originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
172  endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
173 
174  if( OPT_VECTOR2I originEndIntersect = endSide.Intersect( originSide ) )
175  {
176  origin.SetPosition( *originEndIntersect );
177  end.SetPosition( *originEndIntersect );
178  }
179 }
180 
181 
183  EDIT_CONSTRAINT<EDIT_LINE>( aLine ), m_snapFun( std::move(aSnapFun) )
184 {}
185 
186 
188 {
189  VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
190 
191  aHandle.GetOrigin().SetPosition( m_snapFun( aHandle.GetOrigin().GetPosition() ) );
192  aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
193 }
void Apply()
Function Apply()
VECTOR2I m_line
Vector representing the constraining line.
virtual ~EC_CONVERGING()
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:120
Class EDIT_LINE.
Definition: edit_points.h:189
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.
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:370
Template specialization to enable wxStrings for certain containers (e.g. unordered_map) ...
Definition: bitmap.cpp:55
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:99
EC_LINE(EDIT_POINT &aConstrained, const EDIT_POINT &aConstrainer)
static const int delta[8][2]
Definition: solve.cpp:112
Class EC_LINE.
EDIT_POINT & GetOrigin()
Function GetOrigin()
Definition: edit_points.h:256
EC_SNAPLINE(EDIT_LINE &aLine, V2D_TRANSFORM_FUN aSnapFun)
EDIT_POINT & GetEnd()
Function GetEnd()
Definition: edit_points.h:271
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:306
int main(int argc, char **argv)
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:292
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:34
boost::function< VECTOR2D(const VECTOR2D &)> V2D_TRANSFORM_FUN
Typedef for a function that determines snapping point.
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:293
V2D_TRANSFORM_FUN m_snapFun
Function that determines snapping point.
Class EDIT_POINTS.
Definition: edit_points.h:305
EDIT_CONSTRAINT< EDIT_POINT > * m_originSideConstraint
Constraint for origin side segment.
Definition: seg.h:36
virtual VECTOR2I GetPosition() const
Function GetPosition()
Definition: edit_points.h:65
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()
The common library.
VECTOR2< T > Perpendicular() const
Function Perpendicular computes the perpendicular vector.
Definition: vector2d.h:313
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.
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Function Previous()
Class EDIT_POINT.
Definition: edit_points.h:46
Class EDIT_CONSTRAINT.
virtual void SetPosition(const VECTOR2I &aPosition)
Function SetPosition()
Definition: edit_points.h:97