Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 059cda0

Browse files
committed
fix bugs
1 parent 182eb72 commit 059cda0

File tree

5 files changed

+97
-101
lines changed

5 files changed

+97
-101
lines changed

python/src/collisions/collisions.cpp

Lines changed: 88 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,72 @@
66
namespace py = pybind11;
77
using namespace ipc;
88

9+
template <typename collision_type, typename parent_type>
10+
void define_SmoothCollisionTemplate(py::module_& m, std::string name)
11+
{
12+
py::class_<collision_type, parent_type>(m, name.c_str())
13+
.def("name", &collision_type::name, "Get the type name of collision")
14+
.def("num_vertices", &collision_type::num_vertices, "Get the number of vertices");
15+
}
16+
17+
template <int dim>
18+
void define_SmoothCollisions(py::module_& m, std::string name)
19+
{
20+
py::class_<SmoothCollisions<dim>>(m, name.c_str())
21+
.def(py::init())
22+
.def_readwrite("use_high_order_quadrature", &SmoothCollisions<dim>::use_high_order_quadrature)
23+
.def(
24+
"build",
25+
py::overload_cast<
26+
const CollisionMesh&, const Eigen::MatrixXd&, const ParameterType, const bool, const BroadPhaseMethod>(&SmoothCollisions<dim>::build),
27+
R"ipc_Qu8mg5v7(
28+
Initialize the set of collisions used to compute the barrier potential.
29+
30+
Parameters:
31+
mesh: The collision mesh.
32+
vertices: Vertices of the collision mesh.
33+
param: ParameterType.
34+
use_adaptive_dhat: If the adaptive dhat should be used.
35+
broad_phase: Broad phase method.
36+
)ipc_Qu8mg5v7",
37+
py::arg("mesh"), py::arg("vertices"), py::arg("param"),
38+
py::arg("use_adaptive_dhat") = false, py::arg("broad_phase") = DEFAULT_BROAD_PHASE_METHOD)
39+
.def(
40+
"compute_minimum_distance", &SmoothCollisions<dim>::compute_minimum_distance,
41+
R"ipc_Qu8mg5v7(
42+
Computes the minimum distance between any non-adjacent elements.
43+
44+
Parameters:
45+
mesh: The collision mesh.
46+
vertices: Vertices of the collision mesh.
47+
48+
Returns:
49+
The minimum distance between any non-adjacent elements.
50+
)ipc_Qu8mg5v7",
51+
py::arg("mesh"), py::arg("vertices"))
52+
.def("__len__", &SmoothCollisions<dim>::size, "Get the number of collisions.")
53+
.def("empty", &SmoothCollisions<dim>::empty, "Get if the collision set is empty.")
54+
.def("clear", &SmoothCollisions<dim>::clear, "Clear the collision set.")
55+
.def(
56+
"__getitem__",
57+
[](SmoothCollisions<dim>& self, size_t i) -> typename SmoothCollisions<dim>::value_type& { return self[i]; },
58+
py::return_value_policy::reference,
59+
R"ipc_Qu8mg5v7(
60+
Get a reference to collision at index i.
61+
62+
Parameters:
63+
i: The index of the collision.
64+
65+
Returns:
66+
A reference to the collision.
67+
)ipc_Qu8mg5v7",
68+
py::arg("i"))
69+
.def(
70+
"to_string", &SmoothCollisions<dim>::to_string, py::arg("mesh"),
71+
py::arg("vertices"), py::arg("param"))
72+
.def("n_candidates", &SmoothCollisions<dim>::n_candidates, "Get the number of candidates.");
73+
}
74+
975
void define_collisions(py::module_& m)
1076
{
1177
py::class_<Collisions>(m, "Collisions")
@@ -157,82 +223,39 @@ void define_collisions(py::module_& m)
157223
.def_readwrite("fv_collisions", &Collisions::fv_collisions)
158224
.def_readwrite("pv_collisions", &Collisions::pv_collisions);
159225

160-
161-
py::class_<SmoothCollisions<2>>(m, "SmoothCollisions2")
162-
.def(py::init())
226+
py::class_<SmoothCollision<6>>(m, "SmoothCollision2")
227+
.def("n_dofs", &SmoothCollision<6>::n_dofs, "Get the degree of freedom")
163228
.def(
164-
"build",
165-
py::overload_cast<
166-
const CollisionMesh&, const Eigen::MatrixXd&, const ParameterType, const bool, const BroadPhaseMethod>(&SmoothCollisions<2>::build),
229+
"__call__",
230+
&SmoothCollision<6>::operator(),
167231
R"ipc_Qu8mg5v7(
168-
Initialize the set of collisions used to compute the barrier potential.
169-
232+
Compute the potential.
233+
170234
Parameters:
171-
mesh: The collision mesh.
172-
vertices: Vertices of the collision mesh.
173-
param: ParameterType.
174-
use_adaptive_dhat: If the adaptive dhat should be used.
175-
broad_phase: Broad phase method.
235+
positions: The vertex positions.
236+
params: The parameters.
237+
238+
Returns:
239+
The potential (not scaled by the barrier stiffness) of this collision pair.
176240
)ipc_Qu8mg5v7",
177-
py::arg("mesh"), py::arg("vertices"), py::arg("param"),
178-
py::arg("use_adaptive_dhat") = false, py::arg("broad_phase") = DEFAULT_BROAD_PHASE_METHOD)
241+
py::arg("positions"), py::arg("params"))
179242
.def(
180-
"compute_minimum_distance", &SmoothCollisions<2>::compute_minimum_distance,
243+
"__getitem__",
244+
[](SmoothCollision<6>& self, size_t i) -> long { return self[i]; },
181245
R"ipc_Qu8mg5v7(
182-
Computes the minimum distance between any non-adjacent elements.
246+
Get primitive id.
183247
184248
Parameters:
185-
mesh: The collision mesh.
186-
vertices: Vertices of the collision mesh.
249+
i: 0 or 1.
187250
188251
Returns:
189-
The minimum distance between any non-adjacent elements.
190-
)ipc_Qu8mg5v7",
191-
py::arg("mesh"), py::arg("vertices"))
192-
.def("__len__", &SmoothCollisions<2>::size, "Get the number of collisions.")
193-
.def("empty", &SmoothCollisions<2>::empty, "Get if the collision set is empty.")
194-
.def("clear", &SmoothCollisions<2>::clear, "Clear the collision set.")
195-
.def(
196-
"to_string", &SmoothCollisions<2>::to_string, py::arg("mesh"),
197-
py::arg("vertices"), py::arg("param"))
198-
.def("n_candidates", &SmoothCollisions<2>::n_candidates, "Get the number of candidates.");
199-
200-
py::class_<SmoothCollisions<3>>(m, "SmoothCollisions3")
201-
.def(py::init())
202-
.def(
203-
"build",
204-
py::overload_cast<
205-
const CollisionMesh&, const Eigen::MatrixXd&, const ParameterType, const bool, const BroadPhaseMethod>(&SmoothCollisions<3>::build),
206-
R"ipc_Qu8mg5v7(
207-
Initialize the set of collisions used to compute the barrier potential.
208-
209-
Parameters:
210-
mesh: The collision mesh.
211-
vertices: Vertices of the collision mesh.
212-
param: ParameterType.
213-
use_adaptive_dhat: If the adaptive dhat should be used.
214-
broad_phase: Broad phase method.
252+
The index of the primitive.
215253
)ipc_Qu8mg5v7",
216-
py::arg("mesh"), py::arg("vertices"), py::arg("param"),
217-
py::arg("use_adaptive_dhat") = false, py::arg("broad_phase") = DEFAULT_BROAD_PHASE_METHOD)
218-
.def(
219-
"compute_minimum_distance", &SmoothCollisions<3>::compute_minimum_distance,
220-
R"ipc_Qu8mg5v7(
221-
Computes the minimum distance between any non-adjacent elements.
254+
py::arg("i"));
222255

223-
Parameters:
224-
mesh: The collision mesh.
225-
vertices: Vertices of the collision mesh.
256+
define_SmoothCollisionTemplate<SmoothCollisionTemplate<max_vert_2d, Edge2, Point2>, SmoothCollision<6>>(m, "Edge2Point2Collision");
257+
define_SmoothCollisionTemplate<SmoothCollisionTemplate<max_vert_2d, Point2, Point2>, SmoothCollision<6>>(m, "Point2Point2Collision");
226258

227-
Returns:
228-
The minimum distance between any non-adjacent elements.
229-
)ipc_Qu8mg5v7",
230-
py::arg("mesh"), py::arg("vertices"))
231-
.def("__len__", &SmoothCollisions<3>::size, "Get the number of collisions.")
232-
.def("empty", &SmoothCollisions<3>::empty, "Get if the collision set is empty.")
233-
.def("clear", &SmoothCollisions<3>::clear, "Clear the collision set.")
234-
.def(
235-
"to_string", &SmoothCollisions<3>::to_string, py::arg("mesh"),
236-
py::arg("vertices"), py::arg("param"))
237-
.def("n_candidates", &SmoothCollisions<3>::n_candidates, "Get the number of candidates.");
259+
define_SmoothCollisions<2>(m, "SmoothCollisions2");
260+
define_SmoothCollisions<3>(m, "SmoothCollisions3");
238261
}

src/ipc/collision_mesh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ std::vector<long> CollisionMesh::find_vertex_adjacent_vertices(const long &v) co
521521
std::vector<long> neighbors;
522522
if (dim() == 2)
523523
{
524-
neighbors.resize(2);
524+
neighbors.assign(2, -1);
525525
for (long i : vertex_edge_adjacencies()[v])
526526
{
527527
if (edges()(i, 0) == v)

src/ipc/smooth_contact/collisions/smooth_collision.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class SmoothCollisionTemplate : public SmoothCollision<max_vert> {
140140
const Eigen::MatrixXd& V);
141141
virtual ~SmoothCollisionTemplate() = default;
142142

143+
std::string name() const override;
144+
143145
inline int n_dofs() const override { return pA->n_dofs() + pB->n_dofs(); }
144146
CollisionType type() const override;
145147

src/ipc/smooth_contact/primitives/point2.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,6 @@ scalar smooth_point2_term(
7676
return tangent_term * normal_term * ((e0 - v).norm() + (e1 - v).norm()) / 2.;
7777
}
7878

79-
template double smooth_point2_term(
80-
const Eigen::Ref<const Vector2d>& v,
81-
const Eigen::Ref<const Vector2d>& direc,
82-
const Eigen::Ref<const Vector2d>& e0,
83-
const Eigen::Ref<const Vector2d>& e1,
84-
const ParameterType& param);
85-
template ADGrad<12> smooth_point2_term(
86-
const Eigen::Ref<const Vector2<ADGrad<12>>>& v,
87-
const Eigen::Ref<const Vector2<ADGrad<12>>>& direc,
88-
const Eigen::Ref<const Vector2<ADGrad<12>>>& e0,
89-
const Eigen::Ref<const Vector2<ADGrad<12>>>& e1,
90-
const ParameterType& param);
91-
template ADHessian<12> smooth_point2_term(
92-
const Eigen::Ref<const Vector2<ADHessian<12>>>& v,
93-
const Eigen::Ref<const Vector2<ADHessian<12>>>& direc,
94-
const Eigen::Ref<const Vector2<ADHessian<12>>>& e0,
95-
const Eigen::Ref<const Vector2<ADHessian<12>>>& e1,
96-
const ParameterType& param);
97-
template ADGrad<10> smooth_point2_term(
98-
const Eigen::Ref<const Vector2<ADGrad<10>>>& v,
99-
const Eigen::Ref<const Vector2<ADGrad<10>>>& direc,
100-
const Eigen::Ref<const Vector2<ADGrad<10>>>& e0,
101-
const Eigen::Ref<const Vector2<ADGrad<10>>>& e1,
102-
const ParameterType& param);
103-
template ADHessian<10> smooth_point2_term(
104-
const Eigen::Ref<const Vector2<ADHessian<10>>>& v,
105-
const Eigen::Ref<const Vector2<ADHessian<10>>>& direc,
106-
const Eigen::Ref<const Vector2<ADHessian<10>>>& e0,
107-
const Eigen::Ref<const Vector2<ADHessian<10>>>& e1,
108-
const ParameterType& param);
109-
11079
bool smooth_point2_term_type(
11180
const Eigen::Ref<const Vector2d>& v,
11281
const Eigen::Ref<const Vector2d>& direc,

src/ipc/smooth_contact/smooth_collisions_builder.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ void SmoothCollisionsBuilder<2>::add_edge_vertex_collisions(
6060
vert_edge_2_to_id, collisions);
6161

6262
for (int j : { 0, 1 }) {
63+
const auto& vj = mesh.edges()(ei, j);
64+
const double dhat = std::min(vert_dhat(vi), vert_dhat(vj));
65+
if ((vertices.row(vi) - vertices.row(vj)).norm() >= dhat)
66+
continue;
6367
add_collision<
6468
2, SmoothCollisionTemplate<max_vert_2d, Point2, Point2>>(
6569
std::make_shared<
6670
SmoothCollisionTemplate<max_vert_2d, Point2, Point2>>(
67-
mesh.edges()(ei, j), vi, PointPointDistanceType::AUTO, mesh,
68-
param,
69-
std::min(vert_dhat(mesh.edges()(ei, j)), vert_dhat(vi)),
70-
vertices),
71+
std::min<long>(vi, vj), std::max<long>(vi, vj), PointPointDistanceType::AUTO, mesh,
72+
param, dhat, vertices),
7173
vert_vert_2_to_id, collisions);
7274
}
7375
}

0 commit comments

Comments
 (0)