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