]> git.mxchange.org Git - simgear.git/blob - simgear/bvh/BVHSubTreeCollector.hxx
bvh: Move the basic bounding volume tree functionality into core.
[simgear.git] / simgear / 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 BVHStaticTriangle&, const BVHStaticData&);
53     
54     void setSphere(const SGSphered& sphere)
55     { _sphere = sphere; }
56     const SGSphered& getSphere() const
57     { return _sphere; }
58
59     bool haveChildren() const
60     { return !_nodeList.empty(); }
61     
62     void addNode(BVHNode* node);
63     
64     void pushNodeList(NodeList& parentNodeList)
65     { _nodeList.swap(parentNodeList); }
66     void popNodeList(NodeList& parentNodeList, BVHGroup* transform);
67     void popNodeList(NodeList& parentNodeList);
68
69     SGSharedPtr<BVHNode> getNode() const;
70     
71 protected:
72     NodeList _nodeList;
73     SGSharedPtr<const BVHStaticNode> _staticNode;
74     
75 private:
76     SGSphered _sphere;
77 };
78
79 }
80
81 #endif