Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-12 08:21:52

0001 // Created on: 2014-08-15
0002 // Created by: Varvara POSKONINA
0003 // Copyright (c) 2005-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _SelectMgr_SelectableObjectSet_HeaderFile
0017 #define _SelectMgr_SelectableObjectSet_HeaderFile
0018 
0019 #include <NCollection_Handle.hxx>
0020 #include <Select3D_BVHBuilder3d.hxx>
0021 #include <SelectMgr_SelectableObject.hxx>
0022 
0023 //! The purpose of this class is to organize all selectable objects into data structure, allowing to
0024 //! build set of BVH trees for each transformation persistence subclass of selectable objects. This
0025 //! allow to minify number of updates for BVH trees - for example 2D persistent object subclass
0026 //! depends only on camera's projection and the corresponding BVH tree needs to be updated when
0027 //! camera's projection parameters change, while another tree for non-persistent objects can be left
0028 //! unchanged in this case.
0029 class SelectMgr_SelectableObjectSet
0030 {
0031 public:
0032   //! This enumeration declares names for subsets of selectable objects. Each subset has independent
0033   //! BVH tree. The class maintains subsets of selectable objects by their persistence flag. This
0034   //! allows to restric rebuilding of the trees for particular subset when the camera change does
0035   //! not implicitly require it:
0036   //! - BVHSubset_3d refers to the subset of normal world-space 3D objects. Associated BVH tree does
0037   //! not depend on the camera's state at all. This subset uses binned BVH builder with 32 bins and
0038   //! 1 element per leaf.
0039   //! - BVHSubset_3dPersistent refers to the subset of 3D persistent selectable objects (rotate,
0040   //! pan, zoom persistence). Associated BVH tree needs to be updated when either the camera's
0041   //! projection and position change. This subset uses linear BVH builder with 32 levels of depth
0042   //! and 1 element per leaf.
0043   //! - BVHSubset_2dPersistent refers to the subset of 2D persistent selectable objects. Associated
0044   //! BVH tree needs to be updated only when camera's projection changes. Bounding volumes for this
0045   //! object subclass is represented directly in eye space coordinates. This subset uses linear BVH
0046   //! builder with 32 levels of depth and 1 element per leaf.
0047   //! - BVHSubset_ortho3dPersistent refers to the subset of 3D persistent selectable objects
0048   //! (rotate, pan, zoom persistence) that contains `Graphic3d_TMF_OrthoPers` persistence mode.
0049   //! Associated BVH tree needs to be updated when either the camera's projection and position
0050   //! change. This subset uses linear BVH builder with 32 levels of depth and 1 element per leaf.
0051   //! - BVHSubset_ortho2dPersistent refers to the subset of 2D persistent selectable objects
0052   //! that contains `Graphic3d_TMF_OrthoPers` persistence mode. Associated BVH tree
0053   //! needs to be updated only when camera's projection changes. Bounding volumes for this object
0054   //! subclass is represented directly in eye space coordinates. This subset uses linear BVH builder
0055   //! with 32 levels of depth and 1 element per leaf.
0056   enum BVHSubset
0057   {
0058     BVHSubset_3d,
0059     BVHSubset_3dPersistent,
0060     BVHSubset_2dPersistent,
0061     BVHSubset_ortho3dPersistent,
0062     BVHSubset_ortho2dPersistent,
0063     BVHSubsetNb
0064   };
0065 
0066 public:
0067   //! Class to iterate sequentually over all objects from every subset.
0068   class Iterator
0069   {
0070     //! Short-cut definition of map iterator type
0071     typedef NCollection_IndexedMap<Handle(SelectMgr_SelectableObject)>::Iterator ObjectMapIterator;
0072 
0073   public:
0074     //! Default constructor without initialization.
0075     Iterator()
0076         : mySet(NULL),
0077           mySubsetIdx(BVHSubsetNb)
0078     {
0079     }
0080 
0081     //! Constructs and initializes the iterator.
0082     Iterator(const SelectMgr_SelectableObjectSet& theSet) { Init(theSet); }
0083 
0084     //! Initializes the iterator.
0085     void Init(const SelectMgr_SelectableObjectSet& theSet)
0086     {
0087       mySet       = &theSet;
0088       mySubsetIdx = 0;
0089       mySubsetIt  = ObjectMapIterator(theSet.myObjects[mySubsetIdx]);
0090       More();
0091     }
0092 
0093     //! Returns false when there is no more objects to iterate over.
0094     Standard_Boolean More()
0095     {
0096       if (mySubsetIt.More())
0097       {
0098         return Standard_True;
0099       }
0100       else if ((mySubsetIdx == BVHSubsetNb - 1) || mySet == NULL)
0101       {
0102         return Standard_False;
0103       }
0104       mySubsetIt = ObjectMapIterator(mySet->myObjects[++mySubsetIdx]);
0105       return More();
0106     }
0107 
0108     //! Steps to next selectable object in the set.
0109     void Next() { mySubsetIt.Next(); }
0110 
0111     //! Returns current object.
0112     const Handle(SelectMgr_SelectableObject)& Value() const { return mySubsetIt.Value(); }
0113 
0114   private:
0115     const SelectMgr_SelectableObjectSet* mySet;
0116     Standard_Integer                     mySubsetIdx;
0117     ObjectMapIterator                    mySubsetIt;
0118   };
0119 
0120 public:
0121   //! Creates new empty objects set and initializes BVH tree builders for each subset.
0122   Standard_EXPORT SelectMgr_SelectableObjectSet();
0123 
0124   //! Releases resources of selectable object set.
0125   virtual ~SelectMgr_SelectableObjectSet() {}
0126 
0127   //! Adds the new selectable object to the set. The selectable object is placed into one of the
0128   //! predefined subsets depending on its persistence type. After adding an object, this method
0129   //! marks the corresponding BVH tree for rebuild.
0130   //! @return true if selectable object is added, otherwise returns false (selectable object is
0131   //! already in the set).
0132   Standard_EXPORT Standard_Boolean Append(const Handle(SelectMgr_SelectableObject)& theObject);
0133 
0134   //! Removes the selectable object from the set. The selectable object is removed from the subset
0135   //! it has been placed into. After removing an object, this method marks the corresponding
0136   //! BVH tree for rebuild.
0137   //! @return true if selectable object is removed, otherwise returns false (selectable object is
0138   //! not in the set).
0139   Standard_EXPORT Standard_Boolean Remove(const Handle(SelectMgr_SelectableObject)& theObject);
0140 
0141   //! Performs necessary updates when object's persistence types changes.
0142   //! This method should be called right after changing transformation persistence flags of the
0143   //! objects and before updating BVH tree - to provide up-to-date state of the object set.
0144   Standard_EXPORT void ChangeSubset(const Handle(SelectMgr_SelectableObject)& theObject);
0145 
0146   //! Updates outdated BVH trees and remembers the last state of the
0147   //! camera view-projection matrices and viewport (window) dimensions.
0148   Standard_EXPORT void UpdateBVH(const Handle(Graphic3d_Camera)& theCam,
0149                                  const Graphic3d_Vec2i&          theWinSize);
0150 
0151   //! Marks every BVH subset for update.
0152   Standard_EXPORT void MarkDirty();
0153 
0154   //! Returns true if this objects set contains theObject given.
0155   Standard_Boolean Contains(const Handle(SelectMgr_SelectableObject)& theObject) const
0156   {
0157     return myObjects[BVHSubset_3d].Contains(theObject)
0158            || myObjects[BVHSubset_3dPersistent].Contains(theObject)
0159            || myObjects[BVHSubset_2dPersistent].Contains(theObject)
0160            || myObjects[BVHSubset_ortho3dPersistent].Contains(theObject)
0161            || myObjects[BVHSubset_ortho2dPersistent].Contains(theObject);
0162   }
0163 
0164   //! Returns true if the object set does not contain any selectable objects.
0165   Standard_Boolean IsEmpty() const
0166   {
0167     return myObjects[BVHSubset_3d].IsEmpty() && myObjects[BVHSubset_3dPersistent].IsEmpty()
0168            && myObjects[BVHSubset_2dPersistent].IsEmpty()
0169            && myObjects[BVHSubset_ortho3dPersistent].IsEmpty()
0170            && myObjects[BVHSubset_ortho2dPersistent].IsEmpty();
0171   }
0172 
0173   //! Returns true if the specified object subset is empty.
0174   Standard_Boolean IsEmpty(const BVHSubset theSubset) const
0175   {
0176     return myObjects[theSubset].IsEmpty();
0177   }
0178 
0179   //! Returns object from subset theSubset by theIndex given. The method allows to get selectable
0180   //! object referred by the index of an element of the subset's BVH tree.
0181   const Handle(SelectMgr_SelectableObject)& GetObjectById(const BVHSubset        theSubset,
0182                                                           const Standard_Integer theIndex) const
0183   {
0184     return myObjects[theSubset].FindKey(theIndex + 1);
0185   }
0186 
0187   //! Returns computed BVH for the theSubset given.
0188   const opencascade::handle<BVH_Tree<Standard_Real, 3>>& BVH(const BVHSubset theSubset) const
0189   {
0190     return myBVH[theSubset];
0191   }
0192 
0193   //! Dumps the content of me into the stream
0194   Standard_EXPORT void DumpJson(Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0195 
0196 private:
0197   //! Returns an appropriate subset of theObject given depending on its persistence type.
0198   Standard_Integer appropriateSubset(const Handle(SelectMgr_SelectableObject)& theObject)
0199   {
0200     if (theObject->TransformPersistence().IsNull())
0201     {
0202       const PrsMgr_Presentations& aPresentations = theObject->Presentations();
0203       for (PrsMgr_Presentations::Iterator aPrsIter(aPresentations); aPrsIter.More();
0204            aPrsIter.Next())
0205       {
0206         const Handle(PrsMgr_Presentation)& aPrs3d = aPrsIter.ChangeValue();
0207         if (aPrs3d->CStructure()->HasGroupTransformPersistence())
0208         {
0209           return SelectMgr_SelectableObjectSet::BVHSubset_3dPersistent;
0210         }
0211       }
0212       return SelectMgr_SelectableObjectSet::BVHSubset_3d;
0213     }
0214     else if ((theObject->TransformPersistence()->Mode() & Graphic3d_TMF_2d) != 0)
0215     {
0216       if (theObject->TransformPersistence()->IsOrthoPers())
0217       {
0218         return SelectMgr_SelectableObjectSet::BVHSubset_ortho2dPersistent;
0219       }
0220       return SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent;
0221     }
0222     else if (theObject->TransformPersistence()->IsOrthoPers())
0223     {
0224       return SelectMgr_SelectableObjectSet::BVHSubset_ortho3dPersistent;
0225     }
0226     else
0227     {
0228       return SelectMgr_SelectableObjectSet::BVHSubset_3dPersistent;
0229     }
0230   }
0231 
0232   //! Returns current subset of theObject given.
0233   Standard_Integer currentSubset(const Handle(SelectMgr_SelectableObject)& theObject)
0234   {
0235     for (Standard_Integer aSubsetIdx = 0; aSubsetIdx < BVHSubsetNb; ++aSubsetIdx)
0236     {
0237       if (myObjects[aSubsetIdx].Contains(theObject))
0238       {
0239         return aSubsetIdx;
0240       }
0241     }
0242     return -1;
0243   }
0244 
0245 private:
0246   // clang-format off
0247   NCollection_IndexedMap<Handle(SelectMgr_SelectableObject)> myObjects[BVHSubsetNb]; //!< Map of objects for each subset
0248   opencascade::handle<BVH_Tree<Standard_Real, 3> >           myBVH[BVHSubsetNb];     //!< BVH tree computed for each subset
0249   Handle(Select3D_BVHBuilder3d)                              myBuilder[BVHSubsetNb]; //!< Builder allocated for each subset
0250   Standard_Boolean                                           myIsDirty[BVHSubsetNb]; //!< Dirty flag for each subset
0251   Graphic3d_WorldViewProjState                               myLastViewState;        //!< Last view-projection state used for construction of BVH
0252   Graphic3d_Vec2i                                            myLastWinSize;          //!< Last viewport's (window's) width used for construction of BVH
0253   // clang-format on
0254   friend class Iterator;
0255 };
0256 
0257 #endif // _SelectMgr_SelectableObjectSet_HeaderFile