Skip to content

Commit

Permalink
Rework Geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
luraess committed Mar 10, 2024
1 parent e202c25 commit 17023f9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
2 changes: 0 additions & 2 deletions src/FastIce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ https://github.com/PTsolvers/FastIce.jl
greet(; kwargs...) = printstyled(GREETING; kwargs...)
greet_fast(; kwargs...) = printstyled(GREETING_FAST; kwargs...)

# export Geometry

# core modules (included in alphabetical order)
include("Geometry.jl")
include("LevelSets/LevelSets.jl")
Expand Down
59 changes: 48 additions & 11 deletions src/Geometry.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Geometry

export AbstractElevation, AABB, DataElevation, SyntheticElevation
export domain, rotated_domain, rotation, generate_elevation, extents, center, dilate
# export AbstractElevation, AABB, DataElevation, SyntheticElevation
# export domain, rotated_domain, rotation, generate_elevation, extents, center, dilate

abstract type AbstractElevation{T<:Real} end

Expand Down Expand Up @@ -35,19 +35,15 @@ domain(dem::SyntheticElevation) = dem.domain
rotated_domain(dem::AbstractElevation) = domain(dem)
rotation(::AbstractElevation) = [1.0 0.0 0.0; 0.0 1.0 0.0; 0.0 0.0 1.0]

Check warning on line 36 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L34-L36

Added lines #L34 - L36 were not covered by tests

domain(dem::DataElevation) = dem.domain
rotated_domain(dem::DataElevation) = dem.rotated_domain
rotation(dem::DataElevation) = dem.rotation

Check warning on line 40 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L38-L40

Added lines #L38 - L40 were not covered by tests

"Construct AABB from coordinates"
AABB(xs, ys, zs) = AABB(extrema(xs)..., extrema(ys)..., extrema(zs)...)

Check warning on line 43 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L43

Added line #L43 was not covered by tests

"Generate SyntheticElevation data."
function generate_elevation(lx, ly, zmin, zmax, z_bed, z_surf)
domain = AABB(-lx / 2, lx / 2, -ly / 2, ly / 2, zmin, zmax)
return SyntheticElevation(z_bed, z_surf, domain)
end

"AABB extents"
function extents(box::AABB)
return box.xmax - box.xmin, box.ymax - box.ymin, box.zmax - box.zmin
end
extents(box::AABB) = box.xmax - box.xmin, box.ymax - box.ymin, box.zmax - box.zmin

Check warning on line 46 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L46

Added line #L46 was not covered by tests

"AABB center"
function center(box::AABB{T}) where {T}
Expand All @@ -61,4 +57,45 @@ function dilate(box::AABB, fractions)
return AABB(box.xmin - Δx, box.xmax + Δx, box.ymin - Δy, box.ymax + Δy, box.zmin - Δz, box.zmax + Δz)

Check warning on line 57 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L55-L57

Added lines #L55 - L57 were not covered by tests
end

"Filter NaNs."
filtered(X) = filter(_x -> !isnan(_x), X)

Check warning on line 61 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L61

Added line #L61 was not covered by tests

"Create AABB enclosing both box1 and box2"
function union(box1::AABB, box2::AABB)
return AABB(min(box1.xmin, box2.xmin), max(box1.xmax, box2.xmax),

Check warning on line 65 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L64-L65

Added lines #L64 - L65 were not covered by tests
min(box1.ymin, box2.ymin), max(box1.ymax, box2.ymax),
min(box1.zmin, box2.zmin), max(box1.zmax, box2.zmax))
end

"Rotate field `X`, `Y`, `Z` with rotation matrix `R`."
function rotate(X, Y, Z, R)
xrot = R[1, 1] .* X .+ R[1, 2] .* Y .+ R[1, 3] .* Z
yrot = R[2, 1] .* X .+ R[2, 2] .* Y .+ R[2, 3] .* Z
zrot = R[3, 1] .* X .+ R[3, 2] .* Y .+ R[3, 3] .* Z
return xrot, yrot, zrot

Check warning on line 75 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L71-L75

Added lines #L71 - L75 were not covered by tests
end

"Rotate field `X`, `Y`, `Z` with rotation matrix `R` and return extents."
rotate_minmax(X, Y, Z, R) = rotate(collect(extrema(X)), collect(extrema(Y)), collect(extrema(filtered(Z))), R)

Check warning on line 79 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L79

Added line #L79 was not covered by tests

"Generate SyntheticElevation data."
function generate_elevation(lx, ly, zmin, zmax, z_bed, z_surf)
domain = AABB(-lx / 2, lx / 2, -ly / 2, ly / 2, zmin, zmax)
return SyntheticElevation(z_bed, z_surf, domain)

Check warning on line 84 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L82-L84

Added lines #L82 - L84 were not covered by tests
end

"Generate DataElevation data."
function DataElevation(x, y, offsets, z_bed, z_surf, R)

Check warning on line 88 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L88

Added line #L88 was not covered by tests
# get non-rotated domain
domain = AABB(extrema(x)..., extrema(y)..., Float64(minimum([minimum(filtered(z_bed)), minimum(filtered(z_surf))])),

Check warning on line 90 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L90

Added line #L90 was not covered by tests
Float64(maximum([maximum(filtered(z_bed)), maximum(filtered(z_surf))])))
# rotate bed and surface
bed_extents = AABB(rotate_minmax(x, y, z_bed, R)...)
surf_extents = AABB(rotate_minmax(x, y, z_surf, R)...)

Check warning on line 94 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L93-L94

Added lines #L93 - L94 were not covered by tests
# get rotated domain
rotated_domain = union(bed_extents, surf_extents)
dem = DataElevation(x, y, offsets, z_bed, z_surf, R, domain, rotated_domain)
return dem

Check warning on line 98 in src/Geometry.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry.jl#L96-L98

Added lines #L96 - L98 were not covered by tests
end

end # module

0 comments on commit 17023f9

Please sign in to comment.