matrix_3x3.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2019 Google Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef CARDBOARD_SDK_UTIL_MATRIX_3X3_H_
  17. #define CARDBOARD_SDK_UTIL_MATRIX_3X3_H_
  18. #include <array>
  19. #include <cstring> // For memcpy().
  20. #include <istream> // NOLINT
  21. #include <ostream> // NOLINT
  22. namespace cardboard {
  23. // The Matrix3x3 class defines a square 3-dimensional matrix. Elements are
  24. // stored in row-major order.
  25. // TODO(b/135461889): Make this class consistent with Matrix4x4.
  26. class Matrix3x3 {
  27. public:
  28. // The default constructor zero-initializes all elements.
  29. Matrix3x3();
  30. // Dimension-specific constructors that are passed individual element values.
  31. Matrix3x3(double m00, double m01, double m02, double m10, double m11, double m12, double m20,
  32. double m21, double m22);
  33. // Constructor that reads elements from a linear array of the correct size.
  34. explicit Matrix3x3(const double array[3 * 3]);
  35. // Returns a Matrix3x3 containing all zeroes.
  36. static Matrix3x3 Zero();
  37. // Returns an identity Matrix3x3.
  38. static Matrix3x3 Identity();
  39. // Mutable element accessors.
  40. double& operator()(int row, int col) { return elem_[row][col]; }
  41. std::array<double, 3>& operator[](int row) { return elem_[row]; }
  42. // Read-only element accessors.
  43. const double& operator()(int row, int col) const { return elem_[row][col]; }
  44. const std::array<double, 3>& operator[](int row) const { return elem_[row]; }
  45. // Return a pointer to the data for interfacing with libraries.
  46. double* Data() { return &elem_[0][0]; }
  47. const double* Data() const { return &elem_[0][0]; }
  48. // Self-modifying multiplication operators.
  49. void operator*=(double s) { MultiplyScalar(s); }
  50. void operator*=(const Matrix3x3& m) { *this = Product(*this, m); }
  51. // Unary operators.
  52. Matrix3x3 operator-() const { return Negation(); }
  53. // Binary scale operators.
  54. friend Matrix3x3 operator*(const Matrix3x3& m, double s) { return Scale(m, s); }
  55. friend Matrix3x3 operator*(double s, const Matrix3x3& m) { return Scale(m, s); }
  56. // Binary matrix addition.
  57. friend Matrix3x3 operator+(const Matrix3x3& lhs, const Matrix3x3& rhs)
  58. {
  59. return Addition(lhs, rhs);
  60. }
  61. // Binary matrix subtraction.
  62. friend Matrix3x3 operator-(const Matrix3x3& lhs, const Matrix3x3& rhs)
  63. {
  64. return Subtraction(lhs, rhs);
  65. }
  66. // Binary multiplication operator.
  67. friend Matrix3x3 operator*(const Matrix3x3& m0, const Matrix3x3& m1) { return Product(m0, m1); }
  68. // Exact equality and inequality comparisons.
  69. friend bool operator==(const Matrix3x3& m0, const Matrix3x3& m1) { return AreEqual(m0, m1); }
  70. friend bool operator!=(const Matrix3x3& m0, const Matrix3x3& m1) { return !AreEqual(m0, m1); }
  71. private:
  72. // These private functions implement most of the operators.
  73. void MultiplyScalar(double s);
  74. Matrix3x3 Negation() const;
  75. static Matrix3x3 Addition(const Matrix3x3& lhs, const Matrix3x3& rhs);
  76. static Matrix3x3 Subtraction(const Matrix3x3& lhs, const Matrix3x3& rhs);
  77. static Matrix3x3 Scale(const Matrix3x3& m, double s);
  78. static Matrix3x3 Product(const Matrix3x3& m0, const Matrix3x3& m1);
  79. static bool AreEqual(const Matrix3x3& m0, const Matrix3x3& m1);
  80. std::array<std::array<double, 3>, 3> elem_;
  81. };
  82. } // namespace cardboard
  83. #endif // CARDBOARD_SDK_UTIL_MATRIX_3X3_H_