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

faster area #36

Merged
merged 3 commits into from
Jan 2, 2024
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
24 changes: 17 additions & 7 deletions src/methods/area.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

@skygering skygering Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does local do here? Just for my future knowledge?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes the variables available in the scope outside thep loop

# 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this is a pretty good way to do this.

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
end
9 changes: 0 additions & 9 deletions src/transformations/simplify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading