1
# high level user API
2

3
# two major visualization functions are used, namely:
4
# - Memory direction with visualize(::FactorGraph, ...)
5
# - Cloud access with visualize(robot::String, session::String, ...)
6

7

8
"""
9
    $(SIGNATURES)
10

11
Set the global transform for modifying the final draw step, for example Z up or Z down convention.
12

13
Example:
14
```julia
15
using Arena
16
# default is Z up
17

18
# make Z down
19
setGlobalDrawTransform!(quat=Quat(0.0,1.0,0.0,0.0))
20
```
21
"""
22
function setGlobalDrawTransform!(;trans=Translation(0.0,0.0,0.0), quat::Rotations.Quat=Quat(1.0,0.0,0.0,0.0))
23 0
  global drawtransform = trans  LinearMap(quat)
24
end
25

26
"""
27
    $(SIGNATURES)
28

29
Toggle global boolean flag to terminate the visualization loop.
30
"""
31
function stopVis!()
32 0
  global loopvis
33 0
  loopvis = false
34 0
  nothing
35
end
36

37

38
function extractRobotSession!(rose_fgl::Union{FactorGraph, Tuple{<:AbstractString, <:AbstractString}},
39
                              params::Dict{Symbol, Any}  )
40

41 0
    config = nothing
42

43
    # robot and session defaults
44 0
    params[:robotId] = :Bot
45 0
    params[:sessionId] = :Session
46 0
    if isa(rose_fgl, FactorGraph)
47 0
        if rose_fgl.robotname != "" && rose_fgl.robotname != "NA"
48 0
            params[:robotId] = Symbol(rose_fgl.robotname)
49
        end
50 0
        if rose_fgl.sessionname != "" && rose_fgl.sessionname != "NA"
51 0
            params[:sessionId] = Symbol(rose_fgl.sessionname)
52
        end
53
    else
54
        # setup Graff config (iff a Graff visualization)
55 0
        getGraffConfig() == nothing && error("GraffSDK is not configured, please call loadGraffConfig() to set up Graff.")
56 0
        config = getGraffConfig()
57 0
        params[:robotId] = Symbol(rose_fgl[1])
58 0
        params[:sessionId] = Symbol(rose_fgl[2])
59 0
        config.robotId = string(params[:robotId])
60 0
        config.sessionId = string(params[:sessionId])
61
    end
62 0
    return config
63
end
64

65
"""
66
    $(SIGNATURES)
67

68
High level interface to launch webserver process that draws the factor graph contents using Three.js and MeshCat.jl.
69

70
Examples:
71
---------
72

73
```julia
74
# start webserver visible anywhere on the network on port 8000.
75
using GraffSDK, Arena
76
@async visualize( (robot, session), meanmax=:max )
77
```
78

79
Or view local memory `FactorGraph` version:
80
```julia
81
# also open a browser to connect to the three.js webserver
82
using Arena
83
@async visualize(fg, show=true, plugins=[myplugin1;])
84
```
85

86
See src/plugins/Template.jl for defining your own plugin to be rendered in the visualization loop.
87
```julia
88
function pluginCallback(vis::MeshCat.Visualizer, params::Dict{String, Any}, rose_fgl) ... end
89
```
90

91
**Note** plugins do not have to be defined in Arena, Main context or users repo is sufficient.
92
"""
93
function visualize(rose_fgl::Union{FactorGraph, Tuple{<:AbstractString, <:AbstractString}};
94
                   show::Bool=true,
95
                   meanmax=:max,
96
                   plugins::Vector=Function[]  )::Nothing
97
    #
98 0
    global loopvis
99 0
    global drawtransform
100

101 0
    loopvis = true
102

103
    # standard parameters dictionary
104 0
    params = Dict{Symbol, Any}()
105

106
    # set up basics in params and load Graff config if available
107 0
    extractRobotSession!(rose_fgl, params)
108

109
    # the visualizer object itself
110 0
    vis = startDefaultVisualization(show=show)
111

112
    # default variable caching format:  (softtype, already-drawn, mapEst)
113 0
    params[:cachevars] = Dict{Symbol, Tuple{Symbol, Vector{Bool}, Vector{Float64}}}()
114

115
    # Prepend default plugins to be executed
116 0
    plugins = union([cacheVariablePointEst!; visualizeVariableCache!], plugins)
117

118
    # run the visualization loop
119 0
    while loopvis
120
        # iterate through all listed plugin callbacks, such sa pointclouds / images / reprojections / etc.
121 0
        for callback in plugins
122 0
            try
123 0
                callback(vis, params, rose_fgl)
124
            catch ex
125 0
                io = IOBuffer()
126 0
                showerror(io, ex, catch_backtrace())
127 0
                err = String(take!(io))
128 0
                @error "Visualization plugin failure -- callback=$(string(callback)) errored: $ex"
129 0
                @error "Error! $err"
130
            end
131
        end
132

133
        # take a break and repeat
134 0
        sleep(1)
135
    end
136

137 0
    @info "visualize is finalizing."
138 0
    nothing
139
end
140

141

142
### Some old code -- will be deleted soon enough
143

144
# xx, ll = ls(fgl)
145
# for x in xx
146
#   X = getKDE(fgl, x)
147
#   xmx = meanmax == :max ? getKDEMax(X) : getKDEMean(X)
148
#
149
#   if !haskey(cachevars, x)
150
#     triad = Triad(1.0)
151
#     cachevars[x] = deepcopy(xmx)
152
#   end
153
#
154
#     setobject!(vis[:poses][x], triad)
155
#
156
#   cachevars[x][:] .= xmx
157
#   trans = Translation(xmx[1:2]..., 0.0) ∘ LinearMap(RotZ(xmx[3]))
158
#   settransform!(vis[:poses][x], drawtransform ∘ trans)
159
# end

Read our documentation on viewing source code .

Loading