2013-07-16 19:04:14 +00:00
|
|
|
#include "Point.hpp"
|
2013-11-06 22:08:03 +00:00
|
|
|
#include "Line.hpp"
|
2013-11-22 15:01:50 +00:00
|
|
|
#include <cmath>
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
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
|
|
|
|
Point::rotate(double angle, Point* center)
|
|
|
|
{
|
|
|
|
double cur_x = (double)this->x;
|
|
|
|
double cur_y = (double)this->y;
|
2013-09-10 15:14:49 +00:00
|
|
|
this->x = (long)round( (double)center->x + cos(angle) * (cur_x - (double)center->x) - sin(angle) * (cur_y - (double)center->y) );
|
|
|
|
this->y = (long)round( (double)center->y + cos(angle) * (cur_y - (double)center->y) + sin(angle) * (cur_x - (double)center->x) );
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-08-26 20:39:35 +00:00
|
|
|
Point::coincides_with(const Point* point) const
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2013-11-22 21:48:07 +00:00
|
|
|
return this->coincides_with(*point);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Point::coincides_with(const Point &point) const
|
|
|
|
{
|
|
|
|
return this->x == point.x && this->y == point.y;
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 22:52:20 +00:00
|
|
|
int
|
|
|
|
Point::nearest_point_index(const Points points) const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
for (Points::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 */
|
2013-09-17 11:04:36 +00:00
|
|
|
double d = pow(this->x - (*it).x, 2);
|
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 */
|
|
|
|
d += pow(this->y - (*it).y, 2);
|
|
|
|
if (distance != -1 && d > distance) continue;
|
|
|
|
|
|
|
|
idx = it - points.begin();
|
|
|
|
distance = d;
|
|
|
|
|
|
|
|
if (distance < EPSILON) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point*
|
|
|
|
Point::nearest_point(Points points) const
|
|
|
|
{
|
|
|
|
return &(points.at(this->nearest_point_index(points)));
|
|
|
|
}
|
|
|
|
|
2013-08-28 18:32:25 +00:00
|
|
|
double
|
|
|
|
Point::distance_to(const Point* point) const
|
|
|
|
{
|
|
|
|
double dx = ((double)point->x - this->x);
|
|
|
|
double dy = ((double)point->y - this->y);
|
|
|
|
return sqrt(dx*dx + dy*dy);
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:08:03 +00:00
|
|
|
double
|
|
|
|
Point::distance_to(const Line* line) const
|
|
|
|
{
|
2013-11-21 19:25:24 +00:00
|
|
|
return this->distance_to(*line);
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
Point::distance_to(const Line &line) const
|
|
|
|
{
|
|
|
|
if (line.a.coincides_with(&line.b)) return this->distance_to(&line.a);
|
2013-11-06 22:08:03 +00:00
|
|
|
|
2013-11-21 19:25:24 +00:00
|
|
|
double n = (line.b.x - line.a.x) * (line.a.y - this->y)
|
|
|
|
- (line.a.x - this->x) * (line.b.y - line.a.y);
|
2013-11-06 22:08:03 +00:00
|
|
|
|
2013-11-22 15:01:50 +00:00
|
|
|
return std::abs(n) / line.length();
|
2013-11-06 22:08:03 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
double
|
|
|
|
Point::ccw(const Point &p1, const Point &p2) const
|
|
|
|
{
|
|
|
|
return (p2.x - p1.x)*(this->y - p1.y) - (p2.y - p1.y)*(this->x - p1.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
Point::ccw(const Point* p1, const Point* p2) const
|
|
|
|
{
|
|
|
|
return this->ccw(*p1, *p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
Point::ccw(const Line &line) const
|
|
|
|
{
|
|
|
|
return this->ccw(line.a, line.b);
|
|
|
|
}
|
|
|
|
|
2013-09-13 12:48:40 +00:00
|
|
|
#ifdef SLIC3RXS
|
2013-07-16 19:04:14 +00:00
|
|
|
SV*
|
2013-09-03 17:26:58 +00:00
|
|
|
Point::to_SV_ref() {
|
2013-09-02 18:22:20 +00:00
|
|
|
SV* sv = newSV(0);
|
|
|
|
sv_setref_pv( sv, "Slic3r::Point::Ref", (void*)this );
|
|
|
|
return sv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SV*
|
|
|
|
Point::to_SV_clone_ref() const {
|
|
|
|
SV* sv = newSV(0);
|
|
|
|
sv_setref_pv( sv, "Slic3r::Point", new Point(*this) );
|
|
|
|
return sv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SV*
|
|
|
|
Point::to_SV_pureperl() const {
|
2013-07-16 19:04:14 +00: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 12:00:50 +00: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 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Point::from_SV_check(SV* point_sv)
|
|
|
|
{
|
|
|
|
if (sv_isobject(point_sv) && (SvTYPE(SvRV(point_sv)) == SVt_PVMG)) {
|
|
|
|
*this = *(Point*)SvIV((SV*)SvRV( point_sv ));
|
|
|
|
} else {
|
|
|
|
this->from_SV(point_sv);
|
|
|
|
}
|
|
|
|
}
|
2013-09-13 12:48:40 +00:00
|
|
|
#endif
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
}
|