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:
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:
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 #define IS_SCALAR SYS_IsArithmetic<T>::value || SYS_IsSame<T, UT_StringHolder>::value
255 #define ENABLE_IF_SCALAR typename SYS_EnableIf<IS_SCALAR>::type * = 0
256 #define ENABLE_IF_VECTOR typename SYS_DisableIf<IS_SCALAR>::type * = 0
257 
258  template <typename T>
259  static int getTupleSize(ENABLE_IF_VECTOR) { return T::tuple_size; }
260 
261  template <typename T>
262  static int getTupleSize(ENABLE_IF_SCALAR) { return 1; }
263 
264  template <typename ATTRIB_T, typename T>
265  static void getValue(ATTRIB_T *src, T &dest, ENABLE_IF_VECTOR)
266  {
267  std::copy(src, src + T::tuple_size, dest.data());
268  }
269 
270  template <typename ATTRIB_T, typename T>
271  static void getValue(ATTRIB_T *src, T &dest, ENABLE_IF_SCALAR)
272  {
273  dest = *src;
274  }
275 
276  template <typename T, typename ATTRIB_T>
277  static void setValue(const T &src, ATTRIB_T *dest, ENABLE_IF_VECTOR)
278  {
279  std::copy(src.data(), src.data() + T::tuple_size, dest);
280  }
281 
282  template <typename T, typename ATTRIB_T>
283  static void setValue(T src, ATTRIB_T *dest, ENABLE_IF_SCALAR)
284  {
285  *dest = src;
286  }
287 
288 #undef IS_SCALAR
289 #undef ENABLE_IF_SCALAR
290 #undef ENABLE_IF_VECTOR
291 
292  const GU_Detail &myGdp;
293  const GU_PrimPacked *myPrimitive;
294  const GU_Agent *myAgent;
295  GA_Offset myPointOffset;
296  exint myTransformId;
297 
298  GA_ROHandleS myName;
299  BatchROHandleA<UT_Fpreal32Array> myPivot;
300  BatchROHandleA<UT_Fpreal32Array> myOrientation;
301  BatchROHandleA<UT_Fpreal32Array> myPosition;
302 };
303 
304 template <typename ARRAY_T>
306  const 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>
315  GA_Attribute *attrib)
316  : myAttrib(attrib),
317  myCacheOffset(GA_INVALID_OFFSET),
318  myTupleSize(myAttrib.isValid() ? myAttrib->getTupleSize() : -1)
319 {
320 }
321 
322 template <typename ARRAY_T>
323 void
325 {
326  if (GAisValid(myCacheOffset))
327  {
328  myAttrib.set(myCacheOffset, myVals);
329  myCacheOffset = GA_INVALID_OFFSET;
330  }
331 }
332 
333 template <typename ARRAY_T>
334 void
336  GA_AttributeOwner owner,
337  const UT_StringRef &name,
338  int minsize)
339 {
340  myAttrib.bind(gdp, owner, name, minsize);
341  myTupleSize = myAttrib.isValid() ? myAttrib->getTupleSize() : -1;
342 }
343 
344 template <typename T, typename ARRAY_T>
345 bool
348  exint i, T &elem)
349 {
350  UT_ASSERT(handle.isValid());
351  if (!handle.isValid())
352  return false;
353 
354  const int tuple_size = handle.myTupleSize;
355  if (handle.myTupleSize != getTupleSize<T>())
356  {
357  UT_ASSERT_MSG(false, "Incorrect tuple size.");
358  return false;
359  }
360 
361  // Update our cache if we're reading from a different offset.
362  if (handle.myCacheOffset != offset)
363  {
364  handle.myAttrib.get(offset, handle.myVals);
365  handle.myCacheOffset = offset;
366  }
367 
368  if ((i + 1) * tuple_size > handle.myVals.entries())
369  return false;
370 
371  getValue(handle.myVals.array() + i * tuple_size, elem);
372 
373  return true;
374 }
375 
376 template <typename T, typename ARRAY_T>
377 void
380  const T &elem)
381 {
382  const int tuple_size = handle.myTupleSize;
383  if (tuple_size != getTupleSize<T>())
384  {
385  UT_ASSERT_MSG(false, "Incorrect tuple size.");
386  return;
387  }
388 
389  // If we're writing to a different offset, write out the array for the
390  // previous offset.
391  if (handle.myCacheOffset != offset)
392  {
393  handle.flush();
394 
395  handle.myCacheOffset = offset;
396  handle.myAttrib.get(offset, handle.myVals);
397  handle.myVals.setSize(n * tuple_size);
398  }
399 
400  setValue(elem, handle.myVals.array() + i * tuple_size);
401 }
402 
403 #endif
Definition of a geometry attribute.
Definition: GA_Attribute.h:196
const GU_Agent * getAgent() const
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:14163
virtual void getPositionTransform(UT_Matrix4D &xform, bool scale=true) const =0
virtual UT_Quaternion getOrientation() const =0
virtual UT_Vector3 getCentroid() const =0
#define ENABLE_IF_SCALAR
bool GAisValid(GA_Size v)
Definition: GA_Types.h:648
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
GLuint const GLchar * name
Definition: glcorearb.h:785
GLenum src
Definition: glcorearb.h:1792
static bool getArrayAttribElement(const BatchROHandleA< ARRAY_T > &handle, GA_Offset offset, exint i, T &elem)
#define GA_INVALID_OFFSET
Definition: GA_Types.h:677
BatchROHandleA(const GA_Attribute *attrib=nullptr)
static void setArrayAttribElement(BatchRWHandleA< ARRAY_T > &handle, GA_Offset offset, exint i, exint n, const T &elem)
#define ENABLE_IF_VECTOR
#define UT_ASSERT_MSG(ZZ,...)
Definition: UT_Assert.h:174
GA_Size GA_Offset
Definition: GA_Types.h:640
const GU_PrimPacked * getPrimitive() const
virtual const GU_Detail * getSimGdp() const =0
Return the SIM_Object's geometry.
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLhandleARB obj
Definition: glew.h:6266
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:193
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.
GLdouble n
Definition: glcorearb.h:2007
SYS_FORCE_INLINE GA_Offset getPointOffset() const
GA_AttributeOwner
Definition: GA_Types.h:33
BatchRWHandleA(GA_Attribute *attrib=nullptr)
OIIO_API bool copy(string_view from, string_view to, std::string &err)
#define SIM_API
Definition: SIM_API.h:10
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:655
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:95
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:171
GLintptr offset
Definition: glcorearb.h:664