]> Creatis software - bbtk.git/blob - packages/vtk/src/bbtkSimpleUtilities.h
91d2d9762dd88a88559563ab0bc0f8aeeb149a2c
[bbtk.git] / packages / vtk / src / bbtkSimpleUtilities.h
1 #ifndef _BBTKSIMPLEUTILITIES_H_
2 #define _BBTKSIMPLEUTILITIES_H_
3
4 #include <time.h>
5 #include <vtkType.h>
6 #include <vtkImageData.h>
7 #include <cstdlib>
8 #include <iostream>
9 #include <sstream>
10 #include <vector>
11 #include <string>
12 #include <time.h>
13 #include <fstream>
14
15
16
17 namespace bbtk
18 {
19
20
21
22 template <typename T>
23 class Memcache
24 {
25 public:
26         vtkImageData* db;
27
28         Memcache()
29         {
30                 db = NULL;
31         }
32
33         ~Memcache()
34         {
35                 if (db!=NULL)
36                         db->Delete();
37         }
38
39         void init(int x, int y, int z)
40         {
41                 //Se pregunta al OS el tamanio de la palabra de un apuntador
42                 int palabra = sizeof(T);
43                 db = vtkImageData::New();
44                 db->SetScalarType(Memcache::darTipo(palabra));
45                 db->SetDimensions(x,y,z);
46                 db->SetOrigin(0, 0, 0);
47                 db->AllocateScalars();
48                 db->Update();
49         }
50         void* get(int x, int y, int z)
51         {
52                 T* apap = (T*)db->GetScalarPointer(x,y,z);
53                 void* ap = (void*)*apap;
54                 return ap;
55         }
56
57         void* pop(int x, int y, int z)
58         {
59                 T* apap = (T*)db->GetScalarPointer(x,y,z);
60                 void* ap = (void*)*apap;
61                 *apap = NULL;
62                 return ap;
63         }
64
65         void put(int x, int y, int z, void* apt)
66         {
67                 T* punto = (T*)db->GetScalarPointer(x,y,z);
68                 *punto = (T)apt;
69         }
70         void clear()
71         {
72                 int ext[6];
73                 db->GetExtent(ext);
74                 T* ap1;
75                 for (int i=ext[0]; i<=ext[1]; i++)
76                 {
77                         for (int j=ext[2]; j<=ext[3]; j++)
78                         {
79                                 for (int k=ext[4]; k<=ext[5]; k++)
80                                 {
81                                         ap1 = (T*)db->GetScalarPointer(i,j,k);
82                                         *ap1 = NULL;
83                                 }
84                         }
85                 }
86         }
87         static int darTipo(int tamanio)
88         {
89                 if (tamanio == sizeof(char))
90                         return VTK_CHAR;
91                 if (tamanio == sizeof(unsigned char))
92                         return VTK_UNSIGNED_CHAR;
93                 if (tamanio == sizeof(short))
94                         return VTK_SHORT;
95                 if (tamanio == sizeof(unsigned short))
96                         return VTK_UNSIGNED_SHORT;
97                 if (tamanio == sizeof(int))
98                         return VTK_INT;
99                 if (tamanio == sizeof(unsigned int))
100                         return VTK_UNSIGNED_INT;
101                 if (tamanio == sizeof(long))
102                         return VTK_LONG;
103                 if (tamanio == sizeof(unsigned long))
104                         return VTK_UNSIGNED_LONG;
105                 if (tamanio == sizeof(float))
106                         return VTK_FLOAT;
107                 if (tamanio == sizeof(double))
108                         return VTK_DOUBLE;
109                 return 0;
110         }
111
112 };
113
114
115 class tiempo
116 {
117         public:
118         static time_t first, second;
119     static double bytes;
120 static void tic(vtkImageData* img)
121         {
122                 tiempo::bytes = VTK_SIZE(img);
123                 tiempo::first = time (NULL);
124         }
125         static void tic(int bytes_=0)
126         {
127                 tiempo::bytes = bytes_;
128                 tiempo::first = time (NULL);
129         }
130
131         static void toc()
132         {
133                 tiempo::second = time (NULL);
134                 double diferencia = difftime (tiempo::second,tiempo::first);
135                 printf ("Total time for %f Bytes is %f (%f Bytes/s)\n", tiempo::bytes, diferencia, tiempo::bytes/diferencia);
136         }
137
138         static int VTK_SIZE(vtkImageData* img)
139         {
140                 int dims[3];
141                 img->GetDimensions(dims);
142                 int tamanio = VTK_SIZE_T(img->GetScalarType());
143                 std::cout << "[" << dims[0] << "x" << dims[1] << "x" << dims[2] << "x" << tamanio << "]" << std::endl;
144                 return dims[0]*dims[1]*dims[2]*tamanio;
145         }
146         static int VTK_SIZE_T(int tipo)
147         {
148                 switch(tipo)
149                 {
150                         case VTK_CHAR:
151                                 return sizeof(char);
152                         break;
153                         case VTK_UNSIGNED_CHAR:
154                                 return sizeof(unsigned char);
155                         break;
156                         case VTK_SHORT:
157                                 return sizeof(short);
158                         break;
159                         case VTK_UNSIGNED_SHORT:
160                                 return sizeof(unsigned short);
161                         break;
162                         case VTK_INT:
163                                 return sizeof(int);
164                         break;
165                         case VTK_UNSIGNED_INT:
166                                 return sizeof(unsigned int);
167                         break;
168                         case VTK_LONG:
169                                 return sizeof(long);
170                         break;
171                         case VTK_UNSIGNED_LONG:
172                                 return sizeof(unsigned long);
173                         break;
174                         case VTK_FLOAT:
175                                 return sizeof(float);
176                         break;
177                         case VTK_DOUBLE:
178                                 return sizeof(double);
179                         break;
180                         default:
181                                 return 0;
182                         break;
183                 }
184         }
185 };
186
187 /*
188 namespace what
189 {
190            std::string kind(int*);
191            std::string is(int*, bool withkind=false);
192            std::string kind(double* a);
193            std::string is(double* a, bool withkind=false);
194            std::string kind(float* a);
195            std::string is(float* a, bool withkind=false);
196            std::string kind(char* a);
197            std::string is(char* a, bool withkind=false);
198            std::string kind(long* a);
199            std::string is(long* a, bool withkind=false);
200            std::string kind(short* a);
201            std::string is(short* a, bool withkind=false);
202            std::string kind(bool* a);
203            std::string is(bool* a, bool withkind=false);
204            std::string kind(void* a);
205            std::string is(void* a, bool withkind=false);
206
207            std::string kind(unsigned int* a);
208            std::string is(unsigned int* a, bool withkind=false);
209            std::string kind(unsigned char* a);
210            std::string is(unsigned char* a, bool withkind=false);
211            std::string kind(unsigned long* a);
212            std::string is(unsigned long* a, bool withkind=false);
213            std::string kind(unsigned short* a);
214            std::string is(unsigned short* a, bool withkind=false);
215            std::string kind(__int64* a);
216            std::string is(__int64* a, bool withkind=false);
217            std::string kind(unsigned __int64* a);
218            std::string is(unsigned __int64* a, bool withkind=false);
219            std::string kind(long double* a);
220            std::string is(long double* a, bool withkind=false);
221
222            std::string kind(int a);
223            std::string is(int a, bool withkind=false);
224            std::string kind(double a);
225            std::string is(double a, bool withkind=false);
226            std::string kind(float a);
227            std::string is(float a, bool withkind=false);
228            std::string kind(short a);
229            std::string is(short a, bool withkind=false);
230            std::string kind(char a);
231            std::string is(char a, bool withkind=false);
232            std::string kind(long a);
233            std::string is(long a, bool withkind=false);
234            std::string kind(bool a);
235            std::string is(bool a, bool withkind=false);
236            std::string kind(unsigned int a);
237            std::string is(unsigned int a, bool withkind=false);
238            std::string kind(unsigned char a);
239            std::string is(unsigned char a, bool withkind=false);
240            std::string kind(unsigned long a);
241            std::string is(unsigned long a, bool withkind=false);
242            std::string kind(unsigned short a);
243            std::string is(unsigned short a, bool withkind=false);
244            std::string kind(__int64 a);
245            std::string is(__int64 a, bool withkind=false);
246            std::string kind(unsigned __int64 a);
247            std::string is(unsigned __int64 a, bool withkind=false);
248            std::string kind(long double a);
249            std::string is(long double a, bool withkind=false);
250
251            std::string kind(std::vector<int> a);
252            std::string is(std::vector<int> a, bool withkind=false);
253            std::string kind(std::vector<double> a);
254            std::string is(std::vector<double> a, bool withkind=false);
255            std::string kind(std::vector<float> a);
256            std::string is(std::vector<float> a, bool withkind=false);
257            std::string kind(std::vector<short> a);
258            std::string is(std::vector<short> a, bool withkind=false);
259            std::string kind(std::vector<char> a);
260            std::string is(std::vector<char> a, bool withkind=false);
261            std::string kind(std::vector<long> a);
262            std::string is(std::vector<long> a, bool withkind=false);
263            std::string kind(std::vector<bool> a);
264            std::string is(std::vector<bool> a, bool withkind=false);
265            std::string kind(std::vector<__int64> a);
266            std::string is(std::vector<__int64> a, bool withkind=false);
267            std::string kind(std::vector<unsigned int> a);
268            std::string is(std::vector<unsigned int> a, bool withkind=false);
269            std::string kind(std::vector<unsigned char> a);
270            std::string is(std::vector<unsigned char> a, bool withkind=false);
271            std::string kind(std::vector<unsigned long> a);
272            std::string is(std::vector<unsigned long> a, bool withkind=false);
273            std::string kind(std::vector<unsigned short> a);
274            std::string is(std::vector<unsigned short> a, bool withkind=false);
275            std::string kind(std::vector<long double> a);
276            std::string is(std::vector<long double> a, bool withkind=false);
277
278                    std::string is(double* a,int len=1, bool withkind=false);
279                    std::string is(int* a,int len=1, bool withkind=false);
280                    std::string is(float* a,int len=1, bool withkind=false);
281                    std::string is(char* a,int len=1, bool withkind=false);
282 }
283
284 */
285
286 class persistence
287 {
288     public:
289         bool writeFile(std::string stm, std::string content);
290         std::string readFile(std::string stm);
291 };
292
293 class logging
294 {
295     public:
296          std::string fecha();
297          void out(std::string texto);
298          void erase();
299 };
300
301 class translate
302 {
303     public:
304         /**
305         el texto debe ser de la forma [12,45.6] y retorna un vector con la informacion esperada
306         **/
307          std::vector<double> stringTovector(std::string, std::string start="[", std::string end="]");
308          /**
309         Ayuda a parsear una linea de texto que contiene numeros
310          */
311          std::vector<double> stringTovectorDelimited(std::string, std::string);
312
313         std::string vectorToStringDelimited(std::vector<double> a, std::string delim);
314          std::vector<double*> stringToVectorArray(std::string);
315         /**
316         StringSplit toma str y lo parte por el delimitador delim y retorna el vector de partes
317         **/
318          std::vector<std::string> StringSplit(std::string, std::string);
319         /**
320         Lo mismo que stringTovector, pero retornando un arreglo en vez de un vector
321         **/
322          double* stringToArray(std::string);
323          std::string getInnerInfo(std::string);
324 //       std::vector<double*> stringToVectorArray(std::string);
325 };
326
327 class vectores
328 {
329     public:
330         //Recorre el vector de manera ciclica
331         double vectorInfinite(std::vector<double> in, int index);
332 };
333
334 class sorts
335 {
336     public:
337          void bubble(std::vector<double> &array);
338 };
339
340 }
341
342 #endif /* _DLL_H_ */