HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GABC_Util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) COPYRIGHTYEAR
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *----------------------------------------------------------------------------
26  */
27 
28 #ifndef __GABC_Util__
29 #define __GABC_Util__
30 
31 #include "GABC_API.h"
32 #include "GABC_Include.h"
33 #include "GABC_IArchive.h"
34 #include "GABC_IObject.h"
35 #include "GABC_OProperty.h"
36 #include "GABC_Types.h"
37 #include <SYS/SYS_Types.h>
38 #include <UT/UT_BoundingBox.h>
39 #include <UT/UT_JSONParser.h>
40 #include <UT/UT_Matrix4.h>
41 #include <UT/UT_SharedPtr.h>
42 #include <UT/UT_WorkBuffer.h>
43 #include <UT/UT_StringSet.h>
44 
45 class UT_StringArray;
46 
47 namespace GABC_NAMESPACE
48 {
49 
51 {
52 public:
54 
58 
62 
68 
71  typedef std::pair<std::string, GABC_OProperty *> PropertyMapInsert;
72  typedef std::vector<std::string> PathList;
73 
74  /// Class used in traversal of Alembic trees
75  ///
76  /// For standard walking of the tree, you'd likely have a process() method
77  /// like this: @code
78  /// bool process(const GABC_IObject &node)
79  /// {
80  /// doSomething(node);
81  /// return true; // Process other nodes
82  /// }
83  /// @endcode
84  /// However, if you have to pass information to the child nodes, you might
85  /// want to do something like: @code
86  /// bool process(const GABC_IObject &node)
87  /// {
88  /// doSomething(node); // Process this node
89  /// pushState(); // Set information for kids
90  /// walkChildren(node);
91  /// popState(); // Restore parent's state
92  /// return false; // Don't let parent walk kids
93  /// }
94  /// @endcode
96  {
97  public:
99  : myFilenames()
100  , myStartTime(0)
101  , myEndTime(0)
102  , myComputedTimes(false)
103  , myBadArchive(false)
104  {}
105  virtual ~Walker() {}
106 
107  /// @c preProcess() is called on the "root" of the walk. The root may
108  /// @b not be the root of the Alembic file (i.e. when walking lists of
109  /// objects). The @c preProcess() method will be called one time only.
110  virtual bool preProcess(const GABC_IObject &node) { return true; }
111 
112  /// Return true to continue traveral and process the children of the
113  /// given node. Returning false will process the next sibling. Use
114  /// interrupted() to perform an early termination.
115  virtual bool process(const GABC_IObject &node) = 0;
116 
117  /// Allow for interruption of walk
118  virtual bool interrupted() const { return false; }
119 
120  /// Manually walk children of the given node. Returns false if the
121  /// walk was interrupted, true if the walk was completed.
122  bool walkChildren(const GABC_IObject &node);
123 
124  /// Recomputes the time range of the archive given a new object during
125  /// a walk.
126  void computeTimeRange(const GABC_IObject &obj);
127 
128  /// Returns true if a valid time range has been computed during the walk.
130  { return myComputedTimes && myStartTime != myEndTime; }
131 
132  /// Get global start and end times, computed when walking the archive.
134  { return myStartTime; }
136  { return myEndTime; }
137  /// @{
138  /// Access the current filenames
139  const std::vector<std::string> &filenames() const { return myFilenames; }
140  void setFilenames(const std::vector<std::string> &f)
141  { myFilenames = f; }
142  /// @}
143 
144  /// Return whether the walking error was caused by a bad Alembic archive
145  bool badArchive() const { return myBadArchive; }
146 
147  private:
148  std::vector<std::string> myFilenames;
149  fpreal myStartTime;
150  fpreal myEndTime;
151  bool myComputedTimes;
152  bool myBadArchive; // Invalid alembic archive
153 
154  friend class GABC_Util;
155  };
156 
157  /// Event functor called when an archive is flushed from the cache
159  {
160  public:
162  virtual ~ArchiveEventHandler() {}
163 
164  /// Return whether the event handler is wired up to an archive
165  bool valid() const { return myArchive != NULL; }
166 
167  /// Call this method to no longer receive events
168  void stopReceivingEvents();
169 
170  /// This method is called when the archive is cleared. The handler
171  /// will no longer receive any events after the archive is cleared.
172  virtual void cleared() = 0;
173 
174  /// @{
175  /// @private
176  /// Methods used to access internal state
177  void setArchivePtr(void *a) { myArchive = a; }
178  const void *archive() const { return myArchive; }
179  /// @}
180  private:
181  void *myArchive;
182  };
183 
184  //
185  // Alembic Namespace Name
186  //
187 
188  /// Get Alembic namespace name as string
189  static const char *getAlembicCompileNamespace();
190 
191  //
192  // Bounding Box Conversion
193  //
194 
195  /// Create a Box3d from a UT_BoundingBox
196  static Box3d getBox(const UT_BoundingBox &box)
197  {
198  return Box3d(V3d(box.xmin(), box.ymin(), box.zmin()),
199  V3d(box.xmax(), box.ymax(), box.zmax()));
200  }
201  /// Create a UT_BoundingBox from a Box3d
202  static UT_BoundingBox getBox(const Box3d &box)
203  {
204  return UT_BoundingBox(box.min[0], box.min[1], box.min[2],
205  box.max[0], box.max[1], box.max[2]);
206  }
207 
208  //
209  // Cache
210  //
211 
212  /// Clear the cache. If no filename is given, the entire cache will be
213  /// cleared.
214  static void clearCache(const char *filename=NULL);
215  /// Set the cache size
216  static void setFileCacheSize(int nfiles);
217  /// Get the file cache size
218  static int fileCacheSize();
219 
220  //
221  // Events
222  //
223 
224  /// Add an event handler to be notified of events on the given
225  /// list of filenames
226  /// The method returns false if the archive hasn't been loaded yet.
227  static bool addEventHandler(const std::vector<std::string> &filenames,
228  const ArchiveEventHandlerPtr &handler);
229 
230  //
231  // Find Objects in Alembic Hierarchy
232  //
233 
234  /// Find a given GABC_IObject in an Alembic file.
235  static GABC_IObject findObject(const std::vector<std::string> &filenames,
236  const std::string &objectpath);
237  static GABC_IObject findObject(const std::vector<std::string> &filenames,
238  ObjectReaderPtr reader);
239  /// Return a list of all the objects in an Alembic file
240  static const PathList &getObjectList(const std::vector<std::string> &filenames,
241  bool include_face_sets=false);
242 
243  //
244  // Matrix Conversion
245  //
246 
247  /// Create a UT_Matrix4D from an M44d
248  static UT_Matrix4D getM(const M44d &m)
249  {
250  return UT_Matrix4D(m.x);
251  }
252  /// Create an M44d from a UT_Matrix4D
253  static M44d getM(const UT_Matrix4D &m)
254  {
255  return M44d((const double (*)[4])m.data());
256  }
257 
258  //
259  // Transforms
260  //
261 
262  /// Get the local transform for a given node in an Alembic file. The @c
263  /// isConstant flag will be true if the local transform is constant (even
264  /// if parent transforms are non-constant).
265  static bool getLocalTransform(
266  const std::vector<std::string> &filenames,
267  const std::string &objectpath,
268  fpreal sample_time,
269  UT_Matrix4D &xform,
270  bool &isConstant,
271  bool &inheritsXform);
272  /// Get the world transform for a given node in an Alembic file. If the
273  /// given node is a shape node, the transform up to its parent will be
274  /// computed. If the transform is constant (including all parents), the @c
275  /// isConstant flag will be set.
276  ///
277  /// The method returns false if there was an error computing the transform.
278  static bool getWorldTransform(
279  const std::vector<std::string> &filenames,
280  const std::string &objectpath,
281  fpreal sample_time,
282  UT_Matrix4D &xform,
283  bool &isConstant,
284  bool &inheritsXform);
285  /// Get the world transform for a GABC_IObject in an Alembic file. If the
286  /// given node is a shape node, the transform up to its parent will be
287  /// returned.
288  static bool getWorldTransform(
289  const GABC_IObject &object,
290  fpreal sample_time,
291  UT_Matrix4D &xform,
292  bool &isConstant,
293  bool &inheritsXform);
294  /// Test whether an object is static or has an animated transform
295  static bool isTransformAnimated(
296  const GABC_IObject &object);
297 
298  //
299  // Visibility
300  //
301 
302  /// Get the visibility for a GABC_IObject.
303  static GABC_VisibilityType getVisibility(
304  const GABC_IObject &object,
305  fpreal sample_time,
306  bool &animated,
307  bool check_parent);
308 
309  /// Get the bounding box for a GABC_IObject.
310  static bool getBoundingBox(
311  const GABC_IObject &object,
312  fpreal sample_time,
313  UT_BoundingBox &box,
314  bool &isconst);
315 
316  //
317  // Walk Alembic Hierarchy
318  //
319 
320  /// Walk the tree in an alembic file. Returns false if traversal was
321  /// interrupted, otherwise returns true.
322  static bool walk(const std::vector<std::string> &filenames, Walker &walker);
323  /// Process a list of unique objects in an Alembic file (including their
324  /// children)
325  static bool walk(const std::vector<std::string> &filenames, Walker &walker,
326  const UT_StringArray &objects);
327  static bool walk(const std::vector<std::string> &filenames, Walker &walker,
328  const UT_Set<std::string> &objects);
329 
330  //
331  // Alembic Properties
332  //
333 
334  /// Check whether or not an Alembic compound property (like arbGeomParams
335  /// or user properties) is constant/animated over time.
336  static bool isABCPropertyAnimated(ICompoundProperty arb);
337  static bool isABCPropertyConstant(ICompoundProperty arb);
338 
339  //
340  // User Properties
341  //
342 
343  /// Import user properties into two JSON dictionaries, one containing
344  /// values and another containing the metadata for the properties.
345  static bool importUserPropertyDictionary(UT_JSONWriter *vals_writer,
346  UT_JSONWriter *meta_writer,
347  const GABC_IObject &obj,
348  fpreal time);
349 
350  /// Export user properties from two JSON dictionaries (one containing
351  /// values, the other containing metadata used to interpret the values)
352  /// to GABC_OProperties, and store them in the given map.
353  static void exportUserPropertyDictionary(UT_AutoJSONParser &meta_data,
354  UT_AutoJSONParser &vals_data,
355  PropertyMap &up_map,
356  OCompoundProperty *ancestor,
357  GABC_OError &err,
358  const GABC_OOptions &ctx,
359  const GABC_LayerOptions &lopt,
361 
362  static void getUserPropertyTokens(UT_SortedStringSet &tokens,
363  UT_AutoJSONParser &meta_data,
364  UT_AutoJSONParser &vals_data,
365  GABC_OError &err);
366 
370 
371  /// Gets the samples indices (i0 and i1) corresponding to time 't'. The
372  /// bias for blending the samples is returned.
373  static fpreal getSampleIndex(fpreal t, const TimeSamplingPtr &itime,
374  exint nsamp, index_t &i0, index_t &i1);
375 
376  /// Class to efficiently find a new name when a collision is detected.
378  {
379  public:
380  /// Updates 'name' to avoid collisions.
381  void resolve(std::string &name) const;
382  /// Adds 'name' to set of known names.
383  void add(const std::string &name);
384 
385  private:
386  // For names ending with "_number", a mapping from the prefix to the
387  // largest used number.
389  };
390 };
391 
392 } // GABC_NAMESPACE
393 
394 #endif
Alembic::Abc::OArrayProperty OArrayProperty
Definition: GABC_Util.h:66
UT_Matrix4T< fpreal64 > UT_Matrix4D
GT_API const UT_StringHolder filename
Unsorted map container.
Definition: UT_Map.h:83
GT_API const UT_StringHolder time
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
fpreal getStartTime() const
Get global start and end times, computed when walking the archive.
Definition: GABC_Util.h:133
virtual bool interrupted() const
Allow for interruption of walk.
Definition: GABC_Util.h:118
Alembic::Util::shared_ptr< ObjectReader > ObjectReaderPtr
UT_SharedPtr< ArchiveEventHandler > ArchiveEventHandlerPtr
Definition: GABC_Util.h:69
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
static M44d getM(const UT_Matrix4D &m)
Create an M44d from a UT_Matrix4D.
Definition: GABC_Util.h:253
Alembic::Abc::OScalarProperty OScalarProperty
Definition: GABC_Util.h:65
bool valid() const
Return whether the event handler is wired up to an archive.
Definition: GABC_Util.h:165
static Box3d getBox(const UT_BoundingBox &box)
Create a Box3d from a UT_BoundingBox.
Definition: GABC_Util.h:196
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
void setFilenames(const std::vector< std::string > &f)
Definition: GABC_Util.h:140
bool badArchive() const
Return whether the walking error was caused by a bad Alembic archive.
Definition: GABC_Util.h:145
Alembic::Abc::V3d V3d
Definition: GABC_Util.h:59
Alembic::Abc::OCompoundProperty OCompoundProperty
Definition: GABC_Util.h:64
Matrix44< double > M44d
Definition: ImathMatrix.h:866
#define GABC_NAMESPACE
Definition: GABC_API.h:42
const T * data(void) const
Return the raw matrix data.
Definition: UT_Matrix4.h:1053
Alembic::Abc::Box3d Box3d
Definition: GABC_Util.h:60
static const UT_StringHolder theUserPropsValsAttrib
Definition: GABC_Util.h:368
Vec3< double > V3d
Definition: ImathVec.h:694
bool computedValidTimeRange() const
Returns true if a valid time range has been computed during the walk.
Definition: GABC_Util.h:129
Alembic::Util::shared_ptr< TimeSampling > TimeSamplingPtr
Definition: TimeSampling.h:137
GLfloat f
Definition: glcorearb.h:1925
const std::vector< std::string > & filenames() const
Definition: GABC_Util.h:139
static UT_BoundingBox getBox(const Box3d &box)
Create a UT_BoundingBox from a Box3d.
Definition: GABC_Util.h:202
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:28
virtual bool preProcess(const GABC_IObject &node)
Definition: GABC_Util.h:110
UT_BoundingBoxT< float > UT_BoundingBox
Definition: GEO_Detail.h:43
int64 exint
Definition: SYS_Types.h:116
static UT_Matrix4D getM(const M44d &m)
Create a UT_Matrix4D from an M44d.
Definition: GABC_Util.h:248
GLuint const GLchar * name
Definition: glcorearb.h:785
Class to efficiently find a new name when a collision is detected.
Definition: GABC_Util.h:377
Alembic::Abc::M44d M44d
Definition: GABC_Util.h:61
Box< V3d > Box3d
Definition: ImathBox.h:139
double fpreal
Definition: SYS_Types.h:270
Alembic::Abc::ObjectReaderPtr ObjectReaderPtr
Definition: GABC_Util.h:67
static const UT_StringHolder theUserPropsMetaAttrib
Definition: GABC_Util.h:369
UT_Map< std::string, GABC_OProperty * > PropertyMap
Definition: GABC_Util.h:70
Event functor called when an archive is flushed from the cache.
Definition: GABC_Util.h:158
#define GABC_API
Definition: GABC_API.h:37
static const UT_StringHolder theLockGeometryParameter
Definition: GABC_Util.h:367
std::vector< std::string > PathList
Definition: GABC_Util.h:72
Alembic::Abc::index_t index_t
Definition: GABC_Util.h:53
std::pair< std::string, GABC_OProperty * > PropertyMapInsert
Definition: GABC_Util.h:71
Alembic::Abc::chrono_t chrono_t
Definition: GABC_Util.h:56
Alembic::Abc::ICompoundProperty ICompoundProperty
Definition: GABC_Util.h:63
Alembic::Abc::TimeSamplingPtr TimeSamplingPtr
Definition: GABC_Util.h:57