2014-08-11 06:10:37 +00:00
|
|
|
// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
|
2015-03-14 02:49:46 +00:00
|
|
|
// refer to the Cargo.toml file at the top-level directory of this distribution.
|
2014-01-29 02:01:57 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-08-11 06:10:37 +00:00
|
|
|
extern crate cgmath;
|
|
|
|
|
2015-05-06 08:27:52 +00:00
|
|
|
use cgmath::{Matrix4, Matrix3};
|
2014-08-16 04:51:09 +00:00
|
|
|
use cgmath::Quaternion;
|
2014-01-29 02:01:57 +00:00
|
|
|
|
2014-10-14 00:10:54 +00:00
|
|
|
use cgmath::{Rad, rad, ApproxEq};
|
|
|
|
use cgmath::Rotation3;
|
|
|
|
|
2014-11-25 04:24:04 +00:00
|
|
|
use std::f32;
|
2014-11-15 14:59:43 +00:00
|
|
|
|
2014-01-29 02:01:57 +00:00
|
|
|
#[test]
|
2014-04-14 01:30:24 +00:00
|
|
|
fn to_matrix4()
|
2014-01-29 02:01:57 +00:00
|
|
|
{
|
2014-04-14 01:30:24 +00:00
|
|
|
let quaternion = Quaternion::new(2f32, 3f32, 4f32, 5f32);
|
2014-01-29 02:01:57 +00:00
|
|
|
|
2015-05-06 08:27:52 +00:00
|
|
|
let matrix_short: Matrix4<_> = quaternion.into();
|
|
|
|
|
|
|
|
let matrix_long: Matrix3<_> = quaternion.into();
|
|
|
|
let matrix_long: Matrix4<_> = matrix_long.into();
|
2014-01-29 02:01:57 +00:00
|
|
|
|
2014-04-14 01:30:24 +00:00
|
|
|
assert!(matrix_short == matrix_long);
|
2014-01-29 02:01:57 +00:00
|
|
|
}
|
2014-10-14 00:10:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_and_from_quaternion()
|
|
|
|
{
|
|
|
|
fn eq(a: (Rad<f32>, Rad<f32>, Rad<f32>), b: (Rad<f32>, Rad<f32>, Rad<f32>)) {
|
|
|
|
let (ax, ay, az) = a;
|
|
|
|
let (bx, by, bz) = b;
|
|
|
|
if !(ax.approx_eq_eps(&bx, &0.001) &&
|
|
|
|
ay.approx_eq_eps(&by, &0.001) &&
|
|
|
|
az.approx_eq_eps(&bz, &0.001)) {
|
2015-01-09 22:30:19 +00:00
|
|
|
panic!("{:?} != {:?}", a, b)
|
2014-10-14 00:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 04:24:04 +00:00
|
|
|
let hpi = f32::consts::FRAC_PI_2;
|
2014-10-14 00:10:54 +00:00
|
|
|
|
|
|
|
let zero: Quaternion<f32> = Rotation3::from_euler(rad(0f32), rad(0f32), rad(0f32));
|
|
|
|
eq((rad(0f32), rad(0f32), rad(0f32)), zero.to_euler());
|
|
|
|
|
|
|
|
let x_1: Quaternion<f32> = Rotation3::from_euler(rad(1f32), rad(0f32), rad(0f32));
|
|
|
|
eq((rad(1f32), rad(0f32), rad(0f32)), x_1.to_euler());
|
|
|
|
|
|
|
|
let y_1: Quaternion<f32> = Rotation3::from_euler(rad(0f32), rad(1f32), rad(0f32));
|
|
|
|
eq((rad(0f32), rad(1f32), rad(0f32)), y_1.to_euler());
|
|
|
|
|
|
|
|
let z_1: Quaternion<f32> = Rotation3::from_euler(rad(0f32), rad(0f32), rad(1f32));
|
|
|
|
eq((rad(0f32), rad(0f32), rad(1f32)), z_1.to_euler());
|
|
|
|
|
|
|
|
let x_n1: Quaternion<f32> = Rotation3::from_euler(rad(-1f32), rad(0f32), rad(0f32));
|
|
|
|
eq((rad(-1f32), rad(0f32), rad(0f32)), x_n1.to_euler());
|
|
|
|
|
|
|
|
let y_n1: Quaternion<f32> = Rotation3::from_euler(rad(0f32), rad(-1f32), rad(0f32));
|
|
|
|
eq((rad(0f32), rad(-1f32), rad(0f32)), y_n1.to_euler());
|
|
|
|
|
|
|
|
let z_n1: Quaternion<f32> = Rotation3::from_euler(rad(0f32), rad(0f32), rad(-1f32));
|
|
|
|
eq((rad(0f32), rad(0f32), rad(-1f32)), z_n1.to_euler());
|
|
|
|
|
|
|
|
let xzy_1: Quaternion<f32> = Rotation3::from_euler(rad(1f32), rad(1f32), rad(1f32));
|
|
|
|
eq((rad(1f32), rad(1f32), rad(1f32)), xzy_1.to_euler());
|
|
|
|
|
|
|
|
let xzy_n1: Quaternion<f32> = Rotation3::from_euler(rad(-1f32), rad(-1f32), rad(-1f32));
|
|
|
|
eq((rad(-1f32), rad(-1f32), rad(-1f32)), xzy_n1.to_euler());
|
|
|
|
|
|
|
|
let xzy_hp: Quaternion<f32> = Rotation3::from_euler(rad(0f32), rad(hpi), rad(1f32));
|
|
|
|
eq((rad(0f32), rad(hpi), rad(1f32)), xzy_hp.to_euler());
|
|
|
|
|
|
|
|
let xzy_nhp: Quaternion<f32> = Rotation3::from_euler(rad(0f32), rad(-hpi), rad(1f32));
|
|
|
|
eq((rad(0f32), rad(-hpi), rad(1f32)), xzy_nhp.to_euler());
|
|
|
|
|
|
|
|
}
|