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