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
//!
//! # Describing coordinates
//!
//! The `Coordinate` trait provides a way to represent coordinates in arbitary numbers of dimensions. Most of the
//! types in `flo_curves` support arbitrary coordinate types through this trait.
//! 
//! `Coordinate2D` coordinates are a special case of coordinates with only two dimensions. Some operations are
//! only defined for two dimensions: for example, taking the normal of a Bezier curve. The `Coord2` type is
//! supplied as a generic implementation of a 2-dimensional coordinate, though these operations will work on
//! any type for which the `Coordinate2D` trait is defined.
//!

use smallvec::*;

use std::ops::*;

///
/// Represents a value that can be used as a coordinate in a bezier curve
/// 
pub trait Coordinate : Sized+Copy+Add<Self, Output=Self>+Mul<f64, Output=Self>+Sub<Self, Output=Self>+PartialEq {
    ///
    /// Creates a new coordinate from the specified set of components
    /// 
    fn from_components(components: &[f64]) -> Self;

    ///
    /// Returns the origin coordinate
    /// 
    fn origin() -> Self;

    ///
    /// The number of components in this coordinate
    /// 
    fn len() -> usize;

    ///
    /// Retrieves the component at the specified index
    /// 
    fn get(&self, index: usize) -> f64;

    ///
    /// Returns a point made up of the biggest components of the two points
    /// 
    fn from_biggest_components(p1: Self, p2: Self) -> Self;

    ///
    /// Returns a point made up of the smallest components of the two points
    /// 
    fn from_smallest_components(p1: Self, p2: Self) -> Self;

    ///
    /// Computes the distance between this coordinate and another of the same type
    /// 
    #[inline]
    fn distance_to(&self, target: &Self) -> f64 {
        let offset              = *self - *target;
        let squared_distance    = offset.dot(&offset);

        f64::sqrt(squared_distance)
    }

    ///
    /// Computes the dot product for this vector along with another vector
    /// 
    #[inline]
    fn dot(&self, target: &Self) -> f64 {
        let mut dot_product = 0.0;

        for component_index in 0..Self::len() {
            dot_product += self.get(component_index) * target.get(component_index);
        }

        dot_product
    }

    ///
    /// Computes the magnitude of this vector
    /// 
    #[inline]
    fn magnitude(&self) -> f64 {
        f64::sqrt(self.dot(self))
    }

    ///
    /// Treating this as a vector, returns a unit vector in the same direction
    /// 
    #[inline]
    fn to_unit_vector(&self) -> Self {
        let magnitude = self.magnitude();
        if magnitude == 0.0 {
            Self::origin()
        } else {
            *self * (1.0/magnitude)
        }
    }

    ///
    /// Returns true if this coordinate has a NaN component
    ///
    #[inline]
    fn is_nan(&self) -> bool {
        for component in 0..Self::len() {
            if self.get(component).is_nan() {
                return true;
            }
        }

        return false;
    }

    ///
    /// Round this coordinate so that it is accurate to a certain precision
    ///
    #[inline]
    fn round(self, accuracy: f64) -> Self {
        let mut new_components: SmallVec<[_; 4]> = smallvec![];

        for component in 0..Self::len() {
            let unrounded_value = self.get(component);
            let rounded_value   = (unrounded_value/accuracy).round() * accuracy;

            new_components.push(rounded_value);
        }

        Self::from_components(&new_components)
    }

    ///
    /// True if this point is within max_distance of another point
    /// 
    #[inline]
    fn is_near_to(&self, other: &Self, max_distance: f64) -> bool {
        let offset              = *self - *other;
        let squared_distance    = offset.dot(&offset);

        squared_distance <= (max_distance*max_distance)
    }

    ///
    /// Generates a smoothed version of a set of coordinates, using the specified weights
    /// (weights should add up to 1.0).
    /// 
    /// A suggested set of weights might be '[0.25, 0.5, 0.25]', which will slightly
    /// adjust each point according to its neighbours (the central weight is what's
    /// applied to the 'current' point)
    /// 
    fn smooth(points: &[Self], weights: &[f64]) -> Vec<Self> {
        let mut smoothed    = vec![];
        let points_len      = points.len() as i32;
        let weight_len      = weights.len() as i32;
        let weight_offset   = weight_len/2;
        
        for index in 0..points_len {
            let mut res     = Self::origin();
            let initial_pos = index - weight_offset;

            for weight_pos in 0..weight_len {
                let weight      = weights[weight_pos as usize];
                let source_pos  = initial_pos + weight_pos;

                let source_val  = if source_pos < 0 {
                    &points[0]
                } else if source_pos >= points_len {
                    &points[(points_len-1) as usize]
                } else {
                    &points[source_pos as usize]
                };

                res = res + (*source_val * weight);
            }

            smoothed.push(res);
        }

        smoothed
    }
}

///
/// Represents a coordinate with a 2D position
/// 
pub trait Coordinate2D {
    fn x(&self) -> f64;
    fn y(&self) -> f64;

    #[inline]
    fn coords(&self) -> (f64, f64) { (self.x(), self.y()) }
}

///
/// Represents a coordinate with a 3D position
/// 
pub trait Coordinate3D {
    fn x(&self) -> f64;
    fn y(&self) -> f64;
    fn z(&self) -> f64;
}

impl Coordinate for f64 {
    fn from_components(components: &[f64]) -> f64 {
        components[0]
    }

    #[inline] fn origin() -> f64 { 0.0 }
    #[inline] fn len() -> usize { 1 }
    #[inline] fn get(&self, _index: usize) -> f64 { *self }

    #[inline]
    fn from_biggest_components(p1: f64, p2: f64) -> f64 {
        if p1 > p2 {
            p1
        } else {
            p2
        }
    }

    #[inline]
    fn from_smallest_components(p1: f64, p2: f64) -> f64 {
        if p1 < p2 {
            p1
        } else {
            p2
        }
    }

    #[inline]
    fn distance_to(&self, target: &f64) -> f64 {
        f64::abs(self-target)
    }

    fn dot(&self, target: &f64) -> f64 {
        self * target
    }
}

/// Represents a 2D point
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Coord2(pub f64, pub f64);

impl Coordinate2D for Coord2 {
    ///
    /// X component of this coordinate
    /// 
    #[inline]
    fn x(&self) -> f64 {
        self.0
    }

    ///
    /// Y component of this coordinate
    /// 
    #[inline]
    fn y(&self) -> f64 {
        self.1
    }
}

impl Add<Coord2> for Coord2 {
    type Output=Coord2;

    #[inline]
    fn add(self, rhs: Coord2) -> Coord2 {
        Coord2(self.0 + rhs.0, self.1 + rhs.1)
    }
}

impl Sub<Coord2> for Coord2 {
    type Output=Coord2;

    #[inline]
    fn sub(self, rhs: Coord2) -> Coord2 {
        Coord2(self.0 - rhs.0, self.1 - rhs.1)
    }
}

impl Mul<f64> for Coord2 {
    type Output=Coord2;

    #[inline]
    fn mul(self, rhs: f64) -> Coord2 {
        Coord2(self.0 * rhs, self.1 * rhs)
    }
}

impl Coordinate for Coord2 {
    #[inline]
    fn from_components(components: &[f64]) -> Coord2 {
        Coord2(components[0], components[1])
    }

    #[inline]
    fn origin() -> Coord2 {
        Coord2(0.0, 0.0)
    }

    #[inline]
    fn len() -> usize { 2 }

    #[inline]
    fn get(&self, index: usize) -> f64 { 
        match index {
            0 => self.0,
            1 => self.1,
            _ => panic!("Coord2 only has two components")
        }
    }

    fn from_biggest_components(p1: Coord2, p2: Coord2) -> Coord2 {
        Coord2(f64::from_biggest_components(p1.0, p2.0), f64::from_biggest_components(p1.1, p2.1))
    }

    fn from_smallest_components(p1: Coord2, p2: Coord2) -> Coord2 {
        Coord2(f64::from_smallest_components(p1.0, p2.0), f64::from_smallest_components(p1.1, p2.1))
    }

    #[inline]
    fn distance_to(&self, target: &Coord2) -> f64 {
        let dist_x = target.0-self.0;
        let dist_y = target.1-self.1;

        f64::sqrt(dist_x*dist_x + dist_y*dist_y)
    }

    #[inline]
    fn dot(&self, target: &Self) -> f64 {
        self.0*target.0 + self.1*target.1
    }
}