summaryrefslogtreecommitdiffstats
path: root/src/solar_system.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/solar_system.rs')
-rw-r--r--src/solar_system.rs27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/solar_system.rs b/src/solar_system.rs
index 94508fd..7799a59 100644
--- a/src/solar_system.rs
+++ b/src/solar_system.rs
@@ -6,6 +6,8 @@ use self::orbit::*;
use serde::{Deserialize};
use crate::known_stars::*;
+use crate::ntree::NTree;
+use crate::ntree::NTreeNode;
use crate::timeman::Second;
use std::error::Error;
@@ -42,10 +44,26 @@ pub struct SolarSystem
name: String,
bodies: Vec<OrbitalBody>,
+ heirarchy: NTreeNode<BodyId>
}
impl SolarSystem
{
+ fn rebuild_system_heirarchy(
+ bodies: &[OrbitalBody],
+ root: &mut NTreeNode<BodyId>)
+ {
+ for body in bodies {
+ if let Some(orbit) = body.orbit() {
+ if orbit.parent() == *root.value() {
+ let mut subtree = NTreeNode::new(body.id());
+ SolarSystem::rebuild_system_heirarchy(bodies, &mut subtree);
+ root.insert_node(subtree);
+ }
+ }
+ }
+ }
+
pub fn new_from_csv(
id: SystemId,
data: &'static str)
@@ -69,13 +87,17 @@ impl SolarSystem
//if record.radius == 0.0 { continue; }
println!("New body: {:?}", record);
- bodies.push(OrbitalBody::new_from_record(bodies.len(), record));
+ bodies.push(OrbitalBody::new_from_record(bodies.len(), id, record));
}
+ let mut heirarchy_root = NTreeNode::new(0);
+ SolarSystem::rebuild_system_heirarchy(&bodies, &mut heirarchy_root);
+
Ok(Self {
id: id,
name: bodies[0].name().clone(),
bodies: bodies,
+ heirarchy: heirarchy_root
})
}
@@ -102,6 +124,9 @@ impl SolarSystem
pub fn body(&self, id: BodyId) -> &OrbitalBody
{ &self.bodies[id] }
+ pub fn heirarchy(&self) -> &NTreeNode<BodyId>
+ { &self.heirarchy }
+
pub fn body_position(
&self,
body: &OrbitalBody,