1 // 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>
32 #include <limits>
33 #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
41 namespace ceres {
42 namespace internal {
43
44 // Routine to check if ResidualBlock::Evaluate for unary CostFunction
45 // with one residual succeeds with true or dies.
CheckEvaluation(const CostFunction & cost_function,bool is_good)46 void CheckEvaluation(const CostFunction& cost_function, bool is_good) {
47 double x = 1.0;
48 ParameterBlock parameter_block(&x, 1, -1);
49 vector<ParameterBlock*> parameter_blocks;
50 parameter_blocks.push_back(¶meter_block);
51
52 ResidualBlock residual_block(&cost_function,
53 NULL,
54 parameter_blocks,
55 -1);
56
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
65 EXPECT_EQ(residual_block.Evaluate(true,
66 &cost,
67 &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.
74 class GoodCostFunction: public SizedCostFunction<1, 1> {
75 public:
Evaluate(double const * const * parameters,double * residuals,double ** jacobians) const76 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.
89 class NoResidualUpdateCostFunction: public SizedCostFunction<1, 1> {
90 public:
Evaluate(double const * const * parameters,double * residuals,double ** jacobians) const91 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
103 class NoJacobianUpdateCostFunction: public SizedCostFunction<1, 1> {
104 public:
Evaluate(double const * const * parameters,double * residuals,double ** jacobians) const105 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
117 class BadResidualCostFunction: public SizedCostFunction<1, 1> {
118 public:
Evaluate(double const * const * parameters,double * residuals,double ** jacobians) const119 virtual bool Evaluate(double const* const* parameters,
120 double* residuals,
121 double** jacobians) const {
122 residuals[0] = std::numeric_limits<double>::infinity();
123 if (jacobians != NULL && jacobians[0] != NULL) {
124 jacobians[0][0] = 0.0;
125 }
126 return true;
127 }
128 };
129
130 class BadJacobianCostFunction: public SizedCostFunction<1, 1> {
131 public:
Evaluate(double const * const * parameters,double * residuals,double ** jacobians) const132 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) {
137 jacobians[0][0] = std::numeric_limits<double>::quiet_NaN();
138 }
139 return true;
140 }
141 };
142
143 // 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.
TEST(ResidualBlockUtils,CheckAllCombinationsOfBadness)153 TEST(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
168