| Line |
Branch |
Exec |
Source |
| 1 |
|
|
// |
| 2 |
|
|
// Copyright (c) 2024-2025 INRIA |
| 3 |
|
|
// |
| 4 |
|
|
|
| 5 |
|
|
#ifndef __pinocchio_extra_base_visualizer_hpp__ |
| 6 |
|
|
#define __pinocchio_extra_base_visualizer_hpp__ |
| 7 |
|
|
|
| 8 |
|
|
#include "pinocchio/macros.hpp" |
| 9 |
|
|
#include "pinocchio/visualizers/config.hpp" |
| 10 |
|
|
#include "pinocchio/multibody/data.hpp" |
| 11 |
|
|
#include "pinocchio/multibody/geometry.hpp" |
| 12 |
|
|
|
| 13 |
|
|
#include <boost/optional.hpp> |
| 14 |
|
|
#include <utility> |
| 15 |
|
|
|
| 16 |
|
|
namespace pinocchio |
| 17 |
|
|
{ |
| 18 |
|
|
namespace visualizers |
| 19 |
|
|
{ |
| 20 |
|
|
typedef PINOCCHIO_SCALAR_TYPE_DEFAULT Scalar; |
| 21 |
|
|
PINOCCHIO_COMMON_TYPEDEF(PINOCCHIO_SCALAR_TYPE_DEFAULT, PINOCCHIO_OPTIONS_DEFAULT) |
| 22 |
|
|
|
| 23 |
|
|
typedef Eigen::Ref<const VectorXs> ConstVectorRef; |
| 24 |
|
|
typedef Eigen::Ref<const MatrixXs> ConstMatrixRef; |
| 25 |
|
|
|
| 26 |
|
|
/// @brief A base class for defining visualizers for Pinocchio in C++. This |
| 27 |
|
|
/// provides basic building blocks (a base constructor, data members, getters |
| 28 |
|
|
/// for the models). |
| 29 |
|
|
/// @details The base API assumes that the visualizer is not the owner of the |
| 30 |
|
|
/// underlying Pinocchio multibody, visual or collision models. Their lifetimes |
| 31 |
|
|
/// should be managed by the application context itself. |
| 32 |
|
|
/// @remark C++ port of the %BaseVisualizer abstract class in Pinocchio's Python |
| 33 |
|
|
/// bindings. |
| 34 |
|
|
class PINOCCHIO_VISUALIZERS_DLLAPI BaseVisualizer |
| 35 |
|
|
{ |
| 36 |
|
|
public: |
| 37 |
|
|
typedef SE3::Matrix4 Matrix4; |
| 38 |
|
|
|
| 39 |
|
|
/// @brief Class constructor for borrowing external data. |
| 40 |
|
|
/// |
| 41 |
|
|
/// @param model Reference to Pinocchio Model class. |
| 42 |
|
|
/// @param visual_model Reference to visual GeometryModel. |
| 43 |
|
|
/// @param collision_model Pointer to collision model. Pass nullptr if no collision model is |
| 44 |
|
|
/// to be provided. |
| 45 |
|
|
/// @param data Reference to Data object. The visualizer will store an internal reference as a |
| 46 |
|
|
/// pointer. |
| 47 |
|
|
/// @param visual_data Reference to visual (VISUAL) GeometryData object. The visualizer will |
| 48 |
|
|
/// store an internal reference as a pointer. |
| 49 |
|
|
/// @param collision_data Pointer to collision (COLLISION) GeometryData object. The visualizer |
| 50 |
|
|
/// will store this pointer internally as a reference. |
| 51 |
|
|
/// @remark This constructor throws if a \p collision_model is provided with no \p |
| 52 |
|
|
/// collision_data. If the reverse happens, then the collision GeometryData is simply ignored. |
| 53 |
|
|
BaseVisualizer( |
| 54 |
|
|
const Model & model, |
| 55 |
|
|
const GeometryModel & visual_model, |
| 56 |
|
|
const GeometryModel * collision_model, |
| 57 |
|
|
Data & data, |
| 58 |
|
|
GeometryData & visual_data, |
| 59 |
|
|
GeometryData * collision_data); |
| 60 |
|
|
|
| 61 |
|
|
/// @brief Class constructor which will create internally-managed data objects. |
| 62 |
|
|
/// |
| 63 |
|
|
/// @param model Reference to Pinocchio Model class. |
| 64 |
|
|
/// @param visual_model Reference to visual GeometryModel. |
| 65 |
|
|
/// @param collision_model Pointer to collision model. Pass nullptr if no collision model is |
| 66 |
|
|
/// to be provided. |
| 67 |
|
|
BaseVisualizer( |
| 68 |
|
|
const Model & model, |
| 69 |
|
|
const GeometryModel & visual_model, |
| 70 |
|
|
const GeometryModel * collision_model = nullptr); |
| 71 |
|
|
|
| 72 |
|
|
virtual ~BaseVisualizer(); |
| 73 |
|
|
|
| 74 |
|
|
/// @brief Initialize the viewer. |
| 75 |
|
✗ |
virtual void initViewer() |
| 76 |
|
|
{ |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
|
/// @brief Load the Pinocchio model. |
| 80 |
|
|
virtual void loadViewerModel() = 0; |
| 81 |
|
|
|
| 82 |
|
|
/// @brief Re-build data objects. Required if the models were modified. |
| 83 |
|
|
virtual void rebuildData(); |
| 84 |
|
|
|
| 85 |
|
|
/// @brief Display configuration @p q (if an actual value is given) or update |
| 86 |
|
|
/// the Pinocchio frames. |
| 87 |
|
|
virtual void display(const boost::optional<ConstVectorRef> & q = boost::none); |
| 88 |
|
|
|
| 89 |
|
|
/// @copybrief display() |
| 90 |
|
|
template<typename D> |
| 91 |
|
✗ |
void display(const Eigen::MatrixBase<D> & q) |
| 92 |
|
|
{ |
| 93 |
|
✗ |
boost::optional<ConstVectorRef> q_(q); |
| 94 |
|
✗ |
display(q_); |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
|
/// @brief Play an entire trajectory, waiting for time @p dt between each |
| 98 |
|
|
/// keyframe. |
| 99 |
|
|
virtual void play(const std::vector<ConstVectorRef> & qs, Scalar dt); |
| 100 |
|
|
|
| 101 |
|
|
void play(const ConstMatrixRef & qs, Scalar dt); |
| 102 |
|
|
|
| 103 |
|
|
/// @brief Override this in child class when the scene has to be redrawn. |
| 104 |
|
|
/// Useful for @ref play(). |
| 105 |
|
✗ |
virtual bool forceRedraw() |
| 106 |
|
|
{ |
| 107 |
|
✗ |
return true; |
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
/// @brief Set the active camera target. |
| 111 |
|
✗ |
virtual void setCameraTarget(const Eigen::Ref<const Vector3> & /*target*/) |
| 112 |
|
|
{ |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
|
/// @brief Set the active camera position. |
| 116 |
|
✗ |
virtual void setCameraPosition(const Eigen::Ref<const Vector3> & /*position*/) |
| 117 |
|
|
{ |
| 118 |
|
|
} |
| 119 |
|
|
|
| 120 |
|
|
/// @brief Set the active camera 6D pose. |
| 121 |
|
✗ |
virtual void setCameraPose(const Eigen::Ref<const Matrix4> & /*pose*/) |
| 122 |
|
|
{ |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
/// @copybrief setCameraPose() |
| 126 |
|
✗ |
inline void setCameraPose(const SE3 & pose) |
| 127 |
|
|
{ |
| 128 |
|
✗ |
this->setCameraPose(pose.toHomogeneousMatrix()); |
| 129 |
|
|
} |
| 130 |
|
|
|
| 131 |
|
|
/// @brief Set camera zoom level; what this means depends on the |
| 132 |
|
|
/// implementation (FOV zoom or moving forwards). |
| 133 |
|
✗ |
virtual void setCameraZoom(Scalar /*value*/) |
| 134 |
|
|
{ |
| 135 |
|
|
} |
| 136 |
|
|
|
| 137 |
|
|
/// @brief Enable/disable controlling the camera from keyboard and mouse. |
| 138 |
|
✗ |
virtual void enableCameraControl(bool) |
| 139 |
|
|
{ |
| 140 |
|
|
} |
| 141 |
|
|
|
| 142 |
|
|
/// @brief Delete all objects from the scene. |
| 143 |
|
✗ |
virtual void clean() |
| 144 |
|
|
{ |
| 145 |
|
|
} |
| 146 |
|
|
|
| 147 |
|
1 |
const Model & model() const |
| 148 |
|
|
{ |
| 149 |
|
1 |
return m_model; |
| 150 |
|
|
} |
| 151 |
|
|
|
| 152 |
|
1 |
const GeometryModel & visualModel() const |
| 153 |
|
|
{ |
| 154 |
|
1 |
return *m_visualModel; |
| 155 |
|
|
} |
| 156 |
|
|
|
| 157 |
|
✗ |
const GeometryModel & collisionModel() const |
| 158 |
|
|
{ |
| 159 |
|
✗ |
PINOCCHIO_THROW( |
| 160 |
|
|
hasCollisionModel(), std::logic_error, "No collision model in the visualizer."); |
| 161 |
|
✗ |
return *m_collisionModel; |
| 162 |
|
|
} |
| 163 |
|
|
|
| 164 |
|
5 |
bool hasCollisionModel() const |
| 165 |
|
|
{ |
| 166 |
|
5 |
return m_collisionModel != nullptr; |
| 167 |
|
|
} |
| 168 |
|
|
|
| 169 |
|
|
/// @brief Whether the internal data pointers are borrowed (external), or owned. |
| 170 |
|
2 |
bool hasExternalData() const |
| 171 |
|
|
{ |
| 172 |
|
2 |
return !m_ownedData; |
| 173 |
|
|
} |
| 174 |
|
|
|
| 175 |
|
✗ |
Data & data() |
| 176 |
|
|
{ |
| 177 |
|
✗ |
return *m_data; |
| 178 |
|
|
} |
| 179 |
|
|
const Data & data() const |
| 180 |
|
|
{ |
| 181 |
|
|
return *m_data; |
| 182 |
|
|
} |
| 183 |
|
|
|
| 184 |
|
✗ |
GeometryData & visualData() |
| 185 |
|
|
{ |
| 186 |
|
✗ |
return *m_visualData; |
| 187 |
|
|
} |
| 188 |
|
|
const GeometryData & visualData() const |
| 189 |
|
|
{ |
| 190 |
|
|
return *m_visualData; |
| 191 |
|
|
} |
| 192 |
|
|
|
| 193 |
|
✗ |
GeometryData & collisionData() |
| 194 |
|
|
{ |
| 195 |
|
✗ |
PINOCCHIO_THROW( |
| 196 |
|
|
hasCollisionModel(), std::logic_error, "No collision model in the visualizer."); |
| 197 |
|
✗ |
return *m_collisionData; |
| 198 |
|
|
} |
| 199 |
|
|
|
| 200 |
|
|
const GeometryData & collisionData() const |
| 201 |
|
|
{ |
| 202 |
|
|
PINOCCHIO_THROW( |
| 203 |
|
|
hasCollisionModel(), std::logic_error, "No collision model in the visualizer."); |
| 204 |
|
|
return *m_collisionData; |
| 205 |
|
|
} |
| 206 |
|
|
|
| 207 |
|
|
protected: |
| 208 |
|
|
std::reference_wrapper<Model const> m_model; |
| 209 |
|
|
GeometryModel const * m_visualModel; |
| 210 |
|
|
GeometryModel const * m_collisionModel; |
| 211 |
|
|
|
| 212 |
|
|
Data * m_data; |
| 213 |
|
|
GeometryData * m_visualData; |
| 214 |
|
|
GeometryData * m_collisionData; |
| 215 |
|
|
bool m_ownedData; |
| 216 |
|
|
|
| 217 |
|
|
/// @brief This method is called at the beginning of display(). |
| 218 |
|
✗ |
virtual void displayPrecall() |
| 219 |
|
|
{ |
| 220 |
|
|
} |
| 221 |
|
|
virtual void displayImpl() = 0; |
| 222 |
|
|
|
| 223 |
|
|
void destroyData(); |
| 224 |
|
|
}; |
| 225 |
|
|
} // namespace visualizers |
| 226 |
|
|
} // namespace pinocchio |
| 227 |
|
|
|
| 228 |
|
|
#endif // ifndef __pinocchio_extra_base_visualizer_hxx__ |
| 229 |
|
|
|