blob: 251cde42101c2dbf67d1de54de1c4c6ef6696d4f [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// An incomplete C API for Ceres.
32//
33// TODO(keir): Figure out why logging does not seem to work.
34
35#include "ceres/c_api.h"
36
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include <iostream>
38#include <string>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080039#include <vector>
40
Austin Schuh70cc9552019-01-21 19:46:48 -080041#include "ceres/cost_function.h"
42#include "ceres/loss_function.h"
43#include "ceres/problem.h"
44#include "ceres/solver.h"
45#include "ceres/types.h" // for std
46#include "glog/logging.h"
47
48using ceres::Problem;
49
50void ceres_init() {
51 // This is not ideal, but it's not clear what to do if there is no gflags and
52 // no access to command line arguments.
53 char message[] = "<unknown>";
54 google::InitGoogleLogging(message);
55}
56
57ceres_problem_t* ceres_create_problem() {
58 return reinterpret_cast<ceres_problem_t*>(new Problem);
59}
60
61void ceres_free_problem(ceres_problem_t* problem) {
62 delete reinterpret_cast<Problem*>(problem);
63}
64
65// This cost function wraps a C-level function pointer from the user, to bridge
66// between C and C++.
67class CallbackCostFunction : public ceres::CostFunction {
68 public:
69 CallbackCostFunction(ceres_cost_function_t cost_function,
70 void* user_data,
71 int num_residuals,
72 int num_parameter_blocks,
73 int* parameter_block_sizes)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080074 : cost_function_(cost_function), user_data_(user_data) {
Austin Schuh70cc9552019-01-21 19:46:48 -080075 set_num_residuals(num_residuals);
76 for (int i = 0; i < num_parameter_blocks; ++i) {
77 mutable_parameter_block_sizes()->push_back(parameter_block_sizes[i]);
78 }
79 }
80
81 virtual ~CallbackCostFunction() {}
82
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080083 bool Evaluate(double const* const* parameters,
84 double* residuals,
85 double** jacobians) const final {
86 return (*cost_function_)(
87 user_data_, const_cast<double**>(parameters), residuals, jacobians);
Austin Schuh70cc9552019-01-21 19:46:48 -080088 }
89
90 private:
91 ceres_cost_function_t cost_function_;
92 void* user_data_;
93};
94
95// This loss function wraps a C-level function pointer from the user, to bridge
96// between C and C++.
97class CallbackLossFunction : public ceres::LossFunction {
98 public:
99 explicit CallbackLossFunction(ceres_loss_function_t loss_function,
100 void* user_data)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800101 : loss_function_(loss_function), user_data_(user_data) {}
102 void Evaluate(double sq_norm, double* rho) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800103 (*loss_function_)(user_data_, sq_norm, rho);
104 }
105
106 private:
107 ceres_loss_function_t loss_function_;
108 void* user_data_;
109};
110
111// Wrappers for the stock loss functions.
112void* ceres_create_huber_loss_function_data(double a) {
113 return new ceres::HuberLoss(a);
114}
115void* ceres_create_softl1_loss_function_data(double a) {
116 return new ceres::SoftLOneLoss(a);
117}
118void* ceres_create_cauchy_loss_function_data(double a) {
119 return new ceres::CauchyLoss(a);
120}
121void* ceres_create_arctan_loss_function_data(double a) {
122 return new ceres::ArctanLoss(a);
123}
124void* ceres_create_tolerant_loss_function_data(double a, double b) {
125 return new ceres::TolerantLoss(a, b);
126}
127
128void ceres_free_stock_loss_function_data(void* loss_function_data) {
129 delete reinterpret_cast<ceres::LossFunction*>(loss_function_data);
130}
131
132void ceres_stock_loss_function(void* user_data,
133 double squared_norm,
134 double out[3]) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800135 reinterpret_cast<ceres::LossFunction*>(user_data)->Evaluate(squared_norm,
136 out);
Austin Schuh70cc9552019-01-21 19:46:48 -0800137}
138
139ceres_residual_block_id_t* ceres_problem_add_residual_block(
140 ceres_problem_t* problem,
141 ceres_cost_function_t cost_function,
142 void* cost_function_data,
143 ceres_loss_function_t loss_function,
144 void* loss_function_data,
145 int num_residuals,
146 int num_parameter_blocks,
147 int* parameter_block_sizes,
148 double** parameters) {
149 Problem* ceres_problem = reinterpret_cast<Problem*>(problem);
150
151 ceres::CostFunction* callback_cost_function =
152 new CallbackCostFunction(cost_function,
153 cost_function_data,
154 num_residuals,
155 num_parameter_blocks,
156 parameter_block_sizes);
157
158 ceres::LossFunction* callback_loss_function = NULL;
159 if (loss_function != NULL) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800160 callback_loss_function =
161 new CallbackLossFunction(loss_function, loss_function_data);
Austin Schuh70cc9552019-01-21 19:46:48 -0800162 }
163
164 std::vector<double*> parameter_blocks(parameters,
165 parameters + num_parameter_blocks);
166 return reinterpret_cast<ceres_residual_block_id_t*>(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800167 ceres_problem->AddResidualBlock(
168 callback_cost_function, callback_loss_function, parameter_blocks));
Austin Schuh70cc9552019-01-21 19:46:48 -0800169}
170
171void ceres_solve(ceres_problem_t* c_problem) {
172 Problem* problem = reinterpret_cast<Problem*>(c_problem);
173
174 // TODO(keir): Obviously, this way of setting options won't scale or last.
175 // Instead, figure out a way to specify some of the options without
176 // duplicating everything.
177 ceres::Solver::Options options;
178 options.max_num_iterations = 100;
179 options.linear_solver_type = ceres::DENSE_QR;
180 options.minimizer_progress_to_stdout = true;
181
182 ceres::Solver::Summary summary;
183 ceres::Solve(options, problem, &summary);
184 std::cout << summary.FullReport() << "\n";
185}