Skip to content

Commit

Permalink
Merge b998bf8 into 45c61b7
Browse files Browse the repository at this point in the history
  • Loading branch information
mattuntergassmair committed Jan 16, 2020
2 parents 45c61b7 + b998bf8 commit 371aa33
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 65 deletions.
13 changes: 8 additions & 5 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using Documenter, AutoViz
using Documenter
using AutoViz

makedocs(modules = [AutoViz],
format = :html,
sitename="AutoViz.jl")
makedocs(
modules = [AutoViz],
sitename="AutoViz.jl",
format = Documenter.HTML()
)

deploydocs(
repo = "github.com/sisl/AutoViz.jl.git"
)
)
1 change: 0 additions & 1 deletion src/AutoViz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ include("fancy_render.jl")

# renderable interface
include("renderable.jl")
include("text.jl")

export
Renderable,
Expand Down
42 changes: 21 additions & 21 deletions src/overlays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ mutable struct TextParams
retval
end
end
function drawtext(text::AbstractString, y::Int, rendermodel::RenderModel, t::TextParams; incameraframe::Bool=false)
add_instruction!(rendermodel, render_text, (text, t.x, y, t.size, t.color), incameraframe=incameraframe)
function drawtext(text::AbstractString, y::Int, rendermodel::RenderModel, t::TextParams; coordinate_system::Symbol=:camera_pixels)
add_instruction!(rendermodel, render_text, (text, t.x, y, t.size, t.color), coordinate_system=coordinate_system)
y + t.y_jump
end

Expand All @@ -35,14 +35,14 @@ end
font_size::Int = 10 # [pix]
pos::VecE2 = VecE2(10, font_size)
line_spacing::Float64 = 1.5 # multiple of font_size
incameraframe=false
coordinate_system::Symbol=:camera_pixels
end
function add_renderable!(rendermodel::RenderModel, overlay::TextOverlay)
x = overlay.pos.x
y = overlay.pos.y
y_jump = overlay.line_spacing*overlay.font_size
for line in overlay.text
add_instruction!(rendermodel, render_text, (line, x, y, overlay.font_size, overlay.color), incameraframe=overlay.incameraframe)
add_instruction!(rendermodel, render_text, (line, x, y, overlay.font_size, overlay.color), coordinate_system=overlay.coordinate_system)
y += y_jump
end
rendermodel
Expand All @@ -54,7 +54,7 @@ function add_renderable!(rendermodel::RenderModel, overlay::TextOverlay, scene)
y = overlay.pos.y
y_jump = overlay.line_spacing*overlay.font_size
for line in overlay.text
add_instruction!(rendermodel, render_text, (line, x, y, overlay.font_size, overlay.color), incameraframe=overlay.incameraframe)
add_instruction!(rendermodel, render_text, (line, x, y, overlay.font_size, overlay.color), coordinate_system=overlay.coordinate_system)
y += y_jump
end
rendermodel
Expand All @@ -77,7 +77,7 @@ The fill proportion is set using `val`, it should be a number between 0 and 1. I
"""
@with_kw mutable struct HistogramOverlay <: SceneOverlay
pos::VecE2{Float64} = VecE2(0.,0.)
incameraframe::Bool = true
coordinate_system::Symbol = :camera_pixels
label::String = "histogram"
val::Float64 = 0.5 # should be between 0 and 1
width::Float64 = 2.
Expand All @@ -90,11 +90,11 @@ end

function AutoViz.add_renderable!(rendermodel::RenderModel, overlay::HistogramOverlay)
# render value
add_instruction!(rendermodel, render_rect, (overlay.pos.x, overlay.pos.y, overlay.width, overlay.val*overlay.height,overlay.fill_color, true, false), incameraframe=overlay.incameraframe)
add_instruction!(rendermodel, render_rect, (overlay.pos.x, overlay.pos.y, overlay.width, overlay.val*overlay.height,overlay.fill_color, true, false), coordinate_system=overlay.coordinate_system)
# render histogram outline
add_instruction!(rendermodel, render_rect, (overlay.pos.x, overlay.pos.y, overlay.width, overlay.height, overlay.line_color), incameraframe=overlay.incameraframe)
add_instruction!(rendermodel, render_rect, (overlay.pos.x, overlay.pos.y, overlay.width, overlay.height, overlay.line_color), coordinate_system=overlay.coordinate_system)
# label
add_instruction!(rendermodel, render_text, (overlay.label, overlay.label_pos.x, overlay.label_pos.y, overlay.font_size, overlay.line_color), incameraframe=overlay.incameraframe)
add_instruction!(rendermodel, render_text, (overlay.label, overlay.label_pos.x, overlay.label_pos.y, overlay.font_size, overlay.line_color), coordinate_system=overlay.coordinate_system)
end

"""
Expand All @@ -114,7 +114,7 @@ end
function AutoViz.add_renderable!(rendermodel::RenderModel, overlay::IDOverlay, scene::Frame{Entity{S,D,I}}, env::E) where {S,D,I,E}
font_size = overlay.font_size
for veh in scene
add_instruction!(rendermodel, render_text, ("$(veh.id)", veh.state.posG.x + overlay.x_off, veh.state.posG.y + overlay.y_off, font_size, overlay.color), incameraframe=true)
add_instruction!(rendermodel, render_text, ("$(veh.id)", veh.state.posG.x + overlay.x_off, veh.state.posG.y + overlay.y_off, font_size, overlay.color), coordinate_system=:scene)
end
return rendermodel
end
Expand Down Expand Up @@ -236,21 +236,21 @@ function add_renderable!(rendermodel::RenderModel, overlay::CarFollowingStatsOve
text_y = font_size
text_y_jump = round(Int, font_size*1.2)
fmt_txt = @sprintf("id %d", overlay.target_id)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), coordinate_system=:scene)
text_y += text_y_jump

veh_index = findfirst(overlay.target_id, scene)
if veh_index != nothing
veh = scene[veh_index]

if overlay.verbosity 2
add_instruction!( rendermodel, render_text, ("posG: " * string(veh.state.posG), 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, ("posG: " * string(veh.state.posG), 10, text_y, font_size, overlay.color), coordinate_system=:scene)
text_y += text_y_jump
add_instruction!( rendermodel, render_text, ("posF: " * string(veh.state.posF), 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, ("posF: " * string(veh.state.posF), 10, text_y, font_size, overlay.color), coordinate_system=:scene)
text_y += text_y_jump
end
fmt_txt = @sprintf("speed: %0.3f", veh.state.v)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), coordinate_system=:scene)
text_y += text_y_jump


Expand All @@ -259,28 +259,28 @@ function add_renderable!(rendermodel::RenderModel, overlay::CarFollowingStatsOve
v2 = scene[foreinfo.ind]
rel_speed = v2.state.v - veh.state.v
fmt_txt = @sprintf("Δv = %10.3f m/s", rel_speed)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), coordinate_system=:scene)
text_y += text_y_jump
fmt_txt = @sprintf("Δs = %10.3f m/s", foreinfo.Δs)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), coordinate_system=:scene)
text_y += text_y_jump

if overlay.verbosity 2
add_instruction!( rendermodel, render_text, ("posG: " * string(v2.state.posG), 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, ("posG: " * string(v2.state.posG), 10, text_y, font_size, overlay.color), coordinate_system=:scene)
text_y += text_y_jump
add_instruction!( rendermodel, render_text, ("posF: " * string(v2.state.posF), 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, ("posF: " * string(v2.state.posF), 10, text_y, font_size, overlay.color), coordinate_system=:scene)
text_y += text_y_jump
fmt_txt = @sprintf("speed: %.3f", v2.state.v)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), coordinate_system=:scene)
text_y += text_y_jump
end
else
fmt_txt = @sprintf("no front vehicle")
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), coordinate_system=:scene)
end
else
fmt_txt = @sprintf("vehicle %d not found", overlay.target_id)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), incameraframe=false)
add_instruction!( rendermodel, render_text, (fmt_txt, 10, text_y, font_size, overlay.color), coordinate_system=:scene)
end

rendermodel
Expand Down
15 changes: 15 additions & 0 deletions src/renderable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,18 @@ end

# render nothing by not doing anything
add_renderable!(rm::RenderModel, ::Nothing) = rm

"""
Render function for text
"""
function add_renderable!(rm::RenderModel, t::String)
font_size = 12
x = 10
y = 1.5*font_size
y_jump = 1.5 * font_size
for line in split(t, '\n')
add_instruction!(rm, render_text, (line, x, y, font_size, colorant"gray75"), coordinate_system=:camera_pixels)
y += y_jump
end
return rm
end
50 changes: 25 additions & 25 deletions src/rendermodels.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Model to keep track of rendering instructions and background color.
- `instruction_set::AbstractVector{Tuple}`: set of render instructions (function, array of inputs sans ctx, incameraframe)
- `instruction_set::AbstractVector{Tuple}`: set of render instructions (function, array of inputs sans ctx, coordinate_system)
- `background_color::RGB`: background color
"""
@with_kw mutable struct RenderModel
Expand All @@ -15,14 +15,17 @@ Add an instruction to the rendermodel
INPUT:
rendermodel - the RenderModel we are adding the instruction to
f - the function to be called, the first argument must be a CairoContext
arr - tuple of input arguments to f, skipping the CairoContext
incameraframe - we render in the camera frame by default.
To render in the canvas frame (common with text) set this to false
args - tuple of input arguments to f, skipping the CairoContext
coordinate_system - in which coordinate system are the coordinates given (one of :scene, :camera_pixels, :camera_relative)
`:scene` - coordinates are physical coordinates in the world frame in unit [meters]
`:camera_pixels` - coordinates are in pixels and relative to the rectangle selected by the camera in unit [pixels]
`:camera_relative` - coordinates are in percentages in the range 0 to 1 of the rectangle selected by the camera
ex: add_instruction!(rendermodel, render_text, ("hello world", 10, 20, 15, [1.0,1.0,1.0]))
"""
function add_instruction!(rm::RenderModel, f::Function, arr::Tuple; incameraframe::Bool=true)
push!(rm.instruction_set, (f, arr, incameraframe))
function add_instruction!(rm::RenderModel, f::Function, args::Tuple; coordinate_system::Symbol=:scene)
@assert coordinate_system in (:scene, :camera_pixels, :camera_relative) "Invalid coordinate_system"
push!(rm.instruction_set, (f, args, coordinate_system))
rm
end

Expand Down Expand Up @@ -55,45 +58,40 @@ end

function render_to_canvas(rendermodel::RenderModel, camera_state::CameraState, ctx::CairoContext)

# fill with background color
set_source_rgba(ctx, rendermodel.background_color)
paint(ctx)

w, h = canvas_width(camera_state), canvas_height(camera_state)

# render text if no other instructions
if isempty(rendermodel.instruction_set)
text_color = RGB(1.0 - convert(Float64, red(rendermodel.background_color)),
1.0 - convert(Float64, green(rendermodel.background_color)),
1.0 - convert(Float64, blue(rendermodel.background_color)))
render_text(
ctx, "This screen left intentionally blank",
w/2, h/2, 40, text_color, true
ctx, "No rendering instructions found",
w/2, h/2, 40, colorant"red", true
)
return
return rendermodel # terminate here
end

# fill with background color
set_source_rgba(ctx, rendermodel.background_color)
paint(ctx)

w, h = canvas_width(camera_state), canvas_height(camera_state)

# reset the transform
reset_transform(ctx)
translate(ctx, w/2, h/2) # translate to image center
Cairo.scale(ctx, zoom(camera_state), -zoom(camera_state)) # [pix -> m]
Cairo.scale(ctx, zoom(camera_state), -zoom(camera_state)) # [pix -> m], negative zoom flips up and down
rotate(ctx, rotation(camera_state))
x, y = position(camera_state)
translate(ctx, -x, -y) # translate to camera location

# execute all instructions
for tup in rendermodel.instruction_set
func = tup[1]
content = tup[2]
incameraframe = tup[3]
func, content, coordinate_system = tup

if !incameraframe
if coordinate_system in (:camera_pixels, :camera_relative)
save(ctx)
reset_transform(ctx)
if (coordinate_system == :camera_relative) Cairo.scale(ctx, w, h) end
func(ctx, content...)
restore(ctx)
else

elseif coordinate_system == :scene
if func == render_text
# deal with the inverted y-axis issue for text rendered
mat = get_matrix(ctx)
Expand All @@ -110,6 +108,8 @@ function render_to_canvas(rendermodel::RenderModel, camera_state::CameraState, c
# just use the function normally
func(ctx, content...)
end
else
throw(ErrorException("Invalid coordinate system $(coordinate_system). Must be one of :scene, :camera_pixels, :camera_relative"))
end
end

Expand Down
13 changes: 0 additions & 13 deletions src/text.jl

This file was deleted.

0 comments on commit 371aa33

Please sign in to comment.