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();
|
|
|
|
}
|
|
|
|
|
2015-01-06 19:52:36 +00:00
|
|
|
Line::operator Lines() const
|
|
|
|
{
|
|
|
|
Lines lines;
|
|
|
|
lines.push_back(*this);
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-19 17:53:04 +00:00
|
|
|
Point
|
2013-08-28 23:36:42 +00:00
|
|
|
Line::midpoint() const
|
|
|
|
{
|
2015-01-19 17:53:04 +00:00
|
|
|
return Point((this->a.x + this->b.x) / 2.0, (this->a.y + this->b.y) / 2.0);
|
2013-08-28 23:36:42 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-16 15:25:39 +00:00
|
|
|
bool
|
|
|
|
Line::intersection_infinite(const Line &other, Point* point) const
|
|
|
|
{
|
|
|
|
Vector x = this->a.vector_to(other.a);
|
|
|
|
Vector d1 = this->vector();
|
|
|
|
Vector d2 = other.vector();
|
|
|
|
|
|
|
|
double cross = d1.x * d2.y - d1.y * d2.x;
|
|
|
|
if (std::fabs(cross) < EPSILON)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
double t1 = (x.x * d2.y - x.y * d2.x)/cross;
|
|
|
|
point->x = this->a.x + d1.x * t1;
|
|
|
|
point->y = this->a.y + d1.y * t1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:06:30 +00:00
|
|
|
void
|
|
|
|
Linef3::scale(double factor)
|
|
|
|
{
|
|
|
|
this->a.scale(factor);
|
|
|
|
this->b.scale(factor);
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|