HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
instancerWrapper.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_INSTANCERWRAPPER_H__
25 #define __GUSD_INSTANCERWRAPPER_H__
26 
27 #include "pxr/pxr.h"
29 
30 #include "primWrapper.h"
31 
32 #include <UT/UT_Map.h>
33 #include <UT/UT_StringHolder.h>
34 
35 
37 
38 
40 {
42 
43 public:
44 
45  GusdInstancerWrapper( const UsdStagePtr& stage,
46  const SdfPath& path,
47  const GusdContext &ctxt,
48  bool isOverride = false );
49  GusdInstancerWrapper( const UsdGeomPointInstancer& usdInstancer,
50  UsdTimeCode t,
51  GusdPurposeSet purposes );
52  ~GusdInstancerWrapper() override;
53 
54  const UsdGeomImageable getUsdPrim() const override { return m_usdPointInstancer; }
55 
56  bool redefine(
57  const UsdStagePtr& stage,
58  const SdfPath& path,
59  const GusdContext& ctxt,
60  const GT_PrimitiveHandle& sourcePrim ) override;
61 
62  const char* className() const override;
63 
64  void enlargeBounds(UT_BoundingBox boxes[], int nsegments) const override;
65 
66  int getMotionSegments() const override;
67 
68  int64 getMemoryUsage() const override;
69 
70  GT_PrimitiveHandle doSoftCopy() const override;
71 
72  bool
73  updateFromGTPrim(const GT_PrimitiveHandle& sourcePrim,
74  const UT_Matrix4D& houXform,
75  const GusdContext& ctxt,
76  GusdSimpleXformCache& xformCache ) override;
77 
78  bool isValid() const override;
79 
80  bool refine( GT_Refine& refiner,
81  const GT_RefineParms* parms = NULL ) const override;
82 
83  bool unpack(
85  const UT_StringRef& fileName,
86  const SdfPath& primPath,
87  const UT_Matrix4D* xform,
88  fpreal frame,
89  const char * viewportLod,
90  GusdPurposeSet purposes,
91  const GT_RefineParms &rparms) const override;
92 
93 public:
94 
95  static GT_PrimitiveHandle
96  defineForWrite( const GT_PrimitiveHandle& sourcePrim,
97  const UsdStagePtr& stage,
98  const SdfPath& path,
99  const GusdContext& ctxt);
100 
101  static GT_PrimitiveHandle
102  defineForRead( const UsdGeomImageable& sourcePrim,
104  GusdPurposeSet purposes );
105 
106 private:
107  bool initUsdPrim(const UsdStagePtr& stage,
108  const SdfPath& path,
109  bool asOverride);
110 
111  void writePrototypes( const GusdContext& ctxt,
112  const UsdStagePtr& stage,
113  const GT_PrimitiveHandle& sourcePrim);
114 
115  void storePreOverlayData(bool justProtoIndices, const UsdTimeCode* time);
116 
117  void clearPreOverlayData();
118 
119  void setTransformAttrsFromMatrices(const UT_Matrix4D &worldToLocal,
120  const GT_AttributeListHandle gtAttrs,
121  GusdContext ctxt,
122  GT_PrimitiveHandle sourcePrim);
123 
124  void addStandardAttribute(const UsdAttribute &attr,
125  const UT_StringHolder &attr_name,
126  GT_AttributeListHandle &point_attribs,
127  exint num_instances,
128  bool convert_to_radians = false) const;
129 
130 private:
131  UsdGeomPointInstancer m_usdPointInstancer;
132 
133  // A map of tokens to indexes in the point instancer's relationship array.
134  // The tokens could be unique ids built from USD packed prims or
135  // paths to SOP nodes (as in instancepath attributes).
136  RelationshipIndexMap m_relationshipIndexMap;
137 
138  // List of prototype transforms for "subtracting" from final instance
139  // transforms.
140  std::vector<UT_Matrix4D> m_prototypeTransforms;
141 
142  // Scope to write prototypes to (usually ../Prototypes/..).
143  SdfPath m_prototypesScope;
144 
145  // List of attributes to write for point instancer.
146  std::vector<TfToken> m_usdGeomTokens = {UsdGeomTokens->protoIndices,
147  UsdGeomTokens->positions,
148  UsdGeomTokens->orientations,
149  UsdGeomTokens->scales,
150  UsdGeomTokens->velocities,
151  UsdGeomTokens->angularVelocities};
152 
153  // Struct for helping store original data from a point instancer we are
154  // overlaying, so we can partially overlay a subset of points while
155  // writing out the original data for the others in an overlay transform.
156  template <class T>
157  struct PreOverlayDataEntry {
158  std::map<UsdTimeCode, VtArray<T>> preOverlayDataMap;
159  UsdAttribute usdAttr;
160 
161  PreOverlayDataEntry(UsdAttribute usdAttr) : usdAttr(usdAttr) {}
162 
163  PreOverlayDataEntry() {}
164  ~PreOverlayDataEntry() {}
165  PreOverlayDataEntry(PreOverlayDataEntry &&other) {
166  preOverlayDataMap = std::move(other.preOverlayDataMap);
167  usdAttr = other.usdAttr;
168  }
169  PreOverlayDataEntry& operator=(PreOverlayDataEntry &&other) {
170  preOverlayDataMap = std::move(other.preOverlayDataMap);
171  usdAttr = other.usdAttr;
172  return *this;
173  }
174  PreOverlayDataEntry(PreOverlayDataEntry const& other){
175  preOverlayDataMap = other.preOverlayDataMap;
176  usdAttr = other.usdAttr;
177  }
178 
179  // Store this attribute's data at the given time.
180  void storeAtTime(UsdTimeCode time) {
181  VtArray<T> dataArray;
182  if (usdAttr.Get(&dataArray, time)){
183  preOverlayDataMap[time] = dataArray;
184  }
185  }
186 
187  // Get the stored data for this attribute at the given point and time.
188  bool getPointValue(UsdTimeCode time, int ptNum, T& value) {
189  VtArray<T> dataArray;
190  if (preOverlayDataMap.count(time) > 0) {
191  dataArray = preOverlayDataMap[time];
192  if (dataArray.size() > ptNum) {
193  value = dataArray[ptNum];
194  return true;
195  }
196  } else {
197  usdAttr.Get(&dataArray, time);
198  if (dataArray.size() > ptNum) {
199  value = dataArray[ptNum];
200  return true;
201  }
202  }
203  return false;
204  }
205  };
206 
207 
208  // Variant for the different types of data stored by attributes.
209  typedef std::variant<PreOverlayDataEntry<int>,
210  PreOverlayDataEntry<GfVec3f>,
211  PreOverlayDataEntry<GfQuath>> dataEntry;
212 
213  // Map from attribute token to original data from base point instancer.
214  std::map<TfToken, dataEntry> m_preOverlayDataMap;
215 
216  // Visitor for the variant to call the store at time function.
217  struct StoreAtTime
218  {
219  StoreAtTime(UsdTimeCode time) : time(time){}
220 
222 
223  template <typename T>
224  void operator()(T& t) const {
225  t.storeAtTime(time);
226  }
227  };
228 
229  // Visitor to clear stored data in a data entry.
230  struct ClearData
231  {
232  template <typename T>
233  void operator()(T t) const {
234  t.preOverlayDataMap.clear();
235  }
236  };
237 };
238 
240 
241 #endif // __GUSD_INSTANCERWRAPPER_H__
242 
A GT_Primitive that wraps a USD Prim.
Definition: primWrapper.h:90
bool unpack(UT_Array< GU_DetailHandle > &details, const UT_StringRef &fileName, const SdfPath &primPath, const UT_Matrix4D *xform, fpreal frame, const char *viewportLod, GusdPurposeSet purposes, const GT_RefineParms &rparms) const override
bool refine(GT_Refine &refiner, const GT_RefineParms *parms=NULL) const override
GT_API const UT_StringHolder time
~GusdInstancerWrapper() override
SYS_FORCE_INLINE void clear()
static GT_PrimitiveHandle defineForRead(const UsdGeomImageable &sourcePrim, UsdTimeCode time, GusdPurposeSet purposes)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
bool updateFromGTPrim(const GT_PrimitiveHandle &sourcePrim, const UT_Matrix4D &houXform, const GusdContext &ctxt, GusdSimpleXformCache &xformCache) override
int64 exint
Definition: SYS_Types.h:125
bool isValid() const override
Return true if the underlying USD prim is valid.
UT_IntrusiveRefCounter & operator=(const UT_IntrusiveRefCounter &) noexcept
Assignment operator: Does not modify counter.
GT_PrimitiveHandle doSoftCopy() const override
const char * className() const override
bool redefine(const UsdStagePtr &stage, const SdfPath &path, const GusdContext &ctxt, const GT_PrimitiveHandle &sourcePrim) override
Create a new USD prim to match GT primitive.
long long int64
Definition: SYS_Types.h:116
GusdInstancerWrapper(const UsdStagePtr &stage, const SdfPath &path, const GusdContext &ctxt, bool isOverride=false)
int64 getMemoryUsage() const override
Definition: types.h:170
Definition: path.h:290
GLdouble t
Definition: glad.h:2397
Processes primitives generated by refinement process.
Definition: GT_Refine.h:20
USDGEOM_API TfStaticData< UsdGeomTokensType > UsdGeomTokens
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
const UsdGeomImageable getUsdPrim() const override
void enlargeBounds(UT_BoundingBox boxes[], int nsegments) const override
fpreal64 fpreal
Definition: SYS_Types.h:277
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
static GT_PrimitiveHandle defineForWrite(const GT_PrimitiveHandle &sourcePrim, const UsdStagePtr &stage, const SdfPath &path, const GusdContext &ctxt)
Definition: core.h:1131
GusdPurposeSet
Definition: purpose.h:39
std::map< SdfPath, UT_Matrix4D > GusdSimpleXformCache
Definition: primWrapper.h:88
int getMotionSegments() const override