HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GU_AgentShapeLib.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: GU_AgentShapeLib.h (GU Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GU_AgentShapeLib__
12 #define __GU_AgentShapeLib__
13 
14 #include "GU_API.h"
15 #include "GU_DetailHandle.h"
16 #include "GU_LinearSkinDeformer.h"
17 #include <GA/GA_Types.h>
18 #include <UT/UT_Lock.h>
19 #include <UT/UT_IntrusivePtr.h>
20 #include <UT/UT_Map.h>
21 #include <UT/UT_StringSet.h>
22 #include <UT/UT_StringArray.h>
23 #include <UT/UT_StringHolder.h>
24 #include <SYS/SYS_AtomicInt.h>
25 #include <iosfwd>
26 
27 class UT_JSONWriter;
28 class UT_JSONParser;
29 class GEO_Primitive;
30 
35 
36 /// Library for Agent Shapes
37 ///
38 /// An agent shape library stores multiple shapes. There are various ways to add
39 /// shapes to the library (add a full piece of geometry, add geometry and a
40 /// range of primitives, etc.).
41 ///
42 /// Shapes are identified by "name" which must be unique within the library.
43 ///
44 class GU_API GU_AgentShapeLib : public UT_IntrusiveRefCounter<GU_AgentShapeLib>
45 {
46 public:
47  class Shape;
48 
53  typedef void (*ShapeRemovedCallback)(void *data, int shape_id);
54 
55 public:
56  /// Create a new library using the given name.
57  static GU_AgentShapeLibPtr addLibrary(const char *name);
58 
59  /// Load a new library from disk, named using the filename. Returns NULL if
60  /// failed to load.
61  static GU_AgentShapeLibPtr addLibraryFromFile(
62  const char *filename,
63  UT_StringArray& errors);
64 
65  /// Create a clone of a library for modification. The clone will have
66  /// isFile() return false.
67  /// If keep_external_ref is true and the source library was a file
68  /// reference, the source library will be marked as an included file of the
69  /// new library. Otherwise, the new library will be standalone.
70  static GU_AgentShapeLibPtr addLibraryCopy(const GU_AgentShapeLib &src,
71  bool keep_external_ref);
72 
73  /// Register a callback to be invoked when a shape is no longer referenced
74  /// by any shape library.
75  static void registerShapeRemovedCallback(
76  ShapeRemovedCallback cb, void *data = 0);
77  static void unregisterShapeRemovedCallback(
78  ShapeRemovedCallback cb, void *data = 0);
79 
80 private:
81  /// Use the static addLibrary() method to create shape libraries.
83  const char *name,
84  bool is_file);
85 
86 public:
88 
89  int64 getMemoryUsage(bool inclusive) const;
90 
91  /// Save the shape library.
92  bool save(UT_JSONWriter &w) const;
93  /// Load a shape library.
94  bool load(UT_JSONParser &p);
95 
96  /// Add entire geometry as a shape in the library.
97  bool addShape(
98  const UT_StringHolder &key,
99  const GU_ConstDetailHandle &gdp,
100  bool replace_existing=true);
101  /// Add a disk file as a shape.
102  bool addShape(
103  const UT_StringHolder &key,
104  const char *filename,
105  bool replace_existing=true);
106 
107  /// Remove a shape from the library
108  bool removeShape(const UT_StringRef &key);
109 
110  /// @{
111  /// Query information
112  bool isEmpty() const
113  { return myShapeCache.empty(); }
114  exint entries() const
115  { return myShapeCache.size(); }
116  /// @}
117 
118  /// Get a reference to the underlying geometry (which holds all the shapes)
119  GU_ConstDetailHandle detail() const { return myDetail; }
120 
121  /// Find a shape
122  ShapePtr findShape(const UT_StringRef &key) const;
123 
124  /// The name of the shape library (or filename, if loaded from disk).
125  /// @{
126  const UT_StringHolder& name() const { return myName; }
128  { myName = name; }
129  /// @}
130 
131  /// Return whether the library was loaded from disk.
132  bool isFile() const { return myIsFile; }
133  /// Clear the flag marking that the library references a file on disk.
134  void clearIsFile();
135 
136  /// @{
137  /// Set a user defined label for the library. This doesn't have to be
138  /// unique across libraries.
139  const char* label() const;
140  void setLabel(const char *label);
141  /// @}
142 
143  /// Dump the contents of this shape library for debugging purposes.
144  /// If save_file is supplied, the shape library's geometry will be saved
145  /// using that name.
146  void dump(std::ostream& os,
147  const char* save_file = 0) const;
148 
149  /// Get the names of each dangling shape in the shape library. A dangling
150  /// shape is a shape that is only referenced by the shape library.
151  void getDanglingShapes(
152  UT_SortedStringSet &names) const;
153 
154  /// @{
155  /// Iterators
156  const_iterator begin() const;
157  const_iterator end() const;
158  iterator begin();
159  iterator end();
160  /// @}
161 
162 
163 private: // methods
164  /// Include the contents of another shape library. The included library
165  /// should be an external reference to a file on disk.
166  /// Saving this shape library will only write out the non-included shapes,
167  /// plus external references to the included shape libraries.
168  void includeShapeLib(
169  const GU_AgentShapeLibConstPtr &shapelib);
170 
171  void initAttributes(GU_Detail *gdp);
172  void destroyShape(const_iterator &it);
173  bool replaceExistingShape(
174  const UT_StringHolder &key,
175  bool replace_existing);
176  bool addShape(
177  const UT_StringHolder &key,
178  GEO_Primitive *prim);
179 
180 private: // data
181  UT_StringHolder myName;
182  bool myIsFile;
183  GU_DetailHandle myDetail;
184  ShapeMap myShapeCache;
185 
186  GU_AgentShapeLibArray myIncludedShapeLibs;
187 };
188 
189 class GU_API GU_AgentShapeLib::Shape
190  : public UT_IntrusiveRefCounter<GU_AgentShapeLib::Shape>
191 {
192  /// Common data for shapes that are shared between different shape
193  /// libraries. In addition to the packed primitive's geometry, the unique
194  /// ID and GU_LinearSkinDeformerSource are shared between copies.
195  class GU_API ShapeData : public UT_IntrusiveRefCounter<ShapeData>
196  {
197  public:
198  ShapeData();
199  ~ShapeData();
200 
201  int64 getMemoryUsage(bool inclusive) const;
202  int uniqueId() const { return myUniqueId; }
203 
205  getDeformerSource(const GU_ConstDetailHandle &gdh) const;
206 
207  private:
208  int myUniqueId;
209  mutable GU_LinearSkinDeformerSource *myDeformerSource;
210  };
211  typedef UT_IntrusivePtr<ShapeData> ShapeDataPtr;
212 
213 public:
214  Shape(GA_Offset offset);
215  Shape(const Shape &shape);
216 
217  int64 getMemoryUsage(bool inclusive) const;
218 
219  GA_Offset offset() const { return myOffset; }
220  void setOffset(GA_Offset offset) { myOffset = offset; }
221  int uniqueId() const { return myShapeData->uniqueId(); }
222 
223  /// Given its owner shape library, return the geometry for this shape.
224  GU_ConstDetailHandle shapeGeometry(const GU_AgentShapeLib& lib) const;
225 
227 
228  /// Get a GU_LinearSkinDeformerSource given the detail handle for this
229  /// shape.
230  /// @pre gdh.isValid()
231  const DeformerSource &
233  {
234  return myShapeData->getDeformerSource(gdh);
235  }
236 
237 private:
238  GA_Offset myOffset;
239  ShapeDataPtr myShapeData;
240 };
241 
242 #endif
int uniqueId() const
const DeformerSource & getDeformerSource(const GU_ConstDetailHandle &gdh) const
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2544
GA_Offset offset() const
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:72
bool isEmpty() const
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
A reference counter base class for use with UT_IntrusivePtr.
UT_IntrusivePtr< GU_AgentShapeLib > GU_AgentShapeLibPtr
GU_ConstDetailHandle detail() const
Get a reference to the underlying geometry (which holds all the shapes)
const UT_StringHolder & name() const
GA_Size GA_Offset
Definition: GA_Types.h:617
long long int64
Definition: SYS_Types.h:106
void setOffset(GA_Offset offset)
UT_StringMap< ShapePtr > ShapeMap
UT_Array< GU_AgentShapeLibConstPtr > GU_AgentShapeLibArray
Class representing source data for GU_LinearSkinDeformer.
int64 exint
Definition: SYS_Types.h:115
GLuint GLuint end
Definition: glcorearb.h:474
GLintptr offset
Definition: glcorearb.h:664
Wrapper around hboost::intrusive_ptr.
ShapeMap::const_iterator const_iterator
bool isFile() const
Return whether the library was loaded from disk.
#define GU_API
Definition: GU_API.h:11
GLboolean * data
Definition: glcorearb.h:130
ShapeMap::iterator iterator
GLuint const GLchar * name
Definition: glcorearb.h:785
exint entries() const
UT_IntrusivePtr< const GU_AgentShapeLib > GU_AgentShapeLibConstPtr
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
GU_LinearSkinDeformerSource DeformerSource
UT_IntrusivePtr< const Shape > ShapePtr
Parent::iterator iterator
Definition: UT_StringMap.h:51
void setName(const UT_StringHolder &name)
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:50
GLenum src
Definition: glcorearb.h:1792