KiCad PCB EDA Suite
SEG Class Reference

#include <seg.h>

Public Types

using ecoord = VECTOR2I::extended_type
 

Public Member Functions

 SEG ()
 Create an empty (0, 0) segment. More...
 
 SEG (int aX1, int aY1, int aX2, int aY2)
 Create a segment between (aX1, aY1) and (aX2, aY2). More...
 
 SEG (const VECTOR2I &aA, const VECTOR2I &aB)
 Create a segment between (aA) and (aB). More...
 
 SEG (const VECTOR2I &aA, const VECTOR2I &aB, int aIndex)
 Create 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
 Compute the perpendicular projection point of aP on a line passing through ends of the segment. More...
 
int Side (const VECTOR2I &aP) const
 Determine on which side of directed line passing via segment ends point aP lies. More...
 
int LineDistance (const VECTOR2I &aP, bool aDetermineSide=false) const
 Return the closest Euclidean distance between point aP and the line defined by the ends of segment (this). More...
 
EDA_ANGLE Angle (const SEG &aOther) const
 Determine the smallest angle between two segments. More...
 
const VECTOR2I NearestPoint (const VECTOR2I &aP) const
 Compute a point on the segment (this) that is closest to point aP. More...
 
const VECTOR2I NearestPoint (const SEG &aSeg) const
 Compute a point on the segment (this) that is closest to any point on aSeg. More...
 
const VECTOR2I ReflectPoint (const VECTOR2I &aP) const
 Reflect a point using this segment as axis. More...
 
OPT_VECTOR2I Intersect (const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
 Compute intersection point of segment (this) with segment aSeg. More...
 
bool Intersects (const SEG &aSeg) const
 
OPT_VECTOR2I IntersectLines (const SEG &aSeg) const
 Compute the intersection point of lines passing through ends of (this) and aSeg. More...
 
SEG PerpendicularSeg (const VECTOR2I &aP) const
 Compute a segment perpendicular to this one, passing through point aP. More...
 
SEG ParallelSeg (const VECTOR2I &aP) const
 Compute a segment parallel to this one, passing through point aP. More...
 
bool Collide (const SEG &aSeg, int aClearance, int *aActual=nullptr) const
 
ecoord SquaredDistance (const SEG &aSeg) const
 
int Distance (const SEG &aSeg) const
 Compute minimum Euclidean distance to segment aSeg. More...
 
ecoord SquaredDistance (const VECTOR2I &aP) const
 
int Distance (const VECTOR2I &aP) const
 Compute minimum Euclidean distance to point aP. More...
 
void CanonicalCoefs (ecoord &qA, ecoord &qB, ecoord &qC) const
 
bool Collinear (const SEG &aSeg) const
 Check if segment aSeg lies on the same line as (this). More...
 
bool ApproxCollinear (const SEG &aSeg) const
 
bool ApproxParallel (const SEG &aSeg, int aDistanceThreshold=1) const
 
bool ApproxPerpendicular (const SEG &aSeg) const
 
bool Overlaps (const SEG &aSeg) const
 
bool Contains (const SEG &aSeg) const
 
int Length () const
 Return the length (this). More...
 
ecoord SquaredLength () const
 
ecoord TCoef (const VECTOR2I &aP) const
 
int Index () const
 Return the index of this segment in its parent shape (applicable only to non-local segments). More...
 
bool Contains (const VECTOR2I &aP) const
 
void Reverse ()
 
SEG Reversed () const
 Returns the center point of the line. More...
 
VECTOR2I Center () const
 

Static Public Member Functions

static SEG::ecoord Square (int a)
 

Public Attributes

VECTOR2I A
 
VECTOR2I B
 

Private Member Functions

bool ccw (const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
 
bool intersects (const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false, VECTOR2I *aPt=nullptr) const
 

Private Attributes

int m_index
 < index within 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 41 of file seg.h.

Member Typedef Documentation

◆ ecoord

Definition at line 44 of file seg.h.

Constructor & Destructor Documentation

◆ SEG() [1/5]

SEG::SEG ( )
inline

Create an empty (0, 0) segment.

Definition at line 55 of file seg.h.

56 {
57 m_index = -1;
58 }
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:400

References m_index.

Referenced by ParallelSeg(), PerpendicularSeg(), and Reversed().

◆ SEG() [2/5]

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

Create a segment between (aX1, aY1) and (aX2, aY2).

Definition at line 63 of file seg.h.

63 :
64 A( VECTOR2I( aX1, aY1 ) ),
65 B( VECTOR2I( aX2, aY2 ) )
66 {
67 m_index = -1;
68 }
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618

References m_index.

◆ SEG() [3/5]

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

Create a segment between (aA) and (aB).

Definition at line 73 of file seg.h.

73 :
74 A( aA ),
75 B( aB )
76 {
77 m_index = -1;
78 }

References m_index.

◆ SEG() [4/5]

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

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

87 :
88 A( aA ),
89 B( aB )
90 {
91 m_index = aIndex;
92 }

References m_index.

◆ SEG() [5/5]

SEG::SEG ( const SEG aSeg)
inline

Copy constructor.

Definition at line 97 of file seg.h.

97 :
98 A( aSeg.A ),
99 B( aSeg.B ),
100 m_index( aSeg.m_index )
101 {
102 }

Member Function Documentation

◆ Angle()

EDA_ANGLE SEG::Angle ( const SEG aOther) const

Determine the smallest angle between two segments.

Parameters
aOtherpoint to determine the orientation wrs to self
Returns
smallest angle between this and aOther

Definition at line 97 of file seg.cpp.

98{
99 EDA_ANGLE thisAngle = EDA_ANGLE( A - B ).Normalize180();
100 EDA_ANGLE otherAngle = EDA_ANGLE( aOther.A - aOther.B ).Normalize180();
101
102 EDA_ANGLE angle = std::abs( ( thisAngle - otherAngle ).Normalize180() );
103
104 return std::min( ANGLE_180 - angle, angle );
105}
EDA_ANGLE Normalize180()
Definition: eda_angle.h:271
static constexpr EDA_ANGLE & ANGLE_180
Definition: eda_angle.h:416
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:401

References A, std::abs(), PNS::angle(), ANGLE_180, B, and EDA_ANGLE::Normalize180().

Referenced by EDIT_TOOL::DragArcTrack().

◆ ApproxCollinear()

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

Definition at line 280 of file seg.h.

281 {
282 ecoord p, q, r;
283 CanonicalCoefs( p, q, r );
284
285 ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
286 ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
287
288 return std::abs( dist1 ) <= 1 && std::abs( dist2 ) <= 1;
289 }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:256
E_SERIE r
Definition: eserie.cpp:41
VECTOR2I::extended_type ecoord

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

Referenced by PCB_TRACK::ApproxCollinear(), and EDIT_TOOL::FilletTracks().

◆ ApproxParallel()

bool SEG::ApproxParallel ( const SEG aSeg,
int  aDistanceThreshold = 1 
) const
inline

Definition at line 291 of file seg.h.

292 {
293 ecoord p, q, r;
294 CanonicalCoefs( p, q, r );
295
296 ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
297 ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
298
299 return std::abs( dist1 - dist2 ) <= aDistanceThreshold;
300 }

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

Referenced by PCB_GRID_HELPER::AlignToSegment(), ApproxPerpendicular(), PNS::MEANDERED_LINE::CheckSelfIntersections(), CIRCLE::ConstructFromTanTanPt(), PNS::DIFF_PAIR::CoupledLength(), PNS::DIFF_PAIR::CoupledSegmentPairs(), PNS::DP_PRIMITIVE_PAIR::CursorOrientation(), extractDiffPairCoupledItems(), PNS::findCoupledVertices(), and SegParallelCorrect().

◆ ApproxPerpendicular()

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

Definition at line 302 of file seg.h.

303 {
304 SEG perp = PerpendicularSeg( A );
305
306 return aSeg.ApproxParallel( perp );
307 }
Definition: seg.h:42
bool ApproxParallel(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.h:291
SEG PerpendicularSeg(const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP.
Definition: seg.cpp:199

References ApproxParallel(), and PerpendicularSeg().

Referenced by SegPerpendicularCorrect().

◆ CanonicalCoefs()

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

Definition at line 256 of file seg.h.

257 {
258 qA = ecoord{ A.y } - B.y;
259 qB = ecoord{ B.x } - A.x;
260 qC = -qA * A.x - qB * A.y;
261 }

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

◆ ccw()

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

Definition at line 217 of file seg.cpp.

218{
219 return (ecoord) ( aC.y - aA.y ) * ( aB.x - aA.x ) > (ecoord) ( aB.y - aA.y ) * ( aC.x - aA.x );
220}
VECTOR2I::extended_type ecoord
Definition: seg.h:44

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

Referenced by Collide().

◆ Center()

VECTOR2I SEG::Center ( ) const
inline

Definition at line 387 of file seg.h.

388 {
389 return A + ( B - A ) / 2;
390 }

References A.

Referenced by Collide(), PCB_GRID_HELPER::computeAnchors(), CIRCLE::ConstructFromTanTanPt(), and SHAPE_ARC::SHAPE_ARC().

◆ Collide()

bool SEG::Collide ( const SEG aSeg,
int  aClearance,
int *  aActual = nullptr 
) const

Definition at line 223 of file seg.cpp.

224{
225 // check for intersection
226 // fixme: move to a method
227 if( ccw( A, aSeg.A, aSeg.B ) != ccw( B, aSeg.A, aSeg.B ) &&
228 ccw( A, B, aSeg.A ) != ccw( A, B, aSeg.B ) )
229 {
230 if( aActual )
231 *aActual = 0;
232
233 return true;
234 }
235
237
238 dist_sq = std::min( dist_sq, SquaredDistance( aSeg.A ) );
239 dist_sq = std::min( dist_sq, SquaredDistance( aSeg.B ) );
240 dist_sq = std::min( dist_sq, aSeg.SquaredDistance( A ) );
241 dist_sq = std::min( dist_sq, aSeg.SquaredDistance( B ) );
242
243 if( dist_sq == 0 || dist_sq < (ecoord) aClearance * aClearance )
244 {
245 if( aActual )
246 *aActual = isqrt( dist_sq );
247
248 return true;
249 }
250
251 return false;
252}
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:75
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:217
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:79
T isqrt(T x)
Definition: seg.cpp:61

References A, B, ccw(), VECTOR2< int >::ECOORD_MAX, isqrt(), and SquaredDistance().

Referenced by SHAPE_POLY_SET::IsPolygonSelfIntersecting(), SegCollideCorrect(), DRC_TEST_PROVIDER_PHYSICAL_CLEARANCE::testShapeLineChain(), and BOARD::TestZoneIntersection().

◆ Collinear()

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

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

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

Definition at line 269 of file seg.h.

270 {
271 ecoord qa, qb, qc;
272 CanonicalCoefs( qa, qb, qc );
273
274 ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
275 ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
276
277 return ( d1 <= 1 && d2 <= 1 );
278 }

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

Referenced by PNS::DP_GATEWAYS::BuildGeneric(), Contains(), EC_CONVERGING::EC_CONVERGING(), SHAPE_LINE_CHAIN::Intersect(), PNS::OPTIMIZER::mergeColinear(), Overlaps(), SegCollinearCorrect(), and POLYGON_GEOM_MANAGER::updateLeaderPoints().

◆ Contains() [1/2]

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

Definition at line 332 of file seg.h.

333 {
334 if( aSeg.A == aSeg.B ) // single point corner case
335 return Contains( aSeg.A );
336
337 if( !Collinear( aSeg ) )
338 return false;
339
340 if( Contains( aSeg.A ) && Contains( aSeg.B ) )
341 return true;
342
343 return false;
344 }
bool Collinear(const SEG &aSeg) const
Check if segment aSeg lies on the same line as (this).
Definition: seg.h:269
bool Contains(const SEG &aSeg) const
Definition: seg.h:332

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

Referenced by SCH_EAGLE_PLUGIN::adjustNetLabels(), BuildFootprintPolygonOutlines(), PNS::LINE::ChangedArea(), Contains(), findEndSegments(), CIRCLE::Intersect(), SHAPE_LINE_CHAIN::Intersect(), PNS::MEANDERED_LINE::MeanderSegment(), Overlaps(), and PNS::LINE_PLACER::simplifyNewLine().

◆ Contains() [2/2]

bool SEG::Contains ( const VECTOR2I aP) const

Definition at line 255 of file seg.cpp.

256{
257 return Distance( aP ) <= 1;
258}
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition: seg.cpp:319

References Distance().

◆ Distance() [1/2]

◆ Distance() [2/2]

int SEG::Distance ( const VECTOR2I aP) const

Compute minimum Euclidean distance to point aP.

Parameters
aPthe point
Returns
minimum distance

Definition at line 325 of file seg.cpp.

326{
327 return isqrt( SquaredDistance( aP ) );
328}

References isqrt(), and SquaredDistance().

◆ Index()

int SEG::Index ( ) const
inline

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

Returns
index value

Definition at line 369 of file seg.h.

370 {
371 return m_index;
372 }

References m_index.

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

◆ Intersect()

OPT_VECTOR2I SEG::Intersect ( const SEG aSeg,
bool  aIgnoreEndpoints = false,
bool  aLines = false 
) const

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

189{
190 VECTOR2I ip;
191
192 if( intersects( aSeg, aIgnoreEndpoints, aLines, &ip ) )
193 return ip;
194 else
195 return OPT_VECTOR2I();
196}
bool intersects(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false, VECTOR2I *aPt=nullptr) const
Definition: seg.cpp:150
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39

References intersects().

Referenced by EC_CONVERGING::Apply(), PNS::LINE::dragSegment45(), DRAWING_TOOL::DrawVia(), ALTIUM_PCB::HelperParseDimensions6Linear(), SHAPE_LINE_CHAIN::Intersect(), IntersectLines(), SCH_EAGLE_PLUGIN::loadSegments(), NearestPoint(), SHAPE_LINE_CHAIN::SelfIntersecting(), SHAPE_ARC::SHAPE_ARC(), and DRC_TEST_PROVIDER_COPPER_CLEARANCE::testTrackAgainstItem().

◆ IntersectLines()

OPT_VECTOR2I SEG::IntersectLines ( const SEG aSeg) const
inline

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

211 {
212 return Intersect( aSeg, false, true );
213 }
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition: seg.cpp:188

References Intersect().

Referenced by PCB_GRID_HELPER::AlignToSegment(), EC_CONVERGING::Apply(), EC_PERPLINE::Apply(), EC_LINE::Apply(), BuildFootprintPolygonOutlines(), PNS::DP_GATEWAYS::BuildGeneric(), CIRCLE::ConstructFromTanTanPt(), PNS::ConvexHull(), CornerListToPolygon(), EDIT_TOOL::DragArcTrack(), PNS::LINE::dragSegment45(), PNS::OPTIMIZER::mergeObtuse(), and PNS::LINE::snapDraggedCorner().

◆ Intersects()

bool SEG::Intersects ( const SEG aSeg) const

Definition at line 182 of file seg.cpp.

183{
184 return intersects( aSeg );
185}

References intersects().

Referenced by SquaredDistance().

◆ intersects()

bool SEG::intersects ( const SEG aSeg,
bool  aIgnoreEndpoints = false,
bool  aLines = false,
VECTOR2I aPt = nullptr 
) const
private

Definition at line 150 of file seg.cpp.

151{
152 const VECTOR2I e( B - A );
153 const VECTOR2I f( aSeg.B - aSeg.A );
154 const VECTOR2I ac( aSeg.A - A );
155
156 ecoord d = f.Cross( e );
157 ecoord p = f.Cross( ac );
158 ecoord q = e.Cross( ac );
159
160 if( d == 0 )
161 return false;
162
163 if( !aLines && d > 0 && ( q < 0 || q > d || p < 0 || p > d ) )
164 return false;
165
166 if( !aLines && d < 0 && ( q < d || p < d || p > 0 || q > 0 ) )
167 return false;
168
169 if( !aLines && aIgnoreEndpoints && ( q == 0 || q == d ) && ( p == 0 || p == d ) )
170 return false;
171
172 if( aPt )
173 {
174 *aPt = VECTOR2I( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
175 aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
176 }
177
178 return true;
179}
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:105

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

Referenced by Intersect(), and Intersects().

◆ Length()

◆ LineDistance()

int SEG::LineDistance ( const VECTOR2I aP,
bool  aDetermineSide = false 
) const

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

332{
333 ecoord p = ecoord{ A.y } - B.y;
334 ecoord q = ecoord{ B.x } - A.x;
335 ecoord r = -p * A.x - q * A.y;
336 ecoord l = p * p + q * q;
337 ecoord det = p * aP.x + q * aP.y + r;
338 ecoord dist_sq = 0;
339
340 if( l > 0 )
341 {
342 dist_sq = rescale( det, det, l );
343 }
344
345 ecoord dist = isqrt( dist_sq );
346
347 return aDetermineSide ? dist : std::abs( dist );
348}

References std::abs(), isqrt(), r, rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

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

◆ LineProject()

VECTOR2I SEG::LineProject ( const VECTOR2I aP) const

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

303{
304 VECTOR2I d = B - A;
305 ecoord l_squared = d.Dot( d );
306
307 if( l_squared == 0 )
308 return A;
309
310 ecoord t = d.Dot( aP - A );
311
312 int xp = rescale( t, ecoord{ d.x }, l_squared );
313 int yp = rescale( t, ecoord{ d.y }, l_squared );
314
315 return A + VECTOR2I( xp, yp );
316}
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:495

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

Referenced by PNS::LINE_PLACER::buildInitialLine(), PNS::DP_GATEWAYS::BuildOrthoProjections(), commonParallelProjection(), PNS::commonParallelProjection(), CIRCLE::ConstructFromTanTanPt(), EDIT_TOOL::DragArcTrack(), PNS::findCoupledVertices(), CIRCLE::IntersectLine(), PNS::DIFF_PAIR_PLACER::routeHead(), SHAPE_ARC::SHAPE_ARC(), and PNS::MEANDER_SHAPE::updateBaseSegment().

◆ NearestPoint() [1/2]

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

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

Returns
the nearest point

Definition at line 108 of file seg.cpp.

109{
110 if( OPT_VECTOR2I p = Intersect( aSeg ) )
111 return *p;
112
113 const VECTOR2I pts_origin[4] =
114 {
115 aSeg.NearestPoint( A ),
116 aSeg.NearestPoint( B ),
117 NearestPoint( aSeg.A ),
118 NearestPoint( aSeg.B )
119 };
120
121
122 const VECTOR2I* pts_out[4] =
123 {
124 &A,
125 &B,
126 &pts_origin[2],
127 &pts_origin[3]
128 };
129
130 const ecoord pts_dist[4] =
131 {
132 ( pts_origin[0] - A ).SquaredEuclideanNorm(),
133 ( pts_origin[1] - B ).SquaredEuclideanNorm(),
134 ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
135 ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
136 };
137
138 int min_i = 0;
139
140 for( int i = 0; i < 4; i++ )
141 {
142 if( pts_dist[i] < pts_dist[min_i] )
143 min_i = i;
144 }
145
146 return *pts_out[min_i];
147}
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:261

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

◆ NearestPoint() [2/2]

const VECTOR2I SEG::NearestPoint ( const VECTOR2I aP) const

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

Returns
the nearest point

Definition at line 261 of file seg.cpp.

262{
263 VECTOR2I d = B - A;
264 ecoord l_squared = d.Dot( d );
265
266 if( l_squared == 0 )
267 return A;
268
269 ecoord t = d.Dot( aP - A );
270
271 if( t < 0 )
272 return A;
273 else if( t > l_squared )
274 return B;
275
276 int xp = rescale( t, (ecoord) d.x, l_squared );
277 int yp = rescale( t, (ecoord) d.y, l_squared );
278
279 return A + VECTOR2I( xp, yp );
280}

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

Referenced by PCB_POINT_EDITOR::addCorner(), PNS::LINE::ClipToNearestObstacle(), PNS::closestProjectedPoint(), SHAPE_ARC::Collide(), SHAPE_CIRCLE::Collide(), SHAPE_RECT::Collide(), SHAPE_SEGMENT::Collide(), Collide(), SHAPE_LINE_CHAIN_BASE::Collide(), SHAPE_LINE_CHAIN::Collide(), PNS::cursorDistMinimum(), EDIT_TOOL::DragArcTrack(), DRAWING_TOOL::DrawVia(), PCB_POINT_EDITOR::editArcMidKeepEndpoints(), SCH_MOVE_TOOL::moveItem(), NearestPoint(), SHAPE_LINE_CHAIN::NearestPoint(), pushoutForce(), SCH_DRAWING_TOOLS::SingleClickPlace(), SquaredDistance(), DRC_TEST_PROVIDER_PHYSICAL_CLEARANCE::testShapeLineChain(), and PNS::LINE::Walkaround().

◆ operator!=()

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

Definition at line 118 of file seg.h.

119 {
120 return (A != aSeg.A || B != aSeg.B);
121 }

References A, and B.

◆ operator=()

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

Definition at line 104 of file seg.h.

105 {
106 A = aSeg.A;
107 B = aSeg.B;
108 m_index = aSeg.m_index;
109
110 return *this;
111 }

References A, B, and m_index.

◆ operator==()

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

Definition at line 113 of file seg.h.

114 {
115 return (A == aSeg.A && B == aSeg.B) ;
116 }

References A, and B.

◆ Overlaps()

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

Definition at line 309 of file seg.h.

310 {
311 if( aSeg.A == aSeg.B ) // single point corner case
312 {
313 if( A == aSeg.A || B == aSeg.A )
314 return false;
315
316 return Contains( aSeg.A );
317 }
318
319 if( !Collinear( aSeg ) )
320 return false;
321
322 if( Contains( aSeg.A ) || Contains( aSeg.B ) )
323 return true;
324
325 if( aSeg.Contains( A ) || aSeg.Contains( B ) )
326 return true;
327
328 return false;
329 }

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

◆ ParallelSeg()

SEG SEG::ParallelSeg ( const VECTOR2I aP) const

Compute a segment parallel to this one, passing through point aP.

Parameters
aPPoint through which the new segment will pass
Returns
SEG parallel to this passing through point aP

Definition at line 208 of file seg.cpp.

209{
210 VECTOR2I slope( B - A );
211 VECTOR2I endPoint = slope + aP;
212
213 return SEG( aP, endPoint );
214}
SEG()
Create an empty (0, 0) segment.
Definition: seg.h:55

References SEG().

Referenced by BOOST_AUTO_TEST_CASE(), and CIRCLE::ConstructFromTanTanPt().

◆ PerpendicularSeg()

SEG SEG::PerpendicularSeg ( const VECTOR2I aP) const

Compute a segment perpendicular to this one, passing through point aP.

Parameters
aPPoint through which the new segment will pass
Returns
SEG perpendicular to this passing through point aP

Definition at line 199 of file seg.cpp.

200{
201 VECTOR2I slope( B - A );
202 VECTOR2I endPoint = slope.Perpendicular() + aP;
203
204 return SEG( aP, endPoint );
205}
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:307

References VECTOR2< T >::Perpendicular(), and SEG().

Referenced by ApproxPerpendicular(), BOOST_AUTO_TEST_CASE(), CIRCLE::ConstructFromTanTanPt(), and EDIT_TOOL::DragArcTrack().

◆ ReflectPoint()

const VECTOR2I SEG::ReflectPoint ( const VECTOR2I aP) const

Reflect a point using this segment as axis.

Returns
the reflected point

Definition at line 283 of file seg.cpp.

284{
285 VECTOR2I d = B - A;
286 VECTOR2I::extended_type l_squared = d.Dot( d );
287 VECTOR2I::extended_type t = d.Dot( aP - A );
288 VECTOR2I c;
289
290 if( !l_squared )
291 c = aP;
292 else
293 {
294 c.x = A.x + rescale( t, static_cast<VECTOR2I::extended_type>( d.x ), l_squared );
295 c.y = A.y + rescale( t, static_cast<VECTOR2I::extended_type>( d.y ), l_squared );
296 }
297
298 return 2 * c - aP;
299}
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:76

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

Referenced by CornerListToPolygon(), SHAPE_ARC::Mirror(), and SHAPE_LINE_CHAIN::Mirror().

◆ Reverse()

void SEG::Reverse ( )
inline

Definition at line 376 of file seg.h.

377 {
378 std::swap( A, B );
379 }

◆ Reversed()

SEG SEG::Reversed ( ) const
inline

Returns the center point of the line.

Definition at line 381 of file seg.h.

References SEG().

Referenced by PNS::LINE_PLACER::Start().

◆ Side()

int SEG::Side ( const VECTOR2I aP) const
inline

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

144 {
145 const ecoord det = ( B - A ).Cross( aP - A );
146
147 return det < 0 ? -1 : ( det > 0 ? 1 : 0 );
148 }

References A.

Referenced by PNS::ArcHull(), PNS::DP_GATEWAYS::buildDpContinuation(), CornerListToPolygon(), PNS::MEANDER_PLACER::doMove(), EDIT_TOOL::DragArcTrack(), PNS::DP_MEANDER_PLACER::Move(), PNS::DP_MEANDER_PLACER::pairOrientation(), and PNS::SegmentHull().

◆ Square()

◆ SquaredDistance() [1/2]

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

Definition at line 75 of file seg.cpp.

76{
77 if( Intersects( aSeg ) )
78 return 0;
79
80 const VECTOR2I pts[4] =
81 {
82 aSeg.NearestPoint( A ) - A,
83 aSeg.NearestPoint( B ) - B,
84 NearestPoint( aSeg.A ) - aSeg.A,
85 NearestPoint( aSeg.B ) - aSeg.B
86 };
87
89
90 for( int i = 0; i < 4; i++ )
91 m = std::min( m, pts[i].SquaredEuclideanNorm() );
92
93 return m;
94}
bool Intersects(const SEG &aSeg) const
Definition: seg.cpp:182

References A, B, VECTOR2< int >::ECOORD_MAX, Intersects(), and NearestPoint().

Referenced by PNS::MOUSE_TRAIL_TRACER::AddTrailPoint(), PNS::PRESERVE_VERTEX_CONSTRAINT::Check(), Collide(), SHAPE_LINE_CHAIN_BASE::Collide(), SHAPE_LINE_CHAIN::Collide(), SHAPE_RECT::Collide(), SHAPE_SEGMENT::Collide(), SHAPE_POLY_SET::CollideEdge(), Distance(), SHAPE_LINE_CHAIN_BASE::SquaredDistance(), and TestSegmentHit().

◆ SquaredDistance() [2/2]

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

Definition at line 243 of file seg.h.

244 {
245 return ( NearestPoint( aP ) - aP ).SquaredEuclideanNorm();
246 }

References NearestPoint().

◆ SquaredLength()

◆ TCoef()

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

Definition at line 403 of file seg.h.

404{
405 VECTOR2I d = B - A;
406 return d.Dot( aP - A);
407}

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

Referenced by commonParallelProjection(), and PNS::commonParallelProjection().

Friends And Related Function Documentation

◆ operator<<

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

Definition at line 409 of file seg.h.

410{
411 aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
412
413 return aStream;
414}

Member Data Documentation

◆ A

VECTOR2I SEG::A

Definition at line 49 of file seg.h.

Referenced by PNS::NODE::Add(), PNS_KICAD_IFACE::AddItem(), PNS::NODE::addSegment(), BOARD_ADAPTER::addShape(), PNS::DEBUG_DECORATOR::AddShape(), SCH_EAGLE_PLUGIN::adjustNetLabels(), PCB_GRID_HELPER::AlignToSegment(), PNS::SEGMENT::Anchor(), Angle(), ApproxCollinear(), PNS::ApproximateSegmentAsRect(), ApproxParallel(), PNS::TOPOLOGY::AssembleTuningPath(), PNS::DP_MEANDER_PLACER::baselineSegment(), SHAPE_SEGMENT::BBox(), BOOST_AUTO_TEST_CASE(), PNS::DP_GATEWAYS::BuildFromPrimitivePair(), Center(), SHAPE_LINE_CHAIN::CheckClearance(), PNS::DRAGGER::checkVirtualVia(), Collide(), SHAPE_LINE_CHAIN_BASE::Collide(), SHAPE_ARC::Collide(), SHAPE_LINE_CHAIN::Collide(), SHAPE_RECT::Collide(), SHAPE_SEGMENT::Collide(), Collide(), collideArc2Arc(), Collinear(), commonParallelProjection(), PNS::commonParallelProjection(), PCB_GRID_HELPER::computeAnchors(), CIRCLE::ConstructFromTanTanPt(), Contains(), BOARD_ADAPTER::createPadWithMargin(), PNS::cursorDistMinimum(), PNS::DP_PRIMITIVE_PAIR::CursorOrientation(), PNS::MEANDER_PLACER_BASE::cutTunedLine(), DIRECTION_45::DIRECTION_45(), PNS::MEANDER_PLACER::doMove(), PNS::COMPONENT_DRAGGER::Drag(), EDIT_TOOL::DragArcTrack(), PNS::LINE::dragSegment45(), KIGFX::PCB_PAINTER::draw(), ROUTER_PREVIEW_ITEM::drawLineChain(), KI_TEST::DrawSegment(), ROUTER_PREVIEW_ITEM::drawShape(), SHAPE_LINE_CHAIN_BASE::EdgeContainingPoint(), extractDiffPairCoupledItems(), findEndSegments(), PNS::NODE::findRedundantSegment(), PNS::MEANDER_SHAPE::Fit(), PNS::NODE::FixupVirtualVias(), SHAPE_SEGMENT::Format(), PNS::getDanglingAnchor(), BOARD::GetTrackLength(), PNS::LINE_PLACER::handlePullback(), PNS::LINE_PLACER::handleSelfIntersections(), ZONE::HatchBorder(), ALTIUM_PCB::HelperCreateBoardOutline(), PNS::HullIntersection(), SHAPE_LINE_CHAIN::Intersect(), CIRCLE::IntersectLine(), intersects(), isLine45Degree(), PNS::IsSegment45Degree(), KIGFX::VIEW_OVERLAY::Line(), LineProject(), CADSTAR_SCH_ARCHIVE_LOADER::loadShapeVertices(), PNS::MEANDER_SHAPE::MakeArc(), PNS::MEANDER_SHAPE::MakeCorner(), PNS::MEANDER_SHAPE::MakeEmpty(), PNS::MEANDERED_LINE::MeanderSegment(), PNS::OPTIMIZER::mergeDpStep(), PNS::OPTIMIZER::mergeObtuse(), PNS::OPTIMIZER::mergeStep(), SHAPE_SEGMENT::Move(), PNS::MoveDiagonal(), NearestPoint(), SHAPE_LINE_CHAIN::NearestPoint(), NearestPointDbl(), NearestPointFixpt(), operator!=(), operator=(), operator==(), Overlaps(), PNS::DP_MEANDER_PLACER::pairOrientation(), SHAPE_LINE_CHAIN::PathLength(), playground_main_func(), BRDITEMS_PLOTTER::PlotDimension(), SHAPE_LINE_CHAIN::PointAlong(), PNS::MEANDER_SHAPE::Recalculate(), PNS::LINE_PLACER::reduceTail(), ReflectPoint(), PNS::NODE::removeSegmentIndex(), DRC_TEST_PROVIDER_CONNECTION_WIDTH::Run(), DRC_TEST_PROVIDER_EDGE_CLEARANCE::Run(), PCB_DIMENSION_BASE::segCircleIntersection(), GEOM_TEST::SegmentCompletelyInQuadrant(), GEOM_TEST::SegmentCompletelyWithinRadius(), GEOM_TEST::SegmentEndsInQuadrant(), PNS::SegmentHull(), PCB_DIMENSION_BASE::segPolyIntersection(), SHAPE_LINE_CHAIN::SelfIntersecting(), SHAPE_ARC::SHAPE_ARC(), Side(), PNS::LINE::snapToNeighbourSegments(), SHAPE_LINE_CHAIN::Split(), PNS::LINE_PLACER::SplitAdjacentSegments(), SquaredDistance(), SHAPE_POLY_SET::SquaredDistanceToPolygon(), PNS::LINE_PLACER::Start(), PNS::DRAGGER::startDragSegment(), STROKE_PARAMS::Stroke(), PNS::SEGMENT::SwapEnds(), TCoef(), DRC_TEST_PROVIDER_PHYSICAL_CLEARANCE::testShapeLineChain(), DRC_TEST_PROVIDER_COPPER_CLEARANCE::testZonesToZones(), PNS::tightenSegment(), PCB_DIMENSION_BASE::TransformShapeWithClearanceToPolygon(), unfracture(), SHAPE_POLY_SET::unfractureSingle(), PNS::MEANDER_SHAPE::updateBaseSegment(), PCB_DIM_ALIGNED::updateGeometry(), PCB_DIM_ORTHOGONAL::updateGeometry(), PNS_KICAD_IFACE::UpdateItem(), and HYPERLYNX_EXPORTER::writeBoardInfo().

◆ B

VECTOR2I SEG::B

Definition at line 50 of file seg.h.

Referenced by PNS::NODE::Add(), PCB_POINT_EDITOR::addCorner(), PNS_KICAD_IFACE::AddItem(), PNS::MEANDERED_LINE::AddMeander(), PNS::NODE::addSegment(), BOARD_ADAPTER::addShape(), PNS::DEBUG_DECORATOR::AddShape(), SCH_EAGLE_PLUGIN::adjustNetLabels(), PCB_GRID_HELPER::AlignToSegment(), PNS::SEGMENT::Anchor(), Angle(), ApproxCollinear(), PNS::ApproximateSegmentAsRect(), ApproxParallel(), PNS::TOPOLOGY::AssembleTuningPath(), PNS::DP_MEANDER_PLACER::baselineSegment(), SHAPE_SEGMENT::BBox(), BOOST_AUTO_TEST_CASE(), PNS::DP_GATEWAYS::BuildFromPrimitivePair(), SHAPE_LINE_CHAIN::CheckClearance(), PNS::DRAGGER::checkVirtualVia(), PNS::SEGMENT::CLine(), Collide(), SHAPE_ARC::Collide(), SHAPE_RECT::Collide(), SHAPE_SEGMENT::Collide(), Collide(), collideArc2Arc(), Collinear(), commonParallelProjection(), PNS::commonParallelProjection(), PCB_GRID_HELPER::computeAnchors(), CIRCLE::ConstructFromTanTanPt(), Contains(), BOARD_ADAPTER::createPadWithMargin(), PNS::cursorDistMinimum(), PNS::DP_PRIMITIVE_PAIR::CursorOrientation(), PNS::MEANDER_PLACER_BASE::cutTunedLine(), DIRECTION_45::DIRECTION_45(), PNS::MEANDER_PLACER::doMove(), PNS::COMPONENT_DRAGGER::Drag(), EDIT_TOOL::DragArcTrack(), PNS::LINE::dragSegment45(), KIGFX::PCB_PAINTER::draw(), ROUTER_PREVIEW_ITEM::drawLineChain(), KI_TEST::DrawSegment(), ROUTER_PREVIEW_ITEM::drawShape(), SHAPE_LINE_CHAIN_BASE::EdgeContainingPoint(), PNS::MEANDER_SHAPE::End(), PNS::DIFF_PAIR::EndingPrimitives(), findEndSegments(), PNS::NODE::findRedundantSegment(), PNS::MEANDER_SHAPE::Fit(), PNS::NODE::FixupVirtualVias(), SHAPE_SEGMENT::Format(), PNS::getDanglingAnchor(), BOARD::GetTrackLength(), ZONE::HatchBorder(), ALTIUM_PCB::HelperCreateBoardOutline(), PNS::HullIntersection(), SHAPE_LINE_CHAIN::Intersect(), CIRCLE::IntersectLine(), intersects(), isLine45Degree(), PNS::IsSegment45Degree(), KIGFX::VIEW_OVERLAY::Line(), CADSTAR_SCH_ARCHIVE_LOADER::loadShapeVertices(), PNS::MEANDER_SHAPE::MakeArc(), 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(), SHAPE_LINE_CHAIN::NearestPoint(), NearestPointDbl(), NearestPointFixpt(), operator!=(), operator=(), operator==(), Overlaps(), playground_main_func(), BRDITEMS_PLOTTER::PlotDimension(), SHAPE_LINE_CHAIN::PointAlong(), PNS::MEANDER_SHAPE::Recalculate(), PNS::NODE::removeSegmentIndex(), DRC_TEST_PROVIDER_CONNECTION_WIDTH::Run(), DRC_TEST_PROVIDER_EDGE_CLEARANCE::Run(), PCB_DIMENSION_BASE::segCircleIntersection(), GEOM_TEST::SegmentCompletelyInQuadrant(), GEOM_TEST::SegmentCompletelyWithinRadius(), GEOM_TEST::SegmentEndsInQuadrant(), PNS::SegmentHull(), PCB_DIMENSION_BASE::segPolyIntersection(), SHAPE_LINE_CHAIN::SelfIntersecting(), SHAPE_ARC::SHAPE_ARC(), SHAPE_LINE_CHAIN::Split(), PNS::LINE_PLACER::SplitAdjacentSegments(), SquaredDistance(), SHAPE_POLY_SET::SquaredDistanceToPolygon(), PNS::LINE_PLACER::Start(), PNS::DRAGGER::startDragSegment(), STROKE_PARAMS::Stroke(), PNS::SEGMENT::SwapEnds(), DRC_TEST_PROVIDER_PHYSICAL_CLEARANCE::testShapeLineChain(), DRC_TEST_PROVIDER_COPPER_CLEARANCE::testZonesToZones(), PNS::tightenSegment(), PCB_DIMENSION_BASE::TransformShapeWithClearanceToPolygon(), unfracture(), SHAPE_POLY_SET::unfractureSingle(), PNS::MEANDER_SHAPE::updateBaseSegment(), PCB_DIM_ALIGNED::updateGeometry(), PCB_DIM_ORTHOGONAL::updateGeometry(), PCB_DIM_RADIAL::updateGeometry(), PNS_KICAD_IFACE::UpdateItem(), POLYGON_GEOM_MANAGER::updateLeaderPoints(), and HYPERLYNX_EXPORTER::writeBoardInfo().

◆ m_index

int SEG::m_index
private

< index within the parent shape (used when m_is_local == false)

Definition at line 400 of file seg.h.

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


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