Skip to content

Commit

Permalink
ENH: Add Support for loading mesh files
Browse files Browse the repository at this point in the history
* Adds VTK as an optional depenency, if VTK_DIR is defined then
  Autoscoper will be compiled with mesh support
  • Loading branch information
NicerNewerCar committed Aug 15, 2024
1 parent 8fe9748 commit 2fdded0
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 7 deletions.
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ if(NOT DEFINED Autoscoper_EXECUTABLE_LINK_FLAGS)
endif()
mark_as_superbuild(Autoscoper_EXECUTABLE_LINK_FLAGS:STRING)

#-----------------------------------------------------------------------------
# Collision Detection
#-----------------------------------------------------------------------------
if(DEFINED VTK_DIR AND EXISTS ${VTK_DIR})
message(STATUS "Configuring with collision detection")
set(Autoscoper_COLLISION_DETECTION 1)
else()
message(STATUS "Configuring without collision detection. If you want to enable collision detection, please set VTK_DIR.")
set(Autoscoper_COLLISION_DETECTION 0)
endif()
mark_as_superbuild(Autoscoper_COLLISION_DETECTION)

#-----------------------------------------------------------------------------
# Dependencies
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -252,6 +264,9 @@ if(Autoscoper_CONFIGURE_LAUCHER_SCRIPT)
endif()
endif()
endforeach()
if(Autoscoper_COLLISION_DETECTION AND NOT VTK IN_LIST Autoscoper_DEPENDENCIES) # If collision is enabled but VTK was not built as part of the superbuild
list(APPEND _library_paths "${VTK_DIR}/bin/$<CONFIG>/")
endif()

string(REPLACE ";" "${_pathsep}" PATHS_CONFIG "${_library_paths}")

Expand Down Expand Up @@ -283,8 +298,12 @@ if(Autoscoper_INSTALL_DEPENDENCIES)
foreach(dependency IN LISTS Autoscoper_DEPENDENCIES)
install(SCRIPT "${Autoscoper_SUPERBUILD_DIR}/${dependency}-build/cmake_install.cmake")
endforeach()
if(Autoscoper_COLLISION_DETECTION)
install(SCRIPT "${VTK_DIR}/cmake_install.cmake")
endif()
endif()


#-----------------------------------------------------------------------------
# Sample Data
#-----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions SuperBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ExternalProject_Add(${proj}
-DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD}
-DCMAKE_CXX_STANDARD_REQUIRED:BOOL=${CMAKE_CXX_STANDARD_REQUIRED}
-DQt5_DIR:PATH=${Qt5_DIR}
-DVTK_DIR:PATH=${VTK_DIR}
# Options
-DAutoscoper_SUPERBUILD:BOOL=OFF
-DAutoscoper_SUPERBUILD_DIR:PATH=${CMAKE_BINARY_DIR}
Expand Down
29 changes: 29 additions & 0 deletions libautoscoper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ list(APPEND libautoscoper_SOURCES
src/filesystem_compat.cpp
)

if(Autoscoper_COLLISION_DETECTION) # Add collision detection sources
list(APPEND libautoscoper_SOURCES
src/Mesh.cpp
)
list(APPEND libautoscoper_HEADERS
src/Mesh.hpp
)
endif()

if(Autoscoper_RENDERING_BACKEND STREQUAL "CUDA")
# CUDA 10.2 supports C++ up to version 14
# See https://docs.nvidia.com/cuda/archive/10.2/cuda-c-programming-guide/index.html#c-cplusplus-language-support
Expand Down Expand Up @@ -125,6 +134,26 @@ target_compile_definitions(libautoscoper PUBLIC
Autoscoper_RENDERING_USE_${Autoscoper_RENDERING_BACKEND}_BACKEND
)

if (Autoscoper_COLLISION_DETECTION) # Add definitions for collision detection
target_compile_definitions(libautoscoper PUBLIC
Autoscoper_COLLISION_DETECTION
)
find_package(VTK COMPONENTS
CommonCore
CommonSystem
FiltersSources
IOGeometry
)
if(NOT VTK_FOUND)
message(FATAL_ERROR "VTK was not found")
endif()
target_link_libraries(libautoscoper PUBLIC ${VTK_LIBRARIES})
vtk_module_autoinit(
TARGETS libautoscoper
MODULES ${VTK_LIBRARIES}
)
endif()

set_target_properties(libautoscoper PROPERTIES
RUNTIME_OUTPUT_NAME "libautoscoper${Autoscoper_ARTIFACT_SUFFIX}"
LIBRARY_OUTPUT_NAME "libautoscoper${Autoscoper_ARTIFACT_SUFFIX}"
Expand Down
27 changes: 27 additions & 0 deletions libautoscoper/src/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "Mesh.hpp"
#include <vtkSTLReader.h>
#include <vtkSTLWriter.h>

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();
}
17 changes: 17 additions & 0 deletions libautoscoper/src/Mesh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include <string>
#include <vtkPolyData.h>

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;
};
54 changes: 49 additions & 5 deletions libautoscoper/src/Trial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,18 @@ Trial::Trial(const std::string& filename)
std::vector<std::string> volumeFlips;
std::vector<std::string> renderResolution;
std::vector<std::string> optimizationOffsets;

parse(
file, version, mayaCams, camRootDirs, volumeFiles, voxelSizes, volumeFlips, renderResolution, optimizationOffsets);
std::vector<std::string> meshFiles;

parse(file,
version,
mayaCams,
camRootDirs,
volumeFiles,
voxelSizes,
volumeFlips,
renderResolution,
optimizationOffsets,
meshFiles);

file.close();

Expand All @@ -122,7 +131,7 @@ Trial::Trial(const std::string& filename)
convertToAbsolutePaths(volumeFiles, configLocation);
}

validate(mayaCams, camRootDirs, volumeFiles, voxelSizes, filename);
validate(mayaCams, camRootDirs, volumeFiles, voxelSizes, meshFiles, filename);

loadCameras(mayaCams);

Expand All @@ -133,6 +142,8 @@ Trial::Trial(const std::string& filename)
loadOffsets(optimizationOffsets);

loadRenderResolution(renderResolution);

loadMeshes(meshFiles);
}

void Trial::convertToUnixSlashes(std::string& path)
Expand Down Expand Up @@ -193,7 +204,8 @@ void Trial::parse(std::ifstream& file,
std::vector<std::string>& voxelSizes,
std::vector<std::string>& volumeFlips,
std::vector<std::string>& renderResolution,
std::vector<std::string>& optimizationOffsets)
std::vector<std::string>& optimizationOffsets,
std::vector<std::string>& meshFiles)
{

std::string line, key, value;
Expand Down Expand Up @@ -230,6 +242,9 @@ void Trial::parse(std::ifstream& file,
} else if (key.compare("OptimizationOffsets") == 0) {
asys::SystemTools::GetLineFromStream(lineStream, value);
optimizationOffsets.push_back(value);
} else if (key.compare("MeshFile") == 0) {
asys::SystemTools::GetLineFromStream(lineStream, value);
meshFiles.push_back(value);
} else if (key.compare("Version") == 0) {
asys::SystemTools::GetLineFromStream(lineStream, value);
parseVersion(value, version);
Expand All @@ -252,6 +267,7 @@ void Trial::validate(const std::vector<std::string>& mayaCams,
const std::vector<std::string>& camRootDirs,
const std::vector<std::string>& volumeFiles,
const std::vector<std::string>& voxelSizes,
const std::vector<std::string>& meshFiles,
const std::string& filename)
{

Expand Down Expand Up @@ -279,6 +295,14 @@ void Trial::validate(const std::vector<std::string>& mayaCams,
"and "
+ std::to_string(voxelSizes.size()) + " voxel sizes."));
}

if (meshFiles.size() != 0 && volumeFiles.size() != meshFiles.size()) {
throw std::runtime_error(filename, std::string("You must specify a mesh file for each volume or none at all.\n") + "Found"
+ std::to_string(volumeFiles.size())
+ " volumes "
"and "
+ std::to_string(meshFiles.size()) + " mesh files."));
}
}

void Trial::loadCameras(std::vector<std::string>& mayaCams)
Expand Down Expand Up @@ -367,6 +391,26 @@ void Trial::loadRenderResolution(std::vector<std::string>& renderResolution)
}
}

void Trial::loadMeshes(std::vector<std::string>& meshFiles)
{
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
}
}

void Trial::save(const std::string& filename)
{
std::vector<std::string> mayaCamsFiles;
Expand Down
14 changes: 12 additions & 2 deletions libautoscoper/src/Trial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@
#include "Volume.hpp"
#include "VolumeTransform.hpp"

namespace xromm {
#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.
// It should eventually become an in-memory representation of the xromm
// autoscoper file format. Currently that file format does not however hold the
Expand All @@ -72,6 +75,9 @@ class Trial
std::vector<Video> videos;
std::vector<Volume> volumes;
std::vector<VolumeTransform> volumestransform;
#ifdef Autoscoper_COLLISION_DETECTION
std::vector<Mesh> meshes;
#endif // Autoscoper_COLLISION_DETECTION

// State information
int frame;
Expand Down Expand Up @@ -101,7 +107,8 @@ class Trial
std::vector<std::string>& voxelSizes,
std::vector<std::string>& volumeFlips,
std::vector<std::string>& renderResolution,
std::vector<std::string>& optimizationOffsets);
std::vector<std::string>& optimizationOffsets,
std::vector<std::string>& meshFiles);

void parseVersion(const std::string& text, std::vector<int>& version);

Expand All @@ -122,6 +129,7 @@ class Trial
const std::vector<std::string>& camRootDirs,
const std::vector<std::string>& volumeFiles,
const std::vector<std::string>& voxelSizes,
const std::vector<std::string>& meshFiles,
const std::string& filename);

void loadCameras(std::vector<std::string>& mayaCams);
Expand All @@ -135,6 +143,8 @@ class Trial
void loadOffsets(std::vector<std::string>& offsets);

void loadRenderResolution(std::vector<std::string>& renderResolution);

void loadMeshes(std::vector<std::string>& meshFiles);
};
} // namespace xromm

Expand Down

0 comments on commit 2fdded0

Please sign in to comment.