2014-05-02 16:46:22 +00:00
|
|
|
#include "Geometry.hpp"
|
2013-07-16 19:04:14 +00:00
|
|
|
#include "Line.hpp"
|
2013-11-21 14:12:06 +00:00
|
|
|
#include "Polyline.hpp"
|
2013-07-16 19:04:14 +00:00
|
|
|
#include <algorithm>
|
2014-03-04 22:33:13 +00:00
|
|
|
#include <cmath>
|
2014-01-17 13:22:37 +00:00
|
|
|
#include <sstream>
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2014-01-17 13:22:37 +00:00
|
|
|
std::string
|
|
|
|
Line::wkt() const
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << "LINESTRING(" << this->a.x << " " << this->a.y << ","
|
|
|
|
<< this->b.x << " " << this->b.y << ")";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
Line::operator Polyline() const
|
|
|
|
{
|
|
|
|
Polyline pl;
|
|
|
|
pl.points.push_back(this->a);
|
|
|
|
pl.points.push_back(this->b);
|
|
|
|
return pl;
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
void
|
|
|
|
Line::scale(double factor)
|
|
|
|
{
|
|
|
|
this->a.scale(factor);
|
|
|
|
this->b.scale(factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Line::translate(double x, double y)
|
|
|
|
{
|
|
|
|
this->a.translate(x, y);
|
|
|
|
this->b.translate(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-04-24 11:43:24 +00:00
|
|
|
Line::rotate(double angle, const Point ¢er)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
this->a.rotate(angle, center);
|
|
|
|
this->b.rotate(angle, center);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Line::reverse()
|
|
|
|
{
|
|
|
|
std::swap(this->a, this->b);
|
|
|
|
}
|
|
|
|
|
2013-08-28 18:32:25 +00:00
|
|
|
double
|
|
|
|
Line::length() const
|
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
return this->a.distance_to(this->b);
|
2013-08-28 18:32:25 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 23:36:42 +00:00
|
|
|
Point*
|
|
|
|
Line::midpoint() const
|
|
|
|
{
|
|
|
|
return new Point ((this->a.x + this->b.x) / 2.0, (this->a.y + this->b.y) / 2.0);
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:53:20 +00:00
|
|
|
void
|
|
|
|
Line::point_at(double distance, Point* point) const
|
2013-10-27 21:57:25 +00:00
|
|
|
{
|
|
|
|
double len = this->length();
|
2014-03-15 15:53:20 +00:00
|
|
|
*point = this->a;
|
2013-10-27 21:57:25 +00:00
|
|
|
if (this->a.x != this->b.x)
|
2014-03-15 15:53:20 +00:00
|
|
|
point->x = this->a.x + (this->b.x - this->a.x) * distance / len;
|
2013-10-27 21:57:25 +00:00
|
|
|
if (this->a.y != this->b.y)
|
2014-03-15 15:53:20 +00:00
|
|
|
point->y = this->a.y + (this->b.y - this->a.y) * distance / len;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point
|
|
|
|
Line::point_at(double distance) const
|
|
|
|
{
|
|
|
|
Point p;
|
|
|
|
this->point_at(distance, &p);
|
2013-10-27 21:57:25 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:38:10 +00:00
|
|
|
bool
|
2014-04-24 14:40:10 +00:00
|
|
|
Line::coincides_with(const Line &line) const
|
2013-11-06 18:38:10 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
return this->a.coincides_with(line.a) && this->b.coincides_with(line.b);
|
2013-11-06 18:38:10 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:08:03 +00:00
|
|
|
double
|
2014-04-24 14:40:10 +00:00
|
|
|
Line::distance_to(const Point &point) const
|
2013-11-06 22:08:03 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
return point.distance_to(*this);
|
2013-11-06 22:08:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 22:33:13 +00:00
|
|
|
double
|
|
|
|
Line::atan2_() const
|
|
|
|
{
|
|
|
|
return atan2(this->b.y - this->a.y, this->b.x - this->a.x);
|
|
|
|
}
|
|
|
|
|
2014-08-03 13:03:11 +00:00
|
|
|
double
|
|
|
|
Line::orientation() const
|
|
|
|
{
|
|
|
|
double angle = this->atan2_();
|
|
|
|
if (angle < 0) angle = 2*PI + angle;
|
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
2014-03-04 22:33:13 +00:00
|
|
|
double
|
|
|
|
Line::direction() const
|
|
|
|
{
|
|
|
|
double atan2 = this->atan2_();
|
2014-08-04 12:55:13 +00:00
|
|
|
return (fabs(atan2 - PI) < EPSILON) ? 0
|
2014-03-04 22:33:13 +00:00
|
|
|
: (atan2 < 0) ? (atan2 + PI)
|
|
|
|
: atan2;
|
|
|
|
}
|
|
|
|
|
2014-05-02 11:26:59 +00:00
|
|
|
bool
|
|
|
|
Line::parallel_to(double angle) const {
|
2014-05-02 16:46:22 +00:00
|
|
|
return Slic3r::Geometry::directions_parallel(this->direction(), angle);
|
2014-05-02 11:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Line::parallel_to(const Line &line) const {
|
|
|
|
return this->parallel_to(line.direction());
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:43:01 +00:00
|
|
|
Vector
|
|
|
|
Line::vector() const
|
|
|
|
{
|
|
|
|
return Vector(this->b.x - this->a.x, this->b.y - this->a.y);
|
|
|
|
}
|
|
|
|
|
2014-12-15 14:19:42 +00:00
|
|
|
Vector
|
|
|
|
Line::normal() const
|
|
|
|
{
|
|
|
|
return Vector((this->b.y - this->a.y), -(this->b.x - this->a.x));
|
|
|
|
}
|
|
|
|
|
2013-09-13 12:48:40 +00:00
|
|
|
#ifdef SLIC3RXS
|
2014-04-27 17:18:53 +00:00
|
|
|
|
|
|
|
REGISTER_CLASS(Line, "Line");
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
void
|
|
|
|
Line::from_SV(SV* line_sv)
|
|
|
|
{
|
|
|
|
AV* line_av = (AV*)SvRV(line_sv);
|
|
|
|
this->a.from_SV_check(*av_fetch(line_av, 0, 0));
|
|
|
|
this->b.from_SV_check(*av_fetch(line_av, 1, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Line::from_SV_check(SV* line_sv)
|
|
|
|
{
|
|
|
|
if (sv_isobject(line_sv) && (SvTYPE(SvRV(line_sv)) == SVt_PVMG)) {
|
2014-04-27 17:18:53 +00:00
|
|
|
if (!sv_isa(line_sv, perl_class_name(this)) && !sv_isa(line_sv, perl_class_name_ref(this)))
|
|
|
|
CONFESS("Not a valid %s object", perl_class_name(this));
|
2013-07-16 19:04:14 +00:00
|
|
|
*this = *(Line*)SvIV((SV*)SvRV( line_sv ));
|
|
|
|
} else {
|
|
|
|
this->from_SV(line_sv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SV*
|
2013-09-09 20:27:58 +00:00
|
|
|
Line::to_AV() {
|
2013-07-16 19:04:14 +00:00
|
|
|
AV* av = newAV();
|
|
|
|
av_extend(av, 1);
|
|
|
|
|
2014-05-08 12:52:48 +00:00
|
|
|
av_store(av, 0, perl_to_SV_ref(this->a));
|
|
|
|
av_store(av, 1, perl_to_SV_ref(this->b));
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
return newRV_noinc((SV*)av);
|
|
|
|
}
|
|
|
|
|
|
|
|
SV*
|
2013-09-03 17:26:58 +00:00
|
|
|
Line::to_SV_pureperl() const {
|
2013-07-16 19:04:14 +00:00
|
|
|
AV* av = newAV();
|
|
|
|
av_extend(av, 1);
|
|
|
|
av_store(av, 0, this->a.to_SV_pureperl());
|
|
|
|
av_store(av, 1, this->b.to_SV_pureperl());
|
|
|
|
return newRV_noinc((SV*)av);
|
|
|
|
}
|
2013-09-13 12:48:40 +00:00
|
|
|
#endif
|
2013-07-16 19:04:14 +00:00
|
|
|
|
2014-12-16 00:12:37 +00:00
|
|
|
Pointf3
|
|
|
|
Linef3::intersect_plane(double z) const
|
|
|
|
{
|
|
|
|
return Pointf3(
|
|
|
|
this->a.x + (this->b.x - this->a.x) * (z - this->a.z) / (this->b.z - this->a.z),
|
|
|
|
this->a.y + (this->b.y - this->a.y) * (z - this->a.z) / (this->b.z - this->a.z),
|
|
|
|
z
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SLIC3RXS
|
|
|
|
REGISTER_CLASS(Linef3, "Linef3");
|
|
|
|
#endif
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|