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_Algorithm.h>
19 #include <UT/UT_Lock.h>
20 #include <UT/UT_IntrusivePtr.h>
21 #include <UT/UT_Map.h>
22 #include <UT/UT_StringSet.h>
23 #include <UT/UT_StringArray.h>
24 #include <UT/UT_StringHolder.h>
25 #include <UT/UT_StringMap.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 
169  using CycleDetect = UT_CycleDetect<UT_StringHolder>;
170  /// Internal UT_CycleDetect version of addLibraryFromFile().
171  static GU_AgentShapeLibPtr addLibraryFromFile(
172  const UT_StringHolder &filename,
173  CycleDetect &cycle_detect,
174  UT_StringArray& errors);
175 
176  /// Internal UT_CycleDetect version of load().
177  bool load(UT_JSONParser &p,
178  CycleDetect &cycle_detect);
179 
180  /// Include the contents of another shape library. The included library
181  /// should be an external reference to a file on disk.
182  /// Saving this shape library will only write out the non-included shapes,
183  /// plus external references to the included shape libraries.
184  void includeShapeLib(
185  const GU_AgentShapeLibConstPtr &shapelib);
186 
187  void initAttributes(GU_Detail *gdp);
188  void destroyShape(const_iterator &it);
189  bool replaceExistingShape(
190  const UT_StringHolder &key,
191  bool replace_existing);
192  bool addShape(
193  const UT_StringHolder &key,
194  GEO_Primitive *prim);
195 
196  /// Populate myShapeCache from myDetail.
197  bool loadShapesFromDetail(CycleDetect &cycle_detect,
198  UT_StringArray &errors);
199 
200 private: // data
201  UT_StringHolder myName;
202  bool myIsFile;
203  GU_DetailHandle myDetail;
204  ShapeMap myShapeCache;
205 
206  GU_AgentShapeLibArray myIncludedShapeLibs;
207 };
208 
209 class GU_API GU_AgentShapeLib::Shape
210  : public UT_IntrusiveRefCounter<GU_AgentShapeLib::Shape>
211 {
212  /// Common data for shapes that are shared between different shape
213  /// libraries. In addition to the packed primitive's geometry, the unique
214  /// ID and GU_LinearSkinDeformerSourceWeights are shared between copies.
215  class GU_API ShapeData : public UT_IntrusiveRefCounter<ShapeData>
216  {
217  public:
218  ShapeData();
219  ~ShapeData();
220 
221  int64 getMemoryUsage(bool inclusive) const;
222  int uniqueId() const { return myUniqueId; }
223 
224  /// Return the cached shape geometry, or load it from the given shape
225  /// library entry.
226  const GU_ConstDetailHandle &
227  getGeometry(const GU_AgentShapeLib &shapelib, GA_Offset offset) const;
228 
230  getLinearSkinDeformerSourceWeights(
231  const GU_ConstDetailHandle &gdh) const;
232 
233  private:
234  ShapeData(const ShapeData &) = delete;
235 
236  int myUniqueId;
237  mutable GU_ConstDetailHandle myGeometry;
238  mutable GU_LinearSkinDeformerSourceWeights *mySourceWeights;
239  };
240  typedef UT_IntrusivePtr<ShapeData> ShapeDataPtr;
241 
242 public:
243  Shape(GA_Offset offset);
244  Shape(const Shape &shape);
245 
246  int64 getMemoryUsage(bool inclusive) const;
247 
248  GA_Offset offset() const { return myOffset; }
249  void setOffset(GA_Offset offset) { myOffset = offset; }
250  int uniqueId() const { return myShapeData->uniqueId(); }
251 
252  /// Given its owner shape library, return the geometry for this shape.
253  GU_ConstDetailHandle shapeGeometry(const GU_AgentShapeLib& lib) const;
254 
255  /// Get a GU_LinearSkinDeformerSourceWeights given the detail handle for
256  /// this shape.
257  /// @pre gdh.isValid()
260  {
261  return myShapeData->getLinearSkinDeformerSourceWeights(gdh);
262  }
263 
266  {
267  return getLinearSkinDeformerSourceWeights(shapeGeometry(shapelib));
268  }
269 
270 private:
271  GA_Offset myOffset;
272  ShapeDataPtr myShapeData;
273 };
274 
275 #endif
int uniqueId() const
GT_API const UT_StringHolder filename
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
GA_Offset offset() const
void
Definition: png.h:1083
GLboolean * data
Definition: glcorearb.h:131
const GU_LinearSkinDeformerSourceWeights & getLinearSkinDeformerSourceWeights(const GU_ConstDetailHandle &gdh) const
int64 exint
Definition: SYS_Types.h:125
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
bool isEmpty() const
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
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:646
void setOffset(GA_Offset offset)
UT_StringMap< ShapePtr > ShapeMap
GLintptr offset
Definition: glcorearb.h:665
UT_Array< GU_AgentShapeLibConstPtr > GU_AgentShapeLibArray
GLuint GLuint end
Definition: glcorearb.h:475
const GU_LinearSkinDeformerSourceWeights & getLinearSkinDeformerSourceWeights(const GU_AgentShapeLib &shapelib) const
Wrapper around hboost::intrusive_ptr.
long long int64
Definition: SYS_Types.h:116
ShapeMap::const_iterator const_iterator
bool isFile() const
Return whether the library was loaded from disk.
#define GU_API
Definition: GU_API.h:14
GLuint const GLchar * name
Definition: glcorearb.h:786
ShapeMap::iterator iterator
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:857
UT_IntrusivePtr< const Shape > ShapePtr
void setName(const UT_StringHolder &name)
Definition: format.h:895
GLenum src
Definition: glcorearb.h:1793
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:558