SIFT
matrix.hpp
Go to the documentation of this file.
1 #ifndef MATRIX_HPP
2 #define MATRIX_HPP
3 
4 #ifdef _MSC_VER
5 #pragma warning(disable: 4018)
6 #endif
7 
8 #include <iostream>
9 #include <cassert>
10 #include <memory>
11 
12 #include "types.hpp"
13 #include "point.hpp"
14 
15 namespace sift {
16  template <typename T>
17 
22  class Matrix {
23  private:
26 
27  std::shared_ptr<T> _data;
28 
29  public:
30  Matrix() = default;
31 
36  explicit Matrix(u16_t width, u16_t height, const T& def = T()) : _height(height), _width(width) {
37  assert(width > 0 && height > 0);
38 
39  const u32_t size = _width * _height;
40 
41  _data = std::shared_ptr<T>(new T[size], std::default_delete<T[]>());
42  for (u32_t i = 0; i < size; i++) {
43  _data.get()[i] = def;
44  }
45  }
46 
47  u16_t width() const {
48  return _width;
49  }
50 
51  u16_t height() const {
52  return _height;
53  }
54 
56  assert(vec.x < _width && vec.y < _height);
57 
58  const u32_t index = vec.x * _height + vec.y;
59  assert(index < (_width * _height));
60 
61  return _data.get()[index];
62  }
63 
64  const T& operator [](const Point<u16_t, u16_t>& vec) const {
65  assert(vec.x < _width && vec.y < _height);
66 
67  const u32_t index = vec.x * _height + vec.y;
68  assert(index < (_width * _height));
69 
70  return _data.get()[index];
71  }
72 
73  T& operator()(u16_t x, u16_t y) {
74  return (*this)[Point<u16_t, u16_t>(x, y)];
75  }
76 
77  const T& operator()(u16_t x, u16_t y) const {
78  return (*this)[Point<u16_t, u16_t>(x, y)];
79  }
80 
81  T* begin() {
82  return &_data.get()[0];
83  }
84 
85  T* end() {
86  return &_data.get()[_width * _height];
87  }
88 
89  friend std::ostream& operator <<(std::ostream& out, Matrix& m) {
90  const u32_t size = m._width * m._height;
91 
92  for (u32_t i = 0, x = 1; i < size; i++, x++) {
93  out << m._data.get()[i] << ',';
94  if (x >= m._width) {
95  x = 0;
96  out << std::endl;
97  } else {
98  out << "\t";
99  }
100  }
101  return out;
102  }
103  };
104 }
105 #endif
u16_t height() const
Definition: matrix.hpp:51
unsigned short int u16_t
Definition: types.hpp:6
const T & operator()(u16_t x, u16_t y) const
Definition: matrix.hpp:77
unsigned long int u32_t
Definition: types.hpp:8
T x
Definition: point.hpp:16
u16_t width() const
Definition: matrix.hpp:47
u16_t _width
Definition: matrix.hpp:24
Matrix()=default
u16_t _height
Definition: matrix.hpp:25
T & operator()(u16_t x, u16_t y)
Definition: matrix.hpp:73
friend std::ostream & operator<<(std::ostream &out, Matrix &m)
Definition: matrix.hpp:89
T & operator[](const Point< u16_t, u16_t > &vec)
Definition: matrix.hpp:55
T * end()
Definition: matrix.hpp:85
Definition: matrix.hpp:22
T * begin()
Definition: matrix.hpp:81
U y
Definition: point.hpp:21
std::shared_ptr< T > _data
Definition: matrix.hpp:27
Matrix(u16_t width, u16_t height, const T &def=T())
Definition: matrix.hpp:36
Definition: algorithms.cpp:8