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_PRIMWRAPPER_H__
25 #define __GUSD_PRIMWRAPPER_H__
26 
27 #include <GT/GT_Primitive.h>
28 #include <UT/UT_ConcurrentHashMap.h>
29 
30 #include "gusd/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 
222  virtual void addLeadingBookend( double curFrame, double startFrame );
223 
225  virtual void addTrailingBookend( double curFrame );
226 
228  void markVisible( bool in ) { m_visible = in; }
229  bool isVisible() const { return m_visible; }
230 
231  virtual void setVisibility(const TfToken& visibility, UsdTimeCode time);
232 
233  static GT_DataArrayHandle convertPrimvarData(
234  const UsdGeomPrimvar& primvar,
235  UsdTimeCode time );
236 
237  static GT_DataArrayHandle convertPrimvarData(
238  const UsdGeomPrimvar& primvar,
239  const VtValue& val);
240 
244  void loadPrimvars(
245  UsdTimeCode time,
246  const GT_RefineParms* rparms,
247  int minUniform,
248  int minPoint,
249  int minVertex,
250  const std::string& primPath,
251  GT_AttributeListHandle* vertex,
252  GT_AttributeListHandle* point,
253  GT_AttributeListHandle* primitive,
254  GT_AttributeListHandle* constant,
255  const GT_DataArrayHandle& remapIndicies = GT_DataArrayHandle() ) const;
256 
257  // Map to translate from GT_Owner enums to USD interpolation type tokens
258  static std::map<GT_Owner, TfToken> s_ownerToUsdInterp;
259  static std::map<GT_Owner, TfToken> s_ownerToUsdInterpCurve;
260 
261 protected:
262 
265  void updateVisibilityFromGTPrim( const GT_PrimitiveHandle& sourcePrim,
266  UsdTimeCode time,
267  bool forceWrite = true );
268 
271  void updateActiveFromGTPrim( const GT_PrimitiveHandle& sourcePrim,
272  UsdTimeCode time );
273 
274  void updateTransformFromGTPrim( const GfMatrix4d &xform, UsdTimeCode time, bool force );
275 
276  bool updateAttributeFromGTPrim( GT_Owner owner,
277  const std::string& name,
278  const GT_DataArrayHandle& houAttr,
279  UsdAttribute& usdAttr,
280  UsdTimeCode time );
281 
282  bool updatePrimvarFromGTPrim(
283  const TfToken& name,
284  const GT_Owner& owner,
285  const TfToken& interpolation,
286  UsdTimeCode time,
287  const GT_DataArrayHandle& data );
288 
290  bool updatePrimvarFromGTPrim( const GT_AttributeListHandle& gtAttrs,
291  const GusdGT_AttrFilter& primvarFilter,
292  const TfToken& interpolation,
293  UsdTimeCode time );
294 
295  void clearCaches();
296 
305  static GfMatrix4d computeTransform(
306  const UsdPrim& prim,
307  UsdTimeCode time,
308  const UT_Matrix4D& houXform,
309  const GusdSimpleXformCache& xformCache );
310 
311 protected:
312 
313  UsdTimeCode m_time;
314  GusdPurposeSet m_purposes;
315 
316  bool m_visible;
317 
319  // Support for collapsing transform values across frames
320 
321  GfMatrix4d m_xformCache;
322  UsdTimeCode m_lastXformSet;
323  UsdTimeCode m_lastXformCompared;
324 
326  // Support from collapsing attribute values across frames
327 
328  struct AttrLastValueEntry {
329 
330  AttrLastValueEntry( const UsdTimeCode &time, GT_DataArrayHandle data_ ) {
331  data = data_;
332  lastSet = time;
333  lastCompared = time;
334  }
335 
336  GT_DataArrayHandle data;
337  UsdTimeCode lastSet;
338  UsdTimeCode lastCompared;
339  };
340 
341  typedef std::pair<GT_Owner, std::string> AttrLastValueKeyType;
342  typedef UT_Map<AttrLastValueKeyType, AttrLastValueEntry> AttrLastValueDict;
343 
344  mutable AttrLastValueDict m_lastAttrValueDict;
345 
347 
348 private:
349 
350  struct GTTypeInfo {
351  DefinitionForWriteFunction writeFunc;
352  GetPrimNameFunction primNameFunc;
353  bool isGroupType;
354  const char * templateName;
355 
356  GTTypeInfo() :
357  writeFunc( NULL ),
358  primNameFunc( NULL ),
359  isGroupType( false ),
360  templateName(NULL) {}
361  GTTypeInfo(
362  DefinitionForWriteFunction writeFunc_,
363  GetPrimNameFunction primNameFunc_,
364  bool isGroupType_,
365  const char * templateName_ ) :
366  writeFunc( writeFunc_ ),
367  primNameFunc( primNameFunc_ ),
368  isGroupType( isGroupType_ ),
369  templateName( templateName_ ) {}
370  };
371 
372  struct TfTokenHashCmp {
373  static bool equal(const TfToken& a, const TfToken& b)
374  { return a == b; }
375 
376  static size_t hash(const TfToken& key)
377  { return key.Hash(); }
378  };
379 
380  typedef UT_Map<int64, GTTypeInfo> GTTypeInfoMap;
381  typedef UT_Set<int64> GTTypeSet;
382  typedef UT_ConcurrentHashMap<
383  TfToken, DefinitionForReadFunction, TfTokenHashCmp>
384  USDTypeToDefineFuncMap;
385 
386  static GTTypeInfoMap s_gtTypeInfoMap;
387  static USDTypeToDefineFuncMap s_usdTypeToFuncMap;
388  static GTTypeSet s_supportedNativeGTTypes;
389 };
390 
391 PXR_NAMESPACE_CLOSE_SCOPE
392 
393 #endif // __GUSD_PRIMWRAPPER_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:228
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