KiCad PCB EDA Suite
triangle_2d.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015-2016 Mario Luzeiro <[email protected]>
5 * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
29#include "triangle_2d.h"
30#include "../ray.h"
31#include <wx/debug.h>
33#include "../../../3d_fastmath.h"
34
35
36TRIANGLE_2D::TRIANGLE_2D( const SFVEC2F& aV1, const SFVEC2F& aV2, const SFVEC2F& aV3,
37 const BOARD_ITEM& aBoardItem ) :
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}
61
62
63bool TRIANGLE_2D::Intersects( const BBOX_2D& aBBox ) const
64{
65 if( !m_bbox.Intersects( aBBox ) )
66 return false;
67
69 return true;
70}
71
72
73bool TRIANGLE_2D::Overlaps( const BBOX_2D& aBBox ) const
74{
75 // NOT IMPLEMENTED
76 return false;
77}
78
79
80bool TRIANGLE_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
81{
82 return false;
83}
84
85
87{
88 if( !m_bbox.Intersects( aBBox ) )
90
91 // !TODO:
93}
94
95
96bool TRIANGLE_2D::IsPointInside( const SFVEC2F& aPoint ) const
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}
119
120
121void ConvertPolygonToTriangles( const SHAPE_POLY_SET& aPolyList, CONTAINER_2D_BASE& aDstContainer,
122 float aBiuTo3dUnitsScale, const BOARD_ITEM& aBoardItem )
123{
124 VECTOR2I a;
125 VECTOR2I b;
126 VECTOR2I c;
127
128 const_cast<SHAPE_POLY_SET&>( aPolyList ).CacheTriangulation( false );
129 const double conver_d = (double)aBiuTo3dUnitsScale;
130
131 for( unsigned int j = 0; j < aPolyList.TriangulatedPolyCount(); j++ )
132 {
133 const SHAPE_POLY_SET::TRIANGULATED_POLYGON* triPoly = aPolyList.TriangulatedPolygon( j );
134
135 for( size_t i = 0; i < triPoly->GetTriangleCount(); i++ )
136 {
137 triPoly->GetTriangle( i, a, b, c );
138
139 aDstContainer.Add( new TRIANGLE_2D( SFVEC2F( a.x * conver_d, -a.y * conver_d ),
140 SFVEC2F( b.x * conver_d, -b.y * conver_d ),
141 SFVEC2F( c.x * conver_d, -c.y * conver_d ),
142 aBoardItem ) );
143 }
144 }
145}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58
void Add(OBJECT_2D *aObject)
Definition: container_2d.h:49
BBOX_2D m_bbox
Definition: object_2d.h:110
SFVEC2F m_centroid
Definition: object_2d.h:111
void GetTriangle(int index, VECTOR2I &a, VECTOR2I &b, VECTOR2I &c) const
Represent a set of closed polygons.
const TRIANGULATED_POLYGON * TriangulatedPolygon(int aIndex) const
unsigned int TriangulatedPolyCount() const
Return the number of outlines in the set.
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
Definition: triangle_2d.cpp:80
INTERSECTION_RESULT IsBBoxInside(const BBOX_2D &aBBox) const override
Test this object if it's completely outside, intersects, or is completely inside aBBox.
Definition: triangle_2d.cpp:86
float m_p2y_minus_p3y
Definition: triangle_2d.h:60
bool Overlaps(const BBOX_2D &aBBox) const override
Test if the box overlaps the object.
Definition: triangle_2d.cpp:73
SFVEC2F p2
Definition: triangle_2d.h:56
float m_p3y_minus_p1y
Definition: triangle_2d.h:62
float m_p3x_minus_p2x
Definition: triangle_2d.h:61
TRIANGLE_2D(const SFVEC2F &aV1, const SFVEC2F &aV2, const SFVEC2F &aV3, const BOARD_ITEM &aBoardItem)
Definition: triangle_2d.cpp:36
bool Intersects(const BBOX_2D &aBBox) const override
a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅)
Definition: triangle_2d.cpp:63
bool IsPointInside(const SFVEC2F &aPoint) const override
Definition: triangle_2d.cpp:96
SFVEC2F p1
Definition: triangle_2d.h:55
SFVEC2F p3
Definition: triangle_2d.h:57
float m_inv_denominator
Definition: triangle_2d.h:59
float m_p1x_minus_p3x
Definition: triangle_2d.h:63
A triangle object.
Definition: triangle_3d.h:43
OBJECT_2D_TYPE
Definition: object_2d.h:45
INTERSECTION_RESULT
Definition: object_2d.h:37
Manage a bounding box defined by two SFVEC2F min max points.
Definition: bbox_2d.h:42
SFVEC2F GetCenter() const
Definition: bbox_2d.cpp:119
bool Intersects(const BBOX_2D &aBBox) const
Test if a bounding box intersects this box.
Definition: bbox_2d.cpp:211
void Union(const SFVEC2F &aPoint)
Recalculate the bounding box adding a point.
Definition: bbox_2d.cpp:93
bool IsInitialized() const
Check if this bounding box is already initialized.
Definition: bbox_2d.cpp:79
void Reset()
Reset the bounding box to zero and uninitialize it.
Definition: bbox_2d.cpp:86
void ScaleNextUp()
Scale a bounding box to the next float representation making it larger.
Definition: bbox_2d.cpp:162
Definition: ray.h:106
void ConvertPolygonToTriangles(const SHAPE_POLY_SET &aPolyList, CONTAINER_2D_BASE &aDstContainer, float aBiuTo3dUnitsScale, const BOARD_ITEM &aBoardItem)
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42