2020-12-09 15:39:14 +01:00
|
|
|
/* Copyright (C) 2020 Wildfire Games.
|
2023-12-03 01:30:12 +01:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 19:00:33 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 19:00:33 +02:00
|
|
|
* 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
|
2023-12-03 01:30:12 +01:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
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 "MathUtil.h"
|
2010-03-07 22:38:39 +01:00
|
|
|
#include "FixedVector3D.h"
|
|
|
|
|
2019-06-12 22:23:14 +02:00
|
|
|
#include <cmath>
|
2020-12-09 15:39:14 +01:00
|
|
|
#include <algorithm>
|
2019-06-12 22:23:14 +02:00
|
|
|
#include <limits>
|
|
|
|
|
2010-03-07 22:38:39 +01:00
|
|
|
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
|
|
|
|
2019-06-12 22:23:14 +02:00
|
|
|
// static
|
|
|
|
CVector3D CVector3D::Max()
|
|
|
|
{
|
|
|
|
const float max_float = std::numeric_limits<float>::max();
|
|
|
|
return CVector3D(max_float, max_float, max_float);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CVector3D CVector3D::Min()
|
|
|
|
{
|
|
|
|
const float max_float = std::numeric_limits<float>::max();
|
|
|
|
return CVector3D(-max_float, -max_float, -max_float);
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
int CVector3D::operator ! () const
|
2004-05-29 22:53:40 +02:00
|
|
|
{
|
|
|
|
if (X != 0.0f ||
|
|
|
|
Y != 0.0f ||
|
|
|
|
Z != 0.0f)
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2004-05-29 22:53:40 +02:00
|
|
|
return 0;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2004-05-29 22:53:40 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
float CVector3D::LengthSquared () const
|
2006-05-17 05:53:54 +02:00
|
|
|
{
|
|
|
|
return ( SQR(X) + SQR(Y) + SQR(Z) );
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01: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)
|
|
|
|
{
|
2019-06-12 22:23:14 +02:00
|
|
|
return std::max({v.X, v.Y, v.Z});
|
2007-05-30 00:47:26 +02:00
|
|
|
}
|