blob: d3c917ab8a6cd86e17a361d2ae408830c0a746f9 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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 <cmath>
Sameer Agarwal1fdc5202012-05-12 07:29:10 -070032#include <limits>
Keir Mierle8ebb0732012-04-30 23:09:08 -070033#include "gtest/gtest.h"
34#include "ceres/parameter_block.h"
35#include "ceres/residual_block.h"
36#include "ceres/residual_block_utils.h"
37#include "ceres/cost_function.h"
38#include "ceres/internal/scoped_ptr.h"
39#include "ceres/sized_cost_function.h"
40
41namespace ceres {
42namespace internal {
43
44// Routine to check if ResidualBlock::Evaluate for unary CostFunction
45// with one residual succeeds with true or dies.
46void CheckEvaluation(const CostFunction& cost_function, bool is_good) {
47 double x = 1.0;
Keir Mierle04938ef2013-02-17 12:37:55 -080048 ParameterBlock parameter_block(&x, 1, -1);
Keir Mierle8ebb0732012-04-30 23:09:08 -070049 vector<ParameterBlock*> parameter_blocks;
50 parameter_blocks.push_back(&parameter_block);
51
52 ResidualBlock residual_block(&cost_function,
53 NULL,
Keir Mierle04938ef2013-02-17 12:37:55 -080054 parameter_blocks,
55 -1);
Keir Mierle8ebb0732012-04-30 23:09:08 -070056
57 scoped_array<double> scratch(
58 new double[residual_block.NumScratchDoublesForEvaluate()]);
59
60 double cost;
61 double residuals;
62 double jacobian;
63 double* jacobians[] = { &jacobian };
64
Sameer Agarwal039ff072013-02-26 09:15:39 -080065 EXPECT_EQ(residual_block.Evaluate(true,
66 &cost,
Keir Mierle8ebb0732012-04-30 23:09:08 -070067 &residuals,
68 jacobians,
69 scratch.get()), is_good);
70}
71
72// A CostFunction that behaves normaly, i.e., it computes numerically
73// valid residuals and jacobians.
74class GoodCostFunction: public SizedCostFunction<1, 1> {
75 public:
76 virtual bool Evaluate(double const* const* parameters,
77 double* residuals,
78 double** jacobians) const {
79 residuals[0] = 1;
80 if (jacobians != NULL && jacobians[0] != NULL) {
81 jacobians[0][0] = 0.0;
82 }
83 return true;
84 }
85};
86
87// The following four CostFunctions simulate the different ways in
88// which user code can cause ResidualBlock::Evaluate to fail.
89class NoResidualUpdateCostFunction: public SizedCostFunction<1, 1> {
90 public:
91 virtual bool Evaluate(double const* const* parameters,
92 double* residuals,
93 double** jacobians) const {
94 // Forget to update the residuals.
95 // residuals[0] = 1;
96 if (jacobians != NULL && jacobians[0] != NULL) {
97 jacobians[0][0] = 0.0;
98 }
99 return true;
100 }
101};
102
103class NoJacobianUpdateCostFunction: public SizedCostFunction<1, 1> {
104 public:
105 virtual bool Evaluate(double const* const* parameters,
106 double* residuals,
107 double** jacobians) const {
108 residuals[0] = 1;
109 if (jacobians != NULL && jacobians[0] != NULL) {
110 // Forget to update the jacobians.
111 // jacobians[0][0] = 0.0;
112 }
113 return true;
114 }
115};
116
117class BadResidualCostFunction: public SizedCostFunction<1, 1> {
118 public:
119 virtual bool Evaluate(double const* const* parameters,
120 double* residuals,
121 double** jacobians) const {
Sameer Agarwal1fdc5202012-05-12 07:29:10 -0700122 residuals[0] = std::numeric_limits<double>::infinity();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700123 if (jacobians != NULL && jacobians[0] != NULL) {
124 jacobians[0][0] = 0.0;
125 }
126 return true;
127 }
128};
129
130class BadJacobianCostFunction: public SizedCostFunction<1, 1> {
131 public:
132 virtual bool Evaluate(double const* const* parameters,
133 double* residuals,
134 double** jacobians) const {
135 residuals[0] = 1.0;
136 if (jacobians != NULL && jacobians[0] != NULL) {
Sameer Agarwal1fdc5202012-05-12 07:29:10 -0700137 jacobians[0][0] = std::numeric_limits<double>::quiet_NaN();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700138 }
139 return true;
140 }
141};
142
Keir Mierle8ebb0732012-04-30 23:09:08 -0700143// Note: It is preferable to write the below test as:
144//
145// CheckEvaluation(GoodCostFunction(), true);
146// CheckEvaluation(NoResidualUpdateCostFunction(), false);
147// CheckEvaluation(NoJacobianUpdateCostFunction(), false);
148// ...
149//
150// however, there is a bug in the version of GCC on Mac OS X we tested, which
151// requires the objects get put into local variables instead of getting
152// instantiated on the stack.
153TEST(ResidualBlockUtils, CheckAllCombinationsOfBadness) {
154 GoodCostFunction good_fun;
155 CheckEvaluation(good_fun, true);
156 NoResidualUpdateCostFunction no_residual;
157 CheckEvaluation(no_residual, false);
158 NoJacobianUpdateCostFunction no_jacobian;
159 CheckEvaluation(no_jacobian, false);
160 BadResidualCostFunction bad_residual;
161 CheckEvaluation(bad_residual, false);
162 BadJacobianCostFunction bad_jacobian;
163 CheckEvaluation(bad_jacobian, false);
164}
165
166} // namespace internal
167} // namespace ceres