cad_rs/src/main.rs

112 lines
3.7 KiB
Rust

#![feature(drain_filter)]
#![feature(box_patterns)]
#![feature(slice_patterns)]
#![feature(bind_by_move_pattern_guards)]
#![feature(vec_remove_item)]
extern crate nalgebra;
#[macro_use]
extern crate approx;
extern crate itertools;
#[macro_use]
extern crate log;
extern crate env_logger;
mod entity;
mod math;
mod relation;
fn main() {
use entity::{CPoint, PointRef};
use math::{Eqn, Eqns, Expr, Point2, Region2, Rot2};
use relation::{Relation, ResolveResult};
env_logger::init();
println!("Hello, world!");
let u1 = math::Unknown(1);
let u2 = math::Unknown(2);
// let u1 = eqn::Expr::from(1.);
// let u2 = eqn::Expr::from(1.);
let origin = CPoint::new_single(Point2::new(0., 0.)).into_ref();
// let p1 = Point::new_ref(Var::new_full(Point2::new((1.).into(), (1.).into())));
let p1 = CPoint::new(
Point2::new(0., 0.),
Region2::Singleton(Point2::new((u1).into(), (u2).into())),
)
.into_ref();
let p2 = CPoint::new_full(Point2::new(4., 4.)).into_ref();
let p3 = CPoint::new_full(Point2::new(2., 2.)).into_ref();
let mut points: Vec<PointRef> = vec![origin.clone(), p1.clone(), p2.clone(), p3.clone()];
let print_points = |points: &Vec<PointRef>| {
println!(
"origin: {}\np1: {}\np2: {}\np3: {}",
points[0].borrow(),
points[1].borrow(),
points[2].borrow(),
points[3].borrow(),
);
};
print_points(&points);
let c1 = relation::Coincident {
p1: origin.clone(),
p2: p1.clone(),
};
let c4 = relation::PointAngle::new_vertical(p1.clone(), p2.clone());
let c3 = relation::PointAngle::new_horizontal(p2.clone(), p3.clone());
let c2 = relation::AlignedDistance::new_vertical(p1.clone(), p2.clone(), 12.);
let c5 = relation::PointAngle::new(p1.clone(), p3.clone(), Rot2::from_angle_deg(0.572938698));
let mut relations: Vec<Box<dyn Relation>> = vec![
/*Box::new(c1),*/ Box::new(c2),
Box::new(c3),
Box::new(c4),
Box::new(c5),
];
let mut constrained: Vec<Box<dyn Relation>> = Vec::new();
let mut any_underconstrained = true;
let mut any_constrained = true;
let mut any_overconstrained = false;
while any_underconstrained && any_constrained && !any_overconstrained {
any_underconstrained = false;
any_constrained = false;
let newly_constrained = relations.drain_filter(|r| {
let rr = r.resolve();
println!("resolve result: {:?}", rr);
print_points(&points);
let mut pos = p2.borrow().val().clone();
println!("p2 pos: {}", pos);
let mut pos = p3.borrow().val().clone();
println!("p3 pos: {}", pos);
match rr {
ResolveResult::Underconstrained => {
any_underconstrained = true;
false
}
ResolveResult::Constrained => {
any_constrained = true;
true
}
ResolveResult::Overconstrained => {
any_overconstrained = true;
true
}
}
});
constrained.extend(newly_constrained);
}
if any_overconstrained {
println!("The system is overconstrained")
} else if any_underconstrained {
println!("Some constraints could not be solved")
} else {
println!("All constraints have been solved")
}
let e1 = Eqn::new(Expr::Unkn(u1), Expr::Const(1.));
let e2 = Eqn::new(Expr::Unkn(u2), Expr::Const(1.));
let eqns = Eqns(vec![e1, e2].into());
for p in &mut points {
p.borrow_mut().resolve_with(&eqns);
}
print_points(&points);
}