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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use {std, cgmath};
use std::{borrow::Borrow, borrow::BorrowMut};
use {collision, component, geometry, math};
use geometry::{shape, Shape};
pub type KeyType = u32;
const KEY_MAX : KeyType = collision::OBJECT_KEY_MAX;
pub trait Object : Clone + std::fmt::Debug {
fn kind() -> Kind;
fn position (&self) -> &component::Position;
fn position_mut (&mut self) -> &mut component::Position;
}
pub trait Bounded : Object + Shape <f64> {
fn bound (&self) -> &component::Bound;
fn bound_mut (&mut self) -> &mut component::Bound;
fn hit_test (&self, line_segment : &geometry::Simplex1 <f64>)
-> Option <cgmath::Point3 <f64>>
{
let component::Position (ref position) = self.position();
match self.bound().0 {
shape::Variant::Bounded (shape::Bounded::Sphere (ref sphere)) => {
line_segment.intersect_sphere (&sphere.sphere3 (*position))
.map (|((_, first), _)| first)
}
shape::Variant::Bounded (shape::Bounded::Capsule (ref capsule)) => {
line_segment.intersect_capsule (&capsule.capsule3 (*position))
.map (|((_, first), _)| first)
}
shape::Variant::Bounded (shape::Bounded::Cylinder (ref cylinder)) =>
{
line_segment.intersect_cylinder (&cylinder.cylinder3 (*position))
.map (|((_, first), _)| first)
}
shape::Variant::Bounded (shape::Bounded::Cone (ref _cone)) =>
unimplemented!(),
shape::Variant::Bounded (shape::Bounded::Cube (ref _cube)) =>
unimplemented!(),
shape::Variant::Bounded (shape::Bounded::Cuboid (ref _cuboid)) =>
unimplemented!(),
shape::Variant::Unbounded (shape::Unbounded::Halfspace (ref _halfspace))
=> unimplemented!(),
shape::Variant::Unbounded (shape::Unbounded::Orthant (ref _orthant))
=> unimplemented!()
}
}
}
pub trait Temporal : Object {
fn derivatives (&self) -> &component::Derivatives;
fn derivatives_mut (&mut self) -> &mut component::Derivatives;
}
pub trait Inertial : Temporal {
fn mass (&self) -> &component::Mass;
fn mass_mut (&mut self) -> &mut component::Mass;
fn momentum (&self) -> cgmath::Vector3 <f64> {
self.mass().mass() * self.derivatives().velocity
}
fn compute_acceleration (&self) -> cgmath::Vector3 <f64> {
self.mass().mass_reciprocal() * self.derivatives().force
}
fn compute_acceleration_inplace (&mut self) {
let acceleration = self.mass().mass_reciprocal() * self.derivatives().force;
self.derivatives_mut().acceleration = acceleration;
}
}
pub trait Massive : Bounded + Inertial {
fn density (&self) -> Option <f64> {
use geometry::shape::Stereometric;
if let component::Bound (shape::Variant::Bounded (ref bounded)) = self.bound() {
Some (self.mass().mass() / *bounded.volume())
} else {
None
}
}
}
pub trait Solid : Bounded {
fn material (&self) -> &component::Material;
fn material_mut (&mut self) -> &mut component::Material;
}
#[cfg_attr(feature = "derive_serdes", derive(Serialize, Deserialize))]
#[derive(Clone,Copy,Debug,Eq,PartialEq)]
pub enum Kind {
Static,
Dynamic,
Nodetect
}
#[derive(FromVariants,Clone,Debug,PartialEq)]
pub enum Variant {
Static (Static),
Dynamic (Dynamic),
Nodetect (Nodetect),
}
#[cfg_attr(feature = "derive_serdes", derive(Serialize, Deserialize))]
#[derive(Copy,Clone,Debug,Eq,Ord,PartialEq,PartialOrd)]
pub struct Key (KeyType);
#[cfg_attr(feature = "derive_serdes", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,Eq,PartialEq)]
pub struct Identifier {
pub kind : Kind,
pub key : Key
}
#[cfg_attr(feature = "derive_serdes", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,PartialEq)]
pub struct Static {
pub position : component::Position,
pub bound : component::Bound,
pub material : component::Material
}
#[cfg_attr(feature = "derive_serdes", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,PartialEq)]
pub struct Dynamic {
pub position : component::Position,
pub mass : component::Mass,
pub derivatives : component::Derivatives,
pub bound : component::Bound,
pub material : component::Material
}
#[cfg_attr(feature = "derive_serdes", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,PartialEq)]
pub struct Nodetect {
pub position : component::Position,
pub mass : component::Mass,
pub derivatives : component::Derivatives
}
pub fn report_sizes() {
use std::mem::size_of;
println!("object report sizes...");
println!(" size of Variant: {}", size_of::<Variant>());
println!(" size of Static: {}", size_of::<Static>());
println!(" size of Dynamic: {}", size_of::<Dynamic>());
println!(" size of Nodetect: {}", size_of::<Nodetect>());
println!("...object report sizes");
}
impl Object for Static {
fn kind() -> Kind { Kind::Static }
fn position (&self) -> &component::Position {
&self.position
}
fn position_mut (&mut self) -> &mut component::Position {
&mut self.position
}
}
impl Bounded for Static {
fn bound (&self) -> &component::Bound {
&self.bound
}
fn bound_mut (&mut self) -> &mut component::Bound {
&mut self.bound
}
}
impl Solid for Static {
fn material (&self) -> &component::Material {
&self.material
}
fn material_mut (&mut self) -> &mut component::Material {
&mut self.material
}
}
impl Shape <f64> for Static { }
impl shape::Aabb <f64> for Static {
fn aabb (&self) -> geometry::Aabb3 <f64> {
use cgmath::EuclideanSpace;
let component::Bound (ref variant) = self.bound();
let aabb_shape = variant.aabb();
geometry::Aabb3::with_minmax (
aabb_shape.min() + self.position().0.to_vec(),
aabb_shape.max() + self.position().0.to_vec())
}
}
impl shape::Stereometric <f64> for Static {
fn volume (&self) -> math::Positive <f64> {
let component::Bound (ref variant) = self.bound();
variant.volume()
}
}
impl Object for Dynamic {
fn kind() -> Kind { Kind::Dynamic }
fn position (&self) -> &component::Position {
&self.position
}
fn position_mut (&mut self) -> &mut component::Position {
&mut self.position
}
}
impl Bounded for Dynamic {
fn bound (&self) -> &component::Bound {
&self.bound
}
fn bound_mut (&mut self) -> &mut component::Bound {
&mut self.bound
}
}
impl Solid for Dynamic {
fn material (&self) -> &component::Material {
&self.material
}
fn material_mut (&mut self) -> &mut component::Material {
&mut self.material
}
}
impl Temporal for Dynamic {
fn derivatives (&self) -> &component::Derivatives {
&self.derivatives
}
fn derivatives_mut (&mut self) -> &mut component::Derivatives {
&mut self.derivatives
}
}
impl Inertial for Dynamic {
fn mass (&self) -> &component::Mass {
&self.mass
}
fn mass_mut (&mut self) -> &mut component::Mass {
&mut self.mass
}
}
impl Borrow <component::Derivatives> for Dynamic {
fn borrow (&self) -> &component::Derivatives {
&self.derivatives
}
}
impl BorrowMut <component::Derivatives> for Dynamic {
fn borrow_mut (&mut self) -> &mut component::Derivatives {
&mut self.derivatives
}
}
impl Shape <f64> for Dynamic { }
impl shape::Aabb <f64> for Dynamic {
fn aabb (&self) -> geometry::Aabb3 <f64> {
use cgmath::EuclideanSpace;
let component::Bound (ref variant) = self.bound();
let aabb_shape = variant.aabb();
geometry::Aabb3::with_minmax (
aabb_shape.min() + self.position().0.to_vec(),
aabb_shape.max() + self.position().0.to_vec())
}
}
impl shape::Stereometric <f64> for Dynamic {
fn volume (&self) -> math::Positive <f64> {
let component::Bound (ref variant) = self.bound();
variant.volume()
}
}
impl Object for Nodetect {
fn kind() -> Kind { Kind::Nodetect }
fn position (&self) -> &component::Position {
&self.position
}
fn position_mut (&mut self) -> &mut component::Position {
&mut self.position
}
}
impl Temporal for Nodetect {
fn derivatives (&self) -> &component::Derivatives {
&self.derivatives
}
fn derivatives_mut (&mut self) -> &mut component::Derivatives {
&mut self.derivatives
}
}
impl Inertial for Nodetect {
fn mass (&self) -> &component::Mass {
&self.mass
}
fn mass_mut (&mut self) -> &mut component::Mass {
&mut self.mass
}
}
impl Borrow <component::Derivatives> for Nodetect {
fn borrow (&self) -> &component::Derivatives {
&self.derivatives
}
}
impl BorrowMut <component::Derivatives> for Nodetect {
fn borrow_mut (&mut self) -> &mut component::Derivatives {
&mut self.derivatives
}
}
impl Key {
pub const fn from_raw (key : KeyType) -> Self {
Key (key)
}
pub const fn value (&self) -> KeyType {
self.0
}
pub const fn index (&self) -> usize {
self.0 as usize
}
}
impl std::ops::Deref for Key {
type Target = KeyType;
fn deref (&self) -> &KeyType {
&self.0
}
}
impl From <KeyType> for Key {
fn from (key : KeyType) -> Self {
debug_assert!(key < KEY_MAX);
Key (key)
}
}
impl From <usize> for Key {
fn from (key : usize) -> Self {
debug_assert!((key as KeyType) < KEY_MAX);
Key (key as KeyType)
}
}