Image Component Library (ICL)
Array2D.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/Array2D.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/SmartArray.h>
35 #include <ICLUtils/Size.h>
36 #include <ICLUtils/Point.h>
37 #include <algorithm>
38 
39 namespace icl{
40  namespace utils{
41 
43 
60  template<class T>
61  class Array2D{
62 
65 
68 
69  public:
70 
72  typedef T* iterator;
73 
75  typedef const T* const_iterator;
76 
78  inline Array2D(){}
79 
81  inline Array2D(int w,int h):
82  m_size(w,h),m_data(new T[w*h]){}
83 
85  inline Array2D(const Size &s):
86  m_size(s),m_data(new T[s.getDim()]){}
87 
89  template<class Init>
90  inline Array2D(int w, int h, Init init):
91  m_size(w,h),m_data(new T[w*h]){
92  fill(init);
93  }
94 
96  template<class Init>
97  inline Array2D(const Size &s, Init init):
98  m_size(s),m_data(new T[s.getDim()]){
99  fill(init);
100  }
101 
103  inline Array2D(int w, int h, T *data, bool deepCopy=false):
104  m_size(w,h),m_data(deepCopy ? new T[w*h] : data, deepCopy){
105  if(deepCopy) assign(data,data+w*h);
106  }
107 
109  inline Array2D(const Size &s, T *data, bool deepCopy=false):
110  m_size(s),m_data(deepCopy ? new T[s.getDim()] : data, deepCopy){
111  if(deepCopy) assign(data,data+s.getDim());
112  }
113 
115  inline Array2D(int w, int h,const T *data):
116  m_size(w,h),m_data(new T[w*h]){
117  assign(data,data+w*h);
118  }
119 
121  inline Array2D(const Size &s,const T *data):
122  m_size(s),m_data(new T[s.getDim()]){
123  assign(data,data+s.getDim());
124  }
125 
127  template<class Iterator>
128  inline Array2D(int w, int h,Iterator begin, Iterator end):
129  m_size(w,h),m_data(new T[w*h]){
130  assign(begin,end);
131  }
132 
134  template<class Iterator>
135  inline Array2D(const Size &s,Iterator begin, Iterator end):
136  m_size(s),m_data(new T[s.getDim()]){
137  assign(begin,end);
138  }
139 
140 
142  template<class Value>
143  inline void fill(Value val){
144  std::fill(begin(),end(),val);
145  }
146 
148  template<class Iterator>
149  inline void assign(Iterator begin, Iterator end){
150  std::copy(begin,end,this->begin());
151  }
152 
154  inline int getWidth() const { return m_size.width; }
155 
157  inline int getHeight() const { return m_size.height; }
158 
160  inline int getDim() const { return m_size.getDim(); }
161 
163  inline const Size &getSize() const { return m_size; }
164 
165 
167  inline T &operator[](int idx) { return m_data.get()[idx]; }
168 
170  inline const T &operator[](int idx) const{ return m_data.get()[idx]; }
171 
173  inline T &operator()(int x,int y) { return m_data.get()[x+m_size.width*y]; }
174 
176  inline const T &operator()(int x, int y) const{ return m_data.get()[x+m_size.width*y]; }
177 
178 
180  inline iterator begin() { return m_data.get(); }
181 
183  inline const_iterator begin() const { return m_data.get(); }
184 
186  inline iterator end() { return m_data.get()+getDim(); }
187 
189  inline const_iterator end() const{ return m_data.get()+getDim(); }
190 
192  inline Array2D<T> deepCopy() const{
193  return Array2D<T>(getSize(),begin(),end());
194  }
195 
197 
201  inline void detach(){
202  if(m_data.use_count() > 1){
203  SmartArray<T> det(new T[getDim()]);
204  std::copy(begin(),end(),det.get());
205  m_data = det;
206  }
207  }
208 
210  inline T* data() { return begin(); }
211 
213  inline const T* data() const { return begin(); }
214 
216 
218  inline const T &minElem(Point *pos=0) const {
219  int idx = (int)(std::min_element(begin(),end()) - begin());
220  if(pos) *pos = Point(idx%getWidth(),idx/getWidth());
221  return data()[idx];
222  }
223 
225 
227  inline const T &maxElem(Point *pos=0) const {
228  int idx = (int)(std::max_element(begin(),end()) - begin());
229  if(pos) *pos = Point(idx%getWidth(),idx/getWidth());
230  return data()[idx];
231  }
232 
234 
236  void setSize(const Size &size){
237  if(getSize() == size) return;
238  m_size = size;
239  m_data = new T[size.getDim()];
240  }
241 
243 
245  template<class Init>
246  void setSize(const Size &size, const Init &init){
247  setSize(size);
248  fill(init);
249  }
250  };
251 
252 
253 
254  } // namespace utils
255 }
256 
const T & operator()(int x, int y) const
returns element at given x,y position (const)
Definition: Array2D.h:176
const_iterator end() const
upper left matrix element iterator
Definition: Array2D.h:189
iterator end()
upper left matrix element iterator
Definition: Array2D.h:186
Array2D(const Size &s)
Creates an uninitialized matrix of given size.
Definition: Array2D.h:85
undocument this line if you encounter any issues!
Definition: Any.h:37
const T & maxElem(Point *pos=0) const
returns the maximum element of the matrix (operator < must be defined on T)
Definition: Array2D.h:227
const Size & getSize() const
returns the matrix size
Definition: Array2D.h:163
void detach()
ensures that the contained data is not shared by other instances
Definition: Array2D.h:201
const_iterator begin() const
upper left matrix element iterator (const)
Definition: Array2D.h:183
T & operator()(int x, int y)
returns element at given x,y position
Definition: Array2D.h:173
void assign(Iterator begin, Iterator end)
Assigns the matrix from given range.
Definition: Array2D.h:149
const T & operator[](int idx) const
returns element at given linear index (const)
Definition: Array2D.h:170
Array2D(int w, int h, T *data, bool deepCopy=false)
Creates a matrix of size w x h, using given (optionally shared) data.
Definition: Array2D.h:103
T * iterator
iterator type (just a T*)
Definition: Array2D.h:72
const T * data() const
returns the data pointer (const version)
Definition: Array2D.h:213
Array2D(int w, int h, const T *data)
Creates a matrix of size w x h, using given const data (always deep copy)
Definition: Array2D.h:115
Array2D(const Size &s, const T *data)
Creates a matrix of given Size using given const data (always deep copy)
Definition: Array2D.h:121
Array2D(int w, int h)
Creates an uninitialized matrix of given size.
Definition: Array2D.h:81
Array2D(const Size &s, Iterator begin, Iterator end)
Creates a matrix of size w x h, initialized with content from given range.
Definition: Array2D.h:135
ICLQt_API void fill(float r, float g=-1, float b=-1, float alpha=255)
sets the current fill color to given r,g,b,alpha value
int getDim() const
returns the matrix dimension (width*height)
Definition: Array2D.h:160
Size m_size
current dimension
Definition: Array2D.h:64
T * data()
returns the data pointer
Definition: Array2D.h:210
Size class of the ICL.
Definition: Size.h:61
const T & minElem(Point *pos=0) const
returns the minumum element of the matrix (operator < must be defined on T)
Definition: Array2D.h:218
Specialization of the SmartPtrBase class for Arrays.
Definition: SmartArray.h:46
T * get()
returns the pointer to the data
Definition: SmartPtrBase.h:345
Array2D(const Size &s, T *data, bool deepCopy=false)
Creates a matrix of given Size, using given (optionally shared) data.
Definition: Array2D.h:109
Array2D(int w, int h, Init init)
Creates an initialized matrix with given initializer.
Definition: Array2D.h:90
Array2D< T > deepCopy() const
returns a deep copy of this matrix
Definition: Array2D.h:192
void setSize(const Size &size, const Init &init)
sets size and fills with new entries
Definition: Array2D.h:246
Array2D(int w, int h, Iterator begin, Iterator end)
Creates a matrix of size w x h, initialized with content from given range.
Definition: Array2D.h:128
SmartArray< T > m_data
current data
Definition: Array2D.h:67
Simple 2D-Array class that provides shallow copy per default.
Definition: Array2D.h:61
void fill(Value val)
fills the matrix with given value
Definition: Array2D.h:143
Point class of the ICL used e.g. for the Images ROI offset.
Definition: Point.h:58
T & operator[](int idx)
returns element at given linear index
Definition: Array2D.h:167
Array2D(const Size &s, Init init)
Creates an initialized matrix with given initializer.
Definition: Array2D.h:97
void setSize(const Size &size)
sets a new size
Definition: Array2D.h:236
int getDim() const
reutrns width*height
Definition: Size.h:211
void copy(const T *src, const T *srcEnd, T *dst)
moves data from source to destination array (no casting possible)
Definition: CoreFunctions.h:216
const T * const_iterator
const iterator type (just a const T*)
Definition: Array2D.h:75
iterator begin()
upper left matrix element iterator
Definition: Array2D.h:180
Array2D()
Creates null instance.
Definition: Array2D.h:78
int getWidth() const
returns the matrix width
Definition: Array2D.h:154
int getHeight() const
returns the matrix height
Definition: Array2D.h:157