diff --git a/xs/src/libslic3r/MultiPoint.cpp b/xs/src/libslic3r/MultiPoint.cpp index 639eb6bb74..8b181d7bb3 100644 --- a/xs/src/libslic3r/MultiPoint.cpp +++ b/xs/src/libslic3r/MultiPoint.cpp @@ -130,24 +130,6 @@ MultiPoint::remove_duplicate_points() return false; } -void -MultiPoint::append(const Point &point) -{ - this->points.push_back(point); -} - -void -MultiPoint::append(const Points &points) -{ - this->append(points.begin(), points.end()); -} - -void -MultiPoint::append(const Points::const_iterator &begin, const Points::const_iterator &end) -{ - this->points.insert(this->points.end(), begin, end); -} - bool MultiPoint::intersection(const Line& line, Point* intersection) const { diff --git a/xs/src/libslic3r/MultiPoint.hpp b/xs/src/libslic3r/MultiPoint.hpp index 60e24e17ea..0795e7a15b 100644 --- a/xs/src/libslic3r/MultiPoint.hpp +++ b/xs/src/libslic3r/MultiPoint.hpp @@ -18,7 +18,11 @@ class MultiPoint operator Points() const; MultiPoint() {}; - explicit MultiPoint(const Points &_points): points(_points) {}; + MultiPoint(const MultiPoint &other) : points(other.points) {} + MultiPoint(MultiPoint &&other) : points(std::move(other.points)) {} + explicit MultiPoint(const Points &_points): points(_points) {} + MultiPoint& operator=(const MultiPoint &other) { points = other.points; return *this; } + MultiPoint& operator=(MultiPoint &&other) { points = std::move(other.points); return *this; } void scale(double factor); void translate(double x, double y); void translate(const Point &vector); @@ -38,9 +42,17 @@ class MultiPoint bool has_duplicate_points() const; // Remove exact duplicates, return true if any duplicate has been removed. bool remove_duplicate_points(); - void append(const Point &point); - void append(const Points &points); - void append(const Points::const_iterator &begin, const Points::const_iterator &end); + void append(const Point &point) { this->points.push_back(point); } + void append(const Points &src) { this->append(src.begin(), src.end()); } + void append(const Points::const_iterator &begin, const Points::const_iterator &end) { this->points.insert(this->points.end(), begin, end); } + void append(Points &&src) + { + if (this->points.empty()) + this->points = std::move(src); + else + std::move(std::begin(src), std::end(src), std::back_inserter(this->points)); + } + bool intersection(const Line& line, Point* intersection) const; std::string dump_perl() const; diff --git a/xs/src/libslic3r/Point.cpp b/xs/src/libslic3r/Point.cpp index 2ee5dabdd6..db90765d4d 100644 --- a/xs/src/libslic3r/Point.cpp +++ b/xs/src/libslic3r/Point.cpp @@ -174,14 +174,6 @@ Point::nearest_waypoint(const Points &points, const Point &dest, Point* point) c return true; } -double -Point::distance_to(const Point &point) const -{ - double dx = ((double)point.x - this->x); - double dy = ((double)point.y - this->y); - return sqrt(dx*dx + dy*dy); -} - /* distance to the closest point of line */ double Point::distance_to(const Line &line) const diff --git a/xs/src/libslic3r/Point.hpp b/xs/src/libslic3r/Point.hpp index 0405ec0786..396d629a4b 100644 --- a/xs/src/libslic3r/Point.hpp +++ b/xs/src/libslic3r/Point.hpp @@ -54,7 +54,8 @@ class Point size_t nearest_waypoint_index(const Points &points, const Point &point) const; bool nearest_point(const Points &points, Point* point) const; bool nearest_waypoint(const Points &points, const Point &dest, Point* point) const; - double distance_to(const Point &point) const; + double distance_to(const Point &point) const { return sqrt(distance_to_sq(point)); } + double distance_to_sq(const Point &point) const { double dx = double(point.x - this->x); double dy = double(point.y - this->y); return dx*dx + dy*dy; } double distance_to(const Line &line) const; double perp_distance_to(const Line &line) const; double ccw(const Point &p1, const Point &p2) const; diff --git a/xs/src/libslic3r/Polygon.hpp b/xs/src/libslic3r/Polygon.hpp index b0a7ea40a0..7c8612d69b 100644 --- a/xs/src/libslic3r/Polygon.hpp +++ b/xs/src/libslic3r/Polygon.hpp @@ -14,14 +14,19 @@ class Polygon; typedef std::vector Polygons; class Polygon : public MultiPoint { - public: +public: operator Polygons() const; operator Polyline() const; Point& operator[](Points::size_type idx); const Point& operator[](Points::size_type idx) const; - Polygon() {}; - explicit Polygon(const Points &points): MultiPoint(points) {}; + Polygon() {} + explicit Polygon(const Points &points): MultiPoint(points) {} + Polygon(const Polygon &other) : MultiPoint(other.points) {} + Polygon(Polygon &&other) : MultiPoint(std::move(other.points)) {} + Polygon& operator=(const Polygon &other) { points = other.points; return *this; } + Polygon& operator=(Polygon &&other) { points = std::move(other.points); return *this; } + Point last_point() const; virtual Lines lines() const; Polyline split_at_vertex(const Point &point) const; diff --git a/xs/src/libslic3r/Polyline.hpp b/xs/src/libslic3r/Polyline.hpp index b8c04a12ad..007fcba62d 100644 --- a/xs/src/libslic3r/Polyline.hpp +++ b/xs/src/libslic3r/Polyline.hpp @@ -15,7 +15,36 @@ typedef std::vector Polylines; typedef std::vector ThickPolylines; class Polyline : public MultiPoint { - public: +public: + Polyline() {}; + Polyline(const Polyline &other) : MultiPoint(other.points) {} + Polyline(Polyline &&other) : MultiPoint(std::move(other.points)) {} + Polyline& operator=(const Polyline &other) { points = other.points; return *this; } + Polyline& operator=(Polyline &&other) { points = std::move(other.points); return *this; } + + void append(const Point &point) { this->points.push_back(point); } + void append(const Points &src) { this->append(src.begin(), src.end()); } + void append(const Points::const_iterator &begin, const Points::const_iterator &end) { this->points.insert(this->points.end(), begin, end); } + void append(Points &&src) + { + if (this->points.empty()) + this->points = std::move(src); + else + std::move(std::begin(src), std::end(src), std::back_inserter(this->points)); + } + void append(const Polyline &src) + { + points.insert(points.end(), src.points.begin(), src.points.end()); + } + + void append(Polyline &&src) + { + if (this->points.empty()) + this->points = std::move(src.points); + else + std::move(std::begin(src.points), std::end(src.points), std::back_inserter(this->points)); + } + operator Polylines() const; operator Line() const; Point last_point() const; @@ -63,6 +92,19 @@ inline Lines to_lines(const Polylines &polys) return lines; } +inline void polylines_append(Polylines &dst, const Polylines &src) +{ + dst.insert(dst.end(), src.begin(), src.end()); +} + +inline void polylines_append(Polylines &dst, Polylines &&src) +{ + if (dst.empty()) + dst = std::move(src); + else + std::move(std::begin(src), std::end(src), std::back_inserter(dst)); +} + class ThickPolyline : public Polyline { public: std::vector width;