From 7f3277fb5f131d5c6e98b20aa9dfe05a19bdca6d Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Thu, 30 Mar 2023 00:37:20 +0800 Subject: [PATCH] impl dot clamp distance in Rust Vector --- libs/pyglet_rs/src/src/math.rs | 45 +++++++++++++++++++++++--------- libs/pyglet_rs/src/src/pymath.rs | 22 ++++++++++++++++ 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/libs/pyglet_rs/src/src/math.rs b/libs/pyglet_rs/src/src/math.rs index a9a49f3..f121ff6 100644 --- a/libs/pyglet_rs/src/src/math.rs +++ b/libs/pyglet_rs/src/src/math.rs @@ -35,8 +35,10 @@ pub mod vector { fn floordiv(&self, other: &Self) -> Self; fn abs(&self) -> f64; fn neg(&self) -> Self; + fn dot(&self, other: &Self) -> f64; fn round(&self, ndigits: Option) -> Self; - fn radd(&self, other: &Self) -> Self; + fn clamp(&self, min: f64, max: f64) -> Self; + fn distance(&self, other: &Self) -> f64; } impl Add for Vector2 { @@ -172,6 +174,10 @@ pub mod vector { Self::new(-self.x, -self.y) } + fn dot(&self, other: &Self) -> f64 { + self.x * other.x + self.y * other.y + } + fn round(&self, ndigits: Option) -> Self { match ndigits { Some(ndigits) => { @@ -185,8 +191,12 @@ pub mod vector { } } - fn radd(&self, other: &Self) -> Self { - Self::new(self.x + other.x, self.y + other.y) + fn clamp(&self, min: f64, max: f64) -> Self { + Self::new(self.x.clamp(min, max), self.y.clamp(min, max)) + } + + fn distance(&self, other: &Self) -> f64 { + (self - other).abs() } } @@ -212,6 +222,10 @@ pub mod vector { Self::new(-self.x, -self.y, -self.z) } + fn dot(&self, other: &Self) -> f64 { + self.x * other.x + self.y * other.y + self.z * other.z + } + fn round(&self, ndigits: Option) -> Self { match ndigits { Some(ndigits) => { @@ -226,8 +240,12 @@ pub mod vector { } } - fn radd(&self, other: &Self) -> Self { - Self::new(self.x + other.x, self.y + other.y, self.z + other.z) + fn clamp(&self, min: f64, max: f64) -> Self { + Self::new(self.x.clamp(min, max), self.y.clamp(min, max), self.z.clamp(min, max)) + } + + fn distance(&self, other: &Self) -> f64 { + (self - other).abs() } } @@ -254,6 +272,10 @@ pub mod vector { Self::new(-self.x, -self.y, -self.z, -self.w) } + fn dot(&self, other: &Self) -> f64 { + self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w + } + fn round(&self, ndigits: Option) -> Self { match ndigits { Some(ndigits) => { @@ -274,13 +296,12 @@ pub mod vector { } } - fn radd(&self, other: &Self) -> Self { - Self::new( - self.x + other.x, - self.y + other.y, - self.z + other.z, - self.w + other.w, - ) + fn clamp(&self, min: f64, max: f64) -> Self { + Self::new(self.x.clamp(min, max), self.y.clamp(min, max), self.z.clamp(min, max), self.w.clamp(min, max)) + } + + fn distance(&self, other: &Self) -> f64 { + (self - other).abs() } } diff --git a/libs/pyglet_rs/src/src/pymath.rs b/libs/pyglet_rs/src/src/pymath.rs index 6bc5a0b..bb68950 100644 --- a/libs/pyglet_rs/src/src/pymath.rs +++ b/libs/pyglet_rs/src/src/pymath.rs @@ -13,26 +13,31 @@ pub mod python_class { use crate::math::matrix::{Matrix3, Matrix4}; use crate::math::vector::{Vector2, Vector3, Vector4, VectorTrait}; + #[derive(Clone)] #[pyclass(name = "Vector2_rs")] pub struct PyVector2 { pub data: Vector2, } + #[derive(Clone)] #[pyclass(name = "Vector3_rs")] pub struct PyVector3 { pub data: Vector3, } + #[derive(Clone)] #[pyclass(name = "Vector4_rs")] pub struct PyVector4 { pub data: Vector4, } + #[derive(Clone)] #[pyclass(name = "Matrix3_rs")] pub struct PyMatrix3 { pub data: Matrix3, } + #[derive(Clone)] #[pyclass(name = "Matrix4_rs")] pub struct PyMatrix4 { pub data: Matrix4, @@ -72,6 +77,23 @@ pub mod python_class { }; } + fn __radd__(&self, other: &PyAny) -> Self { + return if other.is_instance_of::() { + Self { + data: self.data + other.extract::().unwrap().data, + } + } else { + // if other == 0 + if other.is_none() { + self.clone() + } else { + Self { + data: self.data + other.extract::().unwrap(), + } + } + } + } + fn __sub__(&self, other: &Self) -> Self { return Self { data: self.data - other.data,