nuclear@0: /* nuclear@0: libvmath - a vector math library nuclear@0: Copyright (C) 2004-2011 John Tsiombikas nuclear@0: nuclear@0: This program is free software: you can redistribute it and/or modify nuclear@0: it under the terms of the GNU Lesser General Public License as published nuclear@0: by the Free Software Foundation, either version 3 of the License, or nuclear@0: (at your option) any later version. nuclear@0: nuclear@0: This program is distributed in the hope that it will be useful, nuclear@0: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@0: GNU Lesser General Public License for more details. nuclear@0: nuclear@0: You should have received a copy of the GNU Lesser General Public License nuclear@0: along with this program. If not, see . nuclear@0: */ nuclear@0: nuclear@0: nuclear@0: #include nuclear@0: #include "matrix.h" nuclear@0: #include "vector.h" nuclear@0: #include "quat.h" nuclear@0: nuclear@0: void m3_to_m4(mat4_t dest, mat3_t src) nuclear@0: { nuclear@0: int i, j; nuclear@0: nuclear@0: memset(dest, 0, sizeof(mat4_t)); nuclear@0: for(i=0; i<3; i++) { nuclear@0: for(j=0; j<3; j++) { nuclear@0: dest[i][j] = src[i][j]; nuclear@0: } nuclear@0: } nuclear@0: dest[3][3] = 1.0; nuclear@0: } nuclear@0: nuclear@0: void m3_print(FILE *fp, mat3_t m) nuclear@0: { nuclear@0: int i; nuclear@0: for(i=0; i<3; i++) { nuclear@0: fprintf(fp, "[ %12.5f %12.5f %12.5f ]\n", (float)m[i][0], (float)m[i][1], (float)m[i][2]); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* C matrix 4x4 functions */ nuclear@0: void m4_to_m3(mat3_t dest, mat4_t src) nuclear@0: { nuclear@0: int i, j; nuclear@0: for(i=0; i<3; i++) { nuclear@0: for(j=0; j<3; j++) { nuclear@0: dest[i][j] = src[i][j]; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void m4_set_translation(mat4_t m, scalar_t x, scalar_t y, scalar_t z) nuclear@0: { nuclear@0: m4_identity(m); nuclear@0: m[0][3] = x; nuclear@0: m[1][3] = y; nuclear@0: m[2][3] = z; nuclear@0: } nuclear@0: nuclear@0: void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z) nuclear@0: { nuclear@0: mat4_t tm; nuclear@0: m4_set_translation(tm, x, y, z); nuclear@0: m4_mult(m, m, tm); nuclear@0: } nuclear@0: nuclear@0: void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z) nuclear@0: { nuclear@0: m4_rotate_x(m, x); nuclear@0: m4_rotate_y(m, y); nuclear@0: m4_rotate_z(m, z); nuclear@0: } nuclear@0: nuclear@0: void m4_set_rotation_x(mat4_t m, scalar_t angle) nuclear@0: { nuclear@0: m4_identity(m); nuclear@0: m[1][1] = cos(angle); m[1][2] = -sin(angle); nuclear@0: m[2][1] = sin(angle); m[2][2] = cos(angle); nuclear@0: } nuclear@0: nuclear@0: void m4_rotate_x(mat4_t m, scalar_t angle) nuclear@0: { nuclear@0: mat4_t rm; nuclear@0: m4_set_rotation_x(rm, angle); nuclear@0: m4_mult(m, m, rm); nuclear@0: } nuclear@0: nuclear@0: void m4_set_rotation_y(mat4_t m, scalar_t angle) nuclear@0: { nuclear@0: m4_identity(m); nuclear@0: m[0][0] = cos(angle); m[0][2] = sin(angle); nuclear@0: m[2][0] = -sin(angle); m[2][2] = cos(angle); nuclear@0: } nuclear@0: nuclear@0: void m4_rotate_y(mat4_t m, scalar_t angle) nuclear@0: { nuclear@0: mat4_t rm; nuclear@0: m4_set_rotation_y(rm, angle); nuclear@0: m4_mult(m, m, rm); nuclear@0: } nuclear@0: nuclear@0: void m4_set_rotation_z(mat4_t m, scalar_t angle) nuclear@0: { nuclear@0: m4_identity(m); nuclear@0: m[0][0] = cos(angle); m[0][1] = -sin(angle); nuclear@0: m[1][0] = sin(angle); m[1][1] = cos(angle); nuclear@0: } nuclear@0: nuclear@0: void m4_rotate_z(mat4_t m, scalar_t angle) nuclear@0: { nuclear@0: mat4_t rm; nuclear@0: m4_set_rotation_z(rm, angle); nuclear@0: m4_mult(m, m, rm); nuclear@0: } nuclear@0: nuclear@0: void m4_set_rotation_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z) nuclear@0: { nuclear@0: scalar_t sina = sin(angle); nuclear@0: scalar_t cosa = cos(angle); nuclear@0: scalar_t one_minus_cosa = 1.0 - cosa; nuclear@0: scalar_t nxsq = x * x; nuclear@0: scalar_t nysq = y * y; nuclear@0: scalar_t nzsq = z * z; nuclear@0: nuclear@0: m[0][0] = nxsq + (1.0 - nxsq) * cosa; nuclear@0: m[0][1] = x * y * one_minus_cosa - z * sina; nuclear@0: m[0][2] = x * z * one_minus_cosa + y * sina; nuclear@0: m[1][0] = x * y * one_minus_cosa + z * sina; nuclear@0: m[1][1] = nysq + (1.0 - nysq) * cosa; nuclear@0: m[1][2] = y * z * one_minus_cosa - x * sina; nuclear@0: m[2][0] = x * z * one_minus_cosa - y * sina; nuclear@0: m[2][1] = y * z * one_minus_cosa + x * sina; nuclear@0: m[2][2] = nzsq + (1.0 - nzsq) * cosa; nuclear@0: nuclear@0: /* the rest are identity */ nuclear@0: m[3][0] = m[3][1] = m[3][2] = m[0][3] = m[1][3] = m[2][3] = 0.0; nuclear@0: m[3][3] = 1.0; nuclear@0: } nuclear@0: nuclear@0: void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z) nuclear@0: { nuclear@0: mat4_t xform; nuclear@0: m4_set_rotation_axis(xform, angle, x, y, z); nuclear@0: m4_mult(m, m, xform); nuclear@0: } nuclear@0: nuclear@0: void m4_rotate_quat(mat4_t m, quat_t q) nuclear@0: { nuclear@0: mat4_t rm; nuclear@0: quat_to_mat4(rm, q); nuclear@0: m4_mult(m, m, rm); nuclear@0: } nuclear@0: nuclear@0: void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z) nuclear@0: { nuclear@0: mat4_t sm; nuclear@0: m4_identity(sm); nuclear@0: sm[0][0] = x; nuclear@0: sm[1][1] = y; nuclear@0: sm[2][2] = z; nuclear@0: m4_mult(m, m, sm); nuclear@0: } nuclear@0: nuclear@0: void m4_transpose(mat4_t res, mat4_t m) nuclear@0: { nuclear@0: int i, j; nuclear@0: mat4_t tmp; nuclear@0: m4_copy(tmp, m); nuclear@0: nuclear@0: for(i=0; i<4; i++) { nuclear@0: for(j=0; j<4; j++) { nuclear@0: res[i][j] = tmp[j][i]; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: scalar_t m4_determinant(mat4_t m) nuclear@0: { nuclear@0: scalar_t det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - nuclear@0: (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) + nuclear@0: (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])); nuclear@0: nuclear@0: scalar_t det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - nuclear@0: (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + nuclear@0: (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])); nuclear@0: nuclear@0: scalar_t det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) - nuclear@0: (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + nuclear@0: (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); nuclear@0: nuclear@0: scalar_t det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) - nuclear@0: (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) + nuclear@0: (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); nuclear@0: nuclear@0: return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14; nuclear@0: } nuclear@0: nuclear@0: void m4_adjoint(mat4_t res, mat4_t m) nuclear@0: { nuclear@0: int i, j; nuclear@0: mat4_t coef; nuclear@0: nuclear@0: coef[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - nuclear@0: (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) + nuclear@0: (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])); nuclear@0: coef[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - nuclear@0: (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + nuclear@0: (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])); nuclear@0: coef[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) - nuclear@0: (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + nuclear@0: (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); nuclear@0: coef[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) - nuclear@0: (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) + nuclear@0: (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); nuclear@0: nuclear@0: coef[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - nuclear@0: (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) + nuclear@0: (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])); nuclear@0: coef[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - nuclear@0: (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + nuclear@0: (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])); nuclear@0: coef[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) - nuclear@0: (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + nuclear@0: (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); nuclear@0: coef[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) - nuclear@0: (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) + nuclear@0: (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); nuclear@0: nuclear@0: coef[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) - nuclear@0: (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) + nuclear@0: (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])); nuclear@0: coef[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) - nuclear@0: (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) + nuclear@0: (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])); nuclear@0: coef[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) - nuclear@0: (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) + nuclear@0: (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1])); nuclear@0: coef[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) - nuclear@0: (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) + nuclear@0: (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1])); nuclear@0: nuclear@0: coef[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) - nuclear@0: (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) + nuclear@0: (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])); nuclear@0: coef[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) - nuclear@0: (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) + nuclear@0: (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])); nuclear@0: coef[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) - nuclear@0: (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) + nuclear@0: (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1])); nuclear@0: coef[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) - nuclear@0: (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) + nuclear@0: (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1])); nuclear@0: nuclear@0: m4_transpose(res, coef); nuclear@0: nuclear@0: for(i=0; i<4; i++) { nuclear@0: for(j=0; j<4; j++) { nuclear@0: res[i][j] = j % 2 ? -res[i][j] : res[i][j]; nuclear@0: if(i % 2) res[i][j] = -res[i][j]; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void m4_inverse(mat4_t res, mat4_t m) nuclear@0: { nuclear@0: int i, j; nuclear@0: mat4_t adj; nuclear@0: scalar_t det; nuclear@0: nuclear@0: m4_adjoint(adj, m); nuclear@0: det = m4_determinant(m); nuclear@0: nuclear@0: for(i=0; i<4; i++) { nuclear@0: for(j=0; j<4; j++) { nuclear@0: res[i][j] = adj[i][j] / det; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void m4_print(FILE *fp, mat4_t m) nuclear@0: { nuclear@0: int i; nuclear@0: for(i=0; i<4; i++) { nuclear@0: 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: } nuclear@0: }