From 7997dc3e34eca38376221d98309fbad6fe083437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Milczarek?= Date: Sun, 21 Apr 2024 19:21:56 -0300 Subject: [PATCH] refactored to lib --- .gitignore | 1 + CMakeLists.txt | 16 +++++++++++----- include/pcd2ptx.hpp | 22 ++++++++++++++++++++++ pcd2ptx.cpp | 25 ++++++++++++++++++------- tests/conversion_test.cpp | 10 ++++++++++ 5 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 .gitignore create mode 100644 include/pcd2ptx.hpp create mode 100644 tests/conversion_test.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 028c8e6..4346377 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8 FATAL_ERROR) +cmake_minimum_required(VERSION 3.0 FATAL_ERROR) set(CMAKE_INSTALL_PREFIX /usr/bin) @@ -11,8 +11,14 @@ include_directories(include) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS}) -add_executable (pcd2ptx pcd2ptx.cpp) +file(GLOB HDRS ${CMAKE_CURRENT_SOURCE_DIR}/include/*) + +add_library(pcd2ptx pcd2ptx.cpp ${HDRS}) +target_include_directories(pcd2ptx PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) target_link_libraries (pcd2ptx ${PCL_LIBRARIES}) -install(TARGETS pcd2ptx - DESTINATION ${CMAKE_INSTALL_PREFIX} - ) \ No newline at end of file + + +add_executable(ConversionTest tests/conversion_test.cpp) +target_link_libraries(ConversionTest pcd2ptx) + +add_test(NAME Conversion_Test COMMAND ConversionTest) \ No newline at end of file diff --git a/include/pcd2ptx.hpp b/include/pcd2ptx.hpp new file mode 100644 index 0000000..c97dc17 --- /dev/null +++ b/include/pcd2ptx.hpp @@ -0,0 +1,22 @@ +#pragma once +#include + +#include +#include + +class Pcd2PtxConverter +{ + private: + + std::string inputFile; + std::string outputFile; + void writePTXHeader(std::ofstream &file, const int width, const int height); + void writePTXFileXYZ(const std::string &filename, const pcl::PointCloud::Ptr cloud); + void writePTXFileXYZRGB(const std::string &filename, const pcl::PointCloud::Ptr cloud); + + public: + bool execute(); + void setInputFile(std::string file); + void setOutputFile(std::string file); + +}; \ No newline at end of file diff --git a/pcd2ptx.cpp b/pcd2ptx.cpp index 32de0f3..eec938b 100644 --- a/pcd2ptx.cpp +++ b/pcd2ptx.cpp @@ -2,8 +2,9 @@ #include #include #include +#include -void writePTXHeader(std::ofstream &file, const int width, const int height) +void Pcd2PtxConverter::writePTXHeader(std::ofstream &file, const int width, const int height) { file << width << std::endl << height << std::endl; @@ -19,7 +20,7 @@ void writePTXHeader(std::ofstream &file, const int width, const int height) file << 0.f << " " << 0.f << " " << 0.f << " " << 1.f << std::endl; } -void writePTXFileXYZ(const std::string &filename, const pcl::PointCloud::Ptr cloud) +void Pcd2PtxConverter::writePTXFileXYZ(const std::string &filename, const pcl::PointCloud::Ptr cloud) { std::ofstream file(filename.c_str()); @@ -33,7 +34,7 @@ void writePTXFileXYZ(const std::string &filename, const pcl::PointCloud::Ptr cloud) +void Pcd2PtxConverter::writePTXFileXYZRGB(const std::string &filename, const pcl::PointCloud::Ptr cloud) { std::ofstream file(filename.c_str()); @@ -51,14 +52,24 @@ void writePTXFileXYZRGB(const std::string &filename, const pcl::PointCloud::Ptr cloud(new pcl::PointCloud); - pcl::io::loadPCDFile("pc.pcd", *cloud); + pcl::io::loadPCDFile(inputFile, *cloud); // Save to PTX format - writePTXFileXYZRGB("pc.ptx", cloud); + writePTXFileXYZRGB(outputFile, cloud); - return 0; + return true; +} + +void Pcd2PtxConverter::setInputFile(std::string file) +{ + inputFile = file; +} + +void Pcd2PtxConverter::setOutputFile(std::string file) +{ + outputFile = file; } \ No newline at end of file diff --git a/tests/conversion_test.cpp b/tests/conversion_test.cpp new file mode 100644 index 0000000..dc33da5 --- /dev/null +++ b/tests/conversion_test.cpp @@ -0,0 +1,10 @@ +#include +#include + +int main(int argc, char **argv) +{ + Pcd2PtxConverter converter; + converter.setInputFile("pca-modelo.pcd"); + converter.setOutputFile("pca-modelo.ptx"); + converter.execute(); +} \ No newline at end of file