]> git.mxchange.org Git - simgear.git/blob - simgear/bvh/BVHMotionTransform.cxx
Tweak HTTP code to always sleep.
[simgear.git] / simgear / 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 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21
22 #include "BVHMotionTransform.hxx"
23
24 #include "BVHVisitor.hxx"
25 #include "BVHNode.hxx"
26 #include "BVHGroup.hxx"
27
28 namespace simgear {
29
30 BVHMotionTransform::BVHMotionTransform() :
31     _toWorldReference(SGMatrixd::unit()),
32     _toLocalReference(SGMatrixd::unit()),
33     _toWorldAmplification(1),
34     _toLocalAmplification(1),
35     _linearVelocity(0, 0, 0),
36     _angularVelocity(0, 0, 0),
37     _referenceTime(0),
38     _startTime(0),
39     _endTime(0),
40     _id(0)
41 {
42 }
43
44 BVHMotionTransform::~BVHMotionTransform()
45 {
46 }
47
48 void
49 BVHMotionTransform::accept(BVHVisitor& visitor)
50 {
51     visitor.apply(*this);
52 }
53
54 void
55 BVHMotionTransform::setTransform(const BVHMotionTransform& transform)
56 {
57     _toWorldReference = transform._toWorldReference;
58     _toLocalReference = transform._toLocalReference;
59     _toWorldAmplification = transform._toWorldAmplification;
60     _toLocalAmplification = transform._toLocalAmplification;
61     _linearVelocity = transform._linearVelocity;
62     _angularVelocity = transform._angularVelocity;
63     _referenceTime = transform._referenceTime;
64     _startTime = transform._startTime;
65     _endTime = transform._endTime;
66     _id = transform._id;
67     invalidateParentBound();
68 }
69
70 void
71 BVHMotionTransform::setToWorldTransform(const SGMatrixd& transform)
72 {
73     _toWorldReference = transform;
74     invert(_toLocalReference, transform);
75     updateAmplificationFactors();
76     invalidateParentBound();
77 }
78
79 void
80 BVHMotionTransform::setToLocalTransform(const SGMatrixd& transform)
81 {
82     _toLocalReference = transform;
83     invert(_toWorldReference, transform);
84     updateAmplificationFactors();
85     invalidateParentBound();
86 }
87
88 SGSphered
89 BVHMotionTransform::computeBoundingSphere() const
90 {
91     SGSphered sphere(BVHGroup::computeBoundingSphere());
92     if (sphere.empty())
93         return sphere;
94     SGMatrixd toWorldStart = getToWorldTransform(_startTime);
95     SGVec3d centerStart = toWorldStart.xformPt(sphere.getCenter());
96     SGMatrixd toWorldEnd = getToWorldTransform(_endTime);
97     SGVec3d centerEnd = toWorldEnd.xformPt(sphere.getCenter());
98     double rad = 0.5*length(centerStart - centerEnd) + sphere.getRadius();
99     rad *= _toWorldAmplification;
100     return SGSphered(0.5*(centerStart + centerEnd), rad);
101 }
102
103 void
104 BVHMotionTransform::updateAmplificationFactors()
105 {
106     // Hmm, this is just a hint, true?
107     // But anyway, almost all transforms in a scenegraph will
108     // have them equal to 1 ...
109     double r = norm(_toWorldReference.xformVec(SGVec3d(1, 0, 0)));
110         r = SGMiscd::max(r, norm(_toWorldReference.xformVec(SGVec3d(0, 1, 0))));
111     r = SGMiscd::max(r, norm(_toWorldReference.xformVec(SGVec3d(0, 0, 1))));
112     _toWorldAmplification = r;
113     
114     r = norm(_toLocalReference.xformVec(SGVec3d(1, 0, 0)));
115     r = SGMiscd::max(r, norm(_toLocalReference.xformVec(SGVec3d(0, 1, 0))));
116     r = SGMiscd::max(r, norm(_toLocalReference.xformVec(SGVec3d(0, 0, 1))));
117     _toLocalAmplification = r;
118 }
119
120 }