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

Implement a Convex Hull method #87

Closed
wants to merge 4 commits into from
Closed

Conversation

urschrei
Copy link
Member

The current implementation is slow (the worst case is On4) compared to e.g.
Quickhull (I struggled with the recursion + retained points implementation), but it's a reasonable starting point, and a convex hull method is the fundamental building block for Delaunay triangulation and Voronoi tessellation methods.

The current implementation isn't as fast as e.g.
Quickhull, but it's a reasonable starting point
// Adapted from http://codereview.stackexchange.com/a/141752/2630
// The algorithm is from Heineman, G.T., Pollice, G., Selkow, S., 2008.
// "Algorithms in a Nutshell". O’Reilly Media, Inc., pp261–8
pub fn convex_hull<T>(points: &BTreeSet<Point<T>>) -> Vec<Point<T>>
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't need to be pub, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops, nope.

use types::{Point, Polygon, LineString};
use std::collections::BTreeSet;

impl<T> Eq for Point<T> where T: Float {}
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ord needs Eq, and deriving it in types.rs didn't work…

Copy link
Member

Choose a reason for hiding this comment

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

Is there some reason you need Ord? IIRC, num::Float intentionally doesn't implement Ord since it can't guarantee total order...I think?

Copy link
Member Author

@urschrei urschrei Feb 12, 2017

Choose a reason for hiding this comment

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

This is a problem related to punting on NaN values – BTreeSet needs Ord, but as you say, there's no total ordering for types which include NaN. However, I'm not sure that it should be possible to construct geometries with NaN values in the first place.
I feel like there should be a more elegant solution here, but it'll need input from someone who knows more than I do about floating point…

Copy link
Member

Choose a reason for hiding this comment

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

BTreeSet needs Ord

Ah right, forgot about this. What you have is fine for now. Can you just add a comment next to the Ord implementation stating that it's needed for BTreeSet?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

// we need to close the Polygon
let final_element = hull[0].clone();
hull.push(final_element);
hull.into_iter().collect::<Vec<Point<T>>>()
Copy link
Member

Choose a reason for hiding this comment

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

Prior to this line, is hull not already a Vec<Point<T>>?

impl<T> Convexhull<T> for Polygon<T>
where T: Float
{
fn convexhull(&self) -> Polygon<T> {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: If "convex hull" is two words, I'd personally call this ConvexHull::convex_hull

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

Copy link
Member

@frewsxcv frewsxcv left a comment

Choose a reason for hiding this comment

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

Awesome work, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants