Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestMatrixVectorIO.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
22 
23 
24 
25 
26 /**
27  @test TestMatrixVectorIO.cpp
28  @brief Test Matrix I/O precision issues.
29  @ingroup g_tests
30  @author evers
31 */
32 
33 // one file used for I/O
34 #ifdef WIN32
35 # define TMP "tmp.txt"
36 #else //WIN32
37 # define TMP "/tmp/tmp.txt"
38 #endif //WIN32
39 
40 #include <stdlib.h> // srand
41 #include <string>
42 #include <limits>
43 
44 #include <Base/Math/Vector2.hh>
45 #include <Base/Math/Vector3.hh>
46 #include <Base/Math/Vector4.hh>
47 #include <Base/Math/RGB.hh>
48 #include <Base/Math/RGBA.hh>
49 
50 #include <Base/Math/Matrix2x2.hh>
51 #include <Base/Math/Matrix3x3.hh>
52 #include <Base/Math/Matrix3x4.hh>
53 #include <Base/Math/Matrix4x4.hh>
54 
55 
56 
57 using namespace std;
58 using namespace BIAS;
59 
60 
61 // helper
62 #define DISPVAL(_ARG) {string s(#_ARG); std::cout<<s<<" "<<endl<<_ARG<<endl;}
63 
64 
65 int g_result (0); //< global result
66 int g_samples(20); //< gloabl nr. of random samples (default)
67 
68 
69 // random value [0..1]
70 double rand1(){
71  return ((double)rand() / (double)RAND_MAX);
72 }
73 
74 
75 // typed random function inside values range
76 template <class T>
77 T myrand()
78 {
79  T val = (T) ( rand1() * numeric_limits<T>::max());
80  return val;
81 }
82 
83 
84 void checkRGBuc()
85 {
86  cout<<FUNCNAME<<" ";
87  // check disk I/O
88  for (unsigned int i=0; (int)i<(int)g_samples; i++){
89  RGBuc a(myrand<unsigned char>(), myrand<unsigned char>(), myrand<unsigned char>()), b;
90  a.Save(TMP);
91  b.Load(TMP);
92  if (!(a==b)){
93  BIASERR("failed: Save/Load to "<<TMP<<" on sample "<<i<<"/"<<g_samples<<" with "<<endl
94  <<"\ta="<<a<<endl
95  <<"\tb="<<b );
96  g_result=-1;
97  return;
98  }
99  }
100  cout<<" + OK"<<endl;
101 }
102 
103 
104 void checkRGBAuc()
105 {
106  cout<<FUNCNAME<<" ";
107  // check disk I/O
108  for (unsigned int i=0; (int)i<(int)g_samples; i++){
109  RGBAuc a(myrand<unsigned char>(), myrand<unsigned char>(), myrand<unsigned char>(), myrand<unsigned char>()), b;
110  a.Save(TMP);
111  b.Load(TMP);
112  if (!(a==b)){
113  BIASERR("failed: Save/Load to "<<TMP<<" on sample "<<i<<"/"<<g_samples<<" with "<<endl
114  <<"\ta="<<a<<endl
115  <<"\tb="<<b );
116  g_result=-1;
117  return;
118  }
119  }
120  cout<<" + OK"<<endl;
121 }
122 
123 
124 void checkRGBAf()
125 {
126  cout<<FUNCNAME<<" ";
127  // check disk I/O
128  for (unsigned int i=0; (int)i<(int)g_samples; i++){
129  RGBAf a(myrand<float>(), myrand<float>(), myrand<float>(), myrand<float>()), b;
130  a.Save(TMP);
131  b.Load(TMP);
132  if (!(a==b)){
133  BIASERR("failed: Save/Load to "<<TMP<<" on sample "<<i<<"/"<<g_samples<<" with "<<endl
134  <<"\ta="<<a<<endl
135  <<"\tb="<<b );
136  g_result=-1;
137  return;
138  }
139  }
140  cout<<" + OK"<<endl;
141 }
142 
143 
144 void checkRGBf()
145 {
146  cout<<FUNCNAME<<" ";
147  // check disk I/O
148  for (unsigned int i=0; (int)i<(int)g_samples; i++){
149  RGBf a(myrand<float>(), myrand<float>(), myrand<float>()), b;
150  a.Save(TMP);
151  b.Load(TMP);
152  if (!(a==b)){
153  BIASERR("failed: Save/Load to "<<TMP<<" on sample "<<i<<"/"<<g_samples<<" with "<<endl
154  <<"\ta="<<a<<endl
155  <<"\tb="<<b );
156  g_result=-1;
157  return;
158  }
159  }
160  cout<<" + OK"<<endl;
161 }
162 
163 
164 template <class TYPE, class T>
165 void display(const unsigned int samples=1)
166 {
167 
168 #ifdef WIN32
169  cout
170  <<FUNCNAME<<" "
171  <<"TYPE="<<typeid(TYPE).name()<<" "
172  <<"T="<<typeid(T).name()<<" "<<endl;
173 #else
174  cout<<FUNCNAME<<" "<<endl;
175 #endif
176 
177  TYPE a;
178  a.SetZero();
179  for (unsigned int jj=0; (int)jj<(int)a.GetNumElements(); jj++){
180  if (jj==0)
181  a.GetData()[jj]=numeric_limits<T>::min();
182  else if (jj==1)
183  a.GetData()[jj]=numeric_limits<T>::max();
184  else if (jj==2)
185  a.GetData()[jj]=(T)0;
186  else
187  a.GetData()[jj]=myrand<T>();
188  }
189  cout<<a<<endl;
190 }
191 
192 
193 // For fixed size matrix, vector, i.e. Vector3<> Matrix3x3<> etc.
194 template <class TYPE, class T>
195 void check(const unsigned int samples=1)
196 {
197 
198 #ifdef WIN32
199  cout
200  <<FUNCNAME<<" "
201  <<"TYPE="<<typeid(TYPE).name()<<" "
202  <<"T="<<typeid(T).name()<<" ";
203 #else
204  cout<<FUNCNAME<<" ";
205 #endif
206 
207  // check disk I/O
208  for (unsigned int i=0; i<samples; i++)
209  {
210  TYPE a, b;
211  for (unsigned int jj=0; (int)jj<(int)a.GetNumElements(); jj++)
212  a.GetData()[jj]=myrand<T>();
213 
214  b.SetZero();
215  a.Save(TMP);
216  b.Load(TMP);
217  if (!(a==b)){
218  BIASERR("failed: Save/Load to "<<TMP<<" on sample "<<i+1<<"/"<<samples<<" with "<<endl
219  <<"a="<<a<<endl
220  <<"b="<<b<<endl
221  <<"delta:"<<a-b<<endl
222  );
223  cout<<a<<endl; // DBG
224  g_result=-1;
225  return;
226  }
227  }
228  cout<<"+ OK"<<endl;
229  // example display to check formatting
230  display<TYPE, T>();
231 }
232 
233 
234 // For arbitrary sized Matrix<>
235 // copy of the above with additional 2D dimension
236 template <class TYPE, class T>
237 void checkWithDim2(const unsigned int dim, const unsigned int samples)
238 {
239 
240 #ifdef WIN32
241  cout
242  <<FUNCNAME<<" "
243  <<"TYPE="<<typeid(TYPE).name()<<" "
244  <<"T="<<typeid(T).name()<<" "
245  <<"dim="<<dim<<" ";
246 #else
247  CALLINFO;
248 #endif
249 
250  // check disk I/O
251  for (unsigned int i=0; i<samples; i++)
252  {
253  TYPE a, b;
254  a.newsize(dim, dim+1);
255  for (unsigned int jj=0; (int)jj<(int)a.GetNumElements(); jj++)
256  a.GetData()[jj]=myrand<T>();
257  b.clear();
258  a.Save(TMP);
259  b.Load(TMP);
260  if (!(a==b)){
261  BIASERR("failed: Save/Load to "<<TMP<<" on sample "<<i+1<<"/"<<samples<<" with "<<endl
262  <<"a="<<a<<endl
263  <<"b="<<b<<endl
264  <<"delta:"<<a-b<<endl
265  );
266  g_result=-1;
267  return;
268  }
269  }
270  cout<<"+ OK"<<endl;
271 }
272 
273 
274 
275 // For arbitrary sized Matrix<>
276 // copy of teh above with additional 1D dimension
277 template <class TYPE, class T>
278 void checkWithDim1(const unsigned int dim, const unsigned int samples)
279 {
280 
281 #ifdef WIN32
282  cout
283  <<FUNCNAME<<" "
284  <<"TYPE="<<typeid(TYPE).name()<<" "
285  <<"T="<<typeid(T).name()<<" "
286  <<"dim="<<dim<<" ";
287 #else
288  CALLINFO;
289 #endif
290 
291  // check disk I/O
292  for (unsigned int i=0; i<samples; i++)
293  {
294  TYPE a, b;
295  a.newsize(dim);
296  for (unsigned int jj=0; (int)jj<(int)a.GetNumElements(); jj++)
297  a.GetData()[jj]=myrand<T>();
298  b.clear();
299  a.Save(TMP);
300  b.Load(TMP);
301  if (!(a==b)){
302  BIASERR("failed: Save/Load to "<<TMP<<" on sample "<<i+1<<"/"<<samples<<" with "<<endl
303  <<"a="<<a<<endl
304  <<"b="<<b<<endl
305  <<"delta:"<<a-b<<endl
306  );
307  g_result=-1;
308  return;
309  }
310  }
311  cout<<"+ OK"<<endl;
312 }
313 
314 
315 
316 // checks all value types of one templated class
317 #define CHECK_TYPE( _TPLCLASS ) \
318  check< _TPLCLASS< unsigned char >, unsigned char >(); \
319  check< _TPLCLASS< char >, char >(); \
320  check< _TPLCLASS< int >, int >(); \
321  check< _TPLCLASS< unsigned short>, unsigned short >(); \
322  check< _TPLCLASS< int >, int >(); \
323  check< _TPLCLASS< float >, float >(); \
324  check< _TPLCLASS< double >, double >();
325 
326 
327 // entry
328 int main(int argc, char** argv)
329 {
330  //srand(1); // pseudo random numbers only - same numbers on each run for debugging
331 
332  // ctest needs this for >1024 chars output:
333  cout<<"CTEST_FULL_OUTPUT"<<endl;
334 
335  if (argc>0)
336  cout<<"started "<<argv[0]<<endl;
337 
338  if (argc>1)
339  g_samples=atoi(argv[1]);
340  cout<<"using "<<g_samples<<" random samples for each instance."<<endl;
341 
342  // check fixed size types
343  // macro based check for all storage types (float, int, ...)
344  CHECK_TYPE(Vector2);
345  CHECK_TYPE(Vector3); // contains RGB
346  CHECK_TYPE(Vector4); // contains RGBA
347  CHECK_TYPE(Matrix2x2);
348  CHECK_TYPE(Matrix3x3);
349  CHECK_TYPE(Matrix3x4);
350  CHECK_TYPE(Matrix4x4);
351 
352  // check variable size 2D types
353  checkWithDim2< BIAS::Matrix<double>, double> (7, g_samples); // e.g. 7x(7+1)=56 elements
354  checkWithDim2< BIAS::Matrix<float>, float> (7, g_samples);
355  checkWithDim2< BIAS::Matrix<int>, int> (7, g_samples);
356  checkWithDim2< BIAS::Matrix<unsigned char>, unsigned char> (7, g_samples);
357  checkWithDim2< BIAS::Matrix<char >, char> (7, g_samples);
358  checkWithDim2< BIAS::Matrix<unsigned short>,unsigned short>(7, g_samples);
359 
360  // check variable size 1D types
361  checkWithDim1< BIAS::Vector<double>, double> (42, g_samples); //i.e. 42 elements
362  checkWithDim1< BIAS::Vector<float>, float> (42, g_samples);
363  checkWithDim1< BIAS::Vector<int>, int> (42, g_samples);
364  checkWithDim1< BIAS::Vector<unsigned char>, unsigned char> (42, g_samples);
365  checkWithDim1< BIAS::Vector<char >, char> (42, g_samples);
366  checkWithDim1< BIAS::Vector<unsigned short>,unsigned short>(42, g_samples);
367 
368 
369  // just to be 150% sure, check the typedefed names again
370  checkRGBuc ();
371  checkRGBAuc();
372  checkRGBf ();
373  checkRGBAf ();
374 
375  // show the values to check formatting / layout
376  DISPVAL(RGBuc (0, 255, 3));
377  DISPVAL(RGBAuc(0, 255, 3, 4));
378  DISPVAL(RGBf (1.f/3.f, -2.2f, 3.3f));
379  DISPVAL(RGBAf(1.f/3.f, -2.2f, 3.3f, 4.4f));
380 
381  // done, final result
382  if (g_result!=0){
383  cout<<"test failed with result="<<g_result<<endl;
384  return g_result;
385  } else {
386  cout<<"OK - test succeeded."<<endl;
387  return 0; // success
388  }
389 }
class RGBAf is a synonym for a Vector3 of correct type
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix2x2.hh:48
class RGBAuc is asynonym for a Vector4 of corerct type
class RGBAf is asynonym for a Vector4 of correct type
#define TMP
Test Matrix I/O precision issues.
class Vector2 contains a Vector of dim.
Definition: Vector2.hh:79
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
class RGBuc is a synonym for a Vector3 of correct type
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
is a &#39;fixed size&#39; rectangular matrix of dim.
Definition: Matrix3x3.hh:39
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix4x4.hh:54