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  /// Default constructor. Leaves the matrix component values undefined.
53  GfMatrix2f() = default;
54 
55  /// Constructor. Initializes the matrix from 4 independent
56  /// \c float values, specified in row-major order. For example,
57  /// parameter \e m10 specifies the value in row 1 and column 0.
58  GfMatrix2f(float m00, float m01,
59  float m10, float m11) {
60  Set(m00, m01,
61  m10, m11);
62  }
63 
64  /// Constructor. Initializes the matrix from a 2x2 array
65  /// of \c float values, specified in row-major order.
66  GfMatrix2f(const float m[2][2]) {
67  Set(m);
68  }
69 
70  /// Constructor. Explicitly initializes the matrix to \e s times the
71  /// identity matrix.
72  explicit GfMatrix2f(float s) {
73  SetDiagonal(s);
74  }
75 
76  /// This explicit constructor initializes the matrix to \p s times
77  /// the identity matrix.
78  explicit GfMatrix2f(int s) {
79  SetDiagonal(s);
80  }
81 
82  /// Constructor. Explicitly initializes the matrix to diagonal form,
83  /// with the \e i th element on the diagonal set to <c>v[i]</c>.
84  explicit GfMatrix2f(const GfVec2f& v) {
85  SetDiagonal(v);
86  }
87 
88  /// Constructor. Initialize the matrix from a vector of vectors of
89  /// double. The vector is expected to be 2x2. If it is
90  /// too big, only the first 2 rows and/or columns will be used.
91  /// If it is too small, uninitialized elements will be filled in with
92  /// the corresponding elements from an identity matrix.
93  ///
94  GF_API
95  explicit GfMatrix2f(const std::vector< std::vector<double> >& v);
96 
97  /// Constructor. Initialize the matrix from a vector of vectors of
98  /// float. The vector is expected to be 2x2. If it is
99  /// too big, only the first 2 rows and/or columns will be used.
100  /// If it is too small, uninitialized elements will be filled in with
101  /// the corresponding elements from an identity matrix.
102  ///
103  GF_API
104  explicit GfMatrix2f(const std::vector< std::vector<float> >& v);
105 
106  /// This explicit constructor converts a "double" matrix to a "float" matrix.
107  GF_API
108  explicit GfMatrix2f(const class GfMatrix2d& m);
109 
110  /// Sets a row of the matrix from a Vec2.
111  void SetRow(int i, const GfVec2f & v) {
112  _mtx[i][0] = v[0];
113  _mtx[i][1] = v[1];
114  }
115 
116  /// Sets a column of the matrix from a Vec2.
117  void SetColumn(int i, const GfVec2f & v) {
118  _mtx[0][i] = v[0];
119  _mtx[1][i] = v[1];
120  }
121 
122  /// Gets a row of the matrix as a Vec2.
123  GfVec2f GetRow(int i) const {
124  return GfVec2f(_mtx[i][0], _mtx[i][1]);
125  }
126 
127  /// Gets a column of the matrix as a Vec2.
128  GfVec2f GetColumn(int i) const {
129  return GfVec2f(_mtx[0][i], _mtx[1][i]);
130  }
131 
132  /// Sets the matrix from 4 independent \c float values,
133  /// specified in row-major order. For example, parameter \e m10 specifies
134  /// the value in row 1 and column 0.
135  GfMatrix2f& Set(float m00, float m01,
136  float m10, float m11) {
137  _mtx[0][0] = m00; _mtx[0][1] = m01;
138  _mtx[1][0] = m10; _mtx[1][1] = m11;
139  return *this;
140  }
141 
142  /// Sets the matrix from a 2x2 array of \c float
143  /// values, specified in row-major order.
144  GfMatrix2f& Set(const float m[2][2]) {
145  _mtx[0][0] = m[0][0];
146  _mtx[0][1] = m[0][1];
147  _mtx[1][0] = m[1][0];
148  _mtx[1][1] = m[1][1];
149  return *this;
150  }
151 
152  /// Sets the matrix to the identity matrix.
154  return SetDiagonal(1);
155  }
156 
157  /// Sets the matrix to zero.
159  return SetDiagonal(0);
160  }
161 
162  /// Sets the matrix to \e s times the identity matrix.
163  GF_API
164  GfMatrix2f& SetDiagonal(float s);
165 
166  /// Sets the matrix to have diagonal (<c>v[0], v[1]</c>).
167  GF_API
168  GfMatrix2f& SetDiagonal(const GfVec2f&);
169 
170  /// Fills a 2x2 array of \c float values with the values in
171  /// the matrix, specified in row-major order.
172  GF_API
173  float* Get(float m[2][2]) const;
174 
175  /// Returns raw access to components of matrix as an array of
176  /// \c float values. Components are in row-major order.
177  float* data() {
178  return _mtx.GetData();
179  }
180 
181  /// Returns const raw access to components of matrix as an array of
182  /// \c float values. Components are in row-major order.
183  const float* data() const {
184  return _mtx.GetData();
185  }
186 
187  /// Returns vector components as an array of \c float values.
188  float* GetArray() {
189  return _mtx.GetData();
190  }
191 
192  /// Returns vector components as a const array of \c float values.
193  const float* GetArray() const {
194  return _mtx.GetData();
195  }
196 
197  /// Accesses an indexed row \e i of the matrix as an array of 2 \c
198  /// float values so that standard indexing (such as <c>m[0][1]</c>)
199  /// works correctly.
200  float* operator [](int i) { return _mtx[i]; }
201 
202  /// Accesses an indexed row \e i of the matrix as an array of 2 \c
203  /// float values so that standard indexing (such as <c>m[0][1]</c>)
204  /// works correctly.
205  const float* operator [](int i) const { return _mtx[i]; }
206 
207  /// Hash.
208  friend inline size_t hash_value(GfMatrix2f const &m) {
209  return TfHash::Combine(
210  m._mtx[0][0],
211  m._mtx[0][1],
212  m._mtx[1][0],
213  m._mtx[1][1]
214  );
215  }
216 
217  /// Tests for element-wise matrix equality. All elements must match
218  /// exactly for matrices to be considered equal.
219  GF_API
220  bool operator ==(const GfMatrix2d& m) const;
221 
222  /// Tests for element-wise matrix equality. All elements must match
223  /// exactly for matrices to be considered equal.
224  GF_API
225  bool operator ==(const GfMatrix2f& m) const;
226 
227  /// Tests for element-wise matrix inequality. All elements must match
228  /// exactly for matrices to be considered equal.
229  bool operator !=(const GfMatrix2d& m) const {
230  return !(*this == m);
231  }
232 
233  /// Tests for element-wise matrix inequality. All elements must match
234  /// exactly for matrices to be considered equal.
235  bool operator !=(const GfMatrix2f& m) const {
236  return !(*this == m);
237  }
238 
239  /// Returns the transpose of the matrix.
240  GF_API
241  GfMatrix2f GetTranspose() const;
242 
243  /// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
244  /// matrix is singular. (FLT_MAX is the largest value a \c float can have,
245  /// as defined by the system.) The matrix is considered singular if the
246  /// determinant is less than or equal to the optional parameter \e eps. If
247  /// \e det is non-null, <c>*det</c> is set to the determinant.
248  GF_API
249  GfMatrix2f GetInverse(double* det = NULL, double eps = 0) const;
250 
251  /// Returns the determinant of the matrix.
252  GF_API
253  double GetDeterminant() const;
254 
255 
256  /// Post-multiplies matrix \e m into this matrix.
257  GF_API
258  GfMatrix2f& operator *=(const GfMatrix2f& m);
259 
260  /// Multiplies the matrix by a float.
261  GF_API
262  GfMatrix2f& operator *=(double);
263 
264  /// Returns the product of a matrix and a float.
265  friend GfMatrix2f operator *(const GfMatrix2f& m1, double d)
266  {
267  GfMatrix2f m = m1;
268  return m *= d;
269  }
270 
271  ///
272  // Returns the product of a matrix and a float.
273  friend GfMatrix2f operator *(double d, const GfMatrix2f& m)
274  {
275  return m * d;
276  }
277 
278  /// Adds matrix \e m to this matrix.
279  GF_API
280  GfMatrix2f& operator +=(const GfMatrix2f& m);
281 
282  /// Subtracts matrix \e m from this matrix.
283  GF_API
284  GfMatrix2f& operator -=(const GfMatrix2f& m);
285 
286  /// Returns the unary negation of matrix \e m.
287  GF_API
288  friend GfMatrix2f operator -(const GfMatrix2f& m);
289 
290  /// Adds matrix \e m2 to \e m1
291  friend GfMatrix2f operator +(const GfMatrix2f& m1, const GfMatrix2f& m2)
292  {
293  GfMatrix2f tmp(m1);
294  tmp += m2;
295  return tmp;
296  }
297 
298  /// Subtracts matrix \e m2 from \e m1.
299  friend GfMatrix2f operator -(const GfMatrix2f& m1, const GfMatrix2f& m2)
300  {
301  GfMatrix2f tmp(m1);
302  tmp -= m2;
303  return tmp;
304  }
305 
306  /// Multiplies matrix \e m1 by \e m2.
307  friend GfMatrix2f operator *(const GfMatrix2f& m1, const GfMatrix2f& m2)
308  {
309  GfMatrix2f tmp(m1);
310  tmp *= m2;
311  return tmp;
312  }
313 
314  /// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
315  friend GfMatrix2f operator /(const GfMatrix2f& m1, const GfMatrix2f& m2)
316  {
317  return(m1 * m2.GetInverse());
318  }
319 
320  /// Returns the product of a matrix \e m and a column vector \e vec.
321  friend inline GfVec2f operator *(const GfMatrix2f& m, const GfVec2f& vec) {
322  return GfVec2f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1],
323  vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1]);
324  }
325 
326  /// Returns the product of row vector \e vec and a matrix \e m.
327  friend inline GfVec2f operator *(const GfVec2f &vec, const GfMatrix2f& m) {
328  return GfVec2f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0],
329  vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1]);
330  }
331 
332 
333 private:
334  /// Matrix storage, in row-major order.
336 
337  // Friend declarations
338  friend class GfMatrix2d;
339 };
340 
341 
342 /// Tests for equality within a given tolerance, returning \c true if the
343 /// difference between each component of the matrix is less than or equal
344 /// to \p tolerance, or false otherwise.
345 GF_API
346 bool GfIsClose(GfMatrix2f const &m1, GfMatrix2f const &m2, double tolerance);
347 
348 /// Output a GfMatrix2f
349 /// \ingroup group_gf_DebuggingOutput
350 GF_API std::ostream& operator<<(std::ostream &, GfMatrix2f const &);
351 
353 
354 #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:200
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:78
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
static const size_t numRows
Definition: matrix2f.h:49
GfMatrix2f(float s)
Definition: matrix2f.h:72
GLdouble s
Definition: glad.h:3009
GfMatrix2f(float m00, float m01, float m10, float m11)
Definition: matrix2f.h:58
float ScalarType
Definition: matrix2f.h:47
GfMatrix2f & Set(float m00, float m01, float m10, float m11)
Definition: matrix2f.h:135
GfVec2f GetColumn(int i) const
Gets a column of the matrix as a Vec2.
Definition: matrix2f.h:128
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:117
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:315
GF_API float * Get(float m[2][2]) const
GfMatrix2f(const float m[2][2])
Definition: matrix2f.h:66
bool operator!=(const GfMatrix2d &m) const
Definition: matrix2f.h:229
friend size_t hash_value(GfMatrix2f const &m)
Hash.
Definition: matrix2f.h:208
GfMatrix2f & SetIdentity()
Sets the matrix to the identity matrix.
Definition: matrix2f.h:153
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
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
GfVec2f GetRow(int i) const
Gets a row of the matrix as a Vec2.
Definition: matrix2f.h:123
float * GetArray()
Returns vector components as an array of float values.
Definition: matrix2f.h:188
GF_API GfMatrix2f GetTranspose() const
Returns the transpose of the matrix.
GfMatrix2f & Set(const float m[2][2])
Definition: matrix2f.h:144
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:84
const float * data() const
Definition: matrix2f.h:183
friend GfMatrix2f operator+(const GfMatrix2f &m1, const GfMatrix2f &m2)
Adds matrix m2 to m1.
Definition: matrix2f.h:291
const float * GetArray() const
Returns vector components as a const array of float values.
Definition: matrix2f.h:193
friend GfMatrix2f operator*(const GfMatrix2f &m1, double d)
Returns the product of a matrix and a float.
Definition: matrix2f.h:265
void SetRow(int i, const GfVec2f &v)
Sets a row of the matrix from a Vec2.
Definition: matrix2f.h:111
GfMatrix2f & SetZero()
Sets the matrix to zero.
Definition: matrix2f.h:158
GfMatrix2f()=default
Default constructor. Leaves the matrix component values undefined.
float * data()
Definition: matrix2f.h:177
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.