2010-09-24 18:54:20 +02:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* Provides an interface for a vector in R3 and allows vector and
|
|
|
|
* scalar operations on it
|
2007-05-07 18:33:24 +02:00
|
|
|
*/
|
2004-05-29 22:53:40 +02:00
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-05-29 22:53:40 +02:00
|
|
|
#include "Vector3D.h"
|
|
|
|
|
2005-09-30 02:59:42 +02:00
|
|
|
#include <math.h>
|
2007-09-03 01:38:58 +02:00
|
|
|
#include <float.h>
|
2005-09-30 02:59:42 +02:00
|
|
|
#include "MathUtil.h"
|
2010-03-07 22:38:39 +01:00
|
|
|
#include "FixedVector3D.h"
|
|
|
|
|
|
|
|
CVector3D::CVector3D(const CFixedVector3D& v) :
|
|
|
|
X(v.X.ToFloat()), Y(v.Y.ToFloat()), Z(v.Z.ToFloat())
|
|
|
|
{
|
|
|
|
}
|
2005-09-30 02:59:42 +02:00
|
|
|
|
2004-05-29 22:53:40 +02:00
|
|
|
int CVector3D::operator ! () const
|
|
|
|
{
|
|
|
|
if (X != 0.0f ||
|
|
|
|
Y != 0.0f ||
|
|
|
|
Z != 0.0f)
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CVector3D::Dot (const CVector3D &vector) const
|
|
|
|
{
|
|
|
|
return ( X * vector.X +
|
|
|
|
Y * vector.Y +
|
|
|
|
Z * vector.Z );
|
|
|
|
}
|
|
|
|
|
|
|
|
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-17 05:53:54 +02:00
|
|
|
|
|
|
|
float CVector3D::LengthSquared () const
|
|
|
|
{
|
|
|
|
return ( SQR(X) + SQR(Y) + SQR(Z) );
|
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
float CVector3D::Length () const
|
2004-05-29 22:53:40 +02:00
|
|
|
{
|
2006-05-17 05:53:54 +02:00
|
|
|
return sqrtf ( LengthSquared() );
|
2004-05-29 22:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVector3D::Normalize ()
|
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
float scale = 1.0f/Length ();
|
2004-05-29 22:53:40 +02:00
|
|
|
|
|
|
|
X *= scale;
|
|
|
|
Y *= scale;
|
|
|
|
Z *= scale;
|
|
|
|
}
|
2007-05-30 00:47:26 +02:00
|
|
|
|
2011-03-04 21:02:05 +01:00
|
|
|
CVector3D CVector3D::Normalized () const
|
|
|
|
{
|
|
|
|
float scale = 1.0f/Length ();
|
|
|
|
|
|
|
|
return CVector3D(X * scale, Y * scale, Z * scale);
|
|
|
|
}
|
|
|
|
|
2007-05-30 00:47:26 +02: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;
|
|
|
|
}
|