From 8cdbbb40601557942dece994049b7fec109dbb29 Mon Sep 17 00:00:00 2001 From: Arthur_Ch <56088401+ArthurChains@users.noreply.github.com> Date: Fri, 14 Apr 2023 12:23:19 +0300 Subject: [PATCH] feat: new python bindings --- cpp/pybind/CMakeLists.txt | 11 +++--- cpp/pybind/config.cpp | 32 +++++++++++++++++ cpp/pybind/config.h | 29 ++++++++++++++++ cpp/pybind/map_metrics_pybind.cpp | 36 +++++++++++++++++++ cpp/pybind/map_tree.cpp | 33 ++++++++++++++++++ cpp/pybind/map_tree.h | 29 ++++++++++++++++ cpp/pybind/metrics.cpp | 57 ++++++------------------------- cpp/pybind/metrics.h | 29 ++++++++++++++++ cpp/pybind/utils/cloud_utils.cpp | 41 ++++++++++++++++++++++ cpp/pybind/utils/cloud_utils.h | 33 ++++++++++++++++++ 10 files changed, 278 insertions(+), 52 deletions(-) create mode 100644 cpp/pybind/config.cpp create mode 100644 cpp/pybind/config.h create mode 100644 cpp/pybind/map_metrics_pybind.cpp create mode 100644 cpp/pybind/map_tree.cpp create mode 100644 cpp/pybind/map_tree.h create mode 100644 cpp/pybind/metrics.h create mode 100644 cpp/pybind/utils/cloud_utils.cpp create mode 100644 cpp/pybind/utils/cloud_utils.h diff --git a/cpp/pybind/CMakeLists.txt b/cpp/pybind/CMakeLists.txt index f949386..1472444 100644 --- a/cpp/pybind/CMakeLists.txt +++ b/cpp/pybind/CMakeLists.txt @@ -1,10 +1,11 @@ -pybind11_add_module(pybind metrics.cpp) +pybind11_add_module(pybind + map_metrics_pybind.cpp + config.cpp + map_tree.cpp + metrics.cpp + utils/cloud_utils.cpp) find_package(Eigen3 3.4 REQUIRED) -# TODO: remove? -target_link_libraries(pybind PRIVATE ${cilantro_LIBRARIES}) -target_include_directories(pybind PRIVATE "${CMAKE_BINARY_DIR}/external/cilantro/cilantro/include") - target_link_libraries(pybind PRIVATE Eigen3::Eigen) target_link_libraries(pybind PUBLIC map_metrics) diff --git a/cpp/pybind/config.cpp b/cpp/pybind/config.cpp new file mode 100644 index 0000000..475ae74 --- /dev/null +++ b/cpp/pybind/config.cpp @@ -0,0 +1,32 @@ +// Copyright (c) 2022, Arthur Saliou, Anastasiia Kornilova +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// Created on: May 20, 2022 +// Author: Arthur Saliou +// arthur.salio@gmail.com +// +#include "config.h" + +#include "map_metrics/config.h" + +namespace map_metrics { +void pybindConfig(py::module& m) { + py::module_ m_config = m.def_submodule("config", "Predefined config class"); + + py::class_(m_config, "Config") + .def(py::init(), py::arg("min_knn") = 5, py::arg("knn_rad") = 1.0, py::arg("max_nn") = 30, + py::arg("min_clust_size") = 5); +} +} // namespace map_metrics \ No newline at end of file diff --git a/cpp/pybind/config.h b/cpp/pybind/config.h new file mode 100644 index 0000000..063786d --- /dev/null +++ b/cpp/pybind/config.h @@ -0,0 +1,29 @@ +// Copyright (c) 2022, Arthur Saliou, Anastasiia Kornilova +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// Created on: May 20, 2022 +// Author: Arthur Saliou +// arthur.salio@gmail.com +// +#ifndef MAP_METRICS_PYBIND_CONFIG_H +#define MAP_METRICS_PYBIND_CONFIG_H +#include + +namespace py = pybind11; + +namespace map_metrics { +void pybindConfig(py::module& m); +} +#endif // MAP_METRICS_PYBIND_CONFIG_H diff --git a/cpp/pybind/map_metrics_pybind.cpp b/cpp/pybind/map_metrics_pybind.cpp new file mode 100644 index 0000000..6a997ea --- /dev/null +++ b/cpp/pybind/map_metrics_pybind.cpp @@ -0,0 +1,36 @@ +// Copyright (c) 2022, Arthur Saliou, Anastasiia Kornilova +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// Created on: May 20, 2022 +// Author: Arthur Saliou +// arthur.salio@gmail.com +// +#include "pybind11/pybind11.h" + +#include "config.h" +#include "map_tree.h" +#include "metrics.h" +#include "utils/cloud_utils.h" + +namespace map_metrics { +PYBIND11_MODULE(pybind, m) { + m.doc() = "Pybind metrics module"; + + pybindConfig(m); + pybindMapTree(m); + pybindCloudUtils(m); + pybindMetrics(m); +} +} // namespace map_metrics \ No newline at end of file diff --git a/cpp/pybind/map_tree.cpp b/cpp/pybind/map_tree.cpp new file mode 100644 index 0000000..2039110 --- /dev/null +++ b/cpp/pybind/map_tree.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2022, Arthur Saliou, Anastasiia Kornilova +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// Created on: May 20, 2022 +// Author: Arthur Saliou +// arthur.salio@gmail.com +// +#include "map_tree.h" + +#include + +#include "map_metrics/map_tree.h" + +namespace map_metrics { +void pybindMapTree(py::module& m) { + py::module m_map_tree = m.def_submodule("map_tree", "Map Tree class"); + + py::class_(m_map_tree, "MapTree") + .def(py::init(), py::arg("points"), py::arg("knn_rad")); +} +} // namespace map_metrics \ No newline at end of file diff --git a/cpp/pybind/map_tree.h b/cpp/pybind/map_tree.h new file mode 100644 index 0000000..cf31e35 --- /dev/null +++ b/cpp/pybind/map_tree.h @@ -0,0 +1,29 @@ +// Copyright (c) 2022, Arthur Saliou, Anastasiia Kornilova +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// Created on: May 20, 2022 +// Author: Arthur Saliou +// arthur.salio@gmail.com +// +#ifndef MAP_METRICS_PYBIND_MAP_TREE_H +#define MAP_METRICS_PYBIND_MAP_TREE_H +#include + +namespace py = pybind11; + +namespace map_metrics{ +void pybindMapTree(py::module& m); +} +#endif // MAP_METRICS_PYBIND_MAP_TREE_H diff --git a/cpp/pybind/metrics.cpp b/cpp/pybind/metrics.cpp index a9070a0..6fad011 100644 --- a/cpp/pybind/metrics.cpp +++ b/cpp/pybind/metrics.cpp @@ -17,54 +17,17 @@ // Author: Arthur Saliou // arthur.salio@gmail.com // -#include -#include +#include "metrics.h" -#include -#include -#include +#include -#include -#include -#include +namespace map_metrics { +void pybindMetrics(py::module& m) { + py::module m_metrics = m.def_submodule("metrics", "Map Metrics (MME, MPV, MOM)"); -// Python: arg0: List[numpy.ndarray[numpy.float64[3, n]]], arg1: List[numpy.ndarray[numpy.float64[4, 4]]]) -> float - -namespace py = pybind11; - -std::vector extract_orthogonal( - cilantro::VectorSet3d const & points , config::CustomConfig config, double eps=1e-1) -{ - return orth_extract::ExtractOrthogonalSubset(cilantro::PointCloud3d(points), config, eps); + m_metrics.def("MME", &MME, "Mean Map Entropy map metric", py::arg("map_tree"), py::arg("min_component_size")); + m_metrics.def("MPV", &MPV, "Mean Map Variance map metric", py::arg("map_tree"), py::arg("min_component_size")); + m_metrics.def("MOM", &MOM, "Mutually Orthogonal Metric map metric", py::arg("map_tree"), + py::arg("min_component_size"), py::arg("orthogonal_subset") = std::vector()); } - -PYBIND11_MODULE(map_metrics, m){ - m.doc() = R"(Map-Metrics library -A tool for evaluating the quality of odometry algorithm trajectory.)"; - - py::module_ mcfg = m.def_submodule("config", "Config submodule"); - - py::class_(mcfg, "CustomConfig") - .def(py::init()); - - m.def("mpv", &metrics::GetMPV, "Mean Plane Variance trajectory metric", - py::arg("pcs"), - py::arg("poses"), - py::arg("config") = config::CustomConfig()); - - m.def("mme", &metrics::GetMME, "Mean Map Entropy trajectory metric", - py::arg("pcs"), - py::arg("poses"), - py::arg("config") = config::CustomConfig()); - - m.def("mom", &metrics::GetMOM, "Mutually Orthogonal Metric trajectory metric", - py::arg("pcs"), - py::arg("poses"), - py::arg("config") = config::CustomConfig(), - py::arg("orth_subset") = std::vector()); - - m.def("extract_orthogonal", &extract_orthogonal, "Extract orthogonal plane subset from Point Cloud", - py::arg("pc"), - py::arg("config") = config::CustomConfig(), - py::arg("eps") = 1e-1); -} \ No newline at end of file +} // namespace map_metrics \ No newline at end of file diff --git a/cpp/pybind/metrics.h b/cpp/pybind/metrics.h new file mode 100644 index 0000000..8824794 --- /dev/null +++ b/cpp/pybind/metrics.h @@ -0,0 +1,29 @@ +// Copyright (c) 2022, Arthur Saliou, Anastasiia Kornilova +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// Created on: May 20, 2022 +// Author: Arthur Saliou +// arthur.salio@gmail.com +// +#ifndef MAP_METRICS_PYBIND_METRICS_H +#define MAP_METRICS_PYBIND_METRICS_H +#include + +namespace py = pybind11; + +namespace map_metrics { +void pybindMetrics(py::module& m); +} +#endif // MAP_METRICS_PYBIND_METRICS_H diff --git a/cpp/pybind/utils/cloud_utils.cpp b/cpp/pybind/utils/cloud_utils.cpp new file mode 100644 index 0000000..b2bf9dd --- /dev/null +++ b/cpp/pybind/utils/cloud_utils.cpp @@ -0,0 +1,41 @@ +// Copyright (c) 2022, Arthur Saliou, Anastasiia Kornilova +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// Created on: May 20, 2022 +// Author: Arthur Saliou +// arthur.salio@gmail.com +// +#include "cloud_utils.h" + +#include + +#include "map_metrics/utils/cloud_utils.h" + +namespace map_metrics { +void pybindCloudUtils(py::module& m) { + py::module m_cloud_utils = m.def_submodule("cloud_utils", "Orthogonal extraction, map composition utils"); + + pybindAggregateMap(m_cloud_utils); + pybindFindOrthogonalSubset(m_cloud_utils); +} + +void pybindAggregateMap(py::module& m) { + m.def("aggregate_map", &aggregateMap, py::arg("point_sequence"), py::arg("poses")); +} + +void pybindFindOrthogonalSubset(py::module& m) { + m.def("find_orthogonal_subset", &findOrthogonalSubset, py::arg("points"), py::arg("config")); +} +} // namespace map_metrics \ No newline at end of file diff --git a/cpp/pybind/utils/cloud_utils.h b/cpp/pybind/utils/cloud_utils.h new file mode 100644 index 0000000..e31847e --- /dev/null +++ b/cpp/pybind/utils/cloud_utils.h @@ -0,0 +1,33 @@ +// Copyright (c) 2022, Arthur Saliou, Anastasiia Kornilova +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// Created on: May 20, 2022 +// Author: Arthur Saliou +// arthur.salio@gmail.com +// +#ifndef MAP_METRICS_PYBIND_CLOUD_UTILS_H +#define MAP_METRICS_PYBIND_CLOUD_UTILS_H +#include + +namespace py = pybind11; + +namespace map_metrics { +void pybindCloudUtils(py::module& m); + +void pybindAggregateMap(py::module& m); + +void pybindFindOrthogonalSubset(py::module& m); +} // namespace map_metrics +#endif // MAP_METRICS_PYBIND_CLOUD_UTILS_H