]> Creatis software - bbtk.git/blob - packages/vtk/src/bbtkSimpleUtilities.h
484ec0aa8d31a93413139c32543d2b7feb2cd20d
[bbtk.git] / packages / vtk / src / bbtkSimpleUtilities.h
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 #ifndef _BBTKSIMPLEUTILITIES_H_
29 #define _BBTKSIMPLEUTILITIES_H_
30
31
32 #include "vtkVersion.h"
33
34 #include <time.h>
35 #include <vtkType.h>
36 #include <vtkImageData.h>
37 #include <cstdlib>
38 #include <iostream>
39 #include <sstream>
40 #include <vector>
41 #include <string>
42 #include <time.h>
43 #include <fstream>
44
45
46
47 namespace bbtk
48 {
49
50
51
52 template <typename T>
53 class Memcache
54 {
55 public:
56         vtkImageData* db;
57
58         Memcache()
59         {
60                 db = NULL;
61         }
62
63         ~Memcache()
64         {
65                 if (db!=NULL)
66                         db->Delete();
67         }
68
69         void init(int x, int y, int z)
70         {
71                 //Se pregunta al OS el tamanio de la palabra de un apuntador
72                 int palabra = sizeof(T);
73                 db = vtkImageData::New();
74                 db->SetDimensions(x,y,z);
75                 db->SetOrigin(0, 0, 0);
76
77 //EED 2017-01-01 Migration VTK7
78 #if (VTK_MAJOR_VERSION <= 5) 
79                 db->SetScalarType(Memcache::darTipo(palabra));
80                 db->AllocateScalars();
81                 db->Update();
82 #endif
83 #if (VTK_MAJOR_VERSION >= 6) 
84                 db->AllocateScalars(Memcache::darTipo(palabra),1);
85 #endif
86
87
88         }
89         void* get(int x, int y, int z)
90         {
91                 T* apap = (T*)db->GetScalarPointer(x,y,z);
92                 void* ap = (void*)*apap;
93                 return ap;
94         }
95
96         void* pop(int x, int y, int z)
97         {
98                 T* apap = (T*)db->GetScalarPointer(x,y,z);
99                 void* ap = (void*)*apap;
100                 *apap = NULL;
101                 return ap;
102         }
103
104         void put(int x, int y, int z, void* apt)
105         {
106                 T* punto = (T*)db->GetScalarPointer(x,y,z);
107                 *punto = (T)apt;
108         }
109         void clear()
110         {
111                 int ext[6];
112                 db->GetExtent(ext);
113                 T* ap1;
114                 for (int i=ext[0]; i<=ext[1]; i++)
115                 {
116                         for (int j=ext[2]; j<=ext[3]; j++)
117                         {
118                                 for (int k=ext[4]; k<=ext[5]; k++)
119                                 {
120                                         ap1 = (T*)db->GetScalarPointer(i,j,k);
121                                         *ap1 = NULL;
122                                 }
123                         }
124                 }
125         }
126         static int darTipo(int tamanio)
127         {
128                 if (tamanio == sizeof(char))
129                         return VTK_CHAR;
130                 if (tamanio == sizeof(unsigned char))
131                         return VTK_UNSIGNED_CHAR;
132                 if (tamanio == sizeof(short))
133                         return VTK_SHORT;
134                 if (tamanio == sizeof(unsigned short))
135                         return VTK_UNSIGNED_SHORT;
136                 if (tamanio == sizeof(int))
137                         return VTK_INT;
138                 if (tamanio == sizeof(unsigned int))
139                         return VTK_UNSIGNED_INT;
140                 if (tamanio == sizeof(long))
141                         return VTK_LONG;
142                 if (tamanio == sizeof(unsigned long))
143                         return VTK_UNSIGNED_LONG;
144                 if (tamanio == sizeof(float))
145                         return VTK_FLOAT;
146                 if (tamanio == sizeof(double))
147                         return VTK_DOUBLE;
148                 return 0;
149         }
150
151 };
152
153
154 class tiempo
155 {
156         public:
157         static time_t first, second;
158     static double bytes;
159 static void tic(vtkImageData* img)
160         {
161                 tiempo::bytes = VTK_SIZE(img);
162                 tiempo::first = time (NULL);
163         }
164         static void tic(int bytes_=0)
165         {
166                 tiempo::bytes = bytes_;
167                 tiempo::first = time (NULL);
168         }
169
170         static void toc()
171         {
172                 tiempo::second = time (NULL);
173                 double diferencia = difftime (tiempo::second,tiempo::first);
174                 printf ("Total time for %f Bytes is %f (%f Bytes/s)\n", tiempo::bytes, diferencia, tiempo::bytes/diferencia);
175         }
176
177         static int VTK_SIZE(vtkImageData* img)
178         {
179                 int dims[3];
180                 img->GetDimensions(dims);
181                 int tamanio = VTK_SIZE_T(img->GetScalarType());
182                 std::cout << "[" << dims[0] << "x" << dims[1] << "x" << dims[2] << "x" << tamanio << "]" << std::endl;
183                 return dims[0]*dims[1]*dims[2]*tamanio;
184         }
185         static int VTK_SIZE_T(int tipo)
186         {
187                 switch(tipo)
188                 {
189                         case VTK_CHAR:
190                                 return sizeof(char);
191                         break;
192                         case VTK_UNSIGNED_CHAR:
193                                 return sizeof(unsigned char);
194                         break;
195                         case VTK_SHORT:
196                                 return sizeof(short);
197                         break;
198                         case VTK_UNSIGNED_SHORT:
199                                 return sizeof(unsigned short);
200                         break;
201                         case VTK_INT:
202                                 return sizeof(int);
203                         break;
204                         case VTK_UNSIGNED_INT:
205                                 return sizeof(unsigned int);
206                         break;
207                         case VTK_LONG:
208                                 return sizeof(long);
209                         break;
210                         case VTK_UNSIGNED_LONG:
211                                 return sizeof(unsigned long);
212                         break;
213                         case VTK_FLOAT:
214                                 return sizeof(float);
215                         break;
216                         case VTK_DOUBLE:
217                                 return sizeof(double);
218                         break;
219                         default:
220                                 return 0;
221                         break;
222                 }
223         }
224 };
225
226 /*
227 namespace what
228 {
229            std::string kind(int*);
230            std::string is(int*, bool withkind=false);
231            std::string kind(double* a);
232            std::string is(double* a, bool withkind=false);
233            std::string kind(float* a);
234            std::string is(float* a, bool withkind=false);
235            std::string kind(char* a);
236            std::string is(char* a, bool withkind=false);
237            std::string kind(long* a);
238            std::string is(long* a, bool withkind=false);
239            std::string kind(short* a);
240            std::string is(short* a, bool withkind=false);
241            std::string kind(bool* a);
242            std::string is(bool* a, bool withkind=false);
243            std::string kind(void* a);
244            std::string is(void* a, bool withkind=false);
245
246            std::string kind(unsigned int* a);
247            std::string is(unsigned int* a, bool withkind=false);
248            std::string kind(unsigned char* a);
249            std::string is(unsigned char* a, bool withkind=false);
250            std::string kind(unsigned long* a);
251            std::string is(unsigned long* a, bool withkind=false);
252            std::string kind(unsigned short* a);
253            std::string is(unsigned short* a, bool withkind=false);
254            std::string kind(__int64* a);
255            std::string is(__int64* a, bool withkind=false);
256            std::string kind(unsigned __int64* a);
257            std::string is(unsigned __int64* a, bool withkind=false);
258            std::string kind(long double* a);
259            std::string is(long double* a, bool withkind=false);
260
261            std::string kind(int a);
262            std::string is(int a, bool withkind=false);
263            std::string kind(double a);
264            std::string is(double a, bool withkind=false);
265            std::string kind(float a);
266            std::string is(float a, bool withkind=false);
267            std::string kind(short a);
268            std::string is(short a, bool withkind=false);
269            std::string kind(char a);
270            std::string is(char a, bool withkind=false);
271            std::string kind(long a);
272            std::string is(long a, bool withkind=false);
273            std::string kind(bool a);
274            std::string is(bool a, bool withkind=false);
275            std::string kind(unsigned int a);
276            std::string is(unsigned int a, bool withkind=false);
277            std::string kind(unsigned char a);
278            std::string is(unsigned char a, bool withkind=false);
279            std::string kind(unsigned long a);
280            std::string is(unsigned long a, bool withkind=false);
281            std::string kind(unsigned short a);
282            std::string is(unsigned short a, bool withkind=false);
283            std::string kind(__int64 a);
284            std::string is(__int64 a, bool withkind=false);
285            std::string kind(unsigned __int64 a);
286            std::string is(unsigned __int64 a, bool withkind=false);
287            std::string kind(long double a);
288            std::string is(long double a, bool withkind=false);
289
290            std::string kind(std::vector<int> a);
291            std::string is(std::vector<int> a, bool withkind=false);
292            std::string kind(std::vector<double> a);
293            std::string is(std::vector<double> a, bool withkind=false);
294            std::string kind(std::vector<float> a);
295            std::string is(std::vector<float> a, bool withkind=false);
296            std::string kind(std::vector<short> a);
297            std::string is(std::vector<short> a, bool withkind=false);
298            std::string kind(std::vector<char> a);
299            std::string is(std::vector<char> a, bool withkind=false);
300            std::string kind(std::vector<long> a);
301            std::string is(std::vector<long> a, bool withkind=false);
302            std::string kind(std::vector<bool> a);
303            std::string is(std::vector<bool> a, bool withkind=false);
304            std::string kind(std::vector<__int64> a);
305            std::string is(std::vector<__int64> a, bool withkind=false);
306            std::string kind(std::vector<unsigned int> a);
307            std::string is(std::vector<unsigned int> a, bool withkind=false);
308            std::string kind(std::vector<unsigned char> a);
309            std::string is(std::vector<unsigned char> a, bool withkind=false);
310            std::string kind(std::vector<unsigned long> a);
311            std::string is(std::vector<unsigned long> a, bool withkind=false);
312            std::string kind(std::vector<unsigned short> a);
313            std::string is(std::vector<unsigned short> a, bool withkind=false);
314            std::string kind(std::vector<long double> a);
315            std::string is(std::vector<long double> a, bool withkind=false);
316
317                    std::string is(double* a,int len=1, bool withkind=false);
318                    std::string is(int* a,int len=1, bool withkind=false);
319                    std::string is(float* a,int len=1, bool withkind=false);
320                    std::string is(char* a,int len=1, bool withkind=false);
321 }
322
323 */
324
325 class persistence
326 {
327     public:
328         bool writeFile(std::string stm, std::string content);
329         std::string readFile(std::string stm);
330 };
331
332 class logging
333 {
334     public:
335          std::string fecha();
336          void out(std::string texto);
337          void erase();
338 };
339
340 class translate
341 {
342     public:
343         /**
344         el texto debe ser de la forma [12,45.6] y retorna un vector con la informacion esperada
345         **/
346          std::vector<double> stringTovector(std::string, std::string start="[", std::string end="]");
347          /**
348         Ayuda a parsear una linea de texto que contiene numeros
349          */
350          std::vector<double> stringTovectorDelimited(std::string, std::string);
351
352         std::string vectorToStringDelimited(std::vector<double> a, std::string delim);
353          std::vector<double*> stringToVectorArray(std::string);
354         /**
355         StringSplit toma str y lo parte por el delimitador delim y retorna el vector de partes
356         **/
357          std::vector<std::string> StringSplit(std::string, std::string);
358         /**
359         Lo mismo que stringTovector, pero retornando un arreglo en vez de un vector
360         **/
361          double* stringToArray(std::string);
362          std::string getInnerInfo(std::string);
363 //       std::vector<double*> stringToVectorArray(std::string);
364 };
365
366 class vectores
367 {
368     public:
369         //Recorre el vector de manera ciclica
370         double vectorInfinite(std::vector<double> in, int index);
371 };
372
373 class sorts
374 {
375     public:
376          void bubble(std::vector<double> &array);
377 };
378
379 }
380
381 #endif // _BBTKSIMPLEUTILITIES_H_