Skip to content

4. Object overview

Full example code: examples/visualization/visualization_objects.py

The following objects can be added to a plot

  • Surface SurfaceBuilder
  • Nodes NodeBuilder
  • Edges EdgeBuilder
  • Static text TextBuilder
  • Scalar bar ScalarBar
  • Graph GraphBuilder (collection of nodes and edges)
  • Cycles CycleBuilder (similar to edges)

To see which properties you can set, each of the builders below has a __setters__ property as shown in the previous step.

First we define a simple cube as example data. We add a few edges.

# We define a cube as example data
vertices = np.array([
    [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0],
    [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 1.0],
    [1.0, 1.0, 1.0], [0.0, 1.0, 1.0]
])

# Sides of the cube
quads = np.array([
    [0, 3, 2, 1], [4, 5, 6, 7], [0, 1, 5, 4],
    [1, 2, 6, 5], [2, 3, 7, 6], [3, 0, 4, 7]
])

# Node scalars
scalars = np.array([1, 2, 3, 4, 8, 7, 6, 5])

# Example edges x1, y1, z1, x2, y2, z2
my_edges = np.array([
    [0, 1, 1, 1, 0, 1], [1, 0, 1, 1, 0, 0], [0, 1, 0, 1, 1, 0],
    [0, 1, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1], [1, 0, 0, 1, 1, 0]
])

Defining objects

Surface

from opendgm.visualization import SurfaceBuilder

cube_surface = (
    SurfaceBuilder(vertices, quads)
    .set_opacity(.9)
    .set_scale(.8)
)

Nodes

Using the above vertices we can add them as nodes to our plot using the NodeBuilder class.

from opendgm.visualization import NodeBuilder

nodes = (
    NodeBuilder(vertices)
    .set_scalars(scalars, cmap="hsv")
    .set_size(.1)
)

Edges

from opendgm.visualization import EdgeBuilder

sources = my_edges[:, :3] # Edge starting points
targets = my_edges[:, 3:] # Edge end points

edges = (
    EdgeBuilder(sources, targets)
    .set_scalars(edge_scalars)
    .set_resolution(4) # 4 sides to our arrows
)

Static text

from opendgm.visualization import TextBuilder

text = (
    TextBuilder("This is static text")
    .set_position(0.3, 0.5)
    .set_font_size(32)
    .set_color((255, 229, 104))
)

Scalar bar

from opendgm.visualization import ScalarBar

scalar_bar = (
    ScalarBar(scalars, cmap="hsv")
    .set_title("Color map")
    .set_position(0.1, 0.05)
    .set_number_of_ticks(4)
)

Add all objects to your plot

Now we have all our objects, we can initialize our plot and add them all.

plot = Plot(640, 480)
plot.renderer.add_object(cube_surface)
plot.renderer.add_object(edges)
plot.renderer.add_object(nodes)
plot.renderer.add_object(text)
plot.renderer.add_object(scalar_bar)
plot.start()

Visualization objects


← Previous: Set object properties