use crate::exact::kernel::on_segment;
use crate::index::EdgeIndex;
use crate::topology::label::Location;
use crate::types::{Coord2d, Envelope, LinearRing, MultiPolygon, Polygon, Polygonal};
pub(crate) type Window = (Coord2d, Coord2d);
pub(crate) fn point_window(p: Coord2d) -> Window {
(p, p)
}
pub(crate) fn segment_window(p: Coord2d, q: Coord2d) -> Window {
(
Coord2d::new(p.x.min(q.x), p.y.min(q.y)),
Coord2d::new(p.x.max(q.x), p.y.max(q.y)),
)
}
pub(crate) fn envelope_window(e: (f64, f64, f64, f64)) -> Window {
(Coord2d::new(e.0, e.1), Coord2d::new(e.2, e.3))
}
pub(crate) trait AreaOperand {
fn for_edges_near(&self, w: &Window, f: &mut dyn FnMut(Coord2d, Coord2d) -> bool);
fn for_vertices_near(&self, w: &Window, f: &mut dyn FnMut(Coord2d) -> bool);
fn for_edges_with_ref_near(
&self,
w: &Window,
f: &mut dyn FnMut(usize, usize, Coord2d, Coord2d) -> bool,
);
fn has_edge_outside(&self, w: &Window) -> bool;
fn locate(&self, p: Coord2d) -> Location;
fn envelope(&self) -> Option<(f64, f64, f64, f64)>;
fn all_window(&self) -> Window {
self.envelope().map_or(
(Coord2d::new(0.0, 0.0), Coord2d::new(0.0, 0.0)),
envelope_window,
)
}
}
pub(crate) struct WindowedAreal<'a> {
base: Base<'a>,
window: Window,
near: Vec<(usize, usize, Coord2d, Coord2d)>,
strip: Vec<(usize, usize, Coord2d, Coord2d)>,
envelope: Option<(f64, f64, f64, f64)>,
}
enum Base<'a> {
Plain(Areal<'a>),
Indexed(IndexedAreal<'a>),
}
impl Base<'_> {
fn op(&self) -> &dyn AreaOperand {
match self {
Base::Plain(a) => a,
Base::Indexed(a) => a,
}
}
}
pub(crate) const WINDOW_MIN_EDGES: usize = 256;
impl<'a> WindowedAreal<'a> {
pub(crate) fn worth_it(base: &Areal<'_>) -> bool {
base.rings().map(LinearRing::num_vertices).sum::<usize>() > WINDOW_MIN_EDGES
}
pub(crate) fn of(base: &Areal<'a>, window: Window) -> Option<Self> {
if !Self::worth_it(base) {
return None;
}
let strip_of = |a: Coord2d, b: Coord2d| {
a.x.max(b.x) >= window.0.x && a.y.min(b.y) <= window.1.y && a.y.max(b.y) >= window.0.y
};
let (mut near, mut strip) = (Vec::new(), Vec::new());
let env = crate::types::bbox_fold(base.parts.iter().map(|p| p.exterior().bbox()));
for (part, p) in base.parts.iter().enumerate() {
let rings = core::iter::once(p.exterior()).chain(p.holes().iter());
for (ring, r) in rings.enumerate() {
for (a, b) in r.edges() {
if edge_meets(&window, a, b) {
near.push((part, ring, a, b));
}
if strip_of(a, b) {
strip.push((part, ring, a, b));
}
}
}
}
Some(Self {
base: Base::Plain(Areal::from_parts(base.parts)),
window,
near,
strip,
envelope: env,
})
}
pub(crate) fn of_indexed(base: IndexedAreal<'a>, window: Window) -> Self {
let near = base
.candidates(&window)
.into_iter()
.map(|(p, r, e)| {
let (a, b) = base.edge(p, r, e);
(p, r, a, b)
})
.collect();
let envelope = base.envelope;
let strip = match envelope {
Some(env) if env.2 >= window.0.x => base
.candidates(&(window.0, Coord2d::new(env.2, window.1.y)))
.into_iter()
.map(|(p, r, e)| {
let (a, b) = base.edge(p, r, e);
(p, r, a, b)
})
.collect(),
_ => Vec::new(),
};
Self {
base: Base::Indexed(base),
window,
near,
strip,
envelope,
}
}
fn list_for(&self, w: &Window, rays: bool) -> Option<&[(usize, usize, Coord2d, Coord2d)]> {
let inside =
|o: &Window| w.0.x >= o.0.x && w.0.y >= o.0.y && w.1.x <= o.1.x && w.1.y <= o.1.y;
if inside(&self.window) {
Some(&self.near)
} else if rays
&& w.0.x >= self.window.0.x
&& w.0.y >= self.window.0.y
&& w.1.y <= self.window.1.y
{
Some(&self.strip)
} else {
None
}
}
}
impl AreaOperand for WindowedAreal<'_> {
fn for_edges_near(&self, w: &Window, f: &mut dyn FnMut(Coord2d, Coord2d) -> bool) {
match self.list_for(w, false) {
Some(list) => {
for &(_, _, a, b) in list {
if edge_meets(w, a, b) && !f(a, b) {
return;
}
}
}
None => self.base.op().for_edges_near(w, f),
}
}
fn for_vertices_near(&self, w: &Window, f: &mut dyn FnMut(Coord2d) -> bool) {
match self.list_for(w, false) {
Some(list) => {
for &(_, _, a, b) in list {
if edge_meets(w, a, b) && !f(a) {
return;
}
}
}
None => self.base.op().for_vertices_near(w, f),
}
}
fn for_edges_with_ref_near(
&self,
w: &Window,
f: &mut dyn FnMut(usize, usize, Coord2d, Coord2d) -> bool,
) {
match self.list_for(w, true) {
Some(list) => {
for &(part, ring, a, b) in list {
if edge_meets(w, a, b) && !f(part, ring, a, b) {
return;
}
}
}
None => self.base.op().for_edges_with_ref_near(w, f),
}
}
fn has_edge_outside(&self, w: &Window) -> bool {
self.base.op().has_edge_outside(w)
}
fn locate(&self, p: Coord2d) -> Location {
crate::topology::exact_point::locate(
&crate::topology::exact_point::ExactPoint::Stored(p),
self,
)
}
fn envelope(&self) -> Option<(f64, f64, f64, f64)> {
self.envelope
}
}
fn edge_meets(w: &Window, a: Coord2d, b: Coord2d) -> bool {
a.x.min(b.x) <= w.1.x && a.x.max(b.x) >= w.0.x && a.y.min(b.y) <= w.1.y && a.y.max(b.y) >= w.0.y
}
pub struct Areal<'a> {
parts: &'a [Polygon],
}
pub(crate) fn on_part_boundary(p: Coord2d, part: &Polygon) -> bool {
core::iter::once(part.exterior())
.chain(part.holes())
.any(|r| r.edges().any(|(a, b)| on_segment(p, a, b)))
}
pub(crate) fn inside_part_off_boundary(p: Coord2d, part: &Polygon) -> bool {
crate::exact::kernel::ring_contains_off_boundary(p, part.exterior())
&& !part
.holes()
.iter()
.any(|h| crate::exact::kernel::ring_contains_off_boundary(p, h))
}
impl<'a> Areal<'a> {
pub(crate) fn single(p: &'a Polygon) -> Self {
Self {
parts: core::slice::from_ref(p),
}
}
pub(crate) fn multi(mp: &'a MultiPolygon) -> Self {
Self::from_parts(mp.polygons())
}
pub fn new<P: Polygonal>(operand: &'a P) -> Self {
Self::from_parts(operand.parts())
}
pub(crate) fn from_parts(parts: &'a [Polygon]) -> Self {
Self { parts }
}
pub(crate) fn rings(&self) -> impl Iterator<Item = &'a LinearRing> + '_ {
self.parts
.iter()
.flat_map(|p| core::iter::once(p.exterior()).chain(p.holes().iter()))
}
}
impl AreaOperand for Areal<'_> {
fn for_edges_near(&self, w: &Window, f: &mut dyn FnMut(Coord2d, Coord2d) -> bool) {
for ring in self.rings() {
for (a, b) in ring.edges() {
if edge_meets(w, a, b) && !f(a, b) {
return;
}
}
}
}
fn for_vertices_near(&self, w: &Window, f: &mut dyn FnMut(Coord2d) -> bool) {
for ring in self.rings() {
for (a, b) in ring.edges() {
if edge_meets(w, a, b) && !f(a) {
return;
}
}
}
}
fn for_edges_with_ref_near(
&self,
w: &Window,
f: &mut dyn FnMut(usize, usize, Coord2d, Coord2d) -> bool,
) {
for (part, p) in self.parts.iter().enumerate() {
let rings = core::iter::once(p.exterior()).chain(p.holes().iter());
for (ring, r) in rings.enumerate() {
for (a, b) in r.edges() {
if edge_meets(w, a, b) && !f(part, ring, a, b) {
return;
}
}
}
}
}
fn has_edge_outside(&self, w: &Window) -> bool {
self.rings()
.any(|r| r.edges().any(|(a, b)| !edge_meets(w, a, b)))
}
fn locate(&self, p: Coord2d) -> Location {
if self.parts.iter().any(|q| on_part_boundary(p, q)) {
Location::Boundary
} else if self.parts.iter().any(|q| inside_part_off_boundary(p, q)) {
Location::Interior
} else {
Location::Exterior
}
}
fn envelope(&self) -> Option<(f64, f64, f64, f64)> {
crate::types::bbox_fold(self.parts.iter().map(|p| p.exterior().bbox()))
}
}
pub struct IndexedAreal<'a> {
parts: &'a [Polygon],
index: &'a EdgeIndex,
active: Option<&'a [bool]>,
envelope: Option<(f64, f64, f64, f64)>,
}
impl<'a> IndexedAreal<'a> {
pub(crate) fn new(
parts: &'a [Polygon],
index: &'a EdgeIndex,
active: Option<&'a [bool]>,
envelope: Option<(f64, f64, f64, f64)>,
) -> Self {
Self {
parts,
index,
active,
envelope,
}
}
fn is_active(&self, part: usize) -> bool {
self.active.is_none_or(|a| a[part])
}
fn ring(&self, part: usize, ring: usize) -> &'a LinearRing {
let p = &self.parts[part];
if ring == 0 {
p.exterior()
} else {
&p.holes()[ring - 1]
}
}
fn edge(&self, part: usize, ring: usize, edge: usize) -> (Coord2d, Coord2d) {
let c = self.ring(part, ring).coords().as_slice();
(c[edge], c[(edge + 1) % c.len()])
}
fn window_envelope(w: &Window) -> Option<Envelope> {
Envelope::new(w.0, w.1).ok()
}
fn candidates(&self, w: &Window) -> Vec<(usize, usize, usize)> {
let Some(env) = Self::window_envelope(w) else {
return Vec::new();
};
let mut out = Vec::new();
self.index.candidates(&env, |r| {
if self.is_active(r.part) {
out.push((r.part, r.ring, r.edge));
}
});
out.sort_unstable();
out
}
fn active_edge_count(&self) -> usize {
self.parts
.iter()
.enumerate()
.filter(|(i, _)| self.is_active(*i))
.map(|(_, p)| {
core::iter::once(p.exterior())
.chain(p.holes())
.map(LinearRing::num_vertices)
.sum::<usize>()
})
.sum()
}
}
impl AreaOperand for IndexedAreal<'_> {
fn for_edges_near(&self, w: &Window, f: &mut dyn FnMut(Coord2d, Coord2d) -> bool) {
for (p, r, e) in self.candidates(w) {
let (a, b) = self.edge(p, r, e);
if !f(a, b) {
return;
}
}
}
fn for_vertices_near(&self, w: &Window, f: &mut dyn FnMut(Coord2d) -> bool) {
for (p, r, e) in self.candidates(w) {
let (a, _) = self.edge(p, r, e);
if !f(a) {
return;
}
}
}
fn for_edges_with_ref_near(
&self,
w: &Window,
f: &mut dyn FnMut(usize, usize, Coord2d, Coord2d) -> bool,
) {
for (p, r, e) in self.candidates(w) {
let (a, b) = self.edge(p, r, e);
if !f(p, r, a, b) {
return;
}
}
}
fn has_edge_outside(&self, w: &Window) -> bool {
self.candidates(w).len() < self.active_edge_count()
}
fn locate(&self, p: Coord2d) -> Location {
crate::topology::exact_point::locate(
&crate::topology::exact_point::ExactPoint::Stored(p),
self,
)
}
fn envelope(&self) -> Option<(f64, f64, f64, f64)> {
self.envelope
}
}