KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
24
25#include "triangle_2d.h"
26#include "../ray.h"
27#include <wx/debug.h>
30
31
32TRIANGLE_2D::TRIANGLE_2D( const SFVEC2F& aV1, const SFVEC2F& aV2, const SFVEC2F& aV3,
33 const BOARD_ITEM& aBoardItem ) :
34 OBJECT_2D( OBJECT_2D_TYPE::TRIANGLE, aBoardItem )
35{
36 p1 = aV1;
37 p2 = aV2;
38 p3 = aV3;
39
40 // Pre-Calc values
41 m_inv_denominator = 1.0f / ( ( p2.y - p3.y ) * ( p1.x - p3.x ) +
42 ( p3.x - p2.x ) * ( p1.y - p3.y ) );
43 m_p2y_minus_p3y = ( p2.y - p3.y );
44 m_p3x_minus_p2x = ( p3.x - p2.x );
45 m_p3y_minus_p1y = ( p3.y - p1.y );
46 m_p1x_minus_p3x = ( p1.x - p3.x );
47
48 m_bbox.Reset();
49 m_bbox.Union( aV1 );
50 m_bbox.Union( aV2 );
51 m_bbox.Union( aV3 );
52 m_bbox.ScaleNextUp();
53 m_centroid = m_bbox.GetCenter();
54
55 wxASSERT( m_bbox.IsInitialized() );
56}
57
58
59bool TRIANGLE_2D::Intersects( const BBOX_2D& aBBox ) const
60{
61 if( !m_bbox.Intersects( aBBox ) )
62 return false;
63
65 return true;
66}
67
68
69bool TRIANGLE_2D::Overlaps( const BBOX_2D& aBBox ) const
70{
71 // NOT IMPLEMENTED
72 return false;
73}
74
75
76bool TRIANGLE_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
77{
78 return false;
79}
80
81
83{
84 if( !m_bbox.Intersects( aBBox ) )
86
87 // !TODO:
89}
90
91
92bool TRIANGLE_2D::IsPointInside( const SFVEC2F& aPoint ) const
93{
94 // http://totologic.blogspot.co.uk/2014/01/accurate-point-in-triangle-test.html
95
96 SFVEC2F point_minus_p3 = aPoint - p3;
97
98 // barycentric coordinate system
99 const float a = ( m_p2y_minus_p3y * point_minus_p3.x +
100 m_p3x_minus_p2x * point_minus_p3.y ) * m_inv_denominator;
101
102 if( 0.0f > a || a > 1.0f )
103 return false;
104
105 const float b = ( m_p3y_minus_p1y * point_minus_p3.x +
106 m_p1x_minus_p3x * point_minus_p3.y ) * m_inv_denominator;
107
108 if( 0.0f > b || b > 1.0f )
109 return false;
110
111 const float c = 1.0f - a - b;
112
113 return 0.0f <= c && c <= 1.0f;
114}
115
116
117void ConvertPolygonToTriangles( const SHAPE_POLY_SET& aPolyList, CONTAINER_2D_BASE& aDstContainer,
118 float aBiuTo3dUnitsScale, const BOARD_ITEM& aBoardItem )
119{
120 VECTOR2I a;
121 VECTOR2I b;
122 VECTOR2I c;
123
124 const_cast<SHAPE_POLY_SET&>( aPolyList ).CacheTriangulation();
125 const double conver_d = (double)aBiuTo3dUnitsScale;
126
127 for( unsigned int j = 0; j < aPolyList.TriangulatedPolyCount(); j++ )
128 {
129 const SHAPE_POLY_SET::TRIANGULATED_POLYGON* triPoly = aPolyList.TriangulatedPolygon( j );
130
131 for( size_t i = 0; i < triPoly->GetTriangleCount(); i++ )
132 {
133 triPoly->GetTriangle( i, a, b, c );
134
135 aDstContainer.Add( new TRIANGLE_2D( SFVEC2F( a.x * conver_d, -a.y * conver_d ),
136 SFVEC2F( b.x * conver_d, -b.y * conver_d ),
137 SFVEC2F( c.x * conver_d, -c.y * conver_d ),
138 aBoardItem ) );
139 }
140 }
141}
Defines math related functions.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
void Add(OBJECT_2D *aObject)
BBOX_2D m_bbox
Definition object_2d.h:106
SFVEC2F m_centroid
Definition object_2d.h:107
OBJECT_2D(OBJECT_2D_TYPE aObjType, const BOARD_ITEM &aBoardItem)
Definition object_2d.cpp:27
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 triangulated polygons.
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.
float m_p2y_minus_p3y
Definition triangle_2d.h:55
bool Overlaps(const BBOX_2D &aBBox) const override
Test if the box overlaps the object.
SFVEC2F p2
Definition triangle_2d.h:51
float m_p3y_minus_p1y
Definition triangle_2d.h:57
float m_p3x_minus_p2x
Definition triangle_2d.h:56
TRIANGLE_2D(const SFVEC2F &aV1, const SFVEC2F &aV2, const SFVEC2F &aV3, const BOARD_ITEM &aBoardItem)
bool Intersects(const BBOX_2D &aBBox) const override
a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅)
bool IsPointInside(const SFVEC2F &aPoint) const override
SFVEC2F p1
Definition triangle_2d.h:50
SFVEC2F p3
Definition triangle_2d.h:52
float m_inv_denominator
Definition triangle_2d.h:54
float m_p1x_minus_p3x
Definition triangle_2d.h:58
A triangle object.
Definition triangle_3d.h:39
OBJECT_2D_TYPE
Definition object_2d.h:41
INTERSECTION_RESULT
Definition object_2d.h:33
Manage a bounding box defined by two SFVEC2F min max points.
Definition bbox_2d.h:38
void ConvertPolygonToTriangles(const SHAPE_POLY_SET &aPolyList, CONTAINER_2D_BASE &aDstContainer, float aBiuTo3dUnitsScale, const BOARD_ITEM &aBoardItem)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
glm::vec2 SFVEC2F
Definition xv3d_types.h:38