1 /* Ceres Solver - A fast non-linear least squares minimizer 2 * Copyright 2013 Google Inc. All rights reserved. 3 * http://code.google.com/p/ceres-solver/ 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 (at) 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 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 /* Init the Ceres private data. Must be called before anything else. */ 46 void ceres_init(); 47 48 /* Equivalent to CostFunction::Evaluate() in the C++ API. 49 * 50 * The user may keep private information inside the opaque user_data object. 51 * The pointer here is the same one passed in the ceres_add_residual_block(). 52 */ 53 typedef int (*ceres_cost_function_t)(void* user_data, 54 double** parameters, 55 double* residuals, 56 double** jacobians); 57 58 /* Equivalent to LossFunction::Evaluate() from the C++ API. */ 59 typedef void (*ceres_loss_function_t)(void* user_data, 60 double squared_norm, 61 double out[3]); 62 63 /* Create callback data for Ceres' stock loss functions. 64 * 65 * Ceres has several loss functions available by default, and these functions 66 * expose those to the C API. To use the stock loss functions, call 67 * ceres_create_*_loss_data(), which internally creates an instance of one of 68 * the stock loss functions (for example ceres::CauchyLoss), and pass the 69 * returned "loss_function_data" along with the ceres_stock_loss_function to 70 * ceres_add_residual_block(). 71 * 72 * For example: 73 * 74 * void* cauchy_loss_function_data = 75 * ceres_create_cauchy_loss_function_data(1.2, 0.0); 76 * ceres_problem_add_residual_block( 77 * problem, 78 * my_cost_function, 79 * my_cost_function_data, 80 * ceres_stock_loss_function, 81 * cauchy_loss_function_data, 82 * 1, 83 * 2, 84 * parameter_sizes, 85 * parameter_pointers); 86 * ... 87 * ceres_free_stock_loss_function_data(cauchy_loss_function_data); 88 * 89 * See loss_function.h for the details of each loss function. 90 */ 91 void* ceres_create_huber_loss_function_data(double a); 92 void* ceres_create_softl1_loss_function_data(double a); 93 void* ceres_create_cauchy_loss_function_data(double a); 94 void* ceres_create_arctan_loss_function_data(double a); 95 void* ceres_create_tolerant_loss_function_data(double a, double b); 96 97 /* Free the given stock loss function data. */ 98 void ceres_free_stock_loss_function_data(void* loss_function_data); 99 100 /* This is an implementation of ceres_loss_function_t contained within Ceres 101 * itself, intended as a way to access the various stock Ceres loss functions 102 * from the C API. This should be passed to ceres_add_residual() below, in 103 * combination with a user_data pointer generated by 104 * ceres_create_stock_loss_function() above. */ 105 void ceres_stock_loss_function(void* user_data, 106 double squared_norm, 107 double out[3]); 108 109 /* Equivalent to Problem from the C++ API. */ 110 struct ceres_problem_s; 111 typedef struct ceres_problem_s ceres_problem_t; 112 113 struct ceres_residual_block_id_s; 114 typedef struct ceres_residual_block_id_s ceres_residual_block_id_t; 115 116 /* Create and destroy a problem */ 117 /* TODO(keir): Add options for the problem. */ 118 ceres_problem_t* ceres_create_problem(); 119 void ceres_free_problem(ceres_problem_t* problem); 120 121 /* Add a residual block. */ 122 ceres_residual_block_id_t* ceres_problem_add_residual_block( 123 ceres_problem_t* problem, 124 ceres_cost_function_t cost_function, 125 void* cost_function_data, 126 ceres_loss_function_t loss_function, 127 void* loss_function_data, 128 int num_residuals, 129 int num_parameter_blocks, 130 int* parameter_block_sizes, 131 double** parameters); 132 133 void ceres_solve(ceres_problem_t* problem); 134 135 /* TODO(keir): Figure out a way to pass a config in. */ 136 137 #ifdef __cplusplus 138 } 139 #endif 140 141 #endif /* CERES_PUBLIC_C_API_H_ */ 142