diff --git a/xs/src/ExPolygon.hpp b/xs/src/ExPolygon.hpp index 154969784a..27cea9e3ff 100644 --- a/xs/src/ExPolygon.hpp +++ b/xs/src/ExPolygon.hpp @@ -8,13 +8,10 @@ extern "C" { #include "ppport.h" } -#include "Point.hpp" +#include "Polygon.hpp" namespace Slic3r { -typedef std::vector Polygon; -typedef std::vector Polygons; - class ExPolygon { public: @@ -26,80 +23,35 @@ class ExPolygon void _rotate(double angle, Point* center); }; -#define scale_polygon(poly, factor) \ - for (Polygon::iterator pit = (poly).begin(); pit != (poly).end(); ++pit) { \ - (*pit).x *= factor; \ - (*pit).y *= factor; \ - } - -#define translate_polygon(poly, x, y) \ - for (Polygon::iterator pit = (poly).begin(); pit != (poly).end(); ++pit) { \ - (*pit).x += x; \ - (*pit).y += y; \ - } - -inline void -rotate_polygon(Polygon* poly, double angle, Point* center) -{ - for (Polygon::iterator pit = (*poly).begin(); pit != (*poly).end(); ++pit) { \ - (*pit).rotate(angle, center); - } -} +typedef std::vector ExPolygons; void ExPolygon::scale(double factor) { - scale_polygon(contour, factor); + contour.scale(factor); for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) { - scale_polygon(*it, factor); + (*it).scale(factor); } } void ExPolygon::translate(double x, double y) { - translate_polygon(contour, x, y); + contour.translate(x, y); for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) { - translate_polygon(*it, x, y); + (*it).translate(x, y); } } void ExPolygon::_rotate(double angle, Point* center) { - rotate_polygon(&contour, angle, center); + contour._rotate(angle, center); for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) { - rotate_polygon(&*it, angle, center); + (*it)._rotate(angle, center); } } -void -perl2polygon(SV* poly_sv, Polygon& poly) -{ - AV* poly_av = (AV*)SvRV(poly_sv); - const unsigned int num_points = av_len(poly_av)+1; - poly.resize(num_points); - - for (unsigned int i = 0; i < num_points; i++) { - SV** point_sv = av_fetch(poly_av, i, 0); - AV* point_av = (AV*)SvRV(*point_sv); - Point& p = poly[i]; - p.x = (unsigned long)SvIV(*av_fetch(point_av, 0, 0)); - p.y = (unsigned long)SvIV(*av_fetch(point_av, 1, 0)); - } -} - -SV* -polygon2perl(Polygon& poly) { - const unsigned int num_points = poly.size(); - AV* av = newAV(); - av_extend(av, num_points-1); - for (unsigned int i = 0; i < num_points; i++) { - av_store(av, i, point2perl(poly[i])); - } - return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::Polygon", GV_ADD)); -} - void perl2expolygon(SV* expoly_sv, ExPolygon& expoly) { diff --git a/xs/src/ExPolygonCollection.hpp b/xs/src/ExPolygonCollection.hpp index 127d5178f6..e9a4a29155 100644 --- a/xs/src/ExPolygonCollection.hpp +++ b/xs/src/ExPolygonCollection.hpp @@ -12,8 +12,6 @@ extern "C" { namespace Slic3r { -typedef std::vector ExPolygons; - class ExPolygonCollection { public: diff --git a/xs/src/Point.hpp b/xs/src/Point.hpp index c640e207a7..0225c52037 100644 --- a/xs/src/Point.hpp +++ b/xs/src/Point.hpp @@ -21,6 +21,8 @@ class Point void rotate(double angle, Point* center); }; +typedef std::vector Points; + void Point::rotate(double angle, Point* center) { diff --git a/xs/src/Polygon.hpp b/xs/src/Polygon.hpp new file mode 100644 index 0000000000..78524f5aaf --- /dev/null +++ b/xs/src/Polygon.hpp @@ -0,0 +1,81 @@ +#ifndef slic3r_Polygon_hpp_ +#define slic3r_Polygon_hpp_ + +extern "C" { +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" +#include "ppport.h" +} + +#include "Point.hpp" + +namespace Slic3r { + +class Polygon +{ + public: + Points points; + void scale(double factor); + void translate(double x, double y); + void _rotate(double angle, Point* center); +}; + +typedef std::vector Polygons; + +void +Polygon::scale(double factor) +{ + for (Points::iterator it = points.begin(); it != points.end(); ++it) { + (*it).x *= factor; + (*it).y *= factor; + } +} + +void +Polygon::translate(double x, double y) +{ + for (Points::iterator it = points.begin(); it != points.end(); ++it) { + (*it).x += x; + (*it).y += y; + } +} + +void +Polygon::_rotate(double angle, Point* center) +{ + for (Points::iterator it = points.begin(); it != points.end(); ++it) { + (*it).rotate(angle, center); + } +} + +void +perl2polygon(SV* poly_sv, Polygon& poly) +{ + AV* poly_av = (AV*)SvRV(poly_sv); + const unsigned int num_points = av_len(poly_av)+1; + poly.points.resize(num_points); + + for (unsigned int i = 0; i < num_points; i++) { + SV** point_sv = av_fetch(poly_av, i, 0); + AV* point_av = (AV*)SvRV(*point_sv); + Point& p = poly.points[i]; + p.x = (unsigned long)SvIV(*av_fetch(point_av, 0, 0)); + p.y = (unsigned long)SvIV(*av_fetch(point_av, 1, 0)); + } +} + +SV* +polygon2perl(Polygon& poly) { + const unsigned int num_points = poly.points.size(); + AV* av = newAV(); + av_extend(av, num_points-1); + for (unsigned int i = 0; i < num_points; i++) { + av_store(av, i, point2perl(poly.points[i])); + } + return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::Polygon", GV_ADD)); +} + +} + +#endif