From a522a9d1cdc7e28a50d7b8ed122ed96fedebf0fc Mon Sep 17 00:00:00 2001 From: Julia Blaauboer <44556294+juliablbr@users.noreply.github.com> Date: Thu, 10 Oct 2024 19:35:19 +0200 Subject: [PATCH] Replace `Vector{2,3}` with `nalgebra::Vector{2,3}` (#70) Co-authored-by: Aemulation <112875887+Aemulation@users.noreply.github.com> --- nidhogg/Cargo.toml | 1 + nidhogg/src/backend/lola.rs | 15 +- nidhogg/src/lib.rs | 3 +- nidhogg/src/types/mod.rs | 2 - nidhogg/src/types/vector.rs | 641 ------------------------------------ 5 files changed, 8 insertions(+), 654 deletions(-) delete mode 100644 nidhogg/src/types/vector.rs diff --git a/nidhogg/Cargo.toml b/nidhogg/Cargo.toml index b0be21b..4b60fd4 100644 --- a/nidhogg/Cargo.toml +++ b/nidhogg/Cargo.toml @@ -15,6 +15,7 @@ tracing = "0.1.37" nidhogg_derive = { workspace = true } coppeliasim_zmq_remote_api = { git = "https://github.com/samuel-cavalcanti/rust_zmqRemoteApi", optional = true } num = "0.4.1" +nalgebra = { version = "0.32.6", features = ["serde-serialize"] } bevy_ecs = { version = "0.14.2", optional = true } [dev-dependencies] diff --git a/nidhogg/src/backend/lola.rs b/nidhogg/src/backend/lola.rs index 7bcab61..ffc5f27 100644 --- a/nidhogg/src/backend/lola.rs +++ b/nidhogg/src/backend/lola.rs @@ -4,11 +4,13 @@ use crate::{ types::{ Battery, ForceSensitiveResistorFoot, ForceSensitiveResistors, JointArray, LeftEar, LeftEye, - Rgb, RgbF32, RightEar, RightEye, Skull, SonarEnabled, SonarValues, Touch, Vector2, Vector3, + Rgb, RgbF32, RightEar, RightEye, Skull, SonarEnabled, SonarValues, Touch, }, DisconnectExt, Error, HardwareInfo, NaoBackend, NaoControlMessage, NaoState, Result, }; +use nalgebra::{Vector2, Vector3}; + use rmp_serde::{encode, from_slice}; use serde::{Deserialize, Serialize}; use std::{ @@ -487,20 +489,13 @@ impl FromLoLA<[f32; 14]> for Touch { impl FromLoLA<[f32; 2]> for Vector2 { fn from_lola(value: [f32; 2]) -> Self { - Vector2 { - x: value[0], - y: value[1], - } + Vector2::from(value) } } impl FromLoLA<[f32; 3]> for Vector3 { fn from_lola(value: [f32; 3]) -> Self { - Vector3 { - x: value[0], - y: value[1], - z: value[2], - } + Vector3::from(value) } } diff --git a/nidhogg/src/lib.rs b/nidhogg/src/lib.rs index 9bd4c90..a2ccb94 100644 --- a/nidhogg/src/lib.rs +++ b/nidhogg/src/lib.rs @@ -39,10 +39,11 @@ mod error; pub mod types; pub use error::{Error, Result}; +use nalgebra::{Vector2, Vector3}; use nidhogg_derive::Builder; use types::{ color::RgbF32, Battery, FillExt, ForceSensitiveResistors, JointArray, LeftEar, LeftEye, - RightEar, RightEye, Skull, SonarEnabled, SonarValues, Touch, Vector2, Vector3, + RightEar, RightEye, Skull, SonarEnabled, SonarValues, Touch, }; #[cfg(feature = "serde")] diff --git a/nidhogg/src/types/mod.rs b/nidhogg/src/types/mod.rs index 626c154..a4aae43 100644 --- a/nidhogg/src/types/mod.rs +++ b/nidhogg/src/types/mod.rs @@ -11,11 +11,9 @@ use bevy_ecs::prelude::Resource; pub mod color; mod joint_array; -mod vector; pub use color::{Rgb, RgbF32, RgbU8}; pub use joint_array::JointArray; -pub use vector::{Vector2, Vector3}; /// Trait that introduces the [`fill`](`FillExt::fill`) method for a type, which allows filling in all fields with the same value. pub trait FillExt { diff --git a/nidhogg/src/types/vector.rs b/nidhogg/src/types/vector.rs deleted file mode 100644 index 4361c3b..0000000 --- a/nidhogg/src/types/vector.rs +++ /dev/null @@ -1,641 +0,0 @@ -//! Implements [`Vector3`] and [`Vector2`] types and associated traits, used for directional data. - -use crate::types::FillExt; - -use forward_ref_generic::forward_ref_binop; -use num::traits::{Pow, PrimInt}; -use serde::{Deserialize, Serialize}; -use std::iter::Sum; -use std::ops::{Add, Div, Mul, Sub}; - -/// Struct representing a two-dimensional vector, containing two values of type `T` -#[derive(Debug, Clone, Default, Copy, PartialEq)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Vector2 { - pub x: T, - pub y: T, -} - -/// Struct representing a three-dimensional vector, containing three values of type `T` -#[derive(Debug, Clone, Default, Copy, PartialEq)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Vector3 { - pub x: T, - pub y: T, - pub z: T, -} - -/// Element-wise addition for [`Vector2`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector2; -/// -/// let one = Vector2{x: 1, y: 1}; -/// let two = Vector2{x: 2, y: 2}; -/// let three = Vector2{x: 3, y: 3}; -/// -/// assert_eq!(one + two, three); -/// ``` -impl Add for Vector2 -where - T: Add, -{ - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output { - Self { - x: self.x + rhs.x, - y: self.y + rhs.y, - } - } -} - -forward_ref_binop! { [T] impl Add for Vector2 where T: Copy + Add} - -/// Element-wise subtraction for [`Vector2`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector2; -/// -/// let one = Vector2{x: 1, y: 1}; -/// let two = Vector2{x: 2, y: 2}; -/// let three = Vector2{x: 3, y: 3}; -/// -/// assert_eq!(three - two, one); -/// ``` -impl Sub for Vector2 -where - T: Sub, -{ - type Output = Self; - - fn sub(self, rhs: Self) -> Self::Output { - Self { - x: self.x - rhs.x, - y: self.y - rhs.y, - } - } -} - -forward_ref_binop! { [T] impl Sub for Vector2 where T: Copy + Sub} - -/// Element-wise division for [`Vector2`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector2; -/// -/// let two = Vector2{x: 2, y: 2}; -/// let four = Vector2{x: 4, y: 4}; -/// -/// assert_eq!(four / two, two); -/// ``` -impl Div for Vector2 -where - T: Div, -{ - type Output = Self; - - fn div(self, rhs: Self) -> Self::Output { - Self { - x: self.x / rhs.x, - y: self.y / rhs.y, - } - } -} - -forward_ref_binop! { [T] impl Div for Vector2 where T: Copy + Div} - -/// Element-wise multiplication for [`Vector2`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector2; -/// -/// let two = Vector2{x: 2, y: 2}; -/// let four = Vector2{x: 4, y: 4}; -/// -/// assert_eq!(two * two, four); -/// ``` -impl Mul for Vector2 -where - T: Mul, -{ - type Output = Self; - - fn mul(self, rhs: Self) -> Self::Output { - Self { - x: self.x * rhs.x, - y: self.y * rhs.y, - } - } -} - -forward_ref_binop! { [T] impl Mul for Vector2 where T: Copy + Mul} - -/// Element-wise exponentiation for [`Vector2`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector2; -/// use num::traits::Pow; -/// -/// let two = Vector2{x: 2, y: 2}; -/// let four = Vector2{x: 4, y: 4}; -/// -/// assert_eq!(two.pow(2u32), four); -/// ``` -impl Pow for Vector2 -where - T: Pow, - EXP: PrimInt, -{ - type Output = Self; - - fn pow(self, exponent: EXP) -> Self::Output { - Self { - x: self.x.pow(exponent), - y: self.y.pow(exponent), - } - } -} - -/// Implements the sum trait for [`Vector2`] struct, addition will be conducted element-wise -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector2; -/// -/// let two = Vector2{x: 2, y: 2}; -/// let six = Vector2{x: 6, y: 6}; -/// let array = [two, two, two]; -/// -/// assert_eq!(array.into_iter().sum::>(), six); -/// ``` -impl Sum for Vector2 -where - T: Add + Default, -{ - fn sum(iter: I) -> Vector2 - where - I: Iterator>, - { - iter.fold(Vector2::default(), |acc, elem| acc + elem) - } -} - -/// Implements the sum trait for a reference to a [`Vector2`] struct, addition will be conducted element-wise -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector2; -/// -/// let two = Vector2{x: 2, y: 2}; -/// let six = Vector2{x: 6, y: 6}; -/// let array = [two, two, two]; -/// -/// assert_eq!(array.iter().sum::>(), six); -/// ``` -impl<'a, T> Sum<&'a Vector2> for Vector2 -where - T: Default + Copy + Add, -{ - fn sum(iter: I) -> Vector2 - where - T: Default + Copy + Add, - I: Iterator>, - { - iter.fold(Vector2::default(), |acc, elem| acc + elem) - } -} - -/// # Examples -/// -/// ``` -/// use nidhogg::types::{Vector2, FillExt}; -/// -/// let two = Vector2{x: 2, y: 2}; -/// -/// assert_eq!(two, Vector2::fill(2)); -/// ``` -impl FillExt for Vector2 { - fn fill(value: T) -> Vector2 { - Self { - x: value.clone(), - y: value.clone(), - } - } -} - -/// Element-wise addition for [`Vector3`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector3; -/// -/// let one = Vector3{x: 1, y: 1, z: 1}; -/// let two = Vector3{x: 2, y: 2, z: 2}; -/// let three = Vector3{x: 3, y: 3, z: 3}; -/// -/// assert_eq!(one + two, three); -/// ``` -impl Add for Vector3 -where - T: Add, -{ - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output { - Self { - x: self.x + rhs.x, - y: self.y + rhs.y, - z: self.z + rhs.z, - } - } -} - -forward_ref_binop! { [T] impl Add for Vector3 where T: Copy + Add} - -/// Element-wise subtraction for [`Vector3`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector3; -/// -/// let one = Vector3{x: 1, y: 1, z: 1}; -/// let two = Vector3{x: 2, y: 2, z: 2}; -/// let three = Vector3{x: 3, y: 3, z: 3}; -/// -/// assert_eq!(three - two, one); -/// ``` -impl Sub for Vector3 -where - T: Sub, -{ - type Output = Self; - - fn sub(self, rhs: Self) -> Self::Output { - Self { - x: self.x - rhs.x, - y: self.y - rhs.y, - z: self.z - rhs.z, - } - } -} - -forward_ref_binop! { [T] impl Sub for Vector3 where T: Copy + Sub} - -/// Element-wise division for [`Vector3`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector3; -/// -/// let two = Vector3{x: 2, y: 2, z: 2}; -/// let four = Vector3{x: 4, y: 4, z: 4}; -/// -/// assert_eq!(four / two, two); -/// ``` -impl Div for Vector3 -where - T: Div, -{ - type Output = Self; - - fn div(self, rhs: Self) -> Self::Output { - Self { - x: self.x / rhs.x, - y: self.y / rhs.y, - z: self.z / rhs.z, - } - } -} - -forward_ref_binop! { [T] impl Div for Vector3 where T: Copy + Div} - -/// Element-wise multiplication for [`Vector3`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector3; -/// -/// let two = Vector3{x: 2, y: 2, z: 2}; -/// let four = Vector3{x: 4, y: 4, z: 4}; -/// -/// assert_eq!(two * two, four); -/// ``` -impl Mul for Vector3 -where - T: Mul, -{ - type Output = Self; - - fn mul(self, rhs: Self) -> Self::Output { - Self { - x: self.x * rhs.x, - y: self.y * rhs.y, - z: self.z * rhs.z, - } - } -} - -forward_ref_binop! { [T] impl Mul for Vector3 where T: Copy + Mul} - -/// Element-wise exponentiation for [`Vector3`] struct -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector3; -/// use num::traits::Pow; -/// -/// let two = Vector3{x: 2, y: 2, z: 2}; -/// let four = Vector3{x: 4, y: 4, z: 4}; -/// -/// assert_eq!(two.pow(2u32), four); -/// ``` -impl Pow for Vector3 -where - T: Pow, - EXP: PrimInt, -{ - type Output = Self; - - fn pow(self, exponent: EXP) -> Self::Output { - Self { - x: self.x.pow(exponent), - y: self.y.pow(exponent), - z: self.z.pow(exponent), - } - } -} - -/// Implements the sum trait for [`Vector3`] struct, addition will be conducted element-wise -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector3; -/// -/// let two = Vector3{x: 2, y: 2, z: 2}; -/// let six = Vector3{x: 6, y: 6, z: 6}; -/// let array = [two, two, two]; -/// -/// assert_eq!(array.into_iter().sum::>(), six); -/// ``` -impl Sum for Vector3 -where - T: Add + Default, -{ - fn sum(iter: I) -> Vector3 - where - I: Iterator>, - { - iter.fold(Vector3::default(), |acc, elem| acc + elem) - } -} - -/// Implements the sum trait for a reference to a [`Vector3`] struct, addition will be conducted element-wise -/// -/// # Examples -/// -/// ``` -/// use nidhogg::types::Vector3; -/// -/// let two = Vector3{x: 2, y: 2, z: 2}; -/// let six = Vector3{x: 6, y: 6, z: 6}; -/// let array = [two, two, two]; -/// -/// assert_eq!(array.iter().sum::>(), six); -/// ``` -impl<'a, T> Sum<&'a Vector3> for Vector3 -where - T: Default + Copy + Add, -{ - fn sum(iter: I) -> Vector3 - where - T: Default + Copy + Add, - I: Iterator>, - { - iter.fold(Vector3::default(), |acc, elem| acc + elem) - } -} - -impl FillExt for Vector3 { - fn fill(value: T) -> Vector3 { - Self { - x: value.clone(), - y: value.clone(), - z: value.clone(), - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_vector2_add_sub() { - let vec1 = Vector2:: { x: 2f32, y: 3f32 }; - let vec2 = Vector2:: { x: 1f32, y: 2f32 }; - - assert_eq!(vec1 + vec2, Vector2 { x: 3f32, y: 5f32 },); - assert_eq!(vec1 - vec2, Vector2 { x: 1f32, y: 1f32 },); - } - - #[test] - fn test_vector2_mul_div() { - let vec1 = Vector2:: { x: 8f32, y: 24f32 }; - let vec2 = Vector2:: { x: 2f32, y: 6f32 }; - - assert_eq!( - vec1 * vec2, - Vector2 { - x: 16f32, - y: 144f32, - }, - ); - assert_eq!(vec1 / vec2, Vector2 { x: 4f32, y: 4f32 },); - } - - #[test] - fn test_vector2_pow() { - let base = Vector2:: { x: 10f32, y: 11f32 }; - let exponent = 2; - assert_eq!( - base.pow(exponent), - Vector2 { - x: 100f32, - y: 121f32, - }, - ); - } - - #[test] - fn test_vector2_sum() { - let vec1 = Vector2:: { x: 1f32, y: 1f32 }; - let vec2 = Vector2:: { x: 2f32, y: 2f32 }; - let vec3 = Vector2:: { x: 3f32, y: 3f32 }; - - let array = [vec1, vec2, vec3]; - - assert_eq!( - array.iter().sum::>(), - Vector2 { x: 6f32, y: 6f32 }, - ); - } - - #[test] - fn test_vector2_fill() { - let vec = Vector2::fill(73f32); - - assert_eq!(vec, Vector2 { x: 73f32, y: 73f32 }) - } - - #[test] - fn test_vector3_add_sub() { - let vec1 = Vector3:: { - x: 2f32, - y: 3f32, - z: 4f32, - }; - let vec2 = Vector3:: { - x: 1f32, - y: 2f32, - z: 3f32, - }; - - assert_eq!( - vec1 + vec2, - Vector3 { - x: 3f32, - y: 5f32, - z: 7f32, - }, - ); - assert_eq!( - vec1 - vec2, - Vector3 { - x: 1f32, - y: 1f32, - z: 1f32, - }, - ); - } - - #[test] - fn test_vector3_mul_div() { - let vec1 = Vector3:: { - x: 8f32, - y: 24f32, - z: 40f32, - }; - let vec2 = Vector3:: { - x: 2f32, - y: 6f32, - z: 10f32, - }; - - assert_eq!( - vec1 * vec2, - Vector3 { - x: 16f32, - y: 144f32, - z: 400f32, - }, - ); - assert_eq!( - vec1 / vec2, - Vector3 { - x: 4f32, - y: 4f32, - z: 4f32, - }, - ); - } - - #[test] - fn test_vector3_pow() { - let base = Vector3:: { - x: 10f32, - y: 11f32, - z: 12f32, - }; - let exponent = 2; - assert_eq!( - base.pow(exponent), - Vector3 { - x: 100f32, - y: 121f32, - z: 144f32, - }, - ); - } - - #[test] - fn test_vector3_sum() { - let vec1 = Vector3:: { - x: 1f32, - y: 1f32, - z: 1f32, - }; - let vec2 = Vector3:: { - x: 2f32, - y: 2f32, - z: 2f32, - }; - let vec3 = Vector3:: { - x: 3f32, - y: 3f32, - z: 3f32, - }; - - let array = [vec1, vec2, vec3]; - - assert_eq!( - array.iter().sum::>(), - Vector3 { - x: 6f32, - y: 6f32, - z: 6f32, - }, - ); - - assert_eq!( - array.into_iter().sum::>(), - Vector3 { - x: 6f32, - y: 6f32, - z: 6f32, - }, - ); - } - - #[test] - fn test_vector3_fill() { - let vec = Vector3::fill(73f32); - - assert_eq!( - vec, - Vector3 { - x: 73f32, - y: 73f32, - z: 73f32, - } - ) - } -}