ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
inverted_file_entry.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 <bitset>
11 #include <fstream>
12 
13 #include "retrieval/geometry.h"
14 
15 namespace colmap {
16 namespace retrieval {
17 
18 // An inverted file entry. The template defines the dimensionality of the binary
19 // string used to approximate the descriptor in the Hamming space.
20 // This class is based on an original implementation by Torsten Sattler.
21 template <int N>
23  void Read(std::istream* ifs);
24  void Write(std::ostream* ofs) const;
25 
26  // The identifier of the image this entry is associated with.
27  int image_id = -1;
28 
29  // The index of the feature within the image's keypoints list.
30  int feature_idx = -1;
31 
32  // The geometry of the feature, used for spatial verification.
34 
35  // The binary signature in the Hamming embedding.
36  std::bitset<N> descriptor;
37 };
38 
40 // Implementation
42 
43 template <int N>
44 void InvertedFileEntry<N>::Read(std::istream* ifs) {
45  static_assert(N <= 64, "Dimensionality too large");
46  static_assert(sizeof(unsigned long long) >= 8,
47  "Expected unsigned long to be at least 8 byte");
48  static_assert(sizeof(FeatureGeometry) == 16, "Geometry type size mismatch");
49 
50  int32_t image_id_data = 0;
51  ifs->read(reinterpret_cast<char*>(&image_id_data), sizeof(int32_t));
52  image_id = static_cast<int>(image_id_data);
53 
54  int32_t feature_idx_data = 0;
55  ifs->read(reinterpret_cast<char*>(&feature_idx_data), sizeof(int32_t));
56  feature_idx = static_cast<int>(feature_idx_data);
57 
58  ifs->read(reinterpret_cast<char*>(&geometry), sizeof(FeatureGeometry));
59 
60  uint64_t descriptor_data = 0;
61  ifs->read(reinterpret_cast<char*>(&descriptor_data), sizeof(uint64_t));
62  descriptor = std::bitset<N>(descriptor_data);
63 }
64 
65 template <int N>
66 void InvertedFileEntry<N>::Write(std::ostream* ofs) const {
67  static_assert(N <= 64, "Dimensionality too large");
68  static_assert(sizeof(unsigned long long) >= 8,
69  "Expected unsigned long to be at least 8 byte");
70  static_assert(sizeof(FeatureGeometry) == 16, "Geometry type size mismatch");
71 
72  const int32_t image_id_data = image_id;
73  ofs->write(reinterpret_cast<const char*>(&image_id_data), sizeof(int32_t));
74 
75  const int32_t feature_idx_data = feature_idx;
76  ofs->write(reinterpret_cast<const char*>(&feature_idx_data),
77  sizeof(int32_t));
78 
79  ofs->write(reinterpret_cast<const char*>(&geometry),
80  sizeof(FeatureGeometry));
81 
82  const uint64_t descriptor_data =
83  static_cast<uint64_t>(descriptor.to_ullong());
84  ofs->write(reinterpret_cast<const char*>(&descriptor_data),
85  sizeof(uint64_t));
86 }
87 
88 } // namespace retrieval
89 } // namespace colmap
void Write(std::ostream *ofs) const