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