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 {
|
|
|
|
|
2018-08-28 07:03:03 +00:00
|
|
|
Linef3 transform(const Linef3& line, const Transform3d& t)
|
|
|
|
{
|
|
|
|
typedef Eigen::Matrix<double, 3, 2> LineInMatrixForm;
|
|
|
|
|
|
|
|
LineInMatrixForm world_line;
|
|
|
|
::memcpy((void*)world_line.col(0).data(), (const void*)line.a.data(), 3 * sizeof(double));
|
|
|
|
::memcpy((void*)world_line.col(1).data(), (const void*)line.b.data(), 3 * sizeof(double));
|
|
|
|
|
|
|
|
LineInMatrixForm local_line = t * world_line.colwise().homogeneous();
|
|
|
|
return Linef3(Vec3d(local_line(0, 0), local_line(1, 0), local_line(2, 0)), Vec3d(local_line(0, 1), local_line(1, 1), local_line(2, 1)));
|
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
bool Line::intersection_infinite(const Line &other, Point* point) const
|
2015-01-06 19:52:36 +00:00
|
|
|
{
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
Vec2d a1 = this->a.cast<double>();
|
|
|
|
Vec2d v12 = (other.a - this->a).cast<double>();
|
|
|
|
Vec2d v1 = (this->b - this->a).cast<double>();
|
|
|
|
Vec2d v2 = (other.b - other.a).cast<double>();
|
|
|
|
double denom = cross2(v1, v2);
|
|
|
|
if (std::fabs(denom) < EPSILON)
|
2015-01-16 15:25:39 +00:00
|
|
|
return false;
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double t1 = cross2(v12, v2) / denom;
|
|
|
|
*point = (a1 + t1 * v1).cast<coord_t>();
|
2015-01-16 15:25:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-14 18:29:58 +00:00
|
|
|
// Distance to the closest point of line.
|
|
|
|
double Line::distance_to_squared(const Point &point, const Point &a, const Point &b)
|
2013-11-06 22:08:03 +00:00
|
|
|
{
|
2018-12-14 18:29:58 +00:00
|
|
|
const Vec2d v = (b - a).cast<double>();
|
|
|
|
const Vec2d va = (point - a).cast<double>();
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
const double l2 = v.squaredNorm(); // avoid a sqrt
|
|
|
|
if (l2 == 0.0)
|
2018-12-14 18:29:58 +00:00
|
|
|
// a == b case
|
|
|
|
return va.squaredNorm();
|
|
|
|
// Consider the line extending the segment, parameterized as a + t (b - a).
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
// We find projection of this point onto the line.
|
2018-12-14 18:29:58 +00:00
|
|
|
// It falls where t = [(this-a) . (b-a)] / |b-a|^2
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
const double t = va.dot(v) / l2;
|
2018-12-14 18:29:58 +00:00
|
|
|
if (t < 0.0) return va.squaredNorm(); // beyond the 'a' end of the segment
|
|
|
|
else if (t > 1.0) return (point - b).cast<double>().squaredNorm(); // beyond the 'b' end of the segment
|
|
|
|
return (t * v - va).squaredNorm();
|
2013-11-06 22:08:03 +00:00
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double Line::perp_distance_to(const Point &point) const
|
2014-03-04 22:33:13 +00:00
|
|
|
{
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
const Line &line = *this;
|
|
|
|
const Vec2d v = (line.b - line.a).cast<double>();
|
|
|
|
const Vec2d va = (point - line.a).cast<double>();
|
|
|
|
if (line.a == line.b)
|
|
|
|
return va.norm();
|
|
|
|
return std::abs(cross2(v, va)) / v.norm();
|
2014-03-04 22:33:13 +00:00
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double Line::orientation() const
|
2014-08-03 13:03:11 +00:00
|
|
|
{
|
|
|
|
double angle = this->atan2_();
|
|
|
|
if (angle < 0) angle = 2*PI + angle;
|
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double Line::direction() const
|
2014-03-04 22:33:13 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
bool Line::parallel_to(double angle) const
|
2014-03-05 17:43:01 +00:00
|
|
|
{
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
return Slic3r::Geometry::directions_parallel(this->direction(), angle);
|
2016-03-19 14:33:58 +00:00
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
bool Line::intersection(const Line &l2, Point *intersection) const
|
|
|
|
{
|
|
|
|
const Line &l1 = *this;
|
|
|
|
const Vec2d v1 = (l1.b - l1.a).cast<double>();
|
|
|
|
const Vec2d v2 = (l2.b - l2.a).cast<double>();
|
|
|
|
double denom = cross2(v1, v2);
|
|
|
|
if (fabs(denom) < EPSILON)
|
|
|
|
#if 0
|
|
|
|
// Lines are collinear. Return true if they are coincident (overlappign).
|
|
|
|
return ! (fabs(nume_a) < EPSILON && fabs(nume_b) < EPSILON);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2019-10-15 07:40:40 +00:00
|
|
|
const Vec2d v12 = (l1.a - l2.a).cast<double>();
|
|
|
|
double nume_a = cross2(v2, v12);
|
|
|
|
double nume_b = cross2(v1, v12);
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double t1 = nume_a / denom;
|
|
|
|
double t2 = nume_b / denom;
|
|
|
|
if (t1 >= 0 && t1 <= 1.0f && t2 >= 0 && t2 <= 1.0f) {
|
2016-03-19 14:33:58 +00:00
|
|
|
// Get the intersection point.
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
(*intersection) = (l1.a.cast<double>() + t1 * v1).cast<coord_t>();
|
2016-03-19 14:33:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false; // not intersecting
|
|
|
|
}
|
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
Vec3d Linef3::intersect_plane(double z) const
|
2014-12-16 00:12:37 +00:00
|
|
|
{
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
auto v = (this->b - this->a).cast<double>();
|
2018-08-17 13:53:43 +00:00
|
|
|
double t = (z - this->a(2)) / v(2);
|
2018-08-21 15:43:05 +00:00
|
|
|
return Vec3d(this->a(0) + v(0) * t, this->a(1) + v(1) * t, z);
|
2014-12-16 00:12:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|