KiCad PCB EDA Suite
TRIANGLE_2D Class Reference

#include <triangle_2d.h>

Inheritance diagram for TRIANGLE_2D:
OBJECT_2D

Public Member Functions

 TRIANGLE_2D (const SFVEC2F &aV1, const SFVEC2F &aV2, const SFVEC2F &aV3, const BOARD_ITEM &aBoardItem)
 
const SFVEC2FGetP1 () const
 
const SFVEC2FGetP2 () const
 
const SFVEC2FGetP3 () const
 
bool Overlaps (const BBOX_2D &aBBox) const override
 Test if the box overlaps the object. More...
 
bool Intersects (const BBOX_2D &aBBox) const override
 a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅) More...
 
bool Intersect (const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
 
INTERSECTION_RESULT IsBBoxInside (const BBOX_2D &aBBox) const override
 Test this object if it's completely outside, intersects, or is completely inside aBBox. More...
 
bool IsPointInside (const SFVEC2F &aPoint) const override
 
const BOARD_ITEMGetBoardItem () const
 
const BBOX_2DGetBBox () const
 
const SFVEC2FGetCentroid () const
 
OBJECT_2D_TYPE GetObjectType () const
 

Protected Attributes

BBOX_2D m_bbox
 
SFVEC2F m_centroid
 
OBJECT_2D_TYPE m_obj_type
 
const BOARD_ITEMm_boardItem
 

Private Attributes

SFVEC2F p1
 
SFVEC2F p2
 
SFVEC2F p3
 
float m_inv_denominator
 
float m_p2y_minus_p3y
 
float m_p3x_minus_p2x
 
float m_p3y_minus_p1y
 
float m_p1x_minus_p3x
 

Detailed Description

Definition at line 38 of file triangle_2d.h.

Constructor & Destructor Documentation

◆ TRIANGLE_2D()

TRIANGLE_2D::TRIANGLE_2D ( const SFVEC2F aV1,
const SFVEC2F aV2,
const SFVEC2F aV3,
const BOARD_ITEM aBoardItem 
)

Definition at line 36 of file triangle_2d.cpp.

37  :
38  OBJECT_2D( OBJECT_2D_TYPE::TRIANGLE, aBoardItem )
39 {
40  p1 = aV1;
41  p2 = aV2;
42  p3 = aV3;
43 
44  // Pre-Calc values
45  m_inv_denominator = 1.0f / ( ( p2.y - p3.y ) * ( p1.x - p3.x ) +
46  ( p3.x - p2.x ) * ( p1.y - p3.y ) );
47  m_p2y_minus_p3y = ( p2.y - p3.y );
48  m_p3x_minus_p2x = ( p3.x - p2.x );
49  m_p3y_minus_p1y = ( p3.y - p1.y );
50  m_p1x_minus_p3x = ( p1.x - p3.x );
51 
52  m_bbox.Reset();
53  m_bbox.Union( aV1 );
54  m_bbox.Union( aV2 );
55  m_bbox.Union( aV3 );
58 
59  wxASSERT( m_bbox.IsInitialized() );
60 }
BBOX_2D m_bbox
Definition: object_2d.h:110
SFVEC2F GetCenter() const
Definition: bbox_2d.cpp:119
SFVEC2F p2
Definition: triangle_2d.h:56
SFVEC2F m_centroid
Definition: object_2d.h:111
OBJECT_2D(OBJECT_2D_TYPE aObjType, const BOARD_ITEM &aBoardItem)
Definition: object_2d.cpp:36
float m_p3x_minus_p2x
Definition: triangle_2d.h:61
void Reset()
Reset the bounding box to zero and uninitialize it.
Definition: bbox_2d.cpp:86
SFVEC2F p3
Definition: triangle_2d.h:57
void Union(const SFVEC2F &aPoint)
Recalculate the bounding box adding a point.
Definition: bbox_2d.cpp:93
float m_p3y_minus_p1y
Definition: triangle_2d.h:62
void ScaleNextUp()
Scale a bounding box to the next float representation making it larger.
Definition: bbox_2d.cpp:162
float m_p2y_minus_p3y
Definition: triangle_2d.h:60
bool IsInitialized() const
Check if this bounding box is already initialized.
Definition: bbox_2d.cpp:79
float m_inv_denominator
Definition: triangle_2d.h:59
float m_p1x_minus_p3x
Definition: triangle_2d.h:63
SFVEC2F p1
Definition: triangle_2d.h:55

References BBOX_2D::GetCenter(), BBOX_2D::IsInitialized(), OBJECT_2D::m_bbox, OBJECT_2D::m_centroid, m_inv_denominator, m_p1x_minus_p3x, m_p2y_minus_p3y, m_p3x_minus_p2x, m_p3y_minus_p1y, p1, p2, p3, BBOX_2D::Reset(), BBOX_2D::ScaleNextUp(), and BBOX_2D::Union().

Member Function Documentation

◆ GetBBox()

◆ GetBoardItem()

const BOARD_ITEM& OBJECT_2D::GetBoardItem ( ) const
inlineinherited

Definition at line 66 of file object_2d.h.

66 { return m_boardItem; }
const BOARD_ITEM & m_boardItem
Definition: object_2d.h:114

References OBJECT_2D::m_boardItem.

Referenced by RENDER_3D_RAYTRACE::createItemsFromContainer().

◆ GetCentroid()

const SFVEC2F& OBJECT_2D::GetCentroid ( ) const
inlineinherited

Definition at line 105 of file object_2d.h.

105 { return m_centroid; }
SFVEC2F m_centroid
Definition: object_2d.h:111

References OBJECT_2D::m_centroid.

Referenced by LAYER_ITEM::LAYER_ITEM(), sortByCentroidX(), sortByCentroidY(), and sortByCentroidZ().

◆ GetObjectType()

◆ GetP1()

const SFVEC2F& TRIANGLE_2D::GetP1 ( ) const
inline

Definition at line 44 of file triangle_2d.h.

44 { return p1; }
SFVEC2F p1
Definition: triangle_2d.h:55

References p1.

Referenced by RENDER_3D_OPENGL::addObjectTriangles().

◆ GetP2()

const SFVEC2F& TRIANGLE_2D::GetP2 ( ) const
inline

Definition at line 45 of file triangle_2d.h.

45 { return p2; }
SFVEC2F p2
Definition: triangle_2d.h:56

References p2.

Referenced by RENDER_3D_OPENGL::addObjectTriangles().

◆ GetP3()

const SFVEC2F& TRIANGLE_2D::GetP3 ( ) const
inline

Definition at line 46 of file triangle_2d.h.

46 { return p3; }
SFVEC2F p3
Definition: triangle_2d.h:57

References p3.

Referenced by RENDER_3D_OPENGL::addObjectTriangles().

◆ Intersect()

bool TRIANGLE_2D::Intersect ( const RAYSEG2D aSegRay,
float *  aOutT,
SFVEC2F aNormalOut 
) const
overridevirtual
Parameters
aOutTa value between 0.0 and 1.0 in relation to the time of the hit of the segment.

Implements OBJECT_2D.

Definition at line 80 of file triangle_2d.cpp.

81 {
82  return false;
83 }

◆ Intersects()

bool TRIANGLE_2D::Intersects ( const BBOX_2D aBBox) const
overridevirtual

a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅)

TODO: Optimize

Implements OBJECT_2D.

Definition at line 63 of file triangle_2d.cpp.

64 {
65  if( !m_bbox.Intersects( aBBox ) )
66  return false;
67 
69  return true;
70 }
BBOX_2D m_bbox
Definition: object_2d.h:110
bool Intersects(const BBOX_2D &aBBox) const
Test if a bounding box intersects this box.
Definition: bbox_2d.cpp:211

References BBOX_2D::Intersects(), and OBJECT_2D::m_bbox.

◆ IsBBoxInside()

INTERSECTION_RESULT TRIANGLE_2D::IsBBoxInside ( const BBOX_2D aBBox) const
overridevirtual

Test this object if it's completely outside, intersects, or is completely inside aBBox.

Returns
INTERSECTION_RESULT

Implements OBJECT_2D.

Definition at line 86 of file triangle_2d.cpp.

87 {
88  if( !m_bbox.Intersects( aBBox ) )
90 
91  // !TODO:
93 }
BBOX_2D m_bbox
Definition: object_2d.h:110
bool Intersects(const BBOX_2D &aBBox) const
Test if a bounding box intersects this box.
Definition: bbox_2d.cpp:211

References BBOX_2D::Intersects(), OBJECT_2D::m_bbox, and MISSES.

◆ IsPointInside()

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

Implements OBJECT_2D.

Definition at line 96 of file triangle_2d.cpp.

97 {
98  // http://totologic.blogspot.co.uk/2014/01/accurate-point-in-triangle-test.html
99 
100  SFVEC2F point_minus_p3 = aPoint - p3;
101 
102  // barycentric coordinate system
103  const float a = ( m_p2y_minus_p3y * point_minus_p3.x +
104  m_p3x_minus_p2x * point_minus_p3.y ) * m_inv_denominator;
105 
106  if( 0.0f > a || a > 1.0f )
107  return false;
108 
109  const float b = ( m_p3y_minus_p1y * point_minus_p3.x +
110  m_p1x_minus_p3x * point_minus_p3.y ) * m_inv_denominator;
111 
112  if( 0.0f > b || b > 1.0f )
113  return false;
114 
115  const float c = 1.0f - a - b;
116 
117  return 0.0f <= c && c <= 1.0f;
118 }
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
float m_p3x_minus_p2x
Definition: triangle_2d.h:61
SFVEC2F p3
Definition: triangle_2d.h:57
float m_p3y_minus_p1y
Definition: triangle_2d.h:62
float m_p2y_minus_p3y
Definition: triangle_2d.h:60
float m_inv_denominator
Definition: triangle_2d.h:59
float m_p1x_minus_p3x
Definition: triangle_2d.h:63

References m_inv_denominator, m_p1x_minus_p3x, m_p2y_minus_p3y, m_p3x_minus_p2x, m_p3y_minus_p1y, and p3.

◆ Overlaps()

bool TRIANGLE_2D::Overlaps ( const BBOX_2D aBBox) const
overridevirtual

Test if the box overlaps the object.

Conformance Implements the Overlaps function from the OGC Simple Feature Specification at 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 dimensions 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
aBBoxis the bounding box to test.
Returns
true if the BBox intersects the object or is inside it.

Implements OBJECT_2D.

Definition at line 73 of file triangle_2d.cpp.

74 {
75  // NOT IMPLEMENTED
76  return false;
77 }

Member Data Documentation

◆ m_bbox

◆ m_boardItem

const BOARD_ITEM& OBJECT_2D::m_boardItem
protectedinherited

Definition at line 114 of file object_2d.h.

Referenced by OBJECT_2D::GetBoardItem().

◆ m_centroid

◆ m_inv_denominator

float TRIANGLE_2D::m_inv_denominator
private

Definition at line 59 of file triangle_2d.h.

Referenced by IsPointInside(), and TRIANGLE_2D().

◆ m_obj_type

OBJECT_2D_TYPE OBJECT_2D::m_obj_type
protectedinherited

Definition at line 112 of file object_2d.h.

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

◆ m_p1x_minus_p3x

float TRIANGLE_2D::m_p1x_minus_p3x
private

Definition at line 63 of file triangle_2d.h.

Referenced by IsPointInside(), and TRIANGLE_2D().

◆ m_p2y_minus_p3y

float TRIANGLE_2D::m_p2y_minus_p3y
private

Definition at line 60 of file triangle_2d.h.

Referenced by IsPointInside(), and TRIANGLE_2D().

◆ m_p3x_minus_p2x

float TRIANGLE_2D::m_p3x_minus_p2x
private

Definition at line 61 of file triangle_2d.h.

Referenced by IsPointInside(), and TRIANGLE_2D().

◆ m_p3y_minus_p1y

float TRIANGLE_2D::m_p3y_minus_p1y
private

Definition at line 62 of file triangle_2d.h.

Referenced by IsPointInside(), and TRIANGLE_2D().

◆ p1

SFVEC2F TRIANGLE_2D::p1
private

Definition at line 55 of file triangle_2d.h.

Referenced by GetP1(), and TRIANGLE_2D().

◆ p2

SFVEC2F TRIANGLE_2D::p2
private

Definition at line 56 of file triangle_2d.h.

Referenced by GetP2(), and TRIANGLE_2D().

◆ p3

SFVEC2F TRIANGLE_2D::p3
private

Definition at line 57 of file triangle_2d.h.

Referenced by GetP3(), IsPointInside(), and TRIANGLE_2D().


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