Conditional compilation to exclude all Perl/XS stuff from C++ code

This commit is contained in:
Alessandro Ranellucci 2013-09-13 14:48:40 +02:00
parent e2cb40766b
commit 6e22a82e7d
20 changed files with 133 additions and 87 deletions

View file

@ -24,7 +24,7 @@ my $build = Module::Build::WithXSpp->new(
# _GLIBCXX_USE_C99 : to get the long long type for g++ # _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 # 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 # 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 # Provides extra C typemaps that are auto-merged
extra_typemap_modules => { extra_typemap_modules => {

View file

@ -320,6 +320,7 @@ void safety_offset(ClipperLib::Polygons* &subject)
/////////////////////// ///////////////////////
#ifdef SLIC3RXS
SV* SV*
polynode_children_2_perl(const ClipperLib::PolyNode& node) 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) ); (void)hv_stores( hv, "children", polynode_children_2_perl(node) );
return (SV*)newRV_noinc((SV*)hv); return (SV*)newRV_noinc((SV*)hv);
} }
#endif
} }

View file

@ -77,9 +77,10 @@ void safety_offset(ClipperLib::Polygons* &subject);
///////////////// /////////////////
#ifdef SLIC3RXS
SV* polynode_children_2_perl(const ClipperLib::PolyNode& node); SV* polynode_children_2_perl(const ClipperLib::PolyNode& node);
SV* polynode2perl(const ClipperLib::PolyNode& node); SV* polynode2perl(const ClipperLib::PolyNode& node);
#endif
} }

View file

@ -50,6 +50,7 @@ ExPolygon::is_valid() const
return true; return true;
} }
#ifdef SLIC3RXS
SV* SV*
ExPolygon::to_AV() { ExPolygon::to_AV() {
const unsigned int num_holes = this->holes.size(); const unsigned int num_holes = this->holes.size();
@ -118,5 +119,6 @@ ExPolygon::from_SV_check(SV* expoly_sv)
this->from_SV(expoly_sv); this->from_SV(expoly_sv);
} }
} }
#endif
} }

View file

@ -11,17 +11,20 @@ class ExPolygon
public: public:
Polygon contour; Polygon contour;
Polygons holes; 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(SV* poly_sv);
void from_SV_check(SV* poly_sv); void from_SV_check(SV* poly_sv);
SV* to_AV(); SV* to_AV();
SV* to_SV_ref(); SV* to_SV_ref();
SV* to_SV_clone_ref() const; SV* to_SV_clone_ref() const;
SV* to_SV_pureperl() const; SV* to_SV_pureperl() const;
void scale(double factor); #endif
void translate(double x, double y);
void rotate(double angle, Point* center);
double area() const;
bool is_valid() const;
}; };
typedef std::vector<ExPolygon> ExPolygons; typedef std::vector<ExPolygon> ExPolygons;

View file

@ -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); return new Point ((this->a.x + this->b.x) / 2.0, (this->a.y + this->b.y) / 2.0);
} }
#ifdef SLIC3RXS
void void
Line::from_SV(SV* line_sv) Line::from_SV(SV* line_sv)
{ {
@ -98,5 +99,6 @@ Line::to_SV_pureperl() const {
av_store(av, 1, this->b.to_SV_pureperl()); av_store(av, 1, this->b.to_SV_pureperl());
return newRV_noinc((SV*)av); return newRV_noinc((SV*)av);
} }
#endif
} }

View file

@ -13,18 +13,21 @@ class Line
Point b; Point b;
Line() {}; Line() {};
explicit Line(Point _a, Point _b): a(_a), b(_b) {}; 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 scale(double factor);
void translate(double x, double y); void translate(double x, double y);
void rotate(double angle, Point* center); void rotate(double angle, Point* center);
void reverse(); void reverse();
double length() const; double length() const;
Point* midpoint() 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<Line> Lines; typedef std::vector<Line> Lines;

View file

@ -11,16 +11,19 @@ class MultiPoint
{ {
public: public:
Points points; 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 scale(double factor);
void translate(double x, double y); void translate(double x, double y);
void rotate(double angle, Point* center); void rotate(double angle, Point* center);
void reverse(); void reverse();
Point* first_point() const; Point* first_point() const;
virtual Point* last_point() const = 0; 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
}; };
} }

View file

@ -72,6 +72,7 @@ Point::distance_to(const Point* point) const
return sqrt(dx*dx + dy*dy); return sqrt(dx*dx + dy*dy);
} }
#ifdef SLIC3RXS
SV* SV*
Point::to_SV_ref() { Point::to_SV_ref() {
SV* sv = newSV(0); SV* sv = newSV(0);
@ -112,5 +113,6 @@ Point::from_SV_check(SV* point_sv)
this->from_SV(point_sv); this->from_SV(point_sv);
} }
} }
#endif
} }

View file

@ -16,11 +16,6 @@ class Point
long x; long x;
long y; long y;
explicit Point(long _x = 0, long _y = 0): x(_x), y(_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 scale(double factor);
void translate(double x, double y); void translate(double x, double y);
void rotate(double angle, Point* center); void rotate(double angle, Point* center);
@ -28,6 +23,14 @@ class Point
int nearest_point_index(const Points points) const; int nearest_point_index(const Points points) const;
Point* nearest_point(Points points) const; Point* nearest_point(Points points) const;
double distance_to(const Point* point) 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
}; };
} }

View file

@ -11,20 +11,6 @@ Polygon::last_point() const
return new Point(this->points.front()); // last point == first point for polygons 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 Lines
Polygon::lines() const Polygon::lines() const
{ {
@ -117,4 +103,20 @@ Polygon::is_valid() const
return this->points.size() >= 3; 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
} }

View file

@ -12,8 +12,6 @@ namespace Slic3r {
class Polygon : public MultiPoint { class Polygon : public MultiPoint {
public: public:
Point* last_point() const; Point* last_point() const;
SV* to_SV_ref();
SV* to_SV_clone_ref() const;
Lines lines() const; Lines lines() const;
Polyline* split_at(const Point* point); Polyline* split_at(const Point* point);
Polyline* split_at_index(int index); Polyline* split_at_index(int index);
@ -24,6 +22,11 @@ class Polygon : public MultiPoint {
bool make_counter_clockwise(); bool make_counter_clockwise();
bool make_clockwise(); bool make_clockwise();
bool is_valid() const; bool is_valid() const;
#ifdef SLIC3RXS
SV* to_SV_ref();
SV* to_SV_clone_ref() const;
#endif
}; };
typedef std::vector<Polygon> Polygons; typedef std::vector<Polygon> Polygons;

View file

@ -17,6 +17,7 @@ Polyline::lines(Lines &lines) const
} }
} }
#ifdef SLIC3RXS
SV* SV*
Polyline::to_SV_ref() Polyline::to_SV_ref()
{ {
@ -32,5 +33,6 @@ Polyline::to_SV_clone_ref() const
sv_setref_pv( sv, "Slic3r::Polyline", new Polyline(*this) ); sv_setref_pv( sv, "Slic3r::Polyline", new Polyline(*this) );
return sv; return sv;
} }
#endif
} }

View file

@ -10,8 +10,11 @@ class Polyline : public MultiPoint {
public: public:
Point* last_point() const; Point* last_point() const;
void lines(Lines &lines) const; void lines(Lines &lines) const;
#ifdef SLIC3RXS
SV* to_SV_ref(); SV* to_SV_ref();
SV* to_SV_clone_ref() const; SV* to_SV_clone_ref() const;
#endif
}; };
typedef std::vector<Polyline> Polylines; typedef std::vector<Polyline> Polylines;

View file

@ -2,17 +2,19 @@
namespace Slic3r { namespace Slic3r {
SV*
Surface::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Surface::Ref", (void*)this );
return sv;
}
double double
Surface::area() const Surface::area() const
{ {
return this->expolygon.area(); 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
} }

View file

@ -16,8 +16,11 @@ class Surface
unsigned short thickness_layers; // in layers unsigned short thickness_layers; // in layers
double bridge_angle; double bridge_angle;
unsigned short extra_perimeters; unsigned short extra_perimeters;
SV* to_SV_ref();
double area() const; double area() const;
#ifdef SLIC3RXS
SV* to_SV_ref();
#endif
}; };
typedef std::vector<Surface> Surfaces; typedef std::vector<Surface> Surfaces;

View file

@ -44,13 +44,6 @@ TriangleMesh::~TriangleMesh() {
stl_close(&this->stl); stl_close(&this->stl);
} }
SV*
TriangleMesh::to_SV() {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::TriangleMesh", (void*)this );
return sv;
}
void void
TriangleMesh::ReadSTLFile(char* input_file) { TriangleMesh::ReadSTLFile(char* input_file) {
stl_open(&stl, input_file); stl_open(&stl, input_file);
@ -68,38 +61,6 @@ TriangleMesh::write_binary(char* output_file)
stl_write_binary(&this->stl, 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 void
TriangleMesh::repair() { TriangleMesh::repair() {
@ -599,4 +560,46 @@ TriangleMesh::merge(const TriangleMesh* mesh)
stl_get_size(&this->stl); 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
} }

View file

@ -18,11 +18,9 @@ class TriangleMesh
TriangleMesh(); TriangleMesh();
TriangleMesh(const TriangleMesh &other); TriangleMesh(const TriangleMesh &other);
~TriangleMesh(); ~TriangleMesh();
SV* to_SV();
void ReadSTLFile(char* input_file); void ReadSTLFile(char* input_file);
void write_ascii(char* output_file); void write_ascii(char* output_file);
void write_binary(char* output_file); void write_binary(char* output_file);
void ReadFromPerl(SV* vertices, SV* facets);
void repair(); void repair();
void WriteOBJFile(char* output_file); void WriteOBJFile(char* output_file);
void scale(float factor); void scale(float factor);
@ -35,6 +33,11 @@ class TriangleMesh
void merge(const TriangleMesh* mesh); void merge(const TriangleMesh* mesh);
stl_file stl; stl_file stl;
bool repaired; bool repaired;
#ifdef SLIC3RXS
SV* to_SV();
void ReadFromPerl(SV* vertices, SV* facets);
#endif
}; };
enum FacetEdgeType { feNone, feTop, feBottom }; enum FacetEdgeType { feNone, feTop, feBottom };

View file

@ -5,6 +5,7 @@
#include <ostream> #include <ostream>
#include <iostream> #include <iostream>
#ifdef SLIC3RXS
extern "C" { extern "C" {
#include "EXTERN.h" #include "EXTERN.h"
#include "perl.h" #include "perl.h"
@ -13,6 +14,7 @@ extern "C" {
#undef do_open #undef do_open
#undef do_close #undef do_close
} }
#endif
#define EPSILON 1e-4 #define EPSILON 1e-4

View file

@ -4,6 +4,7 @@ void
confess_at(const char *file, int line, const char *func, confess_at(const char *file, int line, const char *func,
const char *pat, ...) const char *pat, ...)
{ {
#ifdef SLIC3RXS
va_list args; va_list args;
SV *error_sv = newSVpvf("Error in function %s at %s:%d: ", func, SV *error_sv = newSVpvf("Error in function %s at %s:%d: ", func,
file, line); file, line);
@ -23,4 +24,5 @@ confess_at(const char *file, int line, const char *func,
call_pv("Carp::confess", G_DISCARD); call_pv("Carp::confess", G_DISCARD);
FREETMPS; FREETMPS;
LEAVE; LEAVE;
#endif
} }