nalgebra/examples/transform_conversion.rs

25 lines
984 B
Rust
Raw 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::{Isometry2, Similarity2, Vector2};
2021-08-16 00:12:17 +08:00
use std::f32::consts;
2017-02-13 01:17:09 +08:00
fn main() {
// Isometry -> Similarity conversion always succeeds.
let iso = Isometry2::new(Vector2::new(1.0f32, 2.0), na::zero());
let _: Similarity2<f32> = na::convert(iso);
// Similarity -> Isometry conversion fails if the scaling factor is not 1.0.
2021-08-16 00:12:17 +08:00
let sim_without_scaling = Similarity2::new(Vector2::new(1.0f32, 2.0), consts::PI, 1.0);
let sim_with_scaling = Similarity2::new(Vector2::new(1.0f32, 2.0), consts::PI, 2.0);
2017-02-13 01:17:09 +08:00
let iso_success: Option<Isometry2<f32>> = na::try_convert(sim_without_scaling);
2018-02-02 19:26:35 +08:00
let iso_fail: Option<Isometry2<f32>> = na::try_convert(sim_with_scaling);
2017-02-13 01:17:09 +08:00
assert!(iso_success.is_some());
assert!(iso_fail.is_none());
// Similarity -> Isometry conversion can be forced at your own risks.
2020-03-22 06:44:24 +08:00
let iso_forced: Isometry2<f32> = na::convert_unchecked(sim_with_scaling);
2017-02-13 01:17:09 +08:00
assert_eq!(iso_success.unwrap(), iso_forced);
}