1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 //       notice, this list of conditions and the following disclaimer.
9 //
10 //     * Redistributions in binary form must reproduce the above copyright
11 //       notice, this list of conditions and the following disclaimer in the
12 //       documentation and/or other materials provided with the distribution.
13 //
14 //     * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 //       its contributors may be used to endorse or promote products derived
16 //       from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #ifndef COLMAP_SRC_MVS_MAT_H_
33 #define COLMAP_SRC_MVS_MAT_H_
34 
35 #include <fstream>
36 #include <string>
37 #include <vector>
38 
39 #include "util/endian.h"
40 #include "util/logging.h"
41 
42 namespace colmap {
43 namespace mvs {
44 
45 template <typename T>
46 class Mat {
47  public:
48   Mat();
49   Mat(const size_t width, const size_t height, const size_t depth);
50 
51   size_t GetWidth() const;
52   size_t GetHeight() const;
53   size_t GetDepth() const;
54 
55   size_t GetNumBytes() const;
56 
57   T Get(const size_t row, const size_t col, const size_t slice = 0) const;
58   void GetSlice(const size_t row, const size_t col, T* values) const;
59   T* GetPtr();
60   const T* GetPtr() const;
61 
62   const std::vector<T>& GetData() const;
63 
64   void Set(const size_t row, const size_t col, const T value);
65   void Set(const size_t row, const size_t col, const size_t slice,
66            const T value);
67 
68   void Fill(const T value);
69 
70   void Read(const std::string& path);
71   void Write(const std::string& path) const;
72 
73  protected:
74   size_t width_ = 0;
75   size_t height_ = 0;
76   size_t depth_ = 0;
77   std::vector<T> data_;
78 };
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 // Implementation
82 ////////////////////////////////////////////////////////////////////////////////
83 
84 template <typename T>
Mat()85 Mat<T>::Mat() : Mat(0, 0, 0) {}
86 
87 template <typename T>
Mat(const size_t width,const size_t height,const size_t depth)88 Mat<T>::Mat(const size_t width, const size_t height, const size_t depth)
89     : width_(width), height_(height), depth_(depth) {
90   data_.resize(width_ * height_ * depth_, 0);
91 }
92 
93 template <typename T>
GetWidth()94 size_t Mat<T>::GetWidth() const {
95   return width_;
96 }
97 
98 template <typename T>
GetHeight()99 size_t Mat<T>::GetHeight() const {
100   return height_;
101 }
102 
103 template <typename T>
GetDepth()104 size_t Mat<T>::GetDepth() const {
105   return depth_;
106 }
107 
108 template <typename T>
GetNumBytes()109 size_t Mat<T>::GetNumBytes() const {
110   return data_.size() * sizeof(T);
111 }
112 
113 template <typename T>
Get(const size_t row,const size_t col,const size_t slice)114 T Mat<T>::Get(const size_t row, const size_t col, const size_t slice) const {
115   return data_.at(slice * width_ * height_ + row * width_ + col);
116 }
117 
118 template <typename T>
GetSlice(const size_t row,const size_t col,T * values)119 void Mat<T>::GetSlice(const size_t row, const size_t col, T* values) const {
120   for (size_t slice = 0; slice < depth_; ++slice) {
121     values[slice] = Get(row, col, slice);
122   }
123 }
124 
125 template <typename T>
GetPtr()126 T* Mat<T>::GetPtr() {
127   return data_.data();
128 }
129 
130 template <typename T>
GetPtr()131 const T* Mat<T>::GetPtr() const {
132   return data_.data();
133 }
134 
135 template <typename T>
GetData()136 const std::vector<T>& Mat<T>::GetData() const {
137   return data_;
138 }
139 
140 template <typename T>
Set(const size_t row,const size_t col,const T value)141 void Mat<T>::Set(const size_t row, const size_t col, const T value) {
142   Set(row, col, 0, value);
143 }
144 
145 template <typename T>
Set(const size_t row,const size_t col,const size_t slice,const T value)146 void Mat<T>::Set(const size_t row, const size_t col, const size_t slice,
147                  const T value) {
148   data_.at(slice * width_ * height_ + row * width_ + col) = value;
149 }
150 
151 template <typename T>
Fill(const T value)152 void Mat<T>::Fill(const T value) {
153   std::fill(data_.begin(), data_.end(), value);
154 }
155 
156 template <typename T>
Read(const std::string & path)157 void Mat<T>::Read(const std::string& path) {
158   std::fstream text_file(path, std::ios::in | std::ios::binary);
159   CHECK(text_file.is_open()) << path;
160 
161   char unused_char;
162   text_file >> width_ >> unused_char >> height_ >> unused_char >> depth_ >>
163       unused_char;
164   std::streampos pos = text_file.tellg();
165   text_file.close();
166 
167   CHECK_GT(width_, 0);
168   CHECK_GT(height_, 0);
169   CHECK_GT(depth_, 0);
170   data_.resize(width_ * height_ * depth_);
171 
172   std::fstream binary_file(path, std::ios::in | std::ios::binary);
173   CHECK(binary_file.is_open()) << path;
174   binary_file.seekg(pos);
175   ReadBinaryLittleEndian<T>(&binary_file, &data_);
176   binary_file.close();
177 }
178 
179 template <typename T>
Write(const std::string & path)180 void Mat<T>::Write(const std::string& path) const {
181   std::fstream text_file(path, std::ios::out);
182   CHECK(text_file.is_open()) << path;
183   text_file << width_ << "&" << height_ << "&" << depth_ << "&";
184   text_file.close();
185 
186   std::fstream binary_file(path,
187                            std::ios::out | std::ios::binary | std::ios::app);
188   CHECK(binary_file.is_open()) << path;
189   WriteBinaryLittleEndian<T>(&binary_file, data_);
190   binary_file.close();
191 }
192 
193 }  // namespace mvs
194 }  // namespace colmap
195 
196 #endif  // COLMAP_SRC_MVS_MAT_H_
197