| 1 | // This file is part of the AliceVision project. |
| 2 | // Copyright (c) 2016 AliceVision contributors. |
| 3 | // Copyright (c) 2012 openMVG contributors. |
| 4 | // This Source Code Form is subject to the terms of the Mozilla Public License, |
| 5 | // v. 2.0. If a copy of the MPL was not distributed with this file, |
| 6 | // You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | |
| 8 | #include <aliceVision/feature/imageDescriberCommon.hpp> |
| 9 | #include <aliceVision/sfm/utils/statistics.hpp> |
| 10 | #include <aliceVision/sfm/sfm.hpp> |
| 11 | #include <aliceVision/sfm/pipeline/panorama/ReconstructionEngine_panorama.hpp> |
| 12 | |
| 13 | #include <cmath> |
| 14 | #include <cstdio> |
| 15 | #include <iostream> |
| 16 | |
| 17 | #include <boost/test/unit_test.hpp> |
| 18 | |
| 19 | using namespace aliceVision; |
| 20 | |
| 21 | // Test summary: |
| 22 | // - Create a rotation matrix between two views |
| 23 | // - Create two matrices of calibration for two views |
| 24 | |
| 25 | void test_panorama(std::shared_ptr<camera::IntrinsicBase>& intrinsic_gt, |
| 26 | std::shared_ptr<camera::IntrinsicBase>& intrinsic_noisy, |
| 27 | double ratio_inliers) |
| 28 | { |
| 29 | sfmData::SfMData sfmdata; |
| 30 | sfmdata.getIntrinsics().emplace(0, intrinsic_noisy); |
| 31 | |
| 32 | /*Create cameras */ |
| 33 | std::vector<geometry::Pose3> poses_gt; |
| 34 | size_t count = 0; |
| 35 | for (double latitude = -M_PI_2; latitude <= M_PI_2; latitude += 0.8) |
| 36 | { |
| 37 | for (double longitude = -M_PI; longitude <= M_PI; longitude += 0.5) |
| 38 | { |
| 39 | Eigen::Matrix3d matLongitude = Eigen::AngleAxisd(longitude, Vec3(0, 1, 0)).toRotationMatrix(); |
| 40 | Eigen::Matrix3d matLatitude = Eigen::AngleAxisd(latitude, Vec3(1, 0, 0)).toRotationMatrix(); |
| 41 | Eigen::Matrix3d R = matLongitude * matLatitude; |
| 42 | geometry::Pose3 pose(R, Vec3(0, 0, 0)); |
| 43 | poses_gt.push_back(pose); |
| 44 | |
| 45 | std::shared_ptr<sfmData::View> v = std::make_shared<sfmData::View>("fakeimg.png", count, 0, count, 1920, 1080); |
| 46 | sfmdata.getViews().emplace(count, v); |
| 47 | count++; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | feature::FeaturesPerView fpv; |
| 52 | matching::PairwiseMatches matches; |
| 53 | |
| 54 | std::mt19937 mt; |
| 55 | std::uniform_real_distribution<double> dist(0.0, 1.0); |
| 56 | std::normal_distribution<double> noise(0.0, 100.0); |
| 57 | |
| 58 | for (double latitude = -M_PI_2; latitude <= M_PI_2; latitude += 0.02) |
| 59 | { |
| 60 | for (double longitude = -M_PI; longitude <= M_PI; longitude += 0.02) |
| 61 | { |
| 62 | const double Px = cos(latitude) * sin(longitude); |
| 63 | const double Py = sin(latitude); |
| 64 | const double Pz = cos(latitude) * cos(longitude); |
| 65 | |
| 66 | Vec3 pt(Px, Py, Pz); |
| 67 | |
| 68 | std::vector<std::pair<size_t, size_t>> observations; |
| 69 | |
| 70 | /*Project this point on all cameras*/ |
| 71 | for (int idPose = 0; idPose < poses_gt.size(); idPose++) |
| 72 | { |
| 73 | geometry::Pose3 pose = poses_gt[idPose]; |
| 74 | |
| 75 | Vec3 ray = pose(pt); |
| 76 | if (!intrinsic_gt->isVisibleRay(ray)) |
| 77 | { |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | Vec2 im = intrinsic_gt->transformProject(pose, pt.homogeneous(), true); |
| 82 | double dice = dist(mt); |
| 83 | if (dice < ratio_inliers) |
| 84 | { |
| 85 | // Outlier: generate large error |
| 86 | im(0) += noise(mt); |
| 87 | im(1) += noise(mt); |
| 88 | } |
| 89 | if (!intrinsic_gt->isVisible(im)) |
| 90 | { |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | /*If it is visible, store a fake feature */ |
| 95 | feature::MapFeaturesPerView& map = fpv.getData(); |
| 96 | feature::PointFeatures& pfs = map[idPose][feature::EImageDescriberType::UNKNOWN]; |
| 97 | |
| 98 | /*Also store the index of this feature for this view*/ |
| 99 | observations.push_back(std::make_pair(idPose, pfs.size())); |
| 100 | |
| 101 | feature::PointFeature pf(im(0), im(1), 1.0, 0.0); |
| 102 | pfs.push_back(pf); |
| 103 | } |
| 104 | |
| 105 | for (size_t idObs1 = 0; idObs1 < observations.size(); idObs1++) |
| 106 | { |
| 107 | for (size_t idObs2 = idObs1 + 1; idObs2 < observations.size(); idObs2++) |
| 108 | { |
| 109 | size_t idPose1 = observations[idObs1].first; |
| 110 | size_t idPose2 = observations[idObs2].first; |
| 111 | |
| 112 | Pair p = std::make_pair(idPose1, idPose2); |
| 113 | |
| 114 | matching::IndMatch match(observations[idObs1].second, observations[idObs2].second); |
| 115 | |
| 116 | matching::IndMatches& vec_matches = matches[p][feature::EImageDescriberType::UNKNOWN]; |
| 117 | vec_matches.push_back(match); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | sfm::ReconstructionEngine_panorama::Params params; |
| 123 | params.eRelativeRotationMethod = sfm::RELATIVE_ROTATION_FROM_R; |
| 124 | |
| 125 | sfm::ReconstructionEngine_panorama pano(sfmdata, params, ""); |
| 126 | pano.setFeaturesProvider(&fpv); |
| 127 | pano.setMatchesProvider(&matches); |
| 128 | |
| 129 | if (!pano.process()) |
| 130 | { |
| 131 | BOOST_TEST_FAIL("Panorama processing failed"); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (!pano.adjust()) |
| 136 | { |
| 137 | BOOST_TEST_FAIL("Panorama adjustment failed"); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | sfmdata = pano.getSfMData(); |
| 142 | |
| 143 | geometry::Pose3 zRw_gt = poses_gt[0]; |
| 144 | geometry::Pose3 zRw_est = sfmdata.getAbsolutePose(0).getTransform(); |
| 145 | |
| 146 | for (int idPose = 0; idPose < poses_gt.size(); idPose++) |
| 147 | { |
| 148 | geometry::Pose3 cRw_gt = poses_gt[idPose]; |
| 149 | geometry::Pose3 cRw_est = sfmdata.getAbsolutePose(idPose).getTransform(); |
| 150 | |
| 151 | Eigen::Matrix3d cRz_gt = cRw_gt.rotation() * zRw_gt.rotation().transpose(); |
| 152 | Eigen::Matrix3d cRz_est = cRw_est.rotation() * zRw_est.rotation().transpose(); |
| 153 | |
| 154 | Eigen::Matrix3d delta = cRz_gt * cRz_est.transpose(); |
| 155 | |
| 156 | Eigen::AngleAxisd aa; |
| 157 | aa.fromRotationMatrix(delta); |
| 158 | BOOST_CHECK_LT(aa.angle(), 1e-2); |
| 159 | } |
| 160 | } |