]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHMotionTransform.cxx
Initial commit of the bounding volume tree implementation.
[simgear.git] / simgear / scene / bvh / BVHMotionTransform.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 "BVHMotionTransform.hxx"
19
20 #include "BVHVisitor.hxx"
21 #include "BVHNode.hxx"
22 #include "BVHGroup.hxx"
23
24 namespace simgear {
25
26 BVHMotionTransform::BVHMotionTransform() :
27     _toWorldReference(SGMatrixd::unit()),
28     _toLocalReference(SGMatrixd::unit()),
29     _toWorldAmplification(1),
30     _toLocalAmplification(1),
31     _linearVelocity(0, 0, 0),
32     _angularVelocity(0, 0, 0),
33     _referenceTime(0),
34     _endTime(0)
35 {
36 }
37
38 BVHMotionTransform::~BVHMotionTransform()
39 {
40 }
41
42 void
43 BVHMotionTransform::accept(BVHVisitor& visitor)
44 {
45     visitor.apply(*this);
46 }
47
48 void
49 BVHMotionTransform::setTransform(const BVHMotionTransform& transform)
50 {
51     _toWorldReference = transform._toWorldReference;
52     _toLocalReference = transform._toLocalReference;
53     _toWorldAmplification = transform._toWorldAmplification;
54     _toLocalAmplification = transform._toLocalAmplification;
55     _linearVelocity = transform._linearVelocity;
56     _angularVelocity = transform._angularVelocity;
57     _referenceTime = transform._referenceTime;
58     _endTime = transform._endTime;
59     invalidateParentBound();
60 }
61
62 void
63 BVHMotionTransform::setToWorldTransform(const SGMatrixd& transform)
64 {
65     _toWorldReference = transform;
66     invert(_toLocalReference, transform);
67     updateAmplificationFactors();
68     invalidateParentBound();
69 }
70
71 void
72 BVHMotionTransform::setToLocalTransform(const SGMatrixd& transform)
73 {
74     _toLocalReference = transform;
75     invert(_toWorldReference, transform);
76     updateAmplificationFactors();
77     invalidateParentBound();
78 }
79
80 SGSphered
81 BVHMotionTransform::computeBoundingSphere() const
82 {
83     SGSphered sphere(BVHGroup::computeBoundingSphere());
84     if (sphere.empty())
85         return sphere;
86     SGVec3d centerStart = _toWorldReference.xformPt(sphere.getCenter());
87     SGMatrixd toWorldEnd = getToWorldTransform(_endTime);
88     SGVec3d centerEnd = toWorldEnd.xformPt(sphere.getCenter());
89     double rad = 0.5*length(centerStart - centerEnd) + sphere.getRadius();
90     rad *= _toWorldAmplification;
91     return SGSphered(0.5*(centerStart + centerEnd), rad);
92 }
93
94 void
95 BVHMotionTransform::updateAmplificationFactors()
96 {
97     // Hmm, this is just a hint, true?
98     // But anyway, almost all transforms in a scenegraph will
99     // have them equal to 1 ...
100     double r = norm(_toWorldReference.xformVec(SGVec3d(1, 0, 0)));
101     r = std::max(r, norm(_toWorldReference.xformVec(SGVec3d(0, 1, 0))));
102     r = std::max(r, norm(_toWorldReference.xformVec(SGVec3d(0, 0, 1))));
103     _toWorldAmplification = r;
104     
105     r = norm(_toLocalReference.xformVec(SGVec3d(1, 0, 0)));
106     r = std::max(r, norm(_toLocalReference.xformVec(SGVec3d(0, 1, 0))));
107     r = std::max(r, norm(_toLocalReference.xformVec(SGVec3d(0, 0, 1))));
108     _toLocalAmplification = r;
109 }
110
111 }