blob: 629745120d6522af198362bbf9f6db6e5182b3e1 [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: wjr@google.com (William Rucklidge)
30//
31// Tests for the conditioned cost function.
32
33#include "ceres/conditioned_cost_function.h"
34
35#include "ceres/internal/eigen.h"
36#include "ceres/normal_prior.h"
37#include "ceres/types.h"
38#include "gtest/gtest.h"
39
40namespace ceres {
41namespace internal {
42
43// The size of the cost functions we build.
44static const int kTestCostFunctionSize = 3;
45
46// A simple cost function: return ax + b.
47class LinearCostFunction : public CostFunction {
48 public:
49 LinearCostFunction(double a, double b) : a_(a), b_(b) {
50 set_num_residuals(1);
51 mutable_parameter_block_sizes()->push_back(1);
52 }
53
54 virtual bool Evaluate(double const* const* parameters,
55 double* residuals,
56 double** jacobians) const {
57 *residuals = **parameters * a_ + b_;
58 if (jacobians && *jacobians) {
59 **jacobians = a_;
60 }
61
62 return true;
63 }
64
65 private:
66 const double a_, b_;
67};
68
69// Tests that ConditionedCostFunction does what it's supposed to.
70TEST(ConditionedCostFunction, NormalOperation) {
71 double v1[kTestCostFunctionSize], v2[kTestCostFunctionSize],
72 jac[kTestCostFunctionSize * kTestCostFunctionSize],
73 result[kTestCostFunctionSize];
74
75 for (int i = 0; i < kTestCostFunctionSize; i++) {
76 v1[i] = i;
77 v2[i] = i * 10;
78 // Seed a few garbage values in the Jacobian matrix, to make sure that
79 // they're overwritten.
80 jac[i * 2] = i * i;
81 result[i] = i * i * i;
82 }
83
84 // Make a cost function that computes x - v2
85 VectorRef v2_vector(v2, kTestCostFunctionSize, 1);
86 Matrix identity(kTestCostFunctionSize, kTestCostFunctionSize);
87 identity.setIdentity();
88 NormalPrior* difference_cost_function = new NormalPrior(identity, v2_vector);
89
90 std::vector<CostFunction*> conditioners;
91 for (int i = 0; i < kTestCostFunctionSize; i++) {
92 conditioners.push_back(new LinearCostFunction(i + 2, i * 7));
93 }
94
95 ConditionedCostFunction conditioned_cost_function(
96 difference_cost_function, conditioners, TAKE_OWNERSHIP);
97 EXPECT_EQ(difference_cost_function->num_residuals(),
98 conditioned_cost_function.num_residuals());
99 EXPECT_EQ(difference_cost_function->parameter_block_sizes(),
100 conditioned_cost_function.parameter_block_sizes());
101
102 double* parameters[1];
103 parameters[0] = v1;
104 double* jacs[1];
105 jacs[0] = jac;
106
107 conditioned_cost_function.Evaluate(parameters, result, jacs);
108 for (int i = 0; i < kTestCostFunctionSize; i++) {
109 EXPECT_DOUBLE_EQ((i + 2) * (v1[i] - v2[i]) + i * 7, result[i]);
110 }
111
112 for (int i = 0; i < kTestCostFunctionSize; i++) {
113 for (int j = 0; j < kTestCostFunctionSize; j++) {
114 double actual = jac[i * kTestCostFunctionSize + j];
115 if (i != j) {
116 EXPECT_DOUBLE_EQ(0, actual);
117 } else {
118 EXPECT_DOUBLE_EQ(i + 2, actual);
119 }
120 }
121 }
122}
123
124TEST(ConditionedCostFunction, SharedConditionersDoNotTriggerDoubleFree) {
125 // Make a cost function that computes x - v2
126 double v2[kTestCostFunctionSize];
127 VectorRef v2_vector(v2, kTestCostFunctionSize, 1);
128 Matrix identity = Matrix::Identity(kTestCostFunctionSize, kTestCostFunctionSize);
129 NormalPrior* difference_cost_function = new NormalPrior(identity, v2_vector);
130 CostFunction* conditioner = new LinearCostFunction(2, 7);
131 std::vector<CostFunction*> conditioners;
132 for (int i = 0; i < kTestCostFunctionSize; i++) {
133 conditioners.push_back(conditioner);
134 }
135
136 ConditionedCostFunction conditioned_cost_function(
137 difference_cost_function, conditioners, TAKE_OWNERSHIP);
138}
139
140} // namespace internal
141} // namespace ceres