Commit c360ad35 authored by Sreekanth Arikatla's avatar Sreekanth Arikatla

Merge branch 'OctreeBasedCD' into 'master'

Octree-based Collision Detection

See merge request iMSTK/iMSTK!349
parents f5efd023 f85ac035
Pipeline #142885 passed with stage
in 0 seconds
/*=========================================================================
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.
=========================================================================*/
#include "imstkMath.h"
#include <array>
using namespace imstk;
std::pair<StdVectorOfVec3d, std::vector<std::array<size_t, 3>>>
getBox()
{
std::vector<double> buffVertices
{
0.353553385, -0.707106769, -0.353553414,
-0.353553414, -0.707106769, 0.353553385,
0.853553414, 0, 0.146446615,
0.146446615, 0, 0.853553414,
-0.853553414, 0, -0.146446615,
-0.146446615, 0, -0.853553414,
-0.353553385, 0.707106769, 0.353553414,
0.353553414, 0.707106769, -0.353553385
};
std::vector<size_t> buffFaces
{
1, 4, 2,
1, 3, 4,
5, 8, 6,
5, 7, 8,
7, 3, 8,
7, 4, 3,
6, 2, 5,
6, 1, 2,
6, 3, 1,
6, 8, 3,
2, 7, 5,
2, 4, 7
};
StdVectorOfVec3d vertices;
vertices.reserve(buffVertices.size() / 3);
for (size_t i = 0; i < buffVertices.size() / 3; ++i)
{
vertices.emplace_back(Vec3d(buffVertices[i * 3],
buffVertices[i * 3 + 1],
buffVertices[i * 3 + 2]));
}
std::vector<std::array<size_t, 3>> faces;
faces.reserve(buffFaces.size() / 3);
for (size_t i = 0; i < buffFaces.size() / 3; ++i)
{
// Face ID of triangles is 0-based index (data from .obj file is 1-based index)
std::array<size_t, 3> tmp;
tmp[0] = buffFaces[i * 3] - 1;
tmp[1] = buffFaces[i * 3 + 1] - 1;
tmp[2] = buffFaces[i * 3 + 2] - 1;
faces.push_back(std::move(tmp));
}
return { vertices, faces };
}
This source diff could not be displayed because it is too large. You can view the blob instead.
#-----------------------------------------------------------------------------
# Add Example subdirectories
#-----------------------------------------------------------------------------
macro(listOfSubDir result curdir)
file(GLOB children RELATIVE ${curdir} ${curdir}/*)
set(dirlist "")
foreach(child ${children})
if(IS_DIRECTORY ${curdir}/${child})
list(APPEND dirlist ${child})
endif()
endforeach()
set(${result} ${dirlist})
endmacro()
listOfSubDir(subDirs ${CMAKE_CURRENT_SOURCE_DIR})
foreach(subdir ${subDirs})
add_subdirectory(${subdir})
endforeach()
###########################################################################
#
# Copyright (c) Kitware, Inc.
#
# 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.
#
###########################################################################
project(Example-CD-ManualCDWithOctree)
#-----------------------------------------------------------------------------
# Create executable
#-----------------------------------------------------------------------------
add_executable(${PROJECT_NAME} ManualCDWithOctreeExample.cpp
../Sphere.cpp
../Box.cpp
../Triangle.cpp
../BunnyMesh.cpp)
#-----------------------------------------------------------------------------
# Add shaders
#-----------------------------------------------------------------------------
include(imstkCopyAndCompileShaders)
CopyAndCompileShaders()
#-----------------------------------------------------------------------------
# Link libraries to executable
#-----------------------------------------------------------------------------
target_link_libraries(${PROJECT_NAME} SimulationManager)
/*=========================================================================
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.
=========================================================================*/
#include "imstkMath.h"
#include <array>
using namespace imstk;
std::pair<StdVectorOfVec3d, std::vector<std::array<size_t, 3>>>
getSphere()
{
std::vector<double> buffVertices
{
0, 0, -0.5,
0, 0.262865603, -0.425325394,
0.249999985, 0.0812300518, -0.425325394,
0, 0.44721368, -0.223606572,
0.249999985, 0.344095677, -0.262865305,
0.425325423, 0.138196841, -0.223606601,
0.154508501, -0.212662712, -0.425325423,
0.404508621, -0.131432697, -0.262865394,
0.262865603, -0.361803472, -0.223606631,
-0.154508501, -0.212662712, -0.425325423,
0, -0.425325513, -0.262865365,
-0.262865603, -0.361803472, -0.223606631,
-0.249999985, 0.0812300518, -0.425325394,
-0.404508621, -0.131432697, -0.262865394,
-0.425325423, 0.138196841, -0.223606601,
-0.249999985, 0.344095677, -0.262865305,
-0.154508486, 0.4755283, 0,
-0.404508412, 0.293892711, 0,
-0.262865603, 0.361803472, 0.223606631,
-0.5, 0, 0,
-0.404508412, -0.293892711, 0,
-0.425325423, -0.138196841, 0.223606601,
-0.154508486, -0.4755283, 0,
0.154508486, -0.4755283, 0,
0, -0.44721368, 0.223606572,
0.404508412, -0.293892711, 0,
0.5, 0, 0,
0.425325423, -0.138196841, 0.223606601,
0.404508412, 0.293892711, 0,
0.154508486, 0.4755283, 0,
0.262865603, 0.361803472, 0.223606631,
0, 0.425325513, 0.262865365,
-0.404508621, 0.131432697, 0.262865394,
-0.249999985, -0.344095677, 0.262865305,
0.249999985, -0.344095677, 0.262865305,
0.404508621, 0.131432697, 0.262865394,
0, 0, 0.5,
0.154508501, 0.212662712, 0.425325423,
-0.154508501, 0.212662712, 0.425325423,
0.249999985, -0.0812300518, 0.425325394,
0, -0.262865603, 0.425325394,
-0.249999985, -0.0812300518, 0.425325394
};
std::vector<size_t> buffFaces
{
3, 1, 2,
5, 2, 4,
6, 3, 5,
3, 2, 5,
7, 1, 3,
8, 3, 6,
9, 7, 8,
7, 3, 8,
10, 1, 7,
11, 7, 9,
12, 10, 11,
10, 7, 11,
13, 1, 10,
14, 10, 12,
15, 13, 14,
13, 10, 14,
2, 1, 13,
16, 13, 15,
4, 2, 16,
2, 13, 16,
17, 4, 16,
18, 16, 15,
19, 17, 18,
17, 16, 18,
20, 15, 14,
21, 14, 12,
22, 20, 21,
20, 14, 21,
23, 12, 11,
24, 11, 9,
25, 23, 24,
23, 11, 24,
26, 9, 8,
27, 8, 6,
28, 26, 27,
26, 8, 27,
29, 6, 5,
30, 5, 4,
31, 29, 30,
29, 5, 30,
30, 4, 17,
32, 17, 19,
31, 30, 32,
30, 17, 32,
18, 15, 20,
33, 20, 22,
19, 18, 33,
18, 20, 33,
21, 12, 23,
34, 23, 25,
22, 21, 34,
21, 23, 34,
24, 9, 26,
35, 26, 28,
25, 24, 35,
24, 26, 35,
27, 6, 29,
36, 29, 31,
28, 27, 36,
27, 29, 36,
39, 37, 38,
32, 38, 31,
19, 39, 32,
39, 38, 32,
38, 37, 40,
36, 40, 28,
31, 38, 36,
38, 40, 36,
40, 37, 41,
35, 41, 25,
28, 40, 35,
40, 41, 35,
41, 37, 42,
34, 42, 22,
25, 41, 34,
41, 42, 34,
42, 37, 39,
33, 39, 19,
22, 42, 33,
42, 39, 33
};
StdVectorOfVec3d vertices;
vertices.reserve(buffVertices.size() / 3);
for (size_t i = 0; i < buffVertices.size() / 3; ++i)
{
vertices.emplace_back(Vec3d(buffVertices[i * 3],
buffVertices[i * 3 + 1],
buffVertices[i * 3 + 2]));
}
std::vector<std::array<size_t, 3>> faces;
faces.reserve(buffFaces.size() / 3);
for (size_t i = 0; i < buffFaces.size() / 3; ++i)
{
// Face ID of triangles is 0-based index (data from .obj file is 1-based index)
std::array<size_t, 3> tmp;
tmp[0] = buffFaces[i * 3] - 1;
tmp[1] = buffFaces[i * 3 + 1] - 1;
tmp[2] = buffFaces[i * 3 + 2] - 1;
faces.push_back(std::move(tmp));
}
return { vertices, faces };
}
/*=========================================================================
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.
=========================================================================*/
#include "imstkMath.h"
#include <array>
using namespace imstk;
std::pair<StdVectorOfVec3d, std::vector<std::array<size_t, 3>>>
getTriangle()
{
std::vector<double> buffVertices
{
1.0, 0, 0,
0, 0, 2.0,
0, 0, -2.0
};
std::vector<size_t> buffFaces
{
1, 2, 3
};
StdVectorOfVec3d vertices;
vertices.reserve(buffVertices.size() / 3);
for (size_t i = 0; i < buffVertices.size() / 3; ++i)
{
vertices.emplace_back(Vec3d(buffVertices[i * 3],
buffVertices[i * 3 + 1],
buffVertices[i * 3 + 2]));
}
std::vector<std::array<size_t, 3>> faces;
faces.reserve(buffFaces.size() / 3);
for (size_t i = 0; i < buffFaces.size() / 3; ++i)
{
// Face ID of triangles is 0-based index (data from .obj file is 1-based index)
std::array<size_t, 3> tmp;
tmp[0] = buffFaces[i * 3] - 1;
tmp[1] = buffFaces[i * 3 + 1] - 1;
tmp[2] = buffFaces[i * 3 + 2] - 1;
faces.push_back(std::move(tmp));
}
return { vertices, faces };
}
###########################################################################
#
# Copyright (c) Kitware, Inc.
#
# 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.
#
###########################################################################
#-----------------------------------------------------------------------------
# Add Example subdirectories
#-----------------------------------------------------------------------------
macro(listOfSubDir result curdir)
file(GLOB children RELATIVE ${curdir} ${curdir}/*)
set(dirlist "")
foreach(child ${children})
if(IS_DIRECTORY ${curdir}/${child})
list(APPEND dirlist ${child})
endif()
endforeach()
set(${result} ${dirlist})
endmacro()
project(Example-PBDCollision)
listOfSubDir(subDirs ${CMAKE_CURRENT_SOURCE_DIR})
#-----------------------------------------------------------------------------
# Create executable
#-----------------------------------------------------------------------------
add_executable(${PROJECT_NAME} PBDCollisionExample.cpp)
foreach(subdir ${subDirs})
add_subdirectory(${subdir})
endforeach()
#-----------------------------------------------------------------------------
# Add shaders
#-----------------------------------------------------------------------------
include(imstkCopyAndCompileShaders)
CopyAndCompileShaders()
#-----------------------------------------------------------------------------
# Link libraries to executable
#-----------------------------------------------------------------------------
target_link_libraries(${PROJECT_NAME} SimulationManager)
#-----------------------------------------------------------------------------
# Associate external data
#-----------------------------------------------------------------------------
list(APPEND FILE_LIST
asianDragon/,REGEX:.*)
imstk_add_data(${PROJECT_NAME} ${FILE_LIST})
\ No newline at end of file
###########################################################################
#
# Copyright (c) Kitware, Inc.
#
# 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.
#
###########################################################################
project(Example-PBDCollision-ManyDragons)
#-----------------------------------------------------------------------------
# Create executable
#-----------------------------------------------------------------------------
add_executable(${PROJECT_NAME} PBDCollisionManyDragonsExample.cpp)
#-----------------------------------------------------------------------------
# Add shaders
#-----------------------------------------------------------------------------
include(imstkCopyAndCompileShaders)
CopyAndCompileShaders()
#-----------------------------------------------------------------------------
# Link libraries to executable
#-----------------------------------------------------------------------------
target_link_libraries(${PROJECT_NAME} SimulationManager)
#-----------------------------------------------------------------------------
# Associate external data
#-----------------------------------------------------------------------------
list(APPEND FILE_LIST
asianDragon/,REGEX:.*)
imstk_add_data(${PROJECT_NAME} ${FILE_LIST})
\ No newline at end of file
###########################################################################
#
# Copyright (c) Kitware, Inc.
#
# 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.
#
###########################################################################
project(Example-PBDCollision-OneDragon)
#-----------------------------------------------------------------------------
# Create executable
#-----------------------------------------------------------------------------
add_executable(${PROJECT_NAME} PBDCollisionOneDragonExample.cpp)
#-----------------------------------------------------------------------------
# Add shaders
#-----------------------------------------------------------------------------
include(imstkCopyAndCompileShaders)
CopyAndCompileShaders()
#-----------------------------------------------------------------------------
# Link libraries to executable
#-----------------------------------------------------------------------------
target_link_libraries(${PROJECT_NAME} SimulationManager)
#-----------------------------------------------------------------------------
# Associate external data
#-----------------------------------------------------------------------------
list(APPEND FILE_LIST
asianDragon/,REGEX:.*)
imstk_add_data(${PROJECT_NAME} ${FILE_LIST})
\ No newline at end of file
......@@ -22,6 +22,7 @@
#pragma once
#include <array>
#include <algorithm>
#include "imstkMath.h"
#include "imstkLogUtility.h"
......@@ -70,6 +71,11 @@ public:
m_Data[idx] = data;
}
///
/// \brief Sort the collision data using the provided compare function
template<class Comp>
void sort(Comp&& comp) { std::sort(m_Data.begin(), m_Data.end(), comp); }
///
/// \brief Check if the data array is emtpy
///
......
......@@ -21,12 +21,13 @@
#include "imstkCollisionDetection.h"
#include "imstkCollisionData.h"
#include "imstkOctreeBasedCD.h"
// Points to objects
#include "imstkPointSetToCapsuleCD.h"
#include "imstkPointSetToSphereCD.h"
#include "imstkPointSetToPlaneCD.h"
#include "imstkPointSetToVolumeMeshCD.h"
#include "imstkPointSetToSurfaceMeshCD.h"
#include "imstkPointSetToSpherePickingCD.h"
// Mesh to mesh
......@@ -95,19 +96,25 @@ CollisionDetection::makeCollisionDetectionObject(const Type
IMSTK_CHECK_FOR_VALID_GEOMETRIES(pointset, sphere)
return std::make_shared<PointSetToSpherePickingCD>(pointset, sphere, colData);
}
case Type::PointSetToVolumeMesh:
case Type::PointSetToSurfaceMesh:
{
auto pointset = std::dynamic_pointer_cast<PointSet>(objA->getCollidingGeometry());
auto triMesh = std::dynamic_pointer_cast<SurfaceMesh>(objB->getCollidingGeometry());
const auto& geomA = objA->getCollidingGeometry();
const auto& geomB = objB->getCollidingGeometry();
auto pointset = std::dynamic_pointer_cast<PointSet>(geomA);
auto triMesh = std::dynamic_pointer_cast<SurfaceMesh>(geomB);
IMSTK_CHECK_FOR_VALID_GEOMETRIES(pointset, triMesh)
return std::make_shared<PointSetToVolumeMeshCD>(pointset, triMesh, colData);
addCollisionPairToOctree(geomA, geomB, type, colData);
return std::make_shared<PointSetToSurfaceMeshCD>(pointset, triMesh, colData);
}
// Mesh to mesh
case Type::SurfaceMeshToSurfaceMesh:
{
auto meshA = std::dynamic_pointer_cast<SurfaceMesh>(objA->getCollidingGeometry());
auto meshB = std::dynamic_pointer_cast<SurfaceMesh>(objB->getCollidingGeometry());
const auto& geomA = objA->getCollidingGeometry();
const auto& geomB = objB->getCollidingGeometry();
auto meshA = std::dynamic_pointer_cast<SurfaceMesh>(geomA);
auto meshB = std::dynamic_pointer_cast<SurfaceMesh>(geomB);
IMSTK_CHECK_FOR_VALID_GEOMETRIES(meshA, meshB)
addCollisionPairToOctree(geomA, geomB, type, colData);
return std::make_shared<SurfaceMeshToSurfaceMeshCD>(meshA, meshB, colData);
}
case Type::SurfaceMeshToSurfaceMeshCCD:
......@@ -168,4 +175,54 @@ CollisionDetection::CollisionDetection(const CollisionDetection::Type& type, std
{
m_colData = (colData == nullptr) ? std::make_shared<CollisionData>() : colData;
}
// Static functions ==>
void
CollisionDetection::addCollisionPairToOctree(const std::shared_ptr<Geometry>& geomA,
const std::shared_ptr<Geometry>& geomB,
const Type collisionType,
const std::shared_ptr<CollisionData>& collisionData)
{
auto addToOctree =
[&](const std::shared_ptr<Geometry>& geom) {
if (!s_OctreeCD->hasGeometry(geom->getGlobalIndex()))
{
if (geom->getType() == Geometry::Type::PointSet)
{
s_OctreeCD->addPointSet(std::dynamic_pointer_cast<PointSet>(geom));
}
else if (geom->getType() == Geometry::Type::SurfaceMesh)
{
s_OctreeCD->addTriangleMesh(std::dynamic_pointer_cast<SurfaceMesh>(geom));
}
else
{
s_OctreeCD->addAnalyticalGeometry(geom);
}
}
};
addToOctree(geomA);
addToOctree(geomB);
s_OctreeCD->addCollisionPair(geomA, geomB, collisionType, collisionData);
}
void
CollisionDetection::updateInternalOctreeAndDetectCollision()
{
if (s_OctreeCD->getNumCollisionPairs() > 0)
{
s_OctreeCD->update();
s_OctreeCD->detectCollision();
}
}
void
CollisionDetection::clearInternalOctree()
{
s_OctreeCD->clear();
}
// Static octree
std::shared_ptr<OctreeBasedCD> CollisionDetection::s_OctreeCD = std::make_shared<OctreeBasedCD>(Vec3d(0, 0, 0), 100.0, 0.1, 1);
}
......@@ -26,6 +26,8 @@
namespace imstk
{
class CollidingObject;
class OctreeBasedCD;
class Geometry;
struct CollisionData;
///
......@@ -47,7 +49,7 @@ public:
PointSetToPlane,
PointSetToCapsule,
PointSetToSpherePicking,
PointSetToVolumeMesh,
PointSetToSurfaceMesh,
// Mesh to mesh (mesh to analytical object = mesh vertices to analytical object)
SurfaceMeshToSurfaceMesh,
......@@ -66,6 +68,9 @@ public:
///
/// \brief Static factory for collision detection sub classes
/// If the collision pair is PointSet to SurfaceMesh, or SurfaceMesh to SurfaceMesh,
/// it will be added to an internal static octree for detecting collision
/// \todo Other collision pair may be considered to use octree too
///
static std::shared_ptr<CollisionDetection> makeCollisionDetectionObject(