matrix_3x3.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(
  32. double m00,
  33. double m01,
  34. double m02,
  35. double m10,
  36. double m11,
  37. double m12,
  38. double m20,
  39. double m21,
  40. double m22);
  41. // Constructor that reads elements from a linear array of the correct size.
  42. explicit Matrix3x3(const double array[3 * 3]);
  43. // Returns a Matrix3x3 containing all zeroes.
  44. static Matrix3x3 Zero();
  45. // Returns an identity Matrix3x3.
  46. static Matrix3x3 Identity();
  47. // Mutable element accessors.
  48. double& operator()(int row, int col) {
  49. return elem_[row][col];
  50. }
  51. std::array<double, 3>& operator[](int row) {
  52. return elem_[row];
  53. }
  54. // Read-only element accessors.
  55. const double& operator()(int row, int col) const {
  56. return elem_[row][col];
  57. }
  58. const std::array<double, 3>& operator[](int row) const {
  59. return elem_[row];
  60. }
  61. // Return a pointer to the data for interfacing with libraries.
  62. double* Data() {
  63. return &elem_[0][0];
  64. }
  65. const double* Data() const {
  66. return &elem_[0][0];
  67. }
  68. // Self-modifying multiplication operators.
  69. void operator*=(double s) {
  70. MultiplyScalar(s);
  71. }
  72. void operator*=(const Matrix3x3& m) {
  73. *this = Product(*this, m);
  74. }
  75. // Unary operators.
  76. Matrix3x3 operator-() const {
  77. return Negation();
  78. }
  79. // Binary scale operators.
  80. friend Matrix3x3 operator*(const Matrix3x3& m, double s) {
  81. return Scale(m, s);
  82. }
  83. friend Matrix3x3 operator*(double s, const Matrix3x3& m) {
  84. return Scale(m, s);
  85. }
  86. // Binary matrix addition.
  87. friend Matrix3x3 operator+(const Matrix3x3& lhs, const Matrix3x3& rhs) {
  88. return Addition(lhs, rhs);
  89. }
  90. // Binary matrix subtraction.
  91. friend Matrix3x3 operator-(const Matrix3x3& lhs, const Matrix3x3& rhs) {
  92. return Subtraction(lhs, rhs);
  93. }
  94. // Binary multiplication operator.
  95. friend Matrix3x3 operator*(const Matrix3x3& m0, const Matrix3x3& m1) {
  96. return Product(m0, m1);
  97. }
  98. // Exact equality and inequality comparisons.
  99. friend bool operator==(const Matrix3x3& m0, const Matrix3x3& m1) {
  100. return AreEqual(m0, m1);
  101. }
  102. friend bool operator!=(const Matrix3x3& m0, const Matrix3x3& m1) {
  103. return !AreEqual(m0, m1);
  104. }
  105. private:
  106. // These private functions implement most of the operators.
  107. void MultiplyScalar(double s);
  108. Matrix3x3 Negation() const;
  109. static Matrix3x3 Addition(const Matrix3x3& lhs, const Matrix3x3& rhs);
  110. static Matrix3x3 Subtraction(const Matrix3x3& lhs, const Matrix3x3& rhs);
  111. static Matrix3x3 Scale(const Matrix3x3& m, double s);
  112. static Matrix3x3 Product(const Matrix3x3& m0, const Matrix3x3& m1);
  113. static bool AreEqual(const Matrix3x3& m0, const Matrix3x3& m1);
  114. std::array<std::array<double, 3>, 3> elem_;
  115. };
  116. } // namespace cardboard
  117. #endif // CARDBOARD_SDK_UTIL_MATRIX_3X3_H_