use-vector 0.0.7

Small vector primitives and operations for RustUse
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

//! Const-generic vector primitives and operations for `RustUse`.

use core::num::FpCategory;
use core::ops::{Add, Div, Index, IndexMut, Mul, Neg, Sub};

/// A vector with `N` plain `f64` components.
///
/// # Examples
///
/// ```
/// use use_vector::Vector;
///
/// let vector = Vector::<3>::from_array([2.0, 3.0, 6.0]);
///
/// assert_eq!(vector.dimension(), 3);
/// assert_eq!(vector.magnitude(), 7.0);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vector<const N: usize> {
    components: [f64; N],
}

/// A two-dimensional vector.
pub type Vector2 = Vector<2>;

/// A three-dimensional vector.
pub type Vector3 = Vector<3>;

/// A four-dimensional vector.
pub type Vector4 = Vector<4>;

/// Two-dimensional vector primitives and operations.
pub mod vector2 {
    pub use crate::Vector2;
}

/// Three-dimensional vector primitives and operations.
pub mod vector3 {
    pub use crate::Vector3;
}

/// Four-dimensional vector primitives and operations.
pub mod vector4 {
    pub use crate::Vector4;
}

impl<const N: usize> Vector<N> {
    /// The zero vector.
    pub const ZERO: Self = Self {
        components: [0.0; N],
    };

    /// A vector with every component set to `1.0`.
    pub const ONE: Self = Self {
        components: [1.0; N],
    };

    /// Creates a vector from its component array.
    #[must_use]
    pub const fn from_array(components: [f64; N]) -> Self {
        Self { components }
    }

    /// Returns a shared reference to the component array.
    #[must_use]
    pub const fn as_array(&self) -> &[f64; N] {
        &self.components
    }

    /// Returns the component array.
    #[must_use]
    pub const fn into_array(self) -> [f64; N] {
        self.components
    }

    /// Returns the vector dimension.
    #[must_use]
    pub const fn dimension(self) -> usize {
        self.components.len()
    }

    /// Returns the dot product with `other`.
    ///
    /// # Examples
    ///
    /// ```
    /// use use_vector::Vector;
    ///
    /// let a = Vector::<3>::from_array([1.0, 2.0, 3.0]);
    /// let b = Vector::<3>::from_array([4.0, 5.0, 6.0]);
    ///
    /// assert_eq!(a.dot(b), 32.0);
    /// ```
    #[must_use]
    pub fn dot(self, other: Self) -> f64 {
        self.components
            .into_iter()
            .zip(other.components)
            .fold(0.0, |sum, (left, right)| left.mul_add(right, sum))
    }

    /// Returns the squared Euclidean magnitude.
    #[must_use]
    pub fn magnitude_squared(self) -> f64 {
        self.dot(self)
    }

    /// Returns the Euclidean magnitude.
    #[must_use]
    pub fn magnitude(self) -> f64 {
        self.magnitude_squared().sqrt()
    }

    /// Returns a normalized vector when the magnitude is finite and non-zero.
    ///
    /// Returns `None` for zero vectors or vectors with non-finite magnitude.
    ///
    /// # Examples
    ///
    /// ```
    /// use use_vector::Vector2;
    ///
    /// let unit = Vector2::new(3.0, 4.0)
    ///     .normalize()
    ///     .expect("non-zero finite vector should normalize");
    ///
    /// assert!((unit.x() - 0.6).abs() < 1.0e-12);
    /// assert!((unit.y() - 0.8).abs() < 1.0e-12);
    /// ```
    #[must_use]
    pub fn normalize(self) -> Option<Self> {
        let magnitude = self.magnitude();

        if !magnitude.is_finite() || matches!(magnitude.classify(), FpCategory::Zero) {
            return None;
        }

        Some(self / magnitude)
    }

    /// Returns the vector scaled by `scalar`.
    #[must_use]
    pub fn scale(self, scalar: f64) -> Self {
        self * scalar
    }

    /// Returns the Euclidean distance to `other`.
    ///
    /// # Examples
    ///
    /// ```
    /// use use_vector::Vector2;
    ///
    /// let start = Vector2::ZERO;
    /// let end = Vector2::new(3.0, 4.0);
    ///
    /// assert_eq!(start.distance(end), 5.0);
    /// ```
    #[must_use]
    pub fn distance(self, other: Self) -> f64 {
        (other - self).magnitude()
    }

    /// Returns the squared Euclidean distance to `other`.
    #[must_use]
    pub fn distance_squared(self, other: Self) -> f64 {
        (other - self).magnitude_squared()
    }

    /// Returns the linear interpolation between `self` and `other` for `t`.
    #[must_use]
    pub fn lerp(self, other: Self, t: f64) -> Self {
        self + (other - self) * t
    }

    /// Maps each component with `mapper`.
    #[must_use]
    pub fn map_components(self, mut mapper: impl FnMut(f64) -> f64) -> Self {
        let mut components = self.components;

        for component in &mut components {
            *component = mapper(*component);
        }

        Self::from_array(components)
    }

    /// Combines components from `self` and `other` with `mapper`.
    #[must_use]
    pub fn zip_components(self, other: Self, mut mapper: impl FnMut(f64, f64) -> f64) -> Self {
        let mut components = [0.0; N];

        for (index, component) in components.iter_mut().enumerate() {
            *component = mapper(self.components[index], other.components[index]);
        }

        Self::from_array(components)
    }

    /// Returns the component-wise minimum with `other`.
    #[must_use]
    pub fn component_min(self, other: Self) -> Self {
        self.zip_components(other, f64::min)
    }

    /// Returns the component-wise maximum with `other`.
    #[must_use]
    pub fn component_max(self, other: Self) -> Self {
        self.zip_components(other, f64::max)
    }

    /// Returns each component bounded by the matching `minimum` and `maximum` components.
    #[must_use]
    pub fn clamp_components(self, minimum: Self, maximum: Self) -> Self {
        self.zip_components(minimum, f64::max)
            .zip_components(maximum, f64::min)
    }

    /// Returns a vector with the absolute value of each component.
    #[must_use]
    pub fn abs(self) -> Self {
        self.map_components(f64::abs)
    }

    /// Returns `true` when every component is finite.
    #[must_use]
    pub fn is_finite(self) -> bool {
        self.components.into_iter().all(f64::is_finite)
    }

    /// Returns `true` when any component is `NaN`.
    #[must_use]
    pub fn is_nan(self) -> bool {
        self.components.into_iter().any(f64::is_nan)
    }
}

impl Vector<2> {
    /// Creates a two-dimensional vector from its components.
    #[must_use]
    pub const fn new(x: f64, y: f64) -> Self {
        Self::from_array([x, y])
    }

    /// Returns the x component.
    #[must_use]
    pub const fn x(self) -> f64 {
        self.components[0]
    }

    /// Returns the y component.
    #[must_use]
    pub const fn y(self) -> f64 {
        self.components[1]
    }
}

impl Vector<3> {
    /// Creates a three-dimensional vector from its components.
    #[must_use]
    pub const fn new(x: f64, y: f64, z: f64) -> Self {
        Self::from_array([x, y, z])
    }

    /// Returns the x component.
    #[must_use]
    pub const fn x(self) -> f64 {
        self.components[0]
    }

    /// Returns the y component.
    #[must_use]
    pub const fn y(self) -> f64 {
        self.components[1]
    }

    /// Returns the z component.
    #[must_use]
    pub const fn z(self) -> f64 {
        self.components[2]
    }

    /// Returns the cross product with `other`.
    ///
    /// # Examples
    ///
    /// ```
    /// use use_vector::Vector3;
    ///
    /// let x = Vector3::new(1.0, 0.0, 0.0);
    /// let y = Vector3::new(0.0, 1.0, 0.0);
    ///
    /// assert_eq!(x.cross(y), Vector3::new(0.0, 0.0, 1.0));
    /// ```
    #[must_use]
    pub fn cross(self, other: Self) -> Self {
        Self::new(
            mul_sub(self.y(), other.z(), self.z(), other.y()),
            mul_sub(self.z(), other.x(), self.x(), other.z()),
            mul_sub(self.x(), other.y(), self.y(), other.x()),
        )
    }
}

impl Vector<4> {
    /// Creates a four-dimensional vector from its components.
    #[must_use]
    pub const fn new(x: f64, y: f64, z: f64, w: f64) -> Self {
        Self::from_array([x, y, z, w])
    }

    /// Returns the x component.
    #[must_use]
    pub const fn x(self) -> f64 {
        self.components[0]
    }

    /// Returns the y component.
    #[must_use]
    pub const fn y(self) -> f64 {
        self.components[1]
    }

    /// Returns the z component.
    #[must_use]
    pub const fn z(self) -> f64 {
        self.components[2]
    }

    /// Returns the w component.
    #[must_use]
    pub const fn w(self) -> f64 {
        self.components[3]
    }
}

impl<const N: usize> Add for Vector<N> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        self.zip_components(rhs, |left, right| left + right)
    }
}

impl<const N: usize> Sub for Vector<N> {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        self.zip_components(rhs, |left, right| left - right)
    }
}

impl<const N: usize> Mul<f64> for Vector<N> {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self::Output {
        self.map_components(|component| component * rhs)
    }
}

impl<const N: usize> Div<f64> for Vector<N> {
    type Output = Self;

    fn div(self, rhs: f64) -> Self::Output {
        self.map_components(|component| component / rhs)
    }
}

impl<const N: usize> Neg for Vector<N> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        self.map_components(|component| -component)
    }
}

impl<const N: usize> Index<usize> for Vector<N> {
    type Output = f64;

    fn index(&self, index: usize) -> &Self::Output {
        &self.components[index]
    }
}

impl<const N: usize> IndexMut<usize> for Vector<N> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.components[index]
    }
}

impl<const N: usize> AsRef<[f64; N]> for Vector<N> {
    fn as_ref(&self) -> &[f64; N] {
        self.as_array()
    }
}

impl<const N: usize> From<[f64; N]> for Vector<N> {
    fn from(components: [f64; N]) -> Self {
        Self::from_array(components)
    }
}

impl<const N: usize> From<Vector<N>> for [f64; N] {
    fn from(vector: Vector<N>) -> Self {
        vector.into_array()
    }
}

#[inline]
fn mul_sub(a: f64, b: f64, c: f64, d: f64) -> f64 {
    a.mul_add(b, -(c * d))
}

#[cfg(test)]
mod tests {
    use super::{Vector, Vector2, Vector3, Vector4};

    fn approx_eq(left: f64, right: f64) -> bool {
        (left - right).abs() < 1.0e-12
    }

    fn assert_vector_close<const N: usize>(actual: Vector<N>, expected: [f64; N]) {
        let actual_components = actual.into_array();

        for (actual_component, expected_component) in actual_components.iter().zip(expected) {
            assert!(
                approx_eq(*actual_component, expected_component),
                "expected {expected:?}, got {actual_components:?}"
            );
        }
    }

    #[test]
    fn generic_vectors_support_component_workflows() {
        let mut vector = Vector::<3>::from_array([1.0, -2.0, 3.0]);

        assert_eq!(vector.dimension(), 3);
        assert_vector_close(Vector::from_array(*vector.as_array()), [1.0, -2.0, 3.0]);
        assert_vector_close(
            Vector::from_array(<[f64; 3]>::from(vector)),
            [1.0, -2.0, 3.0],
        );
        assert_vector_close(Vector::<3>::from([1.0, 2.0, 3.0]), [1.0, 2.0, 3.0]);
        assert!(approx_eq(vector[0], 1.0));

        vector[1] = 5.0;

        assert_vector_close(Vector::from_array(vector.into_array()), [1.0, 5.0, 3.0]);
        assert_vector_close(Vector::<4>::ZERO, [0.0, 0.0, 0.0, 0.0]);
        assert_vector_close(Vector::<4>::ONE, [1.0, 1.0, 1.0, 1.0]);
    }

    #[test]
    fn generic_vectors_support_arithmetic_and_norms() {
        let a = Vector::<3>::from_array([1.0, 2.0, 3.0]);
        let b = Vector::<3>::from_array([4.0, -2.0, 0.5]);

        assert_vector_close(a + b, [5.0, 0.0, 3.5]);
        assert_vector_close(a - b, [-3.0, 4.0, 2.5]);
        assert_vector_close(a * 2.0, [2.0, 4.0, 6.0]);
        assert_vector_close(a / 2.0, [0.5, 1.0, 1.5]);
        assert_vector_close(-a, [-1.0, -2.0, -3.0]);
        assert!(approx_eq(a.dot(b), 1.5));
        assert!(approx_eq(a.magnitude_squared(), 14.0));
        assert!(approx_eq(a.magnitude(), 14.0_f64.sqrt()));
        assert_vector_close(Vector::<3>::ZERO.lerp(a, 0.25), [0.25, 0.5, 0.75]);
        assert!(approx_eq(Vector::<3>::ZERO.distance(a), a.magnitude()));
        assert!(approx_eq(Vector::<3>::ZERO.distance_squared(a), 14.0));
    }

    #[test]
    fn generic_vectors_support_component_helpers() {
        let a = Vector::<3>::from_array([-1.0, 2.0, 5.0]);
        let b = Vector::<3>::from_array([3.0, -4.0, 4.0]);
        let minimum = Vector::<3>::from_array([0.0, 0.0, 0.0]);
        let maximum = Vector::<3>::from_array([2.0, 3.0, 4.0]);

        assert_vector_close(
            a.map_components(|component| component * 2.0),
            [-2.0, 4.0, 10.0],
        );
        assert_vector_close(
            a.zip_components(b, |left, right| left - right),
            [-4.0, 6.0, 1.0],
        );
        assert_vector_close(a.component_min(b), [-1.0, -4.0, 4.0]);
        assert_vector_close(a.component_max(b), [3.0, 2.0, 5.0]);
        assert_vector_close(a.clamp_components(minimum, maximum), [0.0, 2.0, 4.0]);
        assert_vector_close(a.abs(), [1.0, 2.0, 5.0]);
        assert!(a.is_finite());
        assert!(!Vector::<3>::from_array([f64::INFINITY, 1.0, 2.0]).is_finite());
        assert!(Vector::<3>::from_array([f64::NAN, 1.0, 2.0]).is_nan());
    }

    #[test]
    fn vector2_alias_supports_specialized_construction_and_accessors() {
        let constructed = Vector2::new(3.0, 4.0);

        assert!(approx_eq(constructed.x(), 3.0));
        assert!(approx_eq(constructed.y(), 4.0));
        assert_vector_close(constructed.scale(0.5), [1.5, 2.0]);
        assert!(approx_eq(constructed.dot(Vector2::new(-2.0, 1.0)), -2.0));
        assert!(approx_eq(constructed.magnitude(), 5.0));
        assert!(Vector2::ZERO.normalize().is_none());

        let Some(normalized) = constructed.normalize() else {
            panic!("non-zero finite vector should normalize");
        };

        assert_vector_close(normalized, [0.6, 0.8]);
    }

    #[test]
    fn vector3_alias_supports_cross_product() {
        let constructed = Vector3::new(2.0, 3.0, 6.0);

        assert!(approx_eq(constructed.x(), 2.0));
        assert!(approx_eq(constructed.y(), 3.0));
        assert!(approx_eq(constructed.z(), 6.0));
        assert_vector_close(constructed + Vector3::ONE, [3.0, 4.0, 7.0]);
        assert_vector_close(
            Vector3::new(1.0, 0.0, 0.0).cross(Vector3::new(0.0, 1.0, 0.0)),
            [0.0, 0.0, 1.0],
        );
        assert!(approx_eq(constructed.magnitude(), 7.0));
    }

    #[test]
    fn vector4_alias_supports_specialized_construction_and_accessors() {
        let constructed = Vector4::new(2.0, 4.0, 4.0, 4.0);

        assert!(approx_eq(constructed.x(), 2.0));
        assert!(approx_eq(constructed.y(), 4.0));
        assert!(approx_eq(constructed.z(), 4.0));
        assert!(approx_eq(constructed.w(), 4.0));
        assert_vector_close(constructed + Vector4::ONE, [3.0, 5.0, 5.0, 5.0]);
        assert!(approx_eq(constructed.magnitude_squared(), 52.0));
    }

    #[test]
    fn normalize_rejects_zero_and_non_finite_magnitudes() {
        assert!(Vector2::ZERO.normalize().is_none());
        assert!(Vector2::new(f64::INFINITY, 1.0).normalize().is_none());
        assert!(Vector3::new(f64::NAN, 1.0, 2.0).normalize().is_none());
        assert!(
            Vector4::new(f64::MAX, f64::MAX, f64::MAX, f64::MAX)
                .normalize()
                .is_none()
        );
    }
}