IntersectionPoints.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. ///|/ Copyright (c) Prusa Research 2023 Vojtěch Bubník @bubnikv
  2. ///|/
  3. ///|/ PrusaSlicer is released under the terms of the AGPLv3 or higher
  4. ///|/
  5. #include "IntersectionPoints.hpp"
  6. #include <libslic3r/AABBTreeLines.hpp>
  7. //NOTE: using CGAL SweepLines is slower !!! (example in git history)
  8. namespace {
  9. using namespace Slic3r;
  10. IntersectionsLines compute_intersections(const Lines &lines)
  11. {
  12. if (lines.size() < 3)
  13. return {};
  14. auto tree = AABBTreeLines::build_aabb_tree_over_indexed_lines(lines);
  15. IntersectionsLines result;
  16. for (uint32_t li = 0; li < lines.size()-1; ++li) {
  17. const Line &l = lines[li];
  18. auto intersections = AABBTreeLines::get_intersections_with_line<false, Point, Line>(lines, tree, l);
  19. for (const auto &[p, node_index] : intersections) {
  20. if (node_index - 1 <= li)
  21. continue;
  22. if (const Line &l_ = lines[node_index];
  23. l_.a == l.a ||
  24. l_.a == l.b ||
  25. l_.b == l.a ||
  26. l_.b == l.b )
  27. // it is duplicit point not intersection
  28. continue;
  29. // NOTE: fix AABBTree to compute intersection with double preccission!!
  30. Vec2d intersection_point = p.cast<double>();
  31. result.push_back(IntersectionLines{li, static_cast<uint32_t>(node_index), intersection_point});
  32. }
  33. }
  34. return result;
  35. }
  36. } // namespace
  37. namespace Slic3r {
  38. IntersectionsLines get_intersections(const Lines &lines) { return compute_intersections(lines); }
  39. IntersectionsLines get_intersections(const Polygon &polygon) { return compute_intersections(to_lines(polygon)); }
  40. IntersectionsLines get_intersections(const Polygons &polygons) { return compute_intersections(to_lines(polygons)); }
  41. IntersectionsLines get_intersections(const ExPolygon &expolygon) { return compute_intersections(to_lines(expolygon)); }
  42. IntersectionsLines get_intersections(const ExPolygons &expolygons) { return compute_intersections(to_lines(expolygons)); }
  43. }