]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkDistanceMap.cxx
#3276 creaVtk Feature New Normal - DistanceMap Box
[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 namespace bbcreaVtk
7 {
8
9 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,DistanceMap)
10 BBTK_BLACK_BOX_IMPLEMENTATION(DistanceMap,bbtk::AtomicBlackBox);
11 //===== 
12 // 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)
13 //===== 
14 void DistanceMap::Process()
15 {
16
17 // THE MAIN PROCESSING METHOD BODY
18 //   Here we simply set the input 'In' value to the output 'Out'
19 //   And print out the output value
20 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
21 //    void bbSet{Input|Output}NAME(const TYPE&)
22 //    const TYPE& bbGet{Input|Output}NAME() const 
23 //    Where :
24 //    * NAME is the name of the input/output
25 //      (the one provided in the attribute 'name' of the tag 'input')
26 //    * TYPE is the C++ type of the input/output
27 //      (the one provided in the attribute 'type' of the tag 'input')
28
29         if ( (bbGetInputIn()!=NULL) && (bbGetInputPoint1().size()==3) )
30         {
31
32                 int     ext[6];
33                 int     dim[3];
34                 double  spc[3];
35                 bbGetInputIn()->GetSpacing(spc);
36                 bbGetInputIn()->GetExtent(ext);
37                 dim[0]= ext[1]-ext[0]+1;
38                 dim[1]= ext[3]-ext[2]+1;
39                 dim[2]= ext[5]-ext[4]+1;
40                 if (imageoutput!=NULL) 
41                 {
42                         imageoutput->Delete();
43                 }       
44                 imageoutput = vtkImageData::New();
45                 imageoutput->Initialize();
46                 imageoutput->SetSpacing( spc );
47                 imageoutput->SetDimensions(  dim[0], dim[1], dim[2] );
48
49         //EED 2017-01-01 Migration VTK7
50         #if (VTK_MAJOR_VERSION <= 5) 
51                 imageoutput->SetScalarType( VTK_DOUBLE );       
52                 imageoutput->AllocateScalars();
53         #endif
54         #if (VTK_MAJOR_VERSION >= 6) 
55                 imageoutput->AllocateScalars( VTK_DOUBLE,1 );
56         #endif
57                 imageoutput->Modified();
58
59                 memset( imageoutput->GetScalarPointer() ,0, sizeof(double)*dim[0]*dim[1]*dim[2] );
60                 std::vector<int> lst1X;         
61                 std::vector<int> lst1Y;         
62                 std::vector<int> lst1Z;         
63
64                 std::vector<int> lst2X;         
65                 std::vector<int> lst2Y;         
66                 std::vector<int> lst2Z;         
67
68                 lst1X.push_back( bbGetInputPoint1()[0] );
69                 lst1Y.push_back( bbGetInputPoint1()[1]);
70                 lst1Z.push_back( bbGetInputPoint1()[2]);
71
72                 long int i,size;
73                 double glIn,glOut;
74                 double depth=0;
75                 int px,py,pz;           
76                 int pxOut,pyOut,pzOut;  
77
78                 while (lst1X.size()>0)
79                 {
80                         depth=depth+bbGetInputSlope();
81                         size=lst1X.size();
82                         for (i=0;i<size;i++)
83                         {
84                                 glIn=bbGetInputIn()->GetScalarComponentAsDouble( lst1X[i], lst1Y[i], lst1Z[i], 0);
85                                 if (glIn!=0)
86                                 {
87                                         glOut=imageoutput->GetScalarComponentAsDouble( lst1X[i], lst1Y[i], lst1Z[i], 0);
88                                         if (glOut==0)
89                                         {
90                                                 imageoutput->SetScalarComponentFromDouble( lst1X[i], lst1Y[i], lst1Z[i], 0, depth+(255-glIn));
91                                                 pxOut=lst1X[i]; pyOut=lst1Y[i]; pzOut=lst1Z[i];
92
93
94                                                 for (px=lst1X[i]-1;px<=lst1X[i]+1;px++)
95                                                 {
96                                                         for (py=lst1Y[i]-1;py<=lst1Y[i]+1;py++)
97                                                         {
98                                                                 for (pz=lst1Z[i]-1;pz<=lst1Z[i]+1;pz++)
99                                                                 {
100                                                                         if (!( (px==lst1X[i]) && (py==lst1Y[i]) && (pz==lst1Z[i]) )) 
101                                                                         {
102                                                                                 if ((px>=0) && (px<dim[0]) && (py>=0) && (py<dim[1]) &&  (pz>=0) && (pz<dim[2]) )
103                                                                                         { glOut=imageoutput->GetScalarComponentAsDouble( px,py,pz , 0); } else { glOut=-1; }
104                                                                                 if (glOut==0) { lst2X.push_back(px); lst2Y.push_back(  py ); lst2Z.push_back(  pz ); }
105                                                                         } // if 
106                                                                 } // for kk
107                                                         } // fo jj
108                                                 } // for ii
109
110                                                 if ( bbGetInputPoint2().size()==3)
111                                                 {
112                                                         if ( (bbGetInputPoint2()[0]==lst1X[i]) && (bbGetInputPoint2()[1]==lst1Y[i]) && (bbGetInputPoint2()[2]==lst1Z[i]) )
113                                                         {
114                                                                 i=size;  // out of for lst1
115                                                                 lst2X.clear();
116                                                                 lst2Y.clear();
117                                                                 lst2Z.clear();
118                                                         } // if Arrive to Point2
119                                                 } // Point size 3
120
121                                         }
122
123                                 }// If glIn
124
125                         }  // for lst1
126
127                         lst1X.clear();
128                         lst1Y.clear();
129                         lst1Z.clear();
130
131                         size=lst2X.size();
132                         for (i=0;i<size;i++)
133                         {
134                                 lst1X.push_back( lst2X[i] );
135                                 lst1Y.push_back( lst2Y[i] );
136                                 lst1Z.push_back( lst2Z[i] );
137                         } // for lst2
138
139                         lst2X.clear();
140                         lst2Y.clear();
141                         lst2Z.clear();
142
143                 } // while lstX
144
145
146                 std::vector<int> lstPointOut;
147                 lstPointOut.push_back( pxOut );
148                 lstPointOut.push_back( pyOut );
149                 lstPointOut.push_back( pzOut );
150
151                 std::vector<int> lstPathXOut;
152                 std::vector<int> lstPathYOut;
153                 std::vector<int> lstPathZOut;
154         
155                 lstPathXOut.push_back( pxOut );
156                 lstPathYOut.push_back( pyOut );
157                 lstPathZOut.push_back( pzOut );
158
159                 // find Path
160                 bool    ok2;
161                 bool    ok      = true;
162                 double  min     = imageoutput->GetScalarComponentAsDouble( pxOut,pyOut,pzOut , 0);
163                 int pxOutBack;
164                 int pyOutBack;
165                 int pzOutBack;
166                 while (ok==true)
167                 {       
168                         ok2=false;
169                         for (px=pxOut-1;px<=pxOut+1;px++)
170                         {
171                                 for (py=pyOut-1;py<=pyOut+1;py++)
172                                 {
173                                         for (pz=pzOut-1;pz<=pzOut+1;pz++)
174                                         {
175                                                 if ((px>=0) && (px<dim[0]) && (py>=0) && (py<dim[1]) && (pz>=0) && (pz<dim[2]) )
176                                                 { 
177                                                                 glOut=imageoutput->GetScalarComponentAsDouble( px,py,pz , 0); 
178                                                                 if ( (glOut<min) && (glOut!=0) )
179                                                                 {
180                                                                         min                     = glOut;
181                                                                         pxOutBack       = px;
182                                                                         pyOutBack       = py;
183                                                                         pzOutBack       = pz;
184                                                                         ok2                     = true;
185                                                                 } // if min
186                                                 } // if 
187                                         } // for kk
188                                 } // fo jj
189                         } // for ii
190
191                         if (ok2==true)
192                         {
193                                 pxOut=pxOutBack;
194                                 pyOut=pyOutBack;
195                                 pzOut=pzOutBack;
196                                 lstPathXOut.push_back( pxOut );
197                                 lstPathYOut.push_back( pyOut );
198                                 lstPathZOut.push_back( pzOut );
199                         } else {
200                                 ok=false;
201                         }
202                 }
203
204                 bbSetOutputOut( imageoutput );
205                 bbSetOutputFinalPoint( lstPointOut );
206
207                 bbSetOutputLstPathXOut( lstPathXOut );
208                 bbSetOutputLstPathYOut( lstPathYOut );
209                 bbSetOutputLstPathZOut( lstPathZOut );
210         } else {
211                 printf("EED Warnning! DistanceMap::Process  In or Point1 is EMPTY\n");
212         }  // if In Point1  
213
214 }
215 //===== 
216 // 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)
217 //===== 
218 void DistanceMap::bbUserSetDefaultValues()
219 {
220
221 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
222 //    Here we initialize the input 'In' to 0
223         bbSetInputIn(NULL);
224         bbSetInputSlope(10);
225         imageoutput=NULL;
226   
227 }
228 //===== 
229 // 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)
230 //===== 
231 void DistanceMap::bbUserInitializeProcessing()
232 {
233
234 //  THE INITIALIZATION METHOD BODY :
235 //    Here does nothing 
236 //    but this is where you should allocate the internal/output pointers 
237 //    if any 
238
239   
240 }
241 //===== 
242 // 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)
243 //===== 
244 void DistanceMap::bbUserFinalizeProcessing()
245 {
246
247 //  THE FINALIZATION METHOD BODY :
248 //    Here does nothing 
249 //    but this is where you should desallocate the internal/output pointers 
250 //    if any
251   
252 }
253 }
254 // EO namespace bbcreaVtk
255
256