matrix_3x3.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "matrix_3x3.h"
  17. namespace cardboard {
  18. Matrix3x3::Matrix3x3(double m00, double m01, double m02, double m10, double m11, double m12,
  19. double m20, double m21, double m22)
  20. : elem_ { { { m00, m01, m02 }, { m10, m11, m12 }, { m20, m21, m22 } } }
  21. {
  22. }
  23. Matrix3x3::Matrix3x3()
  24. {
  25. for (int row = 0; row < 3; ++row) {
  26. for (int col = 0; col < 3; ++col)
  27. elem_[row][col] = 0;
  28. }
  29. }
  30. Matrix3x3 Matrix3x3::Zero()
  31. {
  32. Matrix3x3 result;
  33. return result;
  34. }
  35. Matrix3x3 Matrix3x3::Identity()
  36. {
  37. Matrix3x3 result;
  38. for (int row = 0; row < 3; ++row) {
  39. result.elem_[row][row] = 1;
  40. }
  41. return result;
  42. }
  43. void Matrix3x3::MultiplyScalar(double s)
  44. {
  45. for (int row = 0; row < 3; ++row) {
  46. for (int col = 0; col < 3; ++col)
  47. elem_[row][col] *= s;
  48. }
  49. }
  50. Matrix3x3 Matrix3x3::Negation() const
  51. {
  52. Matrix3x3 result;
  53. for (int row = 0; row < 3; ++row) {
  54. for (int col = 0; col < 3; ++col)
  55. result.elem_[row][col] = -elem_[row][col];
  56. }
  57. return result;
  58. }
  59. Matrix3x3 Matrix3x3::Scale(const Matrix3x3& m, double s)
  60. {
  61. Matrix3x3 result;
  62. for (int row = 0; row < 3; ++row) {
  63. for (int col = 0; col < 3; ++col)
  64. result.elem_[row][col] = m.elem_[row][col] * s;
  65. }
  66. return result;
  67. }
  68. Matrix3x3 Matrix3x3::Addition(const Matrix3x3& lhs, const Matrix3x3& rhs)
  69. {
  70. Matrix3x3 result;
  71. for (int row = 0; row < 3; ++row) {
  72. for (int col = 0; col < 3; ++col)
  73. result.elem_[row][col] = lhs.elem_[row][col] + rhs.elem_[row][col];
  74. }
  75. return result;
  76. }
  77. Matrix3x3 Matrix3x3::Subtraction(const Matrix3x3& lhs, const Matrix3x3& rhs)
  78. {
  79. Matrix3x3 result;
  80. for (int row = 0; row < 3; ++row) {
  81. for (int col = 0; col < 3; ++col)
  82. result.elem_[row][col] = lhs.elem_[row][col] - rhs.elem_[row][col];
  83. }
  84. return result;
  85. }
  86. Matrix3x3 Matrix3x3::Product(const Matrix3x3& m0, const Matrix3x3& m1)
  87. {
  88. Matrix3x3 result;
  89. for (int row = 0; row < 3; ++row) {
  90. for (int col = 0; col < 3; ++col) {
  91. result.elem_[row][col] = 0;
  92. for (int i = 0; i < 3; ++i)
  93. result.elem_[row][col] += m0.elem_[row][i] * m1.elem_[i][col];
  94. }
  95. }
  96. return result;
  97. }
  98. bool Matrix3x3::AreEqual(const Matrix3x3& m0, const Matrix3x3& m1)
  99. {
  100. for (int row = 0; row < 3; ++row) {
  101. for (int col = 0; col < 3; ++col) {
  102. if (m0.elem_[row][col] != m1.elem_[row][col])
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. } // namespace cardboard