Volume Cartographer 2.27.0
Tensor3D.hpp
Go to the documentation of this file.
1#pragma once
2
9#include <cassert>
10#include <cstddef>
11#include <memory>
12#include <vector>
13
14#include <opencv2/core.hpp>
15
16namespace volcart
17{
26template <typename DType>
28{
29public:
32 Tensor3D<DType>() = default;
33
42 std::size_t x, std::size_t y, std::size_t z, bool zero = true)
43 : dx_(x), dy_(y), dz_(z)
44 {
45 tensor_.reserve(dz_);
46
47 // XXX check if these dimensions are too large?
48 if (zero) {
49 // Static vars are automatically initialized to zero, so it's a
50 // convenient way to zero-initialize the tensor
51 static DType d;
52 for (std::size_t i = 0; i < dz_; ++i) {
53 tensor_.emplace_back(dy_, dx_, d);
54 }
55 } else {
56 for (std::size_t i = 0; i < dz_; ++i) {
57 tensor_.emplace_back(dy_, dx_);
58 }
59 }
60 }
66 const DType& operator()(std::size_t x, std::size_t y, std::size_t z) const
67 {
68 assert(x < dx_ && x >= 0 && "index out of range");
69 assert(y < dy_ && y >= 0 && "index out of range");
70 assert(z < dz_ && z >= 0 && "index out of range");
71 return tensor_[z](y, x);
72 }
73
75 DType& operator()(std::size_t x, std::size_t y, std::size_t z)
76 {
77 assert(x < dx_ && x >= 0 && "index out of range");
78 assert(y < dy_ && y >= 0 && "index out of range");
79 assert(z < dz_ && z >= 0 && "index out of range");
80 return tensor_[z](y, x);
81 }
86 std::size_t dx() { return dx_; }
87
89 std::size_t dx() const { return dx_; }
90
92 std::size_t dy() { return dy_; }
93
95 std::size_t dy() const { return dy_; }
96
98 std::size_t dz() { return dz_; }
99
101 std::size_t dz() const { return dz_; }
106 const cv::Mat_<DType>& xySlice(std::size_t z) const { return tensor_[z]; }
107
109 cv::Mat_<DType>& xySlice(std::size_t z) { return tensor_[z]; }
110
112 cv::Mat_<DType> xzSlice(std::size_t y) const
113 {
114 cv::Mat_<DType> zSlice(dz_, dx_);
115 for (std::size_t z = 0; z < dz_; ++z) {
116 tensor_[z].row(y).copyTo(zSlice.row(z));
117 }
118 return zSlice;
119 }
124 std::unique_ptr<DType[]> buffer() const
125 {
126 auto buf = std::make_unique<DType[]>(dx_ * dy_ * dz_);
127 for (std::size_t z = 0; z < dz_; ++z) {
128 for (std::size_t y = 0; y < dy_; ++y) {
129 for (std::size_t x = 0; x < dx_; ++x) {
130 buf[z * dx_ * dy_ + y * dx_ + x] = tensor_[z](y, x);
131 }
132 }
133 }
134 return buf;
135 }
138private:
140 std::vector<cv::Mat_<DType>> tensor_;
142 std::size_t dx_;
144 std::size_t dy_;
146 std::size_t dz_;
147};
148} // namespace volcart
149
154template <typename DType>
155std::ostream& operator<<(
156 std::ostream& s, const volcart::Tensor3D<DType>& tensor)
157{
158 for (std::size_t z = 0; z < tensor.dz_; ++z) {
159 s << tensor.xySlice(z) << std::endl;
160 }
161 return s;
162}
A 3rd-order tensor object.
Definition: Tensor3D.hpp:28
std::size_t dx()
Get the size of the X-axis.
Definition: Tensor3D.hpp:86
std::size_t dx_
Definition: Tensor3D.hpp:142
std::size_t dz_
Definition: Tensor3D.hpp:146
DType & operator()(std::size_t x, std::size_t y, std::size_t z)
Get the tensor value at x, y, z.
Definition: Tensor3D.hpp:75
cv::Mat_< DType > xzSlice(std::size_t y) const
Get an XZ cross section of the Tensor at y.
Definition: Tensor3D.hpp:112
std::size_t dz() const
Get the size of the Z-axis.
Definition: Tensor3D.hpp:101
std::ostream & operator<<(std::ostream &s, const volcart::Tensor3D< DType > &tensor)
Write a Tensor3D to an output stream.
Definition: Tensor3D.hpp:155
std::vector< cv::Mat_< DType > > tensor_
Definition: Tensor3D.hpp:140
std::size_t dy_
Definition: Tensor3D.hpp:144
std::size_t dz()
Get the size of the Z-axis.
Definition: Tensor3D.hpp:98
cv::Mat_< DType > & xySlice(std::size_t z)
Get an XY cross section of the Tensor at z.
Definition: Tensor3D.hpp:109
const DType & operator()(std::size_t x, std::size_t y, std::size_t z) const
Get the tensor value at x, y, z.
Definition: Tensor3D.hpp:66
std::size_t dy() const
Get the size of the Y-axis.
Definition: Tensor3D.hpp:95
std::unique_ptr< DType[]> buffer() const
Get a copy of the Tensor as a raw array.
Definition: Tensor3D.hpp:124
const cv::Mat_< DType > & xySlice(std::size_t z) const
Get an XY cross section of the Tensor at z.
Definition: Tensor3D.hpp:106
std::size_t dy()
Get the size of the Y-axis.
Definition: Tensor3D.hpp:92
std::size_t dx() const
Get the size of the X-axis.
Definition: Tensor3D.hpp:89
Volume Cartographer library