fake_bundle_adjustment_jacobian.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // Authors: joydeepb@cs.utexas.edu (Joydeep Biswas)
  30. #include "ceres/fake_bundle_adjustment_jacobian.h"
  31. #include <memory>
  32. #include <random>
  33. #include <string>
  34. #include <utility>
  35. #include "Eigen/Dense"
  36. #include "ceres/block_sparse_matrix.h"
  37. #include "ceres/internal/eigen.h"
  38. namespace ceres::internal {
  39. std::unique_ptr<BlockSparseMatrix> CreateFakeBundleAdjustmentJacobian(
  40. int num_cameras,
  41. int num_points,
  42. int camera_size,
  43. int point_size,
  44. double visibility,
  45. std::mt19937& prng) {
  46. constexpr int kResidualSize = 2;
  47. CompressedRowBlockStructure* bs = new CompressedRowBlockStructure;
  48. int c = 0;
  49. // Add column blocks for each point
  50. for (int i = 0; i < num_points; ++i) {
  51. bs->cols.push_back(Block(point_size, c));
  52. c += point_size;
  53. }
  54. // Add column blocks for each camera.
  55. for (int i = 0; i < num_cameras; ++i) {
  56. bs->cols.push_back(Block(camera_size, c));
  57. c += camera_size;
  58. }
  59. std::bernoulli_distribution visibility_distribution(visibility);
  60. int row_pos = 0;
  61. int cell_pos = 0;
  62. for (int i = 0; i < num_points; ++i) {
  63. for (int j = 0; j < num_cameras; ++j) {
  64. if (!visibility_distribution(prng)) {
  65. continue;
  66. }
  67. bs->rows.emplace_back();
  68. auto& row = bs->rows.back();
  69. row.block.position = row_pos;
  70. row.block.size = kResidualSize;
  71. auto& cells = row.cells;
  72. cells.resize(2);
  73. cells[0].block_id = i;
  74. cells[0].position = cell_pos;
  75. cell_pos += kResidualSize * point_size;
  76. cells[1].block_id = num_points + j;
  77. cells[1].position = cell_pos;
  78. cell_pos += kResidualSize * camera_size;
  79. row_pos += kResidualSize;
  80. }
  81. }
  82. auto jacobian = std::make_unique<BlockSparseMatrix>(bs);
  83. VectorRef(jacobian->mutable_values(), jacobian->num_nonzeros()).setRandom();
  84. return jacobian;
  85. }
  86. std::pair<
  87. std::unique_ptr<PartitionedMatrixView<2, Eigen::Dynamic, Eigen::Dynamic>>,
  88. std::unique_ptr<BlockSparseMatrix>>
  89. CreateFakeBundleAdjustmentPartitionedJacobian(int num_cameras,
  90. int num_points,
  91. int camera_size,
  92. int landmark_size,
  93. double visibility,
  94. std::mt19937& rng) {
  95. using PartitionedView =
  96. PartitionedMatrixView<2, Eigen::Dynamic, Eigen::Dynamic>;
  97. auto block_sparse_matrix = CreateFakeBundleAdjustmentJacobian(
  98. num_cameras, num_points, camera_size, landmark_size, visibility, rng);
  99. LinearSolver::Options options;
  100. options.elimination_groups.push_back(num_points);
  101. auto partitioned_view =
  102. std::make_unique<PartitionedView>(options, *block_sparse_matrix);
  103. return std::make_pair(std::move(partitioned_view),
  104. std::move(block_sparse_matrix));
  105. }
  106. } // namespace ceres::internal