2013-07-16 23:04:14 +04:00
|
|
|
#include "Point.hpp"
|
2013-11-07 02:08:03 +04:00
|
|
|
#include "Line.hpp"
|
2014-04-27 21:18:53 +04:00
|
|
|
#include <cmath>
|
|
|
|
#include <sstream>
|
2013-07-16 23:04:14 +04:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2014-01-10 14:47:16 +04:00
|
|
|
bool
|
2014-03-03 01:36:20 +04:00
|
|
|
Point::operator==(const Point& rhs) const
|
|
|
|
{
|
2014-01-09 22:56:12 +04:00
|
|
|
return this->coincides_with(rhs);
|
|
|
|
}
|
|
|
|
|
2014-01-17 17:22:37 +04:00
|
|
|
std::string
|
|
|
|
Point::wkt() const
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << "POINT(" << this->x << " " << this->y << ")";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2013-07-16 23:04:14 +04:00
|
|
|
void
|
|
|
|
Point::scale(double factor)
|
|
|
|
{
|
|
|
|
this->x *= factor;
|
|
|
|
this->y *= factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Point::translate(double x, double y)
|
|
|
|
{
|
|
|
|
this->x += x;
|
|
|
|
this->y += y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-04-24 15:43:24 +04:00
|
|
|
Point::rotate(double angle, const Point ¢er)
|
2013-07-16 23:04:14 +04:00
|
|
|
{
|
|
|
|
double cur_x = (double)this->x;
|
|
|
|
double cur_y = (double)this->y;
|
2014-04-24 15:43:24 +04:00
|
|
|
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) );
|
2013-07-16 23:04:14 +04:00
|
|
|
}
|
|
|
|
|
2013-11-23 01:48:07 +04:00
|
|
|
bool
|
|
|
|
Point::coincides_with(const Point &point) const
|
|
|
|
{
|
|
|
|
return this->x == point.x && this->y == point.y;
|
2013-07-16 23:04:14 +04:00
|
|
|
}
|
|
|
|
|
2013-08-27 02:52:20 +04:00
|
|
|
int
|
2014-04-24 18:40:10 +04:00
|
|
|
Point::nearest_point_index(const Points &points) const
|
2013-11-24 00:39:05 +04:00
|
|
|
{
|
2014-04-24 18:40:10 +04:00
|
|
|
PointConstPtrs p;
|
2013-11-24 00:39:05 +04:00
|
|
|
p.reserve(points.size());
|
2014-04-24 18:40:10 +04:00
|
|
|
for (Points::const_iterator it = points.begin(); it != points.end(); ++it)
|
2013-11-24 00:39:05 +04:00
|
|
|
p.push_back(&*it);
|
|
|
|
return this->nearest_point_index(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-04-24 18:40:10 +04:00
|
|
|
Point::nearest_point_index(const PointConstPtrs &points) const
|
2013-08-27 02:52:20 +04:00
|
|
|
{
|
|
|
|
int idx = -1;
|
2013-09-17 15:04:36 +04:00
|
|
|
double distance = -1; // double because long is limited to 2147483647 on some platforms and it's not enough
|
2013-08-27 02:52:20 +04:00
|
|
|
|
2014-04-24 18:40:10 +04:00
|
|
|
for (PointConstPtrs::const_iterator it = points.begin(); it != points.end(); ++it) {
|
2013-08-27 02:52:20 +04: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 */
|
2013-11-24 00:39:05 +04:00
|
|
|
double d = pow(this->x - (*it)->x, 2);
|
2013-08-27 02:52:20 +04: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 */
|
2013-11-24 00:39:05 +04:00
|
|
|
d += pow(this->y - (*it)->y, 2);
|
2013-08-27 02:52:20 +04:00
|
|
|
if (distance != -1 && d > distance) continue;
|
|
|
|
|
|
|
|
idx = it - points.begin();
|
|
|
|
distance = d;
|
|
|
|
|
|
|
|
if (distance < EPSILON) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2014-04-24 18:40:10 +04:00
|
|
|
int
|
|
|
|
Point::nearest_point_index(const PointPtrs &points) const
|
2013-08-27 02:52:20 +04:00
|
|
|
{
|
2014-04-24 18:40:10 +04: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-27 02:52:20 +04:00
|
|
|
}
|
|
|
|
|
2014-04-24 18:40:10 +04:00
|
|
|
void
|
|
|
|
Point::nearest_point(const Points &points, Point* point) const
|
2013-08-28 22:32:25 +04:00
|
|
|
{
|
2014-04-24 18:40:10 +04:00
|
|
|
*point = points.at(this->nearest_point_index(points));
|
2013-08-28 22:32:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-07 02:08:03 +04:00
|
|
|
double
|
2014-04-24 18:40:10 +04:00
|
|
|
Point::distance_to(const Point &point) const
|
2013-11-07 02:08:03 +04:00
|
|
|
{
|
2014-04-24 18:40:10 +04:00
|
|
|
double dx = ((double)point.x - this->x);
|
|
|
|
double dy = ((double)point.y - this->y);
|
|
|
|
return sqrt(dx*dx + dy*dy);
|
2013-11-21 23:25:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
Point::distance_to(const Line &line) const
|
|
|
|
{
|
2014-04-24 18:40:10 +04:00
|
|
|
if (line.a.coincides_with(line.b)) return this->distance_to(line.a);
|
2013-11-07 02:08:03 +04:00
|
|
|
|
2014-01-17 17:22:37 +04:00
|
|
|
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);
|
2013-11-07 02:08:03 +04:00
|
|
|
|
2013-11-22 19:01:50 +04:00
|
|
|
return std::abs(n) / line.length();
|
2013-11-07 02:08:03 +04:00
|
|
|
}
|
|
|
|
|
2013-11-23 00:43:35 +04: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 23:19:33 +04: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-23 00:43:35 +04:00
|
|
|
*/
|
|
|
|
double
|
|
|
|
Point::ccw(const Point &p1, const Point &p2) const
|
|
|
|
{
|
2014-01-17 17:49:51 +04:00
|
|
|
return (double)(p2.x - p1.x)*(double)(this->y - p1.y) - (double)(p2.y - p1.y)*(double)(this->x - p1.x);
|
2013-11-23 00:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
Point::ccw(const Line &line) const
|
|
|
|
{
|
|
|
|
return this->ccw(line.a, line.b);
|
|
|
|
}
|
|
|
|
|
2013-09-13 16:48:40 +04:00
|
|
|
#ifdef SLIC3RXS
|
2014-04-27 21:18:53 +04:00
|
|
|
|
|
|
|
REGISTER_CLASS(Point, "Point");
|
|
|
|
|
2013-09-02 22:22:20 +04:00
|
|
|
SV*
|
|
|
|
Point::to_SV_pureperl() const {
|
2013-07-16 23:04:14 +04:00
|
|
|
AV* av = newAV();
|
|
|
|
av_fill(av, 1);
|
|
|
|
av_store(av, 0, newSViv(this->x));
|
|
|
|
av_store(av, 1, newSViv(this->y));
|
|
|
|
return newRV_noinc((SV*)av);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Point::from_SV(SV* point_sv)
|
|
|
|
{
|
|
|
|
AV* point_av = (AV*)SvRV(point_sv);
|
2013-11-11 16:00:50 +04:00
|
|
|
// get a double from Perl and round it, otherwise
|
|
|
|
// it would get truncated
|
|
|
|
this->x = lrint(SvNV(*av_fetch(point_av, 0, 0)));
|
|
|
|
this->y = lrint(SvNV(*av_fetch(point_av, 1, 0)));
|
2013-07-16 23:04:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Point::from_SV_check(SV* point_sv)
|
|
|
|
{
|
|
|
|
if (sv_isobject(point_sv) && (SvTYPE(SvRV(point_sv)) == SVt_PVMG)) {
|
2014-04-27 21:18:53 +04:00
|
|
|
if (!sv_isa(point_sv, perl_class_name(this)) && !sv_isa(point_sv, perl_class_name_ref(this)))
|
|
|
|
CONFESS("Not a valid %s object (got %s)", perl_class_name(this), HvNAME(SvSTASH(SvRV(point_sv))));
|
2013-07-16 23:04:14 +04:00
|
|
|
*this = *(Point*)SvIV((SV*)SvRV( point_sv ));
|
|
|
|
} else {
|
|
|
|
this->from_SV(point_sv);
|
|
|
|
}
|
|
|
|
}
|
2013-12-20 23:54:11 +04:00
|
|
|
|
2014-04-27 21:18:53 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
Pointf::scale(double factor)
|
|
|
|
{
|
|
|
|
this->x *= factor;
|
|
|
|
this->y *= factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pointf::translate(double x, double y)
|
|
|
|
{
|
|
|
|
this->x += x;
|
|
|
|
this->y += y;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SLIC3RXS
|
|
|
|
|
|
|
|
REGISTER_CLASS(Pointf, "Pointf");
|
|
|
|
|
2013-12-20 23:54:11 +04:00
|
|
|
SV*
|
|
|
|
Pointf::to_SV_pureperl() const {
|
|
|
|
AV* av = newAV();
|
|
|
|
av_fill(av, 1);
|
|
|
|
av_store(av, 0, newSVnv(this->x));
|
|
|
|
av_store(av, 1, newSVnv(this->y));
|
|
|
|
return newRV_noinc((SV*)av);
|
|
|
|
}
|
|
|
|
|
2014-03-24 04:07:30 +04:00
|
|
|
bool
|
2013-12-20 23:54:11 +04:00
|
|
|
Pointf::from_SV(SV* point_sv)
|
|
|
|
{
|
|
|
|
AV* point_av = (AV*)SvRV(point_sv);
|
2014-03-24 04:07:30 +04:00
|
|
|
SV* sv_x = *av_fetch(point_av, 0, 0);
|
|
|
|
SV* sv_y = *av_fetch(point_av, 1, 0);
|
2014-04-05 11:40:24 +04:00
|
|
|
if (!looks_like_number(sv_x) || !looks_like_number(sv_y)) return false;
|
2014-03-24 04:07:30 +04:00
|
|
|
|
|
|
|
this->x = SvNV(sv_x);
|
|
|
|
this->y = SvNV(sv_y);
|
|
|
|
return true;
|
2013-12-20 23:54:11 +04:00
|
|
|
}
|
2013-09-13 16:48:40 +04:00
|
|
|
#endif
|
2013-07-16 23:04:14 +04:00
|
|
|
|
2014-01-07 15:48:09 +04:00
|
|
|
void
|
|
|
|
Pointf3::scale(double factor)
|
|
|
|
{
|
|
|
|
Pointf::scale(factor);
|
|
|
|
this->z *= factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pointf3::translate(double x, double y, double z)
|
|
|
|
{
|
|
|
|
Pointf::translate(x, y);
|
|
|
|
this->z += z;
|
|
|
|
}
|
|
|
|
|
2014-04-27 21:18:53 +04:00
|
|
|
#ifdef SLIC3RXS
|
|
|
|
REGISTER_CLASS(Pointf3, "Pointf3");
|
|
|
|
#endif
|
|
|
|
|
2013-07-16 23:04:14 +04:00
|
|
|
}
|