49 long double const a[2] = {destination[0] - source[0],
50 destination[1] - source[1]};
51 long double const b[2] = {pnt[0] - source[0],
54 long double const det_2x2(a[0] * b[1] - a[1] * b[0]);
55 constexpr double eps = std::numeric_limits<double>::epsilon();
61 if (eps < std::abs(det_2x2))
65 if (a[0] * b[0] < 0.0 || a[1] * b[1] < 0.0)
69 if (a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1])
100 if (a[1] < pnt[1] && pnt[1] <= b[1])
109 if (b[1] < pnt[1] && pnt[1] <= a[1])
139 for (sub_polygon_it++;
168 WARN(
"Polygon::initialise(): base polyline is not closed.");
182 std::vector<GeoLib::Point> intersections;
184 for (
auto&& seg_it : polygon)
188 intersections.push_back(s);
192 return intersections;
197 auto const [min_aabb_pnt, max_aabb_pnt] =
_aabb.getMinMaxPoints();
199 if (pnt[0] < min_aabb_pnt[0] || max_aabb_pnt[0] < pnt[0] ||
200 pnt[1] < min_aabb_pnt[1] || max_aabb_pnt[1] < pnt[1])
207 std::size_t n_intersections(0);
209 for (std::size_t k(0); k < n_nodes; k++)
211 if (((*(
getPoint(k)))[1] <= pnt[1] &&
212 pnt[1] <= (*(
getPoint(k + 1)))[1]) ||
213 ((*(
getPoint(k + 1)))[1] <= pnt[1] &&
231 if (n_intersections % 2 == 1)
242 if ((*it)->isPntInPolygon(pnt))
263 const double tol(std::numeric_limits<float>::epsilon());
269 if (sqr_dist_as < tol)
275 if (sqr_dist_bs < tol)
283 std::sort(s.begin(), s.end(),
285 { return MathLib::sqrDist(a, p0) < MathLib::sqrDist(a, p1); });
288 for (std::size_t k(0); k < s.size() - 1;)
292 s.erase(s.begin() + k + 1);
302 0.5 * (a[1] + s[0][1]),
303 0.5 * (a[2] + s[0][2]))))
307 const std::size_t n_sub_segs(s.size() - 1);
308 for (std::size_t k(0); k < n_sub_segs; k++)
311 0.5 * (s[k][1] + s[k + 1][1]),
312 0.5 * (s[k][2] + s[k + 1][2]))))
318 0.5 * (s[0][1] + b[1]),
319 0.5 * (s[0][2] + b[2])));
324 return std::all_of(ply.
begin(), ply.
end(),
325 [
this](
auto const& segment)
326 { return containsSegment(segment); });
333 for (std::size_t k(0); k < ply_size; k++)
341 auto polygon_segment_intersects_line = [&](
auto const& polygon_seg)
344 return std::any_of(ply.
begin(), ply.
end(),
345 [&polygon_seg, &s](
auto const& polyline_seg) {
346 return GeoLib::lineSegmentIntersect(
347 polyline_seg, polygon_seg, s);
351 return std::any_of(std::cbegin(*
this), std::cend(*
this),
352 polygon_segment_intersects_line);
357 std::size_t& seg_num)
const
361 for (
auto seg_it(std::next(
begin(), seg_num)); seg_it !=
end();
366 seg_num = seg_it.getSegmentNumber();
375 for (
auto seg_it(polygon->begin()); seg_it != polygon->end();
381 seg_num = seg_it.getSegmentNumber();
395 std::vector<GeoLib::Point*> tmp_polygon_pnts;
396 for (std::size_t k(0); k < n_pnts; k++)
404 for (
auto& tmp_polygon_pnt : tmp_polygon_pnts)
406 (*tmp_polygon_pnt)[2] =
411 std::size_t min_x_max_y_idx(0);
412 for (std::size_t k(0); k < n_pnts; k++)
414 if ((*(tmp_polygon_pnts[k]))[0] <=
415 (*(tmp_polygon_pnts[min_x_max_y_idx]))[0])
417 if ((*(tmp_polygon_pnts[k]))[0] <
418 (*(tmp_polygon_pnts[min_x_max_y_idx]))[0])
422 else if ((*(tmp_polygon_pnts[k]))[1] >
423 (*(tmp_polygon_pnts[min_x_max_y_idx]))[1])
431 if (0 < min_x_max_y_idx && min_x_max_y_idx < n_pnts - 2)
434 *tmp_polygon_pnts[min_x_max_y_idx],
435 *tmp_polygon_pnts[min_x_max_y_idx + 1]);
439 if (0 == min_x_max_y_idx)
442 *tmp_polygon_pnts[0],
443 *tmp_polygon_pnts[1]);
448 *tmp_polygon_pnts[n_pnts - 1],
449 *tmp_polygon_pnts[0]);
458 for (std::size_t k(0); k < n_pnts; k++)
460 delete tmp_polygon_pnts[k];
465 const std::list<Polygon*>::const_iterator& polygon_it)
479 std::size_t
const intersection_pnt_id(
_ply_pnts.size());
480 const_cast<std::vector<Point*>&
>(
_ply_pnts).push_back(
486 std::swap(idx0, idx1);
490 for (std::size_t k(0); k <= idx0; k++)
492 polyline0.
addPoint((*polygon_it)->getPointID(k));
494 polyline0.
addPoint(intersection_pnt_id);
495 for (std::size_t k(idx1 + 1); k < (*polygon_it)->getNumberOfPoints(); k++)
497 polyline0.
addPoint((*polygon_it)->getPointID(k));
501 polyline1.
addPoint(intersection_pnt_id);
502 for (std::size_t k(idx0 + 1); k <= idx1; k++)
504 polyline1.
addPoint((*polygon_it)->getPointID(k));
506 polyline1.
addPoint(intersection_pnt_id);
509 if (*polygon_it !=
this)
524 const std::list<GeoLib::Polygon*>::iterator& polygon_it)
526 std::size_t
const n((*polygon_it)->getNumberOfPoints() - 1);
527 std::vector<std::size_t> id_vec(n);
528 std::vector<std::size_t> perm(n);
529 for (std::size_t k(0); k < n; k++)
531 id_vec[k] = (*polygon_it)->getPointID(k);
537 for (std::size_t k(0); k < n - 1; k++)
539 if (id_vec[k] == id_vec[k + 1])
541 std::size_t idx0 = perm[k];
542 std::size_t idx1 = perm[k + 1];
546 std::swap(idx0, idx1);
551 for (std::size_t j(0); j <= idx0; j++)
553 polyline0.
addPoint((*polygon_it)->getPointID(j));
555 for (std::size_t j(idx1 + 1);
556 j < (*polygon_it)->getNumberOfPoints();
559 polyline0.
addPoint((*polygon_it)->getPointID(j));
563 for (std::size_t j(idx0); j <= idx1; j++)
565 polyline1.
addPoint((*polygon_it)->getPointID(j));
569 if (*polygon_it !=
this)
577 polygon1_it,
new Polygon(polyline0));
595 const std::size_t start_pnt(lhs.
getPointID(0));
600 for (; k < n - 1 && nfound; k++)
619 for (k = 1; k < n - 1; k++)
633 std::size_t j(k + 2);
634 for (; j < n - 1; j++)
642 for (; j < k + 1; j++)
655 "operator==(Polygon const& lhs, Polygon const& rhs) - not tested case "
656 "(implementation is probably buggy) - please contact "
657 "thomas.fischer@ufz.de mentioning the problem.");
661 std::size_t j(k - 2);
672 for (; j > k - 1; j--)
691 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)