KiCad PCB EDA Suite
arc_geom_manager.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) 2017 Kicad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
25 
26 #include <math/util.h> // for KiROUND
27 
28 using namespace KIGFX::PREVIEW;
29 
30 
32 static double snapAngle( double aAngle )
33 {
34  return KiROUND( aAngle / M_PI_4 ) * M_PI_4;
35 }
36 
37 
39 {
40  switch( getStep() )
41  {
42  case SET_ORIGIN:
43  return setOrigin( aPt );
44  case SET_START:
45  return setStart( aPt );
46  case SET_ANGLE:
47  return setEnd( aPt );
48  case COMPLETE:
49  break;
50  }
51 
52  return false;
53 }
54 
55 
57 {
58  m_clockwise = aCw;
59  m_directionLocked = true;
61 }
62 
63 
65 {
67  m_directionLocked = true;
69 }
70 
71 
73 {
74  return m_origin;
75 }
76 
77 
79 {
80  return m_origin + VECTOR2I( m_radius, 0 ).Rotate( m_startAngle );
81 }
82 
83 
85 {
86  return m_origin + VECTOR2I( m_radius, 0 ).Rotate( m_endAngle );
87 }
88 
89 
91 {
92  return m_radius;
93 }
94 
95 
97 {
98  double angle = m_startAngle;
99 
100  if( m_clockwise )
101  angle -= 2 * M_PI;
102 
103  return -angle;
104 }
105 
106 
108 {
109  double angle = m_endAngle - m_startAngle;
110 
111  if( m_endAngle <= m_startAngle )
112  angle += 2 * M_PI;
113 
114  if( m_clockwise )
115  angle -= 2 * M_PI;
116 
117  return -angle;
118 }
119 
120 
121 bool ARC_GEOM_MANAGER::setOrigin( const VECTOR2I& aOrigin )
122 {
123  m_origin = aOrigin;
124  m_startAngle = 0.0;
125  m_endAngle = 0.0;
126 
127  return true;
128 }
129 
130 
132 {
133  const auto radVec = aEnd - m_origin;
134 
135  m_radius = radVec.EuclideanNorm();
136  m_startAngle = radVec.Angle();
137 
138  if( m_angleSnap )
140 
141  // normalise into 0-2Pi
142  while( m_startAngle < 0 )
143  m_startAngle += M_PI * 2;
144 
146 
147  return m_radius != 0.0;
148 }
149 
150 
151 bool ARC_GEOM_MANAGER::setEnd( const VECTOR2I& aCursor )
152 {
153  const auto radVec = aCursor - m_origin;
154 
155  m_endAngle = radVec.Angle();
156 
157  if( m_angleSnap )
159 
160  // normalise into 0-2Pi
161  while( m_endAngle < 0 )
162  m_endAngle += M_PI * 2;
163 
164  if( !m_directionLocked )
165  {
166  double ccwAngle = m_endAngle - m_startAngle;
167 
168  if( m_endAngle <= m_startAngle )
169  ccwAngle += 2 * M_PI;
170 
171  double cwAngle = std::abs( ccwAngle - 2 * M_PI );
172 
173  if( std::min( ccwAngle, cwAngle ) >= M_PI_2 )
174  m_directionLocked = true;
175  else
176  m_clockwise = cwAngle < ccwAngle;
177  }
178  else if( std::abs( GetSubtended() ) < M_PI_2 )
179  {
180  m_directionLocked = false;
181  }
182 
183  // if the end is the same as the start, this is a bad point
184  return m_endAngle != m_startAngle;
185 }
Waiting to lock in the arc end point
VECTOR2I GetEndRadiusEnd() const
Get the coordinates of the arc end point
bool setEnd(const VECTOR2I &aCursor)
Set a point of the second radius line (collinear with arc end)
#define M_PI_2
Definition: transline.cpp:36
VECTOR2I GetStartRadiusEnd() const
Get the coordinates of the arc start
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
double GetStartAngle() const
Get the angle of the vector leading to the start point (valid if step >= SET_START)
double GetSubtended() const
Get the angle of the vector leading to the end point (valid if step >= SET_ANGLE)
void setGeometryChanged()
Mark the geometry as changed for clients to notice
Waiting to lock in origin point
VECTOR2I GetOrigin() const
Get the centre point of the arc (valid when state > SET_ORIGIN)
double GetRadius() const
Get the radius of the arc (valid if step >= SET_START)
int getStep() const
Get the current stage of the manager
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377
bool setStart(const VECTOR2I &aEnd)
Set the end of the first radius line (arc start)
Waiting to lock in the arc start point
bool acceptPoint(const VECTOR2I &aPt) override
Function that accepts a point for a stage, or rejects it to return to the previous stage
void ToggleClockwise()
Reverse the current are direction
void SetClockwise(bool aCw)
The the arc to be clockwise from start
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
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
bool setOrigin(const VECTOR2I &aOrigin)
Set the centre point of the arc
static double snapAngle(double aAngle)
Snap an angle to the nearest 45 degrees