Klamp't  0.9.0
ManagedGeometry.h
1 #ifndef KLAMPT_MODELING_MANAGED_GEOMETRY_H
2 #define KLAMPT_MODELING_MANAGED_GEOMETRY_H
3 
4 #include <KrisLibrary/geometry/AnyGeometry.h>
5 #include <KrisLibrary/GLdraw/GeometryAppearance.h>
6 #include <memory>
7 #include <map>
8 #include <string>
9 
10 namespace Klampt {
11 
12 class GeometryManager;
13 
63 {
64  public:
65  typedef std::shared_ptr<Geometry::AnyCollisionGeometry3D> GeometryPtr;
66  typedef std::shared_ptr<GLDraw::GeometryAppearance> AppearancePtr;
67 
70  ManagedGeometry(const ManagedGeometry& rhs);
71  ~ManagedGeometry();
73  void Clear();
75  bool Empty() const;
77  GeometryPtr CreateEmpty();
79  bool Load(const std::string& filename);
81  bool LoadNoCache(const std::string& filename);
84  static ManagedGeometry* IsCached(const std::string& filename);
86  bool IsCached() const;
88  bool IsOriginal() const;
90  void AddToCache(const std::string& filename);
92  const std::string& CachedFilename() const;
94  void RemoveFromCache();
97  void TransformGeometry(const Math3D::Matrix4& xform);
99  AppearancePtr Appearance() const;
102  bool IsAppearanceShared() const;
105  void SetUnique();
108  void SetUniqueAppearance();
110  void OnGeometryChange();
112  void DrawGL();
114  void DrawGLOpaque(bool opaque);
116  bool IsDynamicGeometry() const;
119  bool DynamicGeometryUpdate();
120 
122  const ManagedGeometry& operator = (const ManagedGeometry& rhs);
123  operator bool() const { return geometry != NULL; }
124  bool operator !() const { return geometry == NULL; }
126  operator GeometryPtr() { return geometry; }
127  operator GeometryPtr() const { return geometry; }
128  GeometryPtr operator ->() { return geometry; }
129  const GeometryPtr operator ->() const { return geometry; }
130  inline Geometry::AnyCollisionGeometry3D& operator *() { return *geometry; }
131  inline const Geometry::AnyCollisionGeometry3D& operator *() const { return *geometry; }
132 
133  friend class GeometryManager;
134  static GeometryManager manager;
135 
136  private:
137  std::string cacheKey,dynamicGeometrySource;
138  GeometryPtr geometry;
139  AppearancePtr appearance;
140 };
141 
143 {
144 public:
145  GeometryManager();
146  ~GeometryManager();
147  void Clear();
148 
149  friend class ManagedGeometry;
151  {
152  std::vector<ManagedGeometry*> geoms;
153  };
154  std::map<std::string,GeometryList> cache;
155 };
156 
157 } //namespace Klampt
158 
159 #endif
void AddToCache(const std::string &filename)
Adds to cache, if not already in it.
void DrawGL()
Renders the object using OpenGL.
void DrawGLOpaque(bool opaque)
Renders the opaque parts of the object using OpenGL.
void OnGeometryChange()
If the geometry is changed, call this to update the appearance.
GeometryPtr CreateEmpty()
Creates an empty geometry.
bool Load(const std::string &filename)
Loads a geometry, with caching.
AppearancePtr Appearance() const
Returns the shared appearance data.
void Clear()
Erases the items in this geometry.
Definition: ManagedGeometry.h:150
bool IsOriginal() const
Returns true if this instance is cached and is identical to a file on disk.
bool IsCached() const
Returns true if this instance is cached.
A "smart" geometry loading class that caches previous geometries and maintains shared collision detec...
Definition: ManagedGeometry.h:62
bool Empty() const
Returns true if the pointer is NULL or the geometry itself is empty.
bool LoadNoCache(const std::string &filename)
Loads a geometry, without caching.
Definition: ManagedGeometry.h:142
const ManagedGeometry & operator=(const ManagedGeometry &rhs)
assignment is a shallow copy
void RemoveFromCache()
Remove self from cache, if in it.
void TransformGeometry(const Math3D::Matrix4 &xform)
bool IsAppearanceShared() const
const std::string & CachedFilename() const
Returns the filename to which this object is cached.
bool IsDynamicGeometry() const
Returns true if this geometry is connected to a dynamic source.
Definition: ContactDistance.h:6