Skip to content

Commit 4089c09

Browse files
committed
main: add raw csv output
1 parent 853b913 commit 4089c09

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/target
22
**/*.rs.bk
33

4+
dla.csv
45
dla.js
56
dla.png
67
dla.pov

src/main.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ struct App {
2727
#[structopt(short = "g", long = "spawn-radius", default_value = "10")]
2828
spawn_radius: u32,
2929

30-
/// The output formats the scene should be saved as. As of now `javascript`
31-
/// and `povray` are supported.
30+
/// The output formats the scene should be saved as. As of now `javascript,
31+
/// `povray` and `csv` are supported.
3232
#[structopt(short = "s", long = "scene-format", default_value = "povray")]
3333
scene_formats: Vec<SceneFormat>,
3434

@@ -41,6 +41,7 @@ struct App {
4141
enum SceneFormat {
4242
Povray,
4343
Js,
44+
Csv,
4445
}
4546

4647
#[derive(Debug)]
@@ -79,7 +80,7 @@ fn main() -> io::Result<()> {
7980
dla.add(&mut rng);
8081
}
8182

82-
// clear progress line manually
83+
// clear progress line manually to not install another dep
8384
println!(
8485
"\r {:width$} ",
8586
" ",
@@ -112,6 +113,7 @@ It contains {} particles and its bounding box goes from
112113
match r {
113114
SceneFormat::Povray => save_pov_scene(&args.output, &scene)?,
114115
SceneFormat::Js => save_js_scene(&args.output, &scene)?,
116+
SceneFormat::Csv => save_csv_scene(&args.output, &scene)?,
115117
}
116118
}
117119

@@ -275,6 +277,25 @@ single object `DLA` that has the `particles` alongside a `camera` and `lights`.
275277
Ok(())
276278
}
277279

280+
fn save_csv_scene(path: &PathBuf, Scene { dla, .. }: &Scene) -> io::Result<()> {
281+
let path = path.with_extension("csv");
282+
let mut out = BufWriter::new(File::create(&path)?);
283+
284+
for c in dla.cells() {
285+
writeln!(out, "{},{},{}", c.x, c.y, c.z)?;
286+
}
287+
288+
println!(
289+
r#"## Csv Scene
290+
291+
The positions (x,y,z) of all the cells that form the DLA have been saved as a CSV file ({path}).
292+
"#,
293+
path = path.display()
294+
);
295+
296+
Ok(())
297+
}
298+
278299
impl Scene {
279300
/// build a scene from a DLA with camera and lights in a completely
280301
/// arbitrary way.
@@ -346,6 +367,7 @@ impl std::str::FromStr for SceneFormat {
346367
match s {
347368
"povray" => Ok(SceneFormat::Povray),
348369
"javascript" | "js" => Ok(SceneFormat::Js),
370+
"csv" => Ok(SceneFormat::Csv),
349371
s => Err(format!("`{}` is not a valid scene format", s)),
350372
}
351373
}

0 commit comments

Comments
 (0)