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 #ifndef INCLUDED_VECTOR3D 24 #define INCLUDED_VECTOR3D 25 26 class CFixedVector3D; 27 28 class CVector3D 29 { 30 public: 31 float X, Y, Z; 32 33 public: CVector3D()34 CVector3D() : X(0.0f), Y(0.0f), Z(0.0f) {} CVector3D(float x,float y,float z)35 CVector3D(float x, float y, float z) : X(x), Y(y), Z(z) {} 36 CVector3D(const CFixedVector3D& v); 37 38 int operator!() const; 39 40 float& operator[](int index) { return *((&X)+index); } 41 const float& operator[](int index) const { return *((&X)+index); } 42 43 // vector equality (testing float equality, so please be careful if necessary) 44 bool operator==(const CVector3D &vector) const 45 { 46 return (X == vector.X && Y == vector.Y && Z == vector.Z); 47 } 48 49 bool operator!=(const CVector3D& vector) const 50 { 51 return !operator==(vector); 52 } 53 54 CVector3D operator+(const CVector3D& vector) const 55 { 56 return CVector3D(X + vector.X, Y + vector.Y, Z + vector.Z); 57 } 58 59 CVector3D& operator+=(const CVector3D& vector) 60 { 61 X += vector.X; 62 Y += vector.Y; 63 Z += vector.Z; 64 return *this; 65 } 66 67 CVector3D operator-(const CVector3D& vector) const 68 { 69 return CVector3D(X - vector.X, Y - vector.Y, Z - vector.Z); 70 } 71 72 CVector3D& operator-=(const CVector3D& vector) 73 { 74 X -= vector.X; 75 Y -= vector.Y; 76 Z -= vector.Z; 77 return *this; 78 } 79 80 CVector3D operator*(float value) const 81 { 82 return CVector3D(X * value, Y * value, Z * value); 83 } 84 85 CVector3D& operator*=(float value) 86 { 87 X *= value; 88 Y *= value; 89 Z *= value; 90 return *this; 91 } 92 93 CVector3D operator-() const 94 { 95 return CVector3D(-X, -Y, -Z); 96 } 97 98 public: Dot(const CVector3D & vector)99 float Dot (const CVector3D &vector) const 100 { 101 return ( X * vector.X + 102 Y * vector.Y + 103 Z * vector.Z ); 104 } 105 Cross(const CVector3D & vector)106 CVector3D Cross (const CVector3D &vector) const 107 { 108 CVector3D Temp; 109 Temp.X = (Y * vector.Z) - (Z * vector.Y); 110 Temp.Y = (Z * vector.X) - (X * vector.Z); 111 Temp.Z = (X * vector.Y) - (Y * vector.X); 112 return Temp; 113 } 114 115 float Length () const; 116 float LengthSquared () const; 117 void Normalize (); 118 CVector3D Normalized () const; 119 120 // Returns 3 element array of floats, e.g. for glVertex3fv GetFloatArray()121 const float* GetFloatArray() const { return &X; } 122 }; 123 124 extern float MaxComponent(const CVector3D& v); 125 126 #endif 127