From c50ecfb7f8a42ab06a22e97cbb3cc8fe62ec5000 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 6 Jul 2013 15:26:32 +0200 Subject: [PATCH] New Slic3r::Point::XS class --- xs/lib/Slic3r/XS.pm | 3 +++ xs/src/Point.cpp | 14 ++++++++++++++ xs/src/Point.hpp | 21 +++++++++++++++++++++ xs/src/TriangleMesh.hpp | 5 ++++- xs/src/ZTable.hpp | 24 ++++++++++++++++++++++++ xs/src/myinit.h | 19 +++---------------- xs/t/03_point.t | 12 ++++++++++++ xs/xsp/Object.xsp | 1 + xs/xsp/Point.xsp | 25 +++++++++++++++++++++++++ xs/xsp/TriangleMesh.xsp | 1 + xs/xsp/my.map | 1 + 11 files changed, 109 insertions(+), 17 deletions(-) create mode 100644 xs/src/Point.cpp create mode 100644 xs/src/Point.hpp create mode 100644 xs/src/ZTable.hpp create mode 100644 xs/t/03_point.t create mode 100644 xs/xsp/Point.xsp diff --git a/xs/lib/Slic3r/XS.pm b/xs/lib/Slic3r/XS.pm index 0bdf5f31d..1b245329a 100644 --- a/xs/lib/Slic3r/XS.pm +++ b/xs/lib/Slic3r/XS.pm @@ -7,5 +7,8 @@ our $VERSION = '0.01'; use XSLoader; XSLoader::load(__PACKAGE__, $VERSION); +package Slic3r::Point::XS; +use overload + '@{}' => sub { $_[0]->_toPerl }; 1; diff --git a/xs/src/Point.cpp b/xs/src/Point.cpp new file mode 100644 index 000000000..a4c905eae --- /dev/null +++ b/xs/src/Point.cpp @@ -0,0 +1,14 @@ +#include "myinit.h" +#include "Point.hpp" + +Point::Point(unsigned long x, unsigned long y) {} +Point::~Point() {} + + +SV* +Point::_toPerl() { + AV* av = newAV(); + av_fill(av, 1); + av_store_point_xy(av, x, y); + return (SV*)newRV_noinc((SV*)av); +} diff --git a/xs/src/Point.hpp b/xs/src/Point.hpp new file mode 100644 index 000000000..3b7a5e5dd --- /dev/null +++ b/xs/src/Point.hpp @@ -0,0 +1,21 @@ +#ifndef slic3r_Point_hpp_ +#define slic3r_Point_hpp_ + +extern "C" { +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" +#include "ppport.h" +} + +class Point +{ + public: + unsigned long x; + unsigned long y; + Point(unsigned long _x, unsigned long _y): x(_x), y(_y) {}; + ~Point(); + SV* _toPerl(); +}; + +#endif diff --git a/xs/src/TriangleMesh.hpp b/xs/src/TriangleMesh.hpp index 78e62cf9a..56380ebf8 100644 --- a/xs/src/TriangleMesh.hpp +++ b/xs/src/TriangleMesh.hpp @@ -1,3 +1,6 @@ +#ifndef slic3r_TriangleMesh_hpp_ +#define slic3r_TriangleMesh_hpp_ + #include extern "C" { @@ -21,4 +24,4 @@ class TriangleMesh stl_file stl; }; - +#endif diff --git a/xs/src/ZTable.hpp b/xs/src/ZTable.hpp new file mode 100644 index 000000000..045722eec --- /dev/null +++ b/xs/src/ZTable.hpp @@ -0,0 +1,24 @@ +#ifndef slic3r_ZTable_hpp_ +#define slic3r_ZTable_hpp_ + +extern "C" { +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" +#include "ppport.h" +} + +class ZTable +{ + public: + ZTable(std::vector* z_array); + std::vector get_range(unsigned int min_z, unsigned int max_z); + std::vector z; +}; + +ZTable::ZTable(std::vector* ztable) : + z(*ztable) +{ +} + +#endif diff --git a/xs/src/myinit.h b/xs/src/myinit.h index 46e564d2e..4f534519c 100644 --- a/xs/src/myinit.h +++ b/xs/src/myinit.h @@ -3,21 +3,8 @@ #include -//////////////// -class ZTable -{ - public: - ZTable(std::vector* z_array); - std::vector get_range(unsigned int min_z, unsigned int max_z); - std::vector z; -}; - -ZTable::ZTable(std::vector* ztable) : - z(*ztable) -{ -} -//////////////// - -#include "TriangleMesh.hpp" +#define av_store_point_xy(AV, X, Y) \ + av_store(AV, 0, newSViv(X)); \ + av_store(AV, 1, newSViv(Y)) #endif diff --git a/xs/t/03_point.t b/xs/t/03_point.t new file mode 100644 index 000000000..0a876af71 --- /dev/null +++ b/xs/t/03_point.t @@ -0,0 +1,12 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Slic3r::XS; +use Test::More tests => 1; + +my $point = Slic3r::Point::XS->new(10, 15); +is_deeply [ @$point ], [10, 15], 'point roundtrip'; + +__END__ diff --git a/xs/xsp/Object.xsp b/xs/xsp/Object.xsp index d6071977b..2f8a0601e 100644 --- a/xs/xsp/Object.xsp +++ b/xs/xsp/Object.xsp @@ -2,6 +2,7 @@ %{ #include +#include "ZTable.hpp" #include %} diff --git a/xs/xsp/Point.xsp b/xs/xsp/Point.xsp new file mode 100644 index 000000000..db810068b --- /dev/null +++ b/xs/xsp/Point.xsp @@ -0,0 +1,25 @@ +%module{Slic3r::XS}; + +%{ +#include +#include "Point.hpp" +%} + +%name{Slic3r::Point::XS} class Point { + Point(unsigned long _x, unsigned long _y); + ~Point(); + SV* _toPerl(); +}; + +%package{Slic3r::Point::XS}; + +%{ +PROTOTYPES: DISABLE + +std::string +hello_world() + CODE: + RETVAL = "Hello world!"; + OUTPUT: + RETVAL +%} diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp index be0d5b25e..6952dfdbd 100644 --- a/xs/xsp/TriangleMesh.xsp +++ b/xs/xsp/TriangleMesh.xsp @@ -2,6 +2,7 @@ %{ #include +#include "TriangleMesh.hpp" %} %name{Slic3r::TriangleMesh::XS} class TriangleMesh { diff --git a/xs/xsp/my.map b/xs/xsp/my.map index 656389f00..46a311676 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -1,3 +1,4 @@ ZTable* O_OBJECT TriangleMesh* O_OBJECT +Point* O_OBJECT std::vector< unsigned int >* T_STD_VECTOR_UINT_PTR