added summerhack
[summerhack] / tools / curve_draw / vmath / matrix.h
1 /*
2 This file is part of XRay, a photorealistic 3D rendering library.
3 Copyright (C) 2005 John Tsiombikas
4
5 XRay is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 XRay is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with XRay; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /**
21  * @file matrix.h
22  * @author John Tsiombikas
23  * @date 28 June 2005
24  *
25  * Matrix math.
26  */
27
28 #ifndef MATRIX_H_
29 #define MATRIX_H_
30
31 #include <iostream>
32 #include "vmath_types.h"
33
34 /** 3x3 matrix */
35 class Matrix3x3 {
36 private:
37         scalar_t m[3][3];
38 public:
39         
40         static Matrix3x3 identity_matrix;
41
42         Matrix3x3();
43         Matrix3x3(      scalar_t m11, scalar_t m12, scalar_t m13,
44                                 scalar_t m21, scalar_t m22, scalar_t m23,
45                                 scalar_t m31, scalar_t m32, scalar_t m33);
46         
47         Matrix3x3(const Matrix4x4 &mat4x4);
48         
49         // binary operations matrix (op) matrix
50         friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
51         friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
52         friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
53         
54         friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
55         friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
56         friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
57         
58         // binary operations matrix (op) scalar and scalar (op) matrix
59         friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
60         friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
61         
62         friend void operator *=(Matrix3x3 &mat, scalar_t scalar);
63         
64         inline scalar_t *operator [](int index);
65         inline const scalar_t *operator [](int index) const;
66         
67         inline void reset_identity();
68         
69         void translate(const Vector2 &trans);
70         void set_translation(const Vector2 &trans);
71         
72         void rotate(scalar_t angle);                                            // 2d rotation
73         void rotate(const Vector3 &euler_angles);                       // 3d rotation with euler angles
74         void rotate(const Vector3 &axis, scalar_t angle);       // 3d axis/angle rotation
75         void set_rotation(scalar_t angle);
76         void set_rotation(const Vector3 &euler_angles);
77         void set_rotation(const Vector3 &axis, scalar_t angle);
78         
79         void scale(const Vector3 &scale_vec);
80         void set_scaling(const Vector3 &scale_vec);
81         
82         void set_column_vector(const Vector3 &vec, unsigned int col_index);
83         void set_row_vector(const Vector3 &vec, unsigned int row_index);
84         Vector3 get_column_vector(unsigned int col_index) const;
85         Vector3 get_row_vector(unsigned int row_index) const;
86
87         void transpose();
88         Matrix3x3 transposed() const;   
89         scalar_t determinant() const;
90         Matrix3x3 inverse() const;
91         
92         friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat);
93 };
94
95 /** 4x4 matrix */
96 class Matrix4x4 {
97 private:
98         scalar_t m[4][4];
99         mutable float *glmatrix;
100 public:
101         
102         static Matrix4x4 identity_matrix;
103
104         Matrix4x4();
105         Matrix4x4(      scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
106                                 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
107                                 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
108                                 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
109         
110         Matrix4x4(const Matrix3x3 &mat3x3);
111         ~Matrix4x4();
112         
113         // binary operations matrix (op) matrix
114         friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
115         friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
116         friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
117         
118         friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
119         friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
120         friend void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
121         
122         // binary operations matrix (op) scalar and scalar (op) matrix
123         friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
124         friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
125         
126         friend void operator *=(Matrix4x4 &mat, scalar_t scalar);
127         
128         inline scalar_t *operator [](int index);
129         inline const scalar_t *operator [](int index) const;
130         
131         inline void reset_identity();
132         
133         void translate(const Vector3 &trans);
134         void set_translation(const Vector3 &trans);
135         
136         void rotate(const Vector3 &euler_angles);                       // 3d rotation with euler angles
137         void rotate(const Vector3 &axis, scalar_t angle);       // 3d axis/angle rotation
138         void set_rotation(const Vector3 &euler_angles);
139         void set_rotation(const Vector3 &axis, scalar_t angle);
140         
141         void scale(const Vector4 &scale_vec);
142         void set_scaling(const Vector4 &scale_vec);
143         
144         void set_column_vector(const Vector4 &vec, unsigned int col_index);
145         void set_row_vector(const Vector4 &vec, unsigned int row_index);
146         Vector4 get_column_vector(unsigned int col_index) const;
147         Vector4 get_row_vector(unsigned int row_index) const;
148         
149         void transpose();
150         Matrix4x4 transposed() const;
151         scalar_t determinant() const;
152         Matrix4x4 adjoint() const;
153         Matrix4x4 inverse() const;
154         
155         const scalar_t *opengl_matrix() const;
156                 
157         friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat);
158 };
159
160 #include "matrix.inl"
161
162 #endif  // MATRIX_HPP_