doom3-bfg/neo/idlib/math/Rotation.h

237 lines
6.3 KiB
C
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code 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 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code 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 Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#ifndef __MATH_ROTATION_H__
#define __MATH_ROTATION_H__
/*
===============================================================================
Describes a complete rotation in degrees about an abritray axis.
A local rotation matrix is stored for fast rotation of multiple points.
===============================================================================
*/
class idAngles;
class idQuat;
class idMat3;
class idRotation
{
2012-11-26 18:58:24 +00:00
friend class idAngles;
friend class idQuat;
friend class idMat3;
2012-11-26 18:58:24 +00:00
public:
idRotation();
idRotation( const idVec3& rotationOrigin, const idVec3& rotationVec, const float rotationAngle );
void Set( const idVec3& rotationOrigin, const idVec3& rotationVec, const float rotationAngle );
void SetOrigin( const idVec3& rotationOrigin );
void SetVec( const idVec3& rotationVec ); // has to be normalized
2012-11-26 18:58:24 +00:00
void SetVec( const float x, const float y, const float z ); // has to be normalized
void SetAngle( const float rotationAngle );
void Scale( const float s );
void ReCalculateMatrix();
const idVec3& GetOrigin() const;
const idVec3& GetVec() const;
2012-11-26 18:58:24 +00:00
float GetAngle() const;
2012-11-26 18:58:24 +00:00
idRotation operator-() const; // flips rotation
idRotation operator*( const float s ) const; // scale rotation
idRotation operator/( const float s ) const; // scale rotation
idRotation& operator*=( const float s ); // scale rotation
idRotation& operator/=( const float s ); // scale rotation
idVec3 operator*( const idVec3& v ) const; // rotate vector
friend idRotation operator*( const float s, const idRotation& r ); // scale rotation
friend idVec3 operator*( const idVec3& v, const idRotation& r ); // rotate vector
friend idVec3& operator*=( idVec3& v, const idRotation& r ); // rotate vector
2012-11-26 18:58:24 +00:00
idAngles ToAngles() const;
idQuat ToQuat() const;
const idMat3& ToMat3() const;
2012-11-26 18:58:24 +00:00
idMat4 ToMat4() const;
idVec3 ToAngularVelocity() const;
void RotatePoint( idVec3& point ) const;
2012-11-26 18:58:24 +00:00
void Normalize180();
void Normalize360();
2012-11-26 18:58:24 +00:00
private:
idVec3 origin; // origin of rotation
idVec3 vec; // normalized vector to rotate around
float angle; // angle of rotation in degrees
mutable idMat3 axis; // rotation axis
mutable bool axisValid; // true if rotation axis is valid
};
ID_INLINE idRotation::idRotation()
{
2012-11-26 18:58:24 +00:00
}
ID_INLINE idRotation::idRotation( const idVec3& rotationOrigin, const idVec3& rotationVec, const float rotationAngle )
{
2012-11-26 18:58:24 +00:00
origin = rotationOrigin;
vec = rotationVec;
angle = rotationAngle;
axisValid = false;
}
ID_INLINE void idRotation::Set( const idVec3& rotationOrigin, const idVec3& rotationVec, const float rotationAngle )
{
2012-11-26 18:58:24 +00:00
origin = rotationOrigin;
vec = rotationVec;
angle = rotationAngle;
axisValid = false;
}
ID_INLINE void idRotation::SetOrigin( const idVec3& rotationOrigin )
{
2012-11-26 18:58:24 +00:00
origin = rotationOrigin;
}
ID_INLINE void idRotation::SetVec( const idVec3& rotationVec )
{
2012-11-26 18:58:24 +00:00
vec = rotationVec;
axisValid = false;
}
ID_INLINE void idRotation::SetVec( float x, float y, float z )
{
2012-11-26 18:58:24 +00:00
vec[0] = x;
vec[1] = y;
vec[2] = z;
axisValid = false;
}
ID_INLINE void idRotation::SetAngle( const float rotationAngle )
{
2012-11-26 18:58:24 +00:00
angle = rotationAngle;
axisValid = false;
}
ID_INLINE void idRotation::Scale( const float s )
{
2012-11-26 18:58:24 +00:00
angle *= s;
axisValid = false;
}
ID_INLINE void idRotation::ReCalculateMatrix()
{
2012-11-26 18:58:24 +00:00
axisValid = false;
ToMat3();
}
ID_INLINE const idVec3& idRotation::GetOrigin() const
{
2012-11-26 18:58:24 +00:00
return origin;
}
ID_INLINE const idVec3& idRotation::GetVec() const
{
2012-11-26 18:58:24 +00:00
return vec;
}
ID_INLINE float idRotation::GetAngle() const
{
2012-11-26 18:58:24 +00:00
return angle;
}
ID_INLINE idRotation idRotation::operator-() const
{
2012-11-26 18:58:24 +00:00
return idRotation( origin, vec, -angle );
}
ID_INLINE idRotation idRotation::operator*( const float s ) const
{
2012-11-26 18:58:24 +00:00
return idRotation( origin, vec, angle * s );
}
ID_INLINE idRotation idRotation::operator/( const float s ) const
{
2012-11-26 18:58:24 +00:00
assert( s != 0.0f );
return idRotation( origin, vec, angle / s );
}
ID_INLINE idRotation& idRotation::operator*=( const float s )
{
2012-11-26 18:58:24 +00:00
angle *= s;
axisValid = false;
return *this;
}
ID_INLINE idRotation& idRotation::operator/=( const float s )
{
2012-11-26 18:58:24 +00:00
assert( s != 0.0f );
angle /= s;
axisValid = false;
return *this;
}
ID_INLINE idVec3 idRotation::operator*( const idVec3& v ) const
{
if( !axisValid )
{
2012-11-26 18:58:24 +00:00
ToMat3();
}
return ( ( v - origin ) * axis + origin );
2012-11-26 18:58:24 +00:00
}
ID_INLINE idRotation operator*( const float s, const idRotation& r )
{
2012-11-26 18:58:24 +00:00
return r * s;
}
ID_INLINE idVec3 operator*( const idVec3& v, const idRotation& r )
{
2012-11-26 18:58:24 +00:00
return r * v;
}
ID_INLINE idVec3& operator*=( idVec3& v, const idRotation& r )
{
2012-11-26 18:58:24 +00:00
v = r * v;
return v;
}
ID_INLINE void idRotation::RotatePoint( idVec3& point ) const
{
if( !axisValid )
{
2012-11-26 18:58:24 +00:00
ToMat3();
}
point = ( ( point - origin ) * axis + origin );
2012-11-26 18:58:24 +00:00
}
#endif /* !__MATH_ROTATION_H__ */