-
Notifications
You must be signed in to change notification settings - Fork 200
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
Add type Bbox and trait BoundingBox #41
Changes from 1 commit
7c65977
0337873
eccc0c9
b65d3c0
74a316d
49b6172
ce28cca
2134a69
de27c9e
ac69af5
12ef2a5
3ecca84
6556567
8fdb5c9
8e348a3
ab61011
c213d43
5bae671
9bec1b8
6f16131
03293f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use num::{Float}; | ||
use num::Float; | ||
|
||
use types::{Bbox, Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon}; | ||
|
||
|
@@ -25,36 +25,32 @@ pub trait BoundingBox<T: Float> { | |
fn bbox(&self) -> Option<Bbox<T>>; | ||
} | ||
|
||
|
||
fn get_min_max<T>(p: T, min: T, max: T) -> (T, T) | ||
where T: Float | ||
{ | ||
if p > max {(min, p)} else if p < min {(p, max)} else {(min, max)} | ||
} | ||
|
||
fn get_bbox<T>(vect: &Vec<Point<T>>) -> Option<Bbox<T>> | ||
where T: Float | ||
{ | ||
if vect.is_empty() { | ||
return None; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return is now useless: None There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd better if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK for me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment addressed. |
||
} | ||
if vect.len() == 1 { | ||
return Some(Bbox{xmin: vect[0].x(), ymax: vect[0].y(), | ||
xmax: vect[0].x(), ymin: vect[0].y()}) | ||
} else { | ||
let (mut xmax, mut xmin) = (T::neg_infinity(), T::infinity()); | ||
let (mut ymax, mut ymin) = (T::neg_infinity(), T::infinity()); | ||
for pnt in vect.iter() { | ||
let mut xrange = (vect[0].x(), vect[0].x()); | ||
let mut yrange = (vect[0].y(), vect[0].y()); | ||
for pnt in vect[1..].iter() { | ||
let (px, py) = (pnt.x(), pnt.y()); | ||
if px > xmax { | ||
xmax = px; | ||
} else if px < xmin { | ||
xmin = px; | ||
} | ||
if py > ymax { | ||
ymax = py; | ||
} else if py < ymin { | ||
ymin = py; | ||
} | ||
xrange = get_min_max(px, xrange.0, xrange.1); | ||
yrange = get_min_max(py, yrange.0, yrange.1); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be written using a fold. That's just personal preference, so change it if you prefere but keep it like that if you prefere this imperative form. |
||
Some(Bbox{xmin: xmin, ymax: ymax, | ||
xmax: xmax, ymin: ymin}) | ||
Some(Bbox{xmin: xrange.0, xmax: xrange.1, | ||
ymin: yrange.0, ymax: yrange.1}) | ||
} | ||
} | ||
|
||
|
||
impl<T> BoundingBox<T> for MultiPoint<T> | ||
where T: Float | ||
{ | ||
|
@@ -119,7 +115,7 @@ impl<T> BoundingBox<T> for MultiPolygon<T> | |
where T: Float | ||
{ | ||
/// | ||
/// Return the BoundingBox for a MultiLineString | ||
/// Return the BoundingBox for a MultiPolygon | ||
/// | ||
fn bbox(&self) -> Option<Bbox<T>> { | ||
let vect = &self.0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can rewrite this function to take an
IntoIterator<Item = &Point<T>>
. Then, multisomething is just a flat_map.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Mon, Jul 18, 2016 at 9:16 AM, Guillaume P. notifications@github.com
wrote:
Sorry I don't understand how I should modify the function, any hints?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is not addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prototype of the function:
Then, the fold version that I've given should almost work. You can also write an imperative version using for loop.
You can then rewrite MultiSomething as something that look like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Mon, Jul 18, 2016 at 6:29 PM, Guillaume P. notifications@github.com
wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea: https://is.gd/iYnUSP
If you have any question, don't hesitate to ask.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Tue, Jul 19, 2016 at 10:57 AM, Guillaume P. notifications@github.com
wrote:
I've worked on the get_bbox function now it is working accepting an
iterator instead of a vector.
But I'm not able to get the MultiLineString working, if I try with:
get_bbox(&self.0.iter().flat_map(|line| line.0.iter()))
this is the error:
I've looked around but I was not able to fix it myself.
So I've commit the last working version. Can you explain me what is wrong
with: get_bbox(&self.0.iter().flat_map(|line| line.0.iter()))?
Thank you for your patience and support!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you give a reference to an iterator, but you should give directly the iterator that implement IntoIterator.
Thus, just remove the
&
should fix the problem: