1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 // This file is generated by a script.  Do not edit directly.  Edit the
26 // matrix2.template.h file to make changes.
27 
28 #ifndef PXR_BASE_GF_MATRIX2F_H
29 #define PXR_BASE_GF_MATRIX2F_H
30 
31 /// \file gf/matrix2f.h
32 /// \ingroup group_gf_LinearAlgebra
33 
34 #include "pxr/pxr.h"
35 #include "pxr/base/gf/api.h"
36 #include "pxr/base/gf/declare.h"
37 #include "pxr/base/gf/matrixData.h"
38 #include "pxr/base/gf/vec2f.h"
39 #include "pxr/base/gf/traits.h"
40 
41 #include <boost/functional/hash.hpp>
42 
43 #include <iosfwd>
44 #include <vector>
45 
46 PXR_NAMESPACE_OPEN_SCOPE
47 
48 template <>
49 struct GfIsGfMatrix<class GfMatrix2f> { static const bool value = true; };
50 
51 class GfMatrix2d;
52 class GfMatrix2f;
53 
54 /// \class GfMatrix2f
55 /// \ingroup group_gf_LinearAlgebra
56 ///
57 /// Stores a 2x2 matrix of \c float elements. A basic type.
58 ///
59 /// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
60 /// indexes the element in the \e i th row and the \e j th column.
61 ///
62 class GfMatrix2f
63 {
64 public:
65     typedef float ScalarType;
66 
67     static const size_t numRows = 2;
68     static const size_t numColumns = 2;
69 
70     /// Default constructor. Leaves the matrix component values undefined.
71     GfMatrix2f() = default;
72 
73     /// Constructor. Initializes the matrix from 4 independent
74     /// \c float values, specified in row-major order. For example,
75     /// parameter \e m10 specifies the value in row 1 and column 0.
76     GfMatrix2f(float m00, float m01,
77                float m10, float m11) {
78         Set(m00, m01,
79             m10, m11);
80     }
81 
82     /// Constructor. Initializes the matrix from a 2x2 array
83     /// of \c float values, specified in row-major order.
84     GfMatrix2f(const float m[2][2]) {
85         Set(m);
86     }
87 
88     /// Constructor. Explicitly initializes the matrix to \e s times the
89     /// identity matrix.
90     explicit GfMatrix2f(float s) {
91         SetDiagonal(s);
92     }
93 
94     /// This explicit constructor initializes the matrix to \p s times
95     /// the identity matrix.
96     explicit GfMatrix2f(int s) {
97         SetDiagonal(s);
98     }
99 
100     /// Constructor. Explicitly initializes the matrix to diagonal form,
101     /// with the \e i th element on the diagonal set to <c>v[i]</c>.
102     explicit GfMatrix2f(const GfVec2f& v) {
103         SetDiagonal(v);
104     }
105 
106     /// Constructor.  Initialize the matrix from a vector of vectors of
107     /// double. The vector is expected to be 2x2. If it is
108     /// too big, only the first 2 rows and/or columns will be used.
109     /// If it is too small, uninitialized elements will be filled in with
110     /// the corresponding elements from an identity matrix.
111     ///
112     GF_API
113     explicit GfMatrix2f(const std::vector< std::vector<double> >& v);
114 
115     /// Constructor.  Initialize the matrix from a vector of vectors of
116     /// float. The vector is expected to be 2x2. If it is
117     /// too big, only the first 2 rows and/or columns will be used.
118     /// If it is too small, uninitialized elements will be filled in with
119     /// the corresponding elements from an identity matrix.
120     ///
121     GF_API
122     explicit GfMatrix2f(const std::vector< std::vector<float> >& v);
123 
124     /// This explicit constructor converts a "double" matrix to a "float" matrix.
125     GF_API
126     explicit GfMatrix2f(const class GfMatrix2d& m);
127 
128     /// Sets a row of the matrix from a Vec2.
129     void SetRow(int i, const GfVec2f & v) {
130         _mtx[i][0] = v[0];
131         _mtx[i][1] = v[1];
132     }
133 
134     /// Sets a column of the matrix from a Vec2.
135     void SetColumn(int i, const GfVec2f & v) {
136         _mtx[0][i] = v[0];
137         _mtx[1][i] = v[1];
138     }
139 
140     /// Gets a row of the matrix as a Vec2.
141     GfVec2f GetRow(int i) const {
142         return GfVec2f(_mtx[i][0], _mtx[i][1]);
143     }
144 
145     /// Gets a column of the matrix as a Vec2.
146     GfVec2f GetColumn(int i) const {
147         return GfVec2f(_mtx[0][i], _mtx[1][i]);
148     }
149 
150     /// Sets the matrix from 4 independent \c float values,
151     /// specified in row-major order. For example, parameter \e m10 specifies
152     /// the value in row 1 and column 0.
153     GfMatrix2f& Set(float m00, float m01,
154                     float m10, float m11) {
155         _mtx[0][0] = m00; _mtx[0][1] = m01;
156         _mtx[1][0] = m10; _mtx[1][1] = m11;
157         return *this;
158     }
159 
160     /// Sets the matrix from a 2x2 array of \c float
161     /// values, specified in row-major order.
162     GfMatrix2f& Set(const float m[2][2]) {
163         _mtx[0][0] = m[0][0];
164         _mtx[0][1] = m[0][1];
165         _mtx[1][0] = m[1][0];
166         _mtx[1][1] = m[1][1];
167         return *this;
168     }
169 
170     /// Sets the matrix to the identity matrix.
171     GfMatrix2f& SetIdentity() {
172         return SetDiagonal(1);
173     }
174 
175     /// Sets the matrix to zero.
176     GfMatrix2f& SetZero() {
177         return SetDiagonal(0);
178     }
179 
180     /// Sets the matrix to \e s times the identity matrix.
181     GF_API
182     GfMatrix2f& SetDiagonal(float s);
183 
184     /// Sets the matrix to have diagonal (<c>v[0], v[1]</c>).
185     GF_API
186     GfMatrix2f& SetDiagonal(const GfVec2f&);
187 
188     /// Fills a 2x2 array of \c float values with the values in
189     /// the matrix, specified in row-major order.
190     GF_API
191     float* Get(float m[2][2]) const;
192 
193     /// Returns raw access to components of matrix as an array of
194     /// \c float values.  Components are in row-major order.
195     float* data() {
196         return _mtx.GetData();
197     }
198 
199     /// Returns const raw access to components of matrix as an array of
200     /// \c float values.  Components are in row-major order.
201     const float* data() const {
202         return _mtx.GetData();
203     }
204 
205     /// Returns vector components as an array of \c float values.
206     float* GetArray()  {
207         return _mtx.GetData();
208     }
209 
210     /// Returns vector components as a const array of \c float values.
211     const float* GetArray() const {
212         return _mtx.GetData();
213     }
214 
215     /// Accesses an indexed row \e i of the matrix as an array of 2 \c
216     /// float values so that standard indexing (such as <c>m[0][1]</c>)
217     /// works correctly.
218     float* operator [](int i) { return _mtx[i]; }
219 
220     /// Accesses an indexed row \e i of the matrix as an array of 2 \c
221     /// float values so that standard indexing (such as <c>m[0][1]</c>)
222     /// works correctly.
223     const float* operator [](int i) const { return _mtx[i]; }
224 
225     /// Hash.
226     friend inline size_t hash_value(GfMatrix2f const &m) {
227         int nElems = 2 * 2;
228         size_t h = 0;
229         const float *p = m.GetArray();
230         while (nElems--)
231             boost::hash_combine(h, *p++);
232         return h;
233     }
234 
235     /// Tests for element-wise matrix equality. All elements must match
236     /// exactly for matrices to be considered equal.
237     GF_API
238     bool operator ==(const GfMatrix2d& m) const;
239 
240     /// Tests for element-wise matrix equality. All elements must match
241     /// exactly for matrices to be considered equal.
242     GF_API
243     bool operator ==(const GfMatrix2f& m) const;
244 
245     /// Tests for element-wise matrix inequality. All elements must match
246     /// exactly for matrices to be considered equal.
247     bool operator !=(const GfMatrix2d& m) const {
248         return !(*this == m);
249     }
250 
251     /// Tests for element-wise matrix inequality. All elements must match
252     /// exactly for matrices to be considered equal.
253     bool operator !=(const GfMatrix2f& m) const {
254         return !(*this == m);
255     }
256 
257     /// Returns the transpose of the matrix.
258     GF_API
259     GfMatrix2f GetTranspose() const;
260 
261     /// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
262     /// matrix is singular. (FLT_MAX is the largest value a \c float can have,
263     /// as defined by the system.) The matrix is considered singular if the
264     /// determinant is less than or equal to the optional parameter \e eps. If
265     /// \e det is non-null, <c>*det</c> is set to the determinant.
266     GF_API
267     GfMatrix2f GetInverse(double* det = NULL, double eps = 0) const;
268 
269     /// Returns the determinant of the matrix.
270     GF_API
271     double GetDeterminant() const;
272 
273 
274     /// Post-multiplies matrix \e m into this matrix.
275     GF_API
276     GfMatrix2f& operator *=(const GfMatrix2f& m);
277 
278     /// Multiplies the matrix by a float.
279     GF_API
280     GfMatrix2f& operator *=(double);
281 
282     /// Returns the product of a matrix and a float.
283     friend GfMatrix2f operator *(const GfMatrix2f& m1, double d)
284     {
285         GfMatrix2f m = m1;
286         return m *= d;
287     }
288 
289     ///
290     // Returns the product of a matrix and a float.
291     friend GfMatrix2f operator *(double d, const GfMatrix2f& m)
292     {
293         return m * d;
294     }
295 
296     /// Adds matrix \e m to this matrix.
297     GF_API
298     GfMatrix2f& operator +=(const GfMatrix2f& m);
299 
300     /// Subtracts matrix \e m from this matrix.
301     GF_API
302     GfMatrix2f& operator -=(const GfMatrix2f& m);
303 
304     /// Returns the unary negation of matrix \e m.
305     GF_API
306     friend GfMatrix2f operator -(const GfMatrix2f& m);
307 
308     /// Adds matrix \e m2 to \e m1
309     friend GfMatrix2f operator +(const GfMatrix2f& m1, const GfMatrix2f& m2)
310     {
311         GfMatrix2f tmp(m1);
312         tmp += m2;
313         return tmp;
314     }
315 
316     /// Subtracts matrix \e m2 from \e m1.
317     friend GfMatrix2f operator -(const GfMatrix2f& m1, const GfMatrix2f& m2)
318     {
319         GfMatrix2f tmp(m1);
320         tmp -= m2;
321         return tmp;
322     }
323 
324     /// Multiplies matrix \e m1 by \e m2.
325     friend GfMatrix2f operator *(const GfMatrix2f& m1, const GfMatrix2f& m2)
326     {
327         GfMatrix2f tmp(m1);
328         tmp *= m2;
329         return tmp;
330     }
331 
332     /// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
333     friend GfMatrix2f operator /(const GfMatrix2f& m1, const GfMatrix2f& m2)
334     {
335         return(m1 * m2.GetInverse());
336     }
337 
338     /// Returns the product of a matrix \e m and a column vector \e vec.
339     friend inline GfVec2f operator *(const GfMatrix2f& m, const GfVec2f& vec) {
340         return GfVec2f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1],
341                        vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1]);
342     }
343 
344     /// Returns the product of row vector \e vec and a matrix \e m.
345     friend inline GfVec2f operator *(const GfVec2f &vec, const GfMatrix2f& m) {
346         return GfVec2f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0],
347                        vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1]);
348     }
349 
350 
351 private:
352     /// Matrix storage, in row-major order.
353     GfMatrixData<float, 2, 2> _mtx;
354 
355     // Friend declarations
356     friend class GfMatrix2d;
357 };
358 
359 
360 /// Tests for equality within a given tolerance, returning \c true if the
361 /// difference between each component of the matrix is less than or equal
362 /// to \p tolerance, or false otherwise.
363 GF_API
364 bool GfIsClose(GfMatrix2f const &m1, GfMatrix2f const &m2, double tolerance);
365 
366 /// Output a GfMatrix2f
367 /// \ingroup group_gf_DebuggingOutput
368 GF_API std::ostream& operator<<(std::ostream &, GfMatrix2f const &);
369 
370 PXR_NAMESPACE_CLOSE_SCOPE
371 
372 #endif // PXR_BASE_GF_MATRIX2F_H
373