Tyler Hallada 4 years ago
parent
commit
27b05a8948
1 changed files with 71 additions and 28 deletions
  1. 71 28
      src/bin.rs

+ 71 - 28
src/bin.rs

@@ -3,51 +3,84 @@ extern crate clap;
3 3
 extern crate byteorder;
4 4
 extern crate icosahedron;
5 5
 
6
-use std::fs::{File, metadata};
6
+use std::fs::{metadata, File};
7 7
 use std::io::{BufWriter, Write};
8 8
 use std::path::Path;
9 9
 
10
-use byteorder::{WriteBytesExt, LittleEndian};
11
-use icosahedron::{Polyhedron};
10
+use byteorder::{LittleEndian, WriteBytesExt};
11
+use icosahedron::Polyhedron;
12 12
 
13 13
 fn write_to_binary_file(polyhedron: Polyhedron, path: &Path) {
14 14
     let bin_file = File::create(path).expect("Can't create file");
15 15
     let mut writer = BufWriter::new(bin_file);
16 16
     let write_error_message = "Error encountered while writing to binary file";
17
-    writer.write_u32::<LittleEndian>(polyhedron.positions.len() as u32)
17
+    writer
18
+        .write_u32::<LittleEndian>(polyhedron.positions.len() as u32)
18 19
         .expect(write_error_message);
19
-    writer.write_u32::<LittleEndian>(polyhedron.cells.len() as u32)
20
+    writer
21
+        .write_u32::<LittleEndian>(polyhedron.cells.len() as u32)
20 22
         .expect(write_error_message);
21 23
     for position in polyhedron.positions.iter() {
22
-        writer.write_f32::<LittleEndian>(position.0.x).expect(write_error_message);
23
-        writer.write_f32::<LittleEndian>(position.0.y).expect(write_error_message);
24
-        writer.write_f32::<LittleEndian>(position.0.z).expect(write_error_message);
24
+        writer
25
+            .write_f32::<LittleEndian>(position.0.x)
26
+            .expect(write_error_message);
27
+        writer
28
+            .write_f32::<LittleEndian>(position.0.y)
29
+            .expect(write_error_message);
30
+        writer
31
+            .write_f32::<LittleEndian>(position.0.z)
32
+            .expect(write_error_message);
25 33
     }
26 34
     for normal in polyhedron.normals.iter() {
27
-        writer.write_f32::<LittleEndian>(normal.0.x).expect(write_error_message);
28
-        writer.write_f32::<LittleEndian>(normal.0.y).expect(write_error_message);
29
-        writer.write_f32::<LittleEndian>(normal.0.z).expect(write_error_message);
35
+        writer
36
+            .write_f32::<LittleEndian>(normal.0.x)
37
+            .expect(write_error_message);
38
+        writer
39
+            .write_f32::<LittleEndian>(normal.0.y)
40
+            .expect(write_error_message);
41
+        writer
42
+            .write_f32::<LittleEndian>(normal.0.z)
43
+            .expect(write_error_message);
30 44
     }
31 45
     for color in polyhedron.colors.iter() {
32
-        writer.write_f32::<LittleEndian>(color.0.x).expect(write_error_message);
33
-        writer.write_f32::<LittleEndian>(color.0.y).expect(write_error_message);
34
-        writer.write_f32::<LittleEndian>(color.0.z).expect(write_error_message);
46
+        writer
47
+            .write_f32::<LittleEndian>(color.0.x)
48
+            .expect(write_error_message);
49
+        writer
50
+            .write_f32::<LittleEndian>(color.0.y)
51
+            .expect(write_error_message);
52
+        writer
53
+            .write_f32::<LittleEndian>(color.0.z)
54
+            .expect(write_error_message);
35 55
     }
36 56
     for cell in polyhedron.cells.iter() {
37
-        writer.write_u32::<LittleEndian>(cell.a as u32).expect(write_error_message);
38
-        writer.write_u32::<LittleEndian>(cell.b as u32).expect(write_error_message);
39
-        writer.write_u32::<LittleEndian>(cell.c as u32).expect(write_error_message);
57
+        writer
58
+            .write_u32::<LittleEndian>(cell.a as u32)
59
+            .expect(write_error_message);
60
+        writer
61
+            .write_u32::<LittleEndian>(cell.b as u32)
62
+            .expect(write_error_message);
63
+        writer
64
+            .write_u32::<LittleEndian>(cell.c as u32)
65
+            .expect(write_error_message);
40 66
     }
41 67
 }
42 68
 
43 69
 fn write_to_json_file(polyhedron: Polyhedron, path: &Path) {
44 70
     let mut json_file = File::create(path).expect("Can't create file");
45 71
     let json = serde_json::to_string(&polyhedron).expect("Problem serializing");
46
-    json_file.write_all(json.as_bytes()).expect("Can't write to file");
72
+    json_file
73
+        .write_all(json.as_bytes())
74
+        .expect("Can't write to file");
47 75
 }
48 76
 
49
-fn generate_files(dir: &str, format: Format, truncated: bool, colored: bool,
50
-                  param_list: Vec<(f32, u32)>) {
77
+fn generate_files(
78
+    dir: &str,
79
+    format: Format,
80
+    truncated: bool,
81
+    colored: bool,
82
+    param_list: Vec<(f32, u32)>,
83
+) {
51 84
     let mesh_type = if truncated {
52 85
         "hexsphere"
53 86
     } else {
@@ -60,7 +93,6 @@ fn generate_files(dir: &str, format: Format, truncated: bool, colored: bool,
60 93
             mesh_type, param.0, param.1
61 94
         );
62 95
 
63
-
64 96
         let polyhedron = if truncated {
65 97
             let mut hexsphere = Polyhedron::new_truncated_isocahedron(param.0, param.1);
66 98
             hexsphere.compute_triangle_normals();
@@ -83,9 +115,13 @@ fn generate_files(dir: &str, format: Format, truncated: bool, colored: bool,
83 115
         println!("triangles: {}", colored_polyhedron.cells.len());
84 116
         println!("vertices: {}", colored_polyhedron.positions.len());
85 117
 
86
-        let filename = Path::new(dir).join(
87
-            format!("{}_r{}_d{}.{}", mesh_type, param.0, param.1, format.extension())
88
-        );
118
+        let filename = Path::new(dir).join(format!(
119
+            "{}_r{}_d{}.{}",
120
+            mesh_type,
121
+            param.0,
122
+            param.1,
123
+            format.extension()
124
+        ));
89 125
         match format {
90 126
             Format::Bin => write_to_binary_file(colored_polyhedron, &filename),
91 127
             Format::Json => write_to_json_file(colored_polyhedron, &filename),
@@ -93,7 +129,7 @@ fn generate_files(dir: &str, format: Format, truncated: bool, colored: bool,
93 129
     }
94 130
 }
95 131
 
96
-arg_enum!{
132
+arg_enum! {
97 133
     #[derive(Debug)]
98 134
     enum Format {
99 135
         Json,
@@ -118,10 +154,16 @@ fn main() {
118 154
                 if metadata.is_dir() {
119 155
                     Ok(())
120 156
                 } else {
121
-                    Err(String::from(format!("Output '{}' is not a directory", &path_clone)))
157
+                    Err(String::from(format!(
158
+                        "Output '{}' is not a directory",
159
+                        &path_clone
160
+                    )))
122 161
                 }
123 162
             }
124
-            Err(_) => Err(String::from(format!("Directory '{}' doesn't exist", &path_clone)))
163
+            Err(_) => Err(String::from(format!(
164
+                "Directory '{}' doesn't exist",
165
+                &path_clone
166
+            ))),
125 167
         }
126 168
     };
127 169
 
@@ -142,7 +184,8 @@ fn main() {
142 184
             "Format to write the files in.")
143 185
         (@arg output: [OUTPUT] {dir_exists} default_value("output/")
144 186
             "Directory to write the output files to.")
145
-    ).get_matches();
187
+    )
188
+    .get_matches();
146 189
 
147 190
     let truncated = matches.is_present("truncated");
148 191
     let colored = matches.is_present("colored");