DART  6.10.1
Viewer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2021, 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 <osgShadow/ShadowTechnique>
41 #include <osgViewer/Viewer>
42 
43 #include <Eigen/Core>
44 
46 #include "dart/common/Subject.hpp"
47 
48 namespace dart {
49 
50 namespace simulation {
51 class World;
52 } // namespace simulation
53 
54 namespace dynamics {
55 class Entity;
56 class SimpleFrame;
57 class Shape;
58 class BodyNode;
59 } // namespace dynamics
60 
61 namespace gui {
62 namespace osg {
63 
64 class WorldNode;
65 class DefaultEventHandler;
66 class DragAndDrop;
67 class SimpleFrameDnD;
68 class SimpleFrameShapeDnD;
69 class InteractiveFrame;
70 class InteractiveFrameDnD;
71 class BodyNodeDnD;
72 class Viewer;
73 class SaveScreen;
74 
76 class ViewerAttachment : public virtual ::osg::Group
77 {
78 public:
79  friend class Viewer;
80 
83 
85  virtual ~ViewerAttachment();
86 
89  virtual void refresh() = 0;
90 
91  Viewer* getViewer();
92 
93  const Viewer* getViewer() const;
94 
95 protected:
99  virtual void customAttach(Viewer* newViewer);
100 
105  virtual void attach(Viewer* newViewer);
106 
107 private:
109 };
110 
111 class Viewer : public osgViewer::Viewer, public dart::common::Subject
112 {
113 public:
116  Viewer(const ::osg::Vec4& clearColor = ::osg::Vec4(0.9, 0.9, 0.9, 1.0));
117 
119  virtual ~Viewer();
120 
127  // TODO(MXG): Add a bool argument that instructs the viewer to append a date
128  // and time to the name of the file.
129  void captureScreen(const std::string& filename);
130 
141  void record(
142  const std::string& directory,
143  const std::string& prefix = "image",
144  bool restart = false,
145  std::size_t digits = 6);
146 
150  void pauseRecording();
151 
153  bool isRecording() const;
154 
156  virtual void switchDefaultEventHandler(bool _on);
157 
160 
162  virtual void switchHeadlights(bool _on);
163 
165  bool checkHeadlights() const;
166 
170  void addWorldNode(WorldNode* _newWorldNode, bool _active = true);
171 
173  void removeWorldNode(WorldNode* _oldWorldNode);
174 
176  void removeWorldNode(std::shared_ptr<dart::simulation::World> _oldWorld);
177 
181  std::shared_ptr<dart::simulation::World> _world) const;
182 
185  void addAttachment(ViewerAttachment* _attachment);
186 
188  void removeAttachment(ViewerAttachment* _attachment);
189 
191  const std::unordered_set<ViewerAttachment*>& getAttachments() const;
192 
194  ::osg::Group* getLightGroup();
195 
197  const ::osg::Group* getLightGroup() const;
198 
202  const ::osg::ref_ptr<::osg::LightSource>& getLightSource(
203  std::size_t index = 0) const;
204 
206  void setupDefaultLights();
207 
210  void setUpwardsDirection(const ::osg::Vec3& _up);
211 
214  void setUpwardsDirection(const Eigen::Vector3d& _up);
215 
217  void setWorldNodeActive(WorldNode* _node, bool _active = true);
218 
220  void setWorldNodeActive(
221  std::shared_ptr<dart::simulation::World> _world, bool _active = true);
222 
224  void simulate(bool _on);
225 
227  bool isSimulating() const;
228 
230  void allowSimulation(bool _allow);
231 
233  bool isAllowingSimulation() const;
234 
241 
244 
250 
253 
257  bool _useExternalIK = true,
258  bool _useWholeBody = false);
259 
262  bool disableDragAndDrop(DragAndDrop* _dnd);
263 
267 
271 
275 
278  bool disableDragAndDrop(BodyNodeDnD* _dnd);
279 
281  const std::string& getInstructions() const;
282 
286  void addInstructionText(const std::string& _instruction);
287 
289  virtual void updateViewer();
290 
292  void updateDragAndDrops();
293 
295  const ::osg::ref_ptr<::osg::Group>& getRootGroup() const;
296 
299  void setVerticalFieldOfView(double fov);
300 
304  double getVerticalFieldOfView() const;
305 
306 protected:
307  friend class SaveScreen;
308 
310  std::size_t mImageSequenceNum;
311 
313  std::size_t mImageDigits;
314 
317 
320 
322  std::string mImageDirectory;
323 
325  std::string mImagePrefix;
326 
328  std::string mScreenCapName;
329 
331  ::osg::ref_ptr<DefaultEventHandler> mDefaultEventHandler;
332 
334  ::osg::ref_ptr<::osg::Group> mRootGroup;
335 
337  ::osg::ref_ptr<::osg::Group> mLightGroup;
338 
340  ::osg::ref_ptr<::osg::Light> mLight1;
341 
343  ::osg::ref_ptr<::osg::LightSource> mLightSource1;
344 
346  ::osg::ref_ptr<::osg::Light> mLight2;
347 
349  ::osg::ref_ptr<::osg::LightSource> mLightSource2;
350 
352  ::osg::Vec3 mUpwards;
353 
355  ::osg::Vec3 mOver;
356 
359 
362 
365 
368  std::map<::osg::ref_ptr<WorldNode>, bool> mWorldNodes;
369 
370  std::unordered_set<ViewerAttachment*> mAttachments;
371 
373  std::string mInstructions;
374 
375  // TODO(MXG): Consolidate all these maps into a single map of maps which uses
376  // typeid as a key
377 
379  std::map<dart::dynamics::SimpleFrame*, SimpleFrameDnD*> mSimpleFrameDnDMap;
380 
384  std::multimap<dart::dynamics::Shape*, SimpleFrameShapeDnD*>
386 
388  std::map<InteractiveFrame*, InteractiveFrameDnD*> mInteractiveFrameDnDMap;
389 
391  std::map<dart::dynamics::BodyNode*, BodyNodeDnD*> mBodyNodeDnDMap;
392 };
394 
395 } // namespace osg
396 } // namespace gui
397 } // namespace dart
398 
399 #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:1672
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:79
Entity class is a base class for any objects that exist in the kinematic tree structure of DART.
Definition: Entity.hpp:61
Definition: Shape.hpp:54
The SimpleFrame class offers a user-friendly way of creating arbitrary Frames within the kinematic tr...
Definition: SimpleFrame.hpp:52
Definition: DragAndDrop.hpp:265
Definition: DefaultEventHandler.hpp:106
DragAndDrop is a class that facilitates enabling various kinds of dart Entities to be dragged and dro...
Definition: DragAndDrop.hpp:60
Definition: DragAndDrop.hpp:235
Definition: InteractiveFrame.hpp:110
Definition: Viewer.cpp:56
SimpleFrameDnD is a DragAndDrop implementation for SimpleFrame objects.
Definition: DragAndDrop.hpp:177
SimpleFrameShapeDnD is a version of SimpleFrameDnD that allows a specific Shape within the SimpleFram...
Definition: DragAndDrop.hpp:207
Definition: Viewer.hpp:77
ViewerAttachment()
Default constructor.
Definition: Viewer.cpp:141
Viewer * getViewer()
Definition: Viewer.cpp:154
virtual void attach(Viewer *newViewer)
This function will get called when the visual is attached to a new Viewer.
Definition: Viewer.cpp:172
Viewer * mViewer
Definition: Viewer.hpp:108
virtual ~ViewerAttachment()
Virtual destructor.
Definition: Viewer.cpp:147
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:166
virtual void refresh()=0
This function will get called each time the Viewer is refreshed.
Definition: Viewer.hpp:112
void setUpwardsDirection(const ::osg::Vec3 &_up)
Set the direction that this Viewer should consider to be upwards (default is <0,0,...
Definition: Viewer.cpp:504
Viewer(const ::osg::Vec4 &clearColor=::osg::Vec4(0.9, 0.9, 0.9, 1.0))
Constructor for dart::gui::osg::Viewer.
Definition: Viewer.cpp:182
bool isSimulating() const
Return true iff this Viewer is currently set to simulate.
Definition: Viewer.cpp:566
void removeWorldNode(WorldNode *_oldWorldNode)
Remove a WorldNode from this Viewer.
Definition: Viewer.cpp:381
void simulate(bool _on)
Set all currently active WorldNodes to simulate _on.
Definition: Viewer.cpp:550
void addWorldNode(WorldNode *_newWorldNode, bool _active=true)
Add a WorldNode to this Viewer.
Definition: Viewer.cpp:364
virtual void switchHeadlights(bool _on)
Pass in true to turn headlights on, false to turn headlights off.
Definition: Viewer.cpp:305
bool mSimulating
True iff this Viewer is currently simulating.
Definition: Viewer.hpp:358
WorldNode * getWorldNode(std::shared_ptr< dart::simulation::World > _world) const
Get the WorldNode associated with the given _world.
Definition: Viewer.cpp:404
std::string mImageDirectory
Directory for saving images.
Definition: Viewer.hpp:322
std::map< InteractiveFrame *, InteractiveFrameDnD * > mInteractiveFrameDnDMap
Map from InteractiveFrame ptrs to InteractiveFrameDnD ptrs.
Definition: Viewer.hpp:388
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:587
const std::string & getInstructions() const
Get a string containing the user interface constructions for this Viewer.
Definition: Viewer.cpp:794
void addAttachment(ViewerAttachment *_attachment)
Add an attachment to this Viewer.
Definition: Viewer.cpp:424
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:244
bool isAllowingSimulation() const
Return true iff this Viewer is currently allowing simulation to happen.
Definition: Viewer.cpp:581
std::string mInstructions
string of instructions for this Viewer
Definition: Viewer.hpp:373
std::string mScreenCapName
Name for the next screen capture.
Definition: Viewer.hpp:328
::osg::ref_ptr< DefaultEventHandler > mDefaultEventHandler
Default WorldNodeEventHandler for this dart::gui::osg::Viewer.
Definition: Viewer.hpp:331
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:467
::osg::ref_ptr<::osg::Light > mLight2
Non-headlights Light #2.
Definition: Viewer.hpp:346
void setVerticalFieldOfView(double fov)
Sets the vertical field of view of the master camera of the view.
Definition: Viewer.cpp:846
void allowSimulation(bool _allow)
Prevent simulation from starting, even if simulate(true) is called.
Definition: Viewer.cpp:572
virtual void updateViewer()
Called automatically at the beginning of each render cycle.
Definition: Viewer.cpp:806
std::size_t mImageSequenceNum
Current number of the image sequence for screen recording.
Definition: Viewer.hpp:310
std::map< dart::dynamics::SimpleFrame *, SimpleFrameDnD * > mSimpleFrameDnDMap
Map from SimpleFrame ptrs to SimpleFrameDnD ptrs.
Definition: Viewer.hpp:379
::osg::Vec3 mOver
Vector pointing to the side.
Definition: Viewer.hpp:355
::osg::Vec3 mUpwards
Vector pointing upwards.
Definition: Viewer.hpp:352
void updateDragAndDrops()
Called automatically by updateViewer()
Definition: Viewer.cpp:812
::osg::ref_ptr<::osg::Light > mLight1
Non-headlights Light #1.
Definition: Viewer.hpp:340
bool disableDragAndDrop(DragAndDrop *_dnd)
Delete a DragAndDrop object.
Definition: Viewer.cpp:705
void setupDefaultLights()
Set up the default lighting scheme.
Definition: Viewer.cpp:477
bool mHeadlights
True iff headlights were last set to be on.
Definition: Viewer.hpp:364
std::map< dart::dynamics::BodyNode *, BodyNodeDnD * > mBodyNodeDnDMap
Map from BodyNode ptrs to BodyNodeDnD ptrs.
Definition: Viewer.hpp:391
::osg::Group * getLightGroup()
Get the Group node that contains the LightSources for this Viewer.
Definition: Viewer.cpp:455
::osg::ref_ptr<::osg::LightSource > mLightSource2
Non-headlights LightSource #2.
Definition: Viewer.hpp:349
void captureScreen(const std::string &filename)
Capture the current screen in a png file.
Definition: Viewer.cpp:227
void addInstructionText(const std::string &_instruction)
Add something to the instructions for this Viewer.
Definition: Viewer.cpp:800
::osg::ref_ptr<::osg::Group > mLightGroup
The Group Node containing light sources.
Definition: Viewer.hpp:337
virtual ~Viewer()
Destructor.
Definition: Viewer.cpp:217
void pauseRecording()
If the Viewer is recording, then pause the recording.
Definition: Viewer.cpp:274
void setWorldNodeActive(WorldNode *_node, bool _active=true)
Set the given WorldNode to active.
Definition: Viewer.cpp:533
std::unordered_set< ViewerAttachment * > mAttachments
Definition: Viewer.hpp:370
std::map<::osg::ref_ptr< WorldNode >, bool > mWorldNodes
Map of WorldNodes in this dart::gui::osg::Viewer.
Definition: Viewer.hpp:368
double getVerticalFieldOfView() const
Returns the vertical field of view of the master camera of the view.
Definition: Viewer.cpp:877
::osg::ref_ptr<::osg::Group > mRootGroup
The root node of this Viewer.
Definition: Viewer.hpp:334
virtual void switchDefaultEventHandler(bool _on)
Creates the default event handler for this dart::gui::osg::Viewer.
Definition: Viewer.cpp:291
std::string mImagePrefix
Prefix to apply to images.
Definition: Viewer.hpp:325
const ::osg::ref_ptr<::osg::Group > & getRootGroup() const
Get the root ::osg::Group of this Viewer.
Definition: Viewer.cpp:840
std::multimap< dart::dynamics::Shape *, SimpleFrameShapeDnD * > mSimpleFrameShapeDnDMap
Multimap from Shape ptrs to SimpleFrameShapeDnD ptrs.
Definition: Viewer.hpp:385
bool isRecording() const
Returns true if the Viewer is currently recording.
Definition: Viewer.cpp:285
::osg::ref_ptr<::osg::LightSource > mLightSource1
Non-headlights LightSource #1.
Definition: Viewer.hpp:343
DefaultEventHandler * getDefaultEventHandler() const
Return a pointer to the default event handler.
Definition: Viewer.cpp:299
void removeAttachment(ViewerAttachment *_attachment)
Remove the attachment from this Viewer.
Definition: Viewer.cpp:436
std::size_t mImageDigits
Number of digits to use when saving an image sequence.
Definition: Viewer.hpp:313
bool mAllowSimulation
True iff this Viewer is allowing simulation.
Definition: Viewer.hpp:361
bool mScreenCapture
Whether or not the Viewer is staged for a screen capture.
Definition: Viewer.hpp:319
bool mRecording
Whether or not the Viewer is currently recording.
Definition: Viewer.hpp:316
const std::unordered_set< ViewerAttachment * > & getAttachments() const
Get the set of attachments in this Viewer.
Definition: Viewer.cpp:449
bool checkHeadlights() const
True iff headlights are currently set to true.
Definition: Viewer.cpp:358
WorldNode class encapsulates a World to be displayed in OpenSceneGraph.
Definition: WorldNode.hpp:65
class World
Definition: World.hpp:84
Definition: BulletCollisionDetector.cpp:65