#ifndef FILE_GEOMOPS #define FILE_GEOMOPS /* *************************************************************************/ /* File: geomops.hpp */ /* Author: Joachim Schoeberl */ /* Date: 20. Jul. 02 */ /* *************************************************************************/ /* Point - Vector operations */ template inline Vec operator+ (const Vec & a, const Vec & b) { Vec res; for (int i = 0; i < D; i++) res(i) = a(i) + b(i); return res; } template inline Point operator+ (const Point & a, const Vec & b) { Point res; for (int i = 0; i < D; i++) res(i) = a(i) + b(i); return res; } template inline Vec operator- (const Point & a, const Point & b) { Vec res; for (int i = 0; i < D; i++) res(i) = a(i) - b(i); return res; } template inline Point operator- (const Point & a, const Vec & b) { Point res; for (int i = 0; i < D; i++) res(i) = a(i) - b(i); return res; } template inline Vec operator- (const Vec & a, const Vec & b) { Vec res; for (int i = 0; i < D; i++) res(i) = a(i) - b(i); return res; } template inline Vec operator* (double s, const Vec & b) { Vec res; for (int i = 0; i < D; i++) res(i) = s * b(i); return res; } template inline double operator* (const Vec & a, const Vec & b) { double sum = 0; for (int i = 0; i < D; i++) sum += a(i) * b(i); return sum; } template inline Vec operator- (const Vec & b) { Vec res; for (int i = 0; i < D; i++) res(i) = -b(i); return res; } template inline Point & operator+= (Point & a, const Vec & b) { for (int i = 0; i < D; i++) a(i) += b(i); return a; } template inline Vec & operator+= (Vec & a, const Vec & b) { for (int i = 0; i < D; i++) a(i) += b(i); return a; } template inline Point & operator-= (Point & a, const Vec & b) { for (int i = 0; i < D; i++) a(i) -= b(i); return a; } template inline Vec & operator-= (Vec & a, const Vec & b) { for (int i = 0; i < D; i++) a(i) -= b(i); return a; } template inline Vec & operator*= (Vec & a, double s) { for (int i = 0; i < D; i++) a(i) *= s; return a; } template inline Vec & operator/= (Vec & a, double s) { for (int i = 0; i < D; i++) a(i) /= s; return a; } // Matrix - Vector operations /* template inline Vec operator* (const Mat & m, const Vec & v) { Vec res; for (int i = 0; i < H; i++) { res(i) = 0; for (int j = 0; j < W; j++) res(i) += m(i,j) * v(j); } return res; } */ // thanks to VC60 partial template specialization features !!! inline Vec<2> operator* (const Mat<2,2> & m, const Vec<2> & v) { Vec<2> res; for (int i = 0; i < 2; i++) { res(i) = 0; for (int j = 0; j < 2; j++) res(i) += m(i,j) * v(j); } return res; } inline Vec<2> operator* (const Mat<2,3> & m, const Vec<3> & v) { Vec<2> res; for (int i = 0; i < 2; i++) { res(i) = 0; for (int j = 0; j < 3; j++) res(i) += m(i,j) * v(j); } return res; } inline Vec<3> operator* (const Mat<3,2> & m, const Vec<2> & v) { Vec<3> res; for (int i = 0; i < 3; i++) { res(i) = 0; for (int j = 0; j < 2; j++) res(i) += m(i,j) * v(j); } return res; } inline Vec<3> operator* (const Mat<3,3> & m, const Vec<3> & v) { Vec<3> res; for (int i = 0; i < 3; i++) { res(i) = 0; for (int j = 0; j < 3; j++) res(i) += m(i,j) * v(j); } return res; } /* template inline Mat operator* (const Mat & a, const Mat & b) { Mat m; for (int i = 0; i < H1; i++) for (int j = 0; j < W2; j++) { double sum = 0; for (int k = 0; k < W1; k++) sum += a(i,k) * b(k, j); m(i,j) = sum; } return m; } */ inline Mat<2,2> operator* (const Mat<2,2> & a, const Mat<2,2> & b) { Mat<2,2> m; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) { double sum = 0; for (int k = 0; k < 2; k++) sum += a(i,k) * b(k, j); m(i,j) = sum; } return m; } inline Mat<2,2> operator* (const Mat<2,3> & a, const Mat<3,2> & b) { Mat<2,2> m; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) { double sum = 0; for (int k = 0; k < 3; k++) sum += a(i,k) * b(k, j); m(i,j) = sum; } return m; } inline Mat<3,2> operator* (const Mat<3,2> & a, const Mat<2,2> & b) { Mat<3,2> m; for (int i = 0; i < 3; i++) for (int j = 0; j < 2; j++) { double sum = 0; for (int k = 0; k < 2; k++) sum += a(i,k) * b(k, j); m(i,j) = sum; } return m; } inline Mat<2,3> operator* (const Mat<2,2> & a, const Mat<2,3> & b) { Mat<2,3> m; for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) { double sum = 0; for (int k = 0; k < 2; k++) sum += a(i,k) * b(k, j); m(i,j) = sum; } return m; } inline Mat<3,3> operator* (const Mat<3,3> & a, const Mat<3,3> & b) { Mat<3,3> m; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { double sum = 0; for (int k = 0; k < 3; k++) sum += a(i,k) * b(k, j); m(i,j) = sum; } return m; } template inline Mat Trans (const Mat & m) { Mat res; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) res(j,i) = m(i,j); return res; } template inline ostream & operator<< (ostream & ost, const Vec & a) { ost << "("; for (int i = 0; i < D-1; i++) ost << a(i) << ", "; ost << a(D-1) << ")"; return ost; } template inline ostream & operator<< (ostream & ost, const Point & a) { ost << "("; for (int i = 0; i < D-1; i++) ost << a(i) << ", "; ost << a(D-1) << ")"; return ost; } template inline ostream & operator<< (ostream & ost, const Box & b) { ost << b.PMin() << " - " << b.PMax(); return ost; } template inline ostream & operator<< (ostream & ost, const Mat & m) { ost << "("; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) ost << m(i,j) << " "; ost << endl; } return ost; } #endif