]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
11276096f7fadf9b99ecd1cab3ac366e67c3c672
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkReadMHDPlane.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbcreaVtkReadMHDPlane.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include "stdio.h"
8
9 namespace bbcreaVtk
10 {
11
12 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane)
13 BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
14 //===== 
15 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
16 //===== 
17 void ReadMHDPlane::Process()
18 {
19
20 // THE MAIN PROCESSING METHOD BODY
21 //   Here we simply set the input 'In' value to the output 'Out'
22 //   And print out the output value
23 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
24 //    void bbSet{Input|Output}NAME(const TYPE&)
25 //    const TYPE& bbGet{Input|Output}NAME() const 
26 //    Where :
27 //    * NAME is the name of the input/output
28 //      (the one provided in the attribute 'name' of the tag 'input')
29 //    * TYPE is the C++ type of the input/output
30 //      (the one provided in the attribute 'type' of the tag 'input')
31
32 //    bbSetOutputOut( bbGetInputIn() );
33 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
34
35 printf("EED ReadMHDPlane::Process Start\n");
36
37         std::string inputfilename;
38
39         if (bbGetInputDirectionPlane()=="XY")
40         {
41                 inputfilename=bbGetInputFileName();
42         } // if XY
43
44
45         if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
46         {
47
48                 //-- Split FileName
49                 std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
50                 std::string path                =       bbGetInputFileName().substr(0,found+1);
51                 std::string filename    =       bbGetInputFileName().substr(found+1);
52 #ifdef _WIN32
53                 path=path+"YZ_ZX\\";
54 #else
55                 path=path+"YZ_ZX/";
56 #endif
57         
58         
59                 if (bbGetInputDirectionPlane()=="YZ") 
60                 { 
61                         inputfilename = path+filename+"_YZ.mhd";  
62                 } // if YZ
63
64                 if (bbGetInputDirectionPlane()=="ZX")
65                 {
66                         inputfilename = path+filename+"_ZX.mhd";
67                 } // XZ
68         } // if YZ || XZ
69
70         vtkImageData *newImage;
71         long int newHeaderSize;
72     std::string newFileName=inputfilename+"-OneSlice";
73
74     int sx,sy,sz;
75     char mystring[250];
76     char strTmp[20];
77         bool ok=true;
78     FILE *ffIn  = fopen(inputfilename.c_str(),"r");
79         if (ffIn!=NULL)
80         {
81 printf("EED ReadMHDPlane::Process 1\n");
82             FILE *ffOut = fopen(newFileName.c_str(),"w");
83 printf("EED ReadMHDPlane::Process 2 %p  %s\n",ffOut, newFileName.c_str() );
84
85         while(!feof(ffIn))
86         {
87             strcpy(mystring,"\n");
88                         fgets(mystring,250,ffIn);
89 printf("EED ReadMHDPlane::Process MHD:%s\n", mystring);
90                 if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
91                 if (strncmp("DimSize",mystring,6)==0) 
92                         {
93                                 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
94                                 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
95                         newHeaderSize = sx*sy*bbGetInputSlice();
96                                 if (bbGetInputSlice()>=sz) {ok=false;}
97                         } // if
98                 if (strncmp("ElementType = MET_CHAR",mystring,24)==0)   { newHeaderSize=newHeaderSize*1; }
99                 if (strncmp("ElementType = MET_UCHAR",mystring,24)==0)  { newHeaderSize=newHeaderSize*1; }
100                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
101                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
102                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newHeaderSize=newHeaderSize*2; }
103                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
104                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
105                 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
106                 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
107                 if (strncmp("ElementDataFile",mystring,15)==0) 
108                         {
109                                 fprintf(ffOut,"HeaderSize = %ld\n\n", newHeaderSize );
110                         } // if
111 printf("EED ReadMHDPlane::Process 3\n");
112                 fprintf(ffOut,mystring); 
113 printf("EED ReadMHDPlane::Process 4\n");
114         } // while
115                 fclose(ffIn);
116                 fclose(ffOut);
117
118                 if (ok==true)
119                 {
120                         vtkMetaImageReader * reader = vtkMetaImageReader::New();
121                         reader->SetFileName( newFileName.c_str() );
122                         reader->Update();
123                         newImage = reader->GetOutput();
124                 } // if ok
125         } else {
126                 ok=false;
127                 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
128         }
129
130         if (ok==false)
131         {
132                 int i;
133                 int sizeX, sizeY, sizeZ;
134                 sizeX = 200;
135                 sizeY = sizeX;
136                 sizeZ = 1;
137                 newImage = vtkImageData::New();
138                 newImage->Initialize();
139                 newImage->SetScalarTypeToUnsignedChar();
140                 newImage->SetSpacing( 1,1,1 );
141                 newImage->SetDimensions(  sizeX,sizeY,sizeZ );
142                 newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
143                 newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
144                 newImage->SetNumberOfScalarComponents(1);
145                 newImage->AllocateScalars();
146                 newImage->Update();
147                 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
148                 for (i=0; i<sizeX; i++)
149                 {
150                         newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
151                         newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
152                 } // for i
153         } // if ok
154
155         bbSetOutputOut( newImage );
156 printf("EED ReadMHDPlane::Process End\n");
157
158 }
159 //===== 
160 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
161 //===== 
162 void ReadMHDPlane::bbUserSetDefaultValues()
163 {
164
165 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
166 //    Here we initialize the input 'In' to 0
167    bbSetInputFileName("");
168    bbSetInputSlice(0);
169    bbSetInputDirectionPlane("XY");
170   
171 }
172 //===== 
173 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
174 //===== 
175 void ReadMHDPlane::bbUserInitializeProcessing()
176 {
177
178 //  THE INITIALIZATION METHOD BODY :
179 //    Here does nothing 
180 //    but this is where you should allocate the internal/output pointers 
181 //    if any 
182
183   
184 }
185 //===== 
186 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
187 //===== 
188 void ReadMHDPlane::bbUserFinalizeProcessing()
189 {
190
191 //  THE FINALIZATION METHOD BODY :
192 //    Here does nothing 
193 //    but this is where you should desallocate the internal/output pointers 
194 //    if any
195   
196 }
197 }
198 // EO namespace bbcreaVtk
199
200