Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand and clean up API of Vector #1161

Merged
merged 3 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Cycle {
"Invalid cycle: less than 3 edges, but not all are circles"
),
};
let cross_positive = circle.a().cross(&circle.b()) > Scalar::ZERO;
let cross_positive = circle.a().cross2d(&circle.b()) > Scalar::ZERO;

if edge_direction_positive == cross_positive {
return Winding::Ccw;
Expand Down
12 changes: 6 additions & 6 deletions crates/fj-math/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ impl Scalar {
}
}

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

impl Eq for Scalar {}

impl PartialOrd for Scalar {
Expand All @@ -166,12 +172,6 @@ impl Ord for Scalar {
}
}

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

impl Hash for Scalar {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
// To the best of my knowledge, this matches the `PartialEq`
Expand Down
17 changes: 16 additions & 1 deletion crates/fj-math/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,15 @@ impl Vector<2> {
}

/// Compute the 2D cross product with another vector
pub fn cross(&self, other: &Self) -> Scalar {
pub fn cross2d(&self, other: &Self) -> Scalar {
(self.u * other.v) - (self.v * other.u)
}

/// Determine whether this vector is between two other vectors
pub fn is_between(&self, others: [impl Into<Self>; 2]) -> bool {
let [a, b] = others.map(Into::into);
a.cross2d(self) * b.cross2d(self) < Scalar::ZERO
}
}

impl Vector<3> {
Expand Down Expand Up @@ -390,4 +396,13 @@ mod tests {
Scalar::ZERO
);
}

#[test]
fn is_between() {
let v = Vector::from([1., 1.]);

assert!(v.is_between([[1., 0.], [0., 1.]]));
assert!(!v.is_between([[1., 0.], [0., -1.]]));
assert!(!v.is_between([[-1., 0.], [0., 1.]]));
}
}