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;
|
2018-08-14 16:33:26 +00:00
|
|
|
ss << "LINESTRING(" << this->a.x() << " " << this->a.y() << ","
|
|
|
|
<< this->b.x() << " " << this->b.y() << ")";
|
2014-01-17 13:22:37 +00:00
|
|
|
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
|
|
|
|
{
|
2018-08-14 16:33:26 +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;
|
2018-08-14 16:33:26 +00:00
|
|
|
if (this->a.x() != this->b.x())
|
|
|
|
point->x() = this->a.x() + (this->b.x() - this->a.x()) * distance / len;
|
|
|
|
if (this->a.y() != this->b.y())
|
|
|
|
point->y() = this->a.y() + (this->b.y() - this->a.y()) * distance / len;
|
2014-03-15 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2018-08-14 16:33:26 +00:00
|
|
|
double cross = d1.x() * d2.y() - d1.y() * d2.x();
|
2015-01-16 15:25:39 +00:00
|
|
|
if (std::fabs(cross) < EPSILON)
|
|
|
|
return false;
|
|
|
|
|
2018-08-14 16:33:26 +00:00
|
|
|
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;
|
2015-01-16 15:25:39 +00:00
|
|
|
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
|
|
|
{
|
2018-08-15 11:51:40 +00:00
|
|
|
return this->a == line.a && this->b == 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
|
|
|
|
{
|
2018-08-14 16:33:26 +00:00
|
|
|
return atan2(this->b.y() - this->a.y(), this->b.x() - this->a.x());
|
2014-03-04 22:33:13 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2018-08-14 16:33:26 +00:00
|
|
|
return Vector(this->b.x() - this->a.x(), this->b.y() - this->a.y());
|
2014-03-05 17:43:01 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 14:19:42 +00:00
|
|
|
Vector
|
|
|
|
Line::normal() const
|
|
|
|
{
|
2018-08-14 16:33:26 +00:00
|
|
|
return Vector((this->b.y() - this->a.y()), -(this->b.x() - this->a.x()));
|
2014-12-15 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 14:33:58 +00:00
|
|
|
void
|
|
|
|
Line::extend_end(double distance)
|
|
|
|
{
|
|
|
|
// relocate last point by extending the segment by the specified length
|
|
|
|
Line line = *this;
|
|
|
|
line.reverse();
|
|
|
|
this->b = line.point_at(-distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Line::extend_start(double distance)
|
|
|
|
{
|
|
|
|
// relocate first point by extending the first segment by the specified length
|
|
|
|
this->a = this->point_at(-distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Line::intersection(const Line& line, Point* intersection) const
|
|
|
|
{
|
2018-08-14 16:33:26 +00:00
|
|
|
double denom = ((double)(line.b.y() - line.a.y())*(this->b.x() - this->a.x())) -
|
|
|
|
((double)(line.b.x() - line.a.x())*(this->b.y() - this->a.y()));
|
2016-03-19 14:33:58 +00:00
|
|
|
|
2018-08-14 16:33:26 +00:00
|
|
|
double nume_a = ((double)(line.b.x() - line.a.x())*(this->a.y() - line.a.y())) -
|
|
|
|
((double)(line.b.y() - line.a.y())*(this->a.x() - line.a.x()));
|
2016-03-19 14:33:58 +00:00
|
|
|
|
2018-08-14 16:33:26 +00:00
|
|
|
double nume_b = ((double)(this->b.x() - this->a.x())*(this->a.y() - line.a.y())) -
|
|
|
|
((double)(this->b.y() - this->a.y())*(this->a.x() - line.a.x()));
|
2016-03-19 14:33:58 +00:00
|
|
|
|
|
|
|
if (fabs(denom) < EPSILON) {
|
|
|
|
if (fabs(nume_a) < EPSILON && fabs(nume_b) < EPSILON) {
|
|
|
|
return false; // coincident
|
|
|
|
}
|
|
|
|
return false; // parallel
|
|
|
|
}
|
|
|
|
|
|
|
|
double ua = nume_a / denom;
|
|
|
|
double ub = nume_b / denom;
|
|
|
|
|
|
|
|
if (ua >= 0 && ua <= 1.0f && ub >= 0 && ub <= 1.0f)
|
|
|
|
{
|
|
|
|
// Get the intersection point.
|
2018-08-14 16:33:26 +00:00
|
|
|
intersection->x() = this->a.x() + ua*(this->b.x() - this->a.x());
|
|
|
|
intersection->y() = this->a.y() + ua*(this->b.y() - this->a.y());
|
2016-03-19 14:33:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; // not intersecting
|
|
|
|
}
|
|
|
|
|
2016-05-20 04:24:05 +00:00
|
|
|
double
|
|
|
|
Line::ccw(const Point& point) const
|
|
|
|
{
|
|
|
|
return point.ccw(*this);
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:44:10 +00:00
|
|
|
double Line3::length() const
|
|
|
|
{
|
2018-08-15 11:51:40 +00:00
|
|
|
return (b - a).norm();
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Line3::vector() const
|
|
|
|
{
|
2018-08-15 11:51:40 +00:00
|
|
|
return Vector3(b - a);
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 11:51:40 +00:00
|
|
|
Pointf3 Linef3::intersect_plane(double z) const
|
2014-12-16 00:12:37 +00:00
|
|
|
{
|
2018-08-15 11:51:40 +00:00
|
|
|
Vec3d v = this->b - this->a;
|
|
|
|
double t = (z - this->a.z()) / v.z();
|
|
|
|
return Pointf3(this->a.x() + v.x() * t, this->a.y() + v.y() * t, z);
|
2014-12-16 00:12:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|