From f5b7241978a9c2b1033e5e837f5dd7b1db9d69c2 Mon Sep 17 00:00:00 2001 From: kgv Date: Fri, 26 May 2017 10:45:22 +0300 Subject: [PATCH] 0028789: Visualization, TKV3d - extend API for accessing and assigning BVH builders Several methods in Selection classes have been moved to header files for better inlining. BVH_Constants - added new enumeration defining common constant values used with BVH trees. BVH - replaced NCollection_Handle with Standard_Transient handle in classes BVH_Properties, BVH_Builder, BVH_Tree, BVH_Object. Defined global BVH-builders instead of allocating a new one for each object set. SelectMgr_ViewerSelector - added new method ::SetEntitySetBuilder() defining default BVH Tree builder for SelectMgr_SensitiveEntitySet. Added new method SelectMgr_SensitiveEntitySet::SetBuilder() for overriding default BVH tree builder. --- dox/dev_guides/upgrade/upgrade.md | 6 +- src/BRepExtrema/BRepExtrema_OverlapTool.cxx | 4 +- src/BRepExtrema/BRepExtrema_TriangleSet.cxx | 9 +- src/BRepExtrema/BRepExtrema_TriangleSet.hxx | 10 +-- src/BVH/BVH.cxx | 4 + src/BVH/BVH_BinnedBuilder.hxx | 8 +- src/BVH/BVH_Box.hxx | 1 + src/BVH/BVH_Builder.hxx | 51 +++++++---- src/BVH/BVH_Constants.hxx | 45 ++++++++++ src/BVH/BVH_DistanceField.lxx | 7 +- src/BVH/BVH_Geometry.hxx | 28 ++++-- src/BVH/BVH_LinearBuilder.hxx | 4 +- src/BVH/BVH_Object.hxx | 46 ++++++---- src/BVH/BVH_ObjectSet.hxx | 2 +- src/BVH/BVH_PrimitiveSet.hxx | 27 ++++-- src/BVH/BVH_PrimitiveSet3d.hxx | 21 +++++ src/BVH/BVH_Properties.cxx | 9 +- src/BVH/BVH_Properties.hxx | 3 +- src/BVH/BVH_RadixSorter.hxx | 6 +- src/BVH/BVH_SpatialMedianBuilder.hxx | 4 +- src/BVH/BVH_SweepPlaneBuilder.hxx | 4 +- src/BVH/BVH_Tree.hxx | 11 ++- src/BVH/BVH_Triangulation.hxx | 7 ++ src/BVH/BVH_Types.hxx | 1 + src/BVH/FILES | 2 + src/OpenGl/OpenGl_BVHClipPrimitiveSet.cxx | 4 +- src/OpenGl/OpenGl_BVHClipPrimitiveSet.hxx | 7 +- .../OpenGl_BVHClipPrimitiveTrsfPersSet.cxx | 10 +-- .../OpenGl_BVHClipPrimitiveTrsfPersSet.hxx | 26 +++--- src/OpenGl/OpenGl_Layer.cxx | 11 ++- src/OpenGl/OpenGl_Layer.hxx | 12 ++- src/OpenGl/OpenGl_LayerList.cxx | 57 +++++++----- src/OpenGl/OpenGl_LayerList.hxx | 11 ++- src/OpenGl/OpenGl_SceneGeometry.cxx | 57 ++++++------ src/OpenGl/OpenGl_SceneGeometry.hxx | 13 +-- src/OpenGl/OpenGl_View.cxx | 10 +++ src/OpenGl/OpenGl_View.hxx | 9 +- src/OpenGl/OpenGl_View_Raytrace.cxx | 38 +++----- src/Select3D/FILES | 1 + src/Select3D/Select3D_BVHBuilder3d.hxx | 22 +++++ src/Select3D/Select3D_SensitiveSet.cxx | 29 ++++++- src/Select3D/Select3D_SensitiveSet.hxx | 27 ++++-- .../SelectBasics_SensitiveEntity.cxx | 12 --- .../SelectBasics_SensitiveEntity.hxx | 6 +- .../SelectMgr_SelectableObjectSet.cxx | 12 +-- .../SelectMgr_SelectableObjectSet.hxx | 9 +- src/SelectMgr/SelectMgr_Selection.cxx | 12 +-- src/SelectMgr/SelectMgr_SensitiveEntity.cxx | 42 +-------- src/SelectMgr/SelectMgr_SensitiveEntity.hxx | 8 +- .../SelectMgr_SensitiveEntitySet.cxx | 13 +-- .../SelectMgr_SensitiveEntitySet.hxx | 15 ++-- src/SelectMgr/SelectMgr_ViewerSelector.cxx | 87 ++++++++++--------- src/SelectMgr/SelectMgr_ViewerSelector.hxx | 18 ++-- src/StdSelect/StdSelect_BRepSelectionTool.cxx | 20 +++-- 54 files changed, 561 insertions(+), 357 deletions(-) create mode 100644 src/BVH/BVH_Constants.hxx create mode 100644 src/BVH/BVH_PrimitiveSet3d.hxx mode change 100755 => 100644 src/OpenGl/OpenGl_SceneGeometry.cxx mode change 100755 => 100644 src/OpenGl/OpenGl_SceneGeometry.hxx create mode 100644 src/Select3D/Select3D_BVHBuilder3d.hxx diff --git a/dox/dev_guides/upgrade/upgrade.md b/dox/dev_guides/upgrade/upgrade.md index f26694ea3e..6c317a4aba 100644 --- a/dox/dev_guides/upgrade/upgrade.md +++ b/dox/dev_guides/upgrade/upgrade.md @@ -1246,15 +1246,13 @@ In most cases this change should be transparent, however applications implementi * Types GeomPlate_Array1OfHCurveOnSurface and GeomPlate_HArray1OfHCurveOnSurface have been replaced with GeomPlate_Array1OfHCurve and GeomPlate_HArray1OfHCurve correspondingly (accept base class Adaptor3d_HCurve instead of Adaptor3d_HCurveOnSurface). * Enumeration *Image_PixMap::ImgFormat*, previously declared as nested enumeration within class *Image_PixMap*, has been moved to global namespace as *Image_Format* following OCCT coding rules. The enumeration values have suffix Image_Format_ and preserve previous name scheme for easy renaming of old values - e.g. Image_PixMap::ImgGray become Image_Format_Gray. -<<<<<<< HEAD Old definitions are preserved as depreacated aliases to the new ones; * The method BOPAlgo_Builder::Origins() returns BOPCol_DataMapOfShapeListOfShape instead of BOPCol_DataMapOfShapeShape. -======= - Old definitions are preserved as depreacated aliases to the new ones. * The methods BOPDS_DS::IsToSort(const Handle(BOPDS_CommonBlock)&, Standard_Integer&) and BOPDS_DS::SortPaveBlocks(const Handle(BOPDS_CommonBlock)&) have been removed. The sorting is now performed during the addition of the Pave Blocks into Common Block. * The methods BOPAlgo_Tools::MakeBlocks() and BOPAlgo_Tools::MakeBlocksCnx() have been replaced with the single template method BOPAlgo_Tools::MakeBlocks(). The chains of connected elements are now stored into the list of list instead of data map. * The methods BOPAlgo_Tools::FillMap() have been replaced with the single template method BOPAlgo_Tools::FillMap(). ->>>>>>> 648fab3... 0028259: Method MakeBlocksCnx is duplicated in two different places in BOPAlgo +* Package BVH now uses opencascade::handle instead of NCollection_Handle (for classes BVH_Properties, BVH_Builder, BVH_Tree, BVH_Object). + Application code using BVH package directly should be updated accordingly. @subsection upgrade_720_BOP_DataStructure BOP - Pairs of interfering indices diff --git a/src/BRepExtrema/BRepExtrema_OverlapTool.cxx b/src/BRepExtrema/BRepExtrema_OverlapTool.cxx index 464e1e84e5..f2600de4c8 100644 --- a/src/BRepExtrema/BRepExtrema_OverlapTool.cxx +++ b/src/BRepExtrema/BRepExtrema_OverlapTool.cxx @@ -703,8 +703,8 @@ void BRepExtrema_OverlapTool::Perform (const Standard_Real theTolerance) BRepExtrema_StackItem aStack[96]; - const NCollection_Handle >& aBVH1 = mySet1->BVH(); - const NCollection_Handle >& aBVH2 = mySet2->BVH(); + const opencascade::handle >& aBVH1 = mySet1->BVH(); + const opencascade::handle >& aBVH2 = mySet2->BVH(); if (aBVH1.IsNull() || aBVH2.IsNull()) { diff --git a/src/BRepExtrema/BRepExtrema_TriangleSet.cxx b/src/BRepExtrema/BRepExtrema_TriangleSet.cxx index ff4b5382a1..bf971809b0 100644 --- a/src/BRepExtrema/BRepExtrema_TriangleSet.cxx +++ b/src/BRepExtrema/BRepExtrema_TriangleSet.cxx @@ -21,18 +21,16 @@ #include #include - -IMPLEMENT_STANDARD_RTTIEXT(BRepExtrema_TriangleSet,Standard_Transient) +IMPLEMENT_STANDARD_RTTIEXT(BRepExtrema_TriangleSet, BVH_PrimitiveSet3d) //======================================================================= //function : BRepExtrema_TriangleSet //purpose : Creates empty triangle set //======================================================================= BRepExtrema_TriangleSet::BRepExtrema_TriangleSet() -: BVH_PrimitiveSet() { // Set default builder - linear BVH (LBVH) - myBuilder = new BVH_LinearBuilder (5, 32); + myBuilder = new BVH_LinearBuilder (BVH_Constants_LeafNodeSizeDefault, BVH_Constants_MaxTreeDepth); } //======================================================================= @@ -40,10 +38,9 @@ BRepExtrema_TriangleSet::BRepExtrema_TriangleSet() //purpose : Creates triangle set from the given face //======================================================================= BRepExtrema_TriangleSet::BRepExtrema_TriangleSet (const BRepExtrema_ShapeList& theFaces) -: BVH_PrimitiveSet() { // Set default builder - linear BVH (LBVH) - myBuilder = new BVH_LinearBuilder (5, 32); + myBuilder = new BVH_LinearBuilder (BVH_Constants_LeafNodeSizeDefault, BVH_Constants_MaxTreeDepth); Init (theFaces); } diff --git a/src/BRepExtrema/BRepExtrema_TriangleSet.hxx b/src/BRepExtrema/BRepExtrema_TriangleSet.hxx index a8b073f821..ff43a2e711 100644 --- a/src/BRepExtrema/BRepExtrema_TriangleSet.hxx +++ b/src/BRepExtrema/BRepExtrema_TriangleSet.hxx @@ -17,13 +17,13 @@ #define _BRepExtrema_TriangleSet_HeaderFile #include -#include +#include //! List of shapes and their IDs for collision detection. typedef NCollection_Vector BRepExtrema_ShapeList; //! Triangle set corresponding to specific face. -class BRepExtrema_TriangleSet : public BVH_PrimitiveSet, public Standard_Transient +class BRepExtrema_TriangleSet : public BVH_PrimitiveSet3d { public: @@ -45,7 +45,7 @@ public: //! @name methods implementing BVH set interface BVH_Box Box (const Standard_Integer theIndex) const Standard_OVERRIDE; //! Make inherited method Box() visible to avoid CLang warning - using BVH_PrimitiveSet::Box; + using BVH_PrimitiveSet3d::Box; //! Returns centroid position along specified axis. Standard_Real Center (const Standard_Integer theIndex, const Standard_Integer theAxis) const Standard_OVERRIDE; @@ -83,10 +83,10 @@ protected: public: - DEFINE_STANDARD_RTTIEXT(BRepExtrema_TriangleSet,Standard_Transient) + DEFINE_STANDARD_RTTIEXT(BRepExtrema_TriangleSet, BVH_PrimitiveSet3d) }; -DEFINE_STANDARD_HANDLE (BRepExtrema_TriangleSet, Standard_Transient) +DEFINE_STANDARD_HANDLE(BRepExtrema_TriangleSet, BVH_PrimitiveSet3d) #endif // _BRepExtrema_TriangleSet_HeaderFile diff --git a/src/BVH/BVH.cxx b/src/BVH/BVH.cxx index 2301f3f68e..451fd0c19d 100644 --- a/src/BVH/BVH.cxx +++ b/src/BVH/BVH.cxx @@ -25,6 +25,10 @@ #include +IMPLEMENT_STANDARD_RTTIEXT(BVH_BuilderTransient, Standard_Transient) +IMPLEMENT_STANDARD_RTTIEXT(BVH_TreeBaseTransient, Standard_Transient) +IMPLEMENT_STANDARD_RTTIEXT(BVH_ObjectTransient, Standard_Transient) + // Specific instantiations of struct templates to avoid compilation warnings template class NCollection_Vec2; diff --git a/src/BVH/BVH_BinnedBuilder.hxx b/src/BVH/BVH_BinnedBuilder.hxx index eb21a8d256..e8cf2fe552 100644 --- a/src/BVH/BVH_BinnedBuilder.hxx +++ b/src/BVH/BVH_BinnedBuilder.hxx @@ -43,7 +43,7 @@ struct BVH_Bin //! performance is provided even for 4 - 8 bins (it is only 10-20% lower //! in comparison with optimal settings). Note that multiple threads can //! be used only with thread safe BVH primitive sets. -template +template class BVH_BinnedBuilder : public BVH_QueueBuilder { public: @@ -64,9 +64,9 @@ public: public: //! Creates binned SAH BVH builder. - BVH_BinnedBuilder (const Standard_Integer theLeafNodeSize = 5, - const Standard_Integer theMaxTreeDepth = 32, - const Standard_Boolean theDoMainSplits = 0, + BVH_BinnedBuilder (const Standard_Integer theLeafNodeSize = BVH_Constants_LeafNodeSizeDefault, + const Standard_Integer theMaxTreeDepth = BVH_Constants_MaxTreeDepth, + const Standard_Boolean theDoMainSplits = Standard_False, const Standard_Integer theNumOfThreads = 1) : BVH_QueueBuilder (theLeafNodeSize, theMaxTreeDepth, theNumOfThreads), myUseMainAxis (theDoMainSplits) diff --git a/src/BVH/BVH_Box.hxx b/src/BVH/BVH_Box.hxx index 537b4f7a94..8210aa39ba 100644 --- a/src/BVH/BVH_Box.hxx +++ b/src/BVH/BVH_Box.hxx @@ -16,6 +16,7 @@ #ifndef _BVH_Box_Header #define _BVH_Box_Header +#include #include #include diff --git a/src/BVH/BVH_Builder.hxx b/src/BVH/BVH_Builder.hxx index 571767e791..a45d5996a2 100644 --- a/src/BVH/BVH_Builder.hxx +++ b/src/BVH/BVH_Builder.hxx @@ -19,30 +19,43 @@ #include #include -namespace BVH +//! A non-template class for using as base for BVH_Builder +//! (just to have a named base class). +class BVH_BuilderTransient : public Standard_Transient { - //! Minimum node size to split. - const Standard_Real THE_NODE_MIN_SIZE = 1e-5; -} + DEFINE_STANDARD_RTTIEXT(BVH_BuilderTransient, Standard_Transient) +public: + + //! Returns the maximum depth of constructed BVH. + Standard_Integer MaxTreeDepth() const { return myMaxTreeDepth; } + + //! Returns the maximum number of sub-elements in the leaf. + Standard_Integer LeafNodeSize() const { return myLeafNodeSize; } + +protected: + + //! Creates new abstract BVH builder. + BVH_BuilderTransient (const Standard_Integer theLeafNodeSize, + const Standard_Integer theMaxTreeDepth) + : myMaxTreeDepth (theMaxTreeDepth), + myLeafNodeSize (theLeafNodeSize) {} + +protected: + + Standard_Integer myMaxTreeDepth; //!< Maximum depth of constructed BVH + Standard_Integer myLeafNodeSize; //!< Maximum number of objects per leaf + +}; //! Performs construction of BVH tree using bounding //! boxes (AABBs) of abstract objects. //! \tparam T Numeric data type //! \tparam N Vector dimension template -class BVH_Builder +class BVH_Builder : public BVH_BuilderTransient { public: - //! Creates new abstract BVH builder. - BVH_Builder (const Standard_Integer theLeafNodeSize, - const Standard_Integer theMaxTreeDepth) - : myMaxTreeDepth (theMaxTreeDepth), - myLeafNodeSize (theLeafNodeSize) {} - - //! Releases resources of BVH builder. - virtual ~BVH_Builder() {} - //! Builds BVH using specific algorithm. virtual void Build (BVH_Set* theSet, BVH_Tree* theBVH, @@ -50,6 +63,11 @@ public: protected: + //! Creates new abstract BVH builder. + BVH_Builder (const Standard_Integer theLeafNodeSize, + const Standard_Integer theMaxTreeDepth) + : BVH_BuilderTransient (theLeafNodeSize, theMaxTreeDepth) {} + //! Updates depth of constructed BVH tree. void updateDepth (BVH_Tree* theBVH, const Standard_Integer theLevel) const @@ -60,11 +78,6 @@ protected: } } -protected: - - Standard_Integer myMaxTreeDepth; //!< Maximum depth of constructed BVH - Standard_Integer myLeafNodeSize; //!< Maximum number of objects per leaf - }; #endif // _BVH_Builder_Header diff --git a/src/BVH/BVH_Constants.hxx b/src/BVH/BVH_Constants.hxx new file mode 100644 index 0000000000..db5e1707c6 --- /dev/null +++ b/src/BVH/BVH_Constants.hxx @@ -0,0 +1,45 @@ +// Copyright (c) 2017 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +#ifndef _BVH_Constants_Header +#define _BVH_Constants_Header + +enum +{ + //! The optimal tree depth. + //! Should be in sync with maximum stack size while traversing the tree - don't pass the trees of greater depth to OCCT algorithms! + BVH_Constants_MaxTreeDepth = 32, + + //! Leaf node size optimal for complex nodes, + //! e.g. for upper-level BVH trees within multi-level structure (nodes point to another BVH trees). + BVH_Constants_LeafNodeSizeSingle = 1, + //! Average leaf node size (4 primitive per leaf), optimal for average tree nodes. + BVH_Constants_LeafNodeSizeAverage = 4, + //! Default leaf node size (5 primitives per leaf). + BVH_Constants_LeafNodeSizeDefault = 5, + //! Leaf node size (8 primitives per leaf), optimal for small tree nodes (e.g. triangles). + BVH_Constants_LeafNodeSizeSmall = 8, + + //! The optimal number of bins for binned builder. + BVH_Constants_NbBinsOptimal = 32, + //! The maximum number of bins for binned builder (giving the best traversal time at cost of longer tree construction time). + BVH_Constants_NbBinsBest = 48, +}; + +namespace BVH +{ + //! Minimum node size to split. + const double THE_NODE_MIN_SIZE = 1e-5; +} + +#endif // _BVH_Constants_Header diff --git a/src/BVH/BVH_DistanceField.lxx b/src/BVH/BVH_DistanceField.lxx index c3678bcecf..f4ddd367b2 100644 --- a/src/BVH/BVH_DistanceField.lxx +++ b/src/BVH/BVH_DistanceField.lxx @@ -190,14 +190,13 @@ namespace BVH Standard_ASSERT_RETURN (aTriangulation != NULL, "Error: Unsupported BVH object (non triangulation)", aMinDistance); - const NCollection_Handle >& aBVH = aTriangulation->BVH(); - + const opencascade::handle >& aBVH = aTriangulation->BVH(); if (aBVH.IsNull()) { return Standard_False; } - std::pair aStack[32]; + std::pair aStack[BVH_Constants_MaxTreeDepth]; Standard_Integer aHead = -1; Standard_Integer aNode = 0; // root node @@ -318,7 +317,7 @@ namespace BVH return Standard_False; } - std::pair aStack[32]; + std::pair aStack[BVH_Constants_MaxTreeDepth]; Standard_Integer aHead = -1; Standard_Integer aNode = 0; // root node diff --git a/src/BVH/BVH_Geometry.hxx b/src/BVH/BVH_Geometry.hxx index 8cf2162df6..b7d80641d9 100644 --- a/src/BVH/BVH_Geometry.hxx +++ b/src/BVH/BVH_Geometry.hxx @@ -32,10 +32,20 @@ public: //! Creates uninitialized BVH geometry. BVH_Geometry() : myIsDirty (Standard_False), - myBVH (new BVH_Tree()) + myBVH (new BVH_Tree()), + // set default builder - binned SAH split + myBuilder (new BVH_BinnedBuilder (BVH_Constants_LeafNodeSizeSingle)) { - // Set default builder - binned SAH split - myBuilder = new BVH_BinnedBuilder (1 /* primitive per leaf */); + // + } + + //! Creates uninitialized BVH geometry. + BVH_Geometry (const opencascade::handle >& theBuilder) + : myIsDirty (Standard_False), + myBVH (new BVH_Tree()), + myBuilder (theBuilder) + { + // } //! Releases resources of BVH geometry. @@ -64,7 +74,7 @@ public: } //! Returns BVH tree (and builds it if necessary). - virtual const NCollection_Handle >& BVH() + virtual const opencascade::handle >& BVH() { if (myIsDirty) { @@ -74,10 +84,10 @@ public: } //! Returns the method (builder) used to construct BVH. - virtual const NCollection_Handle >& Builder() const { return myBuilder; } + virtual const opencascade::handle >& Builder() const { return myBuilder; } //! Sets the method (builder) used to construct BVH. - virtual void SetBuilder (const NCollection_Handle >& theBuilder) { myBuilder = theBuilder; } + virtual void SetBuilder (const opencascade::handle >& theBuilder) { myBuilder = theBuilder; } protected: @@ -93,9 +103,9 @@ protected: protected: - Standard_Boolean myIsDirty; //!< Is geometry state outdated? - NCollection_Handle > myBVH; //!< Constructed hight-level BVH - NCollection_Handle > myBuilder; //!< Builder for hight-level BVH + Standard_Boolean myIsDirty; //!< Is geometry state outdated? + opencascade::handle > myBVH; //!< Constructed hight-level BVH + opencascade::handle > myBuilder; //!< Builder for hight-level BVH mutable BVH_Box myBox; //!< Cached bounding box of geometric objects diff --git a/src/BVH/BVH_LinearBuilder.hxx b/src/BVH/BVH_LinearBuilder.hxx index 3dd7c4fb49..c2aee79389 100644 --- a/src/BVH/BVH_LinearBuilder.hxx +++ b/src/BVH/BVH_LinearBuilder.hxx @@ -39,8 +39,8 @@ public: public: //! Creates binned LBVH builder. - BVH_LinearBuilder (const Standard_Integer theLeafNodeSize = 5, - const Standard_Integer theMaxTreeDepth = 32); + BVH_LinearBuilder (const Standard_Integer theLeafNodeSize = BVH_Constants_LeafNodeSizeDefault, + const Standard_Integer theMaxTreeDepth = BVH_Constants_MaxTreeDepth); //! Releases resources of LBVH builder. virtual ~BVH_LinearBuilder(); diff --git a/src/BVH/BVH_Object.hxx b/src/BVH/BVH_Object.hxx index 0a265ff2cc..22b317f7af 100644 --- a/src/BVH/BVH_Object.hxx +++ b/src/BVH/BVH_Object.hxx @@ -19,18 +19,44 @@ #include #include -#include +//! A non-template class for using as base for BVH_Object +//! (just to have a named base class). +class BVH_ObjectTransient : public Standard_Transient +{ + DEFINE_STANDARD_RTTIEXT(BVH_ObjectTransient, Standard_Transient) +public: + + //! Returns properties of the geometric object. + virtual const Handle(BVH_Properties)& Properties() const { return myProperties; } + + //! Sets properties of the geometric object. + virtual void SetProperties (const Handle(BVH_Properties)& theProperties) { myProperties = theProperties; } + + //! Marks object state as outdated (needs BVH rebuilding). + virtual void MarkDirty() { myIsDirty = Standard_True; } + +protected: + + //! Creates new abstract geometric object. + BVH_ObjectTransient() : myIsDirty (Standard_False) {} + +protected: + + Standard_Boolean myIsDirty; //!< Marks internal object state as outdated + Handle(BVH_Properties) myProperties; //!< Generic properties assigned to the object + +}; //! Abstract geometric object bounded by BVH box. //! \tparam T Numeric data type //! \tparam N Vector dimension template -class BVH_Object +class BVH_Object : public BVH_ObjectTransient { public: //! Creates new abstract geometric object. - BVH_Object() : myIsDirty (Standard_False) {} + BVH_Object() {} //! Releases resources of geometric object. virtual ~BVH_Object() = 0; @@ -40,20 +66,6 @@ public: //! Returns AABB of the geometric object. virtual BVH_Box Box() const = 0; - //! Returns properties of the geometric object. - virtual const NCollection_Handle& Properties() const { return myProperties; } - - //! Sets properties of the geometric object. - virtual void SetProperties (const NCollection_Handle& theProperties) { myProperties = theProperties; } - - //! Marks object state as outdated (needs BVH rebuilding). - virtual void MarkDirty() { myIsDirty = Standard_True; } - -protected: - - Standard_Boolean myIsDirty; //!< Marks internal object state as outdated - NCollection_Handle myProperties; //!< Generic properties assigned to the object - }; // ======================================================================= diff --git a/src/BVH/BVH_ObjectSet.hxx b/src/BVH/BVH_ObjectSet.hxx index 9cbd9ae791..9a89fcf1e5 100644 --- a/src/BVH/BVH_ObjectSet.hxx +++ b/src/BVH/BVH_ObjectSet.hxx @@ -28,7 +28,7 @@ class BVH_ObjectSet : public BVH_Set public: //! Type of array of geometric objects. - typedef NCollection_Vector > > BVH_ObjectList; + typedef NCollection_Vector > > BVH_ObjectList; public: diff --git a/src/BVH/BVH_PrimitiveSet.hxx b/src/BVH/BVH_PrimitiveSet.hxx index 3ed9b577d1..728d94647d 100644 --- a/src/BVH/BVH_PrimitiveSet.hxx +++ b/src/BVH/BVH_PrimitiveSet.hxx @@ -36,14 +36,23 @@ protected: using BVH_Set::Box; public: - static const Standard_Integer MaxTreeDepth = 32; + static const Standard_Integer MaxTreeDepth = BVH_Constants_MaxTreeDepth; //! Creates set of abstract primitives. BVH_PrimitiveSet() - : myBVH (new BVH_Tree()) + : myBVH (new BVH_Tree()), + // set default builder - binned SAH split + myBuilder (new BVH_BinnedBuilder (BVH_Constants_LeafNodeSizeDefault, BVH_Constants_MaxTreeDepth)) { - // Set default builder - binned SAH split - myBuilder = new BVH_BinnedBuilder (5, MaxTreeDepth); + // + } + + //! Creates set of abstract primitives. + BVH_PrimitiveSet (const opencascade::handle >& theBuilder) + : myBVH (new BVH_Tree()), + myBuilder (theBuilder) + { + // } //! Releases resources of set of abstract primitives. @@ -66,7 +75,7 @@ public: } //! Returns BVH tree (and builds it if necessary). - virtual const NCollection_Handle >& BVH() + virtual const opencascade::handle >& BVH() { if (BVH_Object::myIsDirty) { @@ -76,10 +85,10 @@ public: } //! Returns the method (builder) used to construct BVH. - virtual const NCollection_Handle >& Builder() const { return myBuilder; } + virtual const opencascade::handle >& Builder() const { return myBuilder; } //! Sets the method (builder) used to construct BVH. - virtual void SetBuilder (const NCollection_Handle >& theBuilder) { myBuilder = theBuilder; } + virtual void SetBuilder (const opencascade::handle >& theBuilder) { myBuilder = theBuilder; } protected: @@ -95,8 +104,8 @@ protected: protected: - NCollection_Handle > myBVH; //!< Constructed bottom-level BVH - NCollection_Handle > myBuilder; //!< Builder for bottom-level BVH + opencascade::handle > myBVH; //!< Constructed bottom-level BVH + opencascade::handle > myBuilder; //!< Builder for bottom-level BVH mutable BVH_Box myBox; //!< Cached bounding box of geometric primitives diff --git a/src/BVH/BVH_PrimitiveSet3d.hxx b/src/BVH/BVH_PrimitiveSet3d.hxx new file mode 100644 index 0000000000..5623808440 --- /dev/null +++ b/src/BVH/BVH_PrimitiveSet3d.hxx @@ -0,0 +1,21 @@ +// Copyright (c) 2017 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +#ifndef _BVH_PrimitiveSet3d_Header +#define _BVH_PrimitiveSet3d_Header + +#include + +typedef BVH_PrimitiveSet BVH_PrimitiveSet3d; + +#endif // _BVH_PrimitiveSet3d_Header diff --git a/src/BVH/BVH_Properties.cxx b/src/BVH/BVH_Properties.cxx index 33d7a2c45d..ff84b40dbf 100644 --- a/src/BVH/BVH_Properties.cxx +++ b/src/BVH/BVH_Properties.cxx @@ -15,8 +15,13 @@ #include -//! Abstract properties of geometric object. +IMPLEMENT_STANDARD_RTTIEXT(BVH_Properties, Standard_Transient) + +// ======================================================================= +// function : ~BVH_Properties +// purpose : +// ======================================================================= BVH_Properties::~BVH_Properties() { // -} \ No newline at end of file +} diff --git a/src/BVH/BVH_Properties.hxx b/src/BVH/BVH_Properties.hxx index 9a0e9238e9..2c09d976a0 100644 --- a/src/BVH/BVH_Properties.hxx +++ b/src/BVH/BVH_Properties.hxx @@ -21,8 +21,9 @@ #include //! Abstract properties of geometric object. -class BVH_Properties +class BVH_Properties : public Standard_Transient { + DEFINE_STANDARD_RTTIEXT(BVH_Properties, Standard_Transient) public: //! Releases resources of object properties. diff --git a/src/BVH/BVH_RadixSorter.hxx b/src/BVH/BVH_RadixSorter.hxx index a3cec78547..83acb45e65 100644 --- a/src/BVH/BVH_RadixSorter.hxx +++ b/src/BVH/BVH_RadixSorter.hxx @@ -18,8 +18,8 @@ #include #include -#include #include +#include #include @@ -67,7 +67,7 @@ protected: BVH_Box myBox; //! Morton codes assigned to BVH primitives. - NCollection_Handle > myEncodedLinks; + Handle(NCollection_Shared >) myEncodedLinks; }; @@ -186,7 +186,7 @@ void BVH_RadixSorter::Perform (BVH_Set* theSet, const Standard_Integ const T aReverseSizeY = static_cast (aDimensionY) / Max (static_cast (BVH::THE_NODE_MIN_SIZE), aSceneMax.y() - aSceneMin.y()); const T aReverseSizeZ = static_cast (aDimensionZ) / Max (static_cast (BVH::THE_NODE_MIN_SIZE), aSceneMax.z() - aSceneMin.z()); - myEncodedLinks = new NCollection_Array1 (theStart, theFinal); + myEncodedLinks = new NCollection_Shared >(theStart, theFinal); // Step 1 -- Assign Morton code to each primitive for (Standard_Integer aPrimIdx = theStart; aPrimIdx <= theFinal; ++aPrimIdx) diff --git a/src/BVH/BVH_SpatialMedianBuilder.hxx b/src/BVH/BVH_SpatialMedianBuilder.hxx index 503c35720e..57b3f54eac 100644 --- a/src/BVH/BVH_SpatialMedianBuilder.hxx +++ b/src/BVH/BVH_SpatialMedianBuilder.hxx @@ -26,8 +26,8 @@ class BVH_SpatialMedianBuilder : public BVH_BinnedBuilder public: //! Creates spatial median split builder. - BVH_SpatialMedianBuilder (const Standard_Integer theLeafNodeSize = 5, - const Standard_Integer theMaxTreeDepth = 32, + BVH_SpatialMedianBuilder (const Standard_Integer theLeafNodeSize = BVH_Constants_LeafNodeSizeDefault, + const Standard_Integer theMaxTreeDepth = BVH_Constants_MaxTreeDepth, const Standard_Boolean theToUseMainAxis = Standard_False) : BVH_BinnedBuilder (theLeafNodeSize, theMaxTreeDepth, diff --git a/src/BVH/BVH_SweepPlaneBuilder.hxx b/src/BVH/BVH_SweepPlaneBuilder.hxx index 63921a8abe..4b4f3a0a92 100644 --- a/src/BVH/BVH_SweepPlaneBuilder.hxx +++ b/src/BVH/BVH_SweepPlaneBuilder.hxx @@ -27,8 +27,8 @@ class BVH_SweepPlaneBuilder : public BVH_QueueBuilder public: //! Creates sweep plane SAH BVH builder. - BVH_SweepPlaneBuilder (const Standard_Integer theLeafNodeSize = 5, - const Standard_Integer theMaxTreeDepth = 32, + BVH_SweepPlaneBuilder (const Standard_Integer theLeafNodeSize = BVH_Constants_LeafNodeSizeDefault, + const Standard_Integer theMaxTreeDepth = BVH_Constants_MaxTreeDepth, const Standard_Integer theNumOfThreads = 1) : BVH_QueueBuilder (theLeafNodeSize, theMaxTreeDepth, diff --git a/src/BVH/BVH_Tree.hxx b/src/BVH/BVH_Tree.hxx index 076b9f02ed..a223d14b30 100644 --- a/src/BVH/BVH_Tree.hxx +++ b/src/BVH/BVH_Tree.hxx @@ -20,6 +20,15 @@ template class BVH_Builder; +//! A non-template class for using as base for BVH_TreeBase +//! (just to have a named base class). +class BVH_TreeBaseTransient : public Standard_Transient +{ + DEFINE_STANDARD_RTTIEXT(BVH_TreeBaseTransient, Standard_Transient) +protected: + BVH_TreeBaseTransient() {} +}; + //! Stores parameters of bounding volume hierarchy (BVH). //! Bounding volume hierarchy (BVH) organizes geometric objects in //! the tree based on spatial relationships. Each node in the tree @@ -29,7 +38,7 @@ template class BVH_Builder; //! such as collision detection, ray-tracing, searching of nearest //! objects, and view frustum culling. template -class BVH_TreeBase +class BVH_TreeBase : public BVH_TreeBaseTransient { friend class BVH_Builder; diff --git a/src/BVH/BVH_Triangulation.hxx b/src/BVH/BVH_Triangulation.hxx index 9a33e03533..d4e3dc3d20 100644 --- a/src/BVH/BVH_Triangulation.hxx +++ b/src/BVH/BVH_Triangulation.hxx @@ -33,6 +33,13 @@ public: //! Creates empty triangulation. BVH_Triangulation() {} + //! Creates empty triangulation. + BVH_Triangulation (const opencascade::handle >& theBuilder) + : BVH_PrimitiveSet (theBuilder) + { + // + } + //! Releases resources of triangulation. virtual ~BVH_Triangulation() {} diff --git a/src/BVH/BVH_Types.hxx b/src/BVH/BVH_Types.hxx index a556672efd..70dc9bfafd 100644 --- a/src/BVH/BVH_Types.hxx +++ b/src/BVH/BVH_Types.hxx @@ -25,6 +25,7 @@ #include #include #include +#include // GCC supports shrink function only in C++11 mode #if defined(_BVH_USE_STD_VECTOR_) && defined(_MSC_VER) && !defined(__INTEL_COMPILER) diff --git a/src/BVH/FILES b/src/BVH/FILES index 0bab64bd55..a563d1f113 100644 --- a/src/BVH/FILES +++ b/src/BVH/FILES @@ -6,6 +6,7 @@ BVH_BuildQueue.hxx BVH_BuildQueue.cxx BVH_BuildThread.hxx BVH_BuildThread.cxx +BVH_Constants.hxx BVH_DistanceField.hxx BVH_DistanceField.lxx BVH_Geometry.hxx @@ -13,6 +14,7 @@ BVH_LinearBuilder.hxx BVH_Object.hxx BVH_ObjectSet.hxx BVH_PrimitiveSet.hxx +BVH_PrimitiveSet3d.hxx BVH_Properties.cxx BVH_Properties.hxx BVH_QueueBuilder.hxx diff --git a/src/OpenGl/OpenGl_BVHClipPrimitiveSet.cxx b/src/OpenGl/OpenGl_BVHClipPrimitiveSet.cxx index 0339b5f8cd..1fe7c08cc7 100644 --- a/src/OpenGl/OpenGl_BVHClipPrimitiveSet.cxx +++ b/src/OpenGl/OpenGl_BVHClipPrimitiveSet.cxx @@ -18,13 +18,15 @@ #include #include +IMPLEMENT_STANDARD_RTTIEXT(OpenGl_BVHClipPrimitiveSet, BVH_PrimitiveSet3d) + // ======================================================================= // function : OpenGl_BVHClipPrimitiveSet // purpose : // ======================================================================= OpenGl_BVHClipPrimitiveSet::OpenGl_BVHClipPrimitiveSet() { - myBuilder = new BVH_BinnedBuilder (1, 32); + myBuilder = new BVH_BinnedBuilder (BVH_Constants_LeafNodeSizeSingle, BVH_Constants_MaxTreeDepth); } // ======================================================================= diff --git a/src/OpenGl/OpenGl_BVHClipPrimitiveSet.hxx b/src/OpenGl/OpenGl_BVHClipPrimitiveSet.hxx index 03a710f84e..64cd4ca16a 100644 --- a/src/OpenGl/OpenGl_BVHClipPrimitiveSet.hxx +++ b/src/OpenGl/OpenGl_BVHClipPrimitiveSet.hxx @@ -16,18 +16,19 @@ #ifndef _OpenGl_BVHClipPrimitiveSet_HeaderFile #define _OpenGl_BVHClipPrimitiveSet_HeaderFile -#include +#include #include #include #include //! Set of OpenGl_Structures for building BVH tree. -class OpenGl_BVHClipPrimitiveSet : public BVH_PrimitiveSet +class OpenGl_BVHClipPrimitiveSet : public BVH_PrimitiveSet3d { + DEFINE_STANDARD_RTTIEXT(OpenGl_BVHClipPrimitiveSet, BVH_PrimitiveSet3d) protected: - using BVH_PrimitiveSet::Box; + using BVH_PrimitiveSet3d::Box; public: diff --git a/src/OpenGl/OpenGl_BVHClipPrimitiveTrsfPersSet.cxx b/src/OpenGl/OpenGl_BVHClipPrimitiveTrsfPersSet.cxx index 1798a54019..82576bf861 100644 --- a/src/OpenGl/OpenGl_BVHClipPrimitiveTrsfPersSet.cxx +++ b/src/OpenGl/OpenGl_BVHClipPrimitiveTrsfPersSet.cxx @@ -14,17 +14,17 @@ // commercial license or contractual agreement. #include -#include // ======================================================================= // function : OpenGl_BVHClipPrimitiveTrsfPersSet // purpose : // ======================================================================= -OpenGl_BVHClipPrimitiveTrsfPersSet::OpenGl_BVHClipPrimitiveTrsfPersSet() +OpenGl_BVHClipPrimitiveTrsfPersSet::OpenGl_BVHClipPrimitiveTrsfPersSet (const Handle(Select3D_BVHBuilder3d)& theBuilder) : myIsDirty (Standard_False), - myBVH (new BVH_Tree()) + myBVH (new BVH_Tree()), + myBuilder (theBuilder) { - myBuilder = new BVH_LinearBuilder (1, 32); + // } // ======================================================================= @@ -131,7 +131,7 @@ const OpenGl_Structure* OpenGl_BVHClipPrimitiveTrsfPersSet::GetStructureById (St // function : BVH // purpose : //======================================================================= -const NCollection_Handle >& +const opencascade::handle >& OpenGl_BVHClipPrimitiveTrsfPersSet::BVH (const Handle(Graphic3d_Camera)& theCamera, const OpenGl_Mat4d& theProjectionMatrix, const OpenGl_Mat4d& theWorldViewMatrix, diff --git a/src/OpenGl/OpenGl_BVHClipPrimitiveTrsfPersSet.hxx b/src/OpenGl/OpenGl_BVHClipPrimitiveTrsfPersSet.hxx index 4ddf92cbc8..aa73e3031f 100644 --- a/src/OpenGl/OpenGl_BVHClipPrimitiveTrsfPersSet.hxx +++ b/src/OpenGl/OpenGl_BVHClipPrimitiveTrsfPersSet.hxx @@ -16,7 +16,6 @@ #ifndef _OpenGl_BVHClipPrimitiveTrsfPersSet_HeaderFile #define _OpenGl_BVHClipPrimitiveTrsfPersSet_HeaderFile -#include #include #include #include @@ -25,6 +24,7 @@ #include #include #include +#include //! Set of transformation persistent OpenGl_Structure for building BVH tree. //! Provides built-in mechanism to invalidate tree when world view projection state changes. @@ -39,7 +39,7 @@ private: public: //! Creates an empty primitive set for BVH clipping. - OpenGl_BVHClipPrimitiveTrsfPersSet(); + OpenGl_BVHClipPrimitiveTrsfPersSet (const Handle(Select3D_BVHBuilder3d)& theBuilder); //! Returns total number of structures. virtual Standard_Integer Size() const Standard_OVERRIDE; @@ -76,12 +76,18 @@ public: } //! Returns BVH tree for the given world view projection (builds it if necessary). - const NCollection_Handle >& BVH (const Handle(Graphic3d_Camera)& theCamera, - const OpenGl_Mat4d& theProjectionMatrix, - const OpenGl_Mat4d& theWorldViewMatrix, - const Standard_Integer theViewportWidth, - const Standard_Integer theViewportHeight, - const Graphic3d_WorldViewProjState& theWVPState); + const opencascade::handle >& BVH (const Handle(Graphic3d_Camera)& theCamera, + const OpenGl_Mat4d& theProjectionMatrix, + const OpenGl_Mat4d& theWorldViewMatrix, + const Standard_Integer theViewportWidth, + const Standard_Integer theViewportHeight, + const Graphic3d_WorldViewProjState& theWVPState); + + //! Returns builder for bottom-level BVH. + const Handle(Select3D_BVHBuilder3d)& Builder() const { return myBuilder; } + + //! Assigns builder for bottom-level BVH. + void SetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) { myBuilder = theBuilder; } private: @@ -89,10 +95,10 @@ private: Standard_Boolean myIsDirty; //! Constructed bottom-level BVH. - NCollection_Handle > myBVH; + opencascade::handle > myBVH; //! Builder for bottom-level BVH. - NCollection_Handle > myBuilder; + Handle(Select3D_BVHBuilder3d) myBuilder; //! Indexed map of structures. NCollection_IndexedMap myStructs; diff --git a/src/OpenGl/OpenGl_Layer.cxx b/src/OpenGl/OpenGl_Layer.cxx index d1487d1194..1c82e6bf4e 100644 --- a/src/OpenGl/OpenGl_Layer.cxx +++ b/src/OpenGl/OpenGl_Layer.cxx @@ -22,13 +22,17 @@ #include #include +IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Layer, Standard_Transient) + // ======================================================================= // function : OpenGl_Layer // purpose : // ======================================================================= -OpenGl_Layer::OpenGl_Layer (const Standard_Integer theNbPriorities) +OpenGl_Layer::OpenGl_Layer (const Standard_Integer theNbPriorities, + const Handle(Select3D_BVHBuilder3d)& theBuilder) : myArray (0, theNbPriorities - 1), myNbStructures (0), + myBVHPrimitivesTrsfPers (theBuilder), myBVHIsLeftChildQueuedFirst (Standard_True), myIsBVHPrimitivesNeedsReset (Standard_False) { @@ -521,8 +525,7 @@ void OpenGl_Layer::traverse (OpenGl_BVHTreeSelector& theSelector) const theSelector.CacheClipPtsProjections(); - NCollection_Handle > aBVHTree; - + opencascade::handle > aBVHTree; for (Standard_Integer aBVHTreeIdx = 0; aBVHTreeIdx < 2; ++aBVHTreeIdx) { const Standard_Boolean isTrsfPers = aBVHTreeIdx == 1; @@ -555,7 +558,7 @@ void OpenGl_Layer::traverse (OpenGl_BVHTreeSelector& theSelector) const continue; } - Standard_Integer aStack[32]; + Standard_Integer aStack[BVH_Constants_MaxTreeDepth]; Standard_Integer aHead = -1; for (;;) { diff --git a/src/OpenGl/OpenGl_Layer.hxx b/src/OpenGl/OpenGl_Layer.hxx index 57edf25f96..f04a3a8766 100644 --- a/src/OpenGl/OpenGl_Layer.hxx +++ b/src/OpenGl/OpenGl_Layer.hxx @@ -41,16 +41,24 @@ typedef NCollection_IndexedMap OpenGl_IndexedMapOfStruc typedef NCollection_Array1 OpenGl_ArrayOfIndexedMapOfStructure; //! Presentations list sorted within priorities. -class OpenGl_Layer +class OpenGl_Layer : public Standard_Transient { + DEFINE_STANDARD_RTTIEXT(OpenGl_Layer, Standard_Transient) public: //! Initializes associated priority list and layer properties - OpenGl_Layer (const Standard_Integer theNbPriorities = 11); + OpenGl_Layer (const Standard_Integer theNbPriorities, + const Handle(Select3D_BVHBuilder3d)& theBuilder); //! Destructor. virtual ~OpenGl_Layer(); + //! Returns BVH tree builder for frustom culling. + const Handle(Select3D_BVHBuilder3d)& FrustumCullingBVHBuilder() const { return myBVHPrimitivesTrsfPers.Builder(); } + + //! Assigns BVH tree builder for frustom culling. + void SetFrustumCullingBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) { myBVHPrimitivesTrsfPers.SetBuilder (theBuilder); } + //! Return true if layer was marked with immediate flag. Standard_Boolean IsImmediate() const { return myLayerSettings.IsImmediate(); } diff --git a/src/OpenGl/OpenGl_LayerList.cxx b/src/OpenGl/OpenGl_LayerList.cxx index 40dc81fb21..d7bf035844 100644 --- a/src/OpenGl/OpenGl_LayerList.cxx +++ b/src/OpenGl/OpenGl_LayerList.cxx @@ -15,6 +15,7 @@ #include +#include #include #include #include @@ -31,7 +32,8 @@ //======================================================================= OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities) -: myDefaultLayerIndex (0), +: myBVHBuilder (new BVH_LinearBuilder (BVH_Constants_LeafNodeSizeSingle, BVH_Constants_MaxTreeDepth)), + myDefaultLayerIndex (0), myNbPriorities (theNbPriorities), myNbStructures (0), myImmediateNbStructures (0), @@ -40,19 +42,19 @@ OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities) myRenderTranspFilter (new OpenGl_TransparentFilter()) { // insert default priority layers - myLayers.Append (OpenGl_Layer (myNbPriorities)); + myLayers.Append (new OpenGl_Layer (myNbPriorities, myBVHBuilder)); myLayerIds.Bind (Graphic3d_ZLayerId_BotOSD, myLayers.Upper()); - myLayers.Append (OpenGl_Layer (myNbPriorities)); + myLayers.Append (new OpenGl_Layer (myNbPriorities, myBVHBuilder)); myLayerIds.Bind (Graphic3d_ZLayerId_Default, myLayers.Upper()); - myLayers.Append (OpenGl_Layer (myNbPriorities)); + myLayers.Append (new OpenGl_Layer (myNbPriorities, myBVHBuilder)); myLayerIds.Bind (Graphic3d_ZLayerId_Top, myLayers.Upper()); - myLayers.Append (OpenGl_Layer (myNbPriorities)); + myLayers.Append (new OpenGl_Layer (myNbPriorities, myBVHBuilder)); myLayerIds.Bind (Graphic3d_ZLayerId_Topmost, myLayers.Upper()); - myLayers.Append (OpenGl_Layer (myNbPriorities)); + myLayers.Append (new OpenGl_Layer (myNbPriorities, myBVHBuilder)); myLayerIds.Bind (Graphic3d_ZLayerId_TopOSD, myLayers.Upper()); myDefaultLayerIndex = myLayerIds.Find (Graphic3d_ZLayerId_Default); @@ -69,6 +71,19 @@ OpenGl_LayerList::~OpenGl_LayerList() { } +//======================================================================= +//function : SetFrustumCullingBVHBuilder +//purpose : +//======================================================================= +void OpenGl_LayerList::SetFrustumCullingBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) +{ + myBVHBuilder = theBuilder; + for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next()) + { + anIts.ChangeValue()->SetFrustumCullingBVHBuilder (theBuilder); + } +} + //======================================================================= //function : AddLayer //purpose : @@ -82,7 +97,7 @@ void OpenGl_LayerList::AddLayer (const Graphic3d_ZLayerId theLayerId) } // add the new layer - myLayers.Append (OpenGl_Layer (myNbPriorities)); + myLayers.Append (new OpenGl_Layer (myNbPriorities, myBVHBuilder)); myLayerIds.Bind (theLayerId, myLayers.Length()); myTransparentToProcess.Allocate (myLayers.Length()); @@ -94,7 +109,7 @@ void OpenGl_LayerList::AddLayer (const Graphic3d_ZLayerId theLayerId) //======================================================================= OpenGl_Layer& OpenGl_LayerList::Layer (const Graphic3d_ZLayerId theLayerId) { - return myLayers.ChangeValue (myLayerIds.Find (theLayerId)); + return *myLayers.ChangeValue (myLayerIds.Find (theLayerId)); } //======================================================================= @@ -103,7 +118,7 @@ OpenGl_Layer& OpenGl_LayerList::Layer (const Graphic3d_ZLayerId theLayerId) //======================================================================= const OpenGl_Layer& OpenGl_LayerList::Layer (const Graphic3d_ZLayerId theLayerId) const { - return myLayers.Value (myLayerIds.Find (theLayerId)); + return *myLayers.Value (myLayerIds.Find (theLayerId)); } //======================================================================= @@ -122,8 +137,10 @@ void OpenGl_LayerList::RemoveLayer (const Graphic3d_ZLayerId theLayerId) const Standard_Integer aRemovePos = myLayerIds.Find (theLayerId); // move all displayed structures to first layer - const OpenGl_Layer& aLayerToMove = myLayers.Value (aRemovePos); - myLayers.ChangeFirst().Append (aLayerToMove); + { + const OpenGl_Layer& aLayerToMove = *myLayers.Value (aRemovePos); + myLayers.ChangeFirst()->Append (aLayerToMove); + } // remove layer myLayers.Remove (aRemovePos); @@ -157,7 +174,7 @@ void OpenGl_LayerList::AddStructure (const OpenGl_Structure* theStruct, Standard_Integer aSeqPos = myLayers.Lower(); myLayerIds.Find (theLayerId, aSeqPos); - OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos); + OpenGl_Layer& aLayer = *myLayers.ChangeValue (aSeqPos); aLayer.Add (theStruct, thePriority, isForChangePriority); ++myNbStructures; if (aLayer.IsImmediate()) @@ -182,7 +199,7 @@ void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure* theStructure) Standard_Integer aSeqPos = myLayers.Lower(); myLayerIds.Find (aLayerId, aSeqPos); - OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos); + OpenGl_Layer& aLayer = *myLayers.ChangeValue (aSeqPos); Standard_Integer aPriority = -1; // remove structure from associated list @@ -209,7 +226,7 @@ void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure* theStructure) Standard_Integer aSeqId = 1; for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId) { - OpenGl_Layer& aLayerEx = anIts.ChangeValue(); + OpenGl_Layer& aLayerEx = *anIts.ChangeValue(); if (aSeqPos == aSeqId) { continue; @@ -241,7 +258,7 @@ void OpenGl_LayerList::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId) { Standard_Integer aSeqPos = myLayers.Lower(); myLayerIds.Find (theLayerId, aSeqPos); - OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos); + OpenGl_Layer& aLayer = *myLayers.ChangeValue (aSeqPos); aLayer.InvalidateBVHData(); } @@ -256,7 +273,7 @@ void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure* theStructure, { Standard_Integer aSeqPos = myLayers.Lower(); myLayerIds.Find (theOldLayerId, aSeqPos); - OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos); + OpenGl_Layer& aLayer = *myLayers.ChangeValue (aSeqPos); Standard_Integer aPriority = -1; // take priority and remove structure from list found by @@ -291,7 +308,7 @@ void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure* theStructure, } // try to remove structure and get priority value from this layer - OpenGl_Layer& aLayerEx = anIts.ChangeValue(); + OpenGl_Layer& aLayerEx = *anIts.ChangeValue(); if (aLayerEx.Remove (theStructure, aPriority, Standard_True)) { if (aSeqId == myDefaultLayerIndex @@ -324,7 +341,7 @@ void OpenGl_LayerList::ChangePriority (const OpenGl_Structure* theStructure, { Standard_Integer aSeqPos = myLayers.Lower(); myLayerIds.Find (theLayerId, aSeqPos); - OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos); + OpenGl_Layer& aLayer = *myLayers.ChangeValue (aSeqPos); Standard_Integer anOldPriority = -1; if (aLayer.Remove (theStructure, anOldPriority, Standard_True)) @@ -347,7 +364,7 @@ void OpenGl_LayerList::ChangePriority (const OpenGl_Structure* theStructure, continue; } - OpenGl_Layer& aLayerEx = anIts.ChangeValue(); + OpenGl_Layer& aLayerEx = *anIts.ChangeValue(); if (aLayerEx.Remove (theStructure, anOldPriority, Standard_True)) { --myNbStructures; @@ -436,7 +453,7 @@ void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace)& theWorkspace, if (aSeqId != myDefaultLayerIndex) continue; } - const OpenGl_Layer& aLayer = aLayerIter.Value(); + const OpenGl_Layer& aLayer = *aLayerIter.Value(); if (aLayer.IsImmediate() != theToDrawImmediate) { continue; diff --git a/src/OpenGl/OpenGl_LayerList.hxx b/src/OpenGl/OpenGl_LayerList.hxx index 25d0ca0801..1e7848cba2 100644 --- a/src/OpenGl/OpenGl_LayerList.hxx +++ b/src/OpenGl/OpenGl_LayerList.hxx @@ -26,7 +26,7 @@ class OpenGl_Structure; -typedef NCollection_Sequence OpenGl_SequenceOfLayers; +typedef NCollection_Sequence OpenGl_SequenceOfLayers; typedef NCollection_DataMap OpenGl_LayerSeqIds; //! Class defining the list of layers. @@ -35,7 +35,7 @@ class OpenGl_LayerList public: //! Constructor - OpenGl_LayerList (const Standard_Integer theNbPriorities = 11); + OpenGl_LayerList (const Standard_Integer theNbPriorities); //! Destructor virtual ~OpenGl_LayerList(); @@ -108,6 +108,12 @@ public: //! Returns structure modification state (for ray-tracing). Standard_Size ModificationStateOfRaytracable() const { return myModifStateOfRaytraceable; } + //! Returns BVH tree builder for frustom culling. + const Handle(Select3D_BVHBuilder3d)& FrustumCullingBVHBuilder() const { return myBVHBuilder; } + + //! Assigns BVH tree builder for frustom culling. + void SetFrustumCullingBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder); + protected: //! Filter of TKOpenGl elements for processing only shading geometry and @@ -239,6 +245,7 @@ protected: // number of structures temporary put to default layer OpenGl_SequenceOfLayers myLayers; OpenGl_LayerSeqIds myLayerIds; + Handle(Select3D_BVHBuilder3d) myBVHBuilder; //!< BVH tree builder for frustom culling Standard_Integer myDefaultLayerIndex; //!< index of Graphic3d_ZLayerId_Default layer in myLayers sequence Standard_Integer myNbPriorities; diff --git a/src/OpenGl/OpenGl_SceneGeometry.cxx b/src/OpenGl/OpenGl_SceneGeometry.cxx old mode 100755 new mode 100644 index 59a59b2ca3..6f92d70028 --- a/src/OpenGl/OpenGl_SceneGeometry.cxx +++ b/src/OpenGl/OpenGl_SceneGeometry.cxx @@ -31,6 +31,8 @@ namespace static const BVH_Vec4f ZERO_VEC_4F; } +IMPLEMENT_STANDARD_RTTIEXT(OpenGl_TriangleSet, OpenGl_BVHTriangulation3f) + // ======================================================================= // function : OpenGl_RaytraceMaterial // purpose : Creates new default material @@ -166,48 +168,43 @@ Standard_ShortReal OpenGl_TriangleSet::Center ( // ======================================================================= OpenGl_TriangleSet::BVH_BoxNt OpenGl_TriangleSet::Box() const { - const BVH_Transform* aTransform = - dynamic_cast* > (Properties().operator->()); - BVH_BoxNt aBox = BVH_PrimitiveSet::Box(); - - if (aTransform != NULL) + const BVH_Transform* aTransform = dynamic_cast* > (Properties().get()); + if (aTransform == NULL) { - BVH_BoxNt aTransformedBox; - - for (Standard_Integer aX = 0; aX <= 1; ++aX) - { - for (Standard_Integer aY = 0; aY <= 1; ++aY) - { - for (Standard_Integer aZ = 0; aZ <= 1; ++aZ) - { - BVH_Vec4f aCorner = aTransform->Transform() * BVH_Vec4f ( - aX == 0 ? aBox.CornerMin().x() : aBox.CornerMax().x(), - aY == 0 ? aBox.CornerMin().y() : aBox.CornerMax().y(), - aZ == 0 ? aBox.CornerMin().z() : aBox.CornerMax().z(), - 1.f); - - aTransformedBox.Add (aCorner.xyz()); - } - } - } - - return aTransformedBox; + return aBox; } - return aBox; + BVH_BoxNt aTransformedBox; + for (Standard_Integer aX = 0; aX <= 1; ++aX) + { + for (Standard_Integer aY = 0; aY <= 1; ++aY) + { + for (Standard_Integer aZ = 0; aZ <= 1; ++aZ) + { + BVH_Vec4f aCorner = aTransform->Transform() * BVH_Vec4f ( + aX == 0 ? aBox.CornerMin().x() : aBox.CornerMax().x(), + aY == 0 ? aBox.CornerMin().y() : aBox.CornerMax().y(), + aZ == 0 ? aBox.CornerMin().z() : aBox.CornerMax().z(), + 1.f); + + aTransformedBox.Add (aCorner.xyz()); + } + } + } + return aTransformedBox; } // ======================================================================= // function : OpenGl_TriangleSet // purpose : Creates new OpenGL element triangulation // ======================================================================= -OpenGl_TriangleSet::OpenGl_TriangleSet (const Standard_Size theArrayID) -: BVH_Triangulation(), +OpenGl_TriangleSet::OpenGl_TriangleSet (const Standard_Size theArrayID, + const opencascade::handle >& theBuilder) +: BVH_Triangulation (theBuilder), myArrayID (theArrayID) { - myBuilder = new BVH_BinnedBuilder - (4 /* leaf size */, 32 /* max height */, Standard_False, OSD_Parallel::NbLogicalProcessors() + 1 /* threads */); + // } // ======================================================================= diff --git a/src/OpenGl/OpenGl_SceneGeometry.hxx b/src/OpenGl/OpenGl_SceneGeometry.hxx old mode 100755 new mode 100644 index e467030096..d79f6d502c --- a/src/OpenGl/OpenGl_SceneGeometry.hxx +++ b/src/OpenGl/OpenGl_SceneGeometry.hxx @@ -161,11 +161,13 @@ public: }; //! Shared pointer to quad BVH (QBVH) tree. -typedef NCollection_Handle > QuadBvhHandle; +typedef opencascade::handle > QuadBvhHandle; +typedef BVH_Triangulation OpenGl_BVHTriangulation3f; //! Triangulation of single OpenGL primitive array. -class OpenGl_TriangleSet : public BVH_Triangulation +class OpenGl_TriangleSet : public OpenGl_BVHTriangulation3f { + DEFINE_STANDARD_RTTIEXT(OpenGl_TriangleSet, OpenGl_BVHTriangulation3f) public: //! Value of invalid material index to return in case of errors. @@ -174,7 +176,8 @@ public: public: //! Creates new OpenGL element triangulation. - OpenGl_TriangleSet (const Standard_Size theArrayID); + OpenGl_TriangleSet (const Standard_Size theArrayID, + const opencascade::handle >& theBuilder); //! Returns ID of associated primitive array. Standard_Size AssociatedPArrayID() const @@ -203,13 +206,13 @@ public: } //! Returns AABB of primitive set. - BVH_BoxNt Box() const; + virtual BVH_BoxNt Box() const Standard_OVERRIDE; //! Returns AABB of the given object. using BVH_Triangulation::Box; //! Returns centroid position along the given axis. - Standard_ShortReal Center (const Standard_Integer theIndex, const Standard_Integer theAxis) const; + virtual Standard_ShortReal Center (const Standard_Integer theIndex, const Standard_Integer theAxis) const Standard_OVERRIDE; //! Returns quad BVH (QBVH) tree produced from binary BVH. const QuadBvhHandle& QuadBVH(); diff --git a/src/OpenGl/OpenGl_View.cxx b/src/OpenGl/OpenGl_View.cxx index fd181f838e..99fbd51fe6 100644 --- a/src/OpenGl/OpenGl_View.cxx +++ b/src/OpenGl/OpenGl_View.cxx @@ -31,8 +31,11 @@ #include #include #include +#include #include +#include "../Graphic3d/Graphic3d_Structure.pxx" + IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView) #ifdef HAVE_GL2PS @@ -59,6 +62,7 @@ OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr, myBgColor (Quantity_NOC_BLACK), myCamera (new Graphic3d_Camera()), myToShowGradTrihedron (false), + myZLayers (Structure_MAX_PRIORITY - Structure_MIN_PRIORITY + 1), myStateCounter (theCounter), myLastLightSourceState (0, 0), #if !defined(GL_ES_VERSION_2_0) @@ -83,6 +87,12 @@ OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr, myRaytraceInitStatus (OpenGl_RT_NONE), myIsRaytraceDataValid (Standard_False), myIsRaytraceWarnTextures (Standard_False), + myRaytraceBVHBuilder (new BVH_BinnedBuilder (BVH_Constants_LeafNodeSizeAverage, + BVH_Constants_MaxTreeDepth, + Standard_False, + OSD_Parallel::NbLogicalProcessors() + 1)), + myRaytraceSceneRadius (0.0f), + myRaytraceSceneEpsilon (1.0e-6f), myToUpdateEnvironmentMap (Standard_False), myRaytraceLayerListState (0) { diff --git a/src/OpenGl/OpenGl_View.hxx b/src/OpenGl/OpenGl_View.hxx index 9b98aa94c3..4f4a393d69 100644 --- a/src/OpenGl/OpenGl_View.hxx +++ b/src/OpenGl/OpenGl_View.hxx @@ -814,9 +814,9 @@ protected: //! @name methods related to ray-tracing const Handle(OpenGl_Context)& theGlContext); //! Adds OpenGL primitive array to ray-traced scene geometry. - OpenGl_TriangleSet* addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray, - const Standard_Integer theMatID, - const OpenGl_Mat4* theTrans); + Handle(OpenGl_TriangleSet) addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray, + const Standard_Integer theMatID, + const OpenGl_Mat4* theTrans); //! Adds vertex indices from OpenGL primitive array to ray-traced scene geometry. Standard_Boolean addRaytraceVertexIndices (OpenGl_TriangleSet& theSet, @@ -961,6 +961,9 @@ protected: //! @name fields related to ray-tracing //! 3D scene geometry data for ray-tracing. OpenGl_RaytraceGeometry myRaytraceGeometry; + //! Builder for triangle set. + opencascade::handle > myRaytraceBVHBuilder; + //! Compile-time ray-tracing parameters. RaytracingParams myRaytraceParameters; diff --git a/src/OpenGl/OpenGl_View_Raytrace.cxx b/src/OpenGl/OpenGl_View_Raytrace.cxx index 88c7682287..aac555f1ba 100644 --- a/src/OpenGl/OpenGl_View_Raytrace.cxx +++ b/src/OpenGl/OpenGl_View_Raytrace.cxx @@ -525,9 +525,7 @@ Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* if (aSetIter != myArrayToTrianglesMap.end()) { OpenGl_TriangleSet* aSet = aSetIter->second; - - BVH_Transform* aTransform = new BVH_Transform(); - + opencascade::handle > aTransform = new BVH_Transform(); if (!theTrsf.IsNull()) { theTrsf->Trsf().GetMat4 (aMat4); @@ -535,7 +533,6 @@ Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* } aSet->SetProperties (aTransform); - if (aSet->MaterialIndex() != OpenGl_TriangleSet::INVALID_MATERIAL && aSet->MaterialIndex() != aMatID) { aSet->SetMaterialIndex (aMatID); @@ -543,13 +540,9 @@ Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* } else { - NCollection_Handle > aSet = - addRaytracePrimitiveArray (aPrimArray, aMatID, 0); - - if (!aSet.IsNull()) + if (Handle(OpenGl_TriangleSet) aSet = addRaytracePrimitiveArray (aPrimArray, aMatID, 0)) { - BVH_Transform* aTransform = new BVH_Transform; - + opencascade::handle > aTransform = new BVH_Transform(); if (!theTrsf.IsNull()) { theTrsf->Trsf().GetMat4 (aMat4); @@ -557,7 +550,6 @@ Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* } aSet->SetProperties (aTransform); - myRaytraceGeometry.Objects().Append (aSet); } } @@ -573,9 +565,9 @@ Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* // function : addRaytracePrimitiveArray // purpose : Adds OpenGL primitive array to ray-traced scene geometry // ======================================================================= -OpenGl_TriangleSet* OpenGl_View::addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray, - const Standard_Integer theMaterial, - const OpenGl_Mat4* theTransform) +Handle(OpenGl_TriangleSet) OpenGl_View::addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray, + const Standard_Integer theMaterial, + const OpenGl_Mat4* theTransform) { const Handle(Graphic3d_BoundBuffer)& aBounds = theArray->Bounds(); const Handle(Graphic3d_IndexBuffer)& anIndices = theArray->Indices(); @@ -589,11 +581,10 @@ OpenGl_TriangleSet* OpenGl_View::addRaytracePrimitiveArray (const OpenGl_Primiti #endif || anAttribs.IsNull()) { - return NULL; + return Handle(OpenGl_TriangleSet)(); } OpenGl_Mat4 aNormalMatrix; - if (theTransform != NULL) { Standard_ASSERT_RETURN (theTransform->Inverted (aNormalMatrix), @@ -602,14 +593,13 @@ OpenGl_TriangleSet* OpenGl_View::addRaytracePrimitiveArray (const OpenGl_Primiti aNormalMatrix.Transpose(); } - OpenGl_TriangleSet* aSet = new OpenGl_TriangleSet (theArray->GetUID()); + Handle(OpenGl_TriangleSet) aSet = new OpenGl_TriangleSet (theArray->GetUID(), myRaytraceBVHBuilder); { aSet->Vertices.reserve (anAttribs->NbElements); aSet->Normals.reserve (anAttribs->NbElements); aSet->TexCrds.reserve (anAttribs->NbElements); const size_t aVertFrom = aSet->Vertices.size(); - for (Standard_Integer anAttribIter = 0; anAttribIter < anAttribs->NbAttributes; ++anAttribIter) { const Graphic3d_Attribute& anAttrib = anAttribs->Attribute (anAttribIter); @@ -707,8 +697,8 @@ OpenGl_TriangleSet* OpenGl_View::addRaytracePrimitiveArray (const OpenGl_Primiti if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, aBoundStart, *theArray)) { - delete aSet; - return NULL; + aSet.Nullify(); + return Handle(OpenGl_TriangleSet)(); } aBoundStart += aVertNum; @@ -720,8 +710,8 @@ OpenGl_TriangleSet* OpenGl_View::addRaytracePrimitiveArray (const OpenGl_Primiti if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, 0, *theArray)) { - delete aSet; - return NULL; + aSet.Nullify(); + return Handle(OpenGl_TriangleSet)(); } } } @@ -2135,9 +2125,7 @@ Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& OpenGl_TriangleSet* aTriangleSet = dynamic_cast ( myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->()); - const BVH_Transform* aTransform = - dynamic_cast* > (aTriangleSet->Properties().operator->()); - + const BVH_Transform* aTransform = dynamic_cast* > (aTriangleSet->Properties().get()); Standard_ASSERT_RETURN (aTransform != NULL, "OpenGl_TriangleSet does not contain transform", Standard_False); diff --git a/src/Select3D/FILES b/src/Select3D/FILES index 124695382a..43bee8b6ca 100755 --- a/src/Select3D/FILES +++ b/src/Select3D/FILES @@ -1,4 +1,5 @@ Select3D_BndBox3d.hxx +Select3D_BVHBuilder3d.hxx Select3D_BVHIndexBuffer.hxx Select3D_EntitySequence.hxx Select3D_InteriorSensitivePointSet.cxx diff --git a/src/Select3D/Select3D_BVHBuilder3d.hxx b/src/Select3D/Select3D_BVHBuilder3d.hxx new file mode 100644 index 0000000000..254481e14f --- /dev/null +++ b/src/Select3D/Select3D_BVHBuilder3d.hxx @@ -0,0 +1,22 @@ +// Copyright (c) 2017 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +#ifndef _Select3D_BVHBuilder3d_Header +#define _Select3D_BVHBuilder3d_Header + +#include +#include + +typedef BVH_Builder Select3D_BVHBuilder3d; + +#endif // _Select3D_BVHBuilder3d_Header diff --git a/src/Select3D/Select3D_SensitiveSet.cxx b/src/Select3D/Select3D_SensitiveSet.cxx index a6b8a8e146..be810f0050 100644 --- a/src/Select3D/Select3D_SensitiveSet.cxx +++ b/src/Select3D/Select3D_SensitiveSet.cxx @@ -19,6 +19,30 @@ IMPLEMENT_STANDARD_RTTIEXT(Select3D_SensitiveSet,Select3D_SensitiveEntity) +namespace +{ + //! Default BVH tree builder for sensitive set (optimal for large set of small primitives - for not too long construction time). + static Handle(Select3D_BVHBuilder3d) THE_SENS_SET_BUILDER = new BVH_LinearBuilder (BVH_Constants_LeafNodeSizeSmall, BVH_Constants_MaxTreeDepth); +} + +//======================================================================= +// function : DefaultBVHBuilder +// purpose : +//======================================================================= +const Handle(Select3D_BVHBuilder3d)& Select3D_SensitiveSet::DefaultBVHBuilder() +{ + return THE_SENS_SET_BUILDER; +} + +//======================================================================= +// function : SetDefaultBVHBuilder +// purpose : +//======================================================================= +void Select3D_SensitiveSet::SetDefaultBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) +{ + THE_SENS_SET_BUILDER = theBuilder; +} + //======================================================================= // function : Select3D_SensitiveSet // purpose : Creates new empty sensitive set and its content @@ -27,9 +51,8 @@ Select3D_SensitiveSet::Select3D_SensitiveSet (const Handle(SelectBasics_EntityOw : Select3D_SensitiveEntity (theOwnerId), myDetectedIdx (-1) { - NCollection_Handle< BVH_Builder > aBuilder = new BVH_LinearBuilder (myLeafNodeSize, 32); myContent.SetSensitiveSet (this); - myContent.SetBuilder (aBuilder); + myContent.SetBuilder (THE_SENS_SET_BUILDER); myContent.MarkDirty(); } @@ -61,7 +84,7 @@ Standard_Boolean Select3D_SensitiveSet::Matches (SelectBasics_SelectingVolumeMan return Standard_False; } - Standard_Integer aStack[32]; + Standard_Integer aStack[BVH_Constants_MaxTreeDepth]; Standard_Integer aNode = 0; Standard_Integer aHead = -1; diff --git a/src/Select3D/Select3D_SensitiveSet.hxx b/src/Select3D/Select3D_SensitiveSet.hxx index 94c202c1bf..c64c84c201 100644 --- a/src/Select3D/Select3D_SensitiveSet.hxx +++ b/src/Select3D/Select3D_SensitiveSet.hxx @@ -16,8 +16,9 @@ #ifndef _Select3D_SensitiveSet_Header #define _Select3D_SensitiveSet_Header -#include +#include #include +#include #include #include @@ -31,15 +32,20 @@ //! For example of usage see Select3D_SensitiveTriangulation. class Select3D_SensitiveSet : public Select3D_SensitiveEntity { - static const Standard_Integer myLeafNodeSize = 8; //!< Number of sub-elements in the leaf DEFINE_STANDARD_RTTIEXT(Select3D_SensitiveSet, Select3D_SensitiveEntity) +public: + + //! Return global instance to default BVH builder. + Standard_EXPORT static const Handle(Select3D_BVHBuilder3d)& DefaultBVHBuilder(); + + //! Assign new BVH builder to be used by default for new sensitive sets (assigning is NOT thread-safe!). + Standard_EXPORT static void SetDefaultBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder); + public: //! Creates new empty sensitive set and its content Standard_EXPORT Select3D_SensitiveSet (const Handle(SelectBasics_EntityOwner)& theOwnerId); - ~Select3D_SensitiveSet() {} - public: //! Returns the amount of sub-entities of the complex entity @@ -67,6 +73,9 @@ public: //! but element by element Standard_EXPORT virtual void BVH() Standard_OVERRIDE; + //! Sets the method (builder) used to construct BVH. + void SetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) { myContent.SetBuilder (theBuilder); } + //! Marks BVH tree of the set as outdated. It will be rebuild //! at the next call of BVH() void MarkDirty() { myContent.MarkDirty(); } @@ -83,7 +92,7 @@ public: Standard_EXPORT virtual void Clear() Standard_OVERRIDE; //! Returns a number of nodes in 1 BVH leaf - Standard_Integer GetLeafNodeSize() const { return myLeafNodeSize; } + Standard_Integer GetLeafNodeSize() const { return myContent.Builder()->LeafNodeSize(); } protected: @@ -104,12 +113,12 @@ protected: //! The purpose of this class is to provide a link between BVH_PrimitiveSet //! and Select3D_SensitiveSet instance to build BVH tree for set of sensitives. - class BvhPrimitiveSet : public BVH_PrimitiveSet + class BvhPrimitiveSet : public BVH_PrimitiveSet3d { public: //! Empty constructor. - BvhPrimitiveSet() {} + BvhPrimitiveSet() : BVH_PrimitiveSet3d (Handle(Select3D_BVHBuilder3d)()) {} //! Destructor. ~BvhPrimitiveSet() {} @@ -128,7 +137,7 @@ protected: virtual Select3D_BndBox3d Box (const Standard_Integer theIdx) const Standard_OVERRIDE { return mySensitiveSet->Box (theIdx); } //! Make inherited method Box() visible to avoid CLang warning - using BVH_PrimitiveSet::Box; + using BVH_PrimitiveSet3d::Box; //! Returns center of sensitive with index theIdx in the set along the given axis theAxis virtual Standard_Real Center (const Standard_Integer theIdx, @@ -139,7 +148,7 @@ protected: const Standard_Integer theIdx2) Standard_OVERRIDE { mySensitiveSet->Swap (theIdx1, theIdx2); } //! Returns the tree built for set of sensitives - const NCollection_Handle >& GetBVH() { return BVH(); } + const opencascade::handle >& GetBVH() { return BVH(); } protected: Select3D_SensitiveSet* mySensitiveSet; //!< Set of sensitive entities diff --git a/src/SelectBasics/SelectBasics_SensitiveEntity.cxx b/src/SelectBasics/SelectBasics_SensitiveEntity.cxx index e852d30f74..99fc2a471b 100644 --- a/src/SelectBasics/SelectBasics_SensitiveEntity.cxx +++ b/src/SelectBasics/SelectBasics_SensitiveEntity.cxx @@ -37,15 +37,3 @@ void SelectBasics_SensitiveEntity::Set (const Handle(SelectBasics_EntityOwner)& { myOwnerId = theOwnerId; } - -//======================================================================= -// function : SetSensitivityFactor -// purpose : Allows to manage sensitivity of a particular entity -//======================================================================= -void SelectBasics_SensitiveEntity::SetSensitivityFactor (const Standard_Integer theNewSens) -{ - Standard_ASSERT_RAISE (theNewSens > 0, - "Error! Selection sensitivity have positive value."); - - mySFactor = theNewSens; -} diff --git a/src/SelectBasics/SelectBasics_SensitiveEntity.hxx b/src/SelectBasics/SelectBasics_SensitiveEntity.hxx index 633fed8309..70a79269f3 100644 --- a/src/SelectBasics/SelectBasics_SensitiveEntity.hxx +++ b/src/SelectBasics/SelectBasics_SensitiveEntity.hxx @@ -49,7 +49,11 @@ public: Standard_Integer SensitivityFactor() const { return mySFactor; } //! Allows to manage sensitivity of a particular sensitive entity - Standard_EXPORT void SetSensitivityFactor (const Standard_Integer theNewSens); + void SetSensitivityFactor (const Standard_Integer theNewSens) + { + Standard_ASSERT_RAISE (theNewSens > 0, "Error! Selection sensitivity have positive value."); + mySFactor = theNewSens; + } //! Returns the number of sub-entities or elements in //! sensitive entity. Is used to determine if entity is diff --git a/src/SelectMgr/SelectMgr_SelectableObjectSet.cxx b/src/SelectMgr/SelectMgr_SelectableObjectSet.cxx index 763980c7ef..226d0ac3e4 100644 --- a/src/SelectMgr/SelectMgr_SelectableObjectSet.cxx +++ b/src/SelectMgr/SelectMgr_SelectableObjectSet.cxx @@ -212,9 +212,9 @@ SelectMgr_SelectableObjectSet::SelectMgr_SelectableObjectSet() myBVH[BVHSubset_3dPersistent] = new BVH_Tree(); myBVH[BVHSubset_3d] = new BVH_Tree(); - myBuilder[BVHSubset_2dPersistent] = new BVH_LinearBuilder (1, 32); - myBuilder[BVHSubset_3dPersistent] = new BVH_LinearBuilder (1, 32); - myBuilder[BVHSubset_3d] = new BVH_BinnedBuilder (1, 32, Standard_True); + myBuilder[BVHSubset_2dPersistent] = new BVH_LinearBuilder (BVH_Constants_LeafNodeSizeSingle, BVH_Constants_MaxTreeDepth); + myBuilder[BVHSubset_3dPersistent] = new BVH_LinearBuilder (BVH_Constants_LeafNodeSizeSingle, BVH_Constants_MaxTreeDepth); + myBuilder[BVHSubset_3d] = new BVH_BinnedBuilder (BVH_Constants_LeafNodeSizeSingle, BVH_Constants_MaxTreeDepth, Standard_True); myIsDirty[BVHSubset_2dPersistent] = Standard_False; myIsDirty[BVHSubset_3dPersistent] = Standard_False; @@ -327,7 +327,7 @@ void SelectMgr_SelectableObjectSet::UpdateBVH (const Handle(Graphic3d_Camera)& t BVHBuilderAdaptorRegular anAdaptor (myObjects[BVHSubset_3d]); // update corresponding BVH tree data structure - myBuilder[BVHSubset_3d]->Build (&anAdaptor, myBVH[BVHSubset_3d].operator->(), anAdaptor.Box()); + myBuilder[BVHSubset_3d]->Build (&anAdaptor, myBVH[BVHSubset_3d].get(), anAdaptor.Box()); // release dirty state myIsDirty[BVHSubset_3d] = Standard_False; @@ -349,7 +349,7 @@ void SelectMgr_SelectableObjectSet::UpdateBVH (const Handle(Graphic3d_Camera)& t theCamera, theProjectionMat, theWorldViewMat, theViewportWidth, theViewportHeight); // update corresponding BVH tree data structure - myBuilder[BVHSubset_3dPersistent]->Build (&anAdaptor, myBVH[BVHSubset_3dPersistent].operator->(), anAdaptor.Box()); + myBuilder[BVHSubset_3dPersistent]->Build (&anAdaptor, myBVH[BVHSubset_3dPersistent].get(), anAdaptor.Box()); } // ----------------------------------------------------- @@ -363,7 +363,7 @@ void SelectMgr_SelectableObjectSet::UpdateBVH (const Handle(Graphic3d_Camera)& t theCamera, theProjectionMat, THE_IDENTITY_MAT, theViewportWidth, theViewportHeight); // update corresponding BVH tree data structure - myBuilder[BVHSubset_2dPersistent]->Build (&anAdaptor, myBVH[BVHSubset_2dPersistent].operator->(), anAdaptor.Box()); + myBuilder[BVHSubset_2dPersistent]->Build (&anAdaptor, myBVH[BVHSubset_2dPersistent].get(), anAdaptor.Box()); } // release dirty state for every subset diff --git a/src/SelectMgr/SelectMgr_SelectableObjectSet.hxx b/src/SelectMgr/SelectMgr_SelectableObjectSet.hxx index 978337cf8f..242abdc7e3 100644 --- a/src/SelectMgr/SelectMgr_SelectableObjectSet.hxx +++ b/src/SelectMgr/SelectMgr_SelectableObjectSet.hxx @@ -16,12 +16,13 @@ #ifndef _SelectMgr_SelectableObjectSet_HeaderFile #define _SelectMgr_SelectableObjectSet_HeaderFile -#include +#include #include #include #include #include #include +#include #include //! The purpose of this class is to organize all selectable objects into data structure, allowing to build @@ -174,7 +175,7 @@ public: } //! Returns computed BVH for the theSubset given. - const NCollection_Handle >& BVH(const BVHSubset theSubset) const + const opencascade::handle >& BVH(const BVHSubset theSubset) const { return myBVH[theSubset]; } @@ -214,8 +215,8 @@ private: private: NCollection_IndexedMap myObjects[BVHSubsetNb]; //!< Map of objects for each subset - NCollection_Handle > myBVH[BVHSubsetNb]; //!< BVH tree computed for each subset - NCollection_Handle > myBuilder[BVHSubsetNb]; //!< Builder allocated for each subset + opencascade::handle > myBVH[BVHSubsetNb]; //!< BVH tree computed for each subset + Handle(Select3D_BVHBuilder3d) myBuilder[BVHSubsetNb]; //!< Builder allocated for each subset Standard_Boolean myIsDirty[BVHSubsetNb]; //!< Dirty flag for each subset Graphic3d_WorldViewProjState myLastViewState; //!< Last view-projection state used for construction of BVH Standard_Integer myLastWidth; //!< Last viewport's (window's) width used for construction of BVH diff --git a/src/SelectMgr/SelectMgr_Selection.cxx b/src/SelectMgr/SelectMgr_Selection.cxx index 16193471a0..2604841a8f 100644 --- a/src/SelectMgr/SelectMgr_Selection.cxx +++ b/src/SelectMgr/SelectMgr_Selection.cxx @@ -44,9 +44,9 @@ SelectMgr_Selection::~SelectMgr_Selection() //================================================== void SelectMgr_Selection::Destroy() { - for (Standard_Integer anEntityIdx = 0; anEntityIdx < myEntities.Size(); ++anEntityIdx) + for (NCollection_Vector::Iterator anEntityIter (myEntities); anEntityIter.More(); anEntityIter.Next()) { - Handle(SelectMgr_SensitiveEntity)& anEntity = myEntities.ChangeValue (anEntityIdx); + Handle(SelectMgr_SensitiveEntity)& anEntity = anEntityIter.ChangeValue(); anEntity->BaseSensitive()->Set (NULL); } mySensFactor = 2; @@ -92,9 +92,9 @@ void SelectMgr_Selection::Add (const Handle(SelectBasics_SensitiveEntity)& theSe //================================================== void SelectMgr_Selection::Clear() { - for (Standard_Integer anIdx = 0; anIdx < myEntities.Size(); ++anIdx) + for (NCollection_Vector::Iterator anEntityIter (myEntities); anEntityIter.More(); anEntityIter.Next()) { - Handle(SelectMgr_SensitiveEntity)& anEntity = myEntities.ChangeValue (anIdx); + Handle(SelectMgr_SensitiveEntity)& anEntity = anEntityIter.ChangeValue(); anEntity->Clear(); } @@ -157,9 +157,9 @@ void SelectMgr_Selection::SetSensitivity (const Standard_Integer theNewSens) { mySensFactor = theNewSens; myIsCustomSens = Standard_True; - for (Standard_Integer anIdx = 0; anIdx < myEntities.Size(); ++anIdx) + for (NCollection_Vector::Iterator anEntityIter (myEntities); anEntityIter.More(); anEntityIter.Next()) { - Handle(SelectMgr_SensitiveEntity)& anEntity = myEntities.ChangeValue (anIdx); + Handle(SelectMgr_SensitiveEntity)& anEntity = anEntityIter.ChangeValue(); anEntity->BaseSensitive()->SetSensitivityFactor (theNewSens); } } diff --git a/src/SelectMgr/SelectMgr_SensitiveEntity.cxx b/src/SelectMgr/SelectMgr_SensitiveEntity.cxx index 06cf93380b..f172df2dd7 100644 --- a/src/SelectMgr/SelectMgr_SensitiveEntity.cxx +++ b/src/SelectMgr/SelectMgr_SensitiveEntity.cxx @@ -24,9 +24,10 @@ IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SensitiveEntity,Standard_Transient) // theEntity //======================================================================= SelectMgr_SensitiveEntity::SelectMgr_SensitiveEntity (const Handle(SelectBasics_SensitiveEntity)& theEntity) +: mySensitive (theEntity), + myIsActiveForSelection (Standard_False) { - mySensitive = theEntity; - myIsActiveForSelection = Standard_False; + // } //======================================================================= @@ -38,40 +39,3 @@ void SelectMgr_SensitiveEntity::Clear() mySensitive->Clear(); mySensitive.Nullify(); } - -//======================================================================= -// function : BaseSensitive -// purpose : Returns related instance of SelectBasics class -//======================================================================= -const Handle(SelectBasics_SensitiveEntity)& SelectMgr_SensitiveEntity::BaseSensitive() const -{ - return mySensitive; -} - -//======================================================================= -// function : IsActiveForSelection -// purpose : Returns true if this entity belongs to the active selection -// mode of parent object -//======================================================================= -Standard_Boolean SelectMgr_SensitiveEntity::IsActiveForSelection() const -{ - return myIsActiveForSelection; -} - -//======================================================================= -// function : ResetSelectionActiveStatus -// purpose : Marks entity as inactive for selection -//======================================================================= -void SelectMgr_SensitiveEntity::ResetSelectionActiveStatus() const -{ - myIsActiveForSelection = Standard_False; -} - -//======================================================================= -// function : SetActiveForSelection -// purpose : Marks entity as active for selection -//======================================================================= -void SelectMgr_SensitiveEntity::SetActiveForSelection() const -{ - myIsActiveForSelection = Standard_True; -} diff --git a/src/SelectMgr/SelectMgr_SensitiveEntity.hxx b/src/SelectMgr/SelectMgr_SensitiveEntity.hxx index 893115b0a4..ad6fed2399 100644 --- a/src/SelectMgr/SelectMgr_SensitiveEntity.hxx +++ b/src/SelectMgr/SelectMgr_SensitiveEntity.hxx @@ -35,17 +35,17 @@ public: Standard_EXPORT void Clear(); //! Returns related instance of SelectBasics class - Standard_EXPORT const Handle(SelectBasics_SensitiveEntity)& BaseSensitive() const; + const Handle(SelectBasics_SensitiveEntity)& BaseSensitive() const { return mySensitive; } //! Returns true if this entity belongs to the active selection //! mode of parent object - Standard_EXPORT Standard_Boolean IsActiveForSelection() const; + Standard_Boolean IsActiveForSelection() const { return myIsActiveForSelection; } //! Marks entity as inactive for selection - Standard_EXPORT void ResetSelectionActiveStatus() const; + void ResetSelectionActiveStatus() const { myIsActiveForSelection = Standard_False; } //! Marks entity as active for selection - Standard_EXPORT void SetActiveForSelection() const; + void SetActiveForSelection() const { myIsActiveForSelection = Standard_True; } DEFINE_STANDARD_RTTIEXT(SelectMgr_SensitiveEntity,Standard_Transient) // Type definition diff --git a/src/SelectMgr/SelectMgr_SensitiveEntitySet.cxx b/src/SelectMgr/SelectMgr_SensitiveEntitySet.cxx index 26f186ab5e..7bb41a6d98 100644 --- a/src/SelectMgr/SelectMgr_SensitiveEntitySet.cxx +++ b/src/SelectMgr/SelectMgr_SensitiveEntitySet.cxx @@ -15,18 +15,19 @@ #include -#include - #include #include +IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SensitiveEntitySet, BVH_PrimitiveSet3d) + //======================================================================= // function : SelectMgr_SensitiveEntitySet // purpose : //======================================================================= -SelectMgr_SensitiveEntitySet::SelectMgr_SensitiveEntitySet() +SelectMgr_SensitiveEntitySet::SelectMgr_SensitiveEntitySet (const Handle(Select3D_BVHBuilder3d)& theBuilder) +: BVH_PrimitiveSet3d (theBuilder) { - myBuilder = new BVH_BinnedBuilder (1, 32, Standard_True); + // } //======================================================================= @@ -35,7 +36,7 @@ SelectMgr_SensitiveEntitySet::SelectMgr_SensitiveEntitySet() //======================================================================= void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_SensitiveEntity)& theEntity) { - if (!theEntity->BaseSensitive()->IsKind ("Select3D_SensitiveEntity")) + if (!theEntity->BaseSensitive()->IsKind (STANDARD_TYPE(Select3D_SensitiveEntity))) { theEntity->ResetSelectionActiveStatus(); return; @@ -53,7 +54,7 @@ void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_Selection)& th { for (theSelection->Init(); theSelection->More(); theSelection->Next()) { - if (!theSelection->Sensitive()->BaseSensitive()->IsKind ("Select3D_SensitiveEntity")) + if (!theSelection->Sensitive()->BaseSensitive()->IsKind (STANDARD_TYPE(Select3D_SensitiveEntity))) { theSelection->Sensitive()->ResetSelectionActiveStatus(); continue; diff --git a/src/SelectMgr/SelectMgr_SensitiveEntitySet.hxx b/src/SelectMgr/SelectMgr_SensitiveEntitySet.hxx index 40a0ec9fd9..0b1ce16f14 100644 --- a/src/SelectMgr/SelectMgr_SensitiveEntitySet.hxx +++ b/src/SelectMgr/SelectMgr_SensitiveEntitySet.hxx @@ -16,13 +16,10 @@ #ifndef _SelectMgr_SensitiveEntitySet_HeaderFile #define _SelectMgr_SensitiveEntitySet_HeaderFile -#include - +#include #include -#include - #include - +#include #include #include @@ -31,11 +28,13 @@ typedef NCollection_IndexedMap SelectMgr_Inde //! This class is used to store all calculated sensitive entites of one selectable //! object. It provides an interface for building BVH tree which is used to speed-up //! the performance of searching for overlap among sensitives of one selectable object -class SelectMgr_SensitiveEntitySet : public BVH_PrimitiveSet +class SelectMgr_SensitiveEntitySet : public BVH_PrimitiveSet3d { + DEFINE_STANDARD_RTTIEXT(SelectMgr_SensitiveEntitySet, BVH_PrimitiveSet3d) public: - Standard_EXPORT SelectMgr_SensitiveEntitySet(); + //! Empty constructor. + Standard_EXPORT SelectMgr_SensitiveEntitySet (const Handle(Select3D_BVHBuilder3d)& theBuilder); virtual ~SelectMgr_SensitiveEntitySet() {}; @@ -54,7 +53,7 @@ public: Standard_EXPORT virtual Select3D_BndBox3d Box (const Standard_Integer theIndex) const Standard_OVERRIDE; //! Make inherited method Box() visible to avoid CLang warning - using BVH_PrimitiveSet::Box; + using BVH_PrimitiveSet3d::Box; //! Returns geometry center of sensitive entity index theIdx //! along the given axis theAxis diff --git a/src/SelectMgr/SelectMgr_ViewerSelector.cxx b/src/SelectMgr/SelectMgr_ViewerSelector.cxx index bb95574a01..9bdaafbc9d 100644 --- a/src/SelectMgr/SelectMgr_ViewerSelector.cxx +++ b/src/SelectMgr/SelectMgr_ViewerSelector.cxx @@ -34,7 +34,7 @@ #include -IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_ViewerSelector,MMgt_TShared) +IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_ViewerSelector, Standard_Transient) namespace { // Comparison operator for sorting selection results @@ -95,6 +95,7 @@ myCurRank (0), myIsLeftChildQueuedFirst (Standard_False), myEntityIdx (0) { + myEntitySetBuilder = new BVH_BinnedBuilder (BVH_Constants_LeafNodeSizeSingle, BVH_Constants_MaxTreeDepth, Standard_True); } //================================================== @@ -315,16 +316,14 @@ void SelectMgr_ViewerSelector::traverseObject (const Handle(SelectMgr_Selectable const Standard_Integer theViewportWidth, const Standard_Integer theViewportHeight) { - NCollection_Handle& anEntitySet = - myMapOfObjectSensitives.ChangeFind (theObject); - + Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject); if (anEntitySet->Size() == 0) + { return; + } - const NCollection_Handle >& aSensitivesTree = anEntitySet->BVH(); - + const opencascade::handle >& aSensitivesTree = anEntitySet->BVH(); gp_GTrsf aInversedTrsf; - if (theObject->HasTransformation() || !theObject->TransformPersistence().IsNull()) { if (theObject->TransformPersistence().IsNull()) @@ -363,7 +362,7 @@ void SelectMgr_ViewerSelector::traverseObject (const Handle(SelectMgr_Selectable { return; } - Standard_Integer aStack[32]; + Standard_Integer aStack[BVH_Constants_MaxTreeDepth]; Standard_Integer aHead = -1; for (;;) { @@ -492,7 +491,7 @@ void SelectMgr_ViewerSelector::TraverseSensitives() ? mySelectingVolumeMgr.WorldViewMatrix() : THE_IDENTITY_MAT; - const NCollection_Handle >& aBVHTree = mySelectableObjects.BVH (aBVHSubset); + const opencascade::handle >& aBVHTree = mySelectableObjects.BVH (aBVHSubset); Standard_Integer aNode = 0; if (!aMgr.Overlaps (aBVHTree->MinPoint (0), aBVHTree->MaxPoint (0))) @@ -500,7 +499,7 @@ void SelectMgr_ViewerSelector::TraverseSensitives() continue; } - Standard_Integer aStack[32]; + Standard_Integer aStack[BVH_Constants_MaxTreeDepth]; Standard_Integer aHead = -1; for (;;) { @@ -608,6 +607,19 @@ const SelectMgr_SortCriterion& SelectMgr_ViewerSelector::PickedData(const Standa // //================================================== +//================================================== +// Function: SetEntitySetBuilder +// Purpose : +//================================================== +void SelectMgr_ViewerSelector::SetEntitySetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) +{ + myEntitySetBuilder = theBuilder; + for (SelectMgr_MapOfObjectSensitives::Iterator aSetIter (myMapOfObjectSensitives); aSetIter.More(); aSetIter.Next()) + { + aSetIter.ChangeValue()->SetBuilder (myEntitySetBuilder); + } +} + //================================================== // Function: Contains // Purpose : @@ -747,16 +759,12 @@ void SelectMgr_ViewerSelector::SortResult() if(myIndexes.IsNull() || anExtent != myIndexes->Length()) myIndexes = new TColStd_HArray1OfInteger (1, anExtent); - // to work faster... - TColStd_Array1OfInteger& thearr = myIndexes->ChangeArray1(); - - // indices from 1 to N are loaded - Standard_Integer I ; - for (I=1; I <= anExtent; I++) - thearr(I)=I; - - std::sort (thearr.begin(), thearr.end(), CompareResults (mystored)); - + TColStd_Array1OfInteger& anIndexArray = myIndexes->ChangeArray1(); + for (Standard_Integer anIndexIter = 1; anIndexIter <= anExtent; ++anIndexIter) + { + anIndexArray.SetValue (anIndexIter, anIndexIter); + } + std::sort (anIndexArray.begin(), anIndexArray.end(), CompareResults (mystored)); } //======================================================================= @@ -777,7 +785,7 @@ void SelectMgr_ViewerSelector::AddSelectableObject (const Handle(SelectMgr_Selec if (!myMapOfObjectSensitives.IsBound (theObject)) { mySelectableObjects.Append (theObject); - NCollection_Handle anEntitySet = new SelectMgr_SensitiveEntitySet(); + Handle(SelectMgr_SensitiveEntitySet) anEntitySet = new SelectMgr_SensitiveEntitySet (myEntitySetBuilder); myMapOfObjectSensitives.Bind (theObject, anEntitySet); } } @@ -789,12 +797,10 @@ void SelectMgr_ViewerSelector::AddSelectableObject (const Handle(SelectMgr_Selec void SelectMgr_ViewerSelector::AddSelectionToObject (const Handle(SelectMgr_SelectableObject)& theObject, const Handle(SelectMgr_Selection)& theSelection) { - if (myMapOfObjectSensitives.IsBound (theObject)) + if (Handle(SelectMgr_SensitiveEntitySet)* anEntitySet = myMapOfObjectSensitives.ChangeSeek (theObject)) { - NCollection_Handle& anEntitySet = - myMapOfObjectSensitives.ChangeFind (theObject); - anEntitySet->Append (theSelection); - anEntitySet->BVH(); + (*anEntitySet)->Append (theSelection); + (*anEntitySet)->BVH(); } else { @@ -818,10 +824,10 @@ void SelectMgr_ViewerSelector::MoveSelectableObject (const Handle(SelectMgr_Sele //======================================================================= void SelectMgr_ViewerSelector::RemoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject) { - if (myMapOfObjectSensitives.IsBound (theObject)) + Handle(SelectMgr_SelectableObject) anObj = theObject; + if (myMapOfObjectSensitives.UnBind (theObject)) { mySelectableObjects.Remove (theObject); - myMapOfObjectSensitives.UnBind (theObject); } } @@ -833,11 +839,9 @@ void SelectMgr_ViewerSelector::RemoveSelectableObject (const Handle(SelectMgr_Se void SelectMgr_ViewerSelector::RemoveSelectionOfObject (const Handle(SelectMgr_SelectableObject)& theObject, const Handle(SelectMgr_Selection)& theSelection) { - if (myMapOfObjectSensitives.IsBound (theObject)) + if (Handle(SelectMgr_SensitiveEntitySet)* anEntitySet = myMapOfObjectSensitives.ChangeSeek (theObject)) { - NCollection_Handle& anEntitySet = - myMapOfObjectSensitives.ChangeFind (theObject); - anEntitySet->Remove (theSelection); + (*anEntitySet)->Remove (theSelection); } } @@ -876,7 +880,7 @@ void SelectMgr_ViewerSelector::RebuildSensitivesTree (const Handle(SelectMgr_Sel if (!Contains (theObject)) return; - NCollection_Handle& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject); + Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject); anEntitySet->MarkDirty(); if (theIsForce) @@ -892,12 +896,10 @@ void SelectMgr_ViewerSelector::RebuildSensitivesTree (const Handle(SelectMgr_Sel //======================================================================= void SelectMgr_ViewerSelector::ResetSelectionActivationStatus() { - SelectMgr_MapOfObjectSensitivesIterator aSensitivesIter (myMapOfObjectSensitives); - for ( ; aSensitivesIter.More(); aSensitivesIter.Next()) + for (SelectMgr_MapOfObjectSensitivesIterator aSensitivesIter (myMapOfObjectSensitives); aSensitivesIter.More(); aSensitivesIter.Next()) { - NCollection_Handle& anEntitySet = - aSensitivesIter.ChangeValue(); - Standard_Integer anEntitiesNb = anEntitySet->Size(); + Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = aSensitivesIter.ChangeValue(); + const Standard_Integer anEntitiesNb = anEntitySet->Size(); for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx) { anEntitySet->GetSensitiveById (anIdx)->ResetSelectionActiveStatus(); @@ -924,13 +926,14 @@ void SelectMgr_ViewerSelector::ActiveOwners (NCollection_List& anEntitySet = anIter.Value(); - Standard_Integer anEntitiesNb = anEntitySet->Size(); + const Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = anIter.Value(); + const Standard_Integer anEntitiesNb = anEntitySet->Size(); for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx) { - if (anEntitySet->GetSensitiveById (anIdx)->IsActiveForSelection()) + const Handle(SelectMgr_SensitiveEntity)& aSensitive = anEntitySet->GetSensitiveById (anIdx); + if (aSensitive->IsActiveForSelection()) { - theOwners.Append (anEntitySet->GetSensitiveById (anIdx)->BaseSensitive()->OwnerId()); + theOwners.Append (aSensitive->BaseSensitive()->OwnerId()); } } } diff --git a/src/SelectMgr/SelectMgr_ViewerSelector.hxx b/src/SelectMgr/SelectMgr_ViewerSelector.hxx index 17c7ddc020..42b07ed190 100644 --- a/src/SelectMgr/SelectMgr_ViewerSelector.hxx +++ b/src/SelectMgr/SelectMgr_ViewerSelector.hxx @@ -17,12 +17,12 @@ #ifndef _SelectMgr_ViewerSelector_HeaderFile #define _SelectMgr_ViewerSelector_HeaderFile -#include -#include +#include #include #include #include #include +#include #include #include #include @@ -37,8 +37,8 @@ class SelectMgr_SensitiveEntitySet; class SelectMgr_EntityOwner; class SelectBasics_SensitiveEntity; -typedef NCollection_DataMap > SelectMgr_MapOfObjectSensitives; -typedef NCollection_DataMap >::Iterator SelectMgr_MapOfObjectSensitivesIterator; +typedef NCollection_DataMap SelectMgr_MapOfObjectSensitives; +typedef NCollection_DataMap::Iterator SelectMgr_MapOfObjectSensitivesIterator; typedef NCollection_DataMap SelectMgr_FrustumCache; @@ -127,6 +127,13 @@ public: Standard_EXPORT Standard_Boolean Contains (const Handle(SelectMgr_SelectableObject)& theObject) const; + //! Returns the default builder used to construct BVH of entity set. + const Handle(Select3D_BVHBuilder3d) EntitySetBuilder() { return myEntitySetBuilder; } + + //! Sets the default builder used to construct BVH of entity set. + //! The new builder will be also assigned for already defined objects, but computed BVH trees will not be invalidated. + Standard_EXPORT void SetEntitySetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder); + //! Returns the list of selection modes ModeList found in //! this selector for the selectable object aSelectableObject. //! Returns true if aSelectableObject is referenced inside @@ -319,6 +326,7 @@ protected: mutable SelectMgr_SelectableObjectSet mySelectableObjects; SelectMgr_ToleranceMap myTolerances; NCollection_DataMap myZLayerOrderMap; + Handle(Select3D_BVHBuilder3d) myEntitySetBuilder; private: @@ -330,6 +338,6 @@ private: }; -DEFINE_STANDARD_HANDLE(SelectMgr_ViewerSelector, MMgt_TShared) +DEFINE_STANDARD_HANDLE(SelectMgr_ViewerSelector, Standard_Transient) #endif diff --git a/src/StdSelect/StdSelect_BRepSelectionTool.cxx b/src/StdSelect/StdSelect_BRepSelectionTool.cxx index 0d8ddf0231..c8634654b1 100644 --- a/src/StdSelect/StdSelect_BRepSelectionTool.cxx +++ b/src/StdSelect/StdSelect_BRepSelectionTool.cxx @@ -81,17 +81,19 @@ void StdSelect_BRepSelectionTool::PreBuildBVH (const Handle(SelectMgr_Selection) aSensitive->BVH(); } - if (aSensitive->IsInstance ("Select3D_SensitiveGroup")) + if (!aSensitive->IsInstance (STANDARD_TYPE(Select3D_SensitiveGroup))) { - Handle(Select3D_SensitiveGroup) aGroup (Handle(Select3D_SensitiveGroup)::DownCast (aSensitive)); - const Select3D_EntitySequence& aSubEntities = aGroup->GetEntities(); - for (Select3D_EntitySequenceIter aSubEntitiesIter (aSubEntities); aSubEntitiesIter.More(); aSubEntitiesIter.Next()) + continue; + } + + Handle(Select3D_SensitiveGroup) aGroup = Handle(Select3D_SensitiveGroup)::DownCast (aSensitive); + const Select3D_EntitySequence& aSubEntities = aGroup->GetEntities(); + for (Select3D_EntitySequenceIter aSubEntitiesIter (aSubEntities); aSubEntitiesIter.More(); aSubEntitiesIter.Next()) + { + const Handle(Select3D_SensitiveEntity)& aSubEntity = aSubEntitiesIter.Value(); + if (aSubEntity->NbSubElements() >= BVH_PRIMITIVE_LIMIT) { - const Handle(Select3D_SensitiveEntity)& aSubEntity = aSubEntitiesIter.Value(); - if (aSubEntity->NbSubElements() >= BVH_PRIMITIVE_LIMIT) - { - aSubEntity->BVH(); - } + aSubEntity->BVH(); } } }