1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use crate::data::graph::{Direction, EdgeIndex, Graph, NodeIndex};
use crate::geometry::ColliderHandle;
pub type ColliderGraphIndex = NodeIndex;
pub type RigidBodyGraphIndex = NodeIndex;
pub type TemporaryInteractionIndex = EdgeIndex;
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Clone)]
pub struct InteractionGraph<T> {
pub(crate) graph: Graph<ColliderHandle, T>,
}
impl<T> InteractionGraph<T> {
pub fn new() -> Self {
InteractionGraph {
graph: Graph::with_capacity(10, 10),
}
}
pub fn raw_graph(&self) -> &Graph<ColliderHandle, T> {
&self.graph
}
pub(crate) fn invalid_graph_index() -> ColliderGraphIndex {
ColliderGraphIndex::new(crate::INVALID_U32)
}
pub(crate) fn is_graph_index_valid(index: ColliderGraphIndex) -> bool {
index.index() != crate::INVALID_USIZE
}
pub(crate) fn add_edge(
&mut self,
index1: ColliderGraphIndex,
index2: ColliderGraphIndex,
interaction: T,
) -> TemporaryInteractionIndex {
self.graph.add_edge(index1, index2, interaction)
}
pub(crate) fn remove_edge(
&mut self,
index1: ColliderGraphIndex,
index2: ColliderGraphIndex,
) -> Option<T> {
let id = self.graph.find_edge(index1, index2)?;
self.graph.remove_edge(id)
}
#[must_use = "The graph index of the collision object returned by this method has been changed to `id`."]
pub(crate) fn remove_node(&mut self, id: ColliderGraphIndex) -> Option<ColliderHandle> {
let _ = self.graph.remove_node(id);
self.graph.node_weight(id).cloned()
}
pub fn interactions(&self) -> impl Iterator<Item = &T> {
self.graph.raw_edges().iter().map(move |edge| &edge.weight)
}
pub fn interaction_pair(
&self,
id1: ColliderGraphIndex,
id2: ColliderGraphIndex,
) -> Option<(ColliderHandle, ColliderHandle, &T)> {
self.graph.find_edge(id1, id2).and_then(|edge| {
let endpoints = self.graph.edge_endpoints(edge)?;
let h1 = self.graph.node_weight(endpoints.0)?;
let h2 = self.graph.node_weight(endpoints.1)?;
let weight = self.graph.edge_weight(edge)?;
Some((*h1, *h2, weight))
})
}
pub fn interaction_pair_mut(
&mut self,
id1: ColliderGraphIndex,
id2: ColliderGraphIndex,
) -> Option<(ColliderHandle, ColliderHandle, &mut T)> {
let edge = self.graph.find_edge(id1, id2)?;
let endpoints = self.graph.edge_endpoints(edge)?;
let h1 = *self.graph.node_weight(endpoints.0)?;
let h2 = *self.graph.node_weight(endpoints.1)?;
let weight = self.graph.edge_weight_mut(edge)?;
Some((h1, h2, weight))
}
pub fn interactions_with(
&self,
id: ColliderGraphIndex,
) -> impl Iterator<Item = (ColliderHandle, ColliderHandle, &T)> {
self.graph.edges(id).filter_map(move |e| {
let endpoints = self.graph.edge_endpoints(e.id()).unwrap();
Some((self.graph[endpoints.0], self.graph[endpoints.1], e.weight()))
})
}
pub fn index_interaction(
&self,
id: TemporaryInteractionIndex,
) -> Option<(ColliderHandle, ColliderHandle, &T)> {
if let (Some(e), Some(endpoints)) =
(self.graph.edge_weight(id), self.graph.edge_endpoints(id))
{
Some((self.graph[endpoints.0], self.graph[endpoints.1], e))
} else {
None
}
}
pub fn interactions_with_mut(
&mut self,
id: ColliderGraphIndex,
) -> impl Iterator<
Item = (
ColliderHandle,
ColliderHandle,
TemporaryInteractionIndex,
&mut T,
),
> {
let incoming_edge = self.graph.first_edge(id, Direction::Incoming);
let outgoing_edge = self.graph.first_edge(id, Direction::Outgoing);
InteractionsWithMut {
graph: &mut self.graph,
incoming_edge,
outgoing_edge,
}
}
}
pub struct InteractionsWithMut<'a, T> {
graph: &'a mut Graph<ColliderHandle, T>,
incoming_edge: Option<EdgeIndex>,
outgoing_edge: Option<EdgeIndex>,
}
impl<'a, T> Iterator for InteractionsWithMut<'a, T> {
type Item = (
ColliderHandle,
ColliderHandle,
TemporaryInteractionIndex,
&'a mut T,
);
#[inline]
fn next(
&mut self,
) -> Option<(
ColliderHandle,
ColliderHandle,
TemporaryInteractionIndex,
&'a mut T,
)> {
if let Some(edge) = self.incoming_edge {
self.incoming_edge = self.graph.next_edge(edge, Direction::Incoming);
let endpoints = self.graph.edge_endpoints(edge).unwrap();
let (co1, co2) = (self.graph[endpoints.0], self.graph[endpoints.1]);
let interaction = &mut self.graph[edge];
return Some((co1, co2, edge, unsafe { std::mem::transmute(interaction) }));
}
let edge = self.outgoing_edge?;
self.outgoing_edge = self.graph.next_edge(edge, Direction::Outgoing);
let endpoints = self.graph.edge_endpoints(edge).unwrap();
let (co1, co2) = (self.graph[endpoints.0], self.graph[endpoints.1]);
let interaction = &mut self.graph[edge];
Some((co1, co2, edge, unsafe { std::mem::transmute(interaction) }))
}
}