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__
38 
39 #include "../../../3d_fastmath.h"
40 #include <poly2tri/poly2tri.h>
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  return 0.0f <= a && a <= 1.0f &&
131  0.0f <= b && b <= 1.0f &&
132  0.0f <= c && c <= 1.0f;*/
133 }
134 
135 
136 template <class C> void FreeClear( C & cntr )
137 {
138  for( typename C::iterator it = cntr.begin();
139  it != cntr.end();
140  ++it )
141  {
142  delete * it;
143  }
144 
145  cntr.clear();
146 }
147 
148 // Note: Please check edgeshrink.cpp in order to learn the EdgeShrink propose
149 
150 #define APPLY_EDGE_SHRINK
151 
152 #ifdef APPLY_EDGE_SHRINK
153 extern void EdgeShrink( std::vector<SFVEC2I64> &aPath );
154 
155 #define POLY_SCALE_FACT 256
156 #define POLY_SCALE_FACT_INVERSE (1.0 / (double)(POLY_SCALE_FACT))
157 #endif
158 
160  CGENERICCONTAINER2D &aDstContainer,
161  float aBiuTo3DunitsScale ,
162  const BOARD_ITEM &aBoardItem )
163 {
164  unsigned int nOutlines = aPolyList.OutlineCount();
165 
166 
167  for( unsigned int idx = 0; idx < nOutlines; ++idx )
168  {
169  const SHAPE_LINE_CHAIN &outlinePath = aPolyList.COutline( idx );
170 
171  wxASSERT( outlinePath.PointCount() >= 3 );
172 
173  std::vector<SFVEC2I64> scaledOutline;
174  scaledOutline.resize( outlinePath.PointCount() );
175 
176  // printf("\nidx: %u\n", idx);
177 
178  // Apply a scale to the points
179  for( unsigned int i = 0;
180  i < (unsigned int)outlinePath.PointCount();
181  ++i )
182  {
183  const VECTOR2I& a = outlinePath.CPoint( i );
184 
185 #ifdef APPLY_EDGE_SHRINK
186  scaledOutline[i] = SFVEC2I64( (glm::int64)a.x * POLY_SCALE_FACT,
187  (glm::int64)a.y * POLY_SCALE_FACT );
188 #else
189  scaledOutline[i] = SFVEC2I64( (glm::int64)a.x,
190  (glm::int64)a.y );
191 #endif
192  }
193 
194 #ifdef APPLY_EDGE_SHRINK
195  // Apply a modification to the points
196  EdgeShrink( scaledOutline );
197 #endif
198  // Copy to a array of pointers
199  std::vector<p2t::Point*> polyline;
200  polyline.resize( outlinePath.PointCount() );
201 
202  for( unsigned int i = 0;
203  i < (unsigned int)scaledOutline.size();
204  ++i )
205  {
206  const SFVEC2I64 &a = scaledOutline[i];
207 
208  //printf("%lu %lu\n", a.x, a.y);
209 
210  polyline[i] = new p2t::Point( (double)a.x,
211  (double)a.y );
212  }
213 
214  // Start creating the structured to be triangulated
215  p2t::CDT* cdt = new p2t::CDT( polyline );
216 
217  // Add holes for this outline
218  unsigned int nHoles = aPolyList.HoleCount( idx );
219 
220  std::vector< std::vector<p2t::Point*> > polylineHoles;
221 
222  polylineHoles.resize( nHoles );
223 
224  for( unsigned int idxHole = 0; idxHole < nHoles; ++idxHole )
225  {
226  const SHAPE_LINE_CHAIN &outlineHoles = aPolyList.CHole( idx,
227  idxHole );
228 
229  wxASSERT( outlineHoles.PointCount() >= 3 );
230 
231  std::vector<SFVEC2I64> scaledHole;
232  scaledHole.resize( outlineHoles.PointCount() );
233 
234  // Apply a scale to the points
235  for( unsigned int i = 0;
236  i < (unsigned int)outlineHoles.PointCount();
237  ++i )
238  {
239  const VECTOR2I &h = outlineHoles.CPoint( i );
240 #ifdef APPLY_EDGE_SHRINK
241  scaledHole[i] = SFVEC2I64( (glm::int64)h.x * POLY_SCALE_FACT,
242  (glm::int64)h.y * POLY_SCALE_FACT );
243 #else
244  scaledHole[i] = SFVEC2I64( (glm::int64)h.x,
245  (glm::int64)h.y );
246 #endif
247  }
248 
249 #ifdef APPLY_EDGE_SHRINK
250  // Apply a modification to the points
251  EdgeShrink( scaledHole );
252 #endif
253 
254  // Resize and reserve space
255  polylineHoles[idxHole].resize( outlineHoles.PointCount() );
256 
257  for( unsigned int i = 0;
258  i < (unsigned int)outlineHoles.PointCount();
259  ++i )
260  {
261  const SFVEC2I64 &h = scaledHole[i];
262 
263  polylineHoles[idxHole][i] = new p2t::Point( h.x, h.y );
264  }
265 
266  cdt->AddHole( polylineHoles[idxHole] );
267  }
268 
269  // Triangulate
270  cdt->Triangulate();
271 
272  // Hint: if you find any crashes on the triangulation poly2tri library,
273  // you can use the following site to debug the points and it will mark
274  // the errors in the polygon:
275  // http://r3mi.github.io/poly2tri.js/
276 
277 
278  // Get and add triangles
279  std::vector<p2t::Triangle*> triangles;
280  triangles = cdt->GetTriangles();
281 
282 #ifdef APPLY_EDGE_SHRINK
283  const double conver_d = (double)aBiuTo3DunitsScale *
285 #else
286  const double conver_d = (double)aBiuTo3DunitsScale;
287 #endif
288  for( unsigned int i = 0; i < triangles.size(); ++i )
289  {
290  p2t::Triangle& t = *triangles[i];
291 
292  p2t::Point& a = *t.GetPoint( 0 );
293  p2t::Point& b = *t.GetPoint( 1 );
294  p2t::Point& c = *t.GetPoint( 2 );
295 
296  aDstContainer.Add( new CTRIANGLE2D( SFVEC2F( a.x * conver_d,
297  -a.y * conver_d ),
298  SFVEC2F( b.x * conver_d,
299  -b.y * conver_d ),
300  SFVEC2F( c.x * conver_d,
301  -c.y * conver_d ),
302  aBoardItem ) );
303  }
304 
305  // Delete created data
306  delete cdt;
307 
308  // Free points
309  FreeClear(polyline);
310 
311  for( unsigned int idxHole = 0; idxHole < nHoles; ++idxHole )
312  {
313  FreeClear( polylineHoles[idxHole] );
314  }
315  }
316 }
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
Point * GetPoint(const int &index)
Definition: shapes.h:309
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
void EdgeShrink(std::vector< SFVEC2I64 > &aPath)
Definition: edgeshrink.cpp:74
int PointCount() const
Function PointCount()
Class CBBOX manages a bounding box defined by two SFVEC2F min max points.
Definition: cbbox2d.h:40
double y
Definition: shapes.h:46
int HoleCount(int aOutline) const
Returns the number of holes in a given outline
void AddHole(std::vector< Point * > polyline)
Add a hole.
float m_p3y_minus_p1y
Definition: ctriangle2d.h:49
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
int OutlineCount() const
Returns the number of outlines in the set
double x
Definition: shapes.h:46
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
Definition: cdt.h:47
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.
void Convert_shape_line_polygon_to_triangles(const SHAPE_POLY_SET &aPolyList, CGENERICCONTAINER2D &aDstContainer, float aBiuTo3DunitsScale, const BOARD_ITEM &aBoardItem)
float m_p1x_minus_p3x
Definition: ctriangle2d.h:50
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
void FreeClear(C &cntr)
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
void Triangulate()
Triangulate - do this AFTER you've added the polyline, holes, and Steiner points. ...
CBBOX2D m_bbox
Definition: cobject2d.h:65
void Add(COBJECT2D *aObject)
Definition: ccontainer2d.h:51
std::vector< Triangle * > GetTriangles()
Get CDT triangles.
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
Class SHAPE_LINE_CHAIN.
CTRIANGLE2D(const SFVEC2F &aV1, const SFVEC2F &aV2, const SFVEC2F &aV3, const BOARD_ITEM &aBoardItem)
Definition: ctriangle2d.cpp:43
glm::i64vec2 SFVEC2I64
Definition: xv3d_types.h:44
SFVEC2F m_centroid
Definition: cobject2d.h:66
bool Overlaps(const CBBOX2D &aBBox) const override
Function Overlaps.
Definition: ctriangle2d.cpp:81
float m_p2y_minus_p3y
Definition: ctriangle2d.h:47
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
SFVEC2F p3
Definition: ctriangle2d.h:44
#define POLY_SCALE_FACT
#define POLY_SCALE_FACT_INVERSE
Definition: ray.h:86