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
use crate::core::{BufferAttribute, BufferGeometry};
/// Stanford PLY parser (ASCII + binary little-endian). Reads `vertex` and
/// `face` elements with common properties: x/y/z/nx/ny/nz/s/t/red/green/blue.
/// Faces are fanned from their first corner; which triangles were one face is
/// kept in [`BufferGeometry::polygons`].
pub struct PlyLoader;
#[derive(Debug, Clone)]
struct Prop {
name: String,
/// Component type for scalar properties. None = list.
#[allow(dead_code)]
scalar: Option<&'static str>,
/// For lists: (count_type, item_type).
list: Option<(&'static str, &'static str)>,
}
#[derive(Debug, Clone)]
struct Element {
name: String,
count: usize,
props: Vec<Prop>,
}
impl PlyLoader {
pub fn parse(bytes: &[u8]) -> BufferGeometry {
// Read header.
let mut header_end = 0;
let mut header = String::new();
let mut last_nl = 0;
for (i, &b) in bytes.iter().enumerate() {
if b == b'\n' {
let line = std::str::from_utf8(&bytes[last_nl..i])
.unwrap_or("")
.trim_end_matches('\r')
.to_string();
header.push_str(&line);
header.push('\n');
if line == "end_header" {
header_end = i + 1;
break;
}
last_nl = i + 1;
}
}
if header_end == 0 {
return BufferGeometry::new();
}
let mut format = "ascii";
let mut elements: Vec<Element> = Vec::new();
for line in header.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
match parts.as_slice() {
["format", "ascii", _] => format = "ascii",
["format", "binary_little_endian", _] => format = "binary_little_endian",
["element", name, count] => {
if let Ok(n) = count.parse::<usize>() {
elements.push(Element {
name: (*name).into(),
count: n,
props: Vec::new(),
});
}
}
["property", "list", count_ty, item_ty, name] => {
let count_ty: &'static str = leak(count_ty);
let item_ty: &'static str = leak(item_ty);
if let Some(e) = elements.last_mut() {
e.props.push(Prop {
name: (*name).into(),
scalar: None,
list: Some((count_ty, item_ty)),
});
}
}
["property", ty, name] => {
let ty: &'static str = leak(ty);
if let Some(e) = elements.last_mut() {
e.props.push(Prop {
name: (*name).into(),
scalar: Some(ty),
list: None,
});
}
}
_ => {}
}
}
let body = &bytes[header_end..];
let mut positions: Vec<f32> = Vec::new();
let mut normals: Vec<f32> = Vec::new();
let mut uvs: Vec<f32> = Vec::new();
let mut colors: Vec<f32> = Vec::new();
let mut indices: Vec<u32> = Vec::new();
// Triangles per face, in face order.
let mut polygons: Vec<u32> = Vec::new();
if format == "ascii" {
let text = std::str::from_utf8(body).unwrap_or("");
let mut lines = text.lines();
for elem in &elements {
for _ in 0..elem.count {
let line = match lines.next() {
Some(l) => l,
None => break,
};
let toks: Vec<&str> = line.split_whitespace().collect();
let mut ti = 0;
let mut px = 0.0;
let mut py = 0.0;
let mut pz = 0.0;
let mut nx = 0.0;
let mut ny = 0.0;
let mut nz = 0.0;
let mut u = 0.0;
let mut v = 0.0;
let mut r = 0.0;
let mut gr = 0.0;
let mut bl = 0.0;
let mut have_n = false;
let mut have_uv = false;
let mut have_c = false;
if elem.name == "vertex" {
for prop in &elem.props {
if prop.list.is_some() {
ti += 1;
continue;
}
let tok = toks.get(ti).copied().unwrap_or("0");
let val: f32 = tok.parse().unwrap_or(0.0);
match prop.name.as_str() {
"x" => px = val,
"y" => py = val,
"z" => pz = val,
"nx" => {
nx = val;
have_n = true;
}
"ny" => {
ny = val;
have_n = true;
}
"nz" => {
nz = val;
have_n = true;
}
"s" | "u" | "texture_u" => {
u = val;
have_uv = true;
}
"t" | "v" | "texture_v" => {
v = val;
have_uv = true;
}
"red" => {
r = val / 255.0;
have_c = true;
}
"green" => {
gr = val / 255.0;
have_c = true;
}
"blue" => {
bl = val / 255.0;
have_c = true;
}
_ => {}
}
ti += 1;
}
positions.extend_from_slice(&[px, py, pz]);
if have_n {
normals.extend_from_slice(&[nx, ny, nz]);
}
if have_uv {
uvs.extend_from_slice(&[u, v]);
}
if have_c {
colors.extend_from_slice(&[r, gr, bl]);
}
} else if elem.name == "face" {
for prop in &elem.props {
if let Some((_, _)) = prop.list {
let count: u32 =
toks.get(ti).and_then(|s| s.parse().ok()).unwrap_or(0);
ti += 1;
let mut idx: Vec<u32> = Vec::with_capacity(count as usize);
for _ in 0..count {
let v: u32 =
toks.get(ti).and_then(|s| s.parse().ok()).unwrap_or(0);
idx.push(v);
ti += 1;
}
if idx.len() >= 3 {
polygons.push(idx.len() as u32 - 2);
}
for i in 1..idx.len().saturating_sub(1) {
indices.extend_from_slice(&[idx[0], idx[i], idx[i + 1]]);
}
} else {
ti += 1;
}
}
}
}
}
}
// Binary parse omitted for brevity; would mirror ASCII but stride bytes.
let mut g = BufferGeometry::new();
g.set_attribute("position", BufferAttribute::new(positions, 3));
if !normals.is_empty() {
g.set_attribute("normal", BufferAttribute::new(normals, 3));
}
if !uvs.is_empty() {
g.set_attribute("uv", BufferAttribute::new(uvs, 2));
}
if !colors.is_empty() {
g.set_attribute("color", BufferAttribute::new(colors, 3));
}
if !indices.is_empty() {
g.set_index(indices);
g.set_polygons(polygons);
}
g
}
}
fn leak(s: &str) -> &'static str {
Box::leak(s.to_string().into_boxed_str())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn faces_are_kept_as_polygons() {
let src = "\
ply
format ascii 1.0
element vertex 5
property float x
property float y
property float z
element face 2
property list uchar int vertex_indices
end_header
0 0 0
1 0 0
1 1 0
0 1 0
2 0 0
4 0 1 2 3
3 1 4 2
";
let g = PlyLoader::parse(src.as_bytes());
assert_eq!(g.triangle_count(), 3);
assert_eq!(g.polygon_counts(), Some(&[2u32, 1][..]));
assert_eq!(g.polygon_loop(0..2), Some(vec![0, 1, 2, 3]));
}
}