Skip to content

Commit 0df0f74

Browse files
asinghvi17meggart
andcommitted
add a merge function
Co-authored-by: Fabian Gans <fabiangans@gmx.de>
1 parent 2dd10cd commit 0df0f74

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/utils/UnitSpherical/UnitSpherical.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ include("slerp.jl")
1414
include("cap.jl")
1515

1616
export UnitSphericalPoint, UnitSphereFromGeographic, GeographicFromUnitSphere,
17-
slerp, SphericalCap
18-
export spherical_distance
17+
slerp, SphericalCap, spherical_distance
1918

2019
end

src/utils/UnitSpherical/cap.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,25 @@ SphericalCap(geom) = SphericalCap(GI.trait(geom), geom)
1717
SphericalCap(t::GI.PointTrait, geom) = SphericalCap(t, geom, 0)
1818
# TODO: add implementations for line string and polygon traits
1919
# TODO: add implementations to merge two spherical caps
20+
function _merge(x::SphericalCap, y::SphericalCap)
21+
d = spherical_distance(x.point, y.point)
22+
newradius = (x.radius + y.radius + d) / 2
23+
if newradius < x.radius
24+
#x contains y
25+
x
26+
elseif newradius < y.radius
27+
#y contains x
28+
y
29+
else
30+
excenter = 0.5 * (1 + (y.radius - x.radius) / d)
31+
newcenter = x.point + slerp(x.point, y.point, excenter)
32+
SphericalCap(newcenter, d)
33+
end
34+
end
2035
# TODO: add implementations for multitraits based on this
2136

2237
# TODO: this returns an approximately antipodal point...
2338

24-
2539
# TODO: exact-predicate intersection
2640
# This is all inexact and thus subject to floating point error
2741
function _intersects(x::SphericalCap, y::SphericalCap)
@@ -35,6 +49,10 @@ function _contains(big::SphericalCap, small::SphericalCap)
3549
# small circle fits in big circle
3650
return dist + small.radius < big.radius
3751
end
52+
function _contains(cap::SphericalCap, point::UnitSphericalPoint)
53+
spherical_distance(cap.point, point) <= cap.radius
54+
end
55+
3856

3957
function circumcenter_on_unit_sphere(a::UnitSphericalPoint, b::UnitSphericalPoint, c::UnitSphericalPoint)
4058
LinearAlgebra.normalize(a × b + b × c + c × a)

0 commit comments

Comments
 (0)