vrshoot

annotate libs/vmath/matrix_c.c @ 3:c179c72369be

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