vrshoot

diff libs/vmath/matrix.h @ 0:b2f14e535253

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