|
| 1 | +# ------------------------------------------------------------------ |
| 2 | +# Licensed under the MIT License. See LICENSE in the project root. |
| 3 | +# ------------------------------------------------------------------ |
| 4 | + |
| 5 | +""" |
| 6 | + RegularGrid(dims, origin, spacing) |
| 7 | +
|
| 8 | +A regular grid with dimensions `dims`, lower left corner at `origin` |
| 9 | +and cell spacing `spacing`. The three arguments must have the same length. |
| 10 | +
|
| 11 | + RegularGrid(dims, origin, spacing, offset) |
| 12 | +
|
| 13 | +A regular grid with dimensions `dims`, with lower left corner of element |
| 14 | +`offset` at `origin` and cell spacing `spacing`. |
| 15 | +
|
| 16 | +## Examples |
| 17 | +
|
| 18 | +``` |
| 19 | +RegularGrid((10, 20), Point(LatLon(30.0°, 60.0°)), (1.0, 1.0)) # add coordinate units to spacing |
| 20 | +RegularGrid((10, 20), Point(Polar(0.0mm, 0.0rad)), (10.0mm, 1.0rad)) # convert spacing units to coordinate units |
| 21 | +RegularGrid((10, 20), Point(Marcator(0.0, 0.0)), (1.5, 1.5)) |
| 22 | +RegularGrid((10, 20, 30), Point(Cylindrical(0.0, 0.0, 0.0)), (3.0, 2.0, 1.0)) |
| 23 | +``` |
| 24 | +
|
| 25 | +See also [`CartesianGrid`](@ref). |
| 26 | +""" |
| 27 | +struct RegularGrid{M<:Manifold,C<:CRS,S<:Tuple,N} <: Grid{M,C,N} |
| 28 | + origin::Point{M,C} |
| 29 | + spacing::S |
| 30 | + offset::Dims{N} |
| 31 | + topology::GridTopology{N} |
| 32 | + |
| 33 | + function RegularGrid{M,C,S,N}(origin, spacing, offset, topology) where {M<:Manifold,C<:CRS,S<:Tuple,N} |
| 34 | + if !all(s -> s > zero(s), spacing) |
| 35 | + throw(ArgumentError("spacing must be positive")) |
| 36 | + end |
| 37 | + new(origin, spacing, offset, topology) |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 41 | +function RegularGrid( |
| 42 | + origin::Point{M,C}, |
| 43 | + spacing::Tuple, |
| 44 | + offset::Dims{N}, |
| 45 | + topology::GridTopology{N} |
| 46 | +) where {M<:Manifold,C<:CRS,N} |
| 47 | + if manifold(origin) <: 🌐 && !(crs(origin) <: LatLon) |
| 48 | + throw(ArgumentError("regular spacing on `🌐` requires `LatLon` coordinates")) |
| 49 | + end |
| 50 | + |
| 51 | + T = CoordRefSystems.mactype(C) |
| 52 | + nc = CoordRefSystems.ncoords(C) |
| 53 | + us = CoordRefSystems.units(C) |
| 54 | + ns = length(spacing) |
| 55 | + |
| 56 | + if N ≠ nc |
| 57 | + throw(ArgumentError(""" |
| 58 | + A $N-dimensional regular grid requires an origin with $N coordinates. |
| 59 | + The provided origin has $nc coordinates. |
| 60 | + """)) |
| 61 | + end |
| 62 | + |
| 63 | + if ns ≠ nc |
| 64 | + throw(ArgumentError(""" |
| 65 | + A $N-dimensional regular grid requires $N spacing values. |
| 66 | + The provided spacing has $ns values. |
| 67 | + """)) |
| 68 | + end |
| 69 | + |
| 70 | + sp = ntuple(i -> numconvert(T, _withunit(spacing[i], us[i])), nc) |
| 71 | + |
| 72 | + RegularGrid{M,C,typeof(sp),N}(origin, sp, offset, topology) |
| 73 | +end |
| 74 | + |
| 75 | +function RegularGrid(dims::Dims{N}, origin::Point, spacing::Tuple, offset::Dims{N}=ntuple(i -> 1, N)) where {N} |
| 76 | + if !all(>(0), dims) |
| 77 | + throw(ArgumentError("dimensions must be positive")) |
| 78 | + end |
| 79 | + RegularGrid(origin, spacing, offset, GridTopology(dims)) |
| 80 | +end |
| 81 | + |
| 82 | +spacing(g::RegularGrid) = g.spacing |
| 83 | + |
| 84 | +offset(g::RegularGrid) = g.offset |
| 85 | + |
| 86 | +function vertex(g::RegularGrid, ijk::Dims) |
| 87 | + ctor = CoordRefSystems.constructor(crs(g)) |
| 88 | + orig = CoordRefSystems.values(coords(g.origin)) |
| 89 | + vals = orig .+ (ijk .- g.offset) .* g.spacing |
| 90 | + Point(ctor(vals...)) |
| 91 | +end |
| 92 | + |
| 93 | +@generated function xyz(g::RegularGrid{M,C,S,N}) where {M,C,S,N} |
| 94 | + exprs = ntuple(N) do i |
| 95 | + :(range(start=orig[$i], step=spac[$i], length=(dims[$i] + 1))) |
| 96 | + end |
| 97 | + |
| 98 | + quote |
| 99 | + dims = size(g) |
| 100 | + spac = spacing(g) |
| 101 | + orig = CoordRefSystems.values(coords(g.origin)) |
| 102 | + ($(exprs...),) |
| 103 | + end |
| 104 | +end |
| 105 | + |
| 106 | +XYZ(g::RegularGrid) = XYZ(xyz(g)) |
| 107 | + |
| 108 | +function Base.getindex(g::RegularGrid, I::CartesianIndices) |
| 109 | + @boundscheck _checkbounds(g, I) |
| 110 | + dims = size(I) |
| 111 | + offset = g.offset .- Tuple(first(I)) .+ 1 |
| 112 | + RegularGrid(dims, g.origin, g.spacing, offset) |
| 113 | +end |
| 114 | + |
| 115 | +function ==(g₁::RegularGrid, g₂::RegularGrid) |
| 116 | + orig₁ = CoordRefSystems.values(coords(g₁.origin)) |
| 117 | + orig₂ = CoordRefSystems.values(coords(g₂.origin)) |
| 118 | + g₁.topology == g₂.topology && g₁.spacing == g₂.spacing && orig₁ .- orig₂ == (g₁.offset .- g₂.offset) .* g₁.spacing |
| 119 | +end |
| 120 | + |
| 121 | +# ----------- |
| 122 | +# IO METHODS |
| 123 | +# ----------- |
| 124 | + |
| 125 | +function Base.summary(io::IO, g::RegularGrid) |
| 126 | + dims = join(size(g.topology), "×") |
| 127 | + name = prettyname(g) |
| 128 | + print(io, "$dims $name") |
| 129 | +end |
| 130 | + |
| 131 | +function Base.show(io::IO, ::MIME"text/plain", g::RegularGrid) |
| 132 | + summary(io, g) |
| 133 | + println(io) |
| 134 | + println(io, "├─ minimum: ", minimum(g)) |
| 135 | + println(io, "├─ maximum: ", maximum(g)) |
| 136 | + print(io, "└─ spacing: ", spacing(g)) |
| 137 | +end |
| 138 | + |
| 139 | +# ----------------- |
| 140 | +# HELPER FUNCTIONS |
| 141 | +# ----------------- |
| 142 | + |
| 143 | +_withunit(x::Number, u) = x * u |
| 144 | +_withunit(x::Quantity, u) = uconvert(u, x) |
0 commit comments