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