Skip to content

Commit 16ac573

Browse files
committed
Moved file loading out of main
1 parent e19e3cb commit 16ac573

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

src/inputs.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use clap::{App, Arg, ArgMatches, SubCommand};
2-
use crate::geometry::{SimpleMesh, ToSimpleMeshWithMaterial};
2+
use crate::geometry::{SimpleMesh, ToSimpleMeshWithMaterial, ToSimpleMesh};
33
use crate::context::{Context};
4+
use std::fs::OpenOptions;
5+
use std::path::Path;
46
use std::error::Error;
57

68
pub fn cli_matches<'a>() -> ArgMatches<'a> {
@@ -84,6 +86,39 @@ pub fn to_meshes(models: Vec<tobj::Model>, materials: Vec<tobj::Material>) -> Ve
8486
meshes
8587
}
8688

89+
pub fn match_meshes(matches: &ArgMatches) -> Result<Vec<SimpleMesh>, Box<Error>> {
90+
let mut mesh_queue: Vec<SimpleMesh> = vec![];
91+
for slice in matches.value_of("INPUT FILENAME").unwrap().split(' ') {
92+
let error = |s: &str, e: &str| -> Result<Vec<SimpleMesh>, Box<Error>> {
93+
Err(format!("filename: [{}] couldn't load, {}. {}", slice, s, e).into())
94+
};
95+
// Fill list with file inputs (Splits for spaces -> multiple files)
96+
let path = Path::new(slice);
97+
let mut meshes = match path.extension() {
98+
None => error("couldn't determine filename extension", ""),
99+
Some(ext) => match ext.to_str() {
100+
None => error("couldn't parse filename extension", ""),
101+
Some(extstr) => match &*extstr.to_lowercase() {
102+
"obj" => match tobj::load_obj(&path) {
103+
Err(e) => error("tobj couldnt load/parse OBJ", &e.to_string()),
104+
Ok(present) => Ok(to_meshes(present.0, present.1)),
105+
},
106+
"stl" => match OpenOptions::new().read(true).open(&path) {
107+
Err(e) => error("STL load failed", &e.to_string()),
108+
Ok(mut file) => match stl_io::read_stl(&mut file) {
109+
Err(e) => error("stl_io couldnt parse STL", &e.to_string()),
110+
Ok(stlio_mesh) => Ok(vec![stlio_mesh.to_simple_mesh()]),
111+
},
112+
},
113+
_ => error("unknown filename extension", ""),
114+
},
115+
},
116+
};
117+
mesh_queue.append(&mut meshes.unwrap());
118+
}
119+
Ok(mesh_queue)
120+
}
121+
87122
pub fn match_turntable(matches: &ArgMatches) -> Result<(f32, f32, f32, f32), Box<Error>> {
88123
let mut turntable = (0.0, 0.0, 0.0, 0.0);
89124
if let Some(x) = matches.value_of("x") {

src/main.rs

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crossterm::{cursor, Crossterm, InputEvent, KeyEvent, RawScreen};
22
use std::error::Error;
33
use std::f32;
4-
use std::fs::OpenOptions;
54
use std::io::{stdout, Write};
6-
use std::path::Path;
75
use std::time::Instant;
86

97
use nalgebra::Rotation3;
@@ -22,37 +20,8 @@ pub use inputs::*;
2220

2321
fn main() -> Result<(), Box<Error>> {
2422
let matches = cli_matches(); // Read command line arguments
25-
let mut mesh_queue: Vec<SimpleMesh> = vec![]; // A list of meshes to render
26-
for slice in matches.value_of("INPUT FILENAME").unwrap().split(' ') {
27-
let error = |s: &str, e: &str| -> Vec<SimpleMesh> {
28-
println!("filename: [{}] couldn't load, {}. {}", slice, s, e);
29-
vec![]
30-
};
31-
// Fill list with file inputs (Splits for spaces -> multiple files)
32-
let path = Path::new(slice);
33-
let mut meshes = match path.extension() {
34-
None => error("couldn't determine filename extension", ""),
35-
Some(ext) => match ext.to_str() {
36-
None => error("couldn't parse filename extension", ""),
37-
Some(extstr) => match &*extstr.to_lowercase() {
38-
"obj" => match tobj::load_obj(&path) {
39-
Err(e) => error("tobj couldnt load/parse OBJ", &e.to_string()),
40-
Ok(present) => to_meshes(present.0, present.1),
41-
},
42-
"stl" => match OpenOptions::new().read(true).open(&path) {
43-
Err(e) => error("STL load failed", &e.to_string()),
44-
Ok(mut file) => match stl_io::read_stl(&mut file) {
45-
Err(e) => error("stl_io couldnt parse STL", &e.to_string()),
46-
Ok(stlio_mesh) => vec![stlio_mesh.to_simple_mesh()],
47-
},
48-
},
49-
_ => error("unknown filename extension", ""),
50-
},
51-
},
52-
};
53-
mesh_queue.append(&mut meshes);
54-
}
5523

24+
let mesh_queue: Vec<SimpleMesh> = match_meshes(&matches)?; // A list of meshes to render
5625
let mut turntable = match_turntable(&matches)?;
5726
let crossterm = Crossterm::new();
5827
let input = crossterm.input();
@@ -88,17 +57,21 @@ fn main() -> Result<(), Box<Error>> {
8857
let mut last_time; // Used in the variable time step
8958
loop {
9059
last_time = Instant::now();
91-
if let Some(b) = stdin.next() {
92-
match b {
93-
InputEvent::Keyboard(event) => match event {
94-
KeyEvent::Char('q') => break,
60+
if !context.image {
61+
if let Some(b) = stdin.next() {
62+
match b {
63+
InputEvent::Keyboard(event) => match event {
64+
KeyEvent::Char('q') => {
65+
cursor.show()?;
66+
break;
67+
},
68+
_ => {}
69+
},
9570
_ => {}
96-
},
97-
_ => {}
71+
}
9872
}
9973
}
100-
let rot =
101-
Rotation3::from_euler_angles(turntable.0, turntable.1, turntable.2).to_homogeneous();
74+
let rot = Rotation3::from_euler_angles(turntable.0, turntable.1, turntable.2).to_homogeneous();
10275
context.update(size, &mesh_queue)?; // This checks for if there needs to be a context update
10376
context.clear(); // This clears the z and frame buffer
10477
for mesh in &mesh_queue {
@@ -134,6 +107,5 @@ fn main() -> Result<(), Box<Error>> {
134107
}
135108
}
136109

137-
cursor.show()?;
138110
Ok(())
139111
}

0 commit comments

Comments
 (0)