HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
matrix2d.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_MATRIX2D_H
29 #define PXR_BASE_GF_MATRIX2D_H
30 
31 /// \file gf/matrix2d.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/vec2d.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 GfMatrix2d> { static const bool value = true; };
49 
50 class GfMatrix2d;
51 class GfMatrix2f;
52 
53 /// \class GfMatrix2d
54 /// \ingroup group_gf_LinearAlgebra
55 ///
56 /// Stores a 2x2 matrix of \c double 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 double 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  GfMatrix2d() = default;
71 
72  /// Constructor. Initializes the matrix from 4 independent
73  /// \c double values, specified in row-major order. For example,
74  /// parameter \e m10 specifies the value in row 1 and column 0.
75  GfMatrix2d(double m00, double m01,
76  double m10, double m11) {
77  Set(m00, m01,
78  m10, m11);
79  }
80 
81  /// Constructor. Initializes the matrix from a 2x2 array
82  /// of \c double values, specified in row-major order.
83  GfMatrix2d(const double m[2][2]) {
84  Set(m);
85  }
86 
87  /// Constructor. Explicitly initializes the matrix to \e s times the
88  /// identity matrix.
89  explicit GfMatrix2d(double s) {
90  SetDiagonal(s);
91  }
92 
93  /// This explicit constructor initializes the matrix to \p s times
94  /// the identity matrix.
95  explicit GfMatrix2d(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 GfMatrix2d(const GfVec2d& 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 GfMatrix2d(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 GfMatrix2d(const std::vector< std::vector<float> >& v);
122 
123  /// This explicit constructor converts a "float" matrix to a "double" matrix.
124  GF_API
125  explicit GfMatrix2d(const class GfMatrix2f& m);
126 
127  /// Sets a row of the matrix from a Vec2.
128  void SetRow(int i, const GfVec2d & 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 GfVec2d & 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  GfVec2d GetRow(int i) const {
141  return GfVec2d(_mtx[i][0], _mtx[i][1]);
142  }
143 
144  /// Gets a column of the matrix as a Vec2.
145  GfVec2d GetColumn(int i) const {
146  return GfVec2d(_mtx[0][i], _mtx[1][i]);
147  }
148 
149  /// Sets the matrix from 4 independent \c double values,
150  /// specified in row-major order. For example, parameter \e m10 specifies
151  /// the value in row 1 and column 0.
152  GfMatrix2d& Set(double m00, double m01,
153  double m10, double 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 double
160  /// values, specified in row-major order.
161  GfMatrix2d& Set(const double 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  GfMatrix2d& SetDiagonal(double s);
182 
183  /// Sets the matrix to have diagonal (<c>v[0], v[1]</c>).
184  GF_API
185  GfMatrix2d& SetDiagonal(const GfVec2d&);
186 
187  /// Fills a 2x2 array of \c double values with the values in
188  /// the matrix, specified in row-major order.
189  GF_API
190  double* Get(double m[2][2]) const;
191 
192  /// Returns raw access to components of matrix as an array of
193  /// \c double values. Components are in row-major order.
194  double* data() {
195  return _mtx.GetData();
196  }
197 
198  /// Returns const raw access to components of matrix as an array of
199  /// \c double values. Components are in row-major order.
200  const double* data() const {
201  return _mtx.GetData();
202  }
203 
204  /// Returns vector components as an array of \c double values.
205  double* GetArray() {
206  return _mtx.GetData();
207  }
208 
209  /// Returns vector components as a const array of \c double values.
210  const double* 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  /// double values so that standard indexing (such as <c>m[0][1]</c>)
216  /// works correctly.
217  double* operator [](int i) { return _mtx[i]; }
218 
219  /// Accesses an indexed row \e i of the matrix as an array of 2 \c
220  /// double values so that standard indexing (such as <c>m[0][1]</c>)
221  /// works correctly.
222  const double* operator [](int i) const { return _mtx[i]; }
223 
224  /// Hash.
225  friend inline size_t hash_value(GfMatrix2d 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  GfMatrix2d 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  GfMatrix2d 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  GfMatrix2d& operator *=(const GfMatrix2d& m);
276 
277  /// Multiplies the matrix by a double.
278  GF_API
279  GfMatrix2d& operator *=(double);
280 
281  /// Returns the product of a matrix and a double.
282  friend GfMatrix2d operator *(const GfMatrix2d& m1, double d)
283  {
284  GfMatrix2d m = m1;
285  return m *= d;
286  }
287 
288  ///
289  // Returns the product of a matrix and a double.
290  friend GfMatrix2d operator *(double d, const GfMatrix2d& m)
291  {
292  return m * d;
293  }
294 
295  /// Adds matrix \e m to this matrix.
296  GF_API
297  GfMatrix2d& operator +=(const GfMatrix2d& m);
298 
299  /// Subtracts matrix \e m from this matrix.
300  GF_API
301  GfMatrix2d& operator -=(const GfMatrix2d& m);
302 
303  /// Returns the unary negation of matrix \e m.
304  GF_API
305  friend GfMatrix2d operator -(const GfMatrix2d& m);
306 
307  /// Adds matrix \e m2 to \e m1
308  friend GfMatrix2d operator +(const GfMatrix2d& m1, const GfMatrix2d& m2)
309  {
310  GfMatrix2d tmp(m1);
311  tmp += m2;
312  return tmp;
313  }
314 
315  /// Subtracts matrix \e m2 from \e m1.
316  friend GfMatrix2d operator -(const GfMatrix2d& m1, const GfMatrix2d& m2)
317  {
318  GfMatrix2d tmp(m1);
319  tmp -= m2;
320  return tmp;
321  }
322 
323  /// Multiplies matrix \e m1 by \e m2.
324  friend GfMatrix2d operator *(const GfMatrix2d& m1, const GfMatrix2d& m2)
325  {
326  GfMatrix2d 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 GfMatrix2d operator /(const GfMatrix2d& m1, const GfMatrix2d& 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 GfVec2d operator *(const GfMatrix2d& m, const GfVec2d& vec) {
339  return GfVec2d(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 GfVec2d operator *(const GfVec2d &vec, const GfMatrix2d& m) {
345  return GfVec2d(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  /// Returns the product of a matrix \e m and a column vector \e vec.
350  /// Note that the return type is a \c GfVec2f.
351  GF_API
352  friend GfVec2f operator *(const GfMatrix2d& m, const GfVec2f& vec);
353 
354  /// Returns the product of row vector \e vec and a matrix \e m.
355  /// Note that the return type is a \c GfVec2f.
356  GF_API
357  friend GfVec2f operator *(const GfVec2f &vec, const GfMatrix2d& m);
358 
359 
360 private:
361  /// Matrix storage, in row-major order.
363 
364  // Friend declarations
365  friend class GfMatrix2f;
366 };
367 
368 
369 /// Tests for equality within a given tolerance, returning \c true if the
370 /// difference between each component of the matrix is less than or equal
371 /// to \p tolerance, or false otherwise.
372 GF_API
373 bool GfIsClose(GfMatrix2d const &m1, GfMatrix2d const &m2, double tolerance);
374 
375 /// Output a GfMatrix2d
376 /// \ingroup group_gf_DebuggingOutput
377 GF_API std::ostream& operator<<(std::ostream &, GfMatrix2d const &);
378 
380 
381 #endif // PXR_BASE_GF_MATRIX2D_H
GfMatrix2d(const GfVec2d &v)
Definition: matrix2d.h:101
bool operator!=(const GfMatrix2d &m) const
Definition: matrix2d.h:246
double ScalarType
Definition: matrix2d.h:64
GF_API GfMatrix2d & SetDiagonal(double s)
Sets the matrix to s times the identity matrix.
GfVec2d GetRow(int i) const
Gets a row of the matrix as a Vec2.
Definition: matrix2d.h:140
double * data()
Definition: matrix2d.h:194
double * operator[](int i)
Definition: matrix2d.h:217
*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
GF_API double GetDeterminant() const
Returns the determinant of the matrix.
const GLdouble * v
Definition: glcorearb.h:837
GLdouble s
Definition: glad.h:3009
static const size_t numColumns
Definition: matrix2d.h:67
GF_API GfMatrix2d & operator+=(const GfMatrix2d &m)
Adds matrix m to this matrix.
GF_API GfMatrix2d & operator*=(const GfMatrix2d &m)
Post-multiplies matrix m into this matrix.
GfMatrix2d(double m00, double m01, double m10, double m11)
Definition: matrix2d.h:75
friend size_t hash_value(GfMatrix2d const &m)
Hash.
Definition: matrix2d.h:225
Definition: vec2d.h:62
GfMatrix2d(int s)
Definition: matrix2d.h:95
void SetRow(int i, const GfVec2d &v)
Sets a row of the matrix from a Vec2.
Definition: matrix2d.h:128
GfMatrix2d & Set(double m00, double m01, double m10, double m11)
Definition: matrix2d.h:152
T * GetData()
Return a pointer to the start of all the data.
Definition: matrixData.h:50
GfMatrix2d(const double m[2][2])
Definition: matrix2d.h:83
GfMatrix2d & Set(const double m[2][2])
Definition: matrix2d.h:161
GF_API GfMatrix2d & operator-=(const GfMatrix2d &m)
Subtracts matrix m from this matrix.
GF_API double * Get(double m[2][2]) const
GfVec2d GetColumn(int i) const
Gets a column of the matrix as a Vec2.
Definition: matrix2d.h:145
friend GfMatrix2d operator*(const GfMatrix2d &m1, double d)
Returns the product of a matrix and a double.
Definition: matrix2d.h:282
const double * GetArray() const
Returns vector components as a const array of double values.
Definition: matrix2d.h:210
GfMatrix2d & SetIdentity()
Sets the matrix to the identity matrix.
Definition: matrix2d.h:170
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
GfMatrix2d()=default
Default constructor. Leaves the matrix component values undefined.
GF_API std::ostream & operator<<(std::ostream &, GfMatrix2d const &)
GfMatrix2d & SetZero()
Sets the matrix to zero.
Definition: matrix2d.h:175
GF_API GfMatrix2d GetInverse(double *det=NULL, double eps=0) const
Definition: vec2f.h:62
static const size_t numRows
Definition: matrix2d.h:66
friend GfMatrix2d operator/(const GfMatrix2d &m1, const GfMatrix2d &m2)
Divides matrix m1 by m2 (that is, m1 * inv(m2)).
Definition: matrix2d.h:332
GF_API GfMatrix2d GetTranspose() const
Returns the transpose of the matrix.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
friend GfMatrix2d operator+(const GfMatrix2d &m1, const GfMatrix2d &m2)
Adds matrix m2 to m1.
Definition: matrix2d.h:308
GfMatrix2d(double s)
Definition: matrix2d.h:89
Definition: core.h:1131
void SetColumn(int i, const GfVec2d &v)
Sets a column of the matrix from a Vec2.
Definition: matrix2d.h:134
double * GetArray()
Returns vector components as an array of double values.
Definition: matrix2d.h:205
GF_API bool GfIsClose(GfMatrix2d const &m1, GfMatrix2d const &m2, double tolerance)
const double * data() const
Definition: matrix2d.h:200
GF_API bool operator==(const GfMatrix2d &m) const
#define GF_API
Definition: api.h:40
GF_API friend GfMatrix2d operator-(const GfMatrix2d &m)
Returns the unary negation of matrix m.