1 // This file is part of OpenMVG, an Open Multiple View Geometry C++ library.
2 
3 // Copyright (c) 2018 Yan Qingsong, Pierre Moulon.
4 
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #ifndef IO_READ_GT_DTUMVS_HPP
10 #define IO_READ_GT_DTUMVS_HPP
11 
12 
13 #include "io_readGTInterface.hpp"
14 #include "io_loadImages.hpp"
15 
16 #include "openMVG/cameras/PinholeCamera.hpp"
17 
18 // The feature of the DTU_MVS's Data:
19 // 1. each gt file only stores one view's pose information
20 // 2. the gt file's name contains number that matches with the image
21 // 3. the gt files' number(64) doesn't match the number of the images(49,64)
22 class SfM_Data_GT_Loader_DTU_MVS : public SfM_Data_GT_Loader_Interface
23 {
24 private:
25     std::vector<cameras::PinholeCamera> cameras_data_; // Store all the camera information
26 public:
loadGT()27   bool loadGT() override
28   {
29     // Check all the files under the path
30     std::vector<std::string> gt_files = stlplus::folder_files( this->gt_dir_ );
31     std::sort(gt_files.begin(), gt_files.end());
32 
33     // Check the image_dir to find the image names
34     std::vector<std::string> image_files = stlplus::folder_files( this->image_dir_ );
35     std::sort(image_files.begin(), image_files.end());
36 
37     cameras_data_.reserve(gt_files.size());
38 
39     // Load the gt_data from the file
40     for ( const auto & gt_file_it : gt_files)
41     {
42       std::ifstream gt_file( stlplus::create_filespec(this->gt_dir_, gt_file_it), std::ifstream::in);
43       if (!gt_file)
44       {
45         std::cerr << "Error: Failed to open file '" << gt_file_it << "' for reading" << std::endl;
46         continue;
47       }
48 
49       std::vector<double> val;
50       while (gt_file)
51       {
52         double valT;
53         gt_file >> valT;
54         if (!gt_file.fail())
55           val.push_back(valT);
56       }
57 
58       gt_file.close();
59       if (val.size() == 12)
60       {
61         Mat34 P;
62         P << val[0], val[1], val[2], val[3],
63              val[4], val[5], val[6], val[7],
64              val[8], val[9], val[10], val[11];
65 
66         cameras_data_.emplace_back(P);
67 
68         // Parse image name
69         const std::string index = stlplus::basename_part(stlplus::create_filespec(this->gt_dir_, gt_file_it)).substr(4, 3);
70 
71         // Check image exist
72         for ( const auto image_file_it : image_files)
73         {
74           const std::string image_index = image_file_it.substr(5,3); // Match code
75           if (image_index == index)
76           {
77             images_.push_back( image_file_it );
78             break;
79           }
80         }
81       }
82       else
83       {
84         continue;
85       }
86     }
87 
88     return true;
89   }
90 
loadImages()91   bool loadImages() override
92   {
93     return LoadImages(this->image_dir_, this->images_, this->cameras_data_, this->sfm_data_);
94   }
95 };
96 
97 
98 #endif // IO_READ_GT_DTUMVS_HPP
99