All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primWrapper.h
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_PRIM_WRAPPER_H
25 #define GUSD_PRIM_WRAPPER_H
26 
27 #include <GT/GT_Primitive.h>
28 #include <UT/UT_ConcurrentHashMap.h>
29 
30 #include "api.h"
31 
32 #include "pxr/pxr.h"
33 #include "pxr/usd/usd/prim.h"
34 #include "pxr/usd/usd/timeCode.h"
35 #include "pxr/usd/usdGeom/imageable.h"
36 
37 #include <functional>
38 
39 #include "GT_Utils.h"
40 #include "purpose.h"
41 
42 class GU_PackedImpl;
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
46 class GusdGT_AttrFilter;
47 class UsdGeomXformCache;
48 class GusdContext;
49 
85 
86 
87 typedef std::map<SdfPath,UT_Matrix4D> GusdSimpleXformCache;
88 
89 class GUSD_API GusdPrimWrapper : public GT_Primitive
90 {
91 public:
92 
93  typedef std::function<GT_PrimitiveHandle
94  (const GT_PrimitiveHandle&, /* sourcePrim */
95  const UsdStagePtr&,
96  const SdfPath& /* path */,
97  const GusdContext&)>
98  DefinitionForWriteFunction;
99 
100  typedef std::function<GT_PrimitiveHandle
101  (const UsdGeomImageable&,
102  UsdTimeCode,
103  GusdPurposeSet)>
104  DefinitionForReadFunction;
105 
106  typedef std::function<bool
107  (const GT_PrimitiveHandle&,
108  std::string &primName)>
109  GetPrimNameFunction;
110 
111  typedef std::function<GT_DataArrayHandle
112  ( const GT_DataArrayHandle & )>
113  ResampleArrayFunction;
114 
119  static GT_PrimitiveHandle
120  defineForWrite( const GT_PrimitiveHandle& sourcePrim,
121  const UsdStagePtr& stage,
122  const SdfPath& path,
123  const GusdContext& ctxt);
124 
129  static bool
130  getPrimName( const GT_PrimitiveHandle &sourcePrim,
131  std::string &primName );
132 
133  // When we write USD for the given type, we will use a name like $USDNAME_0.
134  // where USDNAME is the name registered for this type
135  static const char*
136  getUsdName( int gtPrimId );
137 
138  // When we USD for an object that is marked as a group type, we write
139  // the object and then all its children.
140  static bool
141  isGroupType( int gtPrimId );
142 
148  static GT_PrimitiveHandle
149  defineForRead( const UsdGeomImageable& sourcePrim,
150  UsdTimeCode time,
151  GusdPurposeSet purposes );
152 
156  static bool
157  isPointInstancerPrim(const GT_PrimitiveHandle& prim,
158  const GusdContext& ctxt);
159 
162  static bool registerPrimDefinitionFuncForWrite(int gtPrimId,
163  DefinitionForWriteFunction function,
164  GetPrimNameFunction getNameFunction = NULL,
165  bool isGroupType = false,
166  const char* usdName = NULL );
167 
169  static bool registerPrimDefinitionFuncForRead(const TfToken& usdTypeName,
170  DefinitionForReadFunction function);
171 
174  static bool isGTPrimSupported(const GT_PrimitiveHandle& prim);
175 
176  GusdPrimWrapper();
177  GusdPrimWrapper( const UsdTimeCode &time, const GusdPurposeSet &purposes );
178  GusdPrimWrapper( const GusdPrimWrapper &in );
179  virtual ~GusdPrimWrapper();
180 
182  virtual bool isValid() const;
183 
184  virtual const UsdGeomImageable getUsdPrim() const = 0;
185 
186  virtual bool unpack(
187  GU_Detail& gdr,
188  const UT_StringRef& fileName,
189  const SdfPath& primPath,
190  const UT_Matrix4D& xform,
191  fpreal frame,
192  const char * viewportLod,
193  GusdPurposeSet purposes );
194 
201  virtual bool redefine(
202  const UsdStagePtr& stage,
203  const SdfPath& path,
204  const GusdContext& ctxt,
205  const GT_PrimitiveHandle& sourcePrim );
206 
212  virtual bool updateFromGTPrim(
213  const GT_PrimitiveHandle& sourcePrim,
214  const UT_Matrix4D& houXform,
215  const GusdContext& ctxt,
216  GusdSimpleXformCache& xformCache );
217 
221  virtual void addLeadingBookend( double curFrame );
222 
224  virtual void addTrailingBookend( double curFrame );
225 
227  void markVisible( bool in ) { m_visible = in; }
228  bool isVisible() const { return m_visible; }
229 
230  virtual void setVisibility(const TfToken& visibility, UsdTimeCode time);
231 
232  static GT_DataArrayHandle convertPrimvarData(
233  const UsdGeomPrimvar& primvar,
234  UsdTimeCode time );
235 
236  static GT_DataArrayHandle convertPrimvarData(
237  const UsdGeomPrimvar& primvar,
238  const VtValue& val);
239 
243  void loadPrimvars(
244  UsdTimeCode time,
245  const GT_RefineParms* rparms,
246  int minUniform,
247  int minPoint,
248  int minVertex,
249  const std::string& primPath,
250  GT_AttributeListHandle* vertex,
251  GT_AttributeListHandle* point,
252  GT_AttributeListHandle* primitive,
253  GT_AttributeListHandle* constant,
254  const GT_DataArrayHandle& remapIndicies = GT_DataArrayHandle() ) const;
255 
256  // Map to translate from GT_Owner enums to USD interpolation type tokens
257  static std::map<GT_Owner, TfToken> s_ownerToUsdInterp;
258  static std::map<GT_Owner, TfToken> s_ownerToUsdInterpCurve;
259 
260 protected:
261 
264  void updateVisibilityFromGTPrim( const GT_PrimitiveHandle& sourcePrim,
265  UsdTimeCode time,
266  bool forceWrite = true );
267 
270  void updateActiveFromGTPrim( const GT_PrimitiveHandle& sourcePrim,
271  UsdTimeCode time );
272 
273  void updateTransformFromGTPrim( const GfMatrix4d &xform, UsdTimeCode time, bool force );
274 
275  bool updateAttributeFromGTPrim( GT_Owner owner,
276  const std::string& name,
277  const GT_DataArrayHandle& houAttr,
278  UsdAttribute& usdAttr,
279  UsdTimeCode time );
280 
281  bool updatePrimvarFromGTPrim(
282  const TfToken& name,
283  const GT_Owner& owner,
284  const TfToken& interpolation,
285  UsdTimeCode time,
286  const GT_DataArrayHandle& data );
287 
289  bool updatePrimvarFromGTPrim( const GT_AttributeListHandle& gtAttrs,
290  const GusdGT_AttrFilter& primvarFilter,
291  const TfToken& interpolation,
292  UsdTimeCode time );
293 
294  void clearCaches();
295 
304  static GfMatrix4d computeTransform(
305  const UsdPrim& prim,
306  UsdTimeCode time,
307  const UT_Matrix4D& houXform,
308  const GusdSimpleXformCache& xformCache );
309 
310 protected:
311 
312  UsdTimeCode m_time;
313  GusdPurposeSet m_purposes;
314 
315  bool m_visible;
316 
318  // Support for collapsing transform values across frames
319 
320  GfMatrix4d m_xformCache;
321  UsdTimeCode m_lastXformSet;
322  UsdTimeCode m_lastXformCompared;
323 
325  // Support from collapsing attribute values across frames
326 
327  struct AttrLastValueEntry {
328 
329  AttrLastValueEntry( const UsdTimeCode &time, GT_DataArrayHandle data_ ) {
330  data = data_;
331  lastSet = time;
332  lastCompared = time;
333  }
334 
335  GT_DataArrayHandle data;
336  UsdTimeCode lastSet;
337  UsdTimeCode lastCompared;
338  };
339 
340  typedef std::pair<GT_Owner, std::string> AttrLastValueKeyType;
341  typedef UT_Map<AttrLastValueKeyType, AttrLastValueEntry> AttrLastValueDict;
342 
343  mutable AttrLastValueDict m_lastAttrValueDict;
344 
346 
347 private:
348 
349  struct GTTypeInfo {
350  DefinitionForWriteFunction writeFunc;
351  GetPrimNameFunction primNameFunc;
352  bool isGroupType;
353  const char * templateName;
354 
355  GTTypeInfo() :
356  writeFunc( NULL ),
357  primNameFunc( NULL ),
358  isGroupType( false ),
359  templateName(NULL) {}
360  GTTypeInfo(
361  DefinitionForWriteFunction writeFunc_,
362  GetPrimNameFunction primNameFunc_,
363  bool isGroupType_,
364  const char * templateName_ ) :
365  writeFunc( writeFunc_ ),
366  primNameFunc( primNameFunc_ ),
367  isGroupType( isGroupType_ ),
368  templateName( templateName_ ) {}
369  };
370 
371  struct TfTokenHashCmp {
372  static bool equal(const TfToken& a, const TfToken& b)
373  { return a == b; }
374 
375  static size_t hash(const TfToken& key)
376  { return key.Hash(); }
377  };
378 
379  typedef UT_Map<int64, GTTypeInfo> GTTypeInfoMap;
380  typedef UT_Set<int64> GTTypeSet;
381  typedef UT_ConcurrentHashMap<
382  TfToken, DefinitionForReadFunction, TfTokenHashCmp>
383  USDTypeToDefineFuncMap;
384 
385  static GTTypeInfoMap s_gtTypeInfoMap;
386  static USDTypeToDefineFuncMap s_usdTypeToFuncMap;
387  static GTTypeSet s_supportedNativeGTTypes;
388 };
389 
390 PXR_NAMESPACE_CLOSE_SCOPE
391 
392 #endif // GUSD_PRIM_WRAPPER_H
A GT_Primitive that wraps a USD Prim.
Definition: primWrapper.h:89
void markVisible(bool in)
Keep track of the visibility state of the prim for book marks.
Definition: primWrapper.h:227
size_t Hash() const
Return a size_t hash for this token.
Definition: token.h:165
Scenegraph object for authoring and retrieving numeric, string, and array valued data, sampled over time.
Definition: attribute.h:176
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:131
A GusdContext structure is created by the ROPs that write USD files and passed to the GusdPrimWrapper...
Definition: context.h:46
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
Schema wrapper for UsdAttribute for authoring and introspecting attributes that are primvars...
Definition: primvar.h:260
Base class for all prims that may require rendering or visualization of some sort.
Definition: imageable.h:74
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
A caching mechanism for transform matrices.
Definition: xformCache.h:58