iMSTK
Interactive Medical Simulation Toolkit
imstkGeometry.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkEventObject.h"
10 #include "imstkMacros.h"
11 #include "imstkMath.h"
12 
13 #include <unordered_map>
14 #include <string>
15 
16 namespace imstk
17 {
22 class Geometry : public EventObject
23 {
24 public:
30  enum class TransformType
31  {
32  ApplyToData,
33  ConcatenateToTransform
34  };
35 
41  enum class DataType
42  {
43  PreTransform,
44  PostTransform
45  };
46 
47 protected:
49 
50 public:
51  Geometry(const Geometry& other)
52  {
53  m_transformApplied = other.m_transformApplied;
54  m_transform = other.m_transform;
56  }
57 
58  ~Geometry() override = default;
59 
60  void operator=(const Geometry& other)
61  {
62  m_transformApplied = other.m_transformApplied;
63  m_transform = other.m_transform;
65  }
66 
71  virtual const std::string getTypeName() const = 0;
72 
73  // *INDENT-OFF*
74  SIGNAL(Geometry, modified);
75  // *INDENT-ON*
76 
80  virtual void print() const;
81 
85  virtual double getVolume() { return 0.0; };
86 
91  virtual void computeBoundingBox(Vec3d& lowerCorner, Vec3d& upperCorner, const double paddingPercent = 0.0);
92 
96  virtual Vec3d getCenter()
97  {
98  Vec3d min, max;
99  computeBoundingBox(min, max);
100  return (min + max) * 0.5;
101  }
102 
106  void translate(const Vec3d& t, TransformType type = TransformType::ConcatenateToTransform);
107  void translate(double x, double y, double z, TransformType type = TransformType::ConcatenateToTransform);
109 
113  void rotate(const Quatd& q, TransformType type = TransformType::ConcatenateToTransform);
114  void rotate(const Mat3d& m, TransformType type = TransformType::ConcatenateToTransform);
115  void rotate(const Vec3d& axis, double radians, TransformType type = TransformType::ConcatenateToTransform);
117 
121  void scale(const Vec3d& scaling, TransformType type = TransformType::ConcatenateToTransform);
122  void scale(const double scaling, TransformType type = TransformType::ConcatenateToTransform);
124 
128  void transform(const Mat4d& T, TransformType type = TransformType::ConcatenateToTransform);
129 
133  Vec3d getTranslation() const;
134  void setTranslation(const Vec3d& t);
135  void setTranslation(const double x, const double y, const double z);
137 
141  Mat3d getRotation() const;
142  void setRotation(const Mat3d& m);
143  void setRotation(const Quatd& q);
144  void setRotation(const Vec3d& axis, const double angle);
146 
150  Vec3d getScaling() const;
151  void setScaling(const Vec3d& s);
152  void setScaling(const double s);
154 
158  const Mat4d& getTransform() const { return m_transform; }
159  void setTransform(const Mat4d& m)
160  {
161  m_transform = m;
162  m_transformApplied = false;
163  }
164 
166 
171  std::unique_ptr<Geometry> clone()
172  {
173  return std::unique_ptr<Geometry>(cloneImplementation());
174  }
175 
179  const std::string& getName() const { return m_name; }
180  void setName(const std::string& name) { m_name = name; }
181 
185  size_t getGlobalId() const { return m_globalId; }
186 
190  static size_t getNumGlobalIds() { return s_numGlobalIds; }
191 
195  virtual bool isMesh() const { return false; }
196 
201  {
202  m_boundsDirty = true;
203  this->postEvent(Event(Geometry::modified()));
204  }
205 
206  virtual void updatePostTransformData() const { }
207 
208 protected:
212  virtual void applyTransform(const Mat4d&) { }
213 
217  static size_t getUniqueId()
218  {
219  const size_t idx = s_numGlobalIds;
220  s_numGlobalIds++;
221  return idx;
222  }
223 
224 protected:
225  mutable bool m_transformApplied = true; // Internally used for lazy evaluation
226  mutable bool m_boundsDirty = true;
227 
228  Mat4d m_transform = Mat4d::Identity();
229 
230  size_t m_globalId;
231  std::string m_name = "unnamed";
232 
234  static std::atomic<size_t> s_numGlobalIds;
235 
236 private:
237  virtual Geometry* cloneImplementation() const = 0;
238 };
239 } // namespace imstk
Mat4d m_transform
Transformation matrix.
static std::atomic< size_t > s_numGlobalIds
Total number of geometries that have been created in this program.
virtual void print() const
Print.
Vec3d getTranslation() const
Get/Set translation.
std::unique_ptr< Geometry > clone()
polymorphic clone function, utilize this to get a copy of the geometry without casting to the expecte...
virtual bool isMesh() const
Returns true if the geometry is a mesh, else returns false.
static size_t getUniqueId()
Get a unique ID for the new generated geometry object.
Base class for events which contain a type, priority, and data priority defaults to 0 and uses a grea...
void rotate(const Quatd &q, TransformType type=TransformType::ConcatenateToTransform)
Rotate the geometry in Cartesian space.
virtual const std::string getTypeName() const =0
Returns the string representing the type name of the geometry.
virtual void computeBoundingBox(Vec3d &lowerCorner, Vec3d &upperCorner, const double paddingPercent=0.0)
Compute the bounding box for the geometry.
const Mat4d & getTransform() const
Get/Set the transform.
Compound Geometry.
Vec3d getScaling() const
Get/Set scaling.
void transform(const Mat4d &T, TransformType type=TransformType::ConcatenateToTransform)
Applies a rigid transform to the geometry.
static size_t getNumGlobalIds()
Get number of ids/geometries.
size_t m_globalId
Unique ID assigned to each geometry upon construction.
void postModified()
Post modified event.
Mat3d getRotation() const
Get/Set rotation.
void scale(const Vec3d &scaling, TransformType type=TransformType::ConcatenateToTransform)
Scale in Cartesian directions.
virtual Vec3d getCenter()
Returns the bounding box center.
Definition: imstkGeometry.h:96
virtual double getVolume()
Returns the volume of the geometry (if valid)
Definition: imstkGeometry.h:85
Base class for any geometrical representation.
Definition: imstkGeometry.h:22
const std::string & getName() const
Get the name of the geometry.
EventObject is the base class for all objects in iMSTK that can receive and emit events. It supports direct and queued observer functions. Direct observers receive events immediately on the same thread This can either be posted on an object or be a function pointer Queued observers receive events within their queue which they can process whenever they like. These can be connected with the connect/queuedConnect/disconnect functions Lambda recievers cannot be disconnected unless all receivers to a signal are removed.
void translate(const Vec3d &t, TransformType type=TransformType::ConcatenateToTransform)
Translate the geometry in Cartesian space.
size_t getGlobalId() const
Get the global (unique) index of the geometry.
void postEvent(const T &e)
Emits the event Direct observers will be immediately called, in sync Queued observers will receive th...
TransformType
Enumeration for the transformation to apply ApplyToTransform to apply the transformation to the data...
Definition: imstkGeometry.h:30
DataType
Enumeration for the data to retrieve PreTransform for data where transform matrix is not applied Po...
Definition: imstkGeometry.h:41
virtual void applyTransform(const Mat4d &)
Directly apply transform to data.