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