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 #ifndef BVHMotionTransform_hxx
19 #define BVHMotionTransform_hxx
20 
21 #include "BVHVisitor.hxx"
22 #include "BVHNode.hxx"
23 #include "BVHGroup.hxx"
24 
25 namespace simgear {
26 
27 class BVHMotionTransform : public BVHGroup {
28 public:
29     BVHMotionTransform();
30     virtual ~BVHMotionTransform();
31 
32     virtual void accept(BVHVisitor& visitor);
33 
34     void setTransform(const BVHMotionTransform& transform);
35     void setToWorldTransform(const SGMatrixd& transform);
36     void setToLocalTransform(const SGMatrixd& transform);
37 
setLinearVelocity(const SGVec3d & linearVelocity)38     void setLinearVelocity(const SGVec3d& linearVelocity)
39     { _linearVelocity = linearVelocity; }
getLinearVelocity() const40     const SGVec3d& getLinearVelocity() const
41     { return _linearVelocity; }
42 
setAngularVelocity(const SGVec3d & angularVelocity)43     void setAngularVelocity(const SGVec3d& angularVelocity)
44     { _angularVelocity = angularVelocity; }
getAngularVelocity() const45     const SGVec3d& getAngularVelocity() const
46     { return _angularVelocity; }
47 
setReferenceTime(const double & referenceTime)48     void setReferenceTime(const double& referenceTime)
49     { _referenceTime = referenceTime; }
getReferenceTime() const50     const double& getReferenceTime() const
51     { return _referenceTime; }
52 
setStartTime(const double & startTime)53     void setStartTime(const double& startTime)
54     { _startTime = startTime; }
getStartTime() const55     const double& getStartTime() const
56     { return _startTime; }
57 
setEndTime(const double & endTime)58     void setEndTime(const double& endTime)
59     { _endTime = endTime; }
getEndTime() const60     const double& getEndTime() const
61     { return _endTime; }
62 
getToWorldTransform(const double & t) const63     SGMatrixd getToWorldTransform(const double& t) const
64     {
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);
71         return matrix;
72     }
getToLocalTransform(const double & t) const73     SGMatrixd getToLocalTransform(const double& t) const
74     {
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);
81         return matrix;
82     }
83 
getToWorldReferenceTransform() const84     const SGMatrixd& getToWorldReferenceTransform() const
85     { return _toWorldReference; }
getToLocalReferenceTransform() const86     const SGMatrixd& getToLocalReferenceTransform() const
87     { return _toLocalReference; }
88 
getLinearVelocityAt(const SGVec3d & reference) const89     SGVec3d getLinearVelocityAt(const SGVec3d& reference) const
90     { return _linearVelocity + cross(_angularVelocity, reference); }
91 
sphereToLocal(const SGSphered & sphere,const double & t) const92     SGSphered sphereToLocal(const SGSphered& sphere, const double& t) const
93     {
94         SGMatrixd matrix = getToLocalTransform(t);
95         SGVec3d center = matrix.xformPt(sphere.getCenter());
96         double radius = _toLocalAmplification*sphere.getRadius();
97         return SGSphered(center, radius);
98     }
99 
setId(Id id)100     void setId(Id id)
101     { _id = id; }
getId() const102     Id getId() const
103     { return _id; }
104 
105 private:
106     virtual SGSphered computeBoundingSphere() const;
107     void updateAmplificationFactors();
108 
109     SGMatrixd _toWorldReference;
110     SGMatrixd _toLocalReference;
111     double _toWorldAmplification;
112     double _toLocalAmplification;
113 
114     SGVec3d _linearVelocity;
115     SGVec3d _angularVelocity;
116 
117     double _referenceTime;
118     double _startTime;
119     double _endTime;
120 
121     Id _id;
122 };
123 
124 }
125 
126 #endif
127