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_IArchive.h"
33 #include "GABC_IObject.h"
34 #include "GABC_OProperty.h"
35 #include "GABC_Types.h"
36 #include <SYS/SYS_Types.h>
37 #include <UT/UT_BoundingBox.h>
38 #include <UT/UT_JSONParser.h>
39 #include <UT/UT_Matrix4.h>
40 #include <UT/UT_SharedPtr.h>
41 #include <UT/UT_WorkBuffer.h>
42 #include <UT/UT_StringSet.h>
43 
44 class UT_StringArray;
45 
46 namespace GABC_NAMESPACE
47 {
48 
50 {
51 public:
53 
57 
61 
67 
70  typedef std::pair<std::string, GABC_OProperty *> PropertyMapInsert;
71  typedef std::vector<std::string> PathList;
72 
73  /// Class used in traversal of Alembic trees
74  ///
75  /// For standard walking of the tree, you'd likely have a process() method
76  /// like this: @code
77  /// bool process(const GABC_IObject &node)
78  /// {
79  /// doSomething(node);
80  /// return true; // Process other nodes
81  /// }
82  /// @endcode
83  /// However, if you have to pass information to the child nodes, you might
84  /// want to do something like: @code
85  /// bool process(const GABC_IObject &node)
86  /// {
87  /// doSomething(node); // Process this node
88  /// pushState(); // Set information for kids
89  /// walkChildren(node);
90  /// popState(); // Restore parent's state
91  /// return false; // Don't let parent walk kids
92  /// }
93  /// @endcode
95  {
96  public:
98  : myFilenames()
99  , myStartTime(0)
100  , myEndTime(0)
101  , myComputedTimes(false)
102  , myBadArchive(false)
103  {}
104  virtual ~Walker() {}
105 
106  /// @c preProcess() is called on the "root" of the walk. The root may
107  /// @b not be the root of the Alembic file (i.e. when walking lists of
108  /// objects). The @c preProcess() method will be called one time only.
109  virtual bool preProcess(const GABC_IObject &node) { return true; }
110 
111  /// Return true to continue traveral and process the children of the
112  /// given node. Returning false will process the next sibling. Use
113  /// interrupted() to perform an early termination.
114  virtual bool process(const GABC_IObject &node) = 0;
115 
116  /// Allow for interruption of walk
117  virtual bool interrupted() const { return false; }
118 
119  /// Manually walk children of the given node. Returns false if the
120  /// walk was interrupted, true if the walk was completed.
121  bool walkChildren(const GABC_IObject &node);
122 
123  /// Recomputes the time range of the archive given a new object during
124  /// a walk.
125  void computeTimeRange(const GABC_IObject &obj);
126 
127  /// Returns true if a valid time range has been computed during the walk.
129  { return myComputedTimes && myStartTime != myEndTime; }
130 
131  /// Get global start and end times, computed when walking the archive.
133  { return myStartTime; }
135  { return myEndTime; }
136  /// @{
137  /// Access the current filenames
138  const std::vector<std::string> &filenames() const { return myFilenames; }
139  void setFilenames(const std::vector<std::string> &f)
140  { myFilenames = f; }
141  /// @}
142 
143  /// Return whether the walking error was caused by a bad Alembic archive
144  bool badArchive() const { return myBadArchive; }
145 
146  private:
147  std::vector<std::string> myFilenames;
148  fpreal myStartTime;
149  fpreal myEndTime;
150  bool myComputedTimes;
151  bool myBadArchive; // Invalid alembic archive
152 
153  friend class GABC_Util;
154  };
155 
156  /// Event functor called when an archive is flushed from the cache
158  {
159  public:
161  virtual ~ArchiveEventHandler() {}
162 
163  /// Return whether the event handler is wired up to an archive
164  bool valid() const { return myArchive != NULL; }
165 
166  /// Call this method to no longer receive events
167  void stopReceivingEvents();
168 
169  /// This method is called when the archive is cleared. The handler
170  /// will no longer receive any events after the archive is cleared.
171  virtual void cleared(bool purged) = 0;
172 
173  /// @{
174  /// @private
175  /// Methods used to access internal state
176  void setArchivePtr(void *a) { myArchive = a; }
177  const void *archive() const { return myArchive; }
178  /// @}
179  private:
180  void *myArchive;
181  };
182 
183  //
184  // Alembic Namespace Name
185  //
186 
187  /// Get Alembic namespace name as string
188  static const char *getAlembicCompileNamespace();
189 
190  //
191  // Bounding Box Conversion
192  //
193 
194  /// Create a Box3d from a UT_BoundingBox
195  static Box3d getBox(const UT_BoundingBox &box)
196  {
197  return Box3d(V3d(box.xmin(), box.ymin(), box.zmin()),
198  V3d(box.xmax(), box.ymax(), box.zmax()));
199  }
200  /// Create a UT_BoundingBox from a Box3d
201  static UT_BoundingBox getBox(const Box3d &box)
202  {
203  return UT_BoundingBox(box.min[0], box.min[1], box.min[2],
204  box.max[0], box.max[1], box.max[2]);
205  }
206 
207  //
208  // Cache
209  //
210 
211  /// Clear the cache. If no filename is given, the entire cache will be
212  /// cleared.
213  static void clearCache(const char *filename=NULL);
214  /// Set the cache size
215  static void setFileCacheSize(int nfiles);
216  /// Get the file cache size
217  static int fileCacheSize();
218 
219  //
220  // Events
221  //
222 
223  /// Add an event handler to be notified of events on the given
224  /// list of filenames
225  /// The method returns false if the archive hasn't been loaded yet.
226  static bool addEventHandler(const std::vector<std::string> &filenames,
227  const ArchiveEventHandlerPtr &handler);
228 
229  //
230  // Find Objects in Alembic Hierarchy
231  //
232 
233  /// Find a given GABC_IObject in an Alembic file.
234  static GABC_IObject findObject(const std::vector<std::string> &filenames,
235  const std::string &objectpath);
236  static GABC_IObject findObject(const std::vector<std::string> &filenames,
237  ObjectReaderPtr reader);
238  /// Return a list of all the objects in an Alembic file
239  static void getObjectList(PathList &objectpaths,
240  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:65
UT_Matrix4T< fpreal64 > UT_Matrix4D
GT_API const UT_StringHolder filename
Unsorted map container.
Definition: UT_Map.h:107
GT_API const UT_StringHolder time
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
fpreal getStartTime() const
Get global start and end times, computed when walking the archive.
Definition: GABC_Util.h:132
virtual bool interrupted() const
Allow for interruption of walk.
Definition: GABC_Util.h:117
int64 exint
Definition: SYS_Types.h:125
Alembic::Util::shared_ptr< ObjectReader > ObjectReaderPtr
UT_SharedPtr< ArchiveEventHandler > ArchiveEventHandlerPtr
Definition: GABC_Util.h:68
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
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:64
bool valid() const
Return whether the event handler is wired up to an archive.
Definition: GABC_Util.h:164
static Box3d getBox(const UT_BoundingBox &box)
Create a Box3d from a UT_BoundingBox.
Definition: GABC_Util.h:195
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
void setFilenames(const std::vector< std::string > &f)
Definition: GABC_Util.h:139
bool badArchive() const
Return whether the walking error was caused by a bad Alembic archive.
Definition: GABC_Util.h:144
Alembic::Abc::V3d V3d
Definition: GABC_Util.h:58
Alembic::Abc::OCompoundProperty OCompoundProperty
Definition: GABC_Util.h:63
Matrix44< double > M44d
4x4 matrix of double
Definition: ImathMatrix.h:1140
#define GABC_NAMESPACE
Definition: GABC_API.h:42
Alembic::Abc::Box3d Box3d
Definition: GABC_Util.h:59
static const UT_StringHolder theUserPropsValsAttrib
Definition: GABC_Util.h:368
Vec3< double > V3d
Vec3 of double.
Definition: ImathVec.h:852
bool computedValidTimeRange() const
Returns true if a valid time range has been computed during the walk.
Definition: GABC_Util.h:128
Alembic::Util::shared_ptr< TimeSampling > TimeSamplingPtr
Definition: TimeSampling.h:137
GLfloat f
Definition: glcorearb.h:1926
const std::vector< std::string > & filenames() const
Definition: GABC_Util.h:138
static UT_BoundingBox getBox(const Box3d &box)
Create a UT_BoundingBox from a Box3d.
Definition: GABC_Util.h:201
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
virtual bool preProcess(const GABC_IObject &node)
Definition: GABC_Util.h:109
UT_BoundingBoxT< float > UT_BoundingBox
Definition: GEO_Detail.h:41
static UT_Matrix4D getM(const M44d &m)
Create a UT_Matrix4D from an M44d.
Definition: GABC_Util.h:248
GLint i1
Definition: glad.h:2724
GLuint const GLchar * name
Definition: glcorearb.h:786
Class to efficiently find a new name when a collision is detected.
Definition: GABC_Util.h:377
GLdouble t
Definition: glad.h:2397
Alembic::Abc::M44d M44d
Definition: GABC_Util.h:60
Box< V3d > Box3d
3D box of base type double.
Definition: ImathBox.h:167
const T * data() const
Return the raw matrix data.
Definition: UT_Matrix4.h:1163
Alembic::Abc::ObjectReaderPtr ObjectReaderPtr
Definition: GABC_Util.h:66
fpreal64 fpreal
Definition: SYS_Types.h:277
static const UT_StringHolder theUserPropsMetaAttrib
Definition: GABC_Util.h:369
UT_Map< std::string, GABC_OProperty * > PropertyMap
Definition: GABC_Util.h:69
Event functor called when an archive is flushed from the cache.
Definition: GABC_Util.h:157
#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:71
Alembic::Abc::index_t index_t
Definition: GABC_Util.h:52
std::pair< std::string, GABC_OProperty * > PropertyMapInsert
Definition: GABC_Util.h:70
Alembic::Abc::chrono_t chrono_t
Definition: GABC_Util.h:55
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
Alembic::Abc::ICompoundProperty ICompoundProperty
Definition: GABC_Util.h:62
Alembic::Abc::TimeSamplingPtr TimeSamplingPtr
Definition: GABC_Util.h:56