After deadline commit

This commit is contained in:
Thomas Forgione
2018-04-10 13:37:08 +02:00
parent e508e01359
commit 5cc6196523
4 changed files with 33 additions and 20 deletions
+1 -1
View File
@@ -49,5 +49,5 @@ pub trait ElementSerializer {
/// Performs the serialization.
///
/// Writes the serialization on the output and returns Err if an error occured.
fn serialize<W: Write>(&self, element: Element, output: &mut W) -> Result<(), ExportError>;
fn serialize<W: Write>(&self, element: Element, output: &mut W) -> Result<usize, ExportError>;
}
+20 -19
View File
@@ -24,31 +24,31 @@ impl ObjElementSerializer {
}
/// Writes a face to a writable output.
pub fn write_face<W: Write>(&self, f: model::Face, output: &mut W) -> Result<(), ExportError> {
pub fn face_to_string(&self, f: model::Face) -> String {
if f.a.texture_coordinate.is_none() && f.a.normal.is_none() {
// Only vertices
writeln!(output, "f {} {} {}", f.a.vertex + 1, f.b.vertex + 1, f.c.vertex + 1)?;
format!("f {} {} {}", f.a.vertex + 1, f.b.vertex + 1, f.c.vertex + 1)
} else if f.a.normal.is_none() {
// Vertices + tex coords
writeln!(output, "f {}/{} {}/{} {}/{}",
format!("f {}/{} {}/{} {}/{}",
f.a.vertex + 1, f.a.texture_coordinate.unwrap() + 1,
f.b.vertex + 1, f.b.texture_coordinate.unwrap() + 1,
f.c.vertex + 1, f.c.texture_coordinate.unwrap() + 1)?;
f.c.vertex + 1, f.c.texture_coordinate.unwrap() + 1)
} else if f.a.texture_coordinate.is_none() {
// Vertices + normals
writeln!(output, "f {}//{} {}//{} {}//{}",
format!("f {}//{} {}//{} {}//{}",
f.a.vertex + 1, f.a.normal.unwrap() + 1,
f.b.vertex + 1, f.b.normal.unwrap() + 1,
f.c.vertex + 1, f.c.normal.unwrap() + 1)?;
f.c.vertex + 1, f.c.normal.unwrap() + 1)
} else {
// All
writeln!(output, "f {}/{}/{} {}/{}/{} {}/{}/{}",
format!("f {}/{}/{} {}/{}/{} {}/{}/{}",
f.a.vertex + 1, f.a.texture_coordinate.unwrap() + 1, f.a.normal.unwrap() + 1,
f.b.vertex + 1, f.b.texture_coordinate.unwrap() + 1, f.b.normal.unwrap() + 1,
f.c.vertex + 1, f.c.texture_coordinate.unwrap() + 1, f.c.normal.unwrap() + 1)?;
f.c.vertex + 1, f.c.texture_coordinate.unwrap() + 1, f.c.normal.unwrap() + 1)
}
Ok(())
}
}
@@ -67,17 +67,18 @@ impl ElementSerializer for ObjElementSerializer {
/// let result = String::from_utf8(output).unwrap();
/// assert_eq!(result, "v 1 2 3\n");
/// ```
fn serialize<W: Write>(&self, element: Element, output: &mut W) -> Result<(), ExportError> {
fn serialize<W: Write>(&self, element: Element, output: &mut W) -> Result<usize, ExportError> {
match element {
Vertex(v) => writeln!(output, "v {} {} {}", v.x(), v.y(), v.z())?,
TextureCoordinate(vt) => writeln!(output, "vt {} {}", vt.x(), vt.y())?,
Normal(n) => writeln!(output, "vn {} {} {}", n.x(), n.y(), n.z())?,
Face(f) => self.write_face(f, output)?,
UseMaterial(ref n) => writeln!(output, "usemtl {}", n)?,
_ => (),
}
let string = match element {
Vertex(v) => format!("v {} {} {}", v.x(), v.y(), v.z()),
TextureCoordinate(vt) => format!("vt {} {}", vt.x(), vt.y()),
Normal(n) => format!("vn {} {} {}", n.x(), n.y(), n.z()),
Face(f) => self.face_to_string(f),
UseMaterial(ref n) => format!("usemtl {}", n),
_ => return Ok(0),
};
Ok(())
writeln!(output, "{}", string)?;
Ok(string.len() + 1)
}
}