Browse Source

New Slic3r::Point::XS class

Alessandro Ranellucci 11 years ago
parent
commit
c50ecfb7f8
10 changed files with 108 additions and 17 deletions
  1. 3 0
      xs/lib/Slic3r/XS.pm
  2. 14 0
      xs/src/Point.cpp
  3. 21 0
      xs/src/Point.hpp
  4. 4 1
      xs/src/TriangleMesh.hpp
  5. 24 0
      xs/src/ZTable.hpp
  6. 3 16
      xs/src/myinit.h
  7. 12 0
      xs/t/03_point.t
  8. 1 0
      xs/xsp/Object.xsp
  9. 25 0
      xs/xsp/Point.xsp
  10. 1 0
      xs/xsp/TriangleMesh.xsp

+ 3 - 0
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;

+ 14 - 0
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);
+}

+ 21 - 0
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

+ 4 - 1
xs/src/TriangleMesh.hpp

@@ -1,3 +1,6 @@
+#ifndef slic3r_TriangleMesh_hpp_
+#define slic3r_TriangleMesh_hpp_
+
 #include <admesh/stl.h>
 
 extern "C" {
@@ -21,4 +24,4 @@ class TriangleMesh
     stl_file stl;
 };
 
-
+#endif

+ 24 - 0
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<unsigned int>* z_array);
+    std::vector<unsigned int> get_range(unsigned int min_z, unsigned int max_z);
+    std::vector<unsigned int> z;
+};
+
+ZTable::ZTable(std::vector<unsigned int>* ztable) :
+    z(*ztable)
+{
+}
+
+#endif

+ 3 - 16
xs/src/myinit.h

@@ -3,21 +3,8 @@
 
 #include <vector>
 
-////////////////
-class ZTable
-{
-    public:
-    ZTable(std::vector<unsigned int>* z_array);
-    std::vector<unsigned int> get_range(unsigned int min_z, unsigned int max_z);
-    std::vector<unsigned int> z;
-};
-
-ZTable::ZTable(std::vector<unsigned int>* 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

+ 12 - 0
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__

+ 1 - 0
xs/xsp/Object.xsp

@@ -2,6 +2,7 @@
 
 %{
 #include <myinit.h>
+#include "ZTable.hpp"
 #include <vector>
 %}
 

+ 25 - 0
xs/xsp/Point.xsp

@@ -0,0 +1,25 @@
+%module{Slic3r::XS};
+
+%{
+#include <myinit.h>
+#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
+%}

+ 1 - 0
xs/xsp/TriangleMesh.xsp

@@ -2,6 +2,7 @@
 
 %{
 #include <myinit.h>
+#include "TriangleMesh.hpp"
 %}
 
 %name{Slic3r::TriangleMesh::XS} class TriangleMesh {

Some files were not shown because too many files changed in this diff