blob: 91b82bf995f1779b00b8a745e406d1f9a061ed10 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001/* Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08002 * Copyright 2019 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 * A minimal C API for Ceres. Not all functionality is included. This API is
32 * not intended for clients of Ceres, but is instead intended for easing the
33 * process of binding Ceres to other languages.
34 *
35 * Currently this is a work in progress.
36 */
37
38#ifndef CERES_PUBLIC_C_API_H_
39#define CERES_PUBLIC_C_API_H_
40
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080041// clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -080042#include "ceres/internal/port.h"
43#include "ceres/internal/disable_warnings.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080044// clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -080045
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/* Init the Ceres private data. Must be called before anything else. */
51CERES_EXPORT void ceres_init();
52
53/* Equivalent to CostFunction::Evaluate() in the C++ API.
54 *
55 * The user may keep private information inside the opaque user_data object.
56 * The pointer here is the same one passed in the ceres_add_residual_block().
57 */
58typedef int (*ceres_cost_function_t)(void* user_data,
59 double** parameters,
60 double* residuals,
61 double** jacobians);
62
63/* Equivalent to LossFunction::Evaluate() from the C++ API. */
64typedef void (*ceres_loss_function_t)(void* user_data,
65 double squared_norm,
66 double out[3]);
67
68/* Create callback data for Ceres' stock loss functions.
69 *
70 * Ceres has several loss functions available by default, and these functions
71 * expose those to the C API. To use the stock loss functions, call
72 * ceres_create_*_loss_data(), which internally creates an instance of one of
73 * the stock loss functions (for example ceres::CauchyLoss), and pass the
74 * returned "loss_function_data" along with the ceres_stock_loss_function to
75 * ceres_add_residual_block().
76 *
77 * For example:
78 *
79 * void* cauchy_loss_function_data =
80 * ceres_create_cauchy_loss_function_data(1.2, 0.0);
81 * ceres_problem_add_residual_block(
82 * problem,
83 * my_cost_function,
84 * my_cost_function_data,
85 * ceres_stock_loss_function,
86 * cauchy_loss_function_data,
87 * 1,
88 * 2,
89 * parameter_sizes,
90 * parameter_pointers);
91 * ...
92 * ceres_free_stock_loss_function_data(cauchy_loss_function_data);
93 *
94 * See loss_function.h for the details of each loss function.
95 */
96CERES_EXPORT void* ceres_create_huber_loss_function_data(double a);
97CERES_EXPORT void* ceres_create_softl1_loss_function_data(double a);
98CERES_EXPORT void* ceres_create_cauchy_loss_function_data(double a);
99CERES_EXPORT void* ceres_create_arctan_loss_function_data(double a);
100CERES_EXPORT void* ceres_create_tolerant_loss_function_data(double a, double b);
101
102/* Free the given stock loss function data. */
103CERES_EXPORT void ceres_free_stock_loss_function_data(void* loss_function_data);
104
105/* This is an implementation of ceres_loss_function_t contained within Ceres
106 * itself, intended as a way to access the various stock Ceres loss functions
107 * from the C API. This should be passed to ceres_add_residual() below, in
108 * combination with a user_data pointer generated by
109 * ceres_create_stock_loss_function() above. */
110CERES_EXPORT void ceres_stock_loss_function(void* user_data,
111 double squared_norm,
112 double out[3]);
113
114/* Equivalent to Problem from the C++ API. */
115struct ceres_problem_s;
116typedef struct ceres_problem_s ceres_problem_t;
117
118struct ceres_residual_block_id_s;
119typedef struct ceres_residual_block_id_s ceres_residual_block_id_t;
120
121/* Create and destroy a problem */
122/* TODO(keir): Add options for the problem. */
123CERES_EXPORT ceres_problem_t* ceres_create_problem();
124CERES_EXPORT void ceres_free_problem(ceres_problem_t* problem);
125
126/* Add a residual block. */
127CERES_EXPORT ceres_residual_block_id_t* ceres_problem_add_residual_block(
128 ceres_problem_t* problem,
129 ceres_cost_function_t cost_function,
130 void* cost_function_data,
131 ceres_loss_function_t loss_function,
132 void* loss_function_data,
133 int num_residuals,
134 int num_parameter_blocks,
135 int* parameter_block_sizes,
136 double** parameters);
137
138CERES_EXPORT void ceres_solve(ceres_problem_t* problem);
139
140/* TODO(keir): Figure out a way to pass a config in. */
141
142#ifdef __cplusplus
143}
144#endif
145
146#include "ceres/internal/reenable_warnings.h"
147
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800148#endif /* CERES_PUBLIC_C_API_H_ */