Skip to content

Commit

Permalink
Implement PartialEq for geometric types (fixes hannobraun#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jul 18, 2022
1 parent 4dc8ae8 commit ecfd33c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/fj/src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::f64::consts::{PI, TAU};
const GON_RAD: f64 = PI / 200.;

/// An angle
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Angle {
// The value of the angle in radians
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::Shape;
/// # Limitations
///
/// Whether the shapes in the group touch or overlap is not currently checked.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Group {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use fj_proc::*;
use serde::{Deserialize, Serialize};

/// A shape
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub enum Shape {
Expand Down
21 changes: 16 additions & 5 deletions crates/fj/src/shape_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::atomic;
use crate::Shape;

/// A 2-dimensional shape
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub enum Shape2d {
Expand All @@ -28,7 +28,7 @@ impl Shape2d {
}

/// A difference between two shapes
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Difference2d {
Expand Down Expand Up @@ -73,7 +73,7 @@ impl From<Difference2d> for Shape2d {
/// Nothing about these edges is checked right now, but algorithms might assume
/// that the edges are non-overlapping. If you create a `Sketch` with
/// overlapping edges, you're on your own.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Sketch {
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Sketch {
}

/// A chain of elements that is part of a [`Sketch`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub enum Chain {
Expand All @@ -130,7 +130,7 @@ pub enum Chain {
}

/// A circle that is part of a [`Sketch`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Circle {
Expand Down Expand Up @@ -191,6 +191,11 @@ impl PolyChain {
}
}

/// Get a reference to the points in this [`PolyChain`].
fn points(&self) -> &[[f64; 2]] {
unsafe { std::slice::from_raw_parts(self.ptr, self.length) }
}

/// Return the points that define the polygonal chain
pub fn to_points(&self) -> Vec<[f64; 2]> {
// This is sound. All invariants are automatically kept, as the raw
Expand Down Expand Up @@ -229,6 +234,12 @@ impl Clone for PolyChain {
}
}

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

impl Drop for PolyChain {
fn drop(&mut self) {
// Decrement the reference counter
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::{Shape, Shape2d};

/// A sweep of a 2-dimensional shape along straight path
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Sweep {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{Angle, Shape};
///
/// See issue:
/// <https://github.com/hannobraun/Fornjot/issues/101>
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Transform {
Expand Down

0 comments on commit ecfd33c

Please sign in to comment.