use crate::ode::*;
use crate::ode::mat::*;
use crate::ode::prim::*;
use crate::ode::meta::*;
fn fake_type_name_of_val<T>(_: &T) -> &'static str {
std::any::type_name::<T>()
}
enum ClsId {
World = 1, Space, Body, Geom, JointGroup
}
macro_rules! chk_src_type {
($n: expr, $obj: ident, $src: expr) => {{
let o = fake_type_name_of_val(&$obj); let t = fake_type_name_of_val(&$src);
let v = $src as usize;
let mut r = (None, v);
if &t[..5] == "*mut " { let p: Vec<_> = t.match_indices("::").collect(); if p.len() > 0 {
let s = &t[p[p.len() - 1].0+4..]; r = (match s {
"World" => { Some(ClsId::World) },
"Space" => { Some(ClsId::Space) },
"Body" => { Some(ClsId::Body) },
"Geom" => { Some(ClsId::Geom) },
"JointGroup" => { Some(ClsId::JointGroup) },
_ => { println!("unknown pattern of {} {}, {}", $n, o, t); None }
}, v)
}
}
r
}};
}
macro_rules! from_id {
(obg: $obj: ident, $src: expr) => {
let (k, v) = chk_src_type!("Obg", $obj, $src);
match k {
Some(ClsId::Body) => { $obj.body = v },
Some(ClsId::Geom) => { $obj.geom = v },
_ => {}
}
};
(gws: $obj: ident, $src: expr) => {
let (k, v) = chk_src_type!("Gws", $obj, $src);
match k {
Some(ClsId::World) => { $obj.world = v },
Some(ClsId::Space) => { $obj.space = v },
Some(ClsId::Geom) => { $obj.ground = v },
Some(ClsId::JointGroup) => { $obj.contactgroup = v },
_ => {}
}
};
($obj: ident, $src: expr, $dst: ident) => { $obj.$dst = $src as usize };
}
macro_rules! as_id { ($obj: ident, body) => { $obj.body as dBodyID };
($obj: ident, geom) => { $obj.geom as dGeomID };
($obj: ident, world) => { $obj.world as dWorldID };
($obj: ident, space) => { $obj.space as dSpaceID };
($obj: ident, ground) => { $obj.ground as dGeomID };
($obj: ident, contactgroup) => { $obj.contactgroup as dJointGroupID };
($obj: ident, $src: ident, $dst: ty) => { $obj.$src as $dst };
}
pub struct Obg { pub key: String,
body: usize, geom: usize, pub col: dVector4
}
impl Obg {
pub fn new(key: &str, body: dBodyID, geom: dGeomID, col: dVector4) -> Obg {
Obg{key: key.to_string(), body: body as usize, geom: geom as usize, col: col}
}
pub fn body_(&mut self, id: dBodyID) { from_id!(obg: self, id); }
pub fn body(&self) -> dBodyID { as_id!(self, body) }
pub fn geom_(&mut self, id: dGeomID) { from_id!(obg: self, id); }
pub fn geom(&self) -> dGeomID { as_id!(self, geom) }
pub fn pos_ptr_mut(&mut self) -> *mut [dReal; 4] {
unsafe {
let p: *mut dReal = dBodyGetPosition(self.body()) as *mut dReal;
p as *mut [dReal; 4]
}
}
pub fn pos_(&mut self) -> &mut [dReal] {
unsafe {
std::slice::from_raw_parts_mut(self.pos_ptr_mut() as *mut dReal, 4)
}
}
pub fn pos(&self) -> &[dReal] {
unsafe {
let p: *const dReal = dBodyGetPosition(self.body());
std::slice::from_raw_parts(p, 4)
}
}
pub fn pos_vec(&self) -> ODEMat {
ODEMat::as_vec(self.pos())
}
pub fn rot_ptr_mut(&mut self) -> *mut [[dReal; 4]; 3] {
unsafe {
let p: *mut dReal = dBodyGetRotation(self.body()) as *mut dReal;
p as *mut [[dReal; 4]; 3]
}
}
pub fn rot_(&mut self) -> &mut [dReal] {
unsafe {
std::slice::from_raw_parts_mut(self.rot_ptr_mut() as *mut dReal, 12)
}
}
pub fn rot(&self) -> &[dReal] {
unsafe {
let p: *const dReal = dBodyGetRotation(self.body());
std::slice::from_raw_parts(p, 12)
}
}
pub fn rot_mat3(&self) -> ODEMat {
ODEMat::as_mat(3, self.rot())
}
pub fn set_pos(&mut self, pos: dVector3) -> &mut Obg {
unsafe {
dBodySetPosition(self.body(), pos[0], pos[1], pos[2]);
}
self
}
pub fn set_rot(&mut self, rot: dMatrix3) -> &mut Obg {
unsafe {
dBodySetRotation(self.body(), rot.as_ptr() as *mut dReal);
}
self
}
pub fn set_quaternion(&mut self, q: dQuaternion) -> &mut Obg {
unsafe {
dBodySetQuaternion(self.body(), q.as_ptr() as *mut dReal);
}
self
}
}
pub struct Gws { world: usize, space: usize, ground: usize, contactgroup: usize }
impl Gws {
pub fn new() -> Gws {
Gws{world: 0, space: 0, ground: 0, contactgroup: 0}
}
pub fn world_(&mut self, id: dWorldID) { from_id!(gws: self, id); }
pub fn world(&self) -> dWorldID { as_id!(self, world) }
pub fn space_(&mut self, id: dSpaceID) { from_id!(gws: self, id); }
pub fn space(&self) -> dSpaceID { as_id!(self, space) }
pub fn ground_(&mut self, id: dGeomID) { from_id!(gws: self, id); }
pub fn ground(&self) -> dGeomID { as_id!(self, ground) }
pub fn contactgroup_(&mut self, id: dJointGroupID) { from_id!(gws: self, id); }
pub fn contactgroup(&self) -> dJointGroupID { as_id!(self, contactgroup) }
}
pub struct GeomOffset<'a> {
pub gsub: dGeomID,
pub o: &'a dVector3
}
#[derive(Clone)]
pub struct TCMaterial {
pub tex: i32,
pub col: dVector4
}
impl TCMaterial {
pub fn new(t: i32, c: dVector4) -> TCMaterial {
TCMaterial{tex: t, col: c}
}
}
pub struct Cam {
pub pos: Vec<f32>,
pub ypr: Vec<f32>
}
impl Cam {
pub fn new(p: Vec<f32>, y: Vec<f32>) -> Cam {
Cam{pos: p, ypr: y}
}
}