size_t Slic3r::find_closest_in_sorted(const P &p, const std::vector &pts)
{
@@ -62,17 +91,17 @@ size_t Slic3r::find_closest_in_sorted(const P &p, const std::vector
&pts)
if (pts.size() == 1) return 0;
using V = decltype(p.x());
-
+ using It = std::vector
::const_iterator;
// closest point node in X
- Points::const_iterator it_x = std::upper_bound(pts.begin(), pts.end(), p.x(), upper_fnc
);
+ It it_x = std::upper_bound(pts.begin(), pts.end(), p.x(), upper_fnc
);
bool is_it_x_end = it_x == pts.end();
// it_x can't pointing to end so change to last point
if (is_it_x_end) --it_x;
// manhatn distance to closest point
- uint32_t manhattan_dist = manhattan_size(*it_x - p);
+ uint32_t manhattan_dist = manhattan_size(*it_x, p);
// node for lower bound
- Points::const_iterator it_l;
+ It it_l;
if (it_x == pts.begin()) {
it_l = it_x;
} else {
@@ -90,7 +119,7 @@ size_t Slic3r::find_closest_in_sorted(const P &p, const std::vector
&pts)
}
// node for upper bound
- Points::const_iterator it_u;
+ It it_u;
if (is_it_x_end) {
it_u = pts.end();
} else {
@@ -111,7 +140,7 @@ size_t Slic3r::find_closest_in_sorted(const P &p, const std::vector
&pts)
// find closest by squer distance
float dist_sq = std::numeric_limits::max();
size_t result = it_x - pts.begin();
- for (Points::const_iterator it = it_l; it < it_u; ++it) {
+ for (It it = it_l; it < it_u; ++it) {
uint32_t diff_y = std::abs(it->y() - p.y());
if (diff_y > manhattan_dist) continue;
float diff_x = it->x() - p.x();
@@ -125,4 +154,11 @@ size_t Slic3r::find_closest_in_sorted(const P &p, const std::vector &pts)
return result;
}
+template
+std::pair find_closest_in_sorted(
+ const std::vector &pts, const std::vector
&sorted_pts)
+{
+ return {0, 0};
+}
+
#endif // slic3r_ClosestPoint_hpp_
diff --git a/tests/libslic3r/test_closest_point.cpp b/tests/libslic3r/test_closest_point.cpp
index 2204f21ac..75f7d53e3 100644
--- a/tests/libslic3r/test_closest_point.cpp
+++ b/tests/libslic3r/test_closest_point.cpp
@@ -1,12 +1,14 @@
#include
#include
+#include
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));
+ CHECK(std::is_sorted(pts.begin(), pts.end(),
+ closestPoint::sort_fnc));
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));
@@ -21,7 +23,8 @@ TEST_CASE("Find the closest point from 9", "[ClosestPoint]")
// 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));
+ CHECK(std::is_sorted(pts.begin(), pts.end(),
+ closestPoint::sort_fnc));
CHECK(0 == find_closest_in_sorted(Point{-4, 4}, pts));
CHECK(0 == find_closest_in_sorted(Point{-2, 2}, pts));
@@ -40,3 +43,39 @@ TEST_CASE("Find the closest point from 9", "[ClosestPoint]")
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));
+}
\ No newline at end of file