blob: 043f6ab2624e9340816ed532368bf409c7427426 [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: mierle@gmail.com (Keir Mierle)
30
31#include "ceres/c_api.h"
32
33#include <cmath>
34
35#include "glog/logging.h"
36#include "gtest/gtest.h"
37
38// Duplicated from curve_fitting.cc.
39int num_observations = 67;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080040// clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -080041double data[] = {
42 0.000000e+00, 1.133898e+00,
43 7.500000e-02, 1.334902e+00,
44 1.500000e-01, 1.213546e+00,
45 2.250000e-01, 1.252016e+00,
46 3.000000e-01, 1.392265e+00,
47 3.750000e-01, 1.314458e+00,
48 4.500000e-01, 1.472541e+00,
49 5.250000e-01, 1.536218e+00,
50 6.000000e-01, 1.355679e+00,
51 6.750000e-01, 1.463566e+00,
52 7.500000e-01, 1.490201e+00,
53 8.250000e-01, 1.658699e+00,
54 9.000000e-01, 1.067574e+00,
55 9.750000e-01, 1.464629e+00,
56 1.050000e+00, 1.402653e+00,
57 1.125000e+00, 1.713141e+00,
58 1.200000e+00, 1.527021e+00,
59 1.275000e+00, 1.702632e+00,
60 1.350000e+00, 1.423899e+00,
61 1.425000e+00, 1.543078e+00,
62 1.500000e+00, 1.664015e+00,
63 1.575000e+00, 1.732484e+00,
64 1.650000e+00, 1.543296e+00,
65 1.725000e+00, 1.959523e+00,
66 1.800000e+00, 1.685132e+00,
67 1.875000e+00, 1.951791e+00,
68 1.950000e+00, 2.095346e+00,
69 2.025000e+00, 2.361460e+00,
70 2.100000e+00, 2.169119e+00,
71 2.175000e+00, 2.061745e+00,
72 2.250000e+00, 2.178641e+00,
73 2.325000e+00, 2.104346e+00,
74 2.400000e+00, 2.584470e+00,
75 2.475000e+00, 1.914158e+00,
76 2.550000e+00, 2.368375e+00,
77 2.625000e+00, 2.686125e+00,
78 2.700000e+00, 2.712395e+00,
79 2.775000e+00, 2.499511e+00,
80 2.850000e+00, 2.558897e+00,
81 2.925000e+00, 2.309154e+00,
82 3.000000e+00, 2.869503e+00,
83 3.075000e+00, 3.116645e+00,
84 3.150000e+00, 3.094907e+00,
85 3.225000e+00, 2.471759e+00,
86 3.300000e+00, 3.017131e+00,
87 3.375000e+00, 3.232381e+00,
88 3.450000e+00, 2.944596e+00,
89 3.525000e+00, 3.385343e+00,
90 3.600000e+00, 3.199826e+00,
91 3.675000e+00, 3.423039e+00,
92 3.750000e+00, 3.621552e+00,
93 3.825000e+00, 3.559255e+00,
94 3.900000e+00, 3.530713e+00,
95 3.975000e+00, 3.561766e+00,
96 4.050000e+00, 3.544574e+00,
97 4.125000e+00, 3.867945e+00,
98 4.200000e+00, 4.049776e+00,
99 4.275000e+00, 3.885601e+00,
100 4.350000e+00, 4.110505e+00,
101 4.425000e+00, 4.345320e+00,
102 4.500000e+00, 4.161241e+00,
103 4.575000e+00, 4.363407e+00,
104 4.650000e+00, 4.161576e+00,
105 4.725000e+00, 4.619728e+00,
106 4.800000e+00, 4.737410e+00,
107 4.875000e+00, 4.727863e+00,
108 4.950000e+00, 4.669206e+00,
109};
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800110// clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800111
112// A test cost function, similar to the one in curve_fitting.c.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800113static int exponential_residual(void* user_data,
114 double** parameters,
115 double* residuals,
116 double** jacobians) {
117 double* measurement = (double*)user_data;
Austin Schuh70cc9552019-01-21 19:46:48 -0800118 double x = measurement[0];
119 double y = measurement[1];
120 double m = parameters[0][0];
121 double c = parameters[1][0];
122
123 residuals[0] = y - exp(m * x + c);
124 if (jacobians == NULL) {
125 return 1;
126 }
127 if (jacobians[0] != NULL) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800128 jacobians[0][0] = -x * exp(m * x + c); // dr/dm
Austin Schuh70cc9552019-01-21 19:46:48 -0800129 }
130 if (jacobians[1] != NULL) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800131 jacobians[1][0] = -exp(m * x + c); // dr/dc
Austin Schuh70cc9552019-01-21 19:46:48 -0800132 }
133 return 1;
134}
135
136namespace ceres {
137namespace internal {
138
139TEST(C_API, SimpleEndToEndTest) {
140 double m = 0.0;
141 double c = 0.0;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800142 double* parameter_pointers[] = {&m, &c};
143 int parameter_sizes[] = {1, 1};
Austin Schuh70cc9552019-01-21 19:46:48 -0800144
145 ceres_problem_t* problem = ceres_create_problem();
146 for (int i = 0; i < num_observations; ++i) {
147 ceres_problem_add_residual_block(
148 problem,
149 exponential_residual, // Cost function
150 &data[2 * i], // Points to the (x,y) measurement
151 NULL, // Loss function
152 NULL, // Loss function user data
153 1, // Number of residuals
154 2, // Number of parameter blocks
155 parameter_sizes,
156 parameter_pointers);
157 }
158
159 ceres_solve(problem);
160
161 EXPECT_NEAR(0.3, m, 0.02);
162 EXPECT_NEAR(0.1, c, 0.04);
163
164 ceres_free_problem(problem);
165}
166
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800167template <typename T>
Austin Schuh70cc9552019-01-21 19:46:48 -0800168class ScopedSetValue {
169 public:
170 ScopedSetValue(T* variable, T new_value)
171 : variable_(variable), old_value_(*variable) {
172 *variable = new_value;
173 }
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800174 ~ScopedSetValue() { *variable_ = old_value_; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800175
176 private:
177 T* variable_;
178 T old_value_;
179};
180
181TEST(C_API, LossFunctions) {
182 double m = 0.2;
183 double c = 0.03;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800184 double* parameter_pointers[] = {&m, &c};
185 int parameter_sizes[] = {1, 1};
Austin Schuh70cc9552019-01-21 19:46:48 -0800186
187 // Create two outliers, but be careful to leave the data intact.
188 ScopedSetValue<double> outlier1x(&data[12], 2.5);
189 ScopedSetValue<double> outlier1y(&data[13], 1.0e3);
190 ScopedSetValue<double> outlier2x(&data[14], 3.2);
191 ScopedSetValue<double> outlier2y(&data[15], 30e3);
192
193 // Create a cauchy cost function, and reuse it many times.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800194 void* cauchy_loss_data = ceres_create_cauchy_loss_function_data(5.0);
Austin Schuh70cc9552019-01-21 19:46:48 -0800195
196 ceres_problem_t* problem = ceres_create_problem();
197 for (int i = 0; i < num_observations; ++i) {
198 ceres_problem_add_residual_block(
199 problem,
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800200 exponential_residual, // Cost function
201 &data[2 * i], // Points to the (x,y) measurement
202 ceres_stock_loss_function, //
203 cauchy_loss_data, // Loss function user data
204 1, // Number of residuals
205 2, // Number of parameter blocks
Austin Schuh70cc9552019-01-21 19:46:48 -0800206 parameter_sizes,
207 parameter_pointers);
208 }
209
210 ceres_solve(problem);
211
212 EXPECT_NEAR(0.3, m, 0.02);
213 EXPECT_NEAR(0.1, c, 0.04);
214
215 ceres_free_stock_loss_function_data(cauchy_loss_data);
216 ceres_free_problem(problem);
217}
218
219} // namespace internal
220} // namespace ceres