Skip to main content

rill_core/math/vector/
traits.rs

1use core::fmt;
2use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
3
4use crate::Scalar;
5use crate::Transcendental;
6
7/// Основной трейт для векторных типов (базовые операции).
8///
9/// Параметризован типом элемента `T: Scalar` и шириной `N`.
10pub trait Vector<T: Scalar, const N: usize>:
11    Copy
12    + Clone
13    + Send
14    + Sync
15    + 'static
16    + Default
17    + PartialEq
18    + fmt::Debug
19    + Add<Output = Self>
20    + Sub<Output = Self>
21    + Mul<Output = Self>
22    + Div<Output = Self>
23    + Rem<Output = Self>
24    + Neg<Output = Self>
25{
26    fn splat(value: T) -> Self;
27    fn load(slice: &[T]) -> Self;
28    fn store(&self, slice: &mut [T]);
29    fn extract(&self, index: usize) -> T;
30    fn insert(&self, index: usize, value: T) -> Self;
31
32    fn add(&self, other: &Self) -> Self;
33    fn sub(&self, other: &Self) -> Self;
34    fn mul(&self, other: &Self) -> Self;
35    fn div(&self, other: &Self) -> Self;
36    fn rem(&self, other: &Self) -> Self;
37    fn neg(&self) -> Self;
38
39    fn abs(&self) -> Self;
40    fn min(&self, other: &Self) -> Self;
41    fn max(&self, other: &Self) -> Self;
42    fn clamp(&self, min: &Self, max: &Self) -> Self;
43}
44
45/// Трейт для векторных типов с трансцендентными операциями.
46///
47/// Доступен только для `T: Transcendental` (f32, f64).
48pub trait VectorTranscendental<T: Transcendental, const N: usize>: Vector<T, N> {
49    fn sqrt(&self) -> Self;
50    fn exp(&self) -> Self;
51    fn ln(&self) -> Self;
52    fn sin(&self) -> Self;
53    fn cos(&self) -> Self;
54    fn tan(&self) -> Self;
55}
56
57pub trait VectorScalarOps<T: Scalar, const N: usize> {
58    fn add_scalar(&self, scalar: T) -> Self;
59    fn sub_scalar(&self, scalar: T) -> Self;
60    fn mul_scalar(&self, scalar: T) -> Self;
61    fn div_scalar(&self, scalar: T) -> Self;
62    fn rem_scalar(&self, scalar: T) -> Self;
63}
64
65pub trait VectorReduce<T: Scalar, const N: usize> {
66    fn horizontal_sum(&self) -> T;
67    fn horizontal_product(&self) -> T;
68    fn horizontal_min(&self) -> T;
69    fn horizontal_max(&self) -> T;
70    fn horizontal_mean(&self) -> T;
71}
72
73pub trait VectorMask<T: Scalar, const N: usize> {
74    type Mask;
75
76    fn eq(&self, other: &Self) -> Self::Mask;
77    fn ne(&self, other: &Self) -> Self::Mask;
78    fn gt(&self, other: &Self) -> Self::Mask;
79    fn ge(&self, other: &Self) -> Self::Mask;
80    fn lt(&self, other: &Self) -> Self::Mask;
81    fn le(&self, other: &Self) -> Self::Mask;
82    fn select(&self, other: &Self, mask: Self::Mask) -> Self;
83    fn all(mask: &Self::Mask) -> bool;
84}