impl dot clamp distance in Rust Vector

This commit is contained in:
shenjack 2023-03-30 00:37:20 +08:00
parent 391f3df7b8
commit 7f3277fb5f
2 changed files with 55 additions and 12 deletions

View File

@ -35,8 +35,10 @@ pub mod vector {
fn floordiv(&self, other: &Self) -> Self; fn floordiv(&self, other: &Self) -> Self;
fn abs(&self) -> f64; fn abs(&self) -> f64;
fn neg(&self) -> Self; fn neg(&self) -> Self;
fn dot(&self, other: &Self) -> f64;
fn round(&self, ndigits: Option<i64>) -> Self; fn round(&self, ndigits: Option<i64>) -> 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 { impl Add for Vector2 {
@ -172,6 +174,10 @@ pub mod vector {
Self::new(-self.x, -self.y) 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<i64>) -> Self { fn round(&self, ndigits: Option<i64>) -> Self {
match ndigits { match ndigits {
Some(ndigits) => { Some(ndigits) => {
@ -185,8 +191,12 @@ pub mod vector {
} }
} }
fn radd(&self, other: &Self) -> Self { fn clamp(&self, min: f64, max: f64) -> Self {
Self::new(self.x + other.x, self.y + other.y) 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) 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<i64>) -> Self { fn round(&self, ndigits: Option<i64>) -> Self {
match ndigits { match ndigits {
Some(ndigits) => { Some(ndigits) => {
@ -226,8 +240,12 @@ pub mod vector {
} }
} }
fn radd(&self, other: &Self) -> Self { fn clamp(&self, min: f64, max: f64) -> Self {
Self::new(self.x + other.x, self.y + other.y, self.z + other.z) 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) 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<i64>) -> Self { fn round(&self, ndigits: Option<i64>) -> Self {
match ndigits { match ndigits {
Some(ndigits) => { Some(ndigits) => {
@ -274,13 +296,12 @@ pub mod vector {
} }
} }
fn radd(&self, other: &Self) -> Self { fn clamp(&self, min: f64, max: f64) -> Self {
Self::new( Self::new(self.x.clamp(min, max), self.y.clamp(min, max), self.z.clamp(min, max), self.w.clamp(min, max))
self.x + other.x, }
self.y + other.y,
self.z + other.z, fn distance(&self, other: &Self) -> f64 {
self.w + other.w, (self - other).abs()
)
} }
} }

View File

@ -13,26 +13,31 @@ pub mod python_class {
use crate::math::matrix::{Matrix3, Matrix4}; use crate::math::matrix::{Matrix3, Matrix4};
use crate::math::vector::{Vector2, Vector3, Vector4, VectorTrait}; use crate::math::vector::{Vector2, Vector3, Vector4, VectorTrait};
#[derive(Clone)]
#[pyclass(name = "Vector2_rs")] #[pyclass(name = "Vector2_rs")]
pub struct PyVector2 { pub struct PyVector2 {
pub data: Vector2, pub data: Vector2,
} }
#[derive(Clone)]
#[pyclass(name = "Vector3_rs")] #[pyclass(name = "Vector3_rs")]
pub struct PyVector3 { pub struct PyVector3 {
pub data: Vector3, pub data: Vector3,
} }
#[derive(Clone)]
#[pyclass(name = "Vector4_rs")] #[pyclass(name = "Vector4_rs")]
pub struct PyVector4 { pub struct PyVector4 {
pub data: Vector4, pub data: Vector4,
} }
#[derive(Clone)]
#[pyclass(name = "Matrix3_rs")] #[pyclass(name = "Matrix3_rs")]
pub struct PyMatrix3 { pub struct PyMatrix3 {
pub data: Matrix3, pub data: Matrix3,
} }
#[derive(Clone)]
#[pyclass(name = "Matrix4_rs")] #[pyclass(name = "Matrix4_rs")]
pub struct PyMatrix4 { pub struct PyMatrix4 {
pub data: Matrix4, pub data: Matrix4,
@ -72,6 +77,23 @@ pub mod python_class {
}; };
} }
fn __radd__(&self, other: &PyAny) -> Self {
return if other.is_instance_of::<PyVector2>() {
Self {
data: self.data + other.extract::<PyVector2>().unwrap().data,
}
} else {
// if other == 0
if other.is_none() {
self.clone()
} else {
Self {
data: self.data + other.extract::<f64>().unwrap(),
}
}
}
}
fn __sub__(&self, other: &Self) -> Self { fn __sub__(&self, other: &Self) -> Self {
return Self { return Self {
data: self.data - other.data, data: self.data - other.data,