1use crate::ode::*;
5
6impl dSurfaceParameters {
7
8pub fn new() -> dSurfaceParameters {
10 dSurfaceParameters{
11 mode: 0, mu: 0.0, mu2: 0.0, rho: 0.0, rho2: 0.0, rhoN: 0.0, bounce: 0.0, bounce_vel: 0.0, soft_erp: 0.0, soft_cfm: 0.0, motion1: 0.0, motion2: 0.0, motionN: 0.0, slip1: 0.0, slip2: 0.0} }
27
28}
29
30impl dContactGeom {
31
32pub fn new() -> dContactGeom {
34 dContactGeom{
35 pos: [0.0; 4], normal: [0.0; 4], depth: 0.0, g1: 0 as dGeomID,
39 g2: 0 as dGeomID,
40 side1: 0, side2: 0} }
43
44}
45
46impl dContact {
47
48pub fn new() -> dContact {
50 dContact{
51 surface: dSurfaceParameters::new(),
52 geom: dContactGeom::new(),
53 fdir1: [0.0; 4]} }
55
56}
57
58pub use std::f64::consts::PI;
60pub static PId: dReal = PI * 2.0;
62pub static PIh: dReal = PI / 2.0;
64pub static PIt: dReal = PI / 3.0;
66pub static PIq: dReal = PI / 4.0;
68pub static PIx: dReal = PI / 6.0;
70
71pub static PIh3: dReal = PIh * 3.0;
73
74pub static PIt2: dReal = PIt * 2.0;
76pub static PIt4: dReal = PIt * 4.0;
78pub static PIt5: dReal = PIt * 5.0;
80
81pub static PIq3: dReal = PIq * 3.0;
83
84pub static PIx5: dReal = PIx * 5.0;
86
87pub static M4I: dMatrix4 = [
89 1.0, 0.0, 0.0, 0.0,
90 0.0, 1.0, 0.0, 0.0,
91 0.0, 0.0, 1.0, 0.0,
92 0.0, 0.0, 0.0, 1.0];
93
94pub static M3I: dMatrix3 = [
96 1.0, 0.0, 0.0, 0.0,
97 0.0, 1.0, 0.0, 0.0,
98 0.0, 0.0, 1.0, 0.0];
99
100pub static QI: dQuaternion = [1.0, 0.0, 0.0, 0.0];
102
103pub trait Quaternion {
105 fn as_ptr_mut(&mut self) -> *mut dReal;
107 fn as_ptr(&self) -> *const dReal;
109
110 fn new() -> dQuaternion {
112 let mut q: dQuaternion = [0.0; 4];
113unsafe {
114 dQSetIdentity(q.as_ptr_mut());
115}
116 q
117 }
118
119 fn from_R(m: dMatrix3) -> dQuaternion {
121 let mut q: dQuaternion = [0.0; 4];
122unsafe {
123 dQfromR(q.as_ptr_mut(), m.as_ptr() as *mut dReal);
124}
125 q
126 }
127
128 fn to_R(&self) -> dMatrix3 {
130 let mut m: dMatrix3 = [0.0; 12];
131unsafe {
132 dRfromQ(m.as_ptr_mut(), self.as_ptr() as *mut dReal);
133}
134 m
135 }
136
137 fn from_axis_and_angle(axis: [dReal; 3], angle: dReal) -> dQuaternion {
141 let mut q: dQuaternion = [0.0; 4];
142unsafe {
143 dQFromAxisAndAngle(q.as_ptr_mut(), axis[0], axis[1], axis[2], angle);
144}
145 q
146 }
147
148 fn multiply0(p: dQuaternion, q: dQuaternion) -> dQuaternion {
150 dQuaternion::multiply0_pp(p.as_ptr(), q.as_ptr())
151 }
152
153 fn multiply0_pp(p: *const dReal, q: *const dReal) -> dQuaternion {
155 let mut o: dQuaternion = [0.0; 4];
156unsafe {
157 dQMultiply0(o.as_ptr_mut(), p as *mut dReal, q as *mut dReal);
158}
159 o
160 }
161
162 fn multiply0_331(m: dMatrix3, v: dVector3) -> dVector3 {
165 dVector3::multiply0_331_pp(m.as_ptr(), v.as_ptr())
166 }
167
168 fn multiply0_331_pp(m: *const dReal, v: *const dReal) -> dVector3 {
171 let mut o: dVector3 = [0.0; 4];
172unsafe {
173 dMULTIPLY0_331(o.as_ptr_mut(), m, v);
174}
175 o
176 }
177
178 fn multiply0_441(m: dMatrix4, v: dVector4) -> dVector4 {
181 dVector4::multiply0_441_pp(m.as_ptr(), v.as_ptr())
182 }
183
184 fn multiply0_441_pp(m: *const dReal, v: *const dReal) -> dVector4 {
187 let mut o: dVector4 = [0.0; 4];
188unsafe {
189 dMULTIPLY0_441(o.as_ptr_mut(), m, v);
190}
191 o
192 }
193
194 fn conjugate(q: dQuaternion) -> dQuaternion {
196 dQuaternion::conjugate_ptr(q.as_ptr())
197 }
198
199 fn conjugate_ptr(q: *const dReal) -> dQuaternion {
201unsafe {
202 let q = std::slice::from_raw_parts(q, 4);
203 [q[0], -q[1], -q[2], -q[3]]
204}
205 }
206
207 fn prec_eq(&self, e: dReal, q: dQuaternion) -> bool;
209
210 fn as_vec(&self) -> ODEMat;
212}
213
214impl Quaternion for dQuaternion {
215 fn as_ptr_mut(&mut self) -> *mut dReal { &mut (*self)[0] as *mut dReal }
217 fn as_ptr(&self) -> *const dReal { &(*self)[0] as *const dReal }
219
220 fn prec_eq(&self, e: dReal, q: dQuaternion) -> bool {
222 for i in 0..4 {
223 if (self[i] - q[i]).abs() >= e { return false; }
224 }
225 true
226 }
227
228 fn as_vec(&self) -> ODEMat {
230 ODEMat::from_Q(self.as_ptr())
231 }
232}
233
234pub trait Matrix3 {
236 fn as_ptr_mut(&mut self) -> *mut dReal;
238 fn as_ptr(&self) -> *const dReal;
240
241 fn new() -> dMatrix3 {
243 let mut m: dMatrix3 = [0.0; 12];
244unsafe {
245 dRSetIdentity(m.as_ptr_mut());
246}
247 m
248 }
249
250 fn t(m: dMatrix3) -> dMatrix3 {
252 dMatrix3::t_ptr(m.as_ptr())
253 }
254
255 fn t_ptr(m: *const dReal) -> dMatrix3 {
257unsafe {
258 let m = std::slice::from_raw_parts(m, 12);
259 [m[0], m[4], m[8], m[3],
260 m[1], m[5], m[9], m[7],
261 m[2], m[6], m[10], m[11]]
262}
263 }
264
265 fn from_Q(q: dQuaternion) -> dMatrix3 {
267 let mut m: dMatrix3 = [0.0; 12];
268unsafe {
269 dRfromQ(m.as_ptr_mut(), q.as_ptr() as *mut dReal);
270}
271 m
272 }
273
274 fn to_Q(&self) -> dQuaternion {
276 let mut q: dQuaternion = [0.0; 4];
277unsafe {
278 dQfromR(q.as_ptr_mut(), self.as_ptr() as *mut dReal);
279}
280 q
281 }
282
283 fn from_axis_and_angle(axis: [dReal; 3], angle: dReal) -> dMatrix3 {
285 let mut m: dMatrix3 = [0.0; 12];
286unsafe {
287 dRFromAxisAndAngle(m.as_ptr_mut(), axis[0], axis[1], axis[2], angle);
288}
289 m
290 }
291
292 fn from_euler_angles(phi: dReal, theta: dReal, psi: dReal) -> dMatrix3 {
294 let mut m: dMatrix3 = [0.0; 12];
295unsafe {
296 dRFromEulerAngles(m.as_ptr_mut(), phi, theta, psi);
297}
298 m
299 }
300
301 fn from_2_axes(e0: [dReal; 3], e1: [dReal; 3]) -> dMatrix3 {
303 let mut m: dMatrix3 = [0.0; 12];
304unsafe {
305 dRFrom2Axes(m.as_ptr_mut(), e0[0], e0[1], e0[2], e1[0], e1[1], e1[2]);
306}
307 m
308 }
309
310 fn from_z_axis(e: [dReal; 3]) -> dMatrix3 {
312 let mut m: dMatrix3 = [0.0; 12];
313unsafe {
314 dRFromZAxis(m.as_ptr_mut(), e[0], e[1], e[2]);
315}
316 m
317 }
318
319 fn multiply0_333(a: dMatrix3, b: dMatrix3) -> dMatrix3 {
321 dMatrix3::multiply0_333_pp(a.as_ptr(), b.as_ptr())
322 }
323
324 fn multiply0_333_pp(a: *const dReal, b: *const dReal) -> dMatrix3 {
326 let mut m: dMatrix3 = [0.0; 12];
327unsafe {
328 dMULTIPLY0_333(m.as_ptr_mut(), a, b);
329}
330 m
331 }
332
333 fn prec_eq(&self, e: dReal, m: dMatrix3) -> bool;
335
336 fn as_mat(&self) -> ODEMat;
338}
339
340impl Matrix3 for dMatrix3 {
341 fn as_ptr_mut(&mut self) -> *mut dReal { &mut (*self)[0] as *mut dReal }
343 fn as_ptr(&self) -> *const dReal { &(*self)[0] as *const dReal }
345
346 fn prec_eq(&self, e: dReal, m: dMatrix3) -> bool {
348 for i in 0..12 {
349 if (self[i] - m[i]).abs() >= e { return false; }
350 }
351 true
352 }
353
354 fn as_mat(&self) -> ODEMat {
356 ODEMat::from_Mat3(self.as_ptr())
357 }
358}
359
360pub trait Matrix4 {
362 fn as_ptr_mut(&mut self) -> *mut dReal;
364 fn as_ptr(&self) -> *const dReal;
366
367 fn new() -> dMatrix4 {
369 let mut m: dMatrix4 = [0.0; 16];
370 for i in 0..4 { m[i * 5] = 1.0; }
371 m
372 }
373
374 fn t(m: dMatrix4) -> dMatrix4 {
376 dMatrix4::t_ptr(m.as_ptr())
377 }
378
379 fn t_ptr(m: *const dReal) -> dMatrix4 {
381unsafe {
382 let m = std::slice::from_raw_parts(m, 16);
383 [m[0], m[4], m[8], m[12],
384 m[1], m[5], m[9], m[13],
385 m[2], m[6], m[10], m[14],
386 m[3], m[7], m[11], m[15]]
387}
388 }
389
390 fn q_conjugate(m: dMatrix4) -> dMatrix4 {
392 dMatrix4::q_conjugate_ptr(m.as_ptr())
393 }
394
395 fn q_conjugate_ptr(m: *const dReal) -> dMatrix4 {
397unsafe {
398 let m = std::slice::from_raw_parts(m, 16);
399 [m[0], -m[1], -m[2], -m[3],
400 -m[4], m[5], -m[6], -m[7],
401 -m[8], -m[9], m[10], -m[11],
402 -m[12], -m[13], -m[14], m[15]]
403}
404 }
405
406 fn from_Conjugate_Q(q: dQuaternion) -> dMatrix4 {
408 dMatrix4::from_Q(dQuaternion::conjugate(q))
409 }
410
411 fn from_Conjugate_Q_ptr(q: *const dReal) -> dMatrix4 {
413 dMatrix4::from_Q(dQuaternion::conjugate_ptr(q))
414 }
415
416 fn from_P(p: dQuaternion) -> dMatrix4 {
418 dMatrix4::from_P_ptr(p.as_ptr())
419 }
420
421 fn from_P_ptr(p: *const dReal) -> dMatrix4 {
423unsafe {
424 let p = std::slice::from_raw_parts(p, 4);
425 [ p[0], -p[1], -p[2], -p[3],
426 p[1], p[0], p[3], -p[2],
427 p[2], -p[3], p[0], p[1],
428 p[3], p[2], -p[1], p[0]]
429}
430 }
431
432 fn from_Q(q: dQuaternion) -> dMatrix4 {
434 dMatrix4::from_Q_ptr(q.as_ptr())
435 }
436
437 fn from_Q_ptr(q: *const dReal) -> dMatrix4 {
439unsafe {
440 let q = std::slice::from_raw_parts(q, 4);
441 [ q[0], -q[1], -q[2], -q[3],
442 q[1], q[0], -q[3], q[2],
443 q[2], q[3], q[0], -q[1],
444 q[3], -q[2], q[1], q[0]]
445}
446 }
447
448 fn multiply0_444(a: dMatrix4, b: dMatrix4) -> dMatrix4 {
450 dMatrix4::multiply0_444_pp(a.as_ptr(), b.as_ptr())
451 }
452
453 fn multiply0_444_pp(a: *const dReal, b: *const dReal) -> dMatrix4 {
455 let mut m: dMatrix4 = [0.0; 16];
456unsafe {
457 dMULTIPLY0_444(m.as_ptr_mut(), a, b);
458}
459 m
460 }
461
462 fn is_quaternion(&self) -> bool;
464
465 fn to_Q(&self) -> dQuaternion;
467
468 fn prec_eq(&self, e: dReal, m: dMatrix4) -> bool;
470
471 fn as_mat(&self) -> ODEMat;
473}
474
475impl Matrix4 for dMatrix4 {
476 fn as_ptr_mut(&mut self) -> *mut dReal { &mut (*self)[0] as *mut dReal }
478 fn as_ptr(&self) -> *const dReal { &(*self)[0] as *const dReal }
480
481 fn is_quaternion(&self) -> bool {
483 if self[0] != self[5] || self[0] != self[10] || self[0] != self[15] {
484 return false;
485 }
486 if (self[4] != self[14] || self[8] != self[7] || self[12] != self[9])
487 && (self[4] != self[11] || self[8] != self[13] || self[12] != self[6]) {
488 return false;
489 }
490 dMatrix4::t(dMatrix4::q_conjugate(*self)) == *self
491 }
492
493 fn to_Q(&self) -> dQuaternion {
495 if !self.is_quaternion() { panic!("not quaternion"); }
496 [self[0], self[4], self[8], self[12]]
497 }
498
499 fn prec_eq(&self, e: dReal, m: dMatrix4) -> bool {
501 for i in 0..16 {
502 if (self[i] - m[i]).abs() >= e { return false; }
503 }
504 true
505 }
506
507 fn as_mat(&self) -> ODEMat {
509 ODEMat::from_Mat4(self.as_ptr())
510 }
511}
512
513impl dMass {
514
515pub fn new() -> dMass {
517 let mut mass: dMass = dMass{
518 mass: 0.0,
519 c: [0.0; 4], I: [0.0; 12]}; unsafe { dMassSetZero(&mut mass); } mass
523}
524
525}