VecMath.hpp   VecMath.hpp
skipping to change at line 21 skipping to change at line 21
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/ */
// Template vector and matrix library. // Template vector and matrix library.
// Use can to // Use column-major matrices, and in vectors, order vertices like this:
// x, y, z, w (or r, g, b, a), so they can be passed to Renderer.
#ifndef _VECMATH_H_ #ifndef _VECMATH_H_
#define _VECMATH_H_ #define _VECMATH_H_
#include <cmath> #include <cmath>
#include <QString> #include <QString>
template<class T> class Vector2; template<class T> class Vector2;
template<class T> class Vector3; template<class T> class Vector3;
template<class T> class Vector4; template<class T> class Vector4;
template<class T> class Matrix4; template<class T> class Matrix4;
typedef Vector2<double> Vec2d; typedef Vector2<double> Vec2d;
typedef Vector2<float> Vec2f; typedef Vector2<float> Vec2f;
typedef Vector2<int> Vec2i; typedef Vector2<int> Vec2i;
//! @typedef Vec3d //! @typedef Vec3d
//! A 3d vector of doubles. //! A 3d vector of doubles.
typedef Vector3<double> Vec3d; typedef Vector3<double> Vec3d;
//! @typedef Vec3f //! @typedef Vec3f
//! A 3d vector of floats. //! A 3d vector of floats.
typedef Vector3<float> Vec3f; typedef Vector3<float> Vec3f;
//! @typedef Vec4d //! @typedef Vec4d
//! A 4d vector of doubles. //! A 4d vector of doubles.
typedef Vector4<double> Vec4d; typedef Vector4<double> Vec4d;
//! @typedef Vec4f //! @typedef Vec4f
//! A 4d vector of floats. //! A 4d vector of floats.
typedef Vector4<float> Vec4f; typedef Vector4<float> Vec4f;
//! @typedef Vec4i //! @typedef Vec4i
//! A 4d vector of ints. //! A 4d vector of ints.
typedef Vector4<int> Vec4i; typedef Vector4<int> Vec4i;
//! @typedef Mat4d //! @typedef Mat4d
//! A 4x4 matrix of doubles. //! A 4x4 matrix of doubles.
typedef Matrix4<double> Mat4d; typedef Matrix4<double> Mat4d;
//! @typedef Mat4f //! @typedef Mat4f
//! A 4x4 matrix of floats. //! A 4x4 matrix of floats.
typedef Matrix4<float> Mat4f; typedef Matrix4<float> Mat4f;
//! @class Vector2 //! @class Vector2
//! A templatized 2d vector. //! A templatized 2d vector.
//! Use Vec2d or Vec2f typdef for vectors of double and float respectively. //! Use Vec2d or Vec2f typdef for vectors of double and float respectively.
template<class T> class Vector2 template<class T> class Vector2
{ {
public: public:
inline Vector2(); inline Vector2();
inline Vector2(T, T); inline Vector2(T, T);
inline Vector2& operator=(const T*); inline Vector2& operator=(const T*);
inline void set(T, T); inline void set(T, T);
inline bool operator==(const Vector2<T>&) const; inline bool operator==(const Vector2<T>&) const;
inline bool operator!=(const Vector2<T>&) const; inline bool operator!=(const Vector2<T>&) const;
inline const T& operator[](int x) const; inline const T& operator[](int x) const;
inline T& operator[](int); inline T& operator[](int);
inline operator const T*() const; inline operator const T*() const;
inline operator T*(); inline operator T*();
inline operator+=(const Vector2<T>&); inline void operator+=(const Vector2<T>&);
inline operator-=(const Vector2<T>&); inline void operator-=(const Vector2<T>&);
inline operator*=(T); inline void operator*=(T);
inline operator/=(T); inline void operator/=(T);
inline Vector2 operator-(const Vector2<T>&) const; inline Vector2 operator-(const Vector2<T>&) const;
inline Vector2 operator+(const Vector2<T>&) const; inline Vector2 operator+(const Vector2<T>&) const;
inline Vector2 operator-() const; inline Vector2 operator-() const;
inline Vector2 operator+() const; inline Vector2 operator+() const;
inline Vector2 operator*(T) const; inline Vector2 operator*(T) const;
inline Vector2 operator/(T) const; inline Vector2 operator/(T) const;
inline T dot(const Vector2<T>&) const; inline T dot(const Vector2<T>&) const;
inline T length() const; inline T length() const;
inline T lengthSquared() const; inline T lengthSquared() const;
inline void normalize(); inline void normalize();
T v[2]; T v[2];
}; };
//! @class Vector3 //! @class Vector3
//! A templatized 3d vector. //! A templatized 3d vector.
//! Use Vec3d or Vec3f typdef for vectors of double and float respectively. //! Use Vec3d or Vec3f typdef for vectors of double and float respectively.
template<class T> class Vector3 template<class T> class Vector3
{ {
public: public:
inline Vector3(); inline Vector3();
//inline Vector3(const Vector3&); //inline Vector3(const Vector3&);
//template <class T2> inline Vector3(const Vector3<T2>&); //template <class T2> inline Vector3(const Vector3<T2>&);
inline Vector3(T, T, T); inline Vector3(T, T, T);
inline Vector3(T); inline Vector3(T);
skipping to change at line 137 skipping to change at line 137
inline bool operator==(const Vector3<T>&) const; inline bool operator==(const Vector3<T>&) const;
inline bool operator!=(const Vector3<T>&) const; inline bool operator!=(const Vector3<T>&) const;
inline T& operator[](int); inline T& operator[](int);
inline const T& operator[](int) const; inline const T& operator[](int) const;
inline operator const T*() const; inline operator const T*() const;
inline operator T*(); inline operator T*();
inline const T* data() const {return v;} inline const T* data() const {return v;}
inline T* data() {return v;} inline T* data() {return v;}
inline operator+=(const Vector3<T>&); inline void operator+=(const Vector3<T>&);
inline operator-=(const Vector3<T>&); inline void operator-=(const Vector3<T>&);
inline operator*=(T); inline void operator*=(T);
inline operator/=(T); inline void operator/=(T);
inline Vector3 operator-(const Vector3<T>&) const; inline Vector3 operator-(const Vector3<T>&) const;
inline Vector3 operator+(const Vector3<T>&) const; inline Vector3 operator+(const Vector3<T>&) const;
inline Vector3 operator-() const; inline Vector3 operator-() const;
inline Vector3 operator+() const; inline Vector3 operator+() const;
inline Vector3 operator*(T) const; inline Vector3 operator*(T) const;
inline Vector3 operator/(T) const; inline Vector3 operator/(T) const;
skipping to change at line 176 skipping to change at line 176
inline void transfo4d(const Mat4d&); inline void transfo4d(const Mat4d&);
inline void transfo4d(const Mat4f&); inline void transfo4d(const Mat4f&);
T v[3]; // The 3 values T v[3]; // The 3 values
QString toString() const {return QString("[%1, %2, %3]").arg(v[0]).a rg(v[1]).arg(v[2]);} QString toString() const {return QString("[%1, %2, %3]").arg(v[0]).a rg(v[1]).arg(v[2]);}
QString toStringLonLat() const {return QString("[") + QString::numbe r(longitude()*180./M_PI, 'g', 12) + "," + QString::number(latitude()*180./M _PI, 'g', 12)+"]";} QString toStringLonLat() const {return QString("[") + QString::numbe r(longitude()*180./M_PI, 'g', 12) + "," + QString::number(latitude()*180./M _PI, 'g', 12)+"]";}
}; };
//! @class Vector4 //! @class Vector4
//! A templatized 4d vector. //! A templatized 4d vector.
//! Use Vec4d or Vec4f typdef for vectors of double and float respectively. //! Use Vec4d or Vec4f typdef for vectors of double and float respectively.
template<class T> class Vector4 template<class T> class Vector4
{ {
public: public:
inline Vector4(); inline Vector4();
inline Vector4(const Vector3<T>&); inline Vector4(const Vector3<T>&);
inline Vector4(T, T, T); inline Vector4(T, T, T);
inline Vector4(T, T, T, T); inline Vector4(T, T, T, T);
inline Vector4& operator=(const Vector3<T>&); inline Vector4& operator=(const Vector3<T>&);
skipping to change at line 198 skipping to change at line 198
inline void set(T, T, T, T); inline void set(T, T, T, T);
inline bool operator==(const Vector4<T>&) const; inline bool operator==(const Vector4<T>&) const;
inline bool operator!=(const Vector4<T>&) const; inline bool operator!=(const Vector4<T>&) const;
inline T& operator[](int); inline T& operator[](int);
inline const T& operator[](int) const; inline const T& operator[](int) const;
inline operator T*(); inline operator T*();
inline operator const T*() const; inline operator const T*() const;
inline operator+=(const Vector4<T>&); inline void operator+=(const Vector4<T>&);
inline operator-=(const Vector4<T>&); inline void operator-=(const Vector4<T>&);
inline operator*=(T); inline void operator*=(T);
inline operator/=(T); inline void operator/=(T);
inline Vector4 operator-(const Vector4<T>&) const; inline Vector4 operator-(const Vector4<T>&) const;
inline Vector4 operator+(const Vector4<T>&) const; inline Vector4 operator+(const Vector4<T>&) const;
inline Vector4 operator-() const; inline Vector4 operator-() const;
inline Vector4 operator+() const; inline Vector4 operator+() const;
inline Vector4 operator*(T) const; inline Vector4 operator*(T) const;
inline Vector4 operator/(T) const; inline Vector4 operator/(T) const;
skipping to change at line 224 skipping to change at line 224
inline T length() const; inline T length() const;
inline T lengthSquared() const; inline T lengthSquared() const;
inline void normalize(); inline void normalize();
inline void transfo4d(const Mat4d&); inline void transfo4d(const Mat4d&);
T v[4]; // The 4 values T v[4]; // The 4 values
}; };
//! @class Matrix4 //! @class Matrix4
//! A templatized column-major 4x4 matrix. //! A templatized column-major 4x4 matrix.
//! Use Mat4d or Mat4f typdef for matrices of doubles and floats respective ly. //! Use Mat4d or Mat4f typdef for matrices of doubles and floats respective ly.
template<class T> class Matrix4 template<class T> class Matrix4
{ {
public: public:
Matrix4(); Matrix4();
Matrix4(T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T); Matrix4(T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T);
Matrix4(const T*); Matrix4(const T*);
Matrix4(const Vector4<T>&, const Vector4<T>&, Matrix4(const Vector4<T>&, const Vector4<T>&,
const Vector4<T>&, const Vector4<T>&); const Vector4<T>&, const Vector4<T>&);
skipping to change at line 266 skipping to change at line 266
static Matrix4<T> rotation(const Vector3<T>&, T); static Matrix4<T> rotation(const Vector3<T>&, T);
static Matrix4<T> xrotation(T); static Matrix4<T> xrotation(T);
static Matrix4<T> yrotation(T); static Matrix4<T> yrotation(T);
static Matrix4<T> zrotation(T); static Matrix4<T> zrotation(T);
static Matrix4<T> scaling(const Vector3<T>&); static Matrix4<T> scaling(const Vector3<T>&);
static Matrix4<T> scaling(T); static Matrix4<T> scaling(T);
Matrix4<T> transpose() const; Matrix4<T> transpose() const;
Matrix4<T> inverse() const; Matrix4<T> inverse() const;
inline Vector4<T> getRow(const int row) const;
inline Vector4<T> getColumn(const int column) const;
inline void print(void) const; inline void print(void) const;
T r[16]; T r[16];
}; };
//! Serialization routines. //! Serialization routines.
template<class T> QDataStream& operator<<(QDataStream& out, const Vector2<T >& v) {out << v[0] << v[1]; return out;} template<class T> QDataStream& operator<<(QDataStream& out, const Vector2<T >& v) {out << v[0] << v[1]; return out;}
template<class T> QDataStream& operator<<(QDataStream& out, const Vector3<T >& v) {out << v[0] << v[1] << v[2]; return out;} template<class T> QDataStream& operator<<(QDataStream& out, const Vector3<T >& v) {out << v[0] << v[1] << v[2]; return out;}
template<class T> QDataStream& operator<<(QDataStream& out, const Vector4<T >& v) {out << v[0] << v[1] << v[2] << v[3]; return out;} template<class T> QDataStream& operator<<(QDataStream& out, const Vector4<T >& v) {out << v[0] << v[1] << v[2] << v[3]; return out;}
template<class T> QDataStream& operator<<(QDataStream& out, const Matrix4<T >& m) {out << m[0] << m[1] << m[2] << m[3] << m[4] << m[5] << m[6] << m[7] << m[8] << m[9] << m[10] << m[11] << m[12] << m[13] << m[14] << m[15]; retu rn out;} template<class T> QDataStream& operator<<(QDataStream& out, const Matrix4<T >& m) {out << m[0] << m[1] << m[2] << m[3] << m[4] << m[5] << m[6] << m[7] << m[8] << m[9] << m[10] << m[11] << m[12] << m[13] << m[14] << m[15]; retu rn out;}
skipping to change at line 332 skipping to change at line 335
template<class T> Vector2<T>::operator const T*() const template<class T> Vector2<T>::operator const T*() const
{ {
return v; return v;
} }
template<class T> Vector2<T>::operator T*() template<class T> Vector2<T>::operator T*()
{ {
return v; return v;
} }
template<class T> Vector2<T>::operator+=(const Vector2<T>& a) template<class T> void Vector2<T>::operator+=(const Vector2<T>& a)
{ {
v[0] += a.v[0]; v[1] += a.v[1]; v[0] += a.v[0]; v[1] += a.v[1];
} }
template<class T> Vector2<T>::operator-=(const Vector2<T>& a) template<class T> void Vector2<T>::operator-=(const Vector2<T>& a)
{ {
v[0] -= a.v[0]; v[1] -= a.v[1]; v[0] -= a.v[0]; v[1] -= a.v[1];
} }
template<class T> Vector2<T>::operator*=(T s) template<class T> void Vector2<T>::operator*=(T s)
{ {
v[0] *= s; v[1] *= s; v[0] *= s; v[1] *= s;
} }
template<class T> Vector2<T> Vector2<T>::operator-() const template<class T> Vector2<T> Vector2<T>::operator-() const
{ {
return Vector2<T>(-v[0], -v[1]); return Vector2<T>(-v[0], -v[1]);
} }
template<class T> Vector2<T> Vector2<T>::operator+() const template<class T> Vector2<T> Vector2<T>::operator+() const
{ {
return *this; return *this;
skipping to change at line 484 skipping to change at line 484
template<class T> Vector3<T>::operator const T*() const template<class T> Vector3<T>::operator const T*() const
{ {
return v; return v;
} }
template<class T> Vector3<T>::operator T*() template<class T> Vector3<T>::operator T*()
{ {
return v; return v;
} }
template<class T> Vector3<T>::operator+=(const Vector3<T>& a) template<class T> void Vector3<T>::operator+=(const Vector3<T>& a)
{ {
v[0] += a.v[0]; v[1] += a.v[1]; v[2] += a.v[2]; v[0] += a.v[0]; v[1] += a.v[1]; v[2] += a.v[2];
} }
template<class T> Vector3<T>::operator-=(const Vector3<T>& a) template<class T> void Vector3<T>::operator-=(const Vector3<T>& a)
{ {
v[0] -= a.v[0]; v[1] -= a.v[1]; v[2] -= a.v[2]; v[0] -= a.v[0]; v[1] -= a.v[1]; v[2] -= a.v[2];
} }
template<class T> Vector3<T>::operator*=(T s) template<class T> void Vector3<T>::operator*=(T s)
{ {
v[0] *= s; v[1] *= s; v[2] *= s; v[0] *= s; v[1] *= s; v[2] *= s;
} }
template<class T> Vector3<T>::operator/=(T s) template<class T> void Vector3<T>::operator/=(T s)
{ {
v[0] /= s; v[1] /= s; v[2] /= s; v[0] /= s; v[1] /= s; v[2] /= s;
} }
template<class T> Vector3<T> Vector3<T>::operator-() const template<class T> Vector3<T> Vector3<T>::operator-() const
{ {
return Vector3<T>(-v[0], -v[1], -v[2]); return Vector3<T>(-v[0], -v[1], -v[2]);
} }
template<class T> Vector3<T> Vector3<T>::operator+() const template<class T> Vector3<T> Vector3<T>::operator+() const
{ {
return *this; return *this;
skipping to change at line 679 skipping to change at line 675
template<class T> Vector4<T>::operator T*() template<class T> Vector4<T>::operator T*()
{ {
return v; return v;
} }
template<class T> Vector4<T>::operator const T*() const template<class T> Vector4<T>::operator const T*() const
{ {
return v; return v;
} }
template<class T> Vector4<T>::operator+=(const Vector4<T>& a) template<class T> void Vector4<T>::operator+=(const Vector4<T>& a)
{ {
v[0] += a.v[0]; v[1] += a.v[1]; v[2] += a.v[2]; v[3] += a.v[3]; v[0] += a.v[0]; v[1] += a.v[1]; v[2] += a.v[2]; v[3] += a.v[3];
} }
template<class T> Vector4<T>::operator-=(const Vector4<T>& a) template<class T> void Vector4<T>::operator-=(const Vector4<T>& a)
{ {
v[0] -= a.v[0]; v[1] -= a.v[1]; v[2] -= a.v[2]; v[3] -= a/v[3]; v[0] -= a.v[0]; v[1] -= a.v[1]; v[2] -= a.v[2]; v[3] -= a/v[3];
} }
template<class T> Vector4<T>::operator*=(T s) template<class T> void Vector4<T>::operator*=(T s)
{ {
v[0] *= s; v[1] *= s; v[2] *= s; v[3] *= s; v[0] *= s; v[1] *= s; v[2] *= s; v[3] *= s;
} }
template<class T> Vector4<T> Vector4<T>::operator-() const template<class T> Vector4<T> Vector4<T>::operator-() const
{ {
return Vector4<T>(-v[0], -v[1], -v[2], -v[3]); return Vector4<T>(-v[0], -v[1], -v[2], -v[3]);
} }
template<class T> Vector4<T> Vector4<T>::operator+() const template<class T> Vector4<T> Vector4<T>::operator+() const
{ {
return *this; return *this;
skipping to change at line 970 skipping to change at line 963
* Code ripped from the GLU library * Code ripped from the GLU library
* Compute inverse of 4x4 transformation matrix. * Compute inverse of 4x4 transformation matrix.
* Code contributed by Jacques Leroy jle@star.be * Code contributed by Jacques Leroy jle@star.be
* Return zero matrix on failure (singular matrix) * Return zero matrix on failure (singular matrix)
*/ */
template<class T> Matrix4<T> Matrix4<T>::inverse() const template<class T> Matrix4<T> Matrix4<T>::inverse() const
{ {
const T * m = r; const T * m = r;
T out[16]; T out[16];
/* NB. are COLUMN major. */ /* NB. Matrices used by StelRenderer are COLUMN major. */
#define SWAP_ROWS(a, b) { T *_tmp = a; (a)=(b); (b)=_tmp; } #define SWAP_ROWS(a, b) { T *_tmp = a; (a)=(b); (b)=_tmp; }
#define MAT(m,r,c) (m)[(c)*4+(r)] #define MAT(m,r,c) (m)[(c)*4+(r)]
T wtmp[4][8]; T wtmp[4][8];
T m0, m1, m2, m3, s; T m0, m1, m2, m3, s;
T *r0, *r1, *r2, *r3; T *r0, *r1, *r2, *r3;
r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3]; r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1), r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1),
skipping to change at line 1141 skipping to change at line 1134
MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4]; MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4];
MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6]; MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6];
MAT(out, 3, 3) = r3[7]; MAT(out, 3, 3) = r3[7];
return Matrix4<T>(out); return Matrix4<T>(out);
#undef MAT #undef MAT
#undef SWAP_ROWS #undef SWAP_ROWS
} }
template<class T> Vector4<T> Matrix4<T>::getRow(const int row) const
{
return Vector4<T>(r[0 + row], r[4 + row], r[8 + row], r[12 + row]);
}
template<class T> Vector4<T> Matrix4<T>::getColumn(const int column) const
{
return Vector4<T>(r[0 + column * 4], r[1 + column * 4], r[2 + column
* 4], r[3 + column * 4]);
}
template<class T> void Matrix4<T>::print(void) const template<class T> void Matrix4<T>::print(void) const
{ {
printf("[%5.2lf %5.2lf %5.2lf %17.12le]\n" printf("[%5.2lf %5.2lf %5.2lf %17.12le]\n"
"[%5.2lf %5.2lf %5.2lf %17.12le]\n" "[%5.2lf %5.2lf %5.2lf %17.12le]\n"
"[%5.2lf %5.2lf %5.2lf %17.12le]\n" "[%5.2lf %5.2lf %5.2lf %17.12le]\n"
"[%5.2lf %5.2lf %5.2lf %17.12le]\n\n", "[%5.2lf %5.2lf %5.2lf %17.12le]\n\n",
r[0],r[4],r[8],r[12], r[0],r[4],r[8],r[12],
r[1],r[5],r[9],r[13], r[1],r[5],r[9],r[13],
r[2],r[6],r[10],r[14], r[2],r[6],r[10],r[14],
r[3],r[7],r[11],r[15]); r[3],r[7],r[11],r[15]);
End of changes. 38 change blocks.
46 lines changed or deleted 50 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/