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 <math.h> // for sqrt
29 #include <stdlib.h> // for abs
30 #include <ostream> // for operator<<, ostream, basic_os...
31 #include <type_traits> // for swap
32 
33 #include <core/optional.h>
34 #include <math/util.h> // for rescale
35 #include <math/vector2d.h>
36 
38 
39 class SEG
40 {
41 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 
106  bool operator==( const SEG& aSeg ) const
107  {
108  return (A == aSeg.A && B == aSeg.B) ;
109  }
110 
111  bool operator!=( const SEG& aSeg ) const
112  {
113  return (A != aSeg.A || B != aSeg.B);
114  }
115 
116  static SEG::ecoord Square( int a )
117  {
118  return ecoord( a ) * a;
119  }
120 
129  VECTOR2I LineProject( const VECTOR2I& aP ) const;
130 
138  int Side( const VECTOR2I& aP ) const
139  {
140  const ecoord det = ( B - A ).Cross( aP - A );
141 
142  return det < 0 ? -1 : ( det > 0 ? 1 : 0 );
143  }
144 
155  int LineDistance( const VECTOR2I& aP, bool aDetermineSide = false ) const;
156 
163  const VECTOR2I NearestPoint( const VECTOR2I &aP ) const;
164 
169  const VECTOR2I NearestPoint( const SEG &aSeg ) const;
170 
181  OPT_VECTOR2I Intersect( const SEG& aSeg, bool aIgnoreEndpoints = false,
182  bool aLines = false ) const;
183 
191  OPT_VECTOR2I IntersectLines( const SEG& aSeg ) const
192  {
193  return Intersect( aSeg, false, true );
194  }
195 
196  bool Collide( const SEG& aSeg, int aClearance, int* aActual = nullptr ) const;
197 
198  ecoord SquaredDistance( const SEG& aSeg ) const;
199 
207  int Distance( const SEG& aSeg ) const
208  {
209  return sqrt( SquaredDistance( aSeg ) );
210  }
211 
212  ecoord SquaredDistance( const VECTOR2I& aP ) const
213  {
214  return ( NearestPoint( aP ) - aP ).SquaredEuclideanNorm();
215  }
216 
224  int Distance( const VECTOR2I& aP ) const
225  {
226  return sqrt( SquaredDistance( aP ) );
227  }
228 
229  void CanonicalCoefs( ecoord& qA, ecoord& qB, ecoord& qC ) const
230  {
231  qA = A.y - B.y;
232  qB = B.x - A.x;
233  qC = -qA * A.x - qB * A.y;
234  }
235 
243  bool Collinear( const SEG& aSeg ) const
244  {
245  ecoord qa, qb, qc;
246  CanonicalCoefs( qa, qb, qc );
247 
248  ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
249  ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
250 
251  return ( d1 <= 1 && d2 <= 1 );
252  }
253 
254  bool ApproxCollinear( const SEG& aSeg ) const
255  {
256  ecoord p, q, r;
257  CanonicalCoefs( p, q, r );
258 
259  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
260  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
261 
262  return std::abs( dist1 ) <= 1 && std::abs( dist2 ) <= 1;
263  }
264 
265  bool ApproxParallel ( const SEG& aSeg ) const
266  {
267  ecoord p, q, r;
268  CanonicalCoefs( p, q, r );
269 
270  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
271  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
272 
273  return std::abs( dist1 - dist2 ) <= 1;
274  }
275 
276 
277  bool Overlaps( const SEG& aSeg ) const
278  {
279  if( aSeg.A == aSeg.B ) // single point corner case
280  {
281  if( A == aSeg.A || B == aSeg.A )
282  return false;
283 
284  return Contains( aSeg.A );
285  }
286 
287  if( !Collinear( aSeg ) )
288  return false;
289 
290  if( Contains( aSeg.A ) || Contains( aSeg.B ) )
291  return true;
292  if( aSeg.Contains( A ) || aSeg.Contains( B ) )
293  return true;
294 
295  return false;
296  }
297 
298 
299  bool Contains( const SEG& aSeg ) const
300  {
301  if( aSeg.A == aSeg.B ) // single point corner case
302  return Contains( aSeg.A );
303 
304  if( !Collinear( aSeg ) )
305  return false;
306 
307  if( Contains( aSeg.A ) && Contains( aSeg.B ) )
308  return true;
309 
310  return false;
311  }
312 
319  int Length() const
320  {
321  return ( A - B ).EuclideanNorm();
322  }
323 
325  {
326  return ( A - B ).SquaredEuclideanNorm();
327  }
328 
329  ecoord TCoef( const VECTOR2I& aP ) const;
330 
337  int Index() const
338  {
339  return m_index;
340  }
341 
342  bool Contains( const VECTOR2I& aP ) const;
343 
344  void Reverse()
345  {
346  std::swap( A, B );
347  }
348 
350  VECTOR2I Center() const
351  {
352  return A + ( B - A ) / 2;
353  }
354 
355 private:
356  bool ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I &aC ) const;
357 
359  int m_index;
360 };
361 
362 inline VECTOR2I SEG::LineProject( const VECTOR2I& aP ) const
363 {
364  VECTOR2I d = B - A;
365  ecoord l_squared = d.Dot( d );
366 
367  if( l_squared == 0 )
368  return A;
369 
370  ecoord t = d.Dot( aP - A );
371 
372  int xp = rescale( t, (ecoord)d.x, l_squared );
373  int yp = rescale( t, (ecoord)d.y, l_squared );
374 
375  return A + VECTOR2I( xp, yp );
376 }
377 
378 inline int SEG::LineDistance( const VECTOR2I& aP, bool aDetermineSide ) const
379 {
380  ecoord p = A.y - B.y;
381  ecoord q = B.x - A.x;
382  ecoord r = -p * A.x - q * A.y;
383 
384  ecoord dist = ( p * aP.x + q * aP.y + r ) / sqrt( p * p + q * q );
385 
386  return aDetermineSide ? dist : std::abs( dist );
387 }
388 
389 inline SEG::ecoord SEG::TCoef( const VECTOR2I& aP ) const
390 {
391  VECTOR2I d = B - A;
392  return d.Dot( aP - A);
393 }
394 
395 inline const VECTOR2I SEG::NearestPoint( const VECTOR2I& aP ) const
396 {
397  VECTOR2I d = B - A;
398  ecoord l_squared = d.Dot( d );
399 
400  if( l_squared == 0 )
401  return A;
402 
403  ecoord t = d.Dot( aP - A );
404 
405  if( t < 0 )
406  return A;
407  else if( t > l_squared )
408  return B;
409 
410  int xp = rescale( t, (ecoord)d.x, l_squared );
411  int yp = rescale( t, (ecoord)d.y, l_squared );
412 
413  return A + VECTOR2I( xp, yp );
414 }
415 
416 inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg )
417 {
418  aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
419 
420  return aStream;
421 }
422 
423 #endif // __SEG_H
int Length() const
Function Length()
Definition: seg.h:319
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:77
int Index() const
Function Index()
Definition: seg.h:337
bool Overlaps(const SEG &aSeg) const
Definition: seg.h:277
int Distance(const SEG &aSeg) const
Function Distance()
Definition: seg.h:207
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:229
ecoord SquaredLength() const
Definition: seg.h:324
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:122
SEG()
Default constructor Creates an empty (0, 0) segment.
Definition: seg.h:53
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:93
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition: seg.cpp:128
std::ostream & operator<<(std::ostream &aStream, const SEG &aSeg)
Definition: seg.h:416
VECTOR2I::extended_type ecoord
Definition: seg.h:42
bool operator!=(const SEG &aSeg) const
Definition: seg.h:111
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:37
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:191
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Function LineDistance()
Definition: seg.h:378
VECTOR2I Center() const
Returns the center point of the line
Definition: seg.h:350
friend std::ostream & operator<<(std::ostream &aStream, const SEG &aSeg)
Definition: seg.h:416
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
static SEG::ecoord Square(int a)
Definition: seg.h:116
SEG & operator=(const SEG &aSeg)
Definition: seg.h:97
VECTOR2I LineProject(const VECTOR2I &aP) const
Function LineProject()
Definition: seg.h:362
ecoord TCoef(const VECTOR2I &aP) const
Definition: seg.h:389
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:37
bool ApproxParallel(const SEG &aSeg) const
Definition: seg.h:265
VECTOR2I::extended_type ecoord
SEG(const VECTOR2I &aA, const VECTOR2I &aB)
Constructor Creates a segment between (aA) and (aB)
Definition: seg.h:73
SEG(int aX1, int aY1, int aX2, int aY2)
Constructor Creates a segment between (aX1, aY1) and (aX2, aY2)
Definition: seg.h:62
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:395
bool ApproxCollinear(const SEG &aSeg) const
Definition: seg.h:254
SEG(const SEG &aSeg)
Copy constructor.
Definition: seg.h:93
int Distance(const VECTOR2I &aP) const
Function Distance()
Definition: seg.h:224
bool Collinear(const SEG &aSeg) const
Function Collinear()
Definition: seg.h:243
Definition: seg.h:39
void Reverse()
Definition: seg.h:344
bool operator==(const SEG &aSeg) const
Definition: seg.h:106
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:492
ecoord SquaredDistance(const VECTOR2I &aP) const
Definition: seg.h:212
VECTOR2I A
Definition: seg.h:47
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:359
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:95
boost::optional< T > OPT
Definition: optional.h:7
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
int Side(const VECTOR2I &aP) const
Function Side()
Definition: seg.h:138
bool Contains(const SEG &aSeg) const
Definition: seg.h:299
VECTOR2I B
Definition: seg.h:48