Skip to content

Commit

Permalink
Add iDynTree::MeshcatVisualizer class (#1074)
Browse files Browse the repository at this point in the history

Co-authored-by: Silvio Traversaro <silvio.traversaro@iit.it>
  • Loading branch information
GiulioRomualdi and traversaro authored May 19, 2023
1 parent 35b0f76 commit 850b379
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added optional dependency on [meshcat-cpp](https://github.com/ami-iit/meshcat-cpp) (https://github.com/robotology/idyntree/pull/1074).
- Added `iDynTree::MeshcatVisualizer` C++ class (https://github.com/robotology/idyntree/pull/1074).

## [9.0.0] - 2023-05-11

### Fixed
Expand Down
1 change: 1 addition & 0 deletions cmake/iDynTreeDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ endif ()

idyntree_handle_dependency(IPOPT)
idyntree_handle_dependency(Irrlicht DO_NOT_SILENTLY_SEARCH)
idyntree_handle_dependency(MeshcatCpp MAIN_TARGET MeshcatCpp::MeshcatCpp DO_NOT_SILENTLY_SEARCH)
idyntree_handle_dependency(OsqpEigen MAIN_TARGET OsqpEigen::OsqpEigen)
idyntree_handle_dependency(ALGLIB DO_NOT_SILENTLY_SEARCH)
set(ALGLIB_REQUIRED_VERSION 3.14.0)
Expand Down
1 change: 1 addition & 0 deletions doc/build-from-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ In case they are disabled, tipically some functionality of iDynTree is not provi
| [irrlicht](http://irrlicht.sourceforge.net/) | No | `IDYNTREE_USES_IRRLICHT` | ✔️ | ✔️ |
| [glfw](https://www.glfw.org/) | No | `IDYNTREE_USES_IRRLICHT` | ✔️ | ✔️ |
| [osqp-eigen](https://github.com/robotology/osqp-eigen) | No | `IDYNTREE_USES_OSQPEIGEN` | ✔️ | ✔️ |
| [meshcat-cpp](https://github.com/ami-iit/meshcat-cpp) | No | `IDYNTREE_USES_MESHCATCPP` |||


### Install dependencies with conda-forge
Expand Down
12 changes: 12 additions & 0 deletions src/visualization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ if(IDYNTREE_USES_IRRLICHT)
src/FloorGridSceneNode.cpp)
endif()

if(IDYNTREE_USES_MESHCATCPP)
set(iDynTree_visualization_header ${iDynTree_visualization_header} include/iDynTree/MeshcatVisualizer.h)
set(iDynTree_visualization_source ${iDynTree_visualization_source} src/MeshcatVisualizer.cpp)
endif()

source_group("Source Files" FILES ${iDynTree_visualization_source})
source_group("Header Files" FILES ${iDynTree_visualization_header})
source_group("Private Source Files" FILES ${iDynTree_visualization_source})
Expand Down Expand Up @@ -92,6 +97,13 @@ if(IDYNTREE_USES_IRRLICHT)
endif ()
endif()

if(IDYNTREE_USES_MESHCATCPP)
find_package(MeshcatCpp REQUIRED)
target_link_libraries(${libraryname} PRIVATE MeshcatCpp::MeshcatCpp)
endif()



install(TARGETS ${libraryname}
EXPORT iDynTree
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin
Expand Down
81 changes: 81 additions & 0 deletions src/visualization/include/iDynTree/MeshcatVisualizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2023 Fondazione Istituto Italiano di Tecnologia
*
* Licensed under either the GNU Lesser General Public License v3.0 :
* https://www.gnu.org/licenses/lgpl-3.0.html
* or the GNU Lesser General Public License v2.1 :
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* at your option.
*/

#ifndef IDYNTREE_MESHCAT_VISUALIZER_H
#define IDYNTREE_MESHCAT_VISUALIZER_H

#include <memory>
#include <string>

#include <iDynTree/Core/Transform.h>
#include <iDynTree/Core/Span.h>
#include <iDynTree/Core/MatrixView.h>
#include <iDynTree/Core/VectorDynSize.h>
#include <iDynTree/Model/Model.h>

namespace iDynTree
{

/**
* MeshcatVisualizer is an iDynTree-based wrapper to the [meshcat-cpp](https://github.com/ami-iit/meshcat-cpp) visualizer.
* \note Only meshes are supported and the color is taken from the iDynTree::Model
*/
class MeshcatVisualizer
{
public:
MeshcatVisualizer();
~MeshcatVisualizer();

/**
* Load a given model in the visualizer.
* @param model the model that should be loaded.
* @param modelName the name of the model used in the visualizer. Each model you add needs to have an unique name.
* @return True in case of success false otherwise/
* @warning Only meshes are supported. The support to the primary shapes needs to be added.
*/
bool loadModel(const iDynTree::Model& model,
const std::string& modelName);

/**
* Set the state of an already existing model in the visualizer.
* @param world_T_base pose of the base of the model.
* @param jointPositions position of the joints.
* @param modelName the name of the model specified in MeshcatVisualizer::loadModel(),
* @return True in case of success false otherwise.
*/
bool setModelState(const iDynTree::Transform& world_T_base,
const iDynTree::VectorDynSize& jointPositions,
const std::string& modelName);

/**
* Set the state of an already existing model in the visualizer.
* @param world_T_base 4x4 matrix representing the homogeneous transformation,
* @param jointPositions position of the joints,
* @param modelName the name of the model specified in MeshcatVisualizer::loadModel().
* @return True in case of success false otherwise.
*/
bool setModelState(const iDynTree::MatrixView<const double> &world_T_base,
const iDynTree::Span<const double> &jointPositions,
const std::string &modelName);

/**
* Utility function to make the meshcat interface run forever (until the user stops the
* application)
*/
void join();

private:
class Impl;
std::unique_ptr<Impl> m_pimpl;
};

} // namespace iDynTree

#endif // IDYNTREE_MESHCAT_VISUALIZER_H
Loading

0 comments on commit 850b379

Please sign in to comment.