Added proper operator[] functions so that Vec3[i] can be

used on the left side of an assignment.
Also fixed coding style and added documentation.


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@11106 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk
2012-04-10 12:43:15 +00:00
parent 09917fc3a3
commit cddea4ae65

View File

@@ -34,7 +34,6 @@ using namespace irr;
class Vec3 : public btVector3
{
private:
inline float clampToUnity(float f) {return f<-1?f:(f>1?1:f);}
void setPitchRoll(const Vec3 &normal);
public:
@@ -47,28 +46,66 @@ public:
* a vec3).
*/
inline Vec3(const core::vector3df &v) : btVector3(v.X, v.Y, v.Z) {}
// ------------------------------------------------------------------------
/** Initialises a vector from a btVector3 (or a Vec3). */
inline Vec3(const btVector3& a) : btVector3(a) {}
// ------------------------------------------------------------------------
/** Empty constructor. */
inline Vec3() : btVector3() {}
// ------------------------------------------------------------------------
/** Creates a 3d vector from three scalars. */
inline Vec3(float x, float y, float z) : btVector3(x,y,z) {}
// ------------------------------------------------------------------------
/** Initialises a 3d vector from one scalar value, which is used to
* initialise all components. */
inline Vec3(float x) : btVector3(x,x,x) {}
// ------------------------------------------------------------------------
/** Sets the heading, and computes pitch and roll dependent
* on the normal it is displayed on.
* \param heading The heading to set.
* \param normal The normal from which pitch and roll should be computed. */
* \param normal The normal from which pitch and roll should be
* computed. */
inline Vec3(float heading, const Vec3& normal)
{setHeading(heading);
setPitchRoll(normal);}
{
setHeading(heading);
setPitchRoll(normal);
} // Vec3(heading, normal)
void setHPR(const btQuaternion& q);
inline const float operator[](int n) const { return *(&m_floats[0]+n); }
inline const float getHeading() const { return m_floats[1]; }
inline const float getPitch() const { return m_floats[0]; }
inline const float getRoll() const { return m_floats[2]; }
inline const void setHeading(float f) { m_floats[1] = f; }
inline const void setPitch(float f) { m_floats[0] = f; }
inline const void setRoll(float f) { m_floats[2] = f; }
// ------------------------------------------------------------------------
/** Converts a vec3 into an irrlicht vector (which is a simple type cast). */
/** Sets the heading, pitch, roll of this vector that is used to store a
* rotation from a quaternion. */
void setHPR(const btQuaternion& q);
// ------------------------------------------------------------------------
/** Returns a reference to the n-th element (x=0, y=1, z=2, w=3). */
inline const float& operator[](int n) const { return m_floats[n]; }
// ------------------------------------------------------------------------
/** Returns a reference to the n-th element (x=0, y=1, z=2, w=3). */
inline float& operator[](int n) { return m_floats[n]; }
// ------------------------------------------------------------------------
/** Returns the heading of a vector that is used to store a rotation. */
inline const float getHeading() const { return m_floats[1]; }
// ------------------------------------------------------------------------
/** Returns the pitch of a vector that is used to store a rotation. */
inline const float getPitch() const { return m_floats[0]; }
// ------------------------------------------------------------------------
/** Returns the roll of a vector that is used to store a rotation. */
inline const float getRoll() const { return m_floats[2]; }
// ------------------------------------------------------------------------
/** Returns the W component (bullet vectors contain 4 elements, the last
* element is usually unused). */
inline const float getW() const { return m_floats[3]; }
// ------------------------------------------------------------------------
/** Sets the heading of a vector that is used to store a rotation. */
inline const void setHeading(float f) { m_floats[1] = f; }
// ------------------------------------------------------------------------
/** Sets the pitch of a vector that is used to store a rotation. */
inline const void setPitch(float f) { m_floats[0] = f; }
// ------------------------------------------------------------------------
/** Sets the roll of a vector that is used to store a rotation. */
inline const void setRoll(float f) { m_floats[2] = f; }
// ------------------------------------------------------------------------
/** Converts a vec3 into an irrlicht vector (which is a simple type
* cast). */
const core::vector3df& toIrrVector() const
{
return (const core::vector3df&)*this;
@@ -96,32 +133,39 @@ public:
m_floats[2]*=DEGREE_TO_RAD;
} // degreeToRad
// ------------------------------------------------------------------------
Vec3& operator=(const btVector3& a) {*(btVector3*)this=a; return *this;}
/** Sets this = a. */
Vec3& operator=(const btVector3& a) {*(btVector3*)this=a; return *this;}
// ------------------------------------------------------------------------
Vec3& operator=(const btQuaternion& q){setHPR(q); return *this;}
/** Sets the rotation given by the quaternion as HPR vector. */
Vec3& operator=(const btQuaternion& q) {setHPR(q); return *this;}
// ------------------------------------------------------------------------
Vec3 operator-(const Vec3& v1) const {return (Vec3)(*(btVector3*)this-(btVector3)v1);}
/** Computes this = this - v1. */
Vec3 operator-(const Vec3& v1) const {return (Vec3)(*(btVector3*)this
-(btVector3)v1); }
// ------------------------------------------------------------------------
/** Helper functions to treat this vec3 as a 2d vector. This returns the
* square of the length of the first 2 dimensions. */
float length2_2d() const {return m_floats[0]*m_floats[0] + m_floats[2]*m_floats[2];}
float length2_2d() const { return m_floats[0]*m_floats[0]
+ m_floats[2]*m_floats[2]; }
// ------------------------------------------------------------------------
/** Returns the length of this vector in the plane, i.e. the vector is
* used as a 2d vector. */
// ------------------------------------------------------------------------
float length_2d() const {return sqrt(m_floats[0]*m_floats[0] + m_floats[2]*m_floats[2]);}
/** Returns the length of the vector using only the x/z coordinates. */
float length_2d() const {return sqrt( m_floats[0]*m_floats[0]
+ m_floats[2]*m_floats[2]);}
// ------------------------------------------------------------------------
/** Sets this = max(this, a) componentwise.
* \param Vector to compare with. */
void max(const Vec3& a) {if(a.getX()>m_floats[0]) m_floats[0]=a.getX();
if(a.getY()>m_floats[1]) m_floats[1]=a.getY();
if(a.getZ()>m_floats[2]) m_floats[2]=a.getZ();}
void max(const Vec3& a) {if(a.getX()>m_floats[0]) m_floats[0]=a.getX();
if(a.getY()>m_floats[1]) m_floats[1]=a.getY();
if(a.getZ()>m_floats[2]) m_floats[2]=a.getZ();}
// ------------------------------------------------------------------------
/** Sets this = min(this, a) componentwise.
* \param a Vector to compare with. */
void min(const Vec3& a) {if(a.getX()<m_floats[0]) m_floats[0]=a.getX();
if(a.getY()<m_floats[1]) m_floats[1]=a.getY();
if(a.getZ()<m_floats[2]) m_floats[2]=a.getZ();}
void min(const Vec3& a) {if(a.getX()<m_floats[0]) m_floats[0]=a.getX();
if(a.getY()<m_floats[1]) m_floats[1]=a.getY();
if(a.getZ()<m_floats[2]) m_floats[2]=a.getZ();}
// ------------------------------------------------------------------------
/** Determines which side of a line this point is. This is using
* a 2d projection (into the X-Z plane).