Skip to content

Spherical cap plot recipe #363

@asinghvi17

Description

@asinghvi17
using GLMakie
import GeometryOps as GO
import GeometryOps.UnitSpherical
import LinearAlgebra, Rotations, GeometryBasics

function Makie.convert_arguments(::Type{Makie.Mesh}, cap::UnitSpherical.SphericalCap)
    N = 40
    rmin = 0.1
    rect = GeometryBasics.Tesselation(Rect2d(0, 0, 2pi, cap.radius), (N, ceil(Int, cap.radius / rmin)))
    faces = GeometryBasics.decompose(Makie.GLTriangleFace, rect)
    points = GeometryBasics.coordinates(rect)
    # Remove the first N points and set the first point to (0,0)
    # such that the first point is the center of the cap.
    points = points[N:end]
    points[1] = Point2d(0)
    faces = map(faces) do f
        Makie.GLTriangleFace(
            f[1] <= N ? Makie.GLIndex(1) : f[1] - N + 1,
            f[2] <= N ? Makie.GLIndex(1) : f[2] - N + 1,
            f[3] <= N ? Makie.GLIndex(1) : f[3] - N + 1,
        )
    end
    # Convert back to unit-spherical space.
    three_d_points = map(points) do (theta, phi)
        if phi == 0
            offset_point = cap.point
        else
            offset_point = LinearAlgebra.normalize(cap.point + LinearAlgebra.normalize(Point3d(cos(phi), sin(phi), 0.0)))
        end
        LinearAlgebra.normalize(Point3d(cap.point + phi * Rotations.AngleAxis(theta, cap.point...) * offset_point))
    end
    # Convert back to geographic space.
    # This is so that `zlevel` works right.
    return (GeometryBasics.normal_mesh(UnitSpherical.GeographicFromUnitSphere().(three_d_points) .|> Point2d, faces),)
    # return (GeometryBasics.normal_mesh(three_d_points, faces),)
end
Makie.convert_arguments(::Type{Makie.Poly}, cap::UnitSpherical.SphericalCap) = Makie.convert_arguments(Makie.Mesh, cap)
cap = UnitSpherical.SphericalCap((90, 0), 0.3)
f, a, p = meshimage(-180..180, -90..90, GeoMakie.earth(); uv_transform = :rotr90, axis = (; type = GlobeAxis, camera_longlat = (90, 0)))
poly!(a, cap; alpha = 0.7, zlevel = 100_000)
f
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions