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

74  : COBJECT2D( OBJECT2D_TYPE::POLYGON, aBoardItem )
75 {
76  m_open_segments.resize( aOpenSegmentList.size() );
77 
78  // Copy vectors and structures
79  for( unsigned int i = 0; i < aOpenSegmentList.size(); i++ )
80  m_open_segments[i] = aOpenSegmentList[i];
81 
82  m_outers_and_holes = aOuter_and_holes;
83 
84  // Compute bounding box with the points of the polygon
85  m_bbox.Reset();
86 
87  for( unsigned int i = 0; i < m_outers_and_holes.m_Outers.size(); i++ )
88  {
89  for( unsigned int j = 0; j < m_outers_and_holes.m_Outers[i].size(); j++ )
90  m_bbox.Union( ((SEGMENTS)m_outers_and_holes.m_Outers[i])[j].m_Start );
91  }
92 
95 
96  // Some checks
97  wxASSERT( m_open_segments.size() == aOpenSegmentList.size() );
98  wxASSERT( m_open_segments.size() > 0 );
99 
100  wxASSERT( m_outers_and_holes.m_Outers.size() > 0 );
101  wxASSERT( m_outers_and_holes.m_Outers.size() == aOuter_and_holes.m_Outers.size() );
102  wxASSERT( m_outers_and_holes.m_Holes.size() == aOuter_and_holes.m_Holes.size() );
103 
104  wxASSERT( m_outers_and_holes.m_Outers[0].size() >= 3 );
105  wxASSERT( m_outers_and_holes.m_Outers[0].size() ==
106  aOuter_and_holes.m_Outers[0].size() );
107 
108  wxASSERT( m_bbox.IsInitialized() );
109 }
void Union(const SFVEC2F &aPoint)
Function Union recalculate the bounding box adding a point.
Definition: cbbox2d.cpp:94
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:87
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:78
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:163
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:120

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::createItemsFromContainer(), and 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 129 of file cpolygon2d.cpp.

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

113 {
114  return m_bbox.Intersects( aBBox );
115 
116  // !TODO: this is a quick not perfect implementation
117  // in order to make it perfect the box must be checked against all the
118  // polygons in the outers and not inside the holes
119 }
bool Intersects(const CBBOX2D &aBBox) const
Function Intersects test if a bounding box intersects this box.
Definition: cbbox2d.cpp:212
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 188 of file cpolygon2d.cpp.

189 {
190 
192 }

References MISSES.

◆ IsPointInside()

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

Implements COBJECT2D.

Definition at line 195 of file cpolygon2d.cpp.

196 {
197  // NOTE: we could add here a test for the bounding box, but because in the
198  // 3d object it already checked for a 3d bbox.
199 
200  // First test if point is inside a hole.
201  // If true it can early exit
202  for( unsigned int i = 0; i < m_outers_and_holes.m_Holes.size(); i++ )
203  if( !m_outers_and_holes.m_Holes[i].empty() )
205  return false;
206 
207  // At this moment, the point is not inside a hole, so check if it is
208  // inside the polygon
209  for( unsigned int i = 0; i < m_outers_and_holes.m_Outers.size(); i++ )
210  if( !m_outers_and_holes.m_Outers.empty() )
212  return true;
213 
214  // Miss the polygon
215  return false;
216 }
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:40

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

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

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: