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 41 #include <boost/functional/hash.hpp> 42 43 #include <iosfwd> 44 #include <vector> 45 46 PXR_NAMESPACE_OPEN_SCOPE 47 48 template <> 49 struct GfIsGfMatrix<class GfMatrix2d> { static const bool value = true; }; 50 51 class GfMatrix2d; 52 class GfMatrix2f; 53 54 /// \class GfMatrix2d 55 /// \ingroup group_gf_LinearAlgebra 56 /// 57 /// Stores a 2x2 matrix of \c double 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 /// 62 class GfMatrix2d 63 { 64 public: 65 typedef double 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 GfMatrix2d() = default; 72 73 /// Constructor. Initializes the matrix from 4 independent 74 /// \c double values, specified in row-major order. For example, 75 /// parameter \e m10 specifies the value in row 1 and column 0. 76 GfMatrix2d(double m00, double m01, 77 double m10, double m11) { 78 Set(m00, m01, 79 m10, m11); 80 } 81 82 /// Constructor. Initializes the matrix from a 2x2 array 83 /// of \c double values, specified in row-major order. 84 GfMatrix2d(const double m[2][2]) { 85 Set(m); 86 } 87 88 /// Constructor. Explicitly initializes the matrix to \e s times the 89 /// identity matrix. 90 explicit GfMatrix2d(double s) { 91 SetDiagonal(s); 92 } 93 94 /// This explicit constructor initializes the matrix to \p s times 95 /// the identity matrix. 96 explicit GfMatrix2d(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 GfMatrix2d(const GfVec2d& 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 GfMatrix2d(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 GfMatrix2d(const std::vector< std::vector<float> >& v); 123 124 /// This explicit constructor converts a "float" matrix to a "double" matrix. 125 GF_API 126 explicit GfMatrix2d(const class GfMatrix2f& m); 127 128 /// Sets a row of the matrix from a Vec2. 129 void SetRow(int i, const GfVec2d & 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 GfVec2d & 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 GfVec2d GetRow(int i) const { 142 return GfVec2d(_mtx[i][0], _mtx[i][1]); 143 } 144 145 /// Gets a column of the matrix as a Vec2. 146 GfVec2d GetColumn(int i) const { 147 return GfVec2d(_mtx[0][i], _mtx[1][i]); 148 } 149 150 /// Sets the matrix from 4 independent \c double values, 151 /// specified in row-major order. For example, parameter \e m10 specifies 152 /// the value in row 1 and column 0. 153 GfMatrix2d& Set(double m00, double m01, 154 double m10, double 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 double 161 /// values, specified in row-major order. 162 GfMatrix2d& Set(const double 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. 171 GfMatrix2d& SetIdentity() { 172 return SetDiagonal(1); 173 } 174 175 /// Sets the matrix to zero. 176 GfMatrix2d& SetZero() { 177 return SetDiagonal(0); 178 } 179 180 /// Sets the matrix to \e s times the identity matrix. 181 GF_API 182 GfMatrix2d& SetDiagonal(double s); 183 184 /// Sets the matrix to have diagonal (<c>v[0], v[1]</c>). 185 GF_API 186 GfMatrix2d& SetDiagonal(const GfVec2d&); 187 188 /// Fills a 2x2 array of \c double values with the values in 189 /// the matrix, specified in row-major order. 190 GF_API 191 double* Get(double m[2][2]) const; 192 193 /// Returns raw access to components of matrix as an array of 194 /// \c double values. Components are in row-major order. 195 double* data() { 196 return _mtx.GetData(); 197 } 198 199 /// Returns const raw access to components of matrix as an array of 200 /// \c double values. Components are in row-major order. 201 const double* data() const { 202 return _mtx.GetData(); 203 } 204 205 /// Returns vector components as an array of \c double values. 206 double* GetArray() { 207 return _mtx.GetData(); 208 } 209 210 /// Returns vector components as a const array of \c double values. 211 const double* 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 /// double values so that standard indexing (such as <c>m[0][1]</c>) 217 /// works correctly. 218 double* operator [](int i) { return _mtx[i]; } 219 220 /// Accesses an indexed row \e i of the matrix as an array of 2 \c 221 /// double values so that standard indexing (such as <c>m[0][1]</c>) 222 /// works correctly. 223 const double* operator [](int i) const { return _mtx[i]; } 224 225 /// Hash. 226 friend inline size_t hash_value(GfMatrix2d const &m) { 227 int nElems = 2 * 2; 228 size_t h = 0; 229 const double *p = m.GetArray(); 230 while (nElems--) 231 boost::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 GfMatrix2d 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 GfMatrix2d 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 276 GfMatrix2d& operator *=(const GfMatrix2d& m); 277 278 /// Multiplies the matrix by a double. 279 GF_API 280 GfMatrix2d& operator *=(double); 281 282 /// Returns the product of a matrix and a double. 283 friend GfMatrix2d operator *(const GfMatrix2d& m1, double d) 284 { 285 GfMatrix2d m = m1; 286 return m *= d; 287 } 288 289 /// 290 // Returns the product of a matrix and a double. 291 friend GfMatrix2d operator *(double d, const GfMatrix2d& m) 292 { 293 return m * d; 294 } 295 296 /// Adds matrix \e m to this matrix. 297 GF_API 298 GfMatrix2d& operator +=(const GfMatrix2d& m); 299 300 /// Subtracts matrix \e m from this matrix. 301 GF_API 302 GfMatrix2d& operator -=(const GfMatrix2d& m); 303 304 /// Returns the unary negation of matrix \e m. 305 GF_API 306 friend GfMatrix2d operator -(const GfMatrix2d& m); 307 308 /// Adds matrix \e m2 to \e m1 309 friend GfMatrix2d operator +(const GfMatrix2d& m1, const GfMatrix2d& m2) 310 { 311 GfMatrix2d tmp(m1); 312 tmp += m2; 313 return tmp; 314 } 315 316 /// Subtracts matrix \e m2 from \e m1. 317 friend GfMatrix2d operator -(const GfMatrix2d& m1, const GfMatrix2d& m2) 318 { 319 GfMatrix2d tmp(m1); 320 tmp -= m2; 321 return tmp; 322 } 323 324 /// Multiplies matrix \e m1 by \e m2. 325 friend GfMatrix2d operator *(const GfMatrix2d& m1, const GfMatrix2d& m2) 326 { 327 GfMatrix2d 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 GfMatrix2d operator /(const GfMatrix2d& m1, const GfMatrix2d& 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 GfVec2d operator *(const GfMatrix2d& m, const GfVec2d& vec) { 340 return GfVec2d(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 GfVec2d operator *(const GfVec2d &vec, const GfMatrix2d& m) { 346 return GfVec2d(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 /// Returns the product of a matrix \e m and a column vector \e vec. 351 /// Note that the return type is a \c GfVec2f. 352 GF_API 353 friend GfVec2f operator *(const GfMatrix2d& m, const GfVec2f& vec); 354 355 /// Returns the product of row vector \e vec and a matrix \e m. 356 /// Note that the return type is a \c GfVec2f. 357 GF_API 358 friend GfVec2f operator *(const GfVec2f &vec, const GfMatrix2d& m); 359 360 361 private: 362 /// Matrix storage, in row-major order. 363 GfMatrixData<double, 2, 2> _mtx; 364 365 // Friend declarations 366 friend class GfMatrix2f; 367 }; 368 369 370 /// Tests for equality within a given tolerance, returning \c true if the 371 /// difference between each component of the matrix is less than or equal 372 /// to \p tolerance, or false otherwise. 373 GF_API 374 bool GfIsClose(GfMatrix2d const &m1, GfMatrix2d const &m2, double tolerance); 375 376 /// Output a GfMatrix2d 377 /// \ingroup group_gf_DebuggingOutput 378 GF_API std::ostream& operator<<(std::ostream &, GfMatrix2d const &); 379 380 PXR_NAMESPACE_CLOSE_SCOPE 381 382 #endif // PXR_BASE_GF_MATRIX2D_H 383