HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SIM_ConstraintNetworkIterator.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 #ifndef __SIM_ConstraintNetworkIterator_h__
8 #define __SIM_ConstraintNetworkIterator_h__
9 
10 #include "SIM_API.h"
11 #include "SIM_DataFilter.h"
12 #include "SIM_OptionsUser.h"
13 
14 #include <GA/GA_AttributeRef.h>
15 #include <GA/GA_Handle.h>
16 #include <GU/GU_DetailHandle.h>
17 #include <UT/UT_NonCopyable.h>
18 #include <UT/UT_StringMap.h>
19 #include <UT/UT_VectorTypes.h>
20 
21 class GA_Primitive;
22 class GA_PrimitiveGroup;
23 class GU_Detail;
25 class SIM_Data;
26 class SIM_Geometry;
28 class SIM_GeometryCopy;
29 class SIM_Object;
30 class SIM_Time;
31 
32 namespace SIM_ConstraintNetwork
33 {
34  /// The type of a constraint (linear, angular, or both).
35  enum class ConstraintType
36  {
37  Position,
38  Rotation,
39  All
40  };
41 
42  /// The type of anchor specfied by 'anchor_type' attribute. Defaults to
43  /// point.
44  enum class AnchorType
45  {
46  Point,
47  Vertex,
48  Agent
49  };
50 
51  /// How the position of the anchor is interpreted.
52  enum class PositionType
53  {
54  WorldSpace,
58  };
59 
60  /// Creates the 'force' and 'distance' primitive attributes.
63  /// Creates the 'torque' and 'angle' primitive attributes.
66 
68  {
69  public:
70  /// @param container A SIM_Relationship or SIM_Object that contains the
71  /// constraint network as subdata.
72  /// @param data_root Parent of the SIM_ConRel subdata.
73  const_iterator(const SIM_Data &container, const SIM_Data &data_root,
74  const SIM_Geometry *geo, const SIM_Time &t,
76 
77  const_iterator(const SIM_Data &container, const SIM_Data &data_root,
78  const SIM_Geometry *geo, const GU_Detail *gdp,
79  const GA_PrimitiveGroup *broken_group, const SIM_Time &t,
80  const SIM_DataFilter &filter);
81 
82  void rewind();
83  bool atEnd() const;
84  void advance();
85 
86  /// The constraint network geometry.
87  const GU_Detail &getGdp() const { return *myGdp; }
88  /// The number of valid constraints that have been seen so far.
89  exint getConstraintIndex() const { return myConstraintIndex; }
90  /// The primitive index.
91  GA_Index getPrimIndex() const { return myPrimIndex; }
92  /// The primitive offset.
93  GA_Offset getPrimOffset() const;
94 
95  const SIM_Data *getConstraintData() const;
96  ConstraintType getConstraintType() const;
97 
98  /// The point number for this anchor in the constraint network geometry.
99  GA_Index getPointNumber(bool anchor1) const;
100 
101  /// Get the point offset of the point referred to by the anchor.
102  /// Either the anchor directly references a point on an object,
103  /// or it references a vertex on an object which has an associated
104  /// point. Returns GA_INVALID_OFFSET in the case where the anchor doesnt
105  /// reference a point. If 'anchor_pid' or 'anchor_vid' is present, then
106  /// return the first point (or vertex) with same id as on the
107  /// constraint.
108  GA_Offset getAnchorPointOffset(bool anchor1) const;
109 
110  /// Gets the point index of the point referred to by the anchor in a
111  /// similar way to getAnchorPointOffset.
112  GA_Index getAnchorPointIndex(bool anchor1) const;
113 
114  UT_Vector3 getAnchorPosition(bool anchor1) const;
115  UT_Vector3 getAnchorVelocity(bool anchor1) const;
116  /// Returns the type of position described by the anchor (e.g. relative
117  /// offset).
118  PositionType getAnchorPositionType(bool anchor1) const;
119 
120  UT_Quaternion getAnchorOrientation(bool anchor1) const;
121  UT_Vector3 getAnchorAngularVelocity(bool anchor1) const;
122 
123  bool getObjectCentroid(bool anchor1, UT_Vector3 &pos) const;
124 
125  int getAnchorNumConDOFs(bool anchor1) const;
126  UT_Vector3 getAnchorDOFVector(bool anchor1) const;
127 
128  UT_StringHolder getAnchorName(bool anchor1) const;
129 
130  /// Returns the id of the object named by the anchor.
131  /// Returns -1 of the anchor does not name an object.
132  int getObjectId(bool anchor1) const;
133  const SIM_Object *getObject(bool anchor1) const;
134 
135  /// Retrieve a value from the attached subdata for the constraint.
136  template <typename T>
137  T getDataOption(const UT_StringRef &attribute_name) const;
138 
139  /// Returns the value of an attribute of the constraint. If there is no
140  /// primitive attribute with that name, a default value will be taken
141  /// from the attached subdata if possible.
142  template <typename T>
143  T get(const UT_StringRef &attribute_name) const
144  {
145  T value;
146  get(attribute_name, getGdp(), myCachedAttributes, value);
147  return value;
148  }
149 
150  /// Retrieve a value from a primitive attribute of the constraint.
151  template <typename T>
152  bool getPrimitiveAttribute(const UT_StringRef &attribute_name,
153  T &value) const
154  {
155  return getPrimitiveAttribute(attribute_name, getGdp(),
156  myCachedAttributes, value);
157  }
158 
159  /// Returns the value of an attribute of the overall network (i.e. a
160  /// detail attribute). If the attribute does not exist, the default
161  /// value will be returned.
162  template <typename T>
163  T getDetailAttribute(const UT_StringRef &attribute_name,
164  const T &default_value) const;
165 
166  /// Look up an attribute value. If the primitive attribute is not
167  /// present, the value from the subdata will be used. This works for
168  /// both GA_ROAttributeRef and GA_RWAttributeRef, which require const
169  /// and non-const GU_Details, respectively.
170  template <typename T, typename Detail, typename AttributeRef>
171  void get(const UT_StringRef &attribute_name, Detail &gdp,
172  UT_StringMap<AttributeRef> &cache, T &value) const;
173 
174  /// Look up a floating-point attribute value. If present, the primitive
175  /// attribute value will be multiplied with the value from the subdata.
176  template <typename Detail, typename AttributeRef>
177  void get(const UT_StringRef &attribute_name, Detail &gdp,
178  UT_StringMap<AttributeRef> &cache, fpreal &value) const;
179 
180  /// Looks up a value for a primitive attribute. This works for both
181  /// GA_ROAttributeRef and GA_RWAttributeRef, which require const and
182  /// non-const GU_Details, respectively. Returns true if the attribute
183  /// exists, and false otherwise.
184  template <typename T, typename Detail, typename AttributeRef>
185  bool
186  getPrimitiveAttribute(const UT_StringRef &attribute_name, Detail &gdp,
188  T &value) const;
189 
190  private:
191  class CachedObjectData
192  {
193  public:
194  CachedObjectData(const SIM_BaseObjectReader &reader,
195  bool need_object_geo,
196  const UT_Matrix4D *agent_xform);
197  ~CachedObjectData() {}
198 
199  const GU_Detail *getGdp() const
200  {
201  if (myGeoReadLock)
202  {
203  const GU_Detail *gdp = myGeoReadLock->getGdp();
204  UT_ASSERT(gdp);
205  return gdp;
206  }
207  else
208  return 0;
209  }
210 
211  const SIM_Object &myObject;
213 
214  UT_DMatrix4 myPosXform;
215  UT_DMatrix4 myGeoXform;
216  UT_Matrix4D myAgentXform;
217  UT_Quaternion myPosRotation;
218  UT_Quaternion myGeoRotation;
219  UT_Quaternion myAgentRotation;
220  const UT_Vector3 myCentroid;
221 
222  GA_ROHandleQ myOrientRef;
223  GA_ROHandleV3 myVelocityRef;
224  GA_ROHandleV3 myWRef;
225 
226  GA_ROHandleI myAnchorPIDRef;
227  GA_ROHandleI myAnchorVIDRef;
228  };
229 
230  void init(const SIM_Data &container, const SIM_Geometry &geo,
231  const GU_Detail &gdp, const SIM_DataFilter &filter,
232  const SIM_Time &t);
233 
234  /// Helper functions to look up values in a SIM_OptionsUser object.
235  static void getOption(const SIM_OptionsUser *data,
236  const UT_StringRef &attribute_name, int &value)
237  {
238  value = data ? data->getOptions().getOptionI(attribute_name) : 0;
239  }
240  static void getOption(const SIM_OptionsUser *data,
241  const UT_StringRef &attribute_name, fpreal &value)
242  {
243  value = data ? data->getOptions().getOptionF(attribute_name) : 0;
244  }
245 
246  static void
247  getOption(const SIM_OptionsUser *data,
248  const UT_StringRef &attribute_name, UT_Vector2 &value)
249  {
250  value = data ? data->getOptions().getOptionV2(attribute_name) :
251  UT_Vector2D(0, 0);
252  }
253 
254  static void
255  getOption(const SIM_OptionsUser *data,
256  const UT_StringRef &attribute_name, UT_Vector3 &value)
257  {
258  value = data ? data->getOptions().getOptionV3(attribute_name) :
259  UT_Vector3D(0, 0, 0);
260  }
261 
262  static void
263  getOption(const SIM_OptionsUser *data,
264  const UT_StringRef &attribute_name, UT_Quaternion &value)
265  {
266  value = data ? data->getOptions().getOptionQ(attribute_name) :
267  UT_QuaternionD(0, 0, 0, 1);
268  }
269 
270  /// Get the point offset of the point referenced by the anchor.
271  /// Either the anchor directly references a point on an object,
272  /// or it references a vertex on an object which has an associated
273  /// point. Returns GA_INVALID_OFFSET in the case where the anchor doesnt
274  /// reference a point
275  GA_Offset
276  getAnchorPointOffset(bool anchor1, const CachedObjectData *data) const;
277 
278  /// Get the value in the anchor's anchor_id attribute
279  /// (-1 if no such attribute exists)
280  int getAnchorId(bool anchor1) const;
281 
282  /// Get the type of the Anchor (Point or Vertex)
283  AnchorType getAnchorType(bool anchor1) const;
284 
285  UT_Quaternion getInitialAnchorOrientation(bool anchor1) const;
286 
287  GA_StringIndexType getAnchorNameIndex(bool anchor1) const;
288 
289  /// Returns the cached data for an anchor by looking up the name
290  const CachedObjectData *getCachedData(bool anchor1) const;
291 
292  void skipInvalid();
293 
294  GA_Offset getPointOffset(bool anchor1) const;
295 
296  /// Returns false if nothing matched the filter.
297  bool buildConstraintDataCache(const SIM_DataFilter &filter);
298  void buildConstraintTypeCache();
299  void buildXformCache(const SIM_Data &container, const SIM_Time &t);
300 
301  const SIM_Data &myRootData;
302 
304  const GU_Detail *myGdp;
305 
306  UT_DMatrix4 myXform;
307  UT_Quaternion myOrient;
308  GA_Size myPrimIndex;
309  const GA_Primitive *myPrimitive;
310  /// Track how many constraints we've seen so far.
311  exint myConstraintIndex;
312 
313  /// Primitive group containing constraints that have been broken.
314  /// These are ignored by the iterator.
315  const GA_PrimitiveGroup *myBrokenGroup;
316 
317  /// Mapping from object names to cached data necessary for certain
318  /// functions
320 
321  /// Map from the 'constraint_type' attribute's string indices to the
322  /// corresponding ConstraintType enum values.
323  UT_Array<ConstraintType> myConstraintTypes;
324 
325  /// Map from the 'constraint_name' attribute's string indices to the
326  /// corresponding constraint subdata.
327  typedef std::pair<const SIM_Data *, const SIM_OptionsUser *>
328  ConstraintData;
329  UT_Array<ConstraintData> myConstraintDataCache;
330 
331  /// Cache references to attributes.
332  mutable UT_StringMap<GA_ROAttributeRef> myCachedAttributes;
333 
334  GA_ROHandleS myNameRef;
335  GA_ROHandleV3 myLocalPosRef;
336  GA_ROHandleQ myLocalOrientRef;
337  GA_ROHandleI myAnchorIdRef;
338  GA_ROHandleS myAnchorTypeRef;
339  GA_ROHandleV3 myVelocityRef;
340  GA_ROHandleI myNumDOFsRef;
341  GA_ROHandleV3 myDOFVectorRef;
342  GA_ROHandleV3 myRotationRef;
343  GA_ROHandleQ myOrientRef;
344  GA_ROHandleV3 myAngularVelocityRef;
345 
346  GA_ROHandleS myConstraintNameRef;
347  GA_ROHandleS myConstraintTypeRef;
348  };
349 
350  /// Provides a convenient way to process all of the constraints in the
351  /// network, and update attributes of the constraints.
353  {
354  public:
355  /// Use the filter parameter to process only a certain constraint type.
356  /// The default behaviour is to process everything.
357  iterator(const SIM_Data &container, const SIM_Data &data_root,
358  SIM_GeometryCopy *geo, const SIM_Time &t,
359  const SIM_DataFilter &filter = SIM_DataFilterAll());
360  ~iterator();
361 
362  void rewind() { return myImpl.rewind(); }
363  bool atEnd() const { return myImpl.atEnd(); }
364  void advance() { myImpl.advance(); }
365 
366  /// Return the number of constraints that have been seen so far.
367  exint getConstraintIndex() const { return myImpl.getConstraintIndex(); }
368 
369  /// Return the primitive number corresponding to the current constraint.
370  GA_Index getPrimIndex() const { return myImpl.getPrimIndex(); }
371  GA_Offset getPrimOffset() const { return myImpl.getPrimOffset(); }
372 
373  /// Return the point number corresponding to an anchor.
374  GA_Index getPointNumber(bool anchor1) const;
375 
376  GU_Detail &getGdp() const { return *myWriteableGdp; }
377 
378  /// Mark the current constraint as being broken.
379  /// This will cause it to switch to the next constraint type (if
380  /// defined) or break and be skipped on any further traversals of the
381  /// constraints. The broken constraints are placed into a primitive
382  /// group, which can then be used in a SOP solver to trigger events
383  /// when constraints break.
384  void breakConstraint();
385 
386  /// Returns the value of an attribute of the constraint. If there is no
387  /// primitive attribute with that name, a default value will be taken
388  /// from the attached subdata if possible.
389  template <typename T>
390  T get(const UT_StringRef &attribute_name) const
391  {
392  T value;
393  myImpl.get(attribute_name, *myWriteableGdp, myCachedAttributes,
394  value);
395  return value;
396  }
397 
398  /// Retrieve a value from the attached subdata for the constraint.
399  template <typename T>
400  T getDataOption(const UT_StringRef &attribute_name) const
401  {
402  return myImpl.getDataOption<T>(attribute_name);
403  }
404 
405  /// Retrieve a value from a primitive attribute of the constraint.
406  template <typename T>
407  bool getPrimitiveAttribute(const UT_StringRef &attribute_name,
408  T &value) const
409  {
410  return myImpl.getPrimitiveAttribute(attribute_name, *myWriteableGdp,
411  myCachedAttributes, value);
412  }
413 
414  template <typename T>
415  T getDetailAttribute(const UT_StringRef &attribute_name,
416  const T &default_value) const
417  {
418  return myImpl.getDetailAttribute(attribute_name, default_value);
419  }
420 
421  /// Update an attribute of the current constraint. If the primitive
422  /// attribute does not exist, it will be created.
423  /// Returns true if the value was successfully set.
424  bool set(const UT_StringHolder &attribute_name, fpreal val);
425 
426  /// Convenience method to update the constraint about the
427  /// forces or torques applied to satisfy it.
428  void updateLinearConstraintState(fpreal force, fpreal distance);
429  void updateAngularConstraintState(fpreal torque, fpreal angle);
430 
431  /// Returns the underlying const_iterator
432  const_iterator &getConstIterator() { return myImpl; }
433 
434  private:
435  static GA_PrimitiveGroup *getBrokenGroup(GU_Detail *gdp);
436 
437  SIM_GeometryCopy *myGeoCopy;
439  GU_Detail *myWriteableGdp;
440  GA_PrimitiveGroup *myBrokenGroup;
441 
442  /// Cache references to attributes.
443  mutable UT_StringMap<GA_RWAttributeRef> myCachedAttributes;
444 
445  GA_RWHandleF myForceAttrib;
446  GA_RWHandleF myDistanceAttrib;
447  GA_RWHandleF myTorqueAttrib;
448  GA_RWHandleF myAngleAttrib;
449 
450  GA_RWHandleS myConstraintName;
451  GA_RWHandleS myConstraintType;
452  GA_RWHandleS myNextConstraintName;
453  GA_RWHandleS myNextConstraintType;
454 
455  const_iterator myImpl;
456  };
457 }
458 
460 
461 #endif
PositionType
How the position of the anchor is interpreted.
GA_Index getPrimIndex() const
Return the primitive number corresponding to the current constraint.
const UT_Vector2D & getOptionV2(const UT_StringRef &name) const
const_iterator & getConstIterator()
Returns the underlying const_iterator.
UT_Vector2T< fpreal64 > UT_Vector2D
exint getConstraintIndex() const
The number of valid constraints that have been seen so far.
int64 getOptionI(const UT_StringRef &name) const
const UT_Vector3D & getOptionV3(const UT_StringRef &name) const
Read-Write string handle.
Definition: GA_Handle.h:942
bool getPrimitiveAttribute(const UT_StringRef &attribute_name, T &value) const
Retrieve a value from a primitive attribute of the constraint.
This filter accepts any data.
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:211
GA_Size GA_Offset
Definition: GA_Types.h:617
SIM_API void addTorqueAttributes(GU_Detail &gdp, GA_RWHandleF &torque, GA_RWHandleF &angle)
Creates the 'torque' and 'angle' primitive attributes.
SIM_API void addForceAttributes(GU_Detail &gdp, GA_RWHandleF &force, GA_RWHandleF &distance)
Creates the 'force' and 'distance' primitive attributes.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
T distance(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
Definition: UT_Vector4.h:634
int64 exint
Definition: SYS_Types.h:115
T getDetailAttribute(const UT_StringRef &attribute_name, const T &default_value) const
UT_QuaternionT< fpreal64 > UT_QuaternionD
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:480
UT_Vector3T< fpreal64 > UT_Vector3D
bool getPrimitiveAttribute(const UT_StringRef &attribute_name, T &value) const
Retrieve a value from a primitive attribute of the constraint.
const UT_QuaternionD & getOptionQ(const UT_StringRef &name) const
GLboolean * data
Definition: glcorearb.h:130
GA_Index getPrimIndex() const
The primitive index.
GA_Size GA_Index
Define the strictness of GA_Offset/GA_Index.
Definition: GA_Types.h:611
const GU_Detail & getGdp() const
The constraint network geometry.
const SIM_Options & getOptions() const
GLsizei const GLfloat * value
Definition: glcorearb.h:823
double fpreal
Definition: SYS_Types.h:269
ConstraintType
The type of a constraint (linear, angular, or both).
exint getConstraintIndex() const
Return the number of constraints that have been seen so far.
T getDataOption(const UT_StringRef &attribute_name) const
Retrieve a value from the attached subdata for the constraint.
#define SIM_API
Definition: SIM_API.h:10
GLuint GLfloat * val
Definition: glcorearb.h:1607
GA_BlobIndex GA_StringIndexType
Read-only handle for string attribute data.
Definition: GA_Handle.h:873
fpreal64 getOptionF(const UT_StringRef &name) const
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1296
This implements a SIM_Geometry that copies the source geometry.