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 #include "BVHMotionTransform.hxx"
20 #include "BVHVisitor.hxx"
21 #include "BVHNode.hxx"
22 #include "BVHGroup.hxx"
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),
40 BVHMotionTransform::~BVHMotionTransform()
45 BVHMotionTransform::accept(BVHVisitor& visitor)
51 BVHMotionTransform::setTransform(const BVHMotionTransform& transform)
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;
63 invalidateParentBound();
67 BVHMotionTransform::setToWorldTransform(const SGMatrixd& transform)
69 _toWorldReference = transform;
70 invert(_toLocalReference, transform);
71 updateAmplificationFactors();
72 invalidateParentBound();
76 BVHMotionTransform::setToLocalTransform(const SGMatrixd& transform)
78 _toLocalReference = transform;
79 invert(_toWorldReference, transform);
80 updateAmplificationFactors();
81 invalidateParentBound();
85 BVHMotionTransform::computeBoundingSphere() const
87 SGSphered sphere(BVHGroup::computeBoundingSphere());
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);
100 BVHMotionTransform::updateAmplificationFactors()
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;
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;