]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHMotionTransform.hxx
Make the debug geometry stuff work with a time argument.
[simgear.git] / simgear / scene / bvh / BVHMotionTransform.hxx
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
38     void setLinearVelocity(const SGVec3d& linearVelocity)
39     { _linearVelocity = linearVelocity; }
40     const SGVec3d& getLinearVelocity() const
41     { return _linearVelocity; }
42
43     void setAngularVelocity(const SGVec3d& angularVelocity)
44     { _angularVelocity = angularVelocity; }
45     const SGVec3d& getAngularVelocity() const
46     { return _angularVelocity; }
47
48     void setReferenceTime(const double& referenceTime)
49     { _referenceTime = referenceTime; }
50     const double& getReferenceTime() const
51     { return _referenceTime; }
52
53     void setEndTime(const double& endTime)
54     { _endTime = endTime; }
55     const double& getEndTime() const
56     { return _endTime; }
57     
58     SGMatrixd getToWorldTransform(const double& t) const
59     {
60         double dt = t - _referenceTime;
61         if (0 == dt)
62             return _toWorldReference;
63         SGMatrixd matrix(_toWorldReference);
64         matrix.postMultRotate(SGQuatd::fromAngleAxis(dt*_angularVelocity));
65         matrix.postMultTranslate(dt*_linearVelocity);
66         return matrix;
67     }
68     SGMatrixd getToLocalTransform(const double& t) const
69     {
70         double dt = _referenceTime - t;
71         if (0 == dt)
72             return _toLocalReference;
73         SGMatrixd matrix(_toLocalReference);
74         matrix.preMultRotate(SGQuatd::fromAngleAxis(dt*_angularVelocity));
75         matrix.preMultTranslate(dt*_linearVelocity);
76         return matrix;
77     }
78
79     const SGMatrixd& getToWorldReferenceTransform() const
80     { return _toWorldReference; }
81     const SGMatrixd& getToLocalReferenceTransform() const
82     { return _toLocalReference; }
83
84     SGVec3d getLinearVelocityAt(const SGVec3d& reference) const
85     { return _linearVelocity + cross(_angularVelocity, reference); }
86
87     SGSphered sphereToLocal(const SGSphered& sphere, const double& t) const
88     {
89         SGMatrixd matrix = getToLocalTransform(t);
90         SGVec3d center = matrix.xformPt(sphere.getCenter());
91         double radius = _toLocalAmplification*sphere.getRadius();
92         return SGSphered(center, radius);
93     }
94
95     void setId(Id id)
96     { _id = id; }
97     Id getId() const
98     { return _id; }
99
100 private:
101     virtual SGSphered computeBoundingSphere() const;
102     void updateAmplificationFactors();
103     
104     SGMatrixd _toWorldReference;
105     SGMatrixd _toLocalReference;
106     double _toWorldAmplification;
107     double _toLocalAmplification;
108
109     SGVec3d _linearVelocity;
110     SGVec3d _angularVelocity;
111
112     double _referenceTime;
113     double _endTime;
114
115     Id _id;
116 };
117
118 }
119
120 #endif