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.
19 # include <simgear_config.h>
22 #include "BVHMotionTransform.hxx"
24 #include "BVHVisitor.hxx"
25 #include "BVHNode.hxx"
26 #include "BVHGroup.hxx"
30 BVHMotionTransform::BVHMotionTransform() :
31 _toWorldReference(SGMatrixd::unit()),
32 _toLocalReference(SGMatrixd::unit()),
33 _toWorldAmplification(1),
34 _toLocalAmplification(1),
35 _linearVelocity(0, 0, 0),
36 _angularVelocity(0, 0, 0),
44 BVHMotionTransform::~BVHMotionTransform()
49 BVHMotionTransform::accept(BVHVisitor& visitor)
55 BVHMotionTransform::setTransform(const BVHMotionTransform& transform)
57 _toWorldReference = transform._toWorldReference;
58 _toLocalReference = transform._toLocalReference;
59 _toWorldAmplification = transform._toWorldAmplification;
60 _toLocalAmplification = transform._toLocalAmplification;
61 _linearVelocity = transform._linearVelocity;
62 _angularVelocity = transform._angularVelocity;
63 _referenceTime = transform._referenceTime;
64 _startTime = transform._startTime;
65 _endTime = transform._endTime;
67 invalidateParentBound();
71 BVHMotionTransform::setToWorldTransform(const SGMatrixd& transform)
73 _toWorldReference = transform;
74 invert(_toLocalReference, transform);
75 updateAmplificationFactors();
76 invalidateParentBound();
80 BVHMotionTransform::setToLocalTransform(const SGMatrixd& transform)
82 _toLocalReference = transform;
83 invert(_toWorldReference, transform);
84 updateAmplificationFactors();
85 invalidateParentBound();
89 BVHMotionTransform::computeBoundingSphere() const
91 SGSphered sphere(BVHGroup::computeBoundingSphere());
94 SGMatrixd toWorldStart = getToWorldTransform(_startTime);
95 SGVec3d centerStart = toWorldStart.xformPt(sphere.getCenter());
96 SGMatrixd toWorldEnd = getToWorldTransform(_endTime);
97 SGVec3d centerEnd = toWorldEnd.xformPt(sphere.getCenter());
98 double rad = 0.5*length(centerStart - centerEnd) + sphere.getRadius();
99 rad *= _toWorldAmplification;
100 return SGSphered(0.5*(centerStart + centerEnd), rad);
104 BVHMotionTransform::updateAmplificationFactors()
106 // Hmm, this is just a hint, true?
107 // But anyway, almost all transforms in a scenegraph will
108 // have them equal to 1 ...
109 double r = norm(_toWorldReference.xformVec(SGVec3d(1, 0, 0)));
110 r = SGMiscd::max(r, norm(_toWorldReference.xformVec(SGVec3d(0, 1, 0))));
111 r = SGMiscd::max(r, norm(_toWorldReference.xformVec(SGVec3d(0, 0, 1))));
112 _toWorldAmplification = r;
114 r = norm(_toLocalReference.xformVec(SGVec3d(1, 0, 0)));
115 r = SGMiscd::max(r, norm(_toLocalReference.xformVec(SGVec3d(0, 1, 0))));
116 r = SGMiscd::max(r, norm(_toLocalReference.xformVec(SGVec3d(0, 0, 1))));
117 _toLocalAmplification = r;