nalgebra/examples/raw_pointer.rs

37 lines
985 B
Rust
Raw Permalink Normal View History

2017-02-13 01:17:09 +08:00
extern crate nalgebra as na;
2018-02-02 19:26:35 +08:00
use na::{Matrix3, Point3, Vector3};
2017-02-13 01:17:09 +08:00
fn main() {
let v = Vector3::new(1.0f32, 0.0, 1.0);
let p = Point3::new(1.0f32, 0.0, 1.0);
let m = na::one::<Matrix3<f32>>();
// Convert to arrays.
let v_array = v.as_slice();
let p_array = p.coords.as_slice();
let m_array = m.as_slice();
// Get data pointers.
let v_pointer = v_array.as_ptr();
let p_pointer = p_array.as_ptr();
let m_pointer = m_array.as_ptr();
/* Then pass the raw pointers to some graphics API. */
2021-08-16 00:12:17 +08:00
#[allow(clippy::float_cmp)]
2017-02-13 01:17:09 +08:00
unsafe {
assert_eq!(*v_pointer, 1.0);
assert_eq!(*v_pointer.offset(1), 0.0);
assert_eq!(*v_pointer.offset(2), 1.0);
assert_eq!(*p_pointer, 1.0);
assert_eq!(*p_pointer.offset(1), 0.0);
assert_eq!(*p_pointer.offset(2), 1.0);
assert_eq!(*m_pointer, 1.0);
assert_eq!(*m_pointer.offset(4), 1.0);
assert_eq!(*m_pointer.offset(8), 1.0);
}
}