dart
DragAndDrop.hpp
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_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 
60 {
61 public:
62  enum class RotationOption : int
63  {
64 
65  HOLD_MODKEY = 0, // Default setting, hold ctrl key to rotate
66  ALWAYS_ON,
67  ALWAYS_OFF
68 
69  };
70 
72  DragAndDrop(Viewer* viewer, dart::dynamics::Entity* entity);
73 
75  virtual ~DragAndDrop();
76 
78  dart::dynamics::Entity* getEntity() const;
79 
81  virtual void update();
82 
85  void setObstructable(bool _obstructable);
86 
90  bool isObstructable() const;
91 
93  virtual void move() = 0;
94 
97  virtual void saveState() = 0;
98 
101  virtual void release();
102 
104  virtual Eigen::Vector3d getConstrainedDx() const;
105 
107  virtual Eigen::AngleAxisd getConstrainedRotation() const;
108 
110  void unconstrain();
111 
115  void constrainToLine(const Eigen::Vector3d& slope);
116 
120  void constrainToPlane(const Eigen::Vector3d& normal);
121 
123  bool isMoving() const;
124 
126  void setRotationOption(RotationOption option);
127 
129  RotationOption getRotationOption() const;
130 
132  void setRotationModKey(::osgGA::GUIEventAdapter::ModKeyMask rotationModKey);
133 
135  ::osgGA::GUIEventAdapter::ModKeyMask getRotationModKey() const;
136 
137 protected:
139  void handleDestructionNotification(
140  const dart::common::Subject* subscription) override;
141 
144 
147 
149  Eigen::Vector3d mPickedPosition;
150 
153  Eigen::Vector3d mVector;
154 
156  Eigen::Vector3d mPivot;
157 
159  ConstraintType mConstraintType;
160 
163 
165  bool mAmMoving;
166 
168  RotationOption mRotationOption;
169 
171  ::osgGA::GUIEventAdapter::ModKeyMask mRotationModKey;
172 };
173 
174 //==============================================================================
177 {
178 public:
181 
183  virtual ~SimpleFrameDnD() = default;
184 
186  dart::dynamics::SimpleFrame* getSimpleFrame() const;
187 
188  // Documentation inherited
189  void move() override;
190 
191  // Documentation inherited
192  void saveState() override;
193 
194 protected:
197 
199  Eigen::AngleAxisd mSavedRotation;
200 };
201 
202 //==============================================================================
207 {
208 public:
211  Viewer* viewer,
213  dart::dynamics::Shape* shape);
214 
216  virtual ~SimpleFrameShapeDnD() = default;
217 
219  dart::dynamics::Shape* getShape() const;
220 
221  // Documentation inherited
222  void update() override;
223 
224 protected:
225  // Documentation inherited
226  void handleDestructionNotification(
227  const dart::common::Subject* subscription) override;
228 
231 };
232 
233 //==============================================================================
235 {
236 public:
239 
241  virtual ~InteractiveFrameDnD() = default;
242 
244  InteractiveFrame* getFrame() const;
245 
246  // Documentation inherited
247  void update() override;
248 
249  // Documentation inherited
250  void move() override;
251 
252  // Documentation inherited
253  void saveState() override;
254 
255 protected:
257  std::vector<DragAndDrop*> mDnDs;
258 
261 };
262 
263 //==============================================================================
264 class BodyNodeDnD : public DragAndDrop
265 {
266 public:
268  BodyNodeDnD(
269  Viewer* viewer,
271  bool useExternalIK = true,
272  bool useWholeBody = false);
273 
275  virtual ~BodyNodeDnD() = default;
276 
278  dart::dynamics::BodyNode* getBodyNode() const;
279 
280  // Documentation inherited
281  void update() override;
282 
283  // Documentation inherited
284  void move() override;
285 
286  // Documentation inherited
287  void saveState() override;
288 
289  // Documentation inherited
290  void release() override;
291 
296  void useExternalIK(bool external);
297 
299  bool isUsingExternalIK() const;
300 
302  void useWholeBody(bool wholeBody);
303 
305  bool isUsingWholeBody() const;
306 
308  void setPreserveOrientationModKey(
309  ::osgGA::GUIEventAdapter::ModKeyMask modkey);
310 
312  ::osgGA::GUIEventAdapter::ModKeyMask getPreserveOrientationModKey() const;
313 
315  void setJointRestrictionModKey(::osgGA::GUIEventAdapter::ModKeyMask modkey);
316 
318  ::osgGA::GUIEventAdapter::ModKeyMask getJointRestrictionModKey() const;
319 
320 protected:
322  dart::dynamics::WeakBodyNodePtr mBodyNode;
323 
325  dart::dynamics::InverseKinematicsPtr mIK;
326 
329  Eigen::Vector3d mSavedGlobalOffset;
330 
333  Eigen::Vector3d mSavedLocalOffset;
334 
336  Eigen::AngleAxisd mSavedRotation;
337 
340 
343 
345  ::osgGA::GUIEventAdapter::ModKeyMask mPreserveOrientationModKey;
346 
348  ::osgGA::GUIEventAdapter::ModKeyMask mJointRestrictionModKey;
349 
351  std::size_t mAdditionalBodyNodes;
352 };
353 
354 } // namespace osg
355 } // namespace gui
356 } // namespace dart
357 
358 #endif // DART_GUI_OSG_DRAGANDDROP_HPP_
Eigen::Vector3d mVector
Reference vector for constraint (slope for line constraint, or normal for plane constraint) ...
Definition: DragAndDrop.hpp:153
The Subject class is a base class for any object that wants to report when it gets destroyed...
Definition: Subject.hpp:57
DragAndDrop is a class that facilitates enabling various kinds of dart Entities to be dragged and dro...
Definition: DragAndDrop.hpp:59
dart::dynamics::Shape * mShape
Shape associated with this DnD.
Definition: DragAndDrop.hpp:230
Eigen::AngleAxisd mSavedRotation
The saved rotation of the frame.
Definition: DragAndDrop.hpp:199
bool mUseExternalIK
Whether to use an external IK.
Definition: DragAndDrop.hpp:339
Eigen::AngleAxisd mSavedRotation
The rotation of the BodyNode.
Definition: DragAndDrop.hpp:336
Definition: InteractiveFrame.hpp:109
bool mAmMoving
Whether this DnD is currently moving.
Definition: DragAndDrop.hpp:165
::osgGA::GUIEventAdapter::ModKeyMask mPreserveOrientationModKey
Key for preserving orientation.
Definition: DragAndDrop.hpp:345
Entity class is a base class for any objects that exist in the kinematic tree structure of DART...
Definition: Entity.hpp:60
::osgGA::GUIEventAdapter::ModKeyMask mJointRestrictionModKey
Key for restricting joint usage.
Definition: DragAndDrop.hpp:348
Definition: test_Signal.cpp:68
std::vector< DragAndDrop * > mDnDs
Vector of DnD components that are united under this DnD.
Definition: DragAndDrop.hpp:257
Definition: Aspect.cpp:40
Definition: DragAndDrop.hpp:234
ConstraintType mConstraintType
Constraint type for this DnD.
Definition: DragAndDrop.hpp:159
Definition: DragAndDrop.hpp:264
dart::dynamics::InverseKinematicsPtr mIK
The IK module being used by this DnD.
Definition: DragAndDrop.hpp:325
Definition: Shape.hpp:52
Viewer * mViewer
Pointer to the DnD&#39;s Viewer.
Definition: DragAndDrop.hpp:143
BodyNode class represents a single node of the skeleton.
Definition: BodyNode.hpp:74
std::size_t mAdditionalBodyNodes
Currently unused, but this will change in the future.
Definition: DragAndDrop.hpp:351
Eigen::Vector3d mPivot
Point in space about which rotations should happen.
Definition: DragAndDrop.hpp:156
dart::dynamics::Entity * mEntity
Pointer to the DnD&#39;s Entity.
Definition: DragAndDrop.hpp:146
RotationOption mRotationOption
Option for rotation settings.
Definition: DragAndDrop.hpp:168
bool mAmObstructable
Whether other objects can block this one from being picked.
Definition: DragAndDrop.hpp:162
bool mUseWholeBody
Whether to use the whole body.
Definition: DragAndDrop.hpp:342
dart::dynamics::SimpleFrame * mFrame
SimpleFrame pointer.
Definition: DragAndDrop.hpp:196
SimpleFrameShapeDnD is a version of SimpleFrameDnD that allows a specific Shape within the SimpleFram...
Definition: DragAndDrop.hpp:206
InteractiveFrame * mInteractiveFrame
Pointer to the interactive frame.
Definition: DragAndDrop.hpp:260
::osgGA::GUIEventAdapter::ModKeyMask mRotationModKey
Modkey for rotation.
Definition: DragAndDrop.hpp:171
Eigen::Vector3d mPickedPosition
The location in the world that was picked by the user.
Definition: DragAndDrop.hpp:149
dart::dynamics::WeakBodyNodePtr mBodyNode
The BodyNode associated with this DnD.
Definition: DragAndDrop.hpp:322
Definition: Viewer.hpp:111
Eigen::Vector3d mSavedGlobalOffset
The offset of the pick from the BodyNode origin, expressed in global coordinates. ...
Definition: DragAndDrop.hpp:329
SimpleFrameDnD is a DragAndDrop implementation for SimpleFrame objects.
Definition: DragAndDrop.hpp:176
The Observer class should be inherited by any class that wants to respond in a customized way to the ...
Definition: Observer.hpp:51
Eigen::Vector3d mSavedLocalOffset
The offset of the pick from the BodyNode origin, expressed in local coordinates.
Definition: DragAndDrop.hpp:333
The SimpleFrame class offers a user-friendly way of creating arbitrary Frames within the kinematic tr...
Definition: SimpleFrame.hpp:51