1 /* Copyright (C) 2010 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 <math.h>
28 #include <float.h>
29 #include "MathUtil.h"
30 #include "FixedVector3D.h"
31 
CVector3D(const CFixedVector3D & v)32 CVector3D::CVector3D(const CFixedVector3D& v) :
33 	X(v.X.ToFloat()), Y(v.Y.ToFloat()), Z(v.Z.ToFloat())
34 {
35 }
36 
operator !() const37 int CVector3D::operator ! () const
38 {
39 	if (X != 0.0f ||
40 		Y != 0.0f ||
41 		Z != 0.0f)
42 
43 		return 0;
44 
45 	return 1;
46 }
47 
LengthSquared() const48 float CVector3D::LengthSquared () const
49 {
50 	return ( SQR(X) + SQR(Y) + SQR(Z) );
51 }
52 
Length() const53 float CVector3D::Length () const
54 {
55 	return sqrtf ( LengthSquared() );
56 }
57 
Normalize()58 void CVector3D::Normalize ()
59 {
60 	float scale = 1.0f/Length ();
61 
62 	X *= scale;
63 	Y *= scale;
64 	Z *= scale;
65 }
66 
Normalized() const67 CVector3D CVector3D::Normalized () const
68 {
69 	float scale = 1.0f/Length ();
70 
71 	return CVector3D(X * scale, Y * scale, Z * scale);
72 }
73 
74 
75 //-----------------------------------------------------------------------------
76 
MaxComponent(const CVector3D & v)77 float MaxComponent(const CVector3D& v)
78 {
79 	float max = -FLT_MAX;
80 	for(int i = 0; i < 3; i++)
81 		max = std::max(max, v[i]);
82 	return max;
83 }
84