oyk/ode/
meta.rs

1//! meta
2//!
3
4use crate::ode::*;
5use crate::ode::cls::*;
6use crate::ode::krp::*;
7
8/// id for trait MetaInf
9#[derive(Debug)]
10pub enum MetaId {
11  /// ODE Sphere
12  Sphere = 0,
13  /// ODE Box
14  Box,
15  /// ODE Capsule
16  Capsule,
17  /// ODE Cylinder
18  Cylinder,
19  /// ODE Plane
20  Plane,
21  /// ODE Ray
22  Ray,
23  /// ODE Convex
24  Convex,
25  /// ODE GeomTransform
26  GeomTransform,
27  /// ODE TriMesh
28  TriMesh,
29  /// ODE Heightfield
30  Heightfield,
31  /// ODE Composite
32  Composite
33}
34
35// #[macro_export]
36macro_rules! meta_panic {
37  ($s: ident, $e: expr) => { panic!("Expected {} but {:?}", $e, $s.id()); };
38}
39// pub use meta_panic;
40
41/// MetaInf
42pub trait MetaInf {
43  /// MetaID
44  fn id(&self) -> MetaId;
45  /// every struct has krp
46  fn get_krp_mut(&mut self) -> &mut Krp;
47  /// every struct has krp
48  fn get_krp(&self) -> &Krp;
49  /// every struct has tcm
50  fn get_tcm_mut(&mut self) -> &mut TCMaterial;
51  /// every struct has tcm
52  fn get_tcm(&self) -> &TCMaterial;
53  /// as MetaSphere
54  fn as_sphere(&self) -> &MetaSphere { meta_panic!(self, "Sphere"); }
55  /// as MetaBox
56  fn as_box(&self) -> &MetaBox { meta_panic!(self, "Box"); }
57  /// as MetaCapsule
58  fn as_capsule(&self) -> &MetaCapsule { meta_panic!(self, "Capsule"); }
59  /// as MetaCylinder
60  fn as_cylinder(&self) -> &MetaCylinder { meta_panic!(self, "Cylinder"); }
61  /// as MetaPlane
62  fn as_plane(&self) -> &MetaPlane { meta_panic!(self, "Plane"); }
63  /// as MetaConvex
64  fn as_convex(&self) -> &MetaConvex { meta_panic!(self, "Convex"); }
65  /// as MetaTriMesh
66  fn as_trimesh(&self) -> &MetaTriMesh { meta_panic!(self, "TriMesh"); }
67  /// as MetaComposite
68  fn as_composite(&self) -> &MetaComposite { meta_panic!(self, "Composite"); }
69  /// clone MetaInf
70  fn dup(&self) -> Box<dyn MetaInf> { meta_panic!(self, "Clone"); }
71}
72
73/// MetaSphere
74#[derive(Clone)]
75pub struct MetaSphere {
76  /// mass density
77  pub dm: dReal,
78  /// radius
79  pub r: dReal,
80  /// krp
81  pub krp: Krp,
82  /// material
83  pub tcm: TCMaterial
84}
85
86impl MetaSphere {
87  /// construct
88  pub fn new(dm: dReal, r: dReal,
89    krp: Krp, tex: i32, col: dVector4) -> Box<MetaSphere> {
90    Box::new(MetaSphere{dm: dm, r: r,
91      krp: krp, tcm: TCMaterial::new(tex, col)})
92  }
93}
94
95impl MetaInf for MetaSphere {
96  /// MetaID
97  fn id(&self) -> MetaId { MetaId::Sphere }
98  /// every struct has krp
99  fn get_krp_mut(&mut self) -> &mut Krp { &mut self.krp }
100  /// every struct has krp
101  fn get_krp(&self) -> &Krp { &self.krp }
102  /// every struct has tcm
103  fn get_tcm_mut(&mut self) -> &mut TCMaterial { &mut self.tcm }
104  /// every struct has tcm
105  fn get_tcm(&self) -> &TCMaterial { &self.tcm }
106  /// as MetaSphere
107  fn as_sphere(&self) -> &MetaSphere { self }
108  /// clone MetaInf
109  fn dup(&self) -> Box<dyn MetaInf> { Box::new(self.clone()) }
110}
111
112/// MetaBox
113#[derive(Clone)]
114pub struct MetaBox {
115  /// mass density
116  pub dm: dReal,
117  /// lxyz
118  pub lxyz: dVector3,
119  /// krp
120  pub krp: Krp,
121  /// material
122  pub tcm: TCMaterial
123}
124
125impl MetaBox {
126  /// construct
127  pub fn new(dm: dReal, lxyz: dVector3,
128    krp: Krp, tex: i32, col: dVector4) -> Box<MetaBox> {
129    Box::new(MetaBox{dm: dm, lxyz: lxyz,
130      krp: krp, tcm: TCMaterial::new(tex, col)})
131  }
132}
133
134impl MetaInf for MetaBox {
135  /// MetaID
136  fn id(&self) -> MetaId { MetaId::Box }
137  /// every struct has krp
138  fn get_krp_mut(&mut self) -> &mut Krp { &mut self.krp }
139  /// every struct has krp
140  fn get_krp(&self) -> &Krp { &self.krp }
141  /// every struct has tcm
142  fn get_tcm_mut(&mut self) -> &mut TCMaterial { &mut self.tcm }
143  /// every struct has tcm
144  fn get_tcm(&self) -> &TCMaterial { &self.tcm }
145  /// as MetaBox
146  fn as_box(&self) -> &MetaBox { self }
147  /// clone MetaInf
148  fn dup(&self) -> Box<dyn MetaInf> { Box::new(self.clone()) }
149}
150
151/// MetaCapsule
152#[derive(Clone)]
153pub struct MetaCapsule {
154  /// mass density
155  pub dm: dReal,
156  /// radius
157  pub r: dReal,
158  /// length
159  pub l: dReal,
160  /// krp
161  pub krp: Krp,
162  /// material
163  pub tcm: TCMaterial
164}
165
166impl MetaCapsule {
167  /// construct
168  pub fn new(dm: dReal, r: dReal, l: dReal,
169    krp: Krp, tex: i32, col: dVector4) -> Box<MetaCapsule> {
170    Box::new(MetaCapsule{dm: dm, r: r, l: l,
171      krp: krp, tcm: TCMaterial::new(tex, col)})
172  }
173}
174
175impl MetaInf for MetaCapsule {
176  /// MetaID
177  fn id(&self) -> MetaId { MetaId::Capsule }
178  /// every struct has krp
179  fn get_krp_mut(&mut self) -> &mut Krp { &mut self.krp }
180  /// every struct has krp
181  fn get_krp(&self) -> &Krp { &self.krp }
182  /// every struct has tcm
183  fn get_tcm_mut(&mut self) -> &mut TCMaterial { &mut self.tcm }
184  /// every struct has tcm
185  fn get_tcm(&self) -> &TCMaterial { &self.tcm }
186  /// as MetaCapsule
187  fn as_capsule(&self) -> &MetaCapsule { self }
188  /// clone MetaInf
189  fn dup(&self) -> Box<dyn MetaInf> { Box::new(self.clone()) }
190}
191
192/// MetaCylinder
193#[derive(Clone)]
194pub struct MetaCylinder {
195  /// mass density
196  pub dm: dReal,
197  /// radius
198  pub r: dReal,
199  /// length
200  pub l: dReal,
201  /// krp
202  pub krp: Krp,
203  /// material
204  pub tcm: TCMaterial
205}
206
207impl MetaCylinder {
208  /// construct
209  pub fn new(dm: dReal, r: dReal, l: dReal,
210    krp: Krp, tex: i32, col: dVector4) -> Box<MetaCylinder> {
211    Box::new(MetaCylinder{dm: dm, r: r, l: l,
212      krp: krp, tcm: TCMaterial::new(tex, col)})
213  }
214}
215
216impl MetaInf for MetaCylinder {
217  /// MetaID
218  fn id(&self) -> MetaId { MetaId::Cylinder }
219  /// every struct has krp
220  fn get_krp_mut(&mut self) -> &mut Krp { &mut self.krp }
221  /// every struct has krp
222  fn get_krp(&self) -> &Krp { &self.krp }
223  /// every struct has tcm
224  fn get_tcm_mut(&mut self) -> &mut TCMaterial { &mut self.tcm }
225  /// every struct has tcm
226  fn get_tcm(&self) -> &TCMaterial { &self.tcm }
227  /// as MetaCylinder
228  fn as_cylinder(&self) -> &MetaCylinder { self }
229  /// clone MetaInf
230  fn dup(&self) -> Box<dyn MetaInf> { Box::new(self.clone()) }
231}
232
233/// MetaPlane
234#[derive(Clone)]
235pub struct MetaPlane {
236  /// mass density
237  pub dm: dReal,
238  /// lxyz
239  pub lxyz: dVector3,
240  /// norm
241  pub norm: dVector4,
242  /// krp
243  pub krp: Krp,
244  /// material
245  pub tcm: TCMaterial
246}
247
248impl MetaPlane {
249  /// construct
250  pub fn new(dm: dReal, lxyz: dVector3, norm: dVector4,
251    krp: Krp, tex: i32, col: dVector4) -> Box<MetaPlane> {
252    Box::new(MetaPlane{dm: dm, lxyz: lxyz, norm: norm,
253      krp: krp, tcm: TCMaterial::new(tex, col)})
254  }
255}
256
257impl MetaInf for MetaPlane {
258  /// MetaID
259  fn id(&self) -> MetaId { MetaId::Plane }
260  /// every struct has krp
261  fn get_krp_mut(&mut self) -> &mut Krp { &mut self.krp }
262  /// every struct has krp
263  fn get_krp(&self) -> &Krp { &self.krp }
264  /// every struct has tcm
265  fn get_tcm_mut(&mut self) -> &mut TCMaterial { &mut self.tcm }
266  /// every struct has tcm
267  fn get_tcm(&self) -> &TCMaterial { &self.tcm }
268  /// as MetaPlane
269  fn as_plane(&self) -> &MetaPlane { self }
270  /// clone MetaInf
271  fn dup(&self) -> Box<dyn MetaInf> { Box::new(self.clone()) }
272}
273
274/// MetaConvex
275#[derive(Clone)]
276pub struct MetaConvex {
277  /// free flag
278  pub ff: bool,
279  /// mass density
280  pub dm: dReal,
281  /// convexfvp *** CAUTION how to clone ***
282  pub fvp: *mut convexfvp,
283  /// krp
284  pub krp: Krp,
285  /// material
286  pub tcm: TCMaterial
287}
288
289impl MetaConvex {
290  /// construct
291  pub fn new(ff: bool, dm: dReal, fvp: *mut convexfvp,
292    krp: Krp, tex: i32, col: dVector4) -> Box<MetaConvex> {
293    Box::new(MetaConvex{ff: ff, dm: dm, fvp: fvp,
294      krp: krp, tcm: TCMaterial::new(tex, col)})
295  }
296}
297
298impl MetaInf for MetaConvex {
299  /// MetaID
300  fn id(&self) -> MetaId { MetaId::Convex }
301  /// every struct has krp
302  fn get_krp_mut(&mut self) -> &mut Krp { &mut self.krp }
303  /// every struct has krp
304  fn get_krp(&self) -> &Krp { &self.krp }
305  /// every struct has tcm
306  fn get_tcm_mut(&mut self) -> &mut TCMaterial { &mut self.tcm }
307  /// every struct has tcm
308  fn get_tcm(&self) -> &TCMaterial { &self.tcm }
309  /// as MetaConvex
310  fn as_convex(&self) -> &MetaConvex { self }
311  /// clone MetaInf
312  fn dup(&self) -> Box<dyn MetaInf> { Box::new(self.clone()) }
313}
314
315impl Drop for MetaConvex {
316  fn drop(&mut self) {
317    if self.ff && self.fvp != 0 as *mut convexfvp {
318unsafe {
319      FreeConvexFVP(self.fvp, self.ff);
320}
321    }
322  }
323}
324
325/// MetaTriMesh
326#[derive(Clone)]
327pub struct MetaTriMesh {
328  /// free flag
329  pub ff: bool,
330  /// mass density
331  pub dm: dReal,
332  /// trimeshvi *** CAUTION how to clone ***
333  pub tmv: *mut trimeshvi,
334  /// krp
335  pub krp: Krp,
336  /// material
337  pub tcm: TCMaterial
338}
339
340impl MetaTriMesh {
341  /// construct
342  pub fn new(ff: bool, dm: dReal, tmv: *mut trimeshvi,
343    krp: Krp, tex: i32, col: dVector4) -> Box<MetaTriMesh> {
344    Box::new(MetaTriMesh{ff: ff, dm: dm, tmv: tmv,
345      krp: krp, tcm: TCMaterial::new(tex, col)})
346  }
347}
348
349impl MetaInf for MetaTriMesh {
350  /// MetaID
351  fn id(&self) -> MetaId { MetaId::TriMesh }
352  /// every struct has krp
353  fn get_krp_mut(&mut self) -> &mut Krp { &mut self.krp }
354  /// every struct has krp
355  fn get_krp(&self) -> &Krp { &self.krp }
356  /// every struct has tcm
357  fn get_tcm_mut(&mut self) -> &mut TCMaterial { &mut self.tcm }
358  /// every struct has tcm
359  fn get_tcm(&self) -> &TCMaterial { &self.tcm }
360  /// as MetaTriMesh
361  fn as_trimesh(&self) -> &MetaTriMesh { self }
362  /// clone MetaInf
363  fn dup(&self) -> Box<dyn MetaInf> { Box::new(self.clone()) }
364}
365
366impl Drop for MetaTriMesh {
367  fn drop(&mut self) {
368    if self.ff && self.tmv != 0 as *mut trimeshvi {
369unsafe {
370      FreeTriMeshVI(self.tmv, self.ff);
371}
372    }
373  }
374}
375
376/// MetaComposite (not have dup #[derive(Clone)])
377pub struct MetaComposite {
378  /// elements
379  pub elems: Vec<Box<dyn MetaInf>>,
380  /// quaternions
381  pub qs: Vec<dQuaternion>,
382  /// offsets
383  pub ofs: Vec<dVector3>,
384  /// krp
385  pub krp: Krp,
386  /// material
387  pub tcm: TCMaterial
388}
389
390impl MetaComposite {
391  /// construct
392  pub fn new(elems: Vec<Box<dyn MetaInf>>,
393    qs: Vec<dQuaternion>, ofs: Vec<dVector3>,
394    krp: Krp, tex: i32, col: dVector4) -> Box<MetaComposite> {
395    Box::new(MetaComposite{elems: elems, ofs: ofs, qs: qs,
396      krp: krp, tcm: TCMaterial::new(tex, col)})
397  }
398}
399
400impl MetaInf for MetaComposite {
401  /// MetaID
402  fn id(&self) -> MetaId { MetaId::Composite }
403  /// every struct has krp
404  fn get_krp_mut(&mut self) -> &mut Krp { &mut self.krp }
405  /// every struct has krp
406  fn get_krp(&self) -> &Krp { &self.krp }
407  /// every struct has tcm
408  fn get_tcm_mut(&mut self) -> &mut TCMaterial { &mut self.tcm }
409  /// every struct has tcm
410  fn get_tcm(&self) -> &TCMaterial { &self.tcm }
411  /// as MetaComposite
412  fn as_composite(&self) -> &MetaComposite { self }
413}