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