6#include <Eigen/Geometry>
23 const_cast<double*
>(b.
data()),
24 const_cast<double*
>(c.data()));
32 const_cast<double*
>(b.
data()),
33 const_cast<double*
>(c.data()));
43 double const orientation = getOrientation2d(p0, p1, p2);
59 double const orientation = getOrientation2dFast(p0, p1, p2);
71bool parallel(Eigen::Vector3d v, Eigen::Vector3d w)
73 const double eps(std::numeric_limits<double>::epsilon());
74 double const eps_squared = eps * eps;
77 if (v.squaredNorm() < eps_squared)
82 if (w.squaredNorm() < eps_squared)
91 if (std::abs(v[0] - w[0]) > eps)
95 if (std::abs(v[1] - w[1]) > eps)
99 if (std::abs(v[2] - w[2]) > eps)
110 if (std::abs(v[0] - w[0]) > eps)
114 if (std::abs(v[1] - w[1]) > eps)
118 if (std::abs(v[2] - w[2]) > eps)
136 if (!isCoplanar(pa, pb, pc, pd))
146 Eigen::Vector3d
const v = b - a;
147 Eigen::Vector3d
const w = d - c;
148 Eigen::Vector3d
const qp = c - a;
149 Eigen::Vector3d
const pq = a - c;
151 double const eps = std::numeric_limits<double>::epsilon();
152 double const squared_eps = eps * eps;
154 if (qp.squaredNorm() < squared_eps || (d - a).squaredNorm() < squared_eps)
159 if ((c - b).squaredNorm() < squared_eps ||
160 (d - b).squaredNorm() < squared_eps)
166 auto isLineSegmentIntersectingAB =
167 [&v](Eigen::Vector3d
const& ap, std::size_t i)
170 return 0.0 <= ap[i] / v[i] && ap[i] / v[i] <= 1.0;
189 std::size_t i_max(std::abs(v[0]) <= std::abs(v[1]) ? 1 : 0);
190 i_max = std::abs(v[i_max]) <= std::abs(v[2]) ? 2 : i_max;
191 if (isLineSegmentIntersectingAB(qp, i_max))
196 Eigen::Vector3d
const ad = d - a;
197 if (isLineSegmentIntersectingAB(ad, i_max))
208 const double sqr_len_v(v.squaredNorm());
209 const double sqr_len_w(w.squaredNorm());
212 mat(0, 0) = sqr_len_v;
213 mat(0, 1) = -v.dot(w);
214 mat(1, 1) = sqr_len_w;
215 mat(1, 0) = mat(0, 1);
217 Eigen::Vector2d rhs{v.dot(qp), w.dot(pq)};
219 rhs = mat.partialPivLu().solve(rhs);
223 const double l(-1.0 * std::numeric_limits<float>::epsilon());
225 const double u(1.0 + std::numeric_limits<float>::epsilon());
226 if (rhs[0] < l || u < rhs[0] || rhs[1] < l || u < rhs[1])
232 GeoLib::Point const p0(a[0] + rhs[0] * v[0], a[1] + rhs[0] * v[1],
233 a[2] + rhs[0] * v[2]);
234 GeoLib::Point const p1(c[0] + rhs[1] * w[0], c[1] + rhs[1] * w[1],
235 c[2] + rhs[1] * w[2]);
238 double const min_seg_len(
239 std::min(std::sqrt(sqr_len_v), std::sqrt(sqr_len_w)));
240 if (min_dist < min_seg_len * 1e-6)
242 s[0] = 0.5 * (p0[0] + p1[0]);
243 s[1] = 0.5 * (p0[1] + p1[1]);
244 s[2] = 0.5 * (p0[2] + p1[2]);
259 for (seg_it0 = ply->
begin(); seg_it0 != ply->
end() - 2; ++seg_it0)
261 seg_it1 = seg_it0 + 2;
263 for (; seg_it1 != ply->
end(); ++seg_it1)
280 std::vector<GeoLib::Point*>& pnts)
287 Eigen::Matrix3d rot_mat = Eigen::Matrix3d::Zero();
289 if (n[0] == 0 && n[1] == 0)
302 rot_mat(0, 0) = -1.0;
303 rot_mat(2, 2) = -1.0;
310 double const h0(std::sqrt(n[0] * n[0] + n[2] * n[2]));
316 if (h0 < std::numeric_limits<double>::epsilon())
321 rot_mat(1, 2) = -1.0;
327 rot_mat(2, 1) = -1.0;
332 double const h1(1 / n.norm());
335 rot_mat(0, 0) = n[2] / h0;
337 rot_mat(0, 2) = -n[0] / h0;
338 rot_mat(1, 0) = -n[1] * n[0] / h0 * h1;
339 rot_mat(1, 1) = h0 * h1;
340 rot_mat(1, 2) = -n[1] * n[2] / h0 * h1;
341 rot_mat(2, 0) = n[0] * h1;
342 rot_mat(2, 1) = n[1] * h1;
343 rot_mat(2, 2) = n[2] * h1;
350 return rotatePointsToXY(pnts.begin(), pnts.end(), pnts.begin(), pnts.end());
363 double u = pq.cross(pc).dot(pb);
368 double v = pq.cross(pa).dot(pc);
373 double w = pq.cross(pb).dot(pa);
379 const double denom(1.0 / (u + v + w));
383 return std::make_unique<GeoLib::Point>(u * a[0] + v * b[0] + w * c[0],
384 u * a[1] + v * b[1] + w * c[1],
385 u * a[2] + v * b[2] + w * c[2]);
389 std::vector<GeoLib::Polyline*>& plys)
391 auto computeSegmentIntersections =
394 for (
auto seg0_it(poly0.
begin()); seg0_it != poly0.
end(); ++seg0_it)
396 for (
auto seg1_it(poly1.begin()); seg1_it != poly1.end(); ++seg1_it)
401 std::size_t
const id(
403 poly0.
insertPoint(seg0_it.getSegmentNumber() + 1,
id);
404 poly1.insertPoint(seg1_it.getSegmentNumber() + 1,
id);
410 for (
auto it0(plys.begin()); it0 != plys.end(); ++it0)
414 for (; it1 != plys.end(); ++it1)
416 computeSegmentIntersections(*(*it0), *(*it1));
421std::tuple<std::vector<GeoLib::Point*>, Eigen::Vector3d>
425 std::vector<GeoLib::Point*> polygon_points;
434 Eigen::Matrix3d
const rot_mat =
439 std::for_each(polygon_points.begin(), polygon_points.end(),
442 return {polygon_points, plane_normal};
457 if ((orient_abc > 0 && orient_abd > 0) ||
458 (orient_abc < 0 && orient_abd < 0))
460 return std::vector<MathLib::Point3d>();
464 if (orient_abc == 0.0 && orient_abd == 0.0)
466 double const eps(std::numeric_limits<double>::epsilon());
478 auto isPointOnSegment = [](
double q,
double p0,
double p1)
480 double const t((q - p0) / (p1 - p0));
481 return 0 <= t && t <= 1;
485 if (isPointOnSegment(c[0], a[0], b[0]))
488 if (isPointOnSegment(a[0], c[0], d[0]))
498 if (isPointOnSegment(b[0], c[0], d[0]))
503 if (isPointOnSegment(d[0], a[0], b[0]))
507 std::stringstream err;
508 err.precision(std::numeric_limits<double>::max_digits10);
509 err << ab <<
" x " << cd;
511 "The case of parallel line segments ({:s}) is not handled yet. "
517 if (isPointOnSegment(d[0], a[0], b[0]))
520 if (isPointOnSegment(a[0], c[0], d[0]))
530 if (isPointOnSegment(b[0], c[0], d[0]))
535 if (isPointOnSegment(c[0], a[0], b[0]))
540 std::stringstream err;
541 err.precision(std::numeric_limits<double>::max_digits10);
542 err << ab <<
" x " << cd;
544 "The case of parallel line segments ({:s}) is not handled yet. "
548 return std::vector<MathLib::Point3d>();
557 if (b[0] - a[0] != 0)
559 double const t = (c[0] - a[0]) / (b[0] - a[0]);
560 return 0.0 <= t && t <= 1.0;
562 if (b[1] - a[1] != 0)
564 double const t = (c[1] - a[1]) / (b[1] - a[1]);
565 return 0.0 <= t && t <= 1.0;
567 if (b[2] - a[2] != 0)
569 double const t = (c[2] - a[2]) / (b[2] - a[2]);
570 return 0.0 <= t && t <= 1.0;
575 if (orient_abc == 0.0)
577 if (isCollinearPointOntoLineSegment(a, b, c))
581 return std::vector<MathLib::Point3d>();
584 if (orient_abd == 0.0)
586 if (isCollinearPointOntoLineSegment(a, b, d))
590 return std::vector<MathLib::Point3d>();
596 if ((orient_cda > 0 && orient_cdb > 0) ||
597 (orient_cda < 0 && orient_cdb < 0))
599 return std::vector<MathLib::Point3d>();
606 mat(0, 0) = b[0] - a[0];
607 mat(0, 1) = c[0] - d[0];
608 mat(1, 0) = b[1] - a[1];
609 mat(1, 1) = c[1] - d[1];
610 Eigen::Vector2d rhs{c[0] - a[0], c[1] - a[1]};
612 rhs = mat.partialPivLu().solve(rhs);
613 if (0 <= rhs[1] && rhs[1] <= 1.0)
616 {c[0] + rhs[1] * (d[0] - c[0]), c[1] + rhs[1] * (d[1] - c[1]),
617 c[2] + rhs[1] * (d[2] - c[2])}}}};
619 return std::vector<MathLib::Point3d>();
624 std::vector<GeoLib::LineSegment>& sub_segments)
626 double const eps(std::numeric_limits<double>::epsilon());
628 auto findNextSegment =
630 std::vector<GeoLib::LineSegment>& sub_segments,
631 std::vector<GeoLib::LineSegment>::iterator& sub_seg_it)
633 if (sub_seg_it == sub_segments.end())
638 auto act_beg_seg_it = std::find_if(
639 sub_seg_it, sub_segments.end(),
642 return MathLib::sqrDist(seg_beg_pnt, seg.getBeginPoint()) <
644 MathLib::sqrDist(seg_beg_pnt, seg.getEndPoint()) < eps;
646 if (act_beg_seg_it == sub_segments.end())
655 std::swap(act_beg_seg_it->getBeginPoint(),
656 act_beg_seg_it->getEndPoint());
658 assert(sub_seg_it != sub_segments.end());
660 if (sub_seg_it != act_beg_seg_it)
662 std::swap(*sub_seg_it, *act_beg_seg_it);
667 auto seg_it = sub_segments.begin();
668 findNextSegment(seg_beg_pnt, sub_segments, seg_it);
670 while (seg_it != sub_segments.end())
674 if (seg_it != sub_segments.end())
676 findNextSegment(new_seg_beg_pnt, sub_segments, seg_it);
683 Eigen::Matrix3d rot_mat = Eigen::Matrix3d::Zero();
684 const double cos_theta = v[0];
685 const double sin_theta = v[1];
686 rot_mat(0, 0) = rot_mat(1, 1) = cos_theta;
687 rot_mat(0, 1) = sin_theta;
688 rot_mat(1, 0) = -sin_theta;
696 Eigen::Vector3d yy = Eigen::Vector3d::Zero();
697 auto const eps = std::numeric_limits<double>::epsilon();
698 if (std::abs(v[0]) > 0.0 && std::abs(v[1]) + std::abs(v[2]) < eps)
702 else if (std::abs(v[1]) > 0.0 && std::abs(v[0]) + std::abs(v[2]) < eps)
706 else if (std::abs(v[2]) > 0.0 && std::abs(v[0]) + std::abs(v[1]) < eps)
712 for (
unsigned i = 0; i < 3; i++)
714 if (std::abs(v[i]) > 0.0)
722 Eigen::Vector3d
const zz = v.cross(yy).normalized();
724 yy = zz.cross(v).normalized();
726 Eigen::Matrix3d rot_mat;
GeoLib::Point const & getBeginPoint() const
GeoLib::Point const & getEndPoint() const
This class manages pointers to Points in a std::vector along with a name. It also handles the deletio...
std::size_t push_back(Point *pnt)
std::size_t getSegmentNumber() const
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
std::size_t getNumberOfSegments() const
std::size_t getNumberOfPoints() const
const Point * getPoint(std::size_t i) const
returns the i-th point contained in the polyline
virtual bool insertPoint(std::size_t pos, std::size_t pnt_id)
SegmentIterator begin() const
SegmentIterator end() const
Eigen::Vector3d const & asEigenVector3d() const
const double * data() const
Orientation getOrientationFast(MathLib::Point3d const &p0, MathLib::Point3d const &p1, MathLib::Point3d const &p2)
Eigen::Matrix3d compute3DRotationMatrixToX(Eigen::Vector3d const &v)
bool lineSegmentsIntersect(const GeoLib::Polyline *ply, GeoLib::Polyline::SegmentIterator &seg_it0, GeoLib::Polyline::SegmentIterator &seg_it1, GeoLib::Point &intersection_pnt)
void computeAndInsertAllIntersectionPoints(GeoLib::PointVec &pnt_vec, std::vector< GeoLib::Polyline * > &plys)
void rotatePoints(Eigen::Matrix3d const &rot_mat, InputIterator pnts_begin, InputIterator pnts_end)
bool parallel(Eigen::Vector3d v, Eigen::Vector3d w)
std::tuple< std::vector< GeoLib::Point * >, Eigen::Vector3d > rotatePolygonPointsToXY(GeoLib::Polygon const &polygon_in)
Eigen::Matrix3d computeRotationMatrixToXY(Eigen::Vector3d const &n)
Eigen::Matrix3d rotatePointsToXY(InputIterator1 p_pnts_begin, InputIterator1 p_pnts_end, InputIterator2 r_pnts_begin, InputIterator2 r_pnts_end)
void sortSegments(MathLib::Point3d const &seg_beg_pnt, std::vector< GeoLib::LineSegment > &sub_segments)
std::vector< MathLib::Point3d > lineSegmentIntersect2d(GeoLib::LineSegment const &ab, GeoLib::LineSegment const &cd)
std::unique_ptr< GeoLib::Point > triangleLineIntersection(MathLib::Point3d const &a, MathLib::Point3d const &b, MathLib::Point3d const &c, MathLib::Point3d const &p, MathLib::Point3d const &q)
std::pair< Eigen::Vector3d, double > getNewellPlane(InputIterator pnts_begin, InputIterator pnts_end)
Eigen::Matrix3d compute2DRotationMatrixToX(Eigen::Vector3d const &v)
bool lineSegmentIntersect(GeoLib::LineSegment const &s0, GeoLib::LineSegment const &s1, GeoLib::Point &s)
Orientation getOrientation(MathLib::Point3d const &p0, MathLib::Point3d const &p1, MathLib::Point3d const &p2)
double sqrDist2d(MathLib::Point3d const &p0, MathLib::Point3d const &p1)
double sqrDist(MathLib::Point3d const &p0, MathLib::Point3d const &p1)
double getOrientation2dFast(MathLib::Point3d const &a, MathLib::Point3d const &b, MathLib::Point3d const &c)
double getOrientation2d(MathLib::Point3d const &a, MathLib::Point3d const &b, MathLib::Point3d const &c)
double orient2dfast(double *, double *, double *)
double orient2d(double *, double *, double *)