Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input code reorg #25

Merged
merged 6 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions benches/baking.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use criterion::{criterion_group, criterion_main, Criterion};
use polyanya::Mesh;
use polyanya::{Mesh, PolyanyaFile};

fn baking(c: &mut Criterion) {
let mut mesh = Mesh::from_file("meshes/aurora-merged.mesh");

let mut mesh: Mesh = PolyanyaFile::from_file("meshes/aurora-merged.mesh").into();
c.bench_function(&"baking".to_string(), |b| {
b.iter(|| {
mesh.unbake();
Expand Down
8 changes: 3 additions & 5 deletions benches/is_in_mesh.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use glam::Vec2;
use polyanya::Mesh;
use polyanya::{Mesh, PolyanyaFile};

fn is_in_mesh(c: &mut Criterion) {
let mesh = Mesh::from_file("meshes/aurora-merged.mesh");

let mesh: Mesh = PolyanyaFile::from_file("meshes/aurora-merged.mesh").into();
[
Vec2::new(575., 410.),
Vec2::new(728., 148.),
Expand All @@ -24,8 +23,7 @@ fn is_in_mesh(c: &mut Criterion) {
}

fn is_not_in_mesh(c: &mut Criterion) {
let mesh = Mesh::from_file("meshes/aurora-merged.mesh");

let mesh: Mesh = PolyanyaFile::from_file("meshes/aurora-merged.mesh").into();
[
Vec2::new(0., 0.),
Vec2::new(297., 438.),
Expand Down
5 changes: 2 additions & 3 deletions benches/no_path.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use glam::Vec2;
use polyanya::Mesh;
use polyanya::{Mesh, PolyanyaFile};

fn no_path(c: &mut Criterion) {
let mesh = Mesh::from_file("meshes/aurora-merged.mesh");

let mesh: Mesh = PolyanyaFile::from_file("meshes/aurora-merged.mesh").into();
[
(Vec2::new(0.0, 0.0), Vec2::new(0.0, 0.0)),
(Vec2::new(0.0, 0.0), Vec2::new(575.0, 410.0)),
Expand Down
5 changes: 2 additions & 3 deletions benches/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use glam::Vec2;
use polyanya::Mesh;
use polyanya::{Mesh, PolyanyaFile};

macro_rules! assert_delta {
($x:expr, $y:expr) => {
Expand All @@ -13,8 +13,7 @@ macro_rules! assert_delta {
}

fn get_path(c: &mut Criterion) {
let mesh = Mesh::from_file("meshes/aurora-merged.mesh");

let mesh: Mesh = PolyanyaFile::from_file("meshes/aurora-merged.mesh").into();
[
(Vec2::new(993.0, 290.0), Vec2::new(34.0, 622.0), 1123.2226),
(Vec2::new(356.0, 166.0), Vec2::new(661.0, 441.0), 595.041),
Expand Down
5 changes: 2 additions & 3 deletions examples/aurora.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use glam::Vec2;
use polyanya::Mesh;
use polyanya::{Mesh, PolyanyaFile};

macro_rules! assert_delta {
($x:expr, $y:expr) => {
Expand All @@ -18,8 +18,7 @@ fn main() {
)
.expect("set up the subscriber");

let mesh = Mesh::from_file("meshes/aurora-merged.mesh");

let mesh: Mesh = PolyanyaFile::from_file("meshes/aurora-merged.mesh").into();
assert_delta!(
mesh.path(Vec2::new(993.0, 290.0), Vec2::new(34.0, 622.0))
.unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions examples/scenario_runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::{self, BufRead};

use glam::Vec2;
use polyanya::Mesh;
use polyanya::{Mesh, PolyanyaFile};

struct Scenario {
from: Vec2,
Expand Down Expand Up @@ -48,7 +48,7 @@ fn main() {
let mut args = std::env::args();
args.next();

let mesh = Mesh::from_file(&args.next().unwrap());
let mesh: Mesh = PolyanyaFile::from_file(&args.next().unwrap()).into();

for scenario in Scenarios::from_file(&args.next().unwrap()).0 {
mesh.path(scenario.from, scenario.to).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/tests-aurora-merged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {}
#[cfg(test)]
use glam::Vec2;
#[cfg(test)]
use polyanya::Mesh;
use polyanya::{Mesh, PolyanyaFile};

#[cfg(test)]
macro_rules! assert_delta {
Expand All @@ -17,7 +17,7 @@ macro_rules! assert_delta {

#[cfg(test)]
fn aurora_mesh() -> Mesh {
Mesh::from_file("meshes/aurora-merged.mesh".into())
PolyanyaFile::from_file("meshes/aurora-merged.mesh").into()
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions examples/tests-aurora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {}
#[cfg(test)]
use glam::Vec2;
#[cfg(test)]
use polyanya::Mesh;
use polyanya::{Mesh, PolyanyaFile};

#[cfg(test)]
macro_rules! assert_delta {
Expand All @@ -17,7 +17,7 @@ macro_rules! assert_delta {

#[cfg(test)]
fn aurora_mesh() -> Mesh {
Mesh::from_file("meshes/aurora.mesh".into())
PolyanyaFile::from_file("meshes/aurora.mesh").into()
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod polyanya_file;
pub mod trimesh;
90 changes: 90 additions & 0 deletions src/input/polyanya_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::io::{self, BufRead, Read};

use glam::Vec2;

use crate::{Mesh, Polygon, Vertex};

/// A mesh read from a Polyanya file in the format `mesh 2`.
///
/// See <https://github.com/vleue/polyanya/blob/main/meshes/format.txt> for format description.
#[derive(Debug)]
pub struct PolyanyaFile {
/// List of vertex described in the file
pub vertices: Vec<Vertex>,
/// List of polygon described in the file
pub polygons: Vec<Polygon>,
}

impl PolyanyaFile {
/// Create a `Mesh` from a file in the format `mesh 2`.
///
/// See <https://github.com/vleue/polyanya/blob/main/meshes/format.txt> for format description.
pub fn from_file(path: &str) -> PolyanyaFile {
let mut file = std::fs::File::open(path).unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();
Self::from_bytes(&buffer)
}

/// Create a `Mesh` from bytes in the format `mesh 2`.
///
/// See <https://github.com/vleue/polyanya/blob/main/meshes/format.txt> for format description.
pub fn from_bytes(bytes: &[u8]) -> PolyanyaFile {
let mut mesh = PolyanyaFile {
vertices: vec![],
polygons: vec![],
};
let mut nb_vertices = 0;
let mut nb_polygons = 0;
let mut phase = 0;
for line in io::BufReader::new(bytes).lines() {
let line: String = line.unwrap();
if phase == 0 {
if line == "mesh" || line == "2" {
continue;
} else {
(nb_vertices, nb_polygons) = line
.split_once(' ')
.map(|(a, b)| (a.parse().unwrap(), b.parse().unwrap()))
.unwrap();
phase = 1;
continue;
}
}
if phase == 1 {
if nb_vertices > 0 {
nb_vertices -= 1;
let mut values = line.split(' ');
let x = values.next().unwrap().parse().unwrap();
let y = values.next().unwrap().parse().unwrap();
let _ = values.next();
let vertex = Vertex::new(
Vec2::new(x, y),
values.map(|v| v.parse().unwrap()).collect(),
);
mesh.vertices.push(vertex);
} else {
phase = 2;
}
}
if phase == 2 {
if nb_polygons > 0 {
nb_polygons -= 1;
let mut values = line.split(' ');
let n = values.next().unwrap().parse().unwrap();
let polygon = Polygon::using(n, values.map(|v| v.parse().unwrap()).collect());
mesh.polygons.push(polygon)
} else {
panic!("unexpected line");
}
}
}
mesh
}
}

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