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 outer 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 86 of file cpolygon2d.h.

Constructor & Destructor Documentation

◆ CPOLYGONBLOCK2D()

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.

75  : COBJECT2D( OBJECT2D_TYPE::POLYGON, aBoardItem )
76 {
77  m_open_segments.resize( aOpenSegmentList.size() );
78 
79  // Copy vectors and structures
80  for( unsigned int i = 0; i < aOpenSegmentList.size(); i++ )
81  m_open_segments[i] = aOpenSegmentList[i];
82 
83  m_outers_and_holes = aOuter_and_holes;
84 
85  // Compute bounding box with the points of the polygon
86  m_bbox.Reset();
87 
88  for( unsigned int i = 0; i < m_outers_and_holes.m_Outers.size(); i++ )
89  {
90  for( unsigned int j = 0; j < m_outers_and_holes.m_Outers[i].size(); j++ )
91  m_bbox.Union( ((SEGMENTS)m_outers_and_holes.m_Outers[i])[j].m_Start );
92  }
93 
96 
97  // Some checks
98  wxASSERT( m_open_segments.size() == aOpenSegmentList.size() );
99  wxASSERT( m_open_segments.size() > 0 );
100 
101  wxASSERT( m_outers_and_holes.m_Outers.size() > 0 );
102  wxASSERT( m_outers_and_holes.m_Outers.size() == aOuter_and_holes.m_Outers.size() );
103  wxASSERT( m_outers_and_holes.m_Holes.size() == aOuter_and_holes.m_Holes.size() );
104 
105  wxASSERT( m_outers_and_holes.m_Outers[0].size() >= 3 );
106  wxASSERT( m_outers_and_holes.m_Outers[0].size() ==
107  aOuter_and_holes.m_Outers[0].size() );
108 
109  wxASSERT( m_bbox.IsInitialized() );
110 }
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:62
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:96
COBJECT2D(OBJECT2D_TYPE aObjType, const BOARD_ITEM &aBoardItem)
Definition: cobject2d.cpp:37
std::vector< SEGMENTS > m_Outers
Definition: cpolygon2d.h:76
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox2d.cpp:79
std::vector< SEGMENTS > m_Holes
Definition: cpolygon2d.h:77
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_centroid
Definition: cobject2d.h:66
SEGMENTS_WIDTH_NORMALS m_open_segments
This is the outer part of the polygon.
Definition: cpolygon2d.h:93
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, OUTERS_AND_HOLES::m_Holes, m_open_segments, OUTERS_AND_HOLES::m_Outers, m_outers_and_holes, POLYGON, 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::reload().

◆ GetCentroid()

const SFVEC2F& COBJECT2D::GetCentroid ( ) const
inlineinherited

◆ GetObjectType()

◆ Intersect()

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 130 of file cpolygon2d.cpp.

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

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

◆ Intersects()

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 113 of file cpolygon2d.cpp.

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

190 {
191 
193 }

References MISSES.

◆ IsPointInside()

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

Implements COBJECT2D.

Definition at line 196 of file cpolygon2d.cpp.

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

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

◆ Overlaps()

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 123 of file cpolygon2d.cpp.

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

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_open_segments

SEGMENTS_WIDTH_NORMALS CPOLYGONBLOCK2D::m_open_segments
private

This is the outer 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 93 of file cpolygon2d.h.

Referenced by CPOLYGONBLOCK2D(), and Intersect().

◆ m_outers_and_holes

OUTERS_AND_HOLES CPOLYGONBLOCK2D::m_outers_and_holes
private

A polygon block can have multiple polygon and holes.

Definition at line 96 of file cpolygon2d.h.

Referenced by CPOLYGONBLOCK2D(), and IsPointInside().


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