Line data Source code
1 : /* Copyright (C) 2020 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
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.
8 : *
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.
13 : *
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/>.
16 : */
17 :
18 : /*
19 : * Provides an interface for a vector in R3 and allows vector and
20 : * scalar operations on it
21 : */
22 :
23 : #include "precompiled.h"
24 :
25 : #include "Vector3D.h"
26 :
27 : #include "MathUtil.h"
28 : #include "FixedVector3D.h"
29 :
30 : #include <cmath>
31 : #include <algorithm>
32 : #include <limits>
33 :
34 8 : CVector3D::CVector3D(const CFixedVector3D& v) :
35 8 : X(v.X.ToFloat()), Y(v.Y.ToFloat()), Z(v.Z.ToFloat())
36 : {
37 8 : }
38 :
39 : // static
40 1443 : CVector3D CVector3D::Max()
41 : {
42 1443 : const float max_float = std::numeric_limits<float>::max();
43 1443 : return CVector3D(max_float, max_float, max_float);
44 : }
45 :
46 : // static
47 1435 : CVector3D CVector3D::Min()
48 : {
49 1435 : const float max_float = std::numeric_limits<float>::max();
50 1435 : return CVector3D(-max_float, -max_float, -max_float);
51 : }
52 :
53 0 : int CVector3D::operator ! () const
54 : {
55 0 : if (X != 0.0f ||
56 0 : Y != 0.0f ||
57 0 : Z != 0.0f)
58 :
59 0 : return 0;
60 :
61 0 : return 1;
62 : }
63 :
64 172 : float CVector3D::LengthSquared () const
65 : {
66 172 : return ( SQR(X) + SQR(Y) + SQR(Z) );
67 : }
68 :
69 142 : float CVector3D::Length () const
70 : {
71 142 : return sqrtf ( LengthSquared() );
72 : }
73 :
74 60 : void CVector3D::Normalize ()
75 : {
76 60 : float scale = 1.0f/Length ();
77 :
78 60 : X *= scale;
79 60 : Y *= scale;
80 60 : Z *= scale;
81 60 : }
82 :
83 21 : CVector3D CVector3D::Normalized () const
84 : {
85 21 : float scale = 1.0f/Length ();
86 :
87 21 : return CVector3D(X * scale, Y * scale, Z * scale);
88 : }
89 :
90 :
91 : //-----------------------------------------------------------------------------
92 :
93 0 : float MaxComponent(const CVector3D& v)
94 : {
95 0 : return std::max({v.X, v.Y, v.Z});
96 3 : }
|