-
Notifications
You must be signed in to change notification settings - Fork 79
/
scale.jl
53 lines (38 loc) · 1.17 KB
/
scale.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------
"""
Scale(s₁, s₂, ...)
Scale geometry or domain with
strictly positive scaling factors
`s₁, s₂, ...`.
## Examples
```julia
Scale(1.0, 2.0, 3.0)
```
"""
struct Scale{Dim,T} <: CoordinateTransform
factors::NTuple{Dim,T}
function Scale{Dim,T}(factors) where {Dim,T}
any(≤(0), factors) && throw(ArgumentError("Scaling factors must be positive."))
new(factors)
end
end
Scale(factors::NTuple{Dim,T}) where {Dim,T} = Scale{Dim,T}(factors)
Scale(factors...) = Scale(factors)
parameters(t::Scale) = (; factors=t.factors)
isaffine(::Type{<:Scale}) = true
isrevertible(::Type{<:Scale}) = true
isinvertible(::Type{<:Scale}) = true
inverse(t::Scale) = Scale(1 ./ t.factors)
applycoord(t::Scale, v::Vec) = t.factors .* v
# --------------
# SPECIAL CASES
# --------------
function applycoord(t::Scale, g::CartesianGrid)
dims = size(g)
orig = applycoord(t, minimum(g))
spac = t.factors .* spacing(g)
offs = offset(g)
CartesianGrid(dims, orig, spac, offs)
end