1 // This file is part of OpenMVG, an Open Multiple View Geometry C++ library.
2 
3 // Copyright (c) 2015 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 OPENMVG_SFM_SFM_DATA_BA_HPP
10 #define OPENMVG_SFM_SFM_DATA_BA_HPP
11 
12 #include "openMVG/cameras/Camera_Common.hpp"
13 
14 namespace openMVG {
15 namespace sfm {
16 
17 struct SfM_Data;
18 
19 /// Enum to control which parameter(s) of the Camera motion must be refined or not
20 enum class Extrinsic_Parameter_Type : int
21 {
22   // Note: Use power of two values in order to use bitwise operators.
23   NONE                = 1,     // Extrinsic parameters will be considered as FIXED
24   ADJUST_ROTATION     = 2,
25   ADJUST_TRANSLATION  = 4,
26   ADJUST_ALL = ADJUST_ROTATION | ADJUST_TRANSLATION
27 };
28 
29 inline constexpr Extrinsic_Parameter_Type
operator |(Extrinsic_Parameter_Type x,Extrinsic_Parameter_Type y)30 operator|(Extrinsic_Parameter_Type x, Extrinsic_Parameter_Type y)
31 {
32   return static_cast<Extrinsic_Parameter_Type>
33     (static_cast<std::underlying_type<Extrinsic_Parameter_Type>::type>(x) |
34      static_cast<std::underlying_type<Extrinsic_Parameter_Type>::type>(y));
35 }
36 
37 inline constexpr Extrinsic_Parameter_Type
operator &(Extrinsic_Parameter_Type x,Extrinsic_Parameter_Type y)38 operator&(Extrinsic_Parameter_Type x, Extrinsic_Parameter_Type y)
39 {
40   return static_cast<Extrinsic_Parameter_Type>
41     (static_cast<std::underlying_type<Extrinsic_Parameter_Type>::type>(x) &
42      static_cast<std::underlying_type<Extrinsic_Parameter_Type>::type>(y));
43 }
44 
45 /// Enum to control if the Structure must be refined or not
46 enum class Structure_Parameter_Type : bool
47 {
48   NONE = false, // Structure will be held as constant
49   ADJUST_ALL = true
50 };
51 
52 /// Structure to tell to BA if GCP must be use and with which weight
53 struct Control_Point_Parameter
54 {
Control_Point_ParameteropenMVG::sfm::Control_Point_Parameter55   Control_Point_Parameter
56   (
57     double weight_val = 20.0,
58     bool use_control_points = false
59   ): weight(weight_val), bUse_control_points(use_control_points)
60   {}
61   double weight;
62   bool bUse_control_points;
63 };
64 
65 /// Structure to control which parameter will be refined during the BundleAjdustment process
66 struct Optimize_Options
67 {
68   cameras::Intrinsic_Parameter_Type intrinsics_opt;
69   Extrinsic_Parameter_Type extrinsics_opt;
70   Structure_Parameter_Type structure_opt;
71   Control_Point_Parameter control_point_opt;
72   bool use_motion_priors_opt;
73 
Optimize_OptionsopenMVG::sfm::Optimize_Options74   Optimize_Options
75   (
76     const cameras::Intrinsic_Parameter_Type intrinsics = cameras::Intrinsic_Parameter_Type::ADJUST_ALL,
77     const Extrinsic_Parameter_Type extrinsics = Extrinsic_Parameter_Type::ADJUST_ALL,
78     const Structure_Parameter_Type structure = Structure_Parameter_Type::ADJUST_ALL,
79     const Control_Point_Parameter & control_point = Control_Point_Parameter(0.0, false), // Default setting does not use GCP in the BA
80     const bool use_motion_priors = false
81   )
82   :intrinsics_opt(intrinsics),
83    extrinsics_opt(extrinsics),
84    structure_opt(structure),
85    control_point_opt(control_point),
86    use_motion_priors_opt(use_motion_priors)
87   {
88   }
89 };
90 
91 class Bundle_Adjustment
92 {
93   public:
94 
95   virtual ~Bundle_Adjustment() = default;
96 
97   // Perform a Bundle Adjustment on the SfM scene (refinement only asked parameters)
98   virtual bool Adjust
99   (
100     // the SfM scene to refine
101     sfm::SfM_Data & sfm_data,
102     // tell which parameter needs to be adjusted
103     const Optimize_Options & options
104   ) = 0;
105 };
106 
107 } // namespace sfm
108 } // namespace openMVG
109 
110 #endif // OPENMVG_SFM_SFM_DATA_BA_HPP
111