]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkCSVReader.cxx
Merge branch 'vtk8itk5wx3-mingw64' of ssh://git.creatis.insa-lyon.fr/bbtk into vtk8it...
[bbtk.git] / packages / vtk / src / bbvtkCSVReader.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 #include "bbvtkCSVReader.h"
29 #include "bbvtkPackage.h"
30 namespace bbvtk
31 {
32
33 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,CSVReader)
34 BBTK_BLACK_BOX_IMPLEMENTATION(CSVReader,bbtk::AtomicBlackBox);
35 void CSVReader::Process()
36 {
37     if (bbGetInputIn().size() == 0)
38         {
39                 std::cout << "Set In with the file path to the csv file" << std::endl;
40                 return;
41         }
42
43         std::vector< std::vector< double > > matrix;
44         int tamaniox = 0;
45         int tamanioy = 0;
46
47         std::string line;
48         std::ifstream myfile (bbGetInputIn().c_str());
49         if (myfile.is_open())
50         {
51                 while (! myfile.eof() )
52                 {
53                         getline (myfile,line);
54                         if (line.size() > 0)
55                         {
56                             bbtk::translate t;
57                                 std::vector< double > numeros = t.stringTovectorDelimited(line, ",");
58                                 if (numeros.size() > 0)
59                                 {
60                                         tamanioy = numeros.size();
61                                         matrix.push_back(numeros);
62                                         tamaniox++;
63                                 }
64                         }
65                 }
66                 myfile.close();
67
68                 //Se arma la imagen vtk
69
70                 vtkImageData* ans = createImage(matrix, tamaniox, tamanioy, bbGetInputInType());
71
72                 bbSetOutputOut(ans);
73         }
74         else
75         {
76
77         }
78
79 }
80 void CSVReader::bbUserSetDefaultValues()
81 {
82
83 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
84 //    Here we initialize the input 'In' to 0
85    std::string path = "";
86    bbSetInputIn(path);
87
88    bbSetOutputOut(NULL);
89
90 }
91 void CSVReader::bbUserInitializeProcessing()
92 {
93
94 //  THE INITIALIZATION METHOD BODY :
95 //    Here does nothing
96 //    but this is where you should allocate the internal/output pointers
97 //    if any
98
99
100 }
101 void CSVReader::bbUserFinalizeProcessing()
102 {
103
104 //  THE FINALIZATION METHOD BODY :
105 //    Here does nothing
106 //    but this is where you should desallocate the internal/output pointers
107 //    if any
108
109 }
110 vtkImageData* CSVReader::createImage(std::vector< std::vector<double> > info, int x, int y, int scalar_type)
111 {
112         vtkImageData* final = vtkImageData::New();
113
114         //int ext[6];
115         int newDim[3];
116         //double space[3];
117         double origin[3];
118
119         //original->GetSpacing(space);
120         //original->GetExtent(ext);
121         //original->GetOrigin(origin);
122         //original->GetDimensions(newDim);
123
124
125         //final->SetSpacing(space);
126         newDim[0] = x;
127         newDim[1] = y;
128         newDim[2] = 1;
129         final->SetDimensions(newDim);
130         origin[0] = 0;
131         origin[1] = 0;
132         origin[2] = 0;
133         final->SetOrigin(origin);
134
135
136 //EED 2017-01-01 Migration VTK7
137 #if (VTK_MAJOR_VERSION <= 5) 
138         final->SetScalarType(scalar_type);
139         final->AllocateScalars();
140         final->Update();
141 #endif
142 #if (VTK_MAJOR_VERSION >= 6) 
143         final->AllocateScalars(scalar_type,1);
144 #endif
145
146
147         for (int i=0; i<newDim[0]; i++){
148                 for (int j=0; j<newDim[1]; j++){
149                         switch (scalar_type)
150                         {
151                                 case VTK_CHAR:
152                                         char * ap2;
153                                         ap2 = (char *) final->GetScalarPointer(i,j,0);
154                                         *ap2 = (char)info.at(i).at(j);
155                                 break;
156                                 case VTK_UNSIGNED_CHAR:
157                                         unsigned char * ap3;
158                                         ap3 = (unsigned char *) final->GetScalarPointer(i,j,0);
159                                         *ap3 = (unsigned char)info.at(i).at(j);
160                                 break;
161                                 case VTK_SHORT:
162                                         short * ap4;
163                                         ap4 = (short *) final->GetScalarPointer(i,j,0);
164                                         *ap4 = (short)info.at(i).at(j);
165                                 break;
166                                 case VTK_UNSIGNED_SHORT:
167                                         unsigned short * ap5;
168                                         ap5 = (unsigned short *) final->GetScalarPointer(i,j,0);
169                                         *ap5 = (unsigned short)info.at(i).at(j);
170                                 break;
171                                 case VTK_INT:
172                                         int * ap6;
173                                         ap6 = (int *) final->GetScalarPointer(i,j,0);
174                                         *ap6 = (int)info.at(i).at(j);
175                                 break;
176                                 case VTK_UNSIGNED_INT:
177                                         unsigned int * ap7;
178                                         ap7 = (unsigned int *) final->GetScalarPointer(i,j,0);
179                                         *ap7 = (unsigned int)info.at(i).at(j);
180                                 break;
181                                 case VTK_LONG:
182                                         long * ap8;
183                                         ap8 = (long *) final->GetScalarPointer(i,j,0);
184                                         *ap8 = (long)info.at(i).at(j);
185                                 break;
186                                 case VTK_UNSIGNED_LONG:
187                                         unsigned long * ap9;
188                                         ap9 = (unsigned long *) final->GetScalarPointer(i,j,0);
189                                         *ap9 = (unsigned long)info.at(i).at(j);
190                                 break;
191                                 case VTK_FLOAT:
192                                         float * ap10;
193                                         ap10 = (float *) final->GetScalarPointer(i,j,0);
194                                         *ap10 = (float)info.at(i).at(j);
195                                 break;
196                                 case VTK_DOUBLE:
197                                         double * ap11;
198                                         ap11 = (double *) final->GetScalarPointer(i,j,0);
199                                         *ap11 = (double)info.at(i).at(j);
200                                 break;
201                         }
202                 }
203         }
204         return final;
205 }
206 }
207 // EO namespace bbPersistence
208
209