ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
mat.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <fstream>
11 #include <string>
12 #include <vector>
13 
14 #include "util/endian.h"
15 #include "util/logging.h"
16 
17 namespace colmap {
18 namespace mvs {
19 
20 template <typename T>
21 class Mat {
22 public:
23  Mat();
24  Mat(const size_t width, const size_t height, const size_t depth);
25 
26  size_t GetWidth() const;
27  size_t GetHeight() const;
28  size_t GetDepth() const;
29 
30  size_t GetNumBytes() const;
31 
32  T Get(const size_t row, const size_t col, const size_t slice = 0) const;
33  void GetSlice(const size_t row, const size_t col, T* values) const;
34  T* GetPtr();
35  const T* GetPtr() const;
36 
37  const std::vector<T>& GetData() const;
38 
39  void Set(const size_t row, const size_t col, const T value);
40  void Set(const size_t row,
41  const size_t col,
42  const size_t slice,
43  const T value);
44 
45  void Fill(const T value);
46 
47  void Read(const std::string& path);
48  void Write(const std::string& path) const;
49 
50 protected:
51  size_t width_ = 0;
52  size_t height_ = 0;
53  size_t depth_ = 0;
54  std::vector<T> data_;
55 };
56 
58 // Implementation
60 
61 template <typename T>
62 Mat<T>::Mat() : Mat(0, 0, 0) {}
63 
64 template <typename T>
65 Mat<T>::Mat(const size_t width, const size_t height, const size_t depth)
66  : width_(width), height_(height), depth_(depth) {
67  data_.resize(width_ * height_ * depth_, 0);
68 }
69 
70 template <typename T>
71 size_t Mat<T>::GetWidth() const {
72  return width_;
73 }
74 
75 template <typename T>
76 size_t Mat<T>::GetHeight() const {
77  return height_;
78 }
79 
80 template <typename T>
81 size_t Mat<T>::GetDepth() const {
82  return depth_;
83 }
84 
85 template <typename T>
86 size_t Mat<T>::GetNumBytes() const {
87  return data_.size() * sizeof(T);
88 }
89 
90 template <typename T>
91 T Mat<T>::Get(const size_t row, const size_t col, const size_t slice) const {
92  return data_.at(slice * width_ * height_ + row * width_ + col);
93 }
94 
95 template <typename T>
96 void Mat<T>::GetSlice(const size_t row, const size_t col, T* values) const {
97  for (size_t slice = 0; slice < depth_; ++slice) {
98  values[slice] = Get(row, col, slice);
99  }
100 }
101 
102 template <typename T>
104  return data_.data();
105 }
106 
107 template <typename T>
108 const T* Mat<T>::GetPtr() const {
109  return data_.data();
110 }
111 
112 template <typename T>
113 const std::vector<T>& Mat<T>::GetData() const {
114  return data_;
115 }
116 
117 template <typename T>
118 void Mat<T>::Set(const size_t row, const size_t col, const T value) {
119  Set(row, col, 0, value);
120 }
121 
122 template <typename T>
123 void Mat<T>::Set(const size_t row,
124  const size_t col,
125  const size_t slice,
126  const T value) {
127  data_.at(slice * width_ * height_ + row * width_ + col) = value;
128 }
129 
130 template <typename T>
131 void Mat<T>::Fill(const T value) {
132  std::fill(data_.begin(), data_.end(), value);
133 }
134 
135 template <typename T>
136 void Mat<T>::Read(const std::string& path) {
137  std::fstream text_file(path, std::ios::in | std::ios::binary);
138  CHECK(text_file.is_open()) << path;
139 
140  char unused_char;
141  text_file >> width_ >> unused_char >> height_ >> unused_char >> depth_ >>
142  unused_char;
143  std::streampos pos = text_file.tellg();
144  text_file.close();
145 
146  CHECK_GT(width_, 0);
147  CHECK_GT(height_, 0);
148  CHECK_GT(depth_, 0);
149  data_.resize(width_ * height_ * depth_);
150 
151  std::fstream binary_file(path, std::ios::in | std::ios::binary);
152  CHECK(binary_file.is_open()) << path;
153  binary_file.seekg(pos);
154  ReadBinaryLittleEndian<T>(&binary_file, &data_);
155  binary_file.close();
156 }
157 
158 template <typename T>
159 void Mat<T>::Write(const std::string& path) const {
160  std::fstream text_file(path, std::ios::out);
161  CHECK(text_file.is_open()) << path;
162  text_file << width_ << "&" << height_ << "&" << depth_ << "&";
163  text_file.close();
164 
165  std::fstream binary_file(path,
166  std::ios::out | std::ios::binary | std::ios::app);
167  CHECK(binary_file.is_open()) << path;
168  WriteBinaryLittleEndian<T>(&binary_file, data_);
169  binary_file.close();
170 }
171 
172 } // namespace mvs
173 } // namespace colmap
int width
int height
size_t width_
Definition: mat.h:51
size_t GetWidth() const
Definition: mat.h:71
size_t GetDepth() const
Definition: mat.h:81
void Write(const std::string &path) const
Definition: mat.h:159
size_t height_
Definition: mat.h:52
std::vector< T > data_
Definition: mat.h:54
size_t GetNumBytes() const
Definition: mat.h:86
T * GetPtr()
Definition: mat.h:103
void Read(const std::string &path)
Definition: mat.h:136
void Fill(const T value)
Definition: mat.h:131
size_t depth_
Definition: mat.h:53
void Set(const size_t row, const size_t col, const T value)
Definition: mat.h:118
Mat(const size_t width, const size_t height, const size_t depth)
Definition: mat.h:65
T Get(const size_t row, const size_t col, const size_t slice=0) const
Definition: mat.h:91
const T * GetPtr() const
Definition: mat.h:108
size_t GetHeight() const
Definition: mat.h:76
void Set(const size_t row, const size_t col, const size_t slice, const T value)
Definition: mat.h:123
void GetSlice(const size_t row, const size_t col, T *values) const
Definition: mat.h:96
const std::vector< T > & GetData() const
Definition: mat.h:113
static const std::string path
Definition: PointCloud.cpp:59