HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 GU_Agent;
18 class GU_PrimPacked;
19 class SIM_Object;
20 
21 SIM_API void
23  const UT_Quaternion &orient, UT_Matrix4D &xform);
24 
26 {
27 public:
28  SIM_BaseObjectReader(const SIM_Object &obj);
29  virtual ~SIM_BaseObjectReader() {}
30 
31  /// Return the SIM_Object.
32  const SIM_Object &getObject() const { return myObject; }
33 
34  virtual void getPositionTransform(UT_Matrix4D &xform) const = 0;
35  virtual UT_Quaternion getOrientation() const = 0;
36  virtual UT_Vector3 getCentroid() const = 0;
37 
38  /// Return the geometry of the RBD object (i.e. for a packed primitive, the
39  /// unpacked geometry).
40  virtual GU_ConstDetailHandle getObjectGdp() const = 0;
41  /// Return the SIM_Object's geometry.
42  virtual const GU_Detail *getSimGdp() const = 0;
43 
44 protected:
46 };
47 
49 {
50 public:
52 
53  virtual void getPositionTransform(UT_Matrix4D &xform) const;
54  virtual UT_Quaternion getOrientation() const;
55  virtual UT_Vector3 getCentroid() const;
56 
57  virtual GU_ConstDetailHandle getObjectGdp() const;
58  virtual const GU_Detail *getSimGdp() const;
59 
60 private:
61  GU_DetailHandleAutoReadLock myGeometry;
62 };
63 
65 {
66 public:
67  SIM_PackedPrimitiveReader(const SIM_Object &obj, const GU_Detail &gdp);
68 
69  GA_Offset getPointOffset() const { return myPointOffset; }
70  const GU_PrimPacked *getPrimPacked() const { return myPrimPacked; }
71 
72  /// Returns the unpacked geometry of the packed primitive.
73  virtual GU_ConstDetailHandle getObjectGdp() const;
74  virtual const GU_Detail *getSimGdp() const;
75 
76  virtual void getPositionTransform(UT_Matrix4D &xform) const;
77  virtual UT_Vector3 getCentroid() const;
78  const UT_StringHolder &getName() const;
79 
80  static bool isPackedObject(const SIM_Object *obj);
81  void setPrimOffset(GA_Offset offset);
82 
83  UT_Vector3 getPivot() const;
84  UT_Vector3 getPosition() const;
86  UT_Vector3 getVelocity() const;
87  UT_Vector3 getAngularVelocity() const;
88 
89  bool isPivotValid() const;
90  bool isPositionValid() const;
91  bool isOrientationValid() const;
92  bool isVelocityValid() const;
93  bool isAngularVelocityValid() const;
94  bool isNameValid() const;
95 
96 private:
97  const GU_Detail &myGdp;
98 
99  GA_Offset myPointOffset;
100  const GU_PrimPacked *myPrimPacked;
101 
102  GA_ROHandleV3 myPivot;
103  GA_ROHandleQ myOrientation;
104  GA_ROHandleV3 myAngularVelocity;
105  GA_ROHandleV3 myVelocity;
106  GA_ROHandleS myName;
107  GA_ROHandleV3 myRest;
108 };
109 
111 {
112 public:
114 
115  GA_Offset getPointOffset() const { return myPointOffset; }
116  void setPrimOffset(GA_Offset offset);
117 
118  void setPivot(const UT_Vector3 &pivot);
119  void setOrientation(const UT_Quaternion &orient);
120  void setPosition(const UT_Vector3 &pos);
121  void setVelocity(const UT_Vector3 &vel);
122  void setAngularVelocity(const UT_Vector3 &avel);
123  void setName(const char *name);
124 
125  void updatePivotIntrinsic(const UT_Vector3 &pivot,
126  const UT_Matrix4D &position_xform);
127 
128  GU_Detail &getSimGdp() { return myGdp; }
129 
130 private:
131  GA_Offset myPointOffset;
132  GU_PrimPacked *myPrimPacked;
133  GU_Detail &myGdp;
134 
135  GA_RWHandleV3 myPosW;
136  GA_RWHandleV3 myPivotW;
137  GA_RWHandleQ myOrientationW;
138  GA_RWHandleV3 myAngularVelocityW;
139  GA_RWHandleV3 myVelocityW;
140  GA_RWHandleS myNameW;
141  GA_RWHandleV3 myRestW;
142 };
143 
145 {
146 public:
147  template <typename ARRAY_T>
149  {
150  BatchROHandleA(const GA_Attribute *attrib = nullptr);
151  bool isValid() const { return myAttrib.isValid(); }
152 
154  mutable ARRAY_T myVals;
157  };
158 
159  /// Batches all writes to array elements for the current offset, until a
160  /// flush() or its destructor.
161  template <typename ARRAY_T>
163  {
164  BatchRWHandleA(GA_Attribute *attrib = nullptr);
165  ~BatchRWHandleA() { flush(); }
166 
167  void flush();
168  bool isValid() const { return myAttrib.isValid(); }
169 
170  void bumpDataId() { myAttrib.bumpDataId(); }
171 
172  void bind(GA_Detail *gdp, GA_AttributeOwner owner,
173  const UT_StringRef &name, int minsize = 1);
174 
176  mutable ARRAY_T myVals;
179  };
180 
181  SIM_AgentShapeReader(const SIM_Object &obj, const GU_Detail &gdp);
182 
183  void setCurrentShape(GA_Offset prim_offset, exint transform_id);
184 
185  const GU_PrimPacked *getPrimitive() const { return myPrimitive; }
186  const GU_Agent *getAgent() const { return myAgent; }
187  const UT_StringHolder &getAgentName() const;
188  GA_Offset getPointOffset() const { return myPointOffset; }
189  exint getTransformId() const { return myTransformId; }
190 
191  /// Computes the two names that can be used to reference agents in a
192  /// constraint network - either 'dopobject/agentname/transform' or
193  /// 'agentname/transform' can be used. Returns false if the agent does not
194  /// have a name.
195  bool getAnchorNames(UT_WorkBuffer &full_name, UT_StringRef &name) const;
196 
197  bool hasPositionData() const;
198  UT_Vector3 getPivot() const;
199  UT_Vector3 getPosition() const;
201  void getCurrentWorldTransform(UT_Matrix4D &xform) const;
202 
203  virtual void getPositionTransform(UT_Matrix4D &xform) const;
204  virtual UT_Vector3 getCentroid() const;
205 
206  /// Unpack the shape(s) attached to the current transform.
207  bool unpackBoundShapes(GU_Detail &gdp) const;
208  /// Return the geometry of the RBD object (i.e. the shapes attached to the
209  /// current transform).
210  virtual GU_ConstDetailHandle getObjectGdp() const;
211  /// Return the SIM_Object's geometry.
212  virtual const GU_Detail *getSimGdp() const;
213 
214  /// Helper function for reading an element from an array attribute at the
215  /// given offset.
216  template <typename T, typename ARRAY_T>
217  static bool
218  getArrayAttribElement(const BatchROHandleA<ARRAY_T> &handle,
219  GA_Offset offset, exint i, T &elem);
220 
221  /// Helper function for updating an element of an array attribute at the
222  /// given offset.
223  template <typename T, typename ARRAY_T>
224  static void
225  setArrayAttribElement(BatchRWHandleA<ARRAY_T> &handle,
226  GA_Offset offset, exint i, exint n, const T &elem);
227 
228 private:
229 #define IS_SCALAR SYS_IsArithmetic<T>::value || SYS_IsSame<T, UT_StringHolder>::value
230 #define ENABLE_IF_SCALAR typename SYS_EnableIf<IS_SCALAR>::type * = 0
231 #define ENABLE_IF_VECTOR typename SYS_DisableIf<IS_SCALAR>::type * = 0
232 
233  template <typename T>
234  static int getTupleSize(ENABLE_IF_VECTOR) { return T::tuple_size; }
235 
236  template <typename T>
237  static int getTupleSize(ENABLE_IF_SCALAR) { return 1; }
238 
239  template <typename ATTRIB_T, typename T>
240  static void getValue(ATTRIB_T *src, T &dest, ENABLE_IF_VECTOR)
241  {
242  std::copy(src, src + T::tuple_size, dest.data());
243  }
244 
245  template <typename ATTRIB_T, typename T>
246  static void getValue(ATTRIB_T *src, T &dest, ENABLE_IF_SCALAR)
247  {
248  dest = *src;
249  }
250 
251  template <typename T, typename ATTRIB_T>
252  static void setValue(const T &src, ATTRIB_T *dest, ENABLE_IF_VECTOR)
253  {
254  std::copy(src.data(), src.data() + T::tuple_size, dest);
255  }
256 
257  template <typename T, typename ATTRIB_T>
258  static void setValue(T src, ATTRIB_T *dest, ENABLE_IF_SCALAR)
259  {
260  *dest = src;
261  }
262 
263 #undef IS_SCALAR
264 #undef ENABLE_IF_SCALAR
265 #undef ENABLE_IF_VECTOR
266 
267  const GU_Detail &myGdp;
268  const GU_PrimPacked *myPrimitive;
269  const GU_Agent *myAgent;
270  GA_Offset myPointOffset;
271  exint myTransformId;
272 
273  GA_ROHandleS myName;
274  BatchROHandleA<UT_Fpreal32Array> myPivot;
275  BatchROHandleA<UT_Fpreal32Array> myOrientation;
276  BatchROHandleA<UT_Fpreal32Array> myPosition;
277 };
278 
279 template <typename ARRAY_T>
281  const GA_Attribute *attrib)
282  : myAttrib(attrib),
283  myCacheOffset(GA_INVALID_OFFSET),
284  myTupleSize(myAttrib.isValid() ? myAttrib->getTupleSize() : -1)
285 {
286 }
287 
288 template <typename ARRAY_T>
290  GA_Attribute *attrib)
291  : myAttrib(attrib),
292  myCacheOffset(GA_INVALID_OFFSET),
293  myTupleSize(myAttrib.isValid() ? myAttrib->getTupleSize() : -1)
294 {
295 }
296 
297 template <typename ARRAY_T>
298 void
300 {
301  if (GAisValid(myCacheOffset))
302  {
303  myAttrib.set(myCacheOffset, myVals);
304  myCacheOffset = GA_INVALID_OFFSET;
305  }
306 }
307 
308 template <typename ARRAY_T>
309 void
311  GA_AttributeOwner owner,
312  const UT_StringRef &name,
313  int minsize)
314 {
315  myAttrib.bind(gdp, owner, name, minsize);
316  myTupleSize = myAttrib.isValid() ? myAttrib->getTupleSize() : -1;
317 }
318 
319 template <typename T, typename ARRAY_T>
320 bool
323  exint i, T &elem)
324 {
325  UT_ASSERT(handle.isValid());
326  if (!handle.isValid())
327  return false;
328 
329  const int tuple_size = handle.myTupleSize;
330  if (handle.myTupleSize != getTupleSize<T>())
331  {
332  UT_ASSERT_MSG(false, "Incorrect tuple size.");
333  return false;
334  }
335 
336  // Update our cache if we're reading from a different offset.
337  if (handle.myCacheOffset != offset)
338  {
339  handle.myAttrib.get(offset, handle.myVals);
340  handle.myCacheOffset = offset;
341  }
342 
343  if ((i + 1) * tuple_size > handle.myVals.entries())
344  return false;
345 
346  getValue(handle.myVals.array() + i * tuple_size, elem);
347 
348  return true;
349 }
350 
351 template <typename T, typename ARRAY_T>
352 void
355  const T &elem)
356 {
357  const int tuple_size = handle.myTupleSize;
358  if (tuple_size != getTupleSize<T>())
359  {
360  UT_ASSERT_MSG(false, "Incorrect tuple size.");
361  return;
362  }
363 
364  // If we're writing to a different offset, write out the array for the
365  // previous offset.
366  if (handle.myCacheOffset != offset)
367  {
368  handle.flush();
369 
370  handle.myCacheOffset = offset;
371  handle.myAttrib.get(offset, handle.myVals);
372  handle.myVals.setSize(n * tuple_size);
373  }
374 
375  setValue(elem, handle.myVals.array() + i * tuple_size);
376 }
377 
378 #endif
Definition of a geometry attribute.
Definition: GA_Attribute.h:189
virtual void getPositionTransform(UT_Matrix4D &xform) const =0
const GU_Agent * getAgent() const
GA_Offset getPointOffset() const
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:625
exint getTransformId() const
const SIM_Object & getObject() const
Return the SIM_Object.
GA_Offset getPointOffset() const
Read-Write string handle.
Definition: GA_Handle.h:942
3D Vector class.
png_uint_32 i
Definition: png.h:2877
static bool getArrayAttribElement(const BatchROHandleA< ARRAY_T > &handle, GA_Offset offset, exint i, T &elem)
#define GA_INVALID_OFFSET
Definition: GA_Types.h:654
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
GA_Size GA_Offset
Definition: GA_Types.h:617
GLdouble n
Definition: glcorearb.h:2007
const GU_PrimPacked * getPrimitive() const
virtual const GU_Detail * getSimGdp() const =0
Return the SIM_Object's geometry.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
int64 exint
Definition: SYS_Types.h:115
GLintptr offset
Definition: glcorearb.h:664
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:194
virtual ~SIM_BaseObjectReader()
GLuint const GLchar * name
Definition: glcorearb.h:785
const SIM_Object & myObject
GA_API const UT_StringHolder orient
const GU_PrimPacked * getPrimPacked() const
GA_AttributeOwner
Definition: GA_Types.h:33
BatchRWHandleA(GA_Attribute *attrib=nullptr)
GA_Offset getPointOffset() const
#define SIM_API
Definition: SIM_API.h:10
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
Read-only handle for string attribute data.
Definition: GA_Handle.h:873
#define UT_ASSERT_MSG(ZZ, MM)
Definition: UT_Assert.h:105
GLenum src
Definition: glcorearb.h:1792