00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef ARCHON_MATH_MATRIX_H
00021 #define ARCHON_MATH_MATRIX_H
00022
00023 #include <archon/math/quaternion.H>
00024
00025 namespace Archon
00026 {
00027 namespace Math
00028 {
00042 struct Matrix3x3
00043 {
00044 Vector3 x, y, z;
00045
00046 static const Matrix3x3 &zero();
00047 static const Matrix3x3 &identity();
00048
00049 Matrix3x3() {}
00050 Matrix3x3(const Vector3 &x, const Vector3 &y, const Vector3 &z): x(x), y(y), z(z) {}
00051
00083 Matrix3x3(Vector3 y, Vector3 z): x(normalize(y*z)), y(normalize(z*x)), z(normalize(z)) {}
00084
00085 explicit Matrix3x3(const Rotation3 &r) { setRotation(r); }
00086 explicit Matrix3x3(const Quaternion &q) { setRotation(q); }
00087
00088 Vector3 getRow1() const { return Vector3(x[0], y[0], z[0]); }
00089 Vector3 getRow2() const { return Vector3(x[1], y[1], z[1]); }
00090 Vector3 getRow3() const { return Vector3(x[2], y[2], z[2]); }
00091
00092 double dotRow1(const Vector3 &v) const { return x[0]*v[0] + y[0]*v[1] + z[0]*v[2]; }
00093 double dotRow2(const Vector3 &v) const { return x[1]*v[0] + y[1]*v[1] + z[1]*v[2]; }
00094 double dotRow3(const Vector3 &v) const { return x[2]*v[0] + y[2]*v[1] + z[2]*v[2]; }
00095
00096 Vector3 diag() const { return Vector3(x[0], y[1], z[2]); }
00097 double det() const { return x[0]*y[1]*z[2] + y[0]*z[1]*x[2] + z[0]*x[1]*y[2] -
00098 x[0]*z[1]*y[2] - y[0]*x[1]*z[2] - z[0]*y[1]*x[2]; }
00099
00100 Matrix3x3 operator+(const Matrix3x3 &m) const { Matrix3x3 n(*this); return n += m; }
00101 Matrix3x3 operator-(const Matrix3x3 &m) const { Matrix3x3 n(*this); return n -= m; }
00102 Matrix3x3 operator*(const Matrix3x3 &m) const { Matrix3x3 n(*this); return n *= m; }
00103 Matrix3x3 operator/(const Matrix3x3 &m) const { Matrix3x3 n(*this); return n /= m; }
00104
00105 void map(Vector3 &v) const { v.set(dotRow1(v), dotRow2(v), dotRow3(v)); }
00106 Vector3 operator()(const Vector3 &v) const { Vector3 w(v); map(w); return w; }
00107 Vector3 operator*(const Vector3 &v) const { return operator()(v); }
00108
00109 Matrix3x3 operator*(double f) const { Matrix3x3 m(*this); return m *= f; }
00110 Matrix3x3 operator/(double f) const { Matrix3x3 m(*this); return m /= f; }
00111
00112 Matrix3x3 operator-() const { Matrix3x3 m(*this); m.negate(); return m; }
00113
00114
00115 Matrix3x3 &operator+=(const Matrix3x3 &m) { x += m.x; y += m.y; z += m.z; return *this; }
00116 Matrix3x3 &operator-=(const Matrix3x3 &m) { x -= m.x; y -= m.y; z -= m.z; return *this; }
00117 Matrix3x3 &operator*=(const Matrix3x3 &m) { setRow1(dotRow1(m.x), dotRow1(m.y), dotRow1(m.z));
00118 setRow2(dotRow2(m.x), dotRow2(m.y), dotRow2(m.z));
00119 setRow3(dotRow3(m.x), dotRow3(m.y), dotRow3(m.z));
00120 return *this; }
00121 Matrix3x3 &operator/=(const Matrix3x3 &m) { Matrix3x3 n; n.setInverseOf(m);
00122 operator*=(n); return *this; }
00123 Matrix3x3 &operator*=(double f) { x *= f; y *= f; z *= f; return *this; }
00124 Matrix3x3 &operator/=(double f) { x /= f; y /= f; z /= f; return *this; }
00125
00126 bool operator==(const Matrix3x3 &m) const { return x == m.x && y == m.y && z == m.z; }
00127 bool operator!=(const Matrix3x3 &m) const { return x != m.x || y != m.y || z != m.z; }
00128
00129 void set(const Vector3 &x, const Vector3 &y, const Vector3 &z)
00130 {
00131 this->x = x; this->y = y; this->z = z;
00132 }
00133
00134 void setRow1(const Vector3 &v) { x[0] = v[0]; y[0] = v[1]; z[0] = v[2]; }
00135 void setRow2(const Vector3 &v) { x[1] = v[0]; y[1] = v[1]; z[1] = v[2]; }
00136 void setRow3(const Vector3 &v) { x[2] = v[0]; y[2] = v[1]; z[2] = v[2]; }
00137
00138 void setRow1(double x, double y, double z) { this->x[0] = x; this->y[0] = y; this->z[0] = z; }
00139 void setRow2(double x, double y, double z) { this->x[1] = x; this->y[1] = y; this->z[1] = z; }
00140 void setRow3(double x, double y, double z) { this->x[2] = x; this->y[2] = y; this->z[2] = z; }
00141
00147 void setRotation(const Rotation3 &);
00148
00152 void setRotation(const Quaternion &);
00153
00157 void setInverseOf(const Matrix3x3 &);
00158
00159 void negate()
00160 {
00161 x.negate();
00162 y.negate();
00163 z.negate();
00164 }
00165
00166 void transpose()
00167 {
00168 double t;
00169 t = y[0]; y[0] = x[1]; x[1] = t;
00170 t = z[0]; z[0] = x[2]; x[2] = t;
00171 t = z[1]; z[1] = y[2]; y[2] = t;
00172 }
00173
00181 void scale(const Vector3 &s)
00182 {
00183 x*=s[0];
00184 y*=s[1];
00185 z*=s[2];
00186 }
00187
00195 void invScale(const Vector3 &s)
00196 {
00197 x/=s[0];
00198 y/=s[1];
00199 z/=s[2];
00200 }
00201
00209 void rotate(const Rotation3 &r);
00210
00214 void pitch(double angle);
00215
00219 void yaw(double angle);
00220
00224 void roll(double angle);
00225
00234 void rotateRightHandedOrthoNormal(const Rotation3 &);
00235
00244 void pitchRightHandedOrthoNormal(double angle);
00245
00254 void yawRightHandedOrthoNormal(double angle);
00255
00264 void rollRightHandedOrthoNormal(double angle);
00265
00266 private:
00267 static Matrix3x3 makeZero();
00268 static Matrix3x3 makeIdentity();
00269 };
00270
00271 inline Matrix3x3 transpose(const Matrix3x3 &m)
00272 {
00273 Matrix3x3 n(m); n.transpose(); return n;
00274 }
00275
00276 inline Matrix3x3 operator*(double f, const Matrix3x3 &m)
00277 {
00278 Matrix3x3 n(m); return n *= f;
00279 }
00280
00281 inline Matrix3x3 operator/(double f, const Matrix3x3 &m)
00282 {
00283 Matrix3x3 n; n.setInverseOf(m); return n *= f;
00284 }
00285
00289 inline Vector3 &operator*=(Vector3 &v, const Matrix3x3 &m)
00290 {
00291 v.set(dot(v, m.x), dot(v, m.y), dot(v, m.z)); return v;
00292 }
00293
00297 inline Vector3 operator*(const Vector3 &v, const Matrix3x3 &m)
00298 {
00299 Vector3 w(v); return w *= m;
00300 }
00301
00302 std::ostream &operator<<(std::ostream &, const Matrix3x3 &);
00303 }
00304 }
00305
00306 #endif // ARCHON_MATH_MATRIX_H