nalgebra/examples/identity.rs

42 lines
1.1 KiB
Rust
Raw Normal View History

2017-02-13 01:17:09 +08:00
extern crate alga;
extern crate nalgebra as na;
use alga::linear::Transformation;
2018-02-02 19:26:35 +08:00
use na::{Id, Isometry3, Point3, Vector3};
2017-02-13 01:17:09 +08:00
/*
* Applies `n` times the transformation `t` to the vector `v` and sum each
* intermediate value.
*/
fn complicated_algorithm<T>(v: &Vector3<f32>, t: &T, n: usize) -> Vector3<f32>
2018-02-02 19:26:35 +08:00
where
T: Transformation<Point3<f32>>,
{
let mut result = *v;
2017-02-13 01:17:09 +08:00
2018-02-02 19:26:35 +08:00
// Do lots of operations involving t.
for _ in 0..n {
result = v + t.transform_vector(&result);
}
2017-02-13 01:17:09 +08:00
2018-02-02 19:26:35 +08:00
result
2017-02-13 01:17:09 +08:00
}
/*
* The two following calls are equivalent in term of result.
*/
fn main() {
2018-02-02 19:26:35 +08:00
let v = Vector3::new(1.0, 2.0, 3.0);
2017-02-13 01:17:09 +08:00
2018-02-02 19:26:35 +08:00
// The specialization generated by the compiler will do vector additions only.
let result1 = complicated_algorithm(&v, &Id::new(), 100000);
2017-02-13 01:17:09 +08:00
2018-02-02 19:26:35 +08:00
// The specialization generated by the compiler will also include matrix multiplications.
let iso = Isometry3::identity();
let result2 = complicated_algorithm(&v, &iso, 100000);
2017-02-13 01:17:09 +08:00
2018-02-02 19:26:35 +08:00
// They both return the same result.
assert!(result1 == Vector3::new(100001.0, 200002.0, 300003.0));
assert!(result2 == Vector3::new(100001.0, 200002.0, 300003.0));
2017-02-13 01:17:09 +08:00
}