1 // Copyright (C) 2008 - 2009 Mathias Froehlich - Mathias.Froehlich@web.de
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.
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.
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.
18 #ifndef BVHStaticGeometryBuilder_hxx
19 #define BVHStaticGeometryBuilder_hxx
25 #include <simgear/structure/SGReferenced.hxx>
26 #include <simgear/structure/SGSharedPtr.hxx>
28 #include "BVHVisitor.hxx"
29 #include "BVHNode.hxx"
30 #include "BVHGroup.hxx"
31 #include "BVHTransform.hxx"
33 #include "BVHStaticData.hxx"
35 #include "BVHStaticNode.hxx"
36 #include "BVHStaticLeaf.hxx"
37 #include "BVHStaticTriangle.hxx"
38 #include "BVHStaticBinary.hxx"
39 #include "BVHStaticGeometry.hxx"
43 class BVHStaticGeometryBuilder : public SGReferenced {
45 BVHStaticGeometryBuilder() :
46 _staticData(new BVHStaticData),
48 _currentMaterialIndex(~0u)
50 virtual ~BVHStaticGeometryBuilder()
54 LeafRef(const BVHStaticLeaf* leaf, const BVHStaticData& data) :
56 _box(_leaf->computeBoundingBox(data)),
57 _center(_leaf->computeCenter(data))
59 SGSharedPtr<const BVHStaticLeaf> _leaf;
63 typedef std::list<LeafRef> LeafRefList;
65 struct LeafRefLess : public std::binary_function<LeafRef, LeafRef, bool> {
66 LeafRefLess(unsigned sortAxis) : _sortAxis(sortAxis) {}
67 bool operator()(const LeafRef& x, const LeafRef& y)
68 { return x._center[_sortAxis] < y._center[_sortAxis]; }
72 SGSharedPtr<BVHStaticData> _staticData;
73 LeafRefList _leafRefList;
75 typedef std::map<SGVec3f, unsigned> VertexMap;
78 typedef std::set<SGVec3<unsigned> > TriangleSet;
79 TriangleSet _triangleSet;
81 void setCurrentMaterial(const BVHMaterial* material)
83 _currentMaterial = material;
84 _currentMaterialIndex = addMaterial(material);
86 const BVHMaterial* getCurrentMaterial() const
88 return _currentMaterial;
90 unsigned addMaterial(const BVHMaterial* material)
92 MaterialMap::const_iterator i = _materialMap.find(material);
93 if (i != _materialMap.end())
95 unsigned index = _staticData->addMaterial(material);
96 _materialMap[material] = index;
100 typedef std::map<const BVHMaterial*, unsigned> MaterialMap;
101 MaterialMap _materialMap;
102 const BVHMaterial* _currentMaterial;
103 unsigned _currentMaterialIndex;
105 void addTriangle(const SGVec3f& v1, const SGVec3f& v2, const SGVec3f& v3)
107 unsigned indices[3] = { addVertex(v1), addVertex(v2), addVertex(v3) };
108 std::sort(indices, indices + 3);
109 SGVec3<unsigned> indexKey(indices);
110 if (_triangleSet.find(indexKey) != _triangleSet.end())
112 _triangleSet.insert(indexKey);
113 BVHStaticTriangle* staticTriangle;
114 staticTriangle = new BVHStaticTriangle(_currentMaterialIndex, indices);
115 _leafRefList.push_back(LeafRef(staticTriangle, *_staticData));
117 unsigned addVertex(const SGVec3f& v)
119 VertexMap::const_iterator i = _vertexMap.find(v);
120 if (i != _vertexMap.end())
122 unsigned index = _staticData->addVertex(v);
123 _vertexMap[v] = index;
127 BVHStaticGeometry* buildTree()
129 const BVHStaticNode* tree = buildTreeRecursive(_leafRefList);
133 return new BVHStaticGeometry(tree, _staticData);
138 centerSplitLeafs(unsigned splitAxis, const double& splitValue,
139 LeafRefList& leafs, LeafRefList split[2])
141 while (!leafs.empty()) {
142 if (leafs.front()._center[splitAxis] < splitValue) {
143 split[0].splice(split[0].begin(), leafs, leafs.begin());
145 split[1].splice(split[1].begin(), leafs, leafs.begin());
151 equalSplitLeafs(unsigned splitAxis, LeafRefList& leafs,
152 LeafRefList split[2])
154 leafs.sort(LeafRefLess(splitAxis));
158 split[0].splice(split[0].begin(), leafs, leafs.begin());
162 split[1].splice(split[1].begin(), leafs, --leafs.end());
166 static const BVHStaticNode* buildTreeRecursive(LeafRefList& leafs)
168 // recursion termination
171 // FIXME size is O(n)!!!
172 // if (leafs.size() == 1)
173 if (++leafs.begin() == leafs.end())
174 return leafs.front()._leaf;
177 for (LeafRefList::const_iterator i = leafs.begin();
178 i != leafs.end(); ++i)
179 box.expandBy(i->_box);
182 // if (length(box.getSize()) < 1)
183 // return new BVHBox(box);
188 unsigned splitAxis = box.getBroadestAxis();
189 LeafRefList splitLeafs[2];
190 double splitValue = box.getCenter()[splitAxis];
191 centerSplitLeafs(splitAxis, splitValue, leafs, splitLeafs);
193 if (splitLeafs[0].empty() || splitLeafs[1].empty()) {
194 for (unsigned i = 0; i < 3 ; ++i) {
198 leafs.swap(splitLeafs[0]);
199 leafs.splice(leafs.begin(), splitLeafs[1]);
200 splitValue = box.getCenter()[i];
201 centerSplitLeafs(i, splitValue, leafs, splitLeafs);
203 if (!splitLeafs[0].empty() && !splitLeafs[1].empty()) {
209 if (splitLeafs[0].empty() || splitLeafs[1].empty()) {
210 leafs.swap(splitLeafs[0]);
211 leafs.splice(leafs.begin(), splitLeafs[1]);
212 equalSplitLeafs(splitAxis, leafs, splitLeafs);
215 const BVHStaticNode* child0 = buildTreeRecursive(splitLeafs[0]);
216 const BVHStaticNode* child1 = buildTreeRecursive(splitLeafs[1]);
222 return new BVHStaticBinary(splitAxis, child0, child1, box);