Changing the internal representation of Point / Pointf / Point3 / Pointf3 to Eigen Matrix types, first step

This commit is contained in:
bubnikv 2018-08-14 18:33:26 +02:00
parent 077680b806
commit 86da661097
60 changed files with 1228 additions and 1206 deletions

View file

@ -3,21 +3,13 @@
#include "MultiPoint.hpp"
#include "Int128.hpp"
#include <algorithm>
#include <cmath>
namespace Slic3r {
Point::Point(double x, double y)
{
this->x = lrint(x);
this->y = lrint(y);
}
std::string
Point::wkt() const
std::string Point::wkt() const
{
std::ostringstream ss;
ss << "POINT(" << this->x << " " << this->y << ")";
ss << "POINT(" << this->x() << " " << this->y() << ")";
return ss.str();
}
@ -25,58 +17,58 @@ std::string
Point::dump_perl() const
{
std::ostringstream ss;
ss << "[" << this->x << "," << this->y << "]";
ss << "[" << this->x() << "," << this->y() << "]";
return ss.str();
}
void
Point::scale(double factor)
{
this->x *= factor;
this->y *= factor;
this->x() *= factor;
this->y() *= factor;
}
void
Point::translate(double x, double y)
{
this->x += x;
this->y += y;
this->x() += x;
this->y() += y;
}
void
Point::translate(const Vector &vector)
{
this->translate(vector.x, vector.y);
this->translate(vector.x(), vector.y());
}
void
Point::rotate(double angle)
{
double cur_x = (double)this->x;
double cur_y = (double)this->y;
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);
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;
double cur_x = (double)this->x();
double cur_y = (double)this->y();
double s = sin(angle);
double c = cos(angle);
double dx = cur_x - (double)center.x;
double dy = cur_y - (double)center.y;
this->x = (coord_t)round( (double)center.x + c * dx - s * dy );
this->y = (coord_t)round( (double)center.y + c * dy + s * dx );
double dx = cur_x - (double)center.x();
double dy = cur_y - (double)center.y();
this->x() = (coord_t)round( (double)center.x() + c * dx - s * dy );
this->y() = (coord_t)round( (double)center.y() + c * dy + s * dx );
}
bool
Point::coincides_with_epsilon(const Point &point) const
{
return std::abs(this->x - point.x) < SCALED_EPSILON && std::abs(this->y - point.y) < SCALED_EPSILON;
return std::abs(this->x() - point.x()) < SCALED_EPSILON && std::abs(this->y() - point.y()) < SCALED_EPSILON;
}
int
@ -97,12 +89,12 @@ int Point::nearest_point_index(const PointConstPtrs &points) const
for (PointConstPtrs::const_iterator it = points.begin(); it != points.end(); ++it) {
/* If the X distance of the candidate is > than the total distance of the
best previous candidate, we know we don't want it */
double d = sqr<double>(this->x - (*it)->x);
double d = sqr<double>(this->x() - (*it)->x());
if (distance != -1 && d > distance) continue;
/* If the Y distance of the candidate is > than the total distance of the
best previous candidate, we know we don't want it */
d += sqr<double>(this->y - (*it)->y);
d += sqr<double>(this->y() - (*it)->y());
if (distance != -1 && d > distance) continue;
idx = it - points.begin();
@ -137,8 +129,8 @@ Point::nearest_point(const Points &points, Point* point) const
double
Point::distance_to(const Line &line) const
{
const double dx = line.b.x - line.a.x;
const double dy = line.b.y - line.a.y;
const double dx = line.b.x() - line.a.x();
const double dy = line.b.y() - line.a.y();
const double l2 = dx*dx + dy*dy; // avoid a sqrt
if (l2 == 0.0) return this->distance_to(line.a); // line.a == line.b case
@ -146,12 +138,12 @@ Point::distance_to(const Line &line) const
// Consider the line extending the segment, parameterized as line.a + t (line.b - line.a).
// We find projection of this point onto the line.
// It falls where t = [(this-line.a) . (line.b-line.a)] / |line.b-line.a|^2
const double t = ((this->x - line.a.x) * dx + (this->y - line.a.y) * dy) / l2;
const double t = ((this->x() - line.a.x()) * dx + (this->y() - line.a.y()) * dy) / l2;
if (t < 0.0) return this->distance_to(line.a); // beyond the 'a' end of the segment
else if (t > 1.0) return this->distance_to(line.b); // beyond the 'b' end of the segment
Point projection(
line.a.x + t * dx,
line.a.y + t * dy
line.a.x() + t * dx,
line.a.y() + t * dy
);
return this->distance_to(projection);
}
@ -161,8 +153,8 @@ Point::perp_distance_to(const Line &line) const
{
if (line.a.coincides_with(line.b)) return this->distance_to(line.a);
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);
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();
}
@ -177,7 +169,7 @@ Point::perp_distance_to(const Line &line) const
double
Point::ccw(const Point &p1, const Point &p2) const
{
return (double)(p2.x - p1.x)*(double)(this->y - p1.y) - (double)(p2.y - p1.y)*(double)(this->x - p1.x);
return (double)(p2.x() - p1.x())*(double)(this->y() - p1.y()) - (double)(p2.y() - p1.y())*(double)(this->x() - p1.x());
}
double
@ -191,8 +183,8 @@ Point::ccw(const Line &line) const
double
Point::ccw_angle(const Point &p1, const Point &p2) const
{
double angle = atan2(p1.x - this->x, p1.y - this->y)
- atan2(p2.x - this->x, p2.y - this->y);
double angle = atan2(p1.x() - this->x(), p1.y() - this->y())
- atan2(p2.x() - this->x(), p2.y() - this->y());
// we only want to return only positive angles
return angle <= 0 ? angle + 2*PI : angle;
@ -229,9 +221,9 @@ Point::projection_onto(const Line &line) const
If theta is outside the interval [0,1], then one of the Line_Segment's endpoints
must be closest to calling Point.
*/
double lx = (double)(line.b.x - line.a.x);
double ly = (double)(line.b.y - line.a.y);
double theta = ( (double)(line.b.x - this->x)*lx + (double)(line.b.y- this->y)*ly )
double lx = (double)(line.b.x() - line.a.x());
double ly = (double)(line.b.y() - line.a.y());
double theta = ( (double)(line.b.x() - this->x())*lx + (double)(line.b.y()- this->y())*ly )
/ ( sqr<double>(lx) + sqr<double>(ly) );
if (0.0 <= theta && theta <= 1.0)
@ -248,26 +240,26 @@ Point::projection_onto(const Line &line) const
Point
Point::negative() const
{
return Point(-this->x, -this->y);
return Point(-this->x(), -this->y());
}
Vector
Point::vector_to(const Point &point) const
{
return Vector(point.x - this->x, point.y - this->y);
return Vector(point.x() - this->x(), point.y() - this->y());
}
std::ostream&
operator<<(std::ostream &stm, const Pointf &pointf)
{
return stm << pointf.x << "," << pointf.y;
return stm << pointf.x() << "," << pointf.y();
}
std::string
Pointf::wkt() const
{
std::ostringstream ss;
ss << "POINT(" << this->x << " " << this->y << ")";
ss << "POINT(" << this->x() << " " << this->y() << ")";
return ss.str();
}
@ -275,105 +267,105 @@ std::string
Pointf::dump_perl() const
{
std::ostringstream ss;
ss << "[" << this->x << "," << this->y << "]";
ss << "[" << this->x() << "," << this->y() << "]";
return ss.str();
}
void
Pointf::scale(double factor)
{
this->x *= factor;
this->y *= factor;
this->x() *= factor;
this->y() *= factor;
}
void
Pointf::translate(double x, double y)
{
this->x += x;
this->y += y;
this->x() += x;
this->y() += y;
}
void
Pointf::translate(const Vectorf &vector)
{
this->translate(vector.x, vector.y);
this->translate(vector.x(), vector.y());
}
void
Pointf::rotate(double angle)
{
double cur_x = this->x;
double cur_y = this->y;
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;
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;
double cur_x = this->x();
double cur_y = this->y();
double s = sin(angle);
double c = cos(angle);
double dx = cur_x - center.x;
double dy = cur_y - center.y;
this->x = center.x + c * dx - s * dy;
this->y = center.y + c * dy + s * dx;
double dx = cur_x - center.x();
double dy = cur_y - center.y();
this->x() = center.x() + c * dx - s * dy;
this->y() = center.y() + c * dy + s * dx;
}
Pointf
Pointf::negative() const
{
return Pointf(-this->x, -this->y);
return Pointf(-this->x(), -this->y());
}
Vectorf
Pointf::vector_to(const Pointf &point) const
{
return Vectorf(point.x - this->x, point.y - this->y);
return Vectorf(point.x() - this->x(), point.y() - this->y());
}
void
Pointf3::scale(double factor)
{
Pointf::scale(factor);
this->z *= factor;
this->z() *= factor;
}
void
Pointf3::translate(const Vectorf3 &vector)
{
this->translate(vector.x, vector.y, vector.z);
this->translate(vector.x(), vector.y(), vector.z());
}
void
Pointf3::translate(double x, double y, double z)
{
Pointf::translate(x, y);
this->z += z;
this->z() += z;
}
double
Pointf3::distance_to(const Pointf3 &point) const
{
double dx = ((double)point.x - this->x);
double dy = ((double)point.y - this->y);
double dz = ((double)point.z - this->z);
double dx = ((double)point.x() - this->x());
double dy = ((double)point.y() - this->y());
double dz = ((double)point.z() - this->z());
return sqrt(dx*dx + dy*dy + dz*dz);
}
Pointf3
Pointf3::negative() const
{
return Pointf3(-this->x, -this->y, -this->z);
return Pointf3(-this->x(), -this->y(), -this->z());
}
Vectorf3
Pointf3::vector_to(const Pointf3 &point) const
{
return Vectorf3(point.x - this->x, point.y - this->y, point.z - this->z);
return Vectorf3(point.x() - this->x(), point.y() - this->y(), point.z() - this->z());
}
namespace int128 {
@ -382,12 +374,12 @@ int orient(const Point &p1, const Point &p2, const Point &p3)
{
Slic3r::Vector v1(p2 - p1);
Slic3r::Vector v2(p3 - p1);
return Int128::sign_determinant_2x2_filtered(v1.x, v1.y, v2.x, v2.y);
return Int128::sign_determinant_2x2_filtered(v1.x(), v1.y(), v2.x(), v2.y());
}
int cross(const Point &v1, const Point &v2)
{
return Int128::sign_determinant_2x2_filtered(v1.x, v1.y, v2.x, v2.y);
return Int128::sign_determinant_2x2_filtered(v1.x(), v1.y(), v2.x(), v2.y());
}
}