00001 #ifndef __VECTOR_H__
00002 #define __VECTOR_H__
00003
00004 #include "Common/CommonDll.h"
00005 #include <math.h>
00006
00007
00008 class COMMON_API KVector
00009 {
00010 public:
00011 float x;
00012 float y;
00013 float z;
00014
00015
00017
00019 inline KVector()
00020 {
00021
00022
00023
00024 }
00025
00026 inline KVector( float x, float y, float z )
00027 {
00028 this->x = x;
00029 this->y = y;
00030 this->z = z;
00031 }
00032
00034
00036 inline KVector operator + ( KVector& v )
00037 {
00038 KVector result;
00039
00040 result.x = x + v.x;
00041 result.y = y + v.y;
00042 result.z = z + v.z;
00043
00044 return result;
00045 }
00046
00047 inline KVector operator - ( KVector& v )
00048 {
00049 KVector result;
00050
00051 result.x = x - v.x;
00052 result.y = y - v.y;
00053 result.z = z - v.z;
00054
00055 return result;
00056 }
00057
00058 inline KVector operator * ( KVector& v )
00059 {
00060 KVector result;
00061
00062 result.x = x * v.x;
00063 result.y = y * v.y;
00064 result.z = z * v.z;
00065
00066 return result;
00067 }
00068
00069 inline KVector operator / ( KVector& v )
00070 {
00071 KVector result;
00072
00073 result.x = x / v.x;
00074 result.y = y / v.y;
00075 result.z = z / v.z;
00076
00077 return result;
00078 }
00079
00080 inline KVector operator - ( )
00081 {
00082 KVector result;
00083
00084 result.x = -x;
00085 result.y = -y;
00086 result.z = -z;
00087
00088 return result;
00089 }
00090
00091 inline void operator += ( KVector& v )
00092 {
00093 x += v.x;
00094 y += v.y;
00095 z += v.z;
00096 }
00097
00098 inline void operator -= ( KVector& v )
00099 {
00100 x -= v.x;
00101 y -= v.y;
00102 z -= v.z;
00103 }
00104
00105 inline void operator *= ( KVector& v )
00106 {
00107 x *= v.x;
00108 y *= v.y;
00109 z *= v.z;
00110 }
00111
00112 inline void operator /= ( KVector& v )
00113 {
00114 x /= v.x;
00115 y /= v.y;
00116 z /= v.z;
00117 }
00118
00119
00121 inline KVector operator * ( float f )
00122 {
00123 KVector result;
00124
00125 result.x = x * f;
00126 result.y = y * f;
00127 result.z = z * f;
00128
00129 return result;
00130 }
00131
00132 inline void operator *= ( float f )
00133 {
00134 x *= f;
00135 y *= f;
00136 z *= f;
00137 }
00138
00139 inline KVector operator / ( float f )
00140 {
00141 KVector result;
00142
00143 result.x = x / f;
00144 result.y = y / f;
00145 result.z = z / f;
00146
00147 return result;
00148 }
00149
00150 inline void operator /= ( float f )
00151 {
00152 x /= f;
00153 y /= f;
00154 z /= f;
00155 }
00156
00157 inline bool operator == ( KVector& v )
00158 {
00159 return ( ( x == v.x ) && ( y == v.y ) && ( z == v.z ) );
00160 }
00161
00162 inline bool operator != ( KVector& v )
00163 {
00164 return ( ( x != v.x ) || ( y != v.y ) || ( z != v.z ) );
00165 }
00166
00168
00170 inline void Init( float x, float y, float z )
00171 {
00172 this->x = x;
00173 this->y = y;
00174 this->z = z;
00175 }
00176
00177 inline static KVector CrossProduct( KVector& v1, KVector& v2 )
00178 {
00179 KVector result;
00180
00181 result.x = v1.y * v2.z - v1.z * v2.y;
00182 result.y = v1.z * v2.x - v1.x * v2.z;
00183 result.z = v1.x * v2.y - v1.y * v2.x;
00184
00185 return result;
00186 }
00187
00188
00189 inline static float DotProduct( KVector& v1, KVector& v2 )
00190 {
00191 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
00192 }
00193
00194 inline double DotProduct( KVector& v )
00195 {
00196 return x * v.x + y * v.y + z * v.z;
00197 }
00198
00199 inline float DotProductf( KVector& v )
00200 {
00201 return x * v.x + y * v.y + z * v.z;
00202 }
00203
00204 inline static float SquareDistance( KVector& v1, KVector& v2 )
00205 {
00206 return (v1.x - v2.x) * (v1.x - v2.x) + (v1.y - v2.y) * (v1.y - v2.y) + (v1.z - v2.z) * (v1.z - v2.z);
00207 }
00208
00209 inline static float Distance( KVector& v1, KVector& v2 )
00210 {
00211 return sqrtf( SquareDistance( v1, v2 ) );
00212 }
00213
00214 inline float SquareMagnitude()
00215 {
00216 return x * x + y * y + z * z;
00217 }
00218
00219 inline float Magnitude()
00220 {
00221 return sqrtf( SquareMagnitude() );
00222 }
00223
00224 inline void Normalize()
00225 {
00226 float magnitude = Magnitude();
00227
00228 if( !magnitude )
00229 return;
00230
00231 x = x / magnitude;
00232 y = y / magnitude;
00233 z = z / magnitude;
00234 }
00235
00236 inline void SetLength( float Length )
00237 {
00238 Normalize();
00239 x *= Length;
00240 y *= Length;
00241 z *= Length;
00242 }
00243
00244 };
00245
00246
00247
00248
00249
00250
00251
00252 #endif // __VECTOR_H__