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 
33 {
34  VECTOR2I point = aHandle.GetPosition();
35  point.x = m_constrainer.GetPosition().x;
36  aHandle.SetPosition( point );
37 }
38 
39 
41 {
42  VECTOR2I point = aHandle.GetPosition();
43  point.y = m_constrainer.GetPosition().y;
44  aHandle.SetPosition( point );
45 }
46 
47 
49 {
50  // Current line vector
51  VECTOR2I lineVector( aHandle.GetPosition() - m_constrainer.GetPosition() );
52  double angle = lineVector.Angle();
53 
54  // Find the closest angle, which is a multiple of 45 degrees
55  double newAngle = KiROUND( angle / ( M_PI / 4.0 ) ) * M_PI / 4.0;
56  VECTOR2I newLineVector = lineVector.Rotate( newAngle - angle );
57 
58  aHandle.SetPosition( m_constrainer.GetPosition() + newLineVector );
59 }
60 
61 
62 EC_LINE::EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
63  EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
64 {
66 }
67 
68 
69 void EC_LINE::Apply( EDIT_POINT& aHandle )
70 {
72  SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
73 
74  if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
75  aHandle.SetPosition( *intersect );
76 }
77 
78 
79 void EC_CIRCLE::Apply( EDIT_POINT& aHandle )
80 {
81  VECTOR2I centerToEnd = m_end.GetPosition() - m_center.GetPosition();
82  VECTOR2I centerToPoint = aHandle.GetPosition() - m_center.GetPosition();
83 
84  int radius = centerToEnd.EuclideanNorm();
85  double angle = centerToPoint.Angle();
86 
87  VECTOR2I newLine( radius, 0 );
88  newLine = newLine.Rotate( angle );
89 
90  aHandle.SetPosition( m_center.GetPosition() + newLine );
91 }
92 
93 
95  EDIT_CONSTRAINT<EDIT_LINE>( aLine ),
96  m_colinearConstraint( NULL ), m_editPoints( aPoints )
97 {
98  // Dragged segment endings
99  EDIT_POINT& origin = aLine.GetOrigin();
100  EDIT_POINT& end = aLine.GetEnd();
101 
102  // Previous and next points, to make constraining lines (adjacent to the dragged line)
103  EDIT_POINT& prevOrigin = *aPoints.Previous( origin, false );
104  EDIT_POINT& nextEnd = *aPoints.Next( end, false );
105 
106  // Constraints for segments adjacent to the dragged one
107  m_originSideConstraint = new EC_LINE( origin, prevOrigin );
108  m_endSideConstraint = new EC_LINE( end, nextEnd );
109 
110  // Store the current vector of the line
111  m_draggedVector = end.GetPosition() - origin.GetPosition();
112 
113  // Check for colinearity
114  SEG originSide( origin.GetPosition(), prevOrigin.GetPosition() );
115  SEG endSide( end.GetPosition(), nextEnd.GetPosition() );
116  SEG dragged( origin.GetPosition(), end.GetPosition() );
117 
118  if( dragged.Collinear( originSide ) )
120  else if( dragged.Collinear( endSide ) )
122 }
123 
124 
126 {
127  delete m_originSideConstraint;
128  delete m_endSideConstraint;
129  // m_colinearConstraint should not be freed, it is a pointer to one of the above
130 }
131 
132 
134 {
135  // The dragged segment endpoints
136  EDIT_POINT& origin = aHandle.GetOrigin();
137  EDIT_POINT& end = aHandle.GetEnd();
138 
140  {
141  m_colinearConstraint->Apply( origin );
142  m_colinearConstraint->Apply( end );
143  }
144 
145  // The dragged segment
146  SEG dragged( origin.GetPosition(), origin.GetPosition() + m_draggedVector );
147 
148  // Do not allow points on the adjacent segments move freely
151 
152  EDIT_POINT& prevOrigin = *m_editPoints.Previous( origin, false );
153  EDIT_POINT& nextEnd = *m_editPoints.Next( end, false );
154 
155  // Two segments adjacent to the dragged segment
156  SEG originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
157  SEG endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
158 
159  // First intersection point (dragged segment against origin side)
160  if( OPT_VECTOR2I originIntersect = dragged.IntersectLines( originSide ) )
161  origin.SetPosition( *originIntersect );
162 
163  // Second intersection point (dragged segment against end side)
164  if( OPT_VECTOR2I endIntersect = dragged.IntersectLines( endSide ) )
165  end.SetPosition( *endIntersect );
166 
167  // Check if adjacent segments intersect (did we dragged the line to the point that it may
168  // create a selfintersecting polygon?)
169  originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
170  endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
171 
172  if( OPT_VECTOR2I originEndIntersect = endSide.Intersect( originSide ) )
173  {
174  origin.SetPosition( *originEndIntersect );
175  end.SetPosition( *originEndIntersect );
176  }
177 }
178 
179 
181  EDIT_CONSTRAINT<EDIT_LINE>( aLine ), m_snapFun( aSnapFun )
182 {}
183 
184 
186 {
187  VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
188 
189  aHandle.GetOrigin().SetPosition( m_snapFun( aHandle.GetOrigin().GetPosition() ) );
190  aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
191 }
void Apply()
Function Apply()
VECTOR2I m_line
Vector representing the constraining line.
virtual ~EC_CONVERGING()
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
const EDIT_POINT & m_center
Point that imposes the constraint (center of the circle).
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:373
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:99
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
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:309
int main(int argc, char **argv)
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:295
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.
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:37
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:316
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.
const EDIT_POINT & m_constrainer
Point that imposes the constraint.
virtual void SetPosition(const VECTOR2I &aPosition)
Function SetPosition()
Definition: edit_points.h:97