59 long double const a[2] = {destination[0] - source[0],
60 destination[1] - source[1]};
61 long double const b[2] = {pnt[0] - source[0],
64 long double const det_2x2(a[0] * b[1] - a[1] * b[0]);
65 constexpr double eps = std::numeric_limits<double>::epsilon();
71 if (eps < std::abs(det_2x2))
75 if (a[0] * b[0] < 0.0 || a[1] * b[1] < 0.0)
79 if (a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1])
110 if (a[1] < pnt[1] && pnt[1] <= b[1])
119 if (b[1] < pnt[1] && pnt[1] <= a[1])
136 :
Polyline(ply), _aabb(ply.getPointsVec(), ply.getPolylinePointIDs())
149 for (sub_polygon_it++;
178 WARN(
"Polygon::initialise(): base polyline is not closed.");
192 std::vector<GeoLib::Point> intersections;
194 for (
auto&& seg_it : polygon)
198 intersections.push_back(s);
202 return intersections;
209 if (pnt[0] < min_aabb_pnt[0] || max_aabb_pnt[0] < pnt[0] ||
210 pnt[1] < min_aabb_pnt[1] || max_aabb_pnt[1] < pnt[1])
217 std::size_t n_intersections(0);
219 for (std::size_t k(0); k < n_nodes; k++)
221 if (((*(
getPoint(k)))[1] <= pnt[1] &&
222 pnt[1] <= (*(
getPoint(k + 1)))[1]) ||
223 ((*(
getPoint(k + 1)))[1] <= pnt[1] &&
241 if (n_intersections % 2 == 1)
252 if ((*it)->isPntInPolygon(pnt))
273 const double tol(std::numeric_limits<float>::epsilon());
279 if (sqr_dist_as < tol)
285 if (sqr_dist_bs < tol)
293 std::sort(s.begin(), s.end(),
295 { return MathLib::sqrDist(a, p0) < MathLib::sqrDist(a, p1); });
298 for (std::size_t k(0); k < s.size() - 1;)
302 s.erase(s.begin() + k + 1);
312 0.5 * (a[1] + s[0][1]),
313 0.5 * (a[2] + s[0][2]))))
317 const std::size_t n_sub_segs(s.size() - 1);
318 for (std::size_t k(0); k < n_sub_segs; k++)
321 0.5 * (s[k][1] + s[k + 1][1]),
322 0.5 * (s[k][2] + s[k + 1][2]))))
328 0.5 * (s[0][1] + b[1]),
329 0.5 * (s[0][2] + b[2])));
334 return std::all_of(ply.
begin(), ply.
end(),
335 [
this](
auto const& segment)
336 { return containsSegment(segment); });
343 for (std::size_t k(0); k < ply_size; k++)
351 auto polygon_segment_intersects_line = [&](
auto const& polygon_seg)
354 return std::any_of(ply.
begin(), ply.
end(),
355 [&polygon_seg, &s](
auto const& polyline_seg) {
356 return GeoLib::lineSegmentIntersect(
357 polyline_seg, polygon_seg, s);
361 return std::any_of(std::cbegin(*
this), std::cend(*
this),
362 polygon_segment_intersects_line);
367 std::size_t& seg_num)
const
371 for (
auto seg_it(
begin() + seg_num); seg_it !=
end(); ++seg_it)
375 seg_num = seg_it.getSegmentNumber();
384 for (
auto seg_it(polygon->begin()); seg_it != polygon->end();
390 seg_num = seg_it.getSegmentNumber();
404 std::vector<GeoLib::Point*> tmp_polygon_pnts;
405 for (std::size_t k(0); k < n_pnts; k++)
413 for (
auto& tmp_polygon_pnt : tmp_polygon_pnts)
415 (*tmp_polygon_pnt)[2] =
420 std::size_t min_x_max_y_idx(0);
421 for (std::size_t k(0); k < n_pnts; k++)
423 if ((*(tmp_polygon_pnts[k]))[0] <=
424 (*(tmp_polygon_pnts[min_x_max_y_idx]))[0])
426 if ((*(tmp_polygon_pnts[k]))[0] <
427 (*(tmp_polygon_pnts[min_x_max_y_idx]))[0])
431 else if ((*(tmp_polygon_pnts[k]))[1] >
432 (*(tmp_polygon_pnts[min_x_max_y_idx]))[1])
440 if (0 < min_x_max_y_idx && min_x_max_y_idx < n_pnts - 2)
443 *tmp_polygon_pnts[min_x_max_y_idx],
444 *tmp_polygon_pnts[min_x_max_y_idx + 1]);
448 if (0 == min_x_max_y_idx)
451 *tmp_polygon_pnts[0],
452 *tmp_polygon_pnts[1]);
457 *tmp_polygon_pnts[n_pnts - 1],
458 *tmp_polygon_pnts[0]);
467 for (std::size_t k(0); k < n_pnts; k++)
469 delete tmp_polygon_pnts[k];
474 const std::list<Polygon*>::const_iterator& polygon_it)
488 std::size_t
const intersection_pnt_id(
_ply_pnts.size());
489 const_cast<std::vector<Point*>&
>(
_ply_pnts).push_back(
495 std::swap(idx0, idx1);
499 for (std::size_t k(0); k <= idx0; k++)
501 polyline0.addPoint((*polygon_it)->getPointID(k));
503 polyline0.
addPoint(intersection_pnt_id);
504 for (std::size_t k(idx1 + 1); k < (*polygon_it)->getNumberOfPoints(); k++)
506 polyline0.addPoint((*polygon_it)->getPointID(k));
510 polyline1.
addPoint(intersection_pnt_id);
511 for (std::size_t k(idx0 + 1); k <= idx1; k++)
513 polyline1.addPoint((*polygon_it)->getPointID(k));
515 polyline1.addPoint(intersection_pnt_id);
518 if (*polygon_it !=
this)
533 const std::list<GeoLib::Polygon*>::iterator& polygon_it)
535 std::size_t
const n((*polygon_it)->getNumberOfPoints() - 1);
536 std::vector<std::size_t> id_vec(n);
537 std::vector<std::size_t> perm(n);
538 for (std::size_t k(0); k < n; k++)
540 id_vec[k] = (*polygon_it)->getPointID(k);
546 for (std::size_t k(0); k < n - 1; k++)
548 if (id_vec[k] == id_vec[k + 1])
550 std::size_t idx0 = perm[k];
551 std::size_t idx1 = perm[k + 1];
555 std::swap(idx0, idx1);
560 for (std::size_t j(0); j <= idx0; j++)
562 polyline0.addPoint((*polygon_it)->getPointID(j));
564 for (std::size_t j(idx1 + 1);
565 j < (*polygon_it)->getNumberOfPoints();
568 polyline0.addPoint((*polygon_it)->getPointID(j));
572 for (std::size_t j(idx0); j <= idx1; j++)
574 polyline1.addPoint((*polygon_it)->getPointID(j));
578 if (*polygon_it !=
this)
586 polygon1_it,
new Polygon(polyline0));
604 const std::size_t start_pnt(lhs.
getPointID(0));
609 for (; k < n - 1 && nfound; k++)
628 for (k = 1; k < n - 1; k++)
642 std::size_t j(k + 2);
643 for (; j < n - 1; j++)
651 for (; j < k + 1; j++)
664 "operator==(Polygon const& lhs, Polygon const& rhs) - not tested case "
665 "(implementation is probably buggy) - please contact "
666 "thomas.fischer@ufz.de mentioning the problem.");
670 std::size_t j(k - 2);
681 for (; j > k - 1; j--)
700 polygon->initialise();
Definition of analytical geometry functions.
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition of the Polygon class.
MinMaxPoints getMinMaxPoints() const
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()
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
SegmentIterator end() 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)
Definition of the quicksort function.