Berkeley SfM
view.cpp
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 #include "view.h"
39 #include "../slam/landmark.h"
40 #include "../slam/observation.h"
41 
42 namespace bsfm {
43 
44 // Declaration of static member variable.
45 std::unordered_map<ViewIndex, View::Ptr> View::view_registry_;
47 
48 // Factory method. Registers the view and index in the view registry so
49 // that they can be accessed from the static GetView() method. This guarantees
50 // that all views will have uniqueindices.
51 View::Ptr View::Create(const ::bsfm::Camera& camera) {
52  // Create a new View, implicitly assigning a unique index.
53  View::Ptr view(new View(camera));
54  view_registry_.insert(std::make_pair(view->Index(), view));
55  return view;
56 }
57 
58 // Gets the view corresponding to the input view index. If the view has not been
59 // created yet, this method returns a null pointer.
61  auto registry_element = view_registry_.find(view_index);
62  if (registry_element == view_registry_.end()) {
63  LOG(WARNING)
64  << "View does not exist in registry. Returning a null pointer.";
65  return View::Ptr();
66  }
67 
68  return registry_element->second;
69 }
70 
71 // Returns the total number of existing views.
73  return view_registry_.size();
74 }
75 
76 // Returns whether the view index corresponds to a view that has been created.
77 bool View::IsValidView(ViewIndex view_index) {
78  return view_index <= current_view_index_;
79 }
80 
81 // Resets all views and clears the view registry. This should rarely be called,
82 // except when completely resetting the program or reconstruction.
85  view_registry_.clear();
86 }
87 
88 void View::SetCamera(const ::bsfm::Camera& camera) {
89  camera_ = camera;
90 }
91 
93  return camera_;
94 }
95 
96 const ::bsfm::Camera& View::Camera() const {
97  return camera_;
98 }
99 
101  return view_index_;
102 }
103 
104 void View::AddObservation(const Observation::Ptr& observation) {
105  CHECK_NOTNULL(observation.get());
106  observations_.push_back(observation);
107 }
108 
109 const std::vector<Observation::Ptr>& View::Observations() const {
110  return observations_;
111 }
112 
113 bool View::HasObservedLandmark(LandmarkIndex landmark_index) const {
114  return landmarks_.count(landmark_index);
115 }
116 
118  for (const auto& observation : observations_) {
119  CHECK_NOTNULL(observation.get());
120  if (!observation->IsIncorporated()) continue;
121 
122  // Calls to std::set::insert() will overwrite old key value pairs.
123  landmarks_.insert(observation->GetLandmark()->Index());
124  }
125 }
126 
127 const std::unordered_set<LandmarkIndex>& View::ObservedLandmarks() const {
128  return landmarks_;
129 }
130 
131 bool View::SortByIndex(const View::Ptr& lhs, const View::Ptr& rhs) {
132  return lhs->Index()< rhs->Index();
133 }
134 
135 // Hidden constructor. This will be called from the factory method.
136 View::View(const ::bsfm::Camera& camera)
137  : view_index_(NextViewIndex()), camera_(camera) {}
138 
139 // Static method for determining the next index across all Views
140 // constructed so far. This is called in the View constructor.
142  return current_view_index_++;
143 }
144 
145 } //\namespace bsfm
std::shared_ptr< Observation > Ptr
Definition: observation.h:65
unsigned int ViewIndex
Definition: types.h:58
unsigned int LandmarkIndex
Definition: types.h:62
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
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
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
View(const ::bsfm::Camera &camera)
Definition: view.cpp:136
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