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 ( const SFVEC2F v1,
const SFVEC2F v2,
const SFVEC2F v3,
const SFVEC2F v4,
const BOARD_ITEM aBoardItem 
)

Definition at line 34 of file cpolygon4pts2d.cpp.

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

38  :
39  COBJECT2D( OBJ2D_POLYGON4PT, aBoardItem )
40 {/*
41  if( (v1.x > v2.x) || (v1.y < v2.y) )
42  {
43  m_segments[0] = v4;
44  m_segments[1] = v3;
45  m_segments[2] = v2;
46  m_segments[3] = v1;
47  }
48  else
49  {*/
50  m_segments[0] = v1;
51  m_segments[1] = v4;
52  m_segments[2] = v3;
53  m_segments[3] = v2;
54  // }
55 
56  unsigned int i;
57  unsigned int j = 4 - 1;
58 
59  for( i = 0; i < 4; j = i++ )
60  {
61  SFVEC2F slope = m_segments[j] - m_segments[i];
62  m_precalc_slope[i] = slope;
63  m_seg_normal[i] = glm::normalize( SFVEC2F( -slope.y, +slope.x ) );
64  }
65 
66  m_bbox.Reset();
67  m_bbox.Union( v1 );
68  m_bbox.Union( v2 );
69  m_bbox.Union( v3 );
70  m_bbox.Union( v4 );
77 
78  wxASSERT( m_bbox.IsInitialized() );
79 }
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox2d.cpp:79
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
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 GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox2d.cpp:121
SFVEC2F m_centroid
Definition: cobject2d.h:66

Member Function Documentation

const BOARD_ITEM& COBJECT2D::GetBoardItem ( ) const
inlineinherited

Definition at line 75 of file cobject2d.h.

References COBJECT2D::m_boardItem.

Referenced by C3D_RENDER_RAYTRACING::reload().

75 { return m_boardItem; }
const BOARD_ITEM & m_boardItem
Definition: cobject2d.h:69
const SFVEC2F& COBJECT2D::GetCentroid ( ) const
inlineinherited
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]
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]
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]
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]
const SFVEC2F& CPOLYGON4PTS2D::GetV0 ( ) const
inline

Definition at line 54 of file cpolygon4pts2d.h.

Referenced by C3D_RENDER_OGL_LEGACY::add_object_to_triangle_layer().

54 { return m_segments[0]; }
SFVEC2F m_segments[4]
const SFVEC2F& CPOLYGON4PTS2D::GetV1 ( ) const
inline

Definition at line 55 of file cpolygon4pts2d.h.

Referenced by C3D_RENDER_OGL_LEGACY::add_object_to_triangle_layer().

55 { return m_segments[1]; }
SFVEC2F m_segments[4]
const SFVEC2F& CPOLYGON4PTS2D::GetV2 ( ) const
inline

Definition at line 56 of file cpolygon4pts2d.h.

Referenced by C3D_RENDER_OGL_LEGACY::add_object_to_triangle_layer().

56 { return m_segments[2]; }
SFVEC2F m_segments[4]
const SFVEC2F& CPOLYGON4PTS2D::GetV3 ( ) const
inline

Definition at line 57 of file cpolygon4pts2d.h.

Referenced by C3D_RENDER_OGL_LEGACY::add_object_to_triangle_layer().

57 { return m_segments[3]; }
SFVEC2F m_segments[4]
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 138 of file cpolygon4pts2d.cpp.

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

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

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

83 {
84  return m_bbox.Intersects( aBBox );
85 
86  // This source code is not working OK.
87  /*
88  if( !m_bbox.Intersects( aBBox ) )
89  return false;
90 
91  // Check if the bouding box complety have inside the small bouding box
92  if( (aBBox.Max().x > m_bbox.Max().x) &&
93  (aBBox.Max().y > m_bbox.Max().x) &&
94  (aBBox.Min().x < m_bbox.Min().x) &&
95  (aBBox.Min().y < m_bbox.Min().y)
96  )
97  return true;
98 
99  SFVEC2F v[4];
100 
101  v[0] = aBBox.Min();
102  v[1] = SFVEC2F( aBBox.Min().x, aBBox.Max().y );
103  v[2] = aBBox.Max();
104  v[3] = SFVEC2F( aBBox.Max().x, aBBox.Min().y );
105 
106  for( unsigned int i = 0; i < 4; i++ )
107  {
108  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[0], v[1] - v[0] ) )
109  return true;
110  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[1], v[2] - v[1] ) )
111  return true;
112  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[2], v[3] - v[2] ) )
113  return true;
114  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[3], v[0] - v[3] ) )
115  return true;
116  }
117 
118  if( IsPointInside( v[0] ) )
119  return true;
120  if( IsPointInside( v[1] ) )
121  return true;
122  if( IsPointInside( v[2] ) )
123  return true;
124  if( IsPointInside( v[3] ) )
125  return true;
126 
127  return false;*/
128 }
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
INTERSECTION_RESULT CPOLYGON4PTS2D::IsBBoxInside ( const CBBOX2D aBBox) const
overridevirtual

Function IsBBoxInside.

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

Parameters
aBBox- The bounding box to test
Returns
INTERSECTION_RESULT

Implements COBJECT2D.

Definition at line 176 of file cpolygon4pts2d.cpp.

References INTR_MISSES.

177 {
178  // !TODO:
179 
180  return INTR_MISSES;
181 }
bool CPOLYGON4PTS2D::IsPointInside ( const SFVEC2F aPoint) const
overridevirtual

Implements COBJECT2D.

Definition at line 184 of file cpolygon4pts2d.cpp.

References m_segments.

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

132 {
133  // NOT IMPLEMENTED
134  return true;
135 }

Member Data Documentation

const BOARD_ITEM& COBJECT2D::m_boardItem
protectedinherited

Definition at line 69 of file cobject2d.h.

Referenced by COBJECT2D::GetBoardItem().

OBJECT2D_TYPE COBJECT2D::m_obj_type
protectedinherited

Definition at line 67 of file cobject2d.h.

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

SFVEC2F CPOLYGON4PTS2D::m_precalc_slope[4]
private

Definition at line 44 of file cpolygon4pts2d.h.

Referenced by CPOLYGON4PTS2D(), and Intersect().

SFVEC2F CPOLYGON4PTS2D::m_seg_normal[4]
private

Definition at line 45 of file cpolygon4pts2d.h.

Referenced by CPOLYGON4PTS2D(), and Intersect().

SFVEC2F CPOLYGON4PTS2D::m_segments[4]
private

Definition at line 43 of file cpolygon4pts2d.h.

Referenced by CPOLYGON4PTS2D(), Intersect(), and IsPointInside().


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