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
use crate::color;
use crate::geom::{scalar, Point2, Point3};
use crate::math::BaseNum;
use std::ops::{Deref, DerefMut};

/// Types used as vertices that can be used to describe geometric points in space.
pub trait Vertex: Clone + Copy + PartialEq {
    /// The values used to describe the vertex position.
    type Scalar: BaseNum;
}

/// Vertex types that have at least 2 dimensions.
pub trait Vertex2d: Vertex {
    /// The x, y location of the vertex.
    fn point2(self) -> Point2<Self::Scalar>;
}

/// Vertex types that have at least 3 dimensions.
pub trait Vertex3d: Vertex2d {
    /// The x, y, z location of the vertex.
    fn point3(self) -> Point3<Self::Scalar>;
}

/// If a type is not specified for a piece of geometry, this is the default type used.
pub type Default = Point3<scalar::Default>;

/// An iterator yielding a vertex for each index yielded by the given indices iterator.
#[derive(Clone, Debug)]
pub struct IterFromIndices<'a, I, V: 'a = Default> {
    indices: I,
    vertices: &'a [V],
}

/// A vertex that is colored with the given `sRGBA` color.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Srgba<V = Default>(pub V, pub color::Srgba);

/// Produce an iterator yielding a vertex for each index yielded by the given indices iterator.
pub fn iter_from_indices<I, V>(indices: I, vertices: &[V]) -> IterFromIndices<I::IntoIter, V>
where
    I: IntoIterator<Item = usize>,
{
    let indices = indices.into_iter();
    IterFromIndices { indices, vertices }
}

// Iterators

impl<'a, I, V> Iterator for IterFromIndices<'a, I, V>
where
    I: Iterator<Item = usize>,
{
    type Item = &'a V;
    fn next(&mut self) -> Option<Self::Item> {
        let IterFromIndices {
            ref mut indices,
            ref vertices,
        } = *self;
        indices.next().map(|i| &vertices[i])
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.indices.size_hint()
    }
}

impl<'a, I, V> DoubleEndedIterator for IterFromIndices<'a, I, V>
where
    I: Iterator<Item = usize> + DoubleEndedIterator,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        let IterFromIndices {
            ref mut indices,
            ref vertices,
        } = *self;
        indices.next_back().map(|i| &vertices[i])
    }
}

impl<'a, I, V> ExactSizeIterator for IterFromIndices<'a, I, V>
where
    I: Iterator<Item = usize> + ExactSizeIterator,
{
    fn len(&self) -> usize {
        self.indices.len()
    }
}

// Srgba impls.

impl<V> Srgba<V> {
    /// A reference to the inner vertex.
    pub fn vertex(&self) -> &V {
        &self.0
    }
    /// A reference to the inner rgba.
    pub fn rgba(&self) -> &color::Srgba {
        &self.1
    }
}

impl<V> Deref for Srgba<V> {
    type Target = V;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<V> DerefMut for Srgba<V> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<V> From<(V, color::Srgba)> for Srgba<V> {
    fn from((v, rgba): (V, color::Srgba)) -> Self {
        Srgba(v, rgba)
    }
}

impl<V> Into<(V, color::Srgba)> for Srgba<V> {
    fn into(self) -> (V, color::Srgba) {
        let Srgba(v, rgba) = self;
        (v, rgba)
    }
}

// Vertex impls

impl<S> Vertex for Point2<S>
where
    S: BaseNum,
{
    type Scalar = S;
}

impl<S> Vertex for Point3<S>
where
    S: BaseNum,
{
    type Scalar = S;
}

impl<S> Vertex for [S; 2]
where
    S: BaseNum,
{
    type Scalar = S;
}

impl<S> Vertex for [S; 3]
where
    S: BaseNum,
{
    type Scalar = S;
}

impl<S> Vertex for (S, S)
where
    S: BaseNum,
{
    type Scalar = S;
}

impl<S> Vertex for (S, S, S)
where
    S: BaseNum,
{
    type Scalar = S;
}

// impl<S> Vertex for Vector2<S>
// where
//     S: BaseNum,
// {
//     type Scalar = S;
// }
//
// impl<S> Vertex for Vector3<S>
// where
//     S: BaseNum,
// {
//     type Scalar = S;
// }

impl<V> Vertex for Srgba<V>
where
    V: Vertex,
{
    type Scalar = V::Scalar;
}

// Vertex2d impls

impl<S> Vertex2d for Point2<S>
where
    S: BaseNum,
{
    fn point2(self) -> Point2<S> {
        Point2 {
            x: self.x,
            y: self.y,
        }
    }
}

impl<S> Vertex2d for Point3<S>
where
    S: BaseNum,
{
    fn point2(self) -> Point2<S> {
        Point2 {
            x: self.x,
            y: self.y,
        }
    }
}

impl<S> Vertex2d for [S; 2]
where
    S: BaseNum,
{
    fn point2(self) -> Point2<S> {
        Point2 {
            x: self[0],
            y: self[1],
        }
    }
}

impl<S> Vertex2d for [S; 3]
where
    S: BaseNum,
{
    fn point2(self) -> Point2<S> {
        Point2 {
            x: self[0],
            y: self[1],
        }
    }
}

impl<S> Vertex2d for (S, S)
where
    S: BaseNum,
{
    fn point2(self) -> Point2<S> {
        let (x, y) = self;
        Point2 { x, y }
    }
}

impl<S> Vertex2d for (S, S, S)
where
    S: BaseNum,
{
    fn point2(self) -> Point2<S> {
        let (x, y, _) = self;
        Point2 { x, y }
    }
}

// impl<S> Vertex2d for Vector2<S>
// where
//     S: BaseNum,
// {
//     fn point2(self) -> Point2<S> {
//         self
//     }
// }
//
// impl<S> Vertex2d for Vector3<S>
// where
//     S: BaseNum,
// {
//     fn point2(self) -> Point2<S> {
//         Point2 { x: self.x, y: self.y }
//     }
// }

impl<V> Vertex2d for Srgba<V>
where
    V: Vertex2d,
{
    fn point2(self) -> Point2<V::Scalar> {
        self.0.point2()
    }
}

// Vertex3d impls

impl<S> Vertex3d for Point3<S>
where
    S: BaseNum,
{
    fn point3(self) -> Point3<S> {
        Point3 {
            x: self.x,
            y: self.y,
            z: self.z,
        }
    }
}

impl<S> Vertex3d for [S; 3]
where
    S: BaseNum,
{
    fn point3(self) -> Point3<S> {
        Point3 {
            x: self[0],
            y: self[1],
            z: self[2],
        }
    }
}

impl<S> Vertex3d for (S, S, S)
where
    S: BaseNum,
{
    fn point3(self) -> Point3<S> {
        let (x, y, z) = self;
        Point3 { x, y, z }
    }
}

// impl<S> Vertex3d for Vector3<S>
// where
//     S: BaseNum,
// {
//     fn point3(self) -> Point3<S> {
//         self
//     }
// }

impl<V> Vertex3d for Srgba<V>
where
    V: Vertex3d,
{
    fn point3(self) -> Point3<V::Scalar> {
        self.0.point3()
    }
}