c_api.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Ceres Solver - A fast non-linear least squares minimizer
  2. * Copyright 2019 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. * 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. #ifndef CERES_PUBLIC_C_API_H_
  38. #define CERES_PUBLIC_C_API_H_
  39. // clang-format off
  40. #include "ceres/internal/export.h"
  41. #include "ceres/internal/disable_warnings.h"
  42. // clang-format on
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /* Init the Ceres private data. Must be called before anything else. */
  47. CERES_EXPORT void ceres_init();
  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. /* Equivalent to LossFunction::Evaluate() from the C++ API. */
  58. typedef void (*ceres_loss_function_t)(void* user_data,
  59. double squared_norm,
  60. double out[3]);
  61. /* Create callback data for Ceres' stock loss functions.
  62. *
  63. * Ceres has several loss functions available by default, and these functions
  64. * expose those to the C API. To use the stock loss functions, call
  65. * ceres_create_*_loss_data(), which internally creates an instance of one of
  66. * the stock loss functions (for example ceres::CauchyLoss), and pass the
  67. * returned "loss_function_data" along with the ceres_stock_loss_function to
  68. * ceres_add_residual_block().
  69. *
  70. * For example:
  71. *
  72. * void* cauchy_loss_function_data =
  73. * ceres_create_cauchy_loss_function_data(1.2, 0.0);
  74. * ceres_problem_add_residual_block(
  75. * problem,
  76. * my_cost_function,
  77. * my_cost_function_data,
  78. * ceres_stock_loss_function,
  79. * cauchy_loss_function_data,
  80. * 1,
  81. * 2,
  82. * parameter_sizes,
  83. * parameter_pointers);
  84. * ...
  85. * ceres_free_stock_loss_function_data(cauchy_loss_function_data);
  86. *
  87. * See loss_function.h for the details of each loss function.
  88. */
  89. CERES_EXPORT void* ceres_create_huber_loss_function_data(double a);
  90. CERES_EXPORT void* ceres_create_softl1_loss_function_data(double a);
  91. CERES_EXPORT void* ceres_create_cauchy_loss_function_data(double a);
  92. CERES_EXPORT void* ceres_create_arctan_loss_function_data(double a);
  93. CERES_EXPORT void* ceres_create_tolerant_loss_function_data(double a, double b);
  94. /* Free the given stock loss function data. */
  95. CERES_EXPORT void ceres_free_stock_loss_function_data(void* loss_function_data);
  96. /* This is an implementation of ceres_loss_function_t contained within Ceres
  97. * itself, intended as a way to access the various stock Ceres loss functions
  98. * from the C API. This should be passed to ceres_add_residual() below, in
  99. * combination with a user_data pointer generated by
  100. * ceres_create_stock_loss_function() above. */
  101. CERES_EXPORT void ceres_stock_loss_function(void* user_data,
  102. double squared_norm,
  103. double out[3]);
  104. /* Equivalent to Problem from the C++ API. */
  105. struct ceres_problem_s;
  106. typedef struct ceres_problem_s ceres_problem_t;
  107. struct ceres_residual_block_id_s;
  108. typedef struct ceres_residual_block_id_s ceres_residual_block_id_t;
  109. /* Create and destroy a problem */
  110. /* TODO(keir): Add options for the problem. */
  111. CERES_EXPORT ceres_problem_t* ceres_create_problem();
  112. CERES_EXPORT void ceres_free_problem(ceres_problem_t* problem);
  113. /* Add a residual block. */
  114. CERES_EXPORT ceres_residual_block_id_t* ceres_problem_add_residual_block(
  115. ceres_problem_t* problem,
  116. ceres_cost_function_t cost_function,
  117. void* cost_function_data,
  118. ceres_loss_function_t loss_function,
  119. void* loss_function_data,
  120. int num_residuals,
  121. int num_parameter_blocks,
  122. int* parameter_block_sizes,
  123. double** parameters);
  124. CERES_EXPORT void ceres_solve(ceres_problem_t* problem);
  125. /* TODO(keir): Figure out a way to pass a config in. */
  126. #ifdef __cplusplus
  127. }
  128. #endif
  129. #include "ceres/internal/reenable_warnings.h"
  130. #endif /* CERES_PUBLIC_C_API_H_ */