KiCad PCB EDA Suite
SEG Class Reference

#include <seg.h>

Public Member Functions

 SEG ()
 Default constructor Creates an empty (0, 0) segment, locally-referenced. More...
 
 SEG (int aX1, int aY1, int aX2, int aY2)
 Constructor Creates a segment between (aX1, aY1) and (aX2, aY2), locally referenced. More...
 
 SEG (const VECTOR2I &aA, const VECTOR2I &aB)
 Constructor Creates a segment between (aA) and (aB), locally referenced. More...
 
 SEG (VECTOR2I &aA, VECTOR2I &aB)
 Constructor Creates a segment between (aA) and (aB), referencing the passed points. More...
 
 SEG (VECTOR2I &aA, VECTOR2I &aB, int aIndex)
 Constructor Creates a segment between (aA) and (aB), referencing the passed points. More...
 
 SEG (const VECTOR2I &aA, const VECTOR2I &aB, int aIndex)
 Constructor Creates a segment between (aA) and (aB), referenced to a multi-segment shape. More...
 
 SEG (const SEG &aSeg)
 Copy constructor. More...
 
SEGoperator= (const SEG &aSeg)
 
VECTOR2I LineProject (const VECTOR2I &aP) const
 Function LineProject() More...
 
int Side (const VECTOR2I &aP) const
 Function Side() More...
 
int LineDistance (const VECTOR2I &aP, bool aDetermineSide=false) const
 Function LineDistance() More...
 
const VECTOR2I NearestPoint (const VECTOR2I &aP) const
 Function NearestPoint() More...
 
OPT_VECTOR2I Intersect (const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
 Function Intersect() More...
 
OPT_VECTOR2I IntersectLines (const SEG &aSeg) const
 Function IntersectLines() More...
 
bool Collide (const SEG &aSeg, int aClearance) const
 
ecoord SquaredDistance (const SEG &aSeg) const
 
int Distance (const SEG &aSeg) const
 Function Distance() More...
 
ecoord SquaredDistance (const VECTOR2I &aP) const
 
int Distance (const VECTOR2I &aP) const
 Function Distance() More...
 
void CanonicalCoefs (ecoord &qA, ecoord &qB, ecoord &qC) const
 
bool Collinear (const SEG &aSeg) const
 Function Collinear() More...
 
bool ApproxCollinear (const SEG &aSeg) const
 
bool ApproxParallel (const SEG &aSeg) const
 
bool Overlaps (const SEG &aSeg) const
 
int Length () const
 Function Length() More...
 
ecoord SquaredLength () const
 
ecoord TCoef (const VECTOR2I &aP) const
 
int Index () const
 Function Index() More...
 
bool Contains (const VECTOR2I &aP) const
 
bool PointCloserThan (const VECTOR2I &aP, int aDist) const
 
void Reverse ()
 

Public Attributes

VECTOR2IA
 
VECTOR2IB
 

Private Types

typedef VECTOR2I::extended_type ecoord
 

Private Member Functions

bool ccw (const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
 

Private Attributes

VECTOR2I m_a
 
VECTOR2I m_b
 
int m_index
 

index withing the parent shape (used when m_is_local == false)

More...
 

Friends

std::ostream & operator<< (std::ostream &aStream, const SEG &aSeg)
 

Detailed Description

Definition at line 37 of file seg.h.

Member Typedef Documentation

Definition at line 40 of file seg.h.

Constructor & Destructor Documentation

SEG::SEG ( )
inline

Default constructor Creates an empty (0, 0) segment, locally-referenced.

Definition at line 57 of file seg.h.

References m_index.

57  :
58  A( m_a ),
59  B( m_b )
60  {
61  m_index = -1;
62  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
VECTOR2I m_b
Definition: seg.h:42
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:361
VECTOR2I m_a
Definition: seg.h:41
SEG::SEG ( int  aX1,
int  aY1,
int  aX2,
int  aY2 
)
inline

Constructor Creates a segment between (aX1, aY1) and (aX2, aY2), locally referenced.

Definition at line 68 of file seg.h.

References m_index.

68  :
69  A( m_a ),
70  B( m_b )
71  {
72  A = VECTOR2I( aX1, aY1 );
73  B = VECTOR2I( aX2, aY2 );
74  m_index = -1;
75  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590
VECTOR2I & A
Definition: seg.h:51
VECTOR2I m_b
Definition: seg.h:42
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:361
VECTOR2I m_a
Definition: seg.h:41
SEG::SEG ( const VECTOR2I aA,
const VECTOR2I aB 
)
inline

Constructor Creates a segment between (aA) and (aB), locally referenced.

Definition at line 81 of file seg.h.

References m_index.

81  :
82  A( m_a ),
83  B( m_b )
84  {
85  A = aA;
86  B = aB;
87  m_index = -1;
88  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
VECTOR2I m_b
Definition: seg.h:42
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:361
VECTOR2I m_a
Definition: seg.h:41
SEG::SEG ( VECTOR2I aA,
VECTOR2I aB 
)
inline

Constructor Creates a segment between (aA) and (aB), referencing the passed points.

Definition at line 94 of file seg.h.

References m_index.

94  :
95  A( aA ),
96  B( aB )
97  {
98  m_index = -1;
99  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:361
SEG::SEG ( VECTOR2I aA,
VECTOR2I aB,
int  aIndex 
)
inline

Constructor Creates a segment between (aA) and (aB), referencing the passed points.

Definition at line 105 of file seg.h.

References m_index.

105  :
106  A( aA ),
107  B( aB )
108  {
109  m_index = aIndex;
110  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:361
SEG::SEG ( const VECTOR2I aA,
const VECTOR2I aB,
int  aIndex 
)
inline

Constructor Creates a segment between (aA) and (aB), referenced to a multi-segment shape.

Parameters
aAreference to the start point in the parent shape
aBreference to the end point in the parent shape
aIndexindex of the segment within the parent shape

Definition at line 119 of file seg.h.

References m_index.

119  :
120  A( m_a ),
121  B( m_b )
122  {
123  A = aA;
124  B = aB;
125  m_index = aIndex;
126  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
VECTOR2I m_b
Definition: seg.h:42
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:361
VECTOR2I m_a
Definition: seg.h:41
SEG::SEG ( const SEG aSeg)
inline

Copy constructor.

Definition at line 131 of file seg.h.

References A, and B.

131  :
132  A( m_a ),
133  B( m_b ),
134  m_index( aSeg.m_index )
135  {
136  A = aSeg.A;
137  B = aSeg.B;
138  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
VECTOR2I m_b
Definition: seg.h:42
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:361
VECTOR2I m_a
Definition: seg.h:41

Member Function Documentation

bool SEG::ApproxCollinear ( const SEG aSeg) const
inline

Definition at line 275 of file seg.h.

References A, abs, B, CanonicalCoefs(), VECTOR2< T >::x, and VECTOR2< T >::y.

276  {
277  ecoord p, q, r;
278  CanonicalCoefs( p, q, r );
279 
280  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
281  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
282 
283  return std::abs( dist1 ) <= 1 && std::abs( dist2 ) <= 1;
284  }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:250
VECTOR2I & B
Definition: seg.h:52
#define abs(a)
Definition: auxiliary.h:84
VECTOR2I & A
Definition: seg.h:51
VECTOR2I::extended_type ecoord
bool SEG::ApproxParallel ( const SEG aSeg) const
inline

Definition at line 286 of file seg.h.

References A, abs, B, CanonicalCoefs(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by PNS::MEANDERED_LINE::CheckSelfIntersections(), PNS::DIFF_PAIR::CoupledLength(), PNS::DIFF_PAIR::CoupledSegmentPairs(), and PNS::findCoupledVertices().

287  {
288  ecoord p, q, r;
289  CanonicalCoefs( p, q, r );
290 
291  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
292  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
293 
294  return std::abs( dist1 - dist2 ) <= 1;
295  }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:250
VECTOR2I & B
Definition: seg.h:52
#define abs(a)
Definition: auxiliary.h:84
VECTOR2I & A
Definition: seg.h:51
VECTOR2I::extended_type ecoord
void SEG::CanonicalCoefs ( ecoord qA,
ecoord qB,
ecoord qC 
) const
inline

Definition at line 250 of file seg.h.

References VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by ApproxCollinear(), ApproxParallel(), and Collinear().

251  {
252  qA = A.y - B.y;
253  qB = B.x - A.x;
254  qC = -qA * A.x - qB * A.y;
255  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
bool SEG::ccw ( const VECTOR2I aA,
const VECTOR2I aB,
const VECTOR2I aC 
) const
private

Definition at line 128 of file seg.cpp.

References VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by Collide().

129 {
130  return (ecoord) ( aC.y - aA.y ) * ( aB.x - aA.x ) > (ecoord) ( aB.y - aA.y ) * ( aC.x - aA.x );
131 }
VECTOR2I::extended_type ecoord
VECTOR2I::extended_type ecoord
Definition: seg.h:40
bool SEG::Collide ( const SEG aSeg,
int  aClearance 
) const

Definition at line 134 of file seg.cpp.

References A, B, ccw(), and CHK.

Referenced by SHAPE_LINE_CHAIN::Collide(), SHAPE_POLY_SET::IsPolygonSelfIntersecting(), and BOARD::TestAreaIntersection().

135 {
136  // check for intersection
137  // fixme: move to a method
138  if( ccw( A, aSeg.A, aSeg.B ) != ccw( B, aSeg.A, aSeg.B ) &&
139  ccw( A, B, aSeg.A ) != ccw( A, B, aSeg.B ) )
140  return true;
141 
142 #define CHK( _seg, _pt ) \
143  if( (_seg).PointCloserThan( _pt, aClearance ) ) return true;
144 
145  CHK( *this, aSeg.A );
146  CHK( *this, aSeg.B );
147  CHK( aSeg, A );
148  CHK( aSeg, B );
149 #undef CHK
150 
151  return false;
152 }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:128
#define CHK(_seg, _pt)
bool SEG::Collinear ( const SEG aSeg) const
inline

Function Collinear()

Checks if segment aSeg lies on the same line as (this).

Parameters
aSegthe segment to chech colinearity with
Returns
true, when segments are collinear.

Definition at line 264 of file seg.h.

References A, abs, B, CanonicalCoefs(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by PNS::DP_GATEWAYS::BuildGeneric(), SHAPE_LINE_CHAIN::Intersect(), and Overlaps().

265  {
266  ecoord qa, qb, qc;
267  CanonicalCoefs( qa, qb, qc );
268 
269  ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
270  ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
271 
272  return ( d1 <= 1 && d2 <= 1 );
273  }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:250
VECTOR2I & B
Definition: seg.h:52
#define abs(a)
Definition: auxiliary.h:84
VECTOR2I & A
Definition: seg.h:51
VECTOR2I::extended_type ecoord
bool SEG::Contains ( const VECTOR2I aP) const

Definition at line 155 of file seg.cpp.

References PointCloserThan().

Referenced by GRID_HELPER::AlignToSegment(), PNS::LINE::ChangedArea(), SHAPE_LINE_CHAIN::Intersect(), PNS::MEANDERED_LINE::MeanderSegment(), and Overlaps().

156 {
157  return PointCloserThan( aP, 1 );
158 }
bool PointCloserThan(const VECTOR2I &aP, int aDist) const
Definition: seg.cpp:34
int SEG::Distance ( const SEG aSeg) const
inline
int SEG::Distance ( const VECTOR2I aP) const
inline

Function Distance()

Computes minimum Euclidean distance to point aP.

Parameters
aPthe point
Returns
minimum distance

Definition at line 245 of file seg.h.

References SquaredDistance().

246  {
247  return sqrt( SquaredDistance( aP ) );
248  }
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:76
int SEG::Index ( ) const
inline

Function Index()

Return the index of this segment in its parent shape (applicable only to non-local segments)

Returns
index value

Definition at line 343 of file seg.h.

References m_index.

Referenced by PNS::OPTIMIZER::mergeDpStep(), PNS::OPTIMIZER::mergeObtuse(), PNS::OPTIMIZER::mergeStep(), and PNS::LINE::Walkaround().

344  {
345  return m_index;
346  }
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:361
OPT_VECTOR2I SEG::Intersect ( const SEG aSeg,
bool  aIgnoreEndpoints = false,
bool  aLines = false 
) const

Function Intersect()

Computes intersection point of segment (this) with segment aSeg.

Parameters
aSegsegment to intersect with
aIgnoreEndpointsdon't treat corner cases (i.e. end of one segment touching the other) as intersections.
aLinestreat segments as infinite lines
Returns
intersection point, if exists

Definition at line 99 of file seg.cpp.

References A, B, VECTOR2< T >::Cross(), rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by EC_CONVERGING::Apply(), PNS::LINE::DragSegment(), SHAPE_LINE_CHAIN::Intersect(), IntersectLines(), SHAPE_LINE_CHAIN::SelfIntersecting(), and SquaredDistance().

100 {
101  const VECTOR2I e( B - A );
102  const VECTOR2I f( aSeg.B - aSeg.A );
103  const VECTOR2I ac( aSeg.A - A );
104 
105  ecoord d = f.Cross( e );
106  ecoord p = f.Cross( ac );
107  ecoord q = e.Cross( ac );
108 
109  if( d == 0 )
110  return OPT_VECTOR2I();
111 
112  if( !aLines && d > 0 && ( q < 0 || q > d || p < 0 || p > d ) )
113  return OPT_VECTOR2I();
114 
115  if( !aLines && d < 0 && ( q < d || p < d || p > 0 || q > 0 ) )
116  return OPT_VECTOR2I();
117 
118  if( !aLines && aIgnoreEndpoints && ( q == 0 || q == d ) && ( p == 0 || p == d ) )
119  return OPT_VECTOR2I();
120 
121  VECTOR2I ip( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
122  aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
123 
124  return ip;
125 }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
VECTOR2I::extended_type ecoord
boost::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:35
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: math_util.cpp:32
OPT_VECTOR2I SEG::IntersectLines ( const SEG aSeg) const
inline

Function IntersectLines()

Computes the intersection point of lines passing through ends of (this) and aSeg

Parameters
aSegsegment defining the line to intersect with
Returns
intersection point, if exists

Definition at line 212 of file seg.h.

References Intersect().

Referenced by GRID_HELPER::AlignToSegment(), PNS::DP_GATEWAYS::BuildGeneric(), PNS::ConvexHull(), PNS::OPTIMIZER::mergeObtuse(), and PNS::LINE::snapDraggedCorner().

213  {
214  return Intersect( aSeg, false, true );
215  }
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:99
int SEG::Length ( ) const
inline

Function Length()

Returns the length (this)

Returns
length

Definition at line 325 of file seg.h.

Referenced by PNS::MEANDER_SHAPE::BaselineLength(), PNS::DIFF_PAIR::CoupledLength(), PNS::dragCornerInternal(), PNS::LINE::Is45Degree(), SHAPE_LINE_CHAIN::Length(), PNS::MEANDERED_LINE::MeanderSegment(), SHAPE_LINE_CHAIN::PathLength(), and SHAPE_LINE_CHAIN::PointAlong().

326  {
327  return ( A - B ).EuclideanNorm();
328  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
int SEG::LineDistance ( const VECTOR2I aP,
bool  aDetermineSide = false 
) const
inline

Function LineDistance()

Returns the closest Euclidean distance between point aP and the line defined by the ends of segment (this).

Parameters
aDetermineSidewhen true, the sign of the returned value indicates the side of the line at which we are (negative = left)
Returns
the distance

Definition at line 380 of file seg.h.

References A, abs, B, dist, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by GERBER_PLOTTER::FlashPadTrapez(), SHAPE_LINE_CHAIN::NearestPoint(), and PNS::LINE::snapToNeighbourSegments().

381 {
382  ecoord p = A.y - B.y;
383  ecoord q = B.x - A.x;
384  ecoord r = -p * A.x - q * A.y;
385 
386  ecoord dist = ( p * aP.x + q * aP.y + r ) / sqrt( p * p + q * q );
387 
388  return aDetermineSide ? dist : std::abs( dist );
389 }
VECTOR2I & B
Definition: seg.h:52
static const int dist[10][10]
Definition: dist.cpp:57
#define abs(a)
Definition: auxiliary.h:84
VECTOR2I & A
Definition: seg.h:51
VECTOR2I::extended_type ecoord
VECTOR2I SEG::LineProject ( const VECTOR2I aP) const
inline

Function LineProject()

Computes the perpendicular projection point of aP on a line passing through ends of the segment.

Parameters
aPpoint to project
Returns
projected point

Definition at line 364 of file seg.h.

References A, B, VECTOR2< T >::Dot(), rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by PNS::LINE_PLACER::buildInitialLine(), PNS::DP_GATEWAYS::BuildOrthoProjections(), PNS::commonParallelProjection(), PNS::findCoupledVertices(), PNS::LINE_PLACER::rhStopAtNearestObstacle(), PNS::DIFF_PAIR_PLACER::routeHead(), and PNS::MEANDER_SHAPE::updateBaseSegment().

365 {
366  VECTOR2I d = B - A;
367  ecoord l_squared = d.Dot( d );
368 
369  if( l_squared == 0 )
370  return A;
371 
372  ecoord t = d.Dot( aP - A );
373 
374  int xp = rescale( t, (ecoord)d.x, l_squared );
375  int yp = rescale( t, (ecoord)d.y, l_squared );
376 
377  return A + VECTOR2I( xp, yp );
378 }
VECTOR2I & B
Definition: seg.h:52
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:488
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590
VECTOR2I & A
Definition: seg.h:51
VECTOR2I::extended_type ecoord
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: math_util.cpp:32
const VECTOR2I SEG::NearestPoint ( const VECTOR2I aP) const
inline

Function NearestPoint()

Computes a point on the segment (this) that is closest to point aP.

Returns
: nearest point

Definition at line 397 of file seg.h.

References A, B, VECTOR2< T >::Dot(), rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by POINT_EDITOR::addCorner(), Collide(), SHAPE_LINE_CHAIN::NearestPoint(), pushoutForce(), PNS::WALKAROUND::Route(), SquaredDistance(), PNS::MEANDER_SKEW_PLACER::Start(), PNS::MEANDER_PLACER::Start(), and PNS::DP_MEANDER_PLACER::Start().

398 {
399  VECTOR2I d = B - A;
400  ecoord l_squared = d.Dot( d );
401 
402  if( l_squared == 0 )
403  return A;
404 
405  ecoord t = d.Dot( aP - A );
406 
407  if( t < 0 )
408  return A;
409  else if( t > l_squared )
410  return B;
411 
412  int xp = rescale( t, (ecoord)d.x, l_squared );
413  int yp = rescale( t, (ecoord)d.y, l_squared );
414 
415  return A + VECTOR2I( xp, yp );
416 }
VECTOR2I & B
Definition: seg.h:52
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:488
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590
VECTOR2I & A
Definition: seg.h:51
VECTOR2I::extended_type ecoord
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: math_util.cpp:32
SEG& SEG::operator= ( const SEG aSeg)
inline

Definition at line 140 of file seg.h.

References A, B, and m_index.

141  {
142  A = aSeg.A;
143  B = aSeg.B;
144  m_index = aSeg.m_index;
145 
146  return *this;
147  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:361
bool SEG::Overlaps ( const SEG aSeg) const
inline

Definition at line 298 of file seg.h.

References A, B, Collinear(), and Contains().

299  {
300  if( aSeg.A == aSeg.B ) // single point corner case
301  {
302  if( A == aSeg.A || B == aSeg.A )
303  return false;
304 
305  return Contains( aSeg.A );
306  }
307 
308  if( !Collinear( aSeg ) )
309  return false;
310 
311  if( Contains( aSeg.A ) || Contains( aSeg.B ) )
312  return true;
313  if( aSeg.Contains( A ) || aSeg.Contains( B ) )
314  return true;
315 
316  return false;
317  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
bool Collinear(const SEG &aSeg) const
Function Collinear()
Definition: seg.h:264
bool Contains(const VECTOR2I &aP) const
Definition: seg.cpp:155
bool SEG::PointCloserThan ( const VECTOR2I aP,
int  aDist 
) const

Definition at line 34 of file seg.cpp.

References A, abs, B, VECTOR2< T >::Dot(), rescale(), sgn(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by Contains(), and DRC::testTexts().

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 }
VECTOR2I & B
Definition: seg.h:52
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:488
#define abs(a)
Definition: auxiliary.h:84
VECTOR2I & A
Definition: seg.h:51
VECTOR2I::extended_type ecoord
int sgn(T aVal)
Definition: seg.cpp:28
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: math_util.cpp:32
VECTOR2I::extended_type ecoord
Definition: seg.h:40
void SEG::Reverse ( )
inline

Definition at line 352 of file seg.h.

Referenced by PNS::LINE_RESTRICTIONS::allowedAngles().

353  {
354  std::swap( A, B );
355  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
int SEG::Side ( const VECTOR2I aP) const
inline

Function Side()

Determines on which side of directed line passing via segment ends point aP lies.

Parameters
aPpoint to determine the orientation wrs to self
Returns
: < 0: left, 0 : on the line, > 0 : right

Definition at line 166 of file seg.h.

References A, and p2t::Cross().

Referenced by PNS::DP_GATEWAYS::buildDpContinuation(), PNS::pairOrientation(), SHAPE_LINE_CHAIN::PointInside(), and PNS::SegmentHull().

167  {
168  const ecoord det = ( B - A ).Cross( aP - A );
169 
170  return det < 0 ? -1 : ( det > 0 ? 1 : 0 );
171  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
VECTOR2I::extended_type ecoord
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::ecoord SEG::SquaredDistance ( const SEG aSeg) const

Definition at line 76 of file seg.cpp.

References A, B, VECTOR2_TRAITS< int >::ECOORD_MAX, Intersect(), min, and NearestPoint().

Referenced by Distance().

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 }
VECTOR2I & B
Definition: seg.h:52
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:99
VECTOR2I & A
Definition: seg.h:51
static const extended_type ECOORD_MAX
Definition: vector2d.h:59
VECTOR2I::extended_type ecoord
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:397
#define min(a, b)
Definition: auxiliary.h:85
ecoord SEG::SquaredDistance ( const VECTOR2I aP) const
inline

Definition at line 233 of file seg.h.

References NearestPoint().

234  {
235  return ( NearestPoint( aP ) - aP ).SquaredEuclideanNorm();
236  }
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:397
ecoord SEG::SquaredLength ( ) const
inline

Definition at line 330 of file seg.h.

Referenced by PNS::commonParallelProjection().

331  {
332  return ( A - B ).SquaredEuclideanNorm();
333  }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51
SEG::ecoord SEG::TCoef ( const VECTOR2I aP) const
inline

Definition at line 391 of file seg.h.

References A, B, and VECTOR2< T >::Dot().

Referenced by PNS::commonParallelProjection().

392 {
393  VECTOR2I d = B - A;
394  return d.Dot( aP - A);
395 }
VECTOR2I & B
Definition: seg.h:52
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:488
VECTOR2I & A
Definition: seg.h:51

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  aStream,
const SEG aSeg 
)
friend

Definition at line 418 of file seg.h.

419 {
420  aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
421 
422  return aStream;
423 }
VECTOR2I & B
Definition: seg.h:52
VECTOR2I & A
Definition: seg.h:51

Member Data Documentation

VECTOR2I& SEG::A

Definition at line 51 of file seg.h.

Referenced by PNS::NODE::Add(), PNS_KICAD_IFACE::AddItem(), PNS_PCBNEW_DEBUG_DECORATOR::AddSegment(), PNS::NODE::addSegment(), GRID_HELPER::AlignToSegment(), PNS::LINE_RESTRICTIONS::allowedAngles(), PNS::SEGMENT::Anchor(), PNS::DP_PRIMITIVE_PAIR::anchorDirection(), ApproxCollinear(), PNS::ApproximateSegmentAsRect(), ApproxParallel(), PNS::DP_MEANDER_PLACER::baselineSegment(), SHAPE_SEGMENT::BBox(), PNS::DP_GATEWAYS::BuildFromPrimitivePair(), PNS::SHOVE::checkBumpDirection(), PNS::SEGMENT::CLine(), SHAPE_RECT::Collide(), Collide(), SHAPE_LINE_CHAIN::Collide(), Collinear(), PNS::commonParallelProjection(), PNS::TOPOLOGY::ConnectedJoints(), PNS::DP_PRIMITIVE_PAIR::CursorOrientation(), PNS::MEANDER_PLACER_BASE::cutTunedLine(), DIRECTION_45::DIRECTION_45(), SHAPE_POLY_SET::DistanceToPolygon(), DRC::doEdgeZoneDrc(), PNS::MEANDER_PLACER::doMove(), PNS::LINE::DragSegment(), ZONE_CONTAINER::Draw(), ROUTER_PREVIEW_ITEM::drawLineChain(), PNS::LOGGER::dumpShape(), PNS::NODE::findRedundantSegment(), PNS::MEANDER_SHAPE::Fit(), PNS::NODE::followLine(), PNS::DIFF_PAIR_PLACER::getDanglingAnchor(), PNS::LINE_PLACER::handlePullback(), PNS::LINE_PLACER::handleSelfIntersections(), ZONE_CONTAINER::Hatch(), Intersect(), SHAPE_LINE_CHAIN::Intersect(), PNS::LINE::Is45Degree(), LineDistance(), LineProject(), PNS::LOGGER::Log(), PNS::MEANDER_SHAPE::MakeCorner(), PNS::MEANDER_SHAPE::MakeEmpty(), PNS::MEANDERED_LINE::MeanderSegment(), PNS::OPTIMIZER::mergeDpStep(), PNS::LINE_PLACER::mergeHead(), PNS::OPTIMIZER::mergeObtuse(), PNS::OPTIMIZER::mergeStep(), SHAPE_SEGMENT::Move(), PNS::MoveDiagonal(), ZONE_CONTAINER::MoveEdge(), NearestPoint(), operator<<(), operator=(), Overlaps(), PNS::pairOrientation(), SHAPE_LINE_CHAIN::PathLength(), SHAPE_LINE_CHAIN::PointAlong(), PointCloserThan(), SHAPE_LINE_CHAIN::PointInside(), SHAPE_LINE_CHAIN::PointOnEdge(), PNS::MEANDER_SHAPE::Recalculate(), PNS::LINE_PLACER::reduceTail(), PNS::MEANDER_SHAPE::reflect(), PNS::NODE::removeSegmentIndex(), PNS::WALKAROUND::Route(), SEG(), PNS::SegmentHull(), SHAPE_LINE_CHAIN::SelfIntersecting(), Side(), PNS::TOOL_BASE::snapToItem(), PNS::LINE::snapToNeighbourSegments(), SHAPE_LINE_CHAIN::Split(), PNS::LINE_PLACER::splitAdjacentSegments(), SquaredDistance(), PNS::DRAGGER::startDragSegment(), PNS::SEGMENT::SwapEnds(), TCoef(), BOARD::Test_Drc_Areas_Outlines_To_Areas_Outlines(), PNS::MEANDER_SHAPE::updateBaseSegment(), and ROUTER_PREVIEW_ITEM::ViewDraw().

VECTOR2I& SEG::B

Definition at line 52 of file seg.h.

Referenced by PNS::NODE::Add(), PNS_KICAD_IFACE::AddItem(), PNS::MEANDERED_LINE::AddMeander(), PNS_PCBNEW_DEBUG_DECORATOR::AddSegment(), PNS::NODE::addSegment(), GRID_HELPER::AlignToSegment(), PNS::SEGMENT::Anchor(), PNS::DP_PRIMITIVE_PAIR::anchorDirection(), ApproxCollinear(), PNS::ApproximateSegmentAsRect(), ApproxParallel(), PNS::DP_MEANDER_PLACER::baselineSegment(), SHAPE_SEGMENT::BBox(), PNS::DP_GATEWAYS::BuildFromPrimitivePair(), PNS::SHOVE::checkBumpDirection(), PNS::SEGMENT::CLine(), SHAPE_RECT::Collide(), Collide(), SHAPE_LINE_CHAIN::Collide(), Collinear(), PNS::commonParallelProjection(), PNS::TOPOLOGY::ConnectedJoints(), PNS::DP_PRIMITIVE_PAIR::CursorOrientation(), PNS::MEANDER_PLACER_BASE::cutTunedLine(), DIRECTION_45::DIRECTION_45(), DRC::doEdgeZoneDrc(), PNS::MEANDER_PLACER::doMove(), PNS::LINE::DragSegment(), ZONE_CONTAINER::Draw(), ROUTER_PREVIEW_ITEM::drawLineChain(), PNS::LOGGER::dumpShape(), PNS::MEANDER_SHAPE::End(), PNS::NODE::findRedundantSegment(), PNS::MEANDER_SHAPE::Fit(), PNS::NODE::followLine(), PNS::DIFF_PAIR_PLACER::getDanglingAnchor(), ZONE_CONTAINER::Hatch(), Intersect(), SHAPE_LINE_CHAIN::Intersect(), PNS::LINE::Is45Degree(), LineDistance(), LineProject(), PNS::LOGGER::Log(), PNS::MEANDER_SHAPE::MakeCorner(), PNS::MEANDER_SHAPE::MakeEmpty(), PNS::MEANDERED_LINE::MeanderSegment(), PNS::OPTIMIZER::mergeDpStep(), PNS::LINE_PLACER::mergeHead(), PNS::OPTIMIZER::mergeObtuse(), PNS::OPTIMIZER::mergeStep(), SHAPE_SEGMENT::Move(), PNS::MoveDiagonal(), ZONE_CONTAINER::MoveEdge(), NearestPoint(), operator<<(), operator=(), Overlaps(), SHAPE_LINE_CHAIN::PointAlong(), PointCloserThan(), SHAPE_LINE_CHAIN::PointInside(), SHAPE_LINE_CHAIN::PointOnEdge(), PNS::MEANDER_SHAPE::Recalculate(), PNS::MEANDER_SHAPE::reflect(), PNS::NODE::removeSegmentIndex(), PNS::WALKAROUND::Route(), SEG(), PNS::SegmentHull(), SHAPE_LINE_CHAIN::SelfIntersecting(), PNS::TOOL_BASE::snapToItem(), SHAPE_LINE_CHAIN::Split(), PNS::LINE_PLACER::splitAdjacentSegments(), SquaredDistance(), PNS::DRAGGER::startDragSegment(), PNS::SEGMENT::SwapEnds(), TCoef(), BOARD::Test_Drc_Areas_Outlines_To_Areas_Outlines(), PNS::MEANDER_SHAPE::updateBaseSegment(), and ROUTER_PREVIEW_ITEM::ViewDraw().

VECTOR2I SEG::m_a
private

Definition at line 41 of file seg.h.

VECTOR2I SEG::m_b
private

Definition at line 42 of file seg.h.

int SEG::m_index
private

index withing the parent shape (used when m_is_local == false)

Definition at line 361 of file seg.h.

Referenced by Index(), operator=(), and SEG().


The documentation for this class was generated from the following files: