My Project
 All Classes Files Functions Variables Enumerations Pages
matrix33.h
Go to the documentation of this file.
1 /**************************************************************************/
2 /* Copyright 2009 Tim Day */
3 /* */
4 /* This file is part of Fracplanet */
5 /* */
6 /* Fracplanet is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* Fracplanet is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with Fracplanet. If not, see <http://www.gnu.org/licenses/>. */
18 /**************************************************************************/
19 
24 #ifndef _matrix33_h_
25 #define _matrix33_h_
26 
27 #include "xyz.h"
28 
30 class Matrix33
31 {
32  public:
33 
35  XYZ basis[3];
36 
38 
41  {}
42 
44  Matrix33(const XYZ& rx,const XYZ& ry,const XYZ& rz)
45  {
46  basis[0]=rx;
47  basis[1]=ry;
48  basis[2]=rz;
49  }
50 
53  {}
54 
55  void assign(const Matrix33& m)
56  {
57  basis[0]=m.basis[0];
58  basis[1]=m.basis[1];
59  basis[2]=m.basis[2];
60  }
61 
63  const float& element(uint row,uint col) const
64  {
65  return basis[col].element(row);
66  }
67 
69  float& element(uint row,uint col)
70  {
71  return basis[col].element(row);
72  }
73 
75  const XYZ row0() const
76  {
77  return XYZ(basis[0].x,basis[1].x,basis[2].x);
78  }
79 
81  const XYZ row1() const
82  {
83  return XYZ(basis[0].y,basis[1].y,basis[2].y);
84  }
85 
87  const XYZ row2() const
88  {
89  return XYZ(basis[0].z,basis[1].z,basis[2].z);
90  }
91 
93  float cofactor(uint row,uint col) const;
94 
96  float determinant() const;
97 
99  const Matrix33 inverted() const;
100 };
101 
103 inline const Matrix33 operator*(float k,const Matrix33& m)
104 {
105  return Matrix33(k*m.basis[0],k*m.basis[1],k*m.basis[2]);
106 }
107 
109 inline const Matrix33 operator*(const Matrix33& m,float k)
110 {
111  return Matrix33(m.basis[0]*k,m.basis[1]*k,m.basis[2]*k);
112 }
113 
115 inline const Matrix33 operator/(const Matrix33& m,float k)
116 {
117  return Matrix33(m.basis[0]/k,m.basis[1]/k,m.basis[2]/k);
118 }
119 
121 inline const XYZ operator*(const Matrix33& m,const XYZ& v)
122 {
123  return XYZ
124  (
125  m.row0()%v,
126  m.row1()%v,
127  m.row2()%v
128  );
129 }
130 
132 inline const Matrix33 operator*(const Matrix33& a,const Matrix33& b)
133 {
134  return Matrix33
135  (
136  a*b.basis[0],
137  a*b.basis[1],
138  a*b.basis[2]
139  );
140 }
141 
144 {
145  public:
146 
148  {
149  basis[0]=XYZ(1.0f,0.0f,0.0f);
150  basis[1]=XYZ(0.0f,1.0f,0.0f);
151  basis[2]=XYZ(0.0f,0.0f,1.0f);
152  }
153 };
154 
156 {
157  public:
158 
159  Matrix33RotateAboutX(float angle)
160  {
161  const float ca=cos(angle);
162  const float sa=sin(angle);
163  basis[0]=XYZ(1.0f,0.0f,0.0f);
164  basis[1]=XYZ(0.0f, ca, sa);
165  basis[2]=XYZ(0.0f,-sa, ca);
166  }
167 };
168 
170 {
171  public:
172 
173  Matrix33RotateAboutY(float angle)
174  {
175  const float ca=cos(angle);
176  const float sa=sin(angle);
177  basis[0]=XYZ(ca, 0.0f,-sa);
178  basis[1]=XYZ(0.0f,1.0f,0.0f);
179  basis[2]=XYZ(sa ,0.0f,ca);
180  }
181 };
182 
184 {
185  public:
186 
187  Matrix33RotateAboutZ(float angle)
188  {
189  const float ca=cos(angle);
190  const float sa=sin(angle);
191  basis[0]=XYZ( ca,sa,0.0f);
192  basis[1]=XYZ(-sa,ca,0.0f);
193  basis[2]=XYZ(0.0f,0.0f,1.0f);
194  }
195 };
196 
198 {
199  public:
200 
202  Matrix33RotateAboutAxis(const XYZ& axis,float angle)
203  {
204  // Want 2 vectors perpendicular to axis TODO: Check for degenerate cases
205  const XYZ axis_ortho0((axis*XYZ(1.0,0.0,0.0)).normalised());
206  const XYZ axis_ortho1(axis*axis_ortho0);
207 
208  // The matrix which rotates identity basis to axis&orthos. z axis goes to passed in axis
209  const Matrix33 xyz_to_axis
210  (
211  axis_ortho0,
212  axis_ortho1,
213  axis
214  );
215 
216  const Matrix33 axis_to_xyz(xyz_to_axis.inverted());
217 
218  assign(xyz_to_axis*Matrix33RotateAboutZ(angle)*axis_to_xyz);
219  }
220 };
221 
222 #endif