]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkDistanceMap.cxx
#3281 creaVtk Feature New Normal - FindAxisEED
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkDistanceMap.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 "bbcreaVtkDistanceMap.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include "creaVtk_MACROS.h"
8
9 namespace bbcreaVtk
10 {
11
12 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,DistanceMap)
13 BBTK_BLACK_BOX_IMPLEMENTATION(DistanceMap,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 DistanceMap::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         if ( (bbGetInputIn()!=NULL) && (bbGetInputPoint1().size()==3) )
33         {
34
35                 int             ext[6];
36                 int             dim[3];
37                 long int        dimX,dimXY;
38                 double  spc[3];
39                 bbGetInputIn()->GetSpacing(spc);
40                 bbGetInputIn()->GetExtent(ext);
41                 dim[0]  = ext[1]-ext[0]+1;
42                 dim[1]  = ext[3]-ext[2]+1;
43                 dim[2]  = ext[5]-ext[4]+1;
44                 dimX    = dim[0];
45                 dimXY   = dim[0]*dim[1];
46                 if (imageoutput!=NULL) 
47                 {
48                         imageoutput->Delete();
49                 }       
50                 imageoutput = vtkImageData::New();
51                 imageoutput->Initialize();
52                 imageoutput->SetSpacing( spc );
53                 imageoutput->SetDimensions(  dim[0], dim[1], dim[2] );
54
55         //EED 2017-01-01 Migration VTK7
56         #if (VTK_MAJOR_VERSION <= 5) 
57                 imageoutput->SetScalarType( VTK_DOUBLE );       
58                 imageoutput->AllocateScalars();
59         #endif
60         #if (VTK_MAJOR_VERSION >= 6) 
61                 imageoutput->AllocateScalars( VTK_DOUBLE,1 );
62         #endif
63                 imageoutput->Modified();
64
65                 memset( imageoutput->GetScalarPointer() ,0, sizeof(double)*dim[0]*dim[1]*dim[2] );
66                 std::vector<int> lst1X;         
67                 std::vector<int> lst1Y;         
68                 std::vector<int> lst1Z;         
69
70                 std::vector<int> lst2X;         
71                 std::vector<int> lst2Y;         
72                 std::vector<int> lst2Z;         
73
74                 lst1X.push_back( bbGetInputPoint1()[0] );
75                 lst1Y.push_back( bbGetInputPoint1()[1]);
76                 lst1Z.push_back( bbGetInputPoint1()[2]);
77
78                 long int i,size;
79                 double glIn,glOut;
80                 double depth=0;
81                 int px,py,pz;           
82                 int pxOut,pyOut,pzOut;  
83                 double tmpValue;
84
85
86                 DEF_POINTER_IMAGE_VTK_CREA(vIn,ssIn,pIn,stIn,bbGetInputIn())
87                 DEF_POINTER_IMAGE_VTK_CREA(vOut,ssOut,pOut,stOut,imageoutput)
88
89                 while (lst1X.size()>0)
90                 {
91                         depth=depth+bbGetInputSlope();
92                         size=lst1X.size();
93                         for (i=0;i<size;i++)
94                         {
95                                 GETVALUE2_VTK_CREA(glIn,pIn,stIn, lst1X[i] + lst1Y[i]*dimX + lst1Z[i]*dimXY )
96
97 //                              glIn=bbGetInputIn()->GetScalarComponentAsDouble( lst1X[i], lst1Y[i], lst1Z[i], 0);
98                                 if (glIn!=0)
99                                 {
100                                         GETVALUE2_VTK_CREA(glOut,pOut,stOut, lst1X[i] + lst1Y[i]*dimX + lst1Z[i]*dimXY )
101 //                                      glOut=imageoutput->GetScalarComponentAsDouble( lst1X[i], lst1Y[i], lst1Z[i], 0);
102                                         if (glOut==0)
103                                         {
104                                                 tmpValue        =       depth  +  (255-glIn)*bbGetInputAverageRadius()*2;
105                                                 SETVALUE2_VTK_CREA(tmpValue,pOut,stOut,  lst1X[i] + lst1Y[i]*dimX + lst1Z[i]*dimXY )
106 //                                              imageoutput->SetScalarComponentFromDouble( lst1X[i], lst1Y[i], lst1Z[i], 0, depth+(255-glIn)*bbGetInputAverageRadius()*2);
107                                                 pxOut=lst1X[i]; pyOut=lst1Y[i]; pzOut=lst1Z[i];
108
109
110                                                 for (px=lst1X[i]-1;px<=lst1X[i]+1;px++)
111                                                 {
112                                                         for (py=lst1Y[i]-1;py<=lst1Y[i]+1;py++)
113                                                         {
114                                                                 for (pz=lst1Z[i]-1;pz<=lst1Z[i]+1;pz++)
115                                                                 {
116                                                                         if (!( (px==lst1X[i]) && (py==lst1Y[i]) && (pz==lst1Z[i]) )) 
117                                                                         {
118                                                                                 if ((px>=0) && (px<dim[0]) && (py>=0) && (py<dim[1]) &&  (pz>=0) && (pz<dim[2]) )
119                                                                                 { 
120                                                                                         GETVALUE2_VTK_CREA(glOut,pOut,stOut, px + py*dimX + pz*dimXY )
121 //                                                                                      glOut=imageoutput->GetScalarComponentAsDouble( px,py,pz , 0); 
122                                                                                 } else { 
123                                                                                         glOut=-1; 
124                                                                                 } // if px py pz  inside the image
125                                                                                 if (glOut==0) { lst2X.push_back(px); lst2Y.push_back(  py ); lst2Z.push_back(  pz ); }
126                                                                         } // if 
127                                                                 } // for kk
128                                                         } // fo jj
129                                                 } // for ii
130
131                                                 if ( bbGetInputPoint2().size()==3)
132                                                 {
133                                                         if ( (bbGetInputPoint2()[0]==lst1X[i]) && (bbGetInputPoint2()[1]==lst1Y[i]) && (bbGetInputPoint2()[2]==lst1Z[i]) )
134                                                         {
135                                                                 i=size;  // out of for lst1
136                                                                 lst2X.clear();
137                                                                 lst2Y.clear();
138                                                                 lst2Z.clear();
139                                                         } // if Arrive to Point2
140                                                 } // Point size 3
141
142                                         }
143
144                                 }// If glIn
145
146                         }  // for lst1
147
148                         lst1X.clear();
149                         lst1Y.clear();
150                         lst1Z.clear();
151
152                         size=lst2X.size();
153                         for (i=0;i<size;i++)
154                         {
155                                 lst1X.push_back( lst2X[i] );
156                                 lst1Y.push_back( lst2Y[i] );
157                                 lst1Z.push_back( lst2Z[i] );
158                         } // for lst2
159
160                         lst2X.clear();
161                         lst2Y.clear();
162                         lst2Z.clear();
163
164                 } // while lstX
165
166
167                 std::vector<int> lstPointOut;
168                 lstPointOut.push_back( pxOut );
169                 lstPointOut.push_back( pyOut );
170                 lstPointOut.push_back( pzOut );
171
172                 std::vector<int> lstPathXOut;
173                 std::vector<int> lstPathYOut;
174                 std::vector<int> lstPathZOut;
175         
176                 lstPathXOut.push_back( pxOut );
177                 lstPathYOut.push_back( pyOut );
178                 lstPathZOut.push_back( pzOut );
179
180                 // find Path
181                 bool    ok2;
182                 bool    ok      = true;
183                 double  min     = imageoutput->GetScalarComponentAsDouble( pxOut,pyOut,pzOut , 0);
184                 int pxOutBack;
185                 int pyOutBack;
186                 int pzOutBack;
187                 while (ok==true)
188                 {       
189                         ok2=false;
190                         for (px=pxOut-1;px<=pxOut+1;px++)
191                         {
192                                 for (py=pyOut-1;py<=pyOut+1;py++)
193                                 {
194                                         for (pz=pzOut-1;pz<=pzOut+1;pz++)
195                                         {
196                                                 if ((px>=0) && (px<dim[0]) && (py>=0) && (py<dim[1]) && (pz>=0) && (pz<dim[2]) )
197                                                 { 
198                                                                 GETVALUE2_VTK_CREA(glOut,pOut,stOut, px + py*dimX + pz*dimXY )
199 //                                                              glOut=imageoutput->GetScalarComponentAsDouble( px,py,pz , 0); 
200                                                                 if ( (glOut<min) && (glOut!=0) )
201                                                                 {
202                                                                         min                     = glOut;
203                                                                         pxOutBack       = px;
204                                                                         pyOutBack       = py;
205                                                                         pzOutBack       = pz;
206                                                                         ok2                     = true;
207                                                                 } // if min
208                                                 } // if 
209                                         } // for pz
210                                 } // fo py
211                         } // for px
212
213                         if (ok2==true)
214                         {
215                                 pxOut=pxOutBack;
216                                 pyOut=pyOutBack;
217                                 pzOut=pzOutBack;
218                                 lstPathXOut.push_back( pxOut );
219                                 lstPathYOut.push_back( pyOut );
220                                 lstPathZOut.push_back( pzOut );
221                         } else {
222                                 ok=false;
223                         }
224                 } // while
225
226                 bbSetOutputOut( imageoutput );
227                 bbSetOutputFinalPoint( lstPointOut );
228                 bbSetOutputLengthPixels( lstPathXOut.size() );
229
230                 // invert list of points
231                 size=lstPathXOut.size();
232                 long int size2=size/2;
233                 int tmp;        
234                 for (i=0; i<size2;i++)
235                 {
236                         tmp= lstPathXOut[i]; lstPathXOut[i]=lstPathXOut[size-1-i]; lstPathXOut[size-1-i]=tmp;
237                         tmp= lstPathYOut[i]; lstPathYOut[i]=lstPathYOut[size-1-i]; lstPathYOut[size-1-i]=tmp;
238                         tmp= lstPathZOut[i]; lstPathZOut[i]=lstPathZOut[size-1-i]; lstPathZOut[size-1-i]=tmp;
239                 } // for invert list            
240
241                 bbSetOutputLstPathXOut( lstPathXOut );
242                 bbSetOutputLstPathYOut( lstPathYOut );
243                 bbSetOutputLstPathZOut( lstPathZOut );
244         } else {
245                 printf("EED Warnning! DistanceMap::Process  In or Point1 is EMPTY\n");
246         }  // if In Point1  
247
248 }
249 //===== 
250 // 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)
251 //===== 
252 void DistanceMap::bbUserSetDefaultValues()
253 {
254
255 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
256 //    Here we initialize the input 'In' to 0
257         bbSetInputIn(NULL);
258         bbSetInputAverageRadius(10);
259         bbSetInputSlope(10);
260         imageoutput=NULL;
261   
262 }
263 //===== 
264 // 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)
265 //===== 
266 void DistanceMap::bbUserInitializeProcessing()
267 {
268
269 //  THE INITIALIZATION METHOD BODY :
270 //    Here does nothing 
271 //    but this is where you should allocate the internal/output pointers 
272 //    if any 
273
274   
275 }
276 //===== 
277 // 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)
278 //===== 
279 void DistanceMap::bbUserFinalizeProcessing()
280 {
281
282 //  THE FINALIZATION METHOD BODY :
283 //    Here does nothing 
284 //    but this is where you should desallocate the internal/output pointers 
285 //    if any
286   
287 }
288 }
289 // EO namespace bbcreaVtk
290
291