Berkeley SfM
pnp_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: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  * Erik Nelson ( eanelson@eecs.berkeley.edu )
36  */
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 //
40 // These classes define the PnPRansacProblem API, and derive from the base
41 // RansacProblem and RansacModel class/struct.
42 //
43 ///////////////////////////////////////////////////////////////////////////////
44 
45 #ifndef BSFM_RANSAC_2D_3D_RANSAC_PROBLEM_H
46 #define BSFM_RANSAC_2D_3D_RANSAC_PROBLEM_H
47 
48 #include <Eigen/Dense>
49 #include <vector>
50 
51 #include "ransac_problem.h"
52 #include "../camera/camera.h"
53 #include "../camera/camera_intrinsics.h"
54 #include "../geometry/point_3d.h"
55 #include "../slam/landmark.h"
56 #include "../slam/observation.h"
57 #include "../matching/feature.h"
58 #include "../util/disallow_copy_and_assign.h"
59 
60 namespace bsfm {
61 
62 using Eigen::Matrix3d;
63 using Eigen::Vector3d;
64 
65 // ------------ PnPRansacModel derived ------------ //
66 
67 struct PnPRansacModel : public RansacModel<Observation::Ptr> {
69  virtual ~PnPRansacModel();
70 
71  // Define an additional constructor specifically for this model.
72  PnPRansacModel(const Camera& camera,
73  const std::vector<Observation::Ptr>& matches);
74 
75  // Return model error.
76  virtual double Error() const;
77 
78  // Evaluate model on a single data element and update error.
79  virtual bool IsGoodFit(const Observation::Ptr& observation,
80  double error_tolerance);
81 
82  // Compute reprojection error of this landmark.
83  double EvaluateReprojectionError(const Observation::Ptr& observation) const;
84 
85  // Model-specific member variables.
87  std::vector<Observation::Ptr> matches_;
88  double error_;
89 }; //\struct PnPRansacModel
90 
91 
92 // ------------ PnPRansacProblem derived ------------ //
93 
95  : public RansacProblem<Observation::Ptr, PnPRansacModel> {
96  public:
98  virtual ~PnPRansacProblem();
99 
100  // Set intrinsics.
101  void SetIntrinsics(CameraIntrinsics& intrinsics);
102 
103  // Subsample the data.
104  virtual std::vector<Observation::Ptr> SampleData(unsigned int num_samples);
105 
106  // Return the data that was not sampled.
107  virtual std::vector<Observation::Ptr> RemainingData(
108  unsigned int num_sampled_previously) const;
109 
110  // Fit a model to the provided data using PoseEstimatorPnP.
111  virtual PnPRansacModel FitModel(
112  const std::vector<Observation::Ptr>& input_data) const;
113 
114  private:
117 }; //\class PnPRansacProblem
118 
119 } //\namespace bsfm
120 
121 #endif
std::shared_ptr< Observation > Ptr
Definition: observation.h:65
virtual std::vector< Observation::Ptr > SampleData(unsigned int num_samples)
virtual bool IsGoodFit(const Observation::Ptr &observation, double error_tolerance)
virtual PnPRansacModel FitModel(const std::vector< Observation::Ptr > &input_data) const
double EvaluateReprojectionError(const Observation::Ptr &observation) const
void SetIntrinsics(CameraIntrinsics &intrinsics)
virtual double Error() const
CameraIntrinsics intrinsics_
std::vector< Observation::Ptr > matches_
Definition: camera.cpp:50
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
virtual std::vector< Observation::Ptr > RemainingData(unsigned int num_sampled_previously) const