00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _SO_COLLISION_MANAGER_
00026 #define _SO_COLLISION_MANAGER_
00027
00028
00029
00030
00031 #include <Inventor/SbBasic.h>
00032 #include <Inventor/SbLinear.h>
00033
00034
00035
00036
00037 class SoNode;
00038 class SoGroup;
00039 class SoPath;
00040 class SoTransform;
00041 class SoPrimitiveVertex;
00042 class SoSensor;
00043 class SoDragger;
00044 class SoNodeSensor;
00045 class SoFieldSensor;
00046 class SoCallbackAction;
00047
00048
00049
00050
00054 typedef SbBool SoFilterCollisionCB (void* userData,
00055 const SoPath* p1,
00056 const SoPath* p2);
00060 typedef void SoBBoxCollisionCB (void* userData,
00061 const SoPath* p1,
00062 const SoPath* p2);
00063
00064 struct SoCollidingPrimitive {
00066 enum PrimitiveType {
00068 SEGMENT = 2,
00070 LINE_SEGMENT = 2,
00072 TRIANGLE = 3
00073 };
00077 SoPath* path;
00081 PrimitiveType type;
00085 SbVec3f vertex [3];
00086 };
00087
00088
00130 class SoCollisionManager
00131 {
00132
00133 public :
00134
00135
00137 enum Resp {
00139 NEXT_PRIMITIVE,
00141 NEXT_SHAPE,
00143 ABORT
00144 };
00145
00150 typedef Resp SoCollisionCB (void* userData,
00151 const SoCollidingPrimitive* object,
00152 const SoCollidingPrimitive* scene);
00153
00159 SoCollisionManager(SoPath* object,
00160 SoGroup* scene,
00161 SoTransform* transform);
00165 ~SoCollisionManager();
00166
00170 void setObject (SoPath* object);
00171
00175 SoPath* getObject () const;
00176
00180 void setScene (SoGroup* scene);
00181
00185 SoGroup* getScene () const;
00186
00190 void setTransform (SoTransform* transform);
00191
00195 SoTransform* getTransform () const;
00196
00204 void setGluingLevel (unsigned int level);
00205
00209 unsigned int getGluingLevel() const;
00210
00215 void setObjectBBoxOnly (SbBool flag = TRUE);
00216
00221 SbBool isObjectBBoxOnly () const;
00222
00228 void setSceneBBoxOnly (SbBool flag = TRUE);
00229
00234 SbBool isSceneBBoxOnly () const;
00235
00243 void setGluing (SbBool flag = TRUE);
00244
00250 SbBool isGluing () const;
00251
00257 void activate (SbBool flag = TRUE);
00258
00262 SbBool isActivated () const;
00263
00270 void setFilterCallback (SoFilterCollisionCB* f, void* userData = NULL);
00271
00280 void addBBoxCollisionCallback (SoBBoxCollisionCB* f, void* userData = NULL);
00281
00286 void removeBBoxCollisionCallback (SoBBoxCollisionCB* f,
00287 void* userData = NULL);
00301 void addCollisionCallback (SoCollisionCB* f, void* userData = NULL);
00302
00307 void removeCollisionCallback(SoCollisionCB* f, void* userData = NULL);
00308
00309
00311 enum TransformChangeType {
00313 NONE,
00315 TRANSLATION,
00317 SCALE,
00319 ROTATION,
00321 ORIENTATION,
00323 CENTER
00324 };
00325
00327 enum Axis {
00329 X_AXIS = 1,
00331 Y_AXIS = 2,
00333 Z_AXIS = 4
00334 };
00335
00337 enum Position {
00339 BEGIN,
00341 END
00342 };
00343
00344 #ifndef HIDDEN_FROM_DOC
00345
00346 struct ShapeBBoxInformationItem {
00347 SoPath* path;
00348 float x_min, y_min, z_min;
00349 float x_max, y_max, z_max;
00350 };
00351
00352 struct PairTableItem {
00353 int overlap_flags;
00354 float x_min, y_min, z_min;
00355 float x_max, y_max, z_max;
00356 };
00357
00358 struct CompListItem
00359 {
00360 int shape_index;
00361 Position position;
00362 SbBool to_remove;
00363 float x, y, z;
00364 };
00365
00366 struct ManagerListItem {
00367 SoCollisionManager* manager;
00368 ManagerListItem* next;
00369 };
00370
00371 class SceneInformationItem {
00372 public:
00373 ~SceneInformationItem();
00374
00375 SoGroup* root;
00376 SoNodeSensor* sensor;
00377 SbBool need_refresh;
00378 SbBool one_object_has_changed;
00379
00380 int refs;
00381 ManagerListItem* manager_list;
00382 int active_manager_count;
00383
00384 int shape_count;
00385 int shape_bbox_information_reserved_size;
00386 ShapeBBoxInformationItem* shape_bbox_information;
00387
00388 int pair_table_reserved_size;
00389 PairTableItem* pair_table;
00390
00391 int comp_list_size;
00392 int comp_list_reserved_size;
00393 CompListItem* comp_list;
00394
00395
00396 SceneInformationItem* next;
00397 };
00398 #endif // HIDDEN_FROM_DOC
00399
00400 private :
00401
00402
00403 SoPath* m_object;
00404 SoGroup* m_scene;
00405 SoTransform* m_transform;
00406 int m_gluing_level;
00407
00408
00409 SbBool m_object_bbox_only;
00410 SbBool m_scene_bbox_only;
00411 SbBool m_gluing;
00412 SbBool m_activated;
00413
00414
00415 SoFilterCollisionCB* m_filter_cb;
00416 void* m_filter_cb_ud;
00417 void* m_bbox_cb_list;
00418 void* m_collision_cb_list;
00419 void invokeBBoxCallbacks (const SoPath* p1, const SoPath* p2);
00420 Resp invokeCollisionCallbacks (const SoCollidingPrimitive* object,
00421 const SoCollidingPrimitive* scene);
00422
00423
00424 SbBool m_need_check;
00425 SbBool m_object_geometry_has_changed;
00426 int m_primitive_count;
00427 int m_num_primitive_reserved;
00428 SoCollidingPrimitive* m_primitives;
00429 SoPath* m_current_shape_path;
00430 Resp m_upper_response;
00431 SbBool m_new_moving;
00432 SbBool m_new_shapes_pair;
00433 static SceneInformationItem* m_scene_information_list;
00434 SceneInformationItem* m_scene_information;
00435 SbBool m_is_a_good_transform;
00436 SbBool m_there_is_a_good_transform;
00437 SbBool m_need_gluing;
00438 SoTransform* m_last_know_good_transform;
00439 SoTransform* m_temporary_transform;
00440 SoFieldSensor* m_translation_sensor;
00441 SoFieldSensor* m_scale_sensor;
00442 SoFieldSensor* m_rotation_sensor;
00443 SoFieldSensor* m_orientation_sensor;
00444 SoFieldSensor* m_center_sensor;
00445
00446
00447 PairTableItem* pairTableGetItem (int shape1, int shape2);
00448 void pairTableCompute (int (*fct)(const void*, const void*), Axis axis);
00449 static int compListXCompare(const void* v1, const void* v2);
00450 static int compListYCompare(const void* v1, const void* v2);
00451 static int compListZCompare(const void* v1, const void* v2);
00452 void refresh ();
00453 void addPrimitiveToTable (SoPath* p,
00454 SoCollidingPrimitive::PrimitiveType type,
00455 SbVec3f v1,
00456 SbVec3f v2,
00457 SbVec3f v3);
00458 void addPrimitiveToTable (SoPath* p,
00459 SoCollidingPrimitive::PrimitiveType type,
00460 SbVec3f v1,
00461 SbVec3f v2)
00462 { addPrimitiveToTable (p, type, v1, v2, SbVec3f (0, 0, 0)); }
00463 void addTriangleToTable (SoCallbackAction* action,
00464 const SoPrimitiveVertex* v1,
00465 const SoPrimitiveVertex* v2,
00466 const SoPrimitiveVertex* v3);
00467 void addSegmentToTable (SoCallbackAction* action,
00468 const SoPrimitiveVertex* v1,
00469 const SoPrimitiveVertex* v2);
00470 void onObjectTriangle (SoCallbackAction* action,
00471 const SoPrimitiveVertex* v1,
00472 const SoPrimitiveVertex* v2,
00473 const SoPrimitiveVertex* v3);
00474 void onObjectTriangleReal (const SbVec3f v0,
00475 const SbVec3f v1,
00476 const SbVec3f v2);
00477 void onObjectSegment (SoCallbackAction* action,
00478 const SoPrimitiveVertex* v1,
00479 const SoPrimitiveVertex* v2);
00480 SbBool intersectionTwoTriangles (SoCollidingPrimitive* t1,
00481 SoCollidingPrimitive* t2);
00482
00483
00484 SbBool onTransformChange(SoSensor *sensor, TransformChangeType type);
00485 SbBool onTransformChange(SbBool sensorIsNotNULL, TransformChangeType type);
00486 SbBool checkSensor (SoSensor* sensor);
00487 static SceneInformationItem* getSceneInformation(SoCollisionManager* manager);
00488 static void removeSceneInformation (SoCollisionManager* manager);
00489 static void onSceneChange (void* ud, SoSensor* sensor);
00490 static void refresh (SceneInformationItem* scene);
00491 void lastKnowGoodTransformComputing ();
00492
00493
00494 static void addTriangleToTableCB (void* ud, SoCallbackAction* action,
00495 const SoPrimitiveVertex* v1,
00496 const SoPrimitiveVertex* v2,
00497 const SoPrimitiveVertex* v3)
00498 { ((SoCollisionManager*) ud)->addTriangleToTable (action, v1, v2, v3); }
00499 static void addSegmentToTableCB (void* ud, SoCallbackAction* action,
00500 const SoPrimitiveVertex* v1,
00501 const SoPrimitiveVertex* v2)
00502 { ((SoCollisionManager*) ud)->addSegmentToTable (action, v1, v2); }
00503
00504 static void onObjectTriangleCB (void* ud, SoCallbackAction* action,
00505 const SoPrimitiveVertex* v1,
00506 const SoPrimitiveVertex* v2,
00507 const SoPrimitiveVertex* v3)
00508 { ((SoCollisionManager*) ud)->onObjectTriangle (action, v1, v2, v3); }
00509 static void onObjectSegmentCB (void* ud, SoCallbackAction* action,
00510 const SoPrimitiveVertex* v1,
00511 const SoPrimitiveVertex* v2)
00512 { ((SoCollisionManager*) ud)->onObjectSegment (action, v1, v2); }
00513 static void onTranslationChangeCB (void* ud, SoSensor* sensor)
00514 { ((SoCollisionManager*) ud)->onTransformChange (sensor, TRANSLATION); }
00515 static void onScaleChangeCB (void* ud, SoSensor* sensor)
00516 { ((SoCollisionManager*) ud)->onTransformChange (sensor, SCALE); }
00517 static void onRotationChangeCB (void* ud, SoSensor* sensor)
00518 { ((SoCollisionManager*) ud)->onTransformChange (sensor, ROTATION); }
00519 static void onOrientationChangeCB (void* ud, SoSensor* sensor)
00520 { ((SoCollisionManager*) ud)->onTransformChange (sensor, ORIENTATION); }
00521 static void onCenterChangeCB (void* ud, SoSensor* sensor)
00522 { ((SoCollisionManager*) ud)->onTransformChange (sensor, CENTER); }
00523 static void onDraggerMotionCB (void* ud, SoDragger *)
00524 { ((SoCollisionManager*) ud)->onDraggerMotion (); }
00525
00526
00527 SbVec3f m_dragger_translation;
00528 SbRotation m_dragger_rotation;
00529 SbVec3f m_dragger_scale_factor;
00530 SbRotation m_dragger_scale_orientation;
00531 SbVec3f m_dragger_center;
00532 void onDraggerMotion ();
00533
00534
00535 void quickSort(void *array, int nElm, int elmSize,
00536 int (*compare)(const void*, const void*),
00537 char *tmp);
00538
00539 };
00540
00541 #endif //_SO_COLLISION_MANAGER_
00542
00543