00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef VECTOR_3_H_
00026 #define VECTOR_3_H_
00027
00028 #include <Core/System/Math.h>
00029 #include <Core/Primitive/String.h>
00030
00031 namespace Lamp{
00032
00033
00034
00035
00036
00037
00038
00039 class Vector3{
00040 public:
00041
00042
00043
00044
00045 union{
00046
00047 struct{
00048
00049 float x;
00050
00051 float y;
00052
00053 float z;
00054 };
00055
00056
00057 float array[3];
00058 };
00059
00060
00061
00062
00063
00064 static const Vector3 zero;
00065
00066
00067 static const Vector3 unitX;
00068
00069
00070 static const Vector3 unitY;
00071
00072
00073 static const Vector3 unitZ;
00074
00075
00076 static const Vector3 unitScale;
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 inline Vector3(){}
00087
00088
00089
00090
00091
00092
00093
00094 inline Vector3(float sourceX, float sourceY, float sourceZ) :
00095 x(sourceX), y(sourceY), z(sourceZ){
00096 }
00097
00098
00099
00100
00101
00102 inline explicit Vector3(const float* const source) :
00103 x(source[0]), y(source[1]), z(source[2]){
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 inline void set(float sourceX, float sourceY, float sourceZ){
00116 x = sourceX;
00117 y = sourceY;
00118 z = sourceZ;
00119 }
00120
00121
00122
00123
00124
00125 inline void set(const float* const source){
00126 x = source[0];
00127 y = source[1];
00128 z = source[2];
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 inline Vector3 operator +(const Vector3& addVector) const{
00140 return Vector3(
00141 x + addVector.x,
00142 y + addVector.y,
00143 z + addVector.z);
00144 }
00145
00146
00147
00148
00149
00150
00151 inline Vector3 operator -(const Vector3& subVector) const{
00152 return Vector3(
00153 x - subVector.x,
00154 y - subVector.y,
00155 z - subVector.z);
00156 }
00157
00158
00159
00160
00161
00162
00163 inline Vector3 operator *(float mulValue) const{
00164 return Vector3(x * mulValue, y * mulValue, z * mulValue);
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 inline friend Vector3 operator *(float mulValue, const Vector3& mulVector){
00174 return Vector3(
00175 mulVector.x * mulValue,
00176 mulVector.y * mulValue,
00177 mulVector.z * mulValue);
00178 }
00179
00180
00181
00182
00183
00184 inline Vector3 operator +() const{ return *this; }
00185
00186
00187
00188
00189
00190 inline Vector3 operator -() const{ return Vector3(-x, -y, -z); }
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 inline Vector3& operator +=(const Vector3& addVector){
00201 x += addVector.x;
00202 y += addVector.y;
00203 z += addVector.z;
00204 return *this;
00205 }
00206
00207
00208
00209
00210
00211
00212 inline Vector3& operator -=(const Vector3& subVector){
00213 x -= subVector.x;
00214 y -= subVector.y;
00215 z -= subVector.z;
00216 return *this;
00217 }
00218
00219
00220
00221
00222
00223
00224 inline Vector3& operator *=(float mulValue){
00225 x *= mulValue;
00226 y *= mulValue;
00227 z *= mulValue;
00228 return *this;
00229 }
00230
00231
00232
00233
00234 inline Vector3& inverse(){
00235 x = -x;
00236 y = -y;
00237 z = -z;
00238 return *this;
00239 }
00240
00241
00242
00243
00244
00245 inline Vector3& abs(){
00246 x = Math::abs(x);
00247 y = Math::abs(y);
00248 z = Math::abs(z);
00249 return *this;
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 inline float dotProduct(const Vector3& dotVector) const{
00261 return x * dotVector.x + y * dotVector.y + z * dotVector.z;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270 inline Vector3 crossProduct(const Vector3& crossVector) const{
00271 return Vector3(
00272 y * crossVector.z - z * crossVector.y,
00273 z * crossVector.x - x * crossVector.z,
00274 x * crossVector.y - y * crossVector.x
00275 );
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285 inline float getLength() const{
00286 return Math::sqrt(x * x + y * y + z * z);
00287 }
00288
00289
00290
00291
00292
00293 inline Vector3& setLength(float length){
00294 float nowLength = getLength();
00295
00296 Assert(nowLength >= Math::epsilon);
00297 (*this) *= (length / nowLength);
00298 return *this;
00299 }
00300
00301
00302
00303
00304
00305 inline float getSquaredLength() const{
00306 return (x * x + y * y + z * z);
00307 }
00308
00309
00310
00311
00312
00313 inline Vector3& normalize(){
00314 float nowLength = getLength();
00315
00316 Assert(nowLength >= Math::epsilon);
00317 (*this) *= (1.f / nowLength);
00318 return *this;
00319 }
00320
00321
00322
00323
00324
00325 inline bool isZero() const{
00326 return ((Math::abs(x) <= Math::epsilon) &&
00327 (Math::abs(y) <= Math::epsilon) &&
00328 (Math::abs(z) <= Math::epsilon));
00329 }
00330
00331
00332
00333
00334
00335 inline bool isUnit() const{
00336 return (Math::abs(getLength() - 1.f) <= Math::epsilon);
00337 }
00338
00339
00340
00341
00342
00343 inline float maximumValue() const{
00344 float result = x;
00345 if(y > result){ result = y; }
00346 if(z > result){ result = z; }
00347 return result;
00348 }
00349
00350
00351
00352
00353
00354 inline float minimumValue() const{
00355 float result = x;
00356 if(y < result){ result = y; }
00357 if(z < result){ result = z; }
00358 return result;
00359 }
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 inline bool operator ==(const Vector3& target) const{
00370 return ((x == target.x) && (y == target.y) && (z == target.z));
00371 }
00372
00373
00374
00375
00376
00377
00378
00379 inline bool epsilonEquals(const Vector3& target, float epsilon) const{
00380 Assert(epsilon >= 0.f);
00381 return (
00382 (Math::abs(x - target.x) <= epsilon) &&
00383 (Math::abs(y - target.y) <= epsilon) &&
00384 (Math::abs(z - target.z) <= epsilon));
00385 }
00386
00387
00388
00389
00390
00391
00392 inline bool operator !=(const Vector3& target) const{
00393 return ((x != target.x) || (y != target.y) || (z != target.z));
00394 }
00395
00396
00397
00398
00399
00400
00401
00402 inline bool notEpsilonEquals(const Vector3& target, float epsilon) const{
00403 Assert(epsilon >= 0.f);
00404 return (
00405 (Math::abs(x - target.x) > epsilon) ||
00406 (Math::abs(y - target.y) > epsilon) ||
00407 (Math::abs(z - target.z) > epsilon));
00408 }
00409
00410
00411
00412
00413
00414
00415
00416
00417 inline String toString() const{
00418 String returnString;
00419 returnString.format("( %.8f, %.8f, %.8f )", x, y, z);
00420 return returnString;
00421 }
00422
00423
00424 private:
00425
00426 };
00427
00428
00429 }
00430 #endif // End of VECTOR_3_H_
00431