istereo

annotate libs/vmath/matrix_c.c @ 43:73813c1176de

better hgignore
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 14 Aug 2015 04:33:42 +0300
parents c0ae8e668447
children
rev   line source
nuclear@39 1 /*
nuclear@39 2 libvmath - a vector math library
nuclear@39 3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@39 4
nuclear@39 5 This program is free software: you can redistribute it and/or modify
nuclear@39 6 it under the terms of the GNU Lesser General Public License as published
nuclear@39 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@39 8 (at your option) any later version.
nuclear@39 9
nuclear@39 10 This program is distributed in the hope that it will be useful,
nuclear@39 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@39 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@39 13 GNU Lesser General Public License for more details.
nuclear@39 14
nuclear@39 15 You should have received a copy of the GNU Lesser General Public License
nuclear@39 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@39 17 */
nuclear@39 18
nuclear@39 19
nuclear@28 20 #include <stdio.h>
nuclear@28 21 #include "matrix.h"
nuclear@28 22 #include "vector.h"
nuclear@28 23 #include "quat.h"
nuclear@28 24
nuclear@28 25 void m3_to_m4(mat4_t dest, mat3_t src)
nuclear@28 26 {
nuclear@28 27 int i, j;
nuclear@28 28
nuclear@28 29 memset(dest, 0, sizeof(mat4_t));
nuclear@28 30 for(i=0; i<3; i++) {
nuclear@28 31 for(j=0; j<3; j++) {
nuclear@28 32 dest[i][j] = src[i][j];
nuclear@28 33 }
nuclear@28 34 }
nuclear@28 35 dest[3][3] = 1.0;
nuclear@28 36 }
nuclear@28 37
nuclear@28 38 void m3_print(FILE *fp, mat3_t m)
nuclear@28 39 {
nuclear@28 40 int i;
nuclear@28 41 for(i=0; i<3; i++) {
nuclear@28 42 fprintf(fp, "[ %12.5f %12.5f %12.5f ]\n", (float)m[i][0], (float)m[i][1], (float)m[i][2]);
nuclear@28 43 }
nuclear@28 44 }
nuclear@28 45
nuclear@28 46 /* C matrix 4x4 functions */
nuclear@28 47 void m4_to_m3(mat3_t dest, mat4_t src)
nuclear@28 48 {
nuclear@28 49 int i, j;
nuclear@28 50 for(i=0; i<3; i++) {
nuclear@28 51 for(j=0; j<3; j++) {
nuclear@28 52 dest[i][j] = src[i][j];
nuclear@28 53 }
nuclear@28 54 }
nuclear@28 55 }
nuclear@28 56
nuclear@28 57 void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
nuclear@28 58 {
nuclear@28 59 mat4_t tm;
nuclear@28 60 m4_identity(tm);
nuclear@28 61 tm[0][3] = x;
nuclear@28 62 tm[1][3] = y;
nuclear@28 63 tm[2][3] = z;
nuclear@28 64 m4_mult(m, m, tm);
nuclear@28 65 }
nuclear@28 66
nuclear@28 67 void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
nuclear@28 68 {
nuclear@28 69 m4_rotate_x(m, x);
nuclear@28 70 m4_rotate_y(m, y);
nuclear@28 71 m4_rotate_z(m, z);
nuclear@28 72 }
nuclear@28 73
nuclear@28 74 void m4_rotate_x(mat4_t m, scalar_t angle)
nuclear@28 75 {
nuclear@28 76 mat4_t rm;
nuclear@28 77 m4_identity(rm);
nuclear@28 78 rm[1][1] = cos(angle); rm[1][2] = -sin(angle);
nuclear@28 79 rm[2][1] = sin(angle); rm[2][2] = cos(angle);
nuclear@28 80 m4_mult(m, m, rm);
nuclear@28 81 }
nuclear@28 82
nuclear@28 83 void m4_rotate_y(mat4_t m, scalar_t angle)
nuclear@28 84 {
nuclear@28 85 mat4_t rm;
nuclear@28 86 m4_identity(rm);
nuclear@28 87 rm[0][0] = cos(angle); rm[0][2] = sin(angle);
nuclear@28 88 rm[2][0] = -sin(angle); rm[2][2] = cos(angle);
nuclear@28 89 m4_mult(m, m, rm);
nuclear@28 90 }
nuclear@28 91
nuclear@28 92 void m4_rotate_z(mat4_t m, scalar_t angle)
nuclear@28 93 {
nuclear@28 94 mat4_t rm;
nuclear@28 95 m4_identity(rm);
nuclear@28 96 rm[0][0] = cos(angle); rm[0][1] = -sin(angle);
nuclear@28 97 rm[1][0] = sin(angle); rm[1][1] = cos(angle);
nuclear@28 98 m4_mult(m, m, rm);
nuclear@28 99 }
nuclear@28 100
nuclear@28 101 void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
nuclear@28 102 {
nuclear@28 103 mat4_t xform;
nuclear@28 104 scalar_t sina = sin(angle);
nuclear@28 105 scalar_t cosa = cos(angle);
nuclear@28 106 scalar_t one_minus_cosa = 1.0 - cosa;
nuclear@28 107 scalar_t nxsq = x * x;
nuclear@28 108 scalar_t nysq = y * y;
nuclear@28 109 scalar_t nzsq = z * z;
nuclear@28 110
nuclear@28 111 m4_identity(xform);
nuclear@28 112 xform[0][0] = nxsq + (1.0 - nxsq) * cosa;
nuclear@28 113 xform[0][1] = x * y * one_minus_cosa - z * sina;
nuclear@28 114 xform[0][2] = x * z * one_minus_cosa + y * sina;
nuclear@28 115 xform[1][0] = x * y * one_minus_cosa + z * sina;
nuclear@28 116 xform[1][1] = nysq + (1.0 - nysq) * cosa;
nuclear@28 117 xform[1][2] = y * z * one_minus_cosa - x * sina;
nuclear@28 118 xform[2][0] = x * z * one_minus_cosa - y * sina;
nuclear@28 119 xform[2][1] = y * z * one_minus_cosa + x * sina;
nuclear@28 120 xform[2][2] = nzsq + (1.0 - nzsq) * cosa;
nuclear@28 121
nuclear@28 122 m4_mult(m, m, xform);
nuclear@28 123 }
nuclear@28 124
nuclear@28 125 void m4_rotate_quat(mat4_t m, quat_t q)
nuclear@28 126 {
nuclear@28 127 mat4_t rm;
nuclear@28 128 quat_to_mat4(rm, q);
nuclear@28 129 m4_mult(m, m, rm);
nuclear@28 130 }
nuclear@28 131
nuclear@28 132 void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
nuclear@28 133 {
nuclear@28 134 mat4_t sm;
nuclear@28 135 m4_identity(sm);
nuclear@28 136 sm[0][0] = x;
nuclear@28 137 sm[1][1] = y;
nuclear@28 138 sm[2][2] = z;
nuclear@28 139 m4_mult(m, m, sm);
nuclear@28 140 }
nuclear@28 141
nuclear@28 142 void m4_transpose(mat4_t res, mat4_t m)
nuclear@28 143 {
nuclear@28 144 int i, j;
nuclear@28 145 mat4_t tmp;
nuclear@28 146 m4_copy(tmp, m);
nuclear@28 147
nuclear@28 148 for(i=0; i<4; i++) {
nuclear@28 149 for(j=0; j<4; j++) {
nuclear@28 150 res[i][j] = tmp[j][i];
nuclear@28 151 }
nuclear@28 152 }
nuclear@28 153 }
nuclear@28 154
nuclear@28 155 scalar_t m4_determinant(mat4_t m)
nuclear@28 156 {
nuclear@28 157 scalar_t det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@28 158 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
nuclear@28 159 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
nuclear@28 160
nuclear@28 161 scalar_t det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@28 162 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@28 163 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
nuclear@28 164
nuclear@28 165 scalar_t det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
nuclear@28 166 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@28 167 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@28 168
nuclear@28 169 scalar_t det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
nuclear@28 170 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
nuclear@28 171 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@28 172
nuclear@28 173 return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14;
nuclear@28 174 }
nuclear@28 175
nuclear@28 176 void m4_adjoint(mat4_t res, mat4_t m)
nuclear@28 177 {
nuclear@28 178 int i, j;
nuclear@28 179 mat4_t coef;
nuclear@28 180
nuclear@28 181 coef[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@28 182 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
nuclear@28 183 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
nuclear@28 184 coef[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@28 185 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@28 186 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
nuclear@28 187 coef[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
nuclear@28 188 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@28 189 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@28 190 coef[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
nuclear@28 191 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
nuclear@28 192 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@28 193
nuclear@28 194 coef[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@28 195 (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
nuclear@28 196 (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
nuclear@28 197 coef[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@28 198 (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@28 199 (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
nuclear@28 200 coef[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
nuclear@28 201 (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@28 202 (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@28 203 coef[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
nuclear@28 204 (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
nuclear@28 205 (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@28 206
nuclear@28 207 coef[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
nuclear@28 208 (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) +
nuclear@28 209 (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2]));
nuclear@28 210 coef[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
nuclear@28 211 (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
nuclear@28 212 (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2]));
nuclear@28 213 coef[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) -
nuclear@28 214 (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
nuclear@28 215 (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
nuclear@28 216 coef[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) -
nuclear@28 217 (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) +
nuclear@28 218 (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
nuclear@28 219
nuclear@28 220 coef[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
nuclear@28 221 (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) +
nuclear@28 222 (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]));
nuclear@28 223 coef[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
nuclear@28 224 (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
nuclear@28 225 (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]));
nuclear@28 226 coef[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) -
nuclear@28 227 (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
nuclear@28 228 (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
nuclear@28 229 coef[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) -
nuclear@28 230 (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) +
nuclear@28 231 (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
nuclear@28 232
nuclear@28 233 m4_transpose(res, coef);
nuclear@28 234
nuclear@28 235 for(i=0; i<4; i++) {
nuclear@28 236 for(j=0; j<4; j++) {
nuclear@28 237 res[i][j] = j % 2 ? -res[i][j] : res[i][j];
nuclear@28 238 if(i % 2) res[i][j] = -res[i][j];
nuclear@28 239 }
nuclear@28 240 }
nuclear@28 241 }
nuclear@28 242
nuclear@28 243 void m4_inverse(mat4_t res, mat4_t m)
nuclear@28 244 {
nuclear@28 245 int i, j;
nuclear@28 246 mat4_t adj;
nuclear@28 247 scalar_t det;
nuclear@28 248
nuclear@28 249 m4_adjoint(adj, m);
nuclear@28 250 det = m4_determinant(m);
nuclear@28 251
nuclear@28 252 for(i=0; i<4; i++) {
nuclear@28 253 for(j=0; j<4; j++) {
nuclear@28 254 res[i][j] = adj[i][j] / det;
nuclear@28 255 }
nuclear@28 256 }
nuclear@28 257 }
nuclear@28 258
nuclear@28 259 void m4_print(FILE *fp, mat4_t m)
nuclear@28 260 {
nuclear@28 261 int i;
nuclear@28 262 for(i=0; i<4; i++) {
nuclear@28 263 fprintf(fp, "[ %12.5f %12.5f %12.5f %12.5f ]\n", (float)m[i][0], (float)m[i][1], (float)m[i][2], (float)m[i][3]);
nuclear@28 264 }
nuclear@28 265 }