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()));
59 double const orientation =
72bool parallel(Eigen::Vector3d v, Eigen::Vector3d w)
74 const double eps(std::numeric_limits<double>::epsilon());
75 double const eps_squared = eps * eps;
78 if (v.squaredNorm() < eps_squared)
83 if (w.squaredNorm() < eps_squared)
92 if (std::abs(v[0] - w[0]) > eps)
96 if (std::abs(v[1] - w[1]) > eps)
100 if (std::abs(v[2] - w[2]) > eps)
111 if (std::abs(v[0] - w[0]) > eps)
115 if (std::abs(v[1] - w[1]) > eps)
119 if (std::abs(v[2] - w[2]) > eps)
137 if (!isCoplanar(pa, pb, pc, pd))
147 Eigen::Vector3d
const v = b - a;
148 Eigen::Vector3d
const w = d - c;
149 Eigen::Vector3d
const qp = c - a;
150 Eigen::Vector3d
const pq = a - c;
152 double const eps = std::numeric_limits<double>::epsilon();
153 double const squared_eps = eps * eps;
155 if (qp.squaredNorm() < squared_eps || (d - a).squaredNorm() < squared_eps)
160 if ((c - b).squaredNorm() < squared_eps ||
161 (d - b).squaredNorm() < squared_eps)
167 auto isLineSegmentIntersectingAB =
168 [&v](Eigen::Vector3d
const& ap, std::size_t i)
171 return 0.0 <= ap[i] / v[i] && ap[i] / v[i] <= 1.0;
190 std::size_t i_max(std::abs(v[0]) <= std::abs(v[1]) ? 1 : 0);
191 i_max = std::abs(v[i_max]) <= std::abs(v[2]) ? 2 : i_max;
192 if (isLineSegmentIntersectingAB(qp, i_max))
197 Eigen::Vector3d
const ad = d - a;
198 if (isLineSegmentIntersectingAB(ad, i_max))
209 const double sqr_len_v(v.squaredNorm());
210 const double sqr_len_w(w.squaredNorm());
213 mat(0, 0) = sqr_len_v;
214 mat(0, 1) = -v.dot(w);
215 mat(1, 1) = sqr_len_w;
216 mat(1, 0) = mat(0, 1);
218 Eigen::Vector2d rhs{v.dot(qp), w.dot(pq)};
220 rhs = mat.partialPivLu().solve(rhs);
224 const double l(-1.0 * std::numeric_limits<float>::epsilon());
226 const double u(1.0 + std::numeric_limits<float>::epsilon());
227 if (rhs[0] < l || u < rhs[0] || rhs[1] < l || u < rhs[1])
233 GeoLib::Point const p0(a[0] + rhs[0] * v[0], a[1] + rhs[0] * v[1],
234 a[2] + rhs[0] * v[2]);
235 GeoLib::Point const p1(c[0] + rhs[1] * w[0], c[1] + rhs[1] * w[1],
236 c[2] + rhs[1] * w[2]);
239 double const min_seg_len(
240 std::min(std::sqrt(sqr_len_v), std::sqrt(sqr_len_w)));
241 if (min_dist < min_seg_len * 1e-6)
243 s[0] = 0.5 * (p0[0] + p1[0]);
244 s[1] = 0.5 * (p0[1] + p1[1]);
245 s[2] = 0.5 * (p0[2] + p1[2]);
260 for (seg_it0 = ply->
begin(); seg_it0 != ply->
end() - 2; ++seg_it0)
262 seg_it1 = seg_it0 + 2;
264 for (; seg_it1 != ply->
end(); ++seg_it1)
281 std::vector<GeoLib::Point*>& pnts)
288 Eigen::Matrix3d rot_mat = Eigen::Matrix3d::Zero();
290 if (n[0] == 0 && n[1] == 0)
303 rot_mat(0, 0) = -1.0;
304 rot_mat(2, 2) = -1.0;
311 double const h0(std::sqrt(n[0] * n[0] + n[2] * n[2]));
317 if (h0 < std::numeric_limits<double>::epsilon())
322 rot_mat(1, 2) = -1.0;
328 rot_mat(2, 1) = -1.0;
333 double const h1(1 / n.norm());
336 rot_mat(0, 0) = n[2] / h0;
338 rot_mat(0, 2) = -n[0] / h0;
339 rot_mat(1, 0) = -n[1] * n[0] / h0 * h1;
340 rot_mat(1, 1) = h0 * h1;
341 rot_mat(1, 2) = -n[1] * n[2] / h0 * h1;
342 rot_mat(2, 0) = n[0] * h1;
343 rot_mat(2, 1) = n[1] * h1;
344 rot_mat(2, 2) = n[2] * h1;
351 return rotatePointsToXY(pnts.begin(), pnts.end(), pnts.begin(), pnts.end());
364 double u = pq.cross(pc).dot(pb);
369 double v = pq.cross(pa).dot(pc);
374 double w = pq.cross(pb).dot(pa);
380 const double denom(1.0 / (u + v + w));
384 return std::make_unique<GeoLib::Point>(u * a[0] + v * b[0] + w * c[0],
385 u * a[1] + v * b[1] + w * c[1],
386 u * a[2] + v * b[2] + w * c[2]);
390 std::vector<GeoLib::Polyline*>& plys)
392 auto computeSegmentIntersections =
395 for (
auto seg0_it(poly0.
begin()); seg0_it != poly0.
end(); ++seg0_it)
397 for (
auto seg1_it(poly1.begin()); seg1_it != poly1.end(); ++seg1_it)
402 std::size_t
const id(
404 poly0.
insertPoint(seg0_it.getSegmentNumber() + 1,
id);
405 poly1.insertPoint(seg1_it.getSegmentNumber() + 1,
id);
411 for (
auto it0(plys.begin()); it0 != plys.end(); ++it0)
415 for (; it1 != plys.end(); ++it1)
417 computeSegmentIntersections(*(*it0), *(*it1));
422std::tuple<std::vector<GeoLib::Point*>, Eigen::Vector3d>
426 std::vector<GeoLib::Point*> polygon_points;
435 Eigen::Matrix3d
const rot_mat =
440 std::for_each(polygon_points.begin(), polygon_points.end(),
443 return {polygon_points, plane_normal};
458 if ((orient_abc > 0 && orient_abd > 0) ||
459 (orient_abc < 0 && orient_abd < 0))
461 return std::vector<MathLib::Point3d>();
465 if (orient_abc == 0.0 && orient_abd == 0.0)
467 double const eps(std::numeric_limits<double>::epsilon());
479 auto isPointOnSegment = [](
double q,
double p0,
double p1)
481 double const t((q - p0) / (p1 - p0));
482 return 0 <= t && t <= 1;
486 if (isPointOnSegment(c[0], a[0], b[0]))
489 if (isPointOnSegment(a[0], c[0], d[0]))
499 if (isPointOnSegment(b[0], c[0], d[0]))
504 if (isPointOnSegment(d[0], a[0], b[0]))
508 std::stringstream err;
509 err.precision(std::numeric_limits<double>::max_digits10);
510 err << ab <<
" x " << cd;
512 "The case of parallel line segments ({:s}) is not handled yet. "
518 if (isPointOnSegment(d[0], a[0], b[0]))
521 if (isPointOnSegment(a[0], c[0], d[0]))
531 if (isPointOnSegment(b[0], c[0], d[0]))
536 if (isPointOnSegment(c[0], a[0], b[0]))
541 std::stringstream err;
542 err.precision(std::numeric_limits<double>::max_digits10);
543 err << ab <<
" x " << cd;
545 "The case of parallel line segments ({:s}) is not handled yet. "
549 return std::vector<MathLib::Point3d>();
558 if (b[0] - a[0] != 0)
560 double const t = (c[0] - a[0]) / (b[0] - a[0]);
561 return 0.0 <= t && t <= 1.0;
563 if (b[1] - a[1] != 0)
565 double const t = (c[1] - a[1]) / (b[1] - a[1]);
566 return 0.0 <= t && t <= 1.0;
568 if (b[2] - a[2] != 0)
570 double const t = (c[2] - a[2]) / (b[2] - a[2]);
571 return 0.0 <= t && t <= 1.0;
576 if (orient_abc == 0.0)
578 if (isCollinearPointOntoLineSegment(a, b, c))
582 return std::vector<MathLib::Point3d>();
585 if (orient_abd == 0.0)
587 if (isCollinearPointOntoLineSegment(a, b, d))
591 return std::vector<MathLib::Point3d>();
597 if ((orient_cda > 0 && orient_cdb > 0) ||
598 (orient_cda < 0 && orient_cdb < 0))
600 return std::vector<MathLib::Point3d>();
607 mat(0, 0) = b[0] - a[0];
608 mat(0, 1) = c[0] - d[0];
609 mat(1, 0) = b[1] - a[1];
610 mat(1, 1) = c[1] - d[1];
611 Eigen::Vector2d rhs{c[0] - a[0], c[1] - a[1]};
613 rhs = mat.partialPivLu().solve(rhs);
614 if (0 <= rhs[1] && rhs[1] <= 1.0)
617 {c[0] + rhs[1] * (d[0] - c[0]), c[1] + rhs[1] * (d[1] - c[1]),
618 c[2] + rhs[1] * (d[2] - c[2])}}}};
620 return std::vector<MathLib::Point3d>();
625 std::vector<GeoLib::LineSegment>& sub_segments)
627 double const eps(std::numeric_limits<double>::epsilon());
629 auto findNextSegment =
631 std::vector<GeoLib::LineSegment>& sub_segments,
632 std::vector<GeoLib::LineSegment>::iterator& sub_seg_it)
634 if (sub_seg_it == sub_segments.end())
639 auto act_beg_seg_it = std::find_if(
640 sub_seg_it, sub_segments.end(),
643 return MathLib::sqrDist(seg_beg_pnt, seg.getBeginPoint()) <
645 MathLib::sqrDist(seg_beg_pnt, seg.getEndPoint()) < eps;
647 if (act_beg_seg_it == sub_segments.end())
656 std::swap(act_beg_seg_it->getBeginPoint(),
657 act_beg_seg_it->getEndPoint());
659 assert(sub_seg_it != sub_segments.end());
661 if (sub_seg_it != act_beg_seg_it)
663 std::swap(*sub_seg_it, *act_beg_seg_it);
668 auto seg_it = sub_segments.begin();
669 findNextSegment(seg_beg_pnt, sub_segments, seg_it);
671 while (seg_it != sub_segments.end())
675 if (seg_it != sub_segments.end())
677 findNextSegment(new_seg_beg_pnt, sub_segments, seg_it);
684 Eigen::Matrix3d rot_mat = Eigen::Matrix3d::Zero();
685 const double cos_theta = v[0];
686 const double sin_theta = v[1];
687 rot_mat(0, 0) = rot_mat(1, 1) = cos_theta;
688 rot_mat(0, 1) = sin_theta;
689 rot_mat(1, 0) = -sin_theta;
697 Eigen::Vector3d yy = Eigen::Vector3d::Zero();
698 auto const eps = std::numeric_limits<double>::epsilon();
699 if (std::abs(v[0]) > 0.0 && std::abs(v[1]) + std::abs(v[2]) < eps)
703 else if (std::abs(v[1]) > 0.0 && std::abs(v[0]) + std::abs(v[2]) < eps)
707 else if (std::abs(v[2]) > 0.0 && std::abs(v[0]) + std::abs(v[1]) < eps)
713 for (
unsigned i = 0; i < 3; i++)
715 if (std::abs(v[i]) > 0.0)
723 Eigen::Vector3d
const zz = v.cross(yy).normalized();
725 yy = zz.cross(v).normalized();
727 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
double getOrientation2d(MathLib::Point3d const &a, MathLib::Point3d const &b, MathLib::Point3d const &c)
double getOrientation2dFast(MathLib::Point3d const &a, MathLib::Point3d const &b, MathLib::Point3d const &c)
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 orient2dfast(double *, double *, double *)
double orient2d(double *, double *, double *)