goat3d

diff libs/vmath/matrix.h @ 27:4deb0b12fe14

wtf... corrupted heap?
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 08:20:19 +0300
parents
children 9ba3e2fb8a33
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vmath/matrix.h	Sun Sep 29 08:20:19 2013 +0300
     1.3 @@ -0,0 +1,260 @@
     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 +#ifndef VMATH_MATRIX_H_
    1.23 +#define VMATH_MATRIX_H_
    1.24 +
    1.25 +#include <stdio.h>
    1.26 +#include "vmath_types.h"
    1.27 +
    1.28 +#ifdef __cplusplus
    1.29 +extern "C" {
    1.30 +#endif	/* __cplusplus */
    1.31 +
    1.32 +/* C matrix 3x3 functions */
    1.33 +static inline void m3_identity(mat3_t m);
    1.34 +static inline void m3_cons(mat3_t m,
    1.35 +		scalar_t m11, scalar_t m12, scalar_t m13,
    1.36 +		scalar_t m21, scalar_t m22, scalar_t m23,
    1.37 +		scalar_t m31, scalar_t m32, scalar_t m33);
    1.38 +static inline void m3_copy(mat3_t dest, mat3_t src);
    1.39 +void m3_to_m4(mat4_t dest, mat3_t src);
    1.40 +
    1.41 +void m3_print(FILE *fp, mat3_t m);
    1.42 +
    1.43 +/* C matrix 4x4 functions */
    1.44 +static inline void m4_identity(mat4_t m);
    1.45 +static inline void m4_cons(mat4_t m,
    1.46 +		scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
    1.47 +		scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
    1.48 +		scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
    1.49 +		scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
    1.50 +static inline void m4_copy(mat4_t dest, mat4_t src);
    1.51 +void m4_to_m3(mat3_t dest, mat4_t src);
    1.52 +
    1.53 +static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2);
    1.54 +
    1.55 +void m4_set_translation(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
    1.56 +void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
    1.57 +
    1.58 +void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
    1.59 +
    1.60 +void m4_set_rotation_x(mat4_t m, scalar_t angle);
    1.61 +void m4_rotate_x(mat4_t m, scalar_t angle);
    1.62 +void m4_set_rotation_y(mat4_t m, scalar_t angle);
    1.63 +void m4_rotate_y(mat4_t m, scalar_t angle);
    1.64 +void m4_set_rotation_z(mat4_t m, scalar_t angle);
    1.65 +void m4_rotate_z(mat4_t m, scalar_t angle);
    1.66 +/* axis-angle rotation */
    1.67 +void m4_set_rotation_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
    1.68 +void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
    1.69 +/* concatentate a rotation quaternion */
    1.70 +void m4_rotate_quat(mat4_t m, quat_t q);
    1.71 +
    1.72 +void m4_set_scaling(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
    1.73 +void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
    1.74 +
    1.75 +static inline void m4_set_column(mat4_t m, vec4_t v, int idx);
    1.76 +static inline void m4_set_row(mat4_t m, vec4_t v, int idx);
    1.77 +
    1.78 +void m4_transpose(mat4_t res, mat4_t m);
    1.79 +scalar_t m4_determinant(mat4_t m);
    1.80 +void m4_adjoint(mat4_t res, mat4_t m);
    1.81 +void m4_inverse(mat4_t res, mat4_t m);
    1.82 +
    1.83 +void m4_print(FILE *fp, mat4_t m);
    1.84 +
    1.85 +#ifdef __cplusplus
    1.86 +}
    1.87 +
    1.88 +/* when included from C++ source files, also define the matrix classes */
    1.89 +#include <iostream>
    1.90 +
    1.91 +/** 3x3 matrix */
    1.92 +class Matrix3x3 {
    1.93 +public:
    1.94 +	scalar_t m[3][3];
    1.95 +
    1.96 +	static Matrix3x3 identity;
    1.97 +
    1.98 +	Matrix3x3();
    1.99 +	Matrix3x3(	scalar_t m11, scalar_t m12, scalar_t m13,
   1.100 +				scalar_t m21, scalar_t m22, scalar_t m23,
   1.101 +				scalar_t m31, scalar_t m32, scalar_t m33);
   1.102 +	Matrix3x3(const Vector3 &ivec, const Vector3 &jvec, const Vector3 &kvec);
   1.103 +	Matrix3x3(const mat3_t cmat);
   1.104 +
   1.105 +	Matrix3x3(const Matrix4x4 &mat4x4);
   1.106 +
   1.107 +	/* binary operations matrix (op) matrix */
   1.108 +	friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
   1.109 +	friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
   1.110 +	friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
   1.111 +
   1.112 +	friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
   1.113 +	friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
   1.114 +	friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
   1.115 +
   1.116 +	/* binary operations matrix (op) scalar and scalar (op) matrix */
   1.117 +	friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
   1.118 +	friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
   1.119 +
   1.120 +	friend void operator *=(Matrix3x3 &mat, scalar_t scalar);
   1.121 +
   1.122 +	inline scalar_t *operator [](int index);
   1.123 +	inline const scalar_t *operator [](int index) const;
   1.124 +
   1.125 +	inline void reset_identity();
   1.126 +
   1.127 +	void translate(const Vector2 &trans);
   1.128 +	void set_translation(const Vector2 &trans);
   1.129 +
   1.130 +	void rotate(scalar_t angle);						/* 2d rotation */
   1.131 +	void rotate(const Vector3 &euler_angles);			/* 3d rotation with euler angles */
   1.132 +	void rotate(const Vector3 &axis, scalar_t angle);	/* 3d axis/angle rotation */
   1.133 +	void set_rotation(scalar_t angle);
   1.134 +	void set_rotation(const Vector3 &euler_angles);
   1.135 +	void set_rotation(const Vector3 &axis, scalar_t angle);
   1.136 +	Quaternion get_rotation_quat() const;
   1.137 +
   1.138 +	void scale(const Vector3 &scale_vec);
   1.139 +	void set_scaling(const Vector3 &scale_vec);
   1.140 +
   1.141 +	void set_column_vector(const Vector3 &vec, unsigned int col_index);
   1.142 +	void set_row_vector(const Vector3 &vec, unsigned int row_index);
   1.143 +	Vector3 get_column_vector(unsigned int col_index) const;
   1.144 +	Vector3 get_row_vector(unsigned int row_index) const;
   1.145 +
   1.146 +	void transpose();
   1.147 +	Matrix3x3 transposed() const;
   1.148 +	scalar_t determinant() const;
   1.149 +	Matrix3x3 inverse() const;
   1.150 +
   1.151 +	friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat);
   1.152 +};
   1.153 +
   1.154 +/* binary operations matrix (op) matrix */
   1.155 +Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
   1.156 +Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
   1.157 +Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
   1.158 +
   1.159 +void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
   1.160 +void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
   1.161 +void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
   1.162 +
   1.163 +/* binary operations matrix (op) scalar and scalar (op) matrix */
   1.164 +Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
   1.165 +Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
   1.166 +
   1.167 +void operator *=(Matrix3x3 &mat, scalar_t scalar);
   1.168 +
   1.169 +std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat);
   1.170 +
   1.171 +
   1.172 +
   1.173 +/** 4x4 matrix */
   1.174 +class Matrix4x4 {
   1.175 +public:
   1.176 +	scalar_t m[4][4];
   1.177 +
   1.178 +	static Matrix4x4 identity;
   1.179 +
   1.180 +	Matrix4x4();
   1.181 +	Matrix4x4(	scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
   1.182 +				scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
   1.183 +				scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
   1.184 +				scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
   1.185 +	Matrix4x4(const mat4_t cmat);
   1.186 +
   1.187 +	Matrix4x4(const Matrix3x3 &mat3x3);
   1.188 +
   1.189 +	/* binary operations matrix (op) matrix */
   1.190 +	friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
   1.191 +	friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
   1.192 +	friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
   1.193 +
   1.194 +	friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
   1.195 +	friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
   1.196 +	friend inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
   1.197 +
   1.198 +	/* binary operations matrix (op) scalar and scalar (op) matrix */
   1.199 +	friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
   1.200 +	friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
   1.201 +
   1.202 +	friend void operator *=(Matrix4x4 &mat, scalar_t scalar);
   1.203 +
   1.204 +	inline scalar_t *operator [](int index);
   1.205 +	inline const scalar_t *operator [](int index) const;
   1.206 +
   1.207 +	inline void reset_identity();
   1.208 +
   1.209 +	void translate(const Vector3 &trans);
   1.210 +	void set_translation(const Vector3 &trans);
   1.211 +	Vector3 get_translation() const;	/* extract translation */
   1.212 +
   1.213 +	void rotate(const Vector3 &euler_angles);			/* 3d rotation with euler angles */
   1.214 +	void rotate(const Vector3 &axis, scalar_t angle);	/* 3d axis/angle rotation */
   1.215 +	void rotate(const Quaternion &quat);
   1.216 +	void set_rotation(const Vector3 &euler_angles);
   1.217 +	void set_rotation(const Vector3 &axis, scalar_t angle);
   1.218 +	void set_rotation(const Quaternion &quat);
   1.219 +	Quaternion get_rotation_quat() const;		/* extract rotation */
   1.220 +
   1.221 +	void scale(const Vector4 &scale_vec);
   1.222 +	void set_scaling(const Vector4 &scale_vec);
   1.223 +	Vector3 get_scaling() const;		/* extract scaling */
   1.224 +
   1.225 +	void set_perspective(float vfov, float aspect, float znear, float zfar);
   1.226 +	void set_orthographic(float left, float right, float bottom, float top, float znear = -1.0, float zfar = 1.0);
   1.227 +
   1.228 +	void set_column_vector(const Vector4 &vec, unsigned int col_index);
   1.229 +	void set_row_vector(const Vector4 &vec, unsigned int row_index);
   1.230 +	Vector4 get_column_vector(unsigned int col_index) const;
   1.231 +	Vector4 get_row_vector(unsigned int row_index) const;
   1.232 +
   1.233 +	void transpose();
   1.234 +	Matrix4x4 transposed() const;
   1.235 +	scalar_t determinant() const;
   1.236 +	Matrix4x4 adjoint() const;
   1.237 +	Matrix4x4 inverse() const;
   1.238 +
   1.239 +	friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat);
   1.240 +};
   1.241 +
   1.242 +/* binary operations matrix (op) matrix */
   1.243 +Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
   1.244 +Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
   1.245 +inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
   1.246 +
   1.247 +void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
   1.248 +void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
   1.249 +inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
   1.250 +
   1.251 +/* binary operations matrix (op) scalar and scalar (op) matrix */
   1.252 +Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
   1.253 +Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
   1.254 +
   1.255 +void operator *=(Matrix4x4 &mat, scalar_t scalar);
   1.256 +
   1.257 +std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat);
   1.258 +
   1.259 +#endif	/* __cplusplus */
   1.260 +
   1.261 +#include "matrix.inl"
   1.262 +
   1.263 +#endif	/* VMATH_MATRIX_H_ */