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),
39 BVHMotionTransform::~BVHMotionTransform()
44 BVHMotionTransform::accept(BVHVisitor& visitor)
50 BVHMotionTransform::setTransform(const BVHMotionTransform& transform)
52 _toWorldReference = transform._toWorldReference;
53 _toLocalReference = transform._toLocalReference;
54 _toWorldAmplification = transform._toWorldAmplification;
55 _toLocalAmplification = transform._toLocalAmplification;
56 _linearVelocity = transform._linearVelocity;
57 _angularVelocity = transform._angularVelocity;
58 _referenceTime = transform._referenceTime;
59 _endTime = transform._endTime;
61 invalidateParentBound();
65 BVHMotionTransform::setToWorldTransform(const SGMatrixd& transform)
67 _toWorldReference = transform;
68 invert(_toLocalReference, transform);
69 updateAmplificationFactors();
70 invalidateParentBound();
74 BVHMotionTransform::setToLocalTransform(const SGMatrixd& transform)
76 _toLocalReference = transform;
77 invert(_toWorldReference, transform);
78 updateAmplificationFactors();
79 invalidateParentBound();
83 BVHMotionTransform::computeBoundingSphere() const
85 SGSphered sphere(BVHGroup::computeBoundingSphere());
88 SGVec3d centerStart = _toWorldReference.xformPt(sphere.getCenter());
89 SGMatrixd toWorldEnd = getToWorldTransform(_endTime);
90 SGVec3d centerEnd = toWorldEnd.xformPt(sphere.getCenter());
91 double rad = 0.5*length(centerStart - centerEnd) + sphere.getRadius();
92 rad *= _toWorldAmplification;
93 return SGSphered(0.5*(centerStart + centerEnd), rad);
97 BVHMotionTransform::updateAmplificationFactors()
99 // Hmm, this is just a hint, true?
100 // But anyway, almost all transforms in a scenegraph will
101 // have them equal to 1 ...
102 double r = norm(_toWorldReference.xformVec(SGVec3d(1, 0, 0)));
103 r = std::max(r, norm(_toWorldReference.xformVec(SGVec3d(0, 1, 0))));
104 r = std::max(r, norm(_toWorldReference.xformVec(SGVec3d(0, 0, 1))));
105 _toWorldAmplification = r;
107 r = norm(_toLocalReference.xformVec(SGVec3d(1, 0, 0)));
108 r = std::max(r, norm(_toLocalReference.xformVec(SGVec3d(0, 1, 0))));
109 r = std::max(r, norm(_toLocalReference.xformVec(SGVec3d(0, 0, 1))));
110 _toLocalAmplification = r;