-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
99 lines (90 loc) · 3.18 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
extern crate rustc_serialize;
extern crate csv;
extern crate stopwatch;
extern crate kiss3d;
extern crate nalgebra as na;
use na::Translation3;
use kiss3d::window::Window;
use kiss3d::light::Light;
pub mod sim_data;
pub use sim_data::Star;
use stopwatch::Stopwatch;
use std::io::{stdin, stdout, BufRead, Write};
fn load_database() -> Result<Vec<Star>, &'static str> {
let reader_wrap = csv::Reader::from_file("hygdata_v3.csv");
let mut reader;
if let Ok(reader_val) = reader_wrap {
reader = reader_val;
} else {
return Err("Unable to read csv!");
}
let sw = Stopwatch::start_new();
println!("Initializing database from csv...");
let stars_wrap: csv::Result<Vec<Star>> = reader.decode().collect::<csv::Result<Vec<Star>>>();
if let Ok(stars) = stars_wrap {
println!("Loading took {} seconds\n",
sw.elapsed_ms() as f32 / 1000f32);
return Ok(stars);
} else {
return Err("Unable to decode csv file!");
}
}
fn main() {
let maxdist: f64;
let stars: Vec<Star> = load_database().unwrap();
let stdin = stdin();
loop {
print!("> ");
stdout().flush().unwrap();
let mut line = String::new();
stdin.lock().read_line(&mut line).unwrap();
let line = line.trim();
if line == "q" {
return;
} else {
let maxdist_wrap = line.parse::<f64>();
if let Ok(dist) = maxdist_wrap {
maxdist = dist * 0.306601;
break;
} else {
println!("Please enter either 'q' to quit, or a distance in lightyears.");
}
}
}
let sw = Stopwatch::start_new();
let mut named_vec = Vec::new();
for star in stars {
// let Some(mscatid) = star.multistar_catalog_id.clone()
if star.distance < maxdist {
named_vec.push(star.clone());
}
}
named_vec.sort_by(|a, b| a.distance.partial_cmp(&b.distance).unwrap());
let starcount = named_vec.len();
let mut window = Window::new(&format!("Stars within {:.3} lightyears", maxdist * 3.26156));
window.set_framerate_limit(Some(75));
window.set_light(Light::StickToCamera);
// print only the first ten stars within range, rather
// than spending forever printing out the matching stars.
// this should be adjustable in the future.
for star in (&named_vec).into_iter().take(10) {
println!("{:.3} lightyears\n{:?}\n\n", star.distance * 3.26156, star);
}
for star in named_vec {
let mut sph = window.add_sphere(0.015);
if star.id == 0 {
sph.set_color(1.0, 1.0, 0.7);
} else {
let (r, g, b) = star.bv2rgb_opt().or(Some((1.0, 1.0, 1.0))).unwrap();
let mag = star.magnitude as f32;
let lum = f32::min(250.0 * 2.512f32.powf(-mag), 1.0);
sph.set_color(r * lum, g * lum, b * lum);
}
sph.append_translation(&Translation3::new(star.x as f32, star.y as f32, star.z as f32));
}
println!("There were {} matching stars.", starcount);
println!("Processing took {} seconds",
sw.elapsed_ms() as f32 / 1000f32);
while window.render() {}
window.hide();
}