The Fib multimedia system
Fib is a system for storing multimedia data (like images or films).
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cMatrix2D.h
Go to the documentation of this file.
1 /**
2  * @file cMatrix2D
3  * file name: cMatrix2D.h
4  * @author Betti Oesterholz
5  * @date 01.07.2010
6  * @mail webmaster@BioKom.info
7  *
8  * System: C++
9  *
10  * This header specifies a class for a two dimensional matrix of values.
11  * Copyright (C) @c GPL3 2010 Betti Oesterholz
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License (GPL) as
15  * published by the Free Software Foundation, either version 3 of the
16  * License, or any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  *
26  *
27  * This header specifies a class for a two dimensional matrix of values.
28  * (Position counting begins at 0.)
29  *
30  */
31 /*
32 History:
33 01.07.2010 Oesterholz created
34 */
35 
36 #ifndef ___C_MATRIX_2D_H__
37 #define ___C_MATRIX_2D_H__
38 
39 #include "version.h"
40 
41 #include <valarray>
42 #include <ostream>
43 
44 using namespace std;
45 
46 namespace fib{
47 
48 namespace algorithms{
49 
50 namespace nD2{
51 
52 
53 template <class tValue>
54 class cMatrix2D{
55 protected:
56 
57  /**
58  * Matrix with its values.
59  * Position (x,y) is element: x + y * ulSizeX
60  */
61  valarray< tValue > matrix;
62 
63  /**
64  * The size of the matrix in the x (first) direction.
65  */
66  unsigned long ulSizeX;
67 
68  /**
69  * The size of the matrix in the y (second) direction.
70  */
71  unsigned long ulSizeY;
72 
73 public:
74 
75  /**
76  * standardconstructor
77  *
78  * @param ulInSizeX the size of the matrix in the x (first) direction
79  * @param ulInSizeY the size of the matrix in the y (second) direction
80  * @param initValue the value, with which to initialisize the matrix
81  */
82  cMatrix2D( unsigned long ulInSizeX, unsigned long ulInSizeY,
83  const tValue & initValue = 0 );
84 
85  /**
86  * copyconstructor
87  *
88  * @param inMatrix the matrix to copy
89  */
90  cMatrix2D( const cMatrix2D & inMatrix );
91 
92  /**
93  * This method retuns the a reference to the value on the given
94  * position (ulX, ulY) .
95  * Beware: It won't be checked, if the position is out of bounds.
96  *
97  * @param ulX the position in the x (first) direction
98  * @param ulY the position in the y (second) direction
99  * @return a reference to the value on the given position (ulX, ulY)
100  */
101  tValue & get( unsigned long ulX, unsigned long ulY );
102 
103  /**
104  * This method retuns the a reference to the value on the given
105  * position (ulX, ulY) .
106  * Beware: It won't be checked, if the position is out of bounds.
107  *
108  * @param ulX the position in the x (first) direction
109  * @param ulY the position in the y (second) direction
110  * @return a reference to the value on the given position (ulX, ulY)
111  */
112  const tValue & get( unsigned long ulX, unsigned long ulY ) const;
113 
114  /**
115  * This method sets the the given value on the given position (ulX, ulY) .
116  *
117  * @param ulX the position in the x (first) direction
118  * @param ulY the position in the y (second) direction
119  * @param value the value to set
120  * @return true if the value was set, else false (e.g. if the position
121  * is out of bounds)
122  */
123  bool set( unsigned long ulX, unsigned long ulY, const tValue & value );
124 
125  /**
126  * @return a pair (x, y) with the size of the matrix in x (first)
127  * and y (second) direction
128  */
129  pair< unsigned long, unsigned long > size() const;
130 
131  /**
132  * @param ulInSizeX the new size of the matrix in the x (first) direction
133  * @param ulInSizeY the new size of the matrix in the y (second) direction
134  * @param initValue the value, with which to initialisize the matrix new values
135  */
136  void resize(unsigned long ulInSizeX, unsigned long ulInSizeY,
137  const tValue & initValue = 0 );
138 
139  /**
140  * assignment operator
141  * This operator copies the given matrixdata into this matrix.
142  *
143  * @param inMatrix the matrix to copy
144  * @return a reference to this matrix, with the copied values
145  */
146  cMatrix2D<tValue> & operator=( const cMatrix2D & inMatrix );
147 
148  /**
149  * This operator adds the values of the given matrix to the values of
150  * this matrix.
151  * Position not in both matrixes will be ignored.
152  *
153  * @param inMatrix the matrix to add
154  * @return a reference to this matrix
155  */
156  cMatrix2D<tValue> & operator+=( const cMatrix2D & inMatrix );
157 
158  /**
159  * This operator subtracts the values of the given matrix from the
160  * values of this matrix.
161  * Position not in both matrixes will be ignored.
162  *
163  * @param inMatrix the matrix to subtracts
164  * @return a reference to this matrix
165  */
166  cMatrix2D<tValue> & operator-=( const cMatrix2D & inMatrix );
167 
168  /**
169  * This operator multiplys the values of the given matrix to the
170  * values of this matrix.
171  * Position not in both matrixes will be ignored.
172  *
173  * @param inMatrix the matrix to multiply with
174  * @return a reference to this matrix
175  */
176  cMatrix2D<tValue> & operator*=( const cMatrix2D & inMatrix );
177 
178  /**
179  * This operator divides the values of the given matrix with the
180  * values of this matrix.
181  * Position not in both matrixes will be ignored.
182  *
183  * @param inMatrix the matrix to divide with
184  * @return a reference to this matrix
185  */
186  cMatrix2D<tValue> & operator/=( const cMatrix2D & inMatrix );
187 
188  /**
189  * This operator applies the values of the given function to the
190  * values of this matrix.
191  *
192  * @return a reference to this matrix
193  */
194  cMatrix2D<tValue> & apply( tValue func(const tValue&) );
195 
196  /**
197  * @return the minimum value in this matrix
198  */
199  tValue min() const;
200 
201  /**
202  * @return the maximum value in this matrix
203  */
204  tValue max() const;
205 
206  /**
207  * @return the sum of all values in this matrix
208  */
209  tValue sum() const;
210 
211  /**
212  * This method evalues the derivate of this matrix in the given direction.
213  * For this, from each element the higer neibourelement value in the
214  * direction will be subtracted.
215  * The last element in the direction is eleminated. The size of the matrix
216  * is reduced with 1 in the direction.
217  *
218  * @param direction the direction in which to evalue the derivate
219  * - 1: the position in the x (first) direction
220  * - 2: the position in the y (second) direction
221  * @return a reference to this matrix
222  */
223  cMatrix2D<tValue> & derivate( unsigned int direction = 1 );
224 
225  /**
226  * This method evalues the derivate of this matrix in the x (first) direction.
227  * For this, from each element the higer neibourelement value in the
228  * x direction will be subtracted.
229  * The last element in the x direction is eleminated. The size of the matrix
230  * is reduced with 1 in the x direction.
231  *
232  * @return a reference to this matrix
233  */
234  cMatrix2D<tValue> & derivateDirection1();
235 
236  /**
237  * This method evalues the derivate of this matrix in the y (second) direction.
238  * For this, from each element the higer neibourelement value in the
239  * y direction will be subtracted.
240  * The last element in the y direction is eleminated. The size of the matrix
241  * is reduced with 1 in the y direction.
242  *
243  * @return a reference to this matrix
244  */
245  cMatrix2D<tValue> & derivateDirection2();
246 
247  /**
248  * @param condition the condition which should hold for counted values
249  * @return the number of values for which the given condition is true
250  */
251  unsigned long count( bool condition( const tValue & ) ) const;
252 
253  /**
254  * @return the number of values which are 0
255  */
256  unsigned long countNull() const;
257 
258  /**
259  * @param condition the condition which should hold for the values
260  * @return a matrix with the bool values, which are the result of the
261  * given condition on the values of this matrix
262  */
263  cMatrix2D<bool> evalueCondition( bool condition( const tValue & ) ) const;
264 
265  /**
266  * This method prints this matrix in an readebel for to the given stream.
267  *
268  * @param outStream the stream wher to print this matrix to
269  */
270  void print( ostream & outStream ) const;
271 
272 /*TODO later
273  errode1Neibour()
274  errode2Neibour()
275  errode3Neibour()
276  errode4Neibour()
277 */
278 protected:
279 
280  /**
281  * @param value the value for which to check, if it is 0
282  * @return true if the given value is 0, else false
283  */
284  static bool isNullD( const double & value ){
285 
286  const double SAVE_BOUNDERY = 0.0000000001;
287  const double SAVE_BOUNDERY_NEG = SAVE_BOUNDERY * -1.0;
288  return ( (SAVE_BOUNDERY_NEG < value) && (value < SAVE_BOUNDERY) );
289  }
290 
291  /**
292  * @param value the value for which to check, if it is 0
293  * @return true if the given value is 0, else false
294  */
295  static bool isNullLD( const long double & value ){
296 
297  const long double SAVE_BOUNDERY = 0.0000000001;
298  const long double SAVE_BOUNDERY_NEG = SAVE_BOUNDERY * -1.0;
299  return ( (SAVE_BOUNDERY_NEG < value) && (value < SAVE_BOUNDERY) );
300  }
301 
302 
303  /**
304  * @param value the value for which to check, if it is 0
305  * @return true if the given value is 0, else false
306  */
307  static bool isNull( const tValue & value ){
308 
309  return ( value == 0 );
310  }
311 
312 
313 };
314 
315 
316 };//end namespace nD2
317 };//end namespace algorithms
318 };//end namespace fib
319 
320 #define ___C_MATRIX_2D_H_INCLUDE__
321 //include template implementation
322 #include "../src/cMatrix2D.cpp"
323 #undef ___C_MATRIX_2D_H_INCLUDE__
324 
325 #endif //___C_MATRIX_2D_H__