| 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 "g2o/autodiff/autodiff.h" |
| 28 | #include "g2o/core/jacobian_workspace.h" |
| 29 | #include "g2o/core/optimizable_graph.h" |
| 30 | #include "g2o/types/slam3d/dquat2mat.h" |
| 31 | #include "g2o/types/slam3d/edge_pointxyz.h" |
| 32 | #include "g2o/types/slam3d/edge_se3.h" |
| 33 | #include "g2o/types/slam3d/edge_se3_pointxyz.h" |
| 34 | #include "g2o/types/slam3d/edge_se3_xyzprior.h" |
| 35 | #include "gtest/gtest.h" |
| 36 | #include "unit_test/test_helper/evaluate_jacobian.h" |
| 37 | #include "unit_test/test_helper/random_state.h" |
| 38 | |
| 39 | using namespace std; |
| 40 | using namespace g2o; |
| 41 | using namespace g2o::internal; |
| 42 | |
| 43 | TEST(Slam3D, EdgeSE3Jacobian) { |
| 44 | VertexSE3 v1; |
| 45 | v1.setId(0); |
| 46 | |
| 47 | VertexSE3 v2; |
| 48 | v2.setId(1); |
| 49 | |
| 50 | EdgeSE3 e; |
| 51 | e.setVertex(0, &v1); |
| 52 | e.setVertex(1, &v2); |
| 53 | e.setInformation(EdgeSE3::InformationType::Identity()); |
| 54 | |
| 55 | JacobianWorkspace jacobianWorkspace; |
| 56 | JacobianWorkspace numericJacobianWorkspace; |
| 57 | numericJacobianWorkspace.updateSize(&e); |
| 58 | numericJacobianWorkspace.allocate(); |
| 59 | |
| 60 | for (int k = 0; k < 10000; ++k) { |
| 61 | v1.setEstimate(internal::randomIsometry3()); |
| 62 | v2.setEstimate(internal::randomIsometry3()); |
| 63 | e.setMeasurement(internal::randomIsometry3()); |
| 64 | |
| 65 | evaluateJacobian(e, jacobianWorkspace, numericJacobianWorkspace); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | TEST(Slam3D, EdgeSE3XYZPriorJacobian) { |
| 70 | VertexSE3 v1; |
| 71 | v1.setId(0); |
| 72 | |
| 73 | EdgeSE3XYZPrior e; |
| 74 | e.setVertex(0, &v1); |
| 75 | e.setInformation(EdgeSE3XYZPrior::InformationType::Identity()); |
| 76 | |
| 77 | JacobianWorkspace jacobianWorkspace; |
| 78 | JacobianWorkspace numericJacobianWorkspace; |
| 79 | numericJacobianWorkspace.updateSize(&e); |
| 80 | numericJacobianWorkspace.allocate(); |
| 81 | |
| 82 | // test in identity pose |
| 83 | v1.setEstimate(Eigen::Isometry3d::Identity()); |
| 84 | e.setMeasurement(Eigen::Vector3d::Random()); |
| 85 | evaluateJacobianUnary(e, jacobianWorkspace, numericJacobianWorkspace); |
| 86 | |
| 87 | // test in random pose |
| 88 | for (int k = 0; k < 10000; ++k) { |
| 89 | v1.setEstimate(internal::randomIsometry3()); |
| 90 | e.setMeasurement(Eigen::Vector3d::Random()); |
| 91 | |
| 92 | evaluateJacobianUnary(e, jacobianWorkspace, numericJacobianWorkspace); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | TEST(Slam3D, EdgeSE3PointXYZJacobian) { |
| 97 | OptimizableGraph graph; |
| 98 | |
| 99 | VertexSE3* v1 = new VertexSE3; |
| 100 | v1->setId(0); |
| 101 | graph.addVertex(v1); |
| 102 | |
| 103 | VertexPointXYZ* v2 = new VertexPointXYZ; |
| 104 | v2->setId(1); |
| 105 | graph.addVertex(v2); |
| 106 | |
| 107 | ParameterSE3Offset* paramOffset = new ParameterSE3Offset; |
| 108 | paramOffset->setId(0); |
| 109 | graph.addParameter(paramOffset); |
| 110 | |
| 111 | EdgeSE3PointXYZ* e = new EdgeSE3PointXYZ; |
| 112 | e->setVertex(0, v1); |
| 113 | e->setVertex(1, v2); |
| 114 | e->setInformation(EdgePointXYZ::InformationType::Identity()); |
| 115 | e->setParameterId(0, paramOffset->id()); |
| 116 | graph.addEdge(e); |
| 117 | |
| 118 | JacobianWorkspace jacobianWorkspace; |
| 119 | JacobianWorkspace numericJacobianWorkspace; |
| 120 | numericJacobianWorkspace.updateSize(e); |
| 121 | numericJacobianWorkspace.allocate(); |
| 122 | |
| 123 | for (int k = 0; k < 10000; ++k) { |
| 124 | paramOffset->setOffset(internal::randomIsometry3()); |
| 125 | v1->setEstimate(internal::randomIsometry3()); |
| 126 | v2->setEstimate(Eigen::Vector3d::Random()); |
| 127 | e->setMeasurement(Eigen::Vector3d::Random()); |
| 128 | |
| 129 | evaluateJacobian(*e, jacobianWorkspace, numericJacobianWorkspace); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | TEST(Slam3D, EdgePointXYZJacobian) { |
| 134 | VertexPointXYZ v1; |
| 135 | v1.setId(0); |
| 136 | |
| 137 | VertexPointXYZ v2; |
| 138 | v2.setId(1); |
| 139 | |
| 140 | EdgePointXYZ e; |
| 141 | e.setVertex(0, &v1); |
| 142 | e.setVertex(1, &v2); |
| 143 | e.setInformation(EdgePointXYZ::InformationType::Identity()); |
| 144 | |
| 145 | JacobianWorkspace jacobianWorkspace; |
| 146 | JacobianWorkspace numericJacobianWorkspace; |
| 147 | numericJacobianWorkspace.updateSize(&e); |
| 148 | numericJacobianWorkspace.allocate(); |
| 149 | |
| 150 | for (int k = 0; k < 10000; ++k) { |
| 151 | v1.setEstimate(Eigen::Vector3d::Random()); |
| 152 | v2.setEstimate(Eigen::Vector3d::Random()); |
| 153 | e.setMeasurement(Eigen::Vector3d::Random()); |
| 154 | |
| 155 | evaluateJacobian(e, jacobianWorkspace, numericJacobianWorkspace); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * \brief Functor used to compute the Jacobian via AD |
| 161 | */ |
| 162 | struct RotationMatrix2QuaternionManifold { |
| 163 | template <typename T> |
| 164 | bool operator()(const T* rotMatSerialized, T* quaternion) const { |
| 165 | typename Eigen::Matrix<T, 3, 3, Eigen::ColMajor>::ConstMapType R( |
| 166 | rotMatSerialized); |
| 167 | |
| 168 | T t = R.trace(); |
| 169 | if (t > T(0)) { |
| 170 | // cerr << "w"; |
| 171 | t = sqrt(t + T(1)); |
| 172 | // T w = T(0.5)*t; |
| 173 | t = T(0.5) / t; |
| 174 | quaternion[0] = (R(2, 1) - R(1, 2)) * t; |
| 175 | quaternion[1] = (R(0, 2) - R(2, 0)) * t; |
| 176 | quaternion[2] = (R(1, 0) - R(0, 1)) * t; |
| 177 | } else { |
| 178 | int i = 0; |
| 179 | if (R(1, 1) > R(0, 0)) i = 1; |
| 180 | if (R(2, 2) > R(i, i)) i = 2; |
| 181 | int j = (i + 1) % 3; |
| 182 | int k = (j + 1) % 3; |
| 183 | // cerr << i; |
| 184 | |
| 185 | t = sqrt(R(i, i) - R(j, j) - R(k, k) + T(1.0)); |
| 186 | quaternion[i] = T(0.5) * t; |
| 187 | t = T(0.5) / t; |
| 188 | quaternion[j] = (R(j, i) + R(i, j)) * t; |
| 189 | quaternion[k] = (R(k, i) + R(i, k)) * t; |
| 190 | T w = (R(k, j) - R(j, k)) * t; |
| 191 | // normalize to our manifold, such that w is positive |
| 192 | if (w < cst(0)) { |
| 193 | // cerr << " normalizing w > 0 "; |
| 194 | for (int l = 0; l < 3; ++l) quaternion[l] *= T(-1); |
| 195 | } |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | TEST(Slam3D, dqDRJacobian) { |
| 202 | Eigen::Matrix<double, 3, 9, Eigen::ColMajor> dq_dR; |
| 203 | Eigen::Matrix<double, 3, 9, Eigen::RowMajor> dq_dR_AD; |
| 204 | dq_dR_AD.setZero(); // avoid warning about uninitialized memory |
| 205 | for (int k = 0; k < 10000; ++k) { |
| 206 | // create a random rotation matrix by sampling a random 3d vector |
| 207 | // that will be used in axis-angle representation to create the matrix |
| 208 | Vector3 rotAxisAngle = Vector3::Random(); |
| 209 | rotAxisAngle += Vector3::Random(); |
| 210 | AngleAxis rotation(rotAxisAngle.norm(), rotAxisAngle.normalized()); |
| 211 | Matrix3 Re = rotation.toRotationMatrix(); |
| 212 | |
| 213 | // our analytic function which we want to evaluate |
| 214 | Eigen::Matrix<double, 3, 9, Eigen::ColMajor> dq_dR; |
| 215 | compute_dq_dR(dq_dR, Re(0, 0), Re(1, 0), Re(2, 0), Re(0, 1), Re(1, 1), |
| 216 | Re(2, 1), Re(0, 2), Re(1, 2), Re(2, 2)); |
| 217 | |
| 218 | // compute the Jacobian using AD |
| 219 | double* parameters[] = {Re.data()}; |
| 220 | double* jacobians[] = {dq_dR_AD.data()}; |
| 221 | double value[3]; |
| 222 | RotationMatrix2QuaternionManifold rot2quat; |
| 223 | using RotationMatrix2QuaternionManifoldDims = |
| 224 | ceres::internal::StaticParameterDims<9>; |
| 225 | ceres::internal::AutoDifferentiate<3, RotationMatrix2QuaternionManifoldDims, |
| 226 | RotationMatrix2QuaternionManifold, |
| 227 | double>(rot2quat, parameters, 3, value, |
| 228 | jacobians); |
| 229 | |
| 230 | double maxDifference = (dq_dR - dq_dR_AD).array().abs().maxCoeff(); |
| 231 | EXPECT_NEAR(0., maxDifference, 1e-7); |
| 232 | } |
| 233 | } |