1 /********************************************************************************
2 *                                                                               *
3 *            S i n g l e - P r e c i s i o n   4 x 4   M a t r i x              *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1994,2021 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify          *
9 * it under the terms of the GNU Lesser General Public License as published by   *
10 * the Free Software Foundation; either version 3 of the License, or             *
11 * (at your option) any later version.                                           *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
16 * GNU Lesser General Public License for more details.                           *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public License      *
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
20 ********************************************************************************/
21 #ifndef FXMAT4F_H
22 #define FXMAT4F_H
23 
24 namespace FX {
25 
26 
27 class FXQuatf;
28 class FXMat3f;
29 
30 
31 /// Single-precision 4x4 matrix
32 class FXAPI FXMat4f {
33 protected:
34   FXVec4f m[4];
35 public:
36 
37   /// Default constructor; value is not initialized
FXMat4f()38   FXMat4f(){}
39 
40   /// Initialize matrix from scalar
41   FXMat4f(FXfloat s);
42 
43   /// Initialize with 3x3 rotation and scaling matrix
44   FXMat4f(const FXMat3f& s);
45 
46   /// Initialize matrix from another matrix
47   FXMat4f(const FXMat4f& s);
48 
49   /// Initialize matrix from array
50   FXMat4f(const FXfloat s[]);
51 
52   /// Initialize diagonal matrix
53   FXMat4f(FXfloat a,FXfloat b,FXfloat c,FXfloat d);
54 
55   /// Initialize matrix from components
56   FXMat4f(FXfloat a00,FXfloat a01,FXfloat a02,FXfloat a03,
57           FXfloat a10,FXfloat a11,FXfloat a12,FXfloat a13,
58           FXfloat a20,FXfloat a21,FXfloat a22,FXfloat a23,
59           FXfloat a30,FXfloat a31,FXfloat a32,FXfloat a33);
60 
61   /// Initialize matrix from four vectors
62   FXMat4f(const FXVec4f& a,const FXVec4f& b,const FXVec4f& c,const FXVec4f& d);
63 
64   /// Initialize matrix from quaternion
65   FXMat4f(const FXQuatf& quat);
66 
67   /// Assignment from scalar
68   FXMat4f& operator=(FXfloat s);
69 
70   /// Assignment
71   FXMat4f& operator=(const FXMat3f& s);
72   FXMat4f& operator=(const FXMat4f& s);
73 
74   /// Assignment from quaternion
75   FXMat4f& operator=(const FXQuatf& quat);
76 
77   /// Assignment from array
78   FXMat4f& operator=(const FXfloat s[]);
79 
80   /// Set value from scalar
81   FXMat4f& set(FXfloat s);
82 
83   /// Set value from 3x3 rotation and scaling matrix
84   FXMat4f& set(const FXMat3f& s);
85 
86   /// Set value from another matrix
87   FXMat4f& set(const FXMat4f& s);
88 
89   /// Set value from array
90   FXMat4f& set(const FXfloat s[]);
91 
92   /// Set diagonal matrix
93   FXMat4f& set(FXfloat a,FXfloat b,FXfloat c,FXfloat d);
94 
95   /// Set value from components
96   FXMat4f& set(FXfloat a00,FXfloat a01,FXfloat a02,FXfloat a03,
97                FXfloat a10,FXfloat a11,FXfloat a12,FXfloat a13,
98                FXfloat a20,FXfloat a21,FXfloat a22,FXfloat a23,
99                FXfloat a30,FXfloat a31,FXfloat a32,FXfloat a33);
100 
101   /// Set value from four vectors
102   FXMat4f& set(const FXVec4f& a,const FXVec4f& b,const FXVec4f& c,const FXVec4f& d);
103 
104   /// Set value from quaternion
105   FXMat4f& set(const FXQuatf& quat);
106 
107   /// Assignment operators
108   FXMat4f& operator+=(const FXMat4f& s);
109   FXMat4f& operator-=(const FXMat4f& s);
110   FXMat4f& operator*=(const FXMat4f& s);
111   FXMat4f& operator*=(FXfloat s);
112   FXMat4f& operator/=(FXfloat s);
113 
114   /// Indexing
115   FXVec4f& operator[](FXint i){return m[i];}
116   const FXVec4f& operator[](FXint i) const {return m[i];}
117 
118   /// Conversion
119   operator FXfloat*(){return m[0];}
120   operator const FXfloat*() const {return m[0];}
121 
122   /// Unary minus
123   FXMat4f operator-() const;
124 
125   /// Set to identity matrix
126   FXMat4f& identity();
127 
128   /// Return true if identity matrix
129   FXbool isIdentity() const;
130 
131   /// Set orthographic projection from view volume
132   FXMat4f& setOrtho(FXfloat xlo,FXfloat xhi,FXfloat ylo,FXfloat yhi,FXfloat zlo,FXfloat zhi);
133 
134   /// Get view volume from orthographic projection
135   void getOrtho(FXfloat& xlo,FXfloat& xhi,FXfloat& ylo,FXfloat& yhi,FXfloat& zlo,FXfloat& zhi) const;
136 
137   /// Set to inverse orthographic projection
138   FXMat4f& setInverseOrtho(FXfloat xlo,FXfloat xhi,FXfloat ylo,FXfloat yhi,FXfloat zlo,FXfloat zhi);
139 
140   /// Set to perspective projection from view volume
141   FXMat4f& setFrustum(FXfloat xlo,FXfloat xhi,FXfloat ylo,FXfloat yhi,FXfloat zlo,FXfloat zhi);
142 
143   /// Get view volume from perspective projection
144   void getFrustum(FXfloat& xlo,FXfloat& xhi,FXfloat& ylo,FXfloat& yhi,FXfloat& zlo,FXfloat& zhi) const;
145 
146   /// Set to inverse perspective projection from view volume
147   FXMat4f& setInverseFrustum(FXfloat xlo,FXfloat xhi,FXfloat ylo,FXfloat yhi,FXfloat zlo,FXfloat zhi);
148 
149   /// Multiply by left-hand matrix
150   FXMat4f& left();
151 
152   /// Multiply by rotation matrix
153   FXMat4f& rot(const FXMat3f& r);
154 
155   /// Multiply by rotation about unit-quaternion
156   FXMat4f& rot(const FXQuatf& q);
157 
158   /// Multiply by rotation c,s about unit axis
159   FXMat4f& rot(const FXVec3f& v,FXfloat c,FXfloat s);
160 
161   /// Multiply by rotation of phi about unit axis
162   FXMat4f& rot(const FXVec3f& v,FXfloat phi);
163 
164   /// Multiply by x-rotation
165   FXMat4f& xrot(FXfloat c,FXfloat s);
166   FXMat4f& xrot(FXfloat phi);
167 
168   /// Multiply by y-rotation
169   FXMat4f& yrot(FXfloat c,FXfloat s);
170   FXMat4f& yrot(FXfloat phi);
171 
172   /// Multiply by z-rotation
173   FXMat4f& zrot(FXfloat c,FXfloat s);
174   FXMat4f& zrot(FXfloat phi);
175 
176   /// Look at
177   FXMat4f& look(const FXVec3f& from,const FXVec3f& to,const FXVec3f& up);
178 
179   /// Multiply by translation
180   FXMat4f& trans(FXfloat tx,FXfloat ty,FXfloat tz);
181   FXMat4f& trans(const FXVec3f& v);
182 
183   /// Multiply by scaling
184   FXMat4f& scale(FXfloat sx,FXfloat sy,FXfloat sz);
185   FXMat4f& scale(const FXVec3f& v);
186   FXMat4f& scale(FXfloat s);
187 
188   /// Determinant
189   FXfloat det() const;
190 
191   /// Transpose
192   FXMat4f transpose() const;
193 
194   /// Invert
195   FXMat4f invert() const;
196 
197   /// Invert affine matrix
198   FXMat4f affineInvert() const;
199 
200   /// Invert rigid body transform matrix
201   FXMat4f rigidInvert() const;
202 
203   /// Return normal-transformation matrix
204   FXMat3f normalMatrix() const;
205 
206   /// Destructor
~FXMat4f()207  ~FXMat4f(){}
208   };
209 
210 
211 /// Matrix times vector
212 extern FXAPI FXVec3f operator*(const FXMat4f& m,const FXVec3f& v);
213 extern FXAPI FXVec4f operator*(const FXMat4f& m,const FXVec4f& v);
214 
215 /// Vector times matrix
216 extern FXAPI FXVec3f operator*(const FXVec3f& v,const FXMat4f& m);
217 extern FXAPI FXVec4f operator*(const FXVec4f& v,const FXMat4f& m);
218 
219 /// Matrix and matrix addition
220 extern FXAPI FXMat4f operator+(const FXMat4f& a,const FXMat4f& b);
221 extern FXAPI FXMat4f operator-(const FXMat4f& a,const FXMat4f& b);
222 
223 /// Matrix and matrix multiply
224 extern FXAPI FXMat4f operator*(const FXMat4f& a,const FXMat4f& b);
225 
226 /// Scaling
227 extern FXAPI FXMat4f operator*(FXfloat x,const FXMat4f& a);
228 extern FXAPI FXMat4f operator*(const FXMat4f& a,FXfloat x);
229 extern FXAPI FXMat4f operator/(const FXMat4f& a,FXfloat x);
230 extern FXAPI FXMat4f operator/(FXfloat x,const FXMat4f& a);
231 
232 /// Equality tests
233 extern FXAPI FXbool operator==(const FXMat4f& a,const FXMat4f& b);
234 extern FXAPI FXbool operator!=(const FXMat4f& a,const FXMat4f& b);
235 extern FXAPI FXbool operator==(const FXMat4f& a,FXfloat n);
236 extern FXAPI FXbool operator!=(const FXMat4f& a,FXfloat n);
237 extern FXAPI FXbool operator==(FXfloat n,const FXMat4f& a);
238 extern FXAPI FXbool operator!=(FXfloat n,const FXMat4f& a);
239 
240 /// Orthogonalize matrix
241 extern FXAPI FXMat4f orthogonalize(const FXMat4f& m);
242 
243 /// Save matrix to a stream
244 extern FXAPI FXStream& operator<<(FXStream& store,const FXMat4f& m);
245 
246 /// Load matrix from a stream
247 extern FXAPI FXStream& operator>>(FXStream& store,FXMat4f& m);
248 
249 }
250 
251 #endif
252