forked from StudioInfinity/dyna3
Compare commits
5 commits
b3e4e902f3
...
c54b6bc165
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c54b6bc165 | ||
![]() |
f2e84fb64a | ||
![]() |
bbd0835a8f | ||
![]() |
302d93638d | ||
![]() |
fef4127f69 |
4 changed files with 133 additions and 110 deletions
|
@ -133,28 +133,25 @@ details[open]:has(li) .element-switch::after {
|
||||||
font-size: 10pt;
|
font-size: 10pt;
|
||||||
}
|
}
|
||||||
|
|
||||||
.regulator.invalid-constraint {
|
.regulator-input {
|
||||||
color: var(--text-invalid);
|
|
||||||
}
|
|
||||||
|
|
||||||
.regulator > input {
|
|
||||||
color: inherit;
|
color: inherit;
|
||||||
background-color: inherit;
|
background-color: inherit;
|
||||||
border: 1px solid var(--border);
|
border: 1px solid var(--border);
|
||||||
border-radius: 2px;
|
border-radius: 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.regulator > input::placeholder {
|
.regulator-input::placeholder {
|
||||||
color: inherit;
|
color: inherit;
|
||||||
opacity: 54%;
|
opacity: 54%;
|
||||||
font-style: italic;
|
font-style: italic;
|
||||||
}
|
}
|
||||||
|
|
||||||
.regulator.valid-constraint > input {
|
.regulator-input.constraint {
|
||||||
background-color: var(--display-background);
|
background-color: var(--display-background);
|
||||||
}
|
}
|
||||||
|
|
||||||
.regulator.invalid-constraint > input {
|
.regulator-input.invalid {
|
||||||
|
color: var(--text-invalid);
|
||||||
border-color: var(--border-invalid);
|
border-color: var(--border-invalid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,7 +163,7 @@ details[open]:has(li) .element-switch::after {
|
||||||
font-style: normal;
|
font-style: normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
.invalid-constraint > .status::after, details:has(.invalid-constraint):not([open]) .status::after {
|
.regulator-input.invalid + .status::after, details:has(.invalid):not([open]) .status::after {
|
||||||
content: '⚠';
|
content: '⚠';
|
||||||
color: var(--text-invalid);
|
color: var(--text-invalid);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,8 @@ use crate::{
|
||||||
AppState,
|
AppState,
|
||||||
assembly::{
|
assembly::{
|
||||||
Assembly,
|
Assembly,
|
||||||
Regulator,
|
|
||||||
RegulatorRole,
|
|
||||||
Element
|
Element
|
||||||
},
|
}
|
||||||
engine::Q
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* DEBUG */
|
/* DEBUG */
|
||||||
|
@ -200,52 +197,8 @@ pub fn AddRemove() -> View {
|
||||||
(subject_vec[0].clone(), subject_vec[1].clone())
|
(subject_vec[0].clone(), subject_vec[1].clone())
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
let measurement = state.assembly.elements.map(
|
state.assembly.insert_new_regulator(subjects);
|
||||||
move |elts| {
|
|
||||||
let reps = (
|
|
||||||
elts[subjects.0].representation.get_clone(),
|
|
||||||
elts[subjects.1].representation.get_clone()
|
|
||||||
);
|
|
||||||
reps.0.dot(&(&*Q * reps.1))
|
|
||||||
}
|
|
||||||
);
|
|
||||||
let set_point = create_signal(0.0);
|
|
||||||
let role = create_signal(RegulatorRole::Measurement);
|
|
||||||
state.assembly.insert_regulator(Regulator {
|
|
||||||
subjects: subjects,
|
|
||||||
measurement: measurement,
|
|
||||||
set_point: set_point,
|
|
||||||
set_point_text: create_signal(String::new()),
|
|
||||||
role: role,
|
|
||||||
});
|
|
||||||
state.selection.update(|sel| sel.clear());
|
state.selection.update(|sel| sel.clear());
|
||||||
|
|
||||||
/* DEBUG */
|
|
||||||
// print updated regulator list
|
|
||||||
console::log_1(&JsValue::from("Regulators:"));
|
|
||||||
state.assembly.regulators.with(|regs| {
|
|
||||||
for (_, reg) in regs.into_iter() {
|
|
||||||
console::log_5(
|
|
||||||
&JsValue::from(" "),
|
|
||||||
&JsValue::from(reg.subjects.0),
|
|
||||||
&JsValue::from(reg.subjects.1),
|
|
||||||
&JsValue::from(":"),
|
|
||||||
&JsValue::from(reg.set_point.get_untracked())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// update the realization when the regulator becomes
|
|
||||||
// a constraint, or is edited while acting as a constraint
|
|
||||||
create_effect(move || {
|
|
||||||
console::log_1(&JsValue::from(
|
|
||||||
format!("Updated constraint with subjects ({}, {})", subjects.0, subjects.1)
|
|
||||||
));
|
|
||||||
set_point.track();
|
|
||||||
if role.with(|rl| rl.is_valid_constraint()) {
|
|
||||||
state.assembly.realize();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
) { "🔗" }
|
) { "🔗" }
|
||||||
select(bind:value=assembly_name) { /* DEBUG */ // example assembly chooser
|
select(bind:value=assembly_name) { /* DEBUG */ // example assembly chooser
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::{collections::BTreeSet, sync::atomic::{AtomicU64, Ordering}};
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
use web_sys::{console, wasm_bindgen::JsValue}; /* DEBUG */
|
use web_sys::{console, wasm_bindgen::JsValue}; /* DEBUG */
|
||||||
|
|
||||||
use crate::engine::{realize_gram, local_unif_to_std, ConfigSubspace, PartialMatrix};
|
use crate::engine::{Q, local_unif_to_std, realize_gram, ConfigSubspace, PartialMatrix};
|
||||||
|
|
||||||
// the types of the keys we use to access an assembly's elements and regulators
|
// the types of the keys we use to access an assembly's elements and regulators
|
||||||
pub type ElementKey = usize;
|
pub type ElementKey = usize;
|
||||||
|
@ -111,32 +111,42 @@ impl Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum RegulatorRole {
|
// `set_point_spec` is always a valid specification of `set_point`
|
||||||
Measurement,
|
// ┌────────────┬─────────────────────────────────────────────────────┐
|
||||||
Constraint(bool)
|
// │`set_point` │ `set_point_spec` │
|
||||||
}
|
// ┝━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┥
|
||||||
|
// │`Some(x)` │ a string that parses to the floating-point value `x`│
|
||||||
impl RegulatorRole {
|
// ├────────────┼─────────────────────────────────────────────────────┤
|
||||||
pub fn is_valid_constraint(&self) -> bool {
|
// │`None` │ the empty string │
|
||||||
match self {
|
// └────────────┴─────────────────────────────────────────────────────┘
|
||||||
RegulatorRole::Measurement => false,
|
#[derive(Clone, Copy)]
|
||||||
RegulatorRole::Constraint(valid) => *valid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Regulator {
|
pub struct Regulator {
|
||||||
pub subjects: (ElementKey, ElementKey),
|
pub subjects: (ElementKey, ElementKey),
|
||||||
pub measurement: ReadSignal<f64>,
|
pub measurement: ReadSignal<f64>,
|
||||||
pub set_point: Signal<f64>,
|
pub set_point: ReadSignal<Option<f64>>,
|
||||||
pub set_point_text: Signal<String>,
|
|
||||||
pub role: Signal<RegulatorRole>
|
set_point_writable: Signal<Option<f64>>,
|
||||||
|
set_point_spec: Signal<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Regulator {
|
impl Regulator {
|
||||||
fn role_is_valid_constraint_untracked(&self) -> bool {
|
pub fn get_set_point_spec_clone(&self) -> String {
|
||||||
self.role.with_untracked(|role| role.is_valid_constraint())
|
self.set_point_spec.get_clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_set_point_spec_clone_untracked(&self) -> String {
|
||||||
|
self.set_point_spec.get_clone_untracked()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn try_specify_set_point(&self, spec: String) -> bool {
|
||||||
|
match spec.parse::<f64>() {
|
||||||
|
Err(_) if !spec.is_empty() => false,
|
||||||
|
set_pt => {
|
||||||
|
self.set_point_writable.set(set_pt.ok());
|
||||||
|
self.set_point_spec.set(spec);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,6 +244,54 @@ impl Assembly {
|
||||||
subject_regulators.1.update(|regs| regs.insert(key));
|
subject_regulators.1.update(|regs| regs.insert(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn insert_new_regulator(self, subjects: (ElementKey, ElementKey)) {
|
||||||
|
// create and insert a new regulator
|
||||||
|
let measurement = self.elements.map(
|
||||||
|
move |elts| {
|
||||||
|
let reps = (
|
||||||
|
elts[subjects.0].representation.get_clone(),
|
||||||
|
elts[subjects.1].representation.get_clone()
|
||||||
|
);
|
||||||
|
reps.0.dot(&(&*Q * reps.1))
|
||||||
|
}
|
||||||
|
);
|
||||||
|
let set_point_writable = create_signal(None);
|
||||||
|
let set_point = set_point_writable.split().0;
|
||||||
|
self.insert_regulator(Regulator {
|
||||||
|
subjects: subjects,
|
||||||
|
measurement: measurement,
|
||||||
|
set_point: set_point,
|
||||||
|
set_point_writable: set_point_writable,
|
||||||
|
set_point_spec: create_signal(String::new())
|
||||||
|
});
|
||||||
|
|
||||||
|
/* DEBUG */
|
||||||
|
// print updated regulator list
|
||||||
|
console::log_1(&JsValue::from("Regulators:"));
|
||||||
|
self.regulators.with(|regs| {
|
||||||
|
for (_, reg) in regs.into_iter() {
|
||||||
|
console::log_5(
|
||||||
|
&JsValue::from(" "),
|
||||||
|
&JsValue::from(reg.subjects.0),
|
||||||
|
&JsValue::from(reg.subjects.1),
|
||||||
|
&JsValue::from(":"),
|
||||||
|
&JsValue::from(reg.set_point.get_untracked())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// update the realization when the regulator becomes a constraint, or is
|
||||||
|
// edited while acting as a constraint
|
||||||
|
create_effect(move || {
|
||||||
|
console::log_1(&JsValue::from(
|
||||||
|
format!("Updated constraint with subjects ({}, {})", subjects.0, subjects.1)
|
||||||
|
));
|
||||||
|
if set_point.with(|set_pt| set_pt.is_some()) {
|
||||||
|
self.realize();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// --- realization ---
|
// --- realization ---
|
||||||
|
|
||||||
pub fn realize(&self) {
|
pub fn realize(&self) {
|
||||||
|
@ -250,11 +308,14 @@ impl Assembly {
|
||||||
let mut gram_to_be = PartialMatrix::new();
|
let mut gram_to_be = PartialMatrix::new();
|
||||||
self.regulators.with_untracked(|regs| {
|
self.regulators.with_untracked(|regs| {
|
||||||
for (_, reg) in regs {
|
for (_, reg) in regs {
|
||||||
if reg.role_is_valid_constraint_untracked() {
|
match reg.set_point.get_untracked() {
|
||||||
|
Some(set_pt) => {
|
||||||
let subjects = reg.subjects;
|
let subjects = reg.subjects;
|
||||||
let row = elts[subjects.0].column_index.unwrap();
|
let row = elts[subjects.0].column_index.unwrap();
|
||||||
let col = elts[subjects.1].column_index.unwrap();
|
let col = elts[subjects.1].column_index.unwrap();
|
||||||
gram_to_be.push_sym(row, col, reg.set_point.get_untracked());
|
gram_to_be.push_sym(row, col, set_pt);
|
||||||
|
},
|
||||||
|
None => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
use web_sys::{
|
use web_sys::{
|
||||||
Event,
|
|
||||||
HtmlInputElement,
|
|
||||||
KeyboardEvent,
|
KeyboardEvent,
|
||||||
MouseEvent,
|
MouseEvent,
|
||||||
wasm_bindgen::JsCast
|
wasm_bindgen::JsCast
|
||||||
|
@ -14,7 +12,6 @@ use crate::{
|
||||||
assembly::{
|
assembly::{
|
||||||
Regulator,
|
Regulator,
|
||||||
RegulatorKey,
|
RegulatorKey,
|
||||||
RegulatorRole::*,
|
|
||||||
ElementKey
|
ElementKey
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -22,24 +19,46 @@ use crate::{
|
||||||
// an editable view of a regulator
|
// an editable view of a regulator
|
||||||
#[component(inline_props)]
|
#[component(inline_props)]
|
||||||
fn RegulatorInput(regulator: Regulator) -> View {
|
fn RegulatorInput(regulator: Regulator) -> View {
|
||||||
|
let valid = create_signal(true);
|
||||||
|
let value = create_signal(regulator.get_set_point_spec_clone_untracked());
|
||||||
|
|
||||||
|
// this closure resets the input value to the regulator's set point
|
||||||
|
// specification, which is always a valid specification
|
||||||
|
let reset_value = move || {
|
||||||
|
batch(|| {
|
||||||
|
valid.set(true);
|
||||||
|
value.set(regulator.get_set_point_spec_clone());
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
// reset the input value whenever the regulator's set point specification
|
||||||
|
// is updated
|
||||||
|
create_effect(reset_value);
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
input(
|
input(
|
||||||
r#type="text",
|
r#type="text",
|
||||||
placeholder=regulator.measurement.with(|result| result.to_string()),
|
class=move || {
|
||||||
bind:value=regulator.set_point_text,
|
if valid.get() {
|
||||||
on:change=move |event: Event| {
|
match regulator.set_point.get() {
|
||||||
let target: HtmlInputElement = event.target().unwrap().unchecked_into();
|
Some(_) => "regulator-input constraint",
|
||||||
let value = target.value();
|
None => "regulator-input"
|
||||||
if value.is_empty() {
|
}
|
||||||
regulator.role.set(Measurement);
|
|
||||||
} else {
|
} else {
|
||||||
match target.value().parse::<f64>() {
|
"regulator-input invalid"
|
||||||
Ok(set_pt) => batch(|| {
|
}
|
||||||
regulator.set_point.set(set_pt);
|
},
|
||||||
regulator.role.set(Constraint(true));
|
placeholder=regulator.measurement.with(|result| result.to_string()),
|
||||||
}),
|
bind:value=value,
|
||||||
Err(_) => regulator.role.set(Constraint(false))
|
on:change=move |_| valid.set(
|
||||||
};
|
regulator.try_specify_set_point(value.get_clone_untracked())
|
||||||
|
),
|
||||||
|
on:keydown={
|
||||||
|
move |event: KeyboardEvent| {
|
||||||
|
match event.key().as_str() {
|
||||||
|
"Escape" => reset_value(),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -51,22 +70,15 @@ fn RegulatorInput(regulator: Regulator) -> View {
|
||||||
fn RegulatorOutlineItem(regulator_key: RegulatorKey, element_key: ElementKey) -> View {
|
fn RegulatorOutlineItem(regulator_key: RegulatorKey, element_key: ElementKey) -> View {
|
||||||
let state = use_context::<AppState>();
|
let state = use_context::<AppState>();
|
||||||
let assembly = &state.assembly;
|
let assembly = &state.assembly;
|
||||||
let regulator = assembly.regulators.with(|regs| regs[regulator_key].clone());
|
let regulator = assembly.regulators.with(|regs| regs[regulator_key]);
|
||||||
let other_subject = if regulator.subjects.0 == element_key {
|
let other_subject = if regulator.subjects.0 == element_key {
|
||||||
regulator.subjects.1
|
regulator.subjects.1
|
||||||
} else {
|
} else {
|
||||||
regulator.subjects.0
|
regulator.subjects.0
|
||||||
};
|
};
|
||||||
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
|
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
|
||||||
let class = regulator.role.map(
|
|
||||||
|role| match role {
|
|
||||||
Measurement => "regulator",
|
|
||||||
Constraint(true) => "regulator valid-constraint",
|
|
||||||
Constraint(false) => "regulator invalid-constraint"
|
|
||||||
}
|
|
||||||
);
|
|
||||||
view! {
|
view! {
|
||||||
li(class=class.get()) {
|
li(class="regulator") {
|
||||||
div(class="regulator-label") { (other_subject_label) }
|
div(class="regulator-label") { (other_subject_label) }
|
||||||
div(class="regulator-type") { "Inversive distance" }
|
div(class="regulator-type") { "Inversive distance" }
|
||||||
RegulatorInput(regulator=regulator)
|
RegulatorInput(regulator=regulator)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue