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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use crate::{
    d2::{SimpleConvexGeometry, Vertex2D},
    Geometry,
};

/// An angle, in radians.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Default)]
pub struct Rad(pub f32);

/// An angle, in degrees.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Default)]
pub struct Deg(pub f32);

impl From<Rad> for Deg {
    #[inline]
    fn from(rad: Rad) -> Deg {
        Deg(rad.0 * 180.0 / std::f32::consts::PI)
    }
}

impl From<Deg> for Rad {
    #[inline]
    fn from(deg: Deg) -> Rad {
        Rad(deg.0 * std::f32::consts::PI / 180.0)
    }
}

#[derive(Debug, Copy, Clone)]
pub enum ArcType {
    Pie,
    Open,
    Closed,
}

impl Default for ArcType {
    fn default() -> Self {
        ArcType::Pie
    }
}

#[derive(Debug, Copy, Clone, Default)]
pub struct Arc {
    pub arc_type: ArcType,
    pub x: f32,
    pub y: f32,
    pub radius: f32,
    pub angle1: Rad,
    pub angle2: Rad,
    pub segments: u32,
}

impl SimpleConvexGeometry for Arc {
    type Vertices = std::vec::IntoIter<Vertex2D>;

    fn vertices(&self) -> Self::Vertices {
        let Arc {
            arc_type,
            x,
            y,
            radius,
            angle1,
            angle2,
            segments,
        } = *self;
        let (angle1, angle2) = (angle1.0, angle2.0);

        if segments == 0 || (angle1 - angle2).abs() < std::f32::EPSILON {
            return Vec::<Vertex2D>::new().into_iter();
        }

        const TWO_PI: f32 = std::f32::consts::PI * 2.;
        if (angle1 - angle2).abs() >= TWO_PI {
            return SimpleConvexGeometry::vertices(&Circle {
                x,
                y,
                radius,
                segments,
            })
            .collect::<Vec<_>>() // type constraints require that we collect here
            .into_iter();
        }

        let angle_shift = (angle2 - angle1) / segments as f32;
        if angle_shift == 0. {
            return Vec::<Vertex2D>::new().into_iter(); // bail on precision fail
        }

        let mut create_points = {
            let mut phi = angle1;
            move |coordinates: &mut [Vertex2D]| {
                for coordinate in coordinates.iter_mut() {
                    phi += angle_shift;
                    let x = x + radius * phi.cos();
                    let y = y + radius * phi.sin();
                    coordinate.position[0] = x;
                    coordinate.position[1] = y;
                }
            }
        };

        let vertices = match arc_type {
            ArcType::Pie => {
                let num_coords = segments as usize + 3;
                let mut coords = vec![Vertex2D::default(); num_coords];
                coords[0] = Vertex2D {
                    position: [x, y],
                    ..Vertex2D::default()
                };
                create_points(&mut coords[1..]);
                coords
            }
            ArcType::Open => {
                let num_coords = segments as usize + 1;
                let mut coords = vec![Vertex2D::default(); num_coords];
                create_points(&mut coords);
                coords
            }
            ArcType::Closed => {
                let num_coords = segments as usize + 2;
                let mut coords = vec![Vertex2D::default(); num_coords];
                create_points(&mut coords);
                coords[num_coords - 1] = coords[0];
                coords
            }
        };

        vertices.into_iter()
    }

    fn vertex_count(&self) -> usize {
        match self.arc_type {
            ArcType::Pie => self.segments as usize + 3,
            ArcType::Open => self.segments as usize + 1,
            ArcType::Closed => self.segments as usize + 2,
        }
    }
}

#[derive(Debug, Copy, Clone, Default)]
pub struct Circle {
    pub x: f32,
    pub y: f32,
    pub radius: f32,
    pub segments: u32,
}

impl SimpleConvexGeometry for Circle {
    type Vertices = <SimpleConvexPolygon as SimpleConvexGeometry>::Vertices;

    fn vertices(&self) -> Self::Vertices {
        let ellipse: Ellipse = (*self).into();
        let polygon: SimpleConvexPolygon = ellipse.into();
        SimpleConvexGeometry::vertices(&polygon)
    }

    fn vertex_count(&self) -> usize {
        self.segments as usize
    }
}

impl Into<Ellipse> for Circle {
    fn into(self) -> Ellipse {
        Ellipse {
            x: self.x,
            y: self.y,
            radius_x: self.radius,
            radius_y: self.radius,
            segments: self.segments,
        }
    }
}

#[derive(Copy, Clone, Default, Debug)]
pub struct Ellipse {
    pub x: f32,
    pub y: f32,
    pub radius_x: f32,
    pub radius_y: f32,
    pub segments: u32,
}

impl Into<SimpleConvexPolygon> for Ellipse {
    fn into(self) -> SimpleConvexPolygon {
        SimpleConvexPolygon {
            x: self.x,
            y: self.y,
            vertex_count: self.segments,
            radius_x: self.radius_x,
            radius_y: self.radius_y,
        }
    }
}

impl SimpleConvexGeometry for Ellipse {
    type Vertices = <SimpleConvexPolygon as SimpleConvexGeometry>::Vertices;

    fn vertices(&self) -> Self::Vertices {
        let polygon: SimpleConvexPolygon = (*self).into();
        SimpleConvexGeometry::vertices(&polygon)
    }

    fn vertex_count(&self) -> usize {
        self.segments as usize
    }
}

#[derive(Copy, Clone, Default, Debug)]
pub struct Rectangle {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl Rectangle {
    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }
}

impl Geometry<Vertex2D> for Rectangle {
    type Vertices = std::vec::IntoIter<Vertex2D>;
    type Indices = std::iter::Copied<std::slice::Iter<'static, u32>>;

    fn vertices(&self) -> Self::Vertices {
        vec![
            Vertex2D {
                position: [self.x, self.y],
                uv: [0., 0.],
                ..Default::default()
            },
            Vertex2D {
                position: [self.x, self.y + self.height],
                uv: [0., 1.],
                ..Default::default()
            },
            Vertex2D {
                position: [self.x + self.width, self.y + self.height],
                uv: [1., 1.],
                ..Default::default()
            },
            Vertex2D {
                position: [self.x + self.width, self.y],
                uv: [1., 0.],
                ..Default::default()
            },
        ]
        .into_iter()
    }

    fn indices(&self) -> Self::Indices {
        [0, 1, 2, 0, 3, 2].iter().copied()
    }
}

#[derive(Copy, Clone, Default, Debug)]
pub struct RegularPolygon {
    pub x: f32,
    pub y: f32,
    pub vertex_count: u32,
    pub radius: f32,
}

impl RegularPolygon {
    pub fn new(x: f32, y: f32, vertex_count: u32, radius: f32) -> Self {
        Self {
            x,
            y,
            vertex_count,
            radius,
        }
    }
}

impl SimpleConvexGeometry for RegularPolygon {
    type Vertices = std::iter::Map<std::ops::Range<u32>, Box<dyn Fn(u32) -> Vertex2D>>;

    fn vertices(&self) -> Self::Vertices {
        const TWO_PI: f32 = std::f32::consts::PI * 2.;
        let RegularPolygon {
            x,
            y,
            vertex_count,
            radius,
        } = *self;
        let angle_shift = TWO_PI / vertex_count as f32;

        // an allocation is a small price to pay for my sanity
        (0..vertex_count).map(Box::new(move |i| {
            let phi = angle_shift * i as f32;
            let (x, y) = (x + radius * phi.cos(), y + radius * phi.sin());
            Vertex2D::new([x, y], [1., 1., 1., 1.], [0.5, 0.5])
        }))
    }

    fn vertex_count(&self) -> usize {
        self.vertex_count as _
    }
}

#[derive(Copy, Clone, Default, Debug)]
pub struct SimpleConvexPolygon {
    pub x: f32,
    pub y: f32,
    pub vertex_count: u32,
    pub radius_x: f32,
    pub radius_y: f32,
}

impl SimpleConvexGeometry for SimpleConvexPolygon {
    type Vertices = std::iter::Map<std::ops::Range<u32>, Box<dyn Fn(u32) -> Vertex2D>>;

    fn vertices(&self) -> Self::Vertices {
        const TWO_PI: f32 = std::f32::consts::PI * 2.;
        let SimpleConvexPolygon {
            x,
            y,
            vertex_count,
            radius_x,
            radius_y,
        } = *self;
        let angle_shift = TWO_PI / vertex_count as f32;

        // an allocation is a small price to pay for my sanity
        (0..vertex_count).map(Box::new(move |i| {
            let phi = angle_shift * i as f32;
            let (x, y) = (x + radius_x * phi.cos(), y + radius_y * phi.sin());
            Vertex2D::new([x, y], [1., 1., 1., 1.], [0.5, 0.5])
        }))
    }

    fn vertex_count(&self) -> usize {
        self.vertex_count as usize
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn geometry() {
        use crate::Geometry;
        let geometry = SimpleConvexPolygon {
            x: 0.0,
            y: 0.0,
            vertex_count: 3,
            radius_x: 100.,
            radius_y: 100.,
        };

        assert_eq!(Geometry::vertices(&geometry).count(), 3);
        assert_eq!(Geometry::indices(&geometry).count(), 3);

        let geometry = SimpleConvexPolygon {
            vertex_count: 4,
            ..geometry
        };
        assert_eq!(Geometry::vertices(&geometry).count(), 4);
        assert_eq!(Geometry::indices(&geometry).count(), 6);

        let geometry = SimpleConvexPolygon {
            vertex_count: 5,
            ..geometry
        };
        assert_eq!(Geometry::vertices(&geometry).count(), 5);
        assert_eq!(Geometry::indices(&geometry).count(), (5 - 2) * 3);
    }
}