Skip to content

Commit

Permalink
feat: wip - implement first test + fix some unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
RobWalt committed Jul 10, 2023
1 parent ff888b9 commit b328124
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions geo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ rand_distr = "0.4.3"

### boolean-ops test deps
wkt = "0.10.1"
serde_json = "1.0"

[[bench]]
name = "area"
Expand Down
5 changes: 4 additions & 1 deletion geo/src/algorithm/bool_ops/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ impl<T: GeoFloat> RegionAssembly<T> {

while let Some(pt) = iter.next().transpose()? {
let num_segments = iter.intersections().len();
debug_assert!(num_segments % 2 == 0, "assembly segments must be eulierian");
let is_eulierian = num_segments % 2 == 0;
if !is_eulierian {
return Err(GeoError::NotEulierian(num_segments));
}
iter.intersections_mut().sort_unstable_by(compare_crossings);

let first = &iter.intersections()[0];
Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/bool_ops/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl<T: Float, S: Spec<T>> Proc<T, S> {
idx += 1;
}
}
Ok(self.spec.finish())
self.spec.try_finish()
}
}

Expand Down
9 changes: 9 additions & 0 deletions geo/src/algorithm/bool_ops/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub trait Spec<T: GeoFloat> {
fn cross(&self, prev_region: Self::Region, idx: usize) -> Self::Region;
fn output(&mut self, regions: [Self::Region; 2], geom: LineOrPoint<T>, idx: usize);
fn finish(self) -> Self::Output;
fn try_finish(self) -> Result<Self::Output, GeoError>;
}

pub struct BoolOp<T: GeoFloat> {
Expand Down Expand Up @@ -51,6 +52,10 @@ impl<T: GeoFloat> Spec<T> for BoolOp<T> {
fn finish(self) -> Self::Output {
self.assembly.finish()
}

fn try_finish(self) -> Result<Self::Output, GeoError> {
self.assembly.try_finish()
}
}

pub struct ClipOp<T: GeoFloat> {
Expand Down Expand Up @@ -94,6 +99,10 @@ impl<T: GeoFloat> Spec<T> for ClipOp<T> {
fn finish(self) -> Self::Output {
MultiLineString::new(self.assembly.finish())
}

fn try_finish(self) -> Result<Self::Output, GeoError> {
Ok(MultiLineString::new(self.assembly.finish()))
}
}

#[derive(Clone, Copy)]
Expand Down
10 changes: 10 additions & 0 deletions geo/src/algorithm/bool_ops/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,13 @@ fn test_issue_buffer_box() {
let wkt2 = "MULTIPOLYGON(((-164.93595896333647 149.53568721641966,-51.873865625542294 153.2197777241044,-153.80312445248086 153.2197777241044,-266.86521779027504 149.53568721641966,-164.93595896333647 149.53568721641966)))";
check_sweep::<f64>(wkt1, wkt2, OpType::Union).unwrap();
}

#[test]
fn falliable_boolops_on_976() {
init_log();
const DATA : &'static str = "
[{\"exterior\":[{\"x\":-9911.954,\"y\":-8725.639},{\"x\":-9915.633,\"y\":9687.057},{\"x\":-4963.603,\"y\":-45.043945},{\"x\":-9911.954,\"y\":-8725.639}],\"interiors\":[]},{\"exterior\":[{\"x\":-9007.182,\"y\":9094.508},{\"x\":-2687.1802,\"y\":-8199.999},{\"x\":-9915.442,\"y\":8069.0723},{\"x\":-9007.182,\"y\":9094.508}],\"interiors\":[]}]
";
let [a, b]: [Polygon<f32>; 2] = serde_json::from_str(&DATA).unwrap();
matches!(a.try_intersection(&b), Err(GeoError::NotEulierian(_)));
}
1 change: 1 addition & 0 deletions geo/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub enum GeoError {
SegmentNotFoundInActiveSet(usize),
SegmentAlreadyFoundInActiveSet(usize),
ExpectedNonemptyEvents,
NotEulierian(usize),
Unreachable(&'static str),
}

Expand Down

0 comments on commit b328124

Please sign in to comment.