Commit e3b87cff authored by Alexis Girault's avatar Alexis Girault

WIP: Add SCCD and use in MeshToMeshCD

Still need to connect callbacks to retrieve collision data.
parent 5363de10
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
include(imstkAddLibrary) include(imstkAddLibrary)
imstk_add_library( Collision imstk_add_library( Collision
DEPENDS DEPENDS
SCCD
Geometry Geometry
SceneElements SceneElements
) )
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "imstkPlaneToSphereCD.h" #include "imstkPlaneToSphereCD.h"
#include "imstkSphereToSphereCD.h" #include "imstkSphereToSphereCD.h"
#include "imstkMeshToMeshCD.h"
#include "imstkCollidingObject.h" #include "imstkCollidingObject.h"
#include "imstkGeometry.h" #include "imstkGeometry.h"
...@@ -60,6 +61,17 @@ CollisionDetection::make_collision_detection(const Type& type, ...@@ -60,6 +61,17 @@ CollisionDetection::make_collision_detection(const Type& type,
} }
return std::make_shared<SphereToSphereCD>(); return std::make_shared<SphereToSphereCD>();
}break; }break;
case Type::MeshToMesh:
{
if (objA->getCollidingGeometry()->getType() != Geometry::Type::SurfaceMesh ||
objB->getCollidingGeometry()->getType() != Geometry::Type::SurfaceMesh)
{
LOG(WARNING) << "CollisionDetection::make_collision_detection error: "
<< "invalid object geometries for MeshToMesh collision detection.";
return nullptr;
}
return std::make_shared<MeshToMeshCD>();
}break;
default: default:
{ {
LOG(WARNING) << "CollisionDetection::make_collision_detection error: type not implemented."; LOG(WARNING) << "CollisionDetection::make_collision_detection error: type not implemented.";
......
...@@ -36,7 +36,8 @@ public: ...@@ -36,7 +36,8 @@ public:
enum class Type enum class Type
{ {
PlaneToSphere, PlaneToSphere,
SphereToSphere SphereToSphere,
MeshToMesh
}; };
/// ///
......
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version B.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-B.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "imstkMeshToMeshCD.h"
#include "imstkCollidingObject.h"
#include "imstkCollisionData.h"
#include "imstkSurfaceMesh.h"
#include <g3log/g3log.hpp>
namespace imstk {
void
MeshToMeshCD::computeCollisionData(std::shared_ptr<CollidingObject> objA,
std::shared_ptr<CollidingObject> objB,
CollisionData& colDataA,
CollisionData& colDataB)
{
auto geomA = std::dynamic_pointer_cast<SurfaceMesh>(objA->getCollidingGeometry());
auto geomB = std::dynamic_pointer_cast<SurfaceMesh>(objB->getCollidingGeometry());
// Geometries check
if (geomA == nullptr || geomB == nullptr)
{
LOG(WARNING) << "MeshToMeshCD::computeCollisionData error: invalid geometries.";
return;
}
// Init models for continuous CD
if(!m_initialized)
{
// Init
modelA = std::make_shared<DeformModel>(geomA->getVerticesPositions(), geomA->getTrianglesVertices());
modelB = std::make_shared<DeformModel>(geomB->getVerticesPositions(), geomB->getTrianglesVertices());
// Setup Callbacks
modelA->SetEECallBack((ccdEETestCallback*)nullptr);
modelA->SetVFCallBack((ccdVFTestCallback*)nullptr);
// Build BVH
modelA->BuildBVH(true);
modelB->BuildBVH(true);
}
else
{
// Update model
modelA->UpdateVert(geomA->getVerticesPositions());
modelB->UpdateVert(geomB->getVerticesPositions());
modelB->UpdateBoxes();
modelB->UpdateBoxes();
// Update BVH
modelA->RefitBVH();
modelB->RefitBVH();
// Collide
modelA->Collide(modelB.get());
}
}
void
MeshToMeshCD::EECallback(unsigned int e1_v1, unsigned int e1_v2,
unsigned int e2_v1, unsigned int e2_v2, float t)
{
LOG(INFO) <<"EE: e1("<<e1_v1<<", "<<e1_v2<<"), e2("<<e2_v1<<", "<<e2_v2<<") \t@ t="<<t;
}
void
MeshToMeshCD::VFCallback(unsigned int vid, unsigned int fid, float t)
{
LOG(INFO) <<"VF: v("<<vid<<"), f("<<fid<<") \t\t@ t="<<t;
}
}
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#ifndef imstkMeshToMeshCD_h
#define imstkMeshToMeshCD_h
#include "imstkCollisionDetection.h"
#include "DeformModel.h"
#include <memory>
namespace imstk {
class CollidingObject;
class CollisionData;
class MeshToMeshCD : public CollisionDetection
{
public:
///
/// \brief Constructor
///
MeshToMeshCD() : CollisionDetection(CollisionDetection::Type::MeshToMesh) {}
///
/// \brief Destructor
///
~MeshToMeshCD() = default;
///
/// \brief Detect collision and compute collision data
///
void computeCollisionData(std::shared_ptr<CollidingObject> objA,
std::shared_ptr<CollidingObject> objB,
CollisionData& colDataA,
CollisionData& colDataB) override;
private:
void EECallback(unsigned int e1_v1, unsigned int e1_v2, unsigned int e2_v1, unsigned int e2_v2, float t);
void VFCallback(unsigned int vid, unsigned int fid, float t);
bool m_initialized = false;
std::shared_ptr<DeformModel> modelA;
std::shared_ptr<DeformModel> modelB;
};
}
#endif // ifndef imstkMeshToMeshCD_h
#-----------------------------------------------------------------------------
# Add External Project
#-----------------------------------------------------------------------------
include(imstkAddExternalProject)
imstk_add_external_project( SCCD
GIT_REPOSITORY git@gitlab.kitware.com:iMSTK/SCCD.git
GIT_TAG iMSTK
INSTALL_COMMAND ${SKIP_STEP_COMMAND}
CMAKE_ARGS
-DBUILD_SAMPLE_APP:BOOL=OFF
RELATIVE_INCLUDE_PATH "inc"
DEPENDENCIES
Eigen
#VERBOSE
)
# FindOpenMP
find_package(OpenMP)
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
#-----------------------------------------------------------------------------
# Find path
#-----------------------------------------------------------------------------
find_path(SCCD_INCLUDE_DIR
NAMES
ccdAPI.h
)
mark_as_advanced(SCCD_INCLUDE_DIR)
#message(STATUS "SCCD_INCLUDE_DIR : ${SCCD_INCLUDE_DIR}")
#-----------------------------------------------------------------------------
# Find library
#-----------------------------------------------------------------------------
find_library(SCCD_LIBRARY
NAMES
libsccd
sccd
)
mark_as_advanced(SCCD_LIBRARY)
#message(STATUS "SCCD_LIBRARY : ${SCCD_LIBRARY}")
#-----------------------------------------------------------------------------
# Find package
#-----------------------------------------------------------------------------
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SCCD
REQUIRED_VARS
SCCD_INCLUDE_DIR
SCCD_LIBRARY)
#-----------------------------------------------------------------------------
# If missing target, create it
#-----------------------------------------------------------------------------
if(SCCD_FOUND AND NOT TARGET SCCD)
add_library(SCCD INTERFACE IMPORTED)
set_target_properties(SCCD PROPERTIES
INTERFACE_LINK_LIBRARIES "${SCCD_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SCCD_INCLUDE_DIR}"
)
endif()
...@@ -89,6 +89,7 @@ endif() ...@@ -89,6 +89,7 @@ endif()
imstk_define_dependency(g3log) imstk_define_dependency(g3log)
imstk_define_dependency(Eigen) imstk_define_dependency(Eigen)
imstk_define_dependency(SCCD)
imstk_define_dependency(VegaFEM) imstk_define_dependency(VegaFEM)
imstk_define_dependency(VTK) imstk_define_dependency(VTK)
imstk_define_dependency(VRPN) imstk_define_dependency(VRPN)
...@@ -126,6 +127,10 @@ include_directories( ${g3log_INCLUDE_DIR} ) ...@@ -126,6 +127,10 @@ include_directories( ${g3log_INCLUDE_DIR} )
find_package( Eigen 3.1.2 REQUIRED ) find_package( Eigen 3.1.2 REQUIRED )
include_directories( ${Eigen_INCLUDE_DIR} ) include_directories( ${Eigen_INCLUDE_DIR} )
# SCCD
find_package( SCCD REQUIRED )
include_directories( ${SCCD_INCLUDE_DIR} )
# VegaFEM # VegaFEM
find_package( VegaFEM REQUIRED CONFIG ) find_package( VegaFEM REQUIRED CONFIG )
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
using namespace imstk; using namespace imstk;
void testMeshCCD();
void testPenaltyRigidCollision(); void testPenaltyRigidCollision();
void testTwoFalcons(); void testTwoFalcons();
void testObjectController(); void testObjectController();
...@@ -59,7 +60,8 @@ int main() ...@@ -59,7 +60,8 @@ int main()
<< "Starting Sandbox\n" << "Starting Sandbox\n"
<< "****************\n"; << "****************\n";
testPenaltyRigidCollision(); testMeshCCD();
//testPenaltyRigidCollision();
//testTwoFalcons(); //testTwoFalcons();
//testObjectController(); //testObjectController();
//testCameraController(); //testCameraController();
...@@ -76,6 +78,76 @@ int main() ...@@ -76,6 +78,76 @@ int main()
return 0; return 0;
} }
void testMeshCCD()
{
// SDK and Scene
auto sdk = std::make_shared<SimulationManager>();
auto scene = sdk->createNewScene("MeshCCDTest");
// Vertices
std::vector<Vec3d> vertList;
vertList.push_back(Vec3d(0, 0, 0));
vertList.push_back(Vec3d(0.5, 0.5, 0));
vertList.push_back(Vec3d(1, 1, 0));
vertList.push_back(Vec3d(1, 0, 0));
vertList.push_back(Vec3d(0, 1, 0));
vertList.push_back(Vec3d(0.5, 1, 0));
vertList.push_back(Vec3d(0, 0.5, 0));
vertList.push_back(Vec3d(1, 0.5, 0));
vertList.push_back(Vec3d(0.5, 0, 0));
// Triangles
std::vector<SurfaceMesh::TriangleArray> triangles;
triangles.push_back({ { 0, 8, 6 } });
triangles.push_back({ { 7, 2, 5 } });
triangles.push_back({ { 1, 5, 4 } });
triangles.push_back({ { 3, 7, 1 } });
triangles.push_back({ { 8, 1, 6 } });
triangles.push_back({ { 1, 4, 6 } });
triangles.push_back({ { 1, 7, 5 } });
triangles.push_back({ { 3, 1, 8 } });
// SurfaceMesh 1
auto mesh1 = std::make_shared<SurfaceMesh>();
mesh1->setInitialVerticesPositions(vertList);
mesh1->setVerticesPositions(vertList);
mesh1->setTrianglesVertices(triangles);
mesh1->optimizeForDataLocality();
// SurfaceMesh 2
auto mesh2 = std::make_shared<SurfaceMesh>();
mesh2->setInitialVerticesPositions(vertList);
mesh2->setVerticesPositions(vertList);
mesh2->setTrianglesVertices(triangles);
mesh2->optimizeForDataLocality();
mesh2->scale(1.5);
mesh2->translate(BACKWARD_VECTOR*0.25);
mesh2->rotate(imstk::UP_VECTOR, imstk::PI_4);
// Obj1
auto obj1 = std::make_shared<CollidingObject>("obj1");
obj1->setVisualGeometry(mesh1);
obj1->setCollidingGeometry(mesh1);
scene->addSceneObject(obj1);
// Obj2
auto obj2 = std::make_shared<CollidingObject>("obj2");
obj2->setVisualGeometry(mesh2);
obj2->setCollidingGeometry(mesh2);
scene->addSceneObject(obj2);
// Collisions
auto colGraph = scene->getCollisionGraph();
colGraph->addInteractionPair(obj1, obj2,
CollisionDetection::Type::MeshToMesh,
CollisionHandling::Type::None,
CollisionHandling::Type::None);
// Run
sdk->setCurrentScene("MeshCCDTest");
sdk->startSimulation(true);
}
void testPenaltyRigidCollision() void testPenaltyRigidCollision()
{ {
// SDK and Scene // SDK and Scene
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment