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
use std::f32::consts::{self, FRAC_1_SQRT_2};

use super::generators::{IndexedPolygon, SharedVertex};
use super::{MapVertex, Triangle, Vertex};

const TWO_PI: f32 = consts::PI * 2.;

#[derive(Debug)]
enum VertexSection {
    Tip(usize),
    TopRadius(usize),
    BottomRadius(usize),
    BottomCenter,
}

/// The `Cone` mesh will create a mesh that is from 1 to -1
/// The bottom will be a circle around [0, 0, -1] with a radius
/// of 1, all coords on the bottom will follow the plan equation `-z-1=0`
/// The tip of the cone will always be at coord [0, 0, 1]
pub struct Cone {
    u: usize,
    sub_u: usize,
}

impl Cone {
    /// Creates a new `Cone`
    /// `u` is the number of subdivisions around the radius of the cone
    /// it must be greater then 1.
    pub fn new(u: usize) -> Self {
        assert!(u >= 2);
        Cone { u: 0, sub_u: u }
    }

    ///
    fn vertex(&self, sec: VertexSection) -> Vertex {
        let divisions = TWO_PI / self.sub_u as f32;

        match sec {
            VertexSection::Tip(i) => {
                // the normal is in the middle of the two divisions
                // so we add half a subdivision
                let pos = divisions * i as f32 + divisions / 2.;
                Vertex {
                    pos: [0., 0., 1.].into(),
                    normal: [
                        pos.cos() * FRAC_1_SQRT_2,
                        pos.sin() * FRAC_1_SQRT_2,
                        -FRAC_1_SQRT_2,
                    ].into(),
                }
            }
            VertexSection::TopRadius(i) => {
                let pos = divisions * i as f32;
                Vertex {
                    pos: [pos.cos(), pos.sin(), -1.].into(),
                    normal: [
                        pos.cos() * FRAC_1_SQRT_2,
                        pos.sin() * FRAC_1_SQRT_2,
                        -FRAC_1_SQRT_2,
                    ].into(),
                }
            }
            VertexSection::BottomRadius(i) => {
                let pos = divisions * i as f32;
                Vertex {
                    pos: [pos.cos(), pos.sin(), -1.].into(),
                    normal: [0., 0., -1.].into(),
                }
            }
            VertexSection::BottomCenter => Vertex {
                pos: [0., 0., -1.].into(),
                normal: [0., 0., -1.].into(),
            },
        }
    }

    fn index(&self, sec: VertexSection) -> usize {
        match sec {
            VertexSection::Tip(i) => i,
            VertexSection::TopRadius(i) => i + self.sub_u,
            VertexSection::BottomRadius(i) => i + self.sub_u * 2,
            VertexSection::BottomCenter => self.sub_u * 3,
        }
    }

    fn rev_index(&self, idx: usize) -> VertexSection {
        if idx < self.sub_u {
            VertexSection::Tip(idx)
        } else if idx < self.sub_u * 2 {
            VertexSection::TopRadius(idx - self.sub_u)
        } else if idx < self.sub_u * 3 {
            VertexSection::BottomRadius(idx - self.sub_u * 2)
        } else {
            VertexSection::BottomCenter
        }
    }
}

impl Iterator for Cone {
    type Item = Triangle<Vertex>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.u < self.sub_u * 2 {
            let idx = self.u;
            self.u += 1;
            Some(
                self.indexed_polygon(idx)
                    .map_vertex(|i| self.shared_vertex(i)),
            )
        } else {
            None
        }
    }
}

impl SharedVertex<Vertex> for Cone {
    fn shared_vertex(&self, idx: usize) -> Vertex {
        self.vertex(self.rev_index(idx))
    }

    fn shared_vertex_count(&self) -> usize {
        // a unique vertex for every subdivide at the top
        // a unique vertex for every radius, top
        // a unique vertex for every radius, bottom
        // one for the bottom most vertex
        self.sub_u * 3 + 1
    }
}

impl IndexedPolygon<Triangle<usize>> for Cone {
    fn indexed_polygon(&self, idx: usize) -> Triangle<usize> {
        // top
        if idx < self.sub_u {
            let next = if idx != self.sub_u - 1 { idx + 1 } else { 0 };
            Triangle::new(
                self.index(VertexSection::Tip(idx)),
                self.index(VertexSection::TopRadius(idx)),
                self.index(VertexSection::TopRadius(next)),
            )
        // bottom
        } else {
            let idx = idx - self.sub_u;
            let next = if idx != self.sub_u - 1 { idx + 1 } else { 0 };
            Triangle::new(
                self.index(VertexSection::BottomCenter),
                self.index(VertexSection::BottomRadius(next)),
                self.index(VertexSection::BottomRadius(idx)),
            )
        }
    }

    fn indexed_polygon_count(&self) -> usize {
        // a face for every subdivide on the top, and one for every
        // subdivide around the bottom circle.
        self.sub_u * 2
    }
}