Skip to content

Commit

Permalink
Improve visualization library (#802)
Browse files Browse the repository at this point in the history
It does the following:
- It adds a camera animator to move the camera via the mouse
- It fixes the aspect ratio to avoid strange effects when resizing
- It adds an interface to add custom frames to the visualization
- It fixes the floor grid colors
- It adds an interface to have additional rendering target textures (helpful if you need to render to a different size with a different environment and to get the pixels of the corresponding image)
- It fixes the visualizations of ``STL`` meshes.
  • Loading branch information
S-Dafarra authored Jan 26, 2021
1 parent bf8d0f3 commit 4bc88cf
Show file tree
Hide file tree
Showing 25 changed files with 1,511 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add the possibility to plot and update frames in the Matlab visualizer.
- Added ``getFileLocationOnLocalFileSystem`` method in ``ExternalMesh`` that attempts to find the mesh location in the local file system. This is now used by the ``Visualizer`` when loading the robot model (https://github.com/robotology/idyntree/pull/798)
- Add the possibility to extract submatrix with MatrixView (https://github.com/robotology/idyntree/pull/800)
- Improved the Visualizer library: camera animations and corrections, interface for frames and texture, fix of ``STL`` visualization. (https://github.com/robotology/idyntree/pull/802)

### Changed
- Promoted the functions `computeBoundingBoxFromShape` and `computeBoxVertices` to public in the `idyntree-solid-shapes` library (https://github.com/robotology/idyntree/pull/801).
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

cmake_minimum_required(VERSION 3.16)

project(iDynTree VERSION 2.0.2
project(iDynTree VERSION 2.0.100
LANGUAGES C CXX)

# Disable in source build, unless Eclipse is used
Expand Down
1 change: 1 addition & 0 deletions src/tools/idyntree-model-view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ int main(int argc, char** argv)
// Visualize the model
iDynTree::Visualizer visualizer;
ok = visualizer.addModel(mdlLoader.model(),"model");
visualizer.camera().animator()->enableMouseControl();

if( !ok )
{
Expand Down
16 changes: 12 additions & 4 deletions src/visualization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@ if(IDYNTREE_USES_IRRLICHT)
# Some files are compiled only when irrlicht is available
set(iDynTree_visualization_private_headers src/IrrlichtUtils.h
src/Camera.h
src/CameraAnimator.h
src/Environment.h
src/ModelVisualization.h
src/JetsVisualization.h
src/VectorsVisualization.h
src/FrameVisualization.h
src/Texture.h
src/TexturesHandler.h
src/Light.h
src/FloorGridSceneNode.h)
set(iDynTree_visualization_private_source src/Camera.cpp
src/CameraAnimator.cpp
src/Environment.cpp
src/ModelVisualization.cpp
src/JetsVisualization.cpp
src/VectorsVisualization.cpp
src/FrameVisualization.cpp
src/Texture.cpp
src/TexturesHandler.cpp
src/Light.cpp
src/FloorGridSceneNode.cpp)
endif()
Expand Down Expand Up @@ -72,10 +80,10 @@ if(IDYNTREE_USES_IRRLICHT)

# On Apple system, irrlicht uses some system libraries
if (APPLE)
find_library(CARBON_LIBRARY Carbon)
find_library(COCOA_LIBRARY Cocoa)
find_library(IOKIT_LIBRARY IOKit)
target_link_libraries(${libraryname} LINK_PRIVATE ${CARBON_LIBRARY} ${COCOA_LIBRARY} ${IOKIT_LIBRARY})
find_library(CARBON_LIBRARY Carbon)
find_library(COCOA_LIBRARY Cocoa)
find_library(IOKIT_LIBRARY IOKit)
target_link_libraries(${libraryname} LINK_PRIVATE ${CARBON_LIBRARY} ${COCOA_LIBRARY} ${IOKIT_LIBRARY})
endif ()
endif()

Expand Down
222 changes: 215 additions & 7 deletions src/visualization/include/iDynTree/Visualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define IDYNTREE_VISUALIZER_H

#include <string>
#include <vector>

#include <iDynTree/Core/Direction.h>
#include <iDynTree/Core/Position.h>
Expand All @@ -25,6 +26,51 @@ class Model;
class Transform;
class Visualizer;

/**
* Interface to animate the camera and control it via the mouse
*/
class ICameraAnimator
{
public:

/**
* Enable the control of camera via the mouse.
* By default, left button rotates the camera, the right button translates it on the plane.
* The middle button, translates the camera up and down, while the wheel increases or decreases the zoom.
*/
virtual void enableMouseControl(bool enabled = true) = 0;

/**
* Returns the speed of movement
*/
virtual double getMoveSpeed() const = 0;

/**
* Sets the speed of movement
*/
virtual void setMoveSpeed(double moveSpeed) = 0;

/**
* Returns the rotation speed
*/
virtual double getRotateSpeed() const = 0;

/**
* Set the rotation speed
*/
virtual void setRotateSpeed(double rotateSpeed) = 0;

/**
* Returns the zoom speed
*/
virtual double getZoomSpeed() const = 0;

/**
* Set the zoom speed
*/
virtual void setZoomSpeed(double zoomSpeed) = 0;
};

/**
* Interface to manipulate the camera parameters.
*/
Expand All @@ -50,6 +96,14 @@ class ICamera
* Set the up vector of the camera w.r.t to the world.
*/
virtual void setUpVector(const Direction& upVector) = 0;

/**
* Get a pointer to the CameraAnimator object.
* It is not supposed to be deallocated, and its lifespan coincides with the one of the ICamera
* It is accessible only after the visualizer has been initialized.
*/
virtual ICameraAnimator* animator() = 0;

};

/**
Expand Down Expand Up @@ -94,6 +148,25 @@ class ColorViz
ColorViz(const Vector4 & rgba);
};

/**
* Basic structure to encode pixel information
*/
class PixelViz : public ColorViz
{
public:

/**
* Width position of the pixel.
*/
unsigned int width;

/**
* Height position of the pixel.
*/
unsigned int height;

};

enum LightType
{
//! point light, it has a position in space and radiates light in all directions
Expand Down Expand Up @@ -213,6 +286,11 @@ class IEnvironment
*/
virtual void setBackgroundColor(const ColorViz & backgroundColor) = 0;

/**
* Set the floor grid color.
*/
virtual void setFloorGridColor(const ColorViz & floorGridColor) = 0;

/**
* Set the ambient light of the enviroment.
*/
Expand Down Expand Up @@ -358,6 +436,47 @@ class IVectorsVisualization
virtual bool setVectorsAspect(double zeroModulusRadius, double modulusMultiplier, double heightScale) = 0;
};

/**
* Interface to the visualization of frames.
*/
class IFrameVisualization
{
public:

/**
* Destructor
*/
virtual ~IFrameVisualization() = 0;

/**
* Add a frame in the visualization
* Returns the frame index.
*/
virtual size_t addFrame(const Transform& transformation, double arrowLength = 1.0) = 0;

/**
* Set the specified frame visible or not.
* Returns true in case of success, false otherwise (for example if the frame does not exists).
*/
virtual bool setVisible(size_t frameIndex, bool isVisible) = 0;

/**
* Get the number of visualized frames.
*
*/
virtual size_t getNrOfFrames() const = 0;

/**
* Get frame transform.
*/
virtual bool getFrameTransform(size_t frameIndex, Transform& currentTransform) const = 0;

/**
* Update Frame
*/
virtual bool updateFrame(size_t frameIndex, const Transform& transformation) = 0;
};


/**
* Interface to the visualization of a model istance.
Expand Down Expand Up @@ -406,21 +525,21 @@ class IModelVisualization
/**
* Reset the colors of the model.
*/
virtual void resetModelColor() = 0;
virtual void resetModelColor() = 0;

/**
* Set the color of all the geometries of the given link.
*
* This will overwrite the material of the link, but it can be
* reset by resetLinkColor.
*/
virtual bool setLinkColor(const LinkIndex& linkIndex, const ColorViz& linkColor) = 0;

/**
* Reset the colors of given link.
*/
virtual bool resetLinkColor(const LinkIndex& linkIndex) = 0;

/**
* Get the name of the link in the model.
*/
Expand Down Expand Up @@ -450,14 +569,14 @@ class IModelVisualization
* Get a reference to the internal IJetsVisualization interface.
*/
virtual IJetsVisualization& jets() = 0;

/**
* Get the transformation of the model (root link) with respect to visualizer world \f$ w_H_{root}\f$
* The obtained transformation matrix can be used to map any homogeneous vector from the
* model's root link frame to the visualizer world frame.
*/
virtual Transform getWorldModelTransform() = 0;

/**
* Get the transformation of given link with respect to visualizer world \f$ w_H_{link}\f$
* The obtained transformation matrix can be used to map any homogeneous vector from the
Expand All @@ -466,6 +585,51 @@ class IModelVisualization
virtual Transform getWorldLinkTransform(const LinkIndex& linkIndex) = 0;
};

/**
* The interface for an object that can be used as an additional target for the renderer.
* This allows rendering the scene using dimensions and environment that are different from
* the main window. The camera is in common. Any camera change in the main window is also
* reflected in the other textures.
*/
class ITexture
{
public:

/**
* Destructor
*/
virtual ~ITexture() = 0;

/**
* Return an interface to manipulate the texture environment.
*/
virtual IEnvironment& environment() = 0;

/**
* @brief Get the color of the pixel at the given position in the additional texture.
*
* Remember to call draw() first.
* @param width The width of the pixel
* @param height The height of the pixel
* @return The color of the pixel
*/
virtual ColorViz getPixelColor(unsigned int width, unsigned int height) const = 0;

/**
* @brief Get the pixels of the texture.
*
* Remember to call draw() first.
* @param pixels The output pixels. The size of the vector will be equal to the total number of
* pixels of the rendered texture, i.e. width of the texture times its height. Both these two parameters
* can be set in the textureOptions passed to the method ITexturesHandler::add. The pixels are saved in
* col-major format.
* @note This operation may affect the time performances of the visualizer, especially if the texture is large.
* @return True in case of success, false otherwise
*/
virtual bool getPixels(std::vector<PixelViz>& pixels) const = 0;

};

/**
* Visualizer options
*/
Expand Down Expand Up @@ -499,6 +663,33 @@ struct VisualizerOptions
}
};

class ITexturesHandler
{
public:

/**
* Destructor
*/
virtual ~ITexturesHandler() = 0;

/**
* @brief Add a texture
* @param The name of the texture
* @param visualizerOptions The options for the texture
* @note The pointer should not be deleted. Its lifespan coincides with the one of the ITextureHandler.
* @return A ITexture pointer in case of success. A nullptr otherwise
*/
virtual ITexture* add(const std::string& name, const VisualizerOptions& textureOptions = VisualizerOptions()) = 0;

/**
* @brief Get a specific texture
* @param The name of the texture to get.
* @note The pointer should not be deleted. Its lifespan coincides with the one of the ITextureHandler.
* @return the pointer to the texture. A nullptr if that texture does not exists.
*/
virtual ITexture* get(const std::string& name) = 0;
};

/**
* Class to visualize a set of iDynTree models
*/
Expand All @@ -522,7 +713,7 @@ friend class ModelVisualization;
*
* \note this is called implicitly when addModel is called for the first time.
*/
bool init(const VisualizerOptions = VisualizerOptions());
bool init(const VisualizerOptions& visualizerOptions = VisualizerOptions());

/**
* Get number of models visualized.
Expand Down Expand Up @@ -580,6 +771,16 @@ friend class ModelVisualization;
*/
IVectorsVisualization& vectors();

/**
* Get a reference to the internal IFrameVisualization interface.
*/
IFrameVisualization& frames();

/**
* Get a reference to the internal ITexturesHandler interface.
*/
ITexturesHandler& textures();

/**
* Wrap the run method of the Irrlicht device.
*/
Expand All @@ -606,6 +807,13 @@ friend class ModelVisualization;
* Close the visualizer.
*/
void close();

/**
* @brief Get if the visualizer window is active (to allow drawing only if necessary)
* @return True if the window is active, false otherwise.
*/
bool isWindowActive() const;

};

}
Expand Down
Loading

0 comments on commit 4bc88cf

Please sign in to comment.