Class PointViz

Class Documentation

class PointViz

A basic visualizer for sensor data.

Displays a set of point clouds, images, cuboids, and text labels with a few options for coloring and handling input.

All operations are thread safe when running rendering (run() or run_once()) in a separate thread. This is the intended way to use the visualizer library when a nontrivial amount of processing needs to run concurrently with rendering (e.g. when streaming data from a running sensor).

Public Functions

void add_default_controls(std::mutex *mx)

Add default keyboard and mouse bindings to a visualizer instance.

Controls will modify the camera from the thread that calls run() or run_once(), which will require synchronization when using multiple threads.

Parameters:

mx[in] – mutex to lock while modifying camera

explicit PointViz(const std::string &name, bool fix_aspect = false, int window_width = DEFAULT_WINDOW_WIDTH, int window_height = DEFAULT_WINDOW_HEIGHT, bool maximized = false, bool fullscreen = false, bool borderless = false)

Creates a window and initializes the rendering context.

Parameters:
  • name[in] – name of the visualizer, shown in the title bar

  • fix_aspect[in] – Window aspect to set

  • window_width[in] – Window width to set, else uses the default_window_width

  • window_height[in] – Window height to set, else uses the default_window_height

  • maximized[in] – If true, the window will be maximized to fit the screen.

  • fullscreen[in] – If true, the window will be fullscreened.

  • borderless[in] – If true, the window will be borderless.

PointViz(const PointViz&) = delete
PointViz(PointViz&&) = delete
PointViz &operator=(PointViz&) = delete
PointViz &operator=(PointViz&&) = delete
~PointViz()

Tears down the rendering context and closes the viz window.

void run()

Main drawing loop, keeps drawing things until running(false)

Should be called from the main thread for macos compatibility

void run_once()

Run one iteration of the main loop for rendering and input handling.

Should be called from the main thread for macos compatibility

bool running()

Check if the run() has been signaled to exit.

Returns:

true if the run() loop is currently executing

void running(bool state)

Set the running flag.

Will signal run() to exit

Parameters:

state[in] – new value of the flag

void visible(bool state)

Show or hide the visualizer window.

Parameters:

state[in] – true to show

void update()

Update visualization state.

Send state updates to be rendered on the next frame.

void cursor_visible(bool state)

Show or hide the mouse cursor when over this window.

Parameters:

state[in] – true to show

void push_key_handler(std::function<bool(const WindowCtx&, int, int)> &&callback)

Add a callback for handling keyboard input.

Parameters:

callback[in] – the callback. The second argument is the ascii value of the key pressed. Third argument is a bitmask of the modifier keys The callback’s return value determines whether the remaining key callbacks should be called.

void push_mouse_button_handler(std::function<bool(const WindowCtx &ctx, MouseButton button, MouseButtonEvent event, EventModifierKeys mods)> &&callback)

Add a callback for handling mouse button input.

Parameters:

callback[in] – the callback. The callback’s arguments are ctx: the context containing information about the buttons pressed, the mouse position, and the viewport; button: the mouse button pressed; mods: representing which modifier keys are pressed during the mouse click. The callback’s return value determines whether the remaining mouse button callbacks should be called.

void push_scroll_handler(std::function<bool(const WindowCtx&, double x, double y)> &&callback)

Add a callback for handling mouse scrolling input.

Parameters:

callback[in] – the callback. The callback’s arguments are ctx: the context containing information about the buttons pressed, the mouse position, and the viewport; x: the amount of scrolling in the x direction; y: the amount of scrolling in the y direction. The callback’s return value determines whether the remaining mouse scroll callbacks should be called.

void push_mouse_pos_handler(std::function<bool(const WindowCtx&, double x, double y)> &&callback)

Add a callback for handling mouse movement.

Parameters:

callback[in] – the callback. The callback’s arguments are ctx: the context containing information about the buttons pressed, the mouse position, and the viewport; x: the mouse position in the x direction; y: the mouse position in the y direction. The callback’s return value determines whether the remaining mouse position callbacks should be called.

void pop_key_handler()

Remove the last added callback for handling keyboard events.

void pop_mouse_button_handler()

Remove the last added callback for handling mouse button events.

void pop_scroll_handler()

Remove the last added callback for handling mouse scroll events.

void pop_mouse_pos_handler()

Remove the last added callback for handling mouse position events.

void push_frame_buffer_resize_handler(std::function<bool(const WindowCtx&)> &&callback)

Add a callback for handling frame buffer resize events.

Parameters:

callback[in] – function callback of the form f(const WindowCtx&). The callback’s return value determines whether the remaining frame buffer resize callbacks should be called.

void pop_frame_buffer_resize_handler()

Remove the last added callback for handling frame buffer resize events.

Camera &camera()

Get a reference to the camera controls.

Returns:

Handler to the camera object

TargetDisplay &target_display()

Get a reference to the target display controls.

Returns:

Handler to the target display controls

void add(const std::shared_ptr<Cloud> &cloud)

Add an object to the scene.

Parameters:

cloud[in] – Adds a point cloud to the scene

void add(const std::shared_ptr<Image> &image)

Add an object to the scene.

Parameters:

image[in] – Adds an image to the scene

void add(const std::shared_ptr<Mesh> &mesh)

Add an object to the scene.

Parameters:

mesh[in] – Adds a mesh to the scene

void add(const std::shared_ptr<Cuboid> &cuboid)

Add an object to the scene.

Parameters:

cuboid[in] – Adds a cuboid to the scene

void add(const std::shared_ptr<Lines> &lines)

Add an object to the scene.

Parameters:

lines[in] – Adds a lines to the scene

void add(const std::shared_ptr<Label> &label)

Add an object to the scene.

Parameters:

label[in] – Adds a label to the scene

bool remove(const std::shared_ptr<Cloud> &cloud)

Remove an object from the scene.

Parameters:

cloud[in] – Remove a point cloud from the scene

Returns:

true if successfully removed else false

bool remove(const std::shared_ptr<Image> &image)

Remove an object from the scene.

Parameters:

image[in] – Remove an image from the scene

Returns:

true if successfully removed else false

bool remove(const std::shared_ptr<Mesh> &mesh)

Remove an object from the scene.

Parameters:

mesh[in] – Remove a mesh from the scene

Returns:

true if successfully removed else false

bool remove(const std::shared_ptr<Cuboid> &cuboid)

Remove an object from the scene.

Parameters:

cuboid[in] – Remove a cuboid from the scene

Returns:

true if successfully removed else false

bool remove(const std::shared_ptr<Lines> &lines)

Remove an object from the scene.

Parameters:

lines[in] – Remove a lines from the scene

Returns:

true if successfully removed else false

bool remove(const std::shared_ptr<Label> &label)

Remove an object from the scene.

Parameters:

label[in] – Remove a label from the scene

Returns:

true if successfully removed else false

int viewport_width() const

Get a viewport width in pixels.

Returns:

viewport width reported by glfw

int viewport_height() const

Get a viewport height in pixels.

Returns:

viewport height reported by glfw

int window_width() const

Get a window width in screen coordinates.

NOTE: this value maybe different from the viewport size on retina displays

Returns:

window width reported by glfw

int window_height() const

Get a window height in screen coordinates.

Note

this value maybe different from the viewport size on retina displays

Returns:

window height reported by glfw

double fps() const

Get frames per second (FPS) value, updated every second.

Updated every second in the draw() function

Returns:

fps value,

void set_background_color(const vec4f &rgba)

Set the background color of the viz.

Parameters:

rgba[in] – color

std::vector<uint8_t> get_screenshot(uint32_t width, uint32_t height)

Gets a screenshot with the specified size.

Parameters:
  • width[in] – Screenshot width in pixels.

  • height[in] – Screenshot height in pixels.

Returns:

A vector of bytes containing the screenshot’s pixel data in RGB format. Each pixel is represented by three consecutive bytes (RGB), stored in row-major order.

std::vector<uint8_t> get_screenshot(double scale_factor = 1.0)

Gets a screenshot with the specified size.

Parameters:

scale_factor[in] – A multiplier to the current window width and height for determining screenshot size.

Returns:

A vector of bytes containing the screenshot’s pixel data in RGB format. Each pixel is represented by three consecutive bytes (RGB), stored in row-major order.

std::string save_screenshot(const std::string &path, uint32_t width, uint32_t height)

Saves a screenshot with the specified size to the specified path.

Parameters:
  • path[in] – The path where the screenshot should be saved, the filename is auto generated. An empty string means current path.

  • width[in] – Screenshot width in pixels.

  • height[in] – Screenshot height in pixels.

Returns:

The resulting path and filename.

std::string save_screenshot(const std::string &path, double scale_factor = 1.0)

Saves a screenshot to the specified path.

Parameters:
  • path[in] – The path where the screenshot should be saved, the filename is auto generated. An empty string means current path.

  • scale_factor[in] – A multiplier to the window viewport width and height for determining screenshot size.

Returns:

the resulting path and filename, empty string if error.

bool toggle_screen_recording(uint32_t width, uint32_t height)

Starts or stops saving screenshots continuously.

Parameters:
  • width[in] – screenshot width in pixels.

  • height[in] – screenshot height in pixels.

Returns:

true if recording started, false otherwise,

bool toggle_screen_recording(double scale_factor = 1.0)

Starts or stops saving screenshots continuously.

Parameters:

scale_factor[in] – a multiplier to the window viewport width and height for determining screenshot size, at the time of recording start.

Returns:

true if recording started, false otherwise,

std::pair<uint32_t, uint32_t> get_scaled_viewport_size(double scale_factor)

Computes the width and height that result from muliplying the current window viewport width and height for a scale_factor.

Parameters:

scale_factor[in] – A multiplier to the current window viewport width and height

Returns:

A pair that has width fist and height second

void set_notification(const std::string &msg, double duration = 2.0)

Sets a notification message to be displayed in the top-right corner of the window.

Parameters:
  • msg[in] – The message to be displayed.

  • duration[in] – The duration in seconds for which the message will be displayed (default: 2.0 seconds).

bool notification_active() const

Returns true if there is an active notification being displayed.

Returns:

true if there is an active notification being displayed, false otherwise.

Public Members

bool notifications_enabled = {false}

Whether to show notifications.