KiCad PCB EDA Suite
SEG Class Reference

#include <seg.h>

Public Types

using ecoord = VECTOR2I::extended_type
 

Public Member Functions

 SEG ()
 Default constructor Creates an empty (0, 0) segment. More...
 
 SEG (int aX1, int aY1, int aX2, int aY2)
 Constructor Creates a segment between (aX1, aY1) and (aX2, aY2) More...
 
 SEG (const VECTOR2I &aA, const VECTOR2I &aB)
 Constructor Creates a segment between (aA) and (aB) 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)
 
bool operator== (const SEG &aSeg) const
 
bool operator!= (const SEG &aSeg) const
 
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...
 
const VECTOR2I NearestPoint (const SEG &aSeg) const
 Computes a point on the segment (this) that is closest to any point on aSeg. 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 ()
 
VECTOR2I Center () const
 

Returns the center point of the line

More...
 

Public Attributes

VECTOR2I A
 
VECTOR2I B
 

Private Member Functions

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

Private Attributes

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 39 of file seg.h.

Member Typedef Documentation

◆ ecoord

Definition at line 42 of file seg.h.

Constructor & Destructor Documentation

◆ SEG() [1/5]

SEG::SEG ( )
inline

Default constructor Creates an empty (0, 0) segment.

Definition at line 53 of file seg.h.

54  {
55  m_index = -1;
56  }
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:341

References m_index.

◆ SEG() [2/5]

SEG::SEG ( int  aX1,
int  aY1,
int  aX2,
int  aY2 
)
inline

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

Definition at line 62 of file seg.h.

62  :
63  A ( VECTOR2I( aX1, aY1 ) ),
64  B ( VECTOR2I( aX2, aY2 ) )
65  {
66  m_index = -1;
67  }
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
VECTOR2I A
Definition: seg.h:47
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:341
VECTOR2I B
Definition: seg.h:48

References m_index.

◆ SEG() [3/5]

SEG::SEG ( const VECTOR2I aA,
const VECTOR2I aB 
)
inline

Constructor Creates a segment between (aA) and (aB)

Definition at line 73 of file seg.h.

73  : A( aA ), B( aB )
74  {
75  m_index = -1;
76  }
VECTOR2I A
Definition: seg.h:47
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:341
VECTOR2I B
Definition: seg.h:48

References m_index.

◆ SEG() [4/5]

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 85 of file seg.h.

85  : A( aA ), B( aB )
86  {
87  m_index = aIndex;
88  }
VECTOR2I A
Definition: seg.h:47
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:341
VECTOR2I B
Definition: seg.h:48

References m_index.

◆ SEG() [5/5]

SEG::SEG ( const SEG aSeg)
inline

Copy constructor.

Definition at line 93 of file seg.h.

93  : A( aSeg.A ), B( aSeg.B ), m_index( aSeg.m_index )
94  {
95  }
VECTOR2I A
Definition: seg.h:47
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:341
VECTOR2I B
Definition: seg.h:48

Member Function Documentation

◆ ApproxCollinear()

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

Definition at line 249 of file seg.h.

250  {
251  ecoord p, q, r;
252  CanonicalCoefs( p, q, r );
253 
254  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
255  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
256 
257  return std::abs( dist1 ) <= 1 && std::abs( dist2 ) <= 1;
258  }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:224
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

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

Referenced by TRACK::ApproxCollinear().

◆ ApproxParallel()

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

Definition at line 260 of file seg.h.

261  {
262  ecoord p, q, r;
263  CanonicalCoefs( p, q, r );
264 
265  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
266  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
267 
268  return std::abs( dist1 - dist2 ) <= 1;
269  }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:224
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, 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().

◆ CanonicalCoefs()

void SEG::CanonicalCoefs ( ecoord qA,
ecoord qB,
ecoord qC 
) const
inline

Definition at line 224 of file seg.h.

225  {
226  qA = A.y - B.y;
227  qB = B.x - A.x;
228  qC = -qA * A.x - qB * A.y;
229  }
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

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

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

◆ ccw()

bool SEG::ccw ( const VECTOR2I aA,
const VECTOR2I aB,
const VECTOR2I aC 
) const
private

Definition at line 173 of file seg.cpp.

174 {
175  return (ecoord) ( aC.y - aA.y ) * ( aB.x - aA.x ) > (ecoord) ( aB.y - aA.y ) * ( aC.x - aA.x );
176 }
VECTOR2I::extended_type ecoord
Definition: seg.h:42
VECTOR2I::extended_type ecoord

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

Referenced by Collide().

◆ Center()

VECTOR2I SEG::Center ( ) const
inline

Returns the center point of the line

Definition at line 332 of file seg.h.

333  {
334  return A + ( B - A ) / 2;
335  }
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, and B.

Referenced by SCH_EAGLE_PLUGIN::findNearestLinePoint().

◆ Collide()

bool SEG::Collide ( const SEG aSeg,
int  aClearance 
) const

Definition at line 179 of file seg.cpp.

180 {
181  // check for intersection
182  // fixme: move to a method
183  if( ccw( A, aSeg.A, aSeg.B ) != ccw( B, aSeg.A, aSeg.B ) &&
184  ccw( A, B, aSeg.A ) != ccw( A, B, aSeg.B ) )
185  return true;
186 
187 #define CHK( _seg, _pt ) \
188  if( (_seg).PointCloserThan( _pt, aClearance ) ) return true;
189 
190  CHK( *this, aSeg.A );
191  CHK( *this, aSeg.B );
192  CHK( aSeg, A );
193  CHK( aSeg, B );
194 #undef CHK
195 
196  return false;
197 }
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:173
#define CHK(_seg, _pt)
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

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

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

◆ Collinear()

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 238 of file seg.h.

239  {
240  ecoord qa, qb, qc;
241  CanonicalCoefs( qa, qb, qc );
242 
243  ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
244  ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
245 
246  return ( d1 <= 1 && d2 <= 1 );
247  }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:224
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

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

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

◆ Contains()

bool SEG::Contains ( const VECTOR2I aP) const

Definition at line 200 of file seg.cpp.

201 {
202  return PointCloserThan( aP, 1 );
203 }
bool PointCloserThan(const VECTOR2I &aP, int aDist) const
Definition: seg.cpp:37

References PointCloserThan().

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

◆ Distance() [1/2]

◆ Distance() [2/2]

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 219 of file seg.h.

220  {
221  return sqrt( SquaredDistance( aP ) );
222  }
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:88

References SquaredDistance().

◆ Index()

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 317 of file seg.h.

318  {
319  return m_index;
320  }
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:341

References m_index.

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

◆ Intersect()

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 144 of file seg.cpp.

145 {
146  const VECTOR2I e( B - A );
147  const VECTOR2I f( aSeg.B - aSeg.A );
148  const VECTOR2I ac( aSeg.A - A );
149 
150  ecoord d = f.Cross( e );
151  ecoord p = f.Cross( ac );
152  ecoord q = e.Cross( ac );
153 
154  if( d == 0 )
155  return OPT_VECTOR2I();
156 
157  if( !aLines && d > 0 && ( q < 0 || q > d || p < 0 || p > d ) )
158  return OPT_VECTOR2I();
159 
160  if( !aLines && d < 0 && ( q < d || p < d || p > 0 || q > 0 ) )
161  return OPT_VECTOR2I();
162 
163  if( !aLines && aIgnoreEndpoints && ( q == 0 || q == d ) && ( p == 0 || p == d ) )
164  return OPT_VECTOR2I();
165 
166  VECTOR2I ip( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
167  aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
168 
169  return ip;
170 }
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:37
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:47
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:84
VECTOR2I B
Definition: seg.h:48

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

Referenced by EC_CONVERGING::Apply(), SHAPE_POLY_SET::Collide(), PNS::LINE::dragSegment45(), SHAPE_LINE_CHAIN::Intersect(), IntersectLines(), SCH_EAGLE_PLUGIN::loadSegments(), NearestPoint(), SHAPE_LINE_CHAIN::SelfIntersecting(), and SquaredDistance().

◆ IntersectLines()

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 186 of file seg.h.

187  {
188  return Intersect( aSeg, false, true );
189  }
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:144

References Intersect().

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

◆ Length()

int SEG::Length ( ) const
inline

Function Length()

Returns the length (this)

Returns
length

Definition at line 299 of file seg.h.

300  {
301  return ( A - B ).EuclideanNorm();
302  }
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, and B.

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().

◆ LineDistance()

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
aPthe point to test
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 360 of file seg.h.

361 {
362  ecoord p = A.y - B.y;
363  ecoord q = B.x - A.x;
364  ecoord r = -p * A.x - q * A.y;
365 
366  ecoord dist = ( p * aP.x + q * aP.y + r ) / sqrt( p * p + q * q );
367 
368  return aDetermineSide ? dist : std::abs( dist );
369 }
static const int dist[10][10]
Definition: ar_matrix.cpp:326
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

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

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

◆ LineProject()

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 344 of file seg.h.

345 {
346  VECTOR2I d = B - A;
347  ecoord l_squared = d.Dot( d );
348 
349  if( l_squared == 0 )
350  return A;
351 
352  ecoord t = d.Dot( aP - A );
353 
354  int xp = rescale( t, (ecoord)d.x, l_squared );
355  int yp = rescale( t, (ecoord)d.y, l_squared );
356 
357  return A + VECTOR2I( xp, yp );
358 }
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
VECTOR2I::extended_type ecoord
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:492
VECTOR2I A
Definition: seg.h:47
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:84
VECTOR2I B
Definition: seg.h:48

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().

◆ NearestPoint() [1/2]

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
the nearest point

Definition at line 377 of file seg.h.

378 {
379  VECTOR2I d = B - A;
380  ecoord l_squared = d.Dot( d );
381 
382  if( l_squared == 0 )
383  return A;
384 
385  ecoord t = d.Dot( aP - A );
386 
387  if( t < 0 )
388  return A;
389  else if( t > l_squared )
390  return B;
391 
392  int xp = rescale( t, (ecoord)d.x, l_squared );
393  int yp = rescale( t, (ecoord)d.y, l_squared );
394 
395  return A + VECTOR2I( xp, yp );
396 }
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
VECTOR2I::extended_type ecoord
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:492
VECTOR2I A
Definition: seg.h:47
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:84
VECTOR2I B
Definition: seg.h:48

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

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

◆ NearestPoint() [2/2]

const VECTOR2I SEG::NearestPoint ( const SEG aSeg) const

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

Returns
the nearest point

Definition at line 111 of file seg.cpp.

112 {
113  if( auto p = Intersect( aSeg ) )
114  return *p;
115 
116  const VECTOR2I pts_origin[4] =
117  {
118  aSeg.NearestPoint( A ),
119  aSeg.NearestPoint( B ),
120  NearestPoint( aSeg.A ),
121  NearestPoint( aSeg.B )
122  };
123 
124  const ecoord pts_dist[4] =
125  {
126  ( pts_origin[0] - A ).SquaredEuclideanNorm(),
127  ( pts_origin[1] - B ).SquaredEuclideanNorm(),
128  ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
129  ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
130  };
131 
132  int min_i = 0;
133 
134  for( int i = 0; i < 4; i++ )
135  {
136  if( pts_dist[i] < pts_dist[min_i] )
137  min_i = i;
138  }
139 
140  return pts_origin[min_i];
141 }
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:144
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2I::extended_type ecoord
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:377
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, B, Intersect(), and NearestPoint().

◆ operator!=()

bool SEG::operator!= ( const SEG aSeg) const
inline

Definition at line 111 of file seg.h.

112  {
113  return (A != aSeg.A || B != aSeg.B);
114  }
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, and B.

◆ operator=()

SEG& SEG::operator= ( const SEG aSeg)
inline

Definition at line 97 of file seg.h.

98  {
99  A = aSeg.A;
100  B = aSeg.B;
101  m_index = aSeg.m_index;
102 
103  return *this;
104  }
VECTOR2I A
Definition: seg.h:47
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:341
VECTOR2I B
Definition: seg.h:48

References A, B, and m_index.

◆ operator==()

bool SEG::operator== ( const SEG aSeg) const
inline

Definition at line 106 of file seg.h.

107  {
108  return (A == aSeg.A && B == aSeg.B) ;
109  }
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, and B.

◆ Overlaps()

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

Definition at line 272 of file seg.h.

273  {
274  if( aSeg.A == aSeg.B ) // single point corner case
275  {
276  if( A == aSeg.A || B == aSeg.A )
277  return false;
278 
279  return Contains( aSeg.A );
280  }
281 
282  if( !Collinear( aSeg ) )
283  return false;
284 
285  if( Contains( aSeg.A ) || Contains( aSeg.B ) )
286  return true;
287  if( aSeg.Contains( A ) || aSeg.Contains( B ) )
288  return true;
289 
290  return false;
291  }
bool Contains(const VECTOR2I &aP) const
Definition: seg.cpp:200
bool Collinear(const SEG &aSeg) const
Function Collinear()
Definition: seg.h:238
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

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

◆ PointCloserThan()

bool SEG::PointCloserThan ( const VECTOR2I aP,
int  aDist 
) const

Definition at line 37 of file seg.cpp.

38 {
39  // See http://geomalgorithms.com/a02-_lines.html for some explanations and ideas.
40  VECTOR2I d = B - A;
41  ecoord dist_sq = (ecoord) aDist * aDist;
42 
43  SEG::ecoord l_squared = d.Dot( d );
44  SEG::ecoord t = d.Dot( aP - A );
45 
46  if( t <= 0 || !l_squared )
47  return ( aP - A ).SquaredEuclideanNorm() < dist_sq;
48  else if( t >= l_squared )
49  return ( aP - B ).SquaredEuclideanNorm() < dist_sq;
50 
51  // JPC: This code is not trivial and is not commented
52  // and does not work for d.x or d.y = -1...1
53  // I am guessing it is here for calculation time optimization.
54  // if someone can understand it, please fix it.
55  // It can be tested with a segment having d.x or d.y value
56  // is -1 or +1 ("this" is a quasi vertical or horizontal segment)
57  int dxdy = std::abs( d.x ) - std::abs( d.y );
58 
59  if( ( dxdy >= -1 && dxdy <= 1 ) // quasi 45 deg segment
60  /*|| std::abs( d.x ) <= 1 // quasi horizontal segment
61  || std::abs( d.y ) <= 1 // quasi vertical segment */ )
62  {
63  int ca = -sgn( d.y );
64  int cb = sgn( d.x );
65  int cc = -ca * A.x - cb * A.y;
66 
67  ecoord num = (ecoord) ca * aP.x + (ecoord) cb * aP.y + cc;
68  num *= num;
69 
70  if( ca && cb )
71  num >>= 1;
72 
73  if( num > ( dist_sq + 100 ) )
74  return false;
75 
76  else if( num < ( dist_sq - 100 ) )
77  return true;
78  }
79 
80  VECTOR2I nearest;
81  nearest.x = A.x + rescale( t, (ecoord) d.x, l_squared );
82  nearest.y = A.y + rescale( t, (ecoord) d.y, l_squared );
83 
84  return ( nearest - aP ).SquaredEuclideanNorm() <= dist_sq;
85 }
VECTOR2I::extended_type ecoord
Definition: seg.h:42
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2I::extended_type ecoord
int sgn(T aVal)
Definition: seg.cpp:31
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:492
VECTOR2I A
Definition: seg.h:47
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:84
VECTOR2I B
Definition: seg.h:48

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

Referenced by Contains(), and TestSegmentHit().

◆ Reverse()

void SEG::Reverse ( )
inline

Definition at line 326 of file seg.h.

327  {
328  std::swap( A, B );
329  }
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, and B.

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

◆ Side()

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 133 of file seg.h.

134  {
135  const ecoord det = ( B - A ).Cross( aP - A );
136 
137  return det < 0 ? -1 : ( det > 0 ? 1 : 0 );
138  }
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, and B.

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

◆ SquaredDistance() [1/2]

SEG::ecoord SEG::SquaredDistance ( const SEG aSeg) const

Definition at line 88 of file seg.cpp.

89 {
90  // fixme: rather inefficient....
91  if( Intersect( aSeg ) )
92  return 0;
93 
94  const VECTOR2I pts[4] =
95  {
96  aSeg.NearestPoint( A ) - A,
97  aSeg.NearestPoint( B ) - B,
98  NearestPoint( aSeg.A ) - aSeg.A,
99  NearestPoint( aSeg.B ) - aSeg.B
100  };
101 
103 
104  for( int i = 0; i < 4; i++ )
105  m = std::min( m, pts[i].SquaredEuclideanNorm() );
106 
107  return m;
108 }
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:144
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:80
VECTOR2I::extended_type ecoord
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:377
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, B, VECTOR2< T >::ECOORD_MAX, Intersect(), and NearestPoint().

Referenced by Distance().

◆ SquaredDistance() [2/2]

ecoord SEG::SquaredDistance ( const VECTOR2I aP) const
inline

Definition at line 207 of file seg.h.

208  {
209  return ( NearestPoint( aP ) - aP ).SquaredEuclideanNorm();
210  }
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:377

References NearestPoint().

◆ SquaredLength()

ecoord SEG::SquaredLength ( ) const
inline

Definition at line 304 of file seg.h.

305  {
306  return ( A - B ).SquaredEuclideanNorm();
307  }
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

References A, and B.

Referenced by PNS::commonParallelProjection().

◆ TCoef()

SEG::ecoord SEG::TCoef ( const VECTOR2I aP) const
inline

Definition at line 371 of file seg.h.

372 {
373  VECTOR2I d = B - A;
374  return d.Dot( aP - A);
375 }
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:492
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

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

Referenced by PNS::commonParallelProjection().

Friends And Related Function Documentation

◆ operator<<

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

Definition at line 398 of file seg.h.

399 {
400  aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
401 
402  return aStream;
403 }
VECTOR2I A
Definition: seg.h:47
VECTOR2I B
Definition: seg.h:48

Member Data Documentation

◆ A

VECTOR2I SEG::A

Definition at line 47 of file seg.h.

Referenced by PNS::NODE::Add(), PNS_KICAD_IFACE::AddItem(), PNS::NODE::addSegment(), PNS_PCBNEW_DEBUG_DECORATOR::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(), BOOST_AUTO_TEST_CASE(), PNS::DP_GATEWAYS::BuildFromPrimitivePair(), CanonicalCoefs(), Center(), PNS::SHOVE::checkBumpDirection(), SHAPE_LINE_CHAIN::CheckClearance(), PNS::SEGMENT::CLine(), SHAPE_ARC::Collide(), SHAPE_RECT::Collide(), Collide(), SHAPE_LINE_CHAIN::Collide(), SHAPE_POLY_SET::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(), PNS::MEANDER_PLACER::doMove(), PNS::LINE::dragSegment45(), ROUTER_PREVIEW_ITEM::drawLineChain(), KI_TEST::DrawSegment(), PNS::LOGGER::dumpShape(), SHAPE_LINE_CHAIN::EdgeContainingPoint(), 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(), Length(), KIGFX::VIEW_OVERLAY::Line(), 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(), NearestPoint(), operator!=(), POLY_GRID_PARTITION::segsEqual::operator()(), POLY_GRID_PARTITION::segHash::operator()(), operator<<(), operator=(), operator==(), Overlaps(), PNS::DP_MEANDER_PLACER::pairOrientation(), SHAPE_LINE_CHAIN::PathLength(), SHAPE_LINE_CHAIN::PointAlong(), PointCloserThan(), ZONE_CONTAINER::Print(), PNS::MEANDER_SHAPE::Recalculate(), PNS::LINE_PLACER::reduceTail(), PNS::MEANDER_SHAPE::reflect(), PNS::NODE::removeSegmentIndex(), Reverse(), PNS::WALKAROUND::Route(), POLY_GRID_PARTITION::scanCell(), GEOM_TEST::SegmentCompletelyInQuadrant(), GEOM_TEST::SegmentCompletelyWithinRadius(), GEOM_TEST::SegmentEndsInQuadrant(), PNS::SegmentHull(), SHAPE_LINE_CHAIN::SelfIntersecting(), Side(), PNS::TOOL_BASE::snapToItem(), PNS::LINE::snapToNeighbourSegments(), SHAPE_LINE_CHAIN::Split(), PNS::LINE_PLACER::SplitAdjacentSegments(), SquaredDistance(), SquaredLength(), PNS::DRAGGER::startDragSegment(), PNS::SEGMENT::SwapEnds(), TCoef(), DRC::TestZoneToZoneOutline(), unfracture(), SHAPE_POLY_SET::unfractureSingle(), PNS::MEANDER_SHAPE::updateBaseSegment(), and ROUTER_PREVIEW_ITEM::ViewDraw().

◆ B

VECTOR2I SEG::B

Definition at line 48 of file seg.h.

Referenced by PNS::NODE::Add(), PNS_KICAD_IFACE::AddItem(), PNS::MEANDERED_LINE::AddMeander(), PNS::NODE::addSegment(), PNS_PCBNEW_DEBUG_DECORATOR::AddSegment(), GRID_HELPER::AlignToSegment(), PNS::SEGMENT::Anchor(), PNS::DP_PRIMITIVE_PAIR::anchorDirection(), ApproxCollinear(), PNS::ApproximateSegmentAsRect(), ApproxParallel(), PNS::DP_MEANDER_PLACER::baselineSegment(), SHAPE_SEGMENT::BBox(), BOOST_AUTO_TEST_CASE(), POLY_GRID_PARTITION::build(), PNS::DP_GATEWAYS::BuildFromPrimitivePair(), CanonicalCoefs(), Center(), PNS::SHOVE::checkBumpDirection(), SHAPE_LINE_CHAIN::CheckClearance(), PNS::SEGMENT::CLine(), SHAPE_ARC::Collide(), 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(), PNS::MEANDER_PLACER::doMove(), PNS::LINE::dragSegment45(), ROUTER_PREVIEW_ITEM::drawLineChain(), KI_TEST::DrawSegment(), PNS::LOGGER::dumpShape(), SHAPE_LINE_CHAIN::EdgeContainingPoint(), PNS::MEANDER_SHAPE::End(), SCH_EAGLE_PLUGIN::findNearestLinePoint(), 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(), Length(), KIGFX::VIEW_OVERLAY::Line(), 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(), NearestPoint(), operator!=(), POLY_GRID_PARTITION::segsEqual::operator()(), POLY_GRID_PARTITION::segHash::operator()(), operator<<(), operator=(), operator==(), Overlaps(), SHAPE_LINE_CHAIN::PointAlong(), PointCloserThan(), ZONE_CONTAINER::Print(), PNS::MEANDER_SHAPE::Recalculate(), PNS::MEANDER_SHAPE::reflect(), PNS::NODE::removeSegmentIndex(), Reverse(), PNS::WALKAROUND::Route(), POLY_GRID_PARTITION::scanCell(), GEOM_TEST::SegmentCompletelyInQuadrant(), GEOM_TEST::SegmentCompletelyWithinRadius(), GEOM_TEST::SegmentEndsInQuadrant(), PNS::SegmentHull(), SHAPE_LINE_CHAIN::SelfIntersecting(), Side(), PNS::TOOL_BASE::snapToItem(), SHAPE_LINE_CHAIN::Split(), PNS::LINE_PLACER::SplitAdjacentSegments(), SquaredDistance(), SquaredLength(), PNS::DRAGGER::startDragSegment(), PNS::SEGMENT::SwapEnds(), TCoef(), DRC::TestZoneToZoneOutline(), unfracture(), SHAPE_POLY_SET::unfractureSingle(), PNS::MEANDER_SHAPE::updateBaseSegment(), POLYGON_GEOM_MANAGER::updateLeaderPoints(), and ROUTER_PREVIEW_ITEM::ViewDraw().

◆ m_index

int SEG::m_index
private

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

Definition at line 341 of file seg.h.

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


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