Skip to content

Commit

Permalink
update polyline-in-polygon, reuse FastCrossing (#17)
Browse files Browse the repository at this point in the history
* update

* should be ready

* update docs

* update

* fix

* update

* update

* update headers

* fix

* update headers

---------

Co-authored-by: TANG ZHIXIONG <zhixiong.tang@momenta.ai>
  • Loading branch information
district10 and zhixiong-tang committed Jul 2, 2023
1 parent 874d6a8 commit 8a4b93d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ _generate/
*env*
wheelhouse
!test.py
site
4 changes: 2 additions & 2 deletions docs/point-in-polygon.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ python3 benchmarks/benchmark_point_in_polygon.py cubao \
| implementation | time (seconds) | speed up |
| :-- | :-- | :-- |
| shapely | 0.114556074 | |
| matplotlib | 0.001869917 | x61 |
| cubao | 0.001556635 | x73 |
| matplotlib | 0.001869917 | 61x |
| cubao | 0.001556635 | 73x |
17 changes: 17 additions & 0 deletions docs/polyline-in-polygon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# polyline-in-polygon

code:

- <https://github.com/cubao/fast-crossing/blob/master/scripts/debug_polyline_in_polygon.py>

video:

- <https://www.bilibili.com/video/BV1D24y1u7uB>
- <https://www.youtube.com/watch?v=1dPJ3P84FxE>

```python
{%
include-markdown "../scripts/debug_polyline_in_polygon.py"
comments=false
%}
```
2 changes: 1 addition & 1 deletion headers
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ nav:
- Usage: usage.md
- MISC:
- point-in-polygon-test: point-in-polygon.md
- polyline-in-polygon-clipping: polyline-in-polygon.md
- About:
- Release Notes: about/release-notes.md
- License: about/license.md
Expand Down
11 changes: 10 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ PYBIND11_MODULE(_pybind11_fast_crossing, m)
m.def("densify_polyline", &cubao::densify_polyline, //
"polyline"_a, py::kw_only(), "max_gap"_a,
"densify polyline, interpolate to satisfy max_gap");
m.def("polyline_in_polygon", &cubao::polyline_in_polygon, //
m.def("polyline_in_polygon",
py::overload_cast<const cubao::RowVectors &, //
const Eigen::Ref<const cubao::RowVectorsNx2> &,
const cubao::FastCrossing &>(
&cubao::polyline_in_polygon), //
"polyline"_a, "polygon"_a, py::kw_only(), "fc"_a);
m.def("polyline_in_polygon",
py::overload_cast<const cubao::RowVectors &, //
const Eigen::Ref<const cubao::RowVectorsNx2> &,
bool>(&cubao::polyline_in_polygon), //
"polyline"_a, "polygon"_a, py::kw_only(), "is_wgs84"_a = false);

#ifdef VERSION_INFO
Expand Down
17 changes: 13 additions & 4 deletions src/polyline_in_polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ using PolylineChunks = std::map<std::tuple<int, // seg_idx
inline PolylineChunks
polyline_in_polygon(const RowVectors &polyline, //
const Eigen::Ref<const RowVectorsNx2> &polygon,
bool is_wgs84 = false)
const FastCrossing &fc)
{
auto fc = FastCrossing(is_wgs84);
fc.add_polyline(polygon);
auto intersections = fc.intersections(polyline);
// pt, (t, s), cur_label=(poly1, seg1), tree_label=(poly2, seg2)
auto ruler = PolylineRuler(polyline, is_wgs84);
auto ruler = PolylineRuler(polyline, fc.is_wgs84());
// 0.0, [r1, r2, ..., length]
const int N = intersections.size() + 1;
Eigen::VectorXd ranges(N);
Expand Down Expand Up @@ -71,6 +69,17 @@ polyline_in_polygon(const RowVectors &polyline, //
return ret;
}

inline PolylineChunks
polyline_in_polygon(const RowVectors &polyline, //
const Eigen::Ref<const RowVectorsNx2> &polygon,
bool is_wgs84 = false)
{
auto fc = FastCrossing(is_wgs84);
fc.add_polyline(polygon);
fc.finish();
return polyline_in_polygon(polyline, polygon, fc);
}

} // namespace cubao

#endif
18 changes: 18 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import time

import numpy as np
import pytest
Expand Down Expand Up @@ -796,6 +797,23 @@ def test_polyline_in_polygon():
expected_ranges = [2.72883, 14.4676666, 7.01783]
np.testing.assert_allclose(ranges, expected_ranges, atol=1e-4)

# test fc
fc = FastCrossing()
fc.add_polyline(polygon_ABCD)
chunks2 = polyline_in_polygon(polyline_12345, polygon_ABCD, fc=fc)
assert list(chunks.keys()) == list(chunks2.keys())
N = 1000
tick = time.time()
for _ in range(N):
polyline_in_polygon(polyline_12345, polygon_ABCD)
delta1 = time.time() - tick
tick = time.time()
for _ in range(N):
polyline_in_polygon(polyline_12345, polygon_ABCD, fc=fc)
delta2 = time.time() - tick
print(delta1, delta2)
# assert delta2 < delta1

anchor_lla = [123.4, 56.7, 8.9]
chunks = polyline_in_polygon(
tf.enu2lla(polyline_12345, anchor_lla=anchor_lla),
Expand Down

0 comments on commit 8a4b93d

Please sign in to comment.