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  const UT_Vector3 &pos,
24  const UT_Vector3 &pivot,
25  const UT_Quaternion &orient,
26  UT_Matrix4D &xform);
27 
29  const UT_Vector3 &pos,
30  const UT_Vector3 &pivot,
31  const UT_Quaternion &orient,
32  const UT_Vector3 &scale,
33  UT_Matrix4D &xform);
34 
36 {
37 public:
38  SIM_BaseObjectReader(const SIM_Object &obj);
39  virtual ~SIM_BaseObjectReader() {}
40 
41  /// Return the SIM_Object.
42  const SIM_Object &getObject() const { return myObject; }
43 
44  virtual void getPositionTransform(UT_Matrix4D &xform, bool scale = true)
45  const = 0;
46  virtual UT_Quaternion getOrientation() const = 0;
47  virtual UT_Vector3 getCentroid() const = 0;
48 
49  /// Return the geometry of the RBD object (i.e. for a packed primitive, the
50  /// unpacked geometry).
51  virtual GU_ConstDetailHandle getObjectGdp() const = 0;
52  /// Return the SIM_Object's geometry.
53  virtual const GU_Detail *getSimGdp() const = 0;
54 
55 protected:
57 };
58 
60 {
61 public:
63 
64  void getPositionTransform(UT_Matrix4D &xform, bool scale = true)
65  const override;
66  UT_Quaternion getOrientation() const override;
67  UT_Vector3 getCentroid() const override;
68 
69  GU_ConstDetailHandle getObjectGdp() const override;
70  const GU_Detail *getSimGdp() const override;
71 
72 private:
73  GU_DetailHandleAutoReadLock myGeometry;
74 };
75 
77 {
78 public:
79  SIM_PackedPrimitiveReader(const SIM_Object &obj, const GU_Detail &gdp);
80 
81  SYS_FORCE_INLINE GA_Offset getPointOffset() const { return myPointOffset; }
82  /// Returns the primitive associated with the current point offset.
83  const GEO_Primitive *getPrimitive() const { return myPrimitive; }
84 
85  /// Returns the unpacked geometry of the packed primitive.
86  GU_ConstDetailHandle getObjectGdp() const override;
87  const GU_Detail *getSimGdp() const override;
88 
89  void getPositionTransform(UT_Matrix4D &xform, bool scale = true)
90  const override;
91  UT_Vector3 getCentroid() const override;
92  const UT_StringHolder &getName() const;
93 
94  static bool isPackedObject(const SIM_Object *obj);
95  /// Returns true if the primitive can represent an object (e.g. a packed
96  /// primitive or sphere).
97  static bool isValidPrimitive(const GEO_Primitive &prim);
98 
99  /// Update the current packed primitive. Optionally, its point offset can
100  /// be provided if it is already known.
101  void setPrimOffset(GA_Offset primoff, GA_Offset ptoff = GA_INVALID_OFFSET);
102 
103  UT_Vector3 getPivot() const;
104  UT_Vector3 getPosition() const;
105  UT_Quaternion getOrientation() const override;
106  UT_Vector3 getScale() const;
107  UT_Vector3 getVelocity() const;
108  UT_Vector3 getAngularVelocity() const;
109 
110  bool isPivotValid() const;
111  bool isPositionValid() const;
112  bool isOrientationValid() const;
113  bool isVelocityValid() const;
114  bool isAngularVelocityValid() const;
115  bool isNameValid() const;
116 
117 private:
118  const GU_Detail &myGdp;
119 
120  GA_Offset myPointOffset;
121  const GEO_Primitive *myPrimitive;
122 
123  GA_ROHandleV3 myPivot;
124  GA_ROHandleQ myOrientation;
125  GA_ROHandleV3 myScale;
126  GA_ROHandleF myPScale;
127  GA_ROHandleV3 myAngularVelocity;
128  GA_ROHandleV3 myVelocity;
129  GA_ROHandleS myName;
130  GA_ROHandleV3 myRest;
131 };
132 
134 {
135 public:
137 
138  SYS_FORCE_INLINE GA_Offset getPointOffset() const { return myPointOffset; }
139  void setPrimOffset(GA_Offset primoff, GA_Offset ptoff = GA_INVALID_OFFSET);
140 
141  void setPivot(const UT_Vector3 &pivot);
142  UT_Vector3 getPivot() const;
143 
144  void setOrientation(const UT_Quaternion &orient);
145  void setPosition(const UT_Vector3 &pos);
146  void setVelocity(const UT_Vector3 &vel);
147  void setAngularVelocity(const UT_Vector3 &avel);
148  void setName(const char *name);
149 
150  void updatePivotIntrinsic(const UT_Vector3 &pivot,
151  const UT_Matrix4D &position_xform);
152 
153  GU_Detail &getSimGdp() { return myGdp; }
154 
155 private:
156  GA_Offset myPointOffset;
157  GU_PrimPacked *myPrimPacked;
158  GU_Detail &myGdp;
159 
160  GA_RWHandleV3 myPosW;
161  GA_RWHandleV3 myPivotW;
162  GA_RWHandleQ myOrientationW;
163  GA_RWHandleV3 myAngularVelocityW;
164  GA_RWHandleV3 myVelocityW;
165  GA_RWHandleS myNameW;
166  GA_RWHandleV3 myRestW;
167 };
168 
170 {
171 public:
172  template <typename ARRAY_T>
174  {
175  BatchROHandleA(const GA_Attribute *attrib = nullptr);
176  bool isValid() const { return myAttrib.isValid(); }
177 
178  void flush() const { /* no-op */ }
179 
181  mutable ARRAY_T myVals;
184  };
185 
186  /// Batches all writes to array elements for the current offset, until a
187  /// flush() or its destructor.
188  template <typename ARRAY_T>
190  {
191  BatchRWHandleA(GA_Attribute *attrib = nullptr);
192  ~BatchRWHandleA() { flush(); }
193 
194  void flush() const;
195  bool isValid() const { return myAttrib.isValid(); }
196 
197  void bumpDataId() { myAttrib.bumpDataId(); }
198 
199  void bind(GA_Detail *gdp, GA_AttributeOwner owner,
200  const UT_StringRef &name, int minsize = 1);
201 
203  mutable ARRAY_T myVals;
206  };
207 
208  SIM_AgentShapeReader(const SIM_Object &obj, const GU_Detail &gdp);
209 
210  void setCurrentShape(GA_Offset prim_offset, exint transform_id);
211 
212  const GU_PrimPacked *getPrimitive() const { return myPrimitive; }
213  const GU_Agent *getAgent() const { return myAgent; }
214  const UT_StringHolder &getAgentName() const;
215  GA_Offset getPointOffset() const { return myPointOffset; }
216  exint getTransformId() const { return myTransformId; }
217 
218  /// Computes the two names that can be used to reference agents in a
219  /// constraint network - either 'dopobject/agentname/transform' or
220  /// 'agentname/transform' can be used. Returns false if the agent does not
221  /// have a name.
222  bool getAnchorNames(UT_WorkBuffer &full_name, UT_StringRef &name) const;
223 
224  bool hasPositionData() const;
225  UT_Vector3 getPivot() const;
226  UT_Vector3 getPosition() const;
227  UT_Quaternion getOrientation() const override;
228  void getCurrentWorldTransform(UT_Matrix4D &xform) const;
229 
230  void getPositionTransform(UT_Matrix4D &xform, bool scale = true)
231  const override;
232  UT_Vector3 getCentroid() const override;
233 
234  /// Unpack the shape(s) attached to the current joint, optionally applying
235  /// the joint transform to the shape.
236  bool unpackBoundShapes(GU_Detail &gdp, bool apply_joint_xform = true) const;
237  /// Return the geometry of the RBD object (i.e. the shapes attached to the
238  /// current transform).
239  GU_ConstDetailHandle getObjectGdp() const override;
240  /// Return the SIM_Object's geometry.
241  const GU_Detail *getSimGdp() const override;
242 
243  /// Helper function for reading an element from an array attribute at the
244  /// given offset.
245  /// ARRAY_HANDLE_T can be a BatchROHandleA or BatchRWHandleA
246  template <typename T, typename ARRAY_HANDLE_T>
247  static bool
248  getArrayAttribElement(const ARRAY_HANDLE_T &handle,
249  GA_Offset offset, exint i, T &elem);
250 
251  /// Helper function for updating an element of an array attribute at the
252  /// given offset.
253  template <typename T, typename ARRAY_T>
254  static void
255  setArrayAttribElement(BatchRWHandleA<ARRAY_T> &handle,
256  GA_Offset offset, exint i, exint n, const T &elem);
257 
258 private:
259  template <typename T>
260  static constexpr bool isScalar()
261  {
264  }
265 
266  template <typename T>
267  static int getTupleSize()
268  {
269  if constexpr (isScalar<T>())
270  return 1;
271  else
272  return T::tuple_size;
273  }
274 
275  template <typename ATTRIB_T, typename T>
276  static void getValue(ATTRIB_T *src, T &dest)
277  {
278  if constexpr (isScalar<T>())
279  dest = *src;
280  else
281  std::copy(src, src + T::tuple_size, dest.data());
282  }
283 
284  template <typename T, typename ATTRIB_T>
285  static void setValue(const T &src, ATTRIB_T *dest)
286  {
287  if constexpr (isScalar<T>())
288  *dest = src;
289  else
290  std::copy(src.data(), src.data() + T::tuple_size, dest);
291  }
292 
293  const GU_Detail &myGdp;
294  const GU_PrimPacked *myPrimitive;
295  const GU_Agent *myAgent;
296  GA_Offset myPointOffset;
297  exint myTransformId;
298 
299  GA_ROHandleS myName;
300  BatchROHandleA<UT_Fpreal32Array> myPivot;
301  BatchROHandleA<UT_Fpreal32Array> myOrientation;
302  BatchROHandleA<UT_Fpreal32Array> myPosition;
303 };
304 
305 template <typename ARRAY_T>
307  const GA_Attribute *attrib)
308  : myAttrib(attrib),
309  myCacheOffset(GA_INVALID_OFFSET),
310  myTupleSize(myAttrib.isValid() ? myAttrib->getTupleSize() : -1)
311 {
312 }
313 
314 template <typename ARRAY_T>
316  GA_Attribute *attrib)
317  : myAttrib(attrib),
318  myCacheOffset(GA_INVALID_OFFSET),
319  myTupleSize(myAttrib.isValid() ? myAttrib->getTupleSize() : -1)
320 {
321 }
322 
323 template <typename ARRAY_T>
324 void
326 {
327  if (GAisValid(myCacheOffset))
328  {
329  myAttrib.set(myCacheOffset, myVals);
330  myCacheOffset = GA_INVALID_OFFSET;
331  }
332 }
333 
334 template <typename ARRAY_T>
335 void
337  GA_AttributeOwner owner,
338  const UT_StringRef &name,
339  int minsize)
340 {
341  myAttrib.bind(gdp, owner, name, minsize);
342  myTupleSize = myAttrib.isValid() ? myAttrib->getTupleSize() : -1;
343 }
344 
345 template <typename T, typename ARRAY_HANDLE_T>
346 bool
348  const ARRAY_HANDLE_T &handle, GA_Offset offset,
349  exint i, T &elem)
350 {
351  UT_ASSERT(handle.isValid());
352  if (!handle.isValid())
353  return false;
354 
355  const int tuple_size = handle.myTupleSize;
356  if (handle.myTupleSize != getTupleSize<T>())
357  {
358  UT_ASSERT_MSG(false, "Incorrect tuple size.");
359  return false;
360  }
361 
362  // Update our cache if we're reading from a different offset.
363  if (handle.myCacheOffset != offset)
364  {
365  handle.flush();
366 
367  handle.myAttrib.get(offset, handle.myVals);
368  handle.myCacheOffset = offset;
369  }
370 
371  if ((i + 1) * tuple_size > handle.myVals.entries())
372  return false;
373 
374  getValue(handle.myVals.array() + i * tuple_size, elem);
375 
376  return true;
377 }
378 
379 template <typename T, typename ARRAY_T>
380 void
383  const T &elem)
384 {
385  const int tuple_size = handle.myTupleSize;
386  if (tuple_size != getTupleSize<T>())
387  {
388  UT_ASSERT_MSG(false, "Incorrect tuple size.");
389  return;
390  }
391 
392  // If we're writing to a different offset, write out the array for the
393  // previous offset.
394  if (handle.myCacheOffset != offset)
395  {
396  handle.flush();
397 
398  handle.myCacheOffset = offset;
399  handle.myAttrib.get(offset, handle.myVals);
400  handle.myVals.setSize(n * tuple_size);
401  }
402 
403  setValue(elem, handle.myVals.array() + i * tuple_size);
404 }
405 
406 #endif
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
const GU_Agent * getAgent() const
virtual void getPositionTransform(UT_Matrix4D &xform, bool scale=true) const =0
virtual UT_Quaternion getOrientation() const =0
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
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
__hostdev__ void setValue(uint32_t offset, bool v)
Definition: NanoVDB.h:5750
SYS_FORCE_INLINE bool GAisValid(GA_Size v)
Definition: GA_Types.h:655
#define GA_INVALID_OFFSET
Definition: GA_Types.h:687
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:159
GA_Size GA_Offset
Definition: GA_Types.h:646
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
SIM_API void SIMcomputePositionTransform(const UT_Vector3 &pos, const UT_Vector3 &pivot, const UT_Quaternion &orient, UT_Matrix4D &xform)
SYS_FORCE_INLINE T get(GA_Offset off, int comp=0) const
Definition: GA_Handle.h:203
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)
static bool getArrayAttribElement(const ARRAY_HANDLE_T &handle, GA_Offset offset, exint i, T &elem)
#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
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:96
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
Definition: format.h:1821
GLenum src
Definition: glcorearb.h:1793