NAMD
Matrix4 Class Reference

4x4 matrix class with numerous operators, conversions, etc. More...

`#include <Matrix4.h>`

## Public Member Functions

Matrix4 (void)
identity constructor More...

Matrix4 (double f)
const elements constructor More...

Matrix4 (const double *m)
construct from double array More...

Matrix4 (const Matrix4 &m)
copy constructor More...

~Matrix4 (void)
destructor More...

void multpoint3d (const double, double) const
multiplies a 3D point (first arg) by the Matrix, returns in second arg More...

void multnorm3d (const double, double) const
multiplies a 3D norm (first arg) by the Matrix, returns in second arg More...

void multpoint4d (const double, double) const
multiplies a 4D point (first arg) by the Matrix, returns in second arg More...

void identity (void)
clears the matrix (resets it to identity) More...

void constant (double)
sets the matrix so all items are the given constant value More...

int inverse (void)

void transpose (void)
transposes the matrix More...

void loadmatrix (const Matrix4 &m)
replaces this matrix with the given one More...

Matrix4operator= (const Matrix4 &m)

void multmatrix (const Matrix4 &)
premultiply the matrix by the given matrix, this->other * this More...

void rot (double, char)
performs a left-handed rotation around an axis (char == 'x', 'y', or 'z') More...

void rotate_axis (const double axis, double angle)
apply a rotation around the given vector; angle in radians. More...

void transvec (double x, double y, double z)
apply a rotation such that 'x' is brought along the given vector. More...

void transvecinv (double x, double y, double z)
apply a rotation such that the given vector is brought along 'x'. More...

void translate (double, double, double)
performs a translation More...

void translate (double d)

void scale (double, double, double)
performs scaling More...

void scale (double f)

void window (double, double, double, double, double, double)
sets this matrix to represent a window perspective More...

void ortho (double, double, double, double, double, double)
sets this matrix to a 3D orthographic matrix More...

void ortho2 (double, double, double, double)
sets this matrix to a 2D orthographic matrix More...

void lookat (double, double, double, double, double, double, short)

## Public Attributes

double mat 
the matrix itself More...

## Detailed Description

4x4 matrix class with numerous operators, conversions, etc.

Definition at line 26 of file Matrix4.h.

## Constructor & Destructor Documentation

 Matrix4::Matrix4 ( void )
inline

identity constructor

Definition at line 28 of file Matrix4.h.

References identity().

 Matrix4::Matrix4 ( double f )
inline

const elements constructor

Definition at line 29 of file Matrix4.h.

References constant().

 Matrix4::Matrix4 ( const double * m )

construct from double array

Definition at line 36 of file Matrix4.C.

References mat.

36  {
37  memcpy((void *)mat, (const void *)m, 16*sizeof(double));
38 }
double mat
the matrix itself
Definition: Matrix4.h:33
 Matrix4::Matrix4 ( const Matrix4 & m )
inline

copy constructor

Definition at line 31 of file Matrix4.h.

 Matrix4::~Matrix4 ( void )
inline

destructor

Definition at line 32 of file Matrix4.h.

## Member Function Documentation

 void Matrix4::constant ( double f )

sets the matrix so all items are the given constant value

Definition at line 102 of file Matrix4.C.

References mat.

Referenced by lookat(), Matrix4(), ortho(), ortho2(), and window().

102  {
103  for (int i=0; i<16; mat[i++] = f);
104 }
double mat
the matrix itself
Definition: Matrix4.h:33
 void Matrix4::identity ( void )

clears the matrix (resets it to identity)

Definition at line 92 of file Matrix4.C.

References mat.

Referenced by Matrix4().

92  {
93  memset((void *)mat, 0, 16*sizeof(double));
94  mat=1.0;
95  mat=1.0;
96  mat=1.0;
97  mat=1.0;
98 }
double mat
the matrix itself
Definition: Matrix4.h:33
 int Matrix4::inverse ( void )

inverts the matrix, that is, the inverse of the rotation, the inverse of the scaling, and the opposite of the translation vector. returns 0 if there were no problems, -1 if the matrix is singular

Definition at line 110 of file Matrix4.C.

References mat, and MATSWAP.

110  {
111
112  double matr, ident;
113  int i, j, k, l, ll;
114  int icol=0, irow=0;
115  int indxc, indxr, ipiv;
116  double big, dum, pivinv, temp;
117
118  for (i=0; i<4; i++) {
119  for (j=0; j<4; j++) {
120  matr[i][j] = mat[4*i+j];
121  ident[i][j] = 0.0;
122  }
123  ident[i][i]=1.0;
124  }
125  // Gauss-jordan elimination with full pivoting. Yes, folks, a
126  // GL Matrix4 is inverted like any other, since the identity is
127  // still the identity.
128
129  // from numerical recipies in C second edition, pg 39
130
131  for(j=0;j<=3;j++) ipiv[j] = 0;
132  for(i=0;i<=3;i++) {
133  big=0.0;
134  for (j=0;j<=3;j++) {
135  if(ipiv[j] != 1) {
136  for (k=0;k<=3;k++) {
137  if(ipiv[k] == 0) {
138  if(fabs(matr[j][k]) >= big) {
139  big = (double) fabs(matr[j][k]);
140  irow=j;
141  icol=k;
142  }
143  } else if (ipiv[k] > 1) return 1;
144  }
145  }
146  }
147  ++(ipiv[icol]);
148  if (irow != icol) {
149  for (l=0;l<=3;l++) MATSWAP(matr[irow][l],matr[icol][l]);
150  for (l=0;l<=3;l++) MATSWAP(ident[irow][l],ident[icol][l]);
151  }
152  indxr[i]=irow;
153  indxc[i]=icol;
154  if(matr[icol][icol] == 0.0) return 1;
155  pivinv = 1.0 / matr[icol][icol];
156  matr[icol][icol]=1.0;
157  for (l=0;l<=3;l++) matr[icol][l] *= pivinv;
158  for (l=0;l<=3;l++) ident[icol][l] *= pivinv;
159  for (ll=0;ll<=3;ll++) {
160  if (ll != icol) {
161  dum=matr[ll][icol];
162  matr[ll][icol]=0.0;
163  for (l=0;l<=3;l++) matr[ll][l] -= matr[icol][l]*dum;
164  for (l=0;l<=3;l++) ident[ll][l] -= ident[icol][l]*dum;
165  }
166  }
167  }
168  for (l=3;l>=0;l--) {
169  if (indxr[l] != indxc[l]) {
170  for (k=0;k<=3;k++) {
171  MATSWAP(matr[k][indxr[l]],matr[k][indxc[l]]);
172  }
173  }
174  }
175  for (i=0; i<4; i++)
176  for (j=0; j<4; j++)
177  mat[4*i+j] = matr[i][j];
178  return 0;
179 }
#define MATSWAP(a, b)
Definition: Matrix4.C:109
double mat
the matrix itself
Definition: Matrix4.h:33
 void Matrix4::loadmatrix ( const Matrix4 & m )

replaces this matrix with the given one

Definition at line 194 of file Matrix4.C.

References mat.

Referenced by Matrix4(), and operator=().

194  {
195  memcpy((void *)mat, (const void *)m.mat, 16*sizeof(double));
196 }
double mat
the matrix itself
Definition: Matrix4.h:33
 void Matrix4::lookat ( double vx, double vy, double vz, double px, double py, double pz, short twist )

This subroutine defines a viewing transformation with the eye at point (vx,vy,vz) looking at the point (px,py,pz). Twist is the right-hand rotation about this line. The resultant matrix is multiplied with the top of the transformation stack and then replaces it. Precisely, lookat does: lookat=trans(-vx,-vy,-vz)*rotate(theta,y)*rotate(phi,x)*rotate(-twist,z)

Definition at line 336 of file Matrix4.C.

References constant(), mat, multmatrix(), rot(), and translate().

337  {
338  Matrix4 m(0.0);
339  double tmp;
340
341  /* pre multiply stack by rotate(-twist,z) */
342  rot(-twist / 10.0,'z');
343
344  tmp = sqrtf((px-vx)*(px-vx) + (py-vy)*(py-vy) + (pz-vz)*(pz-vz));
345  m.mat = 1.0;
346  m.mat = sqrtf((px-vx)*(px-vx) + (pz-vz)*(pz-vz)) / tmp;
347  m.mat = (vy-py) / tmp;
348  m.mat = -m.mat;
349  m.mat = m.mat;
350  m.mat = 1.0;
351  multmatrix(m);
352
353  /* premultiply by rotate(theta,y) */
354  m.constant(0.0);
355  tmp = sqrtf((px-vx)*(px-vx) + (pz-vz)*(pz-vz));
356  m.mat = (vz-pz) / tmp;
357  m.mat = 1.0;
358  m.mat = m.mat;
359  m.mat = 1.0;
360  m.mat = -(px-vx) / tmp;
361  m.mat = -m.mat;
362  multmatrix(m);
363
364  /* premultiply by trans(-vx,-vy,-vz) */
365  translate(-vx,-vy,-vz);
366 }
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
void translate(double, double, double)
performs a translation
Definition: Matrix4.C:270
void rot(double, char)
performs a left-handed rotation around an axis (char == &#39;x&#39;, &#39;y&#39;, or &#39;z&#39;)
Definition: Matrix4.C:215
void multmatrix(const Matrix4 &)
premultiply the matrix by the given matrix, this-&gt;other * this
Definition: Matrix4.C:199
 void Matrix4::multmatrix ( const Matrix4 & m )

premultiply the matrix by the given matrix, this->other * this

Definition at line 199 of file Matrix4.C.

References mat.

Referenced by lookat(), rot(), scale(), and translate().

199  {
200  double tmp;
201  for (int j=0; j<4; j++) {
202  tmp = mat[j];
203  tmp = mat[4+j];
204  tmp = mat[8+j];
205  tmp = mat[12+j];
206  for (int i=0; i<4; i++) {
207  mat[4*i+j] = m.mat[4*i]*tmp + m.mat[4*i+1]*tmp +
208  m.mat[4*i+2]*tmp + m.mat[4*i+3]*tmp;
209  }
210  }
211 }
double mat
the matrix itself
Definition: Matrix4.h:33
 void Matrix4::multnorm3d ( const double onorm, double nnorm ) const

multiplies a 3D norm (first arg) by the Matrix, returns in second arg

Definition at line 68 of file Matrix4.C.

References mat.

68  {
69  double tmp;
70
71  tmp=onorm*mat + onorm*mat + onorm*mat;
72  tmp=onorm*mat + onorm*mat + onorm*mat;
73  tmp=onorm*mat + onorm*mat + onorm*mat;
74  tmp=onorm*mat + onorm*mat + onorm*mat;
75  double itmp = 1.0 / sqrtf(tmp*tmp + tmp*tmp + tmp*tmp);
76  nnorm=tmp*itmp;
77  nnorm=tmp*itmp;
78  nnorm=tmp*itmp;
79 }
double mat
the matrix itself
Definition: Matrix4.h:33
 void Matrix4::multpoint3d ( const double opoint, double npoint ) const

multiplies a 3D point (first arg) by the Matrix, returns in second arg

Definition at line 41 of file Matrix4.C.

References mat.

41  {
42 #if 0
43  // should try re-testing this formulation to see if it outperforms
44  // the old one, without introducing doubleing point imprecision
45  double tmp;
46  double itmp3 = 1.0 / (opoint*mat + opoint*mat +
47  opoint*mat + mat);
48  npoint=itmp3 * (opoint*mat + opoint*mat + opoint*mat[ 8] + mat);
49  npoint=itmp3 * (opoint*mat + opoint*mat + opoint*mat[ 9] + mat);
50  npoint=itmp3 * (opoint*mat + opoint*mat + opoint*mat + mat);
51 #else
52  double tmp;
53  double itmp3 = 1.0 / (opoint*mat + opoint*mat +
54  opoint*mat + mat);
55  tmp = itmp3*opoint;
56  tmp = itmp3*opoint;
57  tmp = itmp3*opoint;
58  npoint=tmp*mat + tmp*mat + tmp*mat[ 8] + itmp3*mat;
59  npoint=tmp*mat + tmp*mat + tmp*mat[ 9] + itmp3*mat;
60  npoint=tmp*mat + tmp*mat + tmp*mat + itmp3*mat;
61 #endif
62 }
double mat
the matrix itself
Definition: Matrix4.h:33
 void Matrix4::multpoint4d ( const double opoint, double npoint ) const

multiplies a 4D point (first arg) by the Matrix, returns in second arg

Definition at line 83 of file Matrix4.C.

References mat.

83  {
84  npoint=opoint*mat+opoint*mat+opoint*mat+opoint*mat;
85  npoint=opoint*mat+opoint*mat+opoint*mat+opoint*mat;
86  npoint=opoint*mat+opoint*mat+opoint*mat+opoint*mat;
87  npoint=opoint*mat+opoint*mat+opoint*mat+opoint*mat;
88 }
double mat
the matrix itself
Definition: Matrix4.h:33
 Matrix4& Matrix4::operator= ( const Matrix4 & m )
inline

Definition at line 61 of file Matrix4.h.

61 {loadmatrix(m); return *this;}
void loadmatrix(const Matrix4 &m)
replaces this matrix with the given one
Definition: Matrix4.C:194
 void Matrix4::ortho ( double left, double right, double bottom, double top, double nearval, double farval )

sets this matrix to a 3D orthographic matrix

Definition at line 303 of file Matrix4.C.

References constant(), and mat.

304  {
305
306  constant(0.0); // initialize this matrix to 0
307  mat = 2.0 / (right-left);
308  mat = 2.0 / (top-bottom);
309  mat = -2.0 / (farval-nearval);
310  mat = -(right+left) / (right-left);
311  mat = -(top+bottom) / (top-bottom);
312  mat = -(farval+nearval) / (farval-nearval);
313  mat = 1.0;
314 }
double mat
the matrix itself
Definition: Matrix4.h:33
void constant(double)
sets the matrix so all items are the given constant value
Definition: Matrix4.C:102
 void Matrix4::ortho2 ( double left, double right, double bottom, double top )

sets this matrix to a 2D orthographic matrix

Definition at line 318 of file Matrix4.C.

References constant(), and mat.

318  {
319
320  constant(0.0); // initialize this matrix to 0
321  mat = 2.0 / (right-left);
322  mat = 2.0 / (top-bottom);
323  mat = -1.0;
324  mat = -(right+left) / (right-left);
325  mat = -(top+bottom) / (top-bottom);
326  mat = 1.0;
327 }
double mat
the matrix itself
Definition: Matrix4.h:33
void constant(double)
sets the matrix so all items are the given constant value
Definition: Matrix4.C:102
 void Matrix4::rot ( double a, char axis )

performs a left-handed rotation around an axis (char == 'x', 'y', or 'z')

Definition at line 215 of file Matrix4.C.

References DEGTORAD, mat, and multmatrix().

Referenced by lookat(), rotate_axis(), transvec(), and transvecinv().

215  {
216  Matrix4 m; // create identity matrix
217  double angle;
218
219  angle = (double)DEGTORAD(a);
220
221  if (axis == 'x') {
222  m.mat=1.0;
223  m.mat=(double)cos(angle);
224  m.mat=m.mat;
225  m.mat = (double)sin(angle);
226  m.mat = -m.mat;
227  } else if (axis == 'y') {
228  m.mat = (double)cos(angle);
229  m.mat = 1.0;
230  m.mat = m.mat;
231  m.mat = (double) -sin(angle);
232  m.mat = -m.mat;
233  } else if (axis == 'z') {
234  m.mat = (double)cos(angle);
235  m.mat = m.mat;
236  m.mat = 1.0;
237  m.mat = (double)sin(angle);
238  m.mat = -m.mat;
239  }
240  // If there was an error, m is identity so we can multiply anyway.
241  multmatrix(m);
242 }
Definition: Matrix4.C:31
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
double mat
the matrix itself
Definition: Matrix4.h:33
void multmatrix(const Matrix4 &)
premultiply the matrix by the given matrix, this-&gt;other * this
Definition: Matrix4.C:199
 void Matrix4::rotate_axis ( const double axis, double angle )

apply a rotation around the given vector; angle in radians.

Definition at line 245 of file Matrix4.C.

References RADTODEG, rot(), transvec(), and transvecinv().

245  {
246  transvec(axis, axis, axis);
247  rot((double) (RADTODEG(angle)), 'x');
248  transvecinv(axis, axis, axis);
249 }
void transvecinv(double x, double y, double z)
apply a rotation such that the given vector is brought along &#39;x&#39;.
Definition: Matrix4.C:261
Definition: Matrix4.C:32
void transvec(double x, double y, double z)
apply a rotation such that &#39;x&#39; is brought along the given vector.
Definition: Matrix4.C:252
void rot(double, char)
performs a left-handed rotation around an axis (char == &#39;x&#39;, &#39;y&#39;, or &#39;z&#39;)
Definition: Matrix4.C:215
 void Matrix4::scale ( double x, double y, double z )

performs scaling

Definition at line 279 of file Matrix4.C.

References mat, multmatrix(), x, y, and z.

279  {
280  Matrix4 m; // create identity matrix
281  m.mat = x;
282  m.mat = y;
283  m.mat = z;
284  multmatrix(m);
285 }
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
gridSize z
double mat
the matrix itself
Definition: Matrix4.h:33
gridSize y
void multmatrix(const Matrix4 &)
premultiply the matrix by the given matrix, this-&gt;other * this
Definition: Matrix4.C:199
gridSize x
 void Matrix4::scale ( double f )
inline

Definition at line 84 of file Matrix4.h.

References scale().

Referenced by scale().

84 { scale(f, f, f); }
void scale(double, double, double)
performs scaling
Definition: Matrix4.C:279
 void Matrix4::translate ( double x, double y, double z )

performs a translation

Definition at line 270 of file Matrix4.C.

References mat, multmatrix(), x, y, and z.

Referenced by lookat().

270  {
271  Matrix4 m; // create identity matrix
272  m.mat = x;
273  m.mat = y;
274  m.mat = z;
275  multmatrix(m);
276 }
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
gridSize z
double mat
the matrix itself
Definition: Matrix4.h:33
gridSize y
void multmatrix(const Matrix4 &)
premultiply the matrix by the given matrix, this-&gt;other * this
Definition: Matrix4.C:199
gridSize x
 void Matrix4::translate ( double d )
inline

Definition at line 80 of file Matrix4.h.

References translate().

Referenced by translate().

80 { translate(d, d, d); }
void translate(double, double, double)
performs a translation
Definition: Matrix4.C:270
 void Matrix4::transpose ( void )

transposes the matrix

Definition at line 181 of file Matrix4.C.

References mat.

181  {
182  double tmp;
183  int i,j;
184  for(i=0;i<4;i++) {
185  for(j=0;j<4;j++) {
186  tmp[4*i+j] = mat[i+4*j];
187  }
188  }
189  for(i=0;i<16;i++)
190  mat[i] = tmp[i];
191 }
double mat
the matrix itself
Definition: Matrix4.h:33
 void Matrix4::transvec ( double x, double y, double z )

apply a rotation such that 'x' is brought along the given vector.

Definition at line 252 of file Matrix4.C.

References RADTODEG, and rot().

Referenced by obj_transvec(), and rotate_axis().

252  {
253  double theta = atan2(y,x);
254  double length = sqrt(y*y + x*x);
255  double phi = atan2((double) z, length);
256  rot((double) RADTODEG(theta), 'z');
257  rot((double) RADTODEG(-phi), 'y');
258 }
Definition: Matrix4.C:32
gridSize z
gridSize y
void rot(double, char)
performs a left-handed rotation around an axis (char == &#39;x&#39;, &#39;y&#39;, or &#39;z&#39;)
Definition: Matrix4.C:215
gridSize x
 void Matrix4::transvecinv ( double x, double y, double z )

apply a rotation such that the given vector is brought along 'x'.

Definition at line 261 of file Matrix4.C.

References RADTODEG, and rot().

Referenced by obj_transvecinv(), and rotate_axis().

261  {
262  double theta = atan2(y,x);
263  double length = sqrt(y*y + x*x);
264  double phi = atan2((double) z, length);
265  rot((double) RADTODEG(phi), 'y');
266  rot((double) RADTODEG(-theta), 'z');
267 }
Definition: Matrix4.C:32
gridSize z
gridSize y
void rot(double, char)
performs a left-handed rotation around an axis (char == &#39;x&#39;, &#39;y&#39;, or &#39;z&#39;)
Definition: Matrix4.C:215
gridSize x
 void Matrix4::window ( double left, double right, double bottom, double top, double nearval, double farval )

sets this matrix to represent a window perspective

Definition at line 288 of file Matrix4.C.

References constant(), and mat.

289  {
290
291  constant(0.0); // initialize this matrix to 0
292  mat = (2.0*nearval) / (right-left);
293  mat = (2.0*nearval) / (top-bottom);
294  mat = (right+left) / (right-left);
295  mat = (top+bottom) / (top-bottom);
296  mat = -(farval+nearval) / (farval-nearval);
297  mat = -1.0;
298  mat = -(2.0*farval*nearval) / (farval-nearval);
299 }
double mat
the matrix itself
Definition: Matrix4.h:33
void constant(double)
sets the matrix so all items are the given constant value
Definition: Matrix4.C:102

## Member Data Documentation

 double Matrix4::mat

the matrix itself

Definition at line 33 of file Matrix4.h.

The documentation for this class was generated from the following files: