HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
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  : myFilename()
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 filename
138  const std::string &filename() const { return myFilename; }
139  void setFilename(const std::string &f)
140  { myFilename = 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::string myFilename;
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() = 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 filename
224  /// The method returns false if the archive hasn't been loaded yet.
225  static bool addEventHandler(const std::string &filename,
226  const ArchiveEventHandlerPtr &handler);
227 
228  //
229  // Find Objects in Alembic Hierarchy
230  //
231 
232  /// Find a given GABC_IObject in an Alembic file.
233  static GABC_IObject findObject(const std::string &filename,
234  const std::string &objectpath);
235  static GABC_IObject findObject(const std::string &filename,
236  ObjectReaderPtr reader);
237  /// Return a list of all the objects in an Alembic file
238  static const PathList &getObjectList(const std::string &filename,
239  bool include_face_sets=false);
240 
241  //
242  // Matrix Conversion
243  //
244 
245  /// Create a UT_Matrix4D from an M44d
246  static UT_Matrix4D getM(const M44d &m)
247  {
248  return UT_Matrix4D(m.x);
249  }
250  /// Create an M44d from a UT_Matrix4D
251  static M44d getM(const UT_Matrix4D &m)
252  {
253  return M44d((const double (*)[4])m.data());
254  }
255 
256  //
257  // Transforms
258  //
259 
260  /// Get the local transform for a given node in an Alembic file. The @c
261  /// isConstant flag will be true if the local transform is constant (even
262  /// if parent transforms are non-constant).
263  static bool getLocalTransform(
264  const std::string &filename,
265  const std::string &objectpath,
266  fpreal sample_time,
267  UT_Matrix4D &xform,
268  bool &isConstant,
269  bool &inheritsXform);
270  /// Get the world transform for a given node in an Alembic file. If the
271  /// given node is a shape node, the transform up to its parent will be
272  /// computed. If the transform is constant (including all parents), the @c
273  /// isConstant flag will be set.
274  ///
275  /// The method returns false if there was an error computing the transform.
276  static bool getWorldTransform(
277  const std::string &filename,
278  const std::string &objectpath,
279  fpreal sample_time,
280  UT_Matrix4D &xform,
281  bool &isConstant,
282  bool &inheritsXform);
283  /// Get the world transform for a GABC_IObject in an Alembic file. If the
284  /// given node is a shape node, the transform up to its parent will be
285  /// returned.
286  static bool getWorldTransform(
287  const GABC_IObject &object,
288  fpreal sample_time,
289  UT_Matrix4D &xform,
290  bool &isConstant,
291  bool &inheritsXform);
292  /// Test whether an object is static or has an animated transform
293  static bool isTransformAnimated(
294  const GABC_IObject &object);
295 
296  //
297  // Visibility
298  //
299 
300  /// Get the visibility for a GABC_IObject.
301  static GABC_VisibilityType getVisibility(
302  const GABC_IObject &object,
303  fpreal sample_time,
304  bool &animated,
305  bool check_parent);
306 
307  //
308  // Walk Alembic Hierarchy
309  //
310 
311  /// Walk the tree in an alembic file. Returns false if traversal was
312  /// interrupted, otherwise returns true.
313  static bool walk(const std::string &filename, Walker &walker);
314  /// Process a list of unique objects in an Alembic file (including their
315  /// children)
316  static bool walk(const std::string &filename, Walker &walker,
317  const UT_StringArray &objects);
318  static bool walk(const std::string &filename, Walker &walker,
319  const UT_Set<std::string> &objects);
320 
321  //
322  // Alembic Properties
323  //
324 
325  /// Check whether or not an Alembic compound property (like arbGeomParams
326  /// or user properties) is constant/animated over time.
327  static bool isABCPropertyAnimated(ICompoundProperty arb);
328  static bool isABCPropertyConstant(ICompoundProperty arb);
329 
330  //
331  // User Properties
332  //
333 
334  /// Import user properties into two JSON dictionaries, one containing
335  /// values and another containing the metadata for the properties.
336  static bool importUserPropertyDictionary(UT_JSONWriter *data_writer,
337  UT_JSONWriter *meta_writer,
338  const GABC_IObject &obj,
339  fpreal time);
340 
341  /// Export user properties from two JSON dictionaries (one containing
342  /// values, the other containing metadata used to interpret the values)
343  /// to GABC_OProperties, and store them in the given map.
344  static bool exportUserPropertyDictionary(UT_AutoJSONParser &meta_data,
345  UT_AutoJSONParser &vals_data,
346  PropertyMap &up_map,
347  OCompoundProperty *ancestor,
348  GABC_OError &err,
349  const GABC_OOptions &ctx);
350 
354 
355  /// Gets the samples indices (i0 and i1) corresponding to time 't'. The
356  /// bias for blending the samples is returned.
357  static fpreal getSampleIndex(fpreal t, const TimeSamplingPtr &itime,
358  exint nsamp, index_t &i0, index_t &i1);
359 
360  /// Class to efficiently find a new name when a collision is detected.
362  {
363  public:
364  /// Updates 'name' to avoid collisions.
365  void resolve(std::string &name) const;
366  /// Adds 'name' to set of known names.
367  void add(const std::string &name);
368 
369  private:
370  // For names ending with "_number", a mapping from the prefix to the
371  // largest used number.
373  };
374 };
375 
376 } // GABC_NAMESPACE
377 
378 #endif
Alembic::Abc::OArrayProperty OArrayProperty
Definition: GABC_Util.h:65
UT_Matrix4T< fpreal64 > UT_Matrix4D
void setFilename(const std::string &f)
Definition: GABC_Util.h:139
Unsorted map container.
Definition: UT_Map.h:83
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:132
virtual bool interrupted() const
Allow for interruption of walk.
Definition: GABC_Util.h:117
Alembic::Util::shared_ptr< ObjectReader > ObjectReaderPtr
UT_SharedPtr< ArchiveEventHandler > ArchiveEventHandlerPtr
Definition: GABC_Util.h:68
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:251
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:32
Wrapper around hboost::shared_ptr.
Definition: UT_SharedPtr.h:27
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
Definition: ImathMatrix.h:874
#define GABC_NAMESPACE
Definition: GABC_API.h:42
const T * data(void) const
Return the raw matrix data.
Definition: UT_Matrix4.h:959
Alembic::Abc::Box3d Box3d
Definition: GABC_Util.h:59
static const UT_StringHolder theUserPropsValsAttrib
Definition: GABC_Util.h:352
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:128
Alembic::Util::shared_ptr< TimeSampling > TimeSamplingPtr
Definition: TimeSampling.h:137
GLfloat f
Definition: glcorearb.h:1925
static UT_BoundingBox getBox(const Box3d &box)
Create a UT_BoundingBox from a Box3d.
Definition: GABC_Util.h:201
virtual bool preProcess(const GABC_IObject &node)
Definition: GABC_Util.h:109
UT_BoundingBoxT< float > UT_BoundingBox
Definition: GEO_Detail.h:43
int64 exint
Definition: SYS_Types.h:115
static UT_Matrix4D getM(const M44d &m)
Create a UT_Matrix4D from an M44d.
Definition: GABC_Util.h:246
const std::string & filename() const
Definition: GABC_Util.h:138
GLuint const GLchar * name
Definition: glcorearb.h:785
Class to efficiently find a new name when a collision is detected.
Definition: GABC_Util.h:361
Alembic::Abc::M44d M44d
Definition: GABC_Util.h:60
Box< V3d > Box3d
Definition: ImathBox.h:139
double fpreal
Definition: SYS_Types.h:269
Alembic::Abc::ObjectReaderPtr ObjectReaderPtr
Definition: GABC_Util.h:66
static const UT_StringHolder theUserPropsMetaAttrib
Definition: GABC_Util.h:353
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:351
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
Alembic::Abc::ICompoundProperty ICompoundProperty
Definition: GABC_Util.h:62
Alembic::Abc::TimeSamplingPtr TimeSamplingPtr
Definition: GABC_Util.h:56