From 28466750e614320d3e7e62a1221b2c08b186cab8 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 15 Nov 2014 23:06:15 +0100 Subject: [PATCH] Ported some minor methods to XS --- lib/Slic3r/Line.pm | 2 -- lib/Slic3r/Polygon.pm | 10 +--------- lib/Slic3r/Polyline.pm | 29 +---------------------------- xs/src/libslic3r/Polygon.cpp | 13 +++++++++++++ xs/src/libslic3r/Polygon.hpp | 2 ++ xs/src/libslic3r/Polyline.cpp | 29 +++++++++++++++++++++++++++++ xs/src/libslic3r/Polyline.hpp | 3 +++ xs/xsp/Polygon.xsp | 1 + xs/xsp/Polyline.xsp | 11 ++++++++--- 9 files changed, 58 insertions(+), 42 deletions(-) diff --git a/lib/Slic3r/Line.pm b/lib/Slic3r/Line.pm index a50e7064e..bf53520d2 100644 --- a/lib/Slic3r/Line.pm +++ b/lib/Slic3r/Line.pm @@ -5,8 +5,6 @@ use warnings; # a line is a two-points line use parent 'Slic3r::Polyline'; -use Slic3r::Geometry qw(A B X Y); - sub intersection { my $self = shift; my ($line, $require_crossing) = @_; diff --git a/lib/Slic3r/Polygon.pm b/lib/Slic3r/Polygon.pm index d3d8e6a8d..bc0fbc5dd 100644 --- a/lib/Slic3r/Polygon.pm +++ b/lib/Slic3r/Polygon.pm @@ -5,15 +5,7 @@ use warnings; # a polygon is a closed polyline. use parent 'Slic3r::Polyline'; -use Slic3r::Geometry qw( - polygon_segment_having_point - PI X1 X2 Y1 Y2 epsilon scaled_epsilon); -use Slic3r::Geometry::Clipper qw(intersection_pl); - -sub wkt { - my $self = shift; - return sprintf "POLYGON((%s))", join ',', map "$_->[0] $_->[1]", @$self; -} +use Slic3r::Geometry qw(PI); sub dump_perl { my $self = shift; diff --git a/lib/Slic3r/Polyline.pm b/lib/Slic3r/Polyline.pm index 4e3045cdf..a42b5d1c4 100644 --- a/lib/Slic3r/Polyline.pm +++ b/lib/Slic3r/Polyline.pm @@ -2,9 +2,7 @@ package Slic3r::Polyline; use strict; use warnings; -use List::Util qw(first); -use Slic3r::Geometry qw(X Y PI epsilon); -use Slic3r::Geometry::Clipper qw(JT_SQUARE); +use Slic3r::Geometry qw(X Y); sub new_scale { my $class = shift; @@ -12,29 +10,4 @@ sub new_scale { return $class->new(map [ Slic3r::Geometry::scale($_->[X]), Slic3r::Geometry::scale($_->[Y]) ], @points); } -sub wkt { - my $self = shift; - return sprintf "LINESTRING((%s))", join ',', map "$_->[0] $_->[1]", @$self; -} - -sub bounding_box { - my $self = shift; - return Slic3r::Geometry::BoundingBox->new_from_points([ @$self ]); -} - -sub size { - my $self = shift; - return [ Slic3r::Geometry::size_2D($self) ]; -} - -sub is_straight { - my ($self) = @_; - - # Check that each segment's direction is equal to the line connecting - # first point and last point. (Checking each line against the previous - # one would have caused the error to accumulate.) - my $dir = Slic3r::Line->new($self->first_point, $self->last_point)->direction; - return !defined first { !$_->parallel_to($dir) } @{$self->lines}; -} - 1; diff --git a/xs/src/libslic3r/Polygon.cpp b/xs/src/libslic3r/Polygon.cpp index 3fe16bc70..32526e5d2 100644 --- a/xs/src/libslic3r/Polygon.cpp +++ b/xs/src/libslic3r/Polygon.cpp @@ -209,6 +209,19 @@ Polygon::centroid() const return Point(x_temp/(6*area_temp), y_temp/(6*area_temp)); } +std::string +Polygon::wkt() const +{ + std::ostringstream wkt; + wkt << "POLYGON(("; + for (Points::const_iterator p = this->points.begin(); p != this->points.end(); ++p) { + wkt << p->x << " " << p->y; + if (p != this->points.end()-1) wkt << ","; + } + wkt << "))"; + return wkt.str(); +} + #ifdef SLIC3RXS REGISTER_CLASS(Polygon, "Polygon"); diff --git a/xs/src/libslic3r/Polygon.hpp b/xs/src/libslic3r/Polygon.hpp index 816b6be18..e842f1848 100644 --- a/xs/src/libslic3r/Polygon.hpp +++ b/xs/src/libslic3r/Polygon.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "Line.hpp" #include "MultiPoint.hpp" #include "Polyline.hpp" @@ -36,6 +37,7 @@ class Polygon : public MultiPoint { void simplify(double tolerance, Polygons &polygons) const; void triangulate_convex(Polygons* polygons) const; Point centroid() const; + std::string wkt() const; #ifdef SLIC3RXS void from_SV_check(SV* poly_sv); diff --git a/xs/src/libslic3r/Polyline.cpp b/xs/src/libslic3r/Polyline.cpp index f64b1a0b4..1ae5055ba 100644 --- a/xs/src/libslic3r/Polyline.cpp +++ b/xs/src/libslic3r/Polyline.cpp @@ -1,6 +1,7 @@ #include "Polyline.hpp" #include "Line.hpp" #include "Polygon.hpp" +#include namespace Slic3r { @@ -162,6 +163,34 @@ Polyline::split_at(const Point &point, Polyline* p1, Polyline* p2) const } } +bool +Polyline::is_straight() const +{ + /* Check that each segment's direction is equal to the line connecting + first point and last point. (Checking each line against the previous + one would cause the error to accumulate.) */ + double dir = Line(this->first_point(), this->last_point()).direction(); + + Lines lines = this->lines(); + for (Lines::const_iterator line = lines.begin(); line != lines.end(); ++line) { + if (!line->parallel_to(dir)) return false; + } + return true; +} + +std::string +Polyline::wkt() const +{ + std::ostringstream wkt; + wkt << "LINESTRING(("; + for (Points::const_iterator p = this->points.begin(); p != this->points.end(); ++p) { + wkt << p->x << " " << p->y; + if (p != this->points.end()-1) wkt << ","; + } + wkt << "))"; + return wkt.str(); +} + #ifdef SLIC3RXS REGISTER_CLASS(Polyline, "Polyline"); diff --git a/xs/src/libslic3r/Polyline.hpp b/xs/src/libslic3r/Polyline.hpp index de5493099..2a9a79032 100644 --- a/xs/src/libslic3r/Polyline.hpp +++ b/xs/src/libslic3r/Polyline.hpp @@ -3,6 +3,7 @@ #include "Line.hpp" #include "MultiPoint.hpp" +#include namespace Slic3r { @@ -23,6 +24,8 @@ class Polyline : public MultiPoint { void equally_spaced_points(double distance, Points* points) const; void simplify(double tolerance); void split_at(const Point &point, Polyline* p1, Polyline* p2) const; + bool is_straight() const; + std::string wkt() const; #ifdef SLIC3RXS void from_SV_check(SV* poly_sv); diff --git a/xs/xsp/Polygon.xsp b/xs/xsp/Polygon.xsp index 653691e49..cad1907ff 100644 --- a/xs/xsp/Polygon.xsp +++ b/xs/xsp/Polygon.xsp @@ -46,6 +46,7 @@ RETVAL = new BoundingBox(); THIS->bounding_box(RETVAL); %}; + std::string wkt(); %{ Polygon* diff --git a/xs/xsp/Polyline.xsp b/xs/xsp/Polyline.xsp index e97897719..721ac1008 100644 --- a/xs/xsp/Polyline.xsp +++ b/xs/xsp/Polyline.xsp @@ -34,6 +34,13 @@ void simplify(double tolerance); void split_at(Point* point, Polyline* p1, Polyline* p2) %code{% THIS->split_at(*point, p1, p2); %}; + bool is_straight(); + BoundingBox* bounding_box() + %code{% + RETVAL = new BoundingBox(); + THIS->bounding_box(RETVAL); + %}; + std::string wkt(); %{ Polyline* @@ -81,9 +88,7 @@ Polyline::grow(delta, scale = CLIPPER_OFFSET_SCALE, joinType = ClipperLib::jtSqu ClipperLib::JoinType joinType double miterLimit CODE: - Polylines polylines; - polylines.push_back(*THIS); - offset(polylines, RETVAL, delta, scale, joinType, miterLimit); + offset(*THIS, RETVAL, delta, scale, joinType, miterLimit); OUTPUT: RETVAL