use super::super::space::Point;
use super::elem::Elem;
use json::{object, JsonValue};
use std::collections::BTreeMap;
#[derive(Debug, Clone)]
pub struct Node {
pub id: usize,
pub coords: Point,
pub boundary: bool,
elems: [BTreeMap<[u8; 2], usize>; 4],
}
impl Node {
pub fn new(id: usize, coords: Point, boundary: bool) -> Self {
Self {
id,
coords,
boundary,
elems: [
BTreeMap::new(),
BTreeMap::new(),
BTreeMap::new(),
BTreeMap::new(),
],
}
}
pub(crate) fn connect_elem(&mut self, elem: &Elem) {
if let Some(index_of_self) = elem.nodes.iter().position(|node_id| node_id == &self.id) {
let address = elem.h_levels.node_ranking();
if let Some(prev_elem_id) = self.elems[index_of_self].insert(address, elem.id) {
assert_eq!(
prev_elem_id, elem.id,
"Node {} is already connected to Elem {} at {:?} ({}); cannot connect to Elem {}",
self.id,
prev_elem_id,
address,
index_of_self,
elem.id,
);
}
} else {
panic!(
"Elem {} is not connected to Node {}; cannot reciprocate connection!",
elem.id, self.id
);
}
}
#[cfg(feature = "json_export")]
pub fn to_json(&self) -> JsonValue {
object! {
"id": self.id,
"boundary": self.boundary,
"point": self.coords,
"elems": JsonValue::from(self.elems.iter().map(|elem_list| {
elem_list.values().copied().collect()
}).collect::<Vec<Vec<usize>>>())
}
}
}