Berkeley SfM
landmark.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 // The Landmark class defines a 3D point and associated descriptor that can be
41 // used for 2D-3D matching. Each landmark also has a list of observations that
42 // have been associated with it over time.
43 //
44 ///////////////////////////////////////////////////////////////////////////////
45 
46 #ifndef BSFM_SLAM_LANDMARK_H
47 #define BSFM_SLAM_LANDMARK_H
48 
49 #include <Eigen/Core>
50 #include <memory>
51 #include <unordered_map>
52 #include <utility>
53 #include <vector>
54 
55 #include "observation.h"
56 #include "../geometry/point_3d.h"
57 #include "../geometry/triangulation.h"
58 #include "../util/types.h"
59 
60 namespace bsfm {
61 
62 class View;
63 
64 class Landmark {
65  public:
66  typedef std::shared_ptr<Landmark> Ptr;
67  typedef std::shared_ptr<const Landmark> ConstPtr;
68 
69  // Factory method. Registers the landmark and newly created index in the
70  // landmark registry so that they can be accessed from the static
71  // GetLandmark() method. This guarantees that all landmarks will have unique
72  // indices.
73  static Landmark::Ptr Create();
74  ~Landmark() {}
75 
76  // Gets this landmark's index.
77  LandmarkIndex Index() const;
78 
79  // Gets the landmark corresponding to the input index. If the landmark has not
80  // been created yet, this method returns a null pointer.
81  static Landmark::Ptr GetLandmark(LandmarkIndex landmark_index);
82 
83  // Returns the number of existing landmarks.
85 
86  // Returns whether the landmark index corresponds to a landmark that has been
87  // created.
88  static bool IsValidLandmark(LandmarkIndex landmark_index);
89 
90  // Resets all landmarks and clears the landmark registry. If somebody else is
91  // holding onto a shared pointer to a landmark, that landmark will still be
92  // valid and may now have an index that conflicts with landmarks that are
93  // subsequently added to the landmark registry. Therefore this function can
94  // cause some chaos if not used properly. This should rarley need to be
95  // called, except when completely resetting the program or reconstruction.
96  static void ResetLandmarks();
97 
98  // Setters.
99  void SetPosition(const Point3D& position);
100  void SetDescriptor(const ::bsfm::Descriptor& descriptor);
101  void ClearObservations();
102 
103  // Accessors.
104  const Point3D& Position() const;
106  const std::vector<Observation::Ptr>& Observations() const;
107 
108  // Returns a raw pointer to the data elements of the position of the landmark.
109  // This is useful for optimization on landmark positions (e.g. during bundle
110  // adjustment.)
111  double* PositionData();
112 
113  // Add a new observation of the landmark. If 'retriangulate' is true, the
114  // landmark's position will be retriangulated from all observations of it.
115  // This will return false if the observation's descriptor does not match with
116  // our own descriptor, or if we fail to retriangulate the landmark after
117  // incorporating the new observation.
118  bool IncorporateObservation(const Observation::Ptr& observation,
119  bool retriangulate = true);
120 
121  // Get the view that first saw this landmark.
122  std::shared_ptr<View> SourceView() const;
123 
124  // Given a set of views, return whether or not this landmark has been seen by
125  // at least 2 of them.
126  bool SeenByAtLeastTwoViews(const std::vector<ViewIndex>& view_indices);
127 
128  private:
130 
131  // Private constructor to enforce creation via factory method.
132  Landmark();
133 
134  // Static method for determining the next index across all Landmarks
135  // constructed so far. This is called in the Landmark constructor.
137 
138  // The landmark's 3D position.
140 
141  // An index which uniquely defines this landmark.
143 
144  // A registry of all landmarks constructed so far. These can be queried with
145  // the static method GetLandmark();
146  static std::unordered_map<LandmarkIndex, Landmark::Ptr> landmark_registry_;
147 
148  // Observations that were triangulated to find the 3D position of this
149  // landmark.
151 
152  // The descriptor associated with this 3D point. This is assigned based on the
153  // first observation added to the landmark.
155 
156  // The maximum index assigned to any landmark created so far.
157  static LandmarkIndex current_landmark_index_;
158 }; //\class Landmark
159 
160 } //\namespace bsfm
161 
162 #endif
std::shared_ptr< Observation > Ptr
Definition: observation.h:65
const std::vector< Observation::Ptr > & Observations() const
Definition: landmark.cpp:119
static LandmarkIndex NextLandmarkIndex()
Definition: landmark.cpp:227
::bsfm::Descriptor descriptor_
Definition: landmark.h:154
static Landmark::Ptr GetLandmark(LandmarkIndex landmark_index)
Definition: landmark.cpp:59
bool SeenByAtLeastTwoViews(const std::vector< ViewIndex > &view_indices)
Definition: landmark.cpp:198
LandmarkIndex landmark_index_
Definition: landmark.h:142
Point3D position_
Definition: landmark.h:139
STL namespace.
static LandmarkIndex NumExistingLandmarks()
Definition: landmark.cpp:71
unsigned int LandmarkIndex
Definition: types.h:62
static void ResetLandmarks()
Definition: landmark.cpp:83
void ClearObservations()
Definition: landmark.cpp:104
const Point3D & Position() const
Definition: landmark.cpp:109
static Landmark::Ptr Create()
Definition: landmark.cpp:50
std::vector< Observation::Ptr > observations_
Definition: landmark.h:150
static LandmarkIndex current_landmark_index_
Definition: landmark.h:157
LandmarkIndex Index() const
Definition: landmark.cpp:89
Definition: camera.cpp:50
static std::unordered_map< LandmarkIndex, Landmark::Ptr > landmark_registry_
Definition: landmark.h:146
std::shared_ptr< Landmark > Ptr
Definition: landmark.h:66
std::shared_ptr< View > SourceView() const
Definition: landmark.cpp:186
static bool IsValidLandmark(LandmarkIndex landmark_index)
Definition: landmark.cpp:77
const ::bsfm::Descriptor & Descriptor() const
Definition: landmark.cpp:114
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
bool IncorporateObservation(const Observation::Ptr &observation, bool retriangulate=true)
Definition: landmark.cpp:132
double * PositionData()
Definition: landmark.cpp:126
::Eigen::Matrix< double, Eigen::Dynamic, 1 > Descriptor
Definition: types.h:83
void SetDescriptor(const ::bsfm::Descriptor &descriptor)
Definition: landmark.cpp:99
void SetPosition(const Point3D &position)
Definition: landmark.cpp:94
std::shared_ptr< const Landmark > ConstPtr
Definition: landmark.h:67