blob: 56e132487101183c00720f0487e2582e7f660d0e [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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>
Austin Schuh3de38b02024-06-25 18:25:10 -070038#include <memory>
Austin Schuh70cc9552019-01-21 19:46:48 -080039#include <string>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080040#include <vector>
41
Austin Schuh70cc9552019-01-21 19:46:48 -080042#include "ceres/cost_function.h"
43#include "ceres/loss_function.h"
44#include "ceres/problem.h"
45#include "ceres/solver.h"
46#include "ceres/types.h" // for std
47#include "glog/logging.h"
48
49using ceres::Problem;
50
51void ceres_init() {
52 // This is not ideal, but it's not clear what to do if there is no gflags and
53 // no access to command line arguments.
54 char message[] = "<unknown>";
55 google::InitGoogleLogging(message);
56}
57
58ceres_problem_t* ceres_create_problem() {
59 return reinterpret_cast<ceres_problem_t*>(new Problem);
60}
61
62void ceres_free_problem(ceres_problem_t* problem) {
63 delete reinterpret_cast<Problem*>(problem);
64}
65
66// This cost function wraps a C-level function pointer from the user, to bridge
67// between C and C++.
Austin Schuh3de38b02024-06-25 18:25:10 -070068class CERES_NO_EXPORT CallbackCostFunction final : public ceres::CostFunction {
Austin Schuh70cc9552019-01-21 19:46:48 -080069 public:
70 CallbackCostFunction(ceres_cost_function_t cost_function,
71 void* user_data,
72 int num_residuals,
73 int num_parameter_blocks,
74 int* parameter_block_sizes)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080075 : cost_function_(cost_function), user_data_(user_data) {
Austin Schuh70cc9552019-01-21 19:46:48 -080076 set_num_residuals(num_residuals);
77 for (int i = 0; i < num_parameter_blocks; ++i) {
78 mutable_parameter_block_sizes()->push_back(parameter_block_sizes[i]);
79 }
80 }
81
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080082 bool Evaluate(double const* const* parameters,
83 double* residuals,
84 double** jacobians) const final {
85 return (*cost_function_)(
86 user_data_, const_cast<double**>(parameters), residuals, jacobians);
Austin Schuh70cc9552019-01-21 19:46:48 -080087 }
88
89 private:
90 ceres_cost_function_t cost_function_;
91 void* user_data_;
92};
93
94// This loss function wraps a C-level function pointer from the user, to bridge
95// between C and C++.
Austin Schuh3de38b02024-06-25 18:25:10 -070096class CallbackLossFunction final : public ceres::LossFunction {
Austin Schuh70cc9552019-01-21 19:46:48 -080097 public:
98 explicit CallbackLossFunction(ceres_loss_function_t loss_function,
99 void* user_data)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800100 : loss_function_(loss_function), user_data_(user_data) {}
101 void Evaluate(double sq_norm, double* rho) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800102 (*loss_function_)(user_data_, sq_norm, rho);
103 }
104
105 private:
106 ceres_loss_function_t loss_function_;
107 void* user_data_;
108};
109
110// Wrappers for the stock loss functions.
111void* ceres_create_huber_loss_function_data(double a) {
112 return new ceres::HuberLoss(a);
113}
114void* ceres_create_softl1_loss_function_data(double a) {
115 return new ceres::SoftLOneLoss(a);
116}
117void* ceres_create_cauchy_loss_function_data(double a) {
118 return new ceres::CauchyLoss(a);
119}
120void* ceres_create_arctan_loss_function_data(double a) {
121 return new ceres::ArctanLoss(a);
122}
123void* ceres_create_tolerant_loss_function_data(double a, double b) {
124 return new ceres::TolerantLoss(a, b);
125}
126
127void ceres_free_stock_loss_function_data(void* loss_function_data) {
128 delete reinterpret_cast<ceres::LossFunction*>(loss_function_data);
129}
130
131void ceres_stock_loss_function(void* user_data,
132 double squared_norm,
133 double out[3]) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800134 reinterpret_cast<ceres::LossFunction*>(user_data)->Evaluate(squared_norm,
135 out);
Austin Schuh70cc9552019-01-21 19:46:48 -0800136}
137
138ceres_residual_block_id_t* ceres_problem_add_residual_block(
139 ceres_problem_t* problem,
140 ceres_cost_function_t cost_function,
141 void* cost_function_data,
142 ceres_loss_function_t loss_function,
143 void* loss_function_data,
144 int num_residuals,
145 int num_parameter_blocks,
146 int* parameter_block_sizes,
147 double** parameters) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700148 auto* ceres_problem = reinterpret_cast<Problem*>(problem);
Austin Schuh70cc9552019-01-21 19:46:48 -0800149
Austin Schuh3de38b02024-06-25 18:25:10 -0700150 auto callback_cost_function =
151 std::make_unique<CallbackCostFunction>(cost_function,
152 cost_function_data,
153 num_residuals,
154 num_parameter_blocks,
155 parameter_block_sizes);
Austin Schuh70cc9552019-01-21 19:46:48 -0800156
Austin Schuh3de38b02024-06-25 18:25:10 -0700157 std::unique_ptr<ceres::LossFunction> callback_loss_function;
158 if (loss_function != nullptr) {
159 callback_loss_function = std::make_unique<CallbackLossFunction>(
160 loss_function, loss_function_data);
Austin Schuh70cc9552019-01-21 19:46:48 -0800161 }
162
163 std::vector<double*> parameter_blocks(parameters,
164 parameters + num_parameter_blocks);
165 return reinterpret_cast<ceres_residual_block_id_t*>(
Austin Schuh3de38b02024-06-25 18:25:10 -0700166 ceres_problem->AddResidualBlock(callback_cost_function.release(),
167 callback_loss_function.release(),
168 parameter_blocks));
Austin Schuh70cc9552019-01-21 19:46:48 -0800169}
170
171void ceres_solve(ceres_problem_t* c_problem) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700172 auto* problem = reinterpret_cast<Problem*>(c_problem);
Austin Schuh70cc9552019-01-21 19:46:48 -0800173
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}