ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
track.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 <vector>
11 
12 #include "util/logging.h"
13 #include "util/types.h"
14 
15 namespace colmap {
16 
17 // Track class stores all observations of a 3D point.
18 struct TrackElement {
19  TrackElement();
21  // The image in which the track element is observed.
23  // The point in the image that the track element is observed.
25 
26  inline bool operator==(const TrackElement& other) const;
27  inline bool operator!=(const TrackElement& other) const;
28 };
29 
30 class Track {
31 public:
32  Track();
33 
34  // The number of track elements.
35  inline size_t Length() const;
36 
37  // Access all elements.
38  inline const std::vector<TrackElement>& Elements() const;
39  inline std::vector<TrackElement>& Elements();
40  inline void SetElements(const std::vector<TrackElement>& elements);
41 
42  // Access specific elements.
43  inline const TrackElement& Element(const size_t idx) const;
44  inline TrackElement& Element(const size_t idx);
45  inline void SetElement(const size_t idx, const TrackElement& element);
46 
47  // Append new elements.
48  inline void AddElement(const TrackElement& element);
49  inline void AddElement(const image_t image_id, const point2D_t point2D_idx);
50  inline void AddElements(const std::vector<TrackElement>& elements);
51 
52  // Delete existing element.
53  inline void DeleteElement(const size_t idx);
54  void DeleteElement(const image_t image_id, const point2D_t point2D_idx);
55 
56  // Requests that the track capacity be at least enough to contain the
57  // specified number of elements.
58  inline void Reserve(const size_t num_elements);
59 
60  // Shrink the capacity of track vector to fit its size to save memory.
61  inline void Compress();
62 
63  inline bool operator==(const Track& other) const;
64  inline bool operator!=(const Track& other) const;
65 
66 private:
67  std::vector<TrackElement> elements_;
68 };
69 
71 // Implementation
73 
74 bool TrackElement::operator==(const TrackElement& other) const {
75  return image_id == other.image_id && point2D_idx == other.point2D_idx;
76 }
77 
78 bool TrackElement::operator!=(const TrackElement& other) const {
79  return !(*this == other);
80 }
81 
82 size_t Track::Length() const { return elements_.size(); }
83 
84 const std::vector<TrackElement>& Track::Elements() const { return elements_; }
85 
86 std::vector<TrackElement>& Track::Elements() { return elements_; }
87 
88 void Track::SetElements(const std::vector<TrackElement>& elements) {
89  elements_ = elements;
90 }
91 
92 // Access specific elements.
93 const TrackElement& Track::Element(const size_t idx) const {
94  return elements_.at(idx);
95 }
96 
97 TrackElement& Track::Element(const size_t idx) { return elements_.at(idx); }
98 
99 void Track::SetElement(const size_t idx, const TrackElement& element) {
100  elements_.at(idx) = element;
101 }
102 
103 void Track::AddElement(const TrackElement& element) {
104  elements_.push_back(element);
105 }
106 
107 void Track::AddElement(const image_t image_id, const point2D_t point2D_idx) {
108  elements_.emplace_back(image_id, point2D_idx);
109 }
110 
111 void Track::AddElements(const std::vector<TrackElement>& elements) {
112  elements_.insert(elements_.end(), elements.begin(), elements.end());
113 }
114 
115 void Track::DeleteElement(const size_t idx) {
116  CHECK_LT(idx, elements_.size());
117  elements_.erase(elements_.begin() + idx);
118 }
119 
120 void Track::Reserve(const size_t num_elements) {
121  elements_.reserve(num_elements);
122 }
123 
124 void Track::Compress() { elements_.shrink_to_fit(); }
125 
126 bool Track::operator==(const Track& other) const {
127  return elements_ == other.elements_;
128 }
129 
130 bool Track::operator!=(const Track& other) const { return !(*this == other); }
131 
132 } // namespace colmap
void SetElements(const std::vector< TrackElement > &elements)
Definition: track.h:88
void AddElement(const TrackElement &element)
Definition: track.h:103
size_t Length() const
Definition: track.h:82
void AddElements(const std::vector< TrackElement > &elements)
Definition: track.h:111
void Compress()
Definition: track.h:124
bool operator!=(const Track &other) const
Definition: track.h:130
bool operator==(const Track &other) const
Definition: track.h:126
const std::vector< TrackElement > & Elements() const
Definition: track.h:84
void DeleteElement(const size_t idx)
Definition: track.h:115
void Reserve(const size_t num_elements)
Definition: track.h:120
const TrackElement & Element(const size_t idx) const
Definition: track.h:93
void SetElement(const size_t idx, const TrackElement &element)
Definition: track.h:99
uint32_t point2D_t
Definition: types.h:67
uint32_t image_t
Definition: types.h:61
image_t image_id
Definition: track.h:22
bool operator==(const TrackElement &other) const
Definition: track.h:74
bool operator!=(const TrackElement &other) const
Definition: track.h:78
point2D_t point2D_idx
Definition: track.h:24