gausplat_loader/source/polygon/header/
mod.rs

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
//! Polygon header module.

pub mod decode;
pub mod encode;
pub mod property;

pub use super::*;
pub use indexmap::IndexMap;
pub use property::*;

use derive_more::derive::{
    AsRef, Constructor, Deref, DerefMut, Display, From, IntoIterator, IsVariant,
    TryUnwrap,
};
use std::{fmt, str::FromStr};
use Error::*;
use Format::*;

/// Polygon element.
#[derive(
    AsRef,
    Clone,
    Constructor,
    Debug,
    Default,
    Deref,
    DerefMut,
    Display,
    Eq,
    From,
    IntoIterator,
    PartialEq,
)]
#[display("element {name} {count}\n{properties}")]
#[from((usize, String, Properties), (usize, &str, IndexMap<String, Property>))]
pub struct Element {
    /// Element count.
    pub count: usize,
    /// Element name.
    pub name: String,
    /// Owned [`Properties`].
    #[deref]
    #[deref_mut]
    #[into_iterator(owned, ref, ref_mut)]
    pub properties: Properties,
}

/// A map of [`Element::name`] to [`Element`].
#[derive(
    AsRef,
    Clone,
    Constructor,
    Debug,
    Deref,
    DerefMut,
    Default,
    Eq,
    From,
    IntoIterator,
    PartialEq,
)]
pub struct Elements {
    /// Inner map.
    #[into_iterator(owned, ref, ref_mut)]
    pub inner: IndexMap<String, Element>,
}

/// Polygon format.
#[derive(
    Clone, Copy, Debug, Default, Display, Eq, From, Hash, IsVariant, PartialEq, TryUnwrap,
)]
#[try_unwrap(owned, ref, ref_mut)]
pub enum Format {
    /// Binary little endian format.
    #[default]
    #[display("binary_little_endian")]
    BinaryLittleEndian,
    /// ASCII format.
    #[display("ascii")]
    Ascii,
    /// Binary big endian format.
    #[display("binary_big_endian")]
    BinaryBigEndian,
}

/// Polygon header.
#[derive(
    AsRef,
    Clone,
    Constructor,
    Debug,
    Deref,
    DerefMut,
    Display,
    Eq,
    From,
    IntoIterator,
    PartialEq,
)]
#[display("ply\nformat {format} {version}\n{elements}end_header\n")]
#[from((Elements, Format, String), (IndexMap<String, Element>, Format, &str))]
pub struct Header {
    /// Owned [`Elements`].
    #[deref]
    #[deref_mut]
    #[into_iterator(owned, ref, ref_mut)]
    pub elements: Elements,
    /// Format.
    pub format: Format,
    /// Version.
    pub version: String,
}

impl Elements {
    /// Check if the two elements have the same structure.
    ///
    /// It can be used for checking the compatibility of two polygon objects.
    #[inline]
    pub fn is_same_order(
        &self,
        other: &Self,
    ) -> bool {
        self.len().eq(&other.len())
            && self
                .iter()
                .zip(other.iter())
                .all(|(a, b)| a.0 == b.0 && a.1.is_same_order(b.1))
    }
}

impl Format {
    /// Check if the format is binary little endian.
    #[inline]
    pub const fn is_binary_native_endian(&self) -> bool {
        #[cfg(target_endian = "big")]
        return self.is_binary_big_endian();
        #[cfg(target_endian = "little")]
        return self.is_binary_little_endian();
    }

    /// Return the binary format with native endianness.
    #[inline]
    pub const fn binary_native_endian() -> Self {
        #[cfg(target_endian = "big")]
        return Self::BinaryBigEndian;
        #[cfg(target_endian = "little")]
        return Self::BinaryLittleEndian;
    }
}

impl Default for Header {
    #[inline]
    fn default() -> Self {
        Header {
            format: Default::default(),
            elements: Default::default(),
            version: "1.0".into(),
        }
    }
}

impl fmt::Display for Elements {
    #[inline]
    fn fmt(
        &self,
        f: &mut fmt::Formatter<'_>,
    ) -> fmt::Result {
        self.values().try_for_each(|e| write!(f, "{e}"))
    }
}

impl FromStr for Header {
    type Err = Error;

    #[inline]
    fn from_str(input: &str) -> Result<Self, Self::Err> {
        Header::decode(&mut std::io::Cursor::new(input.as_bytes()))
    }
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    #[test]
    fn default() {
        use super::*;

        let taret = true;
        let output = &PropertyKind::default();
        assert_eq!(output.is_scalar(), taret);

        let target = true;
        let output = output.to_string().is_empty();
        assert_eq!(output, target);

        let target = "ply\nformat binary_little_endian 1.0\nend_header\n";
        let output = Header::default().to_string();
        assert_eq!(output, target);

        Header::decode(&mut Cursor::new(target)).unwrap();
    }

    #[test]
    fn is_same_order() {
        use super::*;

        let target = true;
        let output = Elements::default().is_same_order(&Elements::default());
        assert_eq!(output, target);

        let target = false;
        let mut elements = (
            "ply\n\
            format binary_little_endian 1.0\n\
            element vertex 365\n\
            property float x\n\
            property float y\n\
            end_header\n"
                .parse::<Header>()
                .unwrap()
                .elements,
            "ply\n\
            format binary_little_endian 1.0\n\
            element vertex 900\n\
            property float y\n\
            property float x\n\
            end_header\n"
                .parse::<Header>()
                .unwrap()
                .elements,
        );

        let output = elements.0.is_same_order(&elements.1);
        assert_eq!(output, target);

        let target = true;
        elements
            .1
            .get_mut("vertex")
            .unwrap()
            .properties
            .swap_indices(0, 1);
        let output = elements.0.is_same_order(&elements.1);
        assert_eq!(output, target);
    }

    #[test]
    fn format_on_native_endian() {
        use super::*;

        #[cfg(target_endian = "big")]
        {
            let target = Format::BinaryBigEndian;
            let output = Format::binary_native_endian();
            assert_eq!(output, target);
        }
        #[cfg(target_endian = "little")]
        {
            let target = Format::BinaryLittleEndian;
            let output = Format::binary_native_endian();
            assert_eq!(output, target);
        }
    }
}