-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframework-source.cpp
293 lines (196 loc) · 7.88 KB
/
framework-source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* sfs-framework
*
* Nils Hamel - nils.hamel@bluewin.ch
* Charles Papon - charles.papon.90@gmail.com
* Copyright (c) 2019-2020 DHLAB, EPFL & HES-SO Valais-Wallis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "framework-source.hpp"
//
// Sparse source
//
SourceSparse::SourceSparse(std::string imageFolder, std::string firstImage, std::string lastImage, uint32_t increment, double scale) :
Source(
increment,
scale
)
{
/* Image path */
fs::path imagePath(fs::u8path(imageFolder));
/* Image extension */
fs::path imageExtension;
/* Detect valid image in folder */
for(auto & entry: fs::directory_iterator(imagePath)){
/* Extract image extension */
imageExtension = fs::path(entry.path().u8string()).extension();
/* Selection based on file extension */
if((imageExtension==".bmp")||(imageExtension==".jpg")||(imageExtension==".png")||(imageExtension==".tif")){
/* Display pushed file name */
std::cout << "Pushing image " << entry.path().filename() << " ..." << std::endl;
/* Push image in the list */
files.push_back(entry.path().u8string());
}
}
/* sort files list alphabetically */
std::sort(files.begin(), files.end());
/* check for file range boundary */
if(firstImage.empty()){
/* initialise file index */
fileIndex = 0;
}else{
/* detect index of specified initial file */
if((fileIndex = findInVector(files, firstImage).second)<0){
/* display warning */
std::cerr << "Warning : unable to locate specified frist file. Using first file in the list" << std::endl;
/* initialise file index */
fileIndex = 0;
}
}
/* check for file range boundary */
if(lastImage.empty()){
/* initialise last index */
fileLastIndex = files.size();
}else{
/* detect index of specified initial file */
if((fileIndex = findInVector(files, lastImage).second)<0){
/* display warning */
std::cerr << "Warning : unable to locate specified last file. Using last file in the list" << std::endl;
/* initialise last index */
fileLastIndex = files.size();
}
}
}
std::shared_ptr<Viewpoint> SourceSparse::next(){
/* Create new viewpoint instance */
std::shared_ptr<Viewpoint> pushViewpoint = std::make_shared<Viewpoint>();
/* import and scale image */
if(pushViewpoint->setImage(files[fileIndex], imageScale)==false){
/* send critical message */
throw std::runtime_error("Error : unable to import image " + files[fileIndex]);
}
/* assign viewpoint uid (filename) */
pushViewpoint->uid = fs::path(files[fileIndex]).filename();
/* update file index */
fileIndex += fileIncrement;
/* return created viewpoint */
return pushViewpoint;
}
bool SourceSparse::hasNext(){
/* Detect end of image list */
return fileIndex < fileLastIndex;
}
//
// Dense source
//
SourceDense::SourceDense(std::string imageFolder, std::string transformationFile, std::string firstImage, std::string lastImage, int increment, double scale) :
Source(
increment,
scale
),
pictureFolder(imageFolder)
{
/* Transformation input stream */
std::ifstream transformationStream(transformationFile);
/* Viewpoint transformation structure */
ViewPointInfo importTransformation;
/* Initialise index and boundary */
fileIndex = -1;
fileLastIndex = -1;
/* Check stream */
if(transformationStream.is_open()==false){
/* Send critical message */
throw std::runtime_error("Error : unable to read transformation file");
}
/* Reading transformation */
while(transformationStream.eof()==false) {
/* Import viewpoint uid (filename) */
if(transformationStream >> importTransformation.fileName) {
/* Display imported file name */
std::cout << "Pushing image " << importTransformation.fileName << " ..." << std::endl;
/* Import position */
transformationStream >> importTransformation.position(0);
transformationStream >> importTransformation.position(1);
transformationStream >> importTransformation.position(2);
/* Import orientation */
transformationStream >> importTransformation.orientation(0,0);
transformationStream >> importTransformation.orientation(0,1);
transformationStream >> importTransformation.orientation(0,2);
transformationStream >> importTransformation.orientation(1,0);
transformationStream >> importTransformation.orientation(1,1);
transformationStream >> importTransformation.orientation(1,2);
transformationStream >> importTransformation.orientation(2,0);
transformationStream >> importTransformation.orientation(2,1);
transformationStream >> importTransformation.orientation(2,2);
/* Push transformation on list */
list.push_back(importTransformation);
/* Detect first image boundary */
if(importTransformation.fileName==firstImage) {
/* Update index */
fileIndex = list.size() - 1;
}
/* Detect last image boundary */
if(importTransformation.fileName==lastImage) {
/* Update last index */
fileLastIndex = list.size();
}
}
}
/* Close transformation stream */
transformationStream.close();
/* Check detected boundary */
if(fileIndex<0){
/* Check specified boundary */
if(firstImage.empty()==false) {
/* display warning */
std::cerr << "Warning : unable to locate specified frist file. Using first file in the list" << std::endl;
}
/* initialise file index */
fileIndex = 0;
}
/* Check detected boundary */
if(fileLastIndex<0){
/* Check specified boundary */
if(lastImage.empty()==false){
/* Display warning */
std::cerr << "Warning : unable to locate specified last file. Using last file in the list" << std::endl;
}
/* Initialise last index */
fileLastIndex = list.size();
}
}
std::shared_ptr<Viewpoint> SourceDense::next(){
/* Create new viewpoint instance */
std::shared_ptr<Viewpoint> pushViewpoint = std::make_shared<Viewpoint>();
/* import and scale image */
if(pushViewpoint->setImage(pictureFolder + "/" + list[fileIndex].fileName, imageScale)==false){
/* send critical message */
throw std::runtime_error("Error : unable to import image " + list[fileIndex].fileName);
}
/* assign viewpoint uid (filename) */
pushViewpoint->uid = list[fileIndex].fileName;
/* assign viewpoint position */
pushViewpoint->position = list[fileIndex].position;
/* assign viewpoint orientation */
pushViewpoint->orientation = list[fileIndex].orientation;
/* update file index */
fileIndex += fileIncrement;
/* return created viewpoint */
return pushViewpoint;
}
bool SourceDense::hasNext(){
/* Detect end of image list */
return fileIndex < fileLastIndex;
}