KiCad PCB EDA Suite
seg.h
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) 2013 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 #ifndef __SEG_H
26 #define __SEG_H
27 
28 #include <cstdio>
29 #include <climits>
30 
31 #include <math/vector2d.h>
32 
33 #include <boost/optional/optional.hpp>
34 
36 
37 class SEG
38 {
39 private:
41 
42 public:
43  friend inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg );
44 
45  /* Start and the of the segment. Public, to make access simpler.
46  */
49 
53  SEG()
54  {
55  m_index = -1;
56  }
57 
62  SEG( int aX1, int aY1, int aX2, int aY2 ) :
63  A ( VECTOR2I( aX1, aY1 ) ),
64  B ( VECTOR2I( aX2, aY2 ) )
65  {
66  m_index = -1;
67  }
68 
73  SEG( const VECTOR2I& aA, const VECTOR2I& aB ) : A( aA ), B( aB )
74  {
75  m_index = -1;
76  }
77 
85  SEG( const VECTOR2I& aA, const VECTOR2I& aB, int aIndex ) : A( aA ), B( aB )
86  {
87  m_index = aIndex;
88  }
89 
93  SEG( const SEG& aSeg ) : A( aSeg.A ), B( aSeg.B ), m_index( aSeg.m_index )
94  {
95  }
96 
97  SEG& operator=( const SEG& aSeg )
98  {
99  A = aSeg.A;
100  B = aSeg.B;
101  m_index = aSeg.m_index;
102 
103  return *this;
104  }
105 
114  VECTOR2I LineProject( const VECTOR2I& aP ) const;
115 
123  int Side( const VECTOR2I& aP ) const
124  {
125  const ecoord det = ( B - A ).Cross( aP - A );
126 
127  return det < 0 ? -1 : ( det > 0 ? 1 : 0 );
128  }
129 
140  int LineDistance( const VECTOR2I& aP, bool aDetermineSide = false ) const;
141 
148  const VECTOR2I NearestPoint( const VECTOR2I &aP ) const;
149 
160  OPT_VECTOR2I Intersect( const SEG& aSeg, bool aIgnoreEndpoints = false,
161  bool aLines = false ) const;
162 
170  OPT_VECTOR2I IntersectLines( const SEG& aSeg ) const
171  {
172  return Intersect( aSeg, false, true );
173  }
174 
175  bool Collide( const SEG& aSeg, int aClearance ) const;
176 
177  ecoord SquaredDistance( const SEG& aSeg ) const;
178 
186  int Distance( const SEG& aSeg ) const
187  {
188  return sqrt( SquaredDistance( aSeg ) );
189  }
190 
191  ecoord SquaredDistance( const VECTOR2I& aP ) const
192  {
193  return ( NearestPoint( aP ) - aP ).SquaredEuclideanNorm();
194  }
195 
203  int Distance( const VECTOR2I& aP ) const
204  {
205  return sqrt( SquaredDistance( aP ) );
206  }
207 
208  void CanonicalCoefs( ecoord& qA, ecoord& qB, ecoord& qC ) const
209  {
210  qA = A.y - B.y;
211  qB = B.x - A.x;
212  qC = -qA * A.x - qB * A.y;
213  }
214 
222  bool Collinear( const SEG& aSeg ) const
223  {
224  ecoord qa, qb, qc;
225  CanonicalCoefs( qa, qb, qc );
226 
227  ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
228  ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
229 
230  return ( d1 <= 1 && d2 <= 1 );
231  }
232 
233  bool ApproxCollinear( const SEG& aSeg ) const
234  {
235  ecoord p, q, r;
236  CanonicalCoefs( p, q, r );
237 
238  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
239  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
240 
241  return std::abs( dist1 ) <= 1 && std::abs( dist2 ) <= 1;
242  }
243 
244  bool ApproxParallel ( const SEG& aSeg ) const
245  {
246  ecoord p, q, r;
247  CanonicalCoefs( p, q, r );
248 
249  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
250  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
251 
252  return std::abs( dist1 - dist2 ) <= 1;
253  }
254 
255 
256  bool Overlaps( const SEG& aSeg ) const
257  {
258  if( aSeg.A == aSeg.B ) // single point corner case
259  {
260  if( A == aSeg.A || B == aSeg.A )
261  return false;
262 
263  return Contains( aSeg.A );
264  }
265 
266  if( !Collinear( aSeg ) )
267  return false;
268 
269  if( Contains( aSeg.A ) || Contains( aSeg.B ) )
270  return true;
271  if( aSeg.Contains( A ) || aSeg.Contains( B ) )
272  return true;
273 
274  return false;
275  }
276 
283  int Length() const
284  {
285  return ( A - B ).EuclideanNorm();
286  }
287 
288  ecoord SquaredLength() const
289  {
290  return ( A - B ).SquaredEuclideanNorm();
291  }
292 
293  ecoord TCoef( const VECTOR2I& aP ) const;
294 
301  int Index() const
302  {
303  return m_index;
304  }
305 
306  bool Contains( const VECTOR2I& aP ) const;
307 
308  bool PointCloserThan( const VECTOR2I& aP, int aDist ) const;
309 
310  void Reverse()
311  {
312  std::swap( A, B );
313  }
314 
315 private:
316  bool ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I &aC ) const;
317 
319  int m_index;
320 };
321 
322 inline VECTOR2I SEG::LineProject( const VECTOR2I& aP ) const
323 {
324  VECTOR2I d = B - A;
325  ecoord l_squared = d.Dot( d );
326 
327  if( l_squared == 0 )
328  return A;
329 
330  ecoord t = d.Dot( aP - A );
331 
332  int xp = rescale( t, (ecoord)d.x, l_squared );
333  int yp = rescale( t, (ecoord)d.y, l_squared );
334 
335  return A + VECTOR2I( xp, yp );
336 }
337 
338 inline int SEG::LineDistance( const VECTOR2I& aP, bool aDetermineSide ) const
339 {
340  ecoord p = A.y - B.y;
341  ecoord q = B.x - A.x;
342  ecoord r = -p * A.x - q * A.y;
343 
344  ecoord dist = ( p * aP.x + q * aP.y + r ) / sqrt( p * p + q * q );
345 
346  return aDetermineSide ? dist : std::abs( dist );
347 }
348 
349 inline SEG::ecoord SEG::TCoef( const VECTOR2I& aP ) const
350 {
351  VECTOR2I d = B - A;
352  return d.Dot( aP - A);
353 }
354 
355 inline const VECTOR2I SEG::NearestPoint( const VECTOR2I& aP ) const
356 {
357  VECTOR2I d = B - A;
358  ecoord l_squared = d.Dot( d );
359 
360  if( l_squared == 0 )
361  return A;
362 
363  ecoord t = d.Dot( aP - A );
364 
365  if( t < 0 )
366  return A;
367  else if( t > l_squared )
368  return B;
369 
370  int xp = rescale( t, (ecoord)d.x, l_squared );
371  int yp = rescale( t, (ecoord)d.y, l_squared );
372 
373  return A + VECTOR2I( xp, yp );
374 }
375 
376 inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg )
377 {
378  aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
379 
380  return aStream;
381 }
382 
383 #endif // __SEG_H
int Index() const
Function Index()
Definition: seg.h:301
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:208
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:81
ecoord SquaredLength() const
Definition: seg.h:288
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:488
int Side(const VECTOR2I &aP) const
Function Side()
Definition: seg.h:123
int Length() const
Function Length()
Definition: seg.h:283
SEG()
Default constructor Creates an empty (0, 0) segment.
Definition: seg.h:53
VECTOR2I LineProject(const VECTOR2I &aP) const
Function LineProject()
Definition: seg.h:322
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:170
std::ostream & operator<<(std::ostream &aStream, const SEG &aSeg)
Definition: seg.h:376
static const int dist[10][10]
Definition: dist.cpp:57
friend std::ostream & operator<<(std::ostream &aStream, const SEG &aSeg)
Definition: seg.h:376
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:99
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:76
#define abs(a)
Definition: auxiliary.h:84
bool ApproxCollinear(const SEG &aSeg) const
Definition: seg.h:233
SEG & operator=(const SEG &aSeg)
Definition: seg.h:97
ecoord TCoef(const VECTOR2I &aP) const
Definition: seg.h:349
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:128
SEG(const VECTOR2I &aA, const VECTOR2I &aB)
Constructor Creates a segment between (aA) and (aB)
Definition: seg.h:73
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Function LineDistance()
Definition: seg.h:338
SEG(int aX1, int aY1, int aX2, int aY2)
Constructor Creates a segment between (aX1, aY1) and (aX2, aY2)
Definition: seg.h:62
SEG(const SEG &aSeg)
Copy constructor.
Definition: seg.h:93
ecoord SquaredDistance(const VECTOR2I &aP) const
Definition: seg.h:191
boost::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:35
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: math_util.cpp:32
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:355
Definition: seg.h:37
void Reverse()
Definition: seg.h:310
bool Overlaps(const SEG &aSeg) const
Definition: seg.h:256
VECTOR2I A
Definition: seg.h:47
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:319
int Distance(const VECTOR2I &aP) const
Function Distance()
Definition: seg.h:203
VECTOR2I::extended_type ecoord
Definition: seg.h:40
bool PointCloserThan(const VECTOR2I &aP, int aDist) const
Definition: seg.cpp:34
bool Collide(const SEG &aSeg, int aClearance) const
Definition: seg.cpp:134
bool Collinear(const SEG &aSeg) const
Function Collinear()
Definition: seg.h:222
bool ApproxParallel(const SEG &aSeg) const
Definition: seg.h:244
bool Contains(const VECTOR2I &aP) const
Definition: seg.cpp:155
int Distance(const SEG &aSeg) const
Function Distance()
Definition: seg.h:186
double Cross(const Point &a, const Point &b)
Perform the cross product on two vectors. In 2D this produces a scalar.
Definition: shapes.h:287
SEG(const VECTOR2I &aA, const VECTOR2I &aB, int aIndex)
Constructor Creates a segment between (aA) and (aB), referenced to a multi-segment shape...
Definition: seg.h:85
VECTOR2I B
Definition: seg.h:48