Skip to content

Commit

Permalink
impl PartialEq for Shapes, and SharedShape
Browse files Browse the repository at this point in the history
fixes #51
  • Loading branch information
RJ committed Aug 31, 2023
1 parent 7155e57 commit 4c2052a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/shape/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use either::Either;
#[cfg(feature = "rkyv")]
use rkyv::{bytecheck, CheckBytes};

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
#[cfg_attr(
Expand Down
6 changes: 6 additions & 0 deletions src/shape/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pub struct Compound {
aabb: Aabb,
}

impl PartialEq for Compound {
fn eq(&self, other: &Self) -> bool {
self.shapes() == other.shapes()
}
}

impl Compound {
/// Builds a new compound shape.
///
Expand Down
2 changes: 1 addition & 1 deletion src/shape/convex_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use na::{self, ComplexField, RealField, Unit};
derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize),
archive(check_bytes)
)]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct ConvexPolygon {
points: Vec<Point<Real>>,
normals: Vec<Unit<Vector<Real>>>,
Expand Down
6 changes: 6 additions & 0 deletions src/shape/polyline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pub struct Polyline {
indices: Vec<[u32; 2]>,
}

impl PartialEq for Polyline {
fn eq(&self, other: &Self) -> bool {
self.indices() == other.indices() && self.vertices() == other.vertices()
}
}

impl Polyline {
/// Creates a new polyline from a vertex buffer and an index buffer.
pub fn new(vertices: Vec<Point<Real>>, indices: Option<Vec<[u32; 2]>>) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/shape/round_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use na::Unit;
archive(check_bytes)
)]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(C)]
/// A shape with rounded borders.
pub struct RoundShape<S> {
Expand Down
46 changes: 46 additions & 0 deletions src/shape/shared_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use na::Unit;
use std::ops::Deref;
use std::sync::Arc;

use super::TypedShape;

/// The shape of a collider.
#[derive(Clone)]
pub struct SharedShape(pub Arc<dyn Shape>);
Expand Down Expand Up @@ -384,3 +386,47 @@ impl<'de> serde::Deserialize<'de> for SharedShape {
.ok_or(D::Error::custom("Cannot deserialize custom shape."))
}
}

impl PartialEq for SharedShape {
fn eq(&self, other: &Self) -> bool {
// shapes with different types can't be equal.
if self.shape_type() != other.shape_type() {
return false;
}
match self.as_typed_shape() {
TypedShape::Ball(shape) => shape == other.as_ball().unwrap(),
TypedShape::Cuboid(shape) => shape == other.as_cuboid().unwrap(),
TypedShape::RoundCuboid(shape) => shape == other.as_round_cuboid().unwrap(),
TypedShape::Capsule(shape) => shape == other.as_capsule().unwrap(),
TypedShape::Segment(shape) => shape == other.as_segment().unwrap(),
TypedShape::Triangle(shape) => shape == other.as_triangle().unwrap(),
TypedShape::RoundTriangle(shape) => shape == other.as_round_triangle().unwrap(),
TypedShape::TriMesh(shape) => shape == other.as_trimesh().unwrap(),
TypedShape::Polyline(shape) => shape == other.as_polyline().unwrap(),
TypedShape::HalfSpace(shape) => shape == other.as_halfspace().unwrap(),
TypedShape::HeightField(_) => false,
TypedShape::Compound(shape) => shape == other.as_compound().unwrap(),
TypedShape::Custom(_) => false,
#[cfg(feature = "dim3")]
TypedShape::ConvexPolyhedron(shape) => shape == other.as_convex_polyhedron().unwrap(),
#[cfg(feature = "dim3")]
TypedShape::Cylinder(shape) => shape == other.as_cylinder().unwrap(),
#[cfg(feature = "dim3")]
TypedShape::Cone(shape) => shape == other.as_cone().unwrap(),
#[cfg(feature = "dim3")]
TypedShape::RoundCylinder(shape) => shape == other.as_round_cylinder().unwrap(),
#[cfg(feature = "dim3")]
TypedShape::RoundCone(shape) => shape == other.as_round_cone().unwrap(),
#[cfg(feature = "dim3")]
TypedShape::RoundConvexPolyhedron(shape) => {
shape == other.as_round_convex_polyhedron().unwrap()
}
#[cfg(feature = "dim2")]
TypedShape::ConvexPolygon(shape) => shape == other.as_convex_polygon().unwrap(),
#[cfg(feature = "dim2")]
TypedShape::RoundConvexPolygon(shape) => {
shape == other.as_round_convex_polygon().unwrap()
}
}
}
}
16 changes: 16 additions & 0 deletions src/shape/trimesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,22 @@ pub struct GenericTriMesh<Storage: TriMeshStorage> {
flags: TriMeshFlags,
}

impl PartialEq for TriMesh {
fn eq(&self, other: &Self) -> bool {
let triangles = self.triangles();
let other_triangles = other.triangles();
if triangles.len() != other_triangles.len() {
return false;
}
for (t1, t2) in triangles.zip(other_triangles) {
if t1 != t2 {
return false;
}
}
true
}
}

/// A triangle-mesh.
pub type TriMesh = GenericTriMesh<DefaultStorage>;
#[cfg(feature = "cuda")]
Expand Down

0 comments on commit 4c2052a

Please sign in to comment.