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