S
Steven T. Hatton
In the following code I have attempted to perform all calculations that are
repeated in the mathematical expression for the rotation being evaluated.
IOW, I calculate cos(_angle) only once, and put it in a variable. Perhaps
a compiler is smart enough to do this for me. I don't know. When I get a
chance I plan on trying to look at the compiler output to determine what is
happening with gcc.
I'm wondering what others think about this kind of hand optimization.
Matrix3<T> setRotation(const Vector3<T>& axis, const T& angle)
{
axis.normal(_axis); // puts unit-length vector parallel to axis in _axis
_angle = angle;
// need to specialize these for float
T c = cos(_angle);
T s = sin(_angle);
T t = T(1) - c;
// references should vanish at compile time
const T& x = axis[0];
const T& y = axis[1];
const T& z = axis[2];
T txy = t * x * y;
T& tyx(txy);
T txz = t * x * z;
T& tzx(txz);
T tyz = t * y * z;
T& tzy(tyz);
T sx = s * x;
T sy = s * y;
T sz = s * z;
// The following is based on the articel found here
// http://www.gamedev.net/reference/articles/article1199.asp
// I have not yet tested the operations to verify my correction to
// the formula in the article are valid.
_data[0][0] = t*x*x+c; _data[0][1] = tyx + sz; _data[0][2] = tzx-sy;
_data[1][0] = txy-sz; _data[1][1] = t*y*y+c; _data[1][2] = tzy+sx;
_data[2][0] = txz+sy; _data[2][1] = tyz-sx; _data[2][2] = tzz+c;
/* my original form
_data[0][0]=t*x*x+c; _data[0][1]=t*y*x+s*z; _data[0][2]=t*z*x-s*y;
_data[1][0]=t*x*y-s*z; _data[1][1]=t*y*y+c; _data[1][2]=t*z*y+s*x;
_data[2][0]=t*x*z+s*y; _data[2][1]=t*y*z-s*x; _data[2][2]=t*z*z+c;
*/
}
/***************************************************************************
* Copyright (C) 2004 by Steven T. Hatton *
* (e-mail address removed) *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* Gunax lbh sbe gur Qbanyq Xahgu dhbgr. Yvxrjvfr ba vg'f vzcyrzragngvba *
* qrcraqrag. *
***************************************************************************/
repeated in the mathematical expression for the rotation being evaluated.
IOW, I calculate cos(_angle) only once, and put it in a variable. Perhaps
a compiler is smart enough to do this for me. I don't know. When I get a
chance I plan on trying to look at the compiler output to determine what is
happening with gcc.
I'm wondering what others think about this kind of hand optimization.
Matrix3<T> setRotation(const Vector3<T>& axis, const T& angle)
{
axis.normal(_axis); // puts unit-length vector parallel to axis in _axis
_angle = angle;
// need to specialize these for float
T c = cos(_angle);
T s = sin(_angle);
T t = T(1) - c;
// references should vanish at compile time
const T& x = axis[0];
const T& y = axis[1];
const T& z = axis[2];
T txy = t * x * y;
T& tyx(txy);
T txz = t * x * z;
T& tzx(txz);
T tyz = t * y * z;
T& tzy(tyz);
T sx = s * x;
T sy = s * y;
T sz = s * z;
// The following is based on the articel found here
// http://www.gamedev.net/reference/articles/article1199.asp
// I have not yet tested the operations to verify my correction to
// the formula in the article are valid.
_data[0][0] = t*x*x+c; _data[0][1] = tyx + sz; _data[0][2] = tzx-sy;
_data[1][0] = txy-sz; _data[1][1] = t*y*y+c; _data[1][2] = tzy+sx;
_data[2][0] = txz+sy; _data[2][1] = tyz-sx; _data[2][2] = tzz+c;
/* my original form
_data[0][0]=t*x*x+c; _data[0][1]=t*y*x+s*z; _data[0][2]=t*z*x-s*y;
_data[1][0]=t*x*y-s*z; _data[1][1]=t*y*y+c; _data[1][2]=t*z*y+s*x;
_data[2][0]=t*x*z+s*y; _data[2][1]=t*y*z-s*x; _data[2][2]=t*z*z+c;
*/
}
/***************************************************************************
* Copyright (C) 2004 by Steven T. Hatton *
* (e-mail address removed) *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* Gunax lbh sbe gur Qbanyq Xahgu dhbgr. Yvxrjvfr ba vg'f vzcyrzragngvba *
* qrcraqrag. *
***************************************************************************/