Skip to main content

rill_core/math/vector/
scalar.rs

1//! # Скалярные реализации векторных операций
2//!
3//! Fallback реализации для платформ без SIMD поддержки или для отладки.
4
5use super::traits::*;
6use crate::{Scalar, Transcendental};
7
8// -----------------------------------------------------------------------------
9// Скалярные векторные типы
10// -----------------------------------------------------------------------------
11
12/// Скалярный вектор из 1 элемента
13#[derive(Copy, Clone, Debug, PartialEq)]
14pub struct ScalarVector1<T: Scalar>([T; 1]);
15
16/// Скалярный вектор из 2 элементов
17#[derive(Copy, Clone, Debug, PartialEq)]
18pub struct ScalarVector2<T: Scalar>([T; 2]);
19
20/// Скалярный вектор из 4 элементов
21#[derive(Copy, Clone, Debug, PartialEq)]
22pub struct ScalarVector4<T: Scalar>([T; 4]);
23
24/// Скалярный вектор из 8 элементов
25#[derive(Copy, Clone, Debug, PartialEq)]
26pub struct ScalarVector8<T: Scalar>([T; 8]);
27
28// -----------------------------------------------------------------------------
29// Реализация Vector для ScalarVector4
30// -----------------------------------------------------------------------------
31
32impl<T: Scalar> Vector<T, 4> for ScalarVector4<T> {
33    fn splat(value: T) -> Self {
34        ScalarVector4([value; 4])
35    }
36
37    fn load(slice: &[T]) -> Self {
38        let mut arr = [T::ZERO; 4];
39        arr.copy_from_slice(&slice[0..4]);
40        ScalarVector4(arr)
41    }
42
43    fn store(&self, slice: &mut [T]) {
44        slice[0..4].copy_from_slice(&self.0);
45    }
46
47    fn extract(&self, index: usize) -> T {
48        self.0[index]
49    }
50
51    fn insert(&self, index: usize, value: T) -> Self {
52        let mut arr = self.0;
53        arr[index] = value;
54        ScalarVector4(arr)
55    }
56
57    fn add(&self, other: &Self) -> Self {
58        ScalarVector4(core::array::from_fn(|i| self.0[i] + other.0[i]))
59    }
60
61    fn sub(&self, other: &Self) -> Self {
62        ScalarVector4(core::array::from_fn(|i| self.0[i] - other.0[i]))
63    }
64
65    fn mul(&self, other: &Self) -> Self {
66        ScalarVector4(core::array::from_fn(|i| self.0[i] * other.0[i]))
67    }
68
69    fn div(&self, other: &Self) -> Self {
70        ScalarVector4(core::array::from_fn(|i| self.0[i] / other.0[i]))
71    }
72
73    fn rem(&self, other: &Self) -> Self {
74        ScalarVector4(core::array::from_fn(|i| self.0[i] % other.0[i]))
75    }
76
77    fn neg(&self) -> Self {
78        ScalarVector4(core::array::from_fn(|i| -self.0[i]))
79    }
80
81    fn abs(&self) -> Self {
82        ScalarVector4(core::array::from_fn(|i| self.0[i].abs()))
83    }
84
85    fn min(&self, other: &Self) -> Self {
86        ScalarVector4(core::array::from_fn(|i| self.0[i].min(other.0[i])))
87    }
88
89    fn max(&self, other: &Self) -> Self {
90        ScalarVector4(core::array::from_fn(|i| self.0[i].max(other.0[i])))
91    }
92
93    fn clamp(&self, min: &Self, max: &Self) -> Self {
94        ScalarVector4(core::array::from_fn(|i| {
95            self.0[i].clamp(min.0[i], max.0[i])
96        }))
97    }
98}
99
100impl<T: Transcendental> VectorTranscendental<T, 4> for ScalarVector4<T> {
101    fn sqrt(&self) -> Self {
102        ScalarVector4(core::array::from_fn(|i| self.0[i].sqrt()))
103    }
104    fn exp(&self) -> Self {
105        ScalarVector4(core::array::from_fn(|i| self.0[i].exp()))
106    }
107    fn ln(&self) -> Self {
108        ScalarVector4(core::array::from_fn(|i| self.0[i].ln()))
109    }
110    fn sin(&self) -> Self {
111        ScalarVector4(core::array::from_fn(|i| self.0[i].sin()))
112    }
113    fn cos(&self) -> Self {
114        ScalarVector4(core::array::from_fn(|i| self.0[i].cos()))
115    }
116    fn tan(&self) -> Self {
117        ScalarVector4(core::array::from_fn(|i| self.0[i].tan()))
118    }
119}
120
121impl<T: Scalar> Default for ScalarVector4<T> {
122    fn default() -> Self {
123        ScalarVector4([T::ZERO; 4])
124    }
125}
126
127impl<T: Scalar> Vector<T, 1> for ScalarVector1<T> {
128    fn splat(value: T) -> Self {
129        ScalarVector1([value; 1])
130    }
131
132    fn load(slice: &[T]) -> Self {
133        let mut arr = [T::ZERO; 1];
134        arr.copy_from_slice(&slice[0..1]);
135        ScalarVector1(arr)
136    }
137
138    fn store(&self, slice: &mut [T]) {
139        slice[0..1].copy_from_slice(&self.0);
140    }
141
142    fn extract(&self, index: usize) -> T {
143        self.0[index]
144    }
145
146    fn insert(&self, index: usize, value: T) -> Self {
147        let mut arr = self.0;
148        arr[index] = value;
149        ScalarVector1(arr)
150    }
151
152    fn add(&self, other: &Self) -> Self {
153        let mut arr = [T::ZERO; 1];
154        arr[0] = self.0[0] + other.0[0];
155        ScalarVector1(arr)
156    }
157
158    fn sub(&self, other: &Self) -> Self {
159        let mut arr = [T::ZERO; 1];
160        arr[0] = self.0[0] - other.0[0];
161        ScalarVector1(arr)
162    }
163
164    fn mul(&self, other: &Self) -> Self {
165        let mut arr = [T::ZERO; 1];
166        arr[0] = self.0[0] * other.0[0];
167        ScalarVector1(arr)
168    }
169
170    fn div(&self, other: &Self) -> Self {
171        let mut arr = [T::ZERO; 1];
172        arr[0] = self.0[0] / other.0[0];
173        ScalarVector1(arr)
174    }
175
176    fn rem(&self, other: &Self) -> Self {
177        let mut arr = [T::ZERO; 1];
178        arr[0] = self.0[0] % other.0[0];
179        ScalarVector1(arr)
180    }
181
182    fn neg(&self) -> Self {
183        let mut arr = [T::ZERO; 1];
184        arr[0] = -self.0[0];
185        ScalarVector1(arr)
186    }
187
188    fn abs(&self) -> Self {
189        let mut arr = [T::ZERO; 1];
190        arr[0] = self.0[0].abs();
191        ScalarVector1(arr)
192    }
193
194    fn min(&self, other: &Self) -> Self {
195        let mut arr = [T::ZERO; 1];
196        arr[0] = self.0[0].min(other.0[0]);
197        ScalarVector1(arr)
198    }
199
200    fn max(&self, other: &Self) -> Self {
201        let mut arr = [T::ZERO; 1];
202        arr[0] = self.0[0].max(other.0[0]);
203        ScalarVector1(arr)
204    }
205
206    fn clamp(&self, min: &Self, max: &Self) -> Self {
207        let mut arr = [T::ZERO; 1];
208        arr[0] = self.0[0].clamp(min.0[0], max.0[0]);
209        ScalarVector1(arr)
210    }
211}
212
213impl<T: Transcendental> VectorTranscendental<T, 1> for ScalarVector1<T> {
214    fn sqrt(&self) -> Self {
215        let mut a = [T::ZERO; 1];
216        a[0] = self.0[0].sqrt();
217        ScalarVector1(a)
218    }
219    fn exp(&self) -> Self {
220        let mut a = [T::ZERO; 1];
221        a[0] = self.0[0].exp();
222        ScalarVector1(a)
223    }
224    fn ln(&self) -> Self {
225        let mut a = [T::ZERO; 1];
226        a[0] = self.0[0].ln();
227        ScalarVector1(a)
228    }
229    fn sin(&self) -> Self {
230        let mut a = [T::ZERO; 1];
231        a[0] = self.0[0].sin();
232        ScalarVector1(a)
233    }
234    fn cos(&self) -> Self {
235        let mut a = [T::ZERO; 1];
236        a[0] = self.0[0].cos();
237        ScalarVector1(a)
238    }
239    fn tan(&self) -> Self {
240        let mut a = [T::ZERO; 1];
241        a[0] = self.0[0].tan();
242        ScalarVector1(a)
243    }
244}
245
246impl<T: Scalar> Default for ScalarVector1<T> {
247    fn default() -> Self {
248        ScalarVector1([T::ZERO; 1])
249    }
250}
251
252// Реализации для ScalarVector2 и ScalarVector8 аналогичны (опущены для краткости)
253
254impl<T: Scalar> Vector<T, 2> for ScalarVector2<T> {
255    fn splat(value: T) -> Self {
256        ScalarVector2([value; 2])
257    }
258
259    fn load(slice: &[T]) -> Self {
260        let mut arr = [T::ZERO; 2];
261        arr.copy_from_slice(&slice[0..2]);
262        ScalarVector2(arr)
263    }
264
265    fn store(&self, slice: &mut [T]) {
266        slice[0..2].copy_from_slice(&self.0);
267    }
268
269    fn extract(&self, index: usize) -> T {
270        self.0[index]
271    }
272
273    fn insert(&self, index: usize, value: T) -> Self {
274        let mut arr = self.0;
275        arr[index] = value;
276        ScalarVector2(arr)
277    }
278
279    fn add(&self, other: &Self) -> Self {
280        ScalarVector2(core::array::from_fn(|i| self.0[i] + other.0[i]))
281    }
282
283    fn sub(&self, other: &Self) -> Self {
284        ScalarVector2(core::array::from_fn(|i| self.0[i] - other.0[i]))
285    }
286
287    fn mul(&self, other: &Self) -> Self {
288        ScalarVector2(core::array::from_fn(|i| self.0[i] * other.0[i]))
289    }
290
291    fn div(&self, other: &Self) -> Self {
292        ScalarVector2(core::array::from_fn(|i| self.0[i] / other.0[i]))
293    }
294
295    fn rem(&self, other: &Self) -> Self {
296        ScalarVector2(core::array::from_fn(|i| self.0[i] % other.0[i]))
297    }
298
299    fn neg(&self) -> Self {
300        ScalarVector2(core::array::from_fn(|i| -self.0[i]))
301    }
302
303    fn abs(&self) -> Self {
304        ScalarVector2(core::array::from_fn(|i| self.0[i].abs()))
305    }
306
307    fn min(&self, other: &Self) -> Self {
308        ScalarVector2(core::array::from_fn(|i| self.0[i].min(other.0[i])))
309    }
310
311    fn max(&self, other: &Self) -> Self {
312        ScalarVector2(core::array::from_fn(|i| self.0[i].max(other.0[i])))
313    }
314
315    fn clamp(&self, min: &Self, max: &Self) -> Self {
316        ScalarVector2(core::array::from_fn(|i| {
317            self.0[i].clamp(min.0[i], max.0[i])
318        }))
319    }
320}
321
322impl<T: Transcendental> VectorTranscendental<T, 2> for ScalarVector2<T> {
323    fn sqrt(&self) -> Self {
324        ScalarVector2(core::array::from_fn(|i| self.0[i].sqrt()))
325    }
326    fn exp(&self) -> Self {
327        ScalarVector2(core::array::from_fn(|i| self.0[i].exp()))
328    }
329    fn ln(&self) -> Self {
330        ScalarVector2(core::array::from_fn(|i| self.0[i].ln()))
331    }
332    fn sin(&self) -> Self {
333        ScalarVector2(core::array::from_fn(|i| self.0[i].sin()))
334    }
335    fn cos(&self) -> Self {
336        ScalarVector2(core::array::from_fn(|i| self.0[i].cos()))
337    }
338    fn tan(&self) -> Self {
339        ScalarVector2(core::array::from_fn(|i| self.0[i].tan()))
340    }
341}
342
343impl<T: Scalar> Default for ScalarVector2<T> {
344    fn default() -> Self {
345        ScalarVector2([T::ZERO; 2])
346    }
347}
348
349// -----------------------------------------------------------------------------
350// Реализация операторов
351// -----------------------------------------------------------------------------
352
353use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
354
355impl<T: Scalar> Add for ScalarVector4<T> {
356    type Output = Self;
357
358    fn add(self, rhs: Self) -> Self {
359        ScalarVector4(core::array::from_fn(|i| self.0[i] + rhs.0[i]))
360    }
361}
362
363impl<T: Scalar> Sub for ScalarVector4<T> {
364    type Output = Self;
365
366    fn sub(self, rhs: Self) -> Self {
367        ScalarVector4(core::array::from_fn(|i| self.0[i] - rhs.0[i]))
368    }
369}
370
371impl<T: Scalar> Mul for ScalarVector4<T> {
372    type Output = Self;
373
374    fn mul(self, rhs: Self) -> Self {
375        ScalarVector4(core::array::from_fn(|i| self.0[i] * rhs.0[i]))
376    }
377}
378
379impl<T: Scalar> Div for ScalarVector4<T> {
380    type Output = Self;
381
382    fn div(self, rhs: Self) -> Self {
383        ScalarVector4(core::array::from_fn(|i| self.0[i] / rhs.0[i]))
384    }
385}
386
387impl<T: Scalar> Rem for ScalarVector4<T> {
388    type Output = Self;
389
390    fn rem(self, rhs: Self) -> Self {
391        ScalarVector4(core::array::from_fn(|i| self.0[i] % rhs.0[i]))
392    }
393}
394
395impl<T: Scalar> Neg for ScalarVector4<T> {
396    type Output = Self;
397
398    fn neg(self) -> Self {
399        ScalarVector4(core::array::from_fn(|i| -self.0[i]))
400    }
401}
402
403impl<T: Scalar> Add for ScalarVector2<T> {
404    type Output = Self;
405
406    fn add(self, rhs: Self) -> Self {
407        ScalarVector2(core::array::from_fn(|i| self.0[i] + rhs.0[i]))
408    }
409}
410
411impl<T: Scalar> Sub for ScalarVector2<T> {
412    type Output = Self;
413
414    fn sub(self, rhs: Self) -> Self {
415        ScalarVector2(core::array::from_fn(|i| self.0[i] - rhs.0[i]))
416    }
417}
418
419impl<T: Scalar> Mul for ScalarVector2<T> {
420    type Output = Self;
421
422    fn mul(self, rhs: Self) -> Self {
423        ScalarVector2(core::array::from_fn(|i| self.0[i] * rhs.0[i]))
424    }
425}
426
427impl<T: Scalar> Div for ScalarVector2<T> {
428    type Output = Self;
429
430    fn div(self, rhs: Self) -> Self {
431        ScalarVector2(core::array::from_fn(|i| self.0[i] / rhs.0[i]))
432    }
433}
434
435impl<T: Scalar> Rem for ScalarVector2<T> {
436    type Output = Self;
437
438    fn rem(self, rhs: Self) -> Self {
439        ScalarVector2(core::array::from_fn(|i| self.0[i] % rhs.0[i]))
440    }
441}
442
443impl<T: Scalar> Neg for ScalarVector2<T> {
444    type Output = Self;
445
446    fn neg(self) -> Self {
447        ScalarVector2(core::array::from_fn(|i| -self.0[i]))
448    }
449}
450
451impl<T: Scalar> Add for ScalarVector1<T> {
452    type Output = Self;
453
454    fn add(self, rhs: Self) -> Self {
455        let mut arr = [T::ZERO; 1];
456        arr[0] = self.0[0] + rhs.0[0];
457        ScalarVector1(arr)
458    }
459}
460
461impl<T: Scalar> Sub for ScalarVector1<T> {
462    type Output = Self;
463
464    fn sub(self, rhs: Self) -> Self {
465        let mut arr = [T::ZERO; 1];
466        arr[0] = self.0[0] - rhs.0[0];
467        ScalarVector1(arr)
468    }
469}
470
471impl<T: Scalar> Mul for ScalarVector1<T> {
472    type Output = Self;
473
474    fn mul(self, rhs: Self) -> Self {
475        let mut arr = [T::ZERO; 1];
476        arr[0] = self.0[0] * rhs.0[0];
477        ScalarVector1(arr)
478    }
479}
480
481impl<T: Scalar> Div for ScalarVector1<T> {
482    type Output = Self;
483
484    fn div(self, rhs: Self) -> Self {
485        let mut arr = [T::ZERO; 1];
486        arr[0] = self.0[0] / rhs.0[0];
487        ScalarVector1(arr)
488    }
489}
490
491impl<T: Scalar> Rem for ScalarVector1<T> {
492    type Output = Self;
493
494    fn rem(self, rhs: Self) -> Self {
495        let mut arr = [T::ZERO; 1];
496        arr[0] = self.0[0] % rhs.0[0];
497        ScalarVector1(arr)
498    }
499}
500
501impl<T: Scalar> Neg for ScalarVector1<T> {
502    type Output = Self;
503
504    fn neg(self) -> Self {
505        let mut arr = [T::ZERO; 1];
506        arr[0] = -self.0[0];
507        ScalarVector1(arr)
508    }
509}
510
511// -----------------------------------------------------------------------------
512// Операции со скалярами
513// -----------------------------------------------------------------------------
514
515impl<T: Scalar> Mul<T> for ScalarVector4<T> {
516    type Output = Self;
517
518    fn mul(self, rhs: T) -> Self {
519        self * Self::splat(rhs)
520    }
521}
522
523impl<T: Scalar> Div<T> for ScalarVector4<T> {
524    type Output = Self;
525
526    fn div(self, rhs: T) -> Self {
527        self / Self::splat(rhs)
528    }
529}
530
531impl<T: Scalar> Add<T> for ScalarVector4<T> {
532    type Output = Self;
533
534    fn add(self, rhs: T) -> Self {
535        self + Self::splat(rhs)
536    }
537}
538
539impl<T: Scalar> Sub<T> for ScalarVector4<T> {
540    type Output = Self;
541
542    fn sub(self, rhs: T) -> Self {
543        self - Self::splat(rhs)
544    }
545}
546
547impl<T: Scalar> Rem<T> for ScalarVector4<T> {
548    type Output = Self;
549
550    fn rem(self, rhs: T) -> Self {
551        self % Self::splat(rhs)
552    }
553}
554
555impl<T: Scalar> Mul<T> for ScalarVector2<T> {
556    type Output = Self;
557
558    fn mul(self, rhs: T) -> Self {
559        self * Self::splat(rhs)
560    }
561}
562
563impl<T: Scalar> Div<T> for ScalarVector2<T> {
564    type Output = Self;
565
566    fn div(self, rhs: T) -> Self {
567        self / Self::splat(rhs)
568    }
569}
570
571impl<T: Scalar> Add<T> for ScalarVector2<T> {
572    type Output = Self;
573
574    fn add(self, rhs: T) -> Self {
575        self + Self::splat(rhs)
576    }
577}
578
579impl<T: Scalar> Sub<T> for ScalarVector2<T> {
580    type Output = Self;
581
582    fn sub(self, rhs: T) -> Self {
583        self - Self::splat(rhs)
584    }
585}
586
587impl<T: Scalar> Rem<T> for ScalarVector2<T> {
588    type Output = Self;
589
590    fn rem(self, rhs: T) -> Self {
591        self % Self::splat(rhs)
592    }
593}
594
595impl<T: Scalar> Mul<T> for ScalarVector1<T> {
596    type Output = Self;
597
598    fn mul(self, rhs: T) -> Self {
599        self * Self::splat(rhs)
600    }
601}
602
603impl<T: Scalar> Div<T> for ScalarVector1<T> {
604    type Output = Self;
605
606    fn div(self, rhs: T) -> Self {
607        self / Self::splat(rhs)
608    }
609}
610
611impl<T: Scalar> Add<T> for ScalarVector1<T> {
612    type Output = Self;
613
614    fn add(self, rhs: T) -> Self {
615        self + Self::splat(rhs)
616    }
617}
618
619impl<T: Scalar> Sub<T> for ScalarVector1<T> {
620    type Output = Self;
621
622    fn sub(self, rhs: T) -> Self {
623        self - Self::splat(rhs)
624    }
625}
626
627impl<T: Scalar> Rem<T> for ScalarVector1<T> {
628    type Output = Self;
629
630    fn rem(self, rhs: T) -> Self {
631        self % Self::splat(rhs)
632    }
633}
634
635// -----------------------------------------------------------------------------
636// Тесты
637// -----------------------------------------------------------------------------
638
639#[cfg(test)]
640mod tests {
641    use super::*;
642
643    #[test]
644    fn test_scalar_vector4_basic() {
645        let a = ScalarVector4::<f32>::splat(2.0);
646        let b = ScalarVector4::<f32>::splat(3.0);
647        let c = a + b;
648        assert_eq!(c.extract(0), 5.0);
649        assert_eq!(c.extract(3), 5.0);
650    }
651
652    #[test]
653    fn test_scalar_vector4_math() {
654        let a = ScalarVector4::<f32>::splat(0.0);
655        let b = a.sin();
656        assert_eq!(b.extract(0), 0.0);
657
658        let c = ScalarVector4::<f32>::splat(1.0);
659        let d = c.sqrt();
660        assert_eq!(d.extract(0), 1.0);
661    }
662
663    #[test]
664    fn test_scalar_vector2() {
665        let a = ScalarVector2::<f64>::splat(5.0);
666        let b = ScalarVector2::<f64>::splat(2.0);
667        let c = a * b;
668        assert_eq!(c.extract(0), 10.0);
669        assert_eq!(c.extract(1), 10.0);
670    }
671}