Skip to content

Commit

Permalink
Added a way to cast to primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasJacob committed May 26, 2024
1 parent cd982b2 commit 0575198
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/primitives/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,8 @@ impl Parametric for Arc {
fn set_data(&mut self, data: DVectorView<f64>) {
self.data = SVector::from_row_slice(data.as_slice());
}

fn as_primitive(self) -> super::Primitive {
super::Primitive::Arc(self)
}
}
4 changes: 4 additions & 0 deletions src/primitives/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ impl Parametric for Circle {
fn get_gradient(&self) -> DVector<f64> {
DVector::from_row_slice(self.gradient.as_slice())
}

fn as_primitive(self) -> super::Primitive {
super::Primitive::Circle(self)
}
}
4 changes: 4 additions & 0 deletions src/primitives/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ impl Parametric for Line {
// empty vector
DVector::from_row_slice(&[])
}

fn as_primitive(self) -> super::Primitive {
super::Primitive::Line(self)
}
}
59 changes: 59 additions & 0 deletions src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,63 @@ pub trait Parametric {
fn get_data(&self) -> DVector<f64>;
fn set_data(&mut self, data: DVectorView<f64>);
fn get_gradient(&self) -> DVector<f64>;
fn as_primitive(self) -> Primitive;
}

pub enum Primitive {
Point2(point2::Point2),
Line(line::Line),
Arc(arc::Arc),
Circle(circle::Circle),
}

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

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 as_primitive(self) -> Primitive {
self
}
}
4 changes: 4 additions & 0 deletions src/primitives/point2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ impl Parametric for Point2 {
fn get_gradient(&self) -> DVector<f64> {
DVector::from_row_slice(self.gradient.as_slice())
}

fn as_primitive(self) -> super::Primitive {
super::Primitive::Point2(self)
}
}

0 comments on commit 0575198

Please sign in to comment.