HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIM_ObjectReader.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  */
7 
8 #ifndef __SIM_ObjectReader_h__
9 #define __SIM_ObjectReader_h__
10 
11 #include "SIM_API.h"
12 
13 #include <GA/GA_Handle.h>
14 #include <GU/GU_DetailHandle.h>
15 #include <SYS/SYS_TypeTraits.h>
16 
17 class GEO_Primitive;
18 class GU_Agent;
19 class GU_PrimPacked;
20 class SIM_Object;
21 
23 {
24 public:
25  SIM_BaseObjectReader(const SIM_Object &obj);
26  virtual ~SIM_BaseObjectReader() {}
27 
28  /// Return the SIM_Object.
29  const SIM_Object &getObject() const { return myObject; }
30 
31  virtual void getPositionTransform(UT_Matrix4D &xform, bool scale = true)
32  const = 0;
33  virtual UT_Quaternion getOrientation() const = 0;
34  virtual UT_Vector3 getCentroid() const = 0;
35 
36  /// Return the geometry of the RBD object (i.e. for a packed primitive, the
37  /// unpacked geometry).
38  virtual GU_ConstDetailHandle getObjectGdp() const = 0;
39  /// Return the SIM_Object's geometry.
40  virtual const GU_Detail *getSimGdp() const = 0;
41 
42 protected:
44 };
45 
47 {
48 public:
50 
51  void getPositionTransform(UT_Matrix4D &xform, bool scale = true)
52  const override;
53  UT_Quaternion getOrientation() const override;
54  UT_Vector3 getCentroid() const override;
55 
56  GU_ConstDetailHandle getObjectGdp() const override;
57  const GU_Detail *getSimGdp() const override;
58 
59 private:
60  GU_DetailHandleAutoReadLock myGeometry;
61 };
62 
64 {
65 public:
66  SIM_PackedPrimitiveReader(const SIM_Object &obj, const GU_Detail &gdp);
67 
68  SYS_FORCE_INLINE GA_Offset getPointOffset() const { return myPointOffset; }
69  /// Returns the primitive associated with the current point offset.
70  const GEO_Primitive *getPrimitive() const { return myPrimitive; }
71 
72  /// Returns the unpacked geometry of the packed primitive.
73  GU_ConstDetailHandle getObjectGdp() const override;
74  const GU_Detail *getSimGdp() const override;
75 
76  void getPositionTransform(UT_Matrix4D &xform, bool scale = true)
77  const override;
78  UT_Vector3 getCentroid() const override;
79  const UT_StringHolder &getName() const;
80 
81  static bool isPackedObject(const SIM_Object *obj);
82  /// Returns true if the primitive can represent an object (e.g. a packed
83  /// primitive or sphere).
84  static bool isValidPrimitive(const GEO_Primitive &prim);
85 
86  /// Update the current packed primitive. Optionally, its point offset can
87  /// be provided if it is already known.
88  void setPrimOffset(GA_Offset primoff, GA_Offset ptoff = GA_INVALID_OFFSET);
89 
90  UT_Vector3 getPivot() const;
91  UT_Vector3 getPosition() const;
92  UT_Quaternion getOrientation() const override;
93  UT_Vector3 getScale() const;
94  UT_Vector3 getVelocity() const;
95  UT_Vector3 getAngularVelocity() const;
96 
97  bool isPivotValid() const;
98  bool isPositionValid() const;
99  bool isOrientationValid() const;
100  bool isVelocityValid() const;
101  bool isAngularVelocityValid() const;
102  bool isNameValid() const;
103 
104 private:
105  const GU_Detail &myGdp;
106 
107  GA_Offset myPointOffset;
108  const GEO_Primitive *myPrimitive;
109 
110  GA_ROHandleV3 myPivot;
111  GA_ROHandleQ myOrientation;
112  GA_ROHandleV3 myScale;
113  GA_ROHandleF myPScale;
114  GA_ROHandleV3 myAngularVelocity;
115  GA_ROHandleV3 myVelocity;
116  GA_ROHandleS myName;
117  GA_ROHandleV3 myRest;
118 };
119 
121 {
122 public:
124 
125  SYS_FORCE_INLINE GA_Offset getPointOffset() const { return myPointOffset; }
126  void setPrimOffset(GA_Offset primoff, GA_Offset ptoff = GA_INVALID_OFFSET);
127 
128  void setPivot(const UT_Vector3 &pivot);
129  UT_Vector3 getPivot() const;
130 
131  void setOrientation(const UT_Quaternion &orient);
132  void setPosition(const UT_Vector3 &pos);
133  void setVelocity(const UT_Vector3 &vel);
134  void setAngularVelocity(const UT_Vector3 &avel);
135  void setName(const char *name);
136 
137  void updatePivotIntrinsic(const UT_Vector3 &pivot,
138  const UT_Matrix4D &position_xform);
139 
140  GU_Detail &getSimGdp() { return myGdp; }
141 
142 private:
143  GA_Offset myPointOffset;
144  GU_PrimPacked *myPrimPacked;
145  GU_Detail &myGdp;
146 
147  GA_RWHandleV3 myPosW;
148  GA_RWHandleV3 myPivotW;
149  GA_RWHandleQ myOrientationW;
150  GA_RWHandleV3 myAngularVelocityW;
151  GA_RWHandleV3 myVelocityW;
152  GA_RWHandleS myNameW;
153  GA_RWHandleV3 myRestW;
154 };
155 
157 {
158 public:
159  template <typename ARRAY_T>
161  {
162  BatchROHandleA(const GA_Attribute *attrib = nullptr);
163  bool isValid() const { return myAttrib.isValid(); }
164 
165  void flush() const { /* no-op */ }
166 
168  mutable ARRAY_T myVals;
171  };
172 
173  /// Batches all writes to array elements for the current offset, until a
174  /// flush() or its destructor.
175  template <typename ARRAY_T>
177  {
178  BatchRWHandleA(GA_Attribute *attrib = nullptr);
179  ~BatchRWHandleA() { flush(); }
180 
181  void flush() const;
182  bool isValid() const { return myAttrib.isValid(); }
183 
184  void bumpDataId() { myAttrib.bumpDataId(); }
185 
186  void bind(GA_Detail *gdp, GA_AttributeOwner owner,
187  const UT_StringRef &name, int minsize = 1);
188 
190  mutable ARRAY_T myVals;
193  };
194 
195  SIM_AgentShapeReader(const SIM_Object &obj, const GU_Detail &gdp);
196 
197  void setCurrentShape(GA_Offset prim_offset, exint transform_id);
198 
199  const GU_PrimPacked *getPrimitive() const { return myPrimitive; }
200  const GU_Agent *getAgent() const { return myAgent; }
201  const UT_StringHolder &getAgentName() const;
202  GA_Offset getPointOffset() const { return myPointOffset; }
203  exint getTransformId() const { return myTransformId; }
204 
205  /// Computes the two names that can be used to reference agents in a
206  /// constraint network - either 'dopobject/agentname/transform' or
207  /// 'agentname/transform' can be used. Returns false if the agent does not
208  /// have a name.
209  bool getAnchorNames(UT_WorkBuffer &full_name, UT_StringRef &name) const;
210 
211  bool hasPositionData() const;
212  UT_Vector3 getPivot() const;
213  UT_Vector3 getPosition() const;
214  UT_Quaternion getOrientation() const override;
215  UT_Vector3 getScale() const;
216  void getCurrentWorldTransform(UT_Matrix4D &xform) const;
217 
218  void getPositionTransform(UT_Matrix4D &xform, bool scale = true)
219  const override;
220  UT_Vector3 getCentroid() const override;
221 
222  /// Unpack the shape(s) attached to the current joint, optionally applying
223  /// the joint transform to the shape.
224  bool unpackBoundShapes(GU_Detail &gdp, bool apply_joint_xform = true) const;
225  /// Return the geometry of the RBD object (i.e. the shapes attached to the
226  /// current transform).
227  GU_ConstDetailHandle getObjectGdp() const override;
228  /// Return the SIM_Object's geometry.
229  const GU_Detail *getSimGdp() const override;
230 
231  /// Helper function for reading an element from an array attribute at the
232  /// given offset.
233  /// ARRAY_HANDLE_T can be a BatchROHandleA or BatchRWHandleA
234  template <typename T, typename ARRAY_HANDLE_T>
235  static bool
236  getArrayAttribElement(const ARRAY_HANDLE_T &handle,
237  GA_Offset offset, exint i, T &elem,
238  bool required = true);
239 
240  /// Helper function for updating an element of an array attribute at the
241  /// given offset.
242  template <typename T, typename ARRAY_T>
243  static void
244  setArrayAttribElement(BatchRWHandleA<ARRAY_T> &handle,
245  GA_Offset offset, exint i, exint n, const T &elem);
246 
247 private:
248  template <typename T>
249  static constexpr bool isScalar()
250  {
253  }
254 
255  template <typename T>
256  static int getTupleSize()
257  {
258  if constexpr (isScalar<T>())
259  return 1;
260  else
261  return T::tuple_size;
262  }
263 
264  template <typename ATTRIB_T, typename T>
265  static void getValue(ATTRIB_T *src, T &dest)
266  {
267  if constexpr (isScalar<T>())
268  dest = *src;
269  else
270  std::copy(src, src + T::tuple_size, dest.data());
271  }
272 
273  template <typename T, typename ATTRIB_T>
274  static void setValue(const T &src, ATTRIB_T *dest)
275  {
276  if constexpr (isScalar<T>())
277  *dest = src;
278  else
279  std::copy(src.data(), src.data() + T::tuple_size, dest);
280  }
281 
282  const GU_Detail &myGdp;
283  const GU_PrimPacked *myPrimitive;
284  const GU_Agent *myAgent;
285  GA_Offset myPointOffset;
286  exint myTransformId;
287 
288  GA_ROHandleS myName;
289  BatchROHandleA<UT_Fpreal32Array> myPivot;
290  BatchROHandleA<UT_Fpreal32Array> myOrientation;
291  BatchROHandleA<UT_Fpreal32Array> myPosition;
292  BatchROHandleA<UT_Fpreal32Array> myScale;
293 };
294 
295 template <typename ARRAY_T>
297  const GA_Attribute *attrib)
298  : myAttrib(attrib),
299  myCacheOffset(GA_INVALID_OFFSET),
300  myTupleSize(myAttrib.isValid() ? myAttrib->getTupleSize() : -1)
301 {
302 }
303 
304 template <typename ARRAY_T>
306  GA_Attribute *attrib)
307  : myAttrib(attrib),
308  myCacheOffset(GA_INVALID_OFFSET),
309  myTupleSize(myAttrib.isValid() ? myAttrib->getTupleSize() : -1)
310 {
311 }
312 
313 template <typename ARRAY_T>
314 void
316 {
317  if (GAisValid(myCacheOffset))
318  {
319  myAttrib.set(myCacheOffset, myVals);
320  myCacheOffset = GA_INVALID_OFFSET;
321  }
322 }
323 
324 template <typename ARRAY_T>
325 void
327  GA_AttributeOwner owner,
328  const UT_StringRef &name,
329  int minsize)
330 {
331  myAttrib.bind(gdp, owner, name, minsize);
332  myTupleSize = myAttrib.isValid() ? myAttrib->getTupleSize() : -1;
333 }
334 
335 template <typename T, typename ARRAY_HANDLE_T>
336 bool
338  const ARRAY_HANDLE_T &handle, GA_Offset offset,
339  exint i, T &elem, bool required)
340 {
341  UT_ASSERT(handle.isValid() || !required);
342  if (!handle.isValid())
343  return false;
344 
345  const int tuple_size = handle.myTupleSize;
346  if (handle.myTupleSize != getTupleSize<T>())
347  {
348  UT_ASSERT_MSG(false, "Incorrect tuple size.");
349  return false;
350  }
351 
352  // Update our cache if we're reading from a different offset.
353  if (handle.myCacheOffset != offset)
354  {
355  handle.flush();
356 
357  handle.myAttrib.get(offset, handle.myVals);
358  handle.myCacheOffset = offset;
359  }
360 
361  if ((i + 1) * tuple_size > handle.myVals.entries())
362  return false;
363 
364  getValue(handle.myVals.array() + i * tuple_size, elem);
365 
366  return true;
367 }
368 
369 template <typename T, typename ARRAY_T>
370 void
373  const T &elem)
374 {
375  const int tuple_size = handle.myTupleSize;
376  if (tuple_size != getTupleSize<T>())
377  {
378  UT_ASSERT_MSG(false, "Incorrect tuple size.");
379  return;
380  }
381 
382  // If we're writing to a different offset, write out the array for the
383  // previous offset.
384  if (handle.myCacheOffset != offset)
385  {
386  handle.flush();
387 
388  handle.myCacheOffset = offset;
389  handle.myAttrib.get(offset, handle.myVals);
390  handle.myVals.setSize(n * tuple_size);
391  }
392 
393  setValue(elem, handle.myVals.array() + i * tuple_size);
394 }
395 
396 #endif
Definition of a geometry attribute.
Definition: GA_Attribute.h:203
const GU_Agent * getAgent() const
virtual void getPositionTransform(UT_Matrix4D &xform, bool scale=true) const =0
virtual UT_Quaternion getOrientation() const =0
virtual UT_Vector3 getCentroid() const =0
exint getTransformId() const
const SIM_Object & getObject() const
Return the SIM_Object.
SYS_FORCE_INLINE GA_Offset getPointOffset() const
int64 exint
Definition: SYS_Types.h:125
GA_Offset getPointOffset() const
SYS_FORCE_INLINE bool GAisValid(GA_Size v)
Definition: GA_Types.h:662
#define GA_INVALID_OFFSET
Definition: GA_Types.h:694
BatchROHandleA(const GA_Attribute *attrib=nullptr)
__hostdev__ float getValue(uint32_t i) const
Definition: NanoVDB.h:5578
static void setArrayAttribElement(BatchRWHandleA< ARRAY_T > &handle, GA_Offset offset, exint i, exint n, const T &elem)
#define UT_ASSERT_MSG(ZZ,...)
Definition: UT_Assert.h:168
GA_Size GA_Offset
Definition: GA_Types.h:653
GA_API const UT_StringHolder scale
GLdouble n
Definition: glcorearb.h:2008
const GU_PrimPacked * getPrimitive() const
GLintptr offset
Definition: glcorearb.h:665
virtual const GU_Detail * getSimGdp() const =0
Return the SIM_Object's geometry.
PXL_API const char * getName(const ColorSpace *space)
Return the name of the color space.
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_FORCE_INLINE T get(GA_Offset off, int comp=0) const
Definition: GA_Handle.h:210
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual ~SIM_BaseObjectReader()
const SIM_Object & myObject
GA_API const UT_StringHolder orient
const GEO_Primitive * getPrimitive() const
Returns the primitive associated with the current point offset.
SYS_FORCE_INLINE GA_Offset getPointOffset() const
GA_AttributeOwner
Definition: GA_Types.h:35
BatchRWHandleA(GA_Attribute *attrib=nullptr)
#define SIM_API
Definition: SIM_API.h:12
Vec3< typename MatType::value_type > getScale(const MatType &mat)
Return a Vec3 representing the lengths of the passed matrix's upper 3×3's rows.
Definition: Mat.h:633
GA_API const UT_StringHolder pivot
GU_API void xform(CE_Context &context, bool recompile, int npts, const cl::Buffer &outPos, const cl::Buffer &inPos, const cl::Buffer &surfacexform, const cl::Buffer *grp=nullptr)
void bind(GA_Detail *gdp, GA_AttributeOwner owner, const UT_StringRef &name, int minsize=1)
virtual GU_ConstDetailHandle getObjectGdp() const =0
Container class for all geometry.
Definition: GA_Detail.h:105
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
static bool getArrayAttribElement(const ARRAY_HANDLE_T &handle, GA_Offset offset, exint i, T &elem, bool required=true)
Definition: format.h:1821
GLenum src
Definition: glcorearb.h:1793