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