]> git.mxchange.org Git - simgear.git/blob - simgear/bvh/BVHGroup.cxx
Use quadtree to improve culling of STG objects
[simgear.git] / simgear / bvh / BVHGroup.cxx
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 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21
22 #include "BVHGroup.hxx"
23
24 #include <algorithm>
25
26 namespace simgear {
27
28 BVHGroup::BVHGroup()
29 {
30 }
31
32 BVHGroup::~BVHGroup()
33 {
34     ChildList::iterator i;
35     for (i = _children.begin(); i != _children.end(); ++i) {
36         (*i)->removeParent(this);
37         *i = 0;
38     }
39 }
40
41 void
42 BVHGroup::accept(BVHVisitor& visitor)
43 {
44     visitor.apply(*this);
45 }
46
47 void
48 BVHGroup::clear()
49 {
50     _children.clear();
51     invalidateBound();
52 }
53
54 void
55 BVHGroup::addChild(BVHNode* child)
56 {
57     if (!child)
58         return;
59     ChildList::iterator i;
60     i = std::find(_children.begin(), _children.end(), child);
61     if (i != _children.end())
62         return;
63     invalidateBound();
64     child->addParent(this);
65     _children.push_back(child);
66 }
67
68 void
69 BVHGroup::removeChild(BVHNode* child)
70 {
71     if (!child)
72         return;
73     ChildList::iterator i;
74     i = std::find(_children.begin(), _children.end(), child);
75     if (i == _children.end())
76         return;
77     invalidateBound();
78     child->removeParent(this);
79     _children.erase(i);
80 }
81
82 SGSphered
83 BVHGroup::computeBoundingSphere() const
84 {
85     SGSphered sphere;
86     ChildList::const_iterator i;
87     for (i = _children.begin(); i != _children.end(); ++i)
88         sphere.expandBy((*i)->getBoundingSphere());
89     return sphere;
90 }
91
92 }