You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.6 KiB
128 lines
3.6 KiB
//
|
|
// Stuff for manipulating 3 dimensional vectors.
|
|
//
|
|
// Georg Hopp <georg@steffers.org>
|
|
//
|
|
// Copyright © 2019 Georg Hopp
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
//
|
|
use std::fmt::{Formatter, Display, Result};
|
|
use std::ops::{Add, Sub, Neg, Mul, Div};
|
|
|
|
use crate::trigonometry::Trig;
|
|
|
|
#[derive(Debug, Eq, Clone, Copy)]
|
|
pub struct Vector<T>(pub T, pub T, pub T)
|
|
where T: Add + Sub + Neg + Mul + Div + Trig + Copy;
|
|
|
|
impl<T> Vector<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T> + Trig + Copy {
|
|
pub fn x(self) -> T { self.0 }
|
|
pub fn y(self) -> T { self.1 }
|
|
pub fn z(self) -> T { self.2 }
|
|
|
|
pub fn abs(self) -> T {
|
|
let Vector(x, y, z) = self;
|
|
(x * x + y * y + z * z).sqrt().unwrap()
|
|
}
|
|
|
|
pub fn mul(self, s :&T) -> Self {
|
|
let Vector(x, y, z) = self;
|
|
Vector(x * *s, y * *s, z * *s)
|
|
}
|
|
|
|
pub fn dot(self, other :Self) -> T {
|
|
let Vector(x1, y1, z1) = self;
|
|
let Vector(x2, y2, z2) = other;
|
|
|
|
x1 * x2 + y1 * y2 + z1 * z2
|
|
}
|
|
|
|
pub fn norm(self) -> Self {
|
|
// TODO This can result in 0 or inf Vectors…
|
|
// Maybe we need to handle zero and inf abs here…
|
|
self.mul(&self.abs())
|
|
}
|
|
|
|
pub fn distance(self, other :Self) -> T {
|
|
(self - other).abs()
|
|
}
|
|
}
|
|
|
|
impl<T> Display for Vector<T>
|
|
where T: Add + Sub + Neg + Mul + Div + Trig + Display + Copy {
|
|
fn fmt(&self, f :&mut Formatter<'_>) -> Result {
|
|
let Vector(x, y, z) = self;
|
|
write!(f, "({}, {}, {})", x, y, z)
|
|
}
|
|
}
|
|
|
|
impl<T> PartialEq for Vector<T>
|
|
where T: Add + Sub + Neg + Mul + Div + Trig + PartialEq + Copy {
|
|
fn eq(&self, other :&Self) -> bool {
|
|
let Vector(x1, y1, z1) = self;
|
|
let Vector(x2, y2, z2) = other;
|
|
x1 == x2 && y1 == y2 && z1 == z2
|
|
}
|
|
}
|
|
|
|
impl<T> Add for Vector<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T> + Trig + Copy {
|
|
type Output = Self;
|
|
|
|
fn add(self, other :Self) -> Self {
|
|
let Vector(x1, y1, z1) = self;
|
|
let Vector(x2, y2, z2) = other;
|
|
Vector(x1 + x2, y1 + y2, z1 + z2)
|
|
}
|
|
}
|
|
|
|
impl<T> Sub for Vector<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T> + Trig + Copy {
|
|
type Output = Self;
|
|
|
|
fn sub(self, other :Self) -> Self {
|
|
self + -other
|
|
}
|
|
}
|
|
|
|
impl<T> Neg for Vector<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T> + Trig + Copy {
|
|
type Output = Self;
|
|
|
|
fn neg(self) -> Self {
|
|
let Vector(x, y, z) = self;
|
|
Self(-x, -y, -z)
|
|
}
|
|
}
|
|
|
|
impl<T> Mul for Vector<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T> + Trig + Copy {
|
|
type Output = Self;
|
|
|
|
fn mul(self, other :Self) -> Self {
|
|
let Vector(ax, ay, az) = self;
|
|
let Vector(bx, by, bz) = other;
|
|
|
|
Vector( ay * bz - az * by
|
|
, az * bx - ax * bz
|
|
, ax * by - ay * bx )
|
|
}
|
|
}
|