Berkeley SfM
ransac_problem.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: Erik Nelson ( eanelson@eecs.berkeley.edu )
35  * David Fridovich-Keil ( dfk@eecs.berkeley.edu )
36  */
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 //
40 // This class defines an abstract base class for RANSAC problems.
41 // Structs/classes deriving from the RansacProblem and RansacModel interfaces
42 // will provide the specific details and model type that the the specific RANSAC
43 // problem will use.
44 //
45 ///////////////////////////////////////////////////////////////////////////////
46 
47 
48 #ifndef BSFM_RANSAC_RANSAC_PROBLEM_H
49 #define BSFM_RANSAC_RANSAC_PROBLEM_H
50 
51 #include <vector>
52 
53 #include "../util/disallow_copy_and_assign.h"
54 
55 namespace bsfm {
56 
57 // Derive from this struct when defining a specific RANSAC problem!
58 template <typename DataType>
59 struct RansacModel {
61  virtual ~RansacModel() {}
62 
63  // ----- Define these remaining methods in a derived struct! ----- //
64  virtual double Error() const = 0;
65  virtual bool IsGoodFit(const DataType& data_point,
66  double error_tolerance) = 0;
67 }; //\struct RansacModel
68 
69 // Derive from this class when defining a specific RANSAC problem!
70 template <typename DataType, typename ModelType>
72  public:
73  RansacProblem();
74  virtual ~RansacProblem() { }
75 
76  virtual inline void SetModel(const ModelType& model);
77  virtual inline void SetData(
78  const std::vector<DataType>& data);
79  virtual inline void SetInliers(
80  const std::vector<DataType>& inliers);
81 
82  virtual inline void SetSolutionFound(bool solution_found);
83  virtual inline bool SolutionFound();
84  virtual inline const ModelType& Model() const;
85  virtual inline const std::vector<DataType>& Inliers() const;
86 
87  // ----- Define these remaining methods in a derived class! ----- //
88  virtual std::vector<DataType> SampleData(unsigned int num_samples) = 0;
89  virtual std::vector<DataType> RemainingData(
90  unsigned int num_sampled_previously) const = 0;
91  virtual ModelType FitModel(const std::vector<DataType>& input_data) const = 0;
92 
93  protected:
94  std::vector<DataType> data_;
95  std::vector<DataType> inliers_;
96  ModelType model_;
98 
99  private:
101 }; //\class RansacProblem
102 
103 // -------------------- Implementation -------------------- //
104 
105 template <typename DataType, typename ModelType>
107  : model_(ModelType()), solution_found_(false) {}
108 
109 template <typename DataType, typename ModelType>
110 void RansacProblem<DataType, ModelType>::SetModel(const ModelType& model) {
111  model_ = model;
112 }
113 
114 template <typename DataType, typename ModelType>
116  const std::vector<DataType>& data) {
117  data_ = data;
118 }
119 
120 template <typename DataType, typename ModelType>
122  const std::vector<DataType>& inliers) {
123  inliers_ = inliers;
124 }
125 
126 template <typename DataType, typename ModelType>
128  solution_found_ = solution_found;
129 }
130 
131 template <typename DataType, typename ModelType>
133  return solution_found_;
134 }
135 
136 template <typename DataType, typename ModelType>
138  return model_;
139 }
140 
141 template <typename DataType, typename ModelType>
142 const std::vector<DataType>& RansacProblem<DataType, ModelType>::Inliers()
143  const {
144  return inliers_;
145 }
146 
147 } //\namespace bsfm
148 
149 #endif
virtual void SetSolutionFound(bool solution_found)
virtual std::vector< DataType > RemainingData(unsigned int num_sampled_previously) const =0
virtual const ModelType & Model() const
virtual double Error() const =0
virtual void SetInliers(const std::vector< DataType > &inliers)
virtual bool IsGoodFit(const DataType &data_point, double error_tolerance)=0
virtual void SetModel(const ModelType &model)
std::vector< DataType > data_
virtual const std::vector< DataType > & Inliers() const
virtual std::vector< DataType > SampleData(unsigned int num_samples)=0
virtual ModelType FitModel(const std::vector< DataType > &input_data) const =0
Definition: camera.cpp:50
virtual ~RansacModel()
virtual void SetData(const std::vector< DataType > &data)
virtual bool SolutionFound()
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
std::vector< DataType > inliers_
virtual ~RansacProblem()