HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
matrix2f.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 ////////////////////////////////////////////////////////////////////////
8 // This file is generated by a script. Do not edit directly. Edit the
9 // matrix2.template.h file to make changes.
10 
11 #ifndef PXR_BASE_GF_MATRIX2F_H
12 #define PXR_BASE_GF_MATRIX2F_H
13 
14 /// \file gf/matrix2f.h
15 /// \ingroup group_gf_LinearAlgebra
16 
17 #include "pxr/pxr.h"
18 #include "pxr/base/gf/api.h"
19 #include "pxr/base/gf/declare.h"
20 #include "pxr/base/gf/matrixData.h"
21 #include "pxr/base/gf/vec2f.h"
22 #include "pxr/base/gf/traits.h"
23 #include "pxr/base/tf/hash.h"
24 
25 #include <iosfwd>
26 #include <vector>
27 
29 
30 template <>
31 struct GfIsGfMatrix<class GfMatrix2f> { static const bool value = true; };
32 
33 class GfMatrix2d;
34 class GfMatrix2f;
35 
36 /// \class GfMatrix2f
37 /// \ingroup group_gf_LinearAlgebra
38 ///
39 /// Stores a 2x2 matrix of \c float elements. A basic type.
40 ///
41 /// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
42 /// indexes the element in the \e i th row and the \e j th column.
43 ///
45 {
46 public:
47  typedef float ScalarType;
48 
49  static const size_t numRows = 2;
50  static const size_t numColumns = 2;
51 
52  /// GfMatrix2f value-initializes to zero and performs no default
53  /// initialization, like float or double.
54  GfMatrix2f() = default;
55 
56  /// Constructor. Initializes the matrix from 4 independent
57  /// \c float values, specified in row-major order. For example,
58  /// parameter \e m10 specifies the value in row 1 and column 0.
59  GfMatrix2f(float m00, float m01,
60  float m10, float m11) {
61  Set(m00, m01,
62  m10, m11);
63  }
64 
65  /// Constructor. Initializes the matrix from a 2x2 array
66  /// of \c float values, specified in row-major order.
67  GfMatrix2f(const float m[2][2]) {
68  Set(m);
69  }
70 
71  /// Constructor. Explicitly initializes the matrix to \e s times the
72  /// identity matrix.
73  explicit GfMatrix2f(float s) {
74  SetDiagonal(s);
75  }
76 
77  /// This explicit constructor initializes the matrix to \p s times
78  /// the identity matrix.
79  explicit GfMatrix2f(int s) {
80  SetDiagonal(s);
81  }
82 
83  /// Constructor. Explicitly initializes the matrix to diagonal form,
84  /// with the \e i th element on the diagonal set to <c>v[i]</c>.
85  explicit GfMatrix2f(const GfVec2f& v) {
86  SetDiagonal(v);
87  }
88 
89  /// Constructor. Initialize the matrix from a vector of vectors of
90  /// double. The vector is expected to be 2x2. If it is
91  /// too big, only the first 2 rows and/or columns will be used.
92  /// If it is too small, uninitialized elements will be filled in with
93  /// the corresponding elements from an identity matrix.
94  ///
95  GF_API
96  explicit GfMatrix2f(const std::vector< std::vector<double> >& v);
97 
98  /// Constructor. Initialize the matrix from a vector of vectors of
99  /// float. The vector is expected to be 2x2. If it is
100  /// too big, only the first 2 rows and/or columns will be used.
101  /// If it is too small, uninitialized elements will be filled in with
102  /// the corresponding elements from an identity matrix.
103  ///
104  GF_API
105  explicit GfMatrix2f(const std::vector< std::vector<float> >& v);
106 
107  /// This explicit constructor converts a "double" matrix to a "float" matrix.
108  GF_API
109  explicit GfMatrix2f(const class GfMatrix2d& m);
110 
111  /// Sets a row of the matrix from a Vec2.
112  void SetRow(int i, const GfVec2f & v) {
113  _mtx[i][0] = v[0];
114  _mtx[i][1] = v[1];
115  }
116 
117  /// Sets a column of the matrix from a Vec2.
118  void SetColumn(int i, const GfVec2f & v) {
119  _mtx[0][i] = v[0];
120  _mtx[1][i] = v[1];
121  }
122 
123  /// Gets a row of the matrix as a Vec2.
124  GfVec2f GetRow(int i) const {
125  return GfVec2f(_mtx[i][0], _mtx[i][1]);
126  }
127 
128  /// Gets a column of the matrix as a Vec2.
129  GfVec2f GetColumn(int i) const {
130  return GfVec2f(_mtx[0][i], _mtx[1][i]);
131  }
132 
133  /// Sets the matrix from 4 independent \c float values,
134  /// specified in row-major order. For example, parameter \e m10 specifies
135  /// the value in row 1 and column 0.
136  GfMatrix2f& Set(float m00, float m01,
137  float m10, float m11) {
138  _mtx[0][0] = m00; _mtx[0][1] = m01;
139  _mtx[1][0] = m10; _mtx[1][1] = m11;
140  return *this;
141  }
142 
143  /// Sets the matrix from a 2x2 array of \c float
144  /// values, specified in row-major order.
145  GfMatrix2f& Set(const float m[2][2]) {
146  _mtx[0][0] = m[0][0];
147  _mtx[0][1] = m[0][1];
148  _mtx[1][0] = m[1][0];
149  _mtx[1][1] = m[1][1];
150  return *this;
151  }
152 
153  /// Sets the matrix to the identity matrix.
155  return SetDiagonal(1);
156  }
157 
158  /// Sets the matrix to zero.
160  return SetDiagonal(0);
161  }
162 
163  /// Sets the matrix to \e s times the identity matrix.
164  GF_API
165  GfMatrix2f& SetDiagonal(float s);
166 
167  /// Sets the matrix to have diagonal (<c>v[0], v[1]</c>).
168  GF_API
169  GfMatrix2f& SetDiagonal(const GfVec2f&);
170 
171  /// Fills a 2x2 array of \c float values with the values in
172  /// the matrix, specified in row-major order.
173  GF_API
174  float* Get(float m[2][2]) const;
175 
176  /// Returns raw access to components of matrix as an array of
177  /// \c float values. Components are in row-major order.
178  float* data() {
179  return _mtx.GetData();
180  }
181 
182  /// Returns const raw access to components of matrix as an array of
183  /// \c float values. Components are in row-major order.
184  const float* data() const {
185  return _mtx.GetData();
186  }
187 
188  /// Returns vector components as an array of \c float values.
189  float* GetArray() {
190  return _mtx.GetData();
191  }
192 
193  /// Returns vector components as a const array of \c float values.
194  const float* GetArray() const {
195  return _mtx.GetData();
196  }
197 
198  /// Accesses an indexed row \e i of the matrix as an array of 2 \c
199  /// float values so that standard indexing (such as <c>m[0][1]</c>)
200  /// works correctly.
201  float* operator [](int i) { return _mtx[i]; }
202 
203  /// Accesses an indexed row \e i of the matrix as an array of 2 \c
204  /// float values so that standard indexing (such as <c>m[0][1]</c>)
205  /// works correctly.
206  const float* operator [](int i) const { return _mtx[i]; }
207 
208  /// Hash.
209  friend inline size_t hash_value(GfMatrix2f const &m) {
210  return TfHash::Combine(
211  m._mtx[0][0],
212  m._mtx[0][1],
213  m._mtx[1][0],
214  m._mtx[1][1]
215  );
216  }
217 
218  /// Tests for element-wise matrix equality. All elements must match
219  /// exactly for matrices to be considered equal.
220  GF_API
221  bool operator ==(const GfMatrix2d& m) const;
222 
223  /// Tests for element-wise matrix equality. All elements must match
224  /// exactly for matrices to be considered equal.
225  GF_API
226  bool operator ==(const GfMatrix2f& m) const;
227 
228  /// Tests for element-wise matrix inequality. All elements must match
229  /// exactly for matrices to be considered equal.
230  bool operator !=(const GfMatrix2d& m) const {
231  return !(*this == m);
232  }
233 
234  /// Tests for element-wise matrix inequality. All elements must match
235  /// exactly for matrices to be considered equal.
236  bool operator !=(const GfMatrix2f& m) const {
237  return !(*this == m);
238  }
239 
240  /// Returns the transpose of the matrix.
241  GF_API
242  GfMatrix2f GetTranspose() const;
243 
244  /// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
245  /// matrix is singular. (FLT_MAX is the largest value a \c float can have,
246  /// as defined by the system.) The matrix is considered singular if the
247  /// determinant is less than or equal to the optional parameter \e eps. If
248  /// \e det is non-null, <c>*det</c> is set to the determinant.
249  GF_API
250  GfMatrix2f GetInverse(double* det = NULL, double eps = 0) const;
251 
252  /// Returns the determinant of the matrix.
253  GF_API
254  double GetDeterminant() const;
255 
256 
257  /// Post-multiplies matrix \e m into this matrix.
258  GF_API
259  GfMatrix2f& operator *=(const GfMatrix2f& m);
260 
261  /// Multiplies the matrix by a float.
262  GF_API
263  GfMatrix2f& operator *=(double);
264 
265  /// Returns the product of a matrix and a float.
266  friend GfMatrix2f operator *(const GfMatrix2f& m1, double d)
267  {
268  GfMatrix2f m = m1;
269  return m *= d;
270  }
271 
272  ///
273  // Returns the product of a matrix and a float.
274  friend GfMatrix2f operator *(double d, const GfMatrix2f& m)
275  {
276  return m * d;
277  }
278 
279  /// Adds matrix \e m to this matrix.
280  GF_API
281  GfMatrix2f& operator +=(const GfMatrix2f& m);
282 
283  /// Subtracts matrix \e m from this matrix.
284  GF_API
285  GfMatrix2f& operator -=(const GfMatrix2f& m);
286 
287  /// Returns the unary negation of matrix \e m.
288  GF_API
289  friend GfMatrix2f operator -(const GfMatrix2f& m);
290 
291  /// Adds matrix \e m2 to \e m1
292  friend GfMatrix2f operator +(const GfMatrix2f& m1, const GfMatrix2f& m2)
293  {
294  GfMatrix2f tmp(m1);
295  tmp += m2;
296  return tmp;
297  }
298 
299  /// Subtracts matrix \e m2 from \e m1.
300  friend GfMatrix2f operator -(const GfMatrix2f& m1, const GfMatrix2f& m2)
301  {
302  GfMatrix2f tmp(m1);
303  tmp -= m2;
304  return tmp;
305  }
306 
307  /// Multiplies matrix \e m1 by \e m2.
308  friend GfMatrix2f operator *(const GfMatrix2f& m1, const GfMatrix2f& m2)
309  {
310  GfMatrix2f tmp(m1);
311  tmp *= m2;
312  return tmp;
313  }
314 
315  /// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
316  friend GfMatrix2f operator /(const GfMatrix2f& m1, const GfMatrix2f& m2)
317  {
318  return(m1 * m2.GetInverse());
319  }
320 
321  /// Returns the product of a matrix \e m and a column vector \e vec.
322  friend inline GfVec2f operator *(const GfMatrix2f& m, const GfVec2f& vec) {
323  return GfVec2f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1],
324  vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1]);
325  }
326 
327  /// Returns the product of row vector \e vec and a matrix \e m.
328  friend inline GfVec2f operator *(const GfVec2f &vec, const GfMatrix2f& m) {
329  return GfVec2f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0],
330  vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1]);
331  }
332 
333 
334 private:
335  /// Matrix storage, in row-major order.
337 
338  // Friend declarations
339  friend class GfMatrix2d;
340 };
341 
342 
343 /// Tests for equality within a given tolerance, returning \c true if the
344 /// difference between each component of the matrix is less than or equal
345 /// to \p tolerance, or false otherwise.
346 GF_API
347 bool GfIsClose(GfMatrix2f const &m1, GfMatrix2f const &m2, double tolerance);
348 
349 /// Output a GfMatrix2f
350 /// \ingroup group_gf_DebuggingOutput
351 GF_API std::ostream& operator<<(std::ostream &, GfMatrix2f const &);
352 
354 
355 #endif // PXR_BASE_GF_MATRIX2F_H
GF_API bool GfIsClose(GfMatrix2f const &m1, GfMatrix2f const &m2, double tolerance)
GF_API GfMatrix2f GetInverse(double *det=NULL, double eps=0) const
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
float * operator[](int i)
Definition: matrix2f.h:201
GF_API double GetDeterminant() const
Returns the determinant of the matrix.
GF_API GfMatrix2f & operator-=(const GfMatrix2f &m)
Subtracts matrix m from this matrix.
GfMatrix2f(int s)
Definition: matrix2f.h:79
const GLdouble * v
Definition: glcorearb.h:837
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
static const size_t numRows
Definition: matrix2f.h:49
GfMatrix2f(float s)
Definition: matrix2f.h:73
GLdouble s
Definition: glad.h:3009
GfMatrix2f(float m00, float m01, float m10, float m11)
Definition: matrix2f.h:59
float ScalarType
Definition: matrix2f.h:47
GfMatrix2f & Set(float m00, float m01, float m10, float m11)
Definition: matrix2f.h:136
GfVec2f GetColumn(int i) const
Gets a column of the matrix as a Vec2.
Definition: matrix2f.h:129
GF_API GfMatrix2f & operator+=(const GfMatrix2f &m)
Adds matrix m to this matrix.
void SetColumn(int i, const GfVec2f &v)
Sets a column of the matrix from a Vec2.
Definition: matrix2f.h:118
GF_API bool operator==(const GfMatrix2d &m) const
T * GetData()
Return a pointer to the start of all the data.
Definition: matrixData.h:33
friend GfMatrix2f operator/(const GfMatrix2f &m1, const GfMatrix2f &m2)
Divides matrix m1 by m2 (that is, m1 * inv(m2)).
Definition: matrix2f.h:316
GF_API float * Get(float m[2][2]) const
GfMatrix2f(const float m[2][2])
Definition: matrix2f.h:67
bool operator!=(const GfMatrix2d &m) const
Definition: matrix2f.h:230
friend size_t hash_value(GfMatrix2f const &m)
Hash.
Definition: matrix2f.h:209
GfMatrix2f & SetIdentity()
Sets the matrix to the identity matrix.
Definition: matrix2f.h:154
GF_API friend GfMatrix2f operator-(const GfMatrix2f &m)
Returns the unary negation of matrix m.
GF_API std::ostream & operator<<(std::ostream &, GfMatrix2f const &)
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
GfVec2f GetRow(int i) const
Gets a row of the matrix as a Vec2.
Definition: matrix2f.h:124
float * GetArray()
Returns vector components as an array of float values.
Definition: matrix2f.h:189
GF_API GfMatrix2f GetTranspose() const
Returns the transpose of the matrix.
GfMatrix2f & Set(const float m[2][2])
Definition: matrix2f.h:145
Definition: vec2f.h:45
static const size_t numColumns
Definition: matrix2f.h:50
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GfMatrix2f(const GfVec2f &v)
Definition: matrix2f.h:85
const float * data() const
Definition: matrix2f.h:184
friend GfMatrix2f operator+(const GfMatrix2f &m1, const GfMatrix2f &m2)
Adds matrix m2 to m1.
Definition: matrix2f.h:292
const float * GetArray() const
Returns vector components as a const array of float values.
Definition: matrix2f.h:194
friend GfMatrix2f operator*(const GfMatrix2f &m1, double d)
Returns the product of a matrix and a float.
Definition: matrix2f.h:266
void SetRow(int i, const GfVec2f &v)
Sets a row of the matrix from a Vec2.
Definition: matrix2f.h:112
GfMatrix2f & SetZero()
Sets the matrix to zero.
Definition: matrix2f.h:159
GfMatrix2f()=default
float * data()
Definition: matrix2f.h:178
GF_API GfMatrix2f & SetDiagonal(float s)
Sets the matrix to s times the identity matrix.
#define GF_API
Definition: api.h:23
GF_API GfMatrix2f & operator*=(const GfMatrix2f &m)
Post-multiplies matrix m into this matrix.