~ubuntu-branches/ubuntu/raring/ceres-solver/raring

« back to all changes in this revision

Viewing changes to internal/ceres/residual_block_utils_test.cc

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2012-06-04 07:15:43 UTC
  • Revision ID: package-import@ubuntu.com-20120604071543-zx6uthupvmtqn3k2
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
46
void CheckEvaluation(const CostFunction& cost_function, bool is_good) {
 
47
  double x = 1.0;
 
48
  ParameterBlock parameter_block(&x, 1);
 
49
  vector<ParameterBlock*> parameter_blocks;
 
50
  parameter_blocks.push_back(&parameter_block);
 
51
 
 
52
  ResidualBlock residual_block(&cost_function,
 
53
                               NULL,
 
54
                               parameter_blocks);
 
55
 
 
56
  scoped_array<double> scratch(
 
57
      new double[residual_block.NumScratchDoublesForEvaluate()]);
 
58
 
 
59
  double cost;
 
60
  double residuals;
 
61
  double jacobian;
 
62
  double* jacobians[] = { &jacobian };
 
63
 
 
64
  EXPECT_EQ(residual_block.Evaluate(&cost,
 
65
                                    &residuals,
 
66
                                    jacobians,
 
67
                                    scratch.get()), is_good);
 
68
}
 
69
 
 
70
// A CostFunction that behaves normaly, i.e., it computes numerically
 
71
// valid residuals and jacobians.
 
72
class GoodCostFunction: public SizedCostFunction<1, 1> {
 
73
 public:
 
74
  virtual bool Evaluate(double const* const* parameters,
 
75
                        double* residuals,
 
76
                        double** jacobians) const {
 
77
    residuals[0] = 1;
 
78
    if (jacobians != NULL && jacobians[0] != NULL) {
 
79
      jacobians[0][0] = 0.0;
 
80
    }
 
81
    return true;
 
82
  }
 
83
};
 
84
 
 
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> {
 
88
 public:
 
89
  virtual bool Evaluate(double const* const* parameters,
 
90
                        double* residuals,
 
91
                        double** jacobians) const {
 
92
    // Forget to update the residuals.
 
93
    // residuals[0] = 1;
 
94
    if (jacobians != NULL && jacobians[0] != NULL) {
 
95
      jacobians[0][0] = 0.0;
 
96
    }
 
97
    return true;
 
98
  }
 
99
};
 
100
 
 
101
class NoJacobianUpdateCostFunction: public SizedCostFunction<1, 1> {
 
102
 public:
 
103
  virtual bool Evaluate(double const* const* parameters,
 
104
                        double* residuals,
 
105
                        double** jacobians) const {
 
106
    residuals[0] = 1;
 
107
    if (jacobians != NULL && jacobians[0] != NULL) {
 
108
      // Forget to update the jacobians.
 
109
      // jacobians[0][0] = 0.0;
 
110
    }
 
111
    return true;
 
112
  }
 
113
};
 
114
 
 
115
class BadResidualCostFunction: public SizedCostFunction<1, 1> {
 
116
 public:
 
117
  virtual bool Evaluate(double const* const* parameters,
 
118
                        double* residuals,
 
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;
 
123
    }
 
124
    return true;
 
125
  }
 
126
};
 
127
 
 
128
class BadJacobianCostFunction: public SizedCostFunction<1, 1> {
 
129
 public:
 
130
  virtual bool Evaluate(double const* const* parameters,
 
131
                        double* residuals,
 
132
                        double** jacobians) const {
 
133
    residuals[0] = 1.0;
 
134
    if (jacobians != NULL && jacobians[0] != NULL) {
 
135
      jacobians[0][0] = std::numeric_limits<double>::quiet_NaN();
 
136
    }
 
137
    return true;
 
138
  }
 
139
};
 
140
 
 
141
// Note: It is preferable to write the below test as:
 
142
//
 
143
//  CheckEvaluation(GoodCostFunction(), true);
 
144
//  CheckEvaluation(NoResidualUpdateCostFunction(), false);
 
145
//  CheckEvaluation(NoJacobianUpdateCostFunction(), false);
 
146
//  ...
 
147
//
 
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);
 
162
}
 
163
 
 
164
}  // namespace internal
 
165
}  // namespace ceres