-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.h
204 lines (173 loc) · 6.51 KB
/
cube.h
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Developed by Jimmy Hu */
#ifndef VolumetricImage_H
#define VolumetricImage_H
#include <algorithm>
#include <array>
#include <chrono>
#include <complex>
#include <concepts>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <numeric>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
#include "basic_functions.h"
namespace TinyDIP
{
template <typename ElementT>
class VolumetricImage
{
public:
VolumetricImage() = default;
VolumetricImage(const std::size_t newWidth, const std::size_t newHeight, const std::size_t newDepth):
width(width),
height(height),
depth(newDepth),
data(width * height * depth) { }
VolumetricImage(const int newWidth, const int newHeight, const int newDepth, ElementT initVal):
width(newWidth),
height(newHeight),
depth(newDepth),
data(width * height * depth, initVal) {}
VolumetricImage(const std::vector<ElementT>& input, std::size_t newWidth, std::size_t newHeight, const std::size_t newDepth):
width(newWidth),
height(newHeight),
depth(newDepth)
{
if (input.size() != newWidth * newHeight * newDepth)
{
throw std::runtime_error("Data input and the given size are mismatched!");
}
data = std::move(input);
}
VolumetricImage(const std::vector<Image<ElementT>>& input)
{
width = input[0].getWidth();
height = input[0].getHeight();
depth = input.size();
data.resize(width * height * depth);
for (std::size_t z = 0; z < input.size(); ++z)
{
for (std::size_t y = 0; y < height; ++y)
{
for (std::size_t x = 0; x < width; ++x)
{
data[z * width * height + y * width + x] = input[z].at(x, y);
}
}
}
}
constexpr ElementT& at(const std::size_t x, const std::size_t y, const std::size_t z)
{
checkBoundary(x, y, z);
return data[z * width * height + y * width + x];
}
constexpr ElementT const& at(const std::size_t x, const std::size_t y, const std::size_t z) const
{
checkBoundary(x, y, z);
return data[z * width * height + y * width + x];
}
constexpr auto getWidth() const noexcept
{
return width;
}
constexpr auto getHeight() const noexcept
{
return height;
}
constexpr auto getDepth() const noexcept
{
return depth;
}
std::vector<ElementT> const& getData() const noexcept { return data; } // expose the internal data
void print(std::string separator = "\t", std::ostream& os = std::cout) const
{
for(std::size_t z = 0; z < depth; ++z)
{
for (std::size_t y = 0; y < height; ++y)
{
for (std::size_t x = 0; x < width; ++x)
{
// Ref: https://isocpp.org/wiki/faq/input-output#print-char-or-ptr-as-number
os << +at(x, y, z) << separator;
}
os << "\n";
}
os << "\n";
}
os << "\n";
return;
}
friend std::ostream& operator<<(std::ostream& os, const VolumetricImage<ElementT>& rhs)
{
const std::string separator = "\t";
rhs.print(separator, os);
return os;
}
VolumetricImage<ElementT>& operator+=(const VolumetricImage<ElementT>& rhs)
{
check_size_same(rhs, *this);
std::transform(std::ranges::cbegin(data), std::ranges::cend(data), std::ranges::cbegin(rhs.data),
std::ranges::begin(data), std::plus<>{});
return *this;
}
VolumetricImage<ElementT>& operator-=(const VolumetricImage<ElementT>& rhs)
{
check_size_same(rhs, *this);
std::transform(std::ranges::cbegin(data), std::ranges::cend(data), std::ranges::cbegin(rhs.data),
std::ranges::begin(data), std::minus<>{});
return *this;
}
VolumetricImage<ElementT>& operator*=(const VolumetricImage<ElementT>& rhs)
{
check_size_same(rhs, *this);
std::transform(std::ranges::cbegin(data), std::ranges::cend(data), std::ranges::cbegin(rhs.data),
std::ranges::begin(data), std::multiplies<>{});
return *this;
}
VolumetricImage<ElementT>& operator/=(const VolumetricImage<ElementT>& rhs)
{
check_size_same(rhs, *this);
std::transform(std::ranges::cbegin(data), std::ranges::cend(data), std::ranges::cbegin(rhs.data),
std::ranges::begin(data), std::divides<>{});
return *this;
}
friend bool operator==(VolumetricImage<ElementT> const&, VolumetricImage<ElementT> const&) = default;
friend bool operator!=(VolumetricImage<ElementT> const&, VolumetricImage<ElementT> const&) = default;
friend VolumetricImage<ElementT> operator+(VolumetricImage<ElementT> input1, const VolumetricImage<ElementT>& input2)
{
return input1 += input2;
}
friend VolumetricImage<ElementT> operator-(VolumetricImage<ElementT> input1, const VolumetricImage<ElementT>& input2)
{
return input1 -= input2;
}
friend VolumetricImage<ElementT> operator*(VolumetricImage<ElementT> input1, ElementT input2)
{
return multiplies(input1, input2);
}
friend VolumetricImage<ElementT> operator*(ElementT input1, VolumetricImage<ElementT> input2)
{
return multiplies(input2, input1);
}
private:
std::size_t width;
std::size_t height;
std::size_t depth;
std::vector<ElementT> data;
void checkBoundary(const size_t x, const size_t y, const size_t z) const
{
if (x >= width)
throw std::out_of_range("Given x out of range!");
if (y >= height)
throw std::out_of_range("Given y out of range!");
if (z >= depth)
throw std::out_of_range("Given z out of range!");
}
};
}
#endif