Volume Cartographer 2.27.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
OrderedPointSet.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <cassert>
6#include <cstddef>
7#include <iostream>
8#include <memory>
9#include <stdexcept>
10
11#include "Exceptions.hpp"
12#include "PointSet.hpp"
13
14namespace volcart
15{
40template <typename T>
41class OrderedPointSet : public PointSet<T>
42{
43public:
44 using BaseClass = PointSet<T>;
45 using BaseClass::BaseClass;
46 using BaseClass::data_;
47
49 using Pointer = std::shared_ptr<OrderedPointSet<T>>;
50
53 explicit OrderedPointSet() = default;
54
56 explicit OrderedPointSet(std::size_t width) : BaseClass(), width_(width)
57 {
59 }
60
62 explicit OrderedPointSet(std::size_t width, T initVal)
64 {
65 data_.assign(width_ * CAPACITY_MULTIPLIER, initVal);
66 }
71 const T& operator()(std::size_t y, std::size_t x) const
72 {
73 assert(x < width_ && "x out of range");
74 assert(y * width_ + x < data_.size() && "(x, y) out of range");
75 return data_[y * width_ + x];
76 }
77
79 T& operator()(std::size_t y, std::size_t x)
80 {
81 assert(x < width_ && "x out of range");
82 assert(y * width_ + x < data_.size() && "(x, y) out of range");
83 return data_[y * width_ + x];
84 }
89 std::size_t width() const { return width_; }
90
92 // data_.size() should be a perfect multiple of width_, so this should
93 // return a whole integer
94 std::size_t height() const
95 {
96 return (width_ == 0 ? 0 : this->size() / width_);
97 }
98
108 void setWidth(std::size_t width)
109 {
110 if (width_ != 0) {
111 auto msg = "Cannot change width if already set";
112 throw std::logic_error(msg);
113 }
114 width_ = width;
115 }
116
121 void reset()
122 {
123 width_ = 0;
124 this->clear();
125 }
130 void pushRow(const std::vector<T>& points)
131 {
132 assert(points.size() == width_ && "row incorrect size");
133 std::copy(
134 std::begin(points), std::end(points), std::back_inserter(data_));
135 }
136
138 void pushRow(std::vector<T>&& points)
139 {
140 assert(points.size() == width_ && "row incorrect size");
141 std::copy(
142 std::begin(points), std::end(points), std::back_inserter(data_));
143 }
144
145 // Cannot add individual points to this class because it would break
146 // width constraint calculation
147 void push_back(const T& val) = delete;
148 void push_back(T&& val) = delete;
149 void emplace_back(const T& val) = delete;
150 void emplace_back(T&& val) = delete;
151
159 {
160 // ps must be same width as this pointset
161 if (width_ != ps.width()) {
162 auto msg = "Cannot append pointset with different width";
163 throw std::logic_error(msg);
164 }
165
166 std::copy(std::begin(ps), std::end(ps), std::back_inserter(data_));
167 }
168
173 std::vector<T> getRow(std::size_t i) const
174 {
175 if (i >= this->height()) {
176 throw std::range_error("get row: i out of range");
177 }
178 std::vector<T> row(width_);
179 std::copy(
180 std::begin(data_) + width_ * i,
181 std::begin(data_) + width_ * (i + 1), std::begin(row));
182 return row;
183 }
184
193 OrderedPointSet copyRows(std::size_t i, std::size_t j) const
194 {
195 if (i >= this->height()) {
196 throw std::range_error("copy row: i out of range");
197 }
198 if (j > this->height()) {
199 throw std::range_error("copy row: j out of range");
200 }
201 if (i == j) {
202 return OrderedPointSet(width_);
203 }
204 if (j < i) {
205 throw std::logic_error("i must be less than j");
206 }
208 std::copy(
209 std::begin(data_) + width_ * i, std::begin(data_) + width_ * j,
210 std::back_inserter(ps.data_));
211 return ps;
212 }
219 std::size_t width, std::size_t height, T initVal)
220 {
222 std::vector<T> v;
223 v.assign(width, initVal);
224 for (std::size_t _ = 0; _ < height; ++_) {
225 ps.pushRow(v);
226 }
227 return ps;
228 }
231private:
233 std::size_t width_{0};
235 constexpr static std::size_t CAPACITY_MULTIPLIER = 20;
236};
237} // namespace volcart
Holds a collection of ordered points.
std::size_t height() const
Return the number of rows in the OrderedPointSet.
OrderedPointSet copyRows(std::size_t i, std::size_t j) const
Get multiple rows of points.
T & operator()(std::size_t y, std::size_t x)
Get a point from the OrderedPointSet at row y, column x.
std::vector< T > getRow(std::size_t i) const
Get a row of points.
void setWidth(std::size_t width)
Set the ordering width.
static OrderedPointSet Fill(std::size_t width, std::size_t height, T initVal)
Create an OrderedPointSet of a specific size, filled with an initial value.
void pushRow(std::vector< T > &&points)
Add a row of points to the OrderedPointSet.
void pushRow(const std::vector< T > &points)
Add a row of points to the OrderedPointSet.
void append(const OrderedPointSet< T > &ps)
Append an OrderedPointSet to the end of the current one.
static constexpr std::size_t CAPACITY_MULTIPLIER
void reset()
Like clear(), but zeroes the OrderedPointSet width so that it can be redefined.
OrderedPointSet(std::size_t width)
Constructor with width parameter.
const T & operator()(std::size_t y, std::size_t x) const
Get a point from the OrderedPointSet at row y, column x.
std::size_t width() const
Return the number of columns in the OrderedPointSet.
OrderedPointSet()=default
Default constructor.
std::shared_ptr< OrderedPointSet< T > > Pointer
OrderedPointSet(std::size_t width, T initVal)
Constructor with width parameter and initialization value.
Holds a collection of points.
Definition: PointSet.hpp:26
Container data_
Definition: PointSet.hpp:207
std::size_t size() const
Get the size of the PointSet.
Definition: PointSet.hpp:88
void clear()
Remove all elements from the PointSet.
Definition: PointSet.hpp:97
Volume Cartographer library