2007-05-07 09:33:24 -07:00
|
|
|
/**
|
|
|
|
|
* =========================================================================
|
|
|
|
|
* File : Vector3D.cpp
|
|
|
|
|
* Project : 0 A.D.
|
|
|
|
|
* Description : Provides an interface for a vector in R3 and
|
|
|
|
|
* allows vector and scalar operations on it
|
|
|
|
|
* =========================================================================
|
|
|
|
|
*/
|
2004-05-29 13:53:40 -07:00
|
|
|
|
2004-06-03 11:38:14 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
2004-05-29 13:53:40 -07:00
|
|
|
#include "Vector3D.h"
|
|
|
|
|
|
2005-09-29 17:59:42 -07:00
|
|
|
#include <math.h>
|
2007-09-02 16:38:58 -07:00
|
|
|
#include <float.h>
|
2005-09-29 17:59:42 -07:00
|
|
|
#include "MathUtil.h"
|
|
|
|
|
|
2004-05-29 13:53:40 -07:00
|
|
|
int CVector3D::operator ! () const
|
|
|
|
|
{
|
|
|
|
|
if (X != 0.0f ||
|
|
|
|
|
Y != 0.0f ||
|
|
|
|
|
Z != 0.0f)
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-09-14 22:31:49 -07:00
|
|
|
bool CVector3D::operator== (const CVector3D &vector) const
|
|
|
|
|
{
|
|
|
|
|
return (X == vector.X && Y == vector.Y && Z == vector.Z);
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-29 13:53:40 -07:00
|
|
|
//vector addition
|
|
|
|
|
CVector3D CVector3D::operator + (const CVector3D &vector) const
|
|
|
|
|
{
|
2005-02-09 15:19:48 -08:00
|
|
|
return CVector3D(X+vector.X, Y+vector.Y, Z+vector.Z);
|
2004-05-29 13:53:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//vector addition/assignment
|
|
|
|
|
CVector3D &CVector3D::operator += (const CVector3D &vector)
|
|
|
|
|
{
|
|
|
|
|
X += vector.X;
|
|
|
|
|
Y += vector.Y;
|
|
|
|
|
Z += vector.Z;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//vector subtraction
|
|
|
|
|
CVector3D CVector3D::operator - (const CVector3D &vector) const
|
|
|
|
|
{
|
2005-02-09 15:19:48 -08:00
|
|
|
return CVector3D(X-vector.X, Y-vector.Y, Z-vector.Z);
|
2004-05-29 13:53:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//vector negation
|
|
|
|
|
CVector3D CVector3D::operator-() const
|
|
|
|
|
{
|
2005-02-09 15:19:48 -08:00
|
|
|
return CVector3D(-X, -Y, -Z);
|
2004-05-29 13:53:40 -07:00
|
|
|
}
|
|
|
|
|
//vector subtrcation/assignment
|
|
|
|
|
CVector3D &CVector3D::operator -= (const CVector3D &vector)
|
|
|
|
|
{
|
|
|
|
|
X -= vector.X;
|
|
|
|
|
Y -= vector.Y;
|
|
|
|
|
Z -= vector.Z;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//scalar multiplication
|
|
|
|
|
CVector3D CVector3D::operator * (float value) const
|
|
|
|
|
{
|
2005-02-09 15:19:48 -08:00
|
|
|
return CVector3D(X*value, Y*value, Z*value);
|
2004-05-29 13:53:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//scalar multiplication/assignment
|
|
|
|
|
CVector3D& CVector3D::operator *= (float value)
|
|
|
|
|
{
|
|
|
|
|
X *= value;
|
|
|
|
|
Y *= value;
|
|
|
|
|
Z *= value;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CVector3D::Set (float x, float y, float z)
|
|
|
|
|
{
|
|
|
|
|
X = x;
|
|
|
|
|
Y = y;
|
|
|
|
|
Z = z;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CVector3D::Clear ()
|
|
|
|
|
{
|
|
|
|
|
X = Y = Z = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Dot product
|
|
|
|
|
float CVector3D::Dot (const CVector3D &vector) const
|
|
|
|
|
{
|
|
|
|
|
return ( X * vector.X +
|
|
|
|
|
Y * vector.Y +
|
|
|
|
|
Z * vector.Z );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Cross product
|
|
|
|
|
CVector3D CVector3D::Cross (const CVector3D &vector) const
|
|
|
|
|
{
|
|
|
|
|
CVector3D Temp;
|
|
|
|
|
|
|
|
|
|
Temp.X = (Y * vector.Z) - (Z * vector.Y);
|
|
|
|
|
Temp.Y = (Z * vector.X) - (X * vector.Z);
|
|
|
|
|
Temp.Z = (X * vector.Y) - (Y * vector.X);
|
|
|
|
|
|
|
|
|
|
return Temp;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-16 20:53:54 -07:00
|
|
|
|
|
|
|
|
float CVector3D::LengthSquared () const
|
|
|
|
|
{
|
|
|
|
|
return ( SQR(X) + SQR(Y) + SQR(Z) );
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
float CVector3D::Length () const
|
2004-05-29 13:53:40 -07:00
|
|
|
{
|
2006-05-16 20:53:54 -07:00
|
|
|
return sqrtf ( LengthSquared() );
|
2004-05-29 13:53:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CVector3D::Normalize ()
|
|
|
|
|
{
|
2007-05-02 05:07:08 -07:00
|
|
|
float scale = 1.0f/Length ();
|
2004-05-29 13:53:40 -07:00
|
|
|
|
|
|
|
|
X *= scale;
|
|
|
|
|
Y *= scale;
|
|
|
|
|
Z *= scale;
|
|
|
|
|
}
|
2007-05-29 15:47:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
float MaxComponent(const CVector3D& v)
|
|
|
|
|
{
|
|
|
|
|
float max = -FLT_MAX;
|
|
|
|
|
for(int i = 0; i < 3; i++)
|
|
|
|
|
max = std::max(max, v[i]);
|
|
|
|
|
return max;
|
|
|
|
|
}
|