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 UT_StringHolder &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(
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 UT_StringHolder &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 from disk or a stream.
95  bool load(UT_JSONParser &p);
96  /// Load a shape library from the provided geometry.
97  bool load(GU_DetailHandle gdh,
98  UT_StringArray &errors);
99 
100  /// Add entire geometry as a shape in the library.
101  bool addShape(
102  const UT_StringHolder &key,
103  const GU_ConstDetailHandle &gdp,
104  bool replace_existing=true);
105  /// Add a disk file as a shape.
106  bool addShape(
107  const UT_StringHolder &key,
108  const char *filename,
109  bool replace_existing=true);
110 
111  /// Remove a shape from the library
112  bool removeShape(const UT_StringRef &key);
113 
114  /// @{
115  /// Query information
116  bool isEmpty() const
117  { return myShapeCache.empty(); }
118  exint entries() const
119  { return myShapeCache.size(); }
120  /// @}
121 
122  /// Get a reference to the underlying geometry (which holds all the shapes)
123  GU_ConstDetailHandle detail() const { return myDetail; }
124 
125  /// Find a shape
126  ShapePtr findShape(const UT_StringRef &key) const;
127 
128  /// The name of the shape library (or filename, if loaded from disk).
129  /// @{
130  const UT_StringHolder& name() const { return myName; }
132  { myName = name; }
133  /// @}
134 
135  /// Return whether the library was loaded from disk.
136  bool isFile() const { return myIsFile; }
137  /// Clear the flag marking that the library references a file on disk.
138  void clearIsFile();
139 
140  /// @{
141  /// Set a user defined label for the library. This doesn't have to be
142  /// unique across libraries.
143  const UT_StringHolder& label() const;
144  void setLabel(const UT_StringHolder &label);
145  /// @}
146 
147  /// Dump the contents of this shape library for debugging purposes.
148  /// If save_file is supplied, the shape library's geometry will be saved
149  /// using that name.
150  void dump(std::ostream& os,
151  const char* save_file = 0) const;
152 
153  /// Get the names of each dangling shape in the shape library. A dangling
154  /// shape is a shape that is only referenced by the shape library.
155  void getDanglingShapes(
156  UT_SortedStringSet &names) const;
157 
158  /// @{
159  /// Iterators
160  const_iterator begin() const;
161  const_iterator end() const;
162  iterator begin();
163  iterator end();
164  /// @}
165 
166 
167 private: // methods
168  /// Include the contents of another shape library. The included library
169  /// should be an external reference to a file on disk.
170  /// Saving this shape library will only write out the non-included shapes,
171  /// plus external references to the included shape libraries.
172  void includeShapeLib(
173  const GU_AgentShapeLibConstPtr &shapelib);
174 
175  void initAttributes(GU_Detail *gdp);
176  void destroyShape(const_iterator &it);
177  bool replaceExistingShape(
178  const UT_StringHolder &key,
179  bool replace_existing);
180  bool addShape(
181  const UT_StringHolder &key,
182  GEO_Primitive *prim);
183 
184  /// Populate myShapeCache from myDetail.
185  bool loadShapesFromDetail(UT_StringArray &errors);
186 
187 private: // data
188  UT_StringHolder myName;
189  bool myIsFile;
190  GU_DetailHandle myDetail;
191  ShapeMap myShapeCache;
192 
193  GU_AgentShapeLibArray myIncludedShapeLibs;
194 };
195 
196 class GU_API GU_AgentShapeLib::Shape
197  : public UT_IntrusiveRefCounter<GU_AgentShapeLib::Shape>
198 {
199  /// Common data for shapes that are shared between different shape
200  /// libraries. In addition to the packed primitive's geometry, the unique
201  /// ID and GU_LinearSkinDeformerSourceWeights are shared between copies.
202  class GU_API ShapeData : public UT_IntrusiveRefCounter<ShapeData>
203  {
204  public:
205  ShapeData();
206  ~ShapeData();
207 
208  int64 getMemoryUsage(bool inclusive) const;
209  int uniqueId() const { return myUniqueId; }
210 
211  /// Return the cached shape geometry, or load it from the given shape
212  /// library entry.
213  const GU_ConstDetailHandle &
214  getGeometry(const GU_AgentShapeLib &shapelib, GA_Offset offset) const;
215 
217  getLinearSkinDeformerSourceWeights(
218  const GU_ConstDetailHandle &gdh) const;
219 
220  private:
221  ShapeData(const ShapeData &) = delete;
222 
223  int myUniqueId;
224  mutable GU_ConstDetailHandle myGeometry;
225  mutable GU_LinearSkinDeformerSourceWeights *mySourceWeights;
226  };
227  typedef UT_IntrusivePtr<ShapeData> ShapeDataPtr;
228 
229 public:
230  Shape(GA_Offset offset);
231  Shape(const Shape &shape);
232 
233  int64 getMemoryUsage(bool inclusive) const;
234 
235  GA_Offset offset() const { return myOffset; }
236  void setOffset(GA_Offset offset) { myOffset = offset; }
237  int uniqueId() const { return myShapeData->uniqueId(); }
238 
239  /// Given its owner shape library, return the geometry for this shape.
240  GU_ConstDetailHandle shapeGeometry(const GU_AgentShapeLib& lib) const;
241 
242  /// Get a GU_LinearSkinDeformerSourceWeights given the detail handle for
243  /// this shape.
244  /// @pre gdh.isValid()
247  {
248  return myShapeData->getLinearSkinDeformerSourceWeights(gdh);
249  }
250 
253  {
254  return getLinearSkinDeformerSourceWeights(shapeGeometry(shapelib));
255  }
256 
257 private:
258  GA_Offset myOffset;
259  ShapeDataPtr myShapeData;
260 };
261 
262 #endif
int uniqueId() const
GT_API const UT_StringHolder filename
GLenum src
Definition: glew.h:2410
GLuint const GLchar * name
Definition: glew.h:1814
GA_Offset offset() const
FMT_CONSTEXPR auto begin(const C &c) -> decltype(c.begin())
Definition: format.h:251
const GU_LinearSkinDeformerSourceWeights & getLinearSkinDeformerSourceWeights(const GU_ConstDetailHandle &gdh) const
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
Class representing source capture weight data for GU_LinearSkinDeformer.
GA_Size GA_Offset
Definition: GA_Types.h:637
long long int64
Definition: SYS_Types.h:111
void setOffset(GA_Offset offset)
UT_StringMap< ShapePtr > ShapeMap
UT_Array< GU_AgentShapeLibConstPtr > GU_AgentShapeLibArray
int64 exint
Definition: SYS_Types.h:120
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1890
GLuint GLuint end
Definition: glew.h:1253
void
Definition: png.h:1083
GLuint const GLuint * names
Definition: glew.h:2690
const GU_LinearSkinDeformerSourceWeights & getLinearSkinDeformerSourceWeights(const GU_AgentShapeLib &shapelib) const
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:14
ShapeMap::iterator iterator
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:51
GLuint GLsizei GLsizei GLchar * label
Definition: glew.h:8986
GLfloat GLfloat p
Definition: glew.h:16321
exint entries() const
UT_IntrusivePtr< const GU_AgentShapeLib > GU_AgentShapeLibConstPtr
UT_IntrusivePtr< const Shape > ShapePtr
void setName(const UT_StringHolder &name)
GLintptr offset
Definition: glew.h:1682