LOOS  v2.3.2
Matrix44.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 
25 
26 
27 #if !defined(LOOS_MATRIX44_HPP)
28 #define LOOS_MATRIX44_HPP
29 
30 #include <iostream>
31 #include <stdexcept>
32 #include <string>
33 
34 #include <loos_defs.hpp>
35 
36 #include <string.h>
37 
38 
39 namespace loos {
40 
41 #if !defined(SWIG)
42  // Forward declaration for matrix-vector multiply
43  template<class T> Coord<T> operator*(const Matrix44<T>&, const Coord<T>&);
44 #endif
45 
47  template<class T>
48  class Matrix44 {
49 
50  T matrix[16];
51 
52  public:
53 
55  Matrix44() { identity(); }
56 
58  explicit Matrix44(const T v) { for (int i = 0; i < 16; i++) matrix[i] = v; }
59 
61  void zero(void) { memset(matrix, 0, 16 * sizeof(T)); }
62 
64  void identity(void) { zero(); matrix[0] = 1; matrix[5] = 1; matrix[10] = 1; matrix[15] = 1; }
65 
67  const T& operator()(const int j, const int i) const {
68  if (j < 0 || i < 0 || i > 3 || j > 3)
69  throw(std::range_error("Indices into matrix are out of range"));
70  return(matrix[j*4+i]);
71  }
72 
73 
74 #if !defined(SWIG)
75  T& operator()(const int j, const int i) {
77  if (j < 0 || i < 0 || i > 3 || j > 3)
78  throw(std::range_error("Indices into matrix are out of range"));
79  return(matrix[j*4+i]);
80  }
81 
83  T& operator[](const int i) {
84  if (i < 0 || i > 15)
85  throw(std::range_error("Index into matrix is out of range"));
86  return(matrix[i]);
87  }
88 
90  const T& operator[](const int i) const {
91  if (i < 0 || i > 15)
92  throw(std::range_error("Index into matrix is out of range"));
93  return(matrix[i]);
94  }
95 
96 
101 
102  friend Matrix44<T> operator+(const T lhs, const Matrix44<T>& rhs) {
103  Matrix44<T> res(rhs);
104  res += lhs;
105  return(res);
106  }
107 
109  friend Matrix44<T> operator-(const T lhs, const Matrix44<T>& rhs) {
110  Matrix44<T> res(rhs);
111  res -= lhs;
112  return(res);
113  }
114 
116  friend Coord<T> operator*<>(const Matrix44<T>&, const Coord<T>&);
117 
118 #endif // !defined(SWIG)
119 
121  T* data(void) { return(matrix); }
122 
123 
126  int i;
127  for (i=0; i<16; i++)
128  matrix[i] += rhs.matrix[i];
129  return(*this);
130  }
131 
134  Matrix44<T> res(*this);
135  res += rhs;
136  return(res);
137  }
138 
141  int i;
142  for (i=0; i<16; i++)
143  matrix[i] -= rhs.matrix[i];
144  return(*this);
145  }
146 
149  Matrix44<T> res(*this);
150  res -= rhs;
151  return(res);
152  }
153 
154 
157  *this = *this * rhs;
158  return(*this);
159  }
160 
162  Matrix44<T> operator*(const Matrix44<T>& rhs) const {
163  Matrix44<T> res;
164 
165  res.matrix[0] = matrix[0]*rhs.matrix[0] + matrix[1]*rhs.matrix[4] + matrix[2]*rhs.matrix[8] + matrix[3]*rhs.matrix[12];
166  res.matrix[1] = matrix[0]*rhs.matrix[1] + matrix[1]*rhs.matrix[5] + matrix[2]*rhs.matrix[9] + matrix[3]*rhs.matrix[13];
167  res.matrix[2] = matrix[0]*rhs.matrix[2] + matrix[1]*rhs.matrix[6] + matrix[2]*rhs.matrix[10] + matrix[3]*rhs.matrix[14];
168  res.matrix[3] = matrix[0]*rhs.matrix[3] + matrix[1]*rhs.matrix[7] + matrix[2]*rhs.matrix[11] + matrix[3]*rhs.matrix[15];
169 
170  res.matrix[4] = matrix[4]*rhs.matrix[0] + matrix[5]*rhs.matrix[4] + matrix[6]*rhs.matrix[8] + matrix[7]*rhs.matrix[12];
171  res.matrix[5] = matrix[4]*rhs.matrix[1] + matrix[5]*rhs.matrix[5] + matrix[6]*rhs.matrix[9] + matrix[7]*rhs.matrix[13];
172  res.matrix[6] = matrix[4]*rhs.matrix[2] + matrix[5]*rhs.matrix[6] + matrix[6]*rhs.matrix[10] + matrix[7]*rhs.matrix[14];
173  res.matrix[7] = matrix[4]*rhs.matrix[3] + matrix[5]*rhs.matrix[7] + matrix[6]*rhs.matrix[11] + matrix[7]*rhs.matrix[15];
174 
175  res.matrix[8] = matrix[8]*rhs.matrix[0] + matrix[9]*rhs.matrix[4] + matrix[10]*rhs.matrix[8] + matrix[11]*rhs.matrix[12];
176  res.matrix[9] = matrix[8]*rhs.matrix[1] + matrix[9]*rhs.matrix[5] + matrix[10]*rhs.matrix[9] + matrix[11]*rhs.matrix[13];
177  res.matrix[10] = matrix[8]*rhs.matrix[2] + matrix[9]*rhs.matrix[6] + matrix[10]*rhs.matrix[10] + matrix[11]*rhs.matrix[14];
178  res.matrix[11] = matrix[8]*rhs.matrix[3] + matrix[9]*rhs.matrix[7] + matrix[10]*rhs.matrix[11] + matrix[11]*rhs.matrix[15];
179 
180  res.matrix[12] = matrix[12]*rhs.matrix[0] + matrix[13]*rhs.matrix[4] + matrix[14]*rhs.matrix[8] + matrix[15]*rhs.matrix[12];
181  res.matrix[13] = matrix[12]*rhs.matrix[1] + matrix[13]*rhs.matrix[5] + matrix[14]*rhs.matrix[9] + matrix[15]*rhs.matrix[13];
182  res.matrix[14] = matrix[12]*rhs.matrix[2] + matrix[13]*rhs.matrix[6] + matrix[14]*rhs.matrix[10] + matrix[15]*rhs.matrix[14];
183  res.matrix[15] = matrix[12]*rhs.matrix[3] + matrix[13]*rhs.matrix[7] + matrix[14]*rhs.matrix[11] + matrix[15]*rhs.matrix[15];
184 
185  return(res);
186  }
187 
189 
195  Matrix44<T>& operator*=(const T x) {
196  for (int i = 0; i < 16; i++)
197  matrix[i] *= x;
198  return(*this);
199  }
200 
202  Matrix44<T> operator*(const T x) {
203  Matrix44<T> res(*this);
204 
205  res *= x;
206  return(res);
207  }
208 
209 
210 #if !defined(SWIG)
211  friend Matrix44<T> operator*(const T x, const Matrix44<T>& rhs) {
213  Matrix44<T> res(rhs);
214 
215  res *= x;
216  return(res);
217  }
218 
219 
221  friend std::ostream& operator<<(std::ostream&os, const Matrix44& m) {
222  int i, j, k;
223 
224  os << "[";
225  for (k=j=0; j<4; j++) {
226  for (i=0; i<4; i++)
227  os << m[k++] << ((i == 3) ? "" : " ");
228  os << ";";
229  }
230  os << "];";
231 
232  return(os);
233  }
234 
235 #endif // !defined(SWIG)
236 
237 
238  };
239 
240 
241 #if !defined(SWIG)
242  template<class T> Coord<T> operator*(const Matrix44<T>& M, const Coord<T>& v) {
245  Coord<T> result;
246 
247  result.v[0] = v.v[0]*M.matrix[0] + v.v[1]*M.matrix[1] + v.v[2]*M.matrix[2] + v.v[3]*M.matrix[3];
248  result.v[1] = v.v[0]*M.matrix[4] + v.v[1]*M.matrix[5] + v.v[2]*M.matrix[6] + v.v[3]*M.matrix[7];
249  result.v[2] = v.v[0]*M.matrix[8] + v.v[1]*M.matrix[9] + v.v[2]*M.matrix[10] + v.v[3]*M.matrix[11];
250  result.v[3] = v.v[0]*M.matrix[12] + v.v[1]*M.matrix[13] + v.v[2]*M.matrix[14] + v.v[3]*M.matrix[15];
251 
252  return(result);
253  }
254 #endif // !defined(SWIG)
255 
256 }
257 
258 #endif
259 
Matrix44< T > & operator+=(const Matrix44< T > &rhs)
Addition of two matrices.
Definition: Matrix44.hpp:125
Matrix44< T > operator*(const T x)
Multiplication by a constant.
Definition: Matrix44.hpp:202
friend Matrix44< T > operator+(const T lhs, const Matrix44< T > &rhs)
Definition: Matrix44.hpp:102
Matrix44(const T v)
Create a new matrix with all elements set to v.
Definition: Matrix44.hpp:58
Basic 3-D coordinates class.
Definition: Coord.hpp:36
Matrix44()
Create a new identity matrix.
Definition: Matrix44.hpp:55
Matrix44< T > & operator*=(const T x)
Multiplication by a constant.
Definition: Matrix44.hpp:195
Specialized 4x4 Matrix class for handling coordinate transforms.
Definition: Coord.hpp:37
const T & operator[](const int i) const
Allow access to the linear array of matrix elements.
Definition: Matrix44.hpp:90
Matrix44< T > & operator*=(const Matrix44< T > &rhs)
Matrix-matrix multiply...
Definition: Matrix44.hpp:156
Matrix44< T > operator-(const Matrix44< T > &rhs)
Subtracting matrices.
Definition: Matrix44.hpp:148
friend Coord< T > operator*(const Matrix44< T > &, const Coord< T > &)
Friend declaration for matrix-vector multiply...
Definition: Matrix44.hpp:244
Matrix44< T > operator*(const Matrix44< T > &rhs) const
Matrix-matrix multiply.
Definition: Matrix44.hpp:162
Coord< T > operator*(const Matrix44< T > &, const Coord< T > &)
Definition: Matrix44.hpp:244
void zero(void)
Zero all elements.
Definition: Matrix44.hpp:61
void identity(void)
Identity matrix.
Definition: Matrix44.hpp:64
T * data(void)
Returns the array pointer.
Definition: Matrix44.hpp:121
Namespace for most things not already encapsulated within a class.
const T & operator()(const int j, const int i) const
Index the matrix element at row j and col i.
Definition: Matrix44.hpp:67
Matrix44< T > operator+(const Matrix44< T > &rhs)
Addition of two matrices.
Definition: Matrix44.hpp:133
friend Matrix44< T > operator-(const T lhs, const Matrix44< T > &rhs)
Subtraction of a constant from a matrix.
Definition: Matrix44.hpp:109
friend std::ostream & operator<<(std::ostream &os, const Matrix44 &m)
Output the matrix in pseudo-XML.
Definition: Matrix44.hpp:221
T & operator[](const int i)
Allow access to the linear array of matrix elements.
Definition: Matrix44.hpp:83
Matrix44< T > & operator-=(const Matrix44< T > &rhs)
Subtracting matrices.
Definition: Matrix44.hpp:140