dbf-halloween2015

diff libs/vmath/matrix.inl @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vmath/matrix.inl	Sun Nov 01 00:36:56 2015 +0200
     1.3 @@ -0,0 +1,200 @@
     1.4 +/*
     1.5 +libvmath - a vector math library
     1.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU Lesser General Public License as published
    1.10 +by the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU Lesser General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU Lesser General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +
    1.22 +#include <string.h>
    1.23 +
    1.24 +#ifdef __cplusplus
    1.25 +extern "C" {
    1.26 +#endif	/* __cplusplus */
    1.27 +
    1.28 +/* C matrix 3x3 functions */
    1.29 +static inline void m3_identity(mat3_t m)
    1.30 +{
    1.31 +	static const mat3_t id = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
    1.32 +	memcpy(m, id, sizeof id);
    1.33 +}
    1.34 +
    1.35 +static inline void m3_cons(mat3_t m,
    1.36 +		scalar_t m11, scalar_t m12, scalar_t m13,
    1.37 +		scalar_t m21, scalar_t m22, scalar_t m23,
    1.38 +		scalar_t m31, scalar_t m32, scalar_t m33)
    1.39 +{
    1.40 +	m[0][0] = m11; m[0][1] = m12; m[0][2] = m13;
    1.41 +	m[1][0] = m21; m[1][1] = m22; m[1][2] = m23;
    1.42 +	m[2][0] = m31; m[2][1] = m32; m[2][2] = m33;
    1.43 +}
    1.44 +
    1.45 +static inline void m3_copy(mat3_t dest, mat3_t src)
    1.46 +{
    1.47 +	memcpy(dest, src, sizeof(mat3_t));
    1.48 +}
    1.49 +
    1.50 +
    1.51 +/* C matrix 4x4 functions */
    1.52 +static inline void m4_identity(mat4_t m)
    1.53 +{
    1.54 +	static const mat4_t id = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
    1.55 +	memcpy(m, id, sizeof id);
    1.56 +}
    1.57 +
    1.58 +static inline void m4_cons(mat4_t m,
    1.59 +		scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
    1.60 +		scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
    1.61 +		scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
    1.62 +		scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44)
    1.63 +{
    1.64 +	m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
    1.65 +	m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
    1.66 +	m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
    1.67 +	m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;
    1.68 +}
    1.69 +
    1.70 +static inline void m4_copy(mat4_t dest, mat4_t src)
    1.71 +{
    1.72 +	memcpy(dest, src, sizeof(mat4_t));
    1.73 +}
    1.74 +
    1.75 +static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2)
    1.76 +{
    1.77 +	mat4_t tmp;
    1.78 +
    1.79 +	/*
    1.80 +	int i, j;
    1.81 +	for(i=0; i<4; i++) {
    1.82 +		for(j=0; j<4; j++) {
    1.83 +			tmp[i][j] = m1[i][0] * m2[0][j] + m1[i][1] * m2[1][j] + m1[i][2] * m2[2][j] + m1[i][3] * m2[3][j];
    1.84 +		}
    1.85 +	}
    1.86 +	*/
    1.87 +
    1.88 +	tmp[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] + m1[0][3] * m2[3][0];
    1.89 +	tmp[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1] + m1[0][2] * m2[2][1] + m1[0][3] * m2[3][1];
    1.90 +	tmp[0][2] = m1[0][0] * m2[0][2] + m1[0][1] * m2[1][2] + m1[0][2] * m2[2][2] + m1[0][3] * m2[3][2];
    1.91 +	tmp[0][3] = m1[0][0] * m2[0][3] + m1[0][1] * m2[1][3] + m1[0][2] * m2[2][3] + m1[0][3] * m2[3][3];
    1.92 +
    1.93 +	tmp[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0] + m1[1][2] * m2[2][0] + m1[1][3] * m2[3][0];
    1.94 +	tmp[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1] + m1[1][2] * m2[2][1] + m1[1][3] * m2[3][1];
    1.95 +	tmp[1][2] = m1[1][0] * m2[0][2] + m1[1][1] * m2[1][2] + m1[1][2] * m2[2][2] + m1[1][3] * m2[3][2];
    1.96 +	tmp[1][3] = m1[1][0] * m2[0][3] + m1[1][1] * m2[1][3] + m1[1][2] * m2[2][3] + m1[1][3] * m2[3][3];
    1.97 +
    1.98 +	tmp[2][0] = m1[2][0] * m2[0][0] + m1[2][1] * m2[1][0] + m1[2][2] * m2[2][0] + m1[2][3] * m2[3][0];
    1.99 +	tmp[2][1] = m1[2][0] * m2[0][1] + m1[2][1] * m2[1][1] + m1[2][2] * m2[2][1] + m1[2][3] * m2[3][1];
   1.100 +	tmp[2][2] = m1[2][0] * m2[0][2] + m1[2][1] * m2[1][2] + m1[2][2] * m2[2][2] + m1[2][3] * m2[3][2];
   1.101 +	tmp[2][3] = m1[2][0] * m2[0][3] + m1[2][1] * m2[1][3] + m1[2][2] * m2[2][3] + m1[2][3] * m2[3][3];
   1.102 +
   1.103 +	tmp[3][0] = m1[3][0] * m2[0][0] + m1[3][1] * m2[1][0] + m1[3][2] * m2[2][0] + m1[3][3] * m2[3][0];
   1.104 +	tmp[3][1] = m1[3][0] * m2[0][1] + m1[3][1] * m2[1][1] + m1[3][2] * m2[2][1] + m1[3][3] * m2[3][1];
   1.105 +	tmp[3][2] = m1[3][0] * m2[0][2] + m1[3][1] * m2[1][2] + m1[3][2] * m2[2][2] + m1[3][3] * m2[3][2];
   1.106 +	tmp[3][3] = m1[3][0] * m2[0][3] + m1[3][1] * m2[1][3] + m1[3][2] * m2[2][3] + m1[3][3] * m2[3][3];
   1.107 +
   1.108 +	m4_copy(res, tmp);
   1.109 +}
   1.110 +
   1.111 +static inline void m4_set_column(mat4_t m, vec4_t v, int idx)
   1.112 +{
   1.113 +	m[0][idx] = v.x;
   1.114 +	m[1][idx] = v.y;
   1.115 +	m[2][idx] = v.z;
   1.116 +	m[3][idx] = v.w;
   1.117 +}
   1.118 +
   1.119 +static inline void m4_set_row(mat4_t m, vec4_t v, int idx)
   1.120 +{
   1.121 +	m[idx][0] = v.x;
   1.122 +	m[idx][1] = v.y;
   1.123 +	m[idx][2] = v.z;
   1.124 +	m[idx][3] = v.w;
   1.125 +}
   1.126 +
   1.127 +#ifdef __cplusplus
   1.128 +}	/* extern "C" */
   1.129 +
   1.130 +
   1.131 +/* unrolled to hell and inline */
   1.132 +inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2)
   1.133 +{
   1.134 +	Matrix4x4 res;
   1.135 +
   1.136 +	/*
   1.137 +	for(i=0; i<4; i++) {
   1.138 +		for(j=0; j<4; j++) {
   1.139 +			res.m[i][j] = m1.m[i][0] * m2.m[0][j] + m1.m[i][1] * m2.m[1][j] + m1.m[i][2] * m2.m[2][j] + m1.m[i][3] * m2.m[3][j];
   1.140 +		}
   1.141 +	}
   1.142 +	*/
   1.143 +
   1.144 +	res.m[0][0] = m1.m[0][0] * m2.m[0][0] + m1.m[0][1] * m2.m[1][0] + m1.m[0][2] * m2.m[2][0] + m1.m[0][3] * m2.m[3][0];
   1.145 +	res.m[0][1] = m1.m[0][0] * m2.m[0][1] + m1.m[0][1] * m2.m[1][1] + m1.m[0][2] * m2.m[2][1] + m1.m[0][3] * m2.m[3][1];
   1.146 +	res.m[0][2] = m1.m[0][0] * m2.m[0][2] + m1.m[0][1] * m2.m[1][2] + m1.m[0][2] * m2.m[2][2] + m1.m[0][3] * m2.m[3][2];
   1.147 +	res.m[0][3] = m1.m[0][0] * m2.m[0][3] + m1.m[0][1] * m2.m[1][3] + m1.m[0][2] * m2.m[2][3] + m1.m[0][3] * m2.m[3][3];
   1.148 +
   1.149 +	res.m[1][0] = m1.m[1][0] * m2.m[0][0] + m1.m[1][1] * m2.m[1][0] + m1.m[1][2] * m2.m[2][0] + m1.m[1][3] * m2.m[3][0];
   1.150 +	res.m[1][1] = m1.m[1][0] * m2.m[0][1] + m1.m[1][1] * m2.m[1][1] + m1.m[1][2] * m2.m[2][1] + m1.m[1][3] * m2.m[3][1];
   1.151 +	res.m[1][2] = m1.m[1][0] * m2.m[0][2] + m1.m[1][1] * m2.m[1][2] + m1.m[1][2] * m2.m[2][2] + m1.m[1][3] * m2.m[3][2];
   1.152 +	res.m[1][3] = m1.m[1][0] * m2.m[0][3] + m1.m[1][1] * m2.m[1][3] + m1.m[1][2] * m2.m[2][3] + m1.m[1][3] * m2.m[3][3];
   1.153 +
   1.154 +	res.m[2][0] = m1.m[2][0] * m2.m[0][0] + m1.m[2][1] * m2.m[1][0] + m1.m[2][2] * m2.m[2][0] + m1.m[2][3] * m2.m[3][0];
   1.155 +	res.m[2][1] = m1.m[2][0] * m2.m[0][1] + m1.m[2][1] * m2.m[1][1] + m1.m[2][2] * m2.m[2][1] + m1.m[2][3] * m2.m[3][1];
   1.156 +	res.m[2][2] = m1.m[2][0] * m2.m[0][2] + m1.m[2][1] * m2.m[1][2] + m1.m[2][2] * m2.m[2][2] + m1.m[2][3] * m2.m[3][2];
   1.157 +	res.m[2][3] = m1.m[2][0] * m2.m[0][3] + m1.m[2][1] * m2.m[1][3] + m1.m[2][2] * m2.m[2][3] + m1.m[2][3] * m2.m[3][3];
   1.158 +
   1.159 +	res.m[3][0] = m1.m[3][0] * m2.m[0][0] + m1.m[3][1] * m2.m[1][0] + m1.m[3][2] * m2.m[2][0] + m1.m[3][3] * m2.m[3][0];
   1.160 +	res.m[3][1] = m1.m[3][0] * m2.m[0][1] + m1.m[3][1] * m2.m[1][1] + m1.m[3][2] * m2.m[2][1] + m1.m[3][3] * m2.m[3][1];
   1.161 +	res.m[3][2] = m1.m[3][0] * m2.m[0][2] + m1.m[3][1] * m2.m[1][2] + m1.m[3][2] * m2.m[2][2] + m1.m[3][3] * m2.m[3][2];
   1.162 +	res.m[3][3] = m1.m[3][0] * m2.m[0][3] + m1.m[3][1] * m2.m[1][3] + m1.m[3][2] * m2.m[2][3] + m1.m[3][3] * m2.m[3][3];
   1.163 +
   1.164 +	return res;
   1.165 +}
   1.166 +
   1.167 +inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2)
   1.168 +{
   1.169 +	Matrix4x4 res = m1 * m2;
   1.170 +	m1 = res;
   1.171 +}
   1.172 +
   1.173 +
   1.174 +inline scalar_t *Matrix3x3::operator [](int index)
   1.175 +{
   1.176 +	return m[index];
   1.177 +}
   1.178 +
   1.179 +inline const scalar_t *Matrix3x3::operator [](int index) const
   1.180 +{
   1.181 +	return m[index];
   1.182 +}
   1.183 +
   1.184 +inline void Matrix3x3::reset_identity()
   1.185 +{
   1.186 +	*this = identity;
   1.187 +}
   1.188 +
   1.189 +inline scalar_t *Matrix4x4::operator [](int index)
   1.190 +{
   1.191 +	return m[index];
   1.192 +}
   1.193 +
   1.194 +inline const scalar_t *Matrix4x4::operator [](int index) const
   1.195 +{
   1.196 +	return m[index];
   1.197 +}
   1.198 +
   1.199 +inline void Matrix4x4::reset_identity()
   1.200 +{
   1.201 +	*this = identity;
   1.202 +}
   1.203 +#endif	/* __cplusplus */