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
#![allow(clippy::many_single_char_names)]
use crate::num::DefaultFloat;
use num::{Float, One, Zero};
use std::{
  array::LengthAtMost32,
  borrow::{Borrow, BorrowMut},
  ops::{
    Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Rem, RemAssign, Sub,
    SubAssign,
  },
};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd)]
pub struct Vector<T = DefaultFloat, const N: usize>(pub [T; N])
where
  [T; N]: LengthAtMost32;
pub type Vec2<T = DefaultFloat> = Vector<T, 2>;
pub type Vec3<T = DefaultFloat> = Vector<T, 3>;
pub type Vec4<T = DefaultFloat> = Vector<T, 4>;

impl<T: Float + Zero, const N: usize> Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  /// Creates a vector of the value v (every element = v).
  pub fn of(v: T) -> Self { Vector([v; N]) }
  /// Takes the dot product of two vectors
  pub fn dot(&self, o: &Self) -> T { (0..N).fold(T::zero(), |acc, n| acc + self.0[n] * o.0[n]) }
  /// Computes the sqr_magnitude of the vector
  pub fn sqr_magn(&self) -> T { self.dot(self) }
  /// Takes the magnitude of the vector
  pub fn magn(&self) -> T { self.sqr_magn().sqrt() }
  /// Returns a unit vector in the same direction as self.
  /// Consider division instead of calling this method if you need efficiency.
  pub fn norm(&self) -> Self { *self / self.magn() }
  pub fn cos_similarity(&self, o: &Self) -> T { self.dot(o) / (self.magn() * o.magn()) }
  pub fn reflect(&self, across: &Self) -> Self {
    *self - (*across) * self.dot(across) * (T::one() + T::one())
  }
  pub fn refract(&self, norm: &Self, eta: T) -> Option<Self> {
    let cos_l = self.dot(norm);
    let discrim = T::one() - eta * eta * (T::one() - cos_l * cos_l);
    if discrim.is_sign_negative() {
      return None;
    }
    let cos_r = discrim.sqrt();
    Some(*self * eta - *norm * (eta * cos_l + cos_r))
  }
  /// Applies this function to every vector value.
  #[inline]
  pub fn apply_fn<F, S>(self, mut f: F) -> Vector<S, N>
  where
    F: FnMut(T) -> S,
    S: Float,
    [S; N]: LengthAtMost32, {
    let mut out: Vector<S, N> = Vector::zero();
    for i in 0..N {
      out[i] = f(self[i]);
    }
    out
  }
  /// Computes a vector from a list of strings.
  pub fn from_str_radix(strs: [&str; N], radix: u32) -> Result<Self, T::FromStrRadixErr> {
    let mut out = Self::zero();
    for i in 0..N {
      out[i] = T::from_str_radix(strs[i], radix)?;
    }
    Ok(out)
  }
  /// Linearly interpolates from self to v according to alpha, where 0 => self, and 1 => v.
  pub fn lerp(&self, v: &Self, alpha: T) -> Self { *self * (T::one() - alpha) + *v * alpha }
  /// Computes the max component of this vector
  pub fn max_component(&self) -> usize {
    let mut max_pos = 0;
    for i in 1..N {
      if self[i] > self[max_pos] {
        max_pos = i;
      }
    }
    max_pos
  }
  /// Computes the minimum component of this vector
  pub fn min_component(&self) -> usize {
    let mut min_pos = 0;
    for i in 1..N {
      if self[i] < self[min_pos] {
        min_pos = i;
      }
    }
    min_pos
  }
  /// Clamps self between min and max
  pub fn clamp(&mut self, min: T, max: T) {
    for i in 0..N {
      self[i] = self[i].min(max).max(min);
    }
  }
  pub fn dist(&self, o: &Self) -> T { (*self - *o).magn() }
  /// Zero-extend this vector to a larger vector.
  /// Must increase the size of the vector or keep it the same size.
  pub fn zxtend<const M: usize>(&self) -> Vector<T, M>
  where
    [T; M]: LengthAtMost32, {
    assert!(M >= N);
    let mut out: Vector<T, M> = Vector::zero();
    for i in 0..N {
      out[i] = self[i];
    }
    out
  }
  /// Shrink this vector to a lower dimension
  /// Must lower or keep the same size.
  pub fn reduce<const M: usize>(&self) -> Vector<T, M>
  where
    [T; M]: LengthAtMost32, {
    assert!(M <= N);
    let mut out: Vector<T, M> = Vector::zero();
    for i in 0..M {
      out[i] = self[i];
    }
    out
  }

  pub fn sift(&self, o: &Self) -> (Self, Self) {
    let mut min = self.clone();
    let mut max = o.clone();
    for i in 0..N {
      if self[i] > o[i] {
        max[i] = self[i];
        min[i] = o[i];
      }
    }
    (min, max)
  }
}

impl<T: Float> Vec3<T> {
  pub fn new(a: T, b: T, c: T) -> Self { Vector([a, b, c]) }
  /// Takes the cross product of self with other
  pub fn cross(&self, o: &Self) -> Self {
    let [a, b, c] = self.0;
    let [x, y, z] = o.0;
    Vector([b * z - c * y, c * x - a * z, a * y - b * z])
  }
  pub fn sided(&self, o: &Self, normal: &Self) -> bool {
    self.cross(o).dot(normal).is_sign_positive()
  }

  /// Returns the homogeneous form of this vector.
  pub fn homogeneous(&self) -> Vec4<T> {
    let &Vector([x, y, z]) = self;
    Vec4::new(x, y, z, T::one())
  }

  pub fn homogenize(&self) -> Vec2<T> {
    let &Vector([x, y, z]) = self;
    Vec2::new(x / z, y / z)
  }

  /// X component of this vector
  pub fn x(&self) -> T { self[0] }
  /// Y component of this vector
  pub fn y(&self) -> T { self[1] }
  /// Z component of this vector
  pub fn z(&self) -> T { self[2] }
}

impl<T: Float> Vec2<T> {
  pub fn new(a: T, b: T) -> Self { Vector([a, b]) }
  pub fn signed_angle(&self, dst: &Self) -> T {
    let [i, j] = self.0;
    let [x, y] = dst.0;
    (i * y - j * x).atan2(self.dot(dst))
  }
  pub fn perp(&self) -> Self {
    let [i, j] = self.0;
    Vec2::new(j, -i)
  }
  pub fn flip(&self) -> Self {
    let [i, j] = self.0;
    Vec2::new(j, i)
  }
  pub fn x(&self) -> T { self[0] }
  pub fn y(&self) -> T { self[1] }
  pub fn homogeneous(&self) -> Vec3<T> {
    let [i, j] = self.0;
    Vec3::new(i, j, T::one())
  }
}

impl<T: Float> Vec4<T> {
  pub fn new(a: T, b: T, c: T, w: T) -> Self { Vector([a, b, c, w]) }
  pub fn x(&self) -> T { self[0] }
  pub fn y(&self) -> T { self[1] }
  pub fn z(&self) -> T { self[2] }
  pub fn w(&self) -> T { self[3] }
  pub fn homogenize(&self) -> Vec3<T> {
    let &Vector([x, y, z, w]) = self;
    Vec3::new(x / w, y / w, z / w)
  }
}

impl<T: Copy, const N: usize> Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  pub fn any<F>(&self, f: F) -> bool
  where
    F: FnMut(&T) -> bool, {
    self.0.iter().any(f)
  }
  pub fn all<F>(&self, f: F) -> bool
  where
    F: FnMut(&T) -> bool, {
    self.0.iter().all(f)
  }
}

// Trait implementations for convenience

impl<T, const N: usize> AsRef<[T]> for Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  fn as_ref(&self) -> &[T] { &self.0 }
}

impl<T, const N: usize> Borrow<[T]> for Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  fn borrow(&self) -> &[T] { &self.0 }
}

impl<T, const N: usize> BorrowMut<[T]> for Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  fn borrow_mut(&mut self) -> &mut [T] { &mut self.0 }
}

// Op implementations

impl<T: Float, const N: usize> One for Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  fn one() -> Self { Vector([T::one(); N]) }
  fn is_one(&self) -> bool { self.all(|v| v.is_one()) }
}

impl<T: Float, const N: usize> Zero for Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  fn zero() -> Self { Vector([T::zero(); N]) }
  fn is_zero(&self) -> bool { self.all(|v| v.is_zero()) }
}

impl<T, const N: usize> Index<usize> for Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  type Output = T;
  fn index(&self, i: usize) -> &Self::Output { &self.0[i] }
}

impl<T, const N: usize> IndexMut<usize> for Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  fn index_mut(&mut self, i: usize) -> &mut Self::Output { &mut self.0[i] }
}

impl<T: Neg<Output = T> + Copy, const N: usize> Neg for Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  type Output = Self;
  fn neg(mut self) -> Self::Output {
    for i in 0..N {
      self.0[i] = -self.0[i];
    }
    self
  }
}

macro_rules! vec_op {
  ($t: ident, $func: ident, $op: tt) => {
    impl<T: Float, const N: usize> $t for Vector<T, N>
    where
      [T; N]: LengthAtMost32,
    {
      type Output = Self;
      fn $func(mut self, o: Self) -> Self::Output {
        for i in 0..N {
          self.0[i] = self.0[i] $op o.0[i];
        }
        self
      }
    }
  };
}

vec_op!(Add, add, +);
vec_op!(Mul, mul, *);
vec_op!(Sub, sub, -);
vec_op!(Div, div, /);
vec_op!(Rem, rem, %);

macro_rules! scalar_op {
  ($t: ident, $func: ident, $op: tt) => {
    impl<T: Float, const N: usize> $t<T> for Vector<T, N>
    where
      [T; N]: LengthAtMost32,
    {
      type Output = Self;
      fn $func(mut self, o: T) -> Self::Output {
        for i in 0..N {
          self.0[i] = self.0[i] $op o;
        }
        self
      }
    }
  };
}

scalar_op!(Add, add, +);
scalar_op!(Mul, mul, *);
scalar_op!(Sub, sub, -);
scalar_op!(Div, div, /);
scalar_op!(Rem, rem, %);

macro_rules! assign_op {
  ($t: ident, $func: ident, $op: tt) => {
    impl<T: $t + Copy, const N: usize> $t<T> for Vector<T, N>
    where
      [T; N]: LengthAtMost32,
    {
      fn $func(&mut self, o: T) {
        for i in 0..N {
          self.0[i] $op o;
        }
      }
    }
    impl<T: $t + Copy, const N: usize> $t for Vector<T, N>
    where
      [T; N]: LengthAtMost32,
    {
      fn $func(&mut self, o: Self) {
        for i in 0..N {
          self.0[i] $op o.0[i];
        }
      }
    }
  };
}

assign_op!(AddAssign, add_assign, +=);
assign_op!(SubAssign, sub_assign, -=);
assign_op!(MulAssign, mul_assign, *=);
assign_op!(DivAssign, div_assign, /=);
assign_op!(RemAssign, rem_assign, %=);

macro_rules! elemwise_impl {
  ($func: ident, $call: path, $name: expr) => {
    #[doc="Element-wise "]
    #[doc=$name]
    #[doc="."]
    pub fn $func(&self) -> Self { self.apply_fn($call) }
  };
  ($func: ident, $call: path) => {
    elemwise_impl!($func, $call, stringify!($func));
  };
}

macro_rules! curried_elemwise_impl {
  ($func: ident, $call: path, $name: expr) => {
    #[doc="Element-wise "]
    #[doc=$name]
    #[doc="."]
    pub fn $func(&self, v: T) -> Self { self.apply_fn(|u| $call(u, v)) }
  };
  ($func: ident, $call: path) => {
    curried_elemwise_impl!($func, $call, stringify!($func));
  };
}
impl<T: Float, const N: usize> Vector<T, N>
where
  [T; N]: LengthAtMost32,
{
  // Trigonometric stuff
  elemwise_impl!(cos, T::cos);
  elemwise_impl!(sin, T::sin);
  elemwise_impl!(tan, T::tan);

  elemwise_impl!(acos, T::acos);
  elemwise_impl!(asin, T::asin);
  elemwise_impl!(atan, T::atan);

  elemwise_impl!(acosh, T::acosh);
  elemwise_impl!(asinh, T::asinh);
  elemwise_impl!(atanh, T::atanh);
  curried_elemwise_impl!(atan2, T::atan2);
  curried_elemwise_impl!(hypot, T::hypot);

  // Rounding stuff
  elemwise_impl!(ceil, T::ceil);
  elemwise_impl!(floor, T::floor);
  elemwise_impl!(round, T::round);

  // Decomposition stuff
  elemwise_impl!(fract, T::fract);
  elemwise_impl!(trunc, T::trunc);

  // Sign value stuff
  elemwise_impl!(abs, T::abs);
  curried_elemwise_impl!(abs_sub, T::abs_sub);
  elemwise_impl!(signum, T::signum);

  // Reciprocal
  elemwise_impl!(recip, T::recip);

  // Logarithmic stuff
  elemwise_impl!(log2, T::log2);
  elemwise_impl!(log10, T::log10);
  elemwise_impl!(ln, T::ln);
  elemwise_impl!(ln_1p, T::ln_1p);
  elemwise_impl!(exp, T::exp);
  elemwise_impl!(exp2, T::exp2);
  elemwise_impl!(exp_m1, T::exp_m1);
  elemwise_impl!(sqrt, T::sqrt);
  elemwise_impl!(cbrt, T::cbrt);
  curried_elemwise_impl!(powf, T::powf);
  pub fn powi(&self, v: i32) -> Self { self.apply_fn(|u| u.powi(v)) }
  curried_elemwise_impl!(log, T::log);

  // Min/max stuff
  curried_elemwise_impl!(max, T::max);
  curried_elemwise_impl!(min, T::min);

  // Degree related stuff
  elemwise_impl!(to_degrees, T::to_degrees);
  elemwise_impl!(to_radians, T::to_radians);
}

#[test]
fn example() {
  let a = Vec3::of(0.0);
  let b = a + 1.0;
  let c = b.sin();
  let _dot = b.dot(&c);

  let x = c.x();
  let y = c.y();
  let z = c.z();

  let Vector([i, j, k]) = c;
  assert_eq!(x, i);
  assert_eq!(y, j);
  assert_eq!(z, k);
}