/
viewer3D.jl
308 lines (272 loc) · 10.9 KB
/
viewer3D.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#= MIT License
Copyright (c) 2020, 2021 Uwe Fechner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. =#
@consts begin
SCALE = 1.2
INITIAL_HEIGHT = 80.0*se().zoom # meter, for demo
MAX_HEIGHT = 200.0*se().zoom # meter, for demo
KITE = FileIO.load(joinpath(dirname(get_data_path()), se().model))
FLYING = [false]
PLAYING = [false]
GUI_ACTIVE = [false]
AXIS_LABEL_SIZE = 30
TEXT_SIZE = 16
running = Observable(false)
starting = [0]
zoom = [1.0]
steering = [0.0]
textnode = Observable("") # lower left
textnode2 = Observable("") # upper right
fontsize = Observable(TEXT_SIZE)
fontsize2 = Observable(AXIS_LABEL_SIZE)
status = Observable("")
plot_file = Observable("")
p1 = Observable(Vector{Point2f}(undef, 6000)) # 5 min
p2 = Observable(Vector{Point2f}(undef, 6000)) # 5 min
pos_x = Observable(0.0f0)
quat = Observable(Quaternionf(0,0,0,1)) # orientation of the kite
kite_pos = Observable(Point3f(1,0,0)) # position of the kite
end
last_status::String=""
"""
abstract type AbstractKiteViewer
All kite viewers must inherit from this type. All methods that are defined on this type must work
with all kite viewers. All exported methods must work on this type.
"""
abstract type AbstractKiteViewer end
"""
const AKV = AbstractKiteViewer
Short alias for the AbstractKiteViewer.
"""
const AKV = AbstractKiteViewer
mutable struct Viewer3D <: AKV
fig::Figure
scene3D::LScene
cam::Camera3D
screen::GLMakie.Screen
points::Vector{Point{3, Float32}}
positions::Observable{Vector{GeometryBasics.Point{3, Float32}}}
part_positions::Observable{Vector{GeometryBasics.Point{3, Float32}}}
markersizes::Observable{Vector{GeometryBasics.Point{3, Float32}}}
rotations::Observable{Vector{GeometryBasics.Point{3, Float32}}}
set::Settings
btn_RESET::Button
btn_ZOOM_in::Button
btn_ZOOM_out::Button
btn_PLAY::Button
btn_AUTO::Button
btn_PARKING::Button
btn_STOP::Button
menu::Union{Menu, Nothing}
menu_rel_tol::Union{Menu, Nothing}
menu_time_lapse::Union{Menu, Nothing}
menu_project::Union{Menu, Nothing}
t_sim::Union{Textbox, Nothing}
btn_OK::Union{Button, Nothing}
sw::Toggle
step::Int64
mod_text::Int64 # 4 means at 40Hz update rate the text is updated only at 10Hz
energy::Float64
show_kite::Bool
stop::Bool
end
function clear_viewer(kv::AKV)
kv.step = 1
kv.energy = 0
stop(kv)
end
function stop(kv::AKV)
kv.stop = true
status[]="Stopped"
running[]=false
end
function pause(kv::AKV)
global last_status
kv.stop = true
last_status=status[]
status[]="Paused"
end
function set_status(kv::AKV, status_text)
global last_status
if status_text != "Paused"
last_status = status[]
end
status[] = status_text
end
function Viewer3D(show_kite=true, autolabel="Autopilot"; precompile=false)
set = se()
Viewer3D(set, show_kite, autolabel; precompile)
end
function Viewer3D(set::Settings, show_kite=true, autolabel="Autopilot"; precompile=false, menus=false)
global last_status
WIDTH = 840
HEIGHT = 900
fig = Figure(size=(WIDTH, HEIGHT), backgroundcolor=RGBf(0.7, 0.8, 1))
sub_fig = fig[1,1]
if menus
menu1 = Menu(fig, bbox = fig.scene.viewport,
options = ["plot_main", "plot_control", "plot_elev_az", "plot_timing", "load simulation", "save simulation"], default = "plot_main")
menu1.width[] =120
menu1.halign[]=:left
menu1.valign[]=:top
menu1.alignmode[]=Outside(30)
menu2 = Menu(fig, bbox = fig.scene.viewport,
options = ["0.001", "0.0001", "0.00001", "0.000001"], default = "0.001")
menu2.width[] =120
menu2.halign[]=:left
menu2.valign[]=:top
menu2.alignmode[]=Outside(30, 0, 0, 60)
label2 = Label(fig, "rel_tol", bbox=fig.scene.viewport)
label2.halign[]=:left
label2.valign[]=:top
label2.alignmode=Outside(160, 0, 0, 70)
menu3 = Menu(fig, bbox = fig.scene.viewport,
options = ["1x", "2x", "3x", "4x", "6x", "8x"], default = "4x")
menu3.width[] =120
menu3.halign[]=:left
menu3.valign[]=:top
menu3.alignmode[]=Outside(30, 0, 0, 90)
label3 = Label(fig, "time_lapse", bbox=fig.scene.viewport)
label3.halign[]=:left
label3.valign[]=:top
label3.alignmode=Outside(160, 0, 0, 100)
menu4 = Menu(fig, bbox = fig.scene.viewport,
options = ["load...", "save as...", "edit..."], default = "load...")
menu4.width[] =120
menu4.halign[]=:left
menu4.valign[]=:top
menu4.alignmode[]=Outside(30, 0, 0, 120)
label4 = Label(fig, "project", bbox=fig.scene.viewport)
label4.halign[]=:left
label4.valign[]=:top
label4.alignmode=Outside(160, 0, 0, 130)
tb = Textbox(fig, bbox = fig.scene.viewport, placeholder = "Simulation time",
validator = UInt16, stored_string="460")
tb.width[] =120
tb.halign[]=:left
tb.valign[]=:top
tb.alignmode[]=Outside(30, 0, 0, 160)
label3 = Label(fig, "t_sim [s]", bbox=fig.scene.viewport)
label3.halign[]=:left
label3.valign[]=:top
label3.alignmode=Outside(160, 0, 0, 167)
btn_OK = Button(fig, bbox=fig.scene.viewport, label = "OK")
btn_OK.halign[]=:left
btn_OK.valign[]=:top
btn_OK.alignmode[]=Outside(160, 0, 0, 30)
else
menu1=nothing
menu2=nothing
menu3=nothing
menu4=nothing
tb = nothing
btn_OK=nothing
end
scene2D = LScene(fig[3,1], show_axis=false, height=16)
scene3D = LScene(sub_fig, show_axis=false, scenekw=(limits=Rect(-7,-10.0,0, 11,10,11), size=(800, 800)))
create_coordinate_system(scene3D)
cam = cameracontrols(scene3D.scene)
FLYING[1] = false
PLAYING[1] = false
GUI_ACTIVE[1] = true
reset_view(cam, scene3D)
fontsize[] = TEXT_SIZE
fontsize2[] = AXIS_LABEL_SIZE
text!(scene3D, "z", position = Point3f(0, 0, 14.6), fontsize = fontsize2, align = (:center, :center), show_axis = false)
text!(scene3D, "x", position = Point3f(17, 0,0), fontsize = fontsize2, align = (:center, :center), show_axis = false)
text!(scene3D, "y", position = Point3f( 0, 14.5, 0), fontsize = fontsize2, align = (:center, :center), show_axis = false)
text!(scene2D, status, position = Point2f( 20, 0), fontsize = TEXT_SIZE, align = (:left, :bottom), show_axis = false, space=:pixel)
text!(scene2D, plot_file, position = Point2f( 420, 0), fontsize = TEXT_SIZE, align = (:left, :bottom), show_axis = false, space=:pixel)
textnode2[]="depower\nsteering:"
fig[2, 1] = buttongrid = GridLayout(tellwidth=false)
l_sublayout = GridLayout()
fig[1:3, 1] = l_sublayout
l_sublayout[:v] = [scene3D, buttongrid, scene2D]
btn_RESET = Button(sub_fig, label = "RESET")
btn_ZOOM_in = Button(sub_fig, label = "Zoom +")
btn_ZOOM_out = Button(sub_fig, label = "Zoom -")
if precompile
btn_PLAY_PAUSE = Button(sub_fig, label = " RUN ")
else
btn_PLAY_PAUSE = Button(sub_fig, label = @lift($running ? "PAUSE" : " RUN "))
end
btn_AUTO = Button(sub_fig, label = autolabel)
btn_PARKING = Button(sub_fig, label = "Parking")
btn_STOP = Button(sub_fig, label = "STOP")
sw = Toggle(sub_fig, active = false)
label = Label(sub_fig, "repeat")
buttongrid[1, 1:9] = [btn_PLAY_PAUSE, btn_ZOOM_in, btn_ZOOM_out, btn_RESET, btn_AUTO,
btn_PARKING, btn_STOP, sw, label]
gl_screen = display(fig)
FLYING[1] = false
PLAYING[1] = false
GUI_ACTIVE[1] = true
reset_view(cam, scene3D)
points=Vector{Point3f}(undef, set.segments+1+4)
pos=Observable([Point3f(x,0,0) for x in 1:set.segments+KITE_SPRINGS]) # positions of the tether segments
part_pos=Observable([Point3f(x,0,0) for x in 1:set.segments+1+4]) # positions of the tether particles
markersizes = Observable([Point3f(1,1,1) for x in 1:set.segments+KITE_SPRINGS]) # includes the segment length
rotations = Observable([Point3f(1,0,0) for x in 1:set.segments+KITE_SPRINGS]) #unit vectors corresponding with
#the orientation of the segments
mod_text = 4
s = Viewer3D(fig, scene3D, cam, gl_screen, points, pos, part_pos, markersizes,
rotations, set, btn_RESET, btn_ZOOM_in, btn_ZOOM_out,
btn_PLAY_PAUSE, btn_AUTO, btn_PARKING, btn_STOP, menu1, menu2, menu3, menu4, tb, btn_OK,
sw, 0, mod_text, 0, show_kite, false)
txt2 = init_system(s, s.scene3D; show_kite=show_kite)
camera = cameracontrols(s.scene3D.scene)
reset_view(camera, s.scene3D)
set_status(s, "Stopped")
on(s.btn_RESET.clicks) do c
reset_view(camera, s.scene3D)
zoom[1] = 1.0
end
on(s.btn_ZOOM_in.clicks) do c
zoom[1] *= 1.2
reset_and_zoom(camera, s.scene3D, zoom[1])
end
on(s.btn_ZOOM_out.clicks) do c
zoom[1] /= 1.2
reset_and_zoom(camera, s.scene3D, zoom[1])
end
on(s.btn_STOP.clicks) do c
stop(s)
end
on(s.btn_PLAY.clicks) do c
if running[]
last_status=status[]
status[]="Paused"
running[]=false
else
set_status(s, last_status)
running[]=true
end
s.stop = ! running[]
end
on(fig.scene.events.window_area) do x
dx = x.widths[1] - WIDTH
dy = x.widths[2] - HEIGHT
txt2.position[] = Point2f(630.0+dx, 735.0+dy)
end
status[] = "Stopped"
s
end
function save_png(viewer; filename="video", index = 1)
buffer = IOBuffer()
@printf(buffer, "%06i", index)
save(filename * String(take!(buffer)) * ".png", viewer.scene)
end