detect_structure.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 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: sameeragarwal@google.com (Sameer Agarwal)
  30. #include "ceres/detect_structure.h"
  31. #include "ceres/internal/eigen.h"
  32. #include "glog/logging.h"
  33. namespace ceres::internal {
  34. void DetectStructure(const CompressedRowBlockStructure& bs,
  35. const int num_eliminate_blocks,
  36. int* row_block_size,
  37. int* e_block_size,
  38. int* f_block_size) {
  39. const int num_row_blocks = bs.rows.size();
  40. *row_block_size = 0;
  41. *e_block_size = 0;
  42. *f_block_size = 0;
  43. // Iterate over row blocks of the matrix, checking if row_block,
  44. // e_block or f_block sizes remain constant.
  45. for (int r = 0; r < num_row_blocks; ++r) {
  46. const CompressedRow& row = bs.rows[r];
  47. // We do not care about the sizes of the blocks in rows which do
  48. // not contain e_blocks.
  49. if (row.cells.front().block_id >= num_eliminate_blocks) {
  50. break;
  51. }
  52. // Detect fixed or dynamic row block size.
  53. if (*row_block_size == 0) {
  54. *row_block_size = row.block.size;
  55. } else if (*row_block_size != Eigen::Dynamic &&
  56. *row_block_size != row.block.size) {
  57. VLOG(2) << "Dynamic row block size because the block size changed from "
  58. << *row_block_size << " to " << row.block.size;
  59. *row_block_size = Eigen::Dynamic;
  60. }
  61. // Detect fixed or dynamic e-block size.
  62. const int e_block_id = row.cells.front().block_id;
  63. if (*e_block_size == 0) {
  64. *e_block_size = bs.cols[e_block_id].size;
  65. } else if (*e_block_size != Eigen::Dynamic &&
  66. *e_block_size != bs.cols[e_block_id].size) {
  67. VLOG(2) << "Dynamic e block size because the block size changed from "
  68. << *e_block_size << " to " << bs.cols[e_block_id].size;
  69. *e_block_size = Eigen::Dynamic;
  70. }
  71. // Detect fixed or dynamic f-block size. We are only interested in
  72. // rows with e-blocks, and the e-block is always the first block,
  73. // so only rows of size greater than 1 are of interest.
  74. if (row.cells.size() > 1) {
  75. if (*f_block_size == 0) {
  76. const int f_block_id = row.cells[1].block_id;
  77. *f_block_size = bs.cols[f_block_id].size;
  78. }
  79. for (int c = 1;
  80. (c < row.cells.size()) && (*f_block_size != Eigen::Dynamic);
  81. ++c) {
  82. const int f_block_id = row.cells[c].block_id;
  83. if (*f_block_size != bs.cols[f_block_id].size) {
  84. VLOG(2) << "Dynamic f block size because the block size "
  85. << "changed from " << *f_block_size << " to "
  86. << bs.cols[f_block_id].size;
  87. *f_block_size = Eigen::Dynamic;
  88. }
  89. }
  90. }
  91. // clang-format off
  92. const bool is_everything_dynamic = (*row_block_size == Eigen::Dynamic &&
  93. *e_block_size == Eigen::Dynamic &&
  94. *f_block_size == Eigen::Dynamic);
  95. // clang-format on
  96. if (is_everything_dynamic) {
  97. break;
  98. }
  99. }
  100. CHECK_NE(*row_block_size, 0) << "No rows found";
  101. CHECK_NE(*e_block_size, 0) << "No e type blocks found";
  102. // clang-format off
  103. VLOG(1) << "Schur complement static structure <"
  104. << *row_block_size << ","
  105. << *e_block_size << ","
  106. << *f_block_size << ">.";
  107. // clang-format on
  108. }
  109. } // namespace ceres::internal