rapier3d/dynamics/joint/impulse_joint/
impulse_joint_set.rs1use parry::utils::hashset::HashSet;
2
3use super::ImpulseJoint;
4use crate::geometry::{InteractionGraph, RigidBodyGraphIndex, TemporaryInteractionIndex};
5
6use crate::data::Coarena;
7use crate::data::arena::Arena;
8use crate::dynamics::{GenericJoint, IslandManager, RigidBodyHandle, RigidBodySet};
9
10#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
13#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
14#[repr(transparent)]
15pub struct ImpulseJointHandle(pub crate::data::arena::Index);
16
17impl ImpulseJointHandle {
18 pub fn into_raw_parts(self) -> (u32, u32) {
20 self.0.into_raw_parts()
21 }
22
23 pub fn from_raw_parts(id: u32, generation: u32) -> Self {
25 Self(crate::data::arena::Index::from_raw_parts(id, generation))
26 }
27
28 pub fn invalid() -> Self {
30 Self(crate::data::arena::Index::from_raw_parts(
31 crate::INVALID_U32,
32 crate::INVALID_U32,
33 ))
34 }
35}
36
37pub(crate) type JointIndex = usize;
38pub(crate) type JointGraphEdge = crate::data::graph::Edge<ImpulseJoint>;
39
40#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
41#[derive(Clone, Default, Debug)]
42pub struct ImpulseJointSet {
44 rb_graph_ids: Coarena<RigidBodyGraphIndex>,
45 joint_ids: Arena<TemporaryInteractionIndex>,
47 joint_graph: InteractionGraph<RigidBodyHandle, ImpulseJoint>,
48 pub(crate) to_wake_up: HashSet<RigidBodyHandle>,
50}
51
52impl ImpulseJointSet {
53 pub fn new() -> Self {
55 Self {
56 rb_graph_ids: Coarena::new(),
57 joint_ids: Arena::new(),
58 joint_graph: InteractionGraph::new(),
59 to_wake_up: HashSet::default(),
60 }
61 }
62
63 pub fn len(&self) -> usize {
65 self.joint_graph.graph.edges.len()
66 }
67
68 pub fn is_empty(&self) -> bool {
70 self.joint_graph.graph.edges.is_empty()
71 }
72
73 pub fn joint_graph(&self) -> &InteractionGraph<RigidBodyHandle, ImpulseJoint> {
75 &self.joint_graph
76 }
77
78 pub fn joints_between(
80 &self,
81 body1: RigidBodyHandle,
82 body2: RigidBodyHandle,
83 ) -> impl Iterator<Item = (ImpulseJointHandle, &ImpulseJoint)> {
84 self.rb_graph_ids
85 .get(body1.0)
86 .zip(self.rb_graph_ids.get(body2.0))
87 .into_iter()
88 .flat_map(move |(id1, id2)| self.joint_graph.interaction_pair(*id1, *id2).into_iter())
89 .map(|inter| (inter.2.handle, inter.2))
90 }
91
92 pub fn attached_joints(
94 &self,
95 body: RigidBodyHandle,
96 ) -> impl Iterator<
97 Item = (
98 RigidBodyHandle,
99 RigidBodyHandle,
100 ImpulseJointHandle,
101 &ImpulseJoint,
102 ),
103 > {
104 self.rb_graph_ids
105 .get(body.0)
106 .into_iter()
107 .flat_map(move |id| self.joint_graph.interactions_with(*id))
108 .map(|inter| (inter.0, inter.1, inter.2.handle, inter.2))
109 }
110
111 pub fn map_attached_joints_mut(
113 &mut self,
114 body: RigidBodyHandle,
115 mut f: impl FnMut(RigidBodyHandle, RigidBodyHandle, ImpulseJointHandle, &mut ImpulseJoint),
116 ) {
117 self.rb_graph_ids.get(body.0).into_iter().for_each(|id| {
118 for inter in self.joint_graph.interactions_with_mut(*id) {
119 (f)(inter.0, inter.1, inter.3.handle, inter.3)
120 }
121 })
122 }
123
124 pub fn attached_enabled_joints(
126 &self,
127 body: RigidBodyHandle,
128 ) -> impl Iterator<
129 Item = (
130 RigidBodyHandle,
131 RigidBodyHandle,
132 ImpulseJointHandle,
133 &ImpulseJoint,
134 ),
135 > {
136 self.attached_joints(body)
137 .filter(|inter| inter.3.data.is_enabled())
138 }
139
140 pub fn contains(&self, handle: ImpulseJointHandle) -> bool {
142 self.joint_ids.contains(handle.0)
143 }
144
145 pub fn get(&self, handle: ImpulseJointHandle) -> Option<&ImpulseJoint> {
147 let id = self.joint_ids.get(handle.0)?;
148 self.joint_graph.graph.edge_weight(*id)
149 }
150
151 pub fn get_mut(
153 &mut self,
154 handle: ImpulseJointHandle,
155 wake_up_connected_bodies: bool,
156 ) -> Option<&mut ImpulseJoint> {
157 let id = self.joint_ids.get(handle.0)?;
158 let joint = self.joint_graph.graph.edge_weight_mut(*id);
159 if wake_up_connected_bodies {
160 if let Some(joint) = &joint {
161 self.to_wake_up.insert(joint.body1);
162 self.to_wake_up.insert(joint.body2);
163 }
164 }
165 joint
166 }
167
168 pub fn get_unknown_gen(&self, i: u32) -> Option<(&ImpulseJoint, ImpulseJointHandle)> {
178 let (id, handle) = self.joint_ids.get_unknown_gen(i)?;
179 Some((
180 self.joint_graph.graph.edge_weight(*id)?,
181 ImpulseJointHandle(handle),
182 ))
183 }
184
185 pub fn get_unknown_gen_mut(
195 &mut self,
196 i: u32,
197 ) -> Option<(&mut ImpulseJoint, ImpulseJointHandle)> {
198 let (id, handle) = self.joint_ids.get_unknown_gen(i)?;
199 Some((
200 self.joint_graph.graph.edge_weight_mut(*id)?,
201 ImpulseJointHandle(handle),
202 ))
203 }
204
205 pub fn iter(&self) -> impl Iterator<Item = (ImpulseJointHandle, &ImpulseJoint)> {
207 self.joint_graph
208 .graph
209 .edges
210 .iter()
211 .map(|e| (e.weight.handle, &e.weight))
212 }
213
214 pub fn iter_mut(&mut self) -> impl Iterator<Item = (ImpulseJointHandle, &mut ImpulseJoint)> {
216 self.joint_graph
217 .graph
218 .edges
219 .iter_mut()
220 .map(|e| (e.weight.handle, &mut e.weight))
221 }
222
223 #[allow(dead_code)] pub(crate) fn joints_mut(&mut self) -> &mut [JointGraphEdge] {
235 &mut self.joint_graph.graph.edges[..]
236 }
237
238 #[cfg(feature = "parallel")]
239 pub(crate) fn joints_vec_mut(&mut self) -> &mut Vec<JointGraphEdge> {
240 &mut self.joint_graph.graph.edges
241 }
242
243 #[profiling::function]
248 pub fn insert(
249 &mut self,
250 body1: RigidBodyHandle,
251 body2: RigidBodyHandle,
252 data: impl Into<GenericJoint>,
253 wake_up: bool,
254 ) -> ImpulseJointHandle {
255 let data = data.into();
256 let handle = self.joint_ids.insert(0.into());
257 let joint = ImpulseJoint {
258 body1,
259 body2,
260 data,
261 impulses: na::zero(),
262 handle: ImpulseJointHandle(handle),
263 };
264
265 let default_id = InteractionGraph::<(), ()>::invalid_graph_index();
266 let mut graph_index1 = *self
267 .rb_graph_ids
268 .ensure_element_exist(joint.body1.0, default_id);
269 let mut graph_index2 = *self
270 .rb_graph_ids
271 .ensure_element_exist(joint.body2.0, default_id);
272
273 if !InteractionGraph::<RigidBodyHandle, ImpulseJoint>::is_graph_index_valid(graph_index1) {
276 graph_index1 = self.joint_graph.graph.add_node(joint.body1);
277 self.rb_graph_ids.insert(joint.body1.0, graph_index1);
278 }
279
280 if !InteractionGraph::<RigidBodyHandle, ImpulseJoint>::is_graph_index_valid(graph_index2) {
281 graph_index2 = self.joint_graph.graph.add_node(joint.body2);
282 self.rb_graph_ids.insert(joint.body2.0, graph_index2);
283 }
284
285 self.joint_ids[handle] = self.joint_graph.add_edge(graph_index1, graph_index2, joint);
286
287 if wake_up {
288 self.to_wake_up.insert(body1);
289 self.to_wake_up.insert(body2);
290 }
291
292 ImpulseJointHandle(handle)
293 }
294
295 pub(crate) fn select_active_interactions(
298 &self,
299 islands: &IslandManager,
300 bodies: &RigidBodySet,
301 out: &mut [Vec<JointIndex>],
302 ) {
303 for out_island in &mut out[..islands.num_islands()] {
304 out_island.clear();
305 }
306
307 for (i, edge) in self.joint_graph.graph.edges.iter().enumerate() {
309 let joint = &edge.weight;
310 let rb1 = &bodies[joint.body1];
311 let rb2 = &bodies[joint.body2];
312
313 if joint.data.is_enabled()
314 && (rb1.is_dynamic() || rb2.is_dynamic())
315 && (!rb1.is_dynamic() || !rb1.is_sleeping())
316 && (!rb2.is_dynamic() || !rb2.is_sleeping())
317 {
318 let island_index = if !rb1.is_dynamic() {
319 rb2.ids.active_island_id
320 } else {
321 rb1.ids.active_island_id
322 };
323
324 out[island_index].push(i);
325 }
326 }
327 }
328
329 #[profiling::function]
334 pub fn remove(&mut self, handle: ImpulseJointHandle, wake_up: bool) -> Option<ImpulseJoint> {
335 let id = self.joint_ids.remove(handle.0)?;
336 let endpoints = self.joint_graph.graph.edge_endpoints(id)?;
337
338 if wake_up {
339 if let Some(rb_handle) = self.joint_graph.graph.node_weight(endpoints.0) {
340 self.to_wake_up.insert(*rb_handle);
341 }
342 if let Some(rb_handle) = self.joint_graph.graph.node_weight(endpoints.1) {
343 self.to_wake_up.insert(*rb_handle);
344 }
345 }
346
347 let removed_joint = self.joint_graph.graph.remove_edge(id);
348
349 if let Some(edge) = self.joint_graph.graph.edge_weight(id) {
350 self.joint_ids[edge.handle.0] = id;
351 }
352
353 removed_joint
354 }
355
356 #[profiling::function]
362 pub fn remove_joints_attached_to_rigid_body(
363 &mut self,
364 handle: RigidBodyHandle,
365 ) -> Vec<ImpulseJointHandle> {
366 let mut deleted = vec![];
367
368 if let Some(deleted_id) = self
369 .rb_graph_ids
370 .remove(handle.0, InteractionGraph::<(), ()>::invalid_graph_index())
371 {
372 if InteractionGraph::<(), ()>::is_graph_index_valid(deleted_id) {
373 let to_delete: Vec<_> = self
378 .joint_graph
379 .interactions_with(deleted_id)
380 .map(|e| (e.0, e.1, e.2.handle))
381 .collect();
382 for (h1, h2, to_delete_handle) in to_delete {
383 deleted.push(to_delete_handle);
384 let to_delete_edge_id = self.joint_ids.remove(to_delete_handle.0).unwrap();
385 self.joint_graph.graph.remove_edge(to_delete_edge_id);
386
387 if let Some(j) = self.joint_graph.graph.edge_weight_mut(to_delete_edge_id) {
389 self.joint_ids[j.handle.0] = to_delete_edge_id;
390 }
391
392 self.to_wake_up.insert(h1);
394 self.to_wake_up.insert(h2);
395 }
396
397 if let Some(other) = self.joint_graph.remove_node(deleted_id) {
398 self.rb_graph_ids.insert(other.0, deleted_id);
401 }
402 }
403 }
404
405 deleted
406 }
407}