Styling fixes to the quaternion arc code
This commit is contained in:
parent
f555074a44
commit
55c212bde1
2 changed files with 26 additions and 15 deletions
|
@ -73,8 +73,8 @@ impl<S: BaseFloat> Quaternion<S> {
|
||||||
let mag_avg = (src.magnitude2() * dst.magnitude2()).sqrt();
|
let mag_avg = (src.magnitude2() * dst.magnitude2()).sqrt();
|
||||||
let dot = src.dot(dst);
|
let dot = src.dot(dst);
|
||||||
if dot.approx_eq(&mag_avg) {
|
if dot.approx_eq(&mag_avg) {
|
||||||
One::one()
|
Quaternion::one()
|
||||||
}else if dot.approx_eq(&-mag_avg) {
|
} else if dot.approx_eq(&-mag_avg) {
|
||||||
let axis = fallback.unwrap_or_else(|| {
|
let axis = fallback.unwrap_or_else(|| {
|
||||||
let mut v = Vector3::unit_x().cross(src);
|
let mut v = Vector3::unit_x().cross(src);
|
||||||
if v.approx_eq(&Zero::zero()) {
|
if v.approx_eq(&Zero::zero()) {
|
||||||
|
@ -82,8 +82,8 @@ impl<S: BaseFloat> Quaternion<S> {
|
||||||
}
|
}
|
||||||
v.normalize()
|
v.normalize()
|
||||||
});
|
});
|
||||||
Rotation3::from_axis_angle(axis, Angle::turn_div_2())
|
Quaternion::from_axis_angle(axis, Rad::turn_div_2())
|
||||||
}else {
|
} else {
|
||||||
Quaternion::from_sv(mag_avg + dot, src.cross(dst)).normalize()
|
Quaternion::from_sv(mag_avg + dot, src.cross(dst)).normalize()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,25 +116,36 @@ mod from {
|
||||||
mod arc {
|
mod arc {
|
||||||
use cgmath::*;
|
use cgmath::*;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn test(src: Vector3<f32>, dst: Vector3<f32>) {
|
fn test(src: Vector3<f32>, dst: Vector3<f32>) {
|
||||||
let q = Quaternion::from_arc(src, dst, None);
|
let q = Quaternion::from_arc(src, dst, None);
|
||||||
let v = q.rotate_vector(src);
|
let v = q.rotate_vector(src);
|
||||||
assert_approx_eq!(v, dst);
|
assert_approx_eq!(v.normalize(), dst.normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_arc() {
|
fn test_same() {
|
||||||
|
let v = Vector3::unit_x();
|
||||||
|
let q = Quaternion::from_arc(v, v, None);
|
||||||
|
assert_eq!(q, Quaternion::new(1.0, 0.0, 0.0, 0.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_opposite() {
|
||||||
let v = Vector3::unit_x();
|
let v = Vector3::unit_x();
|
||||||
{
|
|
||||||
let q = Quaternion::from_arc(v, v, None);
|
|
||||||
assert_eq!(q, Quaternion::new(1.0, 0.0, 0.0, 0.0));
|
|
||||||
}
|
|
||||||
test(v, -v);
|
test(v, -v);
|
||||||
{
|
}
|
||||||
let q: Quaternion<f32> = Quaternion::from_arc(v, Vector3::unit_y(), None);
|
|
||||||
let q2 = Quaternion::from_axis_angle(Vector3::unit_z(), Angle::turn_div_4());
|
#[test]
|
||||||
assert_approx_eq!(q, q2);
|
fn test_random() {
|
||||||
}
|
test(vec3(1.0, 2.0, 3.0), vec3(-4.0, 5.0, -6.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ortho() {
|
||||||
|
let q: Quaternion<f32> = Quaternion::from_arc(Vector3::unit_x(), Vector3::unit_y(), None);
|
||||||
|
let q2 = Quaternion::from_axis_angle(Vector3::unit_z(), Rad::turn_div_4());
|
||||||
|
assert_approx_eq!(q, q2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue