Skip to content

Commit

Permalink
Continue nalgebra migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dotellie committed Nov 10, 2018
1 parent 6f2d404 commit 01f6749
Show file tree
Hide file tree
Showing 39 changed files with 326 additions and 236 deletions.
1 change: 0 additions & 1 deletion amethyst_animation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fnv = "1"
hibitset = { version = "0.5.1", features = ["parallel"] }
itertools = "0.7.8"
log = "0.4"
alga = "0.7"
num-traits = "0.2"
minterpolate = { version = "0.4", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
Expand Down
3 changes: 1 addition & 2 deletions amethyst_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ extern crate hibitset;
extern crate itertools;
#[macro_use]
extern crate log;
extern crate alga;
extern crate num_traits;
extern crate minterpolate;
extern crate num_traits;
#[macro_use]
extern crate serde;

Expand Down
5 changes: 4 additions & 1 deletion amethyst_animation/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ impl AnimationSampling for Transform {
use self::TransformChannel::*;
match channel {
Translation => SamplerPrimitive::Vec3((*self.translation()).into()),
Rotation => SamplerPrimitive::Vec4(self.rotation().as_ref().coords.into()),
Rotation => SamplerPrimitive::Vec4({
let c = self.rotation().as_ref().coords;
[c.w, c.x, c.y, c.z]
}),
Scale => SamplerPrimitive::Vec3(self.scale.into()),
}
}
Expand Down
3 changes: 1 addition & 2 deletions amethyst_animation/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use alga::general::Real;
use num_traits::cast::{NumCast, ToPrimitive};
use minterpolate::InterpolationPrimitive;

use amethyst_core::{
cgmath::{num_traits::NumCast, BaseNum},
nalgebra::Real,
specs::prelude::{Entity, WriteStorage},
};

Expand Down
8 changes: 4 additions & 4 deletions amethyst_controls/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ where
let y = get_input_axis_simple(&self.up_input_axis, &input);
let z = get_input_axis_simple(&self.forward_input_axis, &input);

let dir = Unit::new_normalize(Vector3::new(x, y, z));

for (transform, _) in (&mut transform, &tag).join() {
transform.move_along_local(dir, time.delta_seconds() * self.speed);
if let Some(dir) = Unit::try_new(Vector3::new(x, y, z), 1.0e-6) {
for (transform, _) in (&mut transform, &tag).join() {
transform.move_along_local(dir, time.delta_seconds() * self.speed);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion amethyst_core/src/orientation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Orientation of objects
use nalgebra::{self as na, Matrix3, Vector3};
use nalgebra::{Matrix3, Vector3};

/// This struct contains 3 unit vectors pointing in the given directions.
///
Expand Down
95 changes: 83 additions & 12 deletions amethyst_core/src/transform/components/local_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ impl Transform {
///
/// ```rust,no_run
/// # use amethyst_core::transform::components::Transform;
/// # use amethyst_core::cgmath::{Quaternion, One, Vector3, Point3, Matrix3};
/// # use amethyst_core::nalgebra::{UnitQuaternion, Quaternion, Vector3};
/// let mut t = Transform::default();
/// // No rotation by default
/// assert_eq!(t.rotation, Quaternion::one());
/// assert_eq!(*t.iso.rotation.quaternion(), Quaternion::identity());
/// // look up with up pointing backwards
/// t.look_at(Point3::new(0.0, 1.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
/// t.look_at(Vector3::new(0.0, 1.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
/// // our rotation should match the angle from straight ahead to straight up
/// let rotation = Quaternion::from_arc(
/// Vector3::new(0.0, 0.0, -1.0),
/// Vector3::new(0.0, 1.0, 0.0),
/// None);
/// assert_eq!(t.rotation, rotation);
/// let rotation = UnitQuaternion::rotation_between(
/// &Vector3::new(0.0, 0.0, -1.0),
/// &Vector3::new(0.0, 1.0, 0.0),
/// ).unwrap();
/// assert_eq!(t.iso.rotation, rotation);
/// ```
// FIXME doctest
// TODO: fix example
Expand Down Expand Up @@ -86,41 +86,48 @@ impl Transform {
// w.x, w.y, w.z, w.w, // Column 4
// )

Matrix4::new_nonuniform_scaling(&self.scale) * self.iso.to_homogeneous()
self.iso.to_homogeneous() * Matrix4::new_nonuniform_scaling(&self.scale)
}

/// Returns a reference to the translation vector.
#[inline]
pub fn translation(&self) -> &Vector3<f32> {
&self.iso.translation.vector
}

/// Returns a mutable reference to the translation vector.
#[inline]
pub fn translation_mut(&mut self) -> &mut Vector3<f32> {
&mut self.iso.translation.vector
}

/// Returns a reference to the rotation quaternion.
#[inline]
pub fn rotation(&self) -> &UnitQuaternion<f32> {
&self.iso.rotation
}

/// Returns a mutable reference to the rotation quaternion.
#[inline]
pub fn rotation_mut(&mut self) -> &mut UnitQuaternion<f32> {
&mut self.iso.rotation
}

/// Returns a reference to the isometry of the transform (translation and rotation combined).
#[inline]
pub fn isometry(&self) -> &Isometry3<f32> {
&self.iso
}

/// Returns a mutable reference to the isometry of the transform (translation and rotation
/// combined).
#[inline]
pub fn isometry_mut(&mut self) -> &mut Isometry3<f32> {
&mut self.iso
}

/// Convert this transform's rotation into an Orientation, guaranteed to be 3 unit orthogonal
/// vectors
/// vectors.
pub fn orientation(&self) -> Orientation {
Orientation::from(*self.iso.rotation.to_rotation_matrix().matrix())
}
Expand Down Expand Up @@ -155,8 +162,7 @@ impl Transform {
/// It will not move in the case where the axis is zero, for any distance.
#[inline]
pub fn move_along_local(&mut self, direction: Unit<Vector3<f32>>, distance: f32) -> &mut Self {
self.iso.translation.vector +=
self.iso.rotation * direction.as_ref() * distance;
self.iso.translation.vector += self.iso.rotation * direction.as_ref() * distance;
self
}

Expand Down Expand Up @@ -197,6 +203,48 @@ impl Transform {
self.move_local(Vector3::new(0.0, -amount, 0.0))
}

/// Adds the specified amount to the translation vectors x component.
#[inline]
pub fn add_x(&mut self, amount: f32) -> &mut Self {
self.iso.translation.vector.x += amount;
self
}

/// Adds the specified amount to the translation vectors y component.
#[inline]
pub fn add_y(&mut self, amount: f32) -> &mut Self {
self.iso.translation.vector.y += amount;
self
}

/// Adds the specified amount to the translation vectors z component.
#[inline]
pub fn add_z(&mut self, amount: f32) -> &mut Self {
self.iso.translation.vector.z += amount;
self
}

/// Sets the translation vectors x component to the specified value.
#[inline]
pub fn set_x(&mut self, value: f32) -> &mut Self {
self.iso.translation.vector.x = value;
self
}

/// Sets the translation vectors y component to the specified value.
#[inline]
pub fn set_y(&mut self, value: f32) -> &mut Self {
self.iso.translation.vector.y = value;
self
}

/// Sets the translation vectors z component to the specified value.
#[inline]
pub fn set_z(&mut self, value: f32) -> &mut Self {
self.iso.translation.vector.z = value;
self
}

/// Pitch relatively to the world.
#[inline]
pub fn pitch_global(&mut self, angle: f32) -> &mut Self {
Expand Down Expand Up @@ -255,11 +303,33 @@ impl Transform {
self
}

/// Adds the specified amounts to the translation vector.
pub fn add_xyz(&mut self, x: f32, y: f32, z: f32) -> &mut Self {
self.add_x(x);
self.add_y(y);
self.add_z(z);
self
}

/// Sets the specified values of the translation vector.
pub fn set_xyz(&mut self, x: f32, y: f32, z: f32) -> &mut Self {
self.set_position(Vector3::new(x, y, z))
}

/// Sets the rotation of the transform.
pub fn set_rotation(&mut self, rotation: UnitQuaternion<f32>) -> &mut Self {
self.iso.rotation = rotation;
self
}

/// Sets the scale of the transform.
pub fn set_scale(&mut self, x: f32, y: f32, z: f32) -> &mut Self {
self.scale.x = x;
self.scale.y = y;
self.scale.z = z;
self
}

/// Set the rotation using Euler x, y, z.
///
/// # Arguments
Expand All @@ -272,6 +342,7 @@ impl Transform {
self
}

/// Concatenates another transform onto `self`.
pub fn concat(&mut self, other: &Self) -> &mut Self {
self.scale.component_mul_assign(&other.scale);
self.iso.rotation *= other.iso.rotation;
Expand Down
57 changes: 25 additions & 32 deletions amethyst_core/src/transform/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'a> System<'a> for TransformSystem {

#[cfg(test)]
mod tests {
use cgmath::{Decomposed, Matrix4, One, Quaternion, Vector3, Zero};
use nalgebra::{Matrix4, Quaternion, Unit};
use shred::RunNow;
use specs::prelude::{Builder, World};
use specs_hierarchy::{Hierarchy, HierarchySystem};
Expand All @@ -150,20 +150,15 @@ mod tests {
#[test]
fn transform_matrix() {
let mut transform = Transform::default();
transform.translation = Vector3::new(5.0, 2.0, -0.5);
transform.rotation = Quaternion::new(1.0, 0.0, 0.0, 0.0);
transform.scale = Vector3::new(2.0, 2.0, 2.0);

let decomposed = Decomposed {
rot: transform.rotation,
disp: transform.translation,
scale: 2.0,
};
transform.set_xyz(5.0, 2.0, -0.5);
transform.set_rotation(Unit::new_normalize(Quaternion::new(1.0, 0.0, 0.0, 0.0)));
transform.set_scale(2.0, 2.0, 2.0);

let matrix = transform.matrix();
let cg_matrix: Matrix4<f32> = decomposed.into();
let combined = Matrix4::new_translation(&transform.iso.translation.vector)
* transform.iso.rotation.to_rotation_matrix().to_homogeneous()
* Matrix4::new_scaling(2.0);

assert_eq!(matrix, cg_matrix);
assert_eq!(transform.matrix(), combined);
}

#[test]
Expand Down Expand Up @@ -201,9 +196,7 @@ mod tests {
fn zeroed() {
let (mut world, mut hs, mut system) = transform_world();

let mut transform = Transform::default();
transform.translation = Vector3::zero();
transform.rotation = Quaternion::one();
let transform = Transform::default();

let e1 = world
.create_entity()
Expand Down Expand Up @@ -232,8 +225,8 @@ mod tests {
let (mut world, mut hs, mut system) = transform_world();

let mut local = Transform::default();
local.translation = Vector3::new(5.0, 5.0, 5.0);
local.rotation = Quaternion::new(1.0, 0.5, 0.5, 0.0);
local.set_xyz(5.0, 5.0, 5.0);
local.set_rotation(Unit::new_normalize(Quaternion::new(1.0, 0.5, 0.5, 0.0)));

let e1 = world
.create_entity()
Expand All @@ -260,8 +253,8 @@ mod tests {
let (mut world, mut hs, mut system) = transform_world();

let mut local1 = Transform::default();
local1.translation = Vector3::new(5.0, 5.0, 5.0);
local1.rotation = Quaternion::new(1.0, 0.5, 0.5, 0.0);
local1.set_xyz(5.0, 5.0, 5.0);
local1.set_rotation(Unit::new_normalize(Quaternion::new(1.0, 0.5, 0.5, 0.0)));

let e1 = world
.create_entity()
Expand All @@ -270,8 +263,8 @@ mod tests {
.build();

let mut local2 = Transform::default();
local2.translation = Vector3::new(5.0, 5.0, 5.0);
local2.rotation = Quaternion::new(1.0, 0.5, 0.5, 0.0);
local2.set_xyz(5.0, 5.0, 5.0);
local2.set_rotation(Unit::new_normalize(Quaternion::new(1.0, 0.5, 0.5, 0.0)));

let e2 = world
.create_entity()
Expand All @@ -281,8 +274,8 @@ mod tests {
.build();

let mut local3 = Transform::default();
local3.translation = Vector3::new(5.0, 5.0, 5.0);
local3.rotation = Quaternion::new(1.0, 0.5, 0.5, 0.0);
local3.set_xyz(5.0, 5.0, 5.0);
local3.set_rotation(Unit::new_normalize(Quaternion::new(1.0, 0.5, 0.5, 0.0)));

let e3 = world
.create_entity()
Expand Down Expand Up @@ -328,8 +321,8 @@ mod tests {
let (mut world, mut hs, mut system) = transform_world();

let mut local3 = Transform::default();
local3.translation = Vector3::new(5.0, 5.0, 5.0);
local3.rotation = Quaternion::new(1.0, 0.5, 0.5, 0.0);
local3.set_xyz(5.0, 5.0, 5.0);
local3.set_rotation(Unit::new_normalize(Quaternion::new(1.0, 0.5, 0.5, 0.0)));

let e3 = world
.create_entity()
Expand All @@ -338,8 +331,8 @@ mod tests {
.build();

let mut local2 = Transform::default();
local2.translation = Vector3::new(5.0, 5.0, 5.0);
local2.rotation = Quaternion::new(1.0, 0.5, 0.5, 0.0);
local2.set_xyz(5.0, 5.0, 5.0);
local2.set_rotation(Unit::new_normalize(Quaternion::new(1.0, 0.5, 0.5, 0.0)));

let e2 = world
.create_entity()
Expand All @@ -348,8 +341,8 @@ mod tests {
.build();

let mut local1 = Transform::default();
local1.translation = Vector3::new(5.0, 5.0, 5.0);
local1.rotation = Quaternion::new(1.0, 0.5, 0.5, 0.0);
local1.set_xyz(5.0, 5.0, 5.0);
local1.set_rotation(Unit::new_normalize(Quaternion::new(1.0, 0.5, 0.5, 0.0)));

let e1 = world
.create_entity()
Expand Down Expand Up @@ -401,7 +394,7 @@ mod tests {

let mut local = Transform::default();
// Release the indeterminate forms!
local.translation = Vector3::new(0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0);
local.set_xyz(0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0);

world
.create_entity()
Expand All @@ -421,7 +414,7 @@ mod tests {

let mut local = Transform::default();
// Release the indeterminate forms!
local.translation = Vector3::new(1.0 / 0.0, 1.0 / 0.0, 1.0 / 0.0);
local.set_xyz(1.0 / 0.0, 1.0 / 0.0, 1.0 / 0.0);
world
.create_entity()
.with(local.clone())
Expand Down
Loading

0 comments on commit 01f6749

Please sign in to comment.