]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHSubTreeCollector.hxx
Initial commit of the bounding volume tree implementation.
[simgear.git] / simgear / scene / bvh / BVHSubTreeCollector.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 BVHSubTreeCollector_hxx
19 #define BVHSubTreeCollector_hxx
20
21 #include <simgear/math/SGGeometry.hxx>
22
23 #include "BVHVisitor.hxx"
24 #include "BVHNode.hxx"
25 #include "BVHGroup.hxx"
26 #include "BVHStaticNode.hxx"
27
28 namespace simgear {
29
30 /// Visitor to subcollect parts of an existing bounding volume tree.
31 /// Given a sphere, it takes those sub parts of the tree that intersect the
32 /// sphere. Transform nodes of any kind are preserved to be able to ask for
33 /// intersections a different times. The subcollected tree is kept as small as
34 /// possible as it does avoid having groups with single childs.
35 /// Also the BVHStaticGeometry parts are seached for subtrees that are outside
36 /// the sphere.
37
38 class BVHSubTreeCollector : public BVHVisitor {
39 public:
40     typedef std::vector<SGSharedPtr<BVHNode> > NodeList;
41     
42     BVHSubTreeCollector(const SGSphered& sphere = SGSphered());
43     virtual ~BVHSubTreeCollector();
44     
45     virtual void apply(BVHGroup&);
46     virtual void apply(BVHTransform&);
47     virtual void apply(BVHMotionTransform&);
48     virtual void apply(BVHLineGeometry&);
49     virtual void apply(BVHStaticGeometry&);
50     
51     virtual void apply(const BVHStaticBinary&, const BVHStaticData&);
52     virtual void apply(const BVHStaticLeaf&, const BVHStaticData&);
53     virtual void apply(const BVHStaticTriangle&, const BVHStaticData&);
54     
55     void setSphere(const SGSphered& sphere)
56     { _sphere = sphere; }
57     const SGSphered& getSphere() const
58     { return _sphere; }
59
60     bool haveChildren() const
61     { return !_nodeList.empty(); }
62     
63     void addNode(BVHNode* node);
64     
65     void pushNodeList(NodeList& parentNodeList)
66     { _nodeList.swap(parentNodeList); }
67     void popNodeList(NodeList& parentNodeList, BVHGroup* transform);
68     void popNodeList(NodeList& parentNodeList);
69
70     SGSharedPtr<BVHNode> getNode() const;
71     
72 protected:
73     NodeList _nodeList;
74     SGSharedPtr<const BVHStaticNode> _staticNode;
75     
76 private:
77     SGSphered _sphere;
78 };
79
80 }
81
82 #endif