DART 6.13.2
Loading...
Searching...
No Matches
Viewer.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_GUI_OSG_VIEWER_HPP_
34#define DART_GUI_OSG_VIEWER_HPP_
35
36#include <map>
37#include <memory>
38#include <unordered_set>
39
40#include <Eigen/Core>
41#include <osgShadow/ShadowTechnique>
42#include <osgViewer/Viewer>
43
46
47namespace dart {
48
49namespace simulation {
50class World;
51} // namespace simulation
52
53namespace dynamics {
54class Entity;
55class SimpleFrame;
56class Shape;
57class BodyNode;
58} // namespace dynamics
59
60namespace gui {
61namespace osg {
62
63namespace detail {
64class CameraModeCallback;
65} // namespace detail
66
67class WorldNode;
68class DefaultEventHandler;
69class DragAndDrop;
70class SimpleFrameDnD;
71class SimpleFrameShapeDnD;
72class InteractiveFrame;
73class InteractiveFrameDnD;
74class BodyNodeDnD;
75class Viewer;
76class SaveScreen;
77
79enum class CameraMode
80{
82 RGBA,
83
87 DEPTH,
88};
89
90[[nodiscard]] std::string toString(CameraMode mode);
91
93class ViewerAttachment : public virtual ::osg::Group
94{
95public:
96 friend class Viewer;
97
100
102 virtual ~ViewerAttachment();
103
106 virtual void refresh() = 0;
107
108 Viewer* getViewer();
109
110 const Viewer* getViewer() const;
111
112protected:
116 virtual void customAttach(Viewer* newViewer);
117
122 virtual void attach(Viewer* newViewer);
123
124private:
126};
127
128class Viewer : public osgViewer::Viewer, public dart::common::Subject
129{
130public:
133 Viewer(const ::osg::Vec4& clearColor = ::osg::Vec4(0.9, 0.9, 0.9, 1.0));
134
136 virtual ~Viewer();
137
144 // TODO(MXG): Add a bool argument that instructs the viewer to append a date
145 // and time to the name of the file.
146 void captureScreen(const std::string& filename);
147
158 void record(
159 const std::string& directory,
160 const std::string& prefix = "image",
161 bool restart = false,
162 std::size_t digits = 6);
163
167 void pauseRecording();
168
170 bool isRecording() const;
171
173 virtual void switchDefaultEventHandler(bool _on);
174
177
179 virtual void switchHeadlights(bool _on);
180
182 bool checkHeadlights() const;
183
187 void addWorldNode(WorldNode* _newWorldNode, bool _active = true);
188
190 void removeWorldNode(WorldNode* _oldWorldNode);
191
193 void removeWorldNode(std::shared_ptr<dart::simulation::World> _oldWorld);
194
198 std::shared_ptr<dart::simulation::World> _world) const;
199
202 void addAttachment(ViewerAttachment* _attachment);
203
205 void removeAttachment(ViewerAttachment* _attachment);
206
208 const std::unordered_set<ViewerAttachment*>& getAttachments() const;
209
211 ::osg::Group* getLightGroup();
212
214 const ::osg::Group* getLightGroup() const;
215
219 const ::osg::ref_ptr<::osg::LightSource>& getLightSource(
220 std::size_t index = 0) const;
221
223 void setupDefaultLights();
224
227 void setUpwardsDirection(const ::osg::Vec3& _up);
228
231 void setUpwardsDirection(const Eigen::Vector3d& _up);
232
234 void setWorldNodeActive(WorldNode* _node, bool _active = true);
235
238 std::shared_ptr<dart::simulation::World> _world, bool _active = true);
239
241 void simulate(bool _on);
242
244 bool isSimulating() const;
245
247 void allowSimulation(bool _allow);
248
250 bool isAllowingSimulation() const;
251
258
261
267
270
274 bool _useExternalIK = true,
275 bool _useWholeBody = false);
276
280
284
288
292
296
298 const std::string& getInstructions() const;
299
303 void addInstructionText(const std::string& _instruction);
304
306 virtual void updateViewer();
307
309 void updateDragAndDrops();
310
312 const ::osg::ref_ptr<::osg::Group>& getRootGroup() const;
313
316 void setVerticalFieldOfView(double fov);
317
321 double getVerticalFieldOfView() const;
322
324 void setCameraMode(CameraMode mode);
325
328
329protected:
330 friend class SaveScreen;
331
333 std::size_t mImageSequenceNum;
334
336 std::size_t mImageDigits;
337
340
343
345 std::string mImageDirectory;
346
348 std::string mImagePrefix;
349
351 std::string mScreenCapName;
352
354 ::osg::ref_ptr<DefaultEventHandler> mDefaultEventHandler;
355
357 ::osg::ref_ptr<::osg::Group> mRootGroup;
358
360 ::osg::ref_ptr<::osg::Group> mLightGroup;
361
363 ::osg::ref_ptr<::osg::Light> mLight1;
364
366 ::osg::ref_ptr<::osg::LightSource> mLightSource1;
367
369 ::osg::ref_ptr<::osg::Light> mLight2;
370
372 ::osg::ref_ptr<::osg::LightSource> mLightSource2;
373
375 ::osg::Vec3 mUpwards;
376
378 ::osg::Vec3 mOver;
379
382
385
388
391 std::map<::osg::ref_ptr<WorldNode>, bool> mWorldNodes;
392
393 std::unordered_set<ViewerAttachment*> mAttachments;
394
396 std::string mInstructions;
397
398 // TODO(MXG): Consolidate all these maps into a single map of maps which uses
399 // typeid as a key
400
402 std::map<dart::dynamics::SimpleFrame*, SimpleFrameDnD*> mSimpleFrameDnDMap;
403
407 std::multimap<dart::dynamics::Shape*, SimpleFrameShapeDnD*>
409
411 std::map<InteractiveFrame*, InteractiveFrameDnD*> mInteractiveFrameDnDMap;
412
414 std::map<dart::dynamics::BodyNode*, BodyNodeDnD*> mBodyNodeDnDMap;
415
416private:
418 ::osg::ref_ptr<detail::CameraModeCallback> mCameraModeCallback;
419};
420
422
423} // namespace osg
424} // namespace gui
425} // namespace dart
426
427#endif // DART_GUI_OSG_VIEWER_HPP_
#define DART_DECLARE_CLASS_WITH_VIRTUAL_BASE_END
Definition ClassWithVirtualBase.hpp:44
#define DART_DECLARE_CLASS_WITH_VIRTUAL_BASE_BEGIN
Definition ClassWithVirtualBase.hpp:43
std::size_t index
Definition SkelParser.cpp:1673
The Subject class is a base class for any object that wants to report when it gets destroyed.
Definition Subject.hpp:58
BodyNode class represents a single node of the skeleton.
Definition BodyNode.hpp:80
Entity class is a base class for any objects that exist in the kinematic tree structure of DART.
Definition Entity.hpp:62
Definition Shape.hpp:56
The SimpleFrame class offers a user-friendly way of creating arbitrary Frames within the kinematic tr...
Definition SimpleFrame.hpp:52
Definition DragAndDrop.hpp:266
Definition DefaultEventHandler.hpp:105
DragAndDrop is a class that facilitates enabling various kinds of dart Entities to be dragged and dro...
Definition DragAndDrop.hpp:61
Definition DragAndDrop.hpp:236
Definition InteractiveFrame.hpp:110
Definition Viewer.cpp:69
SimpleFrameDnD is a DragAndDrop implementation for SimpleFrame objects.
Definition DragAndDrop.hpp:178
SimpleFrameShapeDnD is a version of SimpleFrameDnD that allows a specific Shape within the SimpleFram...
Definition DragAndDrop.hpp:208
Definition Viewer.hpp:94
ViewerAttachment()
Default constructor.
Definition Viewer.cpp:154
Viewer * getViewer()
Definition Viewer.cpp:167
virtual void attach(Viewer *newViewer)
This function will get called when the visual is attached to a new Viewer.
Definition Viewer.cpp:185
Viewer * mViewer
Definition Viewer.hpp:125
virtual ~ViewerAttachment()
Virtual destructor.
Definition Viewer.cpp:160
virtual void customAttach(Viewer *newViewer)
This function will be called by attach(Viewer*) so you can do customized setup when the Viewer change...
Definition Viewer.cpp:179
virtual void refresh()=0
This function will get called each time the Viewer is refreshed.
Definition Viewer.hpp:129
void setUpwardsDirection(const ::osg::Vec3 &_up)
Set the direction that this Viewer should consider to be upwards (default is <0,0,...
Definition Viewer.cpp:524
bool isSimulating() const
Return true iff this Viewer is currently set to simulate.
Definition Viewer.cpp:585
void removeWorldNode(WorldNode *_oldWorldNode)
Remove a WorldNode from this Viewer.
Definition Viewer.cpp:401
void simulate(bool _on)
Set all currently active WorldNodes to simulate _on.
Definition Viewer.cpp:569
void addWorldNode(WorldNode *_newWorldNode, bool _active=true)
Add a WorldNode to this Viewer.
Definition Viewer.cpp:382
virtual void switchHeadlights(bool _on)
Pass in true to turn headlights on, false to turn headlights off.
Definition Viewer.cpp:323
CameraMode getCameraMode() const
Returns the camera mode of the primary camera.
Definition Viewer.cpp:933
bool mSimulating
True iff this Viewer is currently simulating.
Definition Viewer.hpp:381
WorldNode * getWorldNode(std::shared_ptr< dart::simulation::World > _world) const
Get the WorldNode associated with the given _world.
Definition Viewer.cpp:424
std::string mImageDirectory
Directory for saving images.
Definition Viewer.hpp:345
std::map< InteractiveFrame *, InteractiveFrameDnD * > mInteractiveFrameDnDMap
Map from InteractiveFrame ptrs to InteractiveFrameDnD ptrs.
Definition Viewer.hpp:411
DragAndDrop * enableDragAndDrop(dart::dynamics::Entity *_entity)
Returns a nullptr if _entity is not a type that can support the built-in drag and drop features,...
Definition Viewer.cpp:606
const std::string & getInstructions() const
Get a string containing the user interface constructions for this Viewer.
Definition Viewer.cpp:813
void addAttachment(ViewerAttachment *_attachment)
Add an attachment to this Viewer.
Definition Viewer.cpp:444
void record(const std::string &directory, const std::string &prefix="image", bool restart=false, std::size_t digits=6)
As the screen refreshes, save screen capture images to the specified directory.
Definition Viewer.cpp:262
bool isAllowingSimulation() const
Return true iff this Viewer is currently allowing simulation to happen.
Definition Viewer.cpp:600
std::string mInstructions
string of instructions for this Viewer
Definition Viewer.hpp:396
std::string mScreenCapName
Name for the next screen capture.
Definition Viewer.hpp:351
::osg::ref_ptr< DefaultEventHandler > mDefaultEventHandler
Default WorldNodeEventHandler for this dart::gui::osg::Viewer.
Definition Viewer.hpp:354
const ::osg::ref_ptr<::osg::LightSource > & getLightSource(std::size_t index=0) const
Get one of the LightSources of this Viewer index either 0 or 1 Useful for shadowing techniques.
Definition Viewer.cpp:487
::osg::ref_ptr<::osg::Light > mLight2
Non-headlights Light #2.
Definition Viewer.hpp:369
::osg::ref_ptr< detail::CameraModeCallback > mCameraModeCallback
Callback to control the camera mode.
Definition Viewer.hpp:418
void setVerticalFieldOfView(double fov)
Sets the vertical field of view of the master camera of the view.
Definition Viewer.cpp:865
void allowSimulation(bool _allow)
Prevent simulation from starting, even if simulate(true) is called.
Definition Viewer.cpp:591
virtual void updateViewer()
Called automatically at the beginning of each render cycle.
Definition Viewer.cpp:825
std::size_t mImageSequenceNum
Current number of the image sequence for screen recording.
Definition Viewer.hpp:333
std::map< dart::dynamics::SimpleFrame *, SimpleFrameDnD * > mSimpleFrameDnDMap
Map from SimpleFrame ptrs to SimpleFrameDnD ptrs.
Definition Viewer.hpp:402
::osg::Vec3 mOver
Vector pointing to the side.
Definition Viewer.hpp:378
::osg::Vec3 mUpwards
Vector pointing upwards.
Definition Viewer.hpp:375
void updateDragAndDrops()
Called automatically by updateViewer()
Definition Viewer.cpp:831
::osg::ref_ptr<::osg::Light > mLight1
Non-headlights Light #1.
Definition Viewer.hpp:363
bool disableDragAndDrop(DragAndDrop *_dnd)
Delete a DragAndDrop object.
Definition Viewer.cpp:724
void setupDefaultLights()
Set up the default lighting scheme.
Definition Viewer.cpp:497
bool mHeadlights
True iff headlights were last set to be on.
Definition Viewer.hpp:387
std::map< dart::dynamics::BodyNode *, BodyNodeDnD * > mBodyNodeDnDMap
Map from BodyNode ptrs to BodyNodeDnD ptrs.
Definition Viewer.hpp:414
::osg::Group * getLightGroup()
Get the Group node that contains the LightSources for this Viewer.
Definition Viewer.cpp:475
::osg::ref_ptr<::osg::LightSource > mLightSource2
Non-headlights LightSource #2.
Definition Viewer.hpp:372
void captureScreen(const std::string &filename)
Capture the current screen in a png file.
Definition Viewer.cpp:245
void addInstructionText(const std::string &_instruction)
Add something to the instructions for this Viewer.
Definition Viewer.cpp:819
::osg::ref_ptr<::osg::Group > mLightGroup
The Group Node containing light sources.
Definition Viewer.hpp:360
virtual ~Viewer()
Destructor.
Definition Viewer.cpp:235
void pauseRecording()
If the Viewer is recording, then pause the recording.
Definition Viewer.cpp:292
void setWorldNodeActive(WorldNode *_node, bool _active=true)
Set the given WorldNode to active.
Definition Viewer.cpp:552
std::unordered_set< ViewerAttachment * > mAttachments
Definition Viewer.hpp:393
std::map<::osg::ref_ptr< WorldNode >, bool > mWorldNodes
Map of WorldNodes in this dart::gui::osg::Viewer.
Definition Viewer.hpp:391
double getVerticalFieldOfView() const
Returns the vertical field of view of the master camera of the view.
Definition Viewer.cpp:896
::osg::ref_ptr<::osg::Group > mRootGroup
The root node of this Viewer.
Definition Viewer.hpp:357
virtual void switchDefaultEventHandler(bool _on)
Creates the default event handler for this dart::gui::osg::Viewer.
Definition Viewer.cpp:309
std::string mImagePrefix
Prefix to apply to images.
Definition Viewer.hpp:348
const ::osg::ref_ptr<::osg::Group > & getRootGroup() const
Get the root ::osg::Group of this Viewer.
Definition Viewer.cpp:859
std::multimap< dart::dynamics::Shape *, SimpleFrameShapeDnD * > mSimpleFrameShapeDnDMap
Multimap from Shape ptrs to SimpleFrameShapeDnD ptrs.
Definition Viewer.hpp:408
bool isRecording() const
Returns true if the Viewer is currently recording.
Definition Viewer.cpp:303
::osg::ref_ptr<::osg::LightSource > mLightSource1
Non-headlights LightSource #1.
Definition Viewer.hpp:366
DefaultEventHandler * getDefaultEventHandler() const
Return a pointer to the default event handler.
Definition Viewer.cpp:317
void removeAttachment(ViewerAttachment *_attachment)
Remove the attachment from this Viewer.
Definition Viewer.cpp:456
std::size_t mImageDigits
Number of digits to use when saving an image sequence.
Definition Viewer.hpp:336
bool mAllowSimulation
True iff this Viewer is allowing simulation.
Definition Viewer.hpp:384
bool mScreenCapture
Whether or not the Viewer is staged for a screen capture.
Definition Viewer.hpp:342
bool mRecording
Whether or not the Viewer is currently recording.
Definition Viewer.hpp:339
const std::unordered_set< ViewerAttachment * > & getAttachments() const
Get the set of attachments in this Viewer.
Definition Viewer.cpp:469
void setCameraMode(CameraMode mode)
Sets the camera mode of the primary camera.
Definition Viewer.cpp:927
bool checkHeadlights() const
True iff headlights are currently set to true.
Definition Viewer.cpp:376
WorldNode class encapsulates a World to be displayed in OpenSceneGraph.
Definition WorldNode.hpp:66
class World
Definition World.hpp:83
CameraMode
Camera mode.
Definition Viewer.hpp:80
@ DEPTH
To render the depth buffer.
@ RGBA
To render the RGBA color.
std::string toString(CameraMode mode)
Definition Viewer.cpp:55
Definition BulletCollisionDetector.cpp:60