forked from pavelsevecek/pvl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniformGrid.hpp
121 lines (98 loc) · 2.39 KB
/
UniformGrid.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pragma once
#include "Vector.hpp"
#include <vector>
namespace Pvl {
template <typename Object, int Dim>
class GridIterator {
public:
using Idxs = Vector<int, Dim>;
private:
Object* ptr_;
std::size_t index_;
Idxs dims_;
public:
GridIterator(Object* ptr, const std::size_t index, const Idxs& dims)
: ptr_(ptr)
, index_(index)
, dims_(dims) {}
GridIterator& operator++() {
ptr_++;
index_++;
return *this;
}
Object& operator*() const {
return *ptr_;
}
bool operator==(const GridIterator& other) const {
return ptr_ == other.ptr_;
}
bool operator!=(const GridIterator& other) const {
return ptr_ != other.ptr_;
}
Idxs idxs() const {
Idxs c;
std::size_t k = index_;
for (int i = 0; i < Dim; ++i) {
c[i] = k % dims_[i];
k /= dims_[i];
}
}
};
template <typename Object, int Dim>
class UniformGrid {
public:
// using Point = Point;
using Idxs = Vector<int, Dim>;
public:
std::vector<Object> data_;
Idxs dims_;
public:
UniformGrid()
: dims_(0) {}
UniformGrid(const Idxs& dims)
: dims_(dims) {
data_.resize(voxelCount(), Object());
}
Object& operator()(const Idxs& idxs) {
return data_[map(idxs)];
}
const Object& operator()(const Idxs& idxs) const {
return data_[map(idxs)];
}
std::size_t voxelCount() const {
std::size_t cnt = 1;
for (int i = 0; i < Dim; ++i) {
cnt *= dims_[i];
}
return cnt;
}
Idxs dimension() const {
return dims_;
}
GridIterator<Object, Dim> begin() {
return { data_.data(), 0, dims_ };
}
GridIterator<Object, Dim> end() {
return { data_.data() + data_.size(), 0, dims_ };
}
GridIterator<const Object, Dim> begin() const {
return { data_.data(), 0, dims_ };
}
GridIterator<const Object, Dim> end() const {
return { data_.data() + data_.size(), 0, dims_ };
}
Object* data() {
return data_.data();
}
private:
std::size_t map(const Idxs& idxs) const {
std::size_t linear = 0;
std::size_t stride = 1;
for (int i = 0; i < Dim; ++i) {
linear += idxs[i] * stride;
stride *= dims_[i];
}
return linear;
}
};
} // namespace Pvl