| 1 | // g2o - General Graph Optimization |
| 2 | // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer in the |
| 13 | // documentation and/or other materials provided with the distribution. |
| 14 | // |
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 16 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 17 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 18 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 19 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 21 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 23 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 24 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | |
| 27 | #include "allocate_optimizer.h" |
| 28 | #include "g2o/core/robust_kernel_factory.h" |
| 29 | #include "g2o/core/robust_kernel_impl.h" |
| 30 | #include "g2o/stuff/misc.h" |
| 31 | #include "gmock/gmock.h" |
| 32 | |
| 33 | using namespace testing; |
| 34 | |
| 35 | TEST(General, RobustKernelFactory) { |
| 36 | g2o::RobustKernelFactory* factory = g2o::RobustKernelFactory::instance(); |
| 37 | |
| 38 | std::vector<std::string> kernels; |
| 39 | factory->fillKnownKernels(kernels); |
| 40 | |
| 41 | ASSERT_THAT(kernels, SizeIs(Gt(0))); |
| 42 | |
| 43 | for (const auto& s : kernels) { |
| 44 | ASSERT_THAT(factory->creator(s), NotNull()); |
| 45 | } |
| 46 | for (const auto& s : kernels) { |
| 47 | g2o::RobustKernel* kernel = factory->construct(s); |
| 48 | ASSERT_THAT(kernel, NotNull()); |
| 49 | delete kernel; |
| 50 | } |
| 51 | |
| 52 | // remove one kernel |
| 53 | std::string kernelToRemove = kernels[kernels.size() / 2]; |
| 54 | factory->unregisterType(kernelToRemove); |
| 55 | // check that we cannot create it |
| 56 | ASSERT_THAT(factory->creator(kernelToRemove), IsNull()); |
| 57 | ASSERT_THAT(factory->construct(kernelToRemove), IsNull()); |
| 58 | // check that we removed exactky that desired kernel |
| 59 | std::vector<std::string> kernelsAfterRemoval; |
| 60 | factory->fillKnownKernels(kernelsAfterRemoval); |
| 61 | ASSERT_THAT(kernelsAfterRemoval, Not(Contains(kernelToRemove))); |
| 62 | ASSERT_THAT(kernelsAfterRemoval, IsSubsetOf(kernels)); |
| 63 | ASSERT_THAT(kernelsAfterRemoval, SizeIs(kernels.size() - 1)); |
| 64 | kernelsAfterRemoval.push_back(kernelToRemove); |
| 65 | ASSERT_THAT(kernelsAfterRemoval, UnorderedElementsAreArray(kernels)); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Type parameterized class for a fixture to setup a linear solver along with |
| 70 | * some data of a linear system to be solved. |
| 71 | */ |
| 72 | template <typename T> |
| 73 | class RobustKernelTests : public Test { |
| 74 | public: |
| 75 | using Kernel = T; |
| 76 | |
| 77 | RobustKernelTests() : Test() { |
| 78 | kernel_.setDelta(0.07); |
| 79 | |
| 80 | error_values_.reserve(3); |
| 81 | error_values_.push_back(0.99 * kernel_.delta() * kernel_.delta()); |
| 82 | error_values_.push_back(0.99 * kernel_.delta()); |
| 83 | error_values_.push_back(1.5 * kernel_.delta()); |
| 84 | } |
| 85 | |
| 86 | protected: |
| 87 | Kernel kernel_; |
| 88 | std::vector<double> error_values_; |
| 89 | |
| 90 | //! Evaluate the robust kernel function |
| 91 | double eval(double val) const { |
| 92 | g2o::Vector3 result; |
| 93 | kernel_.robustify(val, result); |
| 94 | return result[0]; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Estimate the first order derivative numerically |
| 99 | */ |
| 100 | double estimateDerivative(double x) { |
| 101 | constexpr double delta = g2o::cst(1e-9); |
| 102 | |
| 103 | double result = (1 / (2 * delta)) * (eval(x + delta) - eval(x - delta)); |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | double estimate2ndDerivative(double x) { |
| 108 | constexpr double kEps = 1e-5; |
| 109 | |
| 110 | const double result1 = |
| 111 | (1. / (kEps * kEps)) * (2. * eval(x) - 5. * eval(x - kEps) + |
| 112 | 4 * eval(x - 2. * kEps) - eval(x - 3 * kEps)); |
| 113 | const double result2 = |
| 114 | (1. / (kEps * kEps)) * (2. * eval(x) - 5. * eval(x + kEps) + |
| 115 | 4 * eval(x + 2. * kEps) - eval(x + 3 * kEps)); |
| 116 | return 0.5 * result1 + 0.5 * result2; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | TYPED_TEST_SUITE_P(RobustKernelTests); |
| 121 | |
| 122 | TYPED_TEST_P(RobustKernelTests, Values) { |
| 123 | for (auto e : this->error_values_) { |
| 124 | g2o::Vector3 val = g2o::Vector3::Zero(); |
| 125 | this->kernel_.robustify(e, val); |
| 126 | EXPECT_THAT(val(0), Ne(0.)); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | TYPED_TEST_P(RobustKernelTests, Derivative) { |
| 131 | for (auto e : this->error_values_) { |
| 132 | g2o::Vector3 val = g2o::Vector3::Zero(); |
| 133 | this->kernel_.robustify(e, val); |
| 134 | double estimatedJac = this->estimateDerivative(e); |
| 135 | EXPECT_THAT(val(1), DoubleNear(estimatedJac, 1e-5)); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | TYPED_TEST_P(RobustKernelTests, SecondDerivative) { |
| 140 | for (auto e : this->error_values_) { |
| 141 | g2o::Vector3 val = g2o::Vector3::Zero(); |
| 142 | this->kernel_.robustify(e, val); |
| 143 | double estimated2ndDerivative = this->estimate2ndDerivative(e); |
| 144 | EXPECT_THAT(val(2), DoubleNear(estimated2ndDerivative, 1e-3)); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // clang-format off |
| 149 | REGISTER_TYPED_TEST_SUITE_P(RobustKernelTests, Values, Derivative, SecondDerivative); |
| 150 | using RobustKernelTypes = ::testing::Types< |
| 151 | g2o::RobustKernelHuber, |
| 152 | g2o::RobustKernelPseudoHuber, |
| 153 | g2o::RobustKernelCauchy, |
| 154 | g2o::RobustKernelGemanMcClure, |
| 155 | g2o::RobustKernelWelsch, |
| 156 | g2o::RobustKernelFair, |
| 157 | g2o::RobustKernelTukey, |
| 158 | g2o::RobustKernelSaturated, |
| 159 | g2o::RobustKernelDCS |
| 160 | >; |
| 161 | INSTANTIATE_TYPED_TEST_SUITE_P(General, RobustKernelTests, RobustKernelTypes); |
| 162 | // clang-format on |