logo
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
//! Sphere primitives.
//!
//! # Examples
//!
//! Generating a graph from the positional data of a $uv$-sphere.
//!
//! ```rust
//! # extern crate decorum;
//! # extern crate nalgebra;
//! # extern crate plexus;
//! #
//! use decorum::N64;
//! use nalgebra::Point3;
//! use plexus::graph::MeshGraph;
//! use plexus::index::HashIndexer;
//! use plexus::prelude::*;
//! use plexus::primitive::generate::Position;
//! use plexus::primitive::sphere::UvSphere;
//!
//! type E3 = Point3<N64>;
//!
//! let mut graph = UvSphere::new(16, 8)
//!     .polygons::<Position<E3>>()
//!     .collect_with_indexer::<MeshGraph<E3>, _>(HashIndexer::default())
//!     .unwrap();
//! ```

use decorum::Real;
use num::{NumCast, One, ToPrimitive};
use std::cmp;
use theon::adjunct::Map;
use theon::query::Unit;
use theon::space::{EuclideanSpace, FiniteDimensional, Scalar, Vector};
use typenum::U3;

use crate::primitive::generate::{
    AttributeGenerator, AttributePolygonGenerator, AttributeVertexGenerator, Generator,
    IndexingPolygonGenerator, Normal, PolygonGenerator, Position,
};
use crate::primitive::{BoundedPolygon, Tetragon, Trigon};

#[derive(Clone, Copy)]
pub struct Bounds<S>
where
    S: EuclideanSpace,
{
    radius: Scalar<S>,
}

impl<S> Bounds<S>
where
    S: EuclideanSpace,
{
    pub fn with_radius(radius: Scalar<S>) -> Self {
        Bounds { radius }
    }

    pub fn with_width(width: Scalar<S>) -> Self {
        Self::with_radius(width / (Scalar::<S>::one() + One::one()))
    }

    pub fn unit_radius() -> Self {
        Self::with_radius(One::one())
    }

    pub fn unit_width() -> Self {
        Self::with_width(One::one())
    }
}

impl<S> Default for Bounds<S>
where
    S: EuclideanSpace,
{
    fn default() -> Self {
        Self::unit_radius()
    }
}

#[derive(Clone, Copy)]
pub struct UvSphere {
    nu: usize, // Meridians.
    nv: usize, // Parallels.
}

impl UvSphere {
    pub fn new(nu: usize, nv: usize) -> Self {
        UvSphere {
            nu: cmp::max(3, nu),
            nv: cmp::max(2, nv),
        }
    }

    fn vertex_with_position_from<S>(
        &self,
        state: &<Self as AttributeGenerator<Position<S>>>::State,
        u: usize,
        v: usize,
    ) -> S
    where
        Self: AttributeGenerator<Position<S>, State = Bounds<S>>,
        S: EuclideanSpace + FiniteDimensional<N = U3>,
    {
        let one = Scalar::<S>::one();
        let pi = Real::PI;
        let u = (into_scalar::<_, S>(u) / into_scalar::<_, S>(self.nu)) * pi * (one + one);
        let v = (into_scalar::<_, S>(v) / into_scalar::<_, S>(self.nv)) * pi;
        S::from_xyz(
            state.radius * u.cos() * v.sin(),
            state.radius * u.sin() * v.sin(),
            state.radius * v.cos(),
        )
    }

    fn index_for_position(&self, u: usize, v: usize) -> usize {
        if v == 0 {
            0
        }
        else if v == self.nv {
            ((self.nv - 1) * self.nu) + 1
        }
        else {
            ((v - 1) * self.nu) + (u % self.nu) + 1
        }
    }

    fn map_polygon_index(&self, index: usize) -> (usize, usize) {
        (index % self.nu, index / self.nu)
    }
}

impl Default for UvSphere {
    fn default() -> Self {
        UvSphere::new(16, 16)
    }
}

impl PolygonGenerator for UvSphere {
    fn polygon_count(&self) -> usize {
        self.nu * self.nv
    }
}

impl<S> AttributeGenerator<Normal<S>> for UvSphere
where
    S: EuclideanSpace + FiniteDimensional<N = U3>,
{
    type State = ();
}

impl<S> AttributeVertexGenerator<Normal<S>> for UvSphere
where
    S: EuclideanSpace + FiniteDimensional<N = U3>,
{
    type Output = Unit<Vector<S>>;

    fn vertex_count(&self) -> usize {
        (self.nv - 1) * self.nu + 2
    }

    fn vertex_from(&self, _: &Self::State, index: usize) -> Self::Output {
        let position =
            AttributeVertexGenerator::<Position<S>>::vertex_from(self, &Default::default(), index);
        Unit::try_from_inner(position.into_coordinates()).expect("non-zero vector")
    }
}

impl<S> AttributePolygonGenerator<Normal<S>> for UvSphere
where
    S: EuclideanSpace + FiniteDimensional<N = U3>,
{
    type Output = BoundedPolygon<Unit<Vector<S>>>;

    fn polygon_from(&self, _: &Self::State, index: usize) -> Self::Output {
        AttributePolygonGenerator::<Position<S>>::polygon_from(self, &Default::default(), index)
            .map(|position| {
                Unit::try_from_inner(position.into_coordinates()).expect("non-zero vector")
            })
    }
}

impl<S> IndexingPolygonGenerator<Normal<S>> for UvSphere {
    type Output = BoundedPolygon<usize>;

    fn indexing_polygon(&self, index: usize) -> Self::Output {
        IndexingPolygonGenerator::<Position<S>>::indexing_polygon(self, index)
    }
}

impl<S> AttributeGenerator<Position<S>> for UvSphere
where
    S: EuclideanSpace + FiniteDimensional<N = U3>,
{
    type State = Bounds<S>;
}

impl<S> AttributeVertexGenerator<Position<S>> for UvSphere
where
    S: EuclideanSpace + FiniteDimensional<N = U3>,
{
    type Output = S;

    fn vertex_count(&self) -> usize {
        (self.nv - 1) * self.nu + 2
    }

    fn vertex_from(&self, state: &Self::State, index: usize) -> Self::Output {
        let count = AttributeVertexGenerator::<Position<S>>::vertex_count(self);
        if index == 0 {
            self.vertex_with_position_from::<S>(state, 0, 0)
        }
        else if index == (count - 1) {
            self.vertex_with_position_from::<S>(state, 0, self.nv)
        }
        else {
            let index = index - 1;
            self.vertex_with_position_from::<S>(state, index % self.nu, (index / self.nu) + 1)
        }
    }
}

impl<S> AttributePolygonGenerator<Position<S>> for UvSphere
where
    S: EuclideanSpace + FiniteDimensional<N = U3>,
{
    type Output = BoundedPolygon<S>;

    fn polygon_from(&self, state: &Self::State, index: usize) -> Self::Output {
        // Prevent floating point rounding errors by wrapping the incremented
        // values for `(u, v)` into `(p, q)`. This is important for indexing
        // geometry, because small differences in the computation of spatial
        // vertices will produce redundant output vertices. There should be
        // exactly `(nv - 1) * nu + 2` unique values of `(u, v)` used to
        // generate positions.
        //
        // There are two important observations:
        //
        //   1. `u` must wrap, but `v` need not. There are `nu` meridians of
        //      points and polygons, but there are `nv` parallels of polygons
        //      and `nv + 1` parallels of points.
        //   2. `u`, which represents a meridian, is meaningless at the poles,
        //      and can be normalized to zero.
        let (u, v) = self.map_polygon_index(index);
        let (p, q) = ((u + 1) % self.nu, v + 1);

        // Generate the vertices at the requested meridian and parallel. The
        // lower bound of `(u, v)` is always used, so compute that in advance
        // (`lower`). Emit triangles at the poles, otherwise quadrilaterals.
        let lower = self.vertex_with_position_from(state, u, v);
        if v == 0 {
            Trigon::new(
                lower,
                self.vertex_with_position_from(state, u, q),
                self.vertex_with_position_from(state, p, q),
            )
            .into()
        }
        else if v == self.nv - 1 {
            Trigon::new(
                // Normalize `u` at the pole, using `(0, nv)` in place of
                // `(p, q)`.
                self.vertex_with_position_from(state, 0, self.nv),
                self.vertex_with_position_from(state, p, v),
                lower,
            )
            .into()
        }
        else {
            Tetragon::new(
                lower,
                self.vertex_with_position_from(state, u, q),
                self.vertex_with_position_from(state, p, q),
                self.vertex_with_position_from(state, p, v),
            )
            .into()
        }
    }
}

impl<S> IndexingPolygonGenerator<Position<S>> for UvSphere {
    type Output = BoundedPolygon<usize>;

    fn indexing_polygon(&self, index: usize) -> Self::Output {
        let (u, v) = self.map_polygon_index(index);
        let (p, q) = (u + 1, v + 1);

        let low = self.index_for_position(u, v);
        let high = self.index_for_position(p, q);
        if v == 0 {
            Trigon::new(low, self.index_for_position(u, q), high).into()
        }
        else if v == self.nv - 1 {
            Trigon::new(high, self.index_for_position(p, v), low).into()
        }
        else {
            Tetragon::new(
                low,
                self.index_for_position(u, q),
                high,
                self.index_for_position(p, v),
            )
            .into()
        }
    }
}

impl Generator for UvSphere {}

fn into_scalar<T, S>(value: T) -> Scalar<S>
where
    T: ToPrimitive,
    S: EuclideanSpace,
{
    <Scalar<S> as NumCast>::from(value).unwrap()
}

#[cfg(test)]
mod tests {
    use nalgebra::Point3;
    use std::collections::BTreeSet;

    use crate::prelude::*;
    use crate::primitive::generate::Position;
    use crate::primitive::sphere::UvSphere;

    type E3 = Point3<f64>;

    #[test]
    fn vertex_count() {
        assert_eq!(
            5,
            UvSphere::new(3, 2)
                .vertices::<Position<E3>>() // 5 conjoint vertices.
                .count()
        );
    }

    #[test]
    fn polygon_vertex_count() {
        assert_eq!(
            18,
            UvSphere::new(3, 2)
                .polygons::<Position<E3>>() // 6 triangles, 18 vertices.
                .vertices()
                .count()
        );
    }

    #[test]
    fn position_index_to_vertex_mapping() {
        assert_eq!(
            5,
            UvSphere::new(3, 2)
                .indexing_polygons::<Position>() // 18 vertices, 5 indices.
                .vertices()
                .collect::<BTreeSet<_>>()
                .len()
        )
    }
}