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

Vector2DOps Trait - Proposal #1025

Merged
merged 13 commits into from
Jul 31, 2023
2 changes: 2 additions & 0 deletions geo/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Add `TriangulateEarcut` algorithm trait to triangulate polygons with the earcut algorithm.
- <https://github.com/georust/geo/pull/1007>
- Add `Vector2DOps` trait to algorithims module and implemented it for `Coord<T::CoordFloat>`
- <https://github.com/georust/geo/pull/1025>

## 0.25.0

Expand Down
4 changes: 4 additions & 0 deletions geo/src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ pub mod triangulate_earcut;
#[cfg(feature = "earcutr")]
pub use triangulate_earcut::TriangulateEarcut;

/// Vector Operations for 2D coordinates
mod vector_ops;
pub use vector_ops::Vector2DOps;

/// Calculate the Vincenty distance between two `Point`s.
pub mod vincenty_distance;
pub use vincenty_distance::VincentyDistance;
Expand Down
347 changes: 347 additions & 0 deletions geo/src/algorithm/vector_ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,347 @@
//! This module defines the [Vector2DOps] trait and implements it for the
//! [Coord] struct.

use crate::{Coord, CoordFloat, CoordNum};

/// Defines vector operations for 2D coordinate types which implement CoordFloat
///
/// This trait is intended for internal use within the geo crate as a way to
/// bring together the various hand-crafted linear algebra operations used
/// throughout other algorithms and attached to various structs.
///
///

pub trait Vector2DOps<Rhs = Self>
where
Self: Sized,
{
type Scalar: CoordNum + Send + Sync;

/// The euclidean distance between this coordinate and the origin
///
/// `sqrt(x² + y²)`
///
fn magnitude(self) -> Self::Scalar;

/// The squared distance between this coordinate and the origin.
/// (Avoids the square root calculation when it is not needed)
///
/// `x² + y²`
///
fn magnitude_squared(self) -> Self::Scalar;

/// Rotate this coordinate around the origin by 90 degrees clockwise.
///
/// `a.left() => (-a.y, a.x)`
///
/// Assumes a coordinate system where positive `y` is up and positive `x` is
/// to the right. The described rotation direction is consistent with the
/// documentation for [crate::algorithm::rotate::Rotate].
fn left(self) -> Self;

/// Rotate this coordinate around the origin by 90 degrees anti-clockwise.
///
/// `a.right() => (a.y, -a.x)`
///
/// Assumes a coordinate system where positive `y` is up and positive `x` is
/// to the right. The described rotation direction is consistent with the
/// documentation for [crate::algorithm::rotate::Rotate].
fn right(self) -> Self;

/// The inner product of the coordinate components
///
/// `a · b = a.x * b.x + a.y * b.y`
///
fn dot_product(self, other: Rhs) -> Self::Scalar;

/// The calculates the `wedge product` between two vectors.
///
/// `a ∧ b = a.x * b.y - a.y * b.x`
///
/// Also known as:
///
/// - `exterior product`
/// - because the wedge product comes from 'Exterior Algebra'
/// - `perpendicular product`
/// - because it is equivalent to `a.dot(b.right())`
/// - `2D cross product`
/// - because it is equivalent to the signed magnitude of the
/// conventional 3D cross product assuming `z` ordinates are zero
/// - `determinant`
/// - because it is equivalent to the `determinant` of the 2x2 matrix
/// formed by the column-vector inputs.
///
/// ## Examples
///
/// The following list highlights some examples in geo which might be
/// brought together to use this function:
///
/// 1. [geo_types::Point::cross_prod()] is already defined on
/// [geo_types::Point]... but that it seems to be some other
/// operation on 3 points??
/// 2. [geo_types::Line] struct also has a [geo_types::Line::determinant()]
/// function which is the same as `line.start.wedge_product(line.end)`
/// 3. The [crate::algorithm::Kernel::orient2d()] trait default
/// implementation uses cross product to compute orientation. It returns
/// an enum, not the numeric value which is needed for line segment
/// intersection.
///
/// ## Properties
///
/// - The absolute value of the cross product is the area of the
/// parallelogram formed by the operands
/// - Anti-commutative: The sign of the output is reversed if the operands
/// are reversed
/// - If the operands are colinear with the origin, the value is zero
/// - The sign can be used to check if the operands are clockwise with
/// respect to the origin, or phrased differently:
/// "is a to the left of the line between the origin and b"?
/// - If this is what you are using it for, then please use
/// [crate::algorithm::Kernel::orient2d()] instead as this is more
/// explicit and has a `RobustKernel` option for extra precision.
fn wedge_product(self, other: Rhs) -> Self::Scalar;

/// Try to find a vector of unit length in the same direction as this
/// vector.
///
/// Returns `None` if the magnitude of this vector is less than
/// `minimum_magnitude` or the magnitude is not finite.
/// - For f32 the minimum_magnitude can be set to about `1e-30_f32`
/// - For F64 the minimum_magnitude can be set to about `2e-301_f64`
///
/// These values should avoid overflowing to Infinity for coordinate values
/// in the range typically relevant for spatial data (+-40e6) which is the
/// approximate length of the earth's great circle in metres.
fn try_normalize(self, minimum_magnitude: Self::Scalar) -> Option<Self>;
}

impl<T> Vector2DOps for Coord<T>
where
T: CoordFloat + Send + Sync,
{
type Scalar = T;

fn wedge_product(self, right: Coord<T>) -> Self::Scalar {
self.x * right.y - self.y * right.x
}

fn dot_product(self, other: Self) -> Self::Scalar {
self.x * other.x + self.y * other.y
}

fn magnitude(self) -> Self::Scalar {
(self.x * self.x + self.y * self.y).sqrt()
}

fn magnitude_squared(self) -> Self::Scalar {
self.x * self.x + self.y * self.y
}

fn left(self) -> Self {
Self {
x: -self.y,
y: self.x,
}
}

fn right(self) -> Self {
Self {
x: self.y,
y: -self.x,
}
}

fn try_normalize(self, minimum_magnitude: Self::Scalar) -> Option<Self> {
let magnitude = self.magnitude();
if magnitude.is_finite() && magnitude.abs() > minimum_magnitude {
Some(self / magnitude)
} else {
None
}
}
}

#[cfg(test)]
mod test {
// crate dependencies
use crate::coord;

// private imports
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// private imports

nit: comment out of date since this is no longer private

use super::Vector2DOps;

#[test]
fn test_cross_product() {
// perpendicular unit length
let a = coord! { x: 1f64, y: 0f64 };
let b = coord! { x: 0f64, y: 1f64 };

// expect the area of parallelogram
assert_eq!(a.wedge_product(b), 1f64);
// expect swapping will result in negative
assert_eq!(b.wedge_product(a), -1f64);

// Add skew; expect results should be the same
let a = coord! { x: 1f64, y: 0f64 };
let b = coord! { x: 1f64, y: 1f64 };

// expect the area of parallelogram
assert_eq!(a.wedge_product(b), 1f64);
// expect swapping will result in negative
assert_eq!(b.wedge_product(a), -1f64);

// Make Colinear; expect zero
let a = coord! { x: 2f64, y: 2f64 };
let b = coord! { x: 1f64, y: 1f64 };
assert_eq!(a.wedge_product(b), 0f64);
}

#[test]
fn test_dot_product() {
// perpendicular unit length
let a = coord! { x: 1f64, y: 0f64 };
let b = coord! { x: 0f64, y: 1f64 };
// expect zero for perpendicular
assert_eq!(a.dot_product(b), 0f64);

// Parallel, same direction
let a = coord! { x: 1f64, y: 0f64 };
let b = coord! { x: 2f64, y: 0f64 };
// expect +ive product of magnitudes
assert_eq!(a.dot_product(b), 2f64);
// expect swapping will have same result
assert_eq!(b.dot_product(a), 2f64);

// Parallel, opposite direction
let a = coord! { x: 3f64, y: 4f64 };
let b = coord! { x: -3f64, y: -4f64 };
// expect -ive product of magnitudes
assert_eq!(a.dot_product(b), -25f64);
// expect swapping will have same result
assert_eq!(b.dot_product(a), -25f64);
}

#[test]
fn test_magnitude() {
let a = coord! { x: 1f64, y: 0f64 };
assert_eq!(a.magnitude(), 1f64);

let a = coord! { x: 0f64, y: 0f64 };
assert_eq!(a.magnitude(), 0f64);

let a = coord! { x: -3f64, y: 4f64 };
assert_eq!(a.magnitude(), 5f64);
}

#[test]
fn test_magnitude_squared() {
let a = coord! { x: 1f64, y: 0f64 };
assert_eq!(a.magnitude_squared(), 1f64);

let a = coord! { x: 0f64, y: 0f64 };
assert_eq!(a.magnitude_squared(), 0f64);

let a = coord! { x: -3f64, y: 4f64 };
assert_eq!(a.magnitude_squared(), 25f64);
}

#[test]
fn test_left_right() {
let a = coord! { x: 1f64, y: 0f64 };
let a_left = coord! { x: 0f64, y: 1f64 };
let a_right = coord! { x: 0f64, y: -1f64 };

assert_eq!(a.left(), a_left);
assert_eq!(a.right(), a_right);
assert_eq!(a.left(), -a.right());
}

#[test]
fn test_left_right_match_rotate() {
use crate::algorithm::rotate::Rotate;
use crate::Point;
// The aim of this test is to confirm that wording in documentation is
// consistent.

// when the user is in a coordinate system where the y axis is flipped
// (eg screen coordinates in a HTML canvas), then rotation directions
// will be different to those described in the documentation.

// The documentation for the Rotate trait says: 'Positive angles are
// counter-clockwise, and negative angles are clockwise rotations'

let counter_clockwise_rotation_degrees = 90.0;
let clockwise_rotation_degrees = -counter_clockwise_rotation_degrees;

let a: Point = coord! { x: 1.0, y: 0.0 }.into();
let origin: Point = coord! { x: 0.0, y: 0.0 }.into();

// left is anti-clockwise
assert_relative_eq!(
Point::from(a.0.left()),
a.rotate_around_point(counter_clockwise_rotation_degrees, origin),
);
// right is clockwise
assert_relative_eq!(
Point::from(a.0.right()),
a.rotate_around_point(clockwise_rotation_degrees, origin),
);
}

#[test]
fn test_normalize() {
let f64_minimum_threshold = 2e-301f64;
// Already Normalized
let a = coord! {
x: 1.0,
y: 0.0
};
assert_relative_eq!(a.try_normalize(f64_minimum_threshold).unwrap(), a);

// Already Normalized
let a = coord! {
x: 1.0 / f64::sqrt(2.0),
y: -1.0 / f64::sqrt(2.0)
};
assert_relative_eq!(a.try_normalize(f64_minimum_threshold).unwrap(), a);

// Non trivial example
let a = coord! { x: -10.0, y: 8.0 };
assert_relative_eq!(
a.try_normalize(f64_minimum_threshold).unwrap(),
coord! { x: -10.0, y: 8.0 } / f64::sqrt(10.0 * 10.0 + 8.0 * 8.0)
);

// Very Small Input - Fails because below threshold
let a = coord! {
x: 0.0,
y: f64_minimum_threshold / 2.0
};
assert_eq!(a.try_normalize(f64_minimum_threshold), None);

// Zero vector - Fails because below threshold
let a = coord! { x: 0.0, y: 0.0 };
assert_eq!(a.try_normalize(f64_minimum_threshold), None);

// Large vector Pass;
// Note: this fails because the magnitude calculation overflows to infinity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems wrong - this example passes, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes correct that one is meant to pass... looks like I botched a copy paste

// this could be avoided my modifying the implementation to use scaling be avoided using scaling
let a = coord! {
x: f64::sqrt(f64::MAX/2.0),
y: f64::sqrt(f64::MAX/2.0)
};
assert!(a.try_normalize(f64_minimum_threshold).is_some());

// Large Vector Fail;
// Note: This test uses the unstable float_next_up_down feature
// let a = coord! { x: f64::sqrt(f64::MAX/2.0), y: f64::next_up(f64::sqrt(f64::MAX/2.0)) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but if you wanted to show it failing how about something simpler like:

Suggested change
// let a = coord! { x: f64::sqrt(f64::MAX/2.0), y: f64::next_up(f64::sqrt(f64::MAX/2.0)) };
let a = coord! { x: f64::sqrt(f64::MAX/2.0 + 1.0), y: f64::next_up(f64::sqrt(f64::MAX/2.0 + 1.0)) };

Copy link
Contributor Author

@thehappycheese thehappycheese Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @michaelkirk; I think you meant to remove the nightly-only f64::next_up from your suggestion and use + 1.0 instead?

The trouble is that for very large floats we don't know the next largest representable number so we can get the unexpected result;

assert_eq!(f64::MAX/2.0 + 1.0, f64::MAX/2.0); // passes unexpectedly

I have just spotted that there is already a dependency on the float_next_after crate. I will use that instead.


I just had a bit of a noodle and it turns out the difference between f64::MAX/2.0 and the next highest representable number is something like 9.9e291 which is kinda confusing to think about.

println!("{:e}",  f64::MAX / 2.0);
println!("{:e}", (f64::MAX/2.0).next_up());
println!("{:e}", (f64::MAX/2.0).next_up() - f64::MAX / 2.0);
// 8.988465674311579e307
//  8.98846567431158e307
//   9.9792015476736e291

Now I am wondering about the value of some of the asserts in test_normalize(); Perhaps some are really testing the f64 implementation more than the try_normalize and should be removed?

Maybe the best thing is to try add some notes to the documentation rather than try to check everything in the tests? Just thinking out loud here; There are basically 2 ways it can go wrong;

  1. The user has some massive vector (plausibly arising from some algorithm that sums many vectors) where the sum of the squares overflows before the square root. This situation could be mitigated by detecting large vectors and scaling down before doing the calculation.
  2. The user normalizes a really small vector and has chosen a bad value for the threshold causing a divide by zero or overflow (both result in f64::INFINITY).
    • Actually on further investigation coord!{x:0,y:1.0/f64::MAX}.try_normalize(0.0) returns None because coord!{x:0,y:1.0/f64::MAX}.magnitude() underflows to 0.0 😟. This is the same problem in reverse, and it means there are many valid small vectors that will return None despite the magnitude being greater than the user's chosen threshold. This situation could be mitigated by first scaling up the vector before normalizing.

Ok after that 45 min decent into madness I have an idea; it is easier to just check if the magnitude and the final result are finite after the calculation. There is no point guarding against invalid float operations since they do not panic anyway. Also this new version is less annoying to use because you don't need to provide a minimum threshold;

    fn try_normalize(self) -> Option<Self> {
        let magnitude = self.magnitude();
        let result = self / magnitude;
        if result.is_finite() && magnitude.is_finite() {
            Some(result)
        } else {
            None
        }
    }

    fn is_finite(self) -> bool {
        self.x.is_finite() && self.y.is_finite()
    }

I have also improved the doc comment in my next commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reading through my unreasonable suggestion and coming up with something better!

// assert!(a.try_normalize(1e-10f64).is_none());

// NaN
let a = coord! { x: f64::NAN, y: 0.0 };
assert_eq!(a.try_normalize(f64_minimum_threshold), None);

// Infinite
let a = coord! { x: f64::INFINITY, y: 0.0 };
assert_eq!(a.try_normalize(f64_minimum_threshold), None);
}
}