1 /******************************************************************************
2  * Author:   Laurent Kneip                                                    *
3  * Contact:  kneip.laurent@gmail.com                                          *
4  * License:  Copyright (c) 2013 Laurent Kneip, ANU. All rights reserved.      *
5  *                                                                            *
6  * Redistribution and use in source and binary forms, with or without         *
7  * modification, are permitted provided that the following conditions         *
8  * are met:                                                                   *
9  * * Redistributions of source code must retain the above copyright           *
10  *   notice, this list of conditions and the following disclaimer.            *
11  * * Redistributions in binary form must reproduce the above copyright        *
12  *   notice, this list of conditions and the following disclaimer in the      *
13  *   documentation and/or other materials provided with the distribution.     *
14  * * Neither the name of ANU nor the names of its contributors may be         *
15  *   used to endorse or promote products derived from this software without   *
16  *   specific prior written permission.                                       *
17  *                                                                            *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"*
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  *
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
21  * ARE DISCLAIMED. IN NO EVENT SHALL ANU OR THE CONTRIBUTORS BE LIABLE        *
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT         *
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  *
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF     *
28  * SUCH DAMAGE.                                                               *
29  ******************************************************************************/
30 
31 /**
32  * \file NoncentralRelativePoseSacProblem.hpp
33  * \brief Functions for fitting a relative-pose model to a set of bearing-vector
34  *        correspondences from a multi-camera system (non-central). Used with
35  *        a random-sample paradigm for rejecting outlier-correspondences.
36  */
37 
38 #ifndef OPENGV_SAC_PROBLEMS_RELATIVE_POSE_NONCENTRALRELATIVEPOSESACPROBLEM_HPP_
39 #define OPENGV_SAC_PROBLEMS_RELATIVE_POSE_NONCENTRALRELATIVEPOSESACPROBLEM_HPP_
40 
41 #include <opengv/sac/SampleConsensusProblem.hpp>
42 #include <opengv/types.hpp>
43 #include <opengv/relative_pose/RelativeAdapterBase.hpp>
44 
45 /**
46  * \brief The namespace of this library.
47  */
48 namespace opengv
49 {
50 /**
51  * \brief The namespace for the sample consensus problems.
52  */
53 namespace sac_problems
54 {
55 /**
56  * \brief The namespace for the relative pose methods.
57  */
58 namespace relative_pose
59 {
60 
61 /**
62  * Functions for fitting a relative-pose model to a set of bearing-vector
63  * correspondences from a multi-camera system (non-central). Can switch back to
64  * central as well in case only one camera is present. Otherwise using
65  * seventeenpt [12]. Used with a random-sample paradigm for rejecting
66  * outlier-correspondences.
67  */
68 class NoncentralRelativePoseSacProblem :
69     public sac::SampleConsensusProblem<transformation_t>
70 {
71 public:
72   /** The model we are trying to fit (transformation) */
73   typedef transformation_t model_t;
74   /** The type of adapter that is expected by the methods */
75   typedef opengv::relative_pose::RelativeAdapterBase adapter_t;
76 
77   /** The possible algorithms for solving this problem */
78   typedef enum Algorithm
79   {
80     SIXPT = 0,      // [16]
81     GE = 1,         // []
82     SEVENTEENPT = 2 // [12]
83   } algorithm_t;
84 
85   /**
86    * \brief Constructor.
87    * \param[in] adapter Visitor holding bearing vector correspondences etc.
88    * \param[in] algorithm The algorithm to use.
89    * \param[in] asCentral Solve problem with only one camera?
90    * \param[in] randomSeed Whether to seed the random number generator with
91    *            the current time.
92    */
NoncentralRelativePoseSacProblem(adapter_t & adapter,algorithm_t algorithm,bool asCentral=false,bool randomSeed=true)93   NoncentralRelativePoseSacProblem(
94       adapter_t & adapter, algorithm_t algorithm, bool asCentral = false,
95       bool randomSeed = true) :
96       sac::SampleConsensusProblem<model_t> (randomSeed),
97       _adapter(adapter),
98       _algorithm(algorithm),
99       _asCentral(asCentral)
100   {
101     setUniformIndices(adapter.getNumberCorrespondences());
102   };
103 
104   /**
105    * \brief Constructor.
106    * \param[in] adapter Visitor holding bearing vector correspondences etc.
107    * \param[in] algorithm The algorithm to use
108    * \param[in] indices A vector of indices to be used from all available
109    *                    correspondences.
110    * \param[in] asCentral Solve problem with only one camera?
111    * \param[in] randomSeed Whether to seed the random number generator with
112    *            the current time.
113    */
NoncentralRelativePoseSacProblem(adapter_t & adapter,algorithm_t algorithm,const std::vector<int> & indices,bool asCentral=false,bool randomSeed=true)114   NoncentralRelativePoseSacProblem(
115       adapter_t & adapter,
116       algorithm_t algorithm,
117       const std::vector<int> & indices,
118       bool asCentral = false,
119       bool randomSeed = true) :
120       sac::SampleConsensusProblem<model_t> (randomSeed),
121       _adapter(adapter),
122       _algorithm(algorithm),
123       _asCentral(asCentral)
124   {
125     setIndices(indices);
126   };
127 
128   /**
129    * Destructor.
130    */
~NoncentralRelativePoseSacProblem()131   virtual ~NoncentralRelativePoseSacProblem() {};
132 
133   /**
134    * \brief See parent-class.
135    */
136   virtual bool computeModelCoefficients(
137       const std::vector<int> & indices,
138       model_t & outModel) const;
139 
140   /**
141    * \brief See parent-class.
142    */
143   virtual void getSelectedDistancesToModel(
144       const model_t & model,
145       const std::vector<int> & indices,
146       std::vector<double> & scores) const;
147 
148   /**
149    * \brief See parent-class.
150    */
151   virtual void optimizeModelCoefficients(
152       const std::vector<int> & inliers,
153       const model_t & model,
154       model_t & optimized_model);
155 
156   /**
157    * \brief See parent-class.
158    */
159   virtual int getSampleSize() const;
160 
161 protected:
162   /** The adapter holding all input data. */
163   adapter_t & _adapter;
164   /** The algorithm we are using. */
165   algorithm_t _algorithm;
166   /** Use the central algorithm? (only one camera?). */
167   bool _asCentral;
168 };
169 
170 }
171 }
172 }
173 
174 #endif  /* OPENGV_SAC_PROBLEMS_RELATIVE_POSE_NONCENTRALRELATIVEPOSESACPROBLEM_HPP_ */
175