Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Sep 12, 2017
1 parent 0a4dfff commit 7d36831
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 84 deletions.
7 changes: 4 additions & 3 deletions src/algorithm/boundingbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ impl<T> BoundingBox<T> for LineString<T>
/// Return the BoundingBox for a LineString
///
fn bbox(&self) -> Self::Output {
get_bbox(&self.0)
// TODO this should return a Bbox
get_bbox(self.points())
}
}

Expand All @@ -106,7 +107,7 @@ impl<T> BoundingBox<T> for MultiLineString<T>
/// Return the BoundingBox for a MultiLineString
///
fn bbox(&self) -> Self::Output {
get_bbox(self.0.iter().flat_map(|line| line.0.iter()))
get_bbox(self.0.iter().flat_map(|line| line.points().iter()))
}
}

Expand All @@ -133,7 +134,7 @@ impl<T> BoundingBox<T> for MultiPolygon<T>
/// Return the BoundingBox for a MultiPolygon
///
fn bbox(&self) -> Self::Output {
get_bbox(self.0.iter().flat_map(|poly| (poly.exterior).0.iter()))
get_bbox(self.0.iter().flat_map(|poly| poly.exterior.points().iter()))
}
}

Expand Down
72 changes: 27 additions & 45 deletions src/algorithm/centroid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,15 @@ impl<T> Centroid<T> for LineString<T>
// The Centroid of a LineString is the mean of the middle of the segment
// weighted by the length of the segments.
fn centroid(&self) -> Self::Output {
if self.0.is_empty() {
return None;
}
if self.0.len() == 1 {
Some(self.0[0].clone())
} else {
let mut sum_x = T::zero();
let mut sum_y = T::zero();
let mut total_length = T::zero();
for line in self.lines() {
let segment_len = line.length();
let (x1, y1, x2, y2) = (line.start.x(), line.start.y(), line.end.x(), line.end.y());
total_length = total_length + segment_len;
sum_x = sum_x + segment_len * ((x1 + x2) / (T::one() + T::one()));
sum_y = sum_y + segment_len * ((y1 + y2) / (T::one() + T::one()));
}
Some(Point::new(sum_x / total_length, sum_y / total_length))
let mut sum_x = T::zero();
let mut sum_y = T::zero();
let mut total_length = T::zero();
for line in self.lines() {
let segment_len = line.length();
let (x1, y1, x2, y2) = (line.start.x(), line.start.y(), line.end.x(), line.end.y());
total_length = total_length + segment_len;
sum_x = sum_x + segment_len * ((x1 + x2) / (T::one() + T::one()));
sum_y = sum_y + segment_len * ((y1 + y2) / (T::one() + T::one()));
}
Some(Point::new(sum_x / total_length, sum_y / total_length))
}
Expand All @@ -110,34 +102,24 @@ impl<T> Centroid<T> for Polygon<T>
// See here for a formula: http://math.stackexchange.com/a/623849
// See here for detail on alternative methods: https://fotino.me/calculating-centroids/
fn centroid(&self) -> Self::Output {
let linestring = &self.exterior;
let vect = &linestring.0;
if vect.is_empty() {
return None;
}
if vect.len() == 1 {
Some(Point::new(vect[0].x(), vect[0].y()))
} else {
let external_centroid = simple_polygon_centroid(&self.exterior).unwrap();
if !self.interiors.is_empty() {
let external_area = simple_polygon_area(&self.exterior).abs();
// accumulate interior Polygons
let (totals_x, totals_y, internal_area) =
self.interiors
.iter()
.map(|ring| {
let area = simple_polygon_area(ring).abs();
let centroid = simple_polygon_centroid(ring).unwrap();
((centroid.x() * area), (centroid.y() * area), area)
})
.fold((T::zero(), T::zero(), T::zero()),
|accum, val| (accum.0 + val.0, accum.1 + val.1, accum.2 + val.2));
return Some(Point::new(((external_centroid.x() * external_area) - totals_x) /
(external_area - internal_area),
((external_centroid.y() * external_area) - totals_y) /
(external_area - internal_area)));
}
Some(external_centroid)
let external_centroid = simple_polygon_centroid(&self.exterior).unwrap();
if !self.interiors.is_empty() {
let external_area = simple_polygon_area(&self.exterior).abs();
// accumulate interior Polygons
let (totals_x, totals_y, internal_area) =
self.interiors
.iter()
.map(|ring| {
let area = simple_polygon_area(ring).abs();
let centroid = simple_polygon_centroid(ring).unwrap();
((centroid.x() * area), (centroid.y() * area), area)
})
.fold((T::zero(), T::zero(), T::zero()),
|accum, val| (accum.0 + val.0, accum.1 + val.1, accum.2 + val.2));
return Some(Point::new(((external_centroid.x() * external_area) - totals_x) /
(external_area - internal_area),
((external_centroid.y() * external_area) - totals_y) /
(external_area - internal_area)));
}
Some(external_centroid)
}
Expand Down
16 changes: 2 additions & 14 deletions src/algorithm/contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,8 @@ impl<T> Contains<Point<T>> for LineString<T>
where T: Float
{
fn contains(&self, p: &Point<T>) -> bool {
// LineString without points
if self.0.is_empty() {
return false;
}
// LineString with one point equal p
if self.0.len() == 1 {
return self.0[0].contains(p);
}
// check if point is a vertex
if self.0.contains(p) {
if self.points().contains(p) {
return true;
}
for line in self.lines() {
Expand Down Expand Up @@ -142,10 +134,6 @@ fn get_position<T>(p: &Point<T>, linestring: &LineString<T>) -> PositionPoint
// ?updated-min=2011-01-01T00:00:00-06:00&updated-max=2012-01-01T00:00:00-06:00&max-results=19
// Return the position of the point relative to a linestring

// LineString without points
if linestring.0.is_empty() {
return PositionPoint::Outside;
}
// Point is on linestring
if linestring.contains(p) {
return PositionPoint::OnBoundary;
Expand Down Expand Up @@ -223,7 +211,7 @@ impl<T> Contains<LineString<T>> for Polygon<T>
{
fn contains(&self, linestring: &LineString<T>) -> bool {
// All LineString points must be inside the Polygon
if linestring.0.iter().all(|point| self.contains(point)) {
if linestring.points().iter().all(|point| self.contains(point)) {
// The Polygon interior is allowed to intersect with the LineString
// but the Polygon's rings are not
!self.interiors.iter().any(|ring| ring.intersects(linestring))
Expand Down
2 changes: 1 addition & 1 deletion src/algorithm/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<T> Distance<T, Polygon<T>> for Point<T>
// get exterior ring
let exterior = &polygon.exterior;
// No need to continue if the polygon contains the point, or is zero-length
if polygon.contains(self) || exterior.0.is_empty() {
if polygon.contains(self) {
return T::zero();
}
// minimum priority queue
Expand Down
3 changes: 0 additions & 3 deletions src/algorithm/intersects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ impl<T> Intersects<LineString<T>> for LineString<T>
{
// See: https://github.com/brandonxiang/geojson-python-utils/blob/33b4c00c6cf27921fb296052d0c0341bd6ca1af2/geojson_utils.py
fn intersects(&self, linestring: &LineString<T>) -> bool {
if self.0.is_empty() || linestring.0.is_empty() {
return false;
}
for a in self.lines() {
for b in linestring.lines() {
let u_b = (b.end.y() - b.start.y()) * (a.end.x() - a.start.x()) -
Expand Down
15 changes: 4 additions & 11 deletions src/algorithm/rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,6 @@ where
}
}

impl<T> RotatePoint<T> for LineString<T>
where T: Float
{
/// Rotate the LineString about a point by the given number of degrees
fn rotate_around_point(&self, angle: T, point: &Point<T>) -> Self {
unsafe { LineString::new_unchecked(rotation_matrix(angle, point, &self.points())) }
}
}

impl<T> Rotate<T> for Polygon<T>
where
T: Float + FromPrimitive,
Expand All @@ -155,11 +146,13 @@ where
true => self.centroid().unwrap(),
};
Polygon::new(
LineString(rotation_matrix(angle, &centroid, &self.exterior.0)),
// TODO: can this be unchecked
LineString::new(rotation_matrix(angle, &centroid, self.exterior.points())).unwrap(),
self.interiors
.iter()
.map(|ring| {
LineString(rotation_matrix(angle, &centroid, &ring.0))
// TODO: can this be unchecked
LineString::new(rotation_matrix(angle, &centroid, ring.points())).unwrap()
})
.collect(),
)
Expand Down
16 changes: 9 additions & 7 deletions src/algorithm/simplifyvw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ where
}
}
// Simplify shell
rings.push(visvalingam_preserve(geomtype, &exterior.0, epsilon, &mut tree));
rings.push(visvalingam_preserve(geomtype, exterior.points(), epsilon, &mut tree));
// Simplify interior rings, if any
if let Some(interior_rings) = interiors {
for ring in interior_rings {
rings.push(visvalingam_preserve(geomtype, &ring.0, epsilon, &mut tree))
rings.push(visvalingam_preserve(geomtype, ring.points(), epsilon, &mut tree))
}
}
rings
Expand Down Expand Up @@ -370,11 +370,11 @@ where
let point_a = orig[triangle.left];
let point_b = orig[triangle.current];
let point_c = orig[triangle.right];
let bbox = LineString(vec![
let bbox = unsafe { LineString::new_unchecked(vec![
orig[triangle.left],
orig[triangle.current],
orig[triangle.right],
]).bbox()
]) }.bbox()
.unwrap();
let br = Point::new(bbox.xmin, bbox.ymin);
let tl = Point::new(bbox.xmax, bbox.ymax);
Expand Down Expand Up @@ -500,7 +500,8 @@ where
geomtype: GeomType::Line,
};
let mut simplified = vwp_wrapper(&gt, &self, None, epsilon);
LineString(simplified.pop().unwrap())
// can this be unchecked?
LineString::new(simplified.pop().unwrap()).unwrap()
}
}

Expand Down Expand Up @@ -529,8 +530,9 @@ where
geomtype: GeomType::Ring,
};
let mut simplified = vwp_wrapper(&gt, &self.exterior, Some(&self.interiors), epsilon);
let exterior = LineString(simplified.remove(0));
let interiors = simplified.into_iter().map(LineString).collect();
// todo: can these be unchecked?
let exterior = LineString::new(simplified.remove(0)).unwrap();
let interiors = simplified.into_iter().map(|l| LineString::new(l).unwrap()).collect();
Polygon::new(exterior, interiors)
}
}
Expand Down

0 comments on commit 7d36831

Please sign in to comment.