Skip to content

Commit

Permalink
Fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasJacob committed May 27, 2024
1 parent c27675f commit c4a7054
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 165 deletions.
2 changes: 1 addition & 1 deletion src/constraints/angle_between_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ mod tests {
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::AngleBetweenPoints(constr1.clone()))
.unwrap();

println!(
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/coincident/arc_end_point_coincident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::ArcEndPointCoincident(constr1.clone()))
.unwrap();

sketch
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/coincident/arc_start_point_coincident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::ArcStartPointCoincident(constr1.clone()))
.unwrap();

sketch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mod tests {
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::EuclideanDistance(constr1.clone()))
.unwrap();

sketch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mod tests {
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::HorizontalDistance(constr1.clone()))
.unwrap();

sketch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mod tests {
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::VerticalDistance(constr1.clone()))
.unwrap();

sketch
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/fix_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod tests {
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::FixPoint(constr1.clone()))
.unwrap();

sketch
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/lines/equal_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ mod tests {
let constr1 = Rc::new(RefCell::new(EqualLength::new(line1.clone(), line2.clone())));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::EqualLength(constr1.clone()))
.unwrap();

sketch
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/lines/horizontal_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mod tests {
let constr1 = Rc::new(RefCell::new(HorizontalLine::new(line.clone())));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::HorizontalLine(constr1.clone()))
.unwrap();

sketch
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/lines/parallel_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod tests {
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::ParallelLines(constr1.clone()))
.unwrap();

sketch
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/lines/perpendicular_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ mod tests {
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::PerpendicularLines(constr1.clone()))
.unwrap();

sketch
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/lines/vertical_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mod tests {
let constr1 = Rc::new(RefCell::new(VerticalLine::new(line.clone())));
sketch
.borrow_mut()
.add_constraint(ConstraintCell(constr1.clone()))
.add_constraint(ConstraintCell::VerticalLine(constr1.clone()))
.unwrap();

sketch
Expand Down
190 changes: 155 additions & 35 deletions src/constraints/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::cell::RefCell;
use std::cell::{Ref, RefCell, RefMut};
use std::fmt::Debug;
use std::ptr;
use std::rc::Rc;

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize};

#[cfg(feature = "tsify")]
use tsify::Tsify;
Expand Down Expand Up @@ -42,43 +43,162 @@ pub enum ConstraintType {
PerpendicularLines(lines::perpendicular_lines::PerpendicularLines),
}

#[repr(transparent)]
#[derive(Debug, Clone)]
impl Constraint for ConstraintType {
fn references(&self) -> Vec<ParametricCell> {
match self {
ConstraintType::AngleBetweenPoints(c) => c.references(),
ConstraintType::ArcEndPointCoincident(c) => c.references(),
ConstraintType::ArcStartPointCoincident(c) => c.references(),
ConstraintType::EuclideanDistance(c) => c.references(),
ConstraintType::HorizontalDistance(c) => c.references(),
ConstraintType::VerticalDistance(c) => c.references(),
ConstraintType::FixPoint(c) => c.references(),
ConstraintType::EqualLength(c) => c.references(),
ConstraintType::HorizontalLine(c) => c.references(),
ConstraintType::VerticalLine(c) => c.references(),
ConstraintType::ParallelLines(c) => c.references(),
ConstraintType::PerpendicularLines(c) => c.references(),
}
}

fn loss_value(&self) -> f64 {
match self {
ConstraintType::AngleBetweenPoints(c) => c.loss_value(),
ConstraintType::ArcEndPointCoincident(c) => c.loss_value(),
ConstraintType::ArcStartPointCoincident(c) => c.loss_value(),
ConstraintType::EuclideanDistance(c) => c.loss_value(),
ConstraintType::HorizontalDistance(c) => c.loss_value(),
ConstraintType::VerticalDistance(c) => c.loss_value(),
ConstraintType::FixPoint(c) => c.loss_value(),
ConstraintType::EqualLength(c) => c.loss_value(),
ConstraintType::HorizontalLine(c) => c.loss_value(),
ConstraintType::VerticalLine(c) => c.loss_value(),
ConstraintType::ParallelLines(c) => c.loss_value(),
ConstraintType::PerpendicularLines(c) => c.loss_value(),
}
}

fn update_gradient(&mut self) {
match self {
ConstraintType::AngleBetweenPoints(c) => c.update_gradient(),
ConstraintType::ArcEndPointCoincident(c) => c.update_gradient(),
ConstraintType::ArcStartPointCoincident(c) => c.update_gradient(),
ConstraintType::EuclideanDistance(c) => c.update_gradient(),
ConstraintType::HorizontalDistance(c) => c.update_gradient(),
ConstraintType::VerticalDistance(c) => c.update_gradient(),
ConstraintType::FixPoint(c) => c.update_gradient(),
ConstraintType::EqualLength(c) => c.update_gradient(),
ConstraintType::HorizontalLine(c) => c.update_gradient(),
ConstraintType::VerticalLine(c) => c.update_gradient(),
ConstraintType::ParallelLines(c) => c.update_gradient(),
ConstraintType::PerpendicularLines(c) => c.update_gradient(),
}
}

fn get_type(&self) -> ConstraintType {
self.clone()
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "tsify", derive(Tsify))]
#[cfg_attr(feature = "tsify", tsify(into_wasm_abi, from_wasm_abi))]
pub struct ConstraintCell(pub Rc<RefCell<dyn Constraint>>);

impl Serialize for ConstraintCell {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.borrow().get_type().serialize(serializer)
pub enum ConstraintCell {
AngleBetweenPoints(Rc<RefCell<angle_between_points::AngleBetweenPoints>>),
ArcEndPointCoincident(Rc<RefCell<coincident::arc_end_point_coincident::ArcEndPointCoincident>>),
ArcStartPointCoincident(
Rc<RefCell<coincident::arc_start_point_coincident::ArcStartPointCoincident>>,
),
EuclideanDistance(
Rc<RefCell<distance::euclidian_distance_between_points::EuclidianDistanceBetweenPoints>>,
),
HorizontalDistance(
Rc<RefCell<distance::horizontal_distance_between_points::HorizontalDistanceBetweenPoints>>,
),
VerticalDistance(
Rc<RefCell<distance::vertical_distance_between_points::VerticalDistanceBetweenPoints>>,
),
FixPoint(Rc<RefCell<fix_point::FixPoint>>),
EqualLength(Rc<RefCell<lines::equal_length::EqualLength>>),
HorizontalLine(Rc<RefCell<lines::horizontal_line::HorizontalLine>>),
VerticalLine(Rc<RefCell<lines::vertical_line::VerticalLine>>),
ParallelLines(Rc<RefCell<lines::parallel_lines::ParallelLines>>),
PerpendicularLines(Rc<RefCell<lines::perpendicular_lines::PerpendicularLines>>),
}

impl ConstraintCell {
pub fn borrow(&self) -> Ref<dyn Constraint> {
match self {
ConstraintCell::AngleBetweenPoints(c) => c.borrow(),
ConstraintCell::ArcEndPointCoincident(c) => c.borrow(),
ConstraintCell::ArcStartPointCoincident(c) => c.borrow(),
ConstraintCell::EuclideanDistance(c) => c.borrow(),
ConstraintCell::HorizontalDistance(c) => c.borrow(),
ConstraintCell::VerticalDistance(c) => c.borrow(),
ConstraintCell::FixPoint(c) => c.borrow(),
ConstraintCell::EqualLength(c) => c.borrow(),
ConstraintCell::HorizontalLine(c) => c.borrow(),
ConstraintCell::VerticalLine(c) => c.borrow(),
ConstraintCell::ParallelLines(c) => c.borrow(),
ConstraintCell::PerpendicularLines(c) => c.borrow(),
}
}

pub fn borrow_mut(&self) -> RefMut<dyn Constraint> {
match self {
ConstraintCell::AngleBetweenPoints(c) => c.borrow_mut(),
ConstraintCell::ArcEndPointCoincident(c) => c.borrow_mut(),
ConstraintCell::ArcStartPointCoincident(c) => c.borrow_mut(),
ConstraintCell::EuclideanDistance(c) => c.borrow_mut(),
ConstraintCell::HorizontalDistance(c) => c.borrow_mut(),
ConstraintCell::VerticalDistance(c) => c.borrow_mut(),
ConstraintCell::FixPoint(c) => c.borrow_mut(),
ConstraintCell::EqualLength(c) => c.borrow_mut(),
ConstraintCell::HorizontalLine(c) => c.borrow_mut(),
ConstraintCell::VerticalLine(c) => c.borrow_mut(),
ConstraintCell::ParallelLines(c) => c.borrow_mut(),
ConstraintCell::PerpendicularLines(c) => c.borrow_mut(),
}
}

pub fn as_ptr(&self) -> *const dyn Constraint {
match self {
ConstraintCell::AngleBetweenPoints(c) => c.as_ptr(),
ConstraintCell::ArcEndPointCoincident(c) => c.as_ptr(),
ConstraintCell::ArcStartPointCoincident(c) => c.as_ptr(),
ConstraintCell::EuclideanDistance(c) => c.as_ptr(),
ConstraintCell::HorizontalDistance(c) => c.as_ptr(),
ConstraintCell::VerticalDistance(c) => c.as_ptr(),
ConstraintCell::FixPoint(c) => c.as_ptr(),
ConstraintCell::EqualLength(c) => c.as_ptr(),
ConstraintCell::HorizontalLine(c) => c.as_ptr(),
ConstraintCell::VerticalLine(c) => c.as_ptr(),
ConstraintCell::ParallelLines(c) => c.as_ptr(),
ConstraintCell::PerpendicularLines(c) => c.as_ptr(),
}
}
}

impl<'a> Deserialize<'a> for ConstraintCell {
fn deserialize<D>(deserializer: D) -> Result<ConstraintCell, D::Error>
where
D: Deserializer<'a>,
{
let constraint_type = ConstraintType::deserialize(deserializer)?;
let constraint: Rc<RefCell<dyn Constraint>> = match constraint_type {
// TODO: Macro this
ConstraintType::AngleBetweenPoints(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::ArcEndPointCoincident(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::ArcStartPointCoincident(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::EuclideanDistance(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::HorizontalDistance(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::VerticalDistance(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::FixPoint(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::EqualLength(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::HorizontalLine(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::VerticalLine(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::ParallelLines(inner) => Rc::new(RefCell::new(inner)),
ConstraintType::PerpendicularLines(inner) => Rc::new(RefCell::new(inner)),
};

Ok(ConstraintCell(constraint))
// impl Constraint for ConstraintCell {
// fn references(&self) -> Vec<ParametricCell> {
// self.borrow().references()
// }

// fn loss_value(&self) -> f64 {
// self.borrow().loss_value()
// }

// fn update_gradient(&mut self) {
// self.borrow_mut().update_gradient();
// }

// fn get_type(&self) -> ConstraintType {
// self.borrow().get_type()
// }
// }

impl PartialEq for ConstraintCell {
fn eq(&self, other: &Self) -> bool {
ptr::eq(self.as_ptr(), other.as_ptr())
}
}
35 changes: 17 additions & 18 deletions src/examples/test_rectangle_axis_aligned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,52 +70,51 @@ mod tests {
// Fix point a to origin
sketch
.borrow_mut()
.add_constraint(ConstraintCell(Rc::new(RefCell::new(FixPoint::new(
point_a.clone(),
Vector2::new(0.0, 0.0),
)))))
.add_constraint(ConstraintCell::FixPoint(Rc::new(RefCell::new(
FixPoint::new(point_a.clone(), Vector2::new(0.0, 0.0)),
))))
.unwrap();

// Constrain line_a and line_c to be horizontal
sketch
.borrow_mut()
.add_constraint(ConstraintCell(Rc::new(RefCell::new(HorizontalLine::new(
line_a.clone(),
)))))
.add_constraint(ConstraintCell::HorizontalLine(Rc::new(RefCell::new(
HorizontalLine::new(line_a.clone()),
))))
.unwrap();
sketch
.borrow_mut()
.add_constraint(ConstraintCell(Rc::new(RefCell::new(HorizontalLine::new(
line_c.clone(),
)))))
.add_constraint(ConstraintCell::HorizontalLine(Rc::new(RefCell::new(
HorizontalLine::new(line_c.clone()),
))))
.unwrap();

// Constrain line_b and line_d to be vertical
sketch
.borrow_mut()
.add_constraint(ConstraintCell(Rc::new(RefCell::new(VerticalLine::new(
line_b.clone(),
)))))
.add_constraint(ConstraintCell::VerticalLine(Rc::new(RefCell::new(
VerticalLine::new(line_b.clone()),
))))
.unwrap();
sketch
.borrow_mut()
.add_constraint(ConstraintCell(Rc::new(RefCell::new(VerticalLine::new(
line_d.clone(),
)))))
.add_constraint(ConstraintCell::VerticalLine(Rc::new(RefCell::new(
VerticalLine::new(line_d.clone()),
))))
.unwrap();

// Constrain the length of line_a to 2
sketch
.borrow_mut()
.add_constraint(ConstraintCell(Rc::new(RefCell::new(
.add_constraint(ConstraintCell::HorizontalDistance(Rc::new(RefCell::new(
HorizontalDistanceBetweenPoints::new(point_a.clone(), point_b.clone(), 2.0),
))))
.unwrap();

// Constrain the length of line_b to 3
sketch
.borrow_mut()
.add_constraint(ConstraintCell(Rc::new(RefCell::new(
.add_constraint(ConstraintCell::VerticalDistance(Rc::new(RefCell::new(
VerticalDistanceBetweenPoints::new(point_a.clone(), point_d.clone(), 3.0),
))))
.unwrap();
Expand Down
Loading

0 comments on commit c4a7054

Please sign in to comment.