cgv
Loading...
Searching...
No Matches
buffer2d.h
1#pragma once
2
3#include <vector>
4
5namespace cgv {
6namespace data {
7
8template<typename T>
9class buffer2d {
10public:
11 using value_type = T;
12 using vector_type = std::vector<T>;
13
14 buffer2d() {}
15
16 buffer2d(size_t width, size_t height) {
17 set_size_internal(width, height);
18 data_ = vector_type(size_);
19 }
20
21 buffer2d(size_t width, size_t height, const T& init) {
22 set_size_internal(width, height);
23 data_ = vector_type(size_, init);
24 }
25
26 size_t width() const {
27 return width_;
28 }
29
30 size_t height() const {
31 return height_;
32 }
33
34 size_t size() const {
35 return size_;
36 }
37
38 bool empty() const {
39 return size_ == 0;
40 }
41
42 typename std::vector<T>::iterator begin() {
43 return data_.begin();
44 }
45
46 typename vector_type::iterator end() {
47 return data_.end();
48 }
49
50 typename vector_type::const_iterator begin() const {
51 return data_.cbegin();
52 }
53
54 typename vector_type::const_iterator end() const {
55 return data_.cend();
56 }
57
58 T* data() {
59 return data_.data();
60 }
61
62 const T* data() const {
63 return data_.data();
64 }
65
66 void clear() {
67 size_ = 0;
68 data_.clear();
69 }
70
71 void resize(size_t width, size_t height) {
72 set_size_internal(width, height);
73 data_.resize(size_);
74 }
75
76 void resize(size_t width, size_t height, const T& init) {
77 set_size_internal(width, height);
78 data_.resize(size_, init);
79 }
80
81 T& operator[](size_t index) {
82 return data_[index];
83 }
84
85 T operator[](size_t index) const {
86 return data_[index];
87 }
88
89 T& operator()(size_t x, size_t y) {
90 return data_[to_linear_index(x, y)];
91 }
92
93 T operator()(size_t x, size_t y) const {
94 return data_[to_linear_index(x, y)];
95 }
96
97private:
98 void set_size_internal(size_t width, size_t height) {
99 width_ = width;
100 height_ = height;
101 size_ = width_ * height_;
102 }
103
104 size_t to_linear_index(size_t x, size_t y) const {
105 return x + width_ * y;
106 }
107
108 size_t width_ = 0;
109 size_t height_ = 0;
110 size_t size_ = 0;
111 vector_type data_;
112};
113
114} // namespace data
115} // namespace cgv
this header is dependency free
Definition print.h:11