| 1 | // g2o - General Graph Optimization |
| 2 | // Copyright (C) 2014 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/types/slam3d/isometry3d_mappings.h" |
| 28 | #include "gtest/gtest.h" |
| 29 | |
| 30 | using namespace std; |
| 31 | |
| 32 | TEST(Slam3D, OrthogonalMatrix) { |
| 33 | g2o::Matrix3 shouldBeIdentity; |
| 34 | g2o::Matrix3 R = g2o::Matrix3::Identity(); |
| 35 | g2o::Matrix3 rot = (g2o::Matrix3)g2o::AngleAxis(0.01, g2o::Vector3::UnitZ()); |
| 36 | rot = rot * (g2o::Matrix3)g2o::AngleAxis(0.01, g2o::Vector3::UnitX()); |
| 37 | |
| 38 | shouldBeIdentity = R * R.transpose(); |
| 39 | double initialDifference = |
| 40 | (shouldBeIdentity - g2o::Matrix3::Identity()).array().abs().maxCoeff(); |
| 41 | EXPECT_DOUBLE_EQ(0., initialDifference); |
| 42 | |
| 43 | // introduce numerical inaccuracies |
| 44 | for (int i = 0; i < 10000; ++i) R = R * rot; |
| 45 | shouldBeIdentity = R * R.transpose(); |
| 46 | double afterMultDifference = |
| 47 | (shouldBeIdentity - g2o::Matrix3::Identity()).array().abs().maxCoeff(); |
| 48 | EXPECT_GE(afterMultDifference, initialDifference); |
| 49 | |
| 50 | double inaccurateDet = R.determinant(); |
| 51 | g2o::Matrix3 approxSolution = R; |
| 52 | g2o::internal::approximateNearestOrthogonalMatrix(approxSolution); |
| 53 | g2o::internal::nearestOrthogonalMatrix(R); |
| 54 | |
| 55 | EXPECT_LE(std::abs(R.determinant() - 1.), std::abs(inaccurateDet - 1.)); |
| 56 | EXPECT_NEAR(1.0, R.determinant(), 1e-7); |
| 57 | shouldBeIdentity = R * R.transpose(); |
| 58 | double nearestDifference = |
| 59 | (shouldBeIdentity - g2o::Matrix3::Identity()).array().abs().maxCoeff(); |
| 60 | EXPECT_NEAR(0., nearestDifference, 1e-7); |
| 61 | |
| 62 | // norm of the comluns |
| 63 | for (int i = 0; i < 3; ++i) EXPECT_NEAR(1.0, R.col(i).norm(), 1e-7); |
| 64 | |
| 65 | shouldBeIdentity = approxSolution * approxSolution.transpose(); |
| 66 | double approxNearestDifference = |
| 67 | (shouldBeIdentity - Eigen::Matrix3d::Identity()).array().abs().maxCoeff(); |
| 68 | EXPECT_NEAR(0., approxNearestDifference, 1e-6); |
| 69 | EXPECT_NEAR(1.0, approxSolution.determinant(), 1e-6); |
| 70 | for (int i = 0; i < 3; ++i) |
| 71 | EXPECT_NEAR(1.0, approxSolution.col(i).norm(), 1e-6); |
| 72 | } |