HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIM_SweptCollisionUtility.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_SweptCollisionUtility_h__
9 #define __SIM_SweptCollisionUtility_h__
10 
11 #include "SIM_API.h"
12 #include "SIM_SweptCollisionData.h"
13 #include <UT/UT_VectorTypes.h>
14 #include <GA/GA_PageHandle.h>
15 #include <GU/GU_DetailHandle.h>
16 #include <vector>
17 
18 class SIM_Engine;
19 class SIM_Object;
20 class SIM_Impacts;
21 class SIM_Geometry;
22 class SIM_GeometryCopy;
23 class SIM_Time;
24 class GU_Detail;
25 class GU_PrimPacked;
26 
27 //////////////////////////////////////////////////////////////////////////////
28 //
29 // Data structures for merging geometry from an array of SIM_Objects
30 //
31 //////////////////////////////////////////////////////////////////////////////
32 
33 inline bool SIMisValidIndex(const int i, const int n)
34 {
35  return (0 <= i) && (i < n);
36 }
37 
38 // Identifies a point:
39 // the object index it belongs to and the point-id for that geometry
41 {
43  myIndexObject(-1),
44  myPid(-1)
45  {
46  }
47 
48  // Index in std::vector of SIM_Object
50  // point-id relative to object
51  int myPid;
52 };
53 
54 // MergedPoints represents a set of points that belong to a sequence of objects
56 {
57 public:
58  // default construct: merge map for zero objects
60  myIdentities(),
61  myRanges()
62  {
63  }
64 
65  // Number of merged points (total for all objects)
66  inline int getNumPointsMerged() const
67  {
68  return myIdentities.size();
69  }
70 
71  // For a merged point, obtain the object index and object point index (pid)
72  void getObjectAndPid(int& index_object, int& pid, const int p) const
73  {
74  UT_ASSERT( (0 <= p) && (p < myIdentities.size()) );
75 
76  const SIM_MergedIdentityPoint& id(myIdentities[p]);
77  index_object = id.myIndexObject;
78  pid = id.myPid;
79  }
80 
81  // Return the number of objects for this merge map.
82  // Each object index for this merge map lies in the half-open range
83  // [0, getNumObjects() - 1)
84  inline int getNumObjects() const
85  {
86  return myRanges.size();
87  }
88 
89  // Number of points for a object, given the object index
90  inline int getNumPointsObject(const int index_object) const
91  {
92  if( !SIMisValidIndex(index_object, myRanges.size()) )
93  {
94  UT_ASSERT( !"not a valid object index!" );
95  return 0;
96  }
97 
98  const std::pair< int, int >& object_range(myRanges[index_object]);
99 
100  const int num_points_range(object_range.second - object_range.first);
101 
102  UT_ASSERT( num_points_range >= 0 );
103 
104  return num_points_range;
105  }
106 
107 private:
108  // Joint array of point identities for object sequence
109  std::vector<SIM_MergedIdentityPoint> myIdentities;
110  // Contiguous sub-range [begin, end) of indices into myIdentities
111  // for each geometry index
112  std::vector< std::pair< int, int > > myRanges;
113 
114  friend void SIM_API
116  SIM_MergeMap& merged_points,
117  const UT_Array<const GU_Detail *> &gdps
118  );
119 
120  friend class SIM_MergeMapObject;
121 
122  // Disallow
123  SIM_MergeMap(const SIM_MergeMap&);
124  SIM_MergeMap& operator=(const SIM_MergeMap&);
125 };
126 
127 // Get a map for a fixed object that maps local point indices (pids)
128 // to indices for the merged points.
129 // This map is valid only as long as the SIM_MergeMap object
130 // that it was created from exists unchanged.
132 {
133 public:
135  const SIM_MergeMap& merged_points,
136  const int index_object
137  ) :
138  myIdentities(merged_points.myIdentities),
139  myBegin(merged_points.myRanges[index_object].first),
140  myEnd(merged_points.myRanges[index_object].second)
141  {
142  UT_ASSERT( 0 <= index_object );
143  UT_ASSERT( SIMisValidIndex(index_object, merged_points.myRanges.size()) );
144  }
145 
146  // Get merged point index from object point index (pid)
147  // Return -1 if the pid isn't merged.
148  inline int getP(const int pid) const
149  {
150  UT_ASSERT( (0 <= pid) && (pid < myEnd - myBegin) );
151 
152  return myBegin + pid;
153  }
154 
155  inline int getNumPointsObject() const
156  {
157  return myEnd - myBegin;
158  }
159 
160  inline int getNumPointsMerged() const
161  {
162  return myEnd - myBegin;
163  }
164 
165 private:
166  const std::vector<SIM_MergedIdentityPoint>& myIdentities;
167  const int myBegin;
168  const int myEnd;
169 
170  // Disallow
172  SIM_MergeMapObject operator=(const SIM_MergeMapObject&);
173 };
174 
175 /// Unpacks any packed primitives in the given SIM_Geometry and stores the
176 /// original primitive index of each primitive (e.g. for correctly recording
177 /// impacts).
179 {
180 public:
182  SIM_UnpackedGeometry(const GU_Detail &gdp);
183  SIM_UnpackedGeometry(const SIM_Geometry &simgeo);
184 
185  /// Returns the unpacked geometry.
187  {
188  return myGeo;
189  }
190 
191  /// Returns the primitive index in the source geometry that the given
192  /// primitive in the unpacked geometry corresponds to.
194  {
195  return myOriginalPrims(i);
196  }
197 
198 private:
199  void setGeometry(const GU_ConstDetailHandle &gdh);
200 
201  GU_ConstDetailHandle myGeo;
202  GA_IndexArray myOriginalPrims;
203 };
204 
205 //////////////////////////////////////////////////////////////////////////////
206 //
207 // Extract merged position, velocity, mass and thickness from
208 // an array of SIM_Objects
209 //
210 //////////////////////////////////////////////////////////////////////////////
211 
212 // Construct merged array of points for an array of geometries.
213 void SIM_API
215  SIM_MergeMap& merged_points,
216  const UT_Array<const GU_Detail *> &gdps
217 );
218 
219 /// Extract the triangle connectivity for a single object.
220 void SIM_API
222  SIM_TriangleConnectivity &triangles,
223  const SIM_MergeMapObject &merged_points,
224  const SIM_UnpackedGeometry &unpacked_geometry,
225  const char *geopath
226 );
227 
228 // Extract the geometry and the corresponding transform from an object.
229 // The resulting (transform, geometry) pairs can be used with
230 // SIMextractPositionPoints and SIMextractPositionTriangles
231 extern bool SIM_API
233  const SIM_Geometry*& geometry,
234  UT_DMatrix4& geometry_to_sim,
235  const SIM_Object& object
236 );
237 
238 // Extract the geometry and the corresponding transform
239 // that correspond to time t. Return false if this information
240 // couldn't be extracted from updated_object.
241 // This assumes that substep objects are set up correctly.
242 // (This is not the case for particle fluid objects, which
243 // store the substep positions in the final end state object).
244 // The resulting (transform, geometry) pairs can be used with
245 // SIMextractPositionPoints and SIMextractPositionTriangles
246 extern bool SIM_API
248  const SIM_Geometry*& geometry,
249  UT_DMatrix4& geometry_to_sim,
250  const SIM_Engine& engine,
251  const SIM_Object& undated_object,
252  const SIM_Time& t
253 );
254 
255 // Extract merged point thickness array for a set of point-set objects
256 // (e.g., particle objects). The thickness is taken from the "pscale"
257 // attribute, if this attribute is present. If there is no "pscale" attribute,
258 // then default_thickness is used.
259 extern void SIM_API
261  std::vector<fpreal32>& thickness_points,
262  const SIM_MergeMap& merged_points,
263  const UT_Array<const GU_Detail *> &gdps_end,
264  const fpreal default_thickness
265 );
266 
267 // Read the bounce parameter for an object
268 extern fpreal SIM_API
270  const SIM_Object& object,
271  const fpreal default_bounce
272 );
273 
274 // Read the friction parameter for an object
275 extern fpreal SIM_API
277  const SIM_Object& object,
278  const fpreal default_friction
279 );
280 
281 // Read the bounceforward parameter for an object
282 extern fpreal SIM_API
284  const SIM_Object& object,
285  const fpreal default_bounceforward
286 );
287 
288 //////////////////////////////////////////////////////////////////////////////
289 //
290 // Extract merged swept-collision state from an array of SIM_Objects
291 //
292 //////////////////////////////////////////////////////////////////////////////
293 
294 class SIM_Position;
295 class SIM_Object;
296 class SIM_Engine;
297 
298 //
299 // SIMreadSweptState reads from a set of objects all the topological and
300 // spatial data that is needed to perform swept-collision detection for
301 // all these objects.
302 // The data returned by this functions matches the geometry's end topology
303 // (the topology associated with t_end).
304 // (points and primitives may be added/deleted between t_begin and t_end).
305 // The "id" point-attribute will be used to carry over position data between
306 // the possibly different start and end topologies.
307 //
308 // This function will work correctly *only* if the substep objects have been
309 // set up correctly:
310 // It assumes that
311 // engine.getObjectAtTime(object, t_start) and
312 // engine.getObjectAtTime(object, t_end)
313 // return the correct states for the substep times t_start and t_end,
314 // respectively.
315 // (This assumption is broken by the particle fluid solver, which
316 // stores the substep state in the end state for optimization purposes.
317 // Therefore, the particle fluid solver does not use this function;
318 // it has a special code path to get the right information.)
319 //
320 // The topological and spatial data that is returned by SIMreadSweptState
321 // can be used as inputs for the function SIMdetectAndResolveCollisions,
322 // which detects and resolves collisions between two merged sets
323 // of geometry.
324 // The "pscale" attribute is used to initialize the thickness for each point.
325 // If this attribute is absent, then default_thickness is used
326 //
327 extern void SIM_API
329  SIM_MergeMap& merged_points,
330  std::vector<SIM_UnpackedGeometry> &unpacked_geometries_end,
331  std::vector<int>& id_objects,
332  std::vector<fpreal32> *thickness_points,
333  std::vector<fpreal32> *bounce_points,
334  std::vector<fpreal32> *friction_points,
335  std::vector<fpreal32> *bounceforward_points,
336  SIM_PositionPoints& position_points_start,
337  SIM_PositionPoints& position_points_end,
338  SIM_VelocityPoints *velocity_points_end,
339  const SIM_Engine& engine,
340  const std::vector<const SIM_Object*>& objects,
341  const fpreal default_thickness,
342  const fpreal64 t_start,
343  const fpreal64 t_end
344 );
345 
346 // This version reads from geometry.
347 extern void SIM_API
349  SIM_MergeMap& merged_points,
350  SIM_UnpackedGeometry &unpacked_geo_end,
351  std::vector<fpreal32> *thickness_points,
352  SIM_PositionPoints& position_points_start,
353  SIM_PositionPoints& position_points_end,
354  SIM_VelocityPoints *velocity_points_end,
355  const GU_Detail *gdp_start,
356  const UT_DMatrix4 &geoxform_start,
357  const GU_Detail *gdp_end,
358  const UT_DMatrix4 &geoxform_end,
359  const fpreal default_thickness,
360  const fpreal64 t_start,
361  const fpreal64 t_end
362 );
363 
364 // This version reads from a live object. It uses the previous
365 // position attribute to get the start positions rather than
366 // relying on the cache.
367 // You should provide arange. The page handles will be set up
368 // for you.
369 extern bool SIM_API
371  const SIM_Object *object,
372  GU_Detail *gdp,
373  GA_Range &arange,
374  GA_ROPageHandleF *thickness_points,
375  GA_ROPageHandleF *bounce_points,
376  GA_ROPageHandleF *friction_points,
377  GA_ROPageHandleF *dynamicfriction_points,
378  GA_ROPageHandleF *bounceforward_points,
379  GA_ROPageHandleF *mass_points,
380  GA_RWPageHandleV3 &position_points_start,
381  GA_RWPageHandleV3 &position_points_end,
382  GA_RWPageHandleV3 *velocity_points_end,
383  fpreal default_thickness);
384 
385 /// Extract the triangle connectivity for a set of objects.
386 extern void SIM_API
388  SIM_TriangleConnectivity& triangles,
389  const SIM_MergeMap& merged_points,
390  const std::vector<SIM_UnpackedGeometry>& unpacked_geometries,
391  const std::vector<const SIM_Object*>& objects
392 );
393 
394 //////////////////////////////////////////////////////////////////////////////
395 //
396 // Read/Write merged position and merged velocity for an array
397 // of SIM_Objects
398 //
399 //////////////////////////////////////////////////////////////////////////////
400 
401 // Read the merged position sequence for a set of points
402 // that belong to geometries.
403 extern void SIM_API
405  SIM_PositionPoints& position_points,
406  const SIM_MergeMap& merged_points,
407  const std::vector<UT_DMatrix4>& transforms,
408  const UT_Array<const GU_Detail *> &gdps
409 );
410 
411 // Read the merged velocity sequence
412 // for a set of points that belong to geometries.
413 // Use the velocity attribute for this
414 extern void SIM_API
416  SIM_VelocityPoints& velocity_points,
417  const SIM_MergeMap& merged_points,
418  const std::vector<UT_DMatrix4>& transforms,
419  const UT_Array<const GU_Detail*> &gdps
420 );
421 
422 // Set the position and velocity for all points in an array of geometries.
423 // position_points is written to the point positions
424 // velocity_points is written to the velocity attribute "v"
425 // it is assumed that for geometry[i], the map from object space to
426 // world simulation space is given by transforms[i].
427 // The a merged positions and velocities are transformed into
428 // the geometry's object space when they're applied to a point.
429 extern void SIM_API
431  const std::vector<SIM_GeometryCopy*>& geometries,
432  const SIM_MergeMap& merged_points,
433  const SIM_PositionPoints& position_points,
434  const SIM_VelocityPoints& velocity_points,
435  const std::vector<UT_DMatrix4>& transforms
436 );
437 
438 // Read the previous positions for all objects from the previous-position
439 // attribute. The attribute stores the position in world coordinates
440 // (no transform). Elements in r_points for points that don't have
441 // the attribute will retain the values from before the call.
442 extern void SIM_API
444  SIM_PositionPoints& position_points,
445  const SIM_MergeMap& merge_map,
446  const std::vector<const SIM_Geometry*>& geometries_end
447 );
448 
449 // Store the previous positions for all objects to the previous-position
450 // attribute. The attribute stores the position in world coordinates
451 // (no transform). Elements in r_points for points that don't have
452 // the attribute will not have their values written.
453 extern void SIM_API
455  std::vector<SIM_GeometryCopy*>& geometries_end,
456  const SIM_MergeMap& merge_map,
457  const SIM_PositionPoints& position_points
458 );
459 
460 // Copy the given position attribute into the previous position
461 // attribute wherever the exist flag is false. Then set the exist
462 // flag to true everywhere.
463 // pos_ph should be initialized to your position attribute.
464 // prevpos_ph will be created to point to the previous position.
465 extern void SIM_API
467  GU_Detail *gdp,
468  GA_RWPageHandleV3 &prevpos_ph,
469  GA_RWPageHandleV3 &pos_ph
470 );
471 
472 // Copies the given position attribute blindly into the
473 // previous position attribute.
474 extern void SIM_API
476  GU_Detail *gdp,
477  GA_RWPageHandleV3 &prevpos_ph,
478  GA_RWPageHandleV3 &pos_ph
479 );
480 
481 //////////////////////////////////////////////////////////////////////////////
482 //
483 // Detect collisions, resolve collisions, and generate impacts for
484 // an array of SIM_Objects.
485 //
486 //////////////////////////////////////////////////////////////////////////////
487 
488 // This function detects collisions and generates impacts for
489 // a pair of objects (a, b).
490 // that aim to resolve the collisions.
491 extern bool SIM_API
493  SIM_Impacts* impacts_a,
494  SIM_Impacts* impacts_b,
495  const SIM_Engine& engine,
496  SIM_Object*const undated_object_a,
497  const SIM_Object*const undated_object_b,
498  const SIM_Time& t_start,
499  const SIM_Time& t_end,
500  const int impact_flags,
501  const bool pscaleisradius
502 );
503 
504 #endif
fpreal SIM_API SIMreadBounceForwardParameterObject(const SIM_Object &object, const fpreal default_bounceforward)
GLint first
Definition: glcorearb.h:404
int getNumPointsObject(const int index_object) const
png_infop int transforms
Definition: png.h:2591
void SIM_API SIMextractTriangleConnectivityMerged(SIM_TriangleConnectivity &triangles, const SIM_MergeMapObject &merged_points, const SIM_UnpackedGeometry &unpacked_geometry, const char *geopath)
Extract the triangle connectivity for a single object.
int getNumPointsMerged() const
void getObjectAndPid(int &index_object, int &pid, const int p) const
bool SIMisValidIndex(const int i, const int n)
void SIM_API SIMcopyAttribute(GU_Detail *gdp, GA_RWPageHandleV3 &prevpos_ph, GA_RWPageHandleV3 &pos_ph)
void SIM_API SIMreadPositionPoints(SIM_PositionPoints &position_points, const SIM_MergeMap &merged_points, const std::vector< UT_DMatrix4 > &transforms, const UT_Array< const GU_Detail * > &gdps)
void SIM_API SIMreadSweptState(SIM_MergeMap &merged_points, std::vector< SIM_UnpackedGeometry > &unpacked_geometries_end, std::vector< int > &id_objects, std::vector< fpreal32 > *thickness_points, std::vector< fpreal32 > *bounce_points, std::vector< fpreal32 > *friction_points, std::vector< fpreal32 > *bounceforward_points, SIM_PositionPoints &position_points_start, SIM_PositionPoints &position_points_end, SIM_VelocityPoints *velocity_points_end, const SIM_Engine &engine, const std::vector< const SIM_Object * > &objects, const fpreal default_thickness, const fpreal64 t_start, const fpreal64 t_end)
fpreal SIM_API SIMreadBounceParameterObject(const SIM_Object &object, const fpreal default_bounce)
void SIM_API SIMreadPreviousPositionAttribute(SIM_PositionPoints &position_points, const SIM_MergeMap &merge_map, const std::vector< const SIM_Geometry * > &geometries_end)
png_uint_32 i
Definition: png.h:2877
A range of elements in an index-map.
Definition: GA_Range.h:42
GLuint id
Definition: glcorearb.h:654
GLdouble n
Definition: glcorearb.h:2007
void SIM_API SIMwritePreviousPositionAttribute(std::vector< SIM_GeometryCopy * > &geometries_end, const SIM_MergeMap &merge_map, const SIM_PositionPoints &position_points)
void SIM_API SIMreadThicknessPoints(std::vector< fpreal32 > &thickness_points, const SIM_MergeMap &merged_points, const UT_Array< const GU_Detail * > &gdps_end, const fpreal default_thickness)
bool SIM_API SIMgetGeometryAndTransformAtTime(const SIM_Geometry *&geometry, UT_DMatrix4 &geometry_to_sim, const SIM_Engine &engine, const SIM_Object &undated_object, const SIM_Time &t)
double fpreal64
Definition: SYS_Types.h:192
void SIM_API SIMwritePositionAndVelocityPoints(const std::vector< SIM_GeometryCopy * > &geometries, const SIM_MergeMap &merged_points, const SIM_PositionPoints &position_points, const SIM_VelocityPoints &velocity_points, const std::vector< UT_DMatrix4 > &transforms)
int getNumObjects() const
GU_ConstDetailHandle getGeometry() const
Returns the unpacked geometry.
SIM_MergeMapObject(const SIM_MergeMap &merged_points, const int index_object)
void SIM_API SIMmergePoints(SIM_MergeMap &merged_points, const UT_Array< const GU_Detail * > &gdps)
GA_Size GA_Index
Define the strictness of GA_Offset/GA_Index.
Definition: GA_Types.h:611
int getP(const int pid) const
double fpreal
Definition: SYS_Types.h:270
bool SIM_API SIMdetectCollisionsAndGenerateImpulses(SIM_Impacts *impacts_a, SIM_Impacts *impacts_b, const SIM_Engine &engine, SIM_Object *const undated_object_a, const SIM_Object *const undated_object_b, const SIM_Time &t_start, const SIM_Time &t_end, const int impact_flags, const bool pscaleisradius)
#define SIM_API
Definition: SIM_API.h:10
std::vector< SIM_PositionPoint > SIM_PositionPoints
void SIM_API SIMreadVelocityPointsFromAttribute(SIM_VelocityPoints &velocity_points, const SIM_MergeMap &merged_points, const std::vector< UT_DMatrix4 > &transforms, const UT_Array< const GU_Detail * > &gdps)
bool SIM_API SIMgetGeometryAndTransform(const SIM_Geometry *&geometry, UT_DMatrix4 &geometry_to_sim, const SIM_Object &object)
void SIM_API SIMcreatePreviousPositionAttribute(GU_Detail *gdp, GA_RWPageHandleV3 &prevpos_ph, GA_RWPageHandleV3 &pos_ph)
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
std::vector< SIM_VelocityPoint > SIM_VelocityPoints
GA_Index getOriginalPrimIndex(GA_Index i) const
fpreal SIM_API SIMreadFrictionParameterObject(const SIM_Object &object, const fpreal default_friction)
This implements a SIM_Geometry that copies the source geometry.