00001
00002
00003
00004
00005 #ifndef __IRR_POINT_3D_H_INCLUDED__
00006 #define __IRR_POINT_3D_H_INCLUDED__
00007
00008 #include "irrMath.h"
00009
00010 namespace irr
00011 {
00012 namespace core
00013 {
00014
00016
00021 template <class T>
00022 class vector3d
00023 {
00024 public:
00026 vector3d() : X(0), Y(0), Z(0) {}
00028 vector3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}
00030 explicit vector3d(T n) : X(n), Y(n), Z(n) {}
00032 vector3d(const vector3d<T>& other) : X(other.X), Y(other.Y), Z(other.Z) {}
00033
00034
00035
00036 vector3d<T> operator-() const { return vector3d<T>(-X, -Y, -Z); }
00037
00038 vector3d<T>& operator=(const vector3d<T>& other) { X = other.X; Y = other.Y; Z = other.Z; return *this; }
00039
00040 vector3d<T> operator+(const vector3d<T>& other) const { return vector3d<T>(X + other.X, Y + other.Y, Z + other.Z); }
00041 vector3d<T>& operator+=(const vector3d<T>& other) { X+=other.X; Y+=other.Y; Z+=other.Z; return *this; }
00042 vector3d<T> operator+(const T val) const { return vector3d<T>(X + val, Y + val, Z + val); }
00043 vector3d<T>& operator+=(const T val) { X+=val; Y+=val; Z+=val; return *this; }
00044
00045 vector3d<T> operator-(const vector3d<T>& other) const { return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z); }
00046 vector3d<T>& operator-=(const vector3d<T>& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; }
00047 vector3d<T> operator-(const T val) const { return vector3d<T>(X - val, Y - val, Z - val); }
00048 vector3d<T>& operator-=(const T val) { X-=val; Y-=val; Z-=val; return *this; }
00049
00050 vector3d<T> operator*(const vector3d<T>& other) const { return vector3d<T>(X * other.X, Y * other.Y, Z * other.Z); }
00051 vector3d<T>& operator*=(const vector3d<T>& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; }
00052 vector3d<T> operator*(const T v) const { return vector3d<T>(X * v, Y * v, Z * v); }
00053 vector3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; }
00054
00055 vector3d<T> operator/(const vector3d<T>& other) const { return vector3d<T>(X / other.X, Y / other.Y, Z / other.Z); }
00056 vector3d<T>& operator/=(const vector3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; }
00057 vector3d<T> operator/(const T v) const { T i=(T)1.0/v; return vector3d<T>(X * i, Y * i, Z * i); }
00058 vector3d<T>& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; }
00059
00061 bool operator<=(const vector3d<T>&other) const
00062 {
00063 return (X<other.X || core::equals(X, other.X)) ||
00064 (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y))) ||
00065 (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z<other.Z || core::equals(Z, other.Z)));
00066 }
00067
00069 bool operator>=(const vector3d<T>&other) const
00070 {
00071 return (X>other.X || core::equals(X, other.X)) ||
00072 (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y))) ||
00073 (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z>other.Z || core::equals(Z, other.Z)));
00074 }
00075
00077 bool operator<(const vector3d<T>&other) const
00078 {
00079 return (X<other.X && !core::equals(X, other.X)) ||
00080 (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y)) ||
00081 (core::equals(X, other.X) && core::equals(Y, other.Y) && Z<other.Z && !core::equals(Z, other.Z));
00082 }
00083
00085 bool operator>(const vector3d<T>&other) const
00086 {
00087 return (X>other.X && !core::equals(X, other.X)) ||
00088 (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y)) ||
00089 (core::equals(X, other.X) && core::equals(Y, other.Y) && Z>other.Z && !core::equals(Z, other.Z));
00090 }
00091
00093 bool operator==(const vector3d<T>& other) const
00094 {
00095 return this->equals(other);
00096 }
00097
00098 bool operator!=(const vector3d<T>& other) const
00099 {
00100 return !this->equals(other);
00101 }
00102
00103
00104
00106 bool equals(const vector3d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
00107 {
00108 return core::equals(X, other.X, tolerance) &&
00109 core::equals(Y, other.Y, tolerance) &&
00110 core::equals(Z, other.Z, tolerance);
00111 }
00112
00113 vector3d<T>& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}
00114 vector3d<T>& set(const vector3d<T>& p) {X=p.X; Y=p.Y; Z=p.Z;return *this;}
00115
00117 T getLength() const { return core::squareroot( X*X + Y*Y + Z*Z ); }
00118
00120
00122 T getLengthSQ() const { return X*X + Y*Y + Z*Z; }
00123
00125 T dotProduct(const vector3d<T>& other) const
00126 {
00127 return X*other.X + Y*other.Y + Z*other.Z;
00128 }
00129
00131
00132 T getDistanceFrom(const vector3d<T>& other) const
00133 {
00134 return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLength();
00135 }
00136
00138
00139 T getDistanceFromSQ(const vector3d<T>& other) const
00140 {
00141 return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLengthSQ();
00142 }
00143
00145
00147 vector3d<T> crossProduct(const vector3d<T>& p) const
00148 {
00149 return vector3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X);
00150 }
00151
00153
00157 bool isBetweenPoints(const vector3d<T>& begin, const vector3d<T>& end) const
00158 {
00159 const T f = (end - begin).getLengthSQ();
00160 return getDistanceFromSQ(begin) <= f &&
00161 getDistanceFromSQ(end) <= f;
00162 }
00163
00165
00168 vector3d<T>& normalize()
00169 {
00170 f64 length = X*X + Y*Y + Z*Z;
00171 if (length == 0 )
00172 return *this;
00173 length = core::reciprocal_squareroot(length);
00174
00175 X = (T)(X * length);
00176 Y = (T)(Y * length);
00177 Z = (T)(Z * length);
00178 return *this;
00179 }
00180
00182 vector3d<T>& setLength(T newlength)
00183 {
00184 normalize();
00185 return (*this *= newlength);
00186 }
00187
00189 vector3d<T>& invert()
00190 {
00191 X *= -1;
00192 Y *= -1;
00193 Z *= -1;
00194 return *this;
00195 }
00196
00198
00200 void rotateXZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
00201 {
00202 degrees *= DEGTORAD64;
00203 f64 cs = cos(degrees);
00204 f64 sn = sin(degrees);
00205 X -= center.X;
00206 Z -= center.Z;
00207 set((T)(X*cs - Z*sn), Y, (T)(X*sn + Z*cs));
00208 X += center.X;
00209 Z += center.Z;
00210 }
00211
00213
00215 void rotateXYBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
00216 {
00217 degrees *= DEGTORAD64;
00218 f64 cs = cos(degrees);
00219 f64 sn = sin(degrees);
00220 X -= center.X;
00221 Y -= center.Y;
00222 set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z);
00223 X += center.X;
00224 Y += center.Y;
00225 }
00226
00228
00230 void rotateYZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
00231 {
00232 degrees *= DEGTORAD64;
00233 f64 cs = cos(degrees);
00234 f64 sn = sin(degrees);
00235 Z -= center.Z;
00236 Y -= center.Y;
00237 set(X, (T)(Y*cs - Z*sn), (T)(Y*sn + Z*cs));
00238 Z += center.Z;
00239 Y += center.Y;
00240 }
00241
00243
00247 vector3d<T> getInterpolated(const vector3d<T>& other, f64 d) const
00248 {
00249 const f64 inv = 1.0 - d;
00250 return vector3d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d), (T)(other.Z*inv + Z*d));
00251 }
00252
00254
00259 vector3d<T> getInterpolated_quadratic(const vector3d<T>& v2, const vector3d<T>& v3, f64 d) const
00260 {
00261
00262 const f64 inv = (T) 1.0 - d;
00263 const f64 mul0 = inv * inv;
00264 const f64 mul1 = (T) 2.0 * d * inv;
00265 const f64 mul2 = d * d;
00266
00267 return vector3d<T> ((T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
00268 (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2),
00269 (T)(Z * mul0 + v2.Z * mul1 + v3.Z * mul2));
00270 }
00271
00273
00278 vector3d<T>& interpolate(const vector3d<T>& a, const vector3d<T>& b, f64 d)
00279 {
00280 X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
00281 Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
00282 Z = (T)((f64)b.Z + ( ( a.Z - b.Z ) * d ));
00283 return *this;
00284 }
00285
00286
00288
00301 vector3d<T> getHorizontalAngle() const
00302 {
00303 vector3d<T> angle;
00304
00305 const f64 tmp = (atan2((f64)X, (f64)Z) * RADTODEG64);
00306 angle.Y = (T)tmp;
00307
00308 if (angle.Y < 0)
00309 angle.Y += 360;
00310 if (angle.Y >= 360)
00311 angle.Y -= 360;
00312
00313 const f64 z1 = core::squareroot(X*X + Z*Z);
00314
00315 angle.X = (T)(atan2((f64)z1, (f64)Y) * RADTODEG64 - 90.0);
00316
00317 if (angle.X < 0)
00318 angle.X += 360;
00319 if (angle.X >= 360)
00320 angle.X -= 360;
00321
00322 return angle;
00323 }
00324
00326
00330 vector3d<T> getSphericalCoordinateAngles() const
00331 {
00332 vector3d<T> angle;
00333 const f64 length = X*X + Y*Y + Z*Z;
00334
00335 if (length)
00336 {
00337 if (X!=0)
00338 {
00339 angle.Y = (T)(atan2((f64)Z,(f64)X) * RADTODEG64);
00340 }
00341 else if (Z<0)
00342 angle.Y=180;
00343
00344 angle.X = (T)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64);
00345 }
00346 return angle;
00347 }
00348
00350
00357 vector3d<T> rotationToDirection(const vector3d<T> & forwards = vector3d<T>(0, 0, 1)) const
00358 {
00359 const f64 cr = cos( core::DEGTORAD64 * X );
00360 const f64 sr = sin( core::DEGTORAD64 * X );
00361 const f64 cp = cos( core::DEGTORAD64 * Y );
00362 const f64 sp = sin( core::DEGTORAD64 * Y );
00363 const f64 cy = cos( core::DEGTORAD64 * Z );
00364 const f64 sy = sin( core::DEGTORAD64 * Z );
00365
00366 const f64 srsp = sr*sp;
00367 const f64 crsp = cr*sp;
00368
00369 const f64 pseudoMatrix[] = {
00370 ( cp*cy ), ( cp*sy ), ( -sp ),
00371 ( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
00372 ( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
00373
00374 return vector3d<T>(
00375 (T)(forwards.X * pseudoMatrix[0] +
00376 forwards.Y * pseudoMatrix[3] +
00377 forwards.Z * pseudoMatrix[6]),
00378 (T)(forwards.X * pseudoMatrix[1] +
00379 forwards.Y * pseudoMatrix[4] +
00380 forwards.Z * pseudoMatrix[7]),
00381 (T)(forwards.X * pseudoMatrix[2] +
00382 forwards.Y * pseudoMatrix[5] +
00383 forwards.Z * pseudoMatrix[8]));
00384 }
00385
00387
00389 void getAs4Values(T* array) const
00390 {
00391 array[0] = X;
00392 array[1] = Y;
00393 array[2] = Z;
00394 array[3] = 0;
00395 }
00396
00398
00399 void getAs3Values(T* array) const
00400 {
00401 array[0] = X;
00402 array[1] = Y;
00403 array[2] = Z;
00404 }
00405
00406
00408 T X;
00409
00411 T Y;
00412
00414 T Z;
00415 };
00416
00418
00419 template <>
00420 inline vector3d<s32> vector3d<s32>::operator /(s32 val) const {return core::vector3d<s32>(X/val,Y/val,Z/val);}
00421 template <>
00422 inline vector3d<s32>& vector3d<s32>::operator /=(s32 val) {X/=val;Y/=val;Z/=val; return *this;}
00423
00424 template <>
00425 inline vector3d<s32> vector3d<s32>::getSphericalCoordinateAngles() const
00426 {
00427 vector3d<s32> angle;
00428 const f64 length = X*X + Y*Y + Z*Z;
00429
00430 if (length)
00431 {
00432 if (X!=0)
00433 {
00434 angle.Y = round32((f32)(atan2((f64)Z,(f64)X) * RADTODEG64));
00435 }
00436 else if (Z<0)
00437 angle.Y=180;
00438
00439 angle.X = round32((f32)(acos(Y * core::reciprocal_squareroot(length)) * RADTODEG64));
00440 }
00441 return angle;
00442 }
00443
00445 typedef vector3d<f32> vector3df;
00446
00448 typedef vector3d<s32> vector3di;
00449
00451 template<class S, class T>
00452 vector3d<T> operator*(const S scalar, const vector3d<T>& vector) { return vector*scalar; }
00453
00454 }
00455 }
00456
00457 #endif
00458