diff --git a/xs/src/ExtrusionEntity.cpp b/xs/src/ExtrusionEntity.cpp index eb3507df0..974e8910f 100644 --- a/xs/src/ExtrusionEntity.cpp +++ b/xs/src/ExtrusionEntity.cpp @@ -33,7 +33,7 @@ ExtrusionLoop::clone() const } ExtrusionPath* -ExtrusionLoop::split_at_index(int index) +ExtrusionLoop::split_at_index(int index) const { Polyline* poly = this->polygon.split_at_index(index); @@ -48,7 +48,7 @@ ExtrusionLoop::split_at_index(int index) } ExtrusionPath* -ExtrusionLoop::split_at_first_point() +ExtrusionLoop::split_at_first_point() const { return this->split_at_index(0); } diff --git a/xs/src/ExtrusionEntity.hpp b/xs/src/ExtrusionEntity.hpp index 129115460..c9dd6b080 100644 --- a/xs/src/ExtrusionEntity.hpp +++ b/xs/src/ExtrusionEntity.hpp @@ -52,8 +52,8 @@ class ExtrusionLoop : public ExtrusionEntity public: ExtrusionLoop* clone() const; Polygon polygon; - ExtrusionPath* split_at_index(int index); - ExtrusionPath* split_at_first_point(); + ExtrusionPath* split_at_index(int index) const; + ExtrusionPath* split_at_first_point() const; bool make_counter_clockwise(); void reverse(); Point* first_point() const; diff --git a/xs/src/Polygon.cpp b/xs/src/Polygon.cpp index 9ea5c9104..4132a9fba 100644 --- a/xs/src/Polygon.cpp +++ b/xs/src/Polygon.cpp @@ -15,40 +15,40 @@ Lines Polygon::lines() const { Lines lines; - for (int i = 0; i < this->points.size()-1; i++) { - lines.push_back(Line(this->points[i], this->points[i+1])); + lines.reserve(this->points.size()); + for (Points::const_iterator it = this->points.begin(); it != this->points.end()-1; ++it) { + lines.push_back(Line(*it, *(it + 1))); } lines.push_back(Line(this->points.back(), this->points.front())); return lines; } Polyline* -Polygon::split_at(const Point* point) +Polygon::split_at(const Point* point) const { // find index of point for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) { - if ((*it).coincides_with(point)) { + if (it->coincides_with(point)) return this->split_at_index(it - this->points.begin()); - } } - throw "Point not found"; + CONFESS("Point not found"); + return NULL; } Polyline* -Polygon::split_at_index(int index) +Polygon::split_at_index(int index) const { Polyline* poly = new Polyline; - for (int i = index; i < this->points.size(); i++) { - poly->points.push_back( this->points[i] ); - } - for (int i = 0; i <= index; i++) { - poly->points.push_back( this->points[i] ); - } + poly->points.reserve(this->points.size() + 1); + for (Points::const_iterator it = this->points.begin() + index; it != this->points.end(); ++it) + poly->points.push_back(*it); + for (Points::const_iterator it = this->points.begin(); it != this->points.begin() + index + 1; ++it) + poly->points.push_back(*it); return poly; } Polyline* -Polygon::split_at_first_point() +Polygon::split_at_first_point() const { return this->split_at_index(0); } diff --git a/xs/src/Polygon.hpp b/xs/src/Polygon.hpp index 3855ddc0a..ce8beafb1 100644 --- a/xs/src/Polygon.hpp +++ b/xs/src/Polygon.hpp @@ -13,9 +13,9 @@ class Polygon : public MultiPoint { public: Point* last_point() const; Lines lines() const; - Polyline* split_at(const Point* point); - Polyline* split_at_index(int index); - Polyline* split_at_first_point(); + Polyline* split_at(const Point* point) const; + Polyline* split_at_index(int index) const; + Polyline* split_at_first_point() const; double area() const; bool is_counter_clockwise() const; bool is_clockwise() const; diff --git a/xs/src/Polyline.cpp b/xs/src/Polyline.cpp index 71bb9db56..3046f6b47 100644 --- a/xs/src/Polyline.cpp +++ b/xs/src/Polyline.cpp @@ -8,13 +8,15 @@ Polyline::last_point() const return new Point(this->points.back()); } -void -Polyline::lines(Lines &lines) const +Lines +Polyline::lines() const { - lines.clear(); - for (int i = 0; i < this->points.size()-1; i++) { - lines.push_back(Line(this->points[i], this->points[i+1])); + Lines lines; + lines.reserve(this->points.size() - 1); + for (Points::const_iterator it = this->points.begin(); it != this->points.end()-1; ++it) { + lines.push_back(Line(*it, *(it + 1))); } + return lines; } #ifdef SLIC3RXS diff --git a/xs/src/Polyline.hpp b/xs/src/Polyline.hpp index 0658e064f..5ec702726 100644 --- a/xs/src/Polyline.hpp +++ b/xs/src/Polyline.hpp @@ -9,7 +9,7 @@ namespace Slic3r { class Polyline : public MultiPoint { public: Point* last_point() const; - void lines(Lines &lines) const; + Lines lines() const; #ifdef SLIC3RXS SV* to_SV_ref(); diff --git a/xs/xsp/ExtrusionPath.xsp b/xs/xsp/ExtrusionPath.xsp index 34c845707..db2db16ad 100644 --- a/xs/xsp/ExtrusionPath.xsp +++ b/xs/xsp/ExtrusionPath.xsp @@ -15,7 +15,7 @@ %code{% THIS->polyline.points.pop_back(); %}; void reverse(); Lines lines() - %code{% RETVAL = Lines(); THIS->polyline.lines(RETVAL); %}; + %code{% RETVAL = THIS->polyline.lines(); %}; Point* first_point() %code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %}; Point* last_point() diff --git a/xs/xsp/Polyline.xsp b/xs/xsp/Polyline.xsp index 15ee0c5ff..3f681f3cb 100644 --- a/xs/xsp/Polyline.xsp +++ b/xs/xsp/Polyline.xsp @@ -18,8 +18,7 @@ void pop_back() %code{% THIS->points.pop_back(); %}; void reverse(); - Lines lines() - %code{% RETVAL = Lines(); THIS->lines(RETVAL); %}; + Lines lines(); Point* first_point() %code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %}; Point* last_point()