-
-
Notifications
You must be signed in to change notification settings - Fork 827
/
SfMData.hpp
572 lines (498 loc) · 15.5 KB
/
SfMData.hpp
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// This file is part of the AliceVision project.
// Copyright (c) 2016 AliceVision contributors.
// Copyright (c) 2012 openMVG contributors.
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
#pragma once
#include <aliceVision/sfmData/CameraPose.hpp>
#include <aliceVision/sfmData/Landmark.hpp>
#include <aliceVision/sfmData/Constraint2D.hpp>
#include <aliceVision/sfmData/RotationPrior.hpp>
#include <aliceVision/sfmData/View.hpp>
#include <aliceVision/sfmData/Rig.hpp>
#include <aliceVision/camera/camera.hpp>
#include <aliceVision/types.hpp>
#include <stdexcept>
#include <cassert>
namespace aliceVision {
namespace sfmData {
/// Define a collection of View
using Views = HashMap<IndexT, std::shared_ptr<View> >;
/// Define a collection of Pose (indexed by view.getPoseId())
using Poses = HashMap<IndexT, CameraPose>;
/// Define a collection of IntrinsicParameter (indexed by view.getIntrinsicId())
using Intrinsics = HashMap<IndexT, std::shared_ptr<camera::IntrinsicBase> >;
/// Define a collection of landmarks are indexed by their TrackId
using Landmarks = HashMap<IndexT, Landmark>;
/// Define a collection of Rig
using Rigs = std::map<IndexT, Rig>;
/// Define uncertainty per pose
using PosesUncertainty = HashMap<IndexT, Vec6>;
/// Define uncertainty per landmark
using LandmarksUncertainty = HashMap<IndexT, Vec3>;
///Define a collection of constraints
using Constraints2D = std::vector<Constraint2D>;
///Define a collection of rotation priors
using RotationPriors = std::vector<RotationPrior>;
/**
* @brief SfMData container
* Store structure and camera properties
*/
class SfMData
{
public:
/// Considered views
Views views;
/// Considered camera intrinsics (indexed by view.getIntrinsicId())
Intrinsics intrinsics;
/// Structure (3D points with their 2D observations)
Landmarks structure;
/// Controls points (stored as Landmarks (id_feat has no meaning here))
Landmarks control_points;
/// Uncertainty per pose
PosesUncertainty _posesUncertainty;
/// Uncertainty per landmark
LandmarksUncertainty _landmarksUncertainty;
/// 2D Constraints
Constraints2D constraints2d;
/// Rotation priors
RotationPriors rotationpriors;
SfMData();
~SfMData();
// Operators
bool operator==(const SfMData& other) const;
// Accessors
/**
* @brief Get views
* @return views
*/
const Views& getViews() const {return views;}
Views& getViews() {return views;}
/**
* @brief Get poses
* @return poses
*/
const Poses& getPoses() const {return _poses;}
Poses& getPoses() {return _poses;}
/**
* @brief Get rigs
* @return rigs
*/
const Rigs& getRigs() const {return _rigs;}
Rigs& getRigs() {return _rigs;}
/**
* @brief Get intrinsics
* @return intrinsics
*/
const Intrinsics& getIntrinsics() const {return intrinsics;}
Intrinsics& getIntrinsics() {return intrinsics;}
/**
* @brief Get landmarks
* @return landmarks
*/
const Landmarks& getLandmarks() const {return structure;}
Landmarks& getLandmarks() {return structure;}
/**
* @brief Get Constraints2D
* @return Constraints2D
*/
const Constraints2D& getConstraints2D() const {return constraints2d;}
Constraints2D& getConstraints2D() {return constraints2d;}
/**
* @brief Get RotationPriors
* @return RotationPriors
*/
const RotationPriors& getRotationPriors() const {return rotationpriors;}
RotationPriors& getRotationPriors() {return rotationpriors;}
/**
* @brief Get control points
* @return control points
*/
const Landmarks& getControlPoints() const {return control_points;}
Landmarks& getControlPoints() {return control_points;}
/**
* @brief Get relative features folder paths
* @return features folders paths
*/
const std::vector<std::string>& getRelativeFeaturesFolders() const
{
return _featuresFolders;
}
/**
* @brief Get relative matches folder paths
* @return matches folder paths
*/
const std::vector<std::string>& getRelativeMatchesFolders() const
{
return _matchesFolders;
}
/**
* @brief Get absolute features folder paths
* @return features folders paths
*/
std::vector<std::string> getFeaturesFolders() const;
/**
* @brief Get absolute matches folder paths
* @return matches folder paths
*/
std::vector<std::string> getMatchesFolders() const;
/**
* @brief List the view indexes that have valid camera intrinsic and pose.
* @return view indexes list
*/
std::set<IndexT> getValidViews() const;
/**
* @brief List the intrinsic indexes that have valid camera intrinsic and pose.
* @return intrinsic indexes list
*/
std::set<IndexT> getReconstructedIntrinsics() const;
/**
* @brief Return a pointer to an intrinsic if available or nullptr otherwise.
* @param[in] intrinsicId
*/
const camera::IntrinsicBase* getIntrinsicPtr(IndexT intrinsicId) const
{
if(intrinsics.count(intrinsicId))
return intrinsics.at(intrinsicId).get();
return nullptr;
}
/**
* @brief Return a pointer to an intrinsic if available or nullptr otherwise.
* @param[in] intrinsicId
*/
camera::IntrinsicBase* getIntrinsicPtr(IndexT intrinsicId)
{
if(intrinsics.count(intrinsicId))
return intrinsics.at(intrinsicId).get();
return nullptr;
}
/**
* @brief Return a shared pointer to an intrinsic if available or nullptr otherwise.
* @param[in] intrinsicId
*/
std::shared_ptr<camera::IntrinsicBase> getIntrinsicsharedPtr(IndexT intrinsicId)
{
if(intrinsics.count(intrinsicId))
return intrinsics.at(intrinsicId);
return nullptr;
}
/**
* @brief Return a shared pointer to an intrinsic if available or nullptr otherwise.
* @param[in] intrinsicId
*/
const std::shared_ptr<camera::IntrinsicBase> getIntrinsicsharedPtr(IndexT intrinsicId) const
{
if(intrinsics.count(intrinsicId))
return intrinsics.at(intrinsicId);
return nullptr;
}
/**
* @brief Get a set of views keys
* @return set of views keys
*/
std::set<IndexT> getViewsKeys() const
{
std::set<IndexT> viewKeys;
for(auto v: views)
viewKeys.insert(v.first);
return viewKeys;
}
/**
* @brief Check if the given view have defined intrinsic and pose
* @param[in] view The given view
* @return true if intrinsic and pose defined
*/
bool isPoseAndIntrinsicDefined(const View* view) const
{
if (view == nullptr)
return false;
return (
view->getIntrinsicId() != UndefinedIndexT &&
view->getPoseId() != UndefinedIndexT &&
// (!view->isPartOfRig() || getRigSubPose(*view).status != ERigSubPoseStatus::UNINITIALIZED) &&
intrinsics.find(view->getIntrinsicId()) != intrinsics.end() &&
_poses.find(view->getPoseId()) != _poses.end());
}
/**
* @brief Check if the given view have defined intrinsic and pose
* @param[in] viewID The given viewID
* @return true if intrinsic and pose defined
*/
bool isPoseAndIntrinsicDefined(IndexT viewId) const
{
return isPoseAndIntrinsicDefined(views.at(viewId).get());
}
/**
* @brief Check if the given view has an existing pose
* @param[in] view The given view
* @return true if the pose exists
*/
bool existsPose(const View& view) const
{
return (_poses.find(view.getPoseId()) != _poses.end());
}
/**
* @brief Gives the view of the input view id.
* @param[in] viewId The given view id
* @return the corresponding view reference
*/
View& getView(IndexT viewId)
{
return *(views.at(viewId));
}
/**
* @brief Gives the view of the input view id.
* @param[in] viewId The given view id
* @return the corresponding view reference
*/
const View& getView(IndexT viewId) const
{
return *(views.at(viewId));
}
/**
* @brief Gives the pose of the input view. If this view is part of a rig, it returns rigPose + rigSubPose.
* @param[in] view The given view
*
* @warning: This function returns a CameraPose (a temporary object and not a reference),
* because in the RIG context, this pose is the composition of the rig pose and the sub-pose.
*/
const CameraPose getPose(const View& view) const
{
// check the view has valid pose / rig etc
if(!view.isPartOfRig() || view.isPoseIndependant())
{
return _poses.at(view.getPoseId());
}
// get the pose of the rig
CameraPose pose = getRigPose(view);
// multiply rig pose by camera subpose
pose.setTransform(getRigSubPose(view).pose * pose.getTransform());
return pose;
}
/**
* @brief Gives the pose with the given pose id.
* @param[in] poseId The given pose id
*/
const CameraPose& getAbsolutePose(IndexT poseId) const
{
return _poses.at(poseId);
}
/**
* @brief Get the rig of the given view
* @param[in] view The given view
* @return rig of the given view
*/
const Rig& getRig(const View& view) const
{
assert(view.isPartOfRig());
return _rigs.at(view.getRigId());
}
std::set<feature::EImageDescriberType> getLandmarkDescTypes() const
{
std::set<feature::EImageDescriberType> output;
for (auto s : getLandmarks())
{
output.insert(s.second.descType);
}
return output;
}
std::map<feature::EImageDescriberType, int> getLandmarkDescTypesUsages() const
{
std::map<feature::EImageDescriberType, int> output;
for (auto s : getLandmarks())
{
if (output.find(s.second.descType) == output.end())
{
output[s.second.descType] = 1;
}
else
{
++output[s.second.descType];
}
}
return output;
}
/**
* @brief Get the median Camera Exposure Setting
* @return
*/
float getMedianCameraExposureSetting() const
{
std::vector<float> cameraExposureList;
cameraExposureList.reserve(views.size());
for(const auto& view : views)
{
float ce = view.second->getCameraExposureSetting();
if(ce != -1.0f)
{
auto find = std::find(std::begin(cameraExposureList), std::end(cameraExposureList), ce);
if(find == std::end(cameraExposureList))
cameraExposureList.emplace_back(ce);
}
}
std::nth_element(cameraExposureList.begin(), cameraExposureList.begin() + cameraExposureList.size()/2, cameraExposureList.end());
float ceMedian = cameraExposureList[cameraExposureList.size()/2];
return ceMedian;
}
/**
* @brief Add the given \p folder to features folders.
* @note If SfmData's absolutePath has been set,
* an absolute path will be converted to a relative one.
* @param[in] folder path to a folder containing features
*/
inline void addFeaturesFolder(const std::string& folder)
{
addFeaturesFolders({folder});
}
/**
* @brief Add the given \p folders to features folders.
* @note If SfmData's absolutePath has been set,
* absolute paths will be converted to relative ones.
* @param[in] folders paths to folders containing features
*/
void addFeaturesFolders(const std::vector<std::string>& folders);
/**
* @brief Add the given \p folder to matches folders.
* @note If SfmData's absolutePath has been set,
* an absolute path will be converted to a relative one.
* @param[in] folder path to a folder containing matches
*/
inline void addMatchesFolder(const std::string& folder)
{
addMatchesFolders({folder});
}
/**
* @brief Add the given \p folders to matches folders.
* @note If SfmData's absolutePath has been set,
* absolute paths will be converted to relative ones.
* @param[in] folders paths to folders containing matches
*/
void addMatchesFolders(const std::vector<std::string>& folders);
/**
* @brief Replace the current features folders by the given ones.
* @note If SfmData's absolutePath has been set,
* absolute paths will be converted to relative ones.
* @param[in] folders paths to folders containing features
*/
inline void setFeaturesFolders(const std::vector<std::string>& folders)
{
_featuresFolders.clear();
addFeaturesFolders(folders);
}
/**
* @brief Replace the current matches folders by the given ones.
* @note If SfmData's absolutePath has been set,
* absolute paths will be converted to relative ones.
* @param[in] folders paths to folders containing matches
*/
inline void setMatchesFolders(const std::vector<std::string>& folders)
{
_matchesFolders.clear();
addMatchesFolders(folders);
}
/**
* @brief Set the SfMData file absolute path.
* @note Internal relative features/matches folders will be remapped
* to be relative to the new absolute \p path.
* @param[in] path The absolute path to the SfMData file folder
*/
void setAbsolutePath(const std::string& path);
/**
* @brief Set the given pose for the given view
* if the view is part of a rig, this method update rig pose/sub-pose
* @param[in] view The given view
* @param[in] pose The given pose
*/
void setPose(const View& view, const CameraPose& pose);
/**
* @brief Set the given pose for the given poseId
* @param[in] poseId The given poseId
* @param[in] pose The given pose
*/
void setAbsolutePose(IndexT poseId, const CameraPose& pose)
{
_poses[poseId] = pose;
}
/**
* @brief Erase yhe pose for the given poseId
* @param[in] poseId The given poseId
* @param[in] noThrow If false, throw exception if no pose found
*/
void erasePose(IndexT poseId, bool noThrow = false)
{
auto it =_poses.find(poseId);
if(it != _poses.end())
_poses.erase(it);
else if(!noThrow)
throw std::out_of_range(std::string("Can't erase unfind pose ") + std::to_string(poseId));
}
/**
* @brief Reset rigs sub-poses parameters
*/
void resetRigs()
{
for(auto rigIt : _rigs)
rigIt.second.reset();
}
/**
* @brief Insert data from the given sfmData if possible.
* note: This operation doesn't override existing data.
* @param[in] sfmData A given SfMData
*/
void combine(const SfMData& sfmData);
void clear();
private:
/// Absolute path to the SfMData file (should not be saved)
std::string _absolutePath;
/// Features folders path
std::vector<std::string> _featuresFolders;
/// Matches folders path
std::vector<std::string> _matchesFolders;
/// Considered poses (indexed by view.getPoseId())
Poses _poses;
/// Considered rigs
Rigs _rigs;
/**
* @brief Get Rig pose of a given camera view
* @param[in] view The given view
* @return Rig pose of the given camera view
*/
const CameraPose& getRigPose(const View& view) const
{
return _poses.at(view.getPoseId());
}
/**
* @brief Get Rig subPose of a given camera view
* @param[in] view The given view
* @return Rig subPose of the given camera view
*/
const RigSubPose& getRigSubPose(const View& view) const
{
assert(view.isPartOfRig());
const Rig& rig = _rigs.at(view.getRigId());
return rig.getSubPose(view.getSubPoseId());
}
/**
* @brief Get Rig pose of a given camera view
* @param[in] view The given view
* @return Rig pose of the given camera view
*/
CameraPose& getRigPose(const View& view)
{
return _poses.at(view.getPoseId());
}
/**
* @brief Get Rig subPose of a given camera view
* @param[in] view The given view
* @return Rig subPose of the given camera view
*/
RigSubPose& getRigSubPose(const View& view)
{
assert(view.isPartOfRig());
Rig& rig = _rigs.at(view.getRigId());
return rig.getSubPose(view.getSubPoseId());
}
};
} // namespace sfmData
} // namespace aliceVision