From 6e22a82e7d9b3df33c93a506f6404e67b4563b3a Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 13 Sep 2013 14:48:40 +0200 Subject: [PATCH] Conditional compilation to exclude all Perl/XS stuff from C++ code --- xs/Build.PL | 2 +- xs/src/ClipperUtils.cpp | 2 + xs/src/ClipperUtils.hpp | 3 +- xs/src/ExPolygon.cpp | 2 + xs/src/ExPolygon.hpp | 13 ++++--- xs/src/Line.cpp | 2 + xs/src/Line.hpp | 15 +++++--- xs/src/MultiPoint.hpp | 11 ++++-- xs/src/Point.cpp | 2 + xs/src/Point.hpp | 13 ++++--- xs/src/Polygon.cpp | 30 ++++++++------- xs/src/Polygon.hpp | 7 +++- xs/src/Polyline.cpp | 2 + xs/src/Polyline.hpp | 3 ++ xs/src/Surface.cpp | 16 ++++---- xs/src/Surface.hpp | 5 ++- xs/src/TriangleMesh.cpp | 81 +++++++++++++++++++++-------------------- xs/src/TriangleMesh.hpp | 7 +++- xs/src/myinit.h | 2 + xs/src/utils.cpp | 2 + 20 files changed, 133 insertions(+), 87 deletions(-) diff --git a/xs/Build.PL b/xs/Build.PL index edc66fd7..b3586231 100644 --- a/xs/Build.PL +++ b/xs/Build.PL @@ -24,7 +24,7 @@ my $build = Module::Build::WithXSpp->new( # _GLIBCXX_USE_C99 : to get the long long type for g++ # HAS_BOOL : stops Perl/lib/CORE/handy.h from doing "# define bool char" for MSVC # NOGDI : prevents inclusion of wingdi.h which defines functions Polygon() and Polyline() in global namespace - extra_compiler_flags => [qw(-D_GLIBCXX_USE_C99 -DHAS_BOOL -DNOGDI), ($ENV{SLIC3R_DEBUG} ? ' -DSLIC3R_DEBUG -g' : '')], + extra_compiler_flags => [qw(-D_GLIBCXX_USE_C99 -DHAS_BOOL -DNOGDI -DSLIC3RXS), ($ENV{SLIC3R_DEBUG} ? ' -DSLIC3R_DEBUG -g' : '')], # Provides extra C typemaps that are auto-merged extra_typemap_modules => { diff --git a/xs/src/ClipperUtils.cpp b/xs/src/ClipperUtils.cpp index 14546ace..3764988c 100644 --- a/xs/src/ClipperUtils.cpp +++ b/xs/src/ClipperUtils.cpp @@ -320,6 +320,7 @@ void safety_offset(ClipperLib::Polygons* &subject) /////////////////////// +#ifdef SLIC3RXS SV* polynode_children_2_perl(const ClipperLib::PolyNode& node) { @@ -346,5 +347,6 @@ polynode2perl(const ClipperLib::PolyNode& node) (void)hv_stores( hv, "children", polynode_children_2_perl(node) ); return (SV*)newRV_noinc((SV*)hv); } +#endif } diff --git a/xs/src/ClipperUtils.hpp b/xs/src/ClipperUtils.hpp index d49e526d..8f0974d7 100644 --- a/xs/src/ClipperUtils.hpp +++ b/xs/src/ClipperUtils.hpp @@ -77,9 +77,10 @@ void safety_offset(ClipperLib::Polygons* &subject); ///////////////// +#ifdef SLIC3RXS SV* polynode_children_2_perl(const ClipperLib::PolyNode& node); SV* polynode2perl(const ClipperLib::PolyNode& node); - +#endif } diff --git a/xs/src/ExPolygon.cpp b/xs/src/ExPolygon.cpp index c9002344..e17db53b 100644 --- a/xs/src/ExPolygon.cpp +++ b/xs/src/ExPolygon.cpp @@ -50,6 +50,7 @@ ExPolygon::is_valid() const return true; } +#ifdef SLIC3RXS SV* ExPolygon::to_AV() { const unsigned int num_holes = this->holes.size(); @@ -118,5 +119,6 @@ ExPolygon::from_SV_check(SV* expoly_sv) this->from_SV(expoly_sv); } } +#endif } diff --git a/xs/src/ExPolygon.hpp b/xs/src/ExPolygon.hpp index 31b1c304..05eb64d9 100644 --- a/xs/src/ExPolygon.hpp +++ b/xs/src/ExPolygon.hpp @@ -11,17 +11,20 @@ class ExPolygon public: Polygon contour; Polygons holes; + void scale(double factor); + void translate(double x, double y); + void rotate(double angle, Point* center); + double area() const; + bool is_valid() const; + + #ifdef SLIC3RXS void from_SV(SV* poly_sv); void from_SV_check(SV* poly_sv); SV* to_AV(); SV* to_SV_ref(); SV* to_SV_clone_ref() const; SV* to_SV_pureperl() const; - void scale(double factor); - void translate(double x, double y); - void rotate(double angle, Point* center); - double area() const; - bool is_valid() const; + #endif }; typedef std::vector ExPolygons; diff --git a/xs/src/Line.cpp b/xs/src/Line.cpp index 05775c75..42a20132 100644 --- a/xs/src/Line.cpp +++ b/xs/src/Line.cpp @@ -42,6 +42,7 @@ Line::midpoint() const return new Point ((this->a.x + this->b.x) / 2.0, (this->a.y + this->b.y) / 2.0); } +#ifdef SLIC3RXS void Line::from_SV(SV* line_sv) { @@ -98,5 +99,6 @@ Line::to_SV_pureperl() const { av_store(av, 1, this->b.to_SV_pureperl()); return newRV_noinc((SV*)av); } +#endif } diff --git a/xs/src/Line.hpp b/xs/src/Line.hpp index 7015f7c8..d6675e5a 100644 --- a/xs/src/Line.hpp +++ b/xs/src/Line.hpp @@ -13,18 +13,21 @@ class Line Point b; Line() {}; explicit Line(Point _a, Point _b): a(_a), b(_b) {}; - void from_SV(SV* line_sv); - void from_SV_check(SV* line_sv); - SV* to_AV(); - SV* to_SV_ref(); - SV* to_SV_clone_ref() const; - SV* to_SV_pureperl() const; void scale(double factor); void translate(double x, double y); void rotate(double angle, Point* center); void reverse(); double length() const; Point* midpoint() const; + + #ifdef SLIC3RXS + void from_SV(SV* line_sv); + void from_SV_check(SV* line_sv); + SV* to_AV(); + SV* to_SV_ref(); + SV* to_SV_clone_ref() const; + SV* to_SV_pureperl() const; + #endif }; typedef std::vector Lines; diff --git a/xs/src/MultiPoint.hpp b/xs/src/MultiPoint.hpp index 85fc5bad..0c20eb35 100644 --- a/xs/src/MultiPoint.hpp +++ b/xs/src/MultiPoint.hpp @@ -11,16 +11,19 @@ class MultiPoint { public: Points points; - void from_SV(SV* poly_sv); - void from_SV_check(SV* poly_sv); - SV* to_AV(); - SV* to_SV_pureperl() const; void scale(double factor); void translate(double x, double y); void rotate(double angle, Point* center); void reverse(); Point* first_point() const; virtual Point* last_point() const = 0; + + #ifdef SLIC3RXS + void from_SV(SV* poly_sv); + void from_SV_check(SV* poly_sv); + SV* to_AV(); + SV* to_SV_pureperl() const; + #endif }; } diff --git a/xs/src/Point.cpp b/xs/src/Point.cpp index f496c70b..56622d87 100644 --- a/xs/src/Point.cpp +++ b/xs/src/Point.cpp @@ -72,6 +72,7 @@ Point::distance_to(const Point* point) const return sqrt(dx*dx + dy*dy); } +#ifdef SLIC3RXS SV* Point::to_SV_ref() { SV* sv = newSV(0); @@ -112,5 +113,6 @@ Point::from_SV_check(SV* point_sv) this->from_SV(point_sv); } } +#endif } diff --git a/xs/src/Point.hpp b/xs/src/Point.hpp index ea04d8a3..01d6112e 100644 --- a/xs/src/Point.hpp +++ b/xs/src/Point.hpp @@ -16,11 +16,6 @@ class Point long x; long y; explicit Point(long _x = 0, long _y = 0): x(_x), y(_y) {}; - void from_SV(SV* point_sv); - void from_SV_check(SV* point_sv); - SV* to_SV_ref(); - SV* to_SV_clone_ref() const; - SV* to_SV_pureperl() const; void scale(double factor); void translate(double x, double y); void rotate(double angle, Point* center); @@ -28,6 +23,14 @@ class Point int nearest_point_index(const Points points) const; Point* nearest_point(Points points) const; double distance_to(const Point* point) const; + + #ifdef SLIC3RXS + void from_SV(SV* point_sv); + void from_SV_check(SV* point_sv); + SV* to_SV_ref(); + SV* to_SV_clone_ref() const; + SV* to_SV_pureperl() const; + #endif }; } diff --git a/xs/src/Polygon.cpp b/xs/src/Polygon.cpp index 070b1e10..9ea5c910 100644 --- a/xs/src/Polygon.cpp +++ b/xs/src/Polygon.cpp @@ -11,20 +11,6 @@ Polygon::last_point() const return new Point(this->points.front()); // last point == first point for polygons } -SV* -Polygon::to_SV_ref() { - SV* sv = newSV(0); - sv_setref_pv( sv, "Slic3r::Polygon::Ref", (void*)this ); - return sv; -} - -SV* -Polygon::to_SV_clone_ref() const { - SV* sv = newSV(0); - sv_setref_pv( sv, "Slic3r::Polygon", new Polygon(*this) ); - return sv; -} - Lines Polygon::lines() const { @@ -117,4 +103,20 @@ Polygon::is_valid() const return this->points.size() >= 3; } +#ifdef SLIC3RXS +SV* +Polygon::to_SV_ref() { + SV* sv = newSV(0); + sv_setref_pv( sv, "Slic3r::Polygon::Ref", (void*)this ); + return sv; +} + +SV* +Polygon::to_SV_clone_ref() const { + SV* sv = newSV(0); + sv_setref_pv( sv, "Slic3r::Polygon", new Polygon(*this) ); + return sv; +} +#endif + } diff --git a/xs/src/Polygon.hpp b/xs/src/Polygon.hpp index 213c01c8..3855ddc0 100644 --- a/xs/src/Polygon.hpp +++ b/xs/src/Polygon.hpp @@ -12,8 +12,6 @@ namespace Slic3r { class Polygon : public MultiPoint { public: Point* last_point() const; - SV* to_SV_ref(); - SV* to_SV_clone_ref() const; Lines lines() const; Polyline* split_at(const Point* point); Polyline* split_at_index(int index); @@ -24,6 +22,11 @@ class Polygon : public MultiPoint { bool make_counter_clockwise(); bool make_clockwise(); bool is_valid() const; + + #ifdef SLIC3RXS + SV* to_SV_ref(); + SV* to_SV_clone_ref() const; + #endif }; typedef std::vector Polygons; diff --git a/xs/src/Polyline.cpp b/xs/src/Polyline.cpp index 3be8e234..71bb9db5 100644 --- a/xs/src/Polyline.cpp +++ b/xs/src/Polyline.cpp @@ -17,6 +17,7 @@ Polyline::lines(Lines &lines) const } } +#ifdef SLIC3RXS SV* Polyline::to_SV_ref() { @@ -32,5 +33,6 @@ Polyline::to_SV_clone_ref() const sv_setref_pv( sv, "Slic3r::Polyline", new Polyline(*this) ); return sv; } +#endif } diff --git a/xs/src/Polyline.hpp b/xs/src/Polyline.hpp index f0c012bc..0658e064 100644 --- a/xs/src/Polyline.hpp +++ b/xs/src/Polyline.hpp @@ -10,8 +10,11 @@ class Polyline : public MultiPoint { public: Point* last_point() const; void lines(Lines &lines) const; + + #ifdef SLIC3RXS SV* to_SV_ref(); SV* to_SV_clone_ref() const; + #endif }; typedef std::vector Polylines; diff --git a/xs/src/Surface.cpp b/xs/src/Surface.cpp index f5b2c865..63646721 100644 --- a/xs/src/Surface.cpp +++ b/xs/src/Surface.cpp @@ -2,17 +2,19 @@ namespace Slic3r { -SV* -Surface::to_SV_ref() { - SV* sv = newSV(0); - sv_setref_pv( sv, "Slic3r::Surface::Ref", (void*)this ); - return sv; -} - double Surface::area() const { return this->expolygon.area(); } +#ifdef SLIC3RXS +SV* +Surface::to_SV_ref() { + SV* sv = newSV(0); + sv_setref_pv( sv, "Slic3r::Surface::Ref", (void*)this ); + return sv; +} +#endif + } diff --git a/xs/src/Surface.hpp b/xs/src/Surface.hpp index b4325fb8..f7c72f7f 100644 --- a/xs/src/Surface.hpp +++ b/xs/src/Surface.hpp @@ -16,8 +16,11 @@ class Surface unsigned short thickness_layers; // in layers double bridge_angle; unsigned short extra_perimeters; - SV* to_SV_ref(); double area() const; + + #ifdef SLIC3RXS + SV* to_SV_ref(); + #endif }; typedef std::vector Surfaces; diff --git a/xs/src/TriangleMesh.cpp b/xs/src/TriangleMesh.cpp index db712068..f3fd0604 100644 --- a/xs/src/TriangleMesh.cpp +++ b/xs/src/TriangleMesh.cpp @@ -44,13 +44,6 @@ TriangleMesh::~TriangleMesh() { stl_close(&this->stl); } -SV* -TriangleMesh::to_SV() { - SV* sv = newSV(0); - sv_setref_pv( sv, "Slic3r::TriangleMesh", (void*)this ); - return sv; -} - void TriangleMesh::ReadSTLFile(char* input_file) { stl_open(&stl, input_file); @@ -68,38 +61,6 @@ TriangleMesh::write_binary(char* output_file) stl_write_binary(&this->stl, output_file, ""); } -void TriangleMesh::ReadFromPerl(SV* vertices, SV* facets) -{ - stl.stats.type = inmemory; - - // count facets and allocate memory - AV* facets_av = (AV*)SvRV(facets); - stl.stats.number_of_facets = av_len(facets_av)+1; - stl.stats.original_num_facets = stl.stats.number_of_facets; - stl_allocate(&stl); - - // read geometry - AV* vertices_av = (AV*)SvRV(vertices); - for (unsigned int i = 0; i < stl.stats.number_of_facets; i++) { - AV* facet_av = (AV*)SvRV(*av_fetch(facets_av, i, 0)); - stl_facet facet; - facet.normal.x = 0; - facet.normal.y = 0; - facet.normal.z = 0; - for (unsigned int v = 0; v <= 2; v++) { - AV* vertex_av = (AV*)SvRV(*av_fetch(vertices_av, SvIV(*av_fetch(facet_av, v, 0)), 0)); - facet.vertex[v].x = SvNV(*av_fetch(vertex_av, 0, 0)); - facet.vertex[v].y = SvNV(*av_fetch(vertex_av, 1, 0)); - facet.vertex[v].z = SvNV(*av_fetch(vertex_av, 2, 0)); - } - facet.extra[0] = 0; - facet.extra[1] = 0; - - stl.facet_start[i] = facet; - } - - stl_get_size(&(this->stl)); -} void TriangleMesh::repair() { @@ -599,4 +560,46 @@ TriangleMesh::merge(const TriangleMesh* mesh) stl_get_size(&this->stl); } +#ifdef SLIC3RXS +SV* +TriangleMesh::to_SV() { + SV* sv = newSV(0); + sv_setref_pv( sv, "Slic3r::TriangleMesh", (void*)this ); + return sv; +} + +void TriangleMesh::ReadFromPerl(SV* vertices, SV* facets) +{ + stl.stats.type = inmemory; + + // count facets and allocate memory + AV* facets_av = (AV*)SvRV(facets); + stl.stats.number_of_facets = av_len(facets_av)+1; + stl.stats.original_num_facets = stl.stats.number_of_facets; + stl_allocate(&stl); + + // read geometry + AV* vertices_av = (AV*)SvRV(vertices); + for (unsigned int i = 0; i < stl.stats.number_of_facets; i++) { + AV* facet_av = (AV*)SvRV(*av_fetch(facets_av, i, 0)); + stl_facet facet; + facet.normal.x = 0; + facet.normal.y = 0; + facet.normal.z = 0; + for (unsigned int v = 0; v <= 2; v++) { + AV* vertex_av = (AV*)SvRV(*av_fetch(vertices_av, SvIV(*av_fetch(facet_av, v, 0)), 0)); + facet.vertex[v].x = SvNV(*av_fetch(vertex_av, 0, 0)); + facet.vertex[v].y = SvNV(*av_fetch(vertex_av, 1, 0)); + facet.vertex[v].z = SvNV(*av_fetch(vertex_av, 2, 0)); + } + facet.extra[0] = 0; + facet.extra[1] = 0; + + stl.facet_start[i] = facet; + } + + stl_get_size(&(this->stl)); +} +#endif + } diff --git a/xs/src/TriangleMesh.hpp b/xs/src/TriangleMesh.hpp index c8bec55e..d341fa09 100644 --- a/xs/src/TriangleMesh.hpp +++ b/xs/src/TriangleMesh.hpp @@ -18,11 +18,9 @@ class TriangleMesh TriangleMesh(); TriangleMesh(const TriangleMesh &other); ~TriangleMesh(); - SV* to_SV(); void ReadSTLFile(char* input_file); void write_ascii(char* output_file); void write_binary(char* output_file); - void ReadFromPerl(SV* vertices, SV* facets); void repair(); void WriteOBJFile(char* output_file); void scale(float factor); @@ -35,6 +33,11 @@ class TriangleMesh void merge(const TriangleMesh* mesh); stl_file stl; bool repaired; + + #ifdef SLIC3RXS + SV* to_SV(); + void ReadFromPerl(SV* vertices, SV* facets); + #endif }; enum FacetEdgeType { feNone, feTop, feBottom }; diff --git a/xs/src/myinit.h b/xs/src/myinit.h index 84175da9..5f012da8 100644 --- a/xs/src/myinit.h +++ b/xs/src/myinit.h @@ -5,6 +5,7 @@ #include #include +#ifdef SLIC3RXS extern "C" { #include "EXTERN.h" #include "perl.h" @@ -13,6 +14,7 @@ extern "C" { #undef do_open #undef do_close } +#endif #define EPSILON 1e-4 diff --git a/xs/src/utils.cpp b/xs/src/utils.cpp index 7a3b11ad..d0b1e879 100644 --- a/xs/src/utils.cpp +++ b/xs/src/utils.cpp @@ -4,6 +4,7 @@ void confess_at(const char *file, int line, const char *func, const char *pat, ...) { + #ifdef SLIC3RXS va_list args; SV *error_sv = newSVpvf("Error in function %s at %s:%d: ", func, file, line); @@ -23,4 +24,5 @@ confess_at(const char *file, int line, const char *func, call_pv("Carp::confess", G_DISCARD); FREETMPS; LEAVE; + #endif }