Fix the removal of implicit conversions in libfmt 10 by using explicit casts.
[0ad.git] / source / maths / Vector3D.cpp
blob730b94283d71802b0863e1f7b8afc2da13281b04
1 /* Copyright (C) 2020 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
19 * Provides an interface for a vector in R3 and allows vector and
20 * scalar operations on it
23 #include "precompiled.h"
25 #include "Vector3D.h"
27 #include "MathUtil.h"
28 #include "FixedVector3D.h"
30 #include <cmath>
31 #include <algorithm>
32 #include <limits>
34 CVector3D::CVector3D(const CFixedVector3D& v) :
35 X(v.X.ToFloat()), Y(v.Y.ToFloat()), Z(v.Z.ToFloat())
39 // static
40 CVector3D CVector3D::Max()
42 const float max_float = std::numeric_limits<float>::max();
43 return CVector3D(max_float, max_float, max_float);
46 // static
47 CVector3D CVector3D::Min()
49 const float max_float = std::numeric_limits<float>::max();
50 return CVector3D(-max_float, -max_float, -max_float);
53 int CVector3D::operator ! () const
55 if (X != 0.0f ||
56 Y != 0.0f ||
57 Z != 0.0f)
59 return 0;
61 return 1;
64 float CVector3D::LengthSquared () const
66 return ( SQR(X) + SQR(Y) + SQR(Z) );
69 float CVector3D::Length () const
71 return sqrtf ( LengthSquared() );
74 void CVector3D::Normalize ()
76 float scale = 1.0f/Length ();
78 X *= scale;
79 Y *= scale;
80 Z *= scale;
83 CVector3D CVector3D::Normalized () const
85 float scale = 1.0f/Length ();
87 return CVector3D(X * scale, Y * scale, Z * scale);
91 //-----------------------------------------------------------------------------
93 float MaxComponent(const CVector3D& v)
95 return std::max({v.X, v.Y, v.Z});