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