feat: added some operations to vectors and quaternions

This commit is contained in:
lisk77 2025-03-29 00:11:43 +01:00
parent c90d09fd49
commit 1558a9896c
3 changed files with 184 additions and 27 deletions

View file

@ -1,8 +1,9 @@
use std::ops::*;
use std::ops::Mul;
use crate::vector::Vec3;
/// Representation of a quaternion in scalar/vector form
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Quat {
pub s: f32,
pub v: Vec3,
@ -44,6 +45,72 @@ impl Quat {
}
}
impl Add<Quat> for Quat {
type Output = Quat;
fn add(self, other: Quat) -> Quat {
Quat {
s: self.s + other.s,
v: self.v + other.v,
}
}
}
impl Sub<Quat> for Quat {
type Output = Quat;
fn sub(self, other: Quat) -> Quat {
Quat {
s: self.s - other.s,
v: self.v - other.v,
}
}
}
impl Neg for Quat {
type Output = Quat;
fn neg(self) -> Quat {
Quat {
s: -self.s,
v: -self.v,
}
}
}
impl Add<f32> for Quat {
type Output = Quat;
fn add(self, scalar: f32) -> Quat {
Quat {
s: self.s + scalar,
v: self.v,
}
}
}
impl Sub<f32> for Quat {
type Output = Quat;
fn sub(self, scalar: f32) -> Quat {
Quat {
s: self.s - scalar,
v: self.v,
}
}
}
impl Mul<Quat> for f32 {
type Output = Quat;
fn mul(self, quat: Quat) -> Quat {
Quat {
s: self*quat.s,
v: self*quat.v,
}
}
}
impl Mul<Quat> for Quat {
type Output = Quat;
@ -58,3 +125,25 @@ impl Mul<Quat> for Quat {
}
}
}
impl Mul<f32> for Quat {
type Output = Quat;
fn mul(self, scalar: f32) -> Quat {
Quat {
s: self.s*scalar,
v: self.v*scalar,
}
}
}
impl Div<f32> for Quat {
type Output = Quat;
fn div(self, scalar: f32) -> Quat {
Quat {
s: self.s/scalar,
v: self.v/scalar,
}
}
}

View file

@ -12,7 +12,7 @@ static FAC: [i64; 21] = [
121645100408832000,2432902008176640000
];
static iFAC: [f32; 6] = [
static INV_FAC: [f32; 6] = [
1.0,1.0,0.5,0.1666666666666666667,0.04166666666666666667,0.00833333333333333334
];
@ -38,11 +38,11 @@ pub fn ln(x: f32) -> f32 {
}
pub fn log(x: f32) -> f32 {
ln(x)/2.30258509299
ln(x)/ std::f32::consts::LN_10
}
pub fn log2(x: f32) -> f32 {
ln(x)/0.69314718056
ln(x)/ std::f32::consts::LN_2
}
pub fn sin(x: f32) -> f32 {
@ -93,7 +93,8 @@ pub fn clamp(start: f32, end: f32, value: f32) -> f32 {
}
}
pub fn pointDerivative(func: fn(f32) -> f32, x: f32, h: f32) -> f32 {
// Getting the derivative of a function at a point with a given h vaLue for
pub fn point_derivative(func: fn(f32) -> f32, x: f32, h: f32) -> f32 {
(func(x+h) - func(x-h))/(2.0 * h)
}
@ -174,32 +175,32 @@ fn cubic_interpolation(a: f32, b: f32, t: f32) -> f32 {
/// Cubic Bézier Curve in R²
pub fn bezier2(p0: Point2, p1: Point2, p2: Point2, p3: Point2, t: f32) -> Point2 {
let tSquared = t * t;
let tCubed = tSquared * t;
let vP0 = Vec2::from_point(p0);
let vP1 = Vec2::from_point(p1);
let vP2 = Vec2::from_point(p2);
let vP3 = Vec2::from_point(p3);
let t_squared = t * t;
let t_cubed = t_squared * t;
let v_p0 = Vec2::from_point(p0);
let v_p1 = Vec2::from_point(p1);
let v_p2 = Vec2::from_point(p2);
let v_p3 = Vec2::from_point(p3);
Point2::from_vec(vP0 * (-tCubed + 3.0 * tSquared - 3.0 * t + 1.0 ) +
vP1 * (3.0 * tCubed - 6.0 * tSquared + 3.0 * t ) +
vP2 * (-3.0 * tCubed + 3.0 * tSquared ) +
vP3 * tCubed)
Point2::from_vec(v_p0 * (-t_cubed + 3.0 * t_squared - 3.0 * t + 1.0 ) +
v_p1 * (3.0 * t_cubed - 6.0 * t_squared + 3.0 * t ) +
v_p2 * (-3.0 * t_cubed + 3.0 * t_squared ) +
v_p3 * t_cubed)
}
/// Cubic Bézier Curve in R³
pub fn bezier3(p0: Point3, p1: Point3, p2: Point3, p3: Point3, t: f32) -> Point3 {
let tSquared = t * t;
let tCubed = tSquared * t;
let vP0 = Vec3::from_point(p0);
let vP1 = Vec3::from_point(p1);
let vP2 = Vec3::from_point(p2);
let vP3 = Vec3::from_point(p3);
let t_squared = t * t;
let t_cubed = t_squared * t;
let v_p0 = Vec3::from_point(p0);
let v_p1 = Vec3::from_point(p1);
let v_p2 = Vec3::from_point(p2);
let v_p3 = Vec3::from_point(p3);
Point3::from_vec(vP0 * (-tCubed + 3.0 * tSquared - 3.0 * t + 1.0 ) +
vP1 * (3.0 * tCubed - 6.0 * tSquared + 3.0 * t ) +
vP2 * (-3.0 * tCubed + 3.0 * tSquared ) +
vP3 * tCubed)
Point3::from_vec(v_p0 * (-t_cubed + 3.0 * t_squared - 3.0 * t + 1.0 ) +
v_p1 * (3.0 * t_cubed - 6.0 * t_squared + 3.0 * t ) +
v_p2 * (-3.0 * t_cubed + 3.0 * t_squared ) +
v_p3 * t_cubed)
}
// ##################################################

View file

@ -1,8 +1,7 @@
use crate::point::{Point2, Point3};
use crate::quaternion::Quat;
use crate::utilities::acos;
use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
use comet_log::*;
use std::ops::*;
pub trait InnerSpace {
fn dot(&self, other: &Self) -> f32;
@ -140,6 +139,28 @@ impl Mul<f32> for Vec2 {
}
}
impl Mul<Vec2> for f32 {
type Output = Vec2;
fn mul(self, other: Vec2) -> Vec2 {
Vec2 {
x: self * other.x,
y: self * other.y,
}
}
}
impl Div<f32> for Vec2 {
type Output = Vec2;
fn div(self, other: f32) -> Vec2 {
Vec2 {
x: self.x / other,
y: self.y / other,
}
}
}
impl Into<[f32;2]> for Vec2 {
fn into(self) -> [f32;2] {
[self.x, self.y]
@ -674,6 +695,30 @@ impl Mul<f32> for Vec3 {
}
}
impl Mul<Vec3> for f32 {
type Output = Vec3;
fn mul(self, other: Vec3) -> Vec3 {
Vec3 {
x: self * other.x,
y: self * other.y,
z: self * other.z,
}
}
}
impl Div<f32> for Vec3 {
type Output = Vec3;
fn div(self, other: f32) -> Vec3 {
Vec3 {
x: self.x / other,
y: self.y / other,
z: self.z / other,
}
}
}
impl Into<Quat> for Vec3 {
fn into(self) -> Quat {
Quat::new(0.0, self)
@ -3050,6 +3095,28 @@ impl Mul<f32> for Vec4 {
}
}
impl Mul<Vec4> for f32 {
type Output = Vec4;
fn mul(self, other: Vec4) -> Vec4 {
Vec4 {
x: self * other.x,
y: self * other.y,
z: self * other.z,
w: self * other.w,
}
}
}
impl MulAssign<f32> for Vec4 {
fn mul_assign(&mut self, other: f32) {
self.x *= other;
self.y *= other;
self.z *= other;
self.w *= other;
}
}
impl Into<[f32;4]> for Vec4 {
fn into(self) -> [f32;4] {
[self.x, self.y, self.z, self.w]