Slic3r/xs/src/ExPolygon.hpp

85 lines
1.9 KiB
C++
Raw Normal View History

2013-07-06 18:33:49 +04:00
#ifndef slic3r_ExPolygon_hpp_
#define slic3r_ExPolygon_hpp_
extern "C" {
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
}
2013-07-14 17:53:53 +04:00
#include "Polygon.hpp"
2013-07-06 18:33:49 +04:00
namespace Slic3r {
2013-07-06 18:33:49 +04:00
class ExPolygon
{
public:
Polygon contour;
Polygons holes;
2013-07-07 14:41:54 +04:00
SV* arrayref();
2013-07-11 16:08:11 +04:00
void scale(double factor);
void translate(double x, double y);
void rotate(double angle, Point* center);
2013-07-06 18:33:49 +04:00
};
2013-07-14 17:53:53 +04:00
typedef std::vector<ExPolygon> ExPolygons;
2013-07-11 20:55:51 +04:00
2013-07-11 16:08:11 +04:00
void
ExPolygon::scale(double factor)
{
2013-07-14 17:53:53 +04:00
contour.scale(factor);
2013-07-11 16:08:11 +04:00
for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) {
2013-07-14 17:53:53 +04:00
(*it).scale(factor);
2013-07-11 16:08:11 +04:00
}
}
void
ExPolygon::translate(double x, double y)
{
2013-07-14 17:53:53 +04:00
contour.translate(x, y);
for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) {
2013-07-14 17:53:53 +04:00
(*it).translate(x, y);
}
}
2013-07-11 20:55:51 +04:00
void
ExPolygon::rotate(double angle, Point* center)
2013-07-11 20:55:51 +04:00
{
contour.rotate(angle, center);
2013-07-11 20:55:51 +04:00
for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) {
(*it).rotate(angle, center);
2013-07-06 18:33:49 +04:00
}
}
void
perl2expolygon(SV* expoly_sv, ExPolygon& expoly)
{
AV* expoly_av = (AV*)SvRV(expoly_sv);
const unsigned int num_polygons = av_len(expoly_av)+1;
expoly.holes.resize(num_polygons-1);
SV** polygon_sv = av_fetch(expoly_av, 0, 0);
perl2polygon(*polygon_sv, expoly.contour);
for (unsigned int i = 0; i < num_polygons-1; i++) {
polygon_sv = av_fetch(expoly_av, i+1, 0);
perl2polygon(*polygon_sv, expoly.holes[i]);
}
}
SV*
expolygon2perl(ExPolygon& expoly) {
const unsigned int num_holes = expoly.holes.size();
AV* av = newAV();
av_extend(av, num_holes); // -1 +1
av_store(av, 0, polygon2perl(expoly.contour));
for (unsigned int i = 0; i < num_holes; i++) {
av_store(av, i+1, polygon2perl(expoly.holes[i]));
}
return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::ExPolygon", GV_ADD));
}
}
2013-07-06 18:33:49 +04:00
#endif