Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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 | |
| 31 | #include "ceres/residual_block_utils.h" |
| 32 | |
| 33 | #include <cmath> |
| 34 | #include <cstddef> |
| 35 | #include <limits> |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 36 | #include <string> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 37 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | #include "ceres/array_utils.h" |
| 39 | #include "ceres/internal/eigen.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 40 | #include "ceres/internal/export.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 41 | #include "ceres/parameter_block.h" |
| 42 | #include "ceres/residual_block.h" |
| 43 | #include "ceres/stringprintf.h" |
| 44 | #include "glog/logging.h" |
| 45 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 46 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 47 | |
| 48 | void InvalidateEvaluation(const ResidualBlock& block, |
| 49 | double* cost, |
| 50 | double* residuals, |
| 51 | double** jacobians) { |
| 52 | const int num_parameter_blocks = block.NumParameterBlocks(); |
| 53 | const int num_residuals = block.NumResiduals(); |
| 54 | |
| 55 | InvalidateArray(1, cost); |
| 56 | InvalidateArray(num_residuals, residuals); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 57 | if (jacobians != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 58 | for (int i = 0; i < num_parameter_blocks; ++i) { |
| 59 | const int parameter_block_size = block.parameter_blocks()[i]->Size(); |
| 60 | InvalidateArray(num_residuals * parameter_block_size, jacobians[i]); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 65 | std::string EvaluationToString(const ResidualBlock& block, |
| 66 | double const* const* parameters, |
| 67 | double* cost, |
| 68 | double* residuals, |
| 69 | double** jacobians) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 70 | CHECK(cost != nullptr); |
| 71 | CHECK(residuals != nullptr); |
| 72 | |
| 73 | const int num_parameter_blocks = block.NumParameterBlocks(); |
| 74 | const int num_residuals = block.NumResiduals(); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 75 | std::string result = ""; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 76 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 77 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 78 | StringAppendF(&result, |
| 79 | "Residual Block size: %d parameter blocks x %d residuals\n\n", |
| 80 | num_parameter_blocks, num_residuals); |
| 81 | result += |
| 82 | "For each parameter block, the value of the parameters are printed in the first column \n" // NOLINT |
| 83 | "and the value of the jacobian under the corresponding residual. If a ParameterBlock was \n" // NOLINT |
| 84 | "held constant then the corresponding jacobian is printed as 'Not Computed'. If an entry \n" // NOLINT |
| 85 | "of the Jacobian/residual array was requested but was not written to by user code, it is \n" // NOLINT |
| 86 | "indicated by 'Uninitialized'. This is an error. Residuals or Jacobian values evaluating \n" // NOLINT |
| 87 | "to Inf or NaN is also an error. \n\n"; // NOLINT |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 88 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 89 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 90 | std::string space = "Residuals: "; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 91 | result += space; |
| 92 | AppendArrayToString(num_residuals, residuals, &result); |
| 93 | StringAppendF(&result, "\n\n"); |
| 94 | |
| 95 | for (int i = 0; i < num_parameter_blocks; ++i) { |
| 96 | const int parameter_block_size = block.parameter_blocks()[i]->Size(); |
| 97 | StringAppendF( |
| 98 | &result, "Parameter Block %d, size: %d\n", i, parameter_block_size); |
| 99 | StringAppendF(&result, "\n"); |
| 100 | for (int j = 0; j < parameter_block_size; ++j) { |
| 101 | AppendArrayToString(1, parameters[i] + j, &result); |
| 102 | StringAppendF(&result, "| "); |
| 103 | for (int k = 0; k < num_residuals; ++k) { |
| 104 | AppendArrayToString(1, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 105 | (jacobians != nullptr && jacobians[i] != nullptr) |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 106 | ? jacobians[i] + k * parameter_block_size + j |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 107 | : nullptr, |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 108 | &result); |
| 109 | } |
| 110 | StringAppendF(&result, "\n"); |
| 111 | } |
| 112 | StringAppendF(&result, "\n"); |
| 113 | } |
| 114 | StringAppendF(&result, "\n"); |
| 115 | return result; |
| 116 | } |
| 117 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 118 | // TODO(sameeragarwal) Check cost value validness here |
| 119 | // Cost value is a part of evaluation but not checked here since according to |
| 120 | // residual_block.cc cost is not valid at the time this method is called |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 121 | bool IsEvaluationValid(const ResidualBlock& block, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 122 | double const* const* /*parameters*/, |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 123 | double* residuals, |
| 124 | double** jacobians) { |
| 125 | const int num_parameter_blocks = block.NumParameterBlocks(); |
| 126 | const int num_residuals = block.NumResiduals(); |
| 127 | |
| 128 | if (!IsArrayValid(num_residuals, residuals)) { |
| 129 | return false; |
| 130 | } |
| 131 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 132 | if (jacobians != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 133 | for (int i = 0; i < num_parameter_blocks; ++i) { |
| 134 | const int parameter_block_size = block.parameter_blocks()[i]->Size(); |
| 135 | if (!IsArrayValid(num_residuals * parameter_block_size, jacobians[i])) { |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 144 | } // namespace ceres::internal |