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
use std::hash::Hash;
use generational_arena::Arena;
use na::RealField;
use crate::world::GeometricalWorld;
use crate::object::{Body, DefaultColliderHandle, RigidBody, Multibody};
pub trait BodyHandle: Copy + Hash + PartialEq + Eq + 'static + Send + Sync {
}
impl<T: Copy + Hash + PartialEq + Eq + 'static + Send + Sync> BodyHandle for T {
}
pub trait BodySet<N: RealField> {
type Body: ?Sized + Body<N>;
type Handle: BodyHandle;
fn get(&self, handle: Self::Handle) -> Option<&Self::Body>;
fn get_mut(&mut self, handle: Self::Handle) -> Option<&mut Self::Body>;
fn get_pair_mut(&mut self, handle1: Self::Handle, handle2: Self::Handle) -> (Option<&mut Self::Body>, Option<&mut Self::Body>);
fn get_pair(&self, handle1: Self::Handle, handle2: Self::Handle) -> (Option<&Self::Body>, Option<&Self::Body>) {
(self.get(handle1), self.get(handle2))
}
fn contains(&self, handle: Self::Handle) -> bool;
fn foreach(&self, f: impl FnMut(Self::Handle, &Self::Body));
fn foreach_mut(&mut self, f: impl FnMut(Self::Handle, &mut Self::Body));
fn pop_removal_event(&mut self) -> Option<Self::Handle>;
}
pub struct DefaultBodySet<N: RealField> {
bodies: Arena<Box<dyn Body<N>>>,
removed: Vec<DefaultBodyHandle>,
}
impl<N: RealField> DefaultBodySet<N> {
pub fn new() -> Self {
DefaultBodySet {
bodies: Arena::new(),
removed: Vec::new(),
}
}
pub fn insert(&mut self, body: impl Body<N>) -> DefaultBodyHandle {
self.bodies.insert(Box::new(body))
}
pub fn insert_boxed(&mut self, body: Box<dyn Body<N>>) -> DefaultBodyHandle {
self.bodies.insert(body)
}
pub fn remove(&mut self, to_remove: DefaultBodyHandle) -> Option<Box<dyn Body<N>>> {
let res = self.bodies.remove(to_remove)?;
self.removed.push(to_remove);
Some(res)
}
fn contains(&self, handle: DefaultBodyHandle) -> bool {
self.bodies.contains(handle)
}
pub fn get(&self, handle: DefaultBodyHandle) -> Option<&dyn Body<N>> {
self.bodies.get(handle).map(|b| &**b)
}
pub fn get_mut(&mut self, handle: DefaultBodyHandle) -> Option<&mut dyn Body<N>> {
self.bodies.get_mut(handle).map(|b| &mut **b)
}
pub fn iter(&self) -> impl Iterator<Item = (DefaultBodyHandle, &dyn Body<N>)> {
self.bodies.iter().map(|b| (b.0, &**b.1))
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = (DefaultBodyHandle, &mut dyn Body<N>)> {
self.bodies.iter_mut().map(|b| (b.0, &mut **b.1))
}
pub fn rigid_body(&self, handle: DefaultBodyHandle) -> Option<&RigidBody<N>> {
self.get(handle).and_then(|b| b.downcast_ref())
}
pub fn rigid_body_mut(&mut self, handle: DefaultBodyHandle) -> Option<&mut RigidBody<N>> {
self.get_mut(handle).and_then(|b| b.downcast_mut())
}
pub fn multibody(&self, handle: DefaultBodyHandle) -> Option<&Multibody<N>> {
self.get(handle).and_then(|b| b.downcast_ref())
}
pub fn multibody_mut(&mut self, handle: DefaultBodyHandle) -> Option<&mut Multibody<N>> {
self.get_mut(handle).and_then(|b| b.downcast_mut())
}
}
impl<N: RealField> BodySet<N> for DefaultBodySet<N> {
type Body = dyn Body<N>;
type Handle = DefaultBodyHandle;
fn get(&self, handle: Self::Handle) -> Option<&Self::Body> {
self.get(handle)
}
fn get_mut(&mut self, handle: Self::Handle) -> Option<&mut Self::Body> {
self.get_mut(handle)
}
fn get_pair_mut(&mut self, handle1: Self::Handle, handle2: Self::Handle) -> (Option<&mut Self::Body>, Option<&mut Self::Body>) {
assert_ne!(handle1, handle2, "Both body handles must not be equal.");
let b1 = self.get_mut(handle1).map(|b| b as *mut dyn Body<N>);
let b2 = self.get_mut(handle2).map(|b| b as *mut dyn Body<N>);
unsafe {
use std::mem;
(
b1.map(|b| mem::transmute(b)),
b2.map(|b| mem::transmute(b))
)
}
}
fn contains(&self, handle: Self::Handle) -> bool {
self.contains(handle)
}
fn foreach(&self, mut f: impl FnMut(Self::Handle, &Self::Body)) {
for (h, b) in self.iter() {
f(h, b)
}
}
fn foreach_mut(&mut self, mut f: impl FnMut(Self::Handle, &mut Self::Body)) {
for (h, b) in self.iter_mut() {
f(h, b)
}
}
fn pop_removal_event(&mut self) -> Option<Self::Handle> {
self.removed.pop()
}
}
pub type DefaultBodyHandle = generational_arena::Index;
pub type DefaultBodyPartHandle = BodyPartHandle<DefaultBodyHandle>;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct BodyPartHandle<Handle: BodyHandle>(pub Handle, pub usize);
pub trait BodyDesc<N: RealField> {
type Body: Body<N>;
fn build_with_handle(&self, gworld: &mut GeometricalWorld<N, DefaultBodyHandle, DefaultColliderHandle>, handle: DefaultBodyHandle) -> Self::Body;
}