blob: 331f5ab47418798f77bea6e4f54c3d6ac446a4e6 [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
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080031#include "ceres/residual_block_utils.h"
32
Austin Schuh70cc9552019-01-21 19:46:48 -080033#include <cmath>
34#include <limits>
35#include <memory>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036
37#include "ceres/cost_function.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include "ceres/parameter_block.h"
39#include "ceres/residual_block.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080040#include "ceres/sized_cost_function.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080041#include "gtest/gtest.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080042
43namespace ceres {
44namespace internal {
45
46// Routine to check if ResidualBlock::Evaluate for unary CostFunction
47// with one residual succeeds with true or dies.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080048static void CheckEvaluation(const CostFunction& cost_function, bool is_good) {
Austin Schuh70cc9552019-01-21 19:46:48 -080049 double x = 1.0;
50 ParameterBlock parameter_block(&x, 1, -1);
51 std::vector<ParameterBlock*> parameter_blocks;
52 parameter_blocks.push_back(&parameter_block);
53
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080054 ResidualBlock residual_block(&cost_function, NULL, parameter_blocks, -1);
Austin Schuh70cc9552019-01-21 19:46:48 -080055
56 std::unique_ptr<double[]> scratch(
57 new double[residual_block.NumScratchDoublesForEvaluate()]);
58
59 double cost;
60 double residuals;
61 double jacobian;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080062 double* jacobians[] = {&jacobian};
Austin Schuh70cc9552019-01-21 19:46:48 -080063
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080064 EXPECT_EQ(residual_block.Evaluate(
65 true, &cost, &residuals, jacobians, scratch.get()),
66 is_good);
Austin Schuh70cc9552019-01-21 19:46:48 -080067}
68
69// A CostFunction that behaves normaly, i.e., it computes numerically
70// valid residuals and jacobians.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080071class GoodCostFunction : public SizedCostFunction<1, 1> {
Austin Schuh70cc9552019-01-21 19:46:48 -080072 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080073 bool Evaluate(double const* const* parameters,
74 double* residuals,
75 double** jacobians) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -080076 residuals[0] = 1;
77 if (jacobians != NULL && jacobians[0] != NULL) {
78 jacobians[0][0] = 0.0;
79 }
80 return true;
81 }
82};
83
84// The following four CostFunctions simulate the different ways in
85// which user code can cause ResidualBlock::Evaluate to fail.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080086class NoResidualUpdateCostFunction : public SizedCostFunction<1, 1> {
Austin Schuh70cc9552019-01-21 19:46:48 -080087 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080088 bool Evaluate(double const* const* parameters,
89 double* residuals,
90 double** jacobians) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -080091 // Forget to update the residuals.
92 // residuals[0] = 1;
93 if (jacobians != NULL && jacobians[0] != NULL) {
94 jacobians[0][0] = 0.0;
95 }
96 return true;
97 }
98};
99
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800100class NoJacobianUpdateCostFunction : public SizedCostFunction<1, 1> {
Austin Schuh70cc9552019-01-21 19:46:48 -0800101 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800102 bool Evaluate(double const* const* parameters,
103 double* residuals,
104 double** jacobians) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800105 residuals[0] = 1;
106 if (jacobians != NULL && jacobians[0] != NULL) {
107 // Forget to update the jacobians.
108 // jacobians[0][0] = 0.0;
109 }
110 return true;
111 }
112};
113
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800114class BadResidualCostFunction : public SizedCostFunction<1, 1> {
Austin Schuh70cc9552019-01-21 19:46:48 -0800115 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800116 bool Evaluate(double const* const* parameters,
117 double* residuals,
118 double** jacobians) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800119 residuals[0] = std::numeric_limits<double>::infinity();
120 if (jacobians != NULL && jacobians[0] != NULL) {
121 jacobians[0][0] = 0.0;
122 }
123 return true;
124 }
125};
126
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800127class BadJacobianCostFunction : public SizedCostFunction<1, 1> {
Austin Schuh70cc9552019-01-21 19:46:48 -0800128 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800129 bool Evaluate(double const* const* parameters,
130 double* residuals,
131 double** jacobians) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800132 residuals[0] = 1.0;
133 if (jacobians != NULL && jacobians[0] != NULL) {
134 jacobians[0][0] = std::numeric_limits<double>::quiet_NaN();
135 }
136 return true;
137 }
138};
139
140// Note: It is preferable to write the below test as:
141//
142// CheckEvaluation(GoodCostFunction(), true);
143// CheckEvaluation(NoResidualUpdateCostFunction(), false);
144// CheckEvaluation(NoJacobianUpdateCostFunction(), false);
145// ...
146//
147// however, there is a bug in the version of GCC on Mac OS X we tested, which
148// requires the objects get put into local variables instead of getting
149// instantiated on the stack.
150TEST(ResidualBlockUtils, CheckAllCombinationsOfBadness) {
151 GoodCostFunction good_fun;
152 CheckEvaluation(good_fun, true);
153 NoResidualUpdateCostFunction no_residual;
154 CheckEvaluation(no_residual, false);
155 NoJacobianUpdateCostFunction no_jacobian;
156 CheckEvaluation(no_jacobian, false);
157 BadResidualCostFunction bad_residual;
158 CheckEvaluation(bad_residual, false);
159 BadJacobianCostFunction bad_jacobian;
160 CheckEvaluation(bad_jacobian, false);
161}
162
163} // namespace internal
164} // namespace ceres