Use XS Point everywhere

This commit is contained in:
Alessandro Ranellucci 2013-07-15 20:31:43 +02:00
parent d0701cdcd4
commit 9af2a1c007
37 changed files with 238 additions and 303 deletions

View file

@ -9,6 +9,7 @@ extern "C" {
}
#include "Polygon.hpp"
#include <vector>
namespace Slic3r {
@ -17,7 +18,8 @@ class ExPolygon
public:
Polygon contour;
Polygons holes;
SV* arrayref();
bool in_collection;
SV* to_SV(bool pureperl = false, bool pureperl_children = false);
void scale(double factor);
void translate(double x, double y);
void rotate(double angle, Point* center);
@ -52,6 +54,25 @@ ExPolygon::rotate(double angle, Point* center)
}
}
SV*
ExPolygon::to_SV(bool pureperl, bool pureperl_children)
{
if (pureperl) {
const unsigned int num_holes = this->holes.size();
AV* av = newAV();
av_extend(av, num_holes); // -1 +1
av_store(av, 0, this->contour.to_SV(pureperl_children, pureperl_children));
for (unsigned int i = 0; i < num_holes; i++) {
av_store(av, i+1, this->holes[i].to_SV(pureperl_children, pureperl_children));
}
return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::ExPolygon", GV_ADD));
} else {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::ExPolygon::XS", this );
return sv;
}
}
void
perl2expolygon(SV* expoly_sv, ExPolygon& expoly)
{
@ -60,23 +81,23 @@ perl2expolygon(SV* expoly_sv, ExPolygon& expoly)
expoly.holes.resize(num_polygons-1);
SV** polygon_sv = av_fetch(expoly_av, 0, 0);
perl2polygon(*polygon_sv, expoly.contour);
expoly.contour.from_SV(*polygon_sv);
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]);
expoly.holes[i].from_SV(*polygon_sv);
}
}
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]));
void
perl2expolygon_check(SV* expoly_sv, ExPolygon& expoly)
{
if (sv_isobject(expoly_sv) && (SvTYPE(SvRV(expoly_sv)) == SVt_PVMG)) {
// a XS ExPolygon was supplied
expoly = *(ExPolygon *)SvIV((SV*)SvRV( expoly_sv ));
} else {
// a Perl arrayref was supplied
perl2expolygon(expoly_sv, expoly);
}
return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::ExPolygon", GV_ADD));
}
}

View file

@ -8,6 +8,7 @@ extern "C" {
#include "ppport.h"
}
#include <vector>
#include <math.h>
namespace Slic3r {
@ -17,7 +18,8 @@ class Point
public:
long x;
long y;
Point(long _x = 0, long _y = 0): x(_x), y(_y) {};
explicit Point(long _x = 0, long _y = 0): x(_x), y(_y) {};
SV* to_SV(bool pureperl = false);
void scale(double factor);
void translate(double x, double y);
void rotate(double angle, Point* center);
@ -56,11 +58,18 @@ Point::coincides_with(Point* point)
}
SV*
point2perl(Point& point) {
AV* av = newAV();
av_fill(av, 1);
av_store_point_xy(av, point.x, point.y);
return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::Point", GV_ADD));
Point::to_SV(bool pureperl) {
if (pureperl) {
AV* av = newAV();
av_fill(av, 1);
av_store(av, 0, newSViv(this->x));
av_store(av, 1, newSViv(this->y));
return newRV_noinc((SV*)av);
} else {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Point", new Point(*this) );
return sv;
}
}
void

View file

@ -8,48 +8,20 @@ extern "C" {
#include "ppport.h"
}
#include <vector>
#include "Polyline.hpp"
namespace Slic3r {
class Polygon : public Polyline {};
class Polygon : public Polyline {
protected:
char* perl_class() {
return (char*)"Slic3r::Polygon";
}
};
typedef std::vector<Polygon> Polygons;
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);
perl2point(*point_sv, poly.points[i]);
}
}
void
perl2polygon_check(SV* poly_sv, Polygon& poly)
{
if (sv_isobject(poly_sv) && (SvTYPE(SvRV(poly_sv)) == SVt_PVMG)) {
poly = *(Polygon*)SvIV((SV*)SvRV( poly_sv ));
} else {
perl2polygon(poly_sv, poly);
}
}
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

View file

@ -10,6 +10,8 @@ extern "C" {
#include "Point.hpp"
#include <algorithm>
#include <string>
#include <vector>
namespace Slic3r {
@ -17,10 +19,17 @@ class Polyline
{
public:
Points points;
void from_SV(SV* poly_sv);
void from_SV_check(SV* poly_sv);
SV* to_SV(bool pureperl = false, bool pureperl_children = false);
void scale(double factor);
void translate(double x, double y);
void rotate(double angle, Point* center);
void reverse();
protected:
virtual char* perl_class() {
return (char*)"Slic3r::Polyline";
}
};
typedef std::vector<Polyline> Polylines;
@ -56,37 +65,37 @@ Polyline::reverse()
}
void
perl2polyline(SV* poly_sv, Polyline& poly)
Polyline::from_SV(SV* poly_sv)
{
AV* poly_av = (AV*)SvRV(poly_sv);
const unsigned int num_points = av_len(poly_av)+1;
poly.points.resize(num_points);
this->points.resize(num_points);
for (unsigned int i = 0; i < num_points; i++) {
SV** point_sv = av_fetch(poly_av, i, 0);
perl2point(*point_sv, poly.points[i]);
perl2point_check(*point_sv, this->points[i]);
}
}
void
perl2polyline_check(SV* poly_sv, Polyline& poly)
Polyline::from_SV_check(SV* poly_sv)
{
if (sv_isobject(poly_sv) && (SvTYPE(SvRV(poly_sv)) == SVt_PVMG)) {
poly = *(Polyline*)SvIV((SV*)SvRV( poly_sv ));
*this = *(Polyline*)SvIV((SV*)SvRV( poly_sv ));
} else {
perl2polyline(poly_sv, poly);
this->from_SV(poly_sv);
}
}
SV*
polyline2perl(Polyline& poly) {
const unsigned int num_points = poly.points.size();
Polyline::to_SV(bool pureperl, bool pureperl_children) {
const unsigned int num_points = this->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]));
av_store(av, i, this->points[i].to_SV(pureperl_children));
}
return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::Polyline", GV_ADD));
return sv_bless(newRV_noinc((SV*)av), gv_stashpv(this->perl_class(), GV_ADD));
}
}

View file

@ -1,13 +1,7 @@
#ifndef _myinit_h_
#define _myinit_h_
#include <vector>
namespace Slic3r {}
using namespace Slic3r;
#define av_store_point_xy(AV, X, Y) \
av_store(AV, 0, newSViv(X)); \
av_store(AV, 1, newSViv(Y))
#endif