c_api.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 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. #include "ceres/c_api.h"
  35. #include <iostream>
  36. #include <memory>
  37. #include <string>
  38. #include <vector>
  39. #include "ceres/cost_function.h"
  40. #include "ceres/loss_function.h"
  41. #include "ceres/problem.h"
  42. #include "ceres/solver.h"
  43. #include "ceres/types.h" // for std
  44. #include "glog/logging.h"
  45. using ceres::Problem;
  46. void ceres_init() {
  47. // This is not ideal, but it's not clear what to do if there is no gflags and
  48. // no access to command line arguments.
  49. char message[] = "<unknown>";
  50. google::InitGoogleLogging(message);
  51. }
  52. ceres_problem_t* ceres_create_problem() {
  53. return reinterpret_cast<ceres_problem_t*>(new Problem);
  54. }
  55. void ceres_free_problem(ceres_problem_t* problem) {
  56. delete reinterpret_cast<Problem*>(problem);
  57. }
  58. // This cost function wraps a C-level function pointer from the user, to bridge
  59. // between C and C++.
  60. class CERES_NO_EXPORT CallbackCostFunction final : public ceres::CostFunction {
  61. public:
  62. CallbackCostFunction(ceres_cost_function_t cost_function,
  63. void* user_data,
  64. int num_residuals,
  65. int num_parameter_blocks,
  66. int* parameter_block_sizes)
  67. : cost_function_(cost_function), user_data_(user_data) {
  68. set_num_residuals(num_residuals);
  69. for (int i = 0; i < num_parameter_blocks; ++i) {
  70. mutable_parameter_block_sizes()->push_back(parameter_block_sizes[i]);
  71. }
  72. }
  73. bool Evaluate(double const* const* parameters,
  74. double* residuals,
  75. double** jacobians) const final {
  76. return (*cost_function_)(
  77. user_data_, const_cast<double**>(parameters), residuals, jacobians);
  78. }
  79. private:
  80. ceres_cost_function_t cost_function_;
  81. void* user_data_;
  82. };
  83. // This loss function wraps a C-level function pointer from the user, to bridge
  84. // between C and C++.
  85. class CallbackLossFunction final : public ceres::LossFunction {
  86. public:
  87. explicit CallbackLossFunction(ceres_loss_function_t loss_function,
  88. void* user_data)
  89. : loss_function_(loss_function), user_data_(user_data) {}
  90. void Evaluate(double sq_norm, double* rho) const final {
  91. (*loss_function_)(user_data_, sq_norm, rho);
  92. }
  93. private:
  94. ceres_loss_function_t loss_function_;
  95. void* user_data_;
  96. };
  97. // Wrappers for the stock loss functions.
  98. void* ceres_create_huber_loss_function_data(double a) {
  99. return new ceres::HuberLoss(a);
  100. }
  101. void* ceres_create_softl1_loss_function_data(double a) {
  102. return new ceres::SoftLOneLoss(a);
  103. }
  104. void* ceres_create_cauchy_loss_function_data(double a) {
  105. return new ceres::CauchyLoss(a);
  106. }
  107. void* ceres_create_arctan_loss_function_data(double a) {
  108. return new ceres::ArctanLoss(a);
  109. }
  110. void* ceres_create_tolerant_loss_function_data(double a, double b) {
  111. return new ceres::TolerantLoss(a, b);
  112. }
  113. void ceres_free_stock_loss_function_data(void* loss_function_data) {
  114. delete reinterpret_cast<ceres::LossFunction*>(loss_function_data);
  115. }
  116. void ceres_stock_loss_function(void* user_data,
  117. double squared_norm,
  118. double out[3]) {
  119. reinterpret_cast<ceres::LossFunction*>(user_data)->Evaluate(squared_norm,
  120. out);
  121. }
  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. auto* ceres_problem = reinterpret_cast<Problem*>(problem);
  133. auto callback_cost_function =
  134. std::make_unique<CallbackCostFunction>(cost_function,
  135. cost_function_data,
  136. num_residuals,
  137. num_parameter_blocks,
  138. parameter_block_sizes);
  139. std::unique_ptr<ceres::LossFunction> callback_loss_function;
  140. if (loss_function != nullptr) {
  141. callback_loss_function = std::make_unique<CallbackLossFunction>(
  142. loss_function, loss_function_data);
  143. }
  144. std::vector<double*> parameter_blocks(parameters,
  145. parameters + num_parameter_blocks);
  146. return reinterpret_cast<ceres_residual_block_id_t*>(
  147. ceres_problem->AddResidualBlock(callback_cost_function.release(),
  148. callback_loss_function.release(),
  149. parameter_blocks));
  150. }
  151. void ceres_solve(ceres_problem_t* c_problem) {
  152. auto* problem = reinterpret_cast<Problem*>(c_problem);
  153. // TODO(keir): Obviously, this way of setting options won't scale or last.
  154. // Instead, figure out a way to specify some of the options without
  155. // duplicating everything.
  156. ceres::Solver::Options options;
  157. options.max_num_iterations = 100;
  158. options.linear_solver_type = ceres::DENSE_QR;
  159. options.minimizer_progress_to_stdout = true;
  160. ceres::Solver::Summary summary;
  161. ceres::Solve(options, problem, &summary);
  162. std::cout << summary.FullReport() << "\n";
  163. }