KiCad PCB EDA Suite
seg.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) 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 #include <geometry/seg.h>
26 
27 template <typename T>
28 int sgn( T aVal )
29 {
30  return ( T( 0 ) < aVal ) - ( aVal < T( 0 ) );
31 }
32 
33 
34 bool SEG::PointCloserThan( const VECTOR2I& aP, int aDist ) const
35 {
36  VECTOR2I d = B - A;
37  ecoord dist_sq = (ecoord) aDist * aDist;
38 
39  SEG::ecoord l_squared = d.Dot( d );
40  SEG::ecoord t = d.Dot( aP - A );
41 
42  if( t <= 0 || !l_squared )
43  return ( aP - A ).SquaredEuclideanNorm() < dist_sq;
44  else if( t >= l_squared )
45  return ( aP - B ).SquaredEuclideanNorm() < dist_sq;
46 
47  int dxdy = abs( d.x ) - abs( d.y );
48 
49  if( ( dxdy >= -1 && dxdy <= 1 ) || abs( d.x ) <= 1 || abs( d.y ) <= 1 )
50  {
51  int ca = -sgn( d.y );
52  int cb = sgn( d.x );
53  int cc = -ca * A.x - cb * A.y;
54 
55  ecoord num = (ecoord) ca * aP.x + (ecoord) cb * aP.y + cc;
56  num *= num;
57 
58  if( ca && cb )
59  num >>= 1;
60 
61  if( num > ( dist_sq + 100 ) )
62  return false;
63 
64  else if( num < ( dist_sq - 100 ) )
65  return true;
66  }
67 
68  VECTOR2I nearest;
69  nearest.x = A.x + rescale( t, (ecoord) d.x, l_squared );
70  nearest.y = A.y + rescale( t, (ecoord) d.y, l_squared );
71 
72  return ( nearest - aP ).SquaredEuclideanNorm() <= dist_sq;
73 }
74 
75 
76 SEG::ecoord SEG::SquaredDistance( const SEG& aSeg ) const
77 {
78  // fixme: rather inefficient....
79  if( Intersect( aSeg ) )
80  return 0;
81 
82  const VECTOR2I pts[4] =
83  {
84  aSeg.NearestPoint( A ) - A,
85  aSeg.NearestPoint( B ) - B,
86  NearestPoint( aSeg.A ) - aSeg.A,
87  NearestPoint( aSeg.B ) - aSeg.B
88  };
89 
91 
92  for( int i = 0; i < 4; i++ )
93  m = std::min( m, pts[i].SquaredEuclideanNorm() );
94 
95  return m;
96 }
97 
98 
99 const VECTOR2I SEG::NearestPoint( const SEG& aSeg ) const
100 {
101  if( auto p = Intersect( aSeg ) )
102  return *p;
103 
104  const VECTOR2I pts_origin[4] =
105  {
106  aSeg.NearestPoint( A ),
107  aSeg.NearestPoint( B ),
108  NearestPoint( aSeg.A ),
109  NearestPoint( aSeg.B )
110  };
111 
112  const ecoord pts_dist[4] =
113  {
114  ( pts_origin[0] - A ).SquaredEuclideanNorm(),
115  ( pts_origin[1] - B ).SquaredEuclideanNorm(),
116  ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
117  ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
118  };
119 
120  int min_i = 0;
121 
122  for( int i = 0; i < 4; i++ )
123  {
124  if( pts_dist[i] < pts_dist[min_i] )
125  min_i = i;
126  }
127 
128  return pts_origin[min_i];
129 }
130 
131 
132 OPT_VECTOR2I SEG::Intersect( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines ) const
133 {
134  const VECTOR2I e( B - A );
135  const VECTOR2I f( aSeg.B - aSeg.A );
136  const VECTOR2I ac( aSeg.A - A );
137 
138  ecoord d = f.Cross( e );
139  ecoord p = f.Cross( ac );
140  ecoord q = e.Cross( ac );
141 
142  if( d == 0 )
143  return OPT_VECTOR2I();
144 
145  if( !aLines && d > 0 && ( q < 0 || q > d || p < 0 || p > d ) )
146  return OPT_VECTOR2I();
147 
148  if( !aLines && d < 0 && ( q < d || p < d || p > 0 || q > 0 ) )
149  return OPT_VECTOR2I();
150 
151  if( !aLines && aIgnoreEndpoints && ( q == 0 || q == d ) && ( p == 0 || p == d ) )
152  return OPT_VECTOR2I();
153 
154  VECTOR2I ip( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
155  aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
156 
157  return ip;
158 }
159 
160 
161 bool SEG::ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) const
162 {
163  return (ecoord) ( aC.y - aA.y ) * ( aB.x - aA.x ) > (ecoord) ( aB.y - aA.y ) * ( aC.x - aA.x );
164 }
165 
166 
167 bool SEG::Collide( const SEG& aSeg, int aClearance ) const
168 {
169  // check for intersection
170  // fixme: move to a method
171  if( ccw( A, aSeg.A, aSeg.B ) != ccw( B, aSeg.A, aSeg.B ) &&
172  ccw( A, B, aSeg.A ) != ccw( A, B, aSeg.B ) )
173  return true;
174 
175 #define CHK( _seg, _pt ) \
176  if( (_seg).PointCloserThan( _pt, aClearance ) ) return true;
177 
178  CHK( *this, aSeg.A );
179  CHK( *this, aSeg.B );
180  CHK( aSeg, A );
181  CHK( aSeg, B );
182 #undef CHK
183 
184  return false;
185 }
186 
187 
188 bool SEG::Contains( const VECTOR2I& aP ) const
189 {
190  return PointCloserThan( aP, 1 );
191 }
extended_type Cross(const VECTOR2< T > &aVector) const
Function Cross() computes cross product of self with aVector.
Definition: vector2d.h:477
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:161
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:132
VECTOR2I::extended_type ecoord
Definition: seg.h:39
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:76
#define abs(a)
Definition: auxiliary.h:84
bool PointCloserThan(const VECTOR2I &aP, int aDist) const
Definition: seg.cpp:34
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:80
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:34
#define CHK(_seg, _pt)
bool Contains(const VECTOR2I &aP) const
Definition: seg.cpp:188
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:374
int sgn(T aVal)
Definition: seg.cpp:28
Definition: seg.h:36
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:485
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
size_t i
Definition: json11.cpp:597
VECTOR2I A
Definition: seg.h:44
bool Collide(const SEG &aSeg, int aClearance) const
Definition: seg.cpp:167
#define min(a, b)
Definition: auxiliary.h:85
VECTOR2I B
Definition: seg.h:45