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
00068 struct SoCollidingPrimitive {
00070 enum PrimitiveType {
00072 SEGMENT = 2,
00074 LINE_SEGMENT = 2,
00076 TRIANGLE = 3
00077 };
00081 SoPath* path;
00085 PrimitiveType type;
00089 SbVec3f vertex [3];
00090 };
00091
00092
00133 class SoCollisionManager
00134 {
00135
00136 public :
00137
00138
00140 enum Resp {
00142 NEXT_PRIMITIVE,
00144 NEXT_SHAPE,
00146 ABORT
00147 };
00148
00153 typedef Resp SoCollisionCB (void* userData,
00154 const SoCollidingPrimitive* object,
00155 const SoCollidingPrimitive* scene);
00156
00162 SoCollisionManager(SoPath* object,
00163 SoGroup* scene,
00164 SoTransform* transform);
00168 ~SoCollisionManager();
00169
00173 void setObject (SoPath* object);
00174
00178 SoPath* getObject () const;
00179
00183 void setScene (SoGroup* scene);
00184
00188 SoGroup* getScene () const;
00189
00193 void setTransform (SoTransform* transform);
00194
00198 SoTransform* getTransform () const;
00199
00207 void setGluingLevel (unsigned int level);
00208
00212 unsigned int getGluingLevel() const;
00213
00218 void setObjectBBoxOnly (SbBool flag = TRUE);
00219
00224 SbBool isObjectBBoxOnly () const;
00225
00231 void setSceneBBoxOnly (SbBool flag = TRUE);
00232
00237 SbBool isSceneBBoxOnly () const;
00238
00246 void setGluing (SbBool flag = TRUE);
00247
00253 SbBool isGluing () const;
00254
00260 void activate (SbBool flag = TRUE);
00261
00265 SbBool isActivated () const;
00266
00273 void setFilterCallback (SoFilterCollisionCB* f, void* userData = NULL);
00274
00283 void addBBoxCollisionCallback (SoBBoxCollisionCB* f, void* userData = NULL);
00284
00289 void removeBBoxCollisionCallback (SoBBoxCollisionCB* f,
00290 void* userData = NULL);
00304 void addCollisionCallback (SoCollisionCB* f, void* userData = NULL);
00305
00310 void removeCollisionCallback(SoCollisionCB* f, void* userData = NULL);
00311
00312
00314 enum TransformChangeType {
00316 NONE,
00318 TRANSLATION,
00320 SCALE,
00322 ROTATION,
00324 ORIENTATION,
00326 CENTER
00327 };
00328
00330 enum Axis {
00332 X_AXIS = 1,
00334 Y_AXIS = 2,
00336 Z_AXIS = 4
00337 };
00338
00340 enum Position {
00342 BEGIN,
00344 END
00345 };
00346
00347 #ifndef HIDDEN_FROM_DOC
00348
00349 struct ShapeBBoxInformationItem {
00350 SoPath* path;
00351 float x_min, y_min, z_min;
00352 float x_max, y_max, z_max;
00353 };
00354
00355 struct PairTableItem {
00356 int overlap_flags;
00357 float x_min, y_min, z_min;
00358 float x_max, y_max, z_max;
00359 };
00360
00361 struct CompListItem
00362 {
00363 int shape_index;
00364 Position position;
00365 SbBool to_remove;
00366 float x, y, z;
00367 };
00368
00369 struct ManagerListItem {
00370 SoCollisionManager* manager;
00371 ManagerListItem* next;
00372 };
00373
00374 class SceneInformationItem {
00375 public:
00376 ~SceneInformationItem();
00377
00378 SoGroup* root;
00379 SoNodeSensor* sensor;
00380 SbBool need_refresh;
00381 SbBool one_object_has_changed;
00382
00383 int refs;
00384 ManagerListItem* manager_list;
00385 int active_manager_count;
00386
00387 int shape_count;
00388 int shape_bbox_information_reserved_size;
00389 ShapeBBoxInformationItem* shape_bbox_information;
00390
00391 int pair_table_reserved_size;
00392 PairTableItem* pair_table;
00393
00394 int comp_list_size;
00395 int comp_list_reserved_size;
00396 CompListItem* comp_list;
00397
00398
00399 SceneInformationItem* next;
00400 };
00401 #endif // HIDDEN_FROM_DOC
00402
00403 private :
00404
00405
00406 SoPath* m_object;
00407 SoGroup* m_scene;
00408 SoTransform* m_transform;
00409 int m_gluing_level;
00410
00411
00412 SbBool m_object_bbox_only;
00413 SbBool m_scene_bbox_only;
00414 SbBool m_gluing;
00415 SbBool m_activated;
00416
00417
00418 SoFilterCollisionCB* m_filter_cb;
00419 void* m_filter_cb_ud;
00420 void* m_bbox_cb_list;
00421 void* m_collision_cb_list;
00422 void invokeBBoxCallbacks (const SoPath* p1, const SoPath* p2);
00423 Resp invokeCollisionCallbacks (const SoCollidingPrimitive* object,
00424 const SoCollidingPrimitive* scene);
00425
00426
00427 SbBool m_need_check;
00428 SbBool m_object_geometry_has_changed;
00429 int m_primitive_count;
00430 int m_num_primitive_reserved;
00431 SoCollidingPrimitive* m_primitives;
00432 SoPath* m_current_shape_path;
00433 Resp m_upper_response;
00434 SbBool m_new_moving;
00435 SbBool m_new_shapes_pair;
00436 static SceneInformationItem* m_scene_information_list;
00437 SceneInformationItem* m_scene_information;
00438 SbBool m_is_a_good_transform;
00439 SbBool m_there_is_a_good_transform;
00440 SbBool m_need_gluing;
00441 SoTransform* m_last_know_good_transform;
00442 SoTransform* m_temporary_transform;
00443 SoFieldSensor* m_translation_sensor;
00444 SoFieldSensor* m_scale_sensor;
00445 SoFieldSensor* m_rotation_sensor;
00446 SoFieldSensor* m_orientation_sensor;
00447 SoFieldSensor* m_center_sensor;
00448
00449
00450 PairTableItem* pairTableGetItem (int shape1, int shape2);
00451 void pairTableCompute (int (*fct)(const void*, const void*), Axis axis);
00452 static int compListXCompare(const void* v1, const void* v2);
00453 static int compListYCompare(const void* v1, const void* v2);
00454 static int compListZCompare(const void* v1, const void* v2);
00455 void refresh ();
00456 void addPrimitiveToTable (SoPath* p,
00457 SoCollidingPrimitive::PrimitiveType type,
00458 SbVec3f v1,
00459 SbVec3f v2,
00460 SbVec3f v3);
00461 void addPrimitiveToTable (SoPath* p,
00462 SoCollidingPrimitive::PrimitiveType type,
00463 SbVec3f v1,
00464 SbVec3f v2)
00465 { addPrimitiveToTable (p, type, v1, v2, SbVec3f (0, 0, 0)); }
00466 void addTriangleToTable (SoCallbackAction* action,
00467 const SoPrimitiveVertex* v1,
00468 const SoPrimitiveVertex* v2,
00469 const SoPrimitiveVertex* v3);
00470 void addSegmentToTable (SoCallbackAction* action,
00471 const SoPrimitiveVertex* v1,
00472 const SoPrimitiveVertex* v2);
00473 void onObjectTriangle (SoCallbackAction* action,
00474 const SoPrimitiveVertex* v1,
00475 const SoPrimitiveVertex* v2,
00476 const SoPrimitiveVertex* v3);
00477 void onObjectTriangleReal (const SbVec3f v0,
00478 const SbVec3f v1,
00479 const SbVec3f v2);
00480 void onObjectSegment (SoCallbackAction* action,
00481 const SoPrimitiveVertex* v1,
00482 const SoPrimitiveVertex* v2);
00483 SbBool intersectionTwoTriangles (SoCollidingPrimitive* t1,
00484 SoCollidingPrimitive* t2);
00485
00486
00487 SbBool onTransformChange(SoSensor *sensor, TransformChangeType type);
00488 SbBool onTransformChange(SbBool sensorIsNotNULL, TransformChangeType type);
00489 SbBool checkSensor (SoSensor* sensor);
00490 static SceneInformationItem* getSceneInformation(SoCollisionManager* manager);
00491 static void removeSceneInformation (SoCollisionManager* manager);
00492 static void onSceneChange (void* ud, SoSensor* sensor);
00493 static void refresh (SceneInformationItem* scene);
00494 void lastKnowGoodTransformComputing ();
00495
00496
00497 static void addTriangleToTableCB (void* ud, SoCallbackAction* action,
00498 const SoPrimitiveVertex* v1,
00499 const SoPrimitiveVertex* v2,
00500 const SoPrimitiveVertex* v3)
00501 { ((SoCollisionManager*) ud)->addTriangleToTable (action, v1, v2, v3); }
00502 static void addSegmentToTableCB (void* ud, SoCallbackAction* action,
00503 const SoPrimitiveVertex* v1,
00504 const SoPrimitiveVertex* v2)
00505 { ((SoCollisionManager*) ud)->addSegmentToTable (action, v1, v2); }
00506
00507 static void onObjectTriangleCB (void* ud, SoCallbackAction* action,
00508 const SoPrimitiveVertex* v1,
00509 const SoPrimitiveVertex* v2,
00510 const SoPrimitiveVertex* v3)
00511 { ((SoCollisionManager*) ud)->onObjectTriangle (action, v1, v2, v3); }
00512 static void onObjectSegmentCB (void* ud, SoCallbackAction* action,
00513 const SoPrimitiveVertex* v1,
00514 const SoPrimitiveVertex* v2)
00515 { ((SoCollisionManager*) ud)->onObjectSegment (action, v1, v2); }
00516 static void onTranslationChangeCB (void* ud, SoSensor* sensor)
00517 { ((SoCollisionManager*) ud)->onTransformChange (sensor, TRANSLATION); }
00518 static void onScaleChangeCB (void* ud, SoSensor* sensor)
00519 { ((SoCollisionManager*) ud)->onTransformChange (sensor, SCALE); }
00520 static void onRotationChangeCB (void* ud, SoSensor* sensor)
00521 { ((SoCollisionManager*) ud)->onTransformChange (sensor, ROTATION); }
00522 static void onOrientationChangeCB (void* ud, SoSensor* sensor)
00523 { ((SoCollisionManager*) ud)->onTransformChange (sensor, ORIENTATION); }
00524 static void onCenterChangeCB (void* ud, SoSensor* sensor)
00525 { ((SoCollisionManager*) ud)->onTransformChange (sensor, CENTER); }
00526 static void onDraggerMotionCB (void* ud, SoDragger *)
00527 { ((SoCollisionManager*) ud)->onDraggerMotion (); }
00528
00529
00530 SbVec3f m_dragger_translation;
00531 SbRotation m_dragger_rotation;
00532 SbVec3f m_dragger_scale_factor;
00533 SbRotation m_dragger_scale_orientation;
00534 SbVec3f m_dragger_center;
00535 void onDraggerMotion ();
00536
00537
00538 void quickSort(void *array, int nElm, int elmSize,
00539 int (*compare)(const void*, const void*),
00540 char *tmp);
00541
00542 };
00543
00544 #endif //_SO_COLLISION_MANAGER_
00545
00546