|
| 1 | +use std::{fs::File, path::Path}; |
| 2 | + |
| 3 | +extern crate ply_rs; |
| 4 | +use ply_rs::{ |
| 5 | + self as ply, |
| 6 | + ply::{Property, PropertyAccess}, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::{Model3D, ModelError}; |
| 10 | + |
| 11 | +#[derive(Debug, Default, Clone, Copy)] |
| 12 | +struct Vertex { |
| 13 | + x: f32, |
| 14 | + y: f32, |
| 15 | + z: f32, |
| 16 | + x_norm: Option<f32>, |
| 17 | + y_norm: Option<f32>, |
| 18 | + z_norm: Option<f32>, |
| 19 | + tex_x: Option<f32>, |
| 20 | + tex_y: Option<f32>, |
| 21 | +} |
| 22 | + |
| 23 | +impl PropertyAccess for Vertex { |
| 24 | + fn new() -> Self { |
| 25 | + Vertex { |
| 26 | + ..Default::default() |
| 27 | + } |
| 28 | + } |
| 29 | + fn set_property(&mut self, key: String, property: Property) { |
| 30 | + match (key.as_ref(), property) { |
| 31 | + ("x", Property::Float(v)) => self.x = v, |
| 32 | + ("y", Property::Float(v)) => self.y = v, |
| 33 | + ("z", Property::Float(v)) => self.z = v, |
| 34 | + |
| 35 | + ("nx", Property::Float(v)) => self.x_norm = Some(v), |
| 36 | + ("ny", Property::Float(v)) => self.y_norm = Some(v), |
| 37 | + ("nz", Property::Float(v)) => self.z_norm = Some(v), |
| 38 | + // NOTE: Blender3D exports texture coordinates as s,t tuples |
| 39 | + ("u" | "s" | "tx" | "texture_u", Property::Float(v)) => self.tex_x = Some(v), |
| 40 | + ("v" | "t" | "ty" | "texture_v", Property::Float(v)) => self.tex_y = Some(v), |
| 41 | + (k, _) => eprintln!("Vertex: Unexpected key/value combination: key: {}", k), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Debug)] |
| 47 | +struct Face { |
| 48 | + vertex_index: Vec<u32>, |
| 49 | +} |
| 50 | + |
| 51 | +impl PropertyAccess for Face { |
| 52 | + fn new() -> Self { |
| 53 | + Face { |
| 54 | + vertex_index: Vec::new(), |
| 55 | + } |
| 56 | + } |
| 57 | + fn set_property(&mut self, key: String, property: Property) { |
| 58 | + match (key.as_ref(), property) { |
| 59 | + ("vertex_index" | "vertex_indices", Property::ListUInt(vec)) => self.vertex_index = vec, |
| 60 | + (k, _) => eprintln!("Face: Unexpected key/value combination: key: {}", k), |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +pub fn load(path: &Path) -> Result<Model3D, ModelError> { |
| 66 | + let mut file = File::open(path).map_err(|e| ModelError::OpenFile(e.to_string()))?; |
| 67 | + let mut reader = std::io::BufReader::new(&mut file); |
| 68 | + |
| 69 | + // Create a parser for each struct. Parsers are cheap objects. |
| 70 | + let vertex_parser = ply::parser::Parser::<Vertex>::new(); |
| 71 | + let face_parser = ply::parser::Parser::<Face>::new(); |
| 72 | + |
| 73 | + // lets first consume the header |
| 74 | + // We also could use `face_parser`, The configuration is a parser's only state. |
| 75 | + // The reading position only depends on `f`. |
| 76 | + let header = vertex_parser.read_header(&mut reader).unwrap(); |
| 77 | + |
| 78 | + // Depending on the header, read the data into our structs.. |
| 79 | + let mut vertex_list = Vec::new(); |
| 80 | + let mut face_list = Vec::new(); |
| 81 | + for (_ignore_key, element) in &header.elements { |
| 82 | + // we could also just parse them in sequence, but the file format might change |
| 83 | + match element.name.as_ref() { |
| 84 | + "vertex" => { |
| 85 | + vertex_list = vertex_parser |
| 86 | + .read_payload_for_element(&mut reader, element, &header) |
| 87 | + .unwrap(); |
| 88 | + } |
| 89 | + "face" => { |
| 90 | + face_list = face_parser |
| 91 | + .read_payload_for_element(&mut reader, element, &header) |
| 92 | + .unwrap(); |
| 93 | + } |
| 94 | + _ => panic!("Enexpeced element!"), |
| 95 | + } |
| 96 | + } |
| 97 | + let mut vertices = Vec::new(); |
| 98 | + for face in face_list { |
| 99 | + // Every face (triangle) has 3 Vertices |
| 100 | + let vertex = vertex_list[face.vertex_index[0] as usize]; |
| 101 | + let vertex1 = vertex_list[face.vertex_index[1] as usize]; |
| 102 | + let vertex2 = vertex_list[face.vertex_index[2] as usize]; |
| 103 | + |
| 104 | + let mut normal = None; |
| 105 | + let mut tex_coord = None; |
| 106 | + if let Some(x) = vertex.x_norm { |
| 107 | + if let Some(y) = vertex.z_norm { |
| 108 | + if let Some(z) = vertex.z_norm { |
| 109 | + normal = Some([x, y, z]) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + if let Some(x) = vertex.tex_x { |
| 114 | + if let Some(y) = vertex.tex_y { |
| 115 | + tex_coord = Some([x, y]) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + let v1 = crate::Vertex { |
| 120 | + position: [vertex.x, vertex.y, vertex.z], |
| 121 | + tex_coord, |
| 122 | + color: None, |
| 123 | + normal, |
| 124 | + }; |
| 125 | + if let Some(x) = vertex1.x_norm { |
| 126 | + if let Some(y) = vertex1.z_norm { |
| 127 | + if let Some(z) = vertex1.z_norm { |
| 128 | + normal = Some([x, y, z]) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + if let Some(x) = vertex1.tex_x { |
| 133 | + if let Some(y) = vertex1.tex_y { |
| 134 | + tex_coord = Some([x, y]) |
| 135 | + } |
| 136 | + } |
| 137 | + let v2 = crate::Vertex { |
| 138 | + position: [vertex1.x, vertex1.y, vertex1.z], |
| 139 | + tex_coord, |
| 140 | + color: None, |
| 141 | + normal, |
| 142 | + }; |
| 143 | + if let Some(x) = vertex2.x_norm { |
| 144 | + if let Some(y) = vertex2.z_norm { |
| 145 | + if let Some(z) = vertex2.z_norm { |
| 146 | + normal = Some([x, y, z]) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + if let Some(x) = vertex2.tex_x { |
| 151 | + if let Some(y) = vertex2.tex_y { |
| 152 | + tex_coord = Some([x, y]) |
| 153 | + } |
| 154 | + } |
| 155 | + let v3 = crate::Vertex { |
| 156 | + position: [vertex2.x, vertex2.y, vertex2.z], |
| 157 | + tex_coord, |
| 158 | + color: None, |
| 159 | + normal, |
| 160 | + }; |
| 161 | + |
| 162 | + vertices.push(v1); |
| 163 | + vertices.push(v2); |
| 164 | + vertices.push(v3); |
| 165 | + } |
| 166 | + let mesh = crate::Mesh { |
| 167 | + vertices, |
| 168 | + indices: None, |
| 169 | + material_index: None, |
| 170 | + mode: crate::RenderMode::TriangleFan, |
| 171 | + name: None, |
| 172 | + }; |
| 173 | + |
| 174 | + Ok(Model3D { |
| 175 | + meshes: vec![mesh], |
| 176 | + materials: vec![], |
| 177 | + format: crate::ModelFormat::PLY, |
| 178 | + }) |
| 179 | +} |
0 commit comments