DART  6.7.3
DragAndDrop.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2019, 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_DRAGANDDROP_HPP_
34 #define DART_GUI_OSG_DRAGANDDROP_HPP_
35 
36 #include <functional>
37 
38 #include <Eigen/Geometry>
39 
40 #include "dart/common/sub_ptr.hpp"
41 #include "dart/dynamics/Entity.hpp"
42 #include "dart/dynamics/Shape.hpp"
43 #include "DefaultEventHandler.hpp"
44 
45 namespace dart {
46 
47 namespace dynamics {
48 class SimpleFrame;
49 } // namespace dynamics
50 
51 namespace gui {
52 namespace osg {
53 
54 class Viewer;
55 class InteractiveFrame;
56 
61 {
62 public:
63 
64  enum class RotationOption : int {
65 
66  HOLD_MODKEY = 0, // Default setting, hold ctrl key to rotate
67  ALWAYS_ON,
69 
70  };
71 
73  DragAndDrop(Viewer* viewer, dart::dynamics::Entity* entity);
74 
76  virtual ~DragAndDrop();
77 
80 
82  virtual void update();
83 
86  void setObstructable(bool _obstructable);
87 
91  bool isObstructable() const;
92 
94  virtual void move() = 0;
95 
98  virtual void saveState() = 0;
99 
102  virtual void release();
103 
105  virtual Eigen::Vector3d getConstrainedDx() const;
106 
108  virtual Eigen::AngleAxisd getConstrainedRotation() const;
109 
111  void unconstrain();
112 
116  void constrainToLine(const Eigen::Vector3d& slope);
117 
121  void constrainToPlane(const Eigen::Vector3d& normal);
122 
124  bool isMoving() const;
125 
128 
131 
133  void setRotationModKey(::osgGA::GUIEventAdapter::ModKeyMask rotationModKey);
134 
136  ::osgGA::GUIEventAdapter::ModKeyMask getRotationModKey() const;
137 
138 protected:
139 
142  const dart::common::Subject* subscription) override;
143 
146 
149 
151  Eigen::Vector3d mPickedPosition;
152 
155  Eigen::Vector3d mVector;
156 
158  Eigen::Vector3d mPivot;
159 
162 
165 
167  bool mAmMoving;
168 
171 
173  ::osgGA::GUIEventAdapter::ModKeyMask mRotationModKey;
174 
175 };
176 
177 //==============================================================================
180 {
181 public:
182 
185 
187  virtual ~SimpleFrameDnD() = default;
188 
191 
192  // Documentation inherited
193  void move() override;
194 
195  // Documentation inherited
196  void saveState() override;
197 
198 protected:
199 
202 
204  Eigen::AngleAxisd mSavedRotation;
205 };
206 
207 //==============================================================================
212 {
213 public:
214 
217  dart::dynamics::Shape* shape);
218 
220  virtual ~SimpleFrameShapeDnD() = default;
221 
224 
225  // Documentation inherited
226  void update() override;
227 
228 protected:
229 
230  // Documentation inherited
232  const dart::common::Subject* subscription) override;
233 
236 };
237 
238 //==============================================================================
240 {
241 public:
242 
245 
247  virtual ~InteractiveFrameDnD() = default;
248 
250  InteractiveFrame* getFrame() const;
251 
252  // Documentation inherited
253  void update() override;
254 
255  // Documentation inherited
256  void move() override;
257 
258  // Documentation inherited
259  void saveState() override;
260 
261 protected:
262 
264  std::vector<DragAndDrop*> mDnDs;
265 
268 };
269 
270 //==============================================================================
271 class BodyNodeDnD : public DragAndDrop
272 {
273 public:
274 
277  bool useExternalIK = true, bool useWholeBody = false);
278 
280  virtual ~BodyNodeDnD() = default;
281 
284 
285  // Documentation inherited
286  void update() override;
287 
288  // Documentation inherited
289  void move() override;
290 
291  // Documentation inherited
292  void saveState() override;
293 
294  // Documentation inherited
295  void release() override;
296 
301  void useExternalIK(bool external);
302 
304  bool isUsingExternalIK() const;
305 
307  void useWholeBody(bool wholeBody);
308 
310  bool isUsingWholeBody() const;
311 
313  void setPreserveOrientationModKey(::osgGA::GUIEventAdapter::ModKeyMask modkey);
314 
316  ::osgGA::GUIEventAdapter::ModKeyMask getPreserveOrientationModKey() const;
317 
319  void setJointRestrictionModKey(::osgGA::GUIEventAdapter::ModKeyMask modkey);
320 
322  ::osgGA::GUIEventAdapter::ModKeyMask getJointRestrictionModKey() const;
323 
324 protected:
325 
328 
331 
334  Eigen::Vector3d mSavedGlobalOffset;
335 
338  Eigen::Vector3d mSavedLocalOffset;
339 
341  Eigen::AngleAxisd mSavedRotation;
342 
345 
348 
350  ::osgGA::GUIEventAdapter::ModKeyMask mPreserveOrientationModKey;
351 
353  ::osgGA::GUIEventAdapter::ModKeyMask mJointRestrictionModKey;
354 
356  std::size_t mAdditionalBodyNodes;
357 };
358 
359 } // namespace osg
360 } // namespace gui
361 } // namespace dart
362 
363 #endif // DART_GUI_OSG_DRAGANDDROP_HPP_
const CollisionOption & option
Collision option of DART.
Definition: FCLCollisionDetector.cpp:154
The Observer class should be inherited by any class that wants to respond in a customized way to the ...
Definition: Observer.hpp:52
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:78
Entity class is a base class for any objects that exist in the kinematic tree structure of DART.
Definition: Entity.hpp:60
Definition: Shape.hpp:53
The SimpleFrame class offers a user-friendly way of creating arbitrary Frames within the kinematic tr...
Definition: SimpleFrame.hpp:52
TemplateInverseKinematicsPtr is a templated class that enables users to create a reference-counting I...
Definition: InverseKinematicsPtr.hpp:49
Definition: DragAndDrop.hpp:272
bool isUsingWholeBody() const
Returns true if this DnD is using the whole body.
Definition: DragAndDrop.cpp:863
void update() override
Called when mouse events are being handled.
Definition: DragAndDrop.cpp:685
dart::dynamics::BodyNode * getBodyNode() const
Get the BodyNode associated with this DnD.
Definition: DragAndDrop.cpp:679
Eigen::AngleAxisd mSavedRotation
The rotation of the BodyNode.
Definition: DragAndDrop.hpp:341
::osgGA::GUIEventAdapter::ModKeyMask getPreserveOrientationModKey() const
Get the key that will preserve the orientation of the body.
Definition: DragAndDrop.cpp:876
::osgGA::GUIEventAdapter::ModKeyMask mPreserveOrientationModKey
Key for preserving orientation.
Definition: DragAndDrop.hpp:350
void setJointRestrictionModKey(::osgGA::GUIEventAdapter::ModKeyMask modkey)
Set the key that will restrict which joints are used by the DnD.
Definition: DragAndDrop.cpp:882
std::size_t mAdditionalBodyNodes
Currently unused, but this will change in the future.
Definition: DragAndDrop.hpp:356
BodyNodeDnD(Viewer *viewer, dart::dynamics::BodyNode *bn, bool useExternalIK=true, bool useWholeBody=false)
Constructor.
Definition: DragAndDrop.cpp:666
void setPreserveOrientationModKey(::osgGA::GUIEventAdapter::ModKeyMask modkey)
Set the key that will preserve the orientation of the body.
Definition: DragAndDrop.cpp:869
void useWholeBody(bool wholeBody)
Tell this DnD to use the whole body.
Definition: DragAndDrop.cpp:857
bool isUsingExternalIK() const
Returns true if it is using an external IK.
Definition: DragAndDrop.cpp:851
::osgGA::GUIEventAdapter::ModKeyMask mJointRestrictionModKey
Key for restricting joint usage.
Definition: DragAndDrop.hpp:353
dart::dynamics::WeakBodyNodePtr mBodyNode
The BodyNode associated with this DnD.
Definition: DragAndDrop.hpp:327
bool mUseWholeBody
Whether to use the whole body.
Definition: DragAndDrop.hpp:347
void saveState() override
Called when a point gets picked, and is used to save the current state of the Entity.
Definition: DragAndDrop.cpp:805
Eigen::Vector3d mSavedLocalOffset
The offset of the pick from the BodyNode origin, expressed in local coordinates.
Definition: DragAndDrop.hpp:338
bool mUseExternalIK
Whether to use an external IK.
Definition: DragAndDrop.hpp:344
Eigen::Vector3d mSavedGlobalOffset
The offset of the pick from the BodyNode origin, expressed in global coordinates.
Definition: DragAndDrop.hpp:334
void move() override
Called to specify how the Entity should be moved.
Definition: DragAndDrop.cpp:732
virtual ~BodyNodeDnD()=default
Virtual destructor.
void release() override
Called when the user lets go of the object that they are dragging and dropping.
Definition: DragAndDrop.cpp:831
dart::dynamics::InverseKinematicsPtr mIK
The IK module being used by this DnD.
Definition: DragAndDrop.hpp:330
::osgGA::GUIEventAdapter::ModKeyMask getJointRestrictionModKey() const
Get the key that will restrict which joints are used by the DnD.
Definition: DragAndDrop.cpp:889
void useExternalIK(bool external)
Tell this DnD to use an external IK instead of the one embedded in the BodyNode.
Definition: DragAndDrop.cpp:845
DragAndDrop is a class that facilitates enabling various kinds of dart Entities to be dragged and dro...
Definition: DragAndDrop.hpp:61
Eigen::Vector3d mPivot
Point in space about which rotations should happen.
Definition: DragAndDrop.hpp:158
virtual void update()
Called when mouse events are being handled.
Definition: DragAndDrop.cpp:79
void setRotationOption(RotationOption option)
Set the option for triggering rotations instead of translations.
Definition: DragAndDrop.cpp:215
::osgGA::GUIEventAdapter::ModKeyMask mRotationModKey
Modkey for rotation.
Definition: DragAndDrop.hpp:173
dart::dynamics::Entity * getEntity() const
Get the Entity that this DragAndDrop is associated with.
Definition: DragAndDrop.cpp:73
void constrainToPlane(const Eigen::Vector3d &normal)
Constrain translation to only occur within the plane defined by the given normal, or constrain rotati...
Definition: DragAndDrop.cpp:202
Eigen::Vector3d mPickedPosition
The location in the world that was picked by the user.
Definition: DragAndDrop.hpp:151
virtual void saveState()=0
Called when a point gets picked, and is used to save the current state of the Entity.
RotationOption getRotationOption() const
Get the current rotation option for this DnD.
Definition: DragAndDrop.cpp:221
bool mAmMoving
Whether this DnD is currently moving.
Definition: DragAndDrop.hpp:167
virtual Eigen::AngleAxisd getConstrainedRotation() const
Default method for getting the rotation requested by the user.
Definition: DragAndDrop.cpp:151
virtual void move()=0
Called to specify how the Entity should be moved.
void handleDestructionNotification(const dart::common::Subject *subscription) override
Perform cleanup when the subject is destroyed.
Definition: DragAndDrop.cpp:240
::osgGA::GUIEventAdapter::ModKeyMask getRotationModKey() const
Get the modkey that will be used to toggle rotation for this DnD.
Definition: DragAndDrop.cpp:234
void setObstructable(bool _obstructable)
If true is passed in, this object will only be drag and droppable if there are no other objects betwe...
Definition: DragAndDrop.cpp:126
bool isObstructable() const
If this is true, then this object will only be drag and droppable if there are no other objects betwe...
Definition: DragAndDrop.cpp:132
bool isMoving() const
Returns the mAmMoving flag.
Definition: DragAndDrop.cpp:209
void constrainToLine(const Eigen::Vector3d &slope)
Constrain translation to only occur along the given slope, or constrain rotation to only occur about ...
Definition: DragAndDrop.cpp:195
virtual ~DragAndDrop()
Virtual destructor.
Definition: DragAndDrop.cpp:67
ConstraintType mConstraintType
Constraint type for this DnD.
Definition: DragAndDrop.hpp:161
RotationOption
Definition: DragAndDrop.hpp:64
RotationOption mRotationOption
Option for rotation settings.
Definition: DragAndDrop.hpp:170
virtual Eigen::Vector3d getConstrainedDx() const
Default method for getting the translation requested by the user.
Definition: DragAndDrop.cpp:144
Viewer * mViewer
Pointer to the DnD's Viewer.
Definition: DragAndDrop.hpp:145
virtual void release()
Called when the user lets go of the object that they are dragging and dropping.
Definition: DragAndDrop.cpp:138
void unconstrain()
Remove all constraints from the dragging and dropping.
Definition: DragAndDrop.cpp:189
Eigen::Vector3d mVector
Reference vector for constraint (slope for line constraint, or normal for plane constraint)
Definition: DragAndDrop.hpp:155
DragAndDrop(Viewer *viewer, dart::dynamics::Entity *entity)
Constructor.
Definition: DragAndDrop.cpp:52
void setRotationModKey(::osgGA::GUIEventAdapter::ModKeyMask rotationModKey)
Set the modkey that will be used to toggle rotation for this DnD.
Definition: DragAndDrop.cpp:227
dart::dynamics::Entity * mEntity
Pointer to the DnD's Entity.
Definition: DragAndDrop.hpp:148
bool mAmObstructable
Whether other objects can block this one from being picked.
Definition: DragAndDrop.hpp:164
Definition: DragAndDrop.hpp:240
void saveState() override
Called when a point gets picked, and is used to save the current state of the Entity.
Definition: DragAndDrop.cpp:660
virtual ~InteractiveFrameDnD()=default
Virtual destructor.
InteractiveFrame * getFrame() const
Get the frame associated with this DnD.
Definition: DragAndDrop.cpp:584
InteractiveFrame * mInteractiveFrame
Pointer to the interactive frame.
Definition: DragAndDrop.hpp:267
void move() override
Called to specify how the Entity should be moved.
Definition: DragAndDrop.cpp:654
std::vector< DragAndDrop * > mDnDs
Vector of DnD components that are united under this DnD.
Definition: DragAndDrop.hpp:264
void update() override
Called when mouse events are being handled.
Definition: DragAndDrop.cpp:590
InteractiveFrameDnD(Viewer *viewer, dart::gui::osg::InteractiveFrame *frame)
Constructor.
Definition: DragAndDrop.cpp:557
Definition: InteractiveFrame.hpp:112
SimpleFrameDnD is a DragAndDrop implementation for SimpleFrame objects.
Definition: DragAndDrop.hpp:180
void move() override
Called to specify how the Entity should be moved.
Definition: DragAndDrop.cpp:266
dart::dynamics::SimpleFrame * getSimpleFrame() const
Get the SimpleFrame associated with this DnD.
Definition: DragAndDrop.cpp:260
Eigen::AngleAxisd mSavedRotation
The saved rotation of the frame.
Definition: DragAndDrop.hpp:204
void saveState() override
Called when a point gets picked, and is used to save the current state of the Entity.
Definition: DragAndDrop.cpp:301
dart::dynamics::SimpleFrame * mFrame
SimpleFrame pointer.
Definition: DragAndDrop.hpp:201
virtual ~SimpleFrameDnD()=default
Virtual destructor.
SimpleFrameDnD(Viewer *viewer, dart::dynamics::SimpleFrame *frame)
Constructor.
Definition: DragAndDrop.cpp:251
SimpleFrameShapeDnD is a version of SimpleFrameDnD that allows a specific Shape within the SimpleFram...
Definition: DragAndDrop.hpp:212
SimpleFrameShapeDnD(Viewer *viewer, dart::dynamics::SimpleFrame *frame, dart::dynamics::Shape *shape)
Constructor.
Definition: DragAndDrop.cpp:308
void handleDestructionNotification(const dart::common::Subject *subscription) override
Perform cleanup when the subject is destroyed.
Definition: DragAndDrop.cpp:369
dart::dynamics::Shape * mShape
Shape associated with this DnD.
Definition: DragAndDrop.hpp:235
void update() override
Called when mouse events are being handled.
Definition: DragAndDrop.cpp:325
virtual ~SimpleFrameShapeDnD()=default
Virtual destructor.
dart::dynamics::Shape * getShape() const
Get the Shape associated with this DnD.
Definition: DragAndDrop.cpp:319
Definition: Viewer.hpp:116
ConstraintType
Definition: DefaultEventHandler.hpp:86
Definition: BulletCollisionDetector.cpp:63