2013-07-16 19:04:14 +00:00
|
|
|
#include "Point.hpp"
|
2013-11-06 22:08:03 +00:00
|
|
|
#include "Line.hpp"
|
2014-05-21 18:08:21 +00:00
|
|
|
#include "MultiPoint.hpp"
|
2018-05-17 08:37:26 +00:00
|
|
|
#include "Int128.hpp"
|
2015-01-03 14:03:53 +00:00
|
|
|
#include <algorithm>
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
void Point::rotate(double angle)
|
2016-04-10 17:06:46 +00:00
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
double cur_x = (double)(*this)(0);
|
|
|
|
double cur_y = (double)(*this)(1);
|
2018-08-15 11:51:40 +00:00
|
|
|
double s = ::sin(angle);
|
|
|
|
double c = ::cos(angle);
|
2018-08-17 13:53:43 +00:00
|
|
|
(*this)(0) = (coord_t)round(c * cur_x - s * cur_y);
|
|
|
|
(*this)(1) = (coord_t)round(c * cur_y + s * cur_x);
|
2016-04-10 17:06:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
void Point::rotate(double angle, const Point ¢er)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
double cur_x = (double)(*this)(0);
|
|
|
|
double cur_y = (double)(*this)(1);
|
2018-08-15 11:51:40 +00:00
|
|
|
double s = ::sin(angle);
|
|
|
|
double c = ::cos(angle);
|
2018-08-17 13:53:43 +00:00
|
|
|
double dx = cur_x - (double)center(0);
|
|
|
|
double dy = cur_y - (double)center(1);
|
|
|
|
(*this)(0) = (coord_t)round( (double)center(0) + c * dx - s * dy );
|
|
|
|
(*this)(1) = (coord_t)round( (double)center(1) + c * dy + s * dx );
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
int Point::nearest_point_index(const Points &points) const
|
2013-11-23 20:39:05 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
PointConstPtrs p;
|
2013-11-23 20:39:05 +00:00
|
|
|
p.reserve(points.size());
|
2014-04-24 14:40:10 +00:00
|
|
|
for (Points::const_iterator it = points.begin(); it != points.end(); ++it)
|
2013-11-23 20:39:05 +00:00
|
|
|
p.push_back(&*it);
|
|
|
|
return this->nearest_point_index(p);
|
|
|
|
}
|
|
|
|
|
2017-05-05 07:59:56 +00:00
|
|
|
int Point::nearest_point_index(const PointConstPtrs &points) const
|
2013-08-26 22:52:20 +00:00
|
|
|
{
|
|
|
|
int idx = -1;
|
2013-09-17 11:04:36 +00:00
|
|
|
double distance = -1; // double because long is limited to 2147483647 on some platforms and it's not enough
|
2013-08-26 22:52:20 +00:00
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
for (PointConstPtrs::const_iterator it = points.begin(); it != points.end(); ++it) {
|
2013-08-26 22:52:20 +00:00
|
|
|
/* If the X distance of the candidate is > than the total distance of the
|
|
|
|
best previous candidate, we know we don't want it */
|
2018-08-17 13:53:43 +00:00
|
|
|
double d = sqr<double>((*this)(0) - (*it)->x());
|
2013-08-26 22:52:20 +00:00
|
|
|
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 */
|
2018-08-17 13:53:43 +00:00
|
|
|
d += sqr<double>((*this)(1) - (*it)->y());
|
2013-08-26 22:52:20 +00:00
|
|
|
if (distance != -1 && d > distance) continue;
|
|
|
|
|
|
|
|
idx = it - points.begin();
|
|
|
|
distance = d;
|
|
|
|
|
|
|
|
if (distance < EPSILON) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
int Point::nearest_point_index(const PointPtrs &points) const
|
2013-08-26 22:52:20 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
PointConstPtrs p;
|
|
|
|
p.reserve(points.size());
|
|
|
|
for (PointPtrs::const_iterator it = points.begin(); it != points.end(); ++it)
|
|
|
|
p.push_back(*it);
|
|
|
|
return this->nearest_point_index(p);
|
2013-08-26 22:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
bool Point::nearest_point(const Points &points, Point* point) const
|
2013-08-28 18:32:25 +00:00
|
|
|
{
|
2014-09-23 18:19:47 +00:00
|
|
|
int idx = this->nearest_point_index(points);
|
|
|
|
if (idx == -1) return false;
|
2015-01-06 19:52:36 +00:00
|
|
|
*point = points.at(idx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:43:35 +00:00
|
|
|
/* Three points are a counter-clockwise turn if ccw > 0, clockwise if
|
|
|
|
* ccw < 0, and collinear if ccw = 0 because ccw is a determinant that
|
|
|
|
* gives the signed area of the triangle formed by p1, p2 and this point.
|
2013-12-12 19:19:33 +00:00
|
|
|
* In other words it is the 2D cross product of p1-p2 and p1-this, i.e.
|
|
|
|
* z-component of their 3D cross product.
|
|
|
|
* We return double because it must be big enough to hold 2*max(|coordinate|)^2
|
2013-11-22 20:43:35 +00:00
|
|
|
*/
|
2018-08-14 19:33:41 +00:00
|
|
|
double Point::ccw(const Point &p1, const Point &p2) const
|
2013-11-22 20:43:35 +00:00
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
return (double)(p2(0) - p1(0))*(double)((*this)(1) - p1(1)) - (double)(p2(1) - p1(1))*(double)((*this)(0) - p1(0));
|
2013-11-22 20:43:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
double Point::ccw(const Line &line) const
|
2013-11-22 20:43:35 +00:00
|
|
|
{
|
|
|
|
return this->ccw(line.a, line.b);
|
|
|
|
}
|
|
|
|
|
2014-12-07 18:53:22 +00:00
|
|
|
// returns the CCW angle between this-p1 and this-p2
|
2014-12-21 21:51:45 +00:00
|
|
|
// i.e. this assumes a CCW rotation from p1 to p2 around this
|
2018-08-14 19:33:41 +00:00
|
|
|
double Point::ccw_angle(const Point &p1, const Point &p2) const
|
2014-12-07 18:53:22 +00:00
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
double angle = atan2(p1(0) - (*this)(0), p1(1) - (*this)(1))
|
|
|
|
- atan2(p2(0) - (*this)(0), p2(1) - (*this)(1));
|
2014-12-21 21:51:45 +00:00
|
|
|
|
|
|
|
// we only want to return only positive angles
|
|
|
|
return angle <= 0 ? angle + 2*PI : angle;
|
2014-12-07 18:53:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
Point Point::projection_onto(const MultiPoint &poly) const
|
2014-05-21 18:08:21 +00:00
|
|
|
{
|
|
|
|
Point running_projection = poly.first_point();
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double running_min = (running_projection - *this).cast<double>().norm();
|
2014-05-21 18:08:21 +00:00
|
|
|
|
|
|
|
Lines lines = poly.lines();
|
|
|
|
for (Lines::const_iterator line = lines.begin(); line != lines.end(); ++line) {
|
|
|
|
Point point_temp = this->projection_onto(*line);
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
if ((point_temp - *this).cast<double>().norm() < running_min) {
|
2014-05-21 18:08:21 +00:00
|
|
|
running_projection = point_temp;
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
running_min = (running_projection - *this).cast<double>().norm();
|
2014-05-21 18:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return running_projection;
|
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
Point Point::projection_onto(const Line &line) const
|
2014-05-21 18:08:21 +00:00
|
|
|
{
|
2018-08-15 11:51:40 +00:00
|
|
|
if (line.a == line.b) return line.a;
|
2014-05-21 18:08:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
(Ported from VisiLibity by Karl J. Obermeyer)
|
|
|
|
The projection of point_temp onto the line determined by
|
|
|
|
line_segment_temp can be represented as an affine combination
|
|
|
|
expressed in the form projection of
|
|
|
|
Point = theta*line_segment_temp.first + (1.0-theta)*line_segment_temp.second.
|
|
|
|
If theta is outside the interval [0,1], then one of the Line_Segment's endpoints
|
|
|
|
must be closest to calling Point.
|
|
|
|
*/
|
2018-08-17 13:53:43 +00:00
|
|
|
double lx = (double)(line.b(0) - line.a(0));
|
|
|
|
double ly = (double)(line.b(1) - line.a(1));
|
|
|
|
double theta = ( (double)(line.b(0) - (*this)(0))*lx + (double)(line.b(1)- (*this)(1))*ly )
|
2016-09-13 11:30:00 +00:00
|
|
|
/ ( sqr<double>(lx) + sqr<double>(ly) );
|
2014-05-21 18:08:21 +00:00
|
|
|
|
|
|
|
if (0.0 <= theta && theta <= 1.0)
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
return (theta * line.a.cast<coordf_t>() + (1.0-theta) * line.b.cast<coordf_t>()).cast<coord_t>();
|
2014-05-21 18:08:21 +00:00
|
|
|
|
|
|
|
// Else pick closest endpoint.
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
return ((line.a - *this).cast<double>().squaredNorm() < (line.b - *this).cast<double>().squaredNorm()) ? line.a : line.b;
|
2014-05-21 18:08:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
std::ostream& operator<<(std::ostream &stm, const Pointf &pointf)
|
2015-05-02 19:43:22 +00:00
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
return stm << pointf(0) << "," << pointf(1);
|
2015-05-02 19:43:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 08:37:26 +00:00
|
|
|
namespace int128 {
|
|
|
|
|
2018-08-17 14:54:07 +00:00
|
|
|
int orient(const Vec2crd &p1, const Vec2crd &p2, const Vec2crd &p3)
|
2018-05-17 08:37:26 +00:00
|
|
|
{
|
|
|
|
Slic3r::Vector v1(p2 - p1);
|
|
|
|
Slic3r::Vector v2(p3 - p1);
|
2018-08-17 13:53:43 +00:00
|
|
|
return Int128::sign_determinant_2x2_filtered(v1(0), v1(1), v2(0), v2(1));
|
2018-05-17 08:37:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 14:54:07 +00:00
|
|
|
int cross(const Vec2crd &v1, const Vec2crd &v2)
|
2018-05-17 08:37:26 +00:00
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
return Int128::sign_determinant_2x2_filtered(v1(0), v1(1), v2(0), v2(1));
|
2018-05-17 08:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|