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
//! Vector and affine spaces.

use approx::AbsDiffEq;
use decorum::Real;
use num::{NumCast, One, Zero};
use std::ops::{Add, Mul, Neg, Sub};
use typenum::consts::{U0, U1, U2, U3};
use typenum::type_operators::Cmp;
use typenum::{Greater, NonZero, Unsigned};

use crate::adjunct::{Adjunct, Fold, Pop, Push, ZipMap};
use crate::ops::{Dot, Project};
use crate::AsPosition;

/// The scalar of a `EuclideanSpace`.
pub type Scalar<S> = <Vector<S> as VectorSpace>::Scalar;

/// The vector (translation, coordinate space, etc.) of a `EuclideanSpace`.
pub type Vector<S> = <S as EuclideanSpace>::CoordinateSpace;

pub trait FiniteDimensional {
    type N: NonZero + Unsigned;

    fn dimensions() -> usize {
        Self::N::USIZE
    }
}

/// Describes the basis of a vector space.
pub trait Basis: FiniteDimensional + Sized {
    type Bases: IntoIterator<Item = Self>;

    /// Gets a type that can be converted into an iterator over the _canonical_
    /// or _standard_ basis vectors of the space.
    ///
    /// Such basis vectors must only have one component set to the
    /// multiplicative identity and all other components set to the additive
    /// identity (one and zero in $\Reals$, respectively). Moreover, the set of
    /// basis vectors must contain ordered and unique elements and be of size
    /// equal to the dimensionality of the space.
    ///
    /// For example, the set of canonical basis vectors for the real coordinate
    /// space $\Reals^3$ is:
    ///
    /// $$
    /// \\{\hat{i},\hat{j},\hat{k}\\}=
    /// \\left\\{
    /// \begin{bmatrix}1\\\0\\\0\end{bmatrix},
    /// \begin{bmatrix}0\\\1\\\0\end{bmatrix},
    /// \begin{bmatrix}0\\\0\\\1\end{bmatrix}
    /// \\right\\}
    /// $$
    fn canonical_basis() -> Self::Bases;

    fn canonical_basis_component(index: usize) -> Option<Self> {
        Self::canonical_basis().into_iter().nth(index)
    }

    /// Gets the canonical basis vector $\hat{i}$ that describes the $x$ axis.
    fn i() -> Self
    where
        Self::N: Cmp<U0, Output = Greater>,
    {
        Self::canonical_basis_component(0).unwrap()
    }

    /// Gets the canonical basis vector $\hat{j}$ that describes the $y$ axis.
    fn j() -> Self
    where
        Self::N: Cmp<U1, Output = Greater>,
    {
        Self::canonical_basis_component(1).unwrap()
    }

    /// Gets the canonical basis vector $\hat{k}$ that describes the $z$ axis.
    fn k() -> Self
    where
        Self::N: Cmp<U2, Output = Greater>,
    {
        Self::canonical_basis_component(2).unwrap()
    }
}

pub trait VectorSpace:
    Add<Output = Self>
    + Adjunct<Item = <Self as VectorSpace>::Scalar>
    + Copy
    + Fold
    + PartialEq
    + Mul<<Self as VectorSpace>::Scalar, Output = Self>
    + Neg<Output = Self>
    + Zero
    + ZipMap<<Self as VectorSpace>::Scalar, Output = Self>
{
    type Scalar: AbsDiffEq + NumCast + Real;

    fn scalar_component(&self, index: usize) -> Option<&Self::Scalar>;

    fn from_x(x: Self::Scalar) -> Self
    where
        Self: Basis + FiniteDimensional<N = U1>,
    {
        Self::i() * x
    }

    fn from_xy(x: Self::Scalar, y: Self::Scalar) -> Self
    where
        Self: Basis + FiniteDimensional<N = U2>,
    {
        (Self::i() * x) + (Self::j() * y)
    }

    fn from_xyz(x: Self::Scalar, y: Self::Scalar, z: Self::Scalar) -> Self
    where
        Self: Basis + FiniteDimensional<N = U3>,
    {
        (Self::i() * x) + (Self::j() * y) + (Self::k() * z)
    }

    fn into_x(self) -> Self::Scalar
    where
        Self: FiniteDimensional<N = U1>,
    {
        self.x()
    }

    fn into_xy(self) -> (Self::Scalar, Self::Scalar)
    where
        Self: FiniteDimensional<N = U2>,
    {
        (self.x(), self.y())
    }

    fn into_xyz(self) -> (Self::Scalar, Self::Scalar, Self::Scalar)
    where
        Self: FiniteDimensional<N = U3>,
    {
        (self.x(), self.y(), self.z())
    }

    fn x(&self) -> Self::Scalar
    where
        Self: FiniteDimensional,
        Self::N: Cmp<U0, Output = Greater>,
    {
        *self.scalar_component(0).unwrap()
    }

    fn y(&self) -> Self::Scalar
    where
        Self: FiniteDimensional,
        Self::N: Cmp<U1, Output = Greater>,
    {
        *self.scalar_component(1).unwrap()
    }

    fn z(&self) -> Self::Scalar
    where
        Self: FiniteDimensional,
        Self::N: Cmp<U2, Output = Greater>,
    {
        *self.scalar_component(2).unwrap()
    }

    fn from_homogeneous(vector: Self::ProjectiveSpace) -> Option<Self>
    where
        Self: Homogeneous,
        Self::ProjectiveSpace: Pop<Output = Self> + VectorSpace<Scalar = Self::Scalar>,
    {
        let (vector, factor) = vector.pop();
        if factor.is_zero() {
            Some(vector)
        }
        else {
            None
        }
    }

    fn into_homogeneous(self) -> Self::ProjectiveSpace
    where
        Self: Homogeneous + Push<Output = <Self as Homogeneous>::ProjectiveSpace>,
        Self::ProjectiveSpace: VectorSpace<Scalar = Self::Scalar>,
    {
        self.push(Zero::zero())
    }

    fn mean<I>(vectors: I) -> Option<Self>
    where
        I: IntoIterator<Item = Self>,
    {
        let mut vectors = vectors.into_iter();
        if let Some(mut sum) = vectors.next() {
            let mut n = 1usize;
            for vector in vectors {
                n += 1;
                sum = sum + vector;
            }
            NumCast::from(n).map(move |n| sum * (Self::Scalar::one() / n))
        }
        else {
            None
        }
    }
}

pub trait InnerSpace: Dot<Output = <Self as VectorSpace>::Scalar> + VectorSpace {
    fn normalize(self) -> Option<Self> {
        let magnitude = self.magnitude();
        if magnitude != Zero::zero() {
            Some(self * (Self::Scalar::one() / magnitude))
        }
        else {
            None
        }
    }

    fn square_magnitude(self) -> Self::Scalar {
        Dot::dot(self, self)
    }

    fn magnitude(self) -> Self::Scalar {
        Real::sqrt(self.square_magnitude())
    }
}

impl<T> Project<T> for T
where
    T: InnerSpace,
{
    type Output = T;

    fn project(self, other: T) -> Self::Output {
        let n = other.dot(self);
        let d = self.dot(self);
        self * (n / d)
    }
}

pub trait DualSpace: FiniteDimensional + VectorSpace {
    type Dual: DualSpace + FiniteDimensional<N = Self::N> + VectorSpace<Scalar = Self::Scalar>;

    fn transpose(self) -> Self::Dual;
}

pub trait Matrix: VectorSpace {
    type Row: DualSpace + FiniteDimensional + VectorSpace<Scalar = Self::Scalar>;
    type Column: DualSpace + FiniteDimensional + VectorSpace<Scalar = Self::Scalar>;
    type Transpose: Matrix<
        Scalar = Self::Scalar,
        Row = <Self::Column as DualSpace>::Dual,
        Column = <Self::Row as DualSpace>::Dual,
    >;

    fn row_count() -> usize {
        Self::Column::dimensions()
    }

    fn column_count() -> usize {
        Self::Row::dimensions()
    }

    fn scalar_component(&self, row: usize, column: usize) -> Option<&Self::Scalar> {
        <Self as VectorSpace>::scalar_component(self, row + (column * Self::row_count()))
    }

    fn row_component(&self, index: usize) -> Option<Self::Row>;

    fn column_component(&self, index: usize) -> Option<Self::Column>;

    fn transpose(self) -> Self::Transpose;
}

pub trait SquareMatrix:
    Matrix<Row = <<Self as Matrix>::Column as DualSpace>::Dual>
    + Mul<Output = Self>
    + Mul<<Self as Matrix>::Column, Output = <Self as Matrix>::Column>
where
    Self::Row: FiniteDimensional<N = <Self::Column as FiniteDimensional>::N>,
{
    fn multiplicative_identity() -> Self;
}

pub trait AffineSpace:
    Add<<Self as AffineSpace>::Translation, Output = Self>
    + Adjunct<Item = <<Self as AffineSpace>::Translation as VectorSpace>::Scalar>
    + Copy
    + Fold
    + PartialEq
    + Sub<Output = <Self as AffineSpace>::Translation>
    + ZipMap<<<Self as AffineSpace>::Translation as VectorSpace>::Scalar, Output = Self>
{
    type Translation: VectorSpace;

    fn translate(self, translation: Self::Translation) -> Self {
        self + translation
    }

    fn difference(self, other: Self) -> Self::Translation {
        self - other
    }
}

pub trait EuclideanSpace:
    AffineSpace<Translation = <Self as EuclideanSpace>::CoordinateSpace>
    + AsPosition<Position = Self>
    + FiniteDimensional
{
    type CoordinateSpace: Basis + InnerSpace + FiniteDimensional<N = <Self as FiniteDimensional>::N>;

    fn origin() -> Self;

    fn from_coordinates(coordinates: Self::CoordinateSpace) -> Self {
        Self::origin() + coordinates
    }

    fn into_coordinates(self) -> Self::CoordinateSpace {
        self - Self::origin()
    }

    fn from_x(x: Scalar<Self>) -> Self
    where
        Self: FiniteDimensional<N = U1>,
    {
        Self::from_coordinates(Vector::<Self>::from_x(x))
    }

    fn from_xy(x: Scalar<Self>, y: Scalar<Self>) -> Self
    where
        Self: FiniteDimensional<N = U2>,
    {
        Self::from_coordinates(Vector::<Self>::from_xy(x, y))
    }

    fn from_xyz(x: Scalar<Self>, y: Scalar<Self>, z: Scalar<Self>) -> Self
    where
        Self: FiniteDimensional<N = U3>,
    {
        Self::from_coordinates(Vector::<Self>::from_xyz(x, y, z))
    }

    fn into_x(self) -> Scalar<Self>
    where
        Self: FiniteDimensional<N = U1>,
    {
        self.into_coordinates().into_x()
    }

    fn into_xy(self) -> (Scalar<Self>, Scalar<Self>)
    where
        Self: FiniteDimensional<N = U2>,
    {
        self.into_coordinates().into_xy()
    }

    fn into_xyz(self) -> (Scalar<Self>, Scalar<Self>, Scalar<Self>)
    where
        Self: FiniteDimensional<N = U3>,
    {
        self.into_coordinates().into_xyz()
    }

    fn from_homogeneous(vector: Self::ProjectiveSpace) -> Option<Self>
    where
        Self: Homogeneous,
        Self::ProjectiveSpace:
            Pop<Output = Self::CoordinateSpace> + VectorSpace<Scalar = Scalar<Self>>,
    {
        let (vector, factor) = vector.pop();
        if factor.is_zero() {
            None
        }
        else {
            Some(Self::from_coordinates(vector * factor.recip()))
        }
    }

    fn into_homogeneous(self) -> Self::ProjectiveSpace
    where
        Self: Homogeneous,
        Self::CoordinateSpace: Push<Output = Self::ProjectiveSpace>,
        Self::ProjectiveSpace: VectorSpace<Scalar = Scalar<Self>>,
    {
        self.into_coordinates().push(One::one())
    }

    fn centroid<I>(points: I) -> Option<Self>
    where
        I: IntoIterator<Item = Self>,
    {
        VectorSpace::mean(points.into_iter().map(|point| point.into_coordinates()))
            .map(|mean| Self::origin() + mean)
    }
}

// TODO: Constrain the dimensionality of the projective space. This introduces
//       noisy type bounds, but ensures that the projective space has exactly
//       one additional dimension (the line at infinity).
//
//   pub trait Homogeneous: FiniteDimensional
//   where
//       Self::N: Add<U1>,
//       <Self::N as Add<U1>>::Output: NonZero + Unsigned,
//   {
//       type ProjectiveSpace: FiniteDimensional<N = <Self::N as Add<U1>>::Output>;
//   }
pub trait Homogeneous: FiniteDimensional {
    type ProjectiveSpace: FiniteDimensional;
}