implement filling polygon with inner offset polygons and pocket cutting
Stefan Schuermans

Stefan Schuermans commited on 2013-01-28 21:50:18
Showing 3 changed files, with 70 additions and 5 deletions.

... ...
@@ -182,10 +182,16 @@ bool CmdParser::procCmd_cut_pocket(std::istream &strm)
182 182
   if (!getLayerPolys(strm, layerName, layer, polys))
183 183
     return false;
184 184
 
185
-  // TODO
186
-  std::cerr << "TODO: cut_pocket" << std::endl;
185
+  // fill insides of polygons by creating inner offset polygons
186
+  Polygons newPolys;
187
+  if (!polys.fillInnerOffset(mSettings.tool_diameter * 0.5, newPolys)) {
188
+    std::cerr << "filling insides of polygons failed" << std::endl;
189
+    return false;
190
+  }
191
+
192
+  // convert polygons back to layer (containing paths)
187 193
   Layer newLayer;
188
-  polys.writeToLayer(newLayer);
194
+  newPolys.writeToLayer(newLayer);
189 195
 
190 196
   // convert layer to G-code
191 197
   newLayer.toGCode(mSettings, mGCode);
... ...
@@ -12,6 +12,7 @@
12 12
 #include <CGAL/Polygon_with_holes_2.h>
13 13
 #include <CGAL/Boolean_set_operations_2.h>
14 14
 #include <CGAL/create_offset_polygons_from_polygon_with_holes_2.h>
15
+#include <CGAL/create_straight_skeleton_from_polygon_with_holes_2.h>
15 16
 
16 17
 #include "layer.h"
17 18
 #include "path.h"
... ...
@@ -161,7 +162,7 @@ bool Polygons::createInnerOffset(double offset, Polygons &offsetPolys) const
161 162
 {
162 163
   // clear output polygons
163 164
   offsetPolys.mPolys.clear();
164
-  // leave if tool diameter is invalid
165
+  // leave if offset size is invalid
165 166
   if (offset <= 0.0) {
166 167
     std::cerr << "invalid polygon offset distance " << offset << std::endl;
167 168
     return false;
... ...
@@ -196,7 +197,7 @@ bool Polygons::createOuterOffset(double offset, Polygons &offsetPolys) const
196 197
 {
197 198
   // clear output polygons
198 199
   offsetPolys.mPolys.clear();
199
-  // leave if tool diameter is invalid
200
+  // leave if offset size is invalid
200 201
   if (offset <= 0.0) {
201 202
     std::cerr << "invalid polygon offset distance " << offset << std::endl;
202 203
     return false;
... ...
@@ -212,6 +213,53 @@ bool Polygons::createOuterOffset(double offset, Polygons &offsetPolys) const
212 213
   return true;
213 214
 }
214 215
 
216
+/**
217
+ * @brief fill insides of polygons by creating inner offset polygons
218
+ * @param[in] offset offset, > 0.0
219
+ * @param[out] offsetPolys offset polygons (!= *this)
220
+ * @return if insides of polygons could be filles with inner offset polygons
221
+ */
222
+bool Polygons::fillInnerOffset(double offset, Polygons &offsetPolys) const
223
+{
224
+  // clear output polygons
225
+  offsetPolys.mPolys.clear();
226
+  // leave if offset size is invalid
227
+  if (offset <= 0.0) {
228
+    std::cerr << "invalid polygon offset distance " << offset << std::endl;
229
+    return false;
230
+  }
231
+
232
+  // process all polygons
233
+  CgPolyHolesVec::const_iterator poly;
234
+  for (poly = mPolys.begin(); poly != mPolys.end(); ++poly) {
235
+
236
+    // create interior straight skeleton
237
+    CgSsPtr skel = CGAL::create_interior_straight_skeleton_2(*poly);
238
+
239
+    // create inner offset polygons with increasing offset
240
+    size_t i;
241
+    for (i = 1; ; ++i) {
242
+
243
+      // create inner offset polygons
244
+      CgPolyPtrVec offPolys =
245
+        CGAL::create_offset_polygons_2<CgPoly>(offset * i, *skel);
246
+
247
+      // no offset polygons -> done
248
+      if (offPolys.empty())
249
+        break;
250
+
251
+      // add offset polygons to output polygons
252
+      CgPolyPtrVec::const_iterator offPoly;
253
+      for (offPoly = offPolys.begin(); offPoly != offPolys.end(); ++offPoly)
254
+        offsetPolys.mPolys.push_back(CgPolyHoles(**offPoly));
255
+
256
+    } // for i
257
+
258
+  } // for poly
259
+
260
+  return true;
261
+}
262
+
215 263
 /**
216 264
  * @brief add all polygons with holes as multiple paths to a layer
217 265
  * @param[in,out] layer layer to add paths to
... ...
@@ -12,6 +12,7 @@
12 12
 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
13 13
 #include <CGAL/Polygon_2.h>
14 14
 #include <CGAL/Polygon_with_holes_2.h>
15
+#include <CGAL/create_straight_skeleton_from_polygon_with_holes_2.h>
15 16
 
16 17
 #include "layer.h"
17 18
 
... ...
@@ -23,10 +24,12 @@ public:
23 24
   typedef CgKern::Point_2                    CgPoint;
24 25
   typedef CGAL::Polygon_2<CgKern>            CgPoly;
25 26
   typedef CGAL::Polygon_with_holes_2<CgKern> CgPolyHoles;
27
+  typedef CGAL::Straight_skeleton_2<CgKern>  CgSs;
26 28
   typedef std::vector<CgPoly>                CgPolyVec;
27 29
   typedef std::vector<CgPolyHoles>           CgPolyHolesVec;
28 30
   typedef boost::shared_ptr<CgPoly>          CgPolyPtr;
29 31
   typedef boost::shared_ptr<CgPolyHoles>     CgPolyHolesPtr;
32
+  typedef boost::shared_ptr<CgSs>            CgSsPtr;
30 33
   typedef std::vector<CgPolyPtr>             CgPolyPtrVec;
31 34
   typedef std::vector<CgPolyHolesPtr>        CgPolyHolesPtrVec;
32 35
 
... ...
@@ -67,6 +70,14 @@ public:
67 70
    */
68 71
   bool createOuterOffset(double offset, Polygons &offsetPolys) const;
69 72
 
73
+  /**
74
+   * @brief fill insides of polygons by creating inner offset polygons
75
+   * @param[in] offset offset, > 0.0
76
+   * @param[out] offsetPolys offset polygons (!= *this)
77
+   * @return if insides of polygons could be filles with inner offset polygons
78
+   */
79
+  bool fillInnerOffset(double offset, Polygons &offsetPolys) const;
80
+
70 81
   /**
71 82
    * @brief add all polygons with holes as multiple paths to a layer
72 83
    * @param[in,out] layer layer to add paths to
73 84