Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vector3D.cpp
Go to the documentation of this file.
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 
33  X(v.X.ToFloat()), Y(v.Y.ToFloat()), Z(v.Z.ToFloat())
34 {
35 }
36 
38 {
39  if (X != 0.0f ||
40  Y != 0.0f ||
41  Z != 0.0f)
42 
43  return 0;
44 
45  return 1;
46 }
47 
48 float CVector3D::Dot (const CVector3D &vector) const
49 {
50  return ( X * vector.X +
51  Y * vector.Y +
52  Z * vector.Z );
53 }
54 
55 CVector3D CVector3D::Cross (const CVector3D &vector) const
56 {
57  CVector3D Temp;
58 
59  Temp.X = (Y * vector.Z) - (Z * vector.Y);
60  Temp.Y = (Z * vector.X) - (X * vector.Z);
61  Temp.Z = (X * vector.Y) - (Y * vector.X);
62 
63  return Temp;
64 }
65 
66 
67 float CVector3D::LengthSquared () const
68 {
69  return ( SQR(X) + SQR(Y) + SQR(Z) );
70 }
71 
72 float CVector3D::Length () const
73 {
74  return sqrtf ( LengthSquared() );
75 }
76 
78 {
79  float scale = 1.0f/Length ();
80 
81  X *= scale;
82  Y *= scale;
83  Z *= scale;
84 }
85 
87 {
88  float scale = 1.0f/Length ();
89 
90  return CVector3D(X * scale, Y * scale, Z * scale);
91 }
92 
93 
94 //-----------------------------------------------------------------------------
95 
96 float MaxComponent(const CVector3D& v)
97 {
98  float max = -FLT_MAX;
99  for(int i = 0; i < 3; i++)
100  max = std::max(max, v[i]);
101  return max;
102 }
CVector3D()
Definition: Vector3D.h:34
Definition: Decompose.h:22
Definition: Decompose.h:22
float Dot(const CVector3D &vector) const
Definition: Vector3D.cpp:48
CVector3D Cross(const CVector3D &vector) const
Definition: Vector3D.cpp:55
#define SQR(x)
Definition: MathUtil.h:23
void Normalize()
Definition: Vector3D.cpp:77
int operator!() const
Definition: Vector3D.cpp:37
float X
Definition: Vector3D.h:31
float Length() const
Definition: Vector3D.cpp:72
float Y
Definition: Vector3D.h:31
Definition: Decompose.h:22
float LengthSquared() const
Definition: Vector3D.cpp:67
float Z
Definition: Vector3D.h:31
float MaxComponent(const CVector3D &v)
Definition: Vector3D.cpp:96
CVector3D Normalized() const
Definition: Vector3D.cpp:86