Easy3D 2.5.3
Viewer Class Reference

The built-in Easy3D viewer. More...

#include <easy3d/viewer/viewer.h>

Inheritance diagram for Viewer:
MultiViewer

Public Member Functions

 Viewer (const std::string &title="Easy3D Viewer", int samples=4, int gl_major=3, int gl_minor=2, bool full_screen=false, bool resizable=true, int depth_bits=24, int stencil_bits=8, int width=800, int height=600)
 Constructor. More...
 
virtual ~Viewer ()
 The destructor.
 
int run (bool see_all=true)
 Run the viewer. More...
 
virtual void exit ()
 Terminate the viewer.
 
Viewer properties
void set_title (const std::string &title)
 Set the window title of the viewer. More...
 
const std::string & title () const
 Query the window title of the viewer. More...
 
void resize (int w, int h)
 Set/Change the viewer size. More...
 
int width () const
 Returns the width of the viewer/window.
 
int height () const
 Returns the height of the viewer/window.
 
void viewer_size (int &w, int &h) const
 Queries the size of the viewer/window.
 
int framebuffer_width () const
 Returns the width of the framebuffer, which is identical to: width() * dpi_scaling().
 
int framebuffer_height () const
 Returns the height of the framebuffer, which is identical to: height() * dpi_scaling().
 
void framebuffer_size (int &w, int &h) const
 Queries the size of the framebuffer.
 
void set_position (int xpos, int ypos)
 Sets the position of the content area of the viewer. More...
 
int samples () const
 Query the actual samples of the viewer. More...
 
float dpi_scaling () const
 Query the scaling factor for high DPI devices (e.g., MackBook pro). More...
 
void set_background_color (const easy3d::vec4 &c)
 Set the background color of the viewer. More...
 
const easy3d::vec4background_color () const
 Query the background color of the viewer. More...
 
Cameracamera ()
 Returns the camera used by the viewer. See Camera.
 
const Cameracamera () const
 Returns the camera used by the viewer. See Camera.
 
File IO
virtual bool open ()
 Open a model (PointCloud/SurfaceMesh/Graph) from a file into the viewer. On success, the viewer will be in charge of the memory management of the model. More...
 
virtual bool save () const
 Save the active model (if exists) to a file. More...
 
Drawable management
bool add_drawable (Drawable *drawable)
 Add a drawable to the viewer to be visualized. After a drawable being added to the viewer, the viewer will be in charge of its memory management. More...
 
bool delete_drawable (Drawable *drawable)
 
const std::vector< Drawable * > & drawables () const
 Query the drawables managed by this viewer. More...
 
void clear_scene ()
 Delete all visual contents of the viewer (all models and drawables).
 
UI-related functions
void update () const
 Update the display (i.e., repaint). More...
 
void fit_screen (const easy3d::Model *model=nullptr)
 Moves the camera so that the entire scene or the active model is centered on the screen at a proper scale. More...
 
bool snapshot () const
 Take a snapshot of the screen and save it to a file. More...
 
bool snapshot (const std::string &file_name, float scaling=1.0f, int samples=0, int back_ground=1, bool expand=true) const
 Take a snapshot of the screen and save it to an image file. Supported image format: png, jpg, bmp, and tga. More...
 
virtual vec3 point_under_pixel (int x, int y, bool &found) const
 Query the XYZ coordinates of the surface point under the cursor. More...
 
const std::string & usage () const
 The usage information of the viewer. For the time being, it is the manual of this default viewer.
 
void set_usage (const std::string &usg)
 

Algorithm execution

enum  Key {
  KEY_UNKNOWN = -1 , KEY_0 = 48 , KEY_1 = 49 , KEY_2 = 50 ,
  KEY_3 = 51 , KEY_4 = 52 , KEY_5 = 53 , KEY_6 = 54 ,
  KEY_7 = 55 , KEY_8 = 56 , KEY_9 = 57 , KEY_A = 65 ,
  KEY_B = 66 , KEY_C = 67 , KEY_D = 68 , KEY_E = 69 ,
  KEY_F = 70 , KEY_G = 71 , KEY_H = 72 , KEY_I = 73 ,
  KEY_J = 74 , KEY_K = 75 , KEY_L = 76 , KEY_M = 77 ,
  KEY_N = 78 , KEY_O = 79 , KEY_P = 80 , KEY_Q = 81 ,
  KEY_R = 82 , KEY_S = 83 , KEY_T = 84 , KEY_U = 85 ,
  KEY_V = 86 , KEY_W = 87 , KEY_X = 88 , KEY_Y = 89 ,
  KEY_Z = 90 , KEY_RIGHT = 262 , KEY_LEFT = 263 , KEY_DOWN = 264 ,
  KEY_UP = 265 , KEY_F1 = 290 , KEY_F2 = 291 , KEY_F3 = 292 ,
  KEY_F4 = 293 , KEY_F5 = 294 , KEY_F6 = 295 , KEY_F7 = 296 ,
  KEY_F8 = 297 , KEY_F9 = 298 , KEY_SPACE = 32 , KEY_COMMA = 44 ,
  KEY_MINUS = 45 , KEY_PERIOD = 46 , KEY_SLASH = 47 , KEY_SEMICOLON = 59 ,
  KEY_EQUAL = 61 , KEY_LEFT_BRACKET = 91 , KEY_BACKSLASH = 92 , KEY_RIGHT_BRACKET = 93
}
 The keys. Currently only a limited number of commonly used keys are supported.
 
enum  Modifier { MODIF_NONE = 0x0000 , MODIF_SHIFT = 0x0001 , MODIF_CTRL = 0x0002 , MODIF_ALT = 0x0004 }
 The key modifiers. Currently only Shift, Ctrl, and Alt are supported.
 
enum  Button { BUTTON_LEFT = 0 , BUTTON_RIGHT = 1 , BUTTON_MIDDLE = 2 }
 Mouse buttons.
 
using Function = std::function< bool(Viewer *viewer, Model *model)>
 
void bind (const Function &func, Model *model, Key key, Modifier modifier=MODIF_NONE)
 Bind a function that will be triggered by the shortcut 'modifier + key'. More...
 

Animation

std::function< bool(Viewer *viewer)> animation_func_
 Function called at an equal interval for animation.
 
void set_animation (bool b)
 Enable/Disable animation. More...
 
bool is_animating () const
 Is animation currently being performed.
 

Model management

bool delete_model (Model *model)
 Delete a model. The memory of the model will be released and its existing drawables also be deleted. More...
 
const std::vector< Model * > & models () const
 Query the models managed by this viewer. More...
 
Modelcurrent_model () const
 Query the active model. More...
 
virtual Modeladd_model (const std::string &file_name, bool create_default_drawables=true)
 Add a model from a file to the viewer to be visualized. On success, the viewer will be in charge of the memory management of the model. The loaded model can be accessed by the 'current_model()' method. More...
 
Modeladd_model (Model *model, bool create_default_drawables=true)
 Add an existing model to the viewer to be visualized. If the model has been successfully added to the viewer, the viewer will be in charge of its memory management. More...
 

Detailed Description

Member Typedef Documentation

◆ Function

using Function = std::function<bool(Viewer* viewer, Model* model)>

A function type, which applies the user's algorithm(s)/operation(s) on the given model.

Parameters
viewerA pointer to this viewer.
modelThe model to be operated on. Example of defining such a function:
bool reconstruct(Viewer* viewer, Model* model) {
auto cloud = dynamic_cast<PointCloud *>(model);
auto mesh = surface_reconstruction(cloud);
if (!mesh)
return false;
viewer->add_model(mesh);
viewer->update();
return true;
}
Viewer(const std::string &title="Easy3D Viewer", int samples=4, int gl_major=3, int gl_minor=2, bool full_screen=false, bool resizable=true, int depth_bits=24, int stencil_bits=8, int width=800, int height=600)
Constructor.
Definition: viewer.cpp:74

Constructor & Destructor Documentation

◆ Viewer()

Viewer ( const std::string &  title = "Easy3D Viewer",
int  samples = 4,
int  gl_major = 3,
int  gl_minor = 2,
bool  full_screen = false,
bool  resizable = true,
int  depth_bits = 24,
int  stencil_bits = 8,
int  width = 800,
int  height = 600 
)
explicit

Constructor.

Parameters
titleThe window title of the viewer, which can be changed by calling set_title() after construction.
samplesThe number of samples for multisample antialiasing
gl_major/gl_minorThe OpenGL version to request. The created context will be compatible with the requested version (if the context creation succeeded).
full_screenDo you want the viewer to be fullscreen?
resizableWhether the viewer will be resizable by the user.
depth_bitsThe desired bit depths of the depth component of the default framebuffer.
stencil_bitsThe desired bit depths of the stencil component of the default framebuffer.
widthThe width of the viewer, which can be changed by calling resize() after construction.
heightThe height of the viewer, which can be changed by calling resize() after construction.
Examples
Tutorial_203_Viewer_wxWidgets, and Tutorial_204_Viewer_Qt.

Member Function Documentation

◆ add_drawable()

bool add_drawable ( Drawable drawable)

Add a drawable to the viewer to be visualized. After a drawable being added to the viewer, the viewer will be in charge of its memory management.

The use of drawables for visualization is quite flexible. Drawables are typically created for rendering 3D models (e.g., point clouds, meshes, graphs) and a 3D model is usually loaded from a file or generated by an algorithm. This method allows the user to visualize drawables without defining a 3D model.

Parameters
drawableThe pointer to the drawable.
Returns
True if the drawable has been added.
Examples
Tutorial_203_Viewer_wxWidgets, Tutorial_301_Drawables, Tutorial_310_TextMesher, and Tutorial_311_Animation.

◆ background_color()

const easy3d::vec4 & background_color ( ) const
inline

Query the background color of the viewer.

Returns
The background color of the viewer
Examples
Tutorial_203_Viewer_wxWidgets.

◆ bind()

void bind ( const Function func,
Model model,
Key  key,
Modifier  modifier = MODIF_NONE 
)
inline

Bind a function that will be triggered by the shortcut 'modifier + key'.

This operation will overwrite the previous function (if exist) bound to the same key-modifier shortcut.

Parameters
funcThe function to be executed, which will be triggered by the shortcut.
modelThe model to be processed.
keyThe shortcut key.
modifierThe shortcut key modifier (e.g., Ctrl, Shift, Alt). This can be MODIF_NONE.
See also
Function
Examples
Tutorial_701_Cloud_NormalEstimation.

◆ current_model()

Model * current_model ( ) const

Query the active model.

The viewer can manage/visualize/process multiple models. The default behavior of the Easy3D viewer is, when a command is triggerred (e.g., the Save menu was clicked), only the active model is processed. This method is used to identify the active model.

Returns
The active model.
Examples
Tutorial_203_Viewer_wxWidgets.

◆ delete_drawable()

bool delete_drawable ( Drawable drawable)

Delete the drawable from the viewer. The related drawables will also be deleted.

Parameters
drawableThe pointer to the drawable.
Returns
True if the drawable has been deleted.
Examples
Tutorial_203_Viewer_wxWidgets.

◆ delete_model()

bool delete_model ( Model model)

Delete a model. The memory of the model will be released and its existing drawables also be deleted.

Parameters
modelThe pointer to the model.
Returns
True if the model has been deleted.
Examples
Tutorial_203_Viewer_wxWidgets, and Tutorial_702_Cloud_SurfaceReconstruction.

◆ dpi_scaling()

float dpi_scaling ( ) const
inline

Query the scaling factor for high DPI devices (e.g., MackBook pro).

Returns
The high DPI scaling factor.
Examples
Tutorial_203_Viewer_wxWidgets.

◆ drawables()

const std::vector< Drawable * > & drawables ( ) const
inline

Query the drawables managed by this viewer.

Returns
The drawables managed by this viewer.
Examples
Tutorial_203_Viewer_wxWidgets, and Tutorial_204_Viewer_Qt.

◆ fit_screen()

void fit_screen ( const easy3d::Model model = nullptr)

Moves the camera so that the entire scene or the active model is centered on the screen at a proper scale.

Parameters
modelThe pointer to the model to be centered on the screen. If nullptr, the entire scene (i.e., all models) will be centered on the screen at a proper scale.
Examples
Tutorial_203_Viewer_wxWidgets, Tutorial_301_Drawables, Tutorial_306_Image, and Tutorial_311_Animation.

◆ models()

const std::vector< Model * > & models ( ) const
inline

Query the models managed by this viewer.

Returns
The models managed by this viewer.
Examples
Tutorial_203_Viewer_wxWidgets, and Tutorial_204_Viewer_Qt.

◆ open()

bool open ( )
virtual

Open a model (PointCloud/SurfaceMesh/Graph) from a file into the viewer. On success, the viewer will be in charge of the memory management of the model.

This method loads a model into the viewer. Internally, it will pop up a file dialog for the user to navigate to the file. After loading the model, the necessary drawables (e.g., "vertices" for point clouds, "faces" for surface meshes, and "edges" and "vertices" for graphs) will be created for visualization.

Returns
true on success and false otherwise.

◆ point_under_pixel()

vec3 point_under_pixel ( int  x,
int  y,
bool &  found 
) const
virtual

Query the XYZ coordinates of the surface point under the cursor.

Parameters
xThe cursor x-coordinate, relative to the left edge of the content area.
yThe cursor y-coordinate, relative to the top edge of the content area.
foundindicates whether the point was found or not.
Returns
The coordinates of the 3D point located at pixel (x,y) on screen. The returned point is valid only if found was returned true.
Attention
The screen point (x, y) is expressed in the screen coordinate system with an origin in the upper left corner. So it doesn't necessarily correspond to a pixel on High DPI devices, e.g., a Mac with a Retina display. If your inherited viewer uses a customized content area, you must also reimplement this function such that the x and y are relative to left and top edges of the content area, respectively.
Note
This method assumes that a GL context is available, and that its content was drawn using the Camera (i.e. using its projection and model-view matrices). This method hence cannot be used for offscreen Camera computations. Use cameraCoordinatesOf() and worldCoordinatesOf() to perform similar operations in that case. The precision of the method highly depends on the z-Buffer, i.e., how the zNear() and zFar() values are fitted to your scene. Loose boundaries will result in imprecision along the viewing direction.

Reimplemented in MultiViewer.

◆ resize()

void resize ( int  w,
int  h 
)

Set/Change the viewer size.

Parameters
w/hThe requested width/height (in pixels) of the viewer.

◆ run()

int run ( bool  see_all = true)

Run the viewer.

Parameters
see_allSee all contents within the screen? If true, the entire scene (i.e., all models) will be centered on the screen at a proper scale.
Examples
Tutorial_201_Viewer_default, Tutorial_205_MultiView, Tutorial_301_Drawables, Tutorial_303_ScalarField, Tutorial_304_VectorField, Tutorial_305_Texture, Tutorial_310_TextMesher, Tutorial_311_Animation, Tutorial_602_ConvexPartition, and Tutorial_701_Cloud_NormalEstimation.

◆ samples()

int samples ( ) const
inline

Query the actual samples of the viewer.

Note
The requested sample may not be supported by the context or graphics driver. This method returns the actual samples that the viewer supports.
Examples
Tutorial_204_Viewer_Qt.

◆ save()

bool save ( ) const
virtual

Save the active model (if exists) to a file.

This method saves the active model to a file. Internally, it will pop up a file dialog for specifying the file name.

Returns
true on success and false otherwise.

◆ set_animation()

void set_animation ( bool  b)

Enable/Disable animation.

Attention
To have animation, animation_func_ must be provided to specify how scene geometry is modified.
Examples
Tutorial_311_Animation.

◆ set_background_color()

void set_background_color ( const easy3d::vec4 c)
inline

Set the background color of the viewer.

Parameters
colorThe background color.
Examples
Tutorial_203_Viewer_wxWidgets.

◆ set_position()

void set_position ( int  xpos,
int  ypos 
)

Sets the position of the content area of the viewer.

This function sets the position, in screen coordinates, of the upper-left corner of the content area of the viewer. If the viewer is a full screen window, this function does nothing.

Parameters
xposThe x-coordinate of the upper-left corner of the content area.
yposThe y-coordinate of the upper-left corner of the content area.
Note
This function must only be called from the main thread.

◆ set_title()

void set_title ( const std::string &  title)

Set the window title of the viewer.

Parameters
titleThe string of the title.

◆ snapshot() [1/2]

bool snapshot ( ) const

Take a snapshot of the screen and save it to a file.

This method takes a snapshot of the screen and saves the snapshot into an image file. Internally, it will pop up a file dialog for specifying the file name.

Returns
true on success and false otherwise.
Examples
Tutorial_203_Viewer_wxWidgets.

◆ snapshot() [2/2]

bool snapshot ( const std::string &  file_name,
float  scaling = 1.0f,
int  samples = 0,
int  back_ground = 1,
bool  expand = true 
) const

Take a snapshot of the screen and save it to an image file. Supported image format: png, jpg, bmp, and tga.

This function renders the scene into a framebuffer and takes a snapshot of the framebuffer. It allow the snapshot image to have a dimension different from the viewer and it has no limit on the image size (if memory allows).

Parameters
file_nameThe image file name.
scalingThe scaling factor that determines the size of the image (default to 1.0, using the viewer size), i.e., image_width = viewer_width * scaling; image_height = viewer_height * scaling;
samplesThe required number of samples for antialiased rendering (can be different from the default framebuffer). The default value is 0 (no antialiasing).
back_groundDetermines the background color. 0: current color; 1: white; 2: transparent.
expandExpand the frustum to ensure the image aspect ratio.
Returns
true on success and false otherwise.

◆ title()

const std::string & title ( ) const
inline

Query the window title of the viewer.

Returns
The string of the window title.
Examples
Tutorial_203_Viewer_wxWidgets.

◆ update()

void update ( ) const

Update the display (i.e., repaint).

This method is used to update the display of the rendering. Client should call it when your data/view is changed.

Examples
Tutorial_203_Viewer_wxWidgets, Tutorial_204_Viewer_Qt, Tutorial_311_Animation, Tutorial_312_MultiThread, Tutorial_701_Cloud_NormalEstimation, Tutorial_702_Cloud_SurfaceReconstruction, and Tutorial_703_Cloud_PlaneExtraction.

Friends And Related Function Documentation

◆ add_model() [1/2]

Model * add_model ( const std::string &  file_name,
bool  create_default_drawables = true 
)
related

Add a model from a file to the viewer to be visualized. On success, the viewer will be in charge of the memory management of the model. The loaded model can be accessed by the 'current_model()' method.

This method loads a model into the viewer. It allows the user to control if default drawables will be created. The default drawables are

  • for point clouds: "vertices".
  • for surface meshes: "faces", "vertices", "edges", "borders", and "locks".
  • for graphs: "vertices" and "edges".
  • polyhedral meshes: "faces:border", "faces:interior", "vertices", and "edges". These drawables are usually sufficient for basic rendering of the model. In case the default drawables don't meet the particular visualization purpose, you can set 'create_default_drawables' to false and create your needed drawables by calling model->renderer()->add_[type]_drawable(). Here the [type] must be one of "points", "lines", and "triangles'.
    Parameters
    file_nameThe string of the file name.
    create_default_drawablesIf true, the default drawables will be created.
    Returns
    The pointer to the model added to the viewer (nullptr if failed). (Model*, bool).
Examples
Tutorial_201_Viewer_default, Tutorial_203_Viewer_wxWidgets, Tutorial_205_MultiView, Tutorial_207_RealCamera, Tutorial_303_ScalarField, Tutorial_304_VectorField, Tutorial_305_Texture, Tutorial_308_TexturedMesh, Tutorial_310_TextMesher, Tutorial_402_FacePicker, Tutorial_404_VirtualScanner, Tutorial_406_CollisionDetection, Tutorial_602_ConvexPartition, Tutorial_701_Cloud_NormalEstimation, and Tutorial_702_Cloud_SurfaceReconstruction.

◆ add_model() [2/2]

Model * add_model ( Model model,
bool  create_default_drawables = true 
)
related

Add an existing model to the viewer to be visualized. If the model has been successfully added to the viewer, the viewer will be in charge of its memory management.

This method adds a model into the viewer. It allows the user to control if default drawables will be created. The default drawables are

  • for point clouds: "vertices".
  • for surface meshes: "faces", "vertices", "edges", "borders", and "locks".
  • for graphs: "vertices" and "edges".
  • polyhedral meshes: "faces:border", "faces:interior", "vertices", and "edges". These drawables are usually sufficient for basic rendering of the model. In case the default drawables don't meet the particular visualization purpose, you can set 'create_default_drawables' to false and create your needed drawables by calling model->renderer()->add_[type]_drawable(). Here the [type] must be one of "points", "lines", and "triangles'.
    Parameters
    modelThe pointer to the model.
    create_default_drawablesIf true, the default drawables will be created.
    Returns
    The pointer to the model added to the viewer (nullptr if failed). (const std::string&, bool).

The documentation for this class was generated from the following files: