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
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
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 const PathList &getObjectList(const std::vector<std::string> &filenames,
240  bool include_face_sets=false);
241 
242  //
243  // Matrix Conversion
244  //
245 
246  /// Create a UT_Matrix4D from an M44d
247  static UT_Matrix4D getM(const M44d &m)
248  {
249  return UT_Matrix4D(m.x);
250  }
251  /// Create an M44d from a UT_Matrix4D
252  static M44d getM(const UT_Matrix4D &m)
253  {
254  return M44d((const double (*)[4])m.data());
255  }
256 
257  //
258  // Transforms
259  //
260 
261  /// Get the local transform for a given node in an Alembic file. The @c
262  /// isConstant flag will be true if the local transform is constant (even
263  /// if parent transforms are non-constant).
264  static bool getLocalTransform(
265  const std::vector<std::string> &filenames,
266  const std::string &objectpath,
267  fpreal sample_time,
268  UT_Matrix4D &xform,
269  bool &isConstant,
270  bool &inheritsXform);
271  /// Get the world transform for a given node in an Alembic file. If the
272  /// given node is a shape node, the transform up to its parent will be
273  /// computed. If the transform is constant (including all parents), the @c
274  /// isConstant flag will be set.
275  ///
276  /// The method returns false if there was an error computing the transform.
277  static bool getWorldTransform(
278  const std::vector<std::string> &filenames,
279  const std::string &objectpath,
280  fpreal sample_time,
281  UT_Matrix4D &xform,
282  bool &isConstant,
283  bool &inheritsXform);
284  /// Get the world transform for a GABC_IObject in an Alembic file. If the
285  /// given node is a shape node, the transform up to its parent will be
286  /// returned.
287  static bool getWorldTransform(
288  const GABC_IObject &object,
289  fpreal sample_time,
290  UT_Matrix4D &xform,
291  bool &isConstant,
292  bool &inheritsXform);
293  /// Test whether an object is static or has an animated transform
294  static bool isTransformAnimated(
295  const GABC_IObject &object);
296 
297  //
298  // Visibility
299  //
300 
301  /// Get the visibility for a GABC_IObject.
302  static GABC_VisibilityType getVisibility(
303  const GABC_IObject &object,
304  fpreal sample_time,
305  bool &animated,
306  bool check_parent);
307 
308  /// Get the bounding box for a GABC_IObject.
309  static bool getBoundingBox(
310  const GABC_IObject &object,
311  fpreal sample_time,
313  bool &isconst);
314 
315  //
316  // Walk Alembic Hierarchy
317  //
318 
319  /// Walk the tree in an alembic file. Returns false if traversal was
320  /// interrupted, otherwise returns true.
321  static bool walk(const std::vector<std::string> &filenames, Walker &walker);
322  /// Process a list of unique objects in an Alembic file (including their
323  /// children)
324  static bool walk(const std::vector<std::string> &filenames, Walker &walker,
325  const UT_StringArray &objects);
326  static bool walk(const std::vector<std::string> &filenames, Walker &walker,
327  const UT_Set<std::string> &objects);
328 
329  //
330  // Alembic Properties
331  //
332 
333  /// Check whether or not an Alembic compound property (like arbGeomParams
334  /// or user properties) is constant/animated over time.
335  static bool isABCPropertyAnimated(ICompoundProperty arb);
336  static bool isABCPropertyConstant(ICompoundProperty arb);
337 
338  //
339  // User Properties
340  //
341 
342  /// Import user properties into two JSON dictionaries, one containing
343  /// values and another containing the metadata for the properties.
344  static bool importUserPropertyDictionary(UT_JSONWriter *vals_writer,
345  UT_JSONWriter *meta_writer,
346  const GABC_IObject &obj,
347  fpreal time);
348 
349  /// Export user properties from two JSON dictionaries (one containing
350  /// values, the other containing metadata used to interpret the values)
351  /// to GABC_OProperties, and store them in the given map.
352  static void exportUserPropertyDictionary(UT_AutoJSONParser &meta_data,
353  UT_AutoJSONParser &vals_data,
354  PropertyMap &up_map,
355  OCompoundProperty *ancestor,
356  GABC_OError &err,
357  const GABC_OOptions &ctx,
358  const GABC_LayerOptions &lopt,
360 
361  static void getUserPropertyTokens(UT_SortedStringSet &tokens,
362  UT_AutoJSONParser &meta_data,
363  UT_AutoJSONParser &vals_data,
364  GABC_OError &err);
365 
369 
370  /// Gets the samples indices (i0 and i1) corresponding to time 't'. The
371  /// bias for blending the samples is returned.
372  static fpreal getSampleIndex(fpreal t, const TimeSamplingPtr &itime,
373  exint nsamp, index_t &i0, index_t &i1);
374 
375  /// Class to efficiently find a new name when a collision is detected.
377  {
378  public:
379  /// Updates 'name' to avoid collisions.
380  void resolve(std::string &name) const;
381  /// Adds 'name' to set of known names.
382  void add(const std::string &name);
383 
384  private:
385  // For names ending with "_number", a mapping from the prefix to the
386  // largest used number.
388  };
389 };
390 
391 } // GABC_NAMESPACE
392 
393 #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
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
static M44d getM(const UT_Matrix4D &m)
Create an M44d from a UT_Matrix4D.
Definition: GABC_Util.h:252
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:35
GLuint const GLchar * name
Definition: glcorearb.h:786
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
GLdouble GLdouble t
Definition: glew.h:1403
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:367
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
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:43
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLhandleARB obj
Definition: glew.h:6266
static UT_Matrix4D getM(const M44d &m)
Create a UT_Matrix4D from an M44d.
Definition: GABC_Util.h:247
Class to efficiently find a new name when a collision is detected.
Definition: GABC_Util.h:376
Alembic::Abc::M44d M44d
Definition: GABC_Util.h:60
Box< V3d > Box3d
3D box of base type double.
Definition: ImathBox.h:167
GLsizei const GLint box[]
Definition: glew.h:11654
const T * data() const
Return the raw matrix data.
Definition: UT_Matrix4.h:1125
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:368
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:366
std::vector< std::string > PathList
Definition: GABC_Util.h:71
const GLdouble * m
Definition: glew.h:9166
GLfloat f
Definition: glcorearb.h:1926
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