HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GABC_GEOWalker.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_GEOWalker__
29 #define __GABC_GEOWalker__
30 
31 #include "GABC_API.h"
32 #include "GABC_IError.h"
33 #include "GABC_Include.h"
34 #include "GABC_Util.h"
35 #include <GA/GA_Handle.h>
36 #include <GU/GU_Detail.h>
37 #include <stack>
38 
39 class GU_PrimPacked;
40 class UT_StringArray;
41 
42 namespace GABC_NAMESPACE
43 {
44 /// @brief Walk an Alembic tree to create Houdini geometry
45 ///
46 /// To convert an Alembic file to Houdini geometry, the code would look
47 /// something like this: @code
48 /// GU_Detail gdp;
49 /// GABC_GEOWalker walker(gdp);
50 ///
51 /// walker.setFrame(frame, fps); // Specify the time sample
52 /// // Set any other parameters you might care about
53 /// walker.setBuildAbcPrim(true);
54 ///
55 /// // Now, walker the Alembic file
56 /// if (!GABC_Util::walker(filename, walker))
57 /// addError();
58 /// @endcode
60 {
61 public:
64 
65  /// Test by loading "test.abc" in the current directory and saving
66  /// "test.geo" as output.
67  static void test();
68 
69  enum GroupMode
70  {
71  ABC_GROUP_NONE, // No geometry groups
72  ABC_GROUP_SHAPE_NODE, // Name geometry group based on shape node
73  ABC_GROUP_XFORM_NODE, // Name geometry group based on transform node
74  ABC_GROUP_SHAPE_BASENAME, // Group by last path component
75  ABC_GROUP_XFORM_BASENAME, // Group by last xform node path component
76  };
77 
79  {
80  BOX_CULL_IGNORE, // Ignore bounding box
81  BOX_CULL_ANY_INSIDE, // Add if boxes overlap
82  BOX_CULL_INSIDE, // Add if box is entirely inside
83  BOX_CULL_ANY_OUTSIDE, // Add if any of object is outside filter box
84  BOX_CULL_OUTSIDE, // Add if object is entirely outside bounds
85  };
86 
88  {
89  SIZE_CULL_IGNORE, // Ignore size constraint
90  SIZE_CULL_AREA, // Add if bounding box area passes size test
91  SIZE_CULL_RADIUS, // Add if bounding box radius passes size test
92  SIZE_CULL_VOLUME, // Add if bounding box volume passes size test
93  };
94 
96  {
97  SIZE_COMPARE_LESSTHAN, // Add if bounding box is smaller than the reference size
98  SIZE_COMPARE_GREATERTHAN, // Add if bounding box is larger than the reference size
99  };
100 
101  enum LoadMode
102  {
103  LOAD_ABC_PRIMITIVES, // Load Alembic primitives
104  LOAD_ABC_UNPACKED, // Load ABC then unpack
105  LOAD_HOUDINI_PRIMITIVES, // Load houdini primitives
106  LOAD_HOUDINI_POINTS, // Load point cloud for objects
107  LOAD_HOUDINI_BOXES, // Load Bounds as Houdini geometry
108  };
109 
110  /// Animating object filter
111  enum AFilter
112  {
113  ABC_AFILTER_STATIC, // Only static geometry
114  ABC_AFILTER_DEFORMING, // Only deforming geometry
115  ABC_AFILTER_TRANSFORMING, // Only transforming geometry
116  ABC_AFILTER_ANIMATING, // Only animating geometry
117  ABC_AFILTER_ALL // All geometry
118  };
119 
120  /// Geometry type filter
121  enum GFilter
122  {
123  ABC_GFILTER_POLYMESH = 0x01, // Load primitives that are polygons
124  ABC_GFILTER_CURVES = 0x02, // Load primitives that are curves
125  ABC_GFILTER_NUPATCH = 0x04, // Load primitives that are NURBS
126  ABC_GFILTER_POINTS = 0x08, // Load primitives that are points
127  ABC_GFILTER_SUBD = 0x10, // Load primitives that are subdivision surfaces
128  ABC_GFILTER_ALL = (ABC_GFILTER_POLYMESH | // Load all primitives
129  ABC_GFILTER_CURVES |
130  ABC_GFILTER_NUPATCH |
131  ABC_GFILTER_POINTS |
132  ABC_GFILTER_SUBD)
133  };
134 
135  /// How Alembic delayed load primitives are attached to GA points
137  {
138  ABCPRIM_SHARED_POINT, // All primitives share a GA point
139  ABCPRIM_UNIQUE_POINT, // Each prim has its own point
140  ABCPRIM_CENTROID_POINT, // Place point at centroid
141  };
142 
143  /// Whether to build polysoup primitives when it's possible
145  {
146  ABC_POLYSOUP_NONE, // Build polygons only
147  ABC_POLYSOUP_POLYMESH, // Polygon Mesh primitives only
148  ABC_POLYSOUP_SUBD, // Polygon soups & subdivision primitives
149  };
150 
151  /// How to load user properties
153  {
154  UP_LOAD_NONE, // Don't load user properties
155  UP_LOAD_DATA, // Load only user property values
156  UP_LOAD_ALL, // Load user property values and metadata
157  };
158 
159  // Helper class stores world transform state.
161  {
162  public:
165 
166  private:
167  friend class GABC_GEOWalker;
168 
169  void push(const M44d &m, bool c)
170  {
171  myM = m;
172  myC = c;
173  }
174  void pop(M44d &m, bool &c) const
175  {
176  m = myM;
177  c = myC;
178  }
179 
180  M44d myM;
181  bool myC;
182  };
183 
184  GABC_GEOWalker(GU_Detail &gdp, GABC_IError &err,
185  bool record_time_range=false);
186  virtual ~GABC_GEOWalker();
187 
188  virtual bool preProcess(const GABC_IObject &node);
189 
190  virtual bool process(const GABC_IObject &node);
191  virtual bool interrupted() const;
192 
193  /// Quickly update ABC primitives with the new time
194  void updateAbcPrims();
195 
196  /// @{
197  /// Get state
198  GU_Detail &detail() const
199  { return myDetail; }
201  { return myErrorHandler; }
202  const UT_String &objectPattern() const
203  { return myObjectPattern; }
205  { return myExcludeObjects; }
207  { return myNameMapPtr; }
209  { return myFacesetAttribute; }
210 
211  fpreal time() const { return myTime; }
212  bool includeXform() const { return myIncludeXform; }
213  bool useVisibility() const { return myUseVisibility; }
214  bool staticTimeZero() const { return myStaticTimeZero; }
215  bool reusePrimitives() const { return myReusePrimitives; }
216  bool buildLocator() const { return myBuildLocator; }
217  LoadUserPropsMode loadUserProps() const { return myLoadUserProps; }
218  LoadMode loadMode() const { return myLoadMode; }
219  bool buildAbcPrim() const
220  { return myLoadMode == LOAD_ABC_PRIMITIVES; }
221  bool buildAbcShape() const { return myBuildAbcShape; }
222  bool buildAbcXform() const { return myBuildAbcXform; }
223  bool pathAttributeChanged() const { return myPathAttributeChanged; }
224  /// @}
225 
226  /// Get a sample selector for the given time
228  { return ISampleSelector(myTime); }
229 
230 
231  /// Keeps track of the number of geometry points added in traversal
232  GA_Offset pointCount() const { return myPointCount; }
233  /// Keeps track of the number of geometry vertices added in traversal
234  GA_Offset vertexCount() const { return myVertexCount; }
235  /// Keeps track of the number of geometry primitives added in traversal
236  GA_Offset primitiveCount() const { return myPrimitiveCount; }
237  /// True if *all* shapes and transforms are constant
238  bool isConstant() const { return myIsConstant; }
239  /// True if *all* shapes have constant topology
240  bool topologyConstant() const { return myTopologyConstant; }
241  /// True if the transform from the root to the current node is constant
242  bool transformConstant() const { return myTransformConstant; }
243  /// True if *all* transforms are constant
244  bool allTransformConstant() const { return myAllTransformConstant; }
245  bool rebuiltNURBS() const { return myRebuiltNURBS; }
246 
247  /// @{
248  /// Set state
249  void setExcludeObjects(const char *s);
250  void setObjectPattern(const char *s)
251  { myObjectPattern.harden(s); }
252  void setFacesetAttribute(const char *s)
253  { myFacesetAttribute.harden(s); }
255  { myNameMapPtr = ptr; }
256  void setPathAttribute(const GA_RWAttributeRef &a);
257  void setTime(fpreal t) { myTime = t; }
258  void setFrame(fpreal f, fpreal fps) { myTime = f/fps; }
259  void setIncludeXform(bool v) { myIncludeXform = v; }
260  void setUseVisibility(bool v) { myUseVisibility = v; }
261  void setStaticTimeZero(bool v) { myStaticTimeZero = v; }
262  void setReusePrimitives(bool v,
263  GA_Offset pts = GA_Offset(0),
264  GA_Offset verts = GA_Offset(0),
265  GA_Offset prims = GA_Offset(0));
266  void setBuildLocator(bool v) { myBuildLocator = v; }
267  void setLoadMode(LoadMode mode) { myLoadMode = mode; }
268  void setBuildAbcShape(bool v) { myBuildAbcShape = v; }
269  void setBuildAbcXform(bool v) { myBuildAbcXform = v; }
270  void setPathAttributeChanged(bool v) { myPathAttributeChanged = v; }
271  void setUserProps(LoadUserPropsMode m) { myLoadUserProps = m; }
272  void setGroupMode(GroupMode m) { myGroupMode = m; }
273  void setAnimationFilter(AFilter m) { myAnimationFilter = m; }
274  void setGeometryFilter(int m) { myGeometryFilter = m; }
275  void setBounds(BoxCullMode mode, const UT_BoundingBox &box);
276  void setPointMode(AbcPrimPointMode mode,
277  GA_Offset shared_point = GA_INVALID_OFFSET);
278  void setPolySoup(AbcPolySoup soup) { myPolySoup = soup; }
279  void setViewportLOD(GEO_ViewportLOD v) { myViewportLOD = v; }
281  { mySizeCullMode = mode; mySizeCompare = cmp; mySize = size; }
282  /// @}
283 
284  /// @{
285  /// State accessors
286  AbcPolySoup polySoup() const { return myPolySoup; }
287  GEO_ViewportLOD viewportLOD() const { return myViewportLOD; }
288  /// @}
289 
290  /// @{
291  /// Primitive group to store subdivision primitives
292  GA_PrimitiveGroup *subdGroup() const { return mySubdGroup; }
293  void setSubdGroup(GA_PrimitiveGroup *g) { mySubdGroup = g; }
294  /// @}
295 
296  /// @{
297  /// State modified during traversal
298  void setNonConstant() { myIsConstant = false; }
299  void setNonConstantTopology() { myTopologyConstant = false; }
300  /// @}
301 
302  /// Keep track of added points/vertices/primitives. This should be called
303  /// after the primitives have been added to the detail.
304  void trackPtVtxPrim(const GABC_IObject &obj,
305  exint npoints, exint nvertex, exint nprim,
306  bool do_transform);
307 
308  /// Push transform during traversal. If the walker is set to include the
309  /// full transforms, the transform passed in will be combined with the
310  /// current transform, otherwise the transform passed in will replace the
311  /// transform state in the walker. The @c const_xform flag indicates
312  /// whether the transform node is constant or not. The current state will
313  /// be stashed in @c stash_prev and should be passed to popTransform().
314  void pushTransform(const M44d &xform, bool const_xform,
315  TransformState &prev_state,
316  bool inheritXforms);
317  /// Pop transform in traveral, pass in the value stored in pushTransform().
318  void popTransform(const TransformState &prev_state);
319 
320  /// Get the current transform
321  const M44d &getTransform() const { return myMatrix; }
322 
323  /// Get the group name associated with an GABC_IObject
324  bool getGroupName(UT_String &str, const GABC_IObject &obj) const;
325 
326  /// Translate attribute names. This will return false if the attribute is
327  /// not allowed.
328  bool translateAttributeName(GA_AttributeOwner own, UT_String &name);
329 
330  /// @{
331  /// Access information about last poly/subd/curve mesh loaded
332  GA_Size lastFaceCount() const { return myLastFaceCount; }
333  GA_Offset lastFaceStart() const { return myLastFaceStart; }
334  void trackLastFace(GA_Size nfaces);
335  void trackSubd(GA_Size nfaces);
336  /// @}
337 
338  /// Get a GA_Offset to which the Alembic delayed load primitive should be
339  /// attached. This may return an invalid offset
340  GA_Offset getPointForAbcPrim();
341  void setPointLocation(GU_PrimPacked *abc, GA_Offset offset) const;
342 
343 protected:
344  /// Verify the object matches filters before generating geometry
345  bool filterObject(const GABC_IObject &obj) const;
346 
347 private:
348  bool matchObjectName(const GABC_IObject &obj) const;
349  bool matchAnimationFilter(const GABC_IObject &obj) const;
350  bool matchGeometryFilter(const GABC_IObject &obj) const;
351  bool matchBounds(const GABC_IObject &obj) const;
352  bool matchSize(const GABC_IObject &obj) const;
353  bool abcPrimPointMode() const
354  { return myAbcPrimPointMode; }
355  GA_Offset abcSharedPoint() const
356  { return myAbcSharedPoint; }
357  void recordTimeRange(const GABC_IObject &node);
358 
359  // Enum values
360  AbcPolySoup myPolySoup;
361  AbcPrimPointMode myAbcPrimPointMode;
362  AFilter myAnimationFilter; // Animating object filter
363  int myGeometryFilter; // Geometry type filter
364  BoxCullMode myBoxCullMode;
365  SizeCullMode mySizeCullMode;
366  SizeCompare mySizeCompare;
367  GroupMode myGroupMode; // How to construct group names
368  LoadMode myLoadMode; // Build Alembic primitives
369  LoadUserPropsMode myLoadUserProps; // How to load user properties
370 
371  GA_Offset myAbcSharedPoint;
372  GA_Offset myLastFaceStart; // Start of faces in last mesh
373  GA_PrimitiveGroup *mySubdGroup;
374  GA_RWHandleS myPathAttribute;
375  GA_Size myLastFaceCount; // Number of faces in last mesh
376  GABC_IError &myErrorHandler;
377  GEO_PackedNameMapPtr myNameMapPtr; // Attribute map for ABC primitives
378  GEO_ViewportLOD myViewportLOD;
379  GU_Detail &myDetail;
380  M44d myMatrix;
381  UT_BoundingBox myCullBox;
382  UT_Interrupt *myBoss;
383  UT_String myFacesetAttribute;
384  UT_String myObjectPattern;
385  UT_StringArray myExcludeObjects;
386  std::stack<GABC_VisibilityType> myVisibilityStack;
387  UT_Set<std::string> myVisited;
388 
389  fpreal myTime; // Alembic evaluation time
390  fpreal mySize;
391 
392  GA_Offset myPointCount; // Points added
393  GA_Offset myPrimitiveCount; // Primitive's added count
394  GA_Offset myVertexCount; // Vertices added
395  int myBossId;
396  bool myBuildAbcXform; // Build primitives for transforms
397  bool myBuildAbcShape; // Build primitives for transforms
398  bool myBuildLocator; // Whether to build Maya locators
399  bool myIncludeXform; // Transform geometry
400  bool myPathAttributeChanged; // Whether path attrib name changed
401  bool myReusePrimitives; // Reuse primitives in input geometry
402  bool myUseVisibility; // Use visibility
403  bool myStaticTimeZero; // All static objects have frame=0
404  bool myRecordTimeRange;
405 
406  // Modified during traversal
407  bool myIsConstant; // Whether all objects are constant
408  bool myTopologyConstant; // Whether topology is constant
409  bool myTransformConstant; // All xforms down the tree are const
410  bool myAllTransformConstant; // All transforms in scene are const
411  bool myRebuiltNURBS; // Whether NURBS were rebuilt
412 };
413 }
414 
415 #endif
void setFacesetAttribute(const char *s)
cvex test(vector P=0;int unbound=3;export float s=0;export vector Cf=0;)
Definition: test.vfl:11
AFilter
Animating object filter.
const GLdouble * v
Definition: glcorearb.h:836
void setFrame(fpreal f, fpreal fps)
png_voidp ptr
Definition: png.h:2145
const GEO_PackedNameMapPtr & nameMapPtr() const
GLboolean GLboolean g
Definition: glcorearb.h:1221
GA_PrimitiveGroup * subdGroup() const
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
void setUserProps(LoadUserPropsMode m)
bool allTransformConstant() const
True if all transforms are constant.
AbcPolySoup polySoup() const
Read-Write string handle.
Definition: GA_Handle.h:942
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:211
bool topologyConstant() const
True if all shapes have constant topology.
AbcPrimPointMode
How Alembic delayed load primitives are attached to GA points.
Matrix44< double > M44d
Definition: ImathMatrix.h:874
GLsizeiptr size
Definition: glcorearb.h:663
#define GABC_NAMESPACE
Definition: GABC_API.h:42
#define GA_INVALID_OFFSET
Definition: GA_Types.h:654
GA_Offset lastFaceStart() const
GA_Size GA_Offset
Definition: GA_Types.h:617
LoadUserPropsMode loadUserProps() const
This class provides a way to manage a reference to an attribute permitting Read-Write access...
GLfloat f
Definition: glcorearb.h:1925
ISampleSelector timeSample() const
Get a sample selector for the given time.
void setNameMapPtr(const GEO_PackedNameMapPtr &ptr)
int64 exint
Definition: SYS_Types.h:115
void setSizeCullMode(SizeCullMode mode, SizeCompare cmp, fpreal size)
GA_Offset primitiveCount() const
Keeps track of the number of geometry primitives added in traversal.
const UT_String & objectPattern() const
GLintptr offset
Definition: glcorearb.h:664
void setObjectPattern(const char *s)
const UT_String & facesetAttribute() const
GLuint const GLchar * name
Definition: glcorearb.h:785
GFilter
Geometry type filter.
GA_Offset vertexCount() const
Keeps track of the number of geometry vertices added in traversal.
AbcPolySoup
Whether to build polysoup primitives when it's possible.
GLenum mode
Definition: glcorearb.h:98
const M44d & getTransform() const
Get the current transform.
int cmp(T a, T b)
Definition: ImathFun.h:119
LoadUserPropsMode
How to load user properties.
GA_AttributeOwner
Definition: GA_Types.h:33
double fpreal
Definition: SYS_Types.h:269
bool isConstant() const
True if all shapes and transforms are constant.
GEO_ViewportLOD
#define GABC_API
Definition: GABC_API.h:37
void setLoadMode(LoadMode mode)
GA_Offset pointCount() const
Keeps track of the number of geometry points added in traversal.
bool transformConstant() const
True if the transform from the root to the current node is constant.
Walk an Alembic tree to create Houdini geometry.
GEO_ViewportLOD viewportLOD() const
void setPolySoup(AbcPolySoup soup)
GU_Detail & detail() const
void setViewportLOD(GEO_ViewportLOD v)
void setSubdGroup(GA_PrimitiveGroup *g)
const UT_StringArray & excludeObjects() const