Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2015 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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 31 | #include "ceres/residual_block_utils.h" |
| 32 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 33 | #include <cmath> |
| 34 | #include <limits> |
| 35 | #include <memory> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 36 | |
| 37 | #include "ceres/cost_function.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | #include "ceres/parameter_block.h" |
| 39 | #include "ceres/residual_block.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 40 | #include "ceres/sized_cost_function.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 41 | #include "gtest/gtest.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 42 | |
| 43 | namespace ceres { |
| 44 | namespace internal { |
| 45 | |
| 46 | // Routine to check if ResidualBlock::Evaluate for unary CostFunction |
| 47 | // with one residual succeeds with true or dies. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 48 | static void CheckEvaluation(const CostFunction& cost_function, bool is_good) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 49 | double x = 1.0; |
| 50 | ParameterBlock parameter_block(&x, 1, -1); |
| 51 | std::vector<ParameterBlock*> parameter_blocks; |
| 52 | parameter_blocks.push_back(¶meter_block); |
| 53 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 54 | ResidualBlock residual_block(&cost_function, NULL, parameter_blocks, -1); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 55 | |
| 56 | std::unique_ptr<double[]> scratch( |
| 57 | new double[residual_block.NumScratchDoublesForEvaluate()]); |
| 58 | |
| 59 | double cost; |
| 60 | double residuals; |
| 61 | double jacobian; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 62 | double* jacobians[] = {&jacobian}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 63 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 64 | EXPECT_EQ(residual_block.Evaluate( |
| 65 | true, &cost, &residuals, jacobians, scratch.get()), |
| 66 | is_good); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // A CostFunction that behaves normaly, i.e., it computes numerically |
| 70 | // valid residuals and jacobians. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 71 | class GoodCostFunction : public SizedCostFunction<1, 1> { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 72 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 73 | bool Evaluate(double const* const* parameters, |
| 74 | double* residuals, |
| 75 | double** jacobians) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 76 | residuals[0] = 1; |
| 77 | if (jacobians != NULL && jacobians[0] != NULL) { |
| 78 | jacobians[0][0] = 0.0; |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | // The following four CostFunctions simulate the different ways in |
| 85 | // which user code can cause ResidualBlock::Evaluate to fail. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 86 | class NoResidualUpdateCostFunction : public SizedCostFunction<1, 1> { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 87 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 88 | bool Evaluate(double const* const* parameters, |
| 89 | double* residuals, |
| 90 | double** jacobians) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 91 | // Forget to update the residuals. |
| 92 | // residuals[0] = 1; |
| 93 | if (jacobians != NULL && jacobians[0] != NULL) { |
| 94 | jacobians[0][0] = 0.0; |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | }; |
| 99 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 100 | class NoJacobianUpdateCostFunction : public SizedCostFunction<1, 1> { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 101 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 102 | bool Evaluate(double const* const* parameters, |
| 103 | double* residuals, |
| 104 | double** jacobians) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 105 | residuals[0] = 1; |
| 106 | if (jacobians != NULL && jacobians[0] != NULL) { |
| 107 | // Forget to update the jacobians. |
| 108 | // jacobians[0][0] = 0.0; |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | }; |
| 113 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 114 | class BadResidualCostFunction : public SizedCostFunction<1, 1> { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 115 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 116 | bool Evaluate(double const* const* parameters, |
| 117 | double* residuals, |
| 118 | double** jacobians) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 119 | residuals[0] = std::numeric_limits<double>::infinity(); |
| 120 | if (jacobians != NULL && jacobians[0] != NULL) { |
| 121 | jacobians[0][0] = 0.0; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | }; |
| 126 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 127 | class BadJacobianCostFunction : public SizedCostFunction<1, 1> { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 128 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 129 | bool Evaluate(double const* const* parameters, |
| 130 | double* residuals, |
| 131 | double** jacobians) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 132 | residuals[0] = 1.0; |
| 133 | if (jacobians != NULL && jacobians[0] != NULL) { |
| 134 | jacobians[0][0] = std::numeric_limits<double>::quiet_NaN(); |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | }; |
| 139 | |
| 140 | // Note: It is preferable to write the below test as: |
| 141 | // |
| 142 | // CheckEvaluation(GoodCostFunction(), true); |
| 143 | // CheckEvaluation(NoResidualUpdateCostFunction(), false); |
| 144 | // CheckEvaluation(NoJacobianUpdateCostFunction(), false); |
| 145 | // ... |
| 146 | // |
| 147 | // however, there is a bug in the version of GCC on Mac OS X we tested, which |
| 148 | // requires the objects get put into local variables instead of getting |
| 149 | // instantiated on the stack. |
| 150 | TEST(ResidualBlockUtils, CheckAllCombinationsOfBadness) { |
| 151 | GoodCostFunction good_fun; |
| 152 | CheckEvaluation(good_fun, true); |
| 153 | NoResidualUpdateCostFunction no_residual; |
| 154 | CheckEvaluation(no_residual, false); |
| 155 | NoJacobianUpdateCostFunction no_jacobian; |
| 156 | CheckEvaluation(no_jacobian, false); |
| 157 | BadResidualCostFunction bad_residual; |
| 158 | CheckEvaluation(bad_residual, false); |
| 159 | BadJacobianCostFunction bad_jacobian; |
| 160 | CheckEvaluation(bad_jacobian, false); |
| 161 | } |
| 162 | |
| 163 | } // namespace internal |
| 164 | } // namespace ceres |