This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
/
transformation.jl
243 lines (206 loc) · 7.18 KB
/
transformation.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
function Transformation(transform_func=identity)
flip = Node((false, false, false))
scale = Node(Vec3f0(1))
scale = lift(flip, scale) do f, s
map((f, s)-> f ? -s : s, Vec(f), s)
end
translation, rotation, align = (
Node(Vec3f0(0)),
Node(Quaternionf0(0, 0, 0, 1)),
Node(Vec2f0(0))
)
trans = nothing
model = map_once(scale, translation, rotation, align, flip) do s, o, q, a, flip
parent = if trans !== nothing && isassigned(trans.parent)
boundingbox(trans.parent[])
else
nothing
end
transformationmatrix(o, s, q, a, flip, parent)
end
return Transformation(
translation,
scale,
rotation,
model,
flip,
align,
Node{Any}(transform_func)
)
end
function Transformation(scene::SceneLike)
flip = Node((false, false, false))
scale = Node(Vec3f0(1))
translation, rotation, align = (
Node(Vec3f0(0)),
Node(Quaternionf0(0, 0, 0, 1)),
Node(Vec2f0(0))
)
pmodel = transformationmatrix(scene)
trans = nothing
model = map_once(scale, translation, rotation, align, pmodel, flip) do s, o, q, a, p, f
bb = if trans !== nothing && isassigned(trans.parent)
boundingbox(trans.parent[])
else
nothing
end
return p * transformationmatrix(o, s, q, align, f, bb)
end
ptrans = transformation(scene)
trans = Transformation(
translation,
scale,
rotation,
model,
flip,
align,
copy(ptrans.transform_func)
)
return trans
end
function translated(scene::Scene, translation...)
tscene = Scene(scene, transformation = Transformation())
transform!(tscene, translation...)
tscene
end
function translated(scene::Scene; kw_args...)
tscene = Scene(scene, transformation = Transformation())
transform!(tscene; kw_args...)
tscene
end
function transform!(
scene::SceneLike;
translation = Vec3f0(0),
scale = Vec3f0(1),
rotation = 0.0,
)
translate!(scene, translation)
scale!(scene, scale)
rotate!(scene, rotation)
end
transformation(t::Scene) = t.transformation
transformation(t::AbstractPlot) = t.transformation
transformation(t::Transformation) = t
scale(t::Transformable) = transformation(t).scale
scale!(t::Transformable, s) = (scale(t)[] = to_ndim(Vec3f0, Float32.(s), 1))
"""
scale!(t::Transformable, x, y)
scale!(t::Transformable, x, y, z)
scale!(t::Transformable, xyz)
scale!(t::Transformable, xyz...)
Scale the given `Transformable` (a Scene or Plot) to the given arguments.
Can take `x, y` or `x, y, z`.
This is an absolute scaling, and there is no option to perform relative scaling.
"""
scale!(t::Transformable, xyz...) = scale!(t, xyz)
rotation(scene::Transformable) = transformation(scene).rotation
function rotate!(::Type{T}, scene::Transformable, q) where T
rot = convert_attribute(q, key"rotation"())
if T === Accum
rot1 = rotation(scene)[]
rotation(scene)[] = rot1 * rot
elseif T == Absolute
rotation(scene)[] = rot
else
error("Unknown transformation: $T")
end
end
"""
rotate!(Accum, scene::Transformable, axis_rot...)
Apply a relative rotation to the Scene, by multiplying by the current rotation.
"""
rotate!(::Type{T}, scene::Transformable, axis_rot...) where T = rotate!(T, scene, axis_rot)
"""
rotate!(scene::Transformable, axis_rot::Quaternion)
rotate!(scene::Transformable, axis_rot::AbstractFloat)
rotate!(scene::Transformable, axis_rot...)
Apply an absolute rotation to the Scene. Rotations are all internally converted to `Quaternion`s.
"""
rotate!(scene::Transformable, axis_rot...) = rotate!(Absolute, scene, axis_rot)
rotate!(scene::Transformable, axis_rot::Quaternion) = rotate!(Absolute, scene, axis_rot)
rotate!(scene::Transformable, axis_rot::AbstractFloat) = rotate!(Absolute, scene, axis_rot)
translation(scene::Transformable) = transformation(scene).translation
"""
Accum
Force transformation to be relative to the current state, not absolute.
"""
struct Accum end
"""
Absolute
Force transformation to be absolute, not relative to the current state.
This is the default setting.
"""
struct Absolute end
function translate!(::Type{T}, scene::Transformable, t) where T
offset = to_ndim(Vec3f0, Float32.(t), 0)
if T === Accum
translation(scene)[] = translation(scene)[] .+ offset
elseif T === Absolute
translation(scene)[] = offset
else
error("Unknown translation type: $T")
end
end
"""
translate!(scene::Transformable, xyz::VecTypes)
translate!(scene::Transformable, xyz...)
Apply an absolute translation to the Scene, translating it to `x, y, z`.
"""
translate!(scene::Transformable, xyz::VecTypes) = translate!(Absolute, scene, xyz)
translate!(scene::Transformable, xyz...) = translate!(Absolute, scene, xyz)
"""
translate!(Accum, scene::Transformable, xyz...)
Translate the scene relative to its current position.
"""
translate!(::Type{T}, scene::Transformable, xyz...) where T = translate!(T, scene, xyz)
function transform!(scene::Transformable, x::Tuple{Symbol, <: Number})
plane, dimval = string(x[1]), Float32(x[2])
if length(plane) != 2 || (!all(x-> x in ('x', 'y', 'z'), plane))
error("plane needs to define a 2D plane in xyz. It should only contain 2 symbols out of (:x, :y, :z). Found: $plane")
end
if all(x-> x in ('x', 'y'), plane) # xy plane
translate!(scene, 0, 0, dimval)
elseif all(x-> x in ('x', 'z'), plane) # xz plane
rotate!(scene, Vec3f0(1, 0, 0), 0.5pi)
translate!(scene, 0, dimval, 0)
else #yz plane
r1 = qrotation(Vec3f0(0, 1, 0), 0.5pi)
r2 = qrotation(Vec3f0(1, 0, 0), 0.5pi)
rotate!(scene, r2 * r1)
translate!(scene, dimval, 0, 0)
end
scene
end
transformationmatrix(x) = transformation(x).model
transform_func(x) = transform_func_obs(x)[]
transform_func_obs(x) = transformation(x).transform_func
"""
apply_transform(f, data)
Apply the data transform func to the data
"""
apply_transform(f::typeof(identity), position::Number) = position
apply_transform(f::typeof(identity), positions::AbstractArray) = positions
apply_transform(f::typeof(identity), positions::AbstractVector) = positions
apply_transform(f::typeof(identity), position::VecTypes) = position
function apply_transform(f, data::AbstractArray)
return map(point-> apply_transform(f, point), data)
end
function apply_transform(f::NTuple{N, Any}, point::VecTypes{N}) where {N, T}
return Point{N, Float32}(ntuple(i-> apply_transform(f[i], point[i]), N))
end
apply_transform(f, number::Number) = f(number)
function apply_transform(f::Union{typeof(log), typeof(log10), typeof(log2)}, number::Number)
if number <= 0.0
return 0.0
else
return f(number)
end
end
function apply_transform(f::Observable, data::Observable)
return lift((f, d)-> apply_transform(f, d), f, data)
end
apply_transform(f, itr::Pair) = apply_transform(f, itr[1]) => apply_transform(f, itr[2])
function apply_transform(f, itr::ClosedInterval)
mini, maxi = extrema(itr)
return apply_transform(f, mini) .. apply_transform(f, maxi)
end