2 #include "OsgMatrixUtil.h"
10 for (
int i = 0; i < 4; ++i)
12 for (
int j = 0; j < 4; ++j)
14 std::cout <<
"[" << matrix(i,j) <<
"]";
16 std::cout << std::endl;
18 std::cout << std::endl;
24 for (
int i = 0; i < 3; ++i)
26 std::cout <<
"[" << vec[i] <<
"]";
28 std::cout << std::endl;
34 for (
int i = 0; i < 4; ++i)
36 std::cout <<
"[" << vec[i] <<
"]";
38 std::cout << std::endl;
44 for (
int i = 0; i < 4; ++i)
46 for (
int j = 0; j < 4; ++j)
54 osg::Vec3 OsgMatrixUtil::GetRow3(
const osg::Matrix& matrix,
int row)
56 return osg::Vec3(matrix(row,0), matrix(row, 1), matrix(row,2));
60 osg::Vec4 OsgMatrixUtil::GetRow4(
const osg::Matrix& matrix,
int row)
62 return osg::Vec4(matrix(row,0), matrix(row,1), matrix(row,2), matrix(row,3));
66 void OsgMatrixUtil::SetRow(osg::Matrix& matrix,
const osg::Vec3& vec,
int row)
68 for (
int i = 0; i < 3; ++i)
70 matrix(row,i) = vec[i];
75 void OsgMatrixUtil::SetRow(osg::Matrix& matrix,
const osg::Vec4& vec,
int row)
77 for (
int i = 0; i < 4; ++i)
79 matrix(row, i) = vec[i];
92 double ch, sh, cp, sp, cr, sr, srsp, crsp, srcp;
96 const osg::Vec3::value_type magic_epsilon = (osg::Vec3::value_type)0.00001;
98 if (osg::equivalent(hpr[0], (osg::Vec3::value_type)0.0, magic_epsilon))
105 sh = sinf(osg::DegreesToRadians(hpr[0]));
106 ch = cosf(osg::DegreesToRadians(hpr[0]));
109 if (osg::equivalent(hpr[1], (osg::Vec3::value_type)0.0, magic_epsilon))
116 sp = sinf(osg::DegreesToRadians(hpr[1]));
117 cp = cosf(osg::DegreesToRadians(hpr[1]));
120 if (osg::equivalent(hpr[2], (osg::Vec3::value_type)0.0, magic_epsilon))
130 sr = sinf(osg::DegreesToRadians(hpr[2]));
131 cr = cosf(osg::DegreesToRadians(hpr[2]));
137 rotation(0, 0) = ch * cr - sh * srsp;
138 rotation(1, 0) = -sh * cp;
139 rotation(2, 0) = sr * ch + sh * crsp;
141 rotation(0, 1) = cr * sh + srsp * ch;
142 rotation(1, 1) = ch * cp;
143 rotation(2, 1) = sr * sh - crsp * ch;
145 rotation(0, 2) = -srcp;
147 rotation(2, 2) = cr * cp;
149 rotation(3, 0) = 0.0;
150 rotation(3, 1) = 0.0;
151 rotation(3, 2) = 0.0;
153 rotation(0, 3) = 0.0;
154 rotation(1, 3) = 0.0;
155 rotation(2, 3) = 0.0;
156 rotation(3, 3) = 1.0;
159 void OsgMatrixUtil::PositionAndHprRadToMatrix(osg::Matrix& rotation,
const osg::Vec3& xyz,
const osg::Vec3& hpr)
162 vHpr[0] = osg::RadiansToDegrees(hpr[0]);
163 vHpr[1] = osg::RadiansToDegrees(hpr[1]);
164 vHpr[2] = osg::RadiansToDegrees(hpr[2]);
173 rotation(3, 0) = xyz[0];
174 rotation(3, 1) = xyz[1];
175 rotation(3, 2) = xyz[2];
181 hpr[0] = osg::DegreesToRadians(hpr[0]);
182 hpr[1] = osg::DegreesToRadians(hpr[1]);
183 hpr[2] = osg::DegreesToRadians(hpr[2]);
195 osg::Vec3 col1(rotation(0, 0), rotation(0, 1), rotation(0, 2));
196 double s = col1.length();
198 const double magic_epsilon = 0.00001;
199 if (s <= magic_epsilon)
201 hpr.set(0.0f, 0.0f, 0.0f);
206 double oneOverS = 1.0f / s;
207 for (
int i = 0; i < 3; ++i)
209 for (
int j = 0; j < 3; ++j)
211 mat(i, j) = rotation(i, j) * oneOverS;
217 double pitch = asin(sin_pitch);
218 hpr[1] = osg::RadiansToDegrees(pitch);
220 double cp = cos(pitch);
222 if (cp > -magic_epsilon && cp < magic_epsilon)
236 hpr[2] = osg::RadiansToDegrees(atan2(sr,cr));
240 double one_over_cp = 1.0 / cp;
241 double sr =
ClampUnity(-mat(0,2) * one_over_cp);
242 double cr =
ClampUnity( mat(2,2) * one_over_cp);
243 double sh =
ClampUnity(-mat(1,0) * one_over_cp);
244 double ch =
ClampUnity( mat(1,1) * one_over_cp);
246 if ((osg::equivalent(sh,0.0,magic_epsilon) && osg::equivalent(ch,0.0,magic_epsilon)) ||
247 (osg::equivalent(sr,0.0,magic_epsilon) && osg::equivalent(cr,0.0,magic_epsilon)) )
256 hpr[0] = osg::RadiansToDegrees(atan2(sh, ch));
259 hpr[2] = osg::RadiansToDegrees(atan2(sr, cr));
266 if (x > 1.0f) {
return 1.0f; }
267 if (x < -1.0f) {
return -1.0f; }
275 xyz[0] = rotation(3, 0);
276 xyz[1] = rotation(3, 1);
277 xyz[2] = rotation(3, 2);
289 vec_in = osg::Matrix::transform3x3(xyz, transformMat);
290 vec_in[0] += transformMat(3,0);
291 vec_in[1] += transformMat(3,1);
292 vec_in[2] += transformMat(3,2);
295 osg::Matrix OsgMatrixUtil::SetupMatrix(CStdFPoint &localPos, CStdFPoint &localRot)
297 osg::Vec3 vPos(localPos.x, localPos.y, localPos.z);
298 osg::Matrix osgLocalMatrix;
299 osgLocalMatrix.makeIdentity();
300 osgLocalMatrix.makeRotate(localRot.z, osg::Vec3d(0, 0, 1), localRot.y, osg::Vec3d(0, 1, 0), localRot.x, osg::Vec3d(1, 0, 0));
301 osgLocalMatrix.setTrans(vPos);
302 return osgLocalMatrix;
315 CStdFPoint OsgMatrixUtil::EulerRotationFromMatrix(osg::Matrix osgMT)
317 return EulerRotationFromMatrix_Static(osgMT);
320 CStdFPoint OsgMatrixUtil::EulerRotationFromMatrix_Static (osg::Matrix osgMT)
324 osg::Matrix3 osgMT3(osgMT(0, 0), osgMT(1, 0), osgMT(2, 0),
325 osgMT(0, 1), osgMT(1, 1), osgMT(2, 1),
326 osgMT(0, 2), osgMT(1, 2), osgMT(2, 2));
333 float xAngle=0, yAngle=0, zAngle=0;
335 if (osgMT3(0, 2) < 1)
337 if (osgMT3(0, 2) > -1)
342 yAngle = (float) asin((
double) osgMT3(0, 2));
343 xAngle = (float) atan2((
double) -osgMT3(1, 2), (double) osgMT3(2, 2));
344 zAngle = (float) atan2((
double) -osgMT3(0, 1), (double) osgMT3(0, 0));
351 yAngle = -(osg::PI/2);
352 xAngle = -atan2((
double) osgMT3(1, 0), (
double) osgMT3(1, 1));
362 xAngle = atan2((
double) osgMT3(1, 0), (
double) osgMT3(1, 1));
366 CStdFPoint vRot(xAngle, yAngle, zAngle);
367 vRot.ClearNearZero();
static void HprToMatrix(osg::Matrix &rotation, const osg::Vec3 &hpr)
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
static void Print(const osg::Matrix &matrix)
Prints a matrix.
static void MatrixToHprRad(osg::Vec3 &hpr, const osg::Matrix &rotation)
static float ClampUnity(float x)
clamps a float from -1 to 1
static void PositionAndHprToMatrix(osg::Matrix &rotation, const osg::Vec3 &xyz, const osg::Vec3 &hpr)
static void TransformVec3(osg::Vec3 &xyz, const osg::Matrix &transformMat)
static void MatrixToHprAndPosition(osg::Vec3 &xyz, osg::Vec3 &hpr, const osg::Matrix &rotation)
static void MatrixToHpr(osg::Vec3 &hpr, const osg::Matrix &rotation)
static void Transpose(osg::Matrix &dest, const osg::Matrix &src)
transposes a matrix