KiCad PCB EDA Suite
CPOLYGONBLOCK2D Class Reference

This class represents a sub polygon block. More...

#include <cpolygon2d.h>

Inheritance diagram for CPOLYGONBLOCK2D:
COBJECT2D

Public Member Functions

 CPOLYGONBLOCK2D (const SEGMENTS_WIDTH_NORMALS &aOpenSegmentList, const OUTERS_AND_HOLES &aOuter_and_holes, const BOARD_ITEM &aBoardItem)
 
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

SEGMENTS_WIDTH_NORMALS m_open_segments
 This is the outter part of the polygon. More...
 
OUTERS_AND_HOLES m_outers_and_holes
 A polygon block can have multiple polygon and holes. More...
 

Detailed Description

This class represents a sub polygon block.

This polygon block was created from a general polygon definition that was sub divided and create blocks of polygons. This polygon class represent a sub part of that main polygon. There is information for the contours (used to test the ray2d intersection) and a close definition of the block polygon to test if a point is inside.

Definition at line 87 of file cpolygon2d.h.

Constructor & Destructor Documentation

CPOLYGONBLOCK2D::CPOLYGONBLOCK2D ( const SEGMENTS_WIDTH_NORMALS aOpenSegmentList,
const OUTERS_AND_HOLES aOuter_and_holes,
const BOARD_ITEM aBoardItem 
)

Definition at line 73 of file cpolygon2d.cpp.

References CBBOX2D::GetCenter(), CBBOX2D::IsInitialized(), COBJECT2D::m_bbox, COBJECT2D::m_centroid, OUTERS_AND_HOLES::m_Holes, m_open_segments, OUTERS_AND_HOLES::m_Outers, m_outers_and_holes, CBBOX2D::Reset(), CBBOX2D::ScaleNextUp(), and CBBOX2D::Union().

75  :
76  COBJECT2D( OBJ2D_POLYGON, aBoardItem )
77 {
78  m_open_segments.resize( aOpenSegmentList.size() );
79 
80  // Copy vectors and structures
81  for( unsigned int i = 0; i < aOpenSegmentList.size(); i++ )
82  m_open_segments[i] = aOpenSegmentList[i];
83 
84  m_outers_and_holes = aOuter_and_holes;
85 
86  // Compute bounding box with the points of the polygon
87  m_bbox.Reset();
88 
89  for( unsigned int i = 0; i < m_outers_and_holes.m_Outers.size(); i++ )
90  {
91  for( unsigned int j = 0; j < m_outers_and_holes.m_Outers[i].size(); j++ )
92  m_bbox.Union( ((SEGMENTS)m_outers_and_holes.m_Outers[i])[j].m_Start );
93  }
94 
97 
98  // Some checks
99  wxASSERT( m_open_segments.size() == aOpenSegmentList.size() );
100  wxASSERT( m_open_segments.size() > 0 );
101 
102  wxASSERT( m_outers_and_holes.m_Outers.size() > 0 );
103  wxASSERT( m_outers_and_holes.m_Outers.size() == aOuter_and_holes.m_Outers.size() );
104  wxASSERT( m_outers_and_holes.m_Holes.size() == aOuter_and_holes.m_Holes.size() );
105 
106  wxASSERT( m_outers_and_holes.m_Outers[0].size() >= 3 );
107  wxASSERT( m_outers_and_holes.m_Outers[0].size() ==
108  aOuter_and_holes.m_Outers[0].size() );
109 
110  wxASSERT( m_bbox.IsInitialized() );
111 }
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
std::vector< POLYSEGMENT > SEGMENTS
Definition: cpolygon2d.h:63
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox2d.cpp:88
OUTERS_AND_HOLES m_outers_and_holes
A polygon block can have multiple polygon and holes.
Definition: cpolygon2d.h:97
COBJECT2D(OBJECT2D_TYPE aObjType, const BOARD_ITEM &aBoardItem)
Definition: cobject2d.cpp:37
std::vector< SEGMENTS > m_Outers
Definition: cpolygon2d.h:77
std::vector< SEGMENTS > m_Holes
Definition: cpolygon2d.h:78
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 GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox2d.cpp:121
SFVEC2F m_centroid
Definition: cobject2d.h:66
SEGMENTS_WIDTH_NORMALS m_open_segments
This is the outter part of the polygon.
Definition: cpolygon2d.h:94

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
bool CPOLYGONBLOCK2D::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 131 of file cpolygon2d.cpp.

References RAYSEG2D::m_End_minus_start, m_open_segments, and RAYSEG2D::m_Start.

134 {
135  int hitIndex = -1;
136  float hitU = 0.0f;
137  float tMin = 0.0f;
138 
139  for( unsigned int i = 0; i < m_open_segments.size(); i++ )
140  {
141  const SFVEC2F &s = m_open_segments[i].m_Precalc_slope;
142  const SFVEC2F &q = m_open_segments[i].m_Start;
143 
144  float rxs = aSegRay.m_End_minus_start.x * s.y -
145  aSegRay.m_End_minus_start.y * s.x;
146 
147  if( fabs(rxs) > FLT_EPSILON )
148  {
149  const float inv_rxs = 1.0f / rxs;
150 
151  const SFVEC2F pq = q - aSegRay.m_Start;
152 
153  const float t = (pq.x * s.y - pq.y * s.x) * inv_rxs;
154 
155  if( (t < 0.0f) || (t > 1.0f) )
156  continue;
157 
158  const float u = ( pq.x * aSegRay.m_End_minus_start.y -
159  pq.y * aSegRay.m_End_minus_start.x ) * inv_rxs;
160 
161  if( (u < 0.0f) || (u > 1.0f) )
162  continue;
163 
164  if( ( hitIndex == -1 ) || ( t <= tMin ) )
165  {
166  tMin = t;
167  hitIndex = i;
168  hitU = u;
169  }
170  }
171  }
172 
173  if( hitIndex >= 0 )
174  {
175  wxASSERT( (tMin >= 0.0f) && (tMin <= 1.0f) );
176 
177  *aOutT = tMin;
178  *aNormalOut = glm::normalize(
179  m_open_segments[hitIndex].m_Normals.m_Start * hitU +
180  m_open_segments[hitIndex].m_Normals.m_End *
181  (1.0f - hitU) );
182 
183  return true;
184  }
185 
186  return false;
187 }
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
SFVEC2F m_Start
Definition: ray.h:88
SEGMENTS_WIDTH_NORMALS m_open_segments
This is the outter part of the polygon.
Definition: cpolygon2d.h:94
SFVEC2F m_End_minus_start
Definition: ray.h:90
bool CPOLYGONBLOCK2D::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 114 of file cpolygon2d.cpp.

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

115 {
116  return m_bbox.Intersects( aBBox );
117 
118  // !TODO: this is a quick not perfect implementation
119  // in order to make it perfect the box must be checked against all the
120  // polygons in the outers and not inside the holes
121 }
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 CPOLYGONBLOCK2D::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 190 of file cpolygon2d.cpp.

References INTR_MISSES.

191 {
192 
193  return INTR_MISSES;
194 }
bool CPOLYGONBLOCK2D::IsPointInside ( const SFVEC2F aPoint) const
overridevirtual

Implements COBJECT2D.

Definition at line 197 of file cpolygon2d.cpp.

References OUTERS_AND_HOLES::m_Holes, OUTERS_AND_HOLES::m_Outers, m_outers_and_holes, and polygon_IsPointInside().

198 {
199  // NOTE: we could add here a test for the bounding box, but because in the
200  // 3d object it already checked for a 3d bbox.
201 
202  // First test if point is inside a hole.
203  // If true it can early exit
204  for( unsigned int i = 0; i < m_outers_and_holes.m_Holes.size(); i++ )
205  if( !m_outers_and_holes.m_Holes[i].empty() )
207  return false;
208 
209  // At this moment, the point is not inside a hole, so check if it is
210  // inside the polygon
211  for( unsigned int i = 0; i < m_outers_and_holes.m_Outers.size(); i++ )
212  if( !m_outers_and_holes.m_Outers.empty() )
214  return true;
215 
216  // Miss the polygon
217  return false;
218 }
OUTERS_AND_HOLES m_outers_and_holes
A polygon block can have multiple polygon and holes.
Definition: cpolygon2d.h:97
std::vector< SEGMENTS > m_Outers
Definition: cpolygon2d.h:77
std::vector< SEGMENTS > m_Holes
Definition: cpolygon2d.h:78
static bool polygon_IsPointInside(const SEGMENTS &aSegments, const SFVEC2F &aPoint)
Definition: cpolygon2d.cpp:41
bool CPOLYGONBLOCK2D::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 124 of file cpolygon2d.cpp.

125 {
126  // NOT IMPLEMENTED
127  return false;
128 }

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

SEGMENTS_WIDTH_NORMALS CPOLYGONBLOCK2D::m_open_segments
private

This is the outter part of the polygon.

This list is used to test a ray intersection with the boundaries of this sub polygon. It contains also the interpolated normals that are passed from the main polygon.

Definition at line 94 of file cpolygon2d.h.

Referenced by CPOLYGONBLOCK2D(), and Intersect().

OUTERS_AND_HOLES CPOLYGONBLOCK2D::m_outers_and_holes
private

A polygon block can have multiple polygon and holes.

Definition at line 97 of file cpolygon2d.h.

Referenced by CPOLYGONBLOCK2D(), and IsPointInside().


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