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
use num::One;
use std::fmt;
use std::cmp::Ordering;
use approx::ApproxEq;

use core::{Scalar, ColumnVector, OwnedColumnVector};
use core::iter::{MatrixIter, MatrixIterMut};
use core::dimension::{DimName, DimNameSum, DimNameAdd, U1};
use core::storage::{Storage, StorageMut, MulStorage};
use core::allocator::{Allocator, SameShapeR};

// XXX Bad name: we can't even add points…
/// The type of the result of the sum of a point with a vector.
pub type PointSum<N, D1, D2, SA> =
    PointBase<N, SameShapeR<D1, D2>,
    <<SA as Storage<N, D1, U1>>::Alloc as Allocator<N, SameShapeR<D1, D2>, U1>>::Buffer>;

/// The type of the result of the multiplication of a point by a matrix.
pub type PointMul<N, R1, C1, SA> = PointBase<N, R1, MulStorage<N, R1, C1, U1, SA>>;

/// A point with an owned storage.
pub type OwnedPoint<N, D, A> = PointBase<N, D, <A as Allocator<N, D, U1>>::Buffer>;

/// A point in a n-dimensional euclidean space.
#[repr(C)]
#[derive(Hash, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct PointBase<N: Scalar, D: DimName, S: Storage<N, D, U1>> {
    /// The coordinates of this point, i.e., the shift from the origin.
    pub coords: ColumnVector<N, D, S>
}

impl<N, D, S> Copy for PointBase<N, D, S>
    where N: Scalar,
          D: DimName,
          S: Storage<N, D, U1> + Copy { }

impl<N, D, S> Clone for PointBase<N, D, S>
    where N: Scalar,
          D: DimName,
          S: Storage<N, D, U1> + Clone {
    #[inline]
    fn clone(&self) -> Self {
        PointBase::from_coordinates(self.coords.clone())
    }
}

impl<N: Scalar, D: DimName, S: Storage<N, D, U1>> PointBase<N, D, S> {
    /// Creates a new point with the given coordinates.
    #[inline]
    pub fn from_coordinates(coords: ColumnVector<N, D, S>) -> PointBase<N, D, S> {
        PointBase {
            coords: coords
        }
    }
}

impl<N: Scalar, D: DimName, S: Storage<N, D, U1>> PointBase<N, D, S> {
    /// Moves this point into one that owns its data.
    #[inline]
    pub fn into_owned(self) -> OwnedPoint<N, D, S::Alloc> {
        PointBase::from_coordinates(self.coords.into_owned())
    }

    /// Clones this point into one that owns its data.
    #[inline]
    pub fn clone_owned(&self) -> OwnedPoint<N, D, S::Alloc> {
        PointBase::from_coordinates(self.coords.clone_owned())
    }

    /// The dimension of this point.
    #[inline]
    pub fn len(&self) -> usize {
        self.coords.len()
    }

    /// The stride of this point. This is the number of buffer element separating each component of
    /// this point.
    #[inline]
    pub fn stride(&self) -> usize {
        self.coords.strides().0
    }

    /// Iterates through this point coordinates.
    #[inline]
    pub fn iter(&self) -> MatrixIter<N, D, U1, S> {
        self.coords.iter()
    }

    /// Gets a reference to i-th element of this point without bound-checking.
    #[inline]
    pub unsafe fn get_unchecked(&self, i: usize) -> &N {
        self.coords.get_unchecked(i, 0)
    }


    /// Converts this point into a vector in homogeneous coordinates, i.e., appends a `1` at the
    /// end of it.
    #[inline]
    pub fn to_homogeneous(&self) -> OwnedColumnVector<N, DimNameSum<D, U1>, S::Alloc>
        where N: One,
              D: DimNameAdd<U1>,
              S::Alloc: Allocator<N, DimNameSum<D, U1>, U1> {

        let mut res = unsafe { OwnedColumnVector::<N, _, S::Alloc>::new_uninitialized() };
        res.fixed_slice_mut::<D, U1>(0, 0).copy_from(&self.coords);
        res[(D::dim(), 0)] = N::one();

        res
    }
}

impl<N: Scalar, D: DimName, S: StorageMut<N, D, U1>> PointBase<N, D, S> {
    /// Mutably iterates through this point coordinates.
    #[inline]
    pub fn iter_mut(&mut self) -> MatrixIterMut<N, D, U1, S> {
        self.coords.iter_mut()
    }

    /// Gets a mutable reference to i-th element of this point without bound-checking.
    #[inline]
    pub unsafe fn get_unchecked_mut(&mut self, i: usize) -> &mut N {
        self.coords.get_unchecked_mut(i, 0)
    }

    /// Swaps two entries without bound-checking.
    #[inline]
    pub unsafe fn swap_unchecked(&mut self, i1: usize, i2: usize) {
        self.coords.swap_unchecked((i1, 0), (i2, 0))
    }
}

impl<N, D: DimName, S> ApproxEq for PointBase<N, D, S>
    where N: Scalar + ApproxEq,
          S: Storage<N, D, U1>,
          N::Epsilon: Copy {
    type Epsilon = N::Epsilon;

    #[inline]
    fn default_epsilon() -> Self::Epsilon {
        N::default_epsilon()
    }

    #[inline]
    fn default_max_relative() -> Self::Epsilon {
        N::default_max_relative()
    }

    #[inline]
    fn default_max_ulps() -> u32 {
        N::default_max_ulps()
    }

    #[inline]
    fn relative_eq(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool {
        self.coords.relative_eq(&other.coords, epsilon, max_relative)
    }

    #[inline]
    fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
        self.coords.ulps_eq(&other.coords, epsilon, max_ulps)
    }
}

impl<N, D: DimName, S> Eq for PointBase<N, D, S>
    where N: Scalar + Eq,
          S: Storage<N, D, U1> { }

impl<N, D: DimName, S> PartialEq for PointBase<N, D, S>
    where N: Scalar,
          S: Storage<N, D, U1> {
    #[inline]
    fn eq(&self, right: &Self) -> bool {
        self.coords == right.coords
    }
}

impl<N, D: DimName, S> PartialOrd for PointBase<N, D, S>
    where N: Scalar + PartialOrd,
          S: Storage<N, D, U1> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.coords.partial_cmp(&other.coords)
    }

    #[inline]
    fn lt(&self, right: &Self) -> bool {
        self.coords.lt(&right.coords)
    }

    #[inline]
    fn le(&self, right: &Self) -> bool {
        self.coords.le(&right.coords)
    }

    #[inline]
    fn gt(&self, right: &Self) -> bool {
        self.coords.gt(&right.coords)
    }

    #[inline]
    fn ge(&self, right: &Self) -> bool {
        self.coords.ge(&right.coords)
    }
}

/*
 *
 * Display
 *
 */
impl<N, D: DimName, S> fmt::Display for PointBase<N, D, S>
    where N: Scalar + fmt::Display,
          S: Storage<N, D, U1> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "{{"));

        let mut it = self.coords.iter();

        try!(write!(f, "{}", *it.next().unwrap()));

        for comp in it {
            try!(write!(f, ", {}", *comp));
        }

        write!(f, "}}")
    }
}