After deadline commit

This commit is contained in:
Thomas Forgione 2018-04-10 13:37:08 +02:00
parent e508e01359
commit 5cc6196523
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
4 changed files with 33 additions and 20 deletions

View File

@ -49,5 +49,5 @@ pub trait ElementSerializer {
/// Performs the serialization. /// Performs the serialization.
/// ///
/// Writes the serialization on the output and returns Err if an error occured. /// 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>;
} }

View File

@ -24,31 +24,31 @@ impl ObjElementSerializer {
} }
/// Writes a face to a writable output. /// 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() { if f.a.texture_coordinate.is_none() && f.a.normal.is_none() {
// Only vertices // 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() { } else if f.a.normal.is_none() {
// Vertices + tex coords // Vertices + tex coords
writeln!(output, "f {}/{} {}/{} {}/{}", format!("f {}/{} {}/{} {}/{}",
f.a.vertex + 1, f.a.texture_coordinate.unwrap() + 1, f.a.vertex + 1, f.a.texture_coordinate.unwrap() + 1,
f.b.vertex + 1, f.b.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() { } else if f.a.texture_coordinate.is_none() {
// Vertices + normals // Vertices + normals
writeln!(output, "f {}//{} {}//{} {}//{}", format!("f {}//{} {}//{} {}//{}",
f.a.vertex + 1, f.a.normal.unwrap() + 1, f.a.vertex + 1, f.a.normal.unwrap() + 1,
f.b.vertex + 1, f.b.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 { } else {
// All // All
writeln!(output, "f {}/{}/{} {}/{}/{} {}/{}/{}", format!("f {}/{}/{} {}/{}/{} {}/{}/{}",
f.a.vertex + 1, f.a.texture_coordinate.unwrap() + 1, f.a.normal.unwrap() + 1, 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.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(); /// let result = String::from_utf8(output).unwrap();
/// assert_eq!(result, "v 1 2 3\n"); /// 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 { let string = match element {
Vertex(v) => writeln!(output, "v {} {} {}", v.x(), v.y(), v.z())?, Vertex(v) => format!("v {} {} {}", v.x(), v.y(), v.z()),
TextureCoordinate(vt) => writeln!(output, "vt {} {}", vt.x(), vt.y())?, TextureCoordinate(vt) => format!("vt {} {}", vt.x(), vt.y()),
Normal(n) => writeln!(output, "vn {} {} {}", n.x(), n.y(), n.z())?, Normal(n) => format!("vn {} {} {}", n.x(), n.y(), n.z()),
Face(f) => self.write_face(f, output)?, Face(f) => self.face_to_string(f),
UseMaterial(ref n) => writeln!(output, "usemtl {}", n)?, UseMaterial(ref n) => format!("usemtl {}", n),
_ => (), _ => return Ok(0),
} };
Ok(()) writeln!(output, "{}", string)?;
Ok(string.len() + 1)
} }
} }

View File

@ -112,6 +112,11 @@ macro_rules! make_vector {
*self /= self.norm() *self /= self.norm()
} }
/// Returns a new normalized vector.
pub fn normalized(&self) -> $name<T> {
*self / self.norm()
}
} }
impl<T: Number> Add for $name<T> { impl<T: Number> Add for $name<T> {
@ -224,6 +229,7 @@ make_vector!(Vector3, 3, (T, T, T), (x, x_mut, 0), (y, y_mut, 1), (z, z_mut, 2))
make_vector!(Vector4, 4, (T, T, T, T), (x, x_mut, 0), (y, y_mut, 1), (z, z_mut, 2), (t, t_mut, 3)); make_vector!(Vector4, 4, (T, T, T, T), (x, x_mut, 0), (y, y_mut, 1), (z, z_mut, 2), (t, t_mut, 3));
impl Vector2<f32> { impl Vector2<f32> {
/// Returns a orthogonal vector to the one passed as parameter.
pub fn orthogonal(&self) -> Vector2<f32> { pub fn orthogonal(&self) -> Vector2<f32> {
Vector2::new( Vector2::new(
self.y(), self.y(),
@ -233,6 +239,7 @@ impl Vector2<f32> {
} }
impl Vector2<i32> { impl Vector2<i32> {
/// Returns a orthogonal vector to the one passed as parameter.
pub fn orthogonal(&self) -> Vector2<i32> { pub fn orthogonal(&self) -> Vector2<i32> {
Vector2::new( Vector2::new(
self.y(), self.y(),

View File

@ -175,6 +175,9 @@ pub trait Parser {
self.priv_parse_into_model(model, path) self.priv_parse_into_model(model, path)
} }
/// Parses a file and adds its content to an already existing model.
///
/// This is the method you should implement for you format parser.
fn priv_parse_into_model(&mut self, model: &mut Model, path: &str) -> Result<(), ParserError>; fn priv_parse_into_model(&mut self, model: &mut Model, path: &str) -> Result<(), ParserError>;
} }
@ -205,6 +208,8 @@ impl<LP: LineParser> Parser for LP {
use std::io::BufRead; use std::io::BufRead;
for (num, line) in file.lines().enumerate() { for (num, line) in file.lines().enumerate() {
let num = num + 1;
if line.is_err() { if line.is_err() {
return Err(ParserError::OtherError(path.to_owned())); return Err(ParserError::OtherError(path.to_owned()));
} }