NAMD
Public Member Functions | Public Attributes | List of all members
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[3], double[3]) const
 multiplies a 3D point (first arg) by the Matrix, returns in second arg More...
 
void multnorm3d (const double[3], double[3]) const
 multiplies a 3D norm (first arg) by the Matrix, returns in second arg More...
 
void multpoint4d (const double[4], double[4]) 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[3], 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[3])
 
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 [16]
 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[16]
the matrix itself
Definition: Matrix4.h:33
Matrix4::Matrix4 ( const Matrix4 m)
inline

copy constructor

Definition at line 31 of file Matrix4.h.

References loadmatrix().

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[16]
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[0]=1.0;
95  mat[5]=1.0;
96  mat[10]=1.0;
97  mat[15]=1.0;
98 }
double mat[16]
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[4][4], ident[4][4];
113  int i, j, k, l, ll;
114  int icol=0, irow=0;
115  int indxc[4], indxr[4], ipiv[4];
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[16]
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[16]
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[0] = 1.0;
346  m.mat[5] = sqrtf((px-vx)*(px-vx) + (pz-vz)*(pz-vz)) / tmp;
347  m.mat[6] = (vy-py) / tmp;
348  m.mat[9] = -m.mat[6];
349  m.mat[10] = m.mat[5];
350  m.mat[15] = 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[0] = (vz-pz) / tmp;
357  m.mat[5] = 1.0;
358  m.mat[10] = m.mat[0];
359  m.mat[15] = 1.0;
360  m.mat[2] = -(px-vx) / tmp;
361  m.mat[8] = -m.mat[2];
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[4];
201  for (int j=0; j<4; j++) {
202  tmp[0] = mat[j];
203  tmp[1] = mat[4+j];
204  tmp[2] = mat[8+j];
205  tmp[3] = mat[12+j];
206  for (int i=0; i<4; i++) {
207  mat[4*i+j] = m.mat[4*i]*tmp[0] + m.mat[4*i+1]*tmp[1] +
208  m.mat[4*i+2]*tmp[2] + m.mat[4*i+3]*tmp[3];
209  }
210  }
211 }
double mat[16]
the matrix itself
Definition: Matrix4.h:33
void Matrix4::multnorm3d ( const double  onorm[3],
double  nnorm[3] 
) 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[4];
70 
71  tmp[0]=onorm[0]*mat[0] + onorm[1]*mat[4] + onorm[2]*mat[8];
72  tmp[1]=onorm[0]*mat[1] + onorm[1]*mat[5] + onorm[2]*mat[9];
73  tmp[2]=onorm[0]*mat[2] + onorm[1]*mat[6] + onorm[2]*mat[10];
74  tmp[3]=onorm[0]*mat[3] + onorm[1]*mat[7] + onorm[2]*mat[11];
75  double itmp = 1.0 / sqrtf(tmp[0]*tmp[0] + tmp[1]*tmp[1] + tmp[2]*tmp[2]);
76  nnorm[0]=tmp[0]*itmp;
77  nnorm[1]=tmp[1]*itmp;
78  nnorm[2]=tmp[2]*itmp;
79 }
double mat[16]
the matrix itself
Definition: Matrix4.h:33
void Matrix4::multpoint3d ( const double  opoint[3],
double  npoint[3] 
) 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[3];
46  double itmp3 = 1.0 / (opoint[0]*mat[3] + opoint[1]*mat[7] +
47  opoint[2]*mat[11] + mat[15]);
48  npoint[0]=itmp3 * (opoint[0]*mat[0] + opoint[1]*mat[4] + opoint[2]*mat[ 8] + mat[12]);
49  npoint[1]=itmp3 * (opoint[0]*mat[1] + opoint[1]*mat[5] + opoint[2]*mat[ 9] + mat[13]);
50  npoint[2]=itmp3 * (opoint[0]*mat[2] + opoint[1]*mat[6] + opoint[2]*mat[10] + mat[14]);
51 #else
52  double tmp[3];
53  double itmp3 = 1.0 / (opoint[0]*mat[3] + opoint[1]*mat[7] +
54  opoint[2]*mat[11] + mat[15]);
55  tmp[0] = itmp3*opoint[0];
56  tmp[1] = itmp3*opoint[1];
57  tmp[2] = itmp3*opoint[2];
58  npoint[0]=tmp[0]*mat[0] + tmp[1]*mat[4] + tmp[2]*mat[ 8] + itmp3*mat[12];
59  npoint[1]=tmp[0]*mat[1] + tmp[1]*mat[5] + tmp[2]*mat[ 9] + itmp3*mat[13];
60  npoint[2]=tmp[0]*mat[2] + tmp[1]*mat[6] + tmp[2]*mat[10] + itmp3*mat[14];
61 #endif
62 }
double mat[16]
the matrix itself
Definition: Matrix4.h:33
void Matrix4::multpoint4d ( const double  opoint[4],
double  npoint[4] 
) 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[0]=opoint[0]*mat[0]+opoint[1]*mat[4]+opoint[2]*mat[8]+opoint[3]*mat[12];
85  npoint[1]=opoint[0]*mat[1]+opoint[1]*mat[5]+opoint[2]*mat[9]+opoint[3]*mat[13];
86  npoint[2]=opoint[0]*mat[2]+opoint[1]*mat[6]+opoint[2]*mat[10]+opoint[3]*mat[14];
87  npoint[3]=opoint[0]*mat[3]+opoint[1]*mat[7]+opoint[2]*mat[11]+opoint[3]*mat[15];
88 }
double mat[16]
the matrix itself
Definition: Matrix4.h:33
Matrix4& Matrix4::operator= ( const Matrix4 m)
inline

Definition at line 61 of file Matrix4.h.

References loadmatrix().

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[0] = 2.0 / (right-left);
308  mat[5] = 2.0 / (top-bottom);
309  mat[10] = -2.0 / (farval-nearval);
310  mat[12] = -(right+left) / (right-left);
311  mat[13] = -(top+bottom) / (top-bottom);
312  mat[14] = -(farval+nearval) / (farval-nearval);
313  mat[15] = 1.0;
314 }
double mat[16]
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[0] = 2.0 / (right-left);
322  mat[5] = 2.0 / (top-bottom);
323  mat[10] = -1.0;
324  mat[12] = -(right+left) / (right-left);
325  mat[13] = -(top+bottom) / (top-bottom);
326  mat[15] = 1.0;
327 }
double mat[16]
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[0]=1.0;
223  m.mat[5]=(double)cos(angle);
224  m.mat[10]=m.mat[5];
225  m.mat[6] = (double)sin(angle);
226  m.mat[9] = -m.mat[6];
227  } else if (axis == 'y') {
228  m.mat[0] = (double)cos(angle);
229  m.mat[5] = 1.0;
230  m.mat[10] = m.mat[0];
231  m.mat[2] = (double) -sin(angle);
232  m.mat[8] = -m.mat[2];
233  } else if (axis == 'z') {
234  m.mat[0] = (double)cos(angle);
235  m.mat[5] = m.mat[0];
236  m.mat[10] = 1.0;
237  m.mat[1] = (double)sin(angle);
238  m.mat[4] = -m.mat[1];
239  }
240  // If there was an error, m is identity so we can multiply anyway.
241  multmatrix(m);
242 }
#define DEGTORAD(a)
Definition: Matrix4.C:31
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
double mat[16]
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[3],
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().

Referenced by obj_transabout().

245  {
246  transvec(axis[0], axis[1], axis[2]);
247  rot((double) (RADTODEG(angle)), 'x');
248  transvecinv(axis[0], axis[1], axis[2]);
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
#define RADTODEG(a)
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[0] = x;
282  m.mat[5] = y;
283  m.mat[10] = z;
284  multmatrix(m);
285 }
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
gridSize z
double mat[16]
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[12] = x;
273  m.mat[13] = y;
274  m.mat[14] = z;
275  multmatrix(m);
276 }
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
gridSize z
double mat[16]
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[3])
inline

Definition at line 80 of file Matrix4.h.

References translate().

Referenced by translate().

80 { translate(d[0], d[1], d[2]); }
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[16];
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[16]
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 }
#define RADTODEG(a)
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 }
#define RADTODEG(a)
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[0] = (2.0*nearval) / (right-left);
293  mat[5] = (2.0*nearval) / (top-bottom);
294  mat[8] = (right+left) / (right-left);
295  mat[9] = (top+bottom) / (top-bottom);
296  mat[10] = -(farval+nearval) / (farval-nearval);
297  mat[11] = -1.0;
298  mat[14] = -(2.0*farval*nearval) / (farval-nearval);
299 }
double mat[16]
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[16]

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