-
Notifications
You must be signed in to change notification settings - Fork 9
experimental UnitSpherical module that can express geometry on unit sphere #285
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
Merged
+1,918
−7
Merged
Changes from 16 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
a85fbb9
experimental UnitSpherical module that can express geometry on unit s…
asinghvi17 a541d50
make folder
asinghvi17 65c1f7a
include in GeometryOps proper
asinghvi17 a0414e3
add StaticArrays
asinghvi17 b1ec59f
Address code review
asinghvi17 6e8baa1
Fix overriding method definition
asinghvi17 af5eac2
fix all doctests and add tests for coord transforms
asinghvi17 c1d766d
add AI generated tests
asinghvi17 b6e9196
fix docs/make.jl
asinghvi17 2dd10cd
fix ci
asinghvi17 0df0f74
add a merge function
asinghvi17 b919342
add example on top
asinghvi17 ab35ea5
Implement robust cross product
asinghvi17 68b2960
add s2 license (apache 2.0, so no issue) and README
asinghvi17 a024fd0
comments improved
asinghvi17 1749fc0
Merge branch 'main' into as/spherical-cap-str
asinghvi17 ea07948
remove overwriting function
asinghvi17 abe6b01
Merge branch 'main' into as/spherical-cap-str
asinghvi17 38e6b28
Add merge function
meggart cd182a0
Merge branch 'main' into as/spherical-cap-str
asinghvi17 778e961
Merge branch 'as/spherical-cap-str' into fg/spherical-2
asinghvi17 fb0df28
Use local find_orthogonal in RobustCrossProduct
asinghvi17 c0af6b0
Delete older incorrect `_merge` implementation
asinghvi17 3334263
simplify spherical cap methods
asinghvi17 327d6df
Merge remote-tracking branch 'origin/main' into fg/spherical-2
asinghvi17 df953be
Fix doctests
asinghvi17 34e0a05
Fix warning about unused type param
asinghvi17 2617062
Add merge function (#332)
asinghvi17 855e1f1
Use robust cross product more widely
asinghvi17 0990967
Format
asinghvi17 f4aa564
Indicate that we should also store cartesian radius in cap
asinghvi17 5977c7b
Minor formatting fix
asinghvi17 03012dd
Make UnitSphericalPoints reconstruct to themselves even in the two ar…
asinghvi17 e94ad07
Use regular LinAlg cross product
asinghvi17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| module UnitSpherical | ||
|
|
||
| using CoordinateTransformations | ||
| using StaticArrays, LinearAlgebra | ||
| import GeoInterface as GI, GeoFormatTypes as GFT | ||
|
|
||
| import Random | ||
|
|
||
| # using TestItems # this is a thin package that allows TestItems.@testitem to be parsed. | ||
|
|
||
| include("point.jl") | ||
| include("coordinate_transforms.jl") | ||
| include("slerp.jl") | ||
| include("cap.jl") | ||
| include("robustcrossproduct/RobustCrossProduct.jl") | ||
|
|
||
| export UnitSphericalPoint, UnitSphereFromGeographic, GeographicFromUnitSphere, | ||
| slerp, SphericalCap, spherical_distance | ||
|
|
||
| # Re-export from RobustCrossProduct | ||
| using .RobustCrossProduct: robust_cross_product | ||
| export robust_cross_product | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # # Spherical Caps | ||
|
|
||
| #= | ||
| ```@meta | ||
| CollapsedDocStrings = true | ||
| ``` | ||
|
|
||
| ```@docs; canonical=false | ||
| SphericalCap | ||
| circumcenter_on_unit_sphere | ||
| ``` | ||
|
|
||
| ## What is SphericalCap? | ||
|
|
||
| A spherical cap represents a section of a unit sphere about some point, bounded by a radius. | ||
| It is defined by a center point on the unit sphere and a radius (in radians). | ||
|
|
||
| Spherical caps are used in: | ||
| - Representing circular regions on a spherical surface | ||
| - Approximating and bounding spherical geometries | ||
| - Spatial indexing and filtering on the unit sphere | ||
| - Implementing containment, intersection, and disjoint predicates | ||
|
|
||
| The `SphericalCap` type offers multiple constructors to create caps from: | ||
| - UnitSphericalPoint and radius | ||
| - Geographic coordinates and radius | ||
| - Three points on the unit sphere (circumcircle) | ||
|
|
||
| ## Examples | ||
|
|
||
| ```@example sphericalcap | ||
| using GeometryOps | ||
| using GeoInterface | ||
|
|
||
| # Create a spherical cap from a point and radius | ||
| point = UnitSphericalPoint(1.0, 0.0, 0.0) # Point on the unit sphere | ||
| cap = SphericalCap(point, 0.5) # Cap with radius 0.5 radians | ||
| ``` | ||
|
|
||
| ```@example sphericalcap | ||
| # Create a spherical cap from geographic coordinates | ||
| lat, lon = 40.0, -74.0 # New York City (approximate) | ||
| point = GeoInterface.Point(lon, lat) | ||
| cap = SphericalCap(point, 0.1) # Cap with radius ~0.1 radians | ||
| ``` | ||
|
|
||
| ```@example sphericalcap | ||
| # Create a spherical cap from three points (circumcircle) | ||
| p1 = UnitSphericalPoint(1.0, 0.0, 0.0) | ||
| p2 = UnitSphericalPoint(0.0, 1.0, 0.0) | ||
| p3 = UnitSphericalPoint(0.0, 0.0, 1.0) | ||
| cap = SphericalCap(p1, p2, p3) | ||
| ``` | ||
|
|
||
| =# | ||
|
|
||
| # Spherical cap implementation | ||
| struct SphericalCap{T} | ||
| point::UnitSphericalPoint{T} | ||
| radius::T | ||
| end | ||
|
|
||
| SphericalCap(point::UnitSphericalPoint{T}, radius::Number) where T = SphericalCap{T}(point, convert(T, radius)) | ||
| SphericalCap(point, radius::Number) = SphericalCap(GI.trait(point), point, radius) | ||
| function SphericalCap(::GI.PointTrait, point, radius::Number) | ||
| return SphericalCap(UnitSphereFromGeographic()(point), radius) | ||
| end | ||
|
|
||
| SphericalCap(geom) = SphericalCap(GI.trait(geom), geom) | ||
| SphericalCap(t::GI.PointTrait, geom) = SphericalCap(t, geom, 0) | ||
| # TODO: add implementations for line string and polygon traits | ||
| # TODO: add implementations to merge two spherical caps | ||
| function _merge(x::SphericalCap, y::SphericalCap) | ||
| d = spherical_distance(x.point, y.point) | ||
| newradius = (x.radius + y.radius + d) / 2 | ||
| if newradius < x.radius | ||
| #x contains y | ||
| x | ||
| elseif newradius < y.radius | ||
| #y contains x | ||
| y | ||
| else | ||
| excenter = 0.5 * (1 + (y.radius - x.radius) / d) | ||
| newcenter = x.point + slerp(x.point, y.point, excenter) | ||
| SphericalCap(newcenter, d) | ||
| end | ||
| end | ||
| # TODO: add implementations for multitraits based on this | ||
|
|
||
| # TODO: this returns an approximately antipodal point... | ||
|
|
||
| # TODO: exact-predicate intersection | ||
| # This is all inexact and thus subject to floating point error | ||
| function _intersects(x::SphericalCap, y::SphericalCap) | ||
| spherical_distance(x.point, y.point) <= x.radius + y.radius | ||
| end | ||
|
|
||
| _disjoint(x::SphericalCap, y::SphericalCap) = !_intersects(x, y) | ||
|
|
||
| function _contains(big::SphericalCap, small::SphericalCap) | ||
| dist = spherical_distance(big.point, small.point) | ||
| # small circle fits in big circle | ||
| return dist + small.radius < big.radius | ||
| end | ||
| function _contains(cap::SphericalCap, point::UnitSphericalPoint) | ||
| spherical_distance(cap.point, point) <= cap.radius | ||
| end | ||
|
|
||
|
|
||
| function circumcenter_on_unit_sphere(a::UnitSphericalPoint, b::UnitSphericalPoint, c::UnitSphericalPoint) | ||
| LinearAlgebra.normalize(a × b + b × c + c × a) | ||
| end | ||
|
|
||
| "Get the circumcenter of the triangle (a, b, c) on the unit sphere. Returns a normalized 3-vector." | ||
| function SphericalCap(a::UnitSphericalPoint, b::UnitSphericalPoint, c::UnitSphericalPoint) | ||
| circumcenter = circumcenter_on_unit_sphere(a, b, c) | ||
| circumradius = spherical_distance(a, circumcenter) | ||
| return SphericalCap(circumcenter, circumradius) | ||
| end | ||
|
|
||
| function _is_ccw_unit_sphere(v_0::S, v_c::S, v_i::S) where S <: UnitSphericalPoint | ||
| # checks if the smaller interior angle for the great circles connecting u-v and v-w is CCW | ||
| return(LinearAlgebra.dot(LinearAlgebra.cross(v_c - v_0,v_i - v_c), v_i) < 0) | ||
asinghvi17 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| function angle_between(a::S, b::S, c::S) where S <: UnitSphericalPoint | ||
| ab = b - a | ||
| bc = c - b | ||
| norm_dot = (ab ⋅ bc) / (LinearAlgebra.norm(ab) * LinearAlgebra.norm(bc)) | ||
| angle = acos(clamp(norm_dot, -1.0, 1.0)) | ||
| if _is_ccw_unit_sphere(a, b, c) | ||
| return angle | ||
| else | ||
| return 2π - angle | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #= | ||
| # Coordinate transformations | ||
| =# | ||
| # Coordinate transformations from lat/long to geographic and back | ||
| """ | ||
| UnitSphereFromGeographic() | ||
|
|
||
| A transformation that converts a geographic point (latitude, longitude) to a | ||
| [`UnitSphericalPoint`] in ℝ³. | ||
|
|
||
| Accepts any [GeoInterface-compatible](https://github.com/JuliaGeo/GeoInterface.jl) point. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```jldoctest | ||
| julia> import GeoInterface as GI; using GeometryOps.UnitSpherical | ||
|
|
||
| julia> UnitSphereFromGeographic()(GI.Point(45, 45)) | ||
| 3-element UnitSphericalPoint{Float64} with indices SOneTo(3): | ||
| 0.5000000000000001 | ||
| 0.5000000000000001 | ||
| 0.7071067811865476 | ||
| ``` | ||
|
|
||
| ```jldoctest | ||
| julia> using GeometryOps.UnitSpherical | ||
|
|
||
| julia> UnitSphereFromGeographic()((45, 45)) | ||
| 3-element UnitSphericalPoint{Float64} with indices SOneTo(3): | ||
| 0.5000000000000001 | ||
| 0.5000000000000001 | ||
| 0.7071067811865476 | ||
| ``` | ||
| """ | ||
| struct UnitSphereFromGeographic <: CoordinateTransformations.Transformation | ||
| end | ||
|
|
||
| function (::UnitSphereFromGeographic)(geographic_point) | ||
| # Asssume that geographic_point is GeoInterface compatible | ||
| # Longitude is directly translatable to a spherical coordinate | ||
| # θ (azimuth) | ||
| θ = GI.x(geographic_point) | ||
| # The polar angle is 90 degrees minus the latitude | ||
| # ϕ (polar angle) | ||
| ϕ = 90 - GI.y(geographic_point) | ||
| # Since this is the unit sphere, the radius is assumed to be 1, | ||
| # and we don't need to multiply by it. | ||
| sinϕ, cosϕ = sincosd(ϕ) | ||
| sinθ, cosθ = sincosd(θ) | ||
|
|
||
| return UnitSphericalPoint( | ||
| sinϕ * cosθ, | ||
| sinϕ * sinθ, | ||
| cosϕ | ||
| ) | ||
| end | ||
|
|
||
| """ | ||
| GeographicFromUnitSphere() | ||
|
|
||
| A transformation that converts a [`UnitSphericalPoint`](@ref) in ℝ³ to a | ||
| 2-tuple geographic point (longitude, latitude), in degrees. | ||
|
|
||
| Accepts any 3-element vector, but the input is assumed to be on the unit sphere. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```jldoctest | ||
| julia> using GeometryOps.UnitSpherical | ||
|
|
||
| julia> GeographicFromUnitSphere()(UnitSphericalPoint(0.5, 0.5, 1/√(2))) | ||
| (45.0, 44.99999999999999) | ||
| ``` | ||
| (the inaccuracy is due to the precision of the `atan` function) | ||
|
|
||
| """ | ||
| struct GeographicFromUnitSphere <: CoordinateTransformations.Transformation | ||
| end | ||
|
|
||
| function (::GeographicFromUnitSphere)(xyz::AbstractVector) | ||
| @assert length(xyz) == 3 "GeographicFromUnitCartesian expects a 3D Cartesian vector" | ||
| x, y, z = xyz | ||
| return ( | ||
| atand(y, x), | ||
| asind(z), | ||
| ) | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.