KiCad PCB EDA Suite
ctriangle2d.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 <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 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 
30 #include "ctriangle2d.h"
31 #include <map>
32 #include <boost/smart_ptr/shared_ptr.hpp>
33 #include <boost/smart_ptr/shared_array.hpp>
34 #include <wx/debug.h>
35 
36 #include <wx/glcanvas.h> // CALLBACK definition, needed on Windows
37  // alse needed on OSX to define __DARWIN__
39 #include "../../../3d_fastmath.h"
40 
41 
42 
44  const SFVEC2F &aV2,
45  const SFVEC2F &aV3,
46  const BOARD_ITEM &aBoardItem ) : COBJECT2D( OBJ2D_TRIANGLE,
47  aBoardItem )
48 {
49  p1 = aV1;
50  p2 = aV2;
51  p3 = aV3;
52 
53  // Pre-Calc values
54  m_inv_denominator = 1.0f / ( (p2.y - p3.y) * (p1.x - p3.x) +
55  (p3.x - p2.x) * (p1.y - p3.y));
56  m_p2y_minus_p3y = (p2.y - p3.y);
57  m_p3x_minus_p2x = (p3.x - p2.x);
58  m_p3y_minus_p1y = (p3.y - p1.y);
59  m_p1x_minus_p3x = (p1.x - p3.x);
60 
61  m_bbox.Reset();
62  m_bbox.Union( aV1 );
63  m_bbox.Union( aV2 );
64  m_bbox.Union( aV3 );
67 
68  wxASSERT( m_bbox.IsInitialized() );
69 }
70 
71 
72 bool CTRIANGLE2D::Intersects( const CBBOX2D &aBBox ) const
73 {
74  if( !m_bbox.Intersects( aBBox ) )
75  return false;
77  return true;
78 }
79 
80 
81 bool CTRIANGLE2D::Overlaps( const CBBOX2D &aBBox ) const
82 {
83  // NOT IMPLEMENTED
84  return false;
85 }
86 
87 
88 bool CTRIANGLE2D::Intersect( const RAYSEG2D &aSegRay,
89  float *aOutT,
90  SFVEC2F *aNormalOut ) const
91 {
92  wxASSERT( aOutT );
93  wxASSERT( aNormalOut );
94  return false;
95 }
96 
97 
99 {
100  if( !m_bbox.Intersects( aBBox ) )
101  return INTR_MISSES;
102  // !TODO:
103  return INTR_MISSES;
104 }
105 
106 
107 bool CTRIANGLE2D::IsPointInside( const SFVEC2F &aPoint ) const
108 {
109  // http://totologic.blogspot.co.uk/2014/01/accurate-point-in-triangle-test.html
110 
111  SFVEC2F point_minus_p3 = aPoint - p3;
112 
113  // barycentric coordinate system
114  const float a = ( m_p2y_minus_p3y * point_minus_p3.x +
115  m_p3x_minus_p2x * point_minus_p3.y ) * m_inv_denominator;
116 
117  if( 0.0f > a || a > 1.0f )
118  return false;
119 
120  const float b = ( m_p3y_minus_p1y * point_minus_p3.x +
121  m_p1x_minus_p3x * point_minus_p3.y ) * m_inv_denominator;
122 
123  if( 0.0f > b || b > 1.0f )
124  return false;
125 
126  const float c = 1.0f - a - b;
127 
128  return 0.0f <= c && c <= 1.0f;
129 }
130 
131 
133  CGENERICCONTAINER2D &aDstContainer,
134  float aBiuTo3DunitsScale ,
135  const BOARD_ITEM &aBoardItem )
136 {
137 
138  aPolyList.CacheTriangulation();
139  const double conver_d = (double)aBiuTo3DunitsScale;
140 
141  for( unsigned int j = 0; j < aPolyList.TriangulatedPolyCount(); j++ )
142  {
143  auto triPoly = aPolyList.TriangulatedPolygon( j );
144 
145  for( size_t i = 0; i < triPoly->GetTriangleCount(); i++ )
146  {
147  VECTOR2I a;
148  VECTOR2I b;
149  VECTOR2I c;
150  triPoly->GetTriangle( i, a, b, c );
151 
152  aDstContainer.Add( new CTRIANGLE2D( SFVEC2F( a.x * conver_d,
153  -a.y * conver_d ),
154  SFVEC2F( b.x * conver_d,
155  -b.y * conver_d ),
156  SFVEC2F( c.x * conver_d,
157  -c.y * conver_d ),
158  aBoardItem ) );
159  }
160 
161  }
162 }
INTERSECTION_RESULT
Definition: cobject2d.h:38
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox2d.cpp:79
void Union(const SFVEC2F &aPoint)
Function Union recalculate the bounding box adding a point.
Definition: cbbox2d.cpp:95
SFVEC2F p1
Definition: ctriangle2d.h:42
float m_p3x_minus_p2x
Definition: ctriangle2d.h:48
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
Class CBBOX manages a bounding box defined by two SFVEC2F min max points.
Definition: cbbox2d.h:40
float m_p3y_minus_p1y
Definition: ctriangle2d.h:49
bool Intersects(const CBBOX2D &aBBox) const
Function Intersects test if a bounding box intersects this box.
Definition: cbbox2d.cpp:213
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox2d.cpp:88
INTERSECTION_RESULT IsBBoxInside(const CBBOX2D &aBBox) const override
Function IsBBoxInside.
Definition: ctriangle2d.cpp:98
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
bool IsPointInside(const SFVEC2F &aPoint) const override
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
Function Intersect.
Definition: ctriangle2d.cpp:88
Class SHAPE_POLY_SET.
const TRIANGULATED_POLYGON * TriangulatedPolygon(int aIndex) const
float m_p1x_minus_p3x
Definition: ctriangle2d.h:50
float m_inv_denominator
Definition: ctriangle2d.h:46
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
void Add(COBJECT2D *aObject)
Definition: ccontainer2d.h:52
SFVEC2F GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox2d.cpp:121
SFVEC2F p2
Definition: ctriangle2d.h:43
bool Intersects(const CBBOX2D &aBBox) const override
Function Intersects.
Definition: ctriangle2d.cpp:72
unsigned int TriangulatedPolyCount() const
Returns the number of triangulated polygons
CTRIANGLE2D(const SFVEC2F &aV1, const SFVEC2F &aV2, const SFVEC2F &aV3, const BOARD_ITEM &aBoardItem)
Definition: ctriangle2d.cpp:43
SFVEC2F m_centroid
Definition: cobject2d.h:66
size_t i
Definition: json11.cpp:597
bool Overlaps(const CBBOX2D &aBBox) const override
Function Overlaps.
Definition: ctriangle2d.cpp:81
float m_p2y_minus_p3y
Definition: ctriangle2d.h:47
SFVEC2F p3
Definition: ctriangle2d.h:44
void Convert_shape_line_polygon_to_triangles(SHAPE_POLY_SET &aPolyList, CGENERICCONTAINER2D &aDstContainer, float aBiuTo3DunitsScale, const BOARD_ITEM &aBoardItem)
Definition: ray.h:86