use core::fmt;
use core::ops::{Deref, DerefMut};
use num::{Float, Num, Signed, Zero};
use crate::slices_methods::{norm_inf, norm_l};
use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
use crate::errors::VectorErrors;
use crate::matrix3x3::M33;
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct V3<T>([T; 3]);
impl<T> V3<T> {
pub const fn new(input: [T; 3]) -> Self {
Self(input)
}
pub const fn new_from(a: T, b: T, c: T) -> Self {
Self::new([a, b, c])
}
}
impl<T: Num + Copy> V3<T> {
#[inline(always)]
pub fn zeros() -> Self {
<Self as Zero>::zero()
}
pub fn ones() -> Self {
let one = T::one();
Self::new_from(one, one, one)
}
#[inline]
pub fn cross(&self, rhs: Self) -> Self {
Self::new_from(
self[1] * rhs[2] - self[2] * rhs[1],
self[2] * rhs[0] - self[0] * rhs[2],
self[0] * rhs[1] - self[1] * rhs[0],
)
}
pub fn x_axis() -> Self {
let one = T::one();
let zero = T::zero();
Self::new_from(one, zero, zero)
}
pub fn y_axis() -> Self {
let one = T::one();
let zero = T::zero();
Self::new_from(zero, one, zero)
}
pub fn z_axis() -> Self {
let one = T::one();
let zero = T::zero();
Self::new_from(zero, zero, one)
}
}
impl<T: Num + Copy + core::cmp::PartialOrd> V3<T> {
pub fn norm_inf(&self) -> T {
norm_inf(&**self)
}
}
impl<T: Num + Copy + Signed + core::iter::Sum> V3<T> {
pub fn norm_l(&self) -> T {
norm_l(&**self)
}
}
impl<T: Num + Copy + Signed> Neg for V3<T> {
type Output = Self;
#[inline(always)]
fn neg(self) -> Self {
Self::new_from(-self[0], -self[1], -self[2])
}
}
impl<T: Float> V3<T> {
#[inline(always)]
pub fn norm2(&self) -> T {
T::sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2])
}
pub fn normalize(&self) -> Result<Self, VectorErrors> {
let n = self.norm2();
if n != T::zero() {
let mut result = Self::zeros();
for i in 0..self.len() {
result[i] = self[i] / n;
}
Ok(result)
} else {
Err(VectorErrors::Norm2IsZero)
}
}
}
impl<T: Num + Copy> Mul<T> for V3<T> {
type Output = Self;
#[inline(always)]
fn mul(self, rhs: T) -> Self::Output {
Self::new_from(self[0] * rhs, self[1] * rhs, self[2] * rhs)
}
}
impl<T: Num + Copy> Div<T> for V3<T> {
type Output = Self;
#[inline(always)]
fn div(self, rhs: T) -> Self::Output {
Self::new_from(self[0] / rhs, self[1] / rhs, self[2] / rhs)
}
}
impl Mul<V3<f32>> for f32 {
type Output = V3<f32>;
#[inline(always)]
fn mul(self, rhs: V3<f32>) -> Self::Output {
V3::new_from(rhs[0] * self, rhs[1] * self, rhs[2] * self)
}
}
impl Mul<V3<f64>> for f64 {
type Output = V3<f64>;
#[inline(always)]
fn mul(self, rhs: V3<f64>) -> Self::Output {
V3::new_from(rhs[0] * self, rhs[1] * self, rhs[2] * self)
}
}
impl<T: Num + Copy> Mul for V3<T> {
type Output = T;
#[inline(always)]
fn mul(self, rhs: Self) -> T {
self[0] * rhs[0] + self[1] * rhs[1] + self[2] * rhs[2]
}
}
impl<T: Num + Copy> Mul<M33<T>> for V3<T> {
type Output = V3<T>;
#[inline]
fn mul(self, rhs: M33<T>) -> V3<T> {
Self::new_from(
rhs[(0, 0)] * self[0] + rhs[(0, 1)] * self[1] + rhs[(0, 2)] * self[2],
rhs[(1, 0)] * self[0] + rhs[(1, 1)] * self[1] + rhs[(1, 2)] * self[2],
rhs[(2, 0)] * self[0] + rhs[(2, 1)] * self[1] + rhs[(2, 2)] * self[2])
}
}
impl<T: Num + Copy> Sub for V3<T> {
type Output = Self;
#[inline(always)]
fn sub(self, rhs: Self) -> Self {
Self::new_from(self[0] - rhs[0], self[1] - rhs[1], self[2] - rhs[2])
}
}
impl<T: Num + Copy> SubAssign for V3<T> {
#[inline(always)]
fn sub_assign(&mut self, other: Self) {
*self = *self - other
}
}
impl<T: Num + Copy> Add for V3<T> {
type Output = Self;
#[inline(always)]
fn add(self, rhs: Self) -> Self {
Self::new_from(self[0] + rhs[0], self[1] + rhs[1], self[2] + rhs[2])
}
}
impl<T: Num + Copy> AddAssign for V3<T> {
#[inline(always)]
fn add_assign(&mut self, other: Self) {
*self = *self + other
}
}
impl<T: Num + Copy> Zero for V3<T> {
#[inline(always)]
fn zero() -> V3<T> {
Self::new_from(T::zero(), T::zero(), T::zero())
}
fn is_zero(&self) -> bool {
*self == V3::zero()
}
}
impl<T> Deref for V3<T> {
type Target = [T; 3];
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for V3<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> From<[T; 3]> for V3<T> {
fn from(data: [T; 3]) -> V3<T> {
V3(data)
}
}
impl<T: Num + fmt::Display> fmt::Display for V3<T> {
fn fmt(&self, dest: &mut fmt::Formatter) -> fmt::Result {
writeln!(
dest,
"[{0:^3.2} {1:^3.2} {2:^3.2}]",
self[0], self[1], self[2]
)
}
}
pub const X_AXIS: V3<f32> = V3::new_from(1.0, 0.0, 0.0);
pub const Y_AXIS: V3<f32> = V3::new_from(0.0, 1.0, 0.0);
pub const Z_AXIS: V3<f32> = V3::new_from(0.0, 0.0, 1.0);
#[cfg(test)]
mod vector3_test {
use crate::vector3::V3;
#[test]
fn create_vector3_test() {
let v = V3::new([1.0, 1.0, 1.0]);
assert_eq!(v[0], 1.0);
assert_eq!(v[1], 1.0);
assert_eq!(v[2], 1.0);
}
#[test]
fn zero_vector3_test() {
let result: V3<f64> = V3::zeros();
let expected = V3::new([0.0, 0.0, 0.0]);
assert_eq!(
&result[..],
&expected[..],
"\nExpected\n{:?}\nfound\n{:?}",
&result[..],
&expected[..]
);
}
#[test]
fn product_test() {
let v1 = V3::new([1.0, 2.0, 3.0]);
let v2 = V3::new([4.0, 5.0, 6.0]);
let result = v1 * v2;
let expected = 32.0;
assert_eq!(result, expected);
}
#[test]
fn add_test() {
let v1 = V3::new([1.0, 2.0, 3.0]);
let v2 = V3::new([4.0, 5.0, 6.0]);
let result = v1 + v2;
let expected = V3::new([5.0, 7.0, 9.0]);
assert_eq!(
&result[..],
&expected[..],
"\nExpected\n{:?}\nfound\n{:?}",
&result[..],
&expected[..]
);
}
#[test]
fn norm2_test() {
let v1 = V3::new([1.0, 2.0, 3.0]);
let expected = 3.7416573867739413;
let result = v1.norm2();
assert_eq!(result, expected);
}
#[test]
fn mul_const_rhs() {
let v = V3::new([1.0, 2.0, 3.0]);
let result: V3<f64> = 2.0 * v;
let expected = V3::new([2.0, 4.0, 6.0]);
assert_eq!(
&result[..],
&expected[..],
"\nExpected\n{:?}\nfound\n{:?}",
&result[..],
&expected[..]
);
}
#[test]
fn mul_const() {
let v = V3::new([1.0, 2.0, 3.0]);
let result = v * 2.0;
let expected = V3::new([2.0, 4.0, 6.0]);
assert_eq!(
&result[..],
&expected[..],
"\nExpected\n{:?}\nfound\n{:?}",
&result[..],
&expected[..]
);
}
#[test]
fn sub_test() {
let v1 = V3::new([1.0, 1.0, 1.0]);
let v2 = V3::new([2.0, 3.0, 4.0]);
let result = v1 - v2;
let expected = V3::new([-1.0, -2.0, -3.0]);
assert_eq!(
&result[..],
&expected[..],
"\nExpected\n{:?}\nfound\n{:?}",
&result[..],
&expected[..]
);
}
#[test]
fn normalize_test() {
let result = V3::new([1.0, 1.0, 1.0]).normalize().unwrap();
let expected = V3::new([0.5773502691896258, 0.5773502691896258, 0.5773502691896258]);
assert_eq!(
&result[..],
&expected[..],
"\nExpected\n{:?}\nfound\n{:?}",
&result[..],
&expected[..]
);
}
#[test]
fn cross_test() {
let x = V3::new([1.0, 0.0, 0.0]);
let y = V3::new([0.0, 1.0, 0.0]);
let result = x.cross(y);
let expected = V3::new([0.0, 0.0, 1.0]);
assert_eq!(
&result[..],
&expected[..],
"\nExpected\n{:?}\nfound\n{:?}",
&result[..],
&expected[..]
);
}
#[test]
fn sub_assigment_test() {
let mut result = V3::new([1.0, 2.0, 3.0]);
let v = V3::new([4.0, 5.0, 6.0]);
let expected = V3::new([-3.0, -3.0, -3.0]);
result -= v;
assert_eq!(
&result[..],
&expected[..],
"\nExpected\n{:?}\nfound\n{:?}",
&result[..],
&expected[..]
);
}
#[test]
fn add_assigment_test() {
let mut result = V3::new_from(1.0, 2.0, 3.0);
let v = V3::new_from(4.0, 5.0, 6.0);
let expected = V3::new_from(5.0, 7.0, 9.0);
result += v;
assert_eq!(
&result[..],
&expected[..],
"\nExpected\n{:?}\nfound\n{:?}",
&result[..],
&expected[..]
);
}
#[test]
fn norm_inf_test() {
let v = V3::new_from(1, -10, 73);
let result = v.norm_inf();
let expected = 73;
assert_eq!(result, expected);
}
#[test]
fn norm_l_test() {
let v = V3::new_from(1, -1, 1);
let result = v.norm_l();
let expected = 3;
assert_eq!(result, expected);
}
}