KiCad PCB EDA Suite
ctriangle2d.cpp File Reference
#include "ctriangle2d.h"
#include <map>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/shared_array.hpp>
#include <wx/debug.h>
#include <wx/glcanvas.h>
#include "../../../3d_fastmath.h"
#include <poly2tri/poly2tri.h>

Go to the source code of this file.

Macros

#define APPLY_EDGE_SHRINK
 
#define POLY_SCALE_FACT   256
 
#define POLY_SCALE_FACT_INVERSE   (1.0 / (double)(POLY_SCALE_FACT))
 

Functions

template<class C >
void FreeClear (C &cntr)
 
void EdgeShrink (std::vector< SFVEC2I64 > &aPath)
 
void Convert_shape_line_polygon_to_triangles (const SHAPE_POLY_SET &aPolyList, CGENERICCONTAINER2D &aDstContainer, float aBiuTo3DunitsScale, const BOARD_ITEM &aBoardItem)
 

Macro Definition Documentation

#define APPLY_EDGE_SHRINK

Definition at line 150 of file ctriangle2d.cpp.

#define POLY_SCALE_FACT   256

Definition at line 155 of file ctriangle2d.cpp.

Referenced by Convert_shape_line_polygon_to_triangles().

#define POLY_SCALE_FACT_INVERSE   (1.0 / (double)(POLY_SCALE_FACT))

Definition at line 156 of file ctriangle2d.cpp.

Referenced by Convert_shape_line_polygon_to_triangles().

Function Documentation

void Convert_shape_line_polygon_to_triangles ( const SHAPE_POLY_SET aPolyList,
CGENERICCONTAINER2D aDstContainer,
float  aBiuTo3DunitsScale,
const BOARD_ITEM aBoardItem 
)

Definition at line 159 of file ctriangle2d.cpp.

References CGENERICCONTAINER2D::Add(), p2t::CDT::AddHole(), SHAPE_POLY_SET::CHole(), SHAPE_POLY_SET::COutline(), SHAPE_LINE_CHAIN::CPoint(), EdgeShrink(), FreeClear(), p2t::Triangle::GetPoint(), p2t::CDT::GetTriangles(), SHAPE_POLY_SET::HoleCount(), SHAPE_POLY_SET::OutlineCount(), SHAPE_LINE_CHAIN::PointCount(), POLY_SCALE_FACT, POLY_SCALE_FACT_INVERSE, p2t::CDT::Triangulate(), p2t::Point::x, VECTOR2< T >::x, p2t::Point::y, and VECTOR2< T >::y.

Referenced by CINFO3D_VISU::AddShapeWithClearanceToContainer(), CINFO3D_VISU::AddSolidAreasShapesToContainer(), CINFO3D_VISU::createNewPadWithClearance(), C3D_RENDER_OGL_LEGACY::generate_3D_Vias_and_Pads(), and C3D_RENDER_OGL_LEGACY::reload().

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 }
Point * GetPoint(const int &index)
Definition: shapes.h:288
void EdgeShrink(std::vector< SFVEC2I64 > &aPath)
Definition: edgeshrink.cpp:74
int PointCount() const
Function PointCount()
double y
Definition: shapes.h:47
int HoleCount(int aOutline) const
Returns the number of holes in a given outline
void AddHole(std::vector< Point * > polyline)
Add a hole.
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:47
Definition: cdt.h:47
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
void FreeClear(C &cntr)
void Triangulate()
Triangulate - do this AFTER you've added the polyline, holes, and Steiner points. ...
void Add(COBJECT2D *aObject)
Definition: ccontainer2d.h:51
std::vector< Triangle * > GetTriangles()
Get CDT triangles.
Class SHAPE_LINE_CHAIN.
glm::i64vec2 SFVEC2I64
Definition: xv3d_types.h:44
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
#define POLY_SCALE_FACT
#define POLY_SCALE_FACT_INVERSE
void EdgeShrink ( std::vector< SFVEC2I64 > &  aPath)

Definition at line 74 of file edgeshrink.cpp.

References S_INC.

Referenced by Convert_shape_line_polygon_to_triangles().

75 {
76  unsigned int prev = aPath.size() - 1;
77 
78  for( unsigned int i = 0; i < aPath.size(); i++ )
79  {
80  // Adjust coordinate by 1 depending on the direction
81  (aPath[i].x - aPath[prev].x) > 0 ? aPath[i].x -= S_INC :
82  aPath[i].x += S_INC;
83 
84  (aPath[i].y - aPath[prev].y) > 0 ? aPath[i].y -= S_INC :
85  aPath[i].y += S_INC;
86 
87  prev = i;
88  }
89 }
#define S_INC
Definition: edgeshrink.cpp:72
template<class C >
void FreeClear ( C &  cntr)

Definition at line 136 of file ctriangle2d.cpp.

Referenced by Convert_shape_line_polygon_to_triangles().

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 }