HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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:
41 
42  static GU_AgentRigPtr addRig(const UT_StringHolder &name);
43  static GU_AgentRigPtr addRigFromFile(
45  UT_StringArray &errors);
46  /// Creates a copy of the rig. The copy will not be marked as an external
47  /// reference.
48  static GU_AgentRigPtr addRigCopy(const GU_AgentRig &src);
49 
50 private:
51  // Use the static addRig() method to create new rigs
52  GU_AgentRig(const UT_StringHolder &name, bool is_file)
53  : myName(name), myIsFile(is_file)
54  {
55  UT_ASSERT(checkValid());
56  }
57 
58 public:
59  ~GU_AgentRig();
60 
61  /// Construct using flat arrays.
62  /// - @c names: The array of names for each transform (one per transform)
63  /// - @c child_counts: An array of child counts for each transform
64  /// Should have the same number of entries as the @c names array
65  /// - @c children: An array of children. This is the concatenation of all
66  /// the child indices for each transform. There should be
67  /// @c sum(child_counts) entries in this array.
68  bool construct(
69  const UT_StringArray& names,
70  const UT_IntArray& child_counts,
71  const UT_IntArray& children);
72 
73  int64 getMemoryUsage(bool inclusive) const;
74 
75  /// @{
76  /// The name of the rig (or filename, if loaded from disk).
77  const UT_StringHolder &name() const
78  { return myName; }
79  void setName(const UT_StringHolder &name)
80  { myName = name; }
81  ///
82 
83  /// Return whether the rig was loaded from disk.
84  bool isFile() const
85  { return myIsFile; }
86  /// Clear the flag marking that the rig references a file on disk.
87  void clearIsFile();
88 
89  /// Clear the rig
90  void clear();
91 
92  /// Check validity of rig
93  bool checkValid() const;
94 
95  /// Return number of transforms
97  { return myTransforms.entries(); }
98  /// Return the name of the given transform
100  { return myTransforms(i); }
101  /// Return the parent of the given transform (or -1 for the root)
103  { return myParents(i); }
104  /// Return the number of children for the given transform
106  {
107  return myChildStarts(i+1) - myChildStarts(i);
108  }
109  /// Return the Nth child for the given transform
111  {
112  UT_ASSERT_P(child < childCount(transform));
113  return myChildren(myChildStarts(transform) + child);
114  }
115  /// Return the root transform(s) of the rig.
116  const UT_IntArray &roots() const
117  { return myRoots; }
118 
119  /// Find a transform by name. Returns -1 for an invalid name
120  exint findTransform(const UT_StringRef &name) const;
121 
122  /// Save a rig
123  bool save(UT_JSONWriter& w) const;
124  /// Load a rig
125  bool load(UT_JSONParser& p, UT_StringArray& errors);
126 
127  /// Dump to stdout for debugging
128  void dump() const;
129 
130  /// Iterator for top down traversal of the rig hierarchy. Each visited
131  /// transform's parent is guaranteed to be visited before its children.
133  {
134  public:
135  const_iterator(const GU_AgentRig &rig, int start = -1)
136  : myRig(rig), myIndex(0), myEnd(0), myQueue(rig.transformCount())
137  {
138  rewind(start);
139  }
140 
141  void rewind(int start = -1)
142  {
143  myIndex = 0;
144  myEnd = 0;
145 
146  if (start < 0)
147  {
148  for(int i : myRig.roots())
149  myQueue[myEnd++] = i;
150  }
151  else
152  myQueue[myEnd++] = start;
153  }
154 
155  inline void advance()
156  {
157  const int i = myQueue[myIndex];
158 
159  // Queue the child transforms for processing.
160  for (int child = 0, n = myRig.childCount(i); child < n; ++child)
161  {
162  myQueue[myEnd++] = myRig.childIndex(i, child);
163  UT_ASSERT_P(myEnd <= myRig.transformCount());
164  }
165 
166  ++myIndex;
167  }
168 
169  inline bool atEnd() const
170  {
171  return myIndex == myEnd;
172  }
173 
174  /// Returns the transform index
175  inline int operator*() const { return myQueue[myIndex]; }
176 
177  /// Pre-increment operator
178  inline const_iterator& operator++() { advance(); return *this; }
179 
180  private:
181  const GU_AgentRig &myRig;
182  int myIndex;
183  int myEnd;
184  // We know our total number of xforms is bounded, so we
185  // can allocate potentially on the stack the full queue.
187  };
188 
189  /// Adds a list of channels to the rig. Agents can store a list of values
190  /// for these channels (e.g. for driving blendshapes), and these channels
191  /// can be sampled from any GU_AgentClip.
192  bool addChannels(const UT_StringArray &names,
193  const UT_Array<FloatType> &default_values,
194  const UT_IntArray &transforms,
195  UT_StringArray &errors);
196  /// Adds or replaces a channel in the rig.
197  void addChannel(const UT_StringHolder &name, FloatType default_value,
198  exint xform_idx);
199 
200  /// Returns the number of channels.
201  exint channelCount() const { return myChannels.size(); }
202  /// Returns the name of the specified channel.
203  const UT_StringHolder &channelName(exint i) const { return myChannels[i]; }
204  /// Returns the default value of the specified channel.
205  FloatType channelDefaultValue(exint i) const;
206  /// Returns the transform index associated with the channel, or -1.
207  exint channelTransform(exint i) const;
208 
209  /// Returns the index of the channel with the specified name, or -1 if it
210  /// does not exist.
211  exint findChannel(const UT_StringRef &name) const;
212 
213 private:
214  UT_StringHolder myName;
215  bool myIsFile;
216 
217  // We build a symbol table sharing the string in the myTransforms array
218  UT_StringArray myTransforms; // One per transform
219  NameMapType myTransformsMap; // Map from names to indices
220 
221  UT_IntArray myParents; // Indices to parent transform
222  UT_IntArray myChildStarts; // One per transform, indices into
223  // myChildren for where the children
224  // for the i'th transform starts.
225  UT_IntArray myChildren; // Child pointers
226  UT_IntArray myRoots; // Root transform(s) of the rig.
227 
228  UT_StringArray myChannels;
229  NameMapType myChannelMap;
230  UT_Array<FloatType> myChannelDefaultValues;
231  UT_IntArray myChannelTransforms;
232 };
233 
234 ///////////////////////////////////////////////////////////////////////////////
235 //
236 // Implementation
237 //
238 
239 inline exint
241 {
242  NameMapConstIter it = myTransformsMap.find(name);
243  if (it == myTransformsMap.end())
244  return -1;
245  return it->second;
246 }
247 
248 inline exint
250 {
251  auto it = myChannelMap.find(name);
252  return it != myChannelMap.end() ? it->second : -1;
253 }
254 
257 {
258  return myChannelDefaultValues[i];
259 }
260 
261 inline exint
263 {
264  return myChannelTransforms[i];
265 }
266 
267 #endif
exint findTransform(const UT_StringRef &name) const
Find a transform by name. Returns -1 for an invalid name.
Definition: GU_AgentRig.h:240
NameMapType::const_iterator NameMapConstIter
Definition: GU_AgentRig.h:38
const_iterator & operator++()
Pre-increment operator.
Definition: GU_AgentRig.h:178
void rewind(int start=-1)
Definition: GU_AgentRig.h:141
GT_API const UT_StringHolder filename
GLenum src
Definition: glew.h:2410
GLuint const GLchar * name
Definition: glew.h:1814
GLuint GLenum GLenum transform
Definition: glew.h:14742
exint childCount(exint i) const
Return the number of children for the given transform.
Definition: GU_AgentRig.h:105
const UT_StringHolder & transformName(exint i) const
Return the name of the given transform.
Definition: GU_AgentRig.h:99
FloatType channelDefaultValue(exint i) const
Returns the default value of the specified channel.
Definition: GU_AgentRig.h:256
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:75
exint channelTransform(exint i) const
Returns the transform index associated with the channel, or -1.
Definition: GU_AgentRig.h:262
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:110
A reference counter base class for use with UT_IntrusivePtr.
NameMapType::iterator NameMapIter
Definition: GU_AgentRig.h:39
Parent::iterator iterator
Definition: UT_StringMap.h:52
exint findChannel(const UT_StringRef &name) const
Definition: GU_AgentRig.h:249
exint parentIndex(exint i) const
Return the parent of the given transform (or -1 for the root)
Definition: GU_AgentRig.h:102
long long int64
Definition: SYS_Types.h:111
A rig for the agent primitive.
Definition: GU_AgentRig.h:34
exint channelCount() const
Returns the number of channels.
Definition: GU_AgentRig.h:201
const UT_IntArray & roots() const
Return the root transform(s) of the rig.
Definition: GU_AgentRig.h:116
int64 exint
Definition: SYS_Types.h:120
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:134
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1890
GLuint const GLuint * names
Definition: glew.h:2690
GLsizei n
Definition: glew.h:4040
Wrapper around hboost::intrusive_ptr.
#define GU_API
Definition: GU_API.h:14
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:51
GLuint start
Definition: glew.h:1253
void setName(const UT_StringHolder &name)
Definition: GU_AgentRig.h:79
GLfloat GLfloat p
Definition: glew.h:16321
int operator*() const
Returns the transform index.
Definition: GU_AgentRig.h:175
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:84
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:135
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:135
const UT_StringHolder & name() const
Definition: GU_AgentRig.h:77
const UT_StringHolder & channelName(exint i) const
Returns the name of the specified channel.
Definition: GU_AgentRig.h:203
exint transformCount() const
Return number of transforms.
Definition: GU_AgentRig.h:96
fpreal32 FloatType
Definition: GU_AgentRig.h:40
float fpreal32
Definition: SYS_Types.h:195