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

Switch to internal bounding box type. #24

Merged
merged 2 commits into from
May 18, 2020
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
4 changes: 2 additions & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "geo-booleanop"
version = "0.2.2"
version = "0.3.0"
authors = ["Bodo Junglas <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -10,7 +10,7 @@ readme = "../README.md"
keywords = ["gis", "geo", "geography", "geospatial"]

[dependencies]
geo-types = { version = "0.4", default-features = false }
geo-types = { version = "0.5.0", default-features = false }
num-traits = "0.2"
robust = "0.1"
float_next_after = "0.1"
Expand Down
9 changes: 5 additions & 4 deletions lib/src/boolean/fill_queue.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use super::helper::Float;
use geo_types::{LineString, Polygon, Rect};
use geo_types::{LineString, Polygon};
use std::collections::BinaryHeap;
use std::rc::{Rc, Weak};

use super::sweep_event::SweepEvent;
use super::Operation;
use super::helper::BoundingBox;

pub fn fill_queue<F>(
subject: &[Polygon<F>],
clipping: &[Polygon<F>],
sbbox: &mut Rect<F>,
cbbox: &mut Rect<F>,
sbbox: &mut BoundingBox<F>,
cbbox: &mut BoundingBox<F>,
operation: Operation,
) -> BinaryHeap<Rc<SweepEvent<F>>>
where
Expand Down Expand Up @@ -53,7 +54,7 @@ fn process_polygon<F>(
is_subject: bool,
contour_id: u32,
event_queue: &mut BinaryHeap<Rc<SweepEvent<F>>>,
bbox: &mut Rect<F>,
bbox: &mut BoundingBox<F>,
is_exterior_ring: bool,
) where
F: Float,
Expand Down
24 changes: 24 additions & 0 deletions lib/src/boolean/helper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use float_next_after::NextAfter as NextAfterFloat;
use geo_types::{Coordinate, CoordinateType};
use num_traits::Float as NumTraitsFloat;
use std::cmp::Ordering;
use std::fmt::{Debug, Display};
Expand Down Expand Up @@ -50,6 +51,29 @@ pub fn less_if_inversed(condition: bool) -> Ordering {
}
}

/// A bounded 2D quadrilateral whose area is defined by minimum and maximum `Coordinates`.
///
/// A simple implementation copied from geo_types 0.4.0, because this version is a better
/// fit for the needs of this crate than the newer ones.
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct BoundingBox<T>
where
T: CoordinateType,
{
pub min: Coordinate<T>,
pub max: Coordinate<T>,
}

impl<T: CoordinateType> BoundingBox<T> {
pub fn width(self) -> T {
self.max.x - self.min.x
}

pub fn height(self) -> T {
self.max.y - self.min.y
}
}

#[cfg(test)]
pub mod test {
use super::Float;
Expand Down
6 changes: 3 additions & 3 deletions lib/src/boolean/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use geo_types::{Coordinate, LineString, MultiPolygon, Polygon, Rect};
use geo_types::{Coordinate, LineString, MultiPolygon, Polygon};

pub mod compare_segments;
pub mod compute_fields;
Expand All @@ -12,7 +12,7 @@ mod signed_area;
pub mod subdivide_segments;
pub mod sweep_event;

pub use helper::Float;
pub use helper::{BoundingBox, Float};

use self::connect_edges::connect_edges;
use self::fill_queue::fill_queue;
Expand Down Expand Up @@ -89,7 +89,7 @@ fn boolean_operation<F>(subject: &[Polygon<F>], clipping: &[Polygon<F>], operati
where
F: Float,
{
let mut sbbox = Rect {
let mut sbbox = BoundingBox {
min: Coordinate {
x: F::infinity(),
y: F::infinity(),
Expand Down
17 changes: 9 additions & 8 deletions lib/src/boolean/segment_intersection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::helper::Float;
use geo_types::{Coordinate, Rect};
use super::helper::BoundingBox;
use geo_types::{Coordinate};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LineIntersection<F>
Expand All @@ -17,7 +18,7 @@ fn get_intersection_bounding_box<F>(
a2: Coordinate<F>,
b1: Coordinate<F>,
b2: Coordinate<F>,
) -> Option<Rect<F>>
) -> Option<BoundingBox<F>>
where
F: Float,
{
Expand All @@ -30,7 +31,7 @@ where
let interval_end_x = a_end_x.min(b_end_x);
let interval_end_y = a_end_y.min(b_end_y);
if interval_start_x <= interval_end_x && interval_start_y <= interval_end_y {
Some(Rect {
Some(BoundingBox {
min: Coordinate {
x: interval_start_x,
y: interval_start_y,
Expand All @@ -46,7 +47,7 @@ where
}

#[inline]
fn constrain_to_bounding_box<F>(p: Coordinate<F>, bb: Rect<F>) -> Coordinate<F>
fn constrain_to_bounding_box<F>(p: Coordinate<F>, bb: BoundingBox<F>) -> Coordinate<F>
where
F: Float,
{
Expand Down Expand Up @@ -198,22 +199,22 @@ mod test {
use super::super::helper::test::xy;
use super::*;

fn rect(min: Coordinate<f64>, max: Coordinate<f64>) -> Rect<f64> {
Rect { min, max }
fn rect(min: Coordinate<f64>, max: Coordinate<f64>) -> BoundingBox<f64> {
BoundingBox { min, max }
}

#[test]
fn test_get_intersection_bounding_box() {
assert_eq!(
get_intersection_bounding_box(xy(0, 0), xy(2, 2), xy(1, 1), xy(3, 3)),
Some(Rect {
Some(BoundingBox {
min: xy(1, 1),
max: xy(2, 2)
}),
);
assert_eq!(
get_intersection_bounding_box(xy(-1, 0), xy(1, 0), xy(0, -1), xy(0, 1)),
Some(Rect {
Some(BoundingBox {
min: xy(0, 0),
max: xy(0, 0)
}),
Expand Down
7 changes: 3 additions & 4 deletions lib/src/boolean/subdivide_segments.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::compare_segments::compare_segments;
use super::compute_fields::compute_fields;
use super::helper::Float;
use super::helper::{BoundingBox, Float};
use super::possible_intersection::possible_intersection;
use super::sweep_event::SweepEvent;
use super::Operation;
use crate::splay::SplaySet;
use geo_types::Rect;
use std::collections::BinaryHeap;
use std::rc::Rc;

Expand All @@ -14,8 +13,8 @@ use super::sweep_event::JsonDebug;

pub fn subdivide<F>(
event_queue: &mut BinaryHeap<Rc<SweepEvent<F>>>,
sbbox: &Rect<F>,
cbbox: &Rect<F>,
sbbox: &BoundingBox<F>,
cbbox: &BoundingBox<F>,
operation: Operation,
) -> Vec<Rc<SweepEvent<F>>>
where
Expand Down
6 changes: 3 additions & 3 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
name = "geo-booleanop-tests"
version = "0.2.2"
version = "0.3.0"
authors = ["Bodo Junglas <[email protected]>"]
edition = "2018"

[dependencies]
geo-booleanop = { path = "../lib", features = [] } # add "debug-booleanop" for debugging
geo = "0.12"
geo = "0.13"

# Note: It is crucial to enable arbitrary_precision on serde_json, otherwise
# JSON parsing isn't exact.
geojson = { version = "0.16", features = ["geo-types"] }
geojson = { version = "0.18", features = ["geo-types"] }
serde_json = { version = "1.0.44", features = ["arbitrary_precision"] }

clap = "2.3.3"
Expand Down
11 changes: 6 additions & 5 deletions tests/src/fill_queue_test.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use super::helper::fixture_shapes;
use geo::{Coordinate, Rect};
use geo::Coordinate;
use geo_booleanop::boolean::fill_queue::fill_queue;
use geo_booleanop::boolean::Operation;
use geo_booleanop::boolean::BoundingBox;
use num_traits::Float;

use super::helper::xy;

#[test]
fn test_two_polygons() {
let (s, c) = fixture_shapes("two_shapes.geojson");
let mut sbbox = Rect {
let mut sbbox = BoundingBox {
min: Coordinate {
x: f64::infinity(),
y: f64::infinity(),
Expand Down Expand Up @@ -59,7 +60,7 @@ fn test_two_polygons() {
#[test]
fn test_fill_event_queue() {
let (s, c) = fixture_shapes("two_triangles.geojson");
let mut sbbox = Rect {
let mut sbbox = BoundingBox {
min: xy(f64::infinity(), f64::infinity()),
max: xy(f64::neg_infinity(), f64::neg_infinity()),
};
Expand All @@ -68,14 +69,14 @@ fn test_fill_event_queue() {

assert_eq!(
sbbox,
Rect {
BoundingBox {
min: xy(20.0, -113.5),
max: xy(226.5, 74.0)
},
);
assert_eq!(
cbbox,
Rect {
BoundingBox {
min: xy(54.5, -198.0),
max: xy(239.5, 33.5)
},
Expand Down
5 changes: 3 additions & 2 deletions tests/src/possible_intersection_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::helper::fixture_shapes;
use geo::{Coordinate, Rect};
use geo::Coordinate;
use geo_booleanop::boolean::compare_segments::compare_segments;
use geo_booleanop::boolean::fill_queue::fill_queue;
use geo_booleanop::boolean::possible_intersection::possible_intersection;
use geo_booleanop::boolean::subdivide_segments::subdivide;
use geo_booleanop::boolean::sweep_event::SweepEvent;
use geo_booleanop::boolean::Operation;
use geo_booleanop::boolean::BoundingBox;
use geo_booleanop::splay::SplaySet;
use num_traits::Float;
use std::cmp::Ordering;
Expand Down Expand Up @@ -74,7 +75,7 @@ fn test_possible_intersection() {
#[test]
fn test_on_two_polygons() {
let (s, c) = fixture_shapes("two_shapes.geojson");
let mut sbbox = Rect {
let mut sbbox = BoundingBox {
min: Coordinate {
x: f64::infinity(),
y: f64::infinity(),
Expand Down