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 
41 #include <hboost/functional/hash.hpp>
42 
43 #include <iosfwd>
44 #include <vector>
45 
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 ///
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.
172  return SetDiagonal(1);
173  }
174 
175  /// Sets the matrix to zero.
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  hboost::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
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
299 
300  /// Subtracts matrix \e m from this matrix.
301  GF_API
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.
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 
371 
372 #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:629
float * operator[](int i)
Definition: matrix2f.h:218
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:96
static const size_t numRows
Definition: matrix2f.h:67
GfMatrix2f(float s)
Definition: matrix2f.h:90
GfMatrix2f(float m00, float m01, float m10, float m11)
Definition: matrix2f.h:76
float ScalarType
Definition: matrix2f.h:65
GfMatrix2f & Set(float m00, float m01, float m10, float m11)
Definition: matrix2f.h:153
GfVec2f GetColumn(int i) const
Gets a column of the matrix as a Vec2.
Definition: matrix2f.h:146
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:135
GF_API bool operator==(const GfMatrix2d &m) const
T * GetData()
Return a pointer to the start of all the data.
Definition: matrixData.h:50
const GLdouble * v
Definition: glcorearb.h:836
friend GfMatrix2f operator/(const GfMatrix2f &m1, const GfMatrix2f &m2)
Divides matrix m1 by m2 (that is, m1 * inv(m2)).
Definition: matrix2f.h:333
GF_API float * Get(float m[2][2]) const
GfMatrix2f(const float m[2][2])
Definition: matrix2f.h:84
bool operator!=(const GfMatrix2d &m) const
Definition: matrix2f.h:247
GLfloat GLfloat p
Definition: glew.h:16656
friend size_t hash_value(GfMatrix2f const &m)
Hash.
Definition: matrix2f.h:226
GfMatrix2f & SetIdentity()
Sets the matrix to the identity matrix.
Definition: matrix2f.h:171
GF_API friend GfMatrix2f operator-(const GfMatrix2f &m)
Returns the unary negation of matrix m.
GF_API std::ostream & operator<<(std::ostream &, GfMatrix2f const &)
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
GfVec2f GetRow(int i) const
Gets a row of the matrix as a Vec2.
Definition: matrix2f.h:141
float * GetArray()
Returns vector components as an array of float values.
Definition: matrix2f.h:206
GF_API GfMatrix2f GetTranspose() const
Returns the transpose of the matrix.
GfMatrix2f & Set(const float m[2][2])
Definition: matrix2f.h:162
Definition: vec2f.h:63
static const size_t numColumns
Definition: matrix2f.h:68
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GfMatrix2f(const GfVec2f &v)
Definition: matrix2f.h:102
const GLdouble * m
Definition: glew.h:9166
GLsizei const GLfloat * value
Definition: glcorearb.h:823
const float * data() const
Definition: matrix2f.h:201
friend GfMatrix2f operator+(const GfMatrix2f &m1, const GfMatrix2f &m2)
Adds matrix m2 to m1.
Definition: matrix2f.h:309
const float * GetArray() const
Returns vector components as a const array of float values.
Definition: matrix2f.h:211
friend GfMatrix2f operator*(const GfMatrix2f &m1, double d)
Returns the product of a matrix and a float.
Definition: matrix2f.h:283
void SetRow(int i, const GfVec2f &v)
Sets a row of the matrix from a Vec2.
Definition: matrix2f.h:129
GfMatrix2f & SetZero()
Sets the matrix to zero.
Definition: matrix2f.h:176
GfMatrix2f()=default
Default constructor. Leaves the matrix component values undefined.
GLdouble s
Definition: glew.h:1395
float * data()
Definition: matrix2f.h:195
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.