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  void setOrientation(const UT_Quaternion &orient);
143  void setPosition(const UT_Vector3 &pos);
144  void setVelocity(const UT_Vector3 &vel);
145  void setAngularVelocity(const UT_Vector3 &avel);
146  void setName(const char *name);
147 
148  void updatePivotIntrinsic(const UT_Vector3 &pivot,
149  const UT_Matrix4D &position_xform);
150 
151  GU_Detail &getSimGdp() { return myGdp; }
152 
153 private:
154  GA_Offset myPointOffset;
155  GU_PrimPacked *myPrimPacked;
156  GU_Detail &myGdp;
157 
158  GA_RWHandleV3 myPosW;
159  GA_RWHandleV3 myPivotW;
160  GA_RWHandleQ myOrientationW;
161  GA_RWHandleV3 myAngularVelocityW;
162  GA_RWHandleV3 myVelocityW;
163  GA_RWHandleS myNameW;
164  GA_RWHandleV3 myRestW;
165 };
166 
168 {
169 public:
170  template <typename ARRAY_T>
172  {
173  BatchROHandleA(const GA_Attribute *attrib = nullptr);
174  bool isValid() const { return myAttrib.isValid(); }
175 
177  mutable ARRAY_T myVals;
180  };
181 
182  /// Batches all writes to array elements for the current offset, until a
183  /// flush() or its destructor.
184  template <typename ARRAY_T>
186  {
187  BatchRWHandleA(GA_Attribute *attrib = nullptr);
188  ~BatchRWHandleA() { flush(); }
189 
190  void flush();
191  bool isValid() const { return myAttrib.isValid(); }
192 
193  void bumpDataId() { myAttrib.bumpDataId(); }
194 
195  void bind(GA_Detail *gdp, GA_AttributeOwner owner,
196  const UT_StringRef &name, int minsize = 1);
197 
199  mutable ARRAY_T myVals;
202  };
203 
204  SIM_AgentShapeReader(const SIM_Object &obj, const GU_Detail &gdp);
205 
206  void setCurrentShape(GA_Offset prim_offset, exint transform_id);
207 
208  const GU_PrimPacked *getPrimitive() const { return myPrimitive; }
209  const GU_Agent *getAgent() const { return myAgent; }
210  const UT_StringHolder &getAgentName() const;
211  GA_Offset getPointOffset() const { return myPointOffset; }
212  exint getTransformId() const { return myTransformId; }
213 
214  /// Computes the two names that can be used to reference agents in a
215  /// constraint network - either 'dopobject/agentname/transform' or
216  /// 'agentname/transform' can be used. Returns false if the agent does not
217  /// have a name.
218  bool getAnchorNames(UT_WorkBuffer &full_name, UT_StringRef &name) const;
219 
220  bool hasPositionData() const;
221  UT_Vector3 getPivot() const;
222  UT_Vector3 getPosition() const;
223  UT_Quaternion getOrientation() const override;
224  void getCurrentWorldTransform(UT_Matrix4D &xform) const;
225 
226  void getPositionTransform(UT_Matrix4D &xform, bool scale = true)
227  const override;
228  UT_Vector3 getCentroid() const override;
229 
230  /// Unpack the shape(s) attached to the current joint, optionally applying
231  /// the joint transform to the shape.
232  bool unpackBoundShapes(GU_Detail &gdp, bool apply_joint_xform = true) const;
233  /// Return the geometry of the RBD object (i.e. the shapes attached to the
234  /// current transform).
235  GU_ConstDetailHandle getObjectGdp() const override;
236  /// Return the SIM_Object's geometry.
237  const GU_Detail *getSimGdp() const override;
238 
239  /// Helper function for reading an element from an array attribute at the
240  /// given offset.
241  template <typename T, typename ARRAY_T>
242  static bool
243  getArrayAttribElement(const BatchROHandleA<ARRAY_T> &handle,
244  GA_Offset offset, exint i, T &elem);
245 
246  /// Helper function for updating an element of an array attribute at the
247  /// given offset.
248  template <typename T, typename ARRAY_T>
249  static void
250  setArrayAttribElement(BatchRWHandleA<ARRAY_T> &handle,
251  GA_Offset offset, exint i, exint n, const T &elem);
252 
253 private:
254  template <typename T>
255  static constexpr bool isScalar()
256  {
259  }
260 
261  template <typename T>
262  static int getTupleSize()
263  {
264  if constexpr (isScalar<T>())
265  return 1;
266  else
267  return T::tuple_size;
268  }
269 
270  template <typename ATTRIB_T, typename T>
271  static void getValue(ATTRIB_T *src, T &dest)
272  {
273  if constexpr (isScalar<T>())
274  dest = *src;
275  else
276  std::copy(src, src + T::tuple_size, dest.data());
277  }
278 
279  template <typename T, typename ATTRIB_T>
280  static void setValue(const T &src, ATTRIB_T *dest)
281  {
282  if constexpr (isScalar<T>())
283  *dest = src;
284  else
285  std::copy(src.data(), src.data() + T::tuple_size, dest);
286  }
287 
288  const GU_Detail &myGdp;
289  const GU_PrimPacked *myPrimitive;
290  const GU_Agent *myAgent;
291  GA_Offset myPointOffset;
292  exint myTransformId;
293 
294  GA_ROHandleS myName;
295  BatchROHandleA<UT_Fpreal32Array> myPivot;
296  BatchROHandleA<UT_Fpreal32Array> myOrientation;
297  BatchROHandleA<UT_Fpreal32Array> myPosition;
298 };
299 
300 template <typename ARRAY_T>
302  const GA_Attribute *attrib)
303  : myAttrib(attrib),
304  myCacheOffset(GA_INVALID_OFFSET),
305  myTupleSize(myAttrib.isValid() ? myAttrib->getTupleSize() : -1)
306 {
307 }
308 
309 template <typename ARRAY_T>
311  GA_Attribute *attrib)
312  : myAttrib(attrib),
313  myCacheOffset(GA_INVALID_OFFSET),
314  myTupleSize(myAttrib.isValid() ? myAttrib->getTupleSize() : -1)
315 {
316 }
317 
318 template <typename ARRAY_T>
319 void
321 {
322  if (GAisValid(myCacheOffset))
323  {
324  myAttrib.set(myCacheOffset, myVals);
325  myCacheOffset = GA_INVALID_OFFSET;
326  }
327 }
328 
329 template <typename ARRAY_T>
330 void
332  GA_AttributeOwner owner,
333  const UT_StringRef &name,
334  int minsize)
335 {
336  myAttrib.bind(gdp, owner, name, minsize);
337  myTupleSize = myAttrib.isValid() ? myAttrib->getTupleSize() : -1;
338 }
339 
340 template <typename T, typename ARRAY_T>
341 bool
344  exint i, T &elem)
345 {
346  UT_ASSERT(handle.isValid());
347  if (!handle.isValid())
348  return false;
349 
350  const int tuple_size = handle.myTupleSize;
351  if (handle.myTupleSize != getTupleSize<T>())
352  {
353  UT_ASSERT_MSG(false, "Incorrect tuple size.");
354  return false;
355  }
356 
357  // Update our cache if we're reading from a different offset.
358  if (handle.myCacheOffset != offset)
359  {
360  handle.myAttrib.get(offset, handle.myVals);
361  handle.myCacheOffset = offset;
362  }
363 
364  if ((i + 1) * tuple_size > handle.myVals.entries())
365  return false;
366 
367  getValue(handle.myVals.array() + i * tuple_size, elem);
368 
369  return true;
370 }
371 
372 template <typename T, typename ARRAY_T>
373 void
376  const T &elem)
377 {
378  const int tuple_size = handle.myTupleSize;
379  if (tuple_size != getTupleSize<T>())
380  {
381  UT_ASSERT_MSG(false, "Incorrect tuple size.");
382  return;
383  }
384 
385  // If we're writing to a different offset, write out the array for the
386  // previous offset.
387  if (handle.myCacheOffset != offset)
388  {
389  handle.flush();
390 
391  handle.myCacheOffset = offset;
392  handle.myAttrib.get(offset, handle.myVals);
393  handle.myVals.setSize(n * tuple_size);
394  }
395 
396  setValue(elem, handle.myVals.array() + i * tuple_size);
397 }
398 
399 #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
static bool getArrayAttribElement(const BatchROHandleA< ARRAY_T > &handle, GA_Offset offset, exint i, T &elem)
#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)
#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:895
GLenum src
Definition: glcorearb.h:1793