| 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 <cmath> |
| 28 | |
| 29 | #include "g2o/core/auto_differentiation.h" |
| 30 | #include "g2o/core/base_fixed_sized_edge.h" |
| 31 | #include "g2o/types/slam2d/edge_se2.h" |
| 32 | #include "g2o/types/slam2d/vertex_point_xy.h" |
| 33 | #include "gtest/gtest.h" |
| 34 | |
| 35 | class VertexFlatSE2 : public g2o::BaseVertex<3, g2o::Vector3> { |
| 36 | public: |
| 37 | virtual void setToOriginImpl() { _estimate.setZero(); } |
| 38 | |
| 39 | virtual void oplusImpl(const double* update) { |
| 40 | _estimate += Eigen::Map<const g2o::Vector3>(update); |
| 41 | _estimate(2) = g2o::normalize_theta(_estimate(2)); |
| 42 | } |
| 43 | |
| 44 | virtual bool read(std::istream&) { return false; } |
| 45 | virtual bool write(std::ostream&) const { return false; } |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * A test case edge connecting three vertices. |
| 50 | * Here we project a point along two poses and compare it against a measurement |
| 51 | */ |
| 52 | class Edge3ADTester |
| 53 | : public g2o::BaseFixedSizedEdge<2, g2o::Vector2, VertexFlatSE2, |
| 54 | VertexFlatSE2, g2o::VertexPointXY> { |
| 55 | public: |
| 56 | using g2o::BaseFixedSizedEdge<2, g2o::Vector2, VertexFlatSE2, VertexFlatSE2, |
| 57 | g2o::VertexPointXY>::linearizeOplus; |
| 58 | |
| 59 | //! apply the SE2 pose on a point |
| 60 | template <typename T> |
| 61 | void project(const T* pose, const T* point, T* result) const { |
| 62 | T cth = cos(pose[2]); |
| 63 | T sth = sin(pose[2]); |
| 64 | |
| 65 | result[0] = pose[0] + cth * point[0] - sth * point[1]; |
| 66 | result[1] = pose[1] + sth * point[0] + cth * point[1]; |
| 67 | } |
| 68 | |
| 69 | //! implementation of the templatized error function |
| 70 | template <typename T> |
| 71 | bool operator()(const T* p1, const T* p2, const T* point, T* error) const { |
| 72 | T aux[2]; |
| 73 | project(p2, point, aux); |
| 74 | T prediction[2]; |
| 75 | project(p1, aux, prediction); |
| 76 | error[0] = prediction[0] - T(measurement()(0)); |
| 77 | error[1] = prediction[1] - T(measurement()(1)); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | // add the AD interface |
| 82 | G2O_MAKE_AUTO_AD_FUNCTIONS |
| 83 | |
| 84 | // NOOPs |
| 85 | bool read(std::istream&) override { return false; }; |
| 86 | bool write(std::ostream&) const override { return false; }; |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * Test implementation of an EdgeSE2 that implements operator() for AD |
| 91 | */ |
| 92 | class EdgeSE2AD : public g2o::EdgeSE2 { |
| 93 | public: |
| 94 | //! re-implementation of the templatized error function |
| 95 | template <typename T> |
| 96 | bool operator()(const T* p1, const T* p2, T* error) const { |
| 97 | //// original implementation |
| 98 | // SE2 delta = _inverseMeasurement * |
| 99 | // (v1->estimate().inverse()*v2->estimate()); _error = delta.toVector(); |
| 100 | |
| 101 | Iso2<T> p1Iso = pointerToIso(p1); |
| 102 | Iso2<T> p2Iso = pointerToIso(p2); |
| 103 | g2o::VectorN<3, T> invMeasAsVector = |
| 104 | _inverseMeasurement.toVector().cast<T>(); |
| 105 | Iso2<T> invMeas = pointerToIso(invMeasAsVector.data()); |
| 106 | |
| 107 | Iso2<T> delta = invMeas * (p1Iso.inverse() * p2Iso); |
| 108 | isoToPointer(delta, error); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | using g2o::BaseBinaryEdge<3, g2o::SE2, g2o::VertexSE2, |
| 113 | g2o::VertexSE2>::linearizeOplus; |
| 114 | G2O_MAKE_AUTO_AD_LINEARIZEOPLUS_BY_GET |
| 115 | |
| 116 | protected: |
| 117 | template <typename T> |
| 118 | using Iso2 = Eigen::Transform<T, 2, Eigen::Isometry, Eigen::ColMajor>; |
| 119 | |
| 120 | template <typename T> |
| 121 | static Iso2<T> pointerToIso(const T* p) { |
| 122 | T cth = cos(p[2]); |
| 123 | T sth = sin(p[2]); |
| 124 | Iso2<T> iso = Iso2<T>::Identity(); |
| 125 | iso.linear() << cth, -sth, sth, cth; |
| 126 | iso.translation()(0) = p[0]; |
| 127 | iso.translation()(1) = p[1]; |
| 128 | return iso; |
| 129 | } |
| 130 | |
| 131 | template <typename T> |
| 132 | static void isoToPointer(Iso2<T> iso, T* p) { |
| 133 | p[0] = iso.translation()(0); |
| 134 | p[1] = iso.translation()(1); |
| 135 | p[2] = atan2(iso.linear()(1, 0), iso.linear()(0, 0)); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | /** |
| 140 | * Test fixture for performing the AD tests. |
| 141 | * We setup three vertices and the edge to perform the tests |
| 142 | */ |
| 143 | class AutoDifferentiation : public ::testing::Test { |
| 144 | protected: |
| 145 | void SetUp() override { |
| 146 | v1.setEstimate(g2o::Vector3(0, 0, 0)); |
| 147 | v2.setEstimate(g2o::Vector3(1, 1, 1)); |
| 148 | point.setEstimate(g2o::Vector2(2, 2)); |
| 149 | |
| 150 | testEdge.setMeasurement(g2o::Vector2(0, 0)); |
| 151 | testEdge.setVertex(0, &v1); |
| 152 | testEdge.setVertex(1, &v2); |
| 153 | testEdge.setVertex(2, &point); |
| 154 | |
| 155 | jacobianWorkspace.updateSize(&testEdge); |
| 156 | jacobianWorkspace.allocate(); |
| 157 | } |
| 158 | VertexFlatSE2 v1; |
| 159 | VertexFlatSE2 v2; |
| 160 | g2o::VertexPointXY point; |
| 161 | Edge3ADTester testEdge; |
| 162 | g2o::JacobianWorkspace jacobianWorkspace; |
| 163 | }; |
| 164 | |
| 165 | TEST_F(AutoDifferentiation, ComputesSomething) { |
| 166 | testEdge.linearizeOplus(jacobianWorkspace); |
| 167 | |
| 168 | #if 0 |
| 169 | std::cerr << PVAR(testEdge.jacobianOplusXn<0>()) << std::endl; |
| 170 | std::cerr << PVAR(testEdge.jacobianOplusXn<1>()) << std::endl; |
| 171 | std::cerr << PVAR(testEdge.jacobianOplusXn<2>()) << std::endl; |
| 172 | #endif |
| 173 | |
| 174 | ASSERT_FALSE(testEdge.jacobianOplusXn<0>().array().isNaN().any()) |
| 175 | << "Jacobian contains NaN"; |
| 176 | ASSERT_FALSE(testEdge.jacobianOplusXn<1>().array().isNaN().any()) |
| 177 | << "Jacobian contains NaN"; |
| 178 | ASSERT_FALSE(testEdge.jacobianOplusXn<2>().array().isNaN().any()) |
| 179 | << "Jacobian contains NaN"; |
| 180 | |
| 181 | ASSERT_FALSE(testEdge.jacobianOplusXn<0>().array().isInf().any()) |
| 182 | << "Jacobian not finite"; |
| 183 | ASSERT_FALSE(testEdge.jacobianOplusXn<1>().array().isInf().any()) |
| 184 | << "Jacobian not finite"; |
| 185 | ASSERT_FALSE(testEdge.jacobianOplusXn<2>().array().isInf().any()) |
| 186 | << "Jacobian not finite"; |
| 187 | |
| 188 | ASSERT_LE(0, testEdge.jacobianOplusXn<0>().array().abs().maxCoeff()) |
| 189 | << "Jacobian is zero"; |
| 190 | ASSERT_LE(0, testEdge.jacobianOplusXn<1>().array().abs().maxCoeff()) |
| 191 | << "Jacobian is zero"; |
| 192 | ASSERT_LE(0, testEdge.jacobianOplusXn<2>().array().abs().maxCoeff()) |
| 193 | << "Jacobian is zero"; |
| 194 | } |
| 195 | |
| 196 | TEST_F(AutoDifferentiation, ComputesNothingForFixed) { |
| 197 | v2.setFixed(true); |
| 198 | |
| 199 | testEdge.linearizeOplus(jacobianWorkspace); |
| 200 | |
| 201 | ASSERT_LE(0, testEdge.jacobianOplusXn<0>().array().abs().maxCoeff()) |
| 202 | << "Jacobian is zero"; |
| 203 | ASSERT_DOUBLE_EQ(0, testEdge.jacobianOplusXn<1>().array().abs().maxCoeff()) |
| 204 | << "Jacobian is non-zero"; |
| 205 | ASSERT_LE(0, testEdge.jacobianOplusXn<2>().array().abs().maxCoeff()) |
| 206 | << "Jacobian is zero"; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Test fixture for performing the AD tests of EdgeSE2. We setup two vertices |
| 211 | * and edges to perform the tests. As edges we have the default EdgeSE2 from g2o |
| 212 | * and the EdgeSE2AD which uses an AD implementation. |
| 213 | */ |
| 214 | class AutoDifferentiationEdgeSE2 : public ::testing::Test { |
| 215 | protected: |
| 216 | void SetUp() override { |
| 217 | v1.setEstimate(g2o::Vector3(0, 0, 0)); |
| 218 | v2.setEstimate(g2o::Vector3(1, 1, 1)); |
| 219 | |
| 220 | g2o::SE2 meas(0.5, 0.5, 0.5); |
| 221 | |
| 222 | testEdge.setMeasurement(meas); |
| 223 | testEdge.setVertex(0, &v1); |
| 224 | testEdge.setVertex(1, &v2); |
| 225 | jacobianWorkspace.updateSize(&testEdge); |
| 226 | jacobianWorkspace.allocate(); |
| 227 | |
| 228 | testEdgeAd.setMeasurement(meas); |
| 229 | testEdgeAd.setVertex(0, &v1); |
| 230 | testEdgeAd.setVertex(1, &v2); |
| 231 | jacobianWorkspaceAd.updateSize(&testEdgeAd); |
| 232 | jacobianWorkspaceAd.allocate(); |
| 233 | } |
| 234 | g2o::VertexSE2 v1; |
| 235 | g2o::VertexSE2 v2; |
| 236 | g2o::EdgeSE2 testEdge; |
| 237 | EdgeSE2AD testEdgeAd; |
| 238 | g2o::JacobianWorkspace jacobianWorkspace; |
| 239 | g2o::JacobianWorkspace jacobianWorkspaceAd; |
| 240 | }; |
| 241 | |
| 242 | TEST_F(AutoDifferentiationEdgeSE2, AdComputesSomething) { |
| 243 | testEdgeAd.linearizeOplus(jacobianWorkspaceAd); |
| 244 | |
| 245 | #if 0 |
| 246 | std::cerr << PVAR(testEdgeAd.jacobianOplusXn<0>()) << std::endl; |
| 247 | std::cerr << PVAR(testEdgeAd.jacobianOplusXn<1>()) << std::endl; |
| 248 | #endif |
| 249 | |
| 250 | ASSERT_FALSE(testEdgeAd.jacobianOplusXn<0>().array().isNaN().any()) |
| 251 | << "Jacobian contains NaN"; |
| 252 | ASSERT_FALSE(testEdgeAd.jacobianOplusXn<1>().array().isNaN().any()) |
| 253 | << "Jacobian contains NaN"; |
| 254 | |
| 255 | ASSERT_FALSE(testEdgeAd.jacobianOplusXn<0>().array().isInf().any()) |
| 256 | << "Jacobian not finite"; |
| 257 | ASSERT_FALSE(testEdgeAd.jacobianOplusXn<1>().array().isInf().any()) |
| 258 | << "Jacobian not finite"; |
| 259 | |
| 260 | ASSERT_LE(0, testEdgeAd.jacobianOplusXn<0>().array().abs().maxCoeff()) |
| 261 | << "Jacobian is zero"; |
| 262 | ASSERT_LE(0, testEdgeAd.jacobianOplusXn<1>().array().abs().maxCoeff()) |
| 263 | << "Jacobian is zero"; |
| 264 | } |
| 265 | |
| 266 | TEST_F(AutoDifferentiationEdgeSE2, AdComputesCorrect) { |
| 267 | // ugly syntax to call the Jacobian into a workspace |
| 268 | testEdge |
| 269 | .g2o::BaseBinaryEdge<3, g2o::SE2, g2o::VertexSE2, |
| 270 | g2o::VertexSE2>::linearizeOplus(jacobianWorkspace); |
| 271 | // here no ugly call needed because we use the base class one directly, see |
| 272 | // above |
| 273 | testEdgeAd.linearizeOplus(jacobianWorkspaceAd); |
| 274 | |
| 275 | ASSERT_TRUE( |
| 276 | testEdgeAd.jacobianOplusXn<0>().isApprox(testEdge.jacobianOplusXn<0>())) |
| 277 | << "Jacobian differs"; |
| 278 | ASSERT_TRUE( |
| 279 | testEdgeAd.jacobianOplusXn<1>().isApprox(testEdge.jacobianOplusXn<1>())) |
| 280 | << "Jacobian differs"; |
| 281 | } |