dbf-halloween2015

annotate libs/vmath/matrix_c.c @ 4:4316c0c879e9

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