use std::cell::RefCell; use std::rc::Rc; use crate::math::{Point2, Region, Region1, Region2, Scalar}; use crate::math::eqn::{Eqns}; use std::fmt; #[derive(Clone, Copy, Debug)] pub struct Var> { value: T, constraints: TRegion, } impl + fmt::Display> fmt::Display for Var { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{{ {} ∈ {} }}", self.value, self.constraints) } } impl + Clone> Var { pub fn new(value: T, constraints: TRegion) -> Self { Self { value, constraints } } pub fn new_full(value: T) -> Self { Self::new(value, TRegion::full()) } pub fn new_single(value: T) -> Self { Self::new(value.clone(), TRegion::singleton(value)) } pub fn val(&self) -> &T { &self.value } pub fn resolve_with(&mut self, eqns: &Eqns) -> bool { let resolved_constraints = self.constraints.clone().evaluate_with(eqns).simplify(); if let Some(n) = resolved_constraints.nearest(&self.value) { self.value = n; true } else { false } } pub fn evaluate_with(&mut self, eqns: &Eqns) -> bool { self.constraints = self.constraints.clone().evaluate_with(eqns).simplify(); if let Some(n) = self.constraints.nearest(&self.value) { self.value = n; true } else { false } } pub fn constraints(&self) -> &TRegion { &self.constraints } pub fn reconstrain(&mut self, new_constraints: TRegion) -> bool { self.constraints = new_constraints; if let Some(n) = self.constraints.nearest(&self.value) { self.value = n; true } else { false } } } type ScalarVar = Var; type PointVar = Var, Region2>; #[derive(Debug)] pub struct Point { // pub id: i64, pub pos: PointVar, } pub type PointRef = Rc>; impl Point { pub fn new_ref(pos: PointVar) -> PointRef { Rc::new(RefCell::new(Point { pos })) } } impl fmt::Display for Point { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Point {{ pos: {} }}", self.pos) } } struct Line { p1: PointRef, p2: PointRef, len: ScalarVar, dir: ScalarVar, } // struct System { // points: Vec, // }