00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "Zdef.h"
00015 #include "ZMacro.h"
00016
00017 #ifndef __CVECTOR3_H_
00018 #define __CVECTOR3_H_
00019
00021
00025 class CVector3 {
00026
00027 public:
00028
00030 zfloat fCoord [3];
00031
00033 inline CVector3 () {
00034
00035 fCoord[0] = 0;
00036 fCoord[1] = 0;
00037 fCoord[2] = 0;
00038
00039 }
00040
00042 inline CVector3 (zfloat valx, zfloat valy, zfloat valz){
00043
00044 fCoord[0] = valx;
00045 fCoord[1] = valy;
00046 fCoord[2] = valz;
00047
00048 }
00049
00051 inline CVector3 (const CVector3& v){
00052
00053 fCoord[0] = v.fCoord[0];
00054 fCoord[1] = v.fCoord[1];
00055 fCoord[2] = v.fCoord[2];
00056
00057 }
00058
00060 ~CVector3 () {};
00061
00063 inline void Cross (const CVector3& v1,const CVector3& v2){
00064
00065 fCoord[0] = v1.fCoord [1]*v2.fCoord[2] - v1.fCoord[2]*v2.fCoord[1];
00066 fCoord[1] = v1.fCoord [2]*v2.fCoord[0] - v1.fCoord[0]*v2.fCoord[2];
00067 fCoord[2] = v1.fCoord [0]*v2.fCoord[1] - v1.fCoord[1]*v2.fCoord[0];
00068
00069 }
00070
00072 inline void Set (zfloat valx, zfloat valy, zfloat valz){
00073
00074 fCoord[0] = valx;
00075 fCoord[1] = valy;
00076 fCoord[2] = valz;
00077
00078 }
00079
00081 inline void Set (const CVector3& v){
00082
00083 fCoord[0] = v.fCoord[0];
00084 fCoord[1] = v.fCoord[1];
00085 fCoord[2] = v.fCoord[2];
00086
00087 }
00088
00090 inline CVector3 operator= (const CVector3& v){
00091
00092 fCoord[0] = v.fCoord[0];
00093 fCoord[1] = v.fCoord[1];
00094 fCoord[2] = v.fCoord[2];
00095
00096 return *this;
00097 }
00098
00100 inline CVector3 operator+= (const CVector3& v){
00101
00102 fCoord[0] += v.fCoord[0];
00103 fCoord[1] += v.fCoord[1];
00104 fCoord[2] += v.fCoord[2];
00105
00106 return *this;
00107
00108 }
00109
00111 inline CVector3 operator-= (const CVector3& v){
00112
00113 fCoord[0] -= v.fCoord[0];
00114 fCoord[1] -= v.fCoord[1];
00115 fCoord[2] -= v.fCoord[2];
00116
00117 return *this;
00118 }
00119
00121 inline CVector3 operator*= (zfloat val){
00122
00123 fCoord[0] *= val;
00124 fCoord[1] *= val;
00125 fCoord[2] *= val;
00126
00127 return *this;
00128 }
00129
00131 inline CVector3 operator/= (zfloat val){
00132
00133 fCoord[0] /= val;
00134 fCoord[1] /= val;
00135 fCoord[2] /= val;
00136
00137 return *this;
00138 }
00139
00141 inline CVector3 operator*= (CVector3 v){
00142
00143 fCoord[0] *= v.fCoord[0];
00144 fCoord[1] *= v.fCoord[1];
00145 fCoord[2] *= v.fCoord[2];
00146
00147 return *this;
00148 }
00149
00151 inline CVector3 operator/= (CVector3 v){
00152
00153 fCoord[0] /= v.fCoord[0];
00154 fCoord[1] /= v.fCoord[1];
00155 fCoord[2] /= v.fCoord[2];
00156
00157 return *this;
00158 }
00159
00161 inline CVector3 operator+(){return *this;}
00162
00164 inline CVector3 operator-(){
00165
00166 return CVector3(-fCoord[0],
00167 -fCoord[1],
00168 -fCoord[2]);
00169 }
00170
00172 inline friend CVector3 operator+ (const CVector3& v1, const CVector3& v2) {
00173
00174 return CVector3 (v1.fCoord[0] + v2.fCoord[0],
00175 v1.fCoord[1] + v2.fCoord[1],
00176 v1.fCoord[2] + v2.fCoord[2]);
00177 }
00178
00180 inline friend CVector3 operator- (const CVector3& v1, const CVector3& v2){
00181
00182 return CVector3 (v1.fCoord[0] - v2.fCoord[0],
00183 v1.fCoord[1] - v2.fCoord[1],
00184 v1.fCoord[2] - v2.fCoord[2]);
00185 }
00186
00188 inline friend zfloat operator* (const CVector3& v1, const CVector3& v2){
00189
00190 return v1.fCoord[0]*v2.fCoord[0] +
00191 v1.fCoord[1]*v2.fCoord[1] +
00192 v1.fCoord[2]*v2.fCoord[2];
00193 }
00194
00196 inline friend CVector3 operator% (const CVector3& v1, const CVector3& v2){
00197
00198 return CVector3 (v1.fCoord[1]*v2.fCoord[2] - v1.fCoord[2]*v2.fCoord[1],
00199 v1.fCoord[2]*v2.fCoord[0] - v1.fCoord[0]*v2.fCoord[2],
00200 v1.fCoord[0]*v2.fCoord[1] - v1.fCoord[1]*v2.fCoord[0]);
00201
00202 }
00203
00204
00206 inline friend CVector3 operator* (const CVector3& v, zfloat val){
00207
00208 return CVector3(v.fCoord[0]*val,
00209 v.fCoord[1]*val,
00210 v.fCoord[2]*val);
00211
00212 }
00213
00215 inline friend CVector3 operator* (zfloat val, const CVector3& v){
00216
00217 return CVector3(v.fCoord[0]*val,
00218 v.fCoord[1]*val,
00219 v.fCoord[2]*val);
00220 }
00221
00223 inline friend CVector3 operator* (const CVector3& v, zuint val){
00224
00225 return CVector3(v.fCoord[0]*(zfloat)val,
00226 v.fCoord[1]*(zfloat)val,
00227 v.fCoord[2]*(zfloat)val);
00228 }
00229
00231 inline friend CVector3 operator* (zuint val, const CVector3& v){
00232
00233 return CVector3(v.fCoord[0]*(zfloat)val,
00234 v.fCoord[1]*(zfloat)val,
00235 v.fCoord[2]*(zfloat)val);
00236 }
00237
00239 inline friend CVector3 operator/ (const CVector3& v, zfloat val){
00240
00241 return CVector3(v.fCoord[0]/val,
00242 v.fCoord[1]/val,
00243 v.fCoord[2]/val);
00244 }
00245
00247 inline friend CVector3 operator/ (zfloat val,const CVector3& v){
00248
00249 return CVector3(v.fCoord[0]/val,
00250 v.fCoord[1]/val,
00251 v.fCoord[2]/val);
00252 }
00253
00255 inline friend CVector3 operator/ (const CVector3& v, zuint val){
00256
00257 return CVector3(v.fCoord[0]/(zfloat)val,
00258 v.fCoord[1]/(zfloat)val,
00259 v.fCoord[2]/(zfloat)val);
00260 }
00261
00262
00264 inline friend CVector3 operator/ (zuint val,const CVector3& v){
00265
00266 return CVector3(v.fCoord[0]/(zfloat)val,
00267 v.fCoord[1]/(zfloat)val,
00268 v.fCoord[2]/(zfloat)val);
00269 }
00270
00271
00273 inline friend bool operator==(const CVector3& v1, const CVector3& v2){
00274
00275 return ZM_FLOAT_EQ(v1.fCoord[0],v2.fCoord[0]) &&
00276 ZM_FLOAT_EQ(v1.fCoord[1],v2.fCoord[1]) &&
00277 ZM_FLOAT_EQ(v1.fCoord[2],v2.fCoord[2]);
00278 }
00279
00281 inline friend bool operator!=(const CVector3& v1, const CVector3& v2){
00282
00283 return v1.fCoord[0] != v2.fCoord[0] ||
00284 v1.fCoord[1] != v2.fCoord[1] ||
00285 v1.fCoord[2] != v2.fCoord[2];
00286 }
00287
00289 inline zfloat Dist (const CVector3 &v) const {
00290
00291 return ZM_SQRT(((fCoord[0] - v.fCoord[0])*(fCoord[0] - v.fCoord[0]))+
00292 ((fCoord[1] - v.fCoord[1])*(fCoord[1] - v.fCoord[1]))+
00293 ((fCoord[2] - v.fCoord[2])*(fCoord[2] - v.fCoord[2])));
00294
00295 }
00296
00298 inline CVector3 Norm (const CVector3& v1, const CVector3& v2, const CVector3& v3) {
00299
00300 Set (CVector3 ((v1-v2)%(v1-v3)));
00301 return *this;
00302
00303 }
00304
00306 inline CVector3 NormUnit (const CVector3& v1, const CVector3& v2, const CVector3& v3) {
00307
00308 Set (Norm(v1,v2,v3));
00309
00310 zfloat module = (zfloat) ZM_SQRT(fCoord[0]*fCoord[0]+fCoord[1]*fCoord[1]+fCoord[2]*fCoord[2]);
00311
00312 fCoord[0] /= module;
00313 fCoord[1] /= module;
00314 fCoord[2] /= module;
00315
00316 return *this;
00317
00318 }
00319
00321 inline zfloat Lenght () const {
00322
00323 return (zfloat) ZM_SQRT(fCoord[0] * fCoord[0]+
00324 fCoord[1] * fCoord[1]+
00325 fCoord[2] * fCoord[2]);
00326
00327 }
00328
00330 inline CVector3 SetLenght (zfloat val) {
00331
00332 Normalize();
00333 *this *= val;
00334 return *this;
00335
00336 }
00337
00338
00340 inline CVector3 Normalize () {
00341
00342 *this /= (zfloat) ZM_SQRT(fCoord[0]*fCoord[0]+fCoord[1]*fCoord[1]+fCoord[2]*fCoord[2]);
00343
00344 return *this;
00345
00346 }
00347
00349 inline CVector3 ClosestPointOnLine (CVector3 v1, CVector3 v2) const {
00350
00351
00352 CVector3 c = *this - v1;
00353 CVector3 V = v2-v1; V.Normalize();
00354 zfloat d = v1.Dist(v2);
00355 zfloat t = V * c;
00356
00357 if (t < 0) return v1;
00358 if (t > d) return v2;
00359
00360 V.SetLenght(t);
00361 return v1+V;
00362
00363 }
00364
00366
00369 inline zfloat IntersectSphere (CVector3 rO, CVector3 rV, zfloat sR) const {
00370
00371 CVector3 Q = *this - rO;
00372 zfloat c = Q.Lenght();
00373 zfloat V = Q * rV;
00374 zfloat d = sR * sR - (c*c - V*V);
00375
00376 if ( d < 0.0) return -1.0;
00377
00378 return V - (zfloat) ZM_SQRT(d);
00379
00380 }
00381
00383
00384 inline zfloat Angle (CVector3 v) const {
00385
00386 zfloat tmp = (*this * v);
00387
00388 if (tmp == 0.0) return 0.0;
00389 else
00390 return (tmp / (Lenght() * v.Lenght()));
00391
00392 }
00393
00394 };
00395
00396 #endif // define __CVECTOR3_H_