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  bool convert_to_radians = false) const;
128 
129 private:
130  UsdGeomPointInstancer m_usdPointInstancer;
131 
132  // A map of tokens to indexes in the point instancer's relationship array.
133  // The tokens could be unique ids built from USD packed prims or
134  // paths to SOP nodes (as in instancepath attributes).
135  RelationshipIndexMap m_relationshipIndexMap;
136 
137  // List of prototype transforms for "subtracting" from final instance
138  // transforms.
139  std::vector<UT_Matrix4D> m_prototypeTransforms;
140 
141  // Scope to write prototypes to (usually ../Prototypes/..).
142  SdfPath m_prototypesScope;
143 
144  // List of attributes to write for point instancer.
145  std::vector<TfToken> m_usdGeomTokens = {UsdGeomTokens->protoIndices,
146  UsdGeomTokens->positions,
147  UsdGeomTokens->orientations,
148  UsdGeomTokens->scales,
149  UsdGeomTokens->velocities,
150  UsdGeomTokens->angularVelocities};
151 
152  // Struct for helping store original data from a point instancer we are
153  // overlaying, so we can partially overlay a subset of points while
154  // writing out the original data for the others in an overlay transform.
155  template <class T>
156  struct PreOverlayDataEntry {
157  std::map<UsdTimeCode, VtArray<T>> preOverlayDataMap;
158  UsdAttribute usdAttr;
159 
160  PreOverlayDataEntry(UsdAttribute usdAttr) : usdAttr(usdAttr) {}
161 
162  PreOverlayDataEntry() {}
163  ~PreOverlayDataEntry() {}
164  PreOverlayDataEntry(PreOverlayDataEntry &&other) {
165  preOverlayDataMap = std::move(other.preOverlayDataMap);
166  usdAttr = other.usdAttr;
167  }
168  PreOverlayDataEntry& operator=(PreOverlayDataEntry &&other) {
169  preOverlayDataMap = std::move(other.preOverlayDataMap);
170  usdAttr = other.usdAttr;
171  return *this;
172  }
173  PreOverlayDataEntry(PreOverlayDataEntry const& other){
174  preOverlayDataMap = other.preOverlayDataMap;
175  usdAttr = other.usdAttr;
176  }
177 
178  // Store this attribute's data at the given time.
179  void storeAtTime(UsdTimeCode time) {
180  VtArray<T> dataArray;
181  if (usdAttr.Get(&dataArray, time)){
182  preOverlayDataMap[time] = dataArray;
183  }
184  }
185 
186  // Get the stored data for this attribute at the given point and time.
187  bool getPointValue(UsdTimeCode time, int ptNum, T& value) {
188  VtArray<T> dataArray;
189  if (preOverlayDataMap.count(time) > 0) {
190  dataArray = preOverlayDataMap[time];
191  if (dataArray.size() > ptNum) {
192  value = dataArray[ptNum];
193  return true;
194  }
195  } else {
196  usdAttr.Get(&dataArray, time);
197  if (dataArray.size() > ptNum) {
198  value = dataArray[ptNum];
199  return true;
200  }
201  }
202  return false;
203  }
204  };
205 
206 
207  // Boost variant for the different types of data stored by attributes.
208  typedef BOOST_NS::variant<PreOverlayDataEntry<int>,
209  PreOverlayDataEntry<GfVec3f>,
210  PreOverlayDataEntry<GfQuath>> dataEntry;
211 
212  // Map from attribute token to original data from base point instancer.
213  std::map<TfToken, dataEntry> m_preOverlayDataMap;
214 
215  // Visitor for the BOOST_NS variant to call the store at time function.
216  struct StoreAtTime : public BOOST_NS::static_visitor<>
217  {
218  StoreAtTime(UsdTimeCode time) : time(time){}
219 
221 
222  template <typename T>
223  void operator()(T& t) const {
224  t.storeAtTime(time);
225  }
226  };
227 
228  // Visitor to clear stored data in a data entry.
229  struct ClearData : public BOOST_NS::static_visitor<>
230  {
231  template <typename T>
232  void operator()(T t) const {
233  t.preOverlayDataMap.clear();
234  }
235  };
236 };
237 
239 
240 #endif // __GUSD_INSTANCERWRAPPER_H__
241 
A GT_Primitive that wraps a USD Prim.
Definition: primWrapper.h:90
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:3340
bool updateFromGTPrim(const GT_PrimitiveHandle &sourcePrim, const UT_Matrix4D &houXform, const GusdContext &ctxt, GusdSimpleXformCache &xformCache) override
GLdouble GLdouble t
Definition: glew.h:1403
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:166
Definition: path.h:288
Processes primitives generated by refinement process.
Definition: GT_Refine.h:20
USDGEOM_API TfStaticData< UsdGeomTokensType > UsdGeomTokens
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
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
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)
GLsizei const GLfloat * value
Definition: glcorearb.h:823
GusdPurposeSet
Definition: purpose.h:39
std::map< SdfPath, UT_Matrix4D > GusdSimpleXformCache
Definition: primWrapper.h:88
int getMotionSegments() const override