// Copyright (C) 2002-2018 Nikolaus Gebhardt // This file is part of the "irrKlang" library. // For conditions of distribution and use, see copyright notice in irrKlang.h #ifndef __IRR_IRRKLANG_VEC_3D_H_INCLUDED__ #define __IRR_IRRKLANG_VEC_3D_H_INCLUDED__ #include #include "ik_irrKlangTypes.h" namespace irrklang { //! a 3d vector template class for representing vectors and points in 3d template class vec3d { public: vec3d(): X(0), Y(0), Z(0) {}; vec3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}; vec3d(const vec3d& other) :X(other.X), Y(other.Y), Z(other.Z) {}; //! constructor creating an irrklang vec3d from an irrlicht vector. #ifdef __IRR_POINT_3D_H_INCLUDED__ template vec3d(const B& other) :X(other.X), Y(other.Y), Z(other.Z) {}; #endif // __IRR_POINT_3D_H_INCLUDED__ // operators vec3d operator-() const { return vec3d(-X, -Y, -Z); } vec3d& operator=(const vec3d& other) { X = other.X; Y = other.Y; Z = other.Z; return *this; } vec3d operator+(const vec3d& other) const { return vec3d(X + other.X, Y + other.Y, Z + other.Z); } vec3d& operator+=(const vec3d& other) { X+=other.X; Y+=other.Y; Z+=other.Z; return *this; } vec3d operator-(const vec3d& other) const { return vec3d(X - other.X, Y - other.Y, Z - other.Z); } vec3d& operator-=(const vec3d& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; } vec3d operator*(const vec3d& other) const { return vec3d(X * other.X, Y * other.Y, Z * other.Z); } vec3d& operator*=(const vec3d& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; } vec3d operator*(const T v) const { return vec3d(X * v, Y * v, Z * v); } vec3d& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; } vec3d operator/(const vec3d& other) const { return vec3d(X / other.X, Y / other.Y, Z / other.Z); } vec3d& operator/=(const vec3d& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; } vec3d operator/(const T v) const { T i=(T)1.0/v; return vec3d(X * i, Y * i, Z * i); } vec3d& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; } bool operator<=(const vec3d&other) const { return X<=other.X && Y<=other.Y && Z<=other.Z;}; bool operator>=(const vec3d&other) const { return X>=other.X && Y>=other.Y && Z>=other.Z;}; bool operator==(const vec3d& other) const { return other.X==X && other.Y==Y && other.Z==Z; } bool operator!=(const vec3d& other) const { return other.X!=X || other.Y!=Y || other.Z!=Z; } // functions //! returns if this vector equalsfloat the other one, taking floating point rounding errors into account bool equals(const vec3d& other) { return equalsfloat(X, other.X) && equalsfloat(Y, other.Y) && equalsfloat(Z, other.Z); } void set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; } void set(const vec3d& p) { X=p.X; Y=p.Y; Z=p.Z;} //! Returns length of the vector. ik_f64 getLength() const { return sqrt(X*X + Y*Y + Z*Z); } //! Returns squared length of the vector. /** This is useful because it is much faster then getLength(). */ ik_f64 getLengthSQ() const { return X*X + Y*Y + Z*Z; } //! Returns the dot product with another vector. T dotProduct(const vec3d& other) const { return X*other.X + Y*other.Y + Z*other.Z; } //! Returns distance from an other point. /** Here, the vector is interpreted as point in 3 dimensional space. */ ik_f64 getDistanceFrom(const vec3d& other) const { ik_f64 vx = X - other.X; ik_f64 vy = Y - other.Y; ik_f64 vz = Z - other.Z; return sqrt(vx*vx + vy*vy + vz*vz); } //! Returns squared distance from an other point. /** Here, the vector is interpreted as point in 3 dimensional space. */ ik_f32 getDistanceFromSQ(const vec3d& other) const { ik_f32 vx = X - other.X; ik_f32 vy = Y - other.Y; ik_f32 vz = Z - other.Z; return (vx*vx + vy*vy + vz*vz); } //! Calculates the cross product with another vector vec3d crossProduct(const vec3d& p) const { return vec3d(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X); } //! Returns if this vector interpreted as a point is on a line between two other points. /** It is assumed that the point is on the line. */ bool isBetweenPoints(const vec3d& begin, const vec3d& end) const { ik_f32 f = (ik_f32)(end - begin).getLengthSQ(); return (ik_f32)getDistanceFromSQ(begin) < f && (ik_f32)getDistanceFromSQ(end) < f; } //! Normalizes the vector. vec3d& normalize() { T l = (T)getLength(); if (l == 0) return *this; l = (T)1.0 / l; X *= l; Y *= l; Z *= l; return *this; } //! Sets the lenght of the vector to a new value void setLength(T newlength) { normalize(); *this *= newlength; } //! Inverts the vector. void invert() { X *= -1.0f; Y *= -1.0f; Z *= -1.0f; } //! Rotates the vector by a specified number of degrees around the Y //! axis and the specified center. //! \param degrees: Number of degrees to rotate around the Y axis. //! \param center: The center of the rotation. void rotateXZBy(ik_f64 degrees, const vec3d& center) { degrees *= IK_DEGTORAD64; T cs = (T)cos(degrees); T sn = (T)sin(degrees); X -= center.X; Z -= center.Z; set(X*cs - Z*sn, Y, X*sn + Z*cs); X += center.X; Z += center.Z; } //! Rotates the vector by a specified number of degrees around the Z //! axis and the specified center. //! \param degrees: Number of degrees to rotate around the Z axis. //! \param center: The center of the rotation. void rotateXYBy(ik_f64 degrees, const vec3d& center) { degrees *= IK_DEGTORAD64; T cs = (T)cos(degrees); T sn = (T)sin(degrees); X -= center.X; Y -= center.Y; set(X*cs - Y*sn, X*sn + Y*cs, Z); X += center.X; Y += center.Y; } //! Rotates the vector by a specified number of degrees around the X //! axis and the specified center. //! \param degrees: Number of degrees to rotate around the X axis. //! \param center: The center of the rotation. void rotateYZBy(ik_f64 degrees, const vec3d& center) { degrees *= IK_DEGTORAD64; T cs = (T)cos(degrees); T sn = (T)sin(degrees); Z -= center.Z; Y -= center.Y; set(X, Y*cs - Z*sn, Y*sn + Z*cs); Z += center.Z; Y += center.Y; } //! Returns interpolated vector. /** \param other: other vector to interpolate between \param d: value between 0.0f and 1.0f. */ vec3d getInterpolated(const vec3d& other, ik_f32 d) const { ik_f32 inv = 1.0f - d; return vec3d(other.X*inv + X*d, other.Y*inv + Y*d, other.Z*inv + Z*d); } //! Gets the Y and Z rotations of a vector. /** Thanks to Arras on the Irrlicht forums to add this method. \return A vector representing the rotation in degrees of this vector. The Z component of the vector will always be 0. */ vec3d getHorizontalAngle() { vec3d angle; angle.Y = (T)atan2(X, Z); angle.Y *= (ik_f32)IK_RADTODEG; if (angle.Y < 0.0f) angle.Y += 360.0f; if (angle.Y >= 360.0f) angle.Y -= 360.0f; ik_f32 z1 = (T)sqrt(X*X + Z*Z); angle.X = (T)atan2(z1, Y); angle.X *= (ik_f32)IK_RADTODEG; angle.X -= 90.0f; if (angle.X < 0.0f) angle.X += 360.0f; if (angle.X >= 360) angle.X -= 360.0f; return angle; } //! Fills an array of 4 values with the vector data (usually floats). /** Useful for setting in shader constants for example. The fourth value will always be 0. */ void getAs4Values(T* array) { array[0] = X; array[1] = Y; array[2] = Z; array[3] = 0; } // member variables T X, Y, Z; }; //! Typedef for a ik_f32 3d vector, a vector using floats for X, Y and Z typedef vec3d vec3df; //! Typedef for an integer 3d vector, a vector using ints for X, Y and Z typedef vec3d vec3di; template vec3d operator*(const S scalar, const vec3d& vector) { return vector*scalar; } } // end namespace irrklang #endif