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