dart
Shape.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_DYNAMICS_SHAPE_HPP_
34 #define DART_DYNAMICS_SHAPE_HPP_
35 
36 #include <memory>
37 
38 #include <Eigen/Dense>
39 
40 #include "dart/common/ClassWithVirtualBase.hpp"
41 #include "dart/common/Deprecated.hpp"
42 #include "dart/common/Signal.hpp"
43 #include "dart/common/Subject.hpp"
44 #include "dart/common/VersionCounter.hpp"
45 #include "dart/dynamics/SmartPointer.hpp"
46 #include "dart/math/Geometry.hpp"
47 
48 namespace dart {
49 namespace dynamics {
50 
51 DART_DECLARE_CLASS_WITH_VIRTUAL_BASE_BEGIN
52 class Shape : public virtual common::Subject,
53  public virtual common::VersionCounter
54 {
55 public:
58 
60  enum ShapeType
61  {
62  SPHERE,
63  BOX,
64  ELLIPSOID,
65  CYLINDER,
66  CAPSULE,
67  CONE,
68  PYRAMID,
69  RECTANGULAR_PYRAMID,
70  PLANE,
71  MULTISPHERE,
72  MESH,
73  SOFT_MESH,
74  LINE_SEGMENT,
75  HEIGHTMAP,
76  UNSUPPORTED
77  };
78 
82  {
83  STATIC = 0,
85  = 1 << 1,
86  DYNAMIC_PRIMITIVE = 1 << 2,
89  = 1 << 3,
91  = 1 << 4,
95  = 1 << 5,
96  DYNAMIC = 0xFF
99  };
100 
103  explicit Shape(ShapeType _type);
104 
106  Shape();
107 
109  virtual ~Shape();
110 
113  virtual const std::string& getType() const = 0;
114 
127  template <typename ShapeT>
128  bool is() const;
129 
133  const math::BoundingBox& getBoundingBox() const;
134 
136  virtual Eigen::Matrix3d computeInertia(double mass) const = 0;
137 
138  Eigen::Matrix3d computeInertiaFromDensity(double density) const;
139 
140  Eigen::Matrix3d computeInertiaFromMass(double mass) const;
141 
146  double getVolume() const;
147 
149  std::size_t getID() const;
150 
152  DART_DEPRECATED(6.1)
153  ShapeType getShapeType() const;
154 
158  void setDataVariance(unsigned int _variance);
159 
162  void addDataVariance(unsigned int _variance);
163 
166  void removeDataVariance(unsigned int _variance);
167 
169  unsigned int getDataVariance() const;
170 
172  bool checkDataVariance(DataVariance type) const;
173 
175  virtual void refreshData();
176 
178  DART_DEPRECATED(6.2)
179  virtual void notifyAlphaUpdate(double alpha);
180 
182  virtual void notifyAlphaUpdated(double alpha);
183 
185  DART_DEPRECATED(6.2)
186  virtual void notifyColorUpdate(const Eigen::Vector4d& color);
187 
189  virtual void notifyColorUpdated(const Eigen::Vector4d& color);
190 
192  std::size_t incrementVersion() override final;
193 
194 protected:
196  virtual void updateVolume() const = 0;
197 
199  virtual void updateBoundingBox() const = 0;
200 
202  mutable math::BoundingBox mBoundingBox;
203 
205  mutable bool mIsBoundingBoxDirty;
206 
208  mutable double mVolume;
209 
211  mutable bool mIsVolumeDirty;
212 
214  const std::size_t mID;
215 
217  unsigned int mVariance;
218 
220  static std::atomic_int mCounter;
221 
225 
226 private:
228  VersionChangedSignal mVersionChangedSignal;
229 
230 public:
232  common::SlotRegister<VersionChangedSignal> onVersionChanged;
233 };
234 DART_DECLARE_CLASS_WITH_VIRTUAL_BASE_END
235 
236 } // namespace dynamics
237 } // namespace dart
238 
239 #include "dart/dynamics/detail/Shape.hpp"
240 
241 #endif // DART_DYNAMICS_SHAPE_HPP_
virtual void notifyColorUpdated(const Eigen::Vector4d &color)
Notify that the color (rgba) of this shape has updated.
Definition: Shape.cpp:174
void setDataVariance(unsigned int _variance)
Set the data variance of this shape.
Definition: Shape.cpp:117
virtual void notifyAlphaUpdate(double alpha)
Notify that the alpha of this shape has updated.
Definition: Shape.cpp:156
The Subject class is a base class for any object that wants to report when it gets destroyed...
Definition: Subject.hpp:57
Vertex positions of a mesh might change (this does not include adding or removing vertices) (this enu...
Definition: Shape.hpp:94
unsigned int getDataVariance() const
Get the data variance of this shape.
Definition: Shape.cpp:135
Definition: MathTypes.hpp:47
virtual void refreshData()
Instruct this shape to update its data.
Definition: Shape.cpp:150
Definition: SharedLibraryManager.hpp:46
double getVolume() const
Returns volume of this shape.
Definition: Shape.cpp:96
VersionCounter is an interface for objects that count their versions.
Definition: VersionCounter.hpp:42
virtual void notifyColorUpdate(const Eigen::Vector4d &color)
Notify that the color (rgba) of this shape has updated.
Definition: Shape.cpp:168
virtual void updateBoundingBox() const =0
Updates bounding box.
void removeDataVariance(unsigned int _variance)
Remove a type of variance from this shape.
Definition: Shape.cpp:129
bool is() const
Get true if the types of this Shape and the template parameter (a shape class) are identical...
Definition: Shape.hpp:42
The relative transform of the Shape might change.
Definition: Shape.hpp:86
Definition: Aspect.cpp:40
common::SlotRegister< VersionChangedSignal > onVersionChanged
Use this to subscribe to version change signals.
Definition: Shape.hpp:232
unsigned int mVariance
The DataVariance of this Shape.
Definition: Shape.hpp:217
Shape()
Constructor.
Definition: Shape.cpp:57
std::size_t incrementVersion() override final
Increment the version of this Shape and notify its subscribers.
Definition: Shape.cpp:180
The primitive properties (such as x/y/z scaling) of the shape might change.
Definition: Shape.hpp:88
void addDataVariance(unsigned int _variance)
Add a type of variance to this shape.
Definition: Shape.cpp:123
math::BoundingBox mBoundingBox
The bounding box (in the local coordinate frame) of the shape.
Definition: Shape.hpp:202
virtual Eigen::Matrix3d computeInertia(double mass) const =0
Computes the inertia.
bool mIsBoundingBoxDirty
Whether bounding box needs update.
Definition: Shape.hpp:205
virtual void notifyAlphaUpdated(double alpha)
Notify that the alpha of this shape has updated.
Definition: Shape.cpp:162
virtual const std::string & getType() const =0
Returns a string representing the shape type.
Definition: Shape.hpp:52
No data will ever change.
Definition: Shape.hpp:84
The number of elements and/or arrangement of elements might change (this includes adding and removing...
Definition: Shape.hpp:98
The coloring or textures of the shape might change.
Definition: Shape.hpp:90
double mVolume
Volume enclosed by the geometry.
Definition: Shape.hpp:208
bool checkDataVariance(DataVariance type) const
True iff this Shape has the specified type of DataVariance.
Definition: Shape.cpp:141
ShapeType
Definition: Shape.hpp:60
ShapeType mType
Definition: Shape.hpp:224
ShapeType getShapeType() const
Definition: Shape.cpp:111
const std::size_t mID
Unique id.
Definition: Shape.hpp:214
bool mIsVolumeDirty
Whether volume needs update.
Definition: Shape.hpp:211
virtual void updateVolume() const =0
Updates volume.
DataVariance
DataVariance can be used by renderers to determine whether it should expect data for this shape to ch...
Definition: Shape.hpp:81
Definition: Geometry.hpp:589
const math::BoundingBox & getBoundingBox() const
Get the bounding box of the shape in its local coordinate frame.
Definition: Shape.cpp:75
virtual ~Shape()
Destructor.
Definition: Shape.cpp:69