3 * @brief The implementation of the gljewel matrix module.
5 * Copyright 2001, 2008 David Ashley <dashxdr@gmail.com>
6 * Copyright 2008 Stephen M. Webb <stephen.webb@bregmasoft.ca>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of Version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 matrix identityquick
={
31 {1.0f
, 0.0f
, 0.0f
, 0.0f
},
32 {0.0f
, 1.0f
, 0.0f
, 0.0f
},
33 {0.0f
, 0.0f
, 1.0f
, 0.0f
},
34 {0.0f
, 0.0f
, 0.0f
, 1.0f
}
38 {0.0f
, 0.0f
, 0.0f
, 0.0f
},
39 {0.0f
, 0.0f
, 0.0f
, 0.0f
},
40 {0.0f
, 0.0f
, 0.0f
, 0.0f
},
41 {0.0f
, 0.0f
, 0.0f
, 0.0f
}
44 void identitymatrix(matrix
*dest
)
46 memcpy(dest
,&identityquick
,sizeof(matrix
));
48 void zeromatrix(matrix
*dest
)
50 memcpy(dest
,&zeroquick
,sizeof(matrix
));
53 void matrixXmatrix(matrix
*dest
,matrix
*left
,matrix
*right
)
64 t
+=(*left
)[k
][j
]*(*right
)[i
][k
];
67 memcpy(dest
,&temp
,sizeof(matrix
));
69 void matrixXvector(vector
*dest
,matrix
*left
,vector
*right
)
78 t
+=(*left
)[j
][i
]*(*right
)[j
];
81 memcpy(dest
,&temp
,sizeof(vector
));
83 void rotationmatrix(matrix
*dest
,float angle
,float x
,float y
,float z
)
86 float c1
,xx
,yy
,zz
,xyc1
,xzc1
,yzc1
,xs
,ys
,zs
;
88 mag
=sqrtf(x
*x
+y
*y
+z
*z
);
111 (*dest
)[0][0]=xx
*c1
+c
;
112 (*dest
)[1][0]=xyc1
-zs
;
113 (*dest
)[2][0]=xzc1
+ys
;
115 (*dest
)[0][1]=xyc1
+zs
;
116 (*dest
)[1][1]=yy
*c1
+c
;
117 (*dest
)[2][1]=yzc1
-xs
;
119 (*dest
)[0][2]=xzc1
-ys
;
120 (*dest
)[1][2]=yzc1
+xs
;
121 (*dest
)[2][2]=zz
*c1
+c
;
129 void xrotmatrix(matrix
*dest
,float angle
)
133 identitymatrix(&temp
);
141 matrixXmatrix(dest
,&temp
,dest
);
143 void yrotmatrix(matrix
*dest
,float angle
)
147 identitymatrix(&temp
);
155 matrixXmatrix(dest
,&temp
,dest
);
157 void zrotmatrix(matrix
*dest
,float angle
)
161 identitymatrix(&temp
);
169 matrixXmatrix(dest
,&temp
,dest
);
174 return (x
<0.0) ? -x
: x
;
177 void swaprows(matrix
*dest
,int r1
,int r2
)
184 (*dest
)[i
][r1
]=(*dest
)[i
][r2
];
188 void scalerow(matrix
*dest
,int r1
,float scale
)
192 (*dest
)[i
][r1
]*=scale
;
194 void addscalerow(matrix
*dest
,int r1
,int r2
,float scale
)
198 (*dest
)[i
][r1
]+=(*dest
)[i
][r2
]*scale
;
201 void printmatrix(matrix
*m
)
208 printf(" %7.3f",(*m
)[i
][j
]);
212 void invertmatrix(matrix
*dest
,matrix
*source
)
220 identitymatrix(dest
);
221 memcpy(temp
,source
,sizeof(temp
));
236 swaprows(dest
,k
,maxnum
);
237 swaprows(&temp
,k
,maxnum
);
247 addscalerow(dest
,i
,k
,t
);
248 addscalerow(&temp
,i
,k
,t
);
259 addscalerow(dest
,i
,k
,t
);
260 addscalerow(&temp
,i
,k
,t
);