Berkeley SfM
view.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 View class models a camera at a specific frame index. Views must be
41 // created with the Create() factory method (construction is disabled). On
42 // creation, a view will be given a unique index which cannot be changed. The
43 // view and index will be registered so that views can be accessed from the
44 // static function GetView().
45 //
46 ///////////////////////////////////////////////////////////////////////////////
47 
48 #ifndef BSFM_SFM_VIEW_H
49 #define BSFM_SFM_VIEW_H
50 
51 #include <memory>
52 #include <unordered_map>
53 #include <unordered_set>
54 #include <utility>
55 
56 #include "../camera/camera.h"
57 #include "../slam/landmark.h"
58 #include "../slam/observation.h"
59 #include "../util/disallow_copy_and_assign.h"
60 #include "../util/types.h"
61 
62 namespace bsfm {
63 
64 class View {
65  public:
66  typedef std::shared_ptr<View> Ptr;
67  typedef std::shared_ptr<const View> ConstPtr;
68 
69  // Factory method. Registers the view and newly created index in the view
70  // registry so that they can be accessed from the static GetView() method.
71  // This guarantees that all views will have unique indices.
72  static View::Ptr Create(const ::bsfm::Camera& camera);
73  ~View() {}
74 
75  // Gets the view corresponding to the input index. If the view has not
76  // been created yet, this method returns a null pointer.
77  static View::Ptr GetView(ViewIndex view_index);
78 
79  // Returns the total number of existing landmarks.
80  static ViewIndex NumExistingViews();
81 
82  // Returns whether the view index corresponds to a view that has been created.
83  static bool IsValidView(ViewIndex view_index);
84 
85  // Resets all views and clears the view registry. If somebody else is holding
86  // onto a shared pointer to a view, that view will still be valid and may now
87  // have an index that conflicts with views that are subsequently added to the
88  // view registry. Therefore this function can cause some chaos if not used
89  // properly. This should rarely need to be called, except when completely
90  // resetting the program or reconstruction.
91  static void ResetViews();
92 
93  // Get and set the camera.
94  void SetCamera(const ::bsfm::Camera& camera);
96  const ::bsfm::Camera& Camera() const;
97 
98  // Get this view's index.
99  ViewIndex Index() const;
100 
101 
102  // Get observations.
103  const std::vector<Observation::Ptr>& Observations() const;
104 
105  // Report whether one of the observations in this view has been matched with a
106  // specific landmark.
107  bool HasObservedLandmark(LandmarkIndex landmark_index) const;
108 
109  // Update the landmark registry by looping over all observations and seeing
110  // which landmarks they have observed.
112 
113  // Return the set of all landmarks that this view can see. The returned set
114  // may not be accurate if UpdateObservedLandmarks() was not called recently.
115  const std::unordered_set<LandmarkIndex>& ObservedLandmarks() const;
116 
117  // For sorting a list of views by their indices.
118  static bool SortByIndex(const View::Ptr& lhs, const View::Ptr& rhs);
119 
120  private:
122 
123  // Private constructor to enfore creation via factory method.
124  View(const ::bsfm::Camera& camera);
125 
126  // Static method for determining the next index across all Views
127  // constructed so far. This is called in the View constructor.
128  static ViewIndex NextViewIndex();
129 
130  // Make Observation a friend class so that it can privately add itself to this
131  // view. Observations will be added to the view whenever they are created.
132  friend class Observation;
133  void AddObservation(const Observation::Ptr& observation);
134 
135  // Includes intrinsics and extrinsics.
137 
138  // An index which uniquely defines this view.
140 
141  // A registry of all views constructed so far. These can be queried with the
142  // static method GetView().
143  static std::unordered_map<ViewIndex, View::Ptr> view_registry_;
144 
145  // A list of all 2D features and landmarks that they correspond to, as seen by
146  // this view.
147  std::vector<Observation::Ptr> observations_;
148 
149  // A registry of all landmarks associated with this view. These are determined
150  // by iterating over observations and getting each observations' corresponding
151  // landmark, if it has been matched.
152  std::unordered_set<LandmarkIndex> landmarks_;
153 
154  // The maximum index assigned to any view created so far.
155  static ViewIndex current_view_index_;
156 }; //\class View
157 
158 } //\namespace bsfm
159 
160 #endif
unsigned int ViewIndex
Definition: types.h:58
STL namespace.
unsigned int LandmarkIndex
Definition: types.h:62
~View()
Definition: view.h:73
bool HasObservedLandmark(LandmarkIndex landmark_index) const
Definition: view.cpp:113
::bsfm::Camera & MutableCamera()
Definition: view.cpp:92
static View::Ptr Create(const ::bsfm::Camera &camera)
Definition: view.cpp:51
const ::bsfm::Camera & Camera() const
Definition: view.cpp:96
std::shared_ptr< const View > ConstPtr
Definition: view.h:67
void SetCamera(const ::bsfm::Camera &camera)
Definition: view.cpp:88
static std::unordered_map< ViewIndex, View::Ptr > view_registry_
Definition: view.h:143
::bsfm::Camera camera_
Definition: view.h:136
static ViewIndex NextViewIndex()
Definition: view.cpp:141
static View::Ptr GetView(ViewIndex view_index)
Definition: view.cpp:60
Definition: camera.cpp:50
ViewIndex view_index_
Definition: view.h:139
std::shared_ptr< View > Ptr
Definition: view.h:66
static bool SortByIndex(const View::Ptr &lhs, const View::Ptr &rhs)
Definition: view.cpp:131
void UpdateObservedLandmarks()
Definition: view.cpp:117
static bool IsValidView(ViewIndex view_index)
Definition: view.cpp:77
std::unordered_set< LandmarkIndex > landmarks_
Definition: view.h:152
ViewIndex Index() const
Definition: view.cpp:100
const std::unordered_set< LandmarkIndex > & ObservedLandmarks() const
Definition: view.cpp:127
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
std::vector< Observation::Ptr > observations_
Definition: view.h:147
static ViewIndex current_view_index_
Definition: view.h:155
void AddObservation(const Observation::Ptr &observation)
Definition: view.cpp:104
static void ResetViews()
Definition: view.cpp:83
static ViewIndex NumExistingViews()
Definition: view.cpp:72
const std::vector< Observation::Ptr > & Observations() const
Definition: view.cpp:109