source: GTP/trunk/Lib/Vis/Preprocessing/src/mixkit/MxMat4.h @ 1097

Revision 1097, 5.1 KB checked in by mattausch, 18 years ago (diff)
Line 
1#ifndef MXMAT4_INCLUDED // -*- C++ -*-
2#define MXMAT4_INCLUDED
3#if !defined(__GNUC__)
4#  pragma once
5#endif
6
7/************************************************************************
8
9  4x4 Matrix class
10
11  Copyright (C) 1998 Michael Garland.  See "COPYING.txt" for details.
12 
13  $Id: MxMat4.h,v 1.1 2002/09/24 16:53:54 wimmer Exp $
14
15 ************************************************************************/
16
17#include "MxMath.h"
18#include "MxVec4.h"
19
20class Mat4
21{
22private:
23    Vec4 row[4];
24
25protected:
26
27    inline void copy(const Mat4& m);
28    inline Vec4 col(int i) const
29        { return Vec4(row[0][i],row[1][i],row[2][i],row[3][i]); }
30
31public:
32    // Standard matrices
33    static Mat4 I;
34    static Mat4 zero;
35    static Mat4 unit;
36
37    static Mat4 trans(double,double,double);
38    static Mat4 scale(double,double,double);
39    static Mat4 xrot(double); //
40    static Mat4 yrot(double); // Arguments are in radians
41    static Mat4 zrot(double); //
42
43
44    // Standard constructors
45    Mat4() { copy(zero); }
46    Mat4(const Vec4& r0,const Vec4& r1,const Vec4& r2,const Vec4& r3)
47    { row[0]=r0; row[1]=r1; row[2]=r2; row[3]=r3; }
48    Mat4(const Mat4& m) { copy(m); }
49
50    // Access methods
51    // M(i, j) == row i;col j
52    double& operator()(int i, int j)       { return row[i][j]; }
53    double  operator()(int i, int j) const { return row[i][j]; }
54    Vec4&       operator[](int i)       { return row[i]; }
55    const Vec4& operator[](int i) const { return row[i]; }
56
57    operator       double*()       { return row[0]; }
58    operator const double*() const { return row[0]; }
59
60    // Comparison methods
61    inline int operator==(const Mat4&);
62
63    // Assignment methods
64    inline Mat4& operator=(const Mat4& m) { copy(m); return *this; }
65    inline Mat4& operator+=(const Mat4& m);
66    inline Mat4& operator-=(const Mat4& m);
67
68    inline Mat4& operator*=(double s);
69    inline Mat4& operator/=(double s);
70
71
72    // Arithmetic methods
73    inline Mat4 operator+(const Mat4& m) const;
74    inline Mat4 operator-(const Mat4& m) const;
75    inline Mat4 operator-() const;
76
77    inline Mat4 operator*(double s) const;
78    inline Mat4 operator/(double s) const;
79    Mat4 operator*(const Mat4& m) const;
80
81    inline Vec4 operator*(const Vec4& v) const; // [x y z w]
82    inline Vec3 operator*(const Vec3& v) const; // [x y z w]
83
84    // Matrix operations
85    double det() const;
86    Mat4 transpose() const;
87    Mat4 adjoint() const;
88    double invert(Mat4&) const;
89    double cramerInvert(Mat4&) const;
90};
91
92
93
94inline void Mat4::copy(const Mat4& m)
95{
96    row[0] = m.row[0]; row[1] = m.row[1];
97    row[2] = m.row[2]; row[3] = m.row[3];
98}
99
100inline int Mat4::operator==(const Mat4& m)
101{
102    return row[0]==m.row[0] &&
103           row[1]==m.row[1] &&
104           row[2]==m.row[2] &&
105           row[3]==m.row[3] ;
106}
107
108inline Mat4& Mat4::operator+=(const Mat4& m)
109{
110    row[0] += m.row[0]; row[1] += m.row[1];
111    row[2] += m.row[2]; row[3] += m.row[3];
112    return *this;
113}
114
115inline Mat4& Mat4::operator-=(const Mat4& m)
116{
117    row[0] -= m.row[0]; row[1] -= m.row[1];
118    row[2] -= m.row[2]; row[3] -= m.row[3];
119    return *this;
120}
121
122inline Mat4& Mat4::operator*=(double s)
123{
124    row[0] *= s; row[1] *= s; row[2] *= s; row[3] *= s;
125    return *this;
126}
127
128inline Mat4& Mat4::operator/=(double s)
129{
130    row[0] /= s; row[1] /= s; row[2] /= s; row[3] /= s;
131    return *this;
132}
133
134inline Mat4 Mat4::operator+(const Mat4& m) const
135{
136    return Mat4(row[0]+m.row[0],
137                row[1]+m.row[1],
138                row[2]+m.row[2],
139                row[3]+m.row[3]);
140}
141
142inline Mat4 Mat4::operator-(const Mat4& m) const
143{
144    return Mat4(row[0]-m.row[0],
145                row[1]-m.row[1],
146                row[2]-m.row[2],
147                row[3]-m.row[3]);
148}
149
150inline Mat4 Mat4::operator-() const
151{
152    return Mat4(-row[0], -row[1], -row[2], -row[3]);
153}
154
155inline Mat4 Mat4::operator*(double s) const
156{
157    return Mat4(row[0]*s, row[1]*s, row[2]*s, row[3]*s);
158}
159
160inline Mat4 Mat4::operator/(double s) const
161{
162    return Mat4(row[0]/s, row[1]/s, row[2]/s, row[3]/s);
163}
164
165inline Vec4 Mat4::operator*(const Vec4& v) const
166{
167    return Vec4(row[0]*v, row[1]*v, row[2]*v, row[3]*v);
168}
169
170//
171// Transform a homogeneous 3-vector and reproject into normal 3-space
172//
173inline Vec3 Mat4::operator*(const Vec3& v) const
174{
175    Vec4 u=Vec4(v,1);
176    double w=row[3]*u;
177
178    if(w==0.0)
179        return Vec3(row[0]*u, row[1]*u, row[2]*u);
180    else
181        return Vec3(row[0]*u/w, row[1]*u/w, row[2]*u/w);
182}
183
184inline ostream& operator<<(ostream& out, const Mat4& M)
185{
186    return out<<M[0]<<endl<<M[1]<<endl<<M[2]<<endl<<M[3];
187}
188
189inline istream& operator>>(istream& in, Mat4& M)
190{
191    return in >> M[0] >> M[1] >> M[2] >> M[3];
192}
193
194extern bool jacobi(const Mat4& m, Vec4& vals, Vec4 vecs[4]);
195
196#ifdef MXGL_INCLUDED
197inline void glGetMatrix(Mat4& m, GLenum which=GL_MODELVIEW_MATRIX)
198{ Mat4 tmp;  glGetDoublev(which, &tmp(0,0));  m=tmp.transpose(); }
199
200inline void glLoadMatrix(const Mat4& m)
201{ Mat4 tmp = m.transpose();  glLoadMatrixd(&tmp(0,0)); }
202
203inline void glMultMatrix(const Mat4& m)
204{ Mat4 tmp = m.transpose();  glMultMatrixd(&tmp(0,0)); }
205#endif
206
207// MXMAT4_INCLUDED
208#endif
Note: See TracBrowser for help on using the repository browser.