Skip to content

Commit

Permalink
Mirror the read API to create a mesh file (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
MJohnson459 committed Aug 7, 2023
1 parent 78af95f commit 40607b7
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion src/input/polyanya_file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{self, BufRead, Read};
use std::io::{self, BufRead, Read, Write};

use glam::Vec2;

Expand Down Expand Up @@ -81,10 +81,73 @@ impl PolyanyaFile {
}
mesh
}

/// Write a `Mesh` to a file in the format `mesh 2`.
///
/// See <https://github.com/vleue/polyanya/blob/main/meshes/format.txt> for format description.
pub fn to_file(&self, path: &str) {
let mut file = std::fs::File::create(path).unwrap();
let bytes = self.to_bytes();
file.write_all(&bytes).unwrap();
}

/// Write a `Mesh` to bytes in the format `mesh 2`.
///
/// See <https://github.com/vleue/polyanya/blob/main/meshes/format.txt> for format description.
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();

bytes.extend_from_slice(b"mesh\n");
bytes.extend_from_slice(b"2\n");
bytes.extend_from_slice(
format!("{} {}\n", self.vertices.len(), self.polygons.len()).as_bytes(),
);
for vertex in &self.vertices {
bytes.extend_from_slice(
format!(
"{} {} {}\n",
vertex.coords.x,
vertex.coords.y,
vertex
.polygons
.iter()
.map(|n| n.to_string())
.collect::<Vec<_>>()
.join(" ")
)
.as_bytes(),
);
}
for polygon in &self.polygons {
bytes.extend_from_slice(
format!(
"{} {}\n",
polygon.vertices.len(),
polygon
.vertices
.iter()
.map(|v| v.to_string())
.collect::<Vec<String>>()
.join(" ")
)
.as_bytes(),
);
}
bytes
}
}

impl From<PolyanyaFile> for Mesh {
fn from(value: PolyanyaFile) -> Self {
Mesh::new(value.vertices, value.polygons)
}
}

impl From<Mesh> for PolyanyaFile {
fn from(mesh: Mesh) -> Self {
PolyanyaFile {
vertices: mesh.vertices,
polygons: mesh.polygons,
}
}
}

0 comments on commit 40607b7

Please sign in to comment.