Skip to content

Commit

Permalink
feat: add pcd2bt to pybind lib
Browse files Browse the repository at this point in the history
  • Loading branch information
YifuTao committed Sep 13, 2024
1 parent c90df62 commit 0a6c87e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 56 deletions.
19 changes: 16 additions & 3 deletions spires_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ project(
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)
find_package(PCL REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(octomap REQUIRED)

# Add a library using FindPython's tooling (pybind11 also provides a helper like
# this)
include_directories(${PCL_INCLUDE_DIRS})
python_add_library(_core MODULE src/spires_cpp/main.cpp WITH_SOABI)
target_link_libraries(_core PRIVATE pybind11::headers ${PCL_LIBRARIES})
include_directories(${PCL_INCLUDE_DIRS} ${Eigen3_INCLUDE_DIRS} ${OCTOMAP_INCLUDE_DIRS})
include_directories(${CMAKE_SOURCE_DIR}/include)

set(SOURCES
src/spires_cpp/main.cpp
src/spires_cpp/octomap/pcd2bt.cpp
)

python_add_library(_core MODULE ${SOURCES} WITH_SOABI)
target_link_libraries(_core PRIVATE
pybind11::headers
${PCL_LIBRARIES}
${OCTOMAP_LIBRARIES}
)

# This is passing in the version as a define just as an example
target_compile_definitions(_core PRIVATE VERSION_INFO=${PROJECT_VERSION})
Expand Down
20 changes: 20 additions & 0 deletions spires_cpp/include/pcd2bt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PROCESS_PCD_FOLDER_H
#define PROCESS_PCD_FOLDER_H

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <dirent.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/transforms.h>
#include <octomap/octomap.h>
#include <octomap/OcTree.h>
#include <Eigen/Geometry>


// Function to process PCD files in a folder and generate an OctoMap
void processPCDFolder(const std::string& folderPath, double resolution, const std::string& save_path);

#endif // PROCESS_PCD_FOLDER_H
4 changes: 2 additions & 2 deletions spires_cpp/src/spires_cpp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations

from ._core import __doc__, __version__, add, subtract
from ._core import __doc__, __version__, processPCDFolder

__all__ = ["__doc__", "__version__", "add", "subtract"]
__all__ = ["__doc__", "__version__", "processPCDFolder"]
19 changes: 6 additions & 13 deletions spires_cpp/src/spires_cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <pybind11/pybind11.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include "progress_bar.h"
#include "pcd2bt.h"

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
Expand All @@ -24,19 +26,10 @@ PYBIND11_MODULE(_core, m) {
add
subtract
)pbdoc";

m.def("add", &add, R"pbdoc(
Add two numbers
Some other explanation about the add function.
)pbdoc");

m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc(
Subtract two numbers
Some other explanation about the subtract function.
)pbdoc");

m.def("processPCDFolder", &processPCDFolder, "Process PCD files and save to OctoMap",
py::arg("folderPath"), py::arg("resolution"), py::arg("save_path"));
// m.def("processPCDFolder", &processPCDFolder, "Process PCD files and save to OctoMap",
// py::arg("folderPath"), py::arg("resolution"), py::arg("save_path"));
#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
Expand Down
39 changes: 1 addition & 38 deletions spires_cpp/src/spires_cpp/octomap/pcd2bt.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <dirent.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/transforms.h>
#include <octomap/octomap.h>
#include <octomap/OcTree.h>
#include <Eigen/Geometry>
#include "pcd2bt.h"

// Function to list files in a directory
std::vector<std::string> listFiles(const std::string& folderPath) {
Expand Down Expand Up @@ -88,30 +78,3 @@ void processPCDFolder(const std::string& folderPath, double resolution, const st
std::cout << "Octomap saved to " << save_path << std::endl;
}

int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <path_to_pcd_folder> -r [resolution] -s [saved_path]" << std::endl;
return 1;
}
std::string folderPath = argv[1];
double resolution = 0.1;
std::string save_path = "result_octomap.bt";

// Parse command line arguments
for (int i = 2; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-r" && i + 1 < argc) {
resolution = std::stod(argv[++i]);
} else if (arg == "-s" && i + 1 < argc) {
save_path = argv[++i];
} else {
std::cerr << "Unknown argument or missing value: " << arg << std::endl;
std::cerr << "Usage: " << argv[0] << " <path_to_pcd_folder> -r [resolution] -s [saved_path]" << std::endl;
return 1;
}
}

processPCDFolder(folderPath, resolution, save_path);

return 0;
}

0 comments on commit 0a6c87e

Please sign in to comment.