Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Collision Detection #128

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
152fc3b
COMP: Include VTK in Superbuild
NicerNewerCar Jun 1, 2023
486a8d1
ENH: Add Support for loading mesh files
NicerNewerCar Jun 1, 2023
9b71a05
ENH: Add Support for loading mesh files
NicerNewerCar Jun 1, 2023
417828f
ENH: Adding and testing functionality for generating collision data WIP
jacob-moore22 Jun 5, 2023
7904abd
STYLE: Merge commit
jacob-moore22 Jun 5, 2023
52b033c
ENH: Adding CD for meshes WIP
jacob-moore22 Jun 6, 2023
072e7df
ENH: Adding CD, working
jacob-moore22 Jun 12, 2023
c486fc5
PERF: Moving creation and initialization of vtkCollisionDetectionFilt…
jacob-moore22 Jun 16, 2023
ea4fcec
PERF: Tidying up code and fixing install issue for RelWithDebInfo
jacob-moore22 Jun 26, 2023
58b7445
BUG: Fix bug with updating translation on spherical CD filter
jacob-moore22 Jun 27, 2023
f1b201f
PERF: PSO performance improvements. WIP
jacob-moore22 Jun 29, 2023
6f1e5f9
ENH: Adding optionion velocity limiting and collision response, WIP
jacob-moore22 Aug 15, 2023
c4cfd8a
COMP: Include VTK in Superbuild
NicerNewerCar Jun 1, 2023
fa28e51
ENH: Add Support for loading mesh files
NicerNewerCar Jun 1, 2023
2c2fd12
ENH: Adding and testing functionality for generating collision data WIP
jacob-moore22 Jun 5, 2023
2d47bc0
ENH: Add Support for loading mesh files
NicerNewerCar Jun 1, 2023
5515dc5
ENH: Adding CD for meshes WIP
jacob-moore22 Jun 6, 2023
b8ab976
ENH: Adding CD, working
jacob-moore22 Jun 12, 2023
f300815
PERF: Moving creation and initialization of vtkCollisionDetectionFilt…
jacob-moore22 Jun 16, 2023
8897a59
PERF: Tidying up code and fixing install issue for RelWithDebInfo
jacob-moore22 Jun 26, 2023
e34c822
BUG: Fix bug with updating translation on spherical CD filter
jacob-moore22 Jun 27, 2023
8367218
PERF: PSO performance improvements. WIP
jacob-moore22 Jun 29, 2023
7e220b9
ENH: Adding optionion velocity limiting and collision response, WIP
jacob-moore22 Aug 15, 2023
6f23669
Merge commit after rebase on main
jacob-moore22 Aug 21, 2023
6ada1fb
ENH: Updating trial initialization to account for meshes
jacob-moore22 Aug 21, 2023
0facb40
BUG: Fix bug when no meshes are loaded when building with collision d…
jacob-moore22 Aug 22, 2023
ce04965
STYLE: Pre-check
jacob-moore22 Aug 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMake/AutoscoperInstallQt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if(WIN32)
${_qt5Core_install_prefix}/bin/Qt5Guid.dll
${_qt5Core_install_prefix}/bin/Qt5Cored.dll
${_qt5Core_install_prefix}/bin/Qt5Networkd.dll
DESTINATION ${Autoscoper_BIN_DIR} CONFIGURATIONS Debug
DESTINATION ${Autoscoper_BIN_DIR} CONFIGURATIONS Release RelWithDebInfo MinSizeRel
COMPONENT Runtime
)
install(
Expand Down
35 changes: 35 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ option(Autoscoper_SUPERBUILD "Build ${PROJECT_NAME} and the projects it depends
mark_as_advanced(Autoscoper_SUPERBUILD)
set(Autoscoper_BINARY_INNER_SUBDIR Autoscoper-build)

option(Autoscoper_BUILD_VTK "Builds VTK alongside Autoscoper." OFF)
mark_as_advanced(Autoscoper_BUILD_VTK)
mark_as_superbuild(Autoscoper_BUILD_VTK)

if(UNIX AND NOT APPLE)
if(NOT DEFINED OpenGL_GL_PREFERENCE)
set(OpenGL_GL_PREFERENCE "LEGACY")
Expand Down Expand Up @@ -154,6 +158,30 @@ if(NOT DEFINED Autoscoper_EXECUTABLE_LINK_FLAGS)
endif()
mark_as_superbuild(Autoscoper_EXECUTABLE_LINK_FLAGS:STRING)

#-----------------------------------------------------------------------------
# Collision Detection
#-----------------------------------------------------------------------------
if(Autoscoper_BUILD_VTK OR (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 either Autoscoper_BUILD_VTK (if you want to build VTK as apart of the Superbuild) or VTK_DIR (If you want to use a pre-built version of VTK).")
set(Autoscoper_COLLISION_DETECTION 0)
endif()
mark_as_superbuild(Autoscoper_COLLISION_DETECTION)

#-----------------------------------------------------------------------------
# Collision Detection
#-----------------------------------------------------------------------------
if(Autoscoper_BUILD_VTK OR (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 either Autoscoper_BUILD_VTK (if you want to build VTK as apart of the Superbuild) or VTK_DIR (If you want to use a pre-built version of VTK).")
set(Autoscoper_COLLISION_DETECTION 0)
endif()
mark_as_superbuild(Autoscoper_COLLISION_DETECTION)

#-----------------------------------------------------------------------------
# Dependencies
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -227,6 +255,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 @@ -258,8 +289,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 AND NOT VTK IN_LIST Autoscoper_DEPENDENCIES) # If collision is enabled but VTK was not built as part of the superbuild
install(SCRIPT "${VTK_DIR}/cmake_install.cmake")
endif()
endif()


#-----------------------------------------------------------------------------
# Sample Data
#-----------------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions SuperBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ if(Autoscoper_RENDERING_BACKEND STREQUAL "OpenCL")
endif()
endif()

if(Autoscoper_BUILD_VTK)
list(APPEND Autoscoper_DEPENDENCIES
VTK
)
endif()

if(Autoscoper_BUILD_VTK)
list(APPEND Autoscoper_DEPENDENCIES
VTK
)
endif()

set(proj ${SUPERBUILD_TOPLEVEL_PROJECT})

ExternalProject_Include_Dependencies(${proj}
Expand All @@ -33,6 +45,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
64 changes: 64 additions & 0 deletions Superbuild/External_VTK.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

set(proj VTK)

set(${proj}_DEPENDENCIES "")

if(Autoscoper_USE_SYSTEM_${proj})
message(FATAL_ERROR "Enabling Autoscoper_USE_SYSTEM_${proj} is not supported !")
endif()

# Sanity checks
if(DEFINED VTK_INCLUDE_DIR AND NOT EXISTS ${VTK_INCLUDE_DIR})
message(FATAL_ERROR "VTK_INCLUDE_DIR variable is defined but corresponds to nonexistent directory")
endif()
if(DEFINED VTK_LIBRARY AND NOT EXISTS ${VTK_LIBRARY})
message(FATAL_ERROR "VTK_LIBRARY variable is defined but corresponds to nonexistent file")
endif()

if((NOT DEFINED VTK_INCLUDE_DIR
OR NOT DEFINED VTK_LIBRARY) AND NOT Autoscoper_USE_SYSTEM_${proj})

set(EP_SOURCE_DIR ${CMAKE_BINARY_DIR}/${proj})
set(EP_BINARY_DIR ${CMAKE_BINARY_DIR}/${proj}-build)
set(EP_INSTALL_DIR ${CMAKE_BINARY_DIR}/${proj}-install)

set(EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS)

if(NOT CMAKE_CONFIGURATION_TYPES)
list(APPEND EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
)
endif()

ExternalProject_Add(${proj}
GIT_REPOSITORY https://gitlab.kitware.com/vtk/vtk.git
GIT_TAG f2c452c9c42005672a3f3ed9218dd9a7fecca79a # v9.2.6
SOURCE_DIR ${EP_SOURCE_DIR}
BINARY_DIR ${EP_BINARY_DIR}
INSTALL_DIR ${EP_INSTALL_DIR}
CMAKE_CACHE_ARGS
# Compiler settings
-DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_EXTENSIONS:BOOL=${CMAKE_CXX_EXTENSIONS}
-DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD}
-DCMAKE_CXX_STANDARD_REQUIRED:BOOL=${CMAKE_CXX_STANDARD_REQUIRED}
-DVTK_USE_CUDA:BOOL=${Autoscoper_BUILD_WITH_CUDA}
# Options
-DBUILD_SHARED_LIBS:BOOL=ON
# Install directories
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
${EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS}
DEPENDS
${${proj}_DEPENDENCIES}
)
set(VTK_DIR ${EP_BINARY_DIR})
set(VTK_SOURCE_DIR ${EP_SOURCE_DIR})
ExternalProject_Message(${proj} "VTK_DIR:${VTK_DIR}")
ExternalProject_Message(${proj} "VTK_SOURCE_DIR:${VTK_SOURCE_DIR}")
mark_as_superbuild(VTK_SOURCE_DIR:PATH)
mark_as_superbuild(
VARS VTK_DIR:PATH
LABELS "FIND_PACKAGE"
)
endif()
30 changes: 30 additions & 0 deletions libautoscoper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ set(libautoscoper_SOURCES
src/VolumeTransform.cpp
)

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")
find_package(CUDA REQUIRED)
include(${CMAKE_CURRENT_SOURCE_DIR}/src/gpu/cuda/CMakeLists.txt)
Expand Down Expand Up @@ -68,6 +77,27 @@ 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
FiltersModeling
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_DIRECTORY "${CMAKE_BINARY_DIR}/${Autoscoper_BIN_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${Autoscoper_BIN_DIR}"
Expand Down
96 changes: 96 additions & 0 deletions libautoscoper/src/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "Mesh.hpp"
#include <vtkSTLReader.h>
#include <vtkSTLWriter.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkCenterOfMass.h>

Mesh::Mesh(const std::string& filename) {

fileName = filename;

vtkSTLReader* reader = vtkSTLReader::New();
this->polyData = vtkPolyData::New();


reader->SetFileName(filename.c_str());
reader->Update();
this->polyData = reader->GetOutput();

// Create OBB Tree
this->meshOBB = vtkOBBTree::New();
// this->meshOBB->SetDataSet(this->polyData);
this->meshOBB->SetMaxLevel(1);

double corner[3];
double maxAxis[3];
double midAxis[3];
double minAxis[3];
double size[3];

this->meshOBB->ComputeOBB(this->polyData, corner, maxAxis, midAxis, minAxis, size);

/*std::cout << "corner = " << corner[0] << ", " << corner[1] << ", " << corner[2] << std::endl;
std::cout << "maxAxis = " << maxAxis[0] << ", " << maxAxis[1] << ", " << maxAxis[2] << std::endl;
std::cout << "midAxis = " << midAxis[0] << ", " << midAxis[1] << ", " << midAxis[2] << std::endl;
std::cout << "minAxis = " << minAxis[0] << ", " << minAxis[1] << ", " << minAxis[2] << std::endl;
std::cout << "size = " << size[0] << ", " << size[1] << ", " << size[2] << std::endl;*/


/*this->meshOBB->Update();
this->meshOBB->PrintSelf(std::cout, vtkIndent(2));*/

// Register so it exists after deleting the reader
this->polyData->Register(nullptr);

// Compute bounding radius
double centerA[3];
this->polyData->GetCenter(centerA);

double bounds[6];
this->polyData->GetBounds(bounds);

boundingRadius =
(bounds[1] - centerA[0]) * (bounds[1] - centerA[0]) +
(bounds[3] - centerA[1]) * (bounds[3] - centerA[1]) +
(bounds[5] - centerA[2]) * (bounds[5] - centerA[2]);

boundingRadius = sqrt(boundingRadius);

std::cout << "Mesh " << filename << ", has a bounding radius of: " << boundingRadius << std::endl;


reader->Delete();
}

// Helper function to transform mesh after reading it in. Physically moves the mesh to the new transformed position.
void Mesh::Transform(double xAngle, double yAngle, double zAngle, double shiftX, double shiftY, double shiftZ)
{
vtkNew<vtkTransform> transform;
// Shift in Y to overlay tiff (double bounding box center
transform->Translate(shiftX, shiftY, shiftZ);
transform->RotateX(xAngle);
transform->RotateY(yAngle);
transform->RotateZ(zAngle);

vtkNew<vtkTransformPolyDataFilter> transformFilter;
transformFilter->SetInputData(this->polyData);
transformFilter->SetTransform(transform);
transformFilter->Update();

this->polyData = transformFilter->GetOutput();
this->polyData->Register(nullptr);
}

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

void Mesh::Transform(double xAngle, double yAngle, double zAngle, double shiftX, double shiftY, double shiftZ);

double getBoundingRadius() const { return boundingRadius; }

std::string fileName;

private:

double boundingRadius = 0.0;

vtkPolyData* polyData;
vtkOBBTree* meshOBB;

};
Loading
Loading