blob: d5b3fa1fa373ed1ab9355d78c97cef5789c4a4cc [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// 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
31#include "ceres/residual_block_utils.h"
32
33#include <cmath>
34#include <cstddef>
35#include <limits>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "ceres/array_utils.h"
38#include "ceres/internal/eigen.h"
39#include "ceres/internal/port.h"
40#include "ceres/parameter_block.h"
41#include "ceres/residual_block.h"
42#include "ceres/stringprintf.h"
43#include "glog/logging.h"
44
45namespace ceres {
46namespace internal {
47
48using std::string;
49
50void InvalidateEvaluation(const ResidualBlock& block,
51 double* cost,
52 double* residuals,
53 double** jacobians) {
54 const int num_parameter_blocks = block.NumParameterBlocks();
55 const int num_residuals = block.NumResiduals();
56
57 InvalidateArray(1, cost);
58 InvalidateArray(num_residuals, residuals);
59 if (jacobians != NULL) {
60 for (int i = 0; i < num_parameter_blocks; ++i) {
61 const int parameter_block_size = block.parameter_blocks()[i]->Size();
62 InvalidateArray(num_residuals * parameter_block_size, jacobians[i]);
63 }
64 }
65}
66
67string EvaluationToString(const ResidualBlock& block,
68 double const* const* parameters,
69 double* cost,
70 double* residuals,
71 double** jacobians) {
72 CHECK(cost != nullptr);
73 CHECK(residuals != nullptr);
74
75 const int num_parameter_blocks = block.NumParameterBlocks();
76 const int num_residuals = block.NumResiduals();
77 string result = "";
78
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080079 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -080080 StringAppendF(&result,
81 "Residual Block size: %d parameter blocks x %d residuals\n\n",
82 num_parameter_blocks, num_residuals);
83 result +=
84 "For each parameter block, the value of the parameters are printed in the first column \n" // NOLINT
85 "and the value of the jacobian under the corresponding residual. If a ParameterBlock was \n" // NOLINT
86 "held constant then the corresponding jacobian is printed as 'Not Computed'. If an entry \n" // NOLINT
87 "of the Jacobian/residual array was requested but was not written to by user code, it is \n" // NOLINT
88 "indicated by 'Uninitialized'. This is an error. Residuals or Jacobian values evaluating \n" // NOLINT
89 "to Inf or NaN is also an error. \n\n"; // NOLINT
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080090 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -080091
92 string space = "Residuals: ";
93 result += space;
94 AppendArrayToString(num_residuals, residuals, &result);
95 StringAppendF(&result, "\n\n");
96
97 for (int i = 0; i < num_parameter_blocks; ++i) {
98 const int parameter_block_size = block.parameter_blocks()[i]->Size();
99 StringAppendF(
100 &result, "Parameter Block %d, size: %d\n", i, parameter_block_size);
101 StringAppendF(&result, "\n");
102 for (int j = 0; j < parameter_block_size; ++j) {
103 AppendArrayToString(1, parameters[i] + j, &result);
104 StringAppendF(&result, "| ");
105 for (int k = 0; k < num_residuals; ++k) {
106 AppendArrayToString(1,
107 (jacobians != NULL && jacobians[i] != NULL)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800108 ? jacobians[i] + k * parameter_block_size + j
109 : NULL,
Austin Schuh70cc9552019-01-21 19:46:48 -0800110 &result);
111 }
112 StringAppendF(&result, "\n");
113 }
114 StringAppendF(&result, "\n");
115 }
116 StringAppendF(&result, "\n");
117 return result;
118}
119
120bool IsEvaluationValid(const ResidualBlock& block,
121 double const* const* parameters,
122 double* cost,
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
132 if (jacobians != NULL) {
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
144} // namespace internal
145} // namespace ceres