HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GU_AgentRig.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_AgentRig.h (GU Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GU_AgentRig__
12 #define __GU_AgentRig__
13 
14 #include "GU_API.h"
15 #include <UT/UT_Array.h>
16 #include <UT/UT_Assert.h>
17 #include <UT/UT_IntrusivePtr.h>
18 #include <UT/UT_StackBuffer.h>
19 #include <UT/UT_StringArray.h>
20 #include <UT/UT_StringHolder.h>
21 #include <UT/UT_StringMap.h>
22 #include <UT/UT_ValArray.h>
23 
24 
25 class UT_JSONParser;
26 class UT_JSONWriter;
27 
31 
32 
33 /// A rig for the agent primitive
34 class GU_API GU_AgentRig : public UT_IntrusiveRefCounter<GU_AgentRig>
35 {
36 public:
40 
41  static GU_AgentRigPtr addRig(const char *name);
42  static GU_AgentRigPtr addRigFromFile(
43  const char *filename,
44  UT_StringArray &errors);
45 
46 private:
47  // Use the static addRig() method to create new rigs
48  GU_AgentRig(const char *name, bool is_file)
49  : myName(name), myIsFile(is_file)
50  {
51  UT_ASSERT(checkValid());
52  }
53 
54 public:
55  ~GU_AgentRig();
56 
57  /// Construct using flat arrays.
58  /// - @c names: The array of names for each transform (one per transform)
59  /// - @c child_counts: An array of child counts for each transform
60  /// Should have the same number of entries as the @c names array
61  /// - @c children: An array of children. This is the concatenation of all
62  /// the child indices for each transform. There should be
63  /// @c sum(child_counts) entries in this array.
64  bool construct(
65  const UT_StringArray& names,
66  const UT_IntArray& child_counts,
67  const UT_IntArray& children);
68 
69  int64 getMemoryUsage(bool inclusive) const;
70 
71  /// Return the name of the rig.
72  const UT_StringHolder &name() const
73  { return myName; }
74  /// Set the name of the rig.
75  void setName(const UT_StringHolder &name)
76  { myName = name; }
77 
78  /// Return whether the rig was loaded from disk.
79  bool isFile() const
80  { return myIsFile; }
81  /// Clear the flag marking that the rig references a file on disk.
82  void clearIsFile();
83 
84  /// Clear the rig
85  void clear();
86 
87  /// Check validity of rig
88  bool checkValid() const;
89 
90  /// Return number of transforms
92  { return myTransforms.entries(); }
93  /// Return the name of the given transform
95  { return myTransforms(i); }
96  /// Return the parent of the given transform (or -1 for the root)
98  { return myParents(i); }
99  /// Return the number of children for the given transform
101  {
102  return myChildStarts(i+1) - myChildStarts(i);
103  }
104  /// Return the Nth child for the given transform
106  {
107  UT_ASSERT_P(child < childCount(transform));
108  return myChildren(myChildStarts(transform) + child);
109  }
110  /// Return the root transform(s) of the rig.
111  const UT_IntArray &roots() const
112  { return myRoots; }
113 
114  /// Find a transform by name. Returns -1 for an invalid name
115  exint findTransform(const UT_StringRef &name) const;
116 
117  /// Save a rig
118  bool save(UT_JSONWriter& w) const;
119  /// Load a rig
120  bool load(UT_JSONParser& p, UT_StringArray& errors);
121 
122  /// Dump to stdout for debugging
123  void dump() const;
124 
125  /// Iterator for top down traversal of the rig hierarchy. Each visited
126  /// transform's parent is guaranteed to be visited before its children.
128  {
129  public:
130  const_iterator(const GU_AgentRig &rig, int start = -1)
131  : myRig(rig), myIndex(0), myEnd(0), myQueue(rig.transformCount())
132  {
133  rewind(start);
134  }
135 
136  void rewind(int start = -1)
137  {
138  myIndex = 0;
139  myEnd = 0;
140 
141  if (start < 0)
142  {
143  for(int i : myRig.roots())
144  myQueue[myEnd++] = i;
145  }
146  else
147  myQueue[myEnd++] = start;
148  }
149 
150  inline void advance()
151  {
152  const int i = myQueue[myIndex];
153 
154  // Queue the child transforms for processing.
155  for (int child = 0, n = myRig.childCount(i); child < n; ++child)
156  {
157  myQueue[myEnd++] = myRig.childIndex(i, child);
158  UT_ASSERT_P(myEnd <= myRig.transformCount());
159  }
160 
161  ++myIndex;
162  }
163 
164  inline bool atEnd() const
165  {
166  return myIndex == myEnd;
167  }
168 
169  /// Returns the transform index
170  inline int operator*() const { return myQueue[myIndex]; }
171 
172  /// Pre-increment operator
173  inline const_iterator& operator++() { advance(); return *this; }
174 
175  private:
176  const GU_AgentRig &myRig;
177  int myIndex;
178  int myEnd;
179  // We know our total number of xforms is bounded, so we
180  // can allocate potentially on the stack the full queue.
182  };
183 
184 private:
185  UT_StringHolder myName;
186  bool myIsFile;
187 
188  // We build a symbol table sharing the string in the myTransforms array
189  UT_StringArray myTransforms; // One per transform
190  NameMapType myTransformsMap; // Map from names to indices
191 
192  UT_IntArray myParents; // Indices to parent transform
193  UT_IntArray myChildStarts; // One per transform, indices into
194  // myChildren for where the children
195  // for the i'th transform starts.
196  UT_IntArray myChildren; // Child pointers
197  UT_IntArray myRoots; // Root transform(s) of the rig.
198 };
199 
200 ///////////////////////////////////////////////////////////////////////////////
201 //
202 // Implementation
203 //
204 
205 inline exint
207 {
208  NameMapConstIter it = myTransformsMap.find(name);
209  if (it == myTransformsMap.end())
210  return -1;
211  return it->second;
212 }
213 
214 #endif
exint findTransform(const UT_StringRef &name) const
Find a transform by name. Returns -1 for an invalid name.
Definition: GU_AgentRig.h:206
NameMapType::const_iterator NameMapConstIter
Definition: GU_AgentRig.h:38
const_iterator & operator++()
Pre-increment operator.
Definition: GU_AgentRig.h:173
void rewind(int start=-1)
Definition: GU_AgentRig.h:136
GLuint start
Definition: glcorearb.h:474
exint childCount(exint i) const
Return the number of children for the given transform.
Definition: GU_AgentRig.h:100
const UT_StringHolder & transformName(exint i) const
Return the name of the given transform.
Definition: GU_AgentRig.h:94
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:72
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
exint childIndex(exint transform, exint child) const
Return the Nth child for the given transform.
Definition: GU_AgentRig.h:105
A reference counter base class for use with UT_IntrusivePtr.
png_uint_32 i
Definition: png.h:2877
NameMapType::iterator NameMapIter
Definition: GU_AgentRig.h:39
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:101
exint parentIndex(exint i) const
Return the parent of the given transform (or -1 for the root)
Definition: GU_AgentRig.h:97
long long int64
Definition: SYS_Types.h:100
GLdouble n
Definition: glcorearb.h:2007
A rig for the agent primitive.
Definition: GU_AgentRig.h:34
const UT_IntArray & roots() const
Return the root transform(s) of the rig.
Definition: GU_AgentRig.h:111
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
int64 exint
Definition: SYS_Types.h:109
Wrapper around hboost::intrusive_ptr.
#define GU_API
Definition: GU_API.h:11
GLuint const GLchar * name
Definition: glcorearb.h:785
GA_API const UT_StringHolder transform
void setName(const UT_StringHolder &name)
Set the name of the rig.
Definition: GU_AgentRig.h:75
int operator*() const
Returns the transform index.
Definition: GU_AgentRig.h:170
UT_StringMap< exint > NameMapType
Definition: GU_AgentRig.h:37
UT_IntrusivePtr< GU_AgentRig > GU_AgentRigPtr
Definition: GU_AgentRig.h:28
bool isFile() const
Return whether the rig was loaded from disk.
Definition: GU_AgentRig.h:79
UT_IntrusivePtr< const GU_AgentRig > GU_AgentRigConstPtr
Definition: GU_AgentRig.h:30
const_iterator(const GU_AgentRig &rig, int start=-1)
Definition: GU_AgentRig.h:130
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
const UT_StringHolder & name() const
Return the name of the rig.
Definition: GU_AgentRig.h:72
exint transformCount() const
Return number of transforms.
Definition: GU_AgentRig.h:91
Parent::iterator iterator
Definition: UT_StringMap.h:51
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:50