From 486a8d1892d8cdbf9b641bf7991e040efd7fe957 Mon Sep 17 00:00:00 2001 From: Anthony Lombardi Date: Thu, 1 Jun 2023 14:00:08 -0400 Subject: [PATCH] ENH: Add Support for loading mesh files --- libautoscoper/CMakeLists.txt | 8 +++++++- libautoscoper/src/Mesh.cpp | 24 +++++++++++++++++++++++ libautoscoper/src/Mesh.hpp | 17 +++++++++++++++++ libautoscoper/src/Trial.cpp | 27 ++++++++++++++++++++++++++ libautoscoper/src/Trial.hpp | 37 ++++++++++++++++++++++-------------- 5 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 libautoscoper/src/Mesh.cpp create mode 100644 libautoscoper/src/Mesh.hpp diff --git a/libautoscoper/CMakeLists.txt b/libautoscoper/CMakeLists.txt index e615f044..9db43b73 100644 --- a/libautoscoper/CMakeLists.txt +++ b/libautoscoper/CMakeLists.txt @@ -33,7 +33,13 @@ set(libautoscoper_SOURCES src/VolumeTransform.cpp ) -if(Autoscoper_COLLISION_DETECTION) # Add collision detection sources +if(Autoscoper_COLLISION_DETECTION) # Add collision detection sources/headers + list(APPEND libautoscoper_SOURCES + src/Mesh.cpp + ) + list(APPEND libautoscoper_HEADERS + src/Mesh.hpp + ) endif() if(Autoscoper_RENDERING_BACKEND STREQUAL "CUDA") diff --git a/libautoscoper/src/Mesh.cpp b/libautoscoper/src/Mesh.cpp new file mode 100644 index 00000000..261118b2 --- /dev/null +++ b/libautoscoper/src/Mesh.cpp @@ -0,0 +1,24 @@ +#include "Mesh.hpp" +#include +#include + +Mesh::Mesh(const std::string& filename) { + vtkSTLReader* reader = vtkSTLReader::New(); + reader->SetFileName(filename.c_str()); + reader->Update(); + this->polyData = reader->GetOutput(); + reader->Delete(); +} + +Mesh::Mesh(const Mesh& other) { + this->polyData = vtkPolyData::New(); + this->polyData->DeepCopy(other.polyData); +} + +void Mesh::Write(const std::string& filename) const { + vtkSTLWriter* writer = vtkSTLWriter::New(); + writer->SetFileName(filename.c_str()); + writer->SetInputData(this->polyData); + writer->Write(); + writer->Delete(); +} diff --git a/libautoscoper/src/Mesh.hpp b/libautoscoper/src/Mesh.hpp new file mode 100644 index 00000000..baf98001 --- /dev/null +++ b/libautoscoper/src/Mesh.hpp @@ -0,0 +1,17 @@ +#pragma once +#include +#include + +class Mesh { +public: + Mesh(const std::string& filename); + Mesh(const Mesh&); + + vtkPolyData* GetPolyData() const { return this->polyData; } + + void Write(const std::string& filename) const; + +private: + + vtkPolyData* polyData; +}; \ No newline at end of file diff --git a/libautoscoper/src/Trial.cpp b/libautoscoper/src/Trial.cpp index 651695ee..dde967d2 100644 --- a/libautoscoper/src/Trial.cpp +++ b/libautoscoper/src/Trial.cpp @@ -75,6 +75,7 @@ namespace xromm std::vector volumeFlips; std::vector renderResolution; std::vector optimizationOffsets; + std::vector meshFiles; std::string line, key, value; while (getline(file, line)) { @@ -114,6 +115,10 @@ namespace xromm getline(lineStream, value); optimizationOffsets.push_back(value); } + else if (key.compare("MeshFile") == 0) { + getline(lineStream, value); + meshFiles.push_back(value); + } } // Close the file. @@ -132,6 +137,9 @@ namespace xromm if (volumeFiles.size() != voxelSizes.size()) { throw std::runtime_error("You must sepcify a voxels size for each volume."); } + if (meshFiles.size() != 0 && volumeFiles.size() != meshFiles.size()) { + throw std::runtime_error("You must sepcify a mesh file for each volume or none at all."); + } cameras.clear(); for (unsigned int i = 0; i < mayaCams.size(); ++i) { @@ -179,6 +187,25 @@ namespace xromm } } + // load in mesh files if they exist + if (meshFiles.size() > 0) { +#ifdef Autoscoper_COLLISION_DETECTION + meshes.clear(); + for (unsigned int i = 0; i < meshFiles.size(); ++i) { + try { + Mesh mesh(meshFiles[i]); + meshes.push_back(mesh); + } + catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + } +#else + std::cerr << "WARNING: Autoscoper was not compiled with collision detection support. No mesh files will be loaded." << std::endl; +#endif // Autoscoper_COLLISION_DETECTION + } + + int maxVideoFrames = 0; videos.clear(); for (unsigned int i = 0; i < camRootDirs.size(); ++i) { diff --git a/libautoscoper/src/Trial.hpp b/libautoscoper/src/Trial.hpp index c0e5fb8a..2cb6bb27 100644 --- a/libautoscoper/src/Trial.hpp +++ b/libautoscoper/src/Trial.hpp @@ -52,6 +52,11 @@ #include "Volume.hpp" #include "VolumeTransform.hpp" +#ifdef Autoscoper_COLLISION_DETECTION +#include "Mesh.hpp" +#endif // Autoscoper_COLLISION_DETECTION + + namespace xromm { // The trial class contains all of the state information for an autoscoper run. @@ -63,28 +68,32 @@ class Trial { public: - // Loads a trial file + // Loads a trial file - Trial(const std::string& filename = ""); + Trial(const std::string& filename = ""); - void save(const std::string& filename); + void save(const std::string& filename); - std::vector cameras; - std::vector