KiCad PCB EDA Suite
POLYGON_4PT_2D Class Reference

Simple non-intersecting polygon with 4 points. More...

#include <4pt_polygon_2d.h>

Inheritance diagram for POLYGON_4PT_2D:
OBJECT_2D

Public Member Functions

 POLYGON_4PT_2D (const SFVEC2F &v1, const SFVEC2F &v2, const SFVEC2F &v3, const SFVEC2F &v4, const BOARD_ITEM &aBoardItem)
 
const SFVEC2FGetV0 () const
 
const SFVEC2FGetV1 () const
 
const SFVEC2FGetV2 () const
 
const SFVEC2FGetV3 () const
 
const SFVEC2FGetN0 () const
 
const SFVEC2FGetN1 () const
 
const SFVEC2FGetN2 () const
 
const SFVEC2FGetN3 () const
 
bool Overlaps (const BBOX_2D &aBBox) const override
 Test if the box overlaps the object. More...
 
bool Intersects (const BBOX_2D &aBBox) const override
 a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅) More...
 
bool Intersect (const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
 
INTERSECTION_RESULT IsBBoxInside (const BBOX_2D &aBBox) const override
 Test this object if it's completely outside, intersects, or is completely inside aBBox. More...
 
bool IsPointInside (const SFVEC2F &aPoint) const override
 
const BOARD_ITEMGetBoardItem () const
 
const BBOX_2DGetBBox () const
 
const SFVEC2FGetCentroid () const
 
OBJECT_2D_TYPE GetObjectType () const
 

Protected Attributes

BBOX_2D m_bbox
 
SFVEC2F m_centroid
 
OBJECT_2D_TYPE m_obj_type
 
const BOARD_ITEMm_boardItem
 

Private Attributes

SFVEC2F m_segments [4]
 
SFVEC2F m_precalc_slope [4]
 
SFVEC2F m_seg_normal [4]
 

Detailed Description

Simple non-intersecting polygon with 4 points.

Used for footprint pads. (rectangles, trapezoids, with rotation.etc). This is a simplified version of the POLYGON_2D class.

Definition at line 41 of file 4pt_polygon_2d.h.

Constructor & Destructor Documentation

◆ POLYGON_4PT_2D()

POLYGON_4PT_2D::POLYGON_4PT_2D ( const SFVEC2F v1,
const SFVEC2F v2,
const SFVEC2F v3,
const SFVEC2F v4,
const BOARD_ITEM aBoardItem 
)

Definition at line 34 of file 4pt_polygon_2d.cpp.

35 :
37{
38 m_segments[0] = v1;
39 m_segments[1] = v4;
40 m_segments[2] = v3;
41 m_segments[3] = v2;
42
43 unsigned int i;
44 unsigned int j = 4 - 1;
45
46 for( i = 0; i < 4; j = i++ )
47 {
48 SFVEC2F slope = m_segments[j] - m_segments[i];
49 m_precalc_slope[i] = slope;
50 m_seg_normal[i] = glm::normalize( SFVEC2F( -slope.y, +slope.x ) );
51 }
52
53 m_bbox.Reset();
54 m_bbox.Union( v1 );
55 m_bbox.Union( v2 );
56 m_bbox.Union( v3 );
57 m_bbox.Union( v4 );
64
65 wxASSERT( m_bbox.IsInitialized() );
66}
BBOX_2D m_bbox
Definition: object_2d.h:110
SFVEC2F m_centroid
Definition: object_2d.h:111
OBJECT_2D(OBJECT_2D_TYPE aObjType, const BOARD_ITEM &aBoardItem)
Definition: object_2d.cpp:31
SFVEC2F m_precalc_slope[4]
SFVEC2F m_seg_normal[4]
SFVEC2F m_segments[4]
SFVEC2F GetCenter() const
Definition: bbox_2d.cpp:119
void Union(const SFVEC2F &aPoint)
Recalculate the bounding box adding a point.
Definition: bbox_2d.cpp:93
bool IsInitialized() const
Check if this bounding box is already initialized.
Definition: bbox_2d.cpp:79
void Reset()
Reset the bounding box to zero and uninitialize it.
Definition: bbox_2d.cpp:86
void ScaleNextUp()
Scale a bounding box to the next float representation making it larger.
Definition: bbox_2d.cpp:162
VECTOR3I v1(5, 5, 5)
VECTOR2I v2(1, 0)
Test suite for KiCad math code.
VECTOR3I v3(1, 1, 1)
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42

References BBOX_2D::GetCenter(), BBOX_2D::IsInitialized(), OBJECT_2D::m_bbox, OBJECT_2D::m_centroid, m_precalc_slope, m_seg_normal, m_segments, POLYGON4PT, BBOX_2D::Reset(), BBOX_2D::ScaleNextUp(), BBOX_2D::Union(), v1, v2, and v3.

Member Function Documentation

◆ GetBBox()

◆ GetBoardItem()

const BOARD_ITEM & OBJECT_2D::GetBoardItem ( ) const
inlineinherited

Definition at line 66 of file object_2d.h.

66{ return m_boardItem; }
const BOARD_ITEM & m_boardItem
Definition: object_2d.h:114

References OBJECT_2D::m_boardItem.

Referenced by RENDER_3D_RAYTRACE::createItemsFromContainer().

◆ GetCentroid()

const SFVEC2F & OBJECT_2D::GetCentroid ( ) const
inlineinherited

Definition at line 105 of file object_2d.h.

105{ return m_centroid; }

References OBJECT_2D::m_centroid.

Referenced by LAYER_ITEM::LAYER_ITEM(), sortByCentroidX(), sortByCentroidY(), and sortByCentroidZ().

◆ GetN0()

const SFVEC2F & POLYGON_4PT_2D::GetN0 ( ) const
inline

Definition at line 52 of file 4pt_polygon_2d.h.

52{ return m_seg_normal[0]; }

References m_seg_normal.

◆ GetN1()

const SFVEC2F & POLYGON_4PT_2D::GetN1 ( ) const
inline

Definition at line 53 of file 4pt_polygon_2d.h.

53{ return m_seg_normal[1]; }

References m_seg_normal.

◆ GetN2()

const SFVEC2F & POLYGON_4PT_2D::GetN2 ( ) const
inline

Definition at line 54 of file 4pt_polygon_2d.h.

54{ return m_seg_normal[2]; }

References m_seg_normal.

◆ GetN3()

const SFVEC2F & POLYGON_4PT_2D::GetN3 ( ) const
inline

Definition at line 55 of file 4pt_polygon_2d.h.

55{ return m_seg_normal[3]; }

References m_seg_normal.

◆ GetObjectType()

◆ GetV0()

const SFVEC2F & POLYGON_4PT_2D::GetV0 ( ) const
inline

Definition at line 47 of file 4pt_polygon_2d.h.

47{ return m_segments[0]; }

References m_segments.

Referenced by RENDER_3D_OPENGL::addObjectTriangles().

◆ GetV1()

const SFVEC2F & POLYGON_4PT_2D::GetV1 ( ) const
inline

Definition at line 48 of file 4pt_polygon_2d.h.

48{ return m_segments[1]; }

References m_segments.

Referenced by RENDER_3D_OPENGL::addObjectTriangles().

◆ GetV2()

const SFVEC2F & POLYGON_4PT_2D::GetV2 ( ) const
inline

Definition at line 49 of file 4pt_polygon_2d.h.

49{ return m_segments[2]; }

References m_segments.

Referenced by RENDER_3D_OPENGL::addObjectTriangles().

◆ GetV3()

const SFVEC2F & POLYGON_4PT_2D::GetV3 ( ) const
inline

Definition at line 50 of file 4pt_polygon_2d.h.

50{ return m_segments[3]; }

References m_segments.

Referenced by RENDER_3D_OPENGL::addObjectTriangles().

◆ Intersect()

bool POLYGON_4PT_2D::Intersect ( const RAYSEG2D aSegRay,
float *  aOutT,
SFVEC2F aNormalOut 
) const
overridevirtual
Parameters
aOutTa value between 0.0 and 1.0 in relation to the time of the hit of the segment.

Implements OBJECT_2D.

Definition at line 82 of file 4pt_polygon_2d.cpp.

83{
84 bool hited = false;
85 unsigned int hitIndex;
86 float bestHitT;
87
88 for( unsigned int i = 0; i < 4; i++ )
89 {
90 float t;
91
92 if( aSegRay.IntersectSegment( m_segments[i], m_precalc_slope[i], &t ) )
93 {
94 if( ( hited == false ) || ( t < bestHitT ) )
95 {
96 hited = true;
97 hitIndex = i;
98 bestHitT = t;
99 }
100 }
101 }
102
103 if( hited )
104 {
105 wxASSERT( ( bestHitT >= 0.0f ) && ( bestHitT <= 1.0f ) );
106
107 if( aOutT )
108 *aOutT = bestHitT;
109
110 if( aNormalOut )
111 *aNormalOut = m_seg_normal[hitIndex];
112
113 return true;
114 }
115
116 return false;
117}
bool IntersectSegment(const SFVEC2F &aStart, const SFVEC2F &aEnd_minus_start, float *aOutT) const
Definition: ray.cpp:262

References RAYSEG2D::IntersectSegment(), m_precalc_slope, m_seg_normal, and m_segments.

◆ Intersects()

bool POLYGON_4PT_2D::Intersects ( const BBOX_2D aBBox) const
overridevirtual

a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅)

Implements OBJECT_2D.

Definition at line 69 of file 4pt_polygon_2d.cpp.

70{
71 return m_bbox.Intersects( aBBox );
72}
bool Intersects(const BBOX_2D &aBBox) const
Test if a bounding box intersects this box.
Definition: bbox_2d.cpp:211

References BBOX_2D::Intersects(), and OBJECT_2D::m_bbox.

◆ IsBBoxInside()

INTERSECTION_RESULT POLYGON_4PT_2D::IsBBoxInside ( const BBOX_2D aBBox) const
overridevirtual

Test this object if it's completely outside, intersects, or is completely inside aBBox.

Returns
INTERSECTION_RESULT

Implements OBJECT_2D.

Definition at line 120 of file 4pt_polygon_2d.cpp.

121{
122 // !TODO:
123
125}

References MISSES.

◆ IsPointInside()

bool POLYGON_4PT_2D::IsPointInside ( const SFVEC2F aPoint) const
overridevirtual

Implements OBJECT_2D.

Definition at line 128 of file 4pt_polygon_2d.cpp.

129{
130 unsigned int i;
131 unsigned int j = 4 - 1;
132 bool oddNodes = false;
133
134 for( i = 0; i < 4; j = i++ )
135 {
136 const float polyJY = m_segments[j].y;
137 const float polyIY = m_segments[i].y;
138
139 if( ( ( polyIY <= aPoint.y ) && ( polyJY >= aPoint.y ) )
140 || ( ( polyJY <= aPoint.y ) && ( polyIY >= aPoint.y ) ) )
141 {
142 const float polyJX = m_segments[j].x;
143 const float polyIX = m_segments[i].x;
144
145 if( ( polyIX <= aPoint.x ) || ( polyJX <= aPoint.x ) )
146 {
147 oddNodes ^= ( ( polyIX + ( ( aPoint.y - polyIY ) / ( polyJY - polyIY ) )
148 * ( polyJX - polyIX ) )
149 < aPoint.x );
150 }
151 }
152 }
153
154 return oddNodes;
155}

References m_segments.

◆ Overlaps()

bool POLYGON_4PT_2D::Overlaps ( const BBOX_2D aBBox) const
overridevirtual

Test if the box overlaps the object.

Conformance Implements the Overlaps function from the OGC Simple Feature Specification at http://www.opengeospatial.org/standards/sfa. a.Overlaps(b) ⇔ ( dim(I(a)) = dim(I(b)) = dim(I(a)I(b))) ∧ (a ∩ b ≠ a) ∧ (a ∩ b ≠ b) It means that the result dimension of an overlap is the same dimensions of the bounding box (so the overlap cannot be a point or a line) and one of the boxes cannot full contain the other box.

Parameters
aBBoxis the bounding box to test.
Returns
true if the BBox intersects the object or is inside it.

Implements OBJECT_2D.

Definition at line 75 of file 4pt_polygon_2d.cpp.

76{
77 // NOT IMPLEMENTED
78 return true;
79}

Member Data Documentation

◆ m_bbox

◆ m_boardItem

const BOARD_ITEM& OBJECT_2D::m_boardItem
protectedinherited

Definition at line 114 of file object_2d.h.

Referenced by OBJECT_2D::GetBoardItem().

◆ m_centroid

◆ m_obj_type

OBJECT_2D_TYPE OBJECT_2D::m_obj_type
protectedinherited

Definition at line 112 of file object_2d.h.

Referenced by OBJECT_2D::GetObjectType(), and OBJECT_2D::OBJECT_2D().

◆ m_precalc_slope

SFVEC2F POLYGON_4PT_2D::m_precalc_slope[4]
private

Definition at line 65 of file 4pt_polygon_2d.h.

Referenced by Intersect(), and POLYGON_4PT_2D().

◆ m_seg_normal

SFVEC2F POLYGON_4PT_2D::m_seg_normal[4]
private

Definition at line 66 of file 4pt_polygon_2d.h.

Referenced by GetN0(), GetN1(), GetN2(), GetN3(), Intersect(), and POLYGON_4PT_2D().

◆ m_segments

SFVEC2F POLYGON_4PT_2D::m_segments[4]
private

Definition at line 64 of file 4pt_polygon_2d.h.

Referenced by GetV0(), GetV1(), GetV2(), GetV3(), Intersect(), IsPointInside(), and POLYGON_4PT_2D().


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