Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zhixiong-tang committed Aug 22, 2023
1 parent 39d703a commit c1d351b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/bindings/polyline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void bind_polyline(py::module &m)
.def("length", &Polyline::length)
.def("along", &Polyline::along, "range"_a, py::kw_only(),
"extend"_a = false)
.def("snap", &Polyline::snap, "point"_a, py::kw_only(), //
"seg_min"_a = std::nullopt, //
.def("nearest", &Polyline::nearest, "point"_a, py::kw_only(), //
"seg_min"_a = std::nullopt, //
"seg_max"_a = std::nullopt)
.def("slice", &Polyline::slice, py::kw_only(), //
"min"_a = std::nullopt, //
Expand Down
6 changes: 3 additions & 3 deletions src/nano_fmm/polyline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ struct Polyline

// P', distance, seg_idx, t
std::tuple<Eigen::Vector3d, double, int, double>
snap(const Eigen::Vector3d &point,
std::optional<int> seg_min = std::nullopt,
std::optional<int> seg_max = std::nullopt) const
nearest(const Eigen::Vector3d &point,
std::optional<int> seg_min = std::nullopt,
std::optional<int> seg_max = std::nullopt) const
{
if (!seg_min) {
seg_min = 0;
Expand Down
15 changes: 9 additions & 6 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def test_cpp_migrated_3():
assert hits != hits2[::-1]


def test_polyline_snap_slice():
def test_polyline_nearest_slice():
enus = [[0, 0, 0], [3, 0, 0], [10, 0, 0], [13, 4, 0]]
polyline = fmm.Polyline(enus)
assert polyline.range(0) == 0.0
Expand All @@ -224,30 +224,33 @@ def test_polyline_snap_slice():
assert polyline.segment_index_t(-6.0) == (0, -2.0)
assert polyline.segment_index_t(20.0) == (2, 2.0)

pt, dist, seg_idx, t = polyline.snap([1.5, 0, 0])
pt, dist, seg_idx, t = polyline.nearest([1.5, 0, 0])
assert np.all(pt == [1.5, 0, 0])
assert dist == 0.0
assert seg_idx == 0 and t == 0.5

pt, dist, seg_idx, t = polyline.snap([1.5, 3, 0])
pt, dist, seg_idx, t = polyline.nearest([1.5, 3, 0])
assert np.all(pt == [1.5, 0, 0])
assert dist == 3.0
assert seg_idx == 0 and t == 0.5

pt, dist, seg_idx, t = polyline.snap([1.5, 3, 0], seg_min=1)
pt, dist, seg_idx, t = polyline.nearest([1.5, 3, 0], seg_min=1)
assert np.all(pt == [3, 0, 0])
assert np.fabs(dist - np.linalg.norm(pt - [1.5, 3, 0])) < 1e-9
assert seg_idx == 1 and t == 0.0

assert np.all(polyline.slice(min=15) == [enus[-1], enus[-1]])
assert len(polyline.slice(min=16)) == 0

anchor = [123.4, 5.6, 7.8]
llas = fmm.utils.enu2lla([*enus, [1.5, 3.0, 0.0]], anchor_lla=anchor)
polyline = fmm.Polyline(llas[:-1], is_wgs84=True)
pt, dist, seg_idx, t = polyline.snap(llas[-1])
pt, dist, seg_idx, t = polyline.nearest(llas[-1])
assert np.fabs(pt - (llas[0] + llas[1]) / 2.0).max() < 1e-18
assert np.fabs(dist - 3.0) < 1e-9
assert seg_idx == 0 and t == 0.5

pt, dist, seg_idx, t = polyline.snap(llas[-1], seg_min=1)
pt, dist, seg_idx, t = polyline.nearest(llas[-1], seg_min=1)
assert np.all(pt == llas[1])
assert dist
assert seg_idx == 1 and t == 0.0

0 comments on commit c1d351b

Please sign in to comment.