KiCad PCB EDA Suite
CPOLYGON4PTS2D Class Reference

This handles simple polygons with 4 points. More...

#include <cpolygon4pts2d.h>

Inheritance diagram for CPOLYGON4PTS2D:
COBJECT2D

Public Member Functions

 CPOLYGON4PTS2D (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 CBBOX2D &aBBox) const override
 Function Overlaps. More...
 
bool Intersects (const CBBOX2D &aBBox) const override
 Function Intersects. More...
 
bool Intersect (const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
 Function Intersect. More...
 
INTERSECTION_RESULT IsBBoxInside (const CBBOX2D &aBBox) const override
 Function IsBBoxInside. More...
 
bool IsPointInside (const SFVEC2F &aPoint) const override
 
const BOARD_ITEMGetBoardItem () const
 
const CBBOX2DGetBBox () const
 
const SFVEC2FGetCentroid () const
 
OBJECT2D_TYPE GetObjectType () const
 

Protected Attributes

CBBOX2D m_bbox
 
SFVEC2F m_centroid
 
OBJECT2D_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

This handles simple polygons with 4 points.

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

Definition at line 40 of file cpolygon4pts2d.h.

Constructor & Destructor Documentation

◆ CPOLYGON4PTS2D()

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

Definition at line 34 of file cpolygon4pts2d.cpp.

36  : COBJECT2D( OBJECT2D_TYPE::POLYGON4PT, aBoardItem )
37 {/*
38  if( (v1.x > v2.x) || (v1.y < v2.y) )
39  {
40  m_segments[0] = v4;
41  m_segments[1] = v3;
42  m_segments[2] = v2;
43  m_segments[3] = v1;
44  }
45  else
46  {*/
47  m_segments[0] = v1;
48  m_segments[1] = v4;
49  m_segments[2] = v3;
50  m_segments[3] = v2;
51  // }
52 
53  unsigned int i;
54  unsigned int j = 4 - 1;
55 
56  for( i = 0; i < 4; j = i++ )
57  {
58  SFVEC2F slope = m_segments[j] - m_segments[i];
59  m_precalc_slope[i] = slope;
60  m_seg_normal[i] = glm::normalize( SFVEC2F( -slope.y, +slope.x ) );
61  }
62 
63  m_bbox.Reset();
64  m_bbox.Union( v1 );
65  m_bbox.Union( v2 );
66  m_bbox.Union( v3 );
67  m_bbox.Union( v4 );
74 
75  wxASSERT( m_bbox.IsInitialized() );
76 }
void Union(const SFVEC2F &aPoint)
Function Union recalculate the bounding box adding a point.
Definition: cbbox2d.cpp:95
SFVEC2F m_segments[4]
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox2d.cpp:88
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
SFVEC2F m_seg_normal[4]
COBJECT2D(OBJECT2D_TYPE aObjType, const BOARD_ITEM &aBoardItem)
Definition: cobject2d.cpp:37
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox2d.cpp:79
void ScaleNextUp()
Function ScaleNextUp scales a bounding box to the next float representation making it larger.
Definition: cbbox2d.cpp:164
CBBOX2D m_bbox
Definition: cobject2d.h:65
SFVEC2F m_precalc_slope[4]
SFVEC2F m_centroid
Definition: cobject2d.h:66
SFVEC2F GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox2d.cpp:121

References CBBOX2D::GetCenter(), CBBOX2D::IsInitialized(), COBJECT2D::m_bbox, COBJECT2D::m_centroid, m_precalc_slope, m_seg_normal, m_segments, POLYGON4PT, CBBOX2D::Reset(), CBBOX2D::ScaleNextUp(), and CBBOX2D::Union().

Member Function Documentation

◆ GetBBox()

◆ GetBoardItem()

const BOARD_ITEM& COBJECT2D::GetBoardItem ( ) const
inlineinherited

Definition at line 75 of file cobject2d.h.

75 { return m_boardItem; }
const BOARD_ITEM & m_boardItem
Definition: cobject2d.h:69

References COBJECT2D::m_boardItem.

Referenced by C3D_RENDER_RAYTRACING::createItemsFromContainer(), and C3D_RENDER_RAYTRACING::reload().

◆ GetCentroid()

const SFVEC2F& COBJECT2D::GetCentroid ( ) const
inlineinherited

◆ GetN0()

const SFVEC2F& CPOLYGON4PTS2D::GetN0 ( ) const
inline

Definition at line 59 of file cpolygon4pts2d.h.

59 { return m_seg_normal[0]; }
SFVEC2F m_seg_normal[4]

References m_seg_normal.

◆ GetN1()

const SFVEC2F& CPOLYGON4PTS2D::GetN1 ( ) const
inline

Definition at line 60 of file cpolygon4pts2d.h.

60 { return m_seg_normal[1]; }
SFVEC2F m_seg_normal[4]

References m_seg_normal.

◆ GetN2()

const SFVEC2F& CPOLYGON4PTS2D::GetN2 ( ) const
inline

Definition at line 61 of file cpolygon4pts2d.h.

61 { return m_seg_normal[2]; }
SFVEC2F m_seg_normal[4]

References m_seg_normal.

◆ GetN3()

const SFVEC2F& CPOLYGON4PTS2D::GetN3 ( ) const
inline

Definition at line 62 of file cpolygon4pts2d.h.

62 { return m_seg_normal[3]; }
SFVEC2F m_seg_normal[4]

References m_seg_normal.

◆ GetObjectType()

◆ GetV0()

const SFVEC2F& CPOLYGON4PTS2D::GetV0 ( ) const
inline

Definition at line 54 of file cpolygon4pts2d.h.

54 { return m_segments[0]; }
SFVEC2F m_segments[4]

References m_segments.

Referenced by C3D_RENDER_OGL_LEGACY::add_object_to_triangle_layer().

◆ GetV1()

const SFVEC2F& CPOLYGON4PTS2D::GetV1 ( ) const
inline

Definition at line 55 of file cpolygon4pts2d.h.

55 { return m_segments[1]; }
SFVEC2F m_segments[4]

References m_segments.

Referenced by C3D_RENDER_OGL_LEGACY::add_object_to_triangle_layer().

◆ GetV2()

const SFVEC2F& CPOLYGON4PTS2D::GetV2 ( ) const
inline

Definition at line 56 of file cpolygon4pts2d.h.

56 { return m_segments[2]; }
SFVEC2F m_segments[4]

References m_segments.

Referenced by C3D_RENDER_OGL_LEGACY::add_object_to_triangle_layer().

◆ GetV3()

const SFVEC2F& CPOLYGON4PTS2D::GetV3 ( ) const
inline

Definition at line 57 of file cpolygon4pts2d.h.

57 { return m_segments[3]; }
SFVEC2F m_segments[4]

References m_segments.

Referenced by C3D_RENDER_OGL_LEGACY::add_object_to_triangle_layer().

◆ Intersect()

bool CPOLYGON4PTS2D::Intersect ( const RAYSEG2D aSegRay,
float *  aOutT,
SFVEC2F aNormalOut 
) const
overridevirtual

Function Intersect.

Intersect

Parameters
aSegRay
aOutTa value between 0.0 and 1.0 in relation to the time of the hit of the segment
aNormalOut
Returns

Implements COBJECT2D.

Definition at line 135 of file cpolygon4pts2d.cpp.

138 {
139  wxASSERT( aOutT );
140  wxASSERT( aNormalOut );
141 
142  bool hited = false;
143  unsigned int hitIndex;
144  float bestHitT;
145 
146  for( unsigned int i = 0; i < 4; i++ )
147  {
148  float t;
149 
150  if( aSegRay.IntersectSegment( m_segments[i], m_precalc_slope[i], &t ) )
151  if( (hited == false) || ( t < bestHitT) )
152  {
153  hited = true;
154  hitIndex = i;
155  bestHitT = t;
156  }
157  }
158 
159  if( hited )
160  {
161  wxASSERT( (bestHitT >= 0.0f) && (bestHitT <= 1.0f) );
162 
163  *aOutT = bestHitT;
164  *aNormalOut = m_seg_normal[hitIndex];
165 
166  return true;
167  }
168 
169  return false;
170 }
SFVEC2F m_segments[4]
bool IntersectSegment(const SFVEC2F &aStart, const SFVEC2F &aEnd_minus_start, float *aOutT) const
Definition: ray.cpp:293
SFVEC2F m_seg_normal[4]
SFVEC2F m_precalc_slope[4]

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

◆ Intersects()

bool CPOLYGON4PTS2D::Intersects ( const CBBOX2D aBBox) const
overridevirtual

Function Intersects.

Intersects - a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅) It intersects if the result intersection is not null

Parameters
aBBox
Returns

Implements COBJECT2D.

Definition at line 79 of file cpolygon4pts2d.cpp.

80 {
81  return m_bbox.Intersects( aBBox );
82 
83  // This source code is not working OK.
84  /*
85  if( !m_bbox.Intersects( aBBox ) )
86  return false;
87 
88  // Check if the bounding box complety have inside the small bounding box
89  if( (aBBox.Max().x > m_bbox.Max().x) &&
90  (aBBox.Max().y > m_bbox.Max().x) &&
91  (aBBox.Min().x < m_bbox.Min().x) &&
92  (aBBox.Min().y < m_bbox.Min().y)
93  )
94  return true;
95 
96  SFVEC2F v[4];
97 
98  v[0] = aBBox.Min();
99  v[1] = SFVEC2F( aBBox.Min().x, aBBox.Max().y );
100  v[2] = aBBox.Max();
101  v[3] = SFVEC2F( aBBox.Max().x, aBBox.Min().y );
102 
103  for( unsigned int i = 0; i < 4; i++ )
104  {
105  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[0], v[1] - v[0] ) )
106  return true;
107  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[1], v[2] - v[1] ) )
108  return true;
109  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[2], v[3] - v[2] ) )
110  return true;
111  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[3], v[0] - v[3] ) )
112  return true;
113  }
114 
115  if( IsPointInside( v[0] ) )
116  return true;
117  if( IsPointInside( v[1] ) )
118  return true;
119  if( IsPointInside( v[2] ) )
120  return true;
121  if( IsPointInside( v[3] ) )
122  return true;
123 
124  return false;*/
125 }
bool Intersects(const CBBOX2D &aBBox) const
Function Intersects test if a bounding box intersects this box.
Definition: cbbox2d.cpp:213
CBBOX2D m_bbox
Definition: cobject2d.h:65

References CBBOX2D::Intersects(), and COBJECT2D::m_bbox.

◆ IsBBoxInside()

INTERSECTION_RESULT CPOLYGON4PTS2D::IsBBoxInside ( const CBBOX2D aBBox) const
overridevirtual

Function IsBBoxInside.

Tests if the bounding is out, intersects or is complety inside

Returns
INTERSECTION_RESULT

Implements COBJECT2D.

Definition at line 173 of file cpolygon4pts2d.cpp.

174 {
175  // !TODO:
176 
178 }

References MISSES.

◆ IsPointInside()

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

Implements COBJECT2D.

Definition at line 181 of file cpolygon4pts2d.cpp.

182 {
183  unsigned int i;
184  unsigned int j = 4 - 1;
185  bool oddNodes = false;
186 
187  for( i = 0; i < 4; j = i++ )
188  {
189  const float polyJY = m_segments[j].y;
190  const float polyIY = m_segments[i].y;
191 
192  if( ((polyIY <= aPoint.y) && (polyJY >= aPoint.y)) ||
193  ((polyJY <= aPoint.y) && (polyIY >= aPoint.y))
194  )
195  {
196  const float polyJX = m_segments[j].x;
197  const float polyIX = m_segments[i].x;
198 
199  if( (polyIX <= aPoint.x) || (polyJX <= aPoint.x) )
200  {
201  oddNodes ^= ( ( polyIX +
202  ( ( aPoint.y - polyIY ) / ( polyJY - polyIY ) ) *
203  ( polyJX - polyIX ) ) < aPoint.x );
204  }
205  }
206  }
207 
208  return oddNodes;
209 }
SFVEC2F m_segments[4]

References m_segments.

◆ Overlaps()

bool CPOLYGON4PTS2D::Overlaps ( const CBBOX2D aBBox) const
overridevirtual

Function Overlaps.

Test if the box overlaps the object Conformance The function overlaps implements function Overlaps from the OGC Simple Feature Specification. 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 dimentions 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
aBBox- The bounding box to test
Returns
true if the BBox intersects the object or is inside it

Implements COBJECT2D.

Definition at line 128 of file cpolygon4pts2d.cpp.

129 {
130  // NOT IMPLEMENTED
131  return true;
132 }

Member Data Documentation

◆ m_bbox

◆ m_boardItem

const BOARD_ITEM& COBJECT2D::m_boardItem
protectedinherited

Definition at line 69 of file cobject2d.h.

Referenced by COBJECT2D::GetBoardItem().

◆ m_centroid

◆ m_obj_type

OBJECT2D_TYPE COBJECT2D::m_obj_type
protectedinherited

Definition at line 67 of file cobject2d.h.

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

◆ m_precalc_slope

SFVEC2F CPOLYGON4PTS2D::m_precalc_slope[4]
private

Definition at line 44 of file cpolygon4pts2d.h.

Referenced by CPOLYGON4PTS2D(), and Intersect().

◆ m_seg_normal

SFVEC2F CPOLYGON4PTS2D::m_seg_normal[4]
private

Definition at line 45 of file cpolygon4pts2d.h.

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

◆ m_segments

SFVEC2F CPOLYGON4PTS2D::m_segments[4]
private

Definition at line 43 of file cpolygon4pts2d.h.

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


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