|
1 | 1 | #include <common.hpp>
|
2 | 2 |
|
3 | 3 | #include <ipc/collision_mesh.hpp>
|
| 4 | +#include <ipc/utils/logger.hpp> |
4 | 5 |
|
5 | 6 | namespace py = pybind11;
|
6 | 7 | using namespace ipc;
|
7 | 8 |
|
| 9 | +#ifdef IPC_TOOLKIT_WITH_ABSEIL |
| 10 | +using MapCanCollide = std::unordered_map< |
| 11 | + std::pair<size_t, size_t>, |
| 12 | + bool, |
| 13 | + absl::Hash<std::pair<size_t, size_t>>>; |
| 14 | +#else |
| 15 | +using MapCanCollide = std::unordered_map< |
| 16 | + std::pair<size_t, size_t>, |
| 17 | + bool, |
| 18 | + Hash<std::pair<size_t, size_t>>>; |
| 19 | +#endif |
| 20 | + |
| 21 | +/// @brief A functor which the value of the pair if it is in the map, otherwise a default value. |
| 22 | +class SparseCanCollide { |
| 23 | +public: |
| 24 | + /// @brief Construct a new Sparse Can Collide object. |
| 25 | + /// @param explicit_values A map from vertex pairs to whether they can collide. Only the upper triangle is used. The map is assumed to be symmetric. |
| 26 | + /// @param default_value The default value to return if the pair is not in the map. |
| 27 | + SparseCanCollide(const MapCanCollide& explicit_values, bool default_value) |
| 28 | + : m_explicit_values(explicit_values) |
| 29 | + , m_default_value(default_value) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + /// @brief Can two vertices collide? |
| 34 | + /// @param i Index of the first vertex. |
| 35 | + /// @param j Index of the second vertex. |
| 36 | + /// @return The value of the pair if it is in the map, otherwise the default value. |
| 37 | + bool operator()(size_t i, size_t j) const |
| 38 | + { |
| 39 | + auto it = m_explicit_values.find({ std::min(i, j), std::max(i, j) }); |
| 40 | + |
| 41 | + assert( |
| 42 | + m_explicit_values.find({ std::max(i, j), std::min(i, j) }) |
| 43 | + == m_explicit_values.end()); |
| 44 | + |
| 45 | + if (it != m_explicit_values.end()) { |
| 46 | + return it->second; |
| 47 | + } |
| 48 | + return m_default_value; |
| 49 | + } |
| 50 | + |
| 51 | +private: |
| 52 | + const MapCanCollide m_explicit_values; |
| 53 | + const bool m_default_value; |
| 54 | +}; |
| 55 | + |
| 56 | +class VertexPatchesCanCollide { |
| 57 | +public: |
| 58 | + /// @brief Construct a new Vertex Patches Can Collide object. |
| 59 | + /// @param vertex_patches Vector of patches labels for each vertex. |
| 60 | + VertexPatchesCanCollide(const Eigen::VectorXi& vertex_patches) |
| 61 | + : m_vertex_patches(vertex_patches) |
| 62 | + { |
| 63 | + } |
| 64 | + |
| 65 | + /// @brief Can two vertices collide? |
| 66 | + /// @param i Index of the first vertex. |
| 67 | + /// @param j Index of the second vertex. |
| 68 | + /// @return true if the vertices are in different patches |
| 69 | + bool operator()(size_t i, size_t j) const |
| 70 | + { |
| 71 | + assert(i < m_vertex_patches.size()); |
| 72 | + assert(j < m_vertex_patches.size()); |
| 73 | + return m_vertex_patches[i] != m_vertex_patches[j]; |
| 74 | + } |
| 75 | + |
| 76 | + size_t num_vertices() const { return m_vertex_patches.size(); } |
| 77 | + |
| 78 | +private: |
| 79 | + const Eigen::VectorXi m_vertex_patches; |
| 80 | +}; |
| 81 | + |
8 | 82 | void define_collision_mesh(py::module_& m)
|
9 | 83 | {
|
| 84 | + py::class_<SparseCanCollide>( |
| 85 | + m, "SparseCanCollide", |
| 86 | + "A functor which the value of the pair if it is in the map, otherwise a default value.") |
| 87 | + .def( |
| 88 | + py::init<const MapCanCollide&, bool>(), |
| 89 | + R"ipc_Qu8mg5v7( |
| 90 | + Construct a new Sparse Can Collide object. |
| 91 | +
|
| 92 | + Parameters: |
| 93 | + explicit_values: A map from vertex pairs to whether they can collide. Only the upper triangle is used. The map is assumed to be symmetric. |
| 94 | + default_value: The default value to return if the pair is not in the map. |
| 95 | + )ipc_Qu8mg5v7", |
| 96 | + py::arg("explicit_values"), py::arg("default_value")) |
| 97 | + .def( |
| 98 | + "__call__", &SparseCanCollide::operator(), R"ipc_Qu8mg5v7( |
| 99 | + Can two vertices collide? |
| 100 | +
|
| 101 | + Parameters: |
| 102 | + i: Index of the first vertex. |
| 103 | + j: Index of the second vertex. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + The value of the pair if it is in the map, otherwise the default value. |
| 107 | + )ipc_Qu8mg5v7", |
| 108 | + py::arg("i"), py::arg("j")); |
| 109 | + |
| 110 | + py::class_<VertexPatchesCanCollide>( |
| 111 | + m, "VertexPatchesCanCollide", |
| 112 | + "A functor which returns true if the vertices are in different patches.") |
| 113 | + .def( |
| 114 | + py::init<const Eigen::VectorXi&>(), py::arg("vertex_patches"), |
| 115 | + R"ipc_Qu8mg5v7( |
| 116 | + Construct a new Vertex Patches Can Collide object. |
| 117 | +
|
| 118 | + Parameters: |
| 119 | + vertex_patches: Vector of patches labels for each vertex. |
| 120 | + )ipc_Qu8mg5v7") |
| 121 | + .def( |
| 122 | + "__call__", &VertexPatchesCanCollide::operator(), R"ipc_Qu8mg5v7( |
| 123 | + Can two vertices collide? |
| 124 | +
|
| 125 | + Parameters: |
| 126 | + i: Index of the first vertex. |
| 127 | + j: Index of the second vertex. |
| 128 | +
|
| 129 | + Returns: |
| 130 | + True if the vertices are in different patches. |
| 131 | + )ipc_Qu8mg5v7", |
| 132 | + py::arg("i"), py::arg("j")); |
| 133 | + |
10 | 134 | py::class_<CollisionMesh>(m, "CollisionMesh")
|
11 | 135 | .def(
|
12 | 136 | py::init<
|
@@ -309,8 +433,41 @@ void define_collision_mesh(py::module_& m)
|
309 | 433 | Matrix that maps from the faces' edges to rows in the edges matrix.
|
310 | 434 | )ipc_Qu8mg5v7",
|
311 | 435 | py::arg("faces"), py::arg("edges"))
|
312 |
| - .def_readwrite( |
313 |
| - "can_collide", &CollisionMesh::can_collide, |
| 436 | + .def_property( |
| 437 | + "can_collide", [](CollisionMesh& self) { return self.can_collide; }, |
| 438 | + [](CollisionMesh& self, const py::object& can_collide) { |
| 439 | + if (py::isinstance<SparseCanCollide>(can_collide)) { |
| 440 | + |
| 441 | + self.can_collide = py::cast<SparseCanCollide>(can_collide); |
| 442 | + |
| 443 | + } else if (py::isinstance<VertexPatchesCanCollide>( |
| 444 | + can_collide)) { |
| 445 | + |
| 446 | + const VertexPatchesCanCollide& vertex_patches_can_collide = |
| 447 | + py::cast<VertexPatchesCanCollide>(can_collide); |
| 448 | + |
| 449 | + if (self.num_vertices() |
| 450 | + != vertex_patches_can_collide.num_vertices()) { |
| 451 | + throw py::value_error( |
| 452 | + "The number of vertices in the VertexPatchesCanCollide object must match the number of vertices in the CollisionMesh."); |
| 453 | + } |
| 454 | + |
| 455 | + self.can_collide = vertex_patches_can_collide; |
| 456 | + |
| 457 | + } else if (py::isinstance<py::function>(can_collide)) { |
| 458 | + |
| 459 | + logger().warn( |
| 460 | + "Using a custom function for can_collide is deprecated because it is slow. " |
| 461 | + "Please use a SparseCanCollide or VertexPatchesCanCollide object."); |
| 462 | + self.can_collide = [can_collide](size_t i, size_t j) { |
| 463 | + return py::cast<bool>(can_collide(i, j)); |
| 464 | + }; |
| 465 | + |
| 466 | + } else { |
| 467 | + throw py::value_error( |
| 468 | + "Unknown type for can_collide. Must be a SparseCanCollide, VertexPatchesCanCollide, or a function."); |
| 469 | + } |
| 470 | + }, |
314 | 471 | R"ipc_Qu8mg5v7(
|
315 | 472 | A function that takes two vertex IDs and returns true if the vertices (and faces or edges containing the vertices) can collide.
|
316 | 473 |
|
|
0 commit comments