Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbubnikv <bubnikv@gmail.com>2016-04-10 20:06:46 +0300
committerbubnikv <bubnikv@gmail.com>2016-04-10 20:06:46 +0300
commit7d54e28e30a4dddc4ab0b5befc4c46d2fe05794b (patch)
treeda10817ffc8c9a1b6475e8a333e3c4436902b160
parentc8ff517389264444bd0fa6a1b5dd9db132cf40cd (diff)
Added optimized methods for point and polyline rotation.
Existing methods for rotation were optimized by calculating the sin/cos values once only. Added an operator- for points.
-rw-r--r--xs/src/libslic3r/MultiPoint.cpp14
-rw-r--r--xs/src/libslic3r/MultiPoint.hpp1
-rw-r--r--xs/src/libslic3r/Point.cpp40
-rw-r--r--xs/src/libslic3r/Point.hpp3
4 files changed, 54 insertions, 4 deletions
diff --git a/xs/src/libslic3r/MultiPoint.cpp b/xs/src/libslic3r/MultiPoint.cpp
index ff2771cff..1e1895634 100644
--- a/xs/src/libslic3r/MultiPoint.cpp
+++ b/xs/src/libslic3r/MultiPoint.cpp
@@ -31,6 +31,20 @@ MultiPoint::translate(const Point &vector)
}
void
+MultiPoint::rotate(double angle)
+{
+ double s = sin(angle);
+ double c = cos(angle);
+ for (Points::iterator it = points.begin(); it != points.end(); ++it) {
+ (*it).rotate(angle);
+ double cur_x = (double)it->x;
+ double cur_y = (double)it->y;
+ it->x = (coord_t)round(c * cur_x - s * cur_y);
+ it->y = (coord_t)round(c * cur_y + s * cur_x);
+ }
+}
+
+void
MultiPoint::rotate(double angle, const Point &center)
{
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
diff --git a/xs/src/libslic3r/MultiPoint.hpp b/xs/src/libslic3r/MultiPoint.hpp
index 18d67a9e4..0f31fd318 100644
--- a/xs/src/libslic3r/MultiPoint.hpp
+++ b/xs/src/libslic3r/MultiPoint.hpp
@@ -22,6 +22,7 @@ class MultiPoint
void scale(double factor);
void translate(double x, double y);
void translate(const Point &vector);
+ void rotate(double angle);
void rotate(double angle, const Point &center);
void reverse();
Point first_point() const;
diff --git a/xs/src/libslic3r/Point.cpp b/xs/src/libslic3r/Point.cpp
index dc9d510c6..8dad1c5d7 100644
--- a/xs/src/libslic3r/Point.cpp
+++ b/xs/src/libslic3r/Point.cpp
@@ -47,12 +47,25 @@ Point::translate(const Vector &vector)
}
void
+Point::rotate(double angle)
+{
+ double cur_x = (double)this->x;
+ double cur_y = (double)this->y;
+ double s = sin(angle);
+ double c = cos(angle);
+ this->x = (coord_t)round(c * cur_x - s * cur_y);
+ this->y = (coord_t)round(c * cur_y + s * cur_x);
+}
+
+void
Point::rotate(double angle, const Point &center)
{
double cur_x = (double)this->x;
double cur_y = (double)this->y;
- this->x = (coord_t)round( (double)center.x + cos(angle) * (cur_x - (double)center.x) - sin(angle) * (cur_y - (double)center.y) );
- this->y = (coord_t)round( (double)center.y + cos(angle) * (cur_y - (double)center.y) + sin(angle) * (cur_x - (double)center.x) );
+ double s = sin(angle);
+ double c = cos(angle);
+ this->x = (coord_t)round( (double)center.x + c * (cur_x - (double)center.x) - s * (cur_y - (double)center.y) );
+ this->y = (coord_t)round( (double)center.y + c * (cur_y - (double)center.y) + s * (cur_x - (double)center.x) );
}
bool
@@ -294,6 +307,12 @@ operator+(const Point& point1, const Point& point2)
}
Point
+operator-(const Point& point1, const Point& point2)
+{
+ return Point(point1.x - point2.x, point1.y - point2.y);
+}
+
+Point
operator*(double scalar, const Point& point2)
{
return Point(scalar * point2.x, scalar * point2.y);
@@ -334,12 +353,25 @@ Pointf::translate(const Vectorf &vector)
}
void
+Pointf::rotate(double angle)
+{
+ double cur_x = this->x;
+ double cur_y = this->y;
+ double s = sin(angle);
+ double c = cos(angle);
+ this->x = c * cur_x - s * cur_y;
+ this->y = c * cur_y + s * cur_x;
+}
+
+void
Pointf::rotate(double angle, const Pointf &center)
{
double cur_x = this->x;
double cur_y = this->y;
- this->x = center.x + cos(angle) * (cur_x - center.x) - sin(angle) * (cur_y - center.y);
- this->y = center.y + cos(angle) * (cur_y - center.y) + sin(angle) * (cur_x - center.x);
+ double s = sin(angle);
+ double c = cos(angle);
+ this->x = center.x + c * (cur_x - center.x) - s * (cur_y - center.y);
+ this->y = center.y + c * (cur_y - center.y) + s * (cur_x - center.x);
}
Pointf
diff --git a/xs/src/libslic3r/Point.hpp b/xs/src/libslic3r/Point.hpp
index dbd1219b9..14c5cf70f 100644
--- a/xs/src/libslic3r/Point.hpp
+++ b/xs/src/libslic3r/Point.hpp
@@ -41,6 +41,7 @@ class Point
void scale(double factor);
void translate(double x, double y);
void translate(const Vector &vector);
+ void rotate(double angle);
void rotate(double angle, const Point &center);
bool coincides_with(const Point &point) const;
bool coincides_with_epsilon(const Point &point) const;
@@ -63,6 +64,7 @@ class Point
};
Point operator+(const Point& point1, const Point& point2);
+Point operator-(const Point& point1, const Point& point2);
Point operator*(double scalar, const Point& point2);
class Point3 : public Point
@@ -90,6 +92,7 @@ class Pointf
void scale(double factor);
void translate(double x, double y);
void translate(const Vectorf &vector);
+ void rotate(double angle);
void rotate(double angle, const Pointf &center);
Pointf negative() const;
Vectorf vector_to(const Pointf &point) const;