]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHStaticBinary.hxx
Initial commit of the bounding volume tree implementation.
[simgear.git] / simgear / scene / bvh / BVHStaticBinary.hxx
1 // Copyright (C) 2008 - 2009  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifndef BVHStaticBinary_hxx
19 #define BVHStaticBinary_hxx
20
21 #include <simgear/math/SGGeometry.hxx>
22 #include <simgear/structure/SGSharedPtr.hxx>
23 #include "BVHStaticNode.hxx"
24
25 namespace simgear {
26
27 class BVHStaticBinary : public BVHStaticNode {
28 public:
29   BVHStaticBinary(unsigned splitAxis, const BVHStaticNode* leftChild,
30                    const BVHStaticNode* rightChild, const SGBoxf& box);
31   virtual ~BVHStaticBinary();
32   virtual void accept(BVHVisitor& visitor, const BVHStaticData& data) const;
33
34   void traverse(BVHVisitor& visitor, const BVHStaticData& data) const
35   {
36     _leftChild->accept(visitor, data);
37     _rightChild->accept(visitor, data);
38   }
39
40   // Traverse call that first enters the child node that is potentially closer
41   // to the given point than the other.
42   template<typename T>
43   void traverse(BVHVisitor& visitor, const BVHStaticData& data,
44                 const SGVec3<T>& pt) const
45   {
46     float center = 0.5f*(_boundingBox.getMin()[_splitAxis]
47                          + _boundingBox.getMax()[_splitAxis]);
48     if (pt[_splitAxis] < center) {
49       _leftChild->accept(visitor, data);
50       _rightChild->accept(visitor, data);
51     } else {
52       _rightChild->accept(visitor, data);
53       _leftChild->accept(visitor, data);
54     }
55   }
56
57   unsigned getSplitAxis() const
58   { return _splitAxis; }
59
60   const BVHStaticNode* getLeftChild() const
61   { return _leftChild; }
62   const BVHStaticNode* getRightChild() const
63   { return _rightChild; }
64
65   const SGBoxf& getBoundingBox() const
66   { return _boundingBox; }
67
68 private:
69   // Note the order of the members, this is to avoid padding
70   unsigned _splitAxis;
71   SGSharedPtr<const BVHStaticNode> _leftChild;
72   SGSharedPtr<const BVHStaticNode> _rightChild;
73   SGBoxf _boundingBox;
74 };
75
76 }
77
78 #endif