HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_USD.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef _GUSD_GU_USD_H_
25 #define _GUSD_GU_USD_H_
26 
27 #include "api.h"
28 #include "defaultArray.h"
29 #include "stageCache.h"
30 #include "USD_Traverse.h"
31 #include "USD_Utils.h"
32 #include "GU_PackedUSD.h"
33 
34 #include <GA/GA_Attribute.h>
35 #include <GA/GA_Handle.h>
36 #include <GA/GA_Names.h>
37 #include <GA/GA_Range.h>
38 #include <UT/UT_ErrorManager.h>
39 #include <UT/UT_StringHolder.h>
40 
41 #include "pxr/pxr.h"
42 
43 class GA_AttributeFilter;
44 class GT_RefineParms;
45 class GU_Detail;
46 class GU_PrimPacked;
47 
49 
50 /** The default names of the USD ref attributes.
51  @{ */
52 #define GUSD_PATH_ATTR "usdpath"
53 #define GUSD_PRIMPATH_ATTR "usdprimpath"
54 #define GUSD_FRAME_ATTR "frame"
55 #define GUSD_VARIANTS_ATTR "usdvariants"
56 #define GUSD_CONSTRAINT_ATTR "usdconstraint"
57 #define GUSD_PURPOSE_ATTR "usdpurpose"
58 #define GUSD_ACTIVE_ATTR "usdactive"
59 #define GUSD_VISIBLE_ATTR "usdvisible"
60 
61 #define GUSD_OVERTRANSFORMS_ATTR "usdovertransforms"
62 #define GUSD_OVERPOINTS_ATTR "usdoverpoints"
63 #define GUSD_OVERPRIMVARS_ATTR "usdoverprimvars"
64 #define GUSD_OVERALL_ATTR "usdoverall"
65 
66 #define GUSD_WRITESTATICTOPOLOGY_ATTR "usdwritestatictopology"
67 #define GUSD_WRITESTATICPRIMVARS_ATTR "usdwritestaticprimvars"
68 #define GUSD_WRITESTATICGEO_ATTR "usdwritestaticgeo"
69 /** @} */
70 
71 /** USD-related options for GT_RefineParms.
72  @{ */
73 #define GUSD_REFINE_ADDPATHATTRIB "usd:addPathAttribute"
74 #define GUSD_REFINE_PATHATTRIB "usd:pathAttribute"
75 #define GUSD_REFINE_ADDPRIMPATHATTRIB "usd:addPrimPathAttribute"
76 #define GUSD_REFINE_PRIMPATHATTRIB "usd:primPathAttribute"
77 #define GUSD_REFINE_ADDINSTANCELEVELATTRIB "usd:addInstanceLevelAttrib"_UTsh
78 #define GUSD_REFINE_INSTANCELEVELATTRIB "usd:instanceLevelAttrib"_UTsh
79 #define GUSD_REFINE_ADDMATERIALPATHATTRIB "usd:addMaterialPathAttribute"
80 #define GUSD_REFINE_ADDVISIBILITYATTRIB "usd:addVisibilityAttribute"
81 #define GUSD_REFINE_IMPORTCOMPUTEDVISIBILITY "usd:importComputedVisibility"
82 #define GUSD_REFINE_ADDXFORMATTRIB "usd:addXformAttribute"
83 #define GUSD_REFINE_NONTRANSFORMINGPATTERN "usd:nonTransformingPrimvarPattern"
84 #define GUSD_REFINE_PRIMVARPATTERN "usd:primvarPattern"
85 #define GUSD_REFINE_IMPORTINHERITEDPRIMVARS "usd:importInheritedPrimvars"
86 #define GUSD_REFINE_ATTRIBUTEPATTERN "usd:attributePattern"
87 #define GUSD_REFINE_TRANSLATESTTOUV "usd:translateSTtoUV"
88 #define GUSD_REFINE_ITERATIONS "usd:iterations"_UTsh
89 #define GUSD_REFINE_UNPACKTOPOLYGONS "usd:unpacktopolygons"_UTsh
90 #define GUSD_REFINE_PIVOTLOCATION "usd:pivotlocation"_UTsh
91 /** @} */
92 
93 /** Set of helpers for working with ranges of prims/points, etc.*/
95 {
96 public:
97  /** Compute an array of offsets from a range.*/
98  static bool OffsetArrayFromRange(const GA_Range& r,
100 
101  /** Compute an array mapping offset->range_index for the given range.*/
102  static bool ComputeRangeIndexMap(const GA_Range& r,
103  GA_OffsetArray& indexMap);
104 
105  static bool
106  BindPrims(GusdStageCacheReader& cache,
107  UT_Array<UsdPrim>& prims,
108  const GA_Detail& gd,
109  const GA_Range& rng,
110  UT_Array<SdfPath>* variants=nullptr,
111  GusdDefaultArray<GusdPurposeSet>* purposes=nullptr,
112  GusdDefaultArray<UsdTimeCode>* times=nullptr,
114 
115  /** Bind prims from references defined in the given attributes.
116  This creates an entry in @a prims for each entry in the given range,
117  mapped to the corresponding prim.
118  If @a variants is non-null, resolved variant paths are stored
119  in the given array.*/
120  static bool
121  BindPrimsFromAttrs(GusdStageCacheReader& cache,
122  UT_Array<UsdPrim>& prims,
123  const GA_Range& rng,
124  const GA_Attribute& pathAttr,
125  const GA_Attribute& primPathAttr,
126  const GA_Attribute* variantsAttr,
127  UT_Array<SdfPath>* variants=nullptr,
129 
130  static bool
131  BindPrimsFromPackedPrims(UT_Array<UsdPrim>& prims,
132  const GA_Range& rng,
133  UT_Array<SdfPath>* variants=nullptr,
134  UT_Array<GusdPurposeSet>* purposes=nullptr,
136 
137  static bool GetTimeCodesFromAttr(const GA_Range& rng,
138  const GA_Attribute& attr,
139  UT_Array<UsdTimeCode>& times);
140 
141  static bool GetTimeCodesFromPackedPrims(const GA_Range& rng,
142  UT_Array<UsdTimeCode>& times);
143 
144  /** Given a string attribute that represents prim paths,
145  return an array of actual prim paths.
146  @{ */
147  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
148  UT_Array<SdfPath>& paths,
150 
151  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
152  const GA_Range& rng,
153  UT_Array<SdfPath>& paths,
155  /** @} */
156 
157 
158  /** Givena string attribute, return an array of tokens.
159  @{ */
160  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
161  UT_Array<TfToken>& tokens,
162  const char* nameSpace=nullptr);
163 
164  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
165  const GA_Range& rng,
166  UT_Array<TfToken>& tokens,
167  const char* nameSpace=nullptr);
168  /** @} */
169 
170 
171  /** Append points to a detail that represent references to prims.
172  The point offsets are contiguous, and the offset of the first
173  point is returned. If any failures occur, and invalid
174  offset is returned.*/
175  static GA_Offset AppendRefPoints(
176  GU_Detail& gd,
177  const UT_Array<UsdPrim>& prims,
178  const char* pathAttrName=GUSD_PATH_ATTR,
179  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
180 
181  typedef GU_PrimPacked* (*PackedPrimBuildFunc)(
182  GU_Detail& detail,
183  const UT_StringHolder& fileName,
184  const SdfPath& primPath,
185  const UsdTimeCode& frame,
186  const char* lod,
187  const GusdPurposeSet purposes );
188 
189  /** Register a function to be used by AppendPackedPrims to build
190  a packed prim of the given type. */
191  static void RegisterPackedPrimBuildFunc( const TfToken& typeName,
192  PackedPrimBuildFunc func );
193 
194  /** Append packed prims to the given detail that reference the
195  given prims with the given variants. */
196  static bool AppendPackedPrims(
197  GU_Detail& gd,
198  const UT_Array<UsdPrim>& prims,
199  const UT_Array<SdfPath>& variants,
200  const GusdDefaultArray<UsdTimeCode>& times,
202  const GusdDefaultArray<GusdPurposeSet>& purposes,
204  /** More specialized function for appending prims to the given detail
205  from a LOP node where the variants are already chosen, and the LOP
206  node path needs to be set as the file path on the packed prim. */
207  static bool AppendPackedPrimsFromLopNode(
208  GU_Detail& gd,
209  const UT_Array<UsdPrim>& prims,
210  const GusdDefaultArray<UT_StringHolder> &stageids,
211  const GusdDefaultArray<UsdTimeCode>& times,
213  const GusdDefaultArray<GusdPurposeSet>& purposes,
215 
217 
218  /** Append prims @a prims, as an expansion of prims defined on
219  @a srcRange. The prim index pairs provide the prim found in
220  the expansion, and the index of the prim in the source range
221  whose expansion produced that prim.
222  Attributes and groups matching @a filter are copied from the source
223  to the newly created ref points.*/
224  static GA_Offset AppendExpandedRefPoints(
225  GU_Detail& gd,
226  const GA_Detail& srcGd,
227  const GA_Range& srcRng,
228  const UT_Array<PrimIndexPair>& prims,
229  const GA_AttributeFilter& filter,
230  const char* pathAttrName=GUSD_PATH_ATTR,
231  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
232 
233  static bool AppendExpandedPackedPrims(
234  GU_Detail& gd,
235  const GA_Detail& srcGd,
236  const GA_Range& srcRng,
237  const UT_Array<PrimIndexPair>& primIndexPairs,
238  const UT_Array<SdfPath>& variants,
239  const GusdDefaultArray<UsdTimeCode>& times,
240  const GA_AttributeFilter& filter,
241  bool unpackToPolygons,
242  const UT_String& primvarPattern,
243  const UT_String& attributePattern,
244  bool translateSTtoUV,
245  const UT_StringRef& nonTransformingPrimvarPattern,
247 
248  static bool AppendExpandedPackedPrimsFromLopNode(
249  GU_Detail& gd,
250  const GA_Detail& srcGd,
251  const GA_Range& srcRng,
252  const UT_Array<PrimIndexPair>& primIndexPairs,
253  const GusdDefaultArray<UsdTimeCode>& times,
254  const GA_AttributeFilter& filter,
255  bool unpackToPolygons,
256  const UT_String& primvarPattern,
257  bool importInheritedPrimvars,
258  const UT_String& attributePattern,
259  bool translateSTtoUV,
260  const UT_StringRef &nonTransformingPrimvarPattern,
262  const UT_StringHolder &filePathAttrib = GUSD_PATH_ATTR,
263  const UT_StringHolder &primPathAttrib = GUSD_PRIMPATH_ATTR,
264  const GT_RefineParms *refineParms = nullptr);
265 
266  /** Apply all variant selections in @a selections to each prim
267  in the range, storing the resulting variant path in @a variantsAttr.
268  For each source prim, this will first validate that the
269  variant selection is valid on the target prims.
270  If @a prevVariants is supplied, the variant selections are added
271  on top of any variant selections in the given paths.*/
272  static bool WriteVariantSelectionsToAttr(
273  GU_Detail& gd,
274  const GA_Range& rng,
275  const UT_Array<UsdPrim>& prims,
276  const GusdUSD_Utils::VariantSelArray& selections,
277  const char* variantsAttr=GUSD_VARIANTS_ATTR,
278  const UT_Array<SdfPath>* prevVariants=nullptr);
279 
280  static bool WriteVariantSelectionsToPackedPrims(
281  GU_Detail& gd,
282  const GA_Range& rng,
283  const UT_Array<UsdPrim>& prims,
284  const GusdUSD_Utils::VariantSelArray& selections,
285  const UT_Array<SdfPath>* prevVariants=nullptr);
286 
287  /** Append variant selections defined by @a orderedVariants and
288  @a variantIndices as an expansion of prims from @a srcRng.
289  Attributes and groups matching @a attrs filter are copied from the
290  source to the newly created ref points.*/
291  static GA_Offset AppendRefPointsForExpandedVariants(
292  GU_Detail& gd,
293  const GA_Detail& srcGd,
294  const GA_Range& srcRng,
295  const UT_Array<UT_StringHolder>& orderedVariants,
296  const GusdUSD_Utils::IndexPairArray& variantIndices,
297  const GA_AttributeFilter& filter,
298  const char* variantsAttr=GUSD_VARIANTS_ATTR);
299 
300  static GA_Offset AppendPackedPrimsForExpandedVariants(
301  GU_Detail& gd,
302  const GA_Detail& srcGd,
303  const GA_Range& srcRng,
304  const UT_Array<UT_StringHolder>& orderedVariants,
305  const GusdUSD_Utils::IndexPairArray& variantIndices,
306  const GA_AttributeFilter& filter);
307 
308  static bool GetPackedPrimStageIdsViewportLODsAndPurposes(
309  const GA_Detail& gd,
310  const GA_OffsetArray& offsets,
311  UT_StringArray& stageIds,
312  UT_StringArray& viewportLODs,
313  UT_Array<GusdPurposeSet>& purposes);
314 
315  /** Compute world transforms from attributes over an array of offsets.
316 
317  In addition to using the standard instancing attributes,
318  this supports an additional schema for non-orthonormal transforms,
319  where the basis vectors (rows) of a rotation matrix are stored
320  as normal attributes 'i', 'j', 'k'.*/
321  static bool ComputeTransformsFromAttrs(const GA_Detail& gd,
322  GA_AttributeOwner owner,
323  const GA_OffsetArray& offsets,
324  UT_Matrix4D* xforms);
325 
326  static bool ComputeTransformsFromPackedPrims(const GA_Detail& gd,
327  const GA_OffsetArray& offsets,
328  UT_Matrix4D* xforms);
329 
330  /** Support representations of attributes when authoring new transforms.*/
332  {
333  ORIENTATTR_ORIENT, //! quaternion orient.
334  ORIENTATTR_IJK, //! vec3 i,j,k (can be non-orthogonal).
335  ORIENTATTR_IGNORE
336  };
337 
339  {
340  SCALEATTR_SCALE, //! scale (vec3).
341  SCALEATTR_PSCALE, //! single pscale.
342  SCALEATTR_IGNORE
343  };
344 
345  /** Create and set transform attributes over the given range.
346  The @a indexMap maps offset->range_index, as computed by
347  ComputeRangeIndexMap().*/
348  static bool SetTransformAttrs(GU_Detail& gd,
349  const GA_Range& r,
350  const GA_OffsetArray& indexMap,
351  OrientAttrRepresentation orientRep,
352  ScaleAttrRepresentation scaleRep,
353  const UT_Matrix4D* xforms);
354 
355  static bool SetPackedPrimTransforms(GU_Detail& gd,
356  const GA_Range& r,
357  const UT_Matrix4D* xforms);
358 
359  static bool MultTransformableAttrs(GU_Detail& gd,
360  const GA_Range& r,
361  const GA_OffsetArray& indexMap,
362  const UT_Matrix4D* xforms,
363  bool keepLengths=false,
364  const GA_AttributeFilter* filter=nullptr);
365 
366  /// Imports \p prim as unpacked geometry in \p gd.
367  static bool ImportPrimUnpacked(GU_Detail& gd,
368  const UsdPrim& prim,
370  const char* lod = nullptr,
371  GusdPurposeSet purpose = GusdPurposeSet(
373  const UT_StringRef &primvarPattern = "*",
374  const UT_StringRef &attributePattern = UT_StringHolder::theEmptyString,
375  bool translateSTtoUV = true,
376  const UT_StringRef &nonTransformingPrimvarPattern = GA_Names::rest,
377  const UT_Matrix4D* xform = nullptr,
378  const GT_RefineParms* refineParms = nullptr);
379 };
380 
382 
383 #endif /*_GUSD_GU_USD_H_*/
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
GT_API const UT_StringHolder time
UT_ErrorSeverity
Definition: UT_Error.h:25
quaternion orient.
Definition: GU_USD.h:334
#define GUSD_PRIMPATH_ATTR
Definition: GU_USD.h:53
A range of elements in an index-map.
Definition: GA_Range.h:42
GLuint GLsizei const GLuint const GLintptr * offsets
Definition: glcorearb.h:2621
#define GUSD_PATH_ATTR
Definition: GU_USD.h:52
GA_Size GA_Offset
Definition: GA_Types.h:646
GusdUSD_Traverse::PrimIndexPair PrimIndexPair
Definition: GU_USD.h:216
Definition: token.h:87
static const UT_StringHolder theEmptyString
Methods for USD scene traversal.
std::pair< UsdPrim, exint > PrimIndexPair
Definition: USD_Traverse.h:60
OrientAttrRepresentation
Definition: GU_USD.h:331
Definition: prim.h:133
Definition: path.h:290
GA_AttributeOwner
Definition: GA_Types.h:35
GLenum func
Definition: glcorearb.h:783
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Container class for all geometry.
Definition: GA_Detail.h:96
#define GUSD_API
Definition: api.h:40
#define GUSD_VARIANTS_ATTR
Definition: GU_USD.h:55
ScaleAttrRepresentation
Definition: GU_USD.h:338
GLboolean r
Definition: glcorearb.h:1222
GusdPurposeSet
Definition: purpose.h:39
GA_API const UT_StringHolder rest
GLint lod
Definition: glcorearb.h:2765
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297