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 
00026 #ifndef _SO_EXTENDED_SELECTION_H_
00027 #define _SO_EXTENDED_SELECTION_H_
00028 
00029 
00030 
00031 
00032 #include <Inventor/actions/SoCallbackAction.h>
00033 #include <Inventor/nodes/SoSubNode.h>
00034 #include <Inventor/nodes/SoSelection.h>
00035 #include <Inventor/nodes/SoColorIndex.h>
00036 #include <Inventor/nodes/SoDrawStyle.h>
00037 #include <Inventor/nodes/SoMatrixTransform.h>
00038 #include <Inventor/SbViewportRegion.h>
00039 #include <Inventor/STL/vector>
00040 #include <Inventor/misc/SoRef.h>
00041 #include <Inventor/SbEventArg.h>
00042 #include <Inventor/SbEventHandler.h>
00043 
00044 
00045 
00046 class SoCamera;
00047 class SoSeparator;
00048 class SoCoordinate3;
00049 class SbViewportRegion;
00050 class SoMaterial;
00051 class SoPath;
00052 class LassoRenderer;
00053 class SoOrthographicCamera;
00054 class LassoPointsWrapper;
00055 class MatricesEvaluator;
00056 class SbPlanarConvexPolygon;
00057 class SimplexSelectorHelper;
00058 
00059 class SoExtSelectionImpl;
00060 
00182 class  SoExtSelection : public SoSelection {
00183   
00184   SO_NODE_HEADER(SoExtSelection);
00185   
00186   
00187  public:
00188 
00190   enum LassoType {
00192     NOLASSO,
00194     LASSO,
00196     RECTANGLE,
00198     ELLIPSE
00199   };  
00200 
00202   enum LassoPolicy {
00204     FULL_BBOX,
00206     PART_BBOX,
00208     FULL,
00210     PART
00211   };  
00212   
00215   enum LassoMode {
00221     VISIBLE_SHAPES,
00222 
00224     ALL_SHAPES
00225   };  
00226 
00231   SoSFEnum lassoType;
00232 
00237   SoSFEnum lassoPolicy;
00238 
00263   SoSFEnum lassoMode;
00264 
00266   enum ReturnType {
00271     NOSELECT_CONTINUE,
00276     SELECT_RETURN,
00280     SELECT_CONTINUE,
00284     NOSELECT_RETURN
00285   }; 
00286   
00290   SoExtSelection();
00291 
00292 #if 1             SoDEPRECATED
00299   void useOverlay(SbBool flg = TRUE)
00300     { m_use_overlay = flg; }
00301 SoDEPRECATED
00306   SbBool isUsingOverlay()
00307     { return m_use_overlay; }
00308 #endif 
00317   void useFastEditing(SbBool flg = TRUE);
00318 
00322   SbBool isUsingFastEditing()
00323     { return m_useFastEditing; }
00324 
00330   SoSeparator *getOverlaySceneGraph()
00331     { return m_lasso_root; }
00332 
00337   void setOverlayLassoColorIndex(int index)
00338     { m_overlay_lasso_color->index.setValue(index); }
00339 
00344   int getOverlayLassoColorIndex()
00345     { return m_overlay_lasso_color->index[0]; }
00346 
00352   void setLassoColor(SbColor c);
00353 
00358   SbColor getLassoColor()
00359     { return m_lasso_color; }
00360 
00365   void setLassoWidth(float width)
00366     { m_lasso_draw_style->lineWidth.setValue(width); }
00367 
00371   float getLassoWidth()
00372     { return m_lasso_draw_style->lineWidth.getValue(); }
00373   
00380   void setOverlayLassoPattern(unsigned short pattern);
00381 
00386   unsigned short getOverlayLassoPattern()
00387     { return(unsigned short) m_lasso_pattern; }
00388 
00393   void animateOverlayLasso(SbBool flg = TRUE);
00394 
00398   SbBool isOverlayLassoAnimated()
00399     { return m_lasso_animation; }
00400 
00423   void select(SoNode *root, int numCoord, SbVec2f *lassoCoord,  
00424               const SbViewportRegion &viewport, SbBool shiftPressed);
00425 
00426 #if 1             SoDEPRECATED
00432   void select(SoNode *root, int numCoord, SbVec3f *lassoCoord,
00433               const SbViewportRegion &viewport, SbBool shiftPressed);
00434 
00435 #endif 
00441   typedef SbBool SoExtSelectionTriangleCB(void *userData,
00442                                           SoCallbackAction *action,
00443                                           const SoPrimitiveVertex *v1, 
00444                                           const SoPrimitiveVertex *v2, 
00445                                           const SoPrimitiveVertex *v3);
00450   typedef SbBool SoExtSelectionLineSegmentCB(void *userData,
00451                                              SoCallbackAction *action,
00452                                              const SoPrimitiveVertex *v1, 
00453                                              const SoPrimitiveVertex *v2);
00458   typedef SbBool SoExtSelectionPointCB(void *userData,
00459                                        SoCallbackAction *action,
00460                                        const SoPrimitiveVertex *v1); 
00461 
00478    typedef SbBool SelectionEventCallback( const SbVec2s& position, const SbVec3f& scenePosition, 
00479                                           SoExtSelection* selection, void* userData );
00480 
00487   virtual void addChild( SoNode *child );
00488 
00494   struct PreFilterEventArg : public SbEventArg {
00495   public:
00496 
00498     PreFilterEventArg(SoPath* path, SoExtSelection* source)
00499       :m_path(path), m_source(source), m_isSkipped(false)
00500     {}
00501 
00503     ~PreFilterEventArg() { }
00504 
00506     SoExtSelection* getSource() const { return m_source; }
00507 
00510     const SoNode* getNode() const { return m_path->getTail(); }
00511 
00513     const SoPath* getPath() const { return m_path; }
00514 
00516     void skipNode()  { m_isSkipped = true;  }
00517 
00519     SbBool isNodeSkipped() const { return m_isSkipped;  }
00520 
00521   private:
00522 
00523     SoPath* m_path;
00524     SoExtSelection* m_source;
00525     SbBool m_isSkipped;
00526   };
00527 
00534    SbEventHandler<PreFilterEventArg&> onPreFilter;
00535 
00553   virtual void setSelectionSceneGraph( SoNode* sceneGraph );
00554 
00560   virtual SoNode* getSelectionSceneGraph() const;
00561 
00567   virtual void setStartSelectionCallback( SelectionEventCallback* callback, void* userData = NULL );
00568 
00574   virtual void setEndSelectionCallback( SelectionEventCallback* callback, void* userData = NULL );
00575 
00582   virtual void setMovingSelectionCallback( SelectionEventCallback* callback, void* userData = NULL );
00583 
00609   void setTriangleFilterCallback(SoExtSelectionTriangleCB *cb, 
00610                                  void *userData = NULL);
00611 
00616   void setLineSegmentFilterCallback(SoExtSelectionLineSegmentCB *cb, 
00617                                     void *userData = NULL);
00622   void setPointFilterCallback(SoExtSelectionPointCB *cb, 
00623                               void *userData = NULL);
00624 
00630   const SbVec2s *getLassoCoordsDC(int &numCoords);
00631 
00637   const SbVec3f *getLassoCoordsWC(int &numCoords);
00638 
00642   const SoPathList &getSelectionPathList() const
00643     { return tempSelectionList;}
00644 
00645   
00646   
00647 
00648  private:
00649   
00650   
00651   virtual void handleEvent(SoHandleEventAction *action);
00652   virtual void search(SoSearchAction *action);
00653 
00654  private:
00655   static void           initClass();
00656   static void           exitClass();
00657 
00658   void setSelection( SbBool on ) { m_makeSelection = on; }
00659   SbBool getSelection() const { return m_makeSelection; }
00660 
00661   
00662  private:
00663 
00664   
00665   SoPathList tempSelectionList;
00666   
00667   SoExtSelectionTriangleCB *triangleCBFunc;
00668   SoExtSelectionLineSegmentCB *lineSegmentCBFunc;
00669   SoExtSelectionPointCB *pointCBFunc;
00670   void *triangleCBData;
00671   void *lineSegmentCBData;
00672   void *pointCBData;
00673 
00674   
00675   
00676   
00677   virtual ~SoExtSelection();
00678 
00682   void manageEventCallbacks( SoHandleEventAction *action );
00683 
00687   void handleSelectionEvent(SoHandleEventAction *action);
00688 
00689   
00690  private:
00691 
00692   enum ShapeType {
00693     TRIANGLE_SHAPE,
00694     LINE_SHAPE,
00695     POINT_SHAPE
00696   };
00697 
00698   typedef void (SoExtSelection::*onTriangleMethod)(SoCallbackAction *action,
00699                                                    const SoPrimitiveVertex *v1,
00700                                                    const SoPrimitiveVertex *v2,
00701                                                    const SoPrimitiveVertex *v3);
00702 
00703   typedef void (SoExtSelection::*onLineMethod)( SoCallbackAction *action,
00704                                                 const SoPrimitiveVertex *v1,
00705                                                 const SoPrimitiveVertex *v2);
00706 
00707   typedef void (SoExtSelection::*onPointMethod)(SoCallbackAction *action,
00708                                                 const SoPrimitiveVertex *v1 );
00709 
00710   typedef SoCallbackAction::Response (SoExtSelection::*onBboxMethod)(SoCallbackAction *action,
00711                                                                      const SbBox3f& bb );
00712 
00713   typedef void (SoExtSelection::*doSelectionMethod)( SimplexSelectorHelper* );
00714 
00715   
00716 
00717   
00718   SoTimerSensor *m_lasso_animation_timer;
00719   SoSeparator *m_lasso_root;
00720   SoColorIndex *m_overlay_lasso_color;
00721   SoDrawStyle *m_lasso_draw_style;
00722   LassoPointsWrapper *m_lasso_points;
00723   SoOrthographicCamera* m_lasso_camera;
00724 
00725   int m_lasso_window_height, m_max_lasso_points;
00726   SbBool m_new_lasso;
00727 
00728   
00729   SbBool m_use_overlay, m_lasso_animation;
00730   SbBool m_useFastEditing;
00731   unsigned long m_lasso_pattern, m_lasso_work_pattern;
00732 
00733   SbColor m_lasso_color;
00734   
00735   
00736   SbBool m_shift_down;
00737   SbBool m_end_triangle_search, m_end_line_search, m_end_point_search;
00738   SbBool m_hasBeenOut;
00739   SbBool m_button_was_pressed;
00740   
00741   
00742   SoPath *m_current_shape;
00743   SoState *m_current_state;
00744   SbViewportRegion m_current_viewport, m_current_local_viewport;
00745 
00746   ShapeType m_currentShapeType;
00747   SbVec3f  m_lassoFirstPoint;
00748   LassoRenderer *m_current_scene_offscreen;
00749   unsigned int m_shape_index;
00750   std::vector<SoRef<SoPath> > m_selectedShapes;
00751 
00752   MatricesEvaluator *m_matricesEvaluator;
00753 
00754   SoGetBoundingBoxAction *m_bbox_action;
00755   SoSeparator *m_bbox_root;
00756   SoMatrixTransform *m_bbox_trans;
00757   SbBool m_do_selection;
00758   
00759   SbViewportRegion vpRegionEvent;
00760 
00761   SbBool m_makeSelection; 
00762 
00763   SoMaterial* m_lassoMaterial;
00764 
00765   SoPickStyle* m_lassoPickStyle;
00766 
00767   SoSeparator* m_defaultSelectionSceneGraph;
00768 
00769   SoSeparator* m_customSceneGraphSeparator;
00770 
00771   SelectionEventCallback* m_startSelectionCallback;
00772   void* m_startSelectionCallbackUserData;
00773 
00774   SelectionEventCallback* m_endSelectionCallback;
00775   void* m_endSelectionCallbackUserData;
00776 
00777   SelectionEventCallback* m_movingSelectionCallback;
00778   void* m_movingSelectionCallbackUserData;
00779 
00780   bool m_isSelecting;
00781 
00782   const SoCamera* m_activeCamera;
00783 
00784   SoExtSelectionImpl* m_impl;
00785 
00786   
00787   
00788   bool m_shouldCheckPathForSelection;
00789 
00790   onTriangleMethod   m_currentTriangleMethod;
00791   onLineMethod       m_currentLineMethod;
00792   onPointMethod      m_currentPointMethod;
00793   onBboxMethod       m_currBboxMethod;
00794   doSelectionMethod  m_currDoSelection;
00795 
00796   
00797   
00798   
00799   static bool s_onlySelectInViewVol;
00800 
00801   
00802   
00803   
00804   void transformToLocalViewport(float &bbox_x_min, float &bbox_y_min, 
00805                                 float &bbox_x_max, float &bbox_y_max);
00806   SbVec3f getVec3fPosition(const SbVec2f &pos, 
00807                            const SbViewportRegion &vp);
00808   SbVec3f getVec3fMousePosition(const SoEvent *event, 
00809                                 const SbViewportRegion &vp);
00810   void vec3fToPixel(const SbVec3f &vec, 
00811                     const SbViewportRegion &vp, 
00812                     long &x, long &y);
00813   int isSquareInsideLasso(const SbVec4f &p0, const SbVec4f &p1,
00814                           const SbVec4f &p2, const SbVec4f &p3);
00815 
00816   SbBool isLassoPtsHalfPlaneLine(const SbVec4f &line0, const SbVec4f &line1) const;
00817 
00818   
00819   void selectObjects(SoNode *root, bool useViewport = false);
00820   void doSelection( SimplexSelectorHelper* ssh );
00821   void doSelection_( SimplexSelectorHelper* ssh );
00822   void computeEllipsePoints(const SbVec3f &start, const SbVec3f &end);
00823   SoCallbackAction::Response windBBoxCheck( SoState *state, const SbXfBox3f &tmpbox ) const;
00824 
00825   
00826   void onLassoAnimation();
00827   static void onLassoAnimationCB(void *data, SoSensor *);
00828 
00829   
00830   void initLasso(int x, int y);
00831   void drawLasso(int x, int y);
00832   void eraseLasso();
00833 
00834   bool setStartSelecting( const SbVec2s &pixel, SbVec3f scenePos );
00835   bool setEndSelecting( const SbVec2s &pixel, SbVec3f scenePos );
00836   bool notifyWhileSelecting( const SbVec2s &pixel, SbVec3f scenePos );
00837 
00838   void setMouseButtonDown( bool value );
00839   
00840   enum TripletOrientation
00841   {
00842     COLINEAR,
00843     CLOCKWISE,
00844     COUNTERCLOCKWISE
00845   };
00846 
00848   static bool isPointOnSegment( const SbVec2f& p, const SbVec2f& s1, const SbVec2f& s2 );
00849 
00851   static TripletOrientation computeOrientation( const SbVec2f& p1, const SbVec2f& p2, const SbVec2f& p3 );
00852 
00854   static bool segmentsIntersection( const SbVec2f& s00, const SbVec2f& s01, const SbVec2f& s10, const SbVec2f& s11 );
00855 
00856   
00857   SoCallbackAction::Response examineCamera(SoCallbackAction *action, const SoNode* node);
00858   SoCallbackAction::Response examineSeparatorPre(SoCallbackAction *action, const SoNode* node);
00859   SoCallbackAction::Response examineSeparatorPost(SoCallbackAction *action, const SoNode* node);
00860   SoCallbackAction::Response examineShape(SoCallbackAction *action, const SoNode* node);
00861   SoCallbackAction::Response examineClipPlane(SoCallbackAction *action, const SoNode* node);
00862   SoCallbackAction::Response postShape(SoCallbackAction *action);
00863   void examineTriangle_(SoCallbackAction *action,
00864                         const SoPrimitiveVertex *v1,
00865                         const SoPrimitiveVertex *v2,
00866                         const SoPrimitiveVertex *v3,
00867                         SimplexSelectorHelper* ssh = NULL);
00868   void examineLine_(SoCallbackAction *action,
00869                     const SoPrimitiveVertex *v1,
00870                     const SoPrimitiveVertex *v2,
00871                     SimplexSelectorHelper* ssh = NULL);
00872   void examinePoint_(SoCallbackAction *action,
00873                     const SoPrimitiveVertex *v1,
00874                     SimplexSelectorHelper* ssh = NULL);
00875   void examineTriangle(SoCallbackAction *action, 
00876                        const SoPrimitiveVertex *v1,
00877                        const SoPrimitiveVertex *v2,
00878                        const SoPrimitiveVertex *v3);
00879   void examineLine(SoCallbackAction *action, 
00880                    const SoPrimitiveVertex *v1,
00881                    const SoPrimitiveVertex *v2);
00882   void examinePoint(SoCallbackAction *action, 
00883                     const SoPrimitiveVertex *v1);
00884   void examineTriangleWClipPlanes(SoCallbackAction *action,
00885                                   const SoPrimitiveVertex *v1,
00886                                   const SoPrimitiveVertex *v2,
00887                                   const SoPrimitiveVertex *v3);
00888   void examineLineWClipPlanes(SoCallbackAction *action,
00889                               const SoPrimitiveVertex *v1,
00890                               const SoPrimitiveVertex *v2);
00891   void examinePointWClipPlanes(SoCallbackAction *action,
00892                                const SoPrimitiveVertex *v1);
00893   SoCallbackAction::Response 
00894     onBBox(SoCallbackAction *action, const SoNode *node);
00895   SoCallbackAction::Response
00896     onBBoxWClipPlanes(SoCallbackAction *action, const SbBox3f& bb);
00897   SoCallbackAction::Response
00898     onBBox_( SoCallbackAction *action, const SbBox3f& bb );
00899   SoCallbackAction::Response 
00900     onAnnoText3(SoCallbackAction *action, const SoNode *node);
00901   SoCallbackAction::Response 
00902     onShape(SoCallbackAction *action, const SoNode *node);
00903   SoCallbackAction::Response 
00904     onBaseKitPre(SoCallbackAction *action, const SoNode *node);
00905 
00906   void onTriangle_(SoCallbackAction *action,
00907                    const SbVec3f &p1,
00908                    const SbVec3f &p2,
00909                    const SbVec3f &p3);
00910   void onLineSegment_(SoCallbackAction *action,
00911                       const SbVec3f &p1,
00912                       const SbVec3f &p2);
00913   void onPoint_(SoCallbackAction *action,
00914                 const SbVec3f &p1);
00915   void onTriangle(SoCallbackAction *action,
00916                   const SoPrimitiveVertex *v1,
00917                   const SoPrimitiveVertex *v2,
00918                   const SoPrimitiveVertex *v3);
00919   void onLineSegment(SoCallbackAction *action, 
00920                      const SoPrimitiveVertex *v1,
00921                      const SoPrimitiveVertex *v2);
00922   void onPoint(SoCallbackAction *action,
00923                const SoPrimitiveVertex *v1);
00924   void onTriangleWClipPlanes(SoCallbackAction *action,
00925                              const SoPrimitiveVertex *v1,
00926                              const SoPrimitiveVertex *v2,
00927                              const SoPrimitiveVertex *v3);
00928   void onLineSegmentWClipPlanes(SoCallbackAction *action,
00929                                 const SoPrimitiveVertex *v1,
00930                                 const SoPrimitiveVertex *v2);
00931   void onPointWClipPlanes(SoCallbackAction *action,
00932                           const SoPrimitiveVertex *v1);
00933 
00934   static SoCallbackAction::Response 
00935     examineCameraCB(void *data, SoCallbackAction *action, const SoNode *);
00936   static SoCallbackAction::Response 
00937     examineSeparatorPostCB(void *data, SoCallbackAction *action, const SoNode *);
00938   static SoCallbackAction::Response
00939     examineSeparatorPreCB(void *data, SoCallbackAction *action, const SoNode *);
00940   static SoCallbackAction::Response 
00941     examineShapeCB(void *data, SoCallbackAction *action, const SoNode *);
00942   static SoCallbackAction::Response 
00943     postShapeCB(void *data, SoCallbackAction *action, const SoNode *);
00944   static void onTriangleCB(void *object, 
00945                            SoCallbackAction *action,
00946                            SoPrimitiveVertex *v1,
00947                            SoPrimitiveVertex *v2, 
00948                            SoPrimitiveVertex *v3);
00949   static void onLineSegmentCB(void *object, SoCallbackAction *action,
00950                               SoPrimitiveVertex *v1,
00951                               SoPrimitiveVertex *v2);
00952 
00953   static void onPointCB(void *object, 
00954                         SoCallbackAction *action, 
00955                         SoPrimitiveVertex *v1);
00956   static void onText2CB(void *object, 
00957                         SoCallbackAction *action, 
00958                         const SoNode *node);
00959   static void onAnnoText3CB(void *object, 
00960                             SoCallbackAction *action, 
00961                             const SoNode *node);
00962   static void onImageCB(void *object, 
00963                         SoCallbackAction *action, 
00964                         const SoNode *node);
00965   static SoCallbackAction::Response 
00966     onShapeCB(void *object, SoCallbackAction *action, const SoNode *node);
00967   static SoCallbackAction::Response 
00968     onBaseKitPreCB(void *object, SoCallbackAction *action, const SoNode *node);
00969 
00970   static SoCallbackAction::Response
00971     onClipPlaneCB(void *object, SoCallbackAction *action, const SoNode * node);
00972 
00973   bool examineTrimmedTriangle( const SoPrimitiveVertex *v1, const SoPrimitiveVertex *v2, const SoPrimitiveVertex *v3, 
00974                                const SbPlanarConvexPolygon& convexHull, SoCallbackAction* action, bool nested = false );
00975   bool examineTrimmedSegment( const SoPrimitiveVertex *v1, const SoPrimitiveVertex *v2,
00976                               const SbPlanarConvexPolygon& convexHull, SoCallbackAction* action );
00977   bool examineTrimmedPoint( const SoPrimitiveVertex *v1,
00978                             const SbPlanarConvexPolygon& convexHull, SoCallbackAction* action );
00979 
00980   void onTrimmedGeom( SoCallbackAction* action, const SbPlanarConvexPolygon& convexHull );
00981 
00982   bool areCallbackFuncsSet() const;
00983 
00984   
00985   
00986   bool isPointBehindNearPlane( const SbVec4f& pointCoordinates ) const;
00987 
00988   friend class SimplexSelectorHelper;
00989 
00990 };
00991 
00992 #endif  // _SO_EXTENDED_SELECTION_H_
00993 
00994