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
//!
//! Contain geometry asset definitions.
//!

mod point_cloud;
pub use point_cloud::*;

mod tri_mesh;
pub use tri_mesh::*;

pub use crate::prelude::*;

///
/// An array of indices. Supports different data types.
///
#[derive(Clone, Debug)]
pub enum Indices {
    /// Do not use indices, ie. the faces are all unconnected.
    None,
    /// Uses unsigned 8 bit integer for each index.
    U8(Vec<u8>),
    /// Uses unsigned 16 bit integer for each index.
    U16(Vec<u16>),
    /// Uses unsigned 32 bit integer for each index.
    U32(Vec<u32>),
}

impl Indices {
    ///
    /// Converts all the indices as `u32` data type.
    ///
    pub fn into_u32(self) -> Option<Vec<u32>> {
        match self {
            Self::None => None,
            Self::U8(mut values) => Some(values.drain(..).map(|i| i as u32).collect::<Vec<_>>()),
            Self::U16(mut values) => Some(values.drain(..).map(|i| i as u32).collect::<Vec<_>>()),
            Self::U32(values) => Some(values),
        }
    }

    ///
    /// Clones and converts all the indices as `u32` data type.
    ///
    pub fn to_u32(&self) -> Option<Vec<u32>> {
        match self {
            Self::None => None,
            Self::U8(values) => Some(values.iter().map(|i| *i as u32).collect::<Vec<_>>()),
            Self::U16(values) => Some(values.iter().map(|i| *i as u32).collect::<Vec<_>>()),
            Self::U32(values) => Some(values.clone()),
        }
    }

    ///
    /// Returns the number of indices.
    ///
    pub fn len(&self) -> Option<usize> {
        match self {
            Self::None => None,
            Self::U8(values) => Some(values.len()),
            Self::U16(values) => Some(values.len()),
            Self::U32(values) => Some(values.len()),
        }
    }
}

impl std::default::Default for Indices {
    fn default() -> Self {
        Self::None
    }
}

///
/// An array of positions. Supports f32 and f64 data types.
///
#[derive(Clone)]
pub enum Positions {
    /// Uses 32 bit float for the vertex positions.
    F32(Vec<Vec3>),
    /// Uses 64 bit float for the vertex positions.
    F64(Vec<Vector3<f64>>),
}

impl Positions {
    ///
    /// Converts and returns all the positions as `f32` data type.
    ///
    pub fn into_f32(self) -> Vec<Vec3> {
        match self {
            Self::F32(values) => values,
            Self::F64(mut values) => values
                .drain(..)
                .map(|v| Vec3::new(v.x as f32, v.y as f32, v.z as f32))
                .collect::<Vec<_>>(),
        }
    }

    ///
    /// Clones and converts all the positions as `f32` data type.
    ///
    pub fn to_f32(&self) -> Vec<Vec3> {
        match self {
            Self::F32(values) => values.clone(),
            Self::F64(values) => values
                .iter()
                .map(|v| Vec3::new(v.x as f32, v.y as f32, v.z as f32))
                .collect::<Vec<_>>(),
        }
    }
    ///
    /// Converts and returns all the positions as `f64` data type.
    ///
    pub fn into_f64(self) -> Vec<Vector3<f64>> {
        match self {
            Self::F32(mut values) => values
                .drain(..)
                .map(|v| Vector3::new(v.x as f64, v.y as f64, v.z as f64))
                .collect::<Vec<_>>(),
            Self::F64(values) => values,
        }
    }

    ///
    /// Clones and converts all the positions as `f64` data type.
    ///
    pub fn to_f64(&self) -> Vec<Vector3<f64>> {
        match self {
            Self::F32(values) => values
                .iter()
                .map(|v| Vector3::new(v.x as f64, v.y as f64, v.z as f64))
                .collect::<Vec<_>>(),
            Self::F64(values) => values.clone(),
        }
    }

    ///
    /// Returns the number of positions.
    ///
    pub fn len(&self) -> usize {
        match self {
            Self::F32(values) => values.len(),
            Self::F64(values) => values.len(),
        }
    }
}

impl std::default::Default for Positions {
    fn default() -> Self {
        Self::F32(Vec::new())
    }
}

impl std::fmt::Debug for Positions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut d = f.debug_struct("Positions");
        match self {
            Self::F32(ind) => d.field("f32", &ind.len()),
            Self::F64(ind) => d.field("f64", &ind.len()),
        };
        d.finish()
    }
}