#ifndef LIN_ALG_H #define LIN_ALG_H #include #include #include "orient_point.h" template class matrix_t { public: matrix_t() {} matrix_t(const point_t& x, const point_t& y, const point_t& z) {a=x; b=y; c=z;} matrix_t(const matrix_t& M) {a=M.a; b=M.b; c=M.c;} matrix_t& operator= (const matrix_t& M) {a=M.a; b=M.b; c=M.c; return *this;} void MakeTranspose() { std::swap(a.y, b.x); std::swap(c.y, b.z); std::swap(a.z, c.x); } matrix_t& operator *= (const matrix_t& M) {return *this = *this * M;} matrix_t& operator += (const matrix_t& M) {a+=M.a; b+=M.b; c+=M.c; return *this;} matrix_t& operator -= (const matrix_t& M) {a-=M.a; b-=M.b; c-=M.c; return *this;} matrix_t operator + (const matrix_t& M) const {return matrix_t(*this) += M;} matrix_t operator - (const matrix_t& M) const {return matrix_t(*this) -= M;} matrix_t operator * (const matrix_t& M) const { matrix_t tmp(M), R; tmp.MakeTranspose(); point_t A(a * tmp.a, a * tmp.b, a * tmp.c); point_t B(b * tmp.a, b * tmp.b, b * tmp.c); point_t C(c * tmp.a, c * tmp.b, c * tmp.c); return matrix_t(A, B, C); } point_t operator * (const point_t& V) const {return point_t(a * V, b * V, c * V);} double Det() const {return a.cross(b) * c;} static double Det(point_t& x, point_t& y, point_t& z) {return x.cross(y) * z;} matrix_t Inverse() const { point_t A(b.cross(c)); point_t B(c.cross(a)); point_t C(a.cross(b)); double D = 1.0 / (C * c); matrix_t M(A * D, B * D, C * D); M.MakeTranspose(); return M; } point_t a, b, c; }; template std::ostream& operator << (std::ostream& o, const point_t& V) { o << "(" << V.x << ", " << V.y << ", " << V.z << ")"; return o; } template std::ostream& operator << (std::ostream& o, const matrix_t& V) { o << "[" << V.a << ", " << V.b << ", " << V.c << "]"; return o; } template void update_min(point_t&mn, const point_t& v) { mn.x = std::min(mn.x, v.x); mn.y = std::min(mn.y, v.y); mn.z = std::min(mn.z, v.z); } template void update_max(point_t&mx, const point_t& v) { mx.x = std::max(mx.x, v.x); mx.y = std::max(mx.y, v.y); mx.z = std::max(mx.z, v.z); } template void update_min_max(point_t&mn, point_t&mx, const point_t& v) { update_min(mn, v); update_max(mx, v); } static point_t unit_x(1,0,0), unit_y(0,1,0), unit_z(0,0,1); static point_t vec_zero(0,0,0), vec_one(1,1,1); static matrix_t mat_zero(vec_zero, vec_zero, vec_zero); static matrix_t mat_ident(unit_x, unit_y, unit_z); typedef point_t vector3f; typedef point_t vector3d; typedef matrix_t matrix3f; typedef matrix_t matrix3d; #endif