Volume Cartographer 2.27.0
PointSet.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <algorithm>
6#include <cassert>
7#include <cstddef>
8#include <memory>
9#include <stdexcept>
10#include <string>
11#include <vector>
12
13#include <opencv2/core.hpp>
14
15namespace volcart
16{
24template <typename T>
26{
27public:
29 using Point = T;
30
33
36 using Container = std::vector<Point>;
37
39 using Iterator = typename Container::iterator;
40
42 using ConstIterator = typename Container::const_iterator;
43
45 constexpr static int FORMAT_VERSION = 1;
48 constexpr static auto HEADER_TERMINATOR = "<>";
49
51 constexpr static auto HEADER_TERMINATOR_REGEX = "^<>$";
52
54 using Pointer = std::shared_ptr<PointSet<T>>;
55
58 explicit PointSet() = default;
59
61 explicit PointSet(std::size_t initSize) { data_.reserve(initSize); }
62
64 explicit PointSet(std::size_t initSize, T initVal)
65 {
66 data_.assign(initSize, initVal);
67 }
72 const T& operator[](std::size_t idx) const
73 {
74 assert(idx < data_.size() && "idx out of range");
75 return data_[idx];
76 }
77
79 T& operator[](std::size_t idx)
80 {
81 assert(idx < data_.size() && "idx out of range");
82 return data_[idx];
83 }
88 std::size_t size() const { return data_.size(); }
89
91 bool empty() const { return data_.empty(); }
92
95
97 void clear() { data_.clear(); }
102 void push_back(const T& val) { data_.push_back(val); }
103
105 void push_back(T&& val) { data_.push_back(val); }
106
108 template <class... Args>
109 void emplace_back(Args&&... args)
110 {
111 data_.emplace_back(std::forward<Args>(args)...);
112 }
113
115 template <class ContainerType>
116 void append(const ContainerType& c)
117 {
118 std::copy(std::begin(c), std::end(c), std::back_inserter(data_));
119 }
125 Iterator begin() { return std::begin(data_); }
126
128 ConstIterator begin() const { return std::begin(data_); }
129
132 Iterator end() { return std::end(data_); }
133
135 ConstIterator end() const { return std::end(data_); }
136
138 T& front() { return data_.front(); }
139
141 const T& front() const { return data_.front(); }
142
144 T& back() { return data_.back(); }
145
147 const T& back() const { return data_.back(); }
155 T min() const
156 {
157 if (empty()) {
158 throw std::range_error("empty PointSet");
159 }
160 return *std::min_element(
161 std::begin(data_), std::end(data_),
162 [](auto lhs, auto rhs) { return cv::norm(lhs) < cv::norm(rhs); });
163 }
164
169 T max() const
170 {
171 if (empty()) {
172 throw std::range_error("empty PointSet");
173 }
174 return *std::max_element(
175 std::begin(data_), std::end(data_),
176 [](auto lhs, auto rhs) { return cv::norm(lhs) < cv::norm(rhs); });
177 }
178
184 std::pair<T, T> minMax() const
185 {
186 if (empty()) {
187 throw std::range_error("empty PointSet");
188 }
189 auto pair = std::minmax_element(
190 std::begin(data_), std::end(data_),
191 [](auto lhs, auto rhs) { return cv::norm(lhs) < cv::norm(rhs); });
192 return {*pair.first, *pair.second};
193 }
199 static PointSet Fill(std::size_t initSize, T initVal)
200 {
201 return PointSet(initSize, initVal);
202 }
205protected:
208};
209} // namespace volcart
Holds a collection of points.
Definition: PointSet.hpp:26
static PointSet Fill(std::size_t initSize, T initVal)
Create a PointSet of a specific size, filled with an initial value.
Definition: PointSet.hpp:199
PointSet()=default
Default constructor.
void append(const ContainerType &c)
Append a PointSet to the end of the current one.
Definition: PointSet.hpp:116
static constexpr auto HEADER_TERMINATOR
Definition: PointSet.hpp:48
typename Container::const_iterator ConstIterator
Definition: PointSet.hpp:42
const T & back() const
Return a reference to the last element in the PointSet.
Definition: PointSet.hpp:147
std::vector< Point > Container
Definition: PointSet.hpp:36
T & back()
Return a reference to the last element in the PointSet.
Definition: PointSet.hpp:144
PointSet(std::size_t initSize)
Construct and preallocate a number of Point elements.
Definition: PointSet.hpp:61
std::pair< T, T > minMax() const
Return a pair of elements containing the points with the smallest and largest absolute norm (L2)
Definition: PointSet.hpp:184
T min() const
Return the element with the smallest absolute norm (L2)
Definition: PointSet.hpp:155
static constexpr auto HEADER_TERMINATOR_REGEX
Definition: PointSet.hpp:51
ConstIterator begin() const
Return an iterator that points to the first element in the PointSet.
Definition: PointSet.hpp:128
ConstIterator end() const
Return an iterator that points to the past-the-end element in the PointSet.
Definition: PointSet.hpp:135
static constexpr int FORMAT_VERSION
Definition: PointSet.hpp:45
Container as_vector()
Get the PointSet storage container.
Definition: PointSet.hpp:94
const T & front() const
Return a reference to the first element in the PointSet.
Definition: PointSet.hpp:141
Iterator begin()
Return an iterator that points to the first element in the PointSet.
Definition: PointSet.hpp:125
Container data_
Definition: PointSet.hpp:207
void push_back(const T &val)
Add a Point to the PointSet.
Definition: PointSet.hpp:102
void emplace_back(Args &&... args)
Emplace a Point at the back of the PointSet.
Definition: PointSet.hpp:109
const T & operator[](std::size_t idx) const
Get a Point by index.
Definition: PointSet.hpp:72
bool empty() const
Return whether the PointSet is empty.
Definition: PointSet.hpp:91
std::size_t size() const
Get the size of the PointSet.
Definition: PointSet.hpp:88
typename Container::iterator Iterator
Definition: PointSet.hpp:39
Iterator end()
Return an iterator that points to the past-the-end element in the PointSet.
Definition: PointSet.hpp:132
T & front()
Return a reference to the first element in the PointSet.
Definition: PointSet.hpp:138
void clear()
Remove all elements from the PointSet.
Definition: PointSet.hpp:97
PointSet(std::size_t initSize, T initVal)
Construct and fill a number of elements with an initial value.
Definition: PointSet.hpp:64
T & operator[](std::size_t idx)
Get a Point by index.
Definition: PointSet.hpp:79
std::shared_ptr< PointSet< T > > Pointer
Definition: PointSet.hpp:54
T max() const
Return the element with the largest absolute norm (L2)
Definition: PointSet.hpp:169
Volume Cartographer library