residual_block_test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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: keir@google.com (Keir Mierle)
  30. #include "ceres/residual_block.h"
  31. #include <cstdint>
  32. #include <string>
  33. #include <vector>
  34. #include "ceres/internal/eigen.h"
  35. #include "ceres/manifold.h"
  36. #include "ceres/parameter_block.h"
  37. #include "ceres/sized_cost_function.h"
  38. #include "gtest/gtest.h"
  39. namespace ceres::internal {
  40. // Trivial cost function that accepts three arguments.
  41. class TernaryCostFunction : public CostFunction {
  42. public:
  43. TernaryCostFunction(int num_residuals,
  44. int32_t parameter_block1_size,
  45. int32_t parameter_block2_size,
  46. int32_t parameter_block3_size) {
  47. set_num_residuals(num_residuals);
  48. mutable_parameter_block_sizes()->push_back(parameter_block1_size);
  49. mutable_parameter_block_sizes()->push_back(parameter_block2_size);
  50. mutable_parameter_block_sizes()->push_back(parameter_block3_size);
  51. }
  52. bool Evaluate(double const* const* parameters,
  53. double* residuals,
  54. double** jacobians) const final {
  55. for (int i = 0; i < num_residuals(); ++i) {
  56. residuals[i] = i;
  57. }
  58. if (jacobians) {
  59. for (int k = 0; k < 3; ++k) {
  60. if (jacobians[k] != nullptr) {
  61. MatrixRef jacobian(
  62. jacobians[k], num_residuals(), parameter_block_sizes()[k]);
  63. jacobian.setConstant(k);
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. };
  70. TEST(ResidualBlock, EvaluateWithNoLossFunctionOrManifolds) {
  71. double scratch[64];
  72. // Prepare the parameter blocks.
  73. double values_x[2];
  74. ParameterBlock x(values_x, 2, -1);
  75. double values_y[3];
  76. ParameterBlock y(values_y, 3, -1);
  77. double values_z[4];
  78. ParameterBlock z(values_z, 4, -1);
  79. std::vector<ParameterBlock*> parameters;
  80. parameters.push_back(&x);
  81. parameters.push_back(&y);
  82. parameters.push_back(&z);
  83. TernaryCostFunction cost_function(3, 2, 3, 4);
  84. // Create the object under tests.
  85. ResidualBlock residual_block(&cost_function, nullptr, parameters, -1);
  86. // Verify getters.
  87. EXPECT_EQ(&cost_function, residual_block.cost_function());
  88. EXPECT_EQ(nullptr, residual_block.loss_function());
  89. EXPECT_EQ(parameters[0], residual_block.parameter_blocks()[0]);
  90. EXPECT_EQ(parameters[1], residual_block.parameter_blocks()[1]);
  91. EXPECT_EQ(parameters[2], residual_block.parameter_blocks()[2]);
  92. EXPECT_EQ(3, residual_block.NumScratchDoublesForEvaluate());
  93. // Verify cost-only evaluation.
  94. double cost;
  95. residual_block.Evaluate(true, &cost, nullptr, nullptr, scratch);
  96. EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost);
  97. // Verify cost and residual evaluation.
  98. double residuals[3];
  99. residual_block.Evaluate(true, &cost, residuals, nullptr, scratch);
  100. EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost);
  101. EXPECT_EQ(0.0, residuals[0]);
  102. EXPECT_EQ(1.0, residuals[1]);
  103. EXPECT_EQ(2.0, residuals[2]);
  104. // Verify cost, residual, and jacobian evaluation.
  105. cost = 0.0;
  106. VectorRef(residuals, 3).setConstant(0.0);
  107. Matrix jacobian_rx(3, 2);
  108. Matrix jacobian_ry(3, 3);
  109. Matrix jacobian_rz(3, 4);
  110. jacobian_rx.setConstant(-1.0);
  111. jacobian_ry.setConstant(-1.0);
  112. jacobian_rz.setConstant(-1.0);
  113. double* jacobian_ptrs[3] = {
  114. jacobian_rx.data(), jacobian_ry.data(), jacobian_rz.data()};
  115. residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch);
  116. EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost);
  117. EXPECT_EQ(0.0, residuals[0]);
  118. EXPECT_EQ(1.0, residuals[1]);
  119. EXPECT_EQ(2.0, residuals[2]);
  120. EXPECT_TRUE((jacobian_rx.array() == 0.0).all()) << "\n" << jacobian_rx;
  121. EXPECT_TRUE((jacobian_ry.array() == 1.0).all()) << "\n" << jacobian_ry;
  122. EXPECT_TRUE((jacobian_rz.array() == 2.0).all()) << "\n" << jacobian_rz;
  123. // Verify cost, residual, and partial jacobian evaluation.
  124. cost = 0.0;
  125. VectorRef(residuals, 3).setConstant(0.0);
  126. jacobian_rx.setConstant(-1.0);
  127. jacobian_ry.setConstant(-1.0);
  128. jacobian_rz.setConstant(-1.0);
  129. jacobian_ptrs[1] = nullptr; // Don't compute the jacobian for y.
  130. residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch);
  131. EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost);
  132. EXPECT_EQ(0.0, residuals[0]);
  133. EXPECT_EQ(1.0, residuals[1]);
  134. EXPECT_EQ(2.0, residuals[2]);
  135. // clang-format off
  136. EXPECT_TRUE((jacobian_rx.array() == 0.0).all()) << "\n" << jacobian_rx;
  137. EXPECT_TRUE((jacobian_ry.array() == -1.0).all()) << "\n" << jacobian_ry;
  138. EXPECT_TRUE((jacobian_rz.array() == 2.0).all()) << "\n" << jacobian_rz;
  139. // clang-format on
  140. }
  141. // Trivial cost function that accepts three arguments.
  142. class LocallyParameterizedCostFunction : public SizedCostFunction<3, 2, 3, 4> {
  143. public:
  144. bool Evaluate(double const* const* parameters,
  145. double* residuals,
  146. double** jacobians) const final {
  147. for (int i = 0; i < num_residuals(); ++i) {
  148. residuals[i] = i;
  149. }
  150. if (jacobians) {
  151. for (int k = 0; k < 3; ++k) {
  152. // The jacobians here are full sized, but they are transformed in the
  153. // evaluator into the "local" jacobian. In the tests, the
  154. // "SubsetManifold" is used, which should pick out columns from these
  155. // jacobians. Put values in the jacobian that make this obvious; in
  156. // particular, make the jacobians like this:
  157. //
  158. // 0 1 2 3 4 ...
  159. // 0 1 2 3 4 ...
  160. // 0 1 2 3 4 ...
  161. //
  162. if (jacobians[k] != nullptr) {
  163. MatrixRef jacobian(
  164. jacobians[k], num_residuals(), parameter_block_sizes()[k]);
  165. for (int j = 0; j < k + 2; ++j) {
  166. jacobian.col(j).setConstant(j);
  167. }
  168. }
  169. }
  170. }
  171. return true;
  172. }
  173. };
  174. TEST(ResidualBlock, EvaluateWithManifolds) {
  175. double scratch[64];
  176. // Prepare the parameter blocks.
  177. double values_x[2];
  178. ParameterBlock x(values_x, 2, -1);
  179. double values_y[3];
  180. ParameterBlock y(values_y, 3, -1);
  181. double values_z[4];
  182. ParameterBlock z(values_z, 4, -1);
  183. std::vector<ParameterBlock*> parameters;
  184. parameters.push_back(&x);
  185. parameters.push_back(&y);
  186. parameters.push_back(&z);
  187. // Make x have the first component fixed.
  188. std::vector<int> x_fixed;
  189. x_fixed.push_back(0);
  190. SubsetManifold x_manifold(2, x_fixed);
  191. x.SetManifold(&x_manifold);
  192. // Make z have the last and last component fixed.
  193. std::vector<int> z_fixed;
  194. z_fixed.push_back(2);
  195. SubsetManifold z_manifold(4, z_fixed);
  196. z.SetManifold(&z_manifold);
  197. LocallyParameterizedCostFunction cost_function;
  198. // Create the object under tests.
  199. ResidualBlock residual_block(&cost_function, nullptr, parameters, -1);
  200. // Verify getters.
  201. EXPECT_EQ(&cost_function, residual_block.cost_function());
  202. EXPECT_EQ(nullptr, residual_block.loss_function());
  203. EXPECT_EQ(parameters[0], residual_block.parameter_blocks()[0]);
  204. EXPECT_EQ(parameters[1], residual_block.parameter_blocks()[1]);
  205. EXPECT_EQ(parameters[2], residual_block.parameter_blocks()[2]);
  206. EXPECT_EQ(3 * (2 + 4) + 3, residual_block.NumScratchDoublesForEvaluate());
  207. // Verify cost-only evaluation.
  208. double cost;
  209. residual_block.Evaluate(true, &cost, nullptr, nullptr, scratch);
  210. EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost);
  211. // Verify cost and residual evaluation.
  212. double residuals[3];
  213. residual_block.Evaluate(true, &cost, residuals, nullptr, scratch);
  214. EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost);
  215. EXPECT_EQ(0.0, residuals[0]);
  216. EXPECT_EQ(1.0, residuals[1]);
  217. EXPECT_EQ(2.0, residuals[2]);
  218. // Verify cost, residual, and jacobian evaluation.
  219. cost = 0.0;
  220. VectorRef(residuals, 3).setConstant(0.0);
  221. Matrix jacobian_rx(3, 1); // Since the first element is fixed.
  222. Matrix jacobian_ry(3, 3);
  223. Matrix jacobian_rz(3, 3); // Since the third element is fixed.
  224. jacobian_rx.setConstant(-1.0);
  225. jacobian_ry.setConstant(-1.0);
  226. jacobian_rz.setConstant(-1.0);
  227. double* jacobian_ptrs[3] = {
  228. jacobian_rx.data(), jacobian_ry.data(), jacobian_rz.data()};
  229. residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch);
  230. EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost);
  231. EXPECT_EQ(0.0, residuals[0]);
  232. EXPECT_EQ(1.0, residuals[1]);
  233. EXPECT_EQ(2.0, residuals[2]);
  234. // clang-format off
  235. Matrix expected_jacobian_rx(3, 1);
  236. expected_jacobian_rx << 1.0, 1.0, 1.0;
  237. Matrix expected_jacobian_ry(3, 3);
  238. expected_jacobian_ry << 0.0, 1.0, 2.0,
  239. 0.0, 1.0, 2.0,
  240. 0.0, 1.0, 2.0;
  241. Matrix expected_jacobian_rz(3, 3);
  242. expected_jacobian_rz << 0.0, 1.0, /* 2.0, */ 3.0, // 3rd parameter constant.
  243. 0.0, 1.0, /* 2.0, */ 3.0,
  244. 0.0, 1.0, /* 2.0, */ 3.0;
  245. EXPECT_EQ(expected_jacobian_rx, jacobian_rx)
  246. << "\nExpected:\n" << expected_jacobian_rx
  247. << "\nActual:\n" << jacobian_rx;
  248. EXPECT_EQ(expected_jacobian_ry, jacobian_ry)
  249. << "\nExpected:\n" << expected_jacobian_ry
  250. << "\nActual:\n" << jacobian_ry;
  251. EXPECT_EQ(expected_jacobian_rz, jacobian_rz)
  252. << "\nExpected:\n " << expected_jacobian_rz
  253. << "\nActual:\n" << jacobian_rz;
  254. // clang-format on
  255. // Verify cost, residual, and partial jacobian evaluation.
  256. cost = 0.0;
  257. VectorRef(residuals, 3).setConstant(0.0);
  258. jacobian_rx.setConstant(-1.0);
  259. jacobian_ry.setConstant(-1.0);
  260. jacobian_rz.setConstant(-1.0);
  261. jacobian_ptrs[1] = nullptr; // Don't compute the jacobian for y.
  262. residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch);
  263. EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost);
  264. EXPECT_EQ(0.0, residuals[0]);
  265. EXPECT_EQ(1.0, residuals[1]);
  266. EXPECT_EQ(2.0, residuals[2]);
  267. EXPECT_EQ(expected_jacobian_rx, jacobian_rx);
  268. EXPECT_TRUE((jacobian_ry.array() == -1.0).all()) << "\n" << jacobian_ry;
  269. EXPECT_EQ(expected_jacobian_rz, jacobian_rz);
  270. }
  271. } // namespace ceres::internal