Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:54

0001 // Created on: 1995-02-15
0002 // Created by: Roberc Coublanc
0003 // Copyright (c) 1995-1999 Matra Datavision
0004 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0005 //
0006 // This file is part of Open CASCADE Technology software library.
0007 //
0008 // This library is free software; you can redistribute it and/or modify it under
0009 // the terms of the GNU Lesser General Public License version 2.1 as published
0010 // by the Free Software Foundation, with special exception defined in the file
0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0012 // distribution for complete text of the license and disclaimer of any warranty.
0013 //
0014 // Alternatively, this file may be used under the terms of Open CASCADE
0015 // commercial license or contractual agreement.
0016 
0017 #ifndef _SelectMgr_ViewerSelector_HeaderFile
0018 #define _SelectMgr_ViewerSelector_HeaderFile
0019 
0020 #include <OSD_Chronometer.hxx>
0021 #include <SelectMgr_BVHThreadPool.hxx>
0022 #include <SelectMgr_IndexedDataMapOfOwnerCriterion.hxx>
0023 #include <SelectMgr_SelectingVolumeManager.hxx>
0024 #include <SelectMgr_Selection.hxx>
0025 #include <SelectMgr_SelectableObject.hxx>
0026 #include <SelectMgr_SelectableObjectSet.hxx>
0027 #include <SelectMgr_StateOfSelection.hxx>
0028 #include <SelectMgr_ToleranceMap.hxx>
0029 #include <SelectMgr_TypeOfDepthTolerance.hxx>
0030 #include <SelectMgr_ViewerSelector.hxx>
0031 #include <Standard_OStream.hxx>
0032 #include <Standard_Transient.hxx>
0033 #include <StdSelect_TypeOfSelectionImage.hxx>
0034 #include <TColStd_HArray1OfInteger.hxx>
0035 
0036 class SelectMgr_SensitiveEntitySet;
0037 class SelectMgr_EntityOwner;
0038 class Select3D_SensitiveEntity;
0039 class V3d_View;
0040 
0041 // resolve name collisions with X11 headers
0042 #ifdef Status
0043   #undef Status
0044 #endif
0045 typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_SensitiveEntitySet) > SelectMgr_MapOfObjectSensitives;
0046 typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_SensitiveEntitySet) >::Iterator SelectMgr_MapOfObjectSensitivesIterator;
0047 
0048 typedef NCollection_DataMap<Standard_Integer, SelectMgr_SelectingVolumeManager> SelectMgr_FrustumCache;
0049 
0050 //! A framework to define finding, sorting the sensitive
0051 //! primitives in a view. Services are also provided to
0052 //! define the return of the owners of those primitives
0053 //! selected. The primitives are sorted by criteria such
0054 //! as priority of the primitive or its depth in the view
0055 //! relative to that of other primitives.
0056 //! Note that in 3D, the inheriting framework
0057 //! StdSelect_ViewerSelector3d   is only to be used
0058 //! if you do not want to use the services provided by
0059 //! AIS.
0060 //! Two tools are available to find and select objects
0061 //! found at a given position in the view. If you want to
0062 //! select the owners of all the objects detected at
0063 //! point x,y,z you use the Init - More - Next - Picked
0064 //! loop. If, on the other hand, you want to select only
0065 //! one object detected at that point, you use the Init -
0066 //! More - OnePicked loop. In this iteration, More is
0067 //! used to see if an object was picked and
0068 //! OnePicked, to get the object closest to the pick position.
0069 //! Viewer selectors are driven by
0070 //! SelectMgr_SelectionManager, and manipulate
0071 //! the SelectMgr_Selection objects given to them by
0072 //! the selection manager.
0073 //!
0074 //! Tolerances are applied to the entities in the following way:
0075 //! 1. tolerance value stored in mytolerance will be used to calculate initial
0076 //!    selecting frustum, which will be applied for intersection testing during
0077 //!    BVH traverse;
0078 //! 2. if tolerance of sensitive entity is less than mytolerance, the frustum for
0079 //!    intersection detection will be resized according to its sensitivity.
0080 class SelectMgr_ViewerSelector : public Standard_Transient
0081 {
0082   DEFINE_STANDARD_RTTIEXT(SelectMgr_ViewerSelector, Standard_Transient)
0083   friend class SelectMgr_SelectionManager;
0084 public:
0085 
0086   //! Constructs an empty selector object.
0087   Standard_EXPORT SelectMgr_ViewerSelector();
0088 
0089   //! Returns custom pixel tolerance value.
0090   Standard_Integer CustomPixelTolerance() const { return myTolerances.CustomTolerance(); }
0091 
0092   //! Sets the pixel tolerance <theTolerance>.
0093   Standard_EXPORT void SetPixelTolerance (const Standard_Integer theTolerance);
0094 
0095   //! Returns the largest sensitivity of picking
0096   Standard_Real Sensitivity() const { return myTolerances.Tolerance(); }
0097 
0098   //! Returns the largest pixel tolerance.
0099   Standard_Integer PixelTolerance() const { return myTolerances.Tolerance(); }
0100 
0101   //! Sorts the detected entities by priority and distance.
0102   Standard_EXPORT virtual void SortResult() const;
0103 
0104   //! Returns the picked element with the highest priority,
0105   //! and which is the closest to the last successful mouse position.
0106   Handle(SelectMgr_EntityOwner) OnePicked() const
0107   {
0108     return mystored.IsEmpty()
0109          ? Handle(SelectMgr_EntityOwner)()
0110          : Picked (1);
0111   }
0112 
0113   //! Return the flag determining precedence of picked depth (distance from eye to entity) over entity priority in sorted results; TRUE by default.
0114   //! When flag is TRUE, priority will be considered only if entities have the same depth  within the tolerance.
0115   //! When flag is FALSE, entities with higher priority will be in front regardless of their depth (like x-ray).
0116   bool ToPickClosest() const { return myToPreferClosest; }
0117 
0118   //! Set flag determining precedence of picked depth over entity priority in sorted results.
0119   void SetPickClosest (bool theToPreferClosest) { myToPreferClosest = theToPreferClosest; }
0120 
0121   //! Return the type of tolerance for considering two entities having a similar depth (distance from eye to entity);
0122   //! SelectMgr_TypeOfDepthTolerance_SensitivityFactor by default.
0123   SelectMgr_TypeOfDepthTolerance DepthToleranceType() const { return myDepthTolType; }
0124 
0125   //! Return the tolerance for considering two entities having a similar depth (distance from eye to entity).
0126   Standard_Real DepthTolerance() const { return myDepthTolerance; }
0127 
0128   //! Set the tolerance for considering two entities having a similar depth (distance from eye to entity).
0129   //! @param theType [in] type of tolerance value
0130   //! @param theTolerance [in] tolerance value in 3D scale (SelectMgr_TypeOfDepthTolerance_Uniform)
0131   //!                          or in pixels (SelectMgr_TypeOfDepthTolerance_UniformPixels);
0132   //!                          value is ignored in case of SelectMgr_TypeOfDepthTolerance_SensitivityFactor
0133   void SetDepthTolerance (SelectMgr_TypeOfDepthTolerance theType,
0134                           Standard_Real theTolerance)
0135   {
0136     myDepthTolType   = theType;
0137     myDepthTolerance = theTolerance;
0138   }
0139 
0140   //! Returns the number of detected owners.
0141   Standard_Integer NbPicked() const { return mystored.Extent(); }
0142 
0143   //! Clears picking results.
0144   Standard_EXPORT void ClearPicked();
0145 
0146   //! Empties all the tables, removes all selections...
0147   void Clear() { ClearPicked(); }
0148 
0149   //! Returns the entity Owner for the object picked at specified position.
0150   //! @param theRank rank of detected object within range 1...NbPicked()
0151   Standard_EXPORT Handle(SelectMgr_EntityOwner) Picked (const Standard_Integer theRank) const;
0152 
0153   //! Returns the Entity for the object picked at specified position.
0154   //! @param theRank rank of detected object within range 1...NbPicked()
0155   Standard_EXPORT const SelectMgr_SortCriterion& PickedData (const Standard_Integer theRank) const;
0156 
0157   //! Returns the Entity for the object picked at specified position.
0158   //! @param theRank rank of detected object within range 1...NbPicked()
0159   const Handle(Select3D_SensitiveEntity)& PickedEntity (const Standard_Integer theRank) const { return PickedData (theRank).Entity; }
0160 
0161   //! Returns the 3D point (intersection of picking axis with the object nearest to eye)
0162   //! for the object picked at specified position.
0163   //! @param theRank rank of detected object within range 1...NbPicked()
0164   gp_Pnt PickedPoint (const Standard_Integer theRank) const { return PickedData (theRank).Point; }
0165 
0166   //! Remove picked entities associated with specified object.
0167   Standard_EXPORT Standard_Boolean RemovePicked (const Handle(SelectMgr_SelectableObject)& theObject);
0168 
0169   Standard_EXPORT Standard_Boolean Contains (const Handle(SelectMgr_SelectableObject)& theObject) const;
0170 
0171   //! Returns the default builder used to construct BVH of entity set.
0172   const Handle(Select3D_BVHBuilder3d) EntitySetBuilder() { return myEntitySetBuilder; }
0173 
0174   //! Sets the default builder used to construct BVH of entity set.
0175   //! The new builder will be also assigned for already defined objects, but computed BVH trees will not be invalidated.
0176   Standard_EXPORT void SetEntitySetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder);
0177 
0178   //! Returns the list of selection modes ModeList found in
0179   //! this selector for the selectable object aSelectableObject.
0180   //! Returns true if aSelectableObject is referenced inside
0181   //! this selector; returns false if the object is not present
0182   //! in this selector.
0183   Standard_EXPORT Standard_Boolean Modes (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
0184                                           TColStd_ListOfInteger& theModeList,
0185                                           const SelectMgr_StateOfSelection theWantedState = SelectMgr_SOS_Any) const;
0186 
0187   //! Returns true if the selectable object
0188   //! aSelectableObject having the selection mode aMode
0189   //! is active in this selector.
0190   Standard_EXPORT Standard_Boolean IsActive (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
0191                                              const Standard_Integer theMode) const;
0192 
0193   //! Returns true if the selectable object
0194   //! aSelectableObject having the selection mode aMode
0195   //! is in this selector.
0196   Standard_EXPORT Standard_Boolean IsInside (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
0197                                              const Standard_Integer theMode) const;
0198 
0199   //! Returns the selection status Status of the selection aSelection.
0200   Standard_EXPORT SelectMgr_StateOfSelection Status (const Handle(SelectMgr_Selection)& theSelection) const;
0201 
0202   Standard_EXPORT TCollection_AsciiString Status (const Handle(SelectMgr_SelectableObject)& theSelectableObject) const;
0203 
0204   //! Returns the list of active entity owners
0205   Standard_EXPORT void ActiveOwners (NCollection_List<Handle(SelectMgr_EntityOwner)>& theOwners) const;
0206 
0207   //! Adds new object to the map of selectable objects
0208   Standard_EXPORT void AddSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject);
0209 
0210   //! Adds new selection to the object and builds its BVH tree
0211   Standard_EXPORT void AddSelectionToObject (const Handle(SelectMgr_SelectableObject)& theObject,
0212                                              const Handle(SelectMgr_Selection)& theSelection);
0213 
0214   //! Moves existing object from set of not transform persistence objects
0215   //! to set of transform persistence objects (or vice versa).
0216   Standard_EXPORT void MoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject);
0217 
0218   //! Removes selectable object from map of selectable ones
0219   Standard_EXPORT void RemoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject);
0220 
0221   //! Removes selection of the object and marks its BVH tree for rebuild
0222   Standard_EXPORT void RemoveSelectionOfObject (const Handle(SelectMgr_SelectableObject)& theObject,
0223                                                 const Handle(SelectMgr_Selection)& theSelection);
0224 
0225   //! Marks BVH of selectable objects for rebuild. Parameter theIsForce set as true
0226   //! guarantees that 1st level BVH for the viewer selector will be rebuilt during this call
0227   Standard_EXPORT void RebuildObjectsTree (const Standard_Boolean theIsForce = Standard_False);
0228 
0229   //! Marks BVH of sensitive entities of particular selectable object for rebuild. Parameter
0230   //! theIsForce set as true guarantees that 2nd level BVH for the object given will be
0231   //! rebuilt during this call
0232   Standard_EXPORT void RebuildSensitivesTree (const Handle(SelectMgr_SelectableObject)& theObject,
0233                                               const Standard_Boolean theIsForce = Standard_False);
0234 
0235   //! Returns instance of selecting volume manager of the viewer selector
0236   SelectMgr_SelectingVolumeManager& GetManager() { return mySelectingVolumeMgr; }
0237 
0238   //! Return map of selectable objects.
0239   const SelectMgr_SelectableObjectSet& SelectableObjects() const { return mySelectableObjects; }
0240 
0241   //! Marks all added sensitive entities of all objects as non-selectable
0242   Standard_EXPORT void ResetSelectionActivationStatus();
0243 
0244   //! Is used for rectangular selection only
0245   //! If theIsToAllow is false, only fully included sensitives will be detected, otherwise the algorithm will
0246   //! mark both included and overlapped entities as matched
0247   Standard_EXPORT void AllowOverlapDetection (const Standard_Boolean theIsToAllow);
0248 
0249 public:
0250 
0251   //! Picks the sensitive entity at the pixel coordinates of
0252   //! the mouse <theXPix> and <theYPix>. The selector looks for touched areas and owners.
0253   Standard_EXPORT void Pick (const Standard_Integer theXPix,
0254                              const Standard_Integer theYPix,
0255                              const Handle(V3d_View)& theView);
0256 
0257   //! Picks the sensitive entity according to the minimum
0258   //! and maximum pixel values <theXPMin>, <theYPMin>, <theXPMax>
0259   //! and <theYPMax> defining a 2D area for selection in the 3D view aView.
0260   Standard_EXPORT void Pick (const Standard_Integer theXPMin,
0261                              const Standard_Integer theYPMin,
0262                              const Standard_Integer theXPMax,
0263                              const Standard_Integer theYPMax,
0264                              const Handle(V3d_View)& theView);
0265 
0266   //! pick action - input pixel values for polyline selection for selection.
0267   Standard_EXPORT void Pick (const TColgp_Array1OfPnt2d& thePolyline,
0268                              const Handle(V3d_View)& theView);
0269 
0270   //! Picks the sensitive entity according to the input axis.
0271   //! This is geometric intersection 3D objects by axis
0272   //! (camera parameters are ignored and objects with transform persistance are skipped).
0273   Standard_EXPORT void Pick (const gp_Ax1& theAxis,
0274                              const Handle(V3d_View)& theView);
0275 
0276   //! Dump of detection results into image.
0277   //! This method performs axis picking for each pixel in the image
0278   //! and generates a color depending on picking results and selection image type.
0279   //! @param theImage       result image, should be initialized
0280   //! @param theView        3D view defining camera position
0281   //! @param theType        type of image to define
0282   //! @param thePickedIndex index of picked entity (1 means topmost)
0283   Standard_EXPORT Standard_Boolean ToPixMap (Image_PixMap&                        theImage,
0284                                              const Handle(V3d_View)&              theView,
0285                                              const StdSelect_TypeOfSelectionImage theType,
0286                                              const Standard_Integer               thePickedIndex = 1);
0287 
0288 public:
0289 
0290   //! Displays sensitives in view <theView>.
0291   Standard_EXPORT void DisplaySensitive (const Handle(V3d_View)& theView);
0292 
0293   Standard_EXPORT void ClearSensitive (const Handle(V3d_View)& theView);
0294 
0295   Standard_EXPORT void DisplaySensitive (const Handle(SelectMgr_Selection)& theSel,
0296                                          const gp_Trsf& theTrsf,
0297                                          const Handle(V3d_View)& theView,
0298                                          const Standard_Boolean theToClearOthers = Standard_True);
0299 
0300   //! Dumps the content of me into the stream
0301   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0302 
0303 public:
0304 
0305   //! Enables/disables building BVH for sensitives in separate threads
0306   Standard_EXPORT void SetToPrebuildBVH(Standard_Boolean theToPrebuild, Standard_Integer theThreadsNum = -1);
0307 
0308   //! Queues a sensitive entity to build its BVH
0309   Standard_EXPORT void QueueBVHBuild(const Handle(Select3D_SensitiveEntity)& theEntity);
0310 
0311   //! Waits BVH threads finished building
0312   Standard_EXPORT void WaitForBVHBuild();
0313 
0314   //! Returns TRUE if building BVH for sensitives in separate threads is enabled
0315   Standard_Boolean ToPrebuildBVH() const
0316   {
0317     return myToPrebuildBVH;
0318   }
0319 
0320 protected:
0321 
0322   //! Traverses BVH containing all added selectable objects and
0323   //! finds candidates for further search of overlap
0324   Standard_EXPORT void TraverseSensitives (const Standard_Integer theViewId = -1);
0325 
0326   //! Internal function that checks if there is possible overlap between some entity of selectable object theObject and
0327   //! current selecting volume.
0328   //! @param theObject [in] the selectable object for traversal.
0329   //! @param theMgr [in] the (un)transformed copy of the selecting volume manager representing active selection frustum.
0330   //! @param theCamera, theProjectionMat, theWorldViewMat [in] the source camera and matrices for theMgr given.
0331   //! @param theViewportWidth, theViewportHeight [in] viewport (window) dimensions for evaluating 
0332   //!        object's transformation persistence.
0333   Standard_EXPORT void traverseObject (const Handle(SelectMgr_SelectableObject)& theObject,
0334                                        const SelectMgr_SelectingVolumeManager& theMgr,
0335                                        const Handle(Graphic3d_Camera)& theCamera,
0336                                        const Graphic3d_Mat4d& theProjectionMat,
0337                                        const Graphic3d_Mat4d& theWorldViewMat,
0338                                        const Graphic3d_Vec2i& theWinSize);
0339 
0340   //! Internal function that checks if a particular sensitive
0341   //! entity theEntity overlaps current selecting volume precisely
0342   Standard_EXPORT void checkOverlap (const Handle(Select3D_SensitiveEntity)& theEntity,
0343                                      const gp_GTrsf& theInversedTrsf,
0344                                      SelectMgr_SelectingVolumeManager& theMgr);
0345 
0346   //! Update z-layers order map.
0347   Standard_EXPORT void updateZLayers (const Handle(V3d_View)& theView);
0348 
0349 private:
0350 
0351   //! Checks if the entity given requires to scale current selecting frustum
0352   Standard_Boolean isToScaleFrustum (const Handle(Select3D_SensitiveEntity)& theEntity);
0353 
0354   //! In case if custom tolerance is set, this method will return sum of entity sensitivity and
0355   //! custom tolerance. Otherwise, pure entity sensitivity factor will be returned.
0356   Standard_Integer sensitivity (const Handle(Select3D_SensitiveEntity)& theEntity) const;
0357 
0358   void Activate (const Handle(SelectMgr_Selection)& theSelection);
0359 
0360   void Deactivate (const Handle(SelectMgr_Selection)& theSelection);
0361 
0362   //! removes a Selection from the Selector
0363   void Remove (const Handle(SelectMgr_Selection)& aSelection);
0364 
0365   //! Internal function that checks if a current selecting frustum needs to be scaled and transformed for the entity and performs necessary calculations.
0366   void computeFrustum (const Handle(Select3D_SensitiveEntity)& theEnt,
0367                        const SelectMgr_SelectingVolumeManager& theMgrGlobal,
0368                        const SelectMgr_SelectingVolumeManager& theMgrObject,
0369                        const gp_GTrsf& theInvTrsf,
0370                        SelectMgr_FrustumCache& theCachedMgrs,
0371                        SelectMgr_SelectingVolumeManager& theResMgr);
0372 
0373 
0374 private:
0375 
0376   //! Compute 3d position for detected entity.
0377   void updatePoint3d (SelectMgr_SortCriterion& theCriterion,
0378                       const SelectBasics_PickResult& thePickResult,
0379                       const Handle(Select3D_SensitiveEntity)& theEntity,
0380                       const gp_GTrsf& theInversedTrsf,
0381                       const SelectMgr_SelectingVolumeManager& theMgr) const;
0382 
0383 protected:
0384 
0385   Standard_Real                                 myDepthTolerance;
0386   SelectMgr_TypeOfDepthTolerance                myDepthTolType;
0387   Standard_Boolean                              myToPreferClosest;
0388   SelectMgr_IndexedDataMapOfOwnerCriterion      mystored;
0389   SelectMgr_SelectingVolumeManager              mySelectingVolumeMgr;
0390   mutable SelectMgr_SelectableObjectSet         mySelectableObjects;
0391   SelectMgr_ToleranceMap                        myTolerances;
0392   NCollection_DataMap<Graphic3d_ZLayerId, Standard_Integer> myZLayerOrderMap;
0393   Handle(Select3D_BVHBuilder3d)                 myEntitySetBuilder;
0394   gp_Pnt                                        myCameraEye;
0395   gp_Dir                                        myCameraDir;
0396   Standard_Real                                 myCameraScale;
0397 
0398   Standard_Boolean                              myToPrebuildBVH;
0399   Handle(SelectMgr_BVHThreadPool)               myBVHThreadPool;
0400 
0401   mutable TColStd_Array1OfInteger              myIndexes;
0402   mutable Standard_Boolean                     myIsSorted;
0403   Standard_Boolean                             myIsLeftChildQueuedFirst;
0404   SelectMgr_MapOfObjectSensitives              myMapOfObjectSensitives;
0405 
0406   Graphic3d_SequenceOfStructure                myStructs; //!< list of debug presentations
0407 
0408 };
0409 
0410 DEFINE_STANDARD_HANDLE(SelectMgr_ViewerSelector, Standard_Transient)
0411 
0412 #endif