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 
40 #include "pxr/pxr.h"
41 
42 class GA_AttributeFilter;
43 class GT_RefineParms;
44 class GU_Detail;
45 class GU_PrimPacked;
46 
48 
49 /** The default names of the USD ref attributes.
50  @{ */
51 #define GUSD_PATH_ATTR "usdpath"
52 #define GUSD_PRIMPATH_ATTR "usdprimpath"
53 #define GUSD_FRAME_ATTR "frame"
54 #define GUSD_VARIANTS_ATTR "usdvariants"
55 #define GUSD_CONSTRAINT_ATTR "usdconstraint"
56 #define GUSD_PURPOSE_ATTR "usdpurpose"
57 #define GUSD_ACTIVE_ATTR "usdactive"
58 #define GUSD_VISIBLE_ATTR "usdvisible"
59 
60 #define GUSD_OVERTRANSFORMS_ATTR "usdovertransforms"
61 #define GUSD_OVERPOINTS_ATTR "usdoverpoints"
62 #define GUSD_OVERPRIMVARS_ATTR "usdoverprimvars"
63 #define GUSD_OVERALL_ATTR "usdoverall"
64 
65 #define GUSD_WRITESTATICTOPOLOGY_ATTR "usdwritestatictopology"
66 #define GUSD_WRITESTATICPRIMVARS_ATTR "usdwritestaticprimvars"
67 #define GUSD_WRITESTATICGEO_ATTR "usdwritestaticgeo"
68 /** @} */
69 
70 /** USD-related options for GT_RefineParms.
71  @{ */
72 #define GUSD_REFINE_ADDPATHATTRIB "usd:addPathAttribute"
73 #define GUSD_REFINE_PATHATTRIB "usd:pathAttribute"
74 #define GUSD_REFINE_ADDPRIMPATHATTRIB "usd:addPrimPathAttribute"
75 #define GUSD_REFINE_PRIMPATHATTRIB "usd:primPathAttribute"
76 #define GUSD_REFINE_ADDVISIBILITYATTRIB "usd:addVisibilityAttribute"
77 #define GUSD_REFINE_ADDXFORMATTRIB "usd:addXformAttribute"
78 #define GUSD_REFINE_NONTRANSFORMINGPATTERN "usd:nonTransformingPrimvarPattern"
79 #define GUSD_REFINE_PRIMVARPATTERN "usd:primvarPattern"
80 #define GUSD_REFINE_IMPORTINHERITEDPRIMVARS "usd:importInheritedPrimvars"
81 #define GUSD_REFINE_ATTRIBUTEPATTERN "usd:attributePattern"
82 #define GUSD_REFINE_TRANSLATESTTOUV "usd:translateSTtoUV"
83 /** @} */
84 
85 /** Set of helpers for working with ranges of prims/points, etc.*/
87 {
88 public:
89  /** Compute an array of offsets from a range.*/
90  static bool OffsetArrayFromRange(const GA_Range& r,
92 
93  /** Compute an array mapping offset->range_index for the given range.*/
94  static bool ComputeRangeIndexMap(const GA_Range& r,
95  GA_OffsetArray& indexMap);
96 
97  static bool
98  BindPrims(GusdStageCacheReader& cache,
99  UT_Array<UsdPrim>& prims,
100  const GA_Detail& gd,
101  const GA_Range& rng,
102  UT_Array<SdfPath>* variants=nullptr,
103  GusdDefaultArray<GusdPurposeSet>* purposes=nullptr,
104  GusdDefaultArray<UsdTimeCode>* times=nullptr,
106 
107  /** Bind prims from references defined in the given attributes.
108  This creates an entry in @a prims for each entry in the given range,
109  mapped to the corresponding prim.
110  If @a variants is non-null, resolved variant paths are stored
111  in the given array.*/
112  static bool
113  BindPrimsFromAttrs(GusdStageCacheReader& cache,
114  UT_Array<UsdPrim>& prims,
115  const GA_Range& rng,
116  const GA_Attribute& pathAttr,
117  const GA_Attribute& primPathAttr,
118  const GA_Attribute* variantsAttr,
119  UT_Array<SdfPath>* variants=nullptr,
121 
122  static bool
123  BindPrimsFromPackedPrims(UT_Array<UsdPrim>& prims,
124  const GA_Range& rng,
125  UT_Array<SdfPath>* variants=nullptr,
126  UT_Array<GusdPurposeSet>* purposes=nullptr,
128 
129  static bool GetTimeCodesFromAttr(const GA_Range& rng,
130  const GA_Attribute& attr,
131  UT_Array<UsdTimeCode>& times);
132 
133  static bool GetTimeCodesFromPackedPrims(const GA_Range& rng,
134  UT_Array<UsdTimeCode>& times);
135 
136  /** Given a string attribute that represents prim paths,
137  return an array of actual prim paths.
138  @{ */
139  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
142 
143  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
144  const GA_Range& rng,
145  UT_Array<SdfPath>& paths,
147  /** @} */
148 
149 
150  /** Givena string attribute, return an array of tokens.
151  @{ */
152  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
153  UT_Array<TfToken>& tokens,
154  const char* nameSpace=nullptr);
155 
156  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
157  const GA_Range& rng,
158  UT_Array<TfToken>& tokens,
159  const char* nameSpace=nullptr);
160  /** @} */
161 
162 
163  /** Append points to a detail that represent references to prims.
164  The point offsets are contiguous, and the offset of the first
165  point is returned. If any failures occur, and invalid
166  offset is returned.*/
167  static GA_Offset AppendRefPoints(
168  GU_Detail& gd,
169  const UT_Array<UsdPrim>& prims,
170  const char* pathAttrName=GUSD_PATH_ATTR,
171  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
172 
173  typedef GU_PrimPacked* (*PackedPrimBuildFunc)(
174  GU_Detail& detail,
175  const UT_StringHolder& fileName,
176  const SdfPath& primPath,
177  const UsdTimeCode& frame,
178  const char* lod,
179  const GusdPurposeSet purposes );
180 
181  /** Register a function to be used by AppendPackedPrims to build
182  a packed prim of the given type. */
183  static void RegisterPackedPrimBuildFunc( const TfToken& typeName,
184  PackedPrimBuildFunc func );
185 
186  /** Append packed prims to the given detail that reference the
187  given prims with the given variants. */
188  static bool AppendPackedPrims(
189  GU_Detail& gd,
190  const UT_Array<UsdPrim>& prims,
191  const UT_Array<SdfPath>& variants,
192  const GusdDefaultArray<UsdTimeCode>& times,
194  const GusdDefaultArray<GusdPurposeSet>& purposes,
196  /** More specialized function for appending prims to the given detail
197  from a LOP node where the variants are already chosen, and the LOP
198  node path needs to be set as the file path on the packed prim. */
199  static bool AppendPackedPrimsFromLopNode(
200  GU_Detail& gd,
201  const UT_Array<UsdPrim>& prims,
202  const GusdDefaultArray<UT_StringHolder> &stageids,
203  const GusdDefaultArray<UsdTimeCode>& times,
205  const GusdDefaultArray<GusdPurposeSet>& purposes,
207 
209 
210  /** Append prims @a prims, as an expansion of prims defined on
211  @a srcRange. The prim index pairs provide the prim found in
212  the expansion, and the index of the prim in the source range
213  whose expansion produced that prim.
214  Attributes and groups matching @a filter are copied from the source
215  to the newly created ref points.*/
216  static GA_Offset AppendExpandedRefPoints(
217  GU_Detail& gd,
218  const GA_Detail& srcGd,
219  const GA_Range& srcRng,
220  const UT_Array<PrimIndexPair>& prims,
221  const GA_AttributeFilter& filter,
222  const char* pathAttrName=GUSD_PATH_ATTR,
223  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
224 
225  static bool AppendExpandedPackedPrims(
226  GU_Detail& gd,
227  const GA_Detail& srcGd,
228  const GA_Range& srcRng,
229  const UT_Array<PrimIndexPair>& primIndexPairs,
230  const UT_Array<SdfPath>& variants,
231  const GusdDefaultArray<UsdTimeCode>& times,
232  const GA_AttributeFilter& filter,
233  bool unpackToPolygons,
234  const UT_String& primvarPattern,
235  const UT_String& attributePattern,
236  bool translateSTtoUV,
237  const UT_StringRef& nonTransformingPrimvarPattern,
239 
240  static bool AppendExpandedPackedPrimsFromLopNode(
241  GU_Detail& gd,
242  const GA_Detail& srcGd,
243  const GA_Range& srcRng,
244  const UT_Array<PrimIndexPair>& primIndexPairs,
245  const GusdDefaultArray<UsdTimeCode>& times,
246  const GA_AttributeFilter& filter,
247  bool unpackToPolygons,
248  const UT_String& primvarPattern,
249  bool importInheritedPrimvars,
250  const UT_String& attributePattern,
251  bool translateSTtoUV,
252  const UT_StringRef &nonTransformingPrimvarPattern,
254  const UT_StringHolder &filePathAttrib = GUSD_PATH_ATTR,
255  const UT_StringHolder &primPathAttrib = GUSD_PRIMPATH_ATTR);
256 
257  /** Apply all variant selections in @a selections to each prim
258  in the range, storing the resulting variant path in @a variantsAttr.
259  For each source prim, this will first validate that the
260  variant selection is valid on the target prims.
261  If @a prevVariants is supplied, the variant selections are added
262  on top of any variant selections in the given paths.*/
263  static bool WriteVariantSelectionsToAttr(
264  GU_Detail& gd,
265  const GA_Range& rng,
266  const UT_Array<UsdPrim>& prims,
267  const GusdUSD_Utils::VariantSelArray& selections,
268  const char* variantsAttr=GUSD_VARIANTS_ATTR,
269  const UT_Array<SdfPath>* prevVariants=nullptr);
270 
271  static bool WriteVariantSelectionsToPackedPrims(
272  GU_Detail& gd,
273  const GA_Range& rng,
274  const UT_Array<UsdPrim>& prims,
275  const GusdUSD_Utils::VariantSelArray& selections,
276  const UT_Array<SdfPath>* prevVariants=nullptr);
277 
278  /** Append variant selections defined by @a orderedVariants and
279  @a variantIndices as an expansion of prims from @a srcRng.
280  Attributes and groups matching @a attrs filter are copied from the
281  source to the newly created ref points.*/
282  static GA_Offset AppendRefPointsForExpandedVariants(
283  GU_Detail& gd,
284  const GA_Detail& srcGd,
285  const GA_Range& srcRng,
286  const UT_Array<UT_StringHolder>& orderedVariants,
287  const GusdUSD_Utils::IndexPairArray& variantIndices,
288  const GA_AttributeFilter& filter,
289  const char* variantsAttr=GUSD_VARIANTS_ATTR);
290 
291  static GA_Offset AppendPackedPrimsForExpandedVariants(
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 
299  static bool GetPackedPrimStageIdsViewportLODsAndPurposes(
300  const GA_Detail& gd,
301  const GA_OffsetArray& offsets,
302  UT_StringArray& stageIds,
303  UT_StringArray& viewportLODs,
304  UT_Array<GusdPurposeSet>& purposes);
305 
306  /** Compute world transforms from attributes over an array of offsets.
307 
308  In addition to using the standard instancing attributes,
309  this supports an additional schema for non-orthonormal transforms,
310  where the basis vectors (rows) of a rotation matrix are stored
311  as normal attributes 'i', 'j', 'k'.*/
312  static bool ComputeTransformsFromAttrs(const GA_Detail& gd,
313  GA_AttributeOwner owner,
314  const GA_OffsetArray& offsets,
315  UT_Matrix4D* xforms);
316 
317  static bool ComputeTransformsFromPackedPrims(const GA_Detail& gd,
318  const GA_OffsetArray& offsets,
319  UT_Matrix4D* xforms);
320 
321  /** Support representations of attributes when authoring new transforms.*/
323  {
324  ORIENTATTR_ORIENT, //! quaternion orient.
325  ORIENTATTR_IJK, //! vec3 i,j,k (can be non-orthogonal).
326  ORIENTATTR_IGNORE
327  };
328 
330  {
331  SCALEATTR_SCALE, //! scale (vec3).
332  SCALEATTR_PSCALE, //! single pscale.
333  SCALEATTR_IGNORE
334  };
335 
336  /** Create and set transform attributes over the given range.
337  The @a indexMap maps offset->range_index, as computed by
338  ComputeRangeIndexMap().*/
339  static bool SetTransformAttrs(GU_Detail& gd,
340  const GA_Range& r,
341  const GA_OffsetArray& indexMap,
342  OrientAttrRepresentation orientRep,
343  ScaleAttrRepresentation scaleRep,
344  const UT_Matrix4D* xforms);
345 
346  static bool SetPackedPrimTransforms(GU_Detail& gd,
347  const GA_Range& r,
348  const UT_Matrix4D* xforms);
349 
350  static bool MultTransformableAttrs(GU_Detail& gd,
351  const GA_Range& r,
352  const GA_OffsetArray& indexMap,
353  const UT_Matrix4D* xforms,
354  bool keepLengths=false,
355  const GA_AttributeFilter* filter=nullptr);
356 
357  /// Imports \p prim as unpacked geometry in \p gd.
358  static bool ImportPrimUnpacked(GU_Detail& gd,
359  const UsdPrim& prim,
361  const char* lod = nullptr,
362  GusdPurposeSet purpose = GusdPurposeSet(
364  const UT_StringRef &primvarPattern = "*",
365  const UT_StringRef &attributePattern = UT_StringHolder::theEmptyString,
366  bool translateSTtoUV = true,
367  const UT_StringRef &nonTransformingPrimvarPattern = GA_Names::rest,
368  const UT_Matrix4D* xform = nullptr,
369  const GT_RefineParms* refineParms = nullptr);
370 };
371 
373 
374 #endif /*_GUSD_GU_USD_H_*/
Definition of a geometry attribute.
Definition: GA_Attribute.h:196
GT_API const UT_StringHolder time
GLuint GLsizei const GLuint const GLintptr * offsets
Definition: glcorearb.h:2620
UT_ErrorSeverity
Definition: UT_Error.h:25
quaternion orient.
Definition: GU_USD.h:325
#define GUSD_PRIMPATH_ATTR
Definition: GU_USD.h:52
A range of elements in an index-map.
Definition: GA_Range.h:42
#define GUSD_PATH_ATTR
Definition: GU_USD.h:51
GA_Size GA_Offset
Definition: GA_Types.h:640
GusdUSD_Traverse::PrimIndexPair PrimIndexPair
Definition: GU_USD.h:208
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:322
Definition: prim.h:132
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1296
Definition: path.h:288
GA_AttributeOwner
Definition: GA_Types.h:33
GLenum func
Definition: glcorearb.h:782
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Container class for all geometry.
Definition: GA_Detail.h:95
#define GUSD_API
Definition: api.h:40
#define GUSD_VARIANTS_ATTR
Definition: GU_USD.h:54
ScaleAttrRepresentation
Definition: GU_USD.h:329
GLboolean r
Definition: glcorearb.h:1221
GusdPurposeSet
Definition: purpose.h:39
GA_API const UT_StringHolder rest
GLint lod
Definition: glcorearb.h:2764
GLenum const void * paths
Definition: glew.h:13872