BlinkenArea - GitList
Repositories
Blog
Wiki
dxfngc
Code
Commits
Branches
Tags
Search
Tree:
ba3a06c
Branches
Tags
master
dxfngc
src
polygons.cpp
implement inside cutting (holes not yet working properly)
Stefan Schuermans
commited
ba3a06c
at 2013-01-27 21:19:38
polygons.cpp
Blame
History
Raw
/* drawing (DXF) to G-code (NGC) converter * Copyright 2013 Stefan Schuermans <stefan@schuermans.info> * Copyleft: CC-BY-SA http://creativecommons.org/licenses/by-sa/3.0/ */ #include <boost/shared_ptr.hpp> #include <iostream> #include <vector> #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/Polygon_2.h> #include <CGAL/Polygon_with_holes_2.h> #include <CGAL/Boolean_set_operations_2.h> #include <CGAL/create_offset_polygons_from_polygon_with_holes_2.h> #include "layer.h" #include "path.h" #include "point.h" #include "polygons.h" /** * @brief clear all polygons */ void Polygons::clear() { mPolys.clear(); } /** * @brief add polygons from layer * @param[in] layer layer to obtain the polygons from * @param[in] eqDist maximum distance of two points to be considered equal * @return if the layer could be converted to polygons and imported */ bool Polygons::addLayer(const Layer &layer, double eqDist) { // convert all paths to simple polygons CgPolyVec simples; Layer::Paths::const_iterator path; for (path = layer.mPaths.begin(); path != layer.mPaths.end(); ++path) { // skip empty paths if (path->mPoints.empty()) continue; // check that path is closed if (!path->mPoints.front().equals(path->mPoints.back(), eqDist)) { std::cerr << "path not closed" << std::endl; return false; } // create simple polygon from path // - do not add last point to polygon, as it is the same as the first one CgPoly simple; Path::Points::const_iterator pt; for (pt = path->mPoints.begin(); pt + 1 != path->mPoints.end(); ++pt) simple.push_back(CgPoint(pt->mX, pt->mY)); // check that polygon is simple if (!simple.is_simple()) { std::cerr << "path is not simple (maybe self-itersecting?)" << std::endl; return false; } // ensure orientation is clockwise (must be the same for all polygons) if (simple.is_clockwise_oriented()) simple.reverse_orientation(); // collect polygons simples.push_back(simple); } // for path // check that no partly overlaps are present // deterine which polygon is contained in which one std::vector<ssize_t> contained_in; // idx: contained poly, val: containing poly ssize_t ia, ib; CgPolyVec::const_iterator a, b; for (a = simples.begin(); a != simples.end(); ++a) contained_in.push_back(-1); // not contained in any polygon for (a = simples.begin(), ia = 0; a != simples.end(); ++a, ++ia) { b = a; ib = ia; for (++b, ++ib; b != simples.end(); ++b, ++ib) { // A and B intersect each other if (CGAL::do_intersect(*a, *b)) { // compute differences A - B and B - A CgPolyHolesVec a_b, b_a; CGAL::difference(*a, *b, std::back_inserter(a_b)); CGAL::difference(*b, *a, std::back_inserter(b_a)); if (a_b.empty()) { // A and B are identical if (b_a.empty()) { std::cerr << "duplicate polygons found - meaning is undefined" << std::endl; return false; } // B contains A (A is hole in B) else { if (contained_in.at(ia) >= 0) { std::cerr << "polygon contained in multiple polygons" " - meaning is undefined" << std::endl; return false; } contained_in.at(ia) = ib; } } else { // A contains B (B is hole in A) if (b_a.empty()) { if (contained_in.at(ib) >= 0) { std::cerr << "polygon contained in multiple polygons" " - meaning is undefined" << std::endl; return false; } contained_in.at(ib) = ia; } // A and B overlap partly else { std::cerr << "polygons overlap partly - meaning is undefined" << std::endl; return false; } } } } // for b } // for a // add found polygons to member variable for (ia = 0; (size_t)ia < contained_in.size(); ++ia) { // outer polygon if (contained_in.at(ia) < 0) { CgPolyHoles poly(simples.at(ia)); // find and add holes for (ib = 0; (size_t)ib < contained_in.size(); ++ib) if (contained_in.at(ib) == ia) poly.add_hole(simples.at(ib)); // add polygon to member variable mPolys.push_back(poly); } } return true; } /** * @brief load polygons from layer * @param[in] layer layer to obtain the polygons from * @param[in] eqDist maximum distance of two points to be considered equal * @return if the layer could be converted to polygons and imported */ bool Polygons::loadLayer(const Layer &layer, double eqDist) { clear(); return addLayer(layer, eqDist); } /** * @brief create inner offset polygons * @param[in] offset offset, > 0.0 * @param[out] offsetPolys offset polygons (!= *this) */ void Polygons::createInnerOffset(double offset, Polygons &offsetPolys) const { // clear output polygons offsetPolys.mPolys.clear(); // leave if tool diameter is invalid if (offset <= 0.0) return; // process all polygons CgPolyHolesVec::const_iterator poly; for (poly = mPolys.begin(); poly != mPolys.end(); ++poly) { // create inner offset polygons CgPolyHolesPtrVec offPolys = CGAL::create_interior_skeleton_and_offset_polygons_with_holes_2( offset, *poly); // add offset polygons to output polygons CgPolyHolesPtrVec::const_iterator offPoly; for (offPoly = offPolys.begin(); offPoly != offPolys.end(); ++offPoly) offsetPolys.mPolys.push_back(**offPoly); } // for poly } /** * @brief add all polygons with holes as multiple paths to a layer * @param[in,out] layer layer to add paths to */ void Polygons::addToLayer(Layer &layer) const { CgPolyHolesVec::const_iterator poly; for (poly = mPolys.begin(); poly != mPolys.end(); ++poly) PolyHolesToLayer(*poly, layer); } /** * @brief write all polygons with holes as multiple paths to a layer * @param[in,out] layer layer to write paths to */ void Polygons::writeToLayer(Layer &layer) const { layer.mPaths.clear(); addToLayer(layer); } /** * @brief add a polygon as path to a layer * @param[in] poly polygon to add to layer * @param[in,out] layer layer to add path to */ void Polygons::PolyToLayer(const CgPoly &poly, Layer &layer) { // ignore empty polygons if (poly.vertices_begin() == poly.vertices_end()) return; // create new path layer.mPaths.push_back(Path()); Path &path = layer.mPaths.back(); // add all points CgPoly::Vertex_const_iterator v; for (v = poly.vertices_begin(); v != poly.vertices_end(); ++v) path.addPoint(v->x(), v->y()); // add first point again to close path v = poly.vertices_begin(); path.addPoint(v->x(), v->y()); } /** * @brief add a polygon with holes as multiple paths to a layer * @param[in] poly polygon with holes to add to layer * @param[in,out] layer layer to add paths to */ void Polygons::PolyHolesToLayer(const CgPolyHoles &poly, Layer &layer) { PolyToLayer(poly.outer_boundary(), layer); CgPolyHoles::Hole_const_iterator hole; for (hole = poly.holes_begin(); hole != poly.holes_end(); ++hole) PolyToLayer(*hole, layer); }