Polygon_2 remove_collinear(const Polygon_2& p, Number_type epsilon) { static log4cplus::Logger logger = log4cplus::Logger::getInstance("polygon_utils.remove_collinear"); LOG4CPLUS_TRACE(logger, pp(p)); if (!p.is_simple()) { stringstream ss; ss << "Polygon is not simple in remove_collinear: " << pp(p); throw logic_error(ss.str()); } Polygon_2::Vertex_circulator start = p.vertices_circulator(); Polygon_2::Vertex_circulator c = start; Polygon_2::Vertex_circulator n = c; Polygon_2::Vertex_circulator prev = c; ++n; --prev; Polygon_2 newp; do { Triangle_2 t(*prev, *c, *n); Number_type a = abs(t.area()); if (a > epsilon) // if (!CGAL::collinear(*prev, *c, *n)) newp.push_back(*c); else LOG4CPLUS_TRACE(logger, "Removing collinearity at " << pp(*c) << " area = " << a); ++prev; ++c; ++n; } while (c != start); return newp; }
Polygon_set_2 join(Array polygons){ vector<Polygon_2> polygon_vector; for (unsigned int i = 0; i < polygons.size(); i++) { Polygon_2 polygon = from_ruby<Polygon_2>(polygons[i]); if (polygon.is_simple()) { polygon_vector.push_back(polygon); } } Polygon_set_2 S; S.join(polygon_vector.begin(), polygon_vector.end()); return S; }
static void CollectEdges(Block_2& io_dst, edge_collector_t<Block_2> * collector, const Polygon_2& src, Block_locator * loc) { DebugAssert(src.size() >= 3); DebugAssert(src.is_simple()); for(int n = 0; n < src.size(); ++n) { collector->input = Pmwx::X_monotone_curve_2(src.edge(n),0); collector->ctr = 0; DebugAssert(collector->input.source() != collector->input.target()); if(loc) CGAL::insert(io_dst, collector->input,*loc); else CGAL::insert(io_dst, collector->input); DebugAssert(collector->ctr > 0); } }
void build_skeleton(const char* fname) { typedef typename Kernel::Point_2 Point_2; typedef CGAL::Polygon_2<Kernel> Polygon_2; typedef CGAL::Straight_skeleton_builder_traits_2<Kernel> SsBuilderTraits; typedef CGAL::Straight_skeleton_2<Kernel> Ss; typedef CGAL::Straight_skeleton_builder_2<SsBuilderTraits,Ss> SsBuilder; Polygon_2 pgn; std::ifstream input(fname); FT x,y; while(input) { input >> x; if (!input) break; input >> y; if (!input) break; pgn.push_back( Point_2( typename Kernel::FT(x), typename Kernel::FT(y) ) ); } input.close(); std::cout << "Polygon has " << pgn.size() << " points\n"; if(!pgn.is_counterclockwise_oriented()) { std::cerr << "Polygon is not CCW Oriented" << std::endl; } if(!pgn.is_simple()) { std::cerr << "Polygon is not simple" << std::endl; } CGAL::Timer time; time.start(); SsBuilder ssb; ssb.enter_contour(pgn.vertices_begin(), pgn.vertices_end()); boost::shared_ptr<Ss> straight_ske = ssb.construct_skeleton(); time.stop(); std::cout << "Time spent to build skeleton " << time.time() << "\n"; if(!straight_ske->is_valid()) { std::cerr << "Straight skeleton is not valid" << std::endl; } std::cerr.precision(60); print_straight_skeleton(*straight_ske); }
void OffsetWorker::offsetPolygon(Polygon_2 poly, double offset) { typedef CGAL::Gps_circle_segment_traits_2<SFCGAL::Kernel> Gps_traits_2; typedef Gps_traits_2::Polygon_2 Offset_polygon_2; if(!poly.is_simple()) { DM::Logger(DM::Warning) << "Can't perform offset polygon is not simple"; } CGAL::Orientation orient = poly.orientation(); if (orient == CGAL::CLOCKWISE) { poly.reverse_orientation(); } const double err_bound = 0.00001; std::list<Offset_polygon_2> inset_polygons; CGAL::approximated_inset_2 (poly, offset, err_bound, std::back_inserter (inset_polygons)); foreach (Offset_polygon_2 p, inset_polygons) { SFCGAL::Polygon po(this->approximate(p)); QString wkt = QString(po.asText(9).c_str()); module->addToSystem(wkt); //emit resultPolygon(wkt); }