All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
instancerWrapper.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_INSTANCERWRAPPER_H__
25 #define __GUSD_INSTANCERWRAPPER_H__
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usdGeom/pointInstancer.h"
29 
30 #include "primWrapper.h"
31 
32 #include <UT/UT_Map.h>
33 #include <UT/UT_StringHolder.h>
34 
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
38 
39 class GusdInstancerWrapper : public GusdPrimWrapper
40 {
41  typedef UT_Map<TfToken, int> RelationshipIndexMap;
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  virtual ~GusdInstancerWrapper();
53 
54  virtual const UsdGeomImageable getUsdPrim() const override { return m_usdPointInstancer; }
55 
56  virtual bool redefine(
57  const UsdStagePtr& stage,
58  const SdfPath& path,
59  const GusdContext& ctxt,
60  const GT_PrimitiveHandle& sourcePrim ) override;
61 
62  virtual const char* className() const override;
63 
64  virtual void enlargeBounds(UT_BoundingBox boxes[], int nsegments) const override;
65 
66  virtual int getMotionSegments() const override;
67 
68  virtual int64 getMemoryUsage() const override;
69 
70  virtual GT_PrimitiveHandle doSoftCopy() const override;
71 
72  virtual bool
73  updateFromGTPrim(const GT_PrimitiveHandle& sourcePrim,
74  const UT_Matrix4D& houXform,
75  const GusdContext& ctxt,
76  GusdSimpleXformCache& xformCache ) override;
77 
78  virtual bool isValid() const override;
79 
80  virtual bool refine( GT_Refine& refiner,
81  const GT_RefineParms* parms = NULL ) const override;
82 
83  virtual bool unpack(
84  GU_Detail& gdr,
85  const UT_StringRef& fileName,
86  const SdfPath& primPath,
87  const UT_Matrix4D& xform,
88  fpreal frame,
89  const char * viewportLod,
90  GusdPurposeSet purposes ) override;
91 
92 public:
93 
94  static GT_PrimitiveHandle
95  defineForWrite( const GT_PrimitiveHandle& sourcePrim,
96  const UsdStagePtr& stage,
97  const SdfPath& path,
98  const GusdContext& ctxt);
99 
100  static GT_PrimitiveHandle
101  defineForRead( const UsdGeomImageable& sourcePrim,
102  UsdTimeCode time,
103  GusdPurposeSet purposes );
104 
105 private:
106  bool initUsdPrim(const UsdStagePtr& stage,
107  const SdfPath& path,
108  bool asOverride);
109 
110  void writePrototypes( const GusdContext& ctxt,
111  const UsdStagePtr& stage,
112  const GT_PrimitiveHandle& sourcePrim);
113 
114  void storePreOverlayData(bool justProtoIndices, const UsdTimeCode* time);
115 
116  void clearPreOverlayData();
117 
118  void setTransformAttrsFromMatrices(const UT_Matrix4D &worldToLocal,
119  const GT_AttributeListHandle gtAttrs,
120  GusdContext ctxt,
121  GT_PrimitiveHandle sourcePrim);
122 
123 private:
124  UsdGeomPointInstancer m_usdPointInstancer;
125 
126  // A map of tokens to indexes in the point instancer's relationship array.
127  // The tokens could be unique ids built from USD packed prims or
128  // paths to SOP nodes (as in instancepath attributes).
129  RelationshipIndexMap m_relationshipIndexMap;
130 
131  // List of prototype transforms for "subtracting" from final instance
132  // transforms.
133  std::vector<UT_Matrix4D> m_prototypeTransforms;
134 
135  // Scope to write prototypes to (usually ../Prototypes/..).
136  SdfPath m_prototypesScope;
137 
138  // List of attributes to write for point instancer.
139  std::vector<TfToken> m_usdGeomTokens = {UsdGeomTokens->protoIndices,
140  UsdGeomTokens->positions,
141  UsdGeomTokens->orientations,
142  UsdGeomTokens->scales,
143  UsdGeomTokens->velocities,
144  UsdGeomTokens->angularVelocities};
145 
146  // Struct for helping store original data from a point instancer we are
147  // overlaying, so we can partially overlay a subset of points while
148  // writing out the original data for the others in an overlay transform.
149  template <class T>
150  struct PreOverlayDataEntry {
151  std::map<UsdTimeCode, VtArray<T>> preOverlayDataMap;
152  UsdAttribute usdAttr;
153 
154  PreOverlayDataEntry(UsdAttribute usdAttr) : usdAttr(usdAttr) {}
155 
156  PreOverlayDataEntry() {}
157  ~PreOverlayDataEntry() {}
158  PreOverlayDataEntry(PreOverlayDataEntry &&other) {
159  preOverlayDataMap = std::move(other.preOverlayDataMap);
160  usdAttr = other.usdAttr;
161  }
162  PreOverlayDataEntry& operator=(PreOverlayDataEntry &&other) {
163  preOverlayDataMap = std::move(other.preOverlayDataMap);
164  usdAttr = other.usdAttr;
165  return *this;
166  }
167  PreOverlayDataEntry(PreOverlayDataEntry const& other){
168  preOverlayDataMap = other.preOverlayDataMap;
169  usdAttr = other.usdAttr;
170  }
171 
172  // Store this attribute's data at the given time.
173  void storeAtTime(UsdTimeCode time) {
174  VtArray<T> dataArray;
175  if (usdAttr.Get(&dataArray, time)){
176  preOverlayDataMap[time] = dataArray;
177  }
178  }
179 
180  // Get the stored data for this attribute at the given point and time.
181  bool getPointValue(UsdTimeCode time, int ptNum, T& value) {
182  VtArray<T> dataArray;
183  if (preOverlayDataMap.count(time) > 0) {
184  dataArray = preOverlayDataMap[time];
185  if (dataArray.size() > ptNum) {
186  value = dataArray[ptNum];
187  return true;
188  }
189  } else {
190  usdAttr.Get(&dataArray, time);
191  if (dataArray.size() > ptNum) {
192  value = dataArray[ptNum];
193  return true;
194  }
195  }
196  return false;
197  }
198  };
199 
200 
201  // Boost variant for the different types of data stored by attributes.
202  typedef boost::variant<PreOverlayDataEntry<int>,
203  PreOverlayDataEntry<GfVec3f>,
204  PreOverlayDataEntry<GfQuath>> dataEntry;
205 
206  // Map from attribute token to original data from base point instancer.
207  std::map<TfToken, dataEntry> m_preOverlayDataMap;
208 
209  // Visitor for the boost variant to call the store at time function.
210  struct StoreAtTime : public boost::static_visitor<>
211  {
212  StoreAtTime(UsdTimeCode time) : time(time){}
213 
214  UsdTimeCode time;
215 
216  template <typename T>
217  void operator()(T& t) const {
218  t.storeAtTime(time);
219  }
220  };
221 
222  // Visitor to clear stored data in a data entry.
223  struct ClearData : public boost::static_visitor<>
224  {
225  template <typename T>
226  void operator()(T t) const {
227  t.preOverlayDataMap.clear();
228  }
229  };
230 };
231 
232 PXR_NAMESPACE_CLOSE_SCOPE
233 
234 #endif // __GUSD_INSTANCERWRAPPER_H__
235 
A GT_Primitive that wraps a USD Prim.
Definition: primWrapper.h:89
Scenegraph object for authoring and retrieving numeric, string, and array valued data, sampled over time.
Definition: attribute.h:176
virtual bool updateFromGTPrim(const GT_PrimitiveHandle &sourcePrim, const UT_Matrix4D &houXform, const GusdContext &ctxt, GusdSimpleXformCache &xformCache)
Fill a USD prim&#39;s attribute samples for a frame from the attributes in a GT primitive.
virtual bool redefine(const UsdStagePtr &stage, const SdfPath &path, const GusdContext &ctxt, const GT_PrimitiveHandle &sourcePrim)
Create a new USD prim to match GT primitive.
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
USDGEOM_API TfStaticData< UsdGeomTokensType > UsdGeomTokens
A global variable with static, efficient TfTokens for use in all public USD API.
static GT_PrimitiveHandle defineForWrite(const GT_PrimitiveHandle &sourcePrim, const UsdStagePtr &stage, const SdfPath &path, const GusdContext &ctxt)
Given a GT_Primitive, create a USD prim of the proper type.
A GusdContext structure is created by the ROPs that write USD files and passed to the GusdPrimWrapper...
Definition: context.h:46
size_t size() const
Return the total number of elements in this array.
Definition: array.h:428
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
virtual bool isValid() const
Return true if the underlying USD prim is valid.
static GT_PrimitiveHandle defineForRead(const UsdGeomImageable &sourcePrim, UsdTimeCode time, GusdPurposeSet purposes)
Given a USD prim, create a GusdPrimWrapper of the proper type.
Encodes vectorized instancing of multiple, potentially animated, prototypes (object/instance masters)...
Base class for all prims that may require rendering or visualization of some sort.
Definition: imageable.h:74