From 75603bd5e8993c562776b9478f4d194428dfe8ee Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Tue, 2 Jan 2024 12:02:13 +0100 Subject: [PATCH] faster area (#36) * faster area * faster * bugfix --- src/methods/area.jl | 24 +++++++++++++++++------- src/transformations/simplify.jl | 9 --------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/methods/area.jl b/src/methods/area.jl index 5b256dbbd..d6626b526 100644 --- a/src/methods/area.jl +++ b/src/methods/area.jl @@ -120,18 +120,28 @@ to be closed. =# function _signed_area(::Type{T}, geom) where T area = zero(T) - # Close curve, even if last point isn't explicitly repeated np = GI.npoint(geom) np == 0 && return area - first_last_equal = equals(GI.getpoint(geom, 1), GI.getpoint(geom, np)) - np -= first_last_equal ? 1 : 0 + + first = true + local pfirst, p1 # Integrate the area under the curve - p1 = GI.getpoint(geom, np) - for i in 1:np - p2 = GI.getpoint(geom, i) + for p2 in GI.getpoint(geom) + # Skip the first and do it later + # This lets us work within one iteration over geom, + # which means on C call when using points from external libraries. + if first + p1 = pfirst = p2 + first = false + continue + end # Accumulate the area into `area` area += GI.x(p1) * GI.y(p2) - GI.y(p1) * GI.x(p2) p1 = p2 end + # Complete the last edge. + # If the first and last where the same this will be zero + p2 = pfirst + area += GI.x(p1) * GI.y(p2) - GI.y(p1) * GI.x(p2) return T(area / 2) -end \ No newline at end of file +end diff --git a/src/transformations/simplify.jl b/src/transformations/simplify.jl index d9a151edd..bdd734592 100644 --- a/src/transformations/simplify.jl +++ b/src/transformations/simplify.jl @@ -129,15 +129,6 @@ function _simplify(::PolygonTrait, alg, geom) end ## For curves and rings we simplify _simplify(::AbstractCurveTrait, alg, geom) = rebuild(geom, simplify(alg, tuple_points(geom))) -function _simplify(::LinearRingTrait, alg, geom) - ## Make a vector of points - points = tuple_points(geom) - - ## Simplify it once - simple = _simplify(alg, points) - - return rebuild(geom, simple) -end """ RadialDistance <: SimplifyAlg