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/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are met:
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.
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.
29
// Author: sameeragarwal@google.com (Sameer Agarwal)
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"
44
// Routine to check if ResidualBlock::Evaluate for unary CostFunction
45
// with one residual succeeds with true or dies.
46
void CheckEvaluation(const CostFunction& cost_function, bool is_good) {
48
ParameterBlock parameter_block(&x, 1);
49
vector<ParameterBlock*> parameter_blocks;
50
parameter_blocks.push_back(¶meter_block);
52
ResidualBlock residual_block(&cost_function,
56
scoped_array<double> scratch(
57
new double[residual_block.NumScratchDoublesForEvaluate()]);
62
double* jacobians[] = { &jacobian };
64
EXPECT_EQ(residual_block.Evaluate(&cost,
67
scratch.get()), is_good);
70
// A CostFunction that behaves normaly, i.e., it computes numerically
71
// valid residuals and jacobians.
72
class GoodCostFunction: public SizedCostFunction<1, 1> {
74
virtual bool Evaluate(double const* const* parameters,
76
double** jacobians) const {
78
if (jacobians != NULL && jacobians[0] != NULL) {
79
jacobians[0][0] = 0.0;
85
// The following four CostFunctions simulate the different ways in
86
// which user code can cause ResidualBlock::Evaluate to fail.
87
class NoResidualUpdateCostFunction: public SizedCostFunction<1, 1> {
89
virtual bool Evaluate(double const* const* parameters,
91
double** jacobians) const {
92
// Forget to update the residuals.
94
if (jacobians != NULL && jacobians[0] != NULL) {
95
jacobians[0][0] = 0.0;
101
class NoJacobianUpdateCostFunction: public SizedCostFunction<1, 1> {
103
virtual bool Evaluate(double const* const* parameters,
105
double** jacobians) const {
107
if (jacobians != NULL && jacobians[0] != NULL) {
108
// Forget to update the jacobians.
109
// jacobians[0][0] = 0.0;
115
class BadResidualCostFunction: public SizedCostFunction<1, 1> {
117
virtual bool Evaluate(double const* const* parameters,
119
double** jacobians) const {
120
residuals[0] = std::numeric_limits<double>::infinity();
121
if (jacobians != NULL && jacobians[0] != NULL) {
122
jacobians[0][0] = 0.0;
128
class BadJacobianCostFunction: public SizedCostFunction<1, 1> {
130
virtual bool Evaluate(double const* const* parameters,
132
double** jacobians) const {
134
if (jacobians != NULL && jacobians[0] != NULL) {
135
jacobians[0][0] = std::numeric_limits<double>::quiet_NaN();
141
// Note: It is preferable to write the below test as:
143
// CheckEvaluation(GoodCostFunction(), true);
144
// CheckEvaluation(NoResidualUpdateCostFunction(), false);
145
// CheckEvaluation(NoJacobianUpdateCostFunction(), false);
148
// however, there is a bug in the version of GCC on Mac OS X we tested, which
149
// requires the objects get put into local variables instead of getting
150
// instantiated on the stack.
151
TEST(ResidualBlockUtils, CheckAllCombinationsOfBadness) {
152
GoodCostFunction good_fun;
153
CheckEvaluation(good_fun, true);
154
NoResidualUpdateCostFunction no_residual;
155
CheckEvaluation(no_residual, false);
156
NoJacobianUpdateCostFunction no_jacobian;
157
CheckEvaluation(no_jacobian, false);
158
BadResidualCostFunction bad_residual;
159
CheckEvaluation(bad_residual, false);
160
BadJacobianCostFunction bad_jacobian;
161
CheckEvaluation(bad_jacobian, false);
164
} // namespace internal