Skip to content

Commit

Permalink
Clean up internal representation of Segment
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Mar 17, 2022
1 parent 3ea35f1 commit 6ee9693
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions fj-math/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@ use super::Point;
/// parameter.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Segment<const D: usize> {
a: Point<D>,
b: Point<D>,
points: [Point<D>; 2],
}

impl<const D: usize> Segment<D> {
/// Access the points of the segment
pub fn points(&self) -> [Point<D>; 2] {
[self.a, self.b]
self.points
}
}

impl Segment<2> {
/// Convert the 2-dimensional segment to a Parry segment
pub fn to_parry(self) -> parry2d_f64::shape::Segment {
[self.a.to_na(), self.b.to_na()].into()
self.points.map(|point| point.to_na()).into()
}
}

impl Segment<3> {
/// Convert the 3-dimensional segment to a Parry segment
pub fn to_parry(self) -> parry3d_f64::shape::Segment {
[self.a.to_na(), self.b.to_na()].into()
self.points.map(|point| point.to_na()).into()
}
}

Expand All @@ -39,15 +38,12 @@ impl<const D: usize> From<[Point<D>; 2]> for Segment<D> {

assert!(a != b, "Invalid segment; both points are identical {a:?}");

Self {
a: points[0],
b: points[1],
}
Self { points }
}
}

impl<const D: usize> fmt::Debug for Segment<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{:?} -> {:?}]", self.a, self.b)
write!(f, "[{:?} -> {:?}]", self.points[0], self.points[1])
}
}

0 comments on commit 6ee9693

Please sign in to comment.