Home | Namespaces | Hierarchy | Alphabetical List | Class list | Files | Namespace Members | Class members | File members | Tutorials

# vector3d.h

Go to the documentation of this file.

    00001 // Copyright (C) 2002-2009 Nikolaus Gebhardt
    00002 // This file is part of the "Irrlicht Engine".
    00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
    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                 // operators
    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
    00060                 bool operator<=(const vector3d<T>&other) const { return X<=other.X && Y<=other.Y && Z<=other.Z;}
    00061                 bool operator>=(const vector3d<T>&other) const { return X>=other.X && Y>=other.Y && Z>=other.Z;}
    00062                 bool operator<(const vector3d<T>&other) const { return X<other.X && Y<other.Y && Z<other.Z;}
    00063                 bool operator>(const vector3d<T>&other) const { return X>other.X && Y>other.Y && Z>other.Z;}
    00064
    00066                 bool operator==(const vector3d<T>& other) const
    00067                 {
    00068                         return this->equals(other);
    00069                 }
    00070
    00071                 bool operator!=(const vector3d<T>& other) const
    00072                 {
    00073                         return !this->equals(other);
    00074                 }
    00075
    00076                 // functions
    00077
    00079                 bool equals(const vector3d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
    00080                 {
    00081                         return core::equals(X, other.X, tolerance) &&
    00082                                 core::equals(Y, other.Y, tolerance) &&
    00083                                 core::equals(Z, other.Z, tolerance);
    00084                 }
    00085
    00086                 vector3d<T>& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}
    00087                 vector3d<T>& set(const vector3d<T>& p) {X=p.X; Y=p.Y; Z=p.Z;return *this;}
    00088
    00090                 T getLength() const { return core::squareroot( X*X + Y*Y + Z*Z ); }
    00091
    00093
    00095                 T getLengthSQ() const { return X*X + Y*Y + Z*Z; }
    00096
    00098                 T dotProduct(const vector3d<T>& other) const
    00099                 {
    00100                         return X*other.X + Y*other.Y + Z*other.Z;
    00101                 }
    00102
    00104
    00105                 T getDistanceFrom(const vector3d<T>& other) const
    00106                 {
    00107                         return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLength();
    00108                 }
    00109
    00111
    00112                 T getDistanceFromSQ(const vector3d<T>& other) const
    00113                 {
    00114                         return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z).getLengthSQ();
    00115                 }
    00116
    00118
    00120                 vector3d<T> crossProduct(const vector3d<T>& p) const
    00121                 {
    00122                         return vector3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X);
    00123                 }
    00124
    00126
    00130                 bool isBetweenPoints(const vector3d<T>& begin, const vector3d<T>& end) const
    00131                 {
    00132                         const T f = (end - begin).getLengthSQ();
    00133                         return getDistanceFromSQ(begin) <= f &&
    00134                                 getDistanceFromSQ(end) <= f;
    00135                 }
    00136
    00138
    00141                 vector3d<T>& normalize()
    00142                 {
    00143                         f64 length = X*X + Y*Y + Z*Z;
    00144                         if (core::equals(length, 0.0)) // this check isn't an optimization but prevents getting NAN in the sqrt.
    00145                                 return *this;
    00146                         length = core::reciprocal_squareroot(length);
    00147
    00148                         X = (T)(X * length);
    00149                         Y = (T)(Y * length);
    00150                         Z = (T)(Z * length);
    00151                         return *this;
    00152                 }
    00153
    00155                 vector3d<T>& setLength(T newlength)
    00156                 {
    00157                         normalize();
    00158                         return (*this *= newlength);
    00159                 }
    00160
    00162                 vector3d<T>& invert()
    00163                 {
    00164                         X *= -1.0f;
    00165                         Y *= -1.0f;
    00166                         Z *= -1.0f;
    00167                         return *this;
    00168                 }
    00169
    00171
    00173                 void rotateXZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
    00174                 {
    00175                         degrees *= DEGTORAD64;
    00176                         f64 cs = cos(degrees);
    00177                         f64 sn = sin(degrees);
    00178                         X -= center.X;
    00179                         Z -= center.Z;
    00180                         set((T)(X*cs - Z*sn), Y, (T)(X*sn + Z*cs));
    00181                         X += center.X;
    00182                         Z += center.Z;
    00183                 }
    00184
    00186
    00188                 void rotateXYBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
    00189                 {
    00190                         degrees *= DEGTORAD64;
    00191                         f64 cs = cos(degrees);
    00192                         f64 sn = sin(degrees);
    00193                         X -= center.X;
    00194                         Y -= center.Y;
    00195                         set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z);
    00196                         X += center.X;
    00197                         Y += center.Y;
    00198                 }
    00199
    00201
    00203                 void rotateYZBy(f64 degrees, const vector3d<T>& center=vector3d<T>())
    00204                 {
    00205                         degrees *= DEGTORAD64;
    00206                         f64 cs = cos(degrees);
    00207                         f64 sn = sin(degrees);
    00208                         Z -= center.Z;
    00209                         Y -= center.Y;
    00210                         set(X, (T)(Y*cs - Z*sn), (T)(Y*sn + Z*cs));
    00211                         Z += center.Z;
    00212                         Y += center.Y;
    00213                 }
    00214
    00216
    00220                 vector3d<T> getInterpolated(const vector3d<T>& other, f64 d) const
    00221                 {
    00222                         const f64 inv = 1.0 - d;
    00223                         return vector3d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d), (T)(other.Z*inv + Z*d));
    00224                 }
    00225
    00227
    00232                 vector3d<T> getInterpolated_quadratic(const vector3d<T>& v2, const vector3d<T>& v3, f64 d) const
    00233                 {
    00234                         // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
    00235                         const f64 inv = (T) 1.0 - d;
    00236                         const f64 mul0 = inv * inv;
    00237                         const f64 mul1 = (T) 2.0 * d * inv;
    00238                         const f64 mul2 = d * d;
    00239
    00240                         return vector3d<T> ((T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
    00241                                         (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2),
    00242                                         (T)(Z * mul0 + v2.Z * mul1 + v3.Z * mul2));
    00243                 }
    00244
    00246
    00251                 vector3d<T>& interpolate(const vector3d<T>& a, const vector3d<T>& b, f64 d)
    00252                 {
    00253                         X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
    00254                         Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
    00255                         Z = (T)((f64)b.Z + ( ( a.Z - b.Z ) * d ));
    00256                         return *this;
    00257                 }
    00258
    00259
    00261
    00274                 vector3d<T> getHorizontalAngle() const
    00275                 {
    00276                         vector3d<T> angle;
    00277
    00278                         angle.Y = (T)(atan2(X, Z) * (T) RADTODEG64);
    00279
    00280                         if (angle.Y < 0.0f)
    00281                                 angle.Y += 360.0f;
    00282                         if (angle.Y >= 360.0f)
    00283                                 angle.Y -= 360.0f;
    00284
    00285                         const T z1 = core::squareroot(X*X + Z*Z);
    00286
    00287                         angle.X = (T)(atan2(z1, (T)Y) * (T) RADTODEG64 - (T) 90.0);
    00288
    00289                         if (angle.X < (T) 0.0)
    00290                                 angle.X += (T) 360.0;
    00291                         if (angle.X >= (T) 360.0)
    00292                                 angle.X -= (T) 360.0;
    00293
    00294                         return angle;
    00295                 }
    00296
    00298
    00305                 vector3d<T> rotationToDirection(const vector3d<T> & forwards = vector3d<T>(0, 0, 1)) const
    00306                 {
    00307                         const f64 cr = cos( core::DEGTORAD64 * X );
    00308                         const f64 sr = sin( core::DEGTORAD64 * X );
    00309                         const f64 cp = cos( core::DEGTORAD64 * Y );
    00310                         const f64 sp = sin( core::DEGTORAD64 * Y );
    00311                         const f64 cy = cos( core::DEGTORAD64 * Z );
    00312                         const f64 sy = sin( core::DEGTORAD64 * Z );
    00313
    00314                         const f64 srsp = sr*sp;
    00315                         const f64 crsp = cr*sp;
    00316
    00317                         const f64 pseudoMatrix[] = {
    00318                                 ( cp*cy ), ( cp*sy ), ( -sp ),
    00319                                 ( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
    00320                                 ( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
    00321
    00322                         return vector3d<T>(
    00323                                 (T)(forwards.X * pseudoMatrix[0] +
    00324                                         forwards.Y * pseudoMatrix[3] +
    00325                                         forwards.Z * pseudoMatrix[6]),
    00326                                 (T)(forwards.X * pseudoMatrix[1] +
    00327                                         forwards.Y * pseudoMatrix[4] +
    00328                                         forwards.Z * pseudoMatrix[7]),
    00329                                 (T)(forwards.X * pseudoMatrix[2] +
    00330                                         forwards.Y * pseudoMatrix[5] +
    00331                                         forwards.Z * pseudoMatrix[8]));
    00332                 }
    00333
    00335
    00337                 void getAs4Values(T* array) const
    00338                 {
    00339                         array[0] = X;
    00340                         array[1] = Y;
    00341                         array[2] = Z;
    00342                         array[3] = 0;
    00343                 }
    00344
    00346                 T X;
    00347
    00349                 T Y;
    00350
    00352                 T Z;
    00353         };
    00354
    00355
    00357         typedef vector3d<f32> vector3df;
    00358
    00360         typedef vector3d<s32> vector3di;
    00361
    00363         template<class S, class T>
    00364         vector3d<T> operator*(const S scalar, const vector3d<T>& vector) { return vector*scalar; }
    00365
    00366 } // end namespace core
    00367 } // end namespace irr
    00368
    00369 #endif
    00370

| The Irrlicht Engine Documentation © 2003-2009 by Nikolaus Gebhardt. Generated on Sun Jan 10 09:24:05 2010 by Doxygen (1.5.6)