Data visualisation
false
false
true
macros_menu.examples>end("Examples").end
ruby
# @title Creating layouts: data visualisation
#
# This application creates a 2-dimensional function plot by employing differently colored layers
# to display the pixel of the data map. 256 Layers are created representing values from 0 to 1.0 of the
# function "sin(r)/r". To make the picture more attractive, five functions starting from different
# origins are superimposed. The function is evaluated on the 500x500 grid, each grid point is assigned a
# value, the value is mapped to a layer and a box is created to represent the pixel.
module Examples
app = RBA::Application.instance
mw = app.main_window
# creates a new layout
mw.create_layout(0)
view = mw.current_view
cv = view.cellview(0)
# creates a new layer in that layout
layout = cv.layout
layer_ids = []
# computes a table for the anti-gamma correction
gamma = 2.2
gamma_map = []
256.times do |l|
gamma_map << (((l / 255.0) ** (1.0 / gamma)) * 255.0 + 0.5).to_i
end
# initializes the layer table with a color map:
# 0 -> blue
# 128 (== 0.5) -> green
# 255 (== 1.0) -> red
256.times do |l|
linfo = RBA::LayerInfo.new
layer_id = layout.insert_layer(linfo)
# computes the color per value
if l >= 128
c = gamma_map[(l - 128) * 2] * 0x10000 + gamma_map[2 * (255 - l)] * 0x100
elsif l > 0
c = gamma_map[(128 - l) * 2] + gamma_map[2 * l] * 0x100
else
c = 0xff
end
# creates a layer view for that layer
ln = RBA::LayerPropertiesNode::new
ln.dither_pattern = 0
ln.fill_color = c
ln.frame_color = c
ln.width = 1
ln.source_layer_index = layer_id
view.insert_layer(view.end_layers, ln)
layer_ids.push(layer_id)
end
# replicates last layer to allow values of 256 (mapped to 255) ..
layer_ids.push(layer_ids[255])
# creates a top cell
topcell = layout.create_cell("top")
# creates the image
nx = 500
ny = 500
radius = 100
pts = [
[ -nx * 0.25, ny * 0.25 ],
[ nx * 0.25, ny * 0.25 ],
[ -nx * 0.25, -ny * 0.25 ],
[ nx * 0.25, -ny * 0.25 ],
[ -nx * 0.15, -ny * 0.35 ],
[ 0, -ny * 0.37 ],
[ nx * 0.15, -ny * 0.35 ]
]
x = -nx / 2
nx.times do
y = -ny / 2
ny.times do
v = 0.0
pts.each do |pt|
r = Math::sqrt((x - pt[0]) ** 2 + (y - pt[1]) ** 2) * Math::PI * 2.0 / radius
if r.abs < 1e-6
v += 1.0
else
v += Math::sin(r) / r
end
end
vi = ([ 1.0, v ].min ** 2 * 255.0 + 0.5).to_i
box = RBA::Box::new(x * 100, y * 100, (x + 1) * 100, (y + 1) * 100)
topcell.shapes(layer_ids[vi]).insert_box(box)
y += 1
end
x += 1
end
# selects his cell as the top cell, fits all and switches on all hierarchy levels
cv.cell = topcell
view.zoom_fit
view.max_hier
end