dynamic_compressed_row_sparse_matrix.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: richie.stebbing@gmail.com (Richard Stebbing)
  30. #include "ceres/dynamic_compressed_row_sparse_matrix.h"
  31. #include <cstring>
  32. namespace ceres::internal {
  33. DynamicCompressedRowSparseMatrix::DynamicCompressedRowSparseMatrix(
  34. int num_rows, int num_cols, int initial_max_num_nonzeros)
  35. : CompressedRowSparseMatrix(num_rows, num_cols, initial_max_num_nonzeros) {
  36. dynamic_cols_.resize(num_rows);
  37. dynamic_values_.resize(num_rows);
  38. }
  39. void DynamicCompressedRowSparseMatrix::InsertEntry(int row,
  40. int col,
  41. const double& value) {
  42. CHECK_GE(row, 0);
  43. CHECK_LT(row, num_rows());
  44. CHECK_GE(col, 0);
  45. CHECK_LT(col, num_cols());
  46. dynamic_cols_[row].push_back(col);
  47. dynamic_values_[row].push_back(value);
  48. }
  49. void DynamicCompressedRowSparseMatrix::ClearRows(int row_start, int num_rows) {
  50. for (int r = 0; r < num_rows; ++r) {
  51. const int i = row_start + r;
  52. CHECK_GE(i, 0);
  53. CHECK_LT(i, this->num_rows());
  54. dynamic_cols_[i].resize(0);
  55. dynamic_values_[i].resize(0);
  56. }
  57. }
  58. void DynamicCompressedRowSparseMatrix::Finalize(int num_additional_elements) {
  59. // `num_additional_elements` is provided as an argument so that additional
  60. // storage can be reserved when it is known by the finalizer.
  61. CHECK_GE(num_additional_elements, 0);
  62. // Count the number of non-zeros and resize `cols_` and `values_`.
  63. int num_jacobian_nonzeros = 0;
  64. for (const auto& dynamic_col : dynamic_cols_) {
  65. num_jacobian_nonzeros += dynamic_col.size();
  66. }
  67. SetMaxNumNonZeros(num_jacobian_nonzeros + num_additional_elements);
  68. // Flatten `dynamic_cols_` into `cols_` and `dynamic_values_`
  69. // into `values_`.
  70. int index_into_values_and_cols = 0;
  71. for (int i = 0; i < num_rows(); ++i) {
  72. mutable_rows()[i] = index_into_values_and_cols;
  73. const int num_nonzero_columns = dynamic_cols_[i].size();
  74. if (num_nonzero_columns > 0) {
  75. memcpy(mutable_cols() + index_into_values_and_cols,
  76. &dynamic_cols_[i][0],
  77. dynamic_cols_[i].size() * sizeof(dynamic_cols_[0][0]));
  78. memcpy(mutable_values() + index_into_values_and_cols,
  79. &dynamic_values_[i][0],
  80. dynamic_values_[i].size() * sizeof(dynamic_values_[0][0]));
  81. index_into_values_and_cols += dynamic_cols_[i].size();
  82. }
  83. }
  84. mutable_rows()[num_rows()] = index_into_values_and_cols;
  85. CHECK_EQ(index_into_values_and_cols, num_jacobian_nonzeros)
  86. << "Ceres bug: final index into values_ and cols_ should be equal to "
  87. << "the number of jacobian nonzeros. Please contact the developers!";
  88. }
  89. } // namespace ceres::internal