3 * Copyright (C) 2006-2007 Mathias Froehlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 # include <simgear_config.h>
26 #include "SGRotateTransform.hxx"
29 set_rotation (osg::Matrix &matrix, double position_rad,
30 const SGVec3d ¢er, const SGVec3d &axis)
32 double temp_angle = -position_rad;
34 double s = sin(temp_angle);
35 double c = cos(temp_angle);
38 // axis was normalized at load time
39 // hint to the compiler to put these into FP registers
44 matrix(0, 0) = t * x * x + c ;
45 matrix(0, 1) = t * y * x - s * z ;
46 matrix(0, 2) = t * z * x + s * y ;
49 matrix(1, 0) = t * x * y + s * z ;
50 matrix(1, 1) = t * y * y + c ;
51 matrix(1, 2) = t * z * y - s * x ;
54 matrix(2, 0) = t * x * z - s * y ;
55 matrix(2, 1) = t * y * z + s * x ;
56 matrix(2, 2) = t * z * z + c ;
59 // hint to the compiler to put these into FP registers
64 matrix(3, 0) = x - x*matrix(0, 0) - y*matrix(1, 0) - z*matrix(2, 0);
65 matrix(3, 1) = y - x*matrix(0, 1) - y*matrix(1, 1) - z*matrix(2, 1);
66 matrix(3, 2) = z - x*matrix(0, 2) - y*matrix(1, 2) - z*matrix(2, 2);
70 SGRotateTransform::SGRotateTransform() :
75 setReferenceFrame(RELATIVE_RF);
79 SGRotateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
80 osg::NodeVisitor* nv) const
82 // This is the fast path, optimize a bit
83 if (_referenceFrame == RELATIVE_RF) {
86 set_rotation(tmp, _angleRad, _center, _axis);
90 set_rotation(tmp, _angleRad, _center, _axis);
97 SGRotateTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
98 osg::NodeVisitor* nv) const
100 if (_referenceFrame == RELATIVE_RF) {
103 set_rotation(tmp, -_angleRad, _center, _axis);
104 matrix.postMult(tmp);
108 set_rotation(tmp, -_angleRad, _center, _axis);
115 SGRotateTransform::computeBound() const
117 osg::BoundingSphere bs = osg::Group::computeBound();
118 osg::BoundingSphere centerbs(_center.osg(), bs.radius());
119 centerbs.expandBy(bs);