Back to home page

EIC code displayed by LXR

 
 

    


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

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 build 
0024 //! set of BVH trees for each transformation persistence subclass of selectable objects. This allow to minify
0025 //! number of updates for BVH trees - for example 2D persistent object subclass depends only on camera's projection
0026 //! and the corresponding BVH tree needs to be updated when camera's projection parameters change, while another
0027 //! tree for non-persistent objects can be left unchanged in this case.
0028 class SelectMgr_SelectableObjectSet
0029 {
0030 public:
0031 
0032   //! This enumeration declares names for subsets of selectable objects. Each subset has independent BVH tree.
0033   //! The class maintains subsets of selectable objects by their persistence flag. This allows to restric
0034   //! rebuilding of the trees for particular subset when the camera change does not implicitly require it:
0035   //! - BVHSubset_3d refers to the subset of normal world-space 3D objects. Associated BVH tree does not depend
0036   //! on the camera's state at all.
0037   //! This subset uses binned BVH builder with 32 bins and 1 element per leaf.
0038   //! - BVHSubset_3dPersistent refers to the subset of 3D persistent selectable objects (rotate, pan, zoom persistence).
0039   //! Associated BVH tree needs to be updated when either the camera's projection and position change.
0040   //! This subset uses linear BVH builder with 32 levels of depth and 1 element per leaf.
0041   //! - BVHSubset_2dPersistent refers to the subset of 2D persistent selectable objects. Associated BVH tree
0042   //! needs to be updated only when camera's projection changes. Bounding volumes for this object subclass
0043   //! is represented directly in eye space coordinates.
0044   //! This subset uses linear BVH builder with 32 levels of depth and 1 element per leaf.
0045   enum BVHSubset
0046   {
0047     BVHSubset_3d,
0048     BVHSubset_3dPersistent,
0049     BVHSubset_2dPersistent,
0050     BVHSubsetNb
0051   };
0052 
0053 public:
0054 
0055   //! Class to iterate sequentually over all objects from every subset.
0056   class Iterator
0057   {
0058     //! Short-cut definition of map iterator type
0059     typedef NCollection_IndexedMap<Handle(SelectMgr_SelectableObject)>::Iterator ObjectMapIterator;
0060 
0061   public:
0062 
0063     //! Default constructor without initialization.
0064     Iterator() : mySet (NULL), mySubsetIdx (BVHSubsetNb) {}
0065 
0066     //! Constructs and initializes the iterator.
0067     Iterator (const SelectMgr_SelectableObjectSet& theSet) { Init (theSet); }
0068 
0069     //! Initializes the iterator.
0070     void Init (const SelectMgr_SelectableObjectSet& theSet)
0071     {
0072       mySet       = &theSet;
0073       mySubsetIdx = 0;
0074       mySubsetIt  = ObjectMapIterator (theSet.myObjects[mySubsetIdx]);
0075       More();
0076     }
0077 
0078     //! Returns false when there is no more objects to iterate over.
0079     Standard_Boolean More()
0080     {
0081       if (mySubsetIt.More())
0082       {
0083         return Standard_True;
0084       }
0085       else if ((mySubsetIdx == BVHSubsetNb - 1) || mySet == NULL)
0086       {
0087         return Standard_False;
0088       }
0089       mySubsetIt = ObjectMapIterator (mySet->myObjects[++mySubsetIdx]);
0090       return More();
0091     }
0092 
0093     //! Steps to next selectable object in the set.
0094     void Next() { mySubsetIt.Next(); }
0095 
0096     //! Returns current object.
0097     const Handle(SelectMgr_SelectableObject)& Value() const { return mySubsetIt.Value(); }
0098 
0099   private:
0100     const SelectMgr_SelectableObjectSet* mySet;
0101     Standard_Integer mySubsetIdx;
0102     ObjectMapIterator mySubsetIt;
0103   };
0104 
0105 public:
0106 
0107   //! Creates new empty objects set and initializes BVH tree builders for each subset.
0108   Standard_EXPORT SelectMgr_SelectableObjectSet();
0109 
0110   //! Releases resources of selectable object set.
0111   virtual ~SelectMgr_SelectableObjectSet() { }
0112 
0113   //! Adds the new selectable object to the set. The selectable object is placed into one of the
0114   //! predefined subsets depending on its persistence type. After adding an object, this method
0115   //! marks the corresponding BVH tree for rebuild.
0116   //! @return true if selectable object is added, otherwise returns false (selectable object is already in the set).
0117   Standard_EXPORT Standard_Boolean Append (const Handle(SelectMgr_SelectableObject)& theObject);
0118 
0119   //! Removes the selectable object from the set. The selectable object is removed from the subset
0120   //! it has been placed into. After removing an object, this method marks the corresponding
0121   //! BVH tree for rebuild.
0122   //! @return true if selectable object is removed, otherwise returns false (selectable object is not in the set).
0123   Standard_EXPORT Standard_Boolean Remove (const Handle(SelectMgr_SelectableObject)& theObject);
0124 
0125   //! Performs necessary updates when object's persistence types changes.
0126   //! This method should be called right after changing transformation persistence flags of the
0127   //! objects and before updating BVH tree - to provide up-to-date state of the object set.
0128   Standard_EXPORT void ChangeSubset (const Handle(SelectMgr_SelectableObject)& theObject);
0129 
0130   //! Updates outdated BVH trees and remembers the last state of the
0131   //! camera view-projection matrices and viewport (window) dimensions.
0132   Standard_EXPORT void UpdateBVH (const Handle(Graphic3d_Camera)& theCam,
0133                                   const Graphic3d_Vec2i& theWinSize);
0134 
0135   //! Marks every BVH subset for update.
0136   Standard_EXPORT void MarkDirty();
0137 
0138   //! Returns true if this objects set contains theObject given.
0139   Standard_Boolean Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
0140   {
0141     return myObjects[BVHSubset_3d].Contains (theObject)
0142         || myObjects[BVHSubset_3dPersistent].Contains (theObject)
0143         || myObjects[BVHSubset_2dPersistent].Contains (theObject);
0144   }
0145 
0146   //! Returns true if the object set does not contain any selectable objects.
0147   Standard_Boolean IsEmpty() const
0148   {
0149     return myObjects[BVHSubset_3d].IsEmpty()
0150         && myObjects[BVHSubset_3dPersistent].IsEmpty()
0151         && myObjects[BVHSubset_2dPersistent].IsEmpty();
0152   }
0153 
0154   //! Returns true if the specified object subset is empty.
0155   Standard_Boolean IsEmpty (const BVHSubset theSubset) const
0156   {
0157     return myObjects[theSubset].IsEmpty();
0158   }
0159 
0160   //! Returns object from subset theSubset by theIndex given. The method allows to get selectable object
0161   //! referred by the index of an element of the subset's BVH tree.
0162   const Handle(SelectMgr_SelectableObject)& GetObjectById (const BVHSubset theSubset,
0163                                                            const Standard_Integer theIndex) const
0164   {
0165     return myObjects[theSubset].FindKey (theIndex + 1);
0166   }
0167 
0168   //! Returns computed BVH for the theSubset given.
0169   const opencascade::handle<BVH_Tree<Standard_Real, 3> >& BVH(const BVHSubset theSubset) const
0170   {
0171     return myBVH[theSubset];
0172   }
0173 
0174   //! Dumps the content of me into the stream
0175   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0176 
0177 private:
0178 
0179   //! Returns an appropriate subset of theObject given depending on its persistence type.
0180   Standard_Integer appropriateSubset (const Handle(SelectMgr_SelectableObject)& theObject)
0181   {
0182     if (theObject->TransformPersistence().IsNull())
0183     {
0184       const PrsMgr_Presentations& aPresentations = theObject->Presentations();
0185       for (PrsMgr_Presentations::Iterator aPrsIter (aPresentations); aPrsIter.More(); aPrsIter.Next())
0186       {
0187         const Handle(PrsMgr_Presentation)& aPrs3d = aPrsIter.ChangeValue();
0188         if (aPrs3d->CStructure()->HasGroupTransformPersistence())
0189         {
0190           return SelectMgr_SelectableObjectSet::BVHSubset_3dPersistent;
0191         }
0192       }
0193       return SelectMgr_SelectableObjectSet::BVHSubset_3d;
0194     }
0195     else if (theObject->TransformPersistence()->Mode() == Graphic3d_TMF_2d)
0196     {
0197       return SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent;
0198     }
0199     else
0200     {
0201       return SelectMgr_SelectableObjectSet::BVHSubset_3dPersistent;
0202     }
0203   }
0204 
0205   //! Returns current subset of theObject given.
0206   Standard_Integer currentSubset (const Handle(SelectMgr_SelectableObject)& theObject)
0207   {
0208     for (Standard_Integer aSubsetIdx = 0; aSubsetIdx < BVHSubsetNb; ++aSubsetIdx)
0209     {
0210       if (myObjects[aSubsetIdx].Contains (theObject))
0211       {
0212         return aSubsetIdx;
0213       }
0214     }
0215     return -1;
0216   }
0217 
0218 private:
0219 
0220   NCollection_IndexedMap<Handle(SelectMgr_SelectableObject)> myObjects[BVHSubsetNb]; //!< Map of objects for each subset
0221   opencascade::handle<BVH_Tree<Standard_Real, 3> >           myBVH[BVHSubsetNb];     //!< BVH tree computed for each subset
0222   Handle(Select3D_BVHBuilder3d)                              myBuilder[BVHSubsetNb]; //!< Builder allocated for each subset
0223   Standard_Boolean                                           myIsDirty[BVHSubsetNb]; //!< Dirty flag for each subset
0224   Graphic3d_WorldViewProjState                               myLastViewState;        //!< Last view-projection state used for construction of BVH
0225   Graphic3d_Vec2i                                            myLastWinSize;          //!< Last viewport's (window's) width used for construction of BVH
0226   friend class Iterator;
0227 };
0228 
0229 #endif // _SelectMgr_SelectableObjectSet_HeaderFile