Skip to content

Commit c4753ba

Browse files
authored
Refactor BroadPhase to Build from Vertex Boxes Directly (#187)
* Refactor BroadPhase to build from vertex boxes directly. - Moved the inclusion of the CCD library to a new position in CMakeLists.txt for better organization. - Updated CPM version from 0.40.2 to 0.42.0 in CPM.cmake. - Modified ccd_query_io.cmake to use the new CPMAddPackage syntax with options. - Updated finite_diff.cmake to use the latest version of the finite-diff package (1.0.3). - Adjusted libigl.cmake to use the new CPMAddPackage syntax with options. - Updated scalable_ccd.cmake to use the new CPMAddPackage syntax with options. - Refactored BroadPhase class methods in broad_phase.cpp and broad_phase.hpp to streamline the build process. - Enhanced HashGrid class methods in hash_grid.cpp and hash_grid.hpp for better clarity and efficiency. - Improved SpatialHash class methods in spatial_hash.cpp and spatial_hash.hpp to utilize precomputed AABBs. - Added new methods to SweepAndPrune and SweepAndTiniestQueue classes for building from precomputed AABBs. - Introduced a new function to suggest a good voxel size based on AABBs in voxel_size_heuristic.cpp and voxel_size_heuristic.hpp. - Optimized edge_edge_mollifier.cpp with FMA operator in gradient calculations. - Updated eigen_ext.tpp to improve error handling during matrix projections. - Added a static method to create an empty interval in interval.hpp. * Fix vertex box conversion in SweepAndTiniestQueue::build method * Test broad phase build from boxes * Fix initialization of vertex_ids in broad phase box test
1 parent 21edcc2 commit c4753ba

26 files changed

+406
-194
lines changed

CMakeLists.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,6 @@ target_link_libraries(ipc_toolkit PRIVATE tight_inclusion::tight_inclusion)
168168
include(scalable_ccd)
169169
target_link_libraries(ipc_toolkit PRIVATE scalable_ccd::scalable_ccd)
170170

171-
# CCD
172-
if(IPC_TOOLKIT_WITH_INEXACT_CCD)
173-
# Etienne Vouga's CTCD Library for the floating point root finding algorithm
174-
include(evouga_ccd)
175-
target_link_libraries(ipc_toolkit PRIVATE evouga::ccd)
176-
endif()
177-
178171
# SimpleBVH
179172
include(simple_bvh)
180173
target_link_libraries(ipc_toolkit PRIVATE simple_bvh::simple_bvh)
@@ -183,6 +176,13 @@ target_link_libraries(ipc_toolkit PRIVATE simple_bvh::simple_bvh)
183176
include(spdlog)
184177
target_link_libraries(ipc_toolkit PUBLIC spdlog::spdlog)
185178

179+
# CCD
180+
if(IPC_TOOLKIT_WITH_INEXACT_CCD)
181+
# Etienne Vouga's CTCD Library for the floating point root finding algorithm
182+
include(evouga_ccd)
183+
target_link_libraries(ipc_toolkit PRIVATE evouga::ccd)
184+
endif()
185+
186186
# rational-cpp (requires GMP)
187187
if(IPC_TOOLKIT_WITH_RATIONAL_INTERSECTION)
188188
include(rational_cpp)

cmake/recipes/CPM.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(CPM_DOWNLOAD_VERSION 0.40.2)
1+
set(CPM_DOWNLOAD_VERSION 0.42.0)
22

33
if(CPM_SOURCE_CACHE)
44
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")

cmake/recipes/ccd_query_io.cmake

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ message(STATUS "Third-party: creating target 'ccd_io::ccd_io'")
88

99
include(ipc_toolkit_tests_data)
1010

11-
set(CCD_IO_DOWNLOAD_SAMPLE_QUERIES ON CACHE BOOL "Download sample CCD queries" FORCE)
12-
set(CCD_IO_SAMPLE_QUERIES_DIR "${IPC_TOOLKIT_TESTS_DATA_DIR}/ccd-queries/" CACHE PATH "Where should we download sample queries?")
13-
1411
include(CPM)
15-
CPMAddPackage("gh:Continuous-Collision-Detection/CCD-Query-IO#36f6093af81a65acc27d9f05ad32d6b5729e8d15")
12+
CPMAddPackage(
13+
URI "gh:Continuous-Collision-Detection/CCD-Query-IO#36f6093af81a65acc27d9f05ad32d6b5729e8d15"
14+
OPTIONS "CCD_IO_DOWNLOAD_SAMPLE_QUERIES ON"
15+
"CCD_IO_SAMPLE_QUERIES_DIR ${IPC_TOOLKIT_TESTS_DATA_DIR}/ccd-queries/"
16+
)

cmake/recipes/finite_diff.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ endif()
77
message(STATUS "Third-party: creating target 'finitediff::finitediff'")
88

99
include(CPM)
10-
CPMAddPackage("gh:zfergus/[email protected].1")
10+
CPMAddPackage("gh:zfergus/[email protected].3")
1111

1212
# Folder name for IDE
1313
set_target_properties(finitediff_finitediff PROPERTIES FOLDER "ThirdParty")

cmake/recipes/libigl.cmake

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ endif()
66

77
message(STATUS "Third-party: creating target 'igl::core'")
88

9-
set(LIBIGL_PREDICATES ON CACHE BOOL "Use exact predicates" FORCE)
10-
119
include(eigen)
1210

1311
include(CPM)
14-
CPMAddPackage("gh:libigl/libigl#89267b4a80b1904de3f6f2812a2053e5e9332b7e")
12+
CPMAddPackage(
13+
URI "gh:libigl/libigl#89267b4a80b1904de3f6f2812a2053e5e9332b7e"
14+
OPTIONS "LIBIGL_PREDICATES ON"
15+
)
1516

1617
# Folder name for IDE
1718
foreach(target_name IN ITEMS core predicates)

cmake/recipes/scalable_ccd.cmake

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ endif()
66

77
message(STATUS "Third-party: creating target 'scalable_ccd::scalable_ccd'")
88

9-
set(SCALABLE_CCD_WITH_CUDA ${IPC_TOOLKIT_WITH_CUDA} CACHE BOOL "Enable CUDA CCD" FORCE)
10-
119
include(CPM)
12-
CPMAddPackage("gh:continuous-collision-detection/scalable-ccd#4fa806f533b19132e696a2dddeab16537025b5f9")
10+
CPMAddPackage(
11+
URI "gh:continuous-collision-detection/scalable-ccd#4fa806f533b19132e696a2dddeab16537025b5f9"
12+
OPTIONS "SCALABLE_CCD_WITH_CUDA ${IPC_TOOLKIT_WITH_CUDA}"
13+
)
1314

1415
# Folder name for IDE
1516
set_target_properties(scalable_ccd PROPERTIES FOLDER "ThirdParty")

src/ipc/broad_phase/broad_phase.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ void BroadPhase::build(
1111
Eigen::ConstRef<Eigen::MatrixXi> faces,
1212
const double inflation_radius)
1313
{
14-
assert(edges.size() == 0 || edges.cols() == 2);
15-
assert(faces.size() == 0 || faces.cols() == 3);
1614
clear();
1715
build_vertex_boxes(vertices, vertex_boxes, inflation_radius);
18-
build_edge_boxes(vertex_boxes, edges, edge_boxes);
19-
build_face_boxes(vertex_boxes, faces, face_boxes);
16+
build(edges, faces);
2017
}
2118

2219
void BroadPhase::build(
@@ -26,11 +23,32 @@ void BroadPhase::build(
2623
Eigen::ConstRef<Eigen::MatrixXi> faces,
2724
const double inflation_radius)
2825
{
29-
assert(edges.size() == 0 || edges.cols() == 2);
30-
assert(faces.size() == 0 || faces.cols() == 3);
3126
clear();
3227
build_vertex_boxes(
3328
vertices_t0, vertices_t1, vertex_boxes, inflation_radius);
29+
build(edges, faces);
30+
}
31+
32+
void BroadPhase::build(
33+
const std::vector<AABB>& _vertex_boxes,
34+
Eigen::ConstRef<Eigen::MatrixXi> edges,
35+
Eigen::ConstRef<Eigen::MatrixXi> faces)
36+
{
37+
clear();
38+
39+
assert(&(this->vertex_boxes) != &_vertex_boxes);
40+
this->vertex_boxes = _vertex_boxes;
41+
42+
build(edges, faces);
43+
}
44+
45+
void BroadPhase::build(
46+
Eigen::ConstRef<Eigen::MatrixXi> edges,
47+
Eigen::ConstRef<Eigen::MatrixXi> faces)
48+
{
49+
assert(vertex_boxes.size() > 0);
50+
assert(edges.size() == 0 || edges.cols() == 2);
51+
assert(faces.size() == 0 || faces.cols() == 3);
3452
build_edge_boxes(vertex_boxes, edges, edge_boxes);
3553
build_face_boxes(vertex_boxes, faces, face_boxes);
3654
}

src/ipc/broad_phase/broad_phase.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ class BroadPhase {
4848
Eigen::ConstRef<Eigen::MatrixXi> faces,
4949
const double inflation_radius = 0);
5050

51+
/// @brief Build the broad phase for collision detection.
52+
/// @param vertex_boxes AABBs for the vertices.
53+
/// @param edges Collision mesh edges
54+
/// @param faces Collision mesh faces
55+
virtual void build(
56+
const std::vector<AABB>& vertex_boxes,
57+
Eigen::ConstRef<Eigen::MatrixXi> edges,
58+
Eigen::ConstRef<Eigen::MatrixXi> faces);
59+
5160
/// @brief Clear any built data.
5261
virtual void clear();
5362

@@ -91,6 +100,14 @@ class BroadPhase {
91100
default_can_vertices_collide;
92101

93102
protected:
103+
/// @brief Build the broad phase for collision detection.
104+
/// @note Assumes the vertex_boxes have been built.
105+
/// @param edges Collision mesh edges
106+
/// @param faces Collision mesh faces
107+
virtual void build(
108+
Eigen::ConstRef<Eigen::MatrixXi> edges,
109+
Eigen::ConstRef<Eigen::MatrixXi> faces);
110+
94111
virtual bool can_edge_vertex_collide(size_t ei, size_t vi) const;
95112
virtual bool can_edges_collide(size_t eai, size_t ebi) const;
96113
virtual bool can_face_vertex_collide(size_t fi, size_t vi) const;

src/ipc/broad_phase/bvh.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,10 @@ BVH::BVH()
2222
BVH::~BVH() = default;
2323

2424
void BVH::build(
25-
Eigen::ConstRef<Eigen::MatrixXd> vertices,
2625
Eigen::ConstRef<Eigen::MatrixXi> edges,
27-
Eigen::ConstRef<Eigen::MatrixXi> faces,
28-
const double inflation_radius)
26+
Eigen::ConstRef<Eigen::MatrixXi> faces)
2927
{
30-
BroadPhase::build(vertices, edges, faces, inflation_radius);
31-
init_bvh(vertex_boxes, *vertex_bvh);
32-
init_bvh(edge_boxes, *edge_bvh);
33-
init_bvh(face_boxes, *face_bvh);
34-
}
35-
36-
void BVH::build(
37-
Eigen::ConstRef<Eigen::MatrixXd> vertices_t0,
38-
Eigen::ConstRef<Eigen::MatrixXd> vertices_t1,
39-
Eigen::ConstRef<Eigen::MatrixXi> edges,
40-
Eigen::ConstRef<Eigen::MatrixXi> faces,
41-
const double inflation_radius)
42-
{
43-
BroadPhase::build(vertices_t0, vertices_t1, edges, faces, inflation_radius);
28+
BroadPhase::build(edges, faces); // Build edge_boxes and face_boxes
4429
init_bvh(vertex_boxes, *vertex_bvh);
4530
init_bvh(edge_boxes, *edge_bvh);
4631
init_bvh(face_boxes, *face_bvh);
@@ -74,9 +59,9 @@ void BVH::detect_candidates(
7459
const std::function<bool(size_t, size_t)>& can_collide,
7560
std::vector<Candidate>& candidates)
7661
{
77-
// O(n^2) or O(n^3) to build
78-
// O(klog(n)) to do a single look up
79-
// O(knlog(n)) to do all look ups
62+
// O(n) or O(n⋅log(n)) to build
63+
// O(k⋅log(n)) to do a single look up
64+
// O(k⋅n⋅log(n)) to do all look ups
8065

8166
tbb::enumerable_thread_specific<std::vector<Candidate>> storage;
8267

src/ipc/broad_phase/bvh.hpp

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,7 @@ class BVH : public BroadPhase {
2020
/// @return The name of the broad phase method.
2121
std::string name() const override { return "BVH"; }
2222

23-
/// @brief Build the broad phase for static collision detection.
24-
/// @param vertices Vertex positions
25-
/// @param edges Collision mesh edges
26-
/// @param faces Collision mesh faces
27-
/// @param inflation_radius Radius of inflation around all elements.
28-
void build(
29-
Eigen::ConstRef<Eigen::MatrixXd> vertices,
30-
Eigen::ConstRef<Eigen::MatrixXi> edges,
31-
Eigen::ConstRef<Eigen::MatrixXi> faces,
32-
const double inflation_radius = 0) override;
33-
34-
/// @brief Build the broad phase for continuous collision detection.
35-
/// @param vertices_t0 Starting vertices of the vertices.
36-
/// @param vertices_t1 Ending vertices of the vertices.
37-
/// @param edges Collision mesh edges
38-
/// @param faces Collision mesh faces
39-
/// @param inflation_radius Radius of inflation around all elements.
40-
void build(
41-
Eigen::ConstRef<Eigen::MatrixXd> vertices_t0,
42-
Eigen::ConstRef<Eigen::MatrixXd> vertices_t1,
43-
Eigen::ConstRef<Eigen::MatrixXi> edges,
44-
Eigen::ConstRef<Eigen::MatrixXi> faces,
45-
const double inflation_radius = 0) override;
23+
using BroadPhase::build;
4624

4725
/// @brief Clear any built data.
4826
void clear() override;
@@ -78,6 +56,14 @@ class BVH : public BroadPhase {
7856
std::vector<FaceFaceCandidate>& candidates) const override;
7957

8058
protected:
59+
/// @brief Build the broad phase for collision detection.
60+
/// @note Assumes the vertex_boxes have been built.
61+
/// @param edges Collision mesh edges
62+
/// @param faces Collision mesh faces
63+
void build(
64+
Eigen::ConstRef<Eigen::MatrixXi> edges,
65+
Eigen::ConstRef<Eigen::MatrixXi> faces) override;
66+
8167
/// @brief Initialize a BVH from a set of boxes.
8268
/// @param[in] boxes Set of boxes to initialize the BVH with.
8369
/// @param[out] bvh The BVH to initialize.

0 commit comments

Comments
 (0)