tang 0.2.0

Math library for physical reality — geometry, spatial algebra, tensor, training, GPU compute, and 3D gaussian splatting
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
use crate::Scalar;
use core::iter::Sum;
use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vec3<S> {
    pub x: S,
    pub y: S,
    pub z: S,
}

impl<S: Scalar> Vec3<S> {
    #[inline]
    pub fn new(x: S, y: S, z: S) -> Self {
        Self { x, y, z }
    }

    #[inline]
    pub fn zero() -> Self {
        Self::new(S::ZERO, S::ZERO, S::ZERO)
    }

    /// Alias for [`zero()`](Self::zero) (nalgebra compatibility).
    #[inline]
    pub fn zeros() -> Self {
        Self::zero()
    }

    #[inline]
    pub fn splat(v: S) -> Self {
        Self::new(v, v, v)
    }

    #[inline]
    pub fn x() -> Self {
        Self::new(S::ONE, S::ZERO, S::ZERO)
    }

    #[inline]
    pub fn y() -> Self {
        Self::new(S::ZERO, S::ONE, S::ZERO)
    }

    #[inline]
    pub fn z() -> Self {
        Self::new(S::ZERO, S::ZERO, S::ONE)
    }

    /// Dot product. Accepts both owned and borrowed args (nalgebra compatibility).
    #[inline]
    pub fn dot(self, rhs: impl Into<Self>) -> S {
        let rhs = rhs.into();
        self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
    }

    /// Cross product. Accepts both owned and borrowed args (nalgebra compatibility).
    #[inline]
    pub fn cross(self, rhs: impl Into<Self>) -> Self {
        let rhs = rhs.into();
        Self::new(
            self.y * rhs.z - self.z * rhs.y,
            self.z * rhs.x - self.x * rhs.z,
            self.x * rhs.y - self.y * rhs.x,
        )
    }

    #[inline]
    pub fn norm_sq(self) -> S {
        self.dot(self)
    }

    /// Alias for [`norm_sq()`](Self::norm_sq) (nalgebra compatibility).
    #[inline]
    pub fn norm_squared(self) -> S {
        self.norm_sq()
    }

    #[inline]
    pub fn norm(self) -> S {
        self.norm_sq().sqrt()
    }

    #[inline]
    pub fn normalize(self) -> Self {
        let n = self.norm();
        self / n
    }

    #[inline]
    pub fn try_normalize(self) -> Option<Self> {
        let n = self.norm();
        if n > S::EPSILON {
            Some(self / n)
        } else {
            None
        }
    }

    #[inline]
    pub fn lerp(self, other: Self, t: S) -> Self {
        self * (S::ONE - t) + other * t
    }

    #[inline]
    pub fn component_min(self, other: Self) -> Self {
        Self::new(
            self.x.min(other.x),
            self.y.min(other.y),
            self.z.min(other.z),
        )
    }

    #[inline]
    pub fn component_max(self, other: Self) -> Self {
        Self::new(
            self.x.max(other.x),
            self.y.max(other.y),
            self.z.max(other.z),
        )
    }

    #[inline]
    pub fn abs(self) -> Self {
        Self::new(self.x.abs(), self.y.abs(), self.z.abs())
    }

    /// Returns the element-wise product (Hadamard product)
    #[inline]
    pub fn hadamard(self, other: Self) -> Self {
        Self::new(self.x * other.x, self.y * other.y, self.z * other.z)
    }

    #[inline]
    pub fn min_element(self) -> S {
        self.x.min(self.y.min(self.z))
    }

    #[inline]
    pub fn max_element(self) -> S {
        self.x.max(self.y.max(self.z))
    }

    /// Extend to Vec4 with a given w component
    #[inline]
    pub fn extend(self, w: S) -> crate::Vec4<S> {
        crate::Vec4::new(self.x, self.y, self.z, w)
    }

    /// Construct from a slice (panics if len < 3)
    #[inline]
    pub fn from_slice(s: &[S]) -> Self {
        Self::new(s[0], s[1], s[2])
    }

    #[inline]
    pub fn as_array(&self) -> [S; 3] {
        [self.x, self.y, self.z]
    }
}

impl<S: Scalar> Default for Vec3<S> {
    fn default() -> Self {
        Self::zero()
    }
}

impl<S: Scalar> From<[S; 3]> for Vec3<S> {
    fn from(a: [S; 3]) -> Self {
        Self::new(a[0], a[1], a[2])
    }
}

// nalgebra compatibility: `v.dot(&w)` and `v.cross(&w)` work via Into.
impl<S: Scalar> From<&Vec3<S>> for Vec3<S> {
    #[inline]
    fn from(v: &Vec3<S>) -> Self {
        *v
    }
}

impl<S: Scalar> From<Vec3<S>> for [S; 3] {
    fn from(v: Vec3<S>) -> Self {
        [v.x, v.y, v.z]
    }
}

impl<S: Scalar> Add for Vec3<S> {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self {
        Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
    }
}

impl<S: Scalar> Sub for Vec3<S> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self {
        Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
    }
}

impl<S: Scalar> Neg for Vec3<S> {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self {
        Self::new(-self.x, -self.y, -self.z)
    }
}

impl<S: Scalar> Mul<S> for Vec3<S> {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: S) -> Self {
        Self::new(self.x * rhs, self.y * rhs, self.z * rhs)
    }
}

impl<S: Scalar> Div<S> for Vec3<S> {
    type Output = Self;
    #[inline]
    fn div(self, rhs: S) -> Self {
        Self::new(self.x / rhs, self.y / rhs, self.z / rhs)
    }
}

impl<S: Scalar> AddAssign for Vec3<S> {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        self.x += rhs.x;
        self.y += rhs.y;
        self.z += rhs.z;
    }
}

impl<S: Scalar> SubAssign for Vec3<S> {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        self.x -= rhs.x;
        self.y -= rhs.y;
        self.z -= rhs.z;
    }
}

impl<S: Scalar> MulAssign<S> for Vec3<S> {
    #[inline]
    fn mul_assign(&mut self, rhs: S) {
        self.x *= rhs;
        self.y *= rhs;
        self.z *= rhs;
    }
}

// Scalar * Vec3 (commutative)
impl Mul<Vec3<f64>> for f64 {
    type Output = Vec3<f64>;
    #[inline]
    fn mul(self, rhs: Vec3<f64>) -> Vec3<f64> {
        rhs * self
    }
}

impl Mul<Vec3<f32>> for f32 {
    type Output = Vec3<f32>;
    #[inline]
    fn mul(self, rhs: Vec3<f32>) -> Vec3<f32> {
        rhs * self
    }
}

// Scalar * &Vec3 (nalgebra compatibility — used with Dir3::as_ref())
impl Mul<&Vec3<f64>> for f64 {
    type Output = Vec3<f64>;
    #[inline]
    fn mul(self, rhs: &Vec3<f64>) -> Vec3<f64> {
        *rhs * self
    }
}

impl Mul<&Vec3<f32>> for f32 {
    type Output = Vec3<f32>;
    #[inline]
    fn mul(self, rhs: &Vec3<f32>) -> Vec3<f32> {
        *rhs * self
    }
}

// Reference-based operators for ergonomic mixed-ref arithmetic.
impl<S: Scalar> Add for &Vec3<S> {
    type Output = Vec3<S>;
    #[inline]
    fn add(self, rhs: &Vec3<S>) -> Vec3<S> {
        *self + *rhs
    }
}

impl<S: Scalar> Sub for &Vec3<S> {
    type Output = Vec3<S>;
    #[inline]
    fn sub(self, rhs: &Vec3<S>) -> Vec3<S> {
        *self - *rhs
    }
}

impl<S: Scalar> Add<Vec3<S>> for &Vec3<S> {
    type Output = Vec3<S>;
    #[inline]
    fn add(self, rhs: Vec3<S>) -> Vec3<S> {
        *self + rhs
    }
}

impl<S: Scalar> Sub<Vec3<S>> for &Vec3<S> {
    type Output = Vec3<S>;
    #[inline]
    fn sub(self, rhs: Vec3<S>) -> Vec3<S> {
        *self - rhs
    }
}

impl<S: Scalar> Add<&Vec3<S>> for Vec3<S> {
    type Output = Vec3<S>;
    #[inline]
    fn add(self, rhs: &Vec3<S>) -> Vec3<S> {
        self + *rhs
    }
}

impl<S: Scalar> Sub<&Vec3<S>> for Vec3<S> {
    type Output = Vec3<S>;
    #[inline]
    fn sub(self, rhs: &Vec3<S>) -> Vec3<S> {
        self - *rhs
    }
}

// Sum for iterators (e.g. normals.iter().copied().sum::<Vec3>())
impl<S: Scalar> Sum for Vec3<S> {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::zero(), |a, b| a + b)
    }
}

impl<'a, S: Scalar> Sum<&'a Vec3<S>> for Vec3<S> {
    fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
        iter.fold(Self::zero(), |a, b| a + *b)
    }
}

impl<S: Scalar> core::fmt::Display for Vec3<S> {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "({}, {}, {})", self.x, self.y, self.z)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dot_product() {
        let a = Vec3::new(1.0, 2.0, 3.0);
        let b = Vec3::new(4.0, 5.0, 6.0);
        assert_eq!(a.dot(b), 32.0);
    }

    #[test]
    fn cross_product() {
        let x = Vec3::<f64>::x();
        let y = Vec3::<f64>::y();
        let z = x.cross(y);
        assert_eq!(z, Vec3::z());
        // Anti-commutative
        assert_eq!(y.cross(x), -z);
    }

    #[test]
    fn normalize() {
        let v = Vec3::new(1.0, 2.0, 2.0);
        let n = v.normalize();
        assert!((n.norm() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn scalar_mul_commutative() {
        let v = Vec3::new(1.0, 2.0, 3.0);
        assert_eq!(v * 2.0, 2.0 * v);
    }

    #[test]
    fn lerp() {
        let a = Vec3::new(0.0, 0.0, 0.0);
        let b = Vec3::new(10.0, 10.0, 10.0);
        let mid = a.lerp(b, 0.5);
        assert_eq!(mid, Vec3::new(5.0, 5.0, 5.0));
    }

    #[test]
    fn f32_vec3() {
        let v = Vec3::<f32>::new(1.0, 0.0, 0.0);
        assert_eq!(v.norm(), 1.0f32);
    }
}