KiCad PCB EDA Suite
shape_arc.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 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@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 <algorithm>
26 #include <vector>
27 
29 #include <geometry/shape_arc.h>
31 
32 bool SHAPE_ARC::Collide( const SEG& aSeg, int aClearance ) const
33 {
34  int minDist = aClearance + m_width / 2;
35  auto centerDist = aSeg.Distance( m_pc );
36  auto p1 = GetP1();
37 
38  if( centerDist < minDist )
39  return true;
40 
41  auto ab = (aSeg.B - aSeg.A );
42  auto ac = ( m_pc - aSeg.A );
43 
44  auto lenAbSq = ab.SquaredEuclideanNorm();
45 
46  auto lambda = (double) ac.Dot( ab ) / (double) lenAbSq;
47 
48 
49  if( lambda >= 0.0 && lambda <= 1.0 )
50  {
51  VECTOR2I p;
52 
53  p.x = (double) aSeg.A.x * lambda + (double) aSeg.B.x * (1.0 - lambda);
54  p.y = (double) aSeg.A.y * lambda + (double) aSeg.B.y * (1.0 - lambda);
55 
56  auto p0pdist = ( m_p0 - p ).EuclideanNorm();
57 
58  if( p0pdist < minDist )
59  return true;
60 
61  auto p1pdist = ( p1 - p ).EuclideanNorm();
62 
63  if( p1pdist < minDist )
64  return true;
65  }
66 
67  auto p0dist = aSeg.Distance( m_p0 );
68 
69  if( p0dist > minDist )
70  return true;
71 
72  auto p1dist = aSeg.Distance( p1 );
73 
74  if( p1dist > minDist )
75  return false;
76 
77 
78  return true;
79 }
80 
81 #if 0
82 bool SHAPE_ARC::ConstructFromCorners( VECTOR2I aP0, VECTOR2I aP1, double aCenterAngle )
83 {
84  VECTOR2D mid = ( VECTOR2D( aP0 ) + VECTOR2D( aP1 ) ) * 0.5;
85  VECTOR2D chord = VECTOR2D( aP1 ) - VECTOR2D( aP0 );
86  double c = (aP1 - aP0).EuclideanNorm() / 2;
87  VECTOR2D d = chord.Rotate( M_PI / 2.0 ).Resize( c );
88 
89  m_pc = mid + d * ( 1.0 / tan( aCenterAngle / 2.0 * M_PI / 180.0 ) );
90  m_p0 = aP0;
91  m_p1 = aP1;
92 
93  return true;
94 }
95 
96 bool SHAPE_ARC::ConstructFromCornerAndAngles( VECTOR2I aP0,
97  double aStartAngle,
98  double aCenterAngle,
99  double aRadius )
100 {
101  m_p0 = aP0;
102  auto d1 = VECTOR2D( 1.0, 0.0 ).Rotate( aStartAngle * M_PI / 180.0 ) * aRadius;
103  auto d2 =
104  VECTOR2D( 1.0, 0.0 ).Rotate( (aStartAngle + aCenterAngle) * M_PI / 180.0 ) * aRadius;
105 
106  m_pc = m_p0 - (VECTOR2I) d1;
107  m_p1 = m_pc + (VECTOR2I) d2;
108 
109  if( aCenterAngle < 0 )
110  std::swap( m_p0, m_p1 );
111 
112  return true;
113 }
114 
115 bool SHAPE_ARC::ConstructFromCenterAndAngles( VECTOR2I aCenter, double aRadius, double aStartAngle, double aCenterAngle )
116 {
117  double ea = aStartAngle + aCenterAngle;
118 
119  m_fullCircle = false;
120  m_pc = aCenter;
121  m_p0.x = (int) ( (double) aCenter.x + aRadius * cos( aStartAngle * M_PI / 180.0 ) );
122  m_p0.y = (int) ( (double) aCenter.y + aRadius * sin( aStartAngle * M_PI / 180.0 ) );
123  m_p1.x = (int) ( (double) aCenter.x + aRadius * cos( ea * M_PI / 180.0 ) );
124  m_p1.y = (int) ( (double) aCenter.y + aRadius * sin( ea * M_PI / 180.0 ) );
125 
126  if( aCenterAngle == 360.0 )
127  {
128  m_fullCircle = true;
129  return true;
130  }
131  else if ( aCenterAngle < 0.0 )
132  {
133  std::swap(m_p0, m_p1);
134  }
135 
136  return true;
137 }
138 #endif
139 
140 
142 {
143  VECTOR2D rvec = m_p0 - m_pc;
144  auto ca = m_centralAngle * M_PI / 180.0;
145  VECTOR2I p1;
146 
147  p1.x = (int) ( m_pc.x + rvec.x * cos( ca ) - rvec.y * sin( ca ) );
148  p1.y = (int) ( m_pc.y + rvec.x * sin( ca ) + rvec.y * cos( ca ) );
149 
150  return p1;
151 }
152 
153 
154 const BOX2I SHAPE_ARC::BBox( int aClearance ) const
155 {
156  BOX2I bbox;
157  std::vector<VECTOR2I> points;
158  points.push_back( m_pc );
159  points.push_back( m_p0 );
160  points.push_back( GetP1() );
161 
162  double start_angle = GetStartAngle();
163  double end_angle = start_angle + GetCentralAngle();
164 
165  // we always count quadrants clockwise (increasing angle)
166  if( start_angle > end_angle )
167  std::swap( start_angle, end_angle );
168 
169  int quad_angle_start = std::ceil( start_angle / 90.0 );
170  int quad_angle_end = std::floor( end_angle / 90.0 );
171 
172  // count through quadrants included in arc
173  for( int quad_angle = quad_angle_start; quad_angle <= quad_angle_end; ++quad_angle )
174  {
175  const int radius = GetRadius();
176  VECTOR2I quad_pt = m_pc;
177 
178  switch( quad_angle % 4 )
179  {
180  case 0: quad_pt += { radius, 0 }; break;
181  case 1:
182  case -3: quad_pt += { 0, radius }; break;
183  case 2:
184  case -2: quad_pt += { -radius, 0 }; break;
185  case 3:
186  case -1: quad_pt += { 0, -radius }; break;
187  default: assert( false );
188  }
189 
190  points.push_back( quad_pt );
191  }
192 
193  bbox.Compute( points );
194 
195  if( aClearance != 0 )
196  bbox.Inflate( aClearance );
197 
198  return bbox;
199 }
200 
201 
202 bool SHAPE_ARC::Collide( const VECTOR2I& aP, int aClearance ) const
203 {
204  assert( false );
205  return false;
206 }
207 
208 
210 {
211  VECTOR2D d( m_p0 - m_pc );
212 
213  auto ang = 180.0 / M_PI * atan2( d.y, d.x );
214 
215  return ang;
216 }
217 
219 {
220  double a = GetStartAngle() + m_centralAngle;
221 
222  if( a < 0.0 )
223  a += 360.0;
224  else if ( a >= 360.0 )
225  a -= 360.0;
226 
227  return a;
228 }
229 
231 {
232  return m_centralAngle;
233 }
234 
236 {
237  return (m_p0 - m_pc).EuclideanNorm();
238 }
239 
240 const SHAPE_LINE_CHAIN SHAPE_ARC::ConvertToPolyline( double aAccuracy ) const
241 {
242  SHAPE_LINE_CHAIN rv;
243  double r = GetRadius();
244  double sa = GetStartAngle();
245  auto c = GetCenter();
246  int n;
247 
248  if( r == 0.0 )
249  {
250  n = 0;
251  }
252  else
253  {
254  n = GetArcToSegmentCount( r, aAccuracy, m_centralAngle );
255  }
256 
257  for( int i = 0; i <= n ; i++ )
258  {
259  double a = sa + m_centralAngle * (double) i / (double) n;
260  double x = c.x + r * cos( a * M_PI / 180.0 );
261  double y = c.y + r * sin( a * M_PI / 180.0 );
262 
263  rv.Append( (int) x, (int) y );
264  }
265 
266  return rv;
267 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
int Distance(const SEG &aSeg) const
Function Distance()
Definition: seg.h:199
int GetArcToSegmentCount(int aRadius, int aErrorMax, double aArcAngleDegree)
void Compute(const Container &aPointList)
Compute the bounding box from a given list of points.
Definition: box2.h:89
double GetStartAngle() const
Definition: shape_arc.cpp:209
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
int GetRadius() const
Definition: shape_arc.cpp:235
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
bool Collide(const SEG &aSeg, int aClearance=0) const override
Function Collide()
Definition: shape_arc.cpp:32
VECTOR2I m_p0
Definition: shape_arc.h:136
const VECTOR2I & GetCenter() const
Definition: shape_arc.h:64
const VECTOR2I GetP1() const
Definition: shape_arc.cpp:141
a few functions useful in geometry calculations.
double GetEndAngle() const
Definition: shape_arc.cpp:218
Definition: seg.h:36
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:385
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:300
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:370
Class SHAPE_LINE_CHAIN.
size_t i
Definition: json11.cpp:597
VECTOR2I A
Definition: seg.h:44
double GetCentralAngle() const
Definition: shape_arc.cpp:230
VECTOR2I m_pc
Definition: shape_arc.h:136
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_arc.cpp:154
const SHAPE_LINE_CHAIN ConvertToPolyline(double aAccuracy=500.0) const
Constructs a SHAPE_LINE_CHAIN of segments from a given arc.
Definition: shape_arc.cpp:240
int m_width
Definition: shape_arc.h:139
double m_centralAngle
Definition: shape_arc.h:137
VECTOR2I B
Definition: seg.h:45