]> Creatis software - bbtk.git/blob - packages/vtk/src/bbtkSimpleUtilities.cxx
#3000 BBTK Bug New Normal - vtk package CleanPlanes box. limits not clear
[bbtk.git] / packages / vtk / src / bbtkSimpleUtilities.cxx
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
29 #include "bbtkSimpleUtilities.h"
30 #define LOG_FILE_NAME "log.txt"
31
32 namespace bbtk
33 {
34
35
36 //template <typename T>
37
38
39
40
41
42 /*
43 namespace what
44 {
45            std::string kind(int*);
46            std::string is(int*, bool withkind=false);
47            std::string kind(double* a);
48            std::string is(double* a, bool withkind=false);
49            std::string kind(float* a);
50            std::string is(float* a, bool withkind=false);
51            std::string kind(char* a);
52            std::string is(char* a, bool withkind=false);
53            std::string kind(long* a);
54            std::string is(long* a, bool withkind=false);
55            std::string kind(short* a);
56            std::string is(short* a, bool withkind=false);
57            std::string kind(bool* a);
58            std::string is(bool* a, bool withkind=false);
59            std::string kind(void* a);
60            std::string is(void* a, bool withkind=false);
61
62            std::string kind(unsigned int* a);
63            std::string is(unsigned int* a, bool withkind=false);
64            std::string kind(unsigned char* a);
65            std::string is(unsigned char* a, bool withkind=false);
66            std::string kind(unsigned long* a);
67            std::string is(unsigned long* a, bool withkind=false);
68            std::string kind(unsigned short* a);
69            std::string is(unsigned short* a, bool withkind=false);
70            std::string kind(__int64* a);
71            std::string is(__int64* a, bool withkind=false);
72            std::string kind(unsigned __int64* a);
73            std::string is(unsigned __int64* a, bool withkind=false);
74            std::string kind(long double* a);
75            std::string is(long double* a, bool withkind=false);
76
77            std::string kind(int a);
78            std::string is(int a, bool withkind=false);
79            std::string kind(double a);
80            std::string is(double a, bool withkind=false);
81            std::string kind(float a);
82            std::string is(float a, bool withkind=false);
83            std::string kind(short a);
84            std::string is(short a, bool withkind=false);
85            std::string kind(char a);
86            std::string is(char a, bool withkind=false);
87            std::string kind(long a);
88            std::string is(long a, bool withkind=false);
89            std::string kind(bool a);
90            std::string is(bool a, bool withkind=false);
91            std::string kind(unsigned int a);
92            std::string is(unsigned int a, bool withkind=false);
93            std::string kind(unsigned char a);
94            std::string is(unsigned char a, bool withkind=false);
95            std::string kind(unsigned long a);
96            std::string is(unsigned long a, bool withkind=false);
97            std::string kind(unsigned short a);
98            std::string is(unsigned short a, bool withkind=false);
99            std::string kind(__int64 a);
100            std::string is(__int64 a, bool withkind=false);
101            std::string kind(unsigned __int64 a);
102            std::string is(unsigned __int64 a, bool withkind=false);
103            std::string kind(long double a);
104            std::string is(long double a, bool withkind=false);
105
106            std::string kind(std::vector<int> a);
107            std::string is(std::vector<int> a, bool withkind=false);
108            std::string kind(std::vector<double> a);
109            std::string is(std::vector<double> a, bool withkind=false);
110            std::string kind(std::vector<float> a);
111            std::string is(std::vector<float> a, bool withkind=false);
112            std::string kind(std::vector<short> a);
113            std::string is(std::vector<short> a, bool withkind=false);
114            std::string kind(std::vector<char> a);
115            std::string is(std::vector<char> a, bool withkind=false);
116            std::string kind(std::vector<long> a);
117            std::string is(std::vector<long> a, bool withkind=false);
118            std::string kind(std::vector<bool> a);
119            std::string is(std::vector<bool> a, bool withkind=false);
120            std::string kind(std::vector<__int64> a);
121            std::string is(std::vector<__int64> a, bool withkind=false);
122            std::string kind(std::vector<unsigned int> a);
123            std::string is(std::vector<unsigned int> a, bool withkind=false);
124            std::string kind(std::vector<unsigned char> a);
125            std::string is(std::vector<unsigned char> a, bool withkind=false);
126            std::string kind(std::vector<unsigned long> a);
127            std::string is(std::vector<unsigned long> a, bool withkind=false);
128            std::string kind(std::vector<unsigned short> a);
129            std::string is(std::vector<unsigned short> a, bool withkind=false);
130            std::string kind(std::vector<long double> a);
131            std::string is(std::vector<long double> a, bool withkind=false);
132
133                    std::string is(double* a,int len=1, bool withkind=false);
134                    std::string is(int* a,int len=1, bool withkind=false);
135                    std::string is(float* a,int len=1, bool withkind=false);
136                    std::string is(char* a,int len=1, bool withkind=false);
137
138 }
139 */
140
141
142
143         bool persistence::writeFile(std::string stm, std::string content)
144         {
145                         std::ofstream myfile (stm.c_str());
146                         if (myfile.is_open())
147                         {
148                                 myfile << content;
149                                 myfile.close();
150                                 return true;
151                         }
152                         else
153                         {
154                                 return false;
155                         }
156         }
157
158         std::string persistence::readFile(std::string path)
159         {
160                 std::string line;
161                 std::stringstream stm;
162                 std::ifstream myfile (path.c_str());
163                 if (myfile.is_open())
164                 {
165                         while (! myfile.eof() )
166                         {
167                                 getline (myfile,line);
168                                 stm << line << std::endl;
169                         }
170                         myfile.close();
171                 }
172                 else
173                 {
174
175                 }
176                 return stm.str();
177         }
178
179
180
181
182         std::string logging::fecha()
183         {
184                 time_t seconds;
185                 seconds = time (NULL);
186
187                 //char timeStr [9];
188                 std::stringstream out;
189                 out << seconds;
190                 std::string la_fecha = out.str();
191                 //_strtime( timeStr );
192                 //std::string la_fecha(timeStr);
193                 return "["+la_fecha+"] ";
194         }
195
196         void logging::out(std::string texto)
197         {
198                 std::ofstream outFile;
199                 outFile.open(LOG_FILE_NAME, std::ios::app);
200                 if (outFile)
201                 {
202                         outFile << fecha() << texto << std::endl;
203                 }
204         }
205
206         void logging::erase()
207         {
208                 std::ofstream outFile;
209                 outFile.open(LOG_FILE_NAME, std::ios::out);
210                 if (outFile)
211                 {
212                         outFile << fecha() << "INICIO" << std::endl;
213                 }
214         }
215
216
217         std::vector<std::string> translate::StringSplit(std::string str, std::string delim)
218         {
219                 std::vector<std::string> results;
220                 int cutAt;
221                 while( (cutAt = (int)str.find_first_of(delim)) != str.npos )
222                 {
223                         if(cutAt > 0)
224                         {
225                                 results.push_back(str.substr(0,cutAt));
226                         }
227                                 str = str.substr(cutAt+1);
228                         }
229                         if(str.length() > 0)
230                         {
231                                 results.push_back(str);
232                         }
233                 return results;
234         }
235
236         std::vector<double> translate::stringTovector(std::string texto, std::string start, std::string end)
237         {
238                 std::vector<double> el_vector;
239                 std::vector<std::string> partes;
240
241
242                 int inicial = (int)texto.find( start, 0 );
243                 int final = (int)texto.find( end, 0 );
244
245                 if (inicial == std::string::npos || final == std::string::npos)
246                         return el_vector;
247                 if (final<=inicial)
248                         return el_vector;
249
250                 std::string contenido = texto.substr(inicial+1, final-inicial-1);
251
252                 partes = StringSplit(contenido, ",");
253                 if (partes.size() != -1){
254                         for (int i=0; i<(int)partes.size(); i++)
255                         {
256                                 if (partes.at(i).size() > 0)
257                                 {
258                                         double uno = atof(partes.at(i).data());
259                                         el_vector.push_back(uno);
260                                 }
261                         }
262                 }
263                 return el_vector;
264         }
265
266         std::vector<double> translate::stringTovectorDelimited(std::string texto, std::string delimitador)
267         {
268                 std::vector<double> el_vector;
269                 std::vector<std::string> partes;
270
271                 std::string contenido = texto;
272
273                 partes = StringSplit(contenido, delimitador);
274                 if (partes.size() != -1){
275                         for (int i=0; i<(int)partes.size(); i++)
276                         {
277                                 double uno = atof(partes.at(i).data());
278                                 el_vector.push_back(uno);
279                         }
280                 }
281                 return el_vector;
282         }
283
284     double* translate::stringToArray(std::string texto)
285         {
286         std::vector<double> el_vector = stringTovector(texto);
287         double* array = new double[el_vector.size()];
288         for (int i=0; i<(int)el_vector.size(); i++)
289         {
290             array[i]=el_vector[i];
291         }
292                 return array;
293         }
294
295         std::string translate::getInnerInfo(std::string texto)
296         {
297                 int first_par = (int)texto.find("[", 0);
298                 int last_par = (int)texto.rfind("]", texto.size());
299
300                 return texto.substr(first_par+1,last_par-first_par-1);
301         }
302
303         std::vector<double*> translate::stringToVectorArray(std::string texto)
304         {
305                 std::vector<double*> el_vector;
306                 std::string innerInfo = getInnerInfo(texto);
307
308                 int first_par = 0;
309                 int last_par = 0;
310                 int lastComma = 0;
311
312                 while (lastComma != std::string::npos)
313                 {
314                         first_par = (int)innerInfo.find("[", lastComma);
315                         last_par = (int)innerInfo.find("]", lastComma);
316                         lastComma = (int)innerInfo.find(",", last_par);
317                         if (first_par == std::string::npos || last_par == std::string::npos)
318                                 break;
319                         std::string subArreglo = innerInfo.substr(first_par,last_par-first_par+1);
320                         double* subArray = stringToArray(subArreglo);
321                         el_vector.push_back(subArray);
322                 }
323                 return el_vector;
324         }
325
326         std::string translate::vectorToStringDelimited(std::vector<double> a, std::string delim)
327         {
328                 std::stringstream out;
329
330         unsigned int i;
331         for (i = 0; i < a.size(); i++)
332         {
333             out << a.at(i);
334             if (i != a.size()-1)
335                 out << delim;
336         }
337         return out.str();
338         }
339
340
341
342 //Recorre el vector de manera ciclica
343 double vectores::vectorInfinite(std::vector<double> in, int index)
344 {
345     //std::cout << "index = " << index;
346     int value;
347         int tamanio = in.size();
348         bool inverso = false;
349         if (tamanio == 0)
350                 return -1;
351
352         if (index < 0)
353         {
354                 inverso = true;
355                 index = abs(index);
356                 value = tamanio - index%tamanio;
357                 if (value == tamanio)
358                    value = 0;
359         }else{
360                 value = index%tamanio;
361         }
362         //std::cout << ", value=" << value << std::endl;
363         //return 0;
364     return in.at(value);
365 }
366
367
368
369         /**
370         Ordenamiento busbuja, array[0] es el maximo
371         http://mathbits.com/mathbits/compsci/Arrays/Bubble.htm
372         **/
373         void sorts::bubble(std::vector<double> &array)
374         {
375                 double i, j, flag = 1;    // set flag to 1 to begin initial pass
376                 double temp;             // holding variable
377                 double arrayLength = array.size();
378                 for(i = 1; (i <= arrayLength) && flag; i++)
379                 {
380                         flag = 0;
381                         for (j=0; j < (arrayLength -1); j++)
382                         {
383                                 if (array[(int)(j+1)] > array[(int)j])      // ascending order simply changes to <
384                                 {
385                                                 temp = array[(int)j];             // swap elements
386                                                 array[(int)j] = array[(int)(j+1)];
387                                                 array[(int)(j+1)] = temp;
388                                                 flag = 1;               // indicates that a swap occurred.
389                                 }
390                         }
391                 }
392         }
393
394 };
395
396