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_RELATIONSHIPPATTERN "usd:relationshipPattern"_UTsh
88 #define GUSD_REFINE_TRANSLATESTTOUV "usd:translateSTtoUV"
89 #define GUSD_REFINE_ITERATIONS "usd:iterations"_UTsh
90 #define GUSD_REFINE_UNPACKTOPOLYGONS "usd:unpacktopolygons"_UTsh
91 #define GUSD_REFINE_PIVOTLOCATION "usd:pivotlocation"_UTsh
92 /** @} */
93 
94 /** Set of helpers for working with ranges of prims/points, etc.*/
96 {
97 public:
98  /** Compute an array of offsets from a range.*/
99  static bool OffsetArrayFromRange(const GA_Range& r,
101 
102  /** Compute an array mapping offset->range_index for the given range.*/
103  static bool ComputeRangeIndexMap(const GA_Range& r,
104  GA_OffsetArray& indexMap);
105 
106  static bool
107  BindPrims(GusdStageCacheReader& cache,
108  UT_Array<UsdPrim>& prims,
109  const GA_Detail& gd,
110  const GA_Range& rng,
111  UT_Array<SdfPath>* variants=nullptr,
112  GusdDefaultArray<GusdPurposeSet>* purposes=nullptr,
113  GusdDefaultArray<UsdTimeCode>* times=nullptr,
115 
116  /** Bind prims from references defined in the given attributes.
117  This creates an entry in @a prims for each entry in the given range,
118  mapped to the corresponding prim.
119  If @a variants is non-null, resolved variant paths are stored
120  in the given array.*/
121  static bool
122  BindPrimsFromAttrs(GusdStageCacheReader& cache,
123  UT_Array<UsdPrim>& prims,
124  const GA_Range& rng,
125  const GA_Attribute& pathAttr,
126  const GA_Attribute& primPathAttr,
127  const GA_Attribute* variantsAttr,
128  UT_Array<SdfPath>* variants=nullptr,
130 
131  static bool
132  BindPrimsFromPackedPrims(UT_Array<UsdPrim>& prims,
133  const GA_Range& rng,
134  UT_Array<SdfPath>* variants=nullptr,
135  UT_Array<GusdPurposeSet>* purposes=nullptr,
137 
138  static bool GetTimeCodesFromAttr(const GA_Range& rng,
139  const GA_Attribute& attr,
140  UT_Array<UsdTimeCode>& times);
141 
142  static bool GetTimeCodesFromPackedPrims(const GA_Range& rng,
143  UT_Array<UsdTimeCode>& times);
144 
145  /** Given a string attribute that represents prim paths,
146  return an array of actual prim paths.
147  @{ */
148  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
149  UT_Array<SdfPath>& paths,
151 
152  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
153  const GA_Range& rng,
154  UT_Array<SdfPath>& paths,
156  /** @} */
157 
158 
159  /** Givena string attribute, return an array of tokens.
160  @{ */
161  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
162  UT_Array<TfToken>& tokens,
163  const char* nameSpace=nullptr);
164 
165  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
166  const GA_Range& rng,
167  UT_Array<TfToken>& tokens,
168  const char* nameSpace=nullptr);
169  /** @} */
170 
171 
172  /** Append points to a detail that represent references to prims.
173  The point offsets are contiguous, and the offset of the first
174  point is returned. If any failures occur, and invalid
175  offset is returned.*/
176  static GA_Offset AppendRefPoints(
177  GU_Detail& gd,
178  const UT_Array<UsdPrim>& prims,
179  const char* pathAttrName=GUSD_PATH_ATTR,
180  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
181 
182  typedef GU_PrimPacked* (*PackedPrimBuildFunc)(
183  GU_Detail& detail,
184  const UT_StringHolder& fileName,
185  const SdfPath& primPath,
186  const UsdTimeCode& frame,
187  const char* lod,
188  const GusdPurposeSet purposes );
189 
190  /** Register a function to be used by AppendPackedPrims to build
191  a packed prim of the given type. */
192  static void RegisterPackedPrimBuildFunc( const TfToken& typeName,
193  PackedPrimBuildFunc func );
194 
195  /** Append packed prims to the given detail that reference the
196  given prims with the given variants. */
197  static bool AppendPackedPrims(
198  GU_Detail& gd,
199  const UT_Array<UsdPrim>& prims,
200  const UT_Array<SdfPath>& variants,
201  const GusdDefaultArray<UsdTimeCode>& times,
203  const GusdDefaultArray<GusdPurposeSet>& purposes,
205  /** More specialized function for appending prims to the given detail
206  from a LOP node where the variants are already chosen, and the LOP
207  node path needs to be set as the file path on the packed prim. */
208  static bool AppendPackedPrimsFromLopNode(
209  GU_Detail& gd,
210  const UT_Array<UsdPrim>& prims,
211  const GusdDefaultArray<UT_StringHolder> &stageids,
212  const GusdDefaultArray<UsdTimeCode>& times,
214  const GusdDefaultArray<GusdPurposeSet>& purposes,
216 
218 
219  /** Append prims @a prims, as an expansion of prims defined on
220  @a srcRange. The prim index pairs provide the prim found in
221  the expansion, and the index of the prim in the source range
222  whose expansion produced that prim.
223  Attributes and groups matching @a filter are copied from the source
224  to the newly created ref points.*/
225  static GA_Offset AppendExpandedRefPoints(
226  GU_Detail& gd,
227  const GA_Detail& srcGd,
228  const GA_Range& srcRng,
229  const UT_Array<PrimIndexPair>& prims,
230  const GA_AttributeFilter& filter,
231  const char* pathAttrName=GUSD_PATH_ATTR,
232  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
233 
234  static bool AppendExpandedPackedPrims(
235  GU_Detail& gd,
236  const GA_Detail& srcGd,
237  const GA_Range& srcRng,
238  const UT_Array<PrimIndexPair>& primIndexPairs,
239  const UT_Array<SdfPath>& variants,
240  const GusdDefaultArray<UsdTimeCode>& times,
241  const GA_AttributeFilter& filter,
242  bool unpackToPolygons,
243  const UT_String& primvarPattern,
244  const UT_String& attributePattern,
245  bool translateSTtoUV,
246  const UT_StringRef& nonTransformingPrimvarPattern,
248 
249  static bool AppendExpandedPackedPrimsFromLopNode(
250  GU_Detail& gd,
251  const GA_Detail& srcGd,
252  const GA_Range& srcRng,
253  const UT_Array<PrimIndexPair>& primIndexPairs,
254  const GusdDefaultArray<UsdTimeCode>& times,
255  const GA_AttributeFilter& filter,
256  bool unpackToPolygons,
257  const UT_String& primvarPattern,
258  bool importInheritedPrimvars,
259  const UT_String& attributePattern,
260  bool translateSTtoUV,
261  const UT_StringRef &nonTransformingPrimvarPattern,
263  const UT_StringHolder &filePathAttrib = GUSD_PATH_ATTR,
264  const UT_StringHolder &primPathAttrib = GUSD_PRIMPATH_ATTR,
265  const GT_RefineParms *refineParms = nullptr);
266 
267  /** Apply all variant selections in @a selections to each prim
268  in the range, storing the resulting variant path in @a variantsAttr.
269  For each source prim, this will first validate that the
270  variant selection is valid on the target prims.
271  If @a prevVariants is supplied, the variant selections are added
272  on top of any variant selections in the given paths.*/
273  static bool WriteVariantSelectionsToAttr(
274  GU_Detail& gd,
275  const GA_Range& rng,
276  const UT_Array<UsdPrim>& prims,
277  const GusdUSD_Utils::VariantSelArray& selections,
278  const char* variantsAttr=GUSD_VARIANTS_ATTR,
279  const UT_Array<SdfPath>* prevVariants=nullptr);
280 
281  static bool WriteVariantSelectionsToPackedPrims(
282  GU_Detail& gd,
283  const GA_Range& rng,
284  const UT_Array<UsdPrim>& prims,
285  const GusdUSD_Utils::VariantSelArray& selections,
286  const UT_Array<SdfPath>* prevVariants=nullptr);
287 
288  /** Append variant selections defined by @a orderedVariants and
289  @a variantIndices as an expansion of prims from @a srcRng.
290  Attributes and groups matching @a attrs filter are copied from the
291  source to the newly created ref points.*/
292  static GA_Offset AppendRefPointsForExpandedVariants(
293  GU_Detail& gd,
294  const GA_Detail& srcGd,
295  const GA_Range& srcRng,
296  const UT_Array<UT_StringHolder>& orderedVariants,
297  const GusdUSD_Utils::IndexPairArray& variantIndices,
298  const GA_AttributeFilter& filter,
299  const char* variantsAttr=GUSD_VARIANTS_ATTR);
300 
301  static GA_Offset AppendPackedPrimsForExpandedVariants(
302  GU_Detail& gd,
303  const GA_Detail& srcGd,
304  const GA_Range& srcRng,
305  const UT_Array<UT_StringHolder>& orderedVariants,
306  const GusdUSD_Utils::IndexPairArray& variantIndices,
307  const GA_AttributeFilter& filter);
308 
309  static bool GetPackedPrimStageIdsViewportLODsAndPurposes(
310  const GA_Detail& gd,
311  const GA_OffsetArray& offsets,
312  UT_StringArray& stageIds,
313  UT_StringArray& viewportLODs,
314  UT_Array<GusdPurposeSet>& purposes);
315 
316  /** Compute world transforms from attributes over an array of offsets.
317 
318  In addition to using the standard instancing attributes,
319  this supports an additional schema for non-orthonormal transforms,
320  where the basis vectors (rows) of a rotation matrix are stored
321  as normal attributes 'i', 'j', 'k'.*/
322  static bool ComputeTransformsFromAttrs(const GA_Detail& gd,
323  GA_AttributeOwner owner,
324  const GA_OffsetArray& offsets,
325  UT_Matrix4D* xforms);
326 
327  static bool ComputeTransformsFromPackedPrims(const GA_Detail& gd,
328  const GA_OffsetArray& offsets,
329  UT_Matrix4D* xforms);
330 
331  /** Support representations of attributes when authoring new transforms.*/
333  {
334  ORIENTATTR_ORIENT, //! quaternion orient.
335  ORIENTATTR_IJK, //! vec3 i,j,k (can be non-orthogonal).
336  ORIENTATTR_IGNORE
337  };
338 
340  {
341  SCALEATTR_SCALE, //! scale (vec3).
342  SCALEATTR_PSCALE, //! single pscale.
343  SCALEATTR_IGNORE
344  };
345 
346  /** Create and set transform attributes over the given range.
347  The @a indexMap maps offset->range_index, as computed by
348  ComputeRangeIndexMap().*/
349  static bool SetTransformAttrs(GU_Detail& gd,
350  const GA_Range& r,
351  const GA_OffsetArray& indexMap,
352  OrientAttrRepresentation orientRep,
353  ScaleAttrRepresentation scaleRep,
354  const UT_Matrix4D* xforms);
355 
356  static bool SetPackedPrimTransforms(GU_Detail& gd,
357  const GA_Range& r,
358  const UT_Matrix4D* xforms);
359 
360  static bool MultTransformableAttrs(GU_Detail& gd,
361  const GA_Range& r,
362  const GA_OffsetArray& indexMap,
363  const UT_Matrix4D* xforms,
364  bool keepLengths=false,
365  const GA_AttributeFilter* filter=nullptr);
366 
367  /// Imports \p prim as unpacked geometry in \p gd.
368  static bool ImportPrimUnpacked(GU_Detail& gd,
369  const UsdPrim& prim,
371  const char* lod = nullptr,
372  GusdPurposeSet purpose = GusdPurposeSet(
374  const UT_StringRef &primvarPattern = "*",
375  const UT_StringRef &attributePattern = UT_StringHolder::theEmptyString,
376  bool translateSTtoUV = true,
377  const UT_StringRef &nonTransformingPrimvarPattern = GA_Names::rest,
378  const UT_Matrix4D* xform = nullptr,
379  const GT_RefineParms* refineParms = nullptr);
380 };
381 
383 
384 #endif /*_GUSD_GU_USD_H_*/
Definition of a geometry attribute.
Definition: GA_Attribute.h:203
GT_API const UT_StringHolder time
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
UT_ErrorSeverity
Definition: UT_Error.h:25
quaternion orient.
Definition: GU_USD.h:335
#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:653
GusdUSD_Traverse::PrimIndexPair PrimIndexPair
Definition: GU_USD.h:217
Definition: token.h:70
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:332
Definition: prim.h:116
Definition: path.h:280
GA_AttributeOwner
Definition: GA_Types.h:35
GLenum func
Definition: glcorearb.h:783
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GU_API void xform(CE_Context &context, bool recompile, int npts, const cl::Buffer &outPos, const cl::Buffer &inPos, const cl::Buffer &surfacexform, const cl::Buffer *grp=nullptr)
Container class for all geometry.
Definition: GA_Detail.h:105
#define GUSD_API
Definition: api.h:42
#define GUSD_VARIANTS_ATTR
Definition: GU_USD.h:55
ScaleAttrRepresentation
Definition: GU_USD.h:339
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