nalgebra/examples/homogeneous_coordinates.rs

45 lines
1.3 KiB
Rust
Raw Permalink Normal View History

2017-02-13 01:17:09 +08:00
#[macro_use]
extern crate approx;
extern crate nalgebra as na;
2018-02-02 19:26:35 +08:00
use na::{Isometry2, Point2, Vector2};
2018-10-22 13:00:10 +08:00
use std::f32;
2017-02-13 01:17:09 +08:00
fn use_dedicated_types() {
let iso = Isometry2::new(Vector2::new(1.0, 1.0), f32::consts::PI);
2018-02-02 19:26:35 +08:00
let pt = Point2::new(1.0, 0.0);
2017-02-13 01:17:09 +08:00
let vec = Vector2::x();
2018-02-02 19:26:35 +08:00
let transformed_pt = iso * pt;
2017-02-13 01:17:09 +08:00
let transformed_vec = iso * vec;
assert_relative_eq!(transformed_pt, Point2::new(0.0, 1.0));
assert_relative_eq!(transformed_vec, Vector2::new(-1.0, 0.0));
}
fn use_homogeneous_coordinates() {
let iso = Isometry2::new(Vector2::new(1.0, 1.0), f32::consts::PI);
2018-02-02 19:26:35 +08:00
let pt = Point2::new(1.0, 0.0);
2017-02-13 01:17:09 +08:00
let vec = Vector2::x();
// Compute using homogeneous coordinates.
let hom_iso = iso.to_homogeneous();
2018-02-02 19:26:35 +08:00
let hom_pt = pt.to_homogeneous();
2017-02-13 01:17:09 +08:00
let hom_vec = vec.to_homogeneous();
2018-02-02 19:26:35 +08:00
let hom_transformed_pt = hom_iso * hom_pt;
2017-02-13 01:17:09 +08:00
let hom_transformed_vec = hom_iso * hom_vec;
// Convert back to the cartesian coordinates.
2018-02-02 19:26:35 +08:00
let transformed_pt = Point2::from_homogeneous(hom_transformed_pt).unwrap();
2017-02-13 01:17:09 +08:00
let transformed_vec = Vector2::from_homogeneous(hom_transformed_vec).unwrap();
assert_relative_eq!(transformed_pt, Point2::new(0.0, 1.0));
assert_relative_eq!(transformed_vec, Vector2::new(-1.0, 0.0));
}
fn main() {
use_dedicated_types();
use_homogeneous_coordinates();
}