New Slic3r::ExPolygon::XS class

This commit is contained in:
Alessandro Ranellucci 2013-07-06 16:33:49 +02:00
parent c2d63bcd09
commit 5a11d4df89
9 changed files with 138 additions and 3 deletions

View File

@ -18,6 +18,7 @@ src/admesh/stlinit.c
src/admesh/stlinit.o
src/admesh/util.c
src/admesh/util.o
src/ExPolygon.hpp
src/myinit.h
src/Point.cpp
src/Point.hpp
@ -30,6 +31,8 @@ src/ZTable.hpp
t/01_trianglemesh.t
t/02_object.t
t/03_point.t
t/04_expolygon.t
xsp/ExPolygon.xsp
xsp/my.map
xsp/mytype.map
xsp/Object.xsp

View File

@ -11,4 +11,8 @@ package Slic3r::Point::XS;
use overload
'@{}' => sub { $_[0]->_toPerl };
package Slic3r::ExPolygon::XS;
use overload
'@{}' => sub { $_[0]->_toPerl };
1;

52
xs/src/ExPolygon.hpp Normal file
View File

@ -0,0 +1,52 @@
#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;
SV* _toPerl();
};
Polygon*
perl2polygon(SV* poly_sv)
{
AV* poly_av = (AV*)SvRV(poly_sv);
const unsigned int num_points = av_len(poly_av)+1;
Polygon* retval = new Polygon(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 = (*retval)[i];
p.x = (unsigned long)SvIV(*av_fetch(point_av, 0, 0));
p.y = (unsigned long)SvIV(*av_fetch(point_av, 1, 0));
}
return retval;
}
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, poly[i]._toPerl());
}
return (SV*)newRV_noinc((SV*)av);
}
#endif

View File

@ -1,7 +1,6 @@
#include "myinit.h"
#include "Point.hpp"
Point::Point(unsigned long x, unsigned long y) {}
Point::~Point() {}

View File

@ -13,7 +13,7 @@ class Point
public:
unsigned long x;
unsigned long y;
Point(unsigned long _x, unsigned long _y): x(_x), y(_y) {};
Point(unsigned long _x = 0, unsigned long _y = 0): x(_x), y(_y) {};
~Point();
SV* _toPerl();
};

25
xs/t/04_expolygon.t Normal file
View File

@ -0,0 +1,25 @@
#!/usr/bin/perl
use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 1;
my $square = [ # ccw
[100, 100],
[200, 100],
[200, 200],
[100, 200],
];
my $hole_in_square = [ # cw
[140, 140],
[140, 160],
[160, 160],
[160, 140],
];
my $expolygon = Slic3r::ExPolygon::XS->new($square, $hole_in_square);
is_deeply [ @$expolygon ], [$square, $hole_in_square], 'expolygon roundtrip';
__END__

51
xs/xsp/ExPolygon.xsp Normal file
View File

@ -0,0 +1,51 @@
%module{Slic3r::XS};
%{
#include <myinit.h>
#include "ExPolygon.hpp"
%}
%name{Slic3r::ExPolygon::XS} class ExPolygon {
%{
ExPolygon*
ExPolygon::new(...)
CODE:
RETVAL = new ExPolygon ();
// ST(0) is class name, ST(1) is contour and others are holes
RETVAL->contour = *perl2polygon(ST(1));
for (unsigned int i = 2; i < items; i++) {
RETVAL->holes.push_back(*perl2polygon(ST(i)));
}
OUTPUT:
RETVAL
SV*
ExPolygon::_toPerl()
CODE:
const unsigned int num_holes = THIS->holes.size();
AV* av = newAV();
av_fill(av, num_holes); // -1 +1
av_store(av, 0, polygon2perl(THIS->contour));
for (unsigned int i = 0; i < num_holes; i++) {
av_store(av, i+1, polygon2perl(THIS->holes[i]));
}
RETVAL = (SV*)newRV_noinc((SV*)av);
OUTPUT:
RETVAL
%}
};
%package{Slic3r::ExPolygon::XS};
%{
PROTOTYPES: DISABLE
std::string
hello_world()
CODE:
RETVAL = "Hello world!";
OUTPUT:
RETVAL
%}

View File

@ -6,7 +6,7 @@
%}
%name{Slic3r::Point::XS} class Point {
Point(unsigned long _x, unsigned long _y);
Point(unsigned long _x = 0, unsigned long _y = 0);
~Point();
SV* _toPerl();
};

View File

@ -1,4 +1,5 @@
ZTable* O_OBJECT
TriangleMesh* O_OBJECT
Point* O_OBJECT
ExPolygon* O_OBJECT
std::vector< unsigned int >* T_STD_VECTOR_UINT_PTR