diff --git a/xs/src/Line.cpp b/xs/src/Line.cpp index 7c0a16032b..16a3241aff 100644 --- a/xs/src/Line.cpp +++ b/xs/src/Line.cpp @@ -1,9 +1,19 @@ #include "Line.hpp" #include "Polyline.hpp" #include +#include namespace Slic3r { +std::string +Line::wkt() const +{ + std::ostringstream ss; + ss << "LINESTRING(" << this->a.x << " " << this->a.y << "," + << this->b.x << " " << this->b.y << ")"; + return ss.str(); +} + Line::operator Polyline() const { Polyline pl; diff --git a/xs/src/Line.hpp b/xs/src/Line.hpp index c387f9b553..308f07f85f 100644 --- a/xs/src/Line.hpp +++ b/xs/src/Line.hpp @@ -16,6 +16,7 @@ class Line Point b; Line() {}; explicit Line(Point _a, Point _b): a(_a), b(_b) {}; + std::string wkt() const; operator Polyline() const; void scale(double factor); void translate(double x, double y); diff --git a/xs/src/Point.cpp b/xs/src/Point.cpp index 7d9d7e12d1..ff63eae58c 100644 --- a/xs/src/Point.cpp +++ b/xs/src/Point.cpp @@ -1,9 +1,18 @@ #include "Point.hpp" #include "Line.hpp" #include +#include namespace Slic3r { +std::string +Point::wkt() const +{ + std::ostringstream ss; + ss << "POINT(" << this->x << " " << this->y << ")"; + return ss.str(); +} + void Point::scale(double factor) { @@ -100,8 +109,8 @@ Point::distance_to(const Line &line) const { if (line.a.coincides_with(&line.b)) return this->distance_to(&line.a); - double n = (line.b.x - line.a.x) * (line.a.y - this->y) - - (line.a.x - this->x) * (line.b.y - line.a.y); + double n = (double)(line.b.x - line.a.x) * (double)(line.a.y - this->y) + - (double)(line.a.x - this->x) * (double)(line.b.y - line.a.y); return std::abs(n) / line.length(); } diff --git a/xs/src/Point.hpp b/xs/src/Point.hpp index 9e621b603a..24e468a068 100644 --- a/xs/src/Point.hpp +++ b/xs/src/Point.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace Slic3r { @@ -20,6 +21,7 @@ class Point coord_t x; coord_t y; explicit Point(coord_t _x = 0, coord_t _y = 0): x(_x), y(_y) {}; + std::string wkt() const; void scale(double factor); void translate(double x, double y); void rotate(double angle, Point* center); diff --git a/xs/t/03_point.t b/xs/t/03_point.t index d8268602cd..39a2313ec6 100644 --- a/xs/t/03_point.t +++ b/xs/t/03_point.t @@ -4,7 +4,7 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 8; +use Test::More tests => 9; my $point = Slic3r::Point->new(10, 15); is_deeply [ @$point ], [10, 15], 'point roundtrip'; @@ -30,4 +30,13 @@ ok !$point->coincides_with($point2), 'coincides_with'; ok $nearest->coincides_with($point2), 'nearest_point'; } +{ + my $line = Slic3r::Line->new( + [18335846,18335845], + [18335846,1664160], + ); + $point = Slic3r::Point->new(1664161,18335848); + is $point->distance_to_line($line), 16671685, 'distance_to_line() does not overflow'; +} + __END__ diff --git a/xs/xsp/Point.xsp b/xs/xsp/Point.xsp index 453afd30a3..8afcf1838a 100644 --- a/xs/xsp/Point.xsp +++ b/xs/xsp/Point.xsp @@ -24,6 +24,7 @@ Point* nearest_point(Points points) %code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->nearest_point(points))); %}; double distance_to(Point* point); + %name{distance_to_line} double distance_to(Line* line); %{