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
/// Represents a position within a substrate.
/// Each position coordinate is mapped to an input of a CPPN.
pub trait Position {
    // Number of dimensions
    fn dims() -> usize;

    fn coords(&self) -> &[f64];
    fn distance_square(&self, other: &Self) -> f64;
    fn distance(&self, other: &Self) -> f64 {
        self.distance_square(other).sqrt()
    }
    fn origin() -> Self;
}

pub trait Interpolate {
    /// Linearly interpolates between `self` and `other`.
    /// For t = 0.0 this should return `self`. For t = 1.0 this
    /// should return `other`.
    fn interpolate(&self, other: &Self, t: f64) -> Self;

    fn interpolate_multi(&self, other: &Self, t: &Self) -> Self;
}

pub struct Position2d([f64; 2]);

impl Position2d {
    #[inline(always)]
    pub fn new(x: f64, y: f64) -> Self {
        Position2d([x, y])
    }

    #[inline(always)]
    pub fn x(&self) -> f64 {
        self.0[0]
    }

    #[inline(always)]
    pub fn y(&self) -> f64 {
        self.0[1]
    }

    #[inline(always)]
    pub fn xy(&self) -> (f64, f64) {
        (self.0[0], self.0[1])
    }
}

impl Position for Position2d {
    #[inline(always)]
    fn dims() -> usize { 2 }

    #[inline(always)]
    fn coords(&self) -> &[f64] {
        &self.0
    }

    #[inline(always)]
    fn origin() -> Self {
        Position2d::new(0.0, 0.0)
    }

    #[inline]
    fn distance_square(&self, other: &Self) -> f64 {
        (self.x() - other.x()).powi(2) + (self.y() - other.y()).powi(2)
    }
}

impl Interpolate for Position2d {
    fn interpolate(&self, other: &Self, t: f64) -> Self {
        let x = self.x() * (1.0 - t) + other.x() * t;
        let y = self.y() * (1.0 - t) + other.y() * t;
        Position2d([x, y])
    }

    fn interpolate_multi(&self, other: &Self, t: &Self) -> Self {
        let tx = t.x();
        let ty = t.y();
        let x = self.x() * (1.0 - tx) + other.x() * tx;
        let y = self.y() * (1.0 - ty) + other.y() * ty;
        Position2d([x, y])
    }
}


pub struct Position3d([f64; 3]);

impl Position3d {
    #[inline(always)]
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Position3d([x, y, z])
    }

    #[inline(always)]
    pub fn x(&self) -> f64 {
        self.0[0]
    }

    #[inline(always)]
    pub fn y(&self) -> f64 {
        self.0[1]
    }

    #[inline(always)]
    pub fn z(&self) -> f64 {
        self.0[2]
    }

    #[inline(always)]
    pub fn xyz(&self) -> (f64, f64, f64) {
        (self.0[0], self.0[1], self.0[2])
    }
}

impl Position for Position3d {
    #[inline(always)]
    fn dims() -> usize { 3 }

    #[inline(always)]
    fn coords(&self) -> &[f64] {
        &self.0
    }

    #[inline(always)]
    fn origin() -> Self {
        Position3d::new(0.0, 0.0, 0.0)
    }

    #[inline]
    fn distance_square(&self, other: &Self) -> f64 {
        (self.x() - other.x()).powi(2) + (self.y() - other.y()).powi(2) +
        (self.z() - other.z()).powi(2)
    }
}

#[test]
fn test_position3d_distance() {
    assert_eq!(0.0, Position3d::origin().distance(&Position3d::origin()));
    assert_eq!(1.0,
               Position3d::origin().distance(&Position3d::new(1.0, 0.0, 0.0)));
    assert_eq!(2.0,
               Position3d::origin().distance(&Position3d::new(2.0, 0.0, 0.0)));
    assert_eq!((2.0f64).sqrt(),
               Position3d::origin().distance(&Position3d::new(1.0, 0.0, 1.0)));
    assert_eq!((2.0f64).sqrt(),
               Position3d::origin().distance(&Position3d::new(1.0, 0.0, -1.0)));
    assert_eq!((2.0f64).sqrt(),
               Position3d::origin().distance(&Position3d::new(-1.0, 0.0, -1.0)));
}

#[test]
fn test_interpolate_one_axis() {
    let a = Position2d::new(-1.0, 0.0);
    let b = Position2d::new(1.0, 0.0);

    assert_eq!((-1.0, 0.0), a.interpolate(&b, 0.0).xy());
    assert_eq!((0.0, 0.0), a.interpolate(&b, 0.5).xy());
    assert_eq!((1.0, 0.0), a.interpolate(&b, 1.0).xy());
}

#[test]
fn test_interpolate_two_axes() {
    let a = Position2d::new(-1.0, 1.0);
    let b = Position2d::new(1.0, -1.0);

    assert_eq!((-1.0, 1.0), a.interpolate(&b, 0.0).xy());
    assert_eq!((0.0, 0.0), a.interpolate(&b, 0.5).xy());
    assert_eq!((1.0, -1.0), a.interpolate(&b, 1.0).xy());
}

#[test]
fn test_interpolate_multi() {
    let a = Position2d::new(-1.0, 1.0);
    let b = Position2d::new(1.0, -1.0);

    assert_eq!((-1.0, -1.0),
               a.interpolate_multi(&b, &Position2d::new(0.0, 1.0)).xy());
    assert_eq!((0.0, 0.0),
               a.interpolate_multi(&b, &Position2d::new(0.5, 0.5)).xy());
    assert_eq!((1.0, 1.0),
               a.interpolate_multi(&b, &Position2d::new(1.0, 0.0)).xy());
}