2013-07-06 14:33:49 +00:00
|
|
|
#ifndef slic3r_ExPolygon_hpp_
|
|
|
|
#define slic3r_ExPolygon_hpp_
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "EXTERN.h"
|
|
|
|
#include "perl.h"
|
|
|
|
#include "XSUB.h"
|
|
|
|
#include "ppport.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "Point.hpp"
|
|
|
|
|
|
|
|
typedef std::vector<Point> Polygon;
|
|
|
|
typedef std::vector<Polygon> Polygons;
|
|
|
|
|
|
|
|
class ExPolygon
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Polygon contour;
|
|
|
|
Polygons holes;
|
2013-07-07 10:41:54 +00:00
|
|
|
SV* arrayref();
|
2013-07-11 12:08:11 +00:00
|
|
|
void scale(double factor);
|
2013-07-11 12:13:30 +00:00
|
|
|
void translate(double x, double y);
|
2013-07-06 14:33:49 +00:00
|
|
|
};
|
|
|
|
|
2013-07-11 12:08:11 +00:00
|
|
|
#define scale_polygon(poly, factor) \
|
|
|
|
for (Polygon::iterator pit = (poly).begin(); pit != (poly).end(); ++pit) { \
|
|
|
|
(*pit).x *= factor; \
|
|
|
|
(*pit).y *= factor; \
|
|
|
|
}
|
|
|
|
|
2013-07-11 12:13:30 +00:00
|
|
|
#define translate_polygon(poly, x, y) \
|
|
|
|
for (Polygon::iterator pit = (poly).begin(); pit != (poly).end(); ++pit) { \
|
|
|
|
(*pit).x += x; \
|
|
|
|
(*pit).y += y; \
|
|
|
|
}
|
|
|
|
|
2013-07-11 12:08:11 +00:00
|
|
|
void
|
|
|
|
ExPolygon::scale(double factor)
|
|
|
|
{
|
|
|
|
scale_polygon(contour, factor);
|
|
|
|
for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) {
|
|
|
|
scale_polygon(*it, factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 12:13:30 +00:00
|
|
|
void
|
|
|
|
ExPolygon::translate(double x, double y)
|
|
|
|
{
|
|
|
|
translate_polygon(contour, x, y);
|
|
|
|
for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) {
|
|
|
|
translate_polygon(*it, x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-07 14:51:02 +00:00
|
|
|
void
|
|
|
|
perl2polygon(SV* poly_sv, Polygon& poly)
|
2013-07-06 14:33:49 +00:00
|
|
|
{
|
|
|
|
AV* poly_av = (AV*)SvRV(poly_sv);
|
|
|
|
const unsigned int num_points = av_len(poly_av)+1;
|
2013-07-07 14:51:02 +00:00
|
|
|
poly.resize(num_points);
|
2013-07-06 14:33:49 +00:00
|
|
|
|
|
|
|
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);
|
2013-07-07 14:51:02 +00:00
|
|
|
Point& p = poly[i];
|
2013-07-06 14:33:49 +00:00
|
|
|
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++) {
|
2013-07-06 14:39:22 +00:00
|
|
|
av_store(av, i, point2perl(poly[i]));
|
2013-07-06 14:33:49 +00:00
|
|
|
}
|
2013-07-07 11:34:55 +00:00
|
|
|
return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::Polygon", GV_ADD));
|
2013-07-06 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|