Using aabb tree for lines to detect closest expolygons

This commit is contained in:
Filip Sykala - NTB T15p 2022-07-22 15:18:34 +02:00
parent 8f4b799ddb
commit a0eecb91c8
8 changed files with 308 additions and 522 deletions

View file

@ -7,7 +7,6 @@ add_executable(${_TEST_NAME}_tests
test_kdtreeindirect.cpp
test_clipper_offset.cpp
test_clipper_utils.cpp
test_closest_point.cpp
test_color.cpp
test_config.cpp
test_curve_fitting.cpp

View file

@ -87,6 +87,49 @@ TEST_CASE("Creating a several 2d lines, testing closest point query", "[AABBIndi
REQUIRE(hit_point_out.y() == Approx(0.5));
}
TEST_CASE("Find the closest point from ExPolys", "[ClosestPoint]") {
//////////////////////////////
// 0 - 3
// |Ex0| 0 - 3
// | |p |Ex1|
// 1 - 2 | |
// 1 - 2
//[0,0]
///////////////////
ExPolygons ex_polys{
/*Ex0*/ {{0, 4}, {0, 1}, {2, 1}, {2, 4}},
/*Ex1*/ {{4, 3}, {4, 0}, {6, 0}, {6, 3}}
};
Vec2d p{2.5, 3.5};
std::vector<Linef> lines;
auto add_lines = [&lines](const Polygon& poly) {
for (const auto &line : poly.lines())
lines.emplace_back(
line.a.cast<double>(),
line.b.cast<double>());
};
for (const ExPolygon &ex_poly : ex_polys) {
add_lines(ex_poly.contour);
for (const Polygon &hole : ex_poly.holes)
add_lines(hole);
}
AABBTreeIndirect::Tree<2, double> tree =
AABBTreeLines::build_aabb_tree_over_indexed_lines(lines);
size_t hit_idx_out = std::numeric_limits<size_t>::max();
Vec2d hit_point_out;
double distance_sq = AABBTreeLines::squared_distance_to_indexed_lines(
lines, tree, p, hit_idx_out, hit_point_out, 0.24/* < (0.5*0.5) */);
CHECK(hit_idx_out == std::numeric_limits<size_t>::max());
distance_sq = AABBTreeLines::squared_distance_to_indexed_lines(
lines, tree, p, hit_idx_out, hit_point_out, 0.26);
CHECK(hit_idx_out != std::numeric_limits<size_t>::max());
//double distance = sqrt(distance_sq);
//const Linef &line = lines[hit_idx_out];
}
#if 0
#include "libslic3r/EdgeGrid.hpp"
#include <iostream>

View file

@ -1,81 +0,0 @@
#include <catch2/catch.hpp>
#include <libslic3r/ClosestPoint.hpp>
#include <libslic3r/Point.hpp>
using namespace Slic3r;
TEST_CASE("Find the closest point from 2", "[ClosestPoint]")
{
Points pts = {{0, 1}, {0, 2}};
CHECK(std::is_sorted(pts.begin(), pts.end(),
closestPoint::sort_fnc<Point>));
CHECK(0 == find_closest_in_sorted(Point{0, 0}, pts));
CHECK(0 == find_closest_in_sorted(Point{1, 1}, pts));
CHECK(1 == find_closest_in_sorted(Point{1, 2}, pts));
}
TEST_CASE("Find the closest point from 9", "[ClosestPoint]")
{
// 0 - 3 - 6
// | | |
// 1 - 4 - 7
// | | |
// 2 - 5 - 8
Points pts = {{-3, 3}, {-3, 0}, {-3, -3}, {0, 3}, {0, 0},
{0, -3}, {3, 3}, {3, 0}, {3, -3}};
CHECK(std::is_sorted(pts.begin(), pts.end(),
closestPoint::sort_fnc<Point>));
CHECK(0 == find_closest_in_sorted(Point{-4, 4}, pts));
CHECK(0 == find_closest_in_sorted(Point{-2, 2}, pts));
// check center
CHECK(4 == find_closest_in_sorted(Point{-1, 1}, pts));
CHECK(4 == find_closest_in_sorted(Point{ 0, 1}, pts));
CHECK(4 == find_closest_in_sorted(Point{ 1, 1}, pts));
CHECK(4 == find_closest_in_sorted(Point{-1, 0}, pts));
CHECK(4 == find_closest_in_sorted(Point{ 0, 0}, pts));
CHECK(4 == find_closest_in_sorted(Point{ 1, 0}, pts));
CHECK(4 == find_closest_in_sorted(Point{-1,-1}, pts));
CHECK(4 == find_closest_in_sorted(Point{ 0,-1}, pts));
CHECK(4 == find_closest_in_sorted(Point{ 1,-1}, pts));
CHECK(4 == find_closest_in_sorted(Point{ 0, 0}, pts));
CHECK(7 == find_closest_in_sorted(Point{2, 1}, pts));
CHECK(8 == find_closest_in_sorted(Point{2,-2}, pts));
}
TEST_CASE("Find the closest point from 9 unsorted", "[ClosestPoint]")
{
// 4 - 3 - 0
// | | |
// 1 - 6 - 5
// | | |
// 2 - 7 - 8
Points pts = {
/*0*/ {3, 3},
/*1*/ {-3, 0},
/*2*/ {-3, -3},
/*3*/ {0, 3},
/*4*/ {-3, 3},
/*5*/ {3, 0},
/*6*/ {0, 0},
/*7*/ {0, -3},
/*8*/ {3, -3}
};
CHECK(4 == find_closest(Point{-4, 4}, pts));
CHECK(4 == find_closest(Point{-2, 2}, pts));
// check center
CHECK(6 == find_closest(Point{-1, 1}, pts));
CHECK(6 == find_closest(Point{ 0, 1}, pts));
CHECK(6 == find_closest(Point{ 1, 1}, pts));
CHECK(6 == find_closest(Point{-1, 0}, pts));
CHECK(6 == find_closest(Point{ 0, 0}, pts));
CHECK(6 == find_closest(Point{ 1, 0}, pts));
CHECK(6 == find_closest(Point{-1,-1}, pts));
CHECK(6 == find_closest(Point{ 0,-1}, pts));
CHECK(6 == find_closest(Point{ 1,-1}, pts));
CHECK(6 == find_closest(Point{ 0, 0}, pts));
CHECK(5 == find_closest(Point{2, 1}, pts));
CHECK(8 == find_closest(Point{2,-2}, pts));
}