]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHMotionTransform.cxx
First step for something doing static friction stuff.
[simgear.git] / simgear / scene / bvh / BVHMotionTransform.cxx
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 #include "BVHMotionTransform.hxx"
19
20 #include "BVHVisitor.hxx"
21 #include "BVHNode.hxx"
22 #include "BVHGroup.hxx"
23
24 namespace simgear {
25
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),
33     _referenceTime(0),
34     _endTime(0),
35     _id(0)
36 {
37 }
38
39 BVHMotionTransform::~BVHMotionTransform()
40 {
41 }
42
43 void
44 BVHMotionTransform::accept(BVHVisitor& visitor)
45 {
46     visitor.apply(*this);
47 }
48
49 void
50 BVHMotionTransform::setTransform(const BVHMotionTransform& transform)
51 {
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;
60     _id = transform._id;
61     invalidateParentBound();
62 }
63
64 void
65 BVHMotionTransform::setToWorldTransform(const SGMatrixd& transform)
66 {
67     _toWorldReference = transform;
68     invert(_toLocalReference, transform);
69     updateAmplificationFactors();
70     invalidateParentBound();
71 }
72
73 void
74 BVHMotionTransform::setToLocalTransform(const SGMatrixd& transform)
75 {
76     _toLocalReference = transform;
77     invert(_toWorldReference, transform);
78     updateAmplificationFactors();
79     invalidateParentBound();
80 }
81
82 SGSphered
83 BVHMotionTransform::computeBoundingSphere() const
84 {
85     SGSphered sphere(BVHGroup::computeBoundingSphere());
86     if (sphere.empty())
87         return sphere;
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);
94 }
95
96 void
97 BVHMotionTransform::updateAmplificationFactors()
98 {
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;
106     
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;
111 }
112
113 }