1 | #ifndef MXMAT3_INCLUDED // -*- C++ -*-
|
---|
2 | #define MXMAT3_INCLUDED
|
---|
3 | #if !defined(__GNUC__)
|
---|
4 | # pragma once
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | /************************************************************************
|
---|
8 |
|
---|
9 | 3x3 Matrix class
|
---|
10 |
|
---|
11 | Copyright (C) 1998 Michael Garland. See "COPYING.txt" for details.
|
---|
12 |
|
---|
13 | $Id: MxMat3.h,v 1.1 2002/09/24 16:53:54 wimmer Exp $
|
---|
14 |
|
---|
15 | ************************************************************************/
|
---|
16 |
|
---|
17 | #include "MxMath.h"
|
---|
18 | #include "MxVec3.h"
|
---|
19 |
|
---|
20 | class Mat3
|
---|
21 | {
|
---|
22 | private:
|
---|
23 | Vec3 row[3];
|
---|
24 |
|
---|
25 | protected:
|
---|
26 |
|
---|
27 | inline void copy(const Mat3& m);
|
---|
28 | inline Vec3 col(int i) const {return Vec3(row[0][i],row[1][i],row[2][i]);}
|
---|
29 |
|
---|
30 | public:
|
---|
31 | // Standard matrices
|
---|
32 | static Mat3 I;
|
---|
33 | static Mat3 zero;
|
---|
34 | static Mat3 unit;
|
---|
35 | static Mat3 diag(const Vec3& v); // diagonal elements from v
|
---|
36 | static Mat3 extend(const Vec3& v); // each row becomes v
|
---|
37 | static Mat3 outer_product(const Vec3& v); // outer product of v by v
|
---|
38 |
|
---|
39 | // Standard constructors
|
---|
40 | Mat3() { copy(zero); }
|
---|
41 | Mat3(const Vec3& r0,const Vec3& r1,const Vec3& r2)
|
---|
42 | { row[0]=r0; row[1]=r1; row[2]=r2; }
|
---|
43 | Mat3(const Mat3& m) { copy(m); }
|
---|
44 |
|
---|
45 | // Access methods
|
---|
46 | // M(i, j) == row i;col j
|
---|
47 | double& operator()(int i, int j) { return row[i][j]; }
|
---|
48 | double operator()(int i, int j) const { return row[i][j]; }
|
---|
49 | Vec3& operator[](int i) { return row[i]; }
|
---|
50 | const Vec3& operator[](int i) const { return row[i]; }
|
---|
51 |
|
---|
52 | operator double*() { return row[0]; }
|
---|
53 | operator const double*() const { return row[0]; }
|
---|
54 |
|
---|
55 | // Assignment methods
|
---|
56 | inline Mat3& operator=(const Mat3& m) { copy(m); return *this; }
|
---|
57 | inline Mat3& operator+=(const Mat3& m);
|
---|
58 | inline Mat3& operator-=(const Mat3& m);
|
---|
59 |
|
---|
60 | inline Mat3& operator*=(double s);
|
---|
61 | inline Mat3& operator/=(double s);
|
---|
62 |
|
---|
63 |
|
---|
64 | // Arithmetic methods
|
---|
65 | inline Mat3 operator+(const Mat3& m) const;
|
---|
66 | inline Mat3 operator-(const Mat3& m) const;
|
---|
67 | inline Mat3 operator-() const;
|
---|
68 |
|
---|
69 | inline Mat3 operator*(double s) const;
|
---|
70 | inline Mat3 operator/(double s) const;
|
---|
71 | Mat3 operator*(const Mat3& m) const;
|
---|
72 |
|
---|
73 | inline Vec3 operator*(const Vec3& v) const; // [x y z]
|
---|
74 |
|
---|
75 | // Matrix operations
|
---|
76 | double det();
|
---|
77 | double trace();
|
---|
78 | Mat3 transpose();
|
---|
79 | Mat3 adjoint();
|
---|
80 | double invert(Mat3&);
|
---|
81 | };
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 | inline void Mat3::copy(const Mat3& m)
|
---|
86 | {
|
---|
87 | row[0] = m.row[0]; row[1] = m.row[1]; row[2] = m.row[2];
|
---|
88 | }
|
---|
89 |
|
---|
90 | inline Mat3& Mat3::operator+=(const Mat3& m)
|
---|
91 | {
|
---|
92 | row[0] += m.row[0]; row[1] += m.row[1]; row[2] += m.row[2];
|
---|
93 | return *this;
|
---|
94 | }
|
---|
95 |
|
---|
96 | inline Mat3& Mat3::operator-=(const Mat3& m)
|
---|
97 | {
|
---|
98 | row[0] -= m.row[0]; row[1] -= m.row[1]; row[2] -= m.row[2];
|
---|
99 | return *this;
|
---|
100 | }
|
---|
101 |
|
---|
102 | inline Mat3& Mat3::operator*=(double s)
|
---|
103 | {
|
---|
104 | row[0] *= s; row[1] *= s; row[2] *= s;
|
---|
105 | return *this;
|
---|
106 | }
|
---|
107 |
|
---|
108 | inline Mat3& Mat3::operator/=(double s)
|
---|
109 | {
|
---|
110 | row[0] /= s; row[1] /= s; row[2] /= s;
|
---|
111 | return *this;
|
---|
112 | }
|
---|
113 |
|
---|
114 | inline Mat3 Mat3::operator+(const Mat3& m) const
|
---|
115 | {
|
---|
116 | return Mat3(row[0]+m.row[0],
|
---|
117 | row[1]+m.row[1],
|
---|
118 | row[2]+m.row[2]);
|
---|
119 | }
|
---|
120 |
|
---|
121 | inline Mat3 Mat3::operator-(const Mat3& m) const
|
---|
122 | {
|
---|
123 | return Mat3(row[0]-m.row[0],
|
---|
124 | row[1]-m.row[1],
|
---|
125 | row[2]-m.row[2]);
|
---|
126 | }
|
---|
127 |
|
---|
128 | inline Mat3 Mat3::operator-() const
|
---|
129 | {
|
---|
130 | return Mat3(-row[0], -row[1], -row[2]);
|
---|
131 | }
|
---|
132 |
|
---|
133 | inline Mat3 Mat3::operator*(double s) const
|
---|
134 | {
|
---|
135 | return Mat3(row[0]*s, row[1]*s, row[2]*s);
|
---|
136 | }
|
---|
137 |
|
---|
138 | inline Mat3 Mat3::operator/(double s) const
|
---|
139 | {
|
---|
140 | return Mat3(row[0]/s, row[1]/s, row[2]/s);
|
---|
141 | }
|
---|
142 |
|
---|
143 | inline Vec3 Mat3::operator*(const Vec3& v) const
|
---|
144 | {
|
---|
145 | return Vec3(row[0]*v, row[1]*v, row[2]*v);
|
---|
146 | }
|
---|
147 |
|
---|
148 | inline ostream& operator<<(ostream& out, const Mat3& M)
|
---|
149 | {
|
---|
150 | return out << M[0] << endl << M[1] << endl << M[2];
|
---|
151 | }
|
---|
152 |
|
---|
153 | inline istream& operator>>(istream& in, Mat3& M)
|
---|
154 | {
|
---|
155 | return in >> M[0] >> M[1] >> M[2];
|
---|
156 | }
|
---|
157 |
|
---|
158 | extern bool jacobi(const Mat3& m, Vec3& vals, Vec3 vecs[3]);
|
---|
159 | extern bool jacobi(const Mat3& m, double *vals, double *vecs);
|
---|
160 |
|
---|
161 | extern bool fast_jacobi(const Mat3& m, Vec3& vals, Vec3 vecs[3]);
|
---|
162 |
|
---|
163 | // MXMAT3_INCLUDED
|
---|
164 | #endif
|
---|