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
use alga::general::{Real, SupersetOf};
use nalgebra::{self, Matrix4, Point2, Point3, Scalar, Unit, Vector3, Vector4};
use num::{Float, Integer};
use std::cmp;
use std::ops;

pub type UScalar = u32;
pub type FScalar = f32;

pub type UPoint2 = Point2<UScalar>;
pub type UPoint3 = Point3<UScalar>;
pub type UVector3 = Vector3<UScalar>;

pub type FPoint2 = Point2<FScalar>;
pub type FPoint3 = Point3<FScalar>;
pub type FVector3 = Vector3<FScalar>;
pub type FVector4 = Vector4<FScalar>;
pub type FMatrix4 = Matrix4<FScalar>;

pub type FRay3 = Ray3<FScalar>;

pub struct Ray3<T>
where
    T: Scalar,
{
    pub origin: Point3<T>,
    pub direction: Unit<Vector3<T>>,
}

impl<T> Ray3<T>
where
    T: Real + Scalar,
{
    pub fn new(origin: Point3<T>, direction: Vector3<T>) -> Self {
        Ray3 {
            origin: origin,
            direction: Unit::new_normalize(direction),
        }
    }

    pub fn x() -> Self {
        Ray3::new(Point3::origin(), Vector3::x())
    }

    pub fn y() -> Self {
        Ray3::new(Point3::origin(), Vector3::y())
    }

    pub fn z() -> Self {
        Ray3::new(Point3::origin(), Vector3::z())
    }
}

pub trait Matrix4Ext<T>
where
    T: Scalar,
{
    fn to_array(&self) -> [[T; 4]; 4];
}

impl<T> Matrix4Ext<T> for Matrix4<T>
where
    T: Scalar,
{
    #[cfg_attr(rustfmt, rustfmt_skip)]
    fn to_array(&self) -> [[T; 4]; 4] {
        [
            [self[0],  self[1],  self[2],  self[3]],
            [self[4],  self[5],  self[6],  self[7]],
            [self[8],  self[9],  self[10], self[11]],
            [self[12], self[13], self[14], self[15]]
        ]
    }
}

// TODO: The `FromSpace` and `IntoSpace` traits may not be useful. Instead, the
//       `nalgebra::convert` function can be used directly.
pub trait FromSpace<T> {
    fn from_space(value: T) -> Self;
}

impl<T, U> FromSpace<Point2<U>> for Point2<T>
where
    T: Scalar + SupersetOf<U>,
    U: Scalar,
{
    fn from_space(point: Point2<U>) -> Self {
        nalgebra::convert(point)
    }
}

impl<T, U> FromSpace<Point3<U>> for Point3<T>
where
    T: Scalar + SupersetOf<U>,
    U: Scalar,
{
    fn from_space(point: Point3<U>) -> Self {
        nalgebra::convert(point)
    }
}

impl<T, U> FromSpace<Vector3<U>> for Vector3<T>
where
    T: Scalar + SupersetOf<U>,
    U: Scalar,
{
    fn from_space(vector: Vector3<U>) -> Self {
        nalgebra::convert(vector)
    }
}

pub trait IntoSpace<T> {
    fn into_space(self) -> T;
}

impl<T, U> IntoSpace<U> for T
where
    U: FromSpace<T>,
{
    fn into_space(self) -> U {
        U::from_space(self)
    }
}

pub trait Clamp<T>
where
    T: PartialOrd,
{
    fn clamp(&self, min: T, max: T) -> Self;
}

impl<T> Clamp<T> for Point3<T>
where
    T: PartialOrd + Scalar,
{
    fn clamp(&self, min: T, max: T) -> Self {
        use nalgebra::clamp;

        Point3::new(
            clamp(self.x, min, max),
            clamp(self.y, min, max),
            clamp(self.z, min, max),
        )
    }
}

pub trait Mask<T>
where
    T: ops::BitAnd<Output = T>,
{
    fn mask(&self, value: T) -> Self;
}

impl<T> Mask<T> for Point3<T>
where
    T: ops::BitAnd<Output = T> + Scalar,
{
    fn mask(&self, value: T) -> Self {
        Point3::new(self.x & value, self.y & value, self.z & value)
    }
}

pub trait UpperBound {
    fn upper_bound(&self, other: &Self) -> Self;
}

impl<T> UpperBound for Point3<T>
where
    T: Ord + Scalar,
{
    fn upper_bound(&self, other: &Self) -> Self {
        Point3::new(
            cmp::max(self.x, other.x),
            cmp::max(self.y, other.y),
            cmp::max(self.z, other.z),
        )
    }
}

pub trait LowerBound {
    fn lower_bound(&self, other: &Self) -> Self;
}

impl<T> LowerBound for Point3<T>
where
    T: Ord + Scalar,
{
    fn lower_bound(&self, other: &Self) -> Self {
        Point3::new(
            cmp::min(self.x, other.x),
            cmp::min(self.y, other.y),
            cmp::min(self.z, other.z),
        )
    }
}

pub trait Interpolate<F>: Sized
where
    F: Float,
{
    fn lerp(&self, other: &Self, f: F) -> Self;

    fn midpoint(&self, other: &Self) -> Self {
        self.lerp(other, F::one() / (F::one() + F::one()))
    }
}

impl<T, F> Interpolate<F> for (T, T)
where
    T: Scalar + SupersetOf<F>,
    F: Float + SupersetOf<T>,
{
    fn lerp(&self, other: &Self, f: F) -> Self {
        (lerp(self.0, other.0, f), lerp(self.1, other.1, f))
    }
}

impl<T, F> Interpolate<F> for (T, T, T)
where
    T: Scalar + SupersetOf<F>,
    F: Float + SupersetOf<T>,
{
    fn lerp(&self, other: &Self, f: F) -> Self {
        (
            lerp(self.0, other.0, f),
            lerp(self.1, other.1, f),
            lerp(self.2, other.2, f),
        )
    }
}

impl<T, F> Interpolate<F> for Point2<T>
where
    T: Scalar + SupersetOf<F>,
    F: Float + SupersetOf<T>,
{
    fn lerp(&self, other: &Self, f: F) -> Self {
        Point2::new(lerp(self.x, other.x, f), lerp(self.y, other.y, f))
    }
}

impl<T, F> Interpolate<F> for Point3<T>
where
    T: Scalar + SupersetOf<F>,
    F: Float + SupersetOf<T>,
{
    fn lerp(&self, other: &Self, f: F) -> Self {
        Point3::new(
            lerp(self.x, other.x, f),
            lerp(self.y, other.y, f),
            lerp(self.z, other.z, f),
        )
    }
}

pub fn lerp<T, F>(a: T, b: T, f: F) -> T
where
    T: Scalar + SupersetOf<F>,
    F: Float + SupersetOf<T>,
{
    use nalgebra::{convert, clamp};

    let f = clamp(f, F::zero(), F::one());
    let af = convert::<T, F>(a) * (F::one() - f);
    let bf = convert::<T, F>(b) * f;
    convert::<F, T>(af + bf)
}

pub fn ordered_pair<T>(a: T, b: T) -> (T, T)
where
    T: PartialOrd,
{
    if a <= b {
        (a, b)
    }
    else {
        (b, a)
    }
}

pub fn partial_min<T>(a: T, b: T) -> T
where
    T: PartialOrd,
{
    if a <= b {
        a
    }
    else {
        b
    }
}

pub fn partial_max<T>(a: T, b: T) -> T
where
    T: PartialOrd,
{
    if a > b {
        a
    }
    else {
        b
    }
}

pub fn umod<T>(n: T, m: T) -> T
where
    T: Copy + Integer,
{
    ((n % m) + m) % m
}