Clean up internal representation of Segment

This commit is contained in:
Hanno Braun 2022-03-17 15:20:03 +01:00
parent 3ea35f18d6
commit 6ee9693cdc

View File

@ -8,28 +8,27 @@ use super::Point;
/// parameter. /// parameter.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)] #[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Segment<const D: usize> { pub struct Segment<const D: usize> {
a: Point<D>, points: [Point<D>; 2],
b: Point<D>,
} }
impl<const D: usize> Segment<D> { impl<const D: usize> Segment<D> {
/// Access the points of the segment /// Access the points of the segment
pub fn points(&self) -> [Point<D>; 2] { pub fn points(&self) -> [Point<D>; 2] {
[self.a, self.b] self.points
} }
} }
impl Segment<2> { impl Segment<2> {
/// Convert the 2-dimensional segment to a Parry segment /// Convert the 2-dimensional segment to a Parry segment
pub fn to_parry(self) -> parry2d_f64::shape::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> { impl Segment<3> {
/// Convert the 3-dimensional segment to a Parry segment /// Convert the 3-dimensional segment to a Parry segment
pub fn to_parry(self) -> parry3d_f64::shape::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()
} }
} }
@ -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:?}"); assert!(a != b, "Invalid segment; both points are identical {a:?}");
Self { Self { points }
a: points[0],
b: points[1],
}
} }
} }
impl<const D: usize> fmt::Debug for Segment<D> { impl<const D: usize> fmt::Debug for Segment<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{:?} -> {:?}]", self.a, self.b) write!(f, "[{:?} -> {:?}]", self.points[0], self.points[1])
} }
} }