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 #ifndef BVHMotionTransform_hxx
19 #define BVHMotionTransform_hxx
21 #include "BVHVisitor.hxx"
22 #include "BVHNode.hxx"
23 #include "BVHGroup.hxx"
27 class BVHMotionTransform : public BVHGroup {
30 virtual ~BVHMotionTransform();
32 virtual void accept(BVHVisitor& visitor);
34 void setTransform(const BVHMotionTransform& transform);
35 void setToWorldTransform(const SGMatrixd& transform);
36 void setToLocalTransform(const SGMatrixd& transform);
38 void setLinearVelocity(const SGVec3d& linearVelocity)
39 { _linearVelocity = linearVelocity; }
40 const SGVec3d& getLinearVelocity() const
41 { return _linearVelocity; }
43 void setAngularVelocity(const SGVec3d& angularVelocity)
44 { _angularVelocity = angularVelocity; }
45 const SGVec3d& getAngularVelocity() const
46 { return _angularVelocity; }
48 void setReferenceTime(const double& referenceTime)
49 { _referenceTime = referenceTime; }
50 const double& getReferenceTime() const
51 { return _referenceTime; }
53 void setStartTime(const double& startTime)
54 { _startTime = startTime; }
55 const double& getStartTime() const
56 { return _startTime; }
58 void setEndTime(const double& endTime)
59 { _endTime = endTime; }
60 const double& getEndTime() const
63 SGMatrixd getToWorldTransform(const double& t) const
65 if (t == _referenceTime)
66 return _toWorldReference;
67 double dt = t - _referenceTime;
68 SGMatrixd matrix(_toWorldReference);
69 matrix.postMultRotate(SGQuatd::fromAngleAxis(dt*_angularVelocity));
70 matrix.postMultTranslate(dt*_linearVelocity);
73 SGMatrixd getToLocalTransform(const double& t) const
75 if (t == _referenceTime)
76 return _toLocalReference;
77 double dt = _referenceTime - t;
78 SGMatrixd matrix(_toLocalReference);
79 matrix.preMultRotate(SGQuatd::fromAngleAxis(dt*_angularVelocity));
80 matrix.preMultTranslate(dt*_linearVelocity);
84 const SGMatrixd& getToWorldReferenceTransform() const
85 { return _toWorldReference; }
86 const SGMatrixd& getToLocalReferenceTransform() const
87 { return _toLocalReference; }
89 SGVec3d getLinearVelocityAt(const SGVec3d& reference) const
90 { return _linearVelocity + cross(_angularVelocity, reference); }
92 SGSphered sphereToLocal(const SGSphered& sphere, const double& t) const
94 SGMatrixd matrix = getToLocalTransform(t);
95 SGVec3d center = matrix.xformPt(sphere.getCenter());
96 double radius = _toLocalAmplification*sphere.getRadius();
97 return SGSphered(center, radius);
106 virtual SGSphered computeBoundingSphere() const;
107 void updateAmplificationFactors();
109 SGMatrixd _toWorldReference;
110 SGMatrixd _toLocalReference;
111 double _toWorldAmplification;
112 double _toLocalAmplification;
114 SGVec3d _linearVelocity;
115 SGVec3d _angularVelocity;
117 double _referenceTime;