Outline: encapsulate assembly data

This commit is contained in:
Aaron Fenyes 2024-09-13 00:40:34 -07:00
parent d481181ef8
commit e6d1e0b865
3 changed files with 44 additions and 32 deletions

View File

@ -2,47 +2,42 @@ use itertools::Itertools;
use nalgebra::DVector;
use sycamore::{prelude::*, web::tags::div};
#[derive(Clone, PartialEq)]
struct Element {
id: String,
label: String,
color: [f32; 3],
rep: DVector<f64>,
}
use crate::assembly::{Assembly, Element};
struct AppState {
// the order of the elements is arbitrary, and it could change at any time
elements: Signal<Vec<Element>>
assembly: Assembly
}
#[component]
pub fn App() -> View {
let state = AppState {
elements: create_signal(vec![
Element {
id: String::from("wing_a"),
label: String::from("Wing A"),
color: [1.00_f32, 0.25_f32, 0.00_f32],
rep: DVector::<f64>::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25])
},
Element {
id: String::from("wing_b"),
label: String::from("Wing B"),
color: [1.00_f32, 0.25_f32, 0.00_f32],
rep: DVector::<f64>::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25])
},
Element {
id: String::from("central"),
label: String::from("Central"),
color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.25, -1.0])
}
])
assembly: Assembly {
elements: create_signal(vec![
Element {
id: String::from("wing_a"),
label: String::from("Wing A"),
color: [1.00_f32, 0.25_f32, 0.00_f32],
rep: DVector::<f64>::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25])
},
Element {
id: String::from("wing_b"),
label: String::from("Wing B"),
color: [1.00_f32, 0.25_f32, 0.00_f32],
rep: DVector::<f64>::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25])
},
Element {
id: String::from("central"),
label: String::from("Central"),
color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.25, -1.0])
}
])
}
};
// sort the elements alphabetically by ID
let elements_sorted = create_memo(move ||
state.elements
state.assembly.elements
.get_clone()
.into_iter()
.sorted_by_key(|elt| elt.id.clone())

View File

@ -0,0 +1,16 @@
use nalgebra::DVector;
use sycamore::reactive::Signal;
#[derive(Clone, PartialEq)]
pub struct Element {
pub id: String,
pub label: String,
pub color: [f32; 3],
pub rep: DVector<f64>,
}
// a complete, view-independent description of an assembly
pub struct Assembly {
// the order of the elements is arbitrary, and it could change at any time
pub elements: Signal<Vec<Element>>
}

View File

@ -1,6 +1,7 @@
use sycamore::prelude::*;
mod app;
mod assembly;
use sycamore::prelude::*;
use app::App;