Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasJacob committed May 27, 2024
1 parent 602557d commit 3a03055
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 98 deletions.
66 changes: 14 additions & 52 deletions src/constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,61 +43,23 @@ pub enum Constraint {
PerpendicularLines(lines::perpendicular_lines::PerpendicularLines),
}

impl ConstraintLike for Constraint {
fn references(&self) -> Vec<PrimitiveCell> {
impl Constraint {
pub fn as_constraint_like(&self) -> &dyn ConstraintLike {
match self {
Constraint::AngleBetweenPoints(c) => c.references(),
Constraint::ArcEndPointCoincident(c) => c.references(),
Constraint::ArcStartPointCoincident(c) => c.references(),
Constraint::EuclideanDistance(c) => c.references(),
Constraint::HorizontalDistance(c) => c.references(),
Constraint::VerticalDistance(c) => c.references(),
Constraint::FixPoint(c) => c.references(),
Constraint::EqualLength(c) => c.references(),
Constraint::HorizontalLine(c) => c.references(),
Constraint::VerticalLine(c) => c.references(),
Constraint::ParallelLines(c) => c.references(),
Constraint::PerpendicularLines(c) => c.references(),
Constraint::AngleBetweenPoints(c) => c,
Constraint::ArcEndPointCoincident(c) => c,
Constraint::ArcStartPointCoincident(c) => c,
Constraint::EuclideanDistance(c) => c,
Constraint::HorizontalDistance(c) => c,
Constraint::VerticalDistance(c) => c,
Constraint::FixPoint(c) => c,
Constraint::EqualLength(c) => c,
Constraint::HorizontalLine(c) => c,
Constraint::VerticalLine(c) => c,
Constraint::ParallelLines(c) => c,
Constraint::PerpendicularLines(c) => c,
}
}

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

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

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

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
52 changes: 6 additions & 46 deletions src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,55 +34,15 @@ pub enum Primitive {
Circle(circle::Circle),
}

impl PrimitiveLike for Primitive {
fn references(&self) -> Vec<PrimitiveCell> {
impl Primitive {
pub fn as_primitive_like(&self) -> &dyn PrimitiveLike {
match self {
Primitive::Point2(p) => p.references(),
Primitive::Line(l) => l.references(),
Primitive::Arc(a) => a.references(),
Primitive::Circle(c) => c.references(),
Primitive::Point2(p) => p,
Primitive::Line(l) => l,
Primitive::Arc(a) => a,
Primitive::Circle(c) => c,
}
}

fn zero_gradient(&mut self) {
match self {
Primitive::Point2(p) => p.zero_gradient(),
Primitive::Line(l) => l.zero_gradient(),
Primitive::Arc(a) => a.zero_gradient(),
Primitive::Circle(c) => c.zero_gradient(),
}
}

fn get_data(&self) -> DVector<f64> {
match self {
Primitive::Point2(p) => p.get_data(),
Primitive::Line(l) => l.get_data(),
Primitive::Arc(a) => a.get_data(),
Primitive::Circle(c) => c.get_data(),
}
}

fn set_data(&mut self, data: DVectorView<f64>) {
match self {
Primitive::Point2(p) => p.set_data(data),
Primitive::Line(l) => l.set_data(data),
Primitive::Arc(a) => a.set_data(data),
Primitive::Circle(c) => c.set_data(data),
}
}

fn get_gradient(&self) -> DVector<f64> {
match self {
Primitive::Point2(p) => p.get_gradient(),
Primitive::Line(l) => l.get_gradient(),
Primitive::Arc(a) => a.get_gradient(),
Primitive::Circle(c) => c.get_gradient(),
}
}

fn to_primitive(&self) -> Primitive {
self.clone()
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down

0 comments on commit 3a03055

Please sign in to comment.