ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
database_cache.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Core>
11 #include <string>
12 #include <unordered_map>
13 #include <unordered_set>
14 #include <vector>
15 
16 #include "base/camera.h"
17 #include "base/camera_models.h"
19 #include "base/database.h"
20 #include "base/image.h"
21 #include "util/alignment.h"
22 #include "util/types.h"
23 
24 namespace colmap {
25 
26 // A class that caches the contents of the database in memory, used to quickly
27 // create new reconstruction instances when multiple models are reconstructed.
29 public:
30  DatabaseCache();
31 
32  // Get number of objects.
33  inline size_t NumCameras() const;
34  inline size_t NumImages() const;
35 
36  // Get specific objects.
37  inline class Camera& Camera(const camera_t camera_id);
38  inline const class Camera& Camera(const camera_t camera_id) const;
39  inline class Image& Image(const image_t image_id);
40  inline const class Image& Image(const image_t image_id) const;
41 
42  // Get all objects.
43  inline const std::unordered_map<camera_t, class Camera>& Cameras() const;
44  inline const std::unordered_map<image_t, class Image>& Images() const;
45 
46  // Check whether specific object exists.
47  inline bool ExistsCamera(const camera_t camera_id) const;
48  inline bool ExistsImage(const image_t image_id) const;
49 
50  // Get reference to correspondence graph.
51  inline const class CorrespondenceGraph& CorrespondenceGraph() const;
52 
53  // Manually add data to cache.
54  void AddCamera(const class Camera& camera);
55  void AddImage(const class Image& image);
56 
57  // Load cameras, images, features, and matches from database.
58  //
59  // @param database Source database from which to load data.
60  // @param min_num_matches Only load image pairs with a minimum number
61  // of matches.
62  // @param ignore_watermarks Whether to ignore watermark image pairs.
63  // @param image_names Whether to use only load the data for a
64  // subset
65  // of the images. All images are used if empty.
66  void Load(const Database& database,
67  const size_t min_num_matches,
68  const bool ignore_watermarks,
69  const std::unordered_set<std::string>& image_names);
70 
71  // Find specific image by name. Note that this uses linear search.
72  const class Image* FindImageWithName(const std::string& name) const;
73 
74 private:
75  class CorrespondenceGraph correspondence_graph_;
76 
77  std::unordered_map<camera_t, class Camera> cameras_;
78  std::unordered_map<image_t, class Image> images_;
79 };
80 
82 // Implementation
84 
85 size_t DatabaseCache::NumCameras() const { return cameras_.size(); }
86 size_t DatabaseCache::NumImages() const { return images_.size(); }
87 
88 class Camera& DatabaseCache::Camera(const camera_t camera_id) {
89  return cameras_.at(camera_id);
90 }
91 
92 const class Camera& DatabaseCache::Camera(const camera_t camera_id) const {
93  return cameras_.at(camera_id);
94 }
95 
96 class Image& DatabaseCache::Image(const image_t image_id) {
97  return images_.at(image_id);
98 }
99 
100 const class Image& DatabaseCache::Image(const image_t image_id) const {
101  return images_.at(image_id);
102 }
103 
104 const std::unordered_map<camera_t, class Camera>& DatabaseCache::Cameras()
105  const {
106  return cameras_;
107 }
108 
109 const std::unordered_map<image_t, class Image>& DatabaseCache::Images() const {
110  return images_;
111 }
112 
113 bool DatabaseCache::ExistsCamera(const camera_t camera_id) const {
114  return cameras_.find(camera_id) != cameras_.end();
115 }
116 
117 bool DatabaseCache::ExistsImage(const image_t image_id) const {
118  return images_.find(image_id) != images_.end();
119 }
120 
122  const {
123  return correspondence_graph_;
124 }
125 
126 } // namespace colmap
std::shared_ptr< core::Tensor > image
std::string name
size_t NumImages() const
const std::unordered_map< camera_t, class Camera > & Cameras() const
bool ExistsCamera(const camera_t camera_id) const
void AddImage(const class Image &image)
class Camera & Camera(const camera_t camera_id)
class Image & Image(const image_t image_id)
void Load(const Database &database, const size_t min_num_matches, const bool ignore_watermarks, const std::unordered_set< std::string > &image_names)
const class Image * FindImageWithName(const std::string &name) const
size_t NumCameras() const
const std::unordered_map< image_t, class Image > & Images() const
const class CorrespondenceGraph & CorrespondenceGraph() const
void AddCamera(const class Camera &camera)
bool ExistsImage(const image_t image_id) const
uint32_t image_t
Definition: types.h:61
uint32_t camera_t
Definition: types.h:58