This commit is contained in:
Tyler Hallada 2019-07-04 17:07:06 -04:00
parent 74eac6115e
commit 27b05a8948

View File

@ -3,51 +3,84 @@ extern crate clap;
extern crate byteorder; extern crate byteorder;
extern crate icosahedron; extern crate icosahedron;
use std::fs::{File, metadata}; use std::fs::{metadata, File};
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use std::path::Path; use std::path::Path;
use byteorder::{WriteBytesExt, LittleEndian}; use byteorder::{LittleEndian, WriteBytesExt};
use icosahedron::{Polyhedron}; use icosahedron::Polyhedron;
fn write_to_binary_file(polyhedron: Polyhedron, path: &Path) { fn write_to_binary_file(polyhedron: Polyhedron, path: &Path) {
let bin_file = File::create(path).expect("Can't create file"); let bin_file = File::create(path).expect("Can't create file");
let mut writer = BufWriter::new(bin_file); let mut writer = BufWriter::new(bin_file);
let write_error_message = "Error encountered while writing to binary file"; let write_error_message = "Error encountered while writing to binary file";
writer.write_u32::<LittleEndian>(polyhedron.positions.len() as u32) writer
.write_u32::<LittleEndian>(polyhedron.positions.len() as u32)
.expect(write_error_message); .expect(write_error_message);
writer.write_u32::<LittleEndian>(polyhedron.cells.len() as u32) writer
.write_u32::<LittleEndian>(polyhedron.cells.len() as u32)
.expect(write_error_message); .expect(write_error_message);
for position in polyhedron.positions.iter() { for position in polyhedron.positions.iter() {
writer.write_f32::<LittleEndian>(position.0.x).expect(write_error_message); writer
writer.write_f32::<LittleEndian>(position.0.y).expect(write_error_message); .write_f32::<LittleEndian>(position.0.x)
writer.write_f32::<LittleEndian>(position.0.z).expect(write_error_message); .expect(write_error_message);
writer
.write_f32::<LittleEndian>(position.0.y)
.expect(write_error_message);
writer
.write_f32::<LittleEndian>(position.0.z)
.expect(write_error_message);
} }
for normal in polyhedron.normals.iter() { for normal in polyhedron.normals.iter() {
writer.write_f32::<LittleEndian>(normal.0.x).expect(write_error_message); writer
writer.write_f32::<LittleEndian>(normal.0.y).expect(write_error_message); .write_f32::<LittleEndian>(normal.0.x)
writer.write_f32::<LittleEndian>(normal.0.z).expect(write_error_message); .expect(write_error_message);
writer
.write_f32::<LittleEndian>(normal.0.y)
.expect(write_error_message);
writer
.write_f32::<LittleEndian>(normal.0.z)
.expect(write_error_message);
} }
for color in polyhedron.colors.iter() { for color in polyhedron.colors.iter() {
writer.write_f32::<LittleEndian>(color.0.x).expect(write_error_message); writer
writer.write_f32::<LittleEndian>(color.0.y).expect(write_error_message); .write_f32::<LittleEndian>(color.0.x)
writer.write_f32::<LittleEndian>(color.0.z).expect(write_error_message); .expect(write_error_message);
writer
.write_f32::<LittleEndian>(color.0.y)
.expect(write_error_message);
writer
.write_f32::<LittleEndian>(color.0.z)
.expect(write_error_message);
} }
for cell in polyhedron.cells.iter() { for cell in polyhedron.cells.iter() {
writer.write_u32::<LittleEndian>(cell.a as u32).expect(write_error_message); writer
writer.write_u32::<LittleEndian>(cell.b as u32).expect(write_error_message); .write_u32::<LittleEndian>(cell.a as u32)
writer.write_u32::<LittleEndian>(cell.c as u32).expect(write_error_message); .expect(write_error_message);
writer
.write_u32::<LittleEndian>(cell.b as u32)
.expect(write_error_message);
writer
.write_u32::<LittleEndian>(cell.c as u32)
.expect(write_error_message);
} }
} }
fn write_to_json_file(polyhedron: Polyhedron, path: &Path) { fn write_to_json_file(polyhedron: Polyhedron, path: &Path) {
let mut json_file = File::create(path).expect("Can't create file"); let mut json_file = File::create(path).expect("Can't create file");
let json = serde_json::to_string(&polyhedron).expect("Problem serializing"); let json = serde_json::to_string(&polyhedron).expect("Problem serializing");
json_file.write_all(json.as_bytes()).expect("Can't write to file"); json_file
.write_all(json.as_bytes())
.expect("Can't write to file");
} }
fn generate_files(dir: &str, format: Format, truncated: bool, colored: bool, fn generate_files(
param_list: Vec<(f32, u32)>) { dir: &str,
format: Format,
truncated: bool,
colored: bool,
param_list: Vec<(f32, u32)>,
) {
let mesh_type = if truncated { let mesh_type = if truncated {
"hexsphere" "hexsphere"
} else { } else {
@ -60,7 +93,6 @@ fn generate_files(dir: &str, format: Format, truncated: bool, colored: bool,
mesh_type, param.0, param.1 mesh_type, param.0, param.1
); );
let polyhedron = if truncated { let polyhedron = if truncated {
let mut hexsphere = Polyhedron::new_truncated_isocahedron(param.0, param.1); let mut hexsphere = Polyhedron::new_truncated_isocahedron(param.0, param.1);
hexsphere.compute_triangle_normals(); hexsphere.compute_triangle_normals();
@ -83,9 +115,13 @@ fn generate_files(dir: &str, format: Format, truncated: bool, colored: bool,
println!("triangles: {}", colored_polyhedron.cells.len()); println!("triangles: {}", colored_polyhedron.cells.len());
println!("vertices: {}", colored_polyhedron.positions.len()); println!("vertices: {}", colored_polyhedron.positions.len());
let filename = Path::new(dir).join( let filename = Path::new(dir).join(format!(
format!("{}_r{}_d{}.{}", mesh_type, param.0, param.1, format.extension()) "{}_r{}_d{}.{}",
); mesh_type,
param.0,
param.1,
format.extension()
));
match format { match format {
Format::Bin => write_to_binary_file(colored_polyhedron, &filename), Format::Bin => write_to_binary_file(colored_polyhedron, &filename),
Format::Json => write_to_json_file(colored_polyhedron, &filename), Format::Json => write_to_json_file(colored_polyhedron, &filename),
@ -93,7 +129,7 @@ fn generate_files(dir: &str, format: Format, truncated: bool, colored: bool,
} }
} }
arg_enum!{ arg_enum! {
#[derive(Debug)] #[derive(Debug)]
enum Format { enum Format {
Json, Json,
@ -118,10 +154,16 @@ fn main() {
if metadata.is_dir() { if metadata.is_dir() {
Ok(()) Ok(())
} else { } else {
Err(String::from(format!("Output '{}' is not a directory", &path_clone))) Err(String::from(format!(
"Output '{}' is not a directory",
&path_clone
)))
} }
} }
Err(_) => Err(String::from(format!("Directory '{}' doesn't exist", &path_clone))) Err(_) => Err(String::from(format!(
"Directory '{}' doesn't exist",
&path_clone
))),
} }
}; };
@ -142,7 +184,8 @@ fn main() {
"Format to write the files in.") "Format to write the files in.")
(@arg output: [OUTPUT] {dir_exists} default_value("output/") (@arg output: [OUTPUT] {dir_exists} default_value("output/")
"Directory to write the output files to.") "Directory to write the output files to.")
).get_matches(); )
.get_matches();
let truncated = matches.is_present("truncated"); let truncated = matches.is_present("truncated");
let colored = matches.is_present("colored"); let colored = matches.is_present("colored");