1 /********************************************************************************
2 *                                                                               *
3 *            D o u b l e - P r e c i s i o n   2 x 2   M a t r i x              *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 2003,2020 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 FXMAT2D_H
22 #define FXMAT2D_H
23 
24 namespace FX {
25 
26 
27 class FXMat3d;
28 
29 
30 /// Double-precision 2x2 matrix
31 class FXAPI FXMat2d {
32 protected:
33   FXVec2d m[2];
34 public:
35 
36   /// Default constructor; value is not initialized
FXMat2d()37   FXMat2d(){}
38 
39   /// Initialize matrix from scalar
40   FXMat2d(FXdouble s);
41 
42   /// Initialize matrix from another matrix
43   FXMat2d(const FXMat2d& s);
44 
45   /// Initialize from rotation and scaling part of 3x3 matrix
46   FXMat2d(const FXMat3d& s);
47 
48   /// Initialize matrix from array
49   FXMat2d(const FXdouble s[]);
50 
51   /// Initialize diagonal matrix
52   FXMat2d(FXdouble a,FXdouble b);
53 
54   /// Initialize matrix from components
55   FXMat2d(FXdouble a00,FXdouble a01,FXdouble a10,FXdouble a11);
56 
57   /// Initialize matrix from two vectors
58   FXMat2d(const FXVec2d& a,const FXVec2d& b);
59 
60   /// Assignment from scalar
61   FXMat2d& operator=(FXdouble s);
62 
63   /// Assignment
64   FXMat2d& operator=(const FXMat2d& s);
65   FXMat2d& operator=(const FXMat3d& s);
66 
67   /// Assignment from array
68   FXMat2d& operator=(const FXdouble s[]);
69 
70   /// Set value from scalar
71   FXMat2d& set(FXdouble s);
72 
73   /// Set value from another matrix
74   FXMat2d& set(const FXMat2d& s);
75 
76   /// Set from rotation and scaling part of 3x3 matrix
77   FXMat2d& set(const FXMat3d& s);
78 
79   /// Set value from array
80   FXMat2d& set(const FXdouble s[]);
81 
82   /// Set diagonal matrix
83   FXMat2d& set(FXdouble a,FXdouble b);
84 
85   /// Set value from components
86   FXMat2d& set(FXdouble a00,FXdouble a01,FXdouble a10,FXdouble a11);
87 
88   /// Set value from two vectors
89   FXMat2d& set(const FXVec2d& a,const FXVec2d& b);
90 
91   /// Assignment operators
92   FXMat2d& operator+=(const FXMat2d& s);
93   FXMat2d& operator-=(const FXMat2d& s);
94   FXMat2d& operator*=(const FXMat2d& s);
95   FXMat2d& operator*=(FXdouble s);
96   FXMat2d& operator/=(FXdouble s);
97 
98   /// Indexing
99   FXVec2d& operator[](FXint i){return m[i];}
100   const FXVec2d& operator[](FXint i) const {return m[i];}
101 
102   /// Conversion
103   operator FXdouble*(){return m[0];}
104   operator const FXdouble*() const {return m[0];}
105 
106   /// Unary minus
107   FXMat2d operator-() const;
108 
109   /// Set to identity matrix
110   FXMat2d& identity();
111 
112   /// Return true if identity matrix
113   FXbool isIdentity() const;
114 
115   /// Multiply by rotation of phi
116   FXMat2d& rot(FXdouble c,FXdouble s);
117   FXMat2d& rot(FXdouble phi);
118 
119   /// Multiply by scaling
120   FXMat2d& scale(FXdouble sx,FXdouble sy);
121   FXMat2d& scale(FXdouble s);
122 
123   /// Determinant
124   FXdouble det() const;
125 
126   /// Transpose
127   FXMat2d transpose() const;
128 
129   /// Invert
130   FXMat2d invert() const;
131 
132   /// Destructor
~FXMat2d()133  ~FXMat2d(){}
134   };
135 
136 
137 /// Matrix times vector
138 extern FXAPI FXVec2d operator*(const FXMat2d& m,const FXVec2d& v);
139 
140 /// Vector times matrix
141 extern FXAPI FXVec2d operator*(const FXVec2d& v,const FXMat2d& m);
142 
143 /// Matrix and matrix addition
144 extern FXAPI FXMat2d operator+(const FXMat2d& a,const FXMat2d& b);
145 extern FXAPI FXMat2d operator-(const FXMat2d& a,const FXMat2d& b);
146 
147 /// Matrix and matrix multiply
148 extern FXAPI FXMat2d operator*(const FXMat2d& a,const FXMat2d& b);
149 
150 /// Scaling
151 extern FXAPI FXMat2d operator*(FXdouble x,const FXMat2d& a);
152 extern FXAPI FXMat2d operator*(const FXMat2d& a,FXdouble x);
153 extern FXAPI FXMat2d operator/(const FXMat2d& a,FXdouble x);
154 extern FXAPI FXMat2d operator/(FXdouble x,const FXMat2d& a);
155 
156 /// Equality tests
157 extern FXAPI FXbool operator==(const FXMat2d& a,const FXMat2d& b);
158 extern FXAPI FXbool operator!=(const FXMat2d& a,const FXMat2d& b);
159 extern FXAPI FXbool operator==(const FXMat2d& a,FXdouble n);
160 extern FXAPI FXbool operator!=(const FXMat2d& a,FXdouble n);
161 extern FXAPI FXbool operator==(FXdouble n,const FXMat2d& a);
162 extern FXAPI FXbool operator!=(FXdouble n,const FXMat2d& a);
163 
164 /// Save matrix to a stream
165 extern FXAPI FXStream& operator<<(FXStream& store,const FXMat2d& m);
166 
167 /// Load matrix from a stream
168 extern FXAPI FXStream& operator>>(FXStream& store,FXMat2d& m);
169 
170 }
171 
172 #endif
173