0030655: Modeling Data - Provide interfaces for selection of the elements from BVH...
[occt.git] / src / BVH / BVH_Geometry.hxx
1 // Created on: 2013-12-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _BVH_Geometry_Header
17 #define _BVH_Geometry_Header
18
19 #include <BVH_ObjectSet.hxx>
20 #include <BVH_Builder.hxx>
21 #include <BVH_BinnedBuilder.hxx>
22
23 //! BVH geometry as a set of abstract geometric objects
24 //! organized with bounding volume hierarchy (BVH).
25 //! \tparam T Numeric data type
26 //! \tparam N Vector dimension
27 template<class T, int N>
28 class BVH_Geometry : public BVH_ObjectSet<T, N>
29 {
30 public:
31
32   //! Creates uninitialized BVH geometry.
33   BVH_Geometry()
34   : myIsDirty (Standard_False),
35     myBVH (new BVH_Tree<T, N>()),
36     // set default builder - binned SAH split
37     myBuilder (new BVH_BinnedBuilder<T, N, BVH_Constants_NbBinsOptimal> (BVH_Constants_LeafNodeSizeSingle))
38   {
39     //
40   }
41
42   //! Creates uninitialized BVH geometry.
43   BVH_Geometry (const opencascade::handle<BVH_Builder<T, N> >& theBuilder)
44   : myIsDirty (Standard_False),
45     myBVH (new BVH_Tree<T, N>()),
46     myBuilder (theBuilder)
47   {
48     //
49   }
50
51   //! Releases resources of BVH geometry.
52   virtual ~BVH_Geometry()
53   {
54     myBVH.Nullify();
55     myBuilder.Nullify();
56   }
57
58 public:
59
60   //! Returns TRUE if geometry state should be updated.
61   virtual Standard_Boolean IsDirty() const { return myIsDirty; }
62
63   //! Marks geometry as outdated.
64   virtual void MarkDirty() { myIsDirty = Standard_True; }
65
66   //! Returns AABB of the given object.
67   using BVH_ObjectSet<T, N>::Box;
68
69   //! Returns AABB of the whole geometry.
70   virtual BVH_Box<T, N> Box() const Standard_OVERRIDE
71   {
72     if (myIsDirty)
73     {
74       myBox = BVH_Set<T, N>::Box();
75     }
76     return myBox;
77   }
78
79   //! Returns BVH tree (and builds it if necessary).
80   virtual const opencascade::handle<BVH_Tree<T, N> >& BVH()
81   {
82     if (myIsDirty)
83     {
84       Update();
85     }
86     return myBVH;
87   }
88
89   //! Returns the method (builder) used to construct BVH.
90   virtual const opencascade::handle<BVH_Builder<T, N> >& Builder() const { return myBuilder; }
91
92   //! Sets the method (builder) used to construct BVH.
93   virtual void SetBuilder (const opencascade::handle<BVH_Builder<T, N> >& theBuilder) { myBuilder = theBuilder; }
94
95 protected:
96
97   //! Updates internal geometry state.
98   virtual void Update()
99   {
100     if (myIsDirty)
101     {
102       myBuilder->Build (this, myBVH.operator->(), Box());
103       myIsDirty = Standard_False;
104     }
105   }
106
107 protected:
108
109   Standard_Boolean                        myIsDirty; //!< Is geometry state outdated?
110   opencascade::handle<BVH_Tree<T, N> >    myBVH;     //!< Constructed hight-level BVH
111   opencascade::handle<BVH_Builder<T, N> > myBuilder; //!< Builder for hight-level BVH
112
113   mutable BVH_Box<T, N> myBox; //!< Cached bounding box of geometric objects
114
115 };
116
117 #endif // _BVH_Geometry_Header