-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
264 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
assertion(cond, msg) | ||
Throws an `AssertionError(msg)` if `cond` is `false`. | ||
""" | ||
assertion(cond, msg) = cond || throw(AssertionError(msg)) | ||
|
||
""" | ||
checkdim(geom, dim) | ||
Throws an `ArgumentError` if the `embeddim` of the geometry `geom` | ||
is different than the specified dimension `dim`. | ||
""" | ||
checkdim(geom, dim) = | ||
embeddim(geom) ≠ dim && throw(ArgumentError("geometry must be embedded in $dim-dimensional space")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
constructor(G) | ||
Given a (parametric) type `G{T₁,T₂,...}`, return the type `G`. | ||
""" | ||
constructor(G::Type) = getfield(Meshes, nameof(G)) | ||
|
||
""" | ||
fitdims(dims, D) | ||
Fit tuple `dims` to a given length `D` by repeating the last dimension. | ||
""" | ||
function fitdims(dims::Dims{N}, D) where {N} | ||
ntuple(i -> i ≤ N ? dims[i] : last(dims), D) | ||
end | ||
|
||
""" | ||
collectat(iter, inds) | ||
Collect iterator `iter` at indices `inds` without materialization. | ||
""" | ||
function collectat(iter, inds) | ||
if isempty(inds) | ||
eltype(iter)[] | ||
else | ||
m = maximum(inds) | ||
e = Iterators.enumerate(iter) | ||
w = Iterators.takewhile(x -> (first(x) ≤ m), e) | ||
f = Iterators.filter(x -> (first(x) ∈ inds), w) | ||
map(last, f) | ||
end | ||
end | ||
|
||
""" | ||
XYZ(xyz) | ||
Generate the coordinate arrays `XYZ` from the coordinate vectors `xyz`. | ||
""" | ||
@generated function XYZ(xyz::NTuple{Dim,<:AbstractVector{T}}) where {Dim,T} | ||
exprs = ntuple(Dim) do d | ||
quote | ||
a = xyz[$d] | ||
A = Array{T,Dim}(undef, length.(xyz)) | ||
@nloops $Dim i A begin | ||
@nref($Dim, A, i) = a[$(Symbol(:i_, d))] | ||
end | ||
A | ||
end | ||
end | ||
Expr(:tuple, exprs...) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
# Comparisons between unitful and non-unitful quantities | ||
|
||
isequalzero(x) = x == zero(x) | ||
isequalone(x) = x == oneunit(x) | ||
|
||
isapproxequal(x, y; atol=atol(x), kwargs...) = isapprox(x, y; atol, kwargs...) | ||
isapproxzero(x; atol=atol(x), kwargs...) = isapprox(x, zero(x); atol, kwargs...) | ||
isapproxone(x; atol=atol(x), kwargs...) = isapprox(x, oneunit(x); atol, kwargs...) | ||
|
||
ispositive(x) = x > zero(x) | ||
isnegative(x) = x < zero(x) | ||
isnonpositive(x) = x ≤ zero(x) | ||
isnonnegative(x) = x ≥ zero(x) | ||
|
||
""" | ||
mayberound(λ, x, tol) | ||
Round `λ` to `x` if it is within the tolerance `tol`. | ||
""" | ||
function mayberound(λ::T, x::T, atol=atol(T)) where {T} | ||
isapprox(λ, x, atol=atol) ? x : λ | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
withcrs(g, v) | ||
Point at the end of the vector `v` with the same CRS of `g`. | ||
""" | ||
function withcrs(g::GeometryOrDomain, v::StaticVector) | ||
C = crs(g) | ||
cart = Cartesian{datum(C)}(Tuple(v)) | ||
ctor = CoordRefSystems.constructor(C) | ||
Point(convert(ctor, cart)) | ||
end | ||
|
||
""" | ||
flat(p) | ||
Flatten coordinates of point `p` to Cartesian coordinates, | ||
ignoring the original units of the coordinate reference system. | ||
""" | ||
flat(p::Point) = Point(flat(coords(p))) | ||
flat(c::CRS) = Cartesian{datum(c)}(CoordRefSystems.raw(c)) |
Oops, something went wrong.