void optimizePolygon(ClipperLib::Polygon& poly) { Point p0 = poly[poly.size()-1]; for(unsigned int i=0;i<poly.size();i++) { Point p1 = poly[i]; if (shorterThen(p0 - p1, 10)) { poly.erase(poly.begin() + i); i --; }else{ Point p2; if (i < poly.size() - 1) p2 = poly[i+1]; else p2 = poly[0]; Point diff0 = normal(p1 - p0, 1000000); Point diff2 = normal(p1 - p2, 1000000); int64_t d = dot(diff0, diff2); if (d < -999999000000LL) { poly.erase(poly.begin() + i); i --; }else{ p0 = p1; } } } }
void ClipperPolygon_to_Slic3rPolygon(const ClipperLib::Polygon &input, Slic3r::Polygon &output) { output.points.clear(); for (ClipperLib::Polygon::const_iterator pit = input.begin(); pit != input.end(); ++pit) { output.points.push_back(Slic3r::Point( (*pit).X, (*pit).Y )); } }
//-------------------------------------------------------------- ofPolyline ofxClipper::polygon_to_ofPolyline(ClipperLib::Polygon& polygon) { vector<ClipperLib::IntPoint>::iterator iter; ofPolyline polyline; for(iter = polygon.begin(); iter != polygon.end(); iter++) { ofPoint pnt((*iter).X / double(clipperGlobalScale), // bring back to our coords (*iter).Y / double(clipperGlobalScale)); // bring back to our coords polyline.addVertex(pnt); } polyline.close(); // close it return polyline; }
int main(int argc, char* argv[]) { if (argc != 3) { cout<<"Usage: offset.exe <input.poly> <output.poly>\n\n Input and output files should be different.\n Only one ring poly files are accepted now without inner rings.\n Default offset is 0.001 degree\n"; return 0; } ifstream f(argv[1], ifstream::in); ofstream outfile(argv[2], ofstream::out); outfile.precision(9); string s; for (int i=0;i<3;i++) { getline(f, s); outfile<<s<<"\n"; } ClipperLib::Polygons polygons; ClipperLib::Polygon polygon; while(!f.eof()) { f>>s; if (s == "END") { break; } double c1 = ::atof(s.c_str()); f>>s; double c2 = ::atof(s.c_str()); ClipperLib::IntPoint point(c1 * MUL, c2 * MUL); polygon.push_back(point); } polygons.push_back(polygon); double distance = 0.001 * MUL; ClipperLib::Polygons res_polygons; ClipperLib::OffsetPolygons (polygons, res_polygons, distance, ClipperLib::jtMiter); if (res_polygons.size() < 1) { cerr<<"Bad output of OffsetPolygons!\n";return 1;} ClipperLib::Polygon result = res_polygons.front(); for (ClipperLib::Polygon::iterator it = result.begin(); it != result.end(); ++it) { ClipperLib::IntPoint point = *it; outfile << " " << (point.X/MUL) << " " << (point.Y/MUL) << " \n"; } outfile<<s; while (!f.eof()) { getline(f, s); outfile<<s<<"\n"; } return 0; }