Minor optimizations

This commit is contained in:
Alessandro Ranellucci 2013-09-13 15:19:15 +02:00
parent 6e22a82e7d
commit 4dce461aaf
8 changed files with 31 additions and 30 deletions

View File

@ -33,7 +33,7 @@ ExtrusionLoop::clone() const
} }
ExtrusionPath* ExtrusionPath*
ExtrusionLoop::split_at_index(int index) ExtrusionLoop::split_at_index(int index) const
{ {
Polyline* poly = this->polygon.split_at_index(index); Polyline* poly = this->polygon.split_at_index(index);
@ -48,7 +48,7 @@ ExtrusionLoop::split_at_index(int index)
} }
ExtrusionPath* ExtrusionPath*
ExtrusionLoop::split_at_first_point() ExtrusionLoop::split_at_first_point() const
{ {
return this->split_at_index(0); return this->split_at_index(0);
} }

View File

@ -52,8 +52,8 @@ class ExtrusionLoop : public ExtrusionEntity
public: public:
ExtrusionLoop* clone() const; ExtrusionLoop* clone() const;
Polygon polygon; Polygon polygon;
ExtrusionPath* split_at_index(int index); ExtrusionPath* split_at_index(int index) const;
ExtrusionPath* split_at_first_point(); ExtrusionPath* split_at_first_point() const;
bool make_counter_clockwise(); bool make_counter_clockwise();
void reverse(); void reverse();
Point* first_point() const; Point* first_point() const;

View File

@ -15,40 +15,40 @@ Lines
Polygon::lines() const Polygon::lines() const
{ {
Lines lines; Lines lines;
for (int i = 0; i < this->points.size()-1; i++) { lines.reserve(this->points.size());
lines.push_back(Line(this->points[i], this->points[i+1])); 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())); lines.push_back(Line(this->points.back(), this->points.front()));
return lines; return lines;
} }
Polyline* Polyline*
Polygon::split_at(const Point* point) Polygon::split_at(const Point* point) const
{ {
// find index of point // find index of point
for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) { 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()); return this->split_at_index(it - this->points.begin());
} }
} CONFESS("Point not found");
throw "Point not found"; return NULL;
} }
Polyline* Polyline*
Polygon::split_at_index(int index) Polygon::split_at_index(int index) const
{ {
Polyline* poly = new Polyline; Polyline* poly = new Polyline;
for (int i = index; i < this->points.size(); i++) { poly->points.reserve(this->points.size() + 1);
poly->points.push_back( this->points[i] ); for (Points::const_iterator it = this->points.begin() + index; it != this->points.end(); ++it)
} poly->points.push_back(*it);
for (int i = 0; i <= index; i++) { for (Points::const_iterator it = this->points.begin(); it != this->points.begin() + index + 1; ++it)
poly->points.push_back( this->points[i] ); poly->points.push_back(*it);
}
return poly; return poly;
} }
Polyline* Polyline*
Polygon::split_at_first_point() Polygon::split_at_first_point() const
{ {
return this->split_at_index(0); return this->split_at_index(0);
} }

View File

@ -13,9 +13,9 @@ class Polygon : public MultiPoint {
public: public:
Point* last_point() const; Point* last_point() const;
Lines lines() const; Lines lines() const;
Polyline* split_at(const Point* point); Polyline* split_at(const Point* point) const;
Polyline* split_at_index(int index); Polyline* split_at_index(int index) const;
Polyline* split_at_first_point(); Polyline* split_at_first_point() const;
double area() const; double area() const;
bool is_counter_clockwise() const; bool is_counter_clockwise() const;
bool is_clockwise() const; bool is_clockwise() const;

View File

@ -8,13 +8,15 @@ Polyline::last_point() const
return new Point(this->points.back()); return new Point(this->points.back());
} }
void Lines
Polyline::lines(Lines &lines) const Polyline::lines() const
{ {
lines.clear(); Lines lines;
for (int i = 0; i < this->points.size()-1; i++) { lines.reserve(this->points.size() - 1);
lines.push_back(Line(this->points[i], this->points[i+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 #ifdef SLIC3RXS

View File

@ -9,7 +9,7 @@ namespace Slic3r {
class Polyline : public MultiPoint { class Polyline : public MultiPoint {
public: public:
Point* last_point() const; Point* last_point() const;
void lines(Lines &lines) const; Lines lines() const;
#ifdef SLIC3RXS #ifdef SLIC3RXS
SV* to_SV_ref(); SV* to_SV_ref();

View File

@ -15,7 +15,7 @@
%code{% THIS->polyline.points.pop_back(); %}; %code{% THIS->polyline.points.pop_back(); %};
void reverse(); void reverse();
Lines lines() Lines lines()
%code{% RETVAL = Lines(); THIS->polyline.lines(RETVAL); %}; %code{% RETVAL = THIS->polyline.lines(); %};
Point* first_point() Point* first_point()
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %}; %code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %};
Point* last_point() Point* last_point()

View File

@ -18,8 +18,7 @@
void pop_back() void pop_back()
%code{% THIS->points.pop_back(); %}; %code{% THIS->points.pop_back(); %};
void reverse(); void reverse();
Lines lines() Lines lines();
%code{% RETVAL = Lines(); THIS->lines(RETVAL); %};
Point* first_point() Point* first_point()
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %}; %code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %};
Point* last_point() Point* last_point()