48 long double const a[2] = {destination[0] - source[0],
49 destination[1] - source[1]};
50 long double const b[2] = {pnt[0] - source[0],
53 long double const det_2x2(a[0] * b[1] - a[1] * b[0]);
54 constexpr double eps = std::numeric_limits<double>::epsilon();
60 if (eps < std::abs(det_2x2))
64 if (a[0] * b[0] < 0.0 || a[1] * b[1] < 0.0)
68 if (a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1])
99 if (a[1] < pnt[1] && pnt[1] <= b[1])
108 if (b[1] < pnt[1] && pnt[1] <= a[1])
138 for (sub_polygon_it++;
167 WARN(
"Polygon::initialise(): base polyline is not closed.");
181 std::vector<GeoLib::Point> intersections;
183 for (
auto&& seg_it : polygon)
187 intersections.push_back(s);
191 return intersections;
196 auto const [min_aabb_pnt, max_aabb_pnt] =
_aabb.getMinMaxPoints();
198 if (pnt[0] < min_aabb_pnt[0] || max_aabb_pnt[0] < pnt[0] ||
199 pnt[1] < min_aabb_pnt[1] || max_aabb_pnt[1] < pnt[1])
206 std::size_t n_intersections(0);
208 for (std::size_t k(0); k < n_nodes; k++)
210 if (((*(
getPoint(k)))[1] <= pnt[1] &&
211 pnt[1] <= (*(
getPoint(k + 1)))[1]) ||
212 ((*(
getPoint(k + 1)))[1] <= pnt[1] &&
230 if (n_intersections % 2 == 1)
241 if ((*it)->isPntInPolygon(pnt))
262 const double tol(std::numeric_limits<float>::epsilon());
268 if (sqr_dist_as < tol)
274 if (sqr_dist_bs < tol)
282 std::sort(s.begin(), s.end(),
284 { return MathLib::sqrDist(a, p0) < MathLib::sqrDist(a, p1); });
287 for (std::size_t k(0); k < s.size() - 1;)
291 s.erase(s.begin() + k + 1);
301 0.5 * (a[1] + s[0][1]),
302 0.5 * (a[2] + s[0][2]))))
306 const std::size_t n_sub_segs(s.size() - 1);
307 for (std::size_t k(0); k < n_sub_segs; k++)
310 0.5 * (s[k][1] + s[k + 1][1]),
311 0.5 * (s[k][2] + s[k + 1][2]))))
317 0.5 * (s[0][1] + b[1]),
318 0.5 * (s[0][2] + b[2])));
323 return std::all_of(ply.
begin(), ply.
end(),
324 [
this](
auto const& segment)
325 { return containsSegment(segment); });
332 for (std::size_t k(0); k < ply_size; k++)
340 auto polygon_segment_intersects_line = [&](
auto const& polygon_seg)
343 return std::any_of(ply.
begin(), ply.
end(),
344 [&polygon_seg, &s](
auto const& polyline_seg) {
345 return GeoLib::lineSegmentIntersect(
346 polyline_seg, polygon_seg, s);
350 return std::any_of(std::cbegin(*
this), std::cend(*
this),
351 polygon_segment_intersects_line);
356 std::size_t& seg_num)
const
360 for (
auto seg_it(
begin() + seg_num); seg_it !=
end(); ++seg_it)
364 seg_num = seg_it.getSegmentNumber();
373 for (
auto seg_it(polygon->begin()); seg_it != polygon->end();
379 seg_num = seg_it.getSegmentNumber();
393 std::vector<GeoLib::Point*> tmp_polygon_pnts;
394 for (std::size_t k(0); k < n_pnts; k++)
402 for (
auto& tmp_polygon_pnt : tmp_polygon_pnts)
404 (*tmp_polygon_pnt)[2] =
409 std::size_t min_x_max_y_idx(0);
410 for (std::size_t k(0); k < n_pnts; k++)
412 if ((*(tmp_polygon_pnts[k]))[0] <=
413 (*(tmp_polygon_pnts[min_x_max_y_idx]))[0])
415 if ((*(tmp_polygon_pnts[k]))[0] <
416 (*(tmp_polygon_pnts[min_x_max_y_idx]))[0])
420 else if ((*(tmp_polygon_pnts[k]))[1] >
421 (*(tmp_polygon_pnts[min_x_max_y_idx]))[1])
429 if (0 < min_x_max_y_idx && min_x_max_y_idx < n_pnts - 2)
432 *tmp_polygon_pnts[min_x_max_y_idx],
433 *tmp_polygon_pnts[min_x_max_y_idx + 1]);
437 if (0 == min_x_max_y_idx)
440 *tmp_polygon_pnts[0],
441 *tmp_polygon_pnts[1]);
446 *tmp_polygon_pnts[n_pnts - 1],
447 *tmp_polygon_pnts[0]);
456 for (std::size_t k(0); k < n_pnts; k++)
458 delete tmp_polygon_pnts[k];
463 const std::list<Polygon*>::const_iterator& polygon_it)
477 std::size_t
const intersection_pnt_id(
_ply_pnts.size());
478 const_cast<std::vector<Point*>&
>(
_ply_pnts).push_back(
484 std::swap(idx0, idx1);
488 for (std::size_t k(0); k <= idx0; k++)
490 polyline0.
addPoint((*polygon_it)->getPointID(k));
492 polyline0.
addPoint(intersection_pnt_id);
493 for (std::size_t k(idx1 + 1); k < (*polygon_it)->getNumberOfPoints(); k++)
495 polyline0.
addPoint((*polygon_it)->getPointID(k));
499 polyline1.
addPoint(intersection_pnt_id);
500 for (std::size_t k(idx0 + 1); k <= idx1; k++)
502 polyline1.
addPoint((*polygon_it)->getPointID(k));
504 polyline1.
addPoint(intersection_pnt_id);
507 if (*polygon_it !=
this)
522 const std::list<GeoLib::Polygon*>::iterator& polygon_it)
524 std::size_t
const n((*polygon_it)->getNumberOfPoints() - 1);
525 std::vector<std::size_t> id_vec(n);
526 std::vector<std::size_t> perm(n);
527 for (std::size_t k(0); k < n; k++)
529 id_vec[k] = (*polygon_it)->getPointID(k);
535 for (std::size_t k(0); k < n - 1; k++)
537 if (id_vec[k] == id_vec[k + 1])
539 std::size_t idx0 = perm[k];
540 std::size_t idx1 = perm[k + 1];
544 std::swap(idx0, idx1);
549 for (std::size_t j(0); j <= idx0; j++)
551 polyline0.
addPoint((*polygon_it)->getPointID(j));
553 for (std::size_t j(idx1 + 1);
554 j < (*polygon_it)->getNumberOfPoints();
557 polyline0.
addPoint((*polygon_it)->getPointID(j));
561 for (std::size_t j(idx0); j <= idx1; j++)
563 polyline1.
addPoint((*polygon_it)->getPointID(j));
567 if (*polygon_it !=
this)
575 polygon1_it,
new Polygon(polyline0));
593 const std::size_t start_pnt(lhs.
getPointID(0));
598 for (; k < n - 1 && nfound; k++)
617 for (k = 1; k < n - 1; k++)
631 std::size_t j(k + 2);
632 for (; j < n - 1; j++)
640 for (; j < k + 1; j++)
653 "operator==(Polygon const& lhs, Polygon const& rhs) - not tested case "
654 "(implementation is probably buggy) - please contact "
655 "thomas.fischer@ufz.de mentioning the problem.");
659 std::size_t j(k - 2);
670 for (; j > k - 1; j--)
689 polygon->initialise();
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
GeoLib::Point const & getBeginPoint() const
GeoLib::Point const & getEndPoint() const
std::list< Polygon * > const & computeListOfSimplePolygons()
bool isPolylineInPolygon(const Polyline &ply) const
void splitPolygonAtPoint(const std::list< Polygon * >::iterator &polygon_it)
bool isPntInPolygon(MathLib::Point3d const &pnt) const
void splitPolygonAtIntersection(const std::list< Polygon * >::const_iterator &polygon_it)
bool isPartOfPolylineInPolygon(const Polyline &ply) const
bool containsSegment(GeoLib::LineSegment const &segment) const
std::list< Polygon * > _simple_polygon_list
bool getNextIntersectionPointPolygonLine(GeoLib::LineSegment const &seg, GeoLib::Point &intersection_pnt, std::size_t &seg_num) const
void ensureCCWOrientation()
Polygon(const Polyline &ply, bool init=true)
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 getPointID(std::size_t const i) const
std::size_t getNumberOfPoints() const
const Point * getPoint(std::size_t i) const
returns the i-th point contained in the polyline
void reverseOrientation()
SegmentIterator begin() const
virtual bool addPoint(std::size_t pnt_id)
const std::vector< Point * > & _ply_pnts
Polyline(const std::vector< Point * > &pnt_vec)
std::vector< std::size_t > const & getPolylinePointIDs() const
SegmentIterator end() const
std::vector< Point * > const & getPointsVec() const
void quicksort(It1 first1, It1 last1, It2 first2, Comparator compare)
bool lineSegmentsIntersect(const GeoLib::Polyline *ply, GeoLib::Polyline::SegmentIterator &seg_it0, GeoLib::Polyline::SegmentIterator &seg_it1, GeoLib::Point &intersection_pnt)
std::vector< GeoLib::Point > getAllIntersectionPoints(Polygon const &polygon, GeoLib::LineSegment const &segment)
Eigen::Matrix3d rotatePointsToXY(InputIterator1 p_pnts_begin, InputIterator1 p_pnts_end, InputIterator2 r_pnts_begin, InputIterator2 r_pnts_end)
Location getLocationOfPoint(MathLib::Point3d const &source, MathLib::Point3d const &destination, MathLib::Point3d const &pnt)
bool operator==(LineSegment const &s0, LineSegment const &s1)
EdgeType getEdgeType(MathLib::Point3d const &a, MathLib::Point3d const &b, MathLib::Point3d const &pnt)
@ INESSENTIAL
INESSENTIAL.
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 sqrDist(MathLib::Point3d const &p0, MathLib::Point3d const &p1)