blob: 91370d8c23d019501e777e90d4347d1b69152683 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 Schuh3de38b02024-06-25 18:25:10 -070036#include <string>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080037
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include "ceres/array_utils.h"
39#include "ceres/internal/eigen.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070040#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080041#include "ceres/parameter_block.h"
42#include "ceres/residual_block.h"
43#include "ceres/stringprintf.h"
44#include "glog/logging.h"
45
Austin Schuh3de38b02024-06-25 18:25:10 -070046namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080047
48void 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 Schuh3de38b02024-06-25 18:25:10 -070057 if (jacobians != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -080058 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 Schuh3de38b02024-06-25 18:25:10 -070065std::string EvaluationToString(const ResidualBlock& block,
66 double const* const* parameters,
67 double* cost,
68 double* residuals,
69 double** jacobians) {
Austin Schuh70cc9552019-01-21 19:46:48 -080070 CHECK(cost != nullptr);
71 CHECK(residuals != nullptr);
72
73 const int num_parameter_blocks = block.NumParameterBlocks();
74 const int num_residuals = block.NumResiduals();
Austin Schuh3de38b02024-06-25 18:25:10 -070075 std::string result = "";
Austin Schuh70cc9552019-01-21 19:46:48 -080076
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080077 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -080078 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 Schuh1d1e6ea2020-12-23 21:56:30 -080088 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -080089
Austin Schuh3de38b02024-06-25 18:25:10 -070090 std::string space = "Residuals: ";
Austin Schuh70cc9552019-01-21 19:46:48 -080091 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 Schuh3de38b02024-06-25 18:25:10 -0700105 (jacobians != nullptr && jacobians[i] != nullptr)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800106 ? jacobians[i] + k * parameter_block_size + j
Austin Schuh3de38b02024-06-25 18:25:10 -0700107 : nullptr,
Austin Schuh70cc9552019-01-21 19:46:48 -0800108 &result);
109 }
110 StringAppendF(&result, "\n");
111 }
112 StringAppendF(&result, "\n");
113 }
114 StringAppendF(&result, "\n");
115 return result;
116}
117
Austin Schuh3de38b02024-06-25 18:25:10 -0700118// 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 Schuh70cc9552019-01-21 19:46:48 -0800121bool IsEvaluationValid(const ResidualBlock& block,
Austin Schuh3de38b02024-06-25 18:25:10 -0700122 double const* const* /*parameters*/,
Austin Schuh70cc9552019-01-21 19:46:48 -0800123 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 Schuh3de38b02024-06-25 18:25:10 -0700132 if (jacobians != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800133 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 Schuh3de38b02024-06-25 18:25:10 -0700144} // namespace ceres::internal