-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from sisl/version2
Version2
- Loading branch information
Showing
12 changed files
with
427 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,77 @@ | ||
# AutoViz | ||
|
||
This package provides visualization tools for [AutomotiveDrivingModels](https://github.com/sisl/AutomotiveDrivingModels.jl). | ||
Built off of PGFPlots and Cairo. | ||
|
||
You can find the [documentation here](http://nbviewer.ipython.org/github/sisl/AutoViz.jl/blob/master/doc/AutoViz.ipynb). | ||
|
||
[![Build Status](https://travis-ci.org/sisl/AutoViz.jl.svg?branch=master)](https://travis-ci.org/sisl/AutoViz.jl) | ||
[![Coverage Status](https://coveralls.io/repos/sisl/AutoViz.jl/badge.svg)](https://coveralls.io/r/sisl/AutoViz.jl) | ||
|
||
A package for rendering simple scenes primarily consisting of cars on roadways using Cairo. | ||
|
||
AutoViz is undergoing significant changes. If you are looking for the version before these changes that is designed around AutomotiveDrivingModels.jl, please checkout the v0.6.0 tag. | ||
|
||
![AutoViz](readmeimage.png) | ||
|
||
## Usage | ||
|
||
The main function is | ||
|
||
```julia | ||
render(scene) | ||
``` | ||
|
||
where scene is an iterable of renderable objects including cars and roadways. | ||
|
||
Example: | ||
```julia | ||
roadway = gen_straight_roadway(3, 100.0) | ||
car = ArrowCar([0.0, 0.0], 0.0, color=colorant"blue") # [north, east], angle | ||
render([roadway, car, "some text"]) | ||
``` | ||
|
||
### Renderable | ||
|
||
*What does it mean to be "renderable"?* | ||
|
||
An object is *directly renderable* if the function `render!(rm::RenderModel, object)` is implemented for it. | ||
|
||
An object is *renderable by conversion* if `convert(Renderable, object)` returns a directly renderable object. | ||
|
||
When `render()` is invoked, direct renderability is checked with `isrenderable(object)`, which defaults to `method_exists(render!, Tuple{RenderModel, typeof(object)})`. If this check fails, a conversion attempt is made with `convert(Renderable, object)`. | ||
|
||
### Roadways and ArrowCars | ||
|
||
The primary basic directly renderable types are `Roadway` (now from `AutomotiveDrivingModels`; soon from `Roadways.jl`) and `ArrowCar`. | ||
|
||
`ArrowCar`s are the pink cars with arrows that are in everyone's videos. You can construct one like this: | ||
|
||
```julia | ||
using Colors | ||
using AutoViz | ||
|
||
# x, y, angle and velocity are from your simulation | ||
|
||
ArrowCar(x, y, angle; color=colorant"green", text="v: $velocity") | ||
``` | ||
|
||
### How to make types renderable | ||
|
||
There are two ways to make renderable types. | ||
|
||
1. You can make your existing types renderable by conversion by defining `convert(::Type{Renderable}, ::MyType)` which should return a directly renderable object, e.g. an `ArrowCar`. | ||
2. You can make types directly renderable by defining `render!(::RenderModel, ::MyType)`. To make things easier for the compiler, you can also define `isrenderable(::Type{MyType}) = true`. If you want to allow others to convert to this type to make their types renderable by conversion, make your type a subtype of `Renderable`. | ||
|
||
## Overlays | ||
|
||
Overlays will function as in the previous version of AutoViz. They will be rendered last with `render!(rendermodel, overlay, scene)`. | ||
|
||
## Additional keyword arguments for `render()` | ||
|
||
The following additional keyword arguments will accepted by `render()`: | ||
|
||
- `canvas_width` | ||
- `canvas_height` | ||
- `rendermodel` | ||
- `overlays` | ||
- `cam` - a camera controlling the field of view as in the previous version of AutoViz | ||
|
||
## `RenderModel`s | ||
|
||
The mid-level interface for this package (which is what you will use when you write `render!()` for your types or when you write an overlay) revolves around adding instructions to a `RenderModel`. Each instruction consists of a function and a tuple of arguments for the function. This is not documented in this readme, but it is fairly easy to figure out by reading `rendermodels.jl`, `overlays.jl`, and `arrowcar.jl`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ Cairo | |
Reel | ||
Interact | ||
NBInclude | ||
Parameters | ||
Parameters | ||
StaticArrays |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#TODO: Add doc string | ||
@with_kw struct ArrowCar{A<:AbstractArray{Float64}, C<:Colorant} <: Renderable | ||
pos::A = SVector(0.0, 0.0) | ||
angle::Float64 = 0.0 | ||
length::Float64 = 4.8 | ||
width::Float64 = 1.8 | ||
color::C = COLOR_CAR_OTHER | ||
text::String = "" # some debugging text to print by the car | ||
id::Int = 0 | ||
end | ||
|
||
ArrowCar(pos::AbstractArray, angle::Float64=0.0; length = 4.8, width = 1.8, color=COLOR_CAR_OTHER, text="", id=0) = ArrowCar(pos, angle, length, width, color, text, id) | ||
ArrowCar(x::Real, y::Real, angle::Float64=0.0; length = 4.8, width = 1.8, color=COLOR_CAR_OTHER, text="", id=0) = ArrowCar(SVector(x, y), angle, length, width, color, text, id) | ||
|
||
function render!(rm::RenderModel, c::ArrowCar) | ||
x = c.pos[1] | ||
y = c.pos[2] | ||
add_instruction!(rm, render_vehicle, (x, y, c.angle, c.length, c.width, c.color)) | ||
add_instruction!(rm, render_text, (c.text, x, y-c.width/2 - 2.0, 10, colorant"white")) | ||
return rm | ||
end | ||
|
||
id(ac::ArrowCar) = ac.id | ||
pos(ac::ArrowCar) = ac.pos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#TODO: Renderable type | ||
abstract type Renderable end | ||
|
||
""" | ||
Return true if an object or type is *directly renderable*, false otherwise. | ||
New types should implement the `isrenderable(t::Type{NewType})` method. | ||
""" | ||
function isrenderable end | ||
|
||
isrenderable(object) = isrenderable(typeof(object)) | ||
isrenderable(::Type{R}) where R <: Renderable = true | ||
isrenderable(t::Type) = method_exists(render!, Tuple{RenderModel, t}) | ||
isrenderable(t::Type{Roadway}) = true | ||
|
||
|
||
function render(scene; # iterable of renderable objects | ||
overlays=[], | ||
rendermodel::RenderModel=RenderModel(), | ||
cam::Camera=FitToContentCamera(), | ||
canvas_height::Int=DEFAULT_CANVAS_HEIGHT, | ||
canvas_width::Int=DEFAULT_CANVAS_WIDTH | ||
) | ||
|
||
s = CairoRGBSurface(canvas_width, canvas_height) | ||
ctx = creategc(s) | ||
clear_setup!(rendermodel) | ||
|
||
for x in scene | ||
if isrenderable(x) | ||
render!(rendermodel, x) | ||
else | ||
render!(rendermodel, convert(Renderable, x)) | ||
end | ||
end | ||
|
||
for o in overlays | ||
render!(rendermodel, o, scene) | ||
end | ||
|
||
camera_set!(rendermodel, cam, scene, canvas_width, canvas_height) | ||
|
||
render(rendermodel, ctx, canvas_width, canvas_height) | ||
return s | ||
end | ||
|
||
function Base.convert(::Type{Renderable}, x::AbstractVector{R}) where R <: Real | ||
@assert length(x) >= 2 | ||
if length(x) == 2 | ||
ac = ArrowCar(SVector(x[1],x[2])) | ||
else | ||
ac = ArrowCar(SVector(x[1],x[2]), x[3]) | ||
end | ||
ac | ||
end | ||
#TODO: Tutorial notebook -> roadways, arrow cars, how to convert: Ekhlas | ||
|
||
#TODO: test -> run tutorial notebook, default conversions, isrenderable(), etc.: Ekhlas | ||
#TODO: Hints for figuring out rendermodels: Zach |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
isrenderable(::Type{String}) = true | ||
|
||
function render!(rm::RenderModel, t::String) | ||
font_size = 10 | ||
x = 10 | ||
y = 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"white"), incameraframe=true) | ||
y += y_jump | ||
end | ||
return rm | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters