Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-30 10:29:21

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