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

Fix viz of projected grids with invalid elements #1140

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions ext/MeshesMakieExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using Colorfy

using Unitful: numtype
using Meshes: lentype
using CoordRefSystems: Projected

import TransformsBase as TB
import Makie.GeometryBasics as GB
Expand Down
63 changes: 60 additions & 3 deletions ext/grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
# process color spec into colorant
colorant = Makie.@lift process($color, $colormap, $colorrange, $alpha)

colors = Makie.@lift begin
if $colorant isa AbstractVector
inds = _invalidinds($grid)
[i ∈ inds ? Makie.Colors.coloralpha(c, 0) : c for (i, c) in enumerate($colorant)]

Check warning on line 56 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L53-L56

Added lines #L53 - L56 were not covered by tests
else
$colorant

Check warning on line 58 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L58

Added line #L58 was not covered by tests
end
end

# number of vertices, elements and colors
nverts = Makie.@lift nvertices($grid)
nelems = Makie.@lift nelements($grid)
Expand All @@ -69,11 +78,11 @@
dims = Makie.@lift size($grid)
vdims = Makie.@lift Meshes.vsize($grid)
texture = if ncolor[] == 1
Makie.@lift fill($colorant, $dims)
Makie.@lift fill($colors, $dims)

Check warning on line 81 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L81

Added line #L81 was not covered by tests
elseif ncolor[] == nelems[]
Makie.@lift reshape($colorant, $dims)
Makie.@lift reshape($colors, $dims)

Check warning on line 83 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L83

Added line #L83 was not covered by tests
elseif ncolor[] == nverts[]
Makie.@lift reshape($colorant, $vdims)
Makie.@lift reshape($colors, $vdims)

Check warning on line 85 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L85

Added line #L85 was not covered by tests
else
throw(ArgumentError("invalid number of colors"))
end
Expand All @@ -96,6 +105,54 @@

_reverse(grid) = crs(grid) <: LatLon && orientation(first(grid)) == CW ? reverse : identity

function _invalidinds(grid)
if crs(grid) <: Projected && (grid isa RegularGrid || (grid isa TransformedGrid && parent(grid) isa RegularGrid))
tgrid = grid |> Proj(LatLon)

Check warning on line 110 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L108-L110

Added lines #L108 - L110 were not covered by tests

dims = size(tgrid)
topo = topology(tgrid)
sx, sy = dims
B = Boundary{2,0}(topo)

Check warning on line 115 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L112-L115

Added lines #L112 - L115 were not covered by tests

lon(i) = coords(vertex(tgrid, i)).lon
function isinvalid(e)
is = B(e)
lon(is[1]) > lon(is[3])

Check warning on line 120 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L117-L120

Added lines #L117 - L120 were not covered by tests
end

inds = Int[]
linds = LinearIndices(dims)
for i in 1:sx
e = linds[i, 1]
if isinvalid(e)
push!(inds, e)

Check warning on line 128 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L123-L128

Added lines #L123 - L128 were not covered by tests
end
end

Check warning on line 130 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L130

Added line #L130 was not covered by tests

# @info inds

if !isempty(inds)
A = Adjacency{2}(topo)
while true
e = last(inds)
es = setdiff(A(e), inds)
i = findfirst(isinvalid, es)
if isnothing(i)
break

Check warning on line 141 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L134-L141

Added lines #L134 - L141 were not covered by tests
else
push!(inds, es[i])

Check warning on line 143 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L143

Added line #L143 was not covered by tests
# @info inds
end
end
inds

Check warning on line 147 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L146-L147

Added lines #L146 - L147 were not covered by tests
else
Int[]

Check warning on line 149 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L149

Added line #L149 was not covered by tests
end
else
Int[]

Check warning on line 152 in ext/grid.jl

View check run for this annotation

Codecov / codecov/patch

ext/grid.jl#L152

Added line #L152 was not covered by tests
end
end

# helper functions to create a minimum number
# of line segments within Cartesian/Rectilinear grid
function xysegments(xs, ys)
Expand Down
Loading