]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHTransform.cxx
Initial commit of the bounding volume tree implementation.
[simgear.git] / simgear / scene / bvh / BVHTransform.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 #include "BVHTransform.hxx"
19
20 #include "BVHVisitor.hxx"
21 #include "BVHNode.hxx"
22 #include "BVHGroup.hxx"
23
24 namespace simgear {
25
26 BVHTransform::BVHTransform() :
27     _toWorld(SGMatrixd::unit()),
28     _toLocal(SGMatrixd::unit()),
29     _toWorldAmplification(1),
30     _toLocalAmplification(1)
31 {
32 }
33
34 BVHTransform::~BVHTransform()
35 {
36 }
37
38 void
39 BVHTransform::accept(BVHVisitor& visitor)
40 {
41     visitor.apply(*this);
42 }
43     
44 void
45 BVHTransform::setTransform(const BVHTransform& transform)
46 {
47     _toWorld = transform._toWorld;
48     _toLocal = transform._toLocal;
49     _toWorldAmplification = transform._toWorldAmplification;
50     _toLocalAmplification = transform._toLocalAmplification;
51     invalidateParentBound();
52 }
53
54 void
55 BVHTransform::setToWorldTransform(const SGMatrixd& transform)
56 {
57     _toWorld = transform;
58     invert(_toLocal, transform);
59     updateAmplificationFactors();
60     invalidateParentBound();
61 }
62
63 void
64 BVHTransform::setToLocalTransform(const SGMatrixd& transform)
65 {
66     _toLocal = transform;
67     invert(_toWorld, transform);
68     updateAmplificationFactors();
69     invalidateParentBound();
70 }
71
72 SGSphered
73 BVHTransform::computeBoundingSphere() const
74 {
75     return sphereToWorld(BVHGroup::computeBoundingSphere());
76 }
77
78 void
79 BVHTransform::updateAmplificationFactors()
80 {
81     // Hmm, this is just a hint, true?
82     // But anyway, almost all transforms in a scenegraph will
83     // have them equal to 1 ...
84     double r = norm(vecToWorld(SGVec3d(1, 0, 0)));
85     r = std::max(r, norm(vecToWorld(SGVec3d(0, 1, 0))));
86     r = std::max(r, norm(vecToWorld(SGVec3d(0, 0, 1))));
87     _toWorldAmplification = r;
88     
89     r = norm(vecToLocal(SGVec3d(1, 0, 0)));
90     r = std::max(r, norm(vecToLocal(SGVec3d(0, 1, 0))));
91     r = std::max(r, norm(vecToLocal(SGVec3d(0, 0, 1))));
92     _toLocalAmplification = r;
93 }
94
95 }