]> Creatis software - bbtk.git/blob - packages/std/src/bbstdVectorFilterDouble.cxx
2258e8190a78bad6cd17b7cf3ab849e0f80a5dcd
[bbtk.git] / packages / std / src / bbstdVectorFilterDouble.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 "bbstdVectorFilterDouble.h"
5 #include "bbstdPackage.h"
6 #include "math.h"
7
8 namespace bbstd
9 {
10
11 BBTK_ADD_BLACK_BOX_TO_PACKAGE(std,VectorFilterDouble)
12 BBTK_BLACK_BOX_IMPLEMENTATION(VectorFilterDouble,bbtk::AtomicBlackBox);
13 //===== 
14 // 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)
15 //===== 
16 void VectorFilterDouble::Process()
17 {
18 // THE MAIN PROCESSING METHOD BODY
19 //   Here we simply set the input 'In' value to the output 'Out'
20 //   And print out the output value
21 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
22 //    void bbSet{Input|Output}NAME(const TYPE&)
23 //    const TYPE& bbGet{Input|Output}NAME() const 
24 //    Where :
25 //    * NAME is the name of the input/output
26 //      (the one provided in the attribute 'name' of the tag 'input')
27 //    * TYPE is the C++ type of the input/output
28 //      (the one provided in the attribute 'type' of the tag 'input')
29         std::vector< std::vector<double> * >  pLstVec;  
30         std::vector< std::vector<double> * >  pLstVecOut;  
31         std::vector<double> In0 = bbGetInputIn0();
32         std::vector<double> In1 = bbGetInputIn1();
33         std::vector<double> In2 = bbGetInputIn2();
34         std::vector<double> In3 = bbGetInputIn3();
35         std::vector<double> In4 = bbGetInputIn4();
36         std::vector<double> In5 = bbGetInputIn5();
37         std::vector<double> In6 = bbGetInputIn6();
38         std::vector<double> In7 = bbGetInputIn7();
39         std::vector<double> In8 = bbGetInputIn8();
40         std::vector<double> In9 = bbGetInputIn9();
41         if (bbGetInputIn0().size()!=0) { pLstVec.push_back( &In0 ); }
42         if (bbGetInputIn1().size()!=0) { pLstVec.push_back( &In1 ); }
43         if (bbGetInputIn2().size()!=0) { pLstVec.push_back( &In2 ); }
44         if (bbGetInputIn3().size()!=0) { pLstVec.push_back( &In3 ); }
45         if (bbGetInputIn4().size()!=0) { pLstVec.push_back( &In4 ); }
46         if (bbGetInputIn5().size()!=0) { pLstVec.push_back( &In5 ); }
47         if (bbGetInputIn6().size()!=0) { pLstVec.push_back( &In6 ); }
48         if (bbGetInputIn7().size()!=0) { pLstVec.push_back( &In7 ); }
49         if (bbGetInputIn8().size()!=0) { pLstVec.push_back( &In8 ); }
50         if (bbGetInputIn9().size()!=0) { pLstVec.push_back( &In9 ); }
51         std::vector<double> Out0;
52         std::vector<double> Out1;
53         std::vector<double> Out2;
54         std::vector<double> Out3;
55         std::vector<double> Out4;
56         std::vector<double> Out5;
57         std::vector<double> Out6;
58         std::vector<double> Out7;
59         std::vector<double> Out8;
60         std::vector<double> Out9;
61         pLstVecOut.push_back( &Out0 );
62         pLstVecOut.push_back( &Out1 );
63         pLstVecOut.push_back( &Out2 );
64         pLstVecOut.push_back( &Out3 );
65         pLstVecOut.push_back( &Out4 );
66         pLstVecOut.push_back( &Out5 );
67         pLstVecOut.push_back( &Out6 );
68         pLstVecOut.push_back( &Out7 );
69         pLstVecOut.push_back( &Out8 );
70         pLstVecOut.push_back( &Out9 );
71         if (bbGetInputType()==0)  // Erase duplicate lines
72         {
73                 if (bbGetInputIn0().size()!=0)  // At least one element 
74                 {
75                         bool okSizeVec=true;
76                         int ipLstvec; 
77                         for (ipLstvec=1;ipLstvec<pLstVec.size();ipLstvec++)
78                         {
79                                 if ( (*pLstVec[ipLstvec]).size()!=(*pLstVec[0]).size() )  { okSizeVec=false; }
80                         }
81                         if ( okSizeVec==false)
82                         {
83                                 printf("EED VectorFilterDouble::Process WARNING! vectors are not of the same size.\n");
84                         } else {
85                                 int iLine;
86                                 int ipLstvec2; 
87                                 bool okLine;
88                                 for (iLine=0 ; iLine < (*pLstVec[0]).size() ; iLine++ ) 
89                                 {
90                                         okLine=false;
91                                         for ( ipLstvec2=0 ; ipLstvec2<pLstVec.size() ; ipLstvec2++)
92                                         {
93                                                 if ( (*pLstVec[ipLstvec2])[iLine]!=(*pLstVec[ipLstvec2])[iLine-1] )  { okLine=true; }
94                                         } // for ipLstVec2
95                                                         
96                                         if ( (okLine==true) || (iLine==0) ) 
97                                         {
98                                                 for (ipLstvec2=0 ; ipLstvec2<pLstVec.size() ; ipLstvec2++)
99                                                 {
100                                                         (*pLstVecOut[ipLstvec2]).push_back( (*pLstVec[ipLstvec2])[iLine] );
101                                                 } // for
102                                         } // if okLine
103                                 } // for iLine
104                         } // if okSizeVec
105                 } // bbGetInputIn0()  size
106         } // Type==0
107         if (bbGetInputType()==1) // Resize vector
108         {
109                 int             ipLstvec2;
110                 double  iLine;
111                 int     sizeLines       = (*pLstVec[0]).size();
112                 double  step            = (double)sizeLines/((double)bbGetInputk1()[0]-1);
113                 for (iLine=0 ; iLine<sizeLines ; iLine=iLine+step)
114                 {
115                         for (ipLstvec2=0 ; ipLstvec2<pLstVec.size() ; ipLstvec2++)
116                         {
117                                 (*pLstVecOut[ipLstvec2]).push_back( (*pLstVec[ipLstvec2])[(int)iLine] );
118                         } // for                        
119                 } // for ipLstVec
120                 // adding the lastone
121                 for (ipLstvec2=0 ; ipLstvec2<pLstVec.size() ; ipLstvec2++)
122                 {
123                         (*pLstVecOut[ipLstvec2]).push_back( (*pLstVec[ipLstvec2])[(int)(sizeLines-1)] );
124                 } // for
125         } // Type==1
126         if (bbGetInputType()==2) // Insert intermediate points
127         {
128                 int             ipLstvec;
129                 int     i;
130                 int     size;
131                 double  result;
132                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
133                 {
134                         size=(*pLstVec[ipLstvec]).size();
135                         if (size>=2)
136                         {
137                                 for (i=0;i<size-1;i++)
138                                 {
139                                         (*pLstVecOut[ipLstvec]).push_back( (*pLstVec[ipLstvec])[i] );   // First item                   
140                                         result=((*pLstVec[ipLstvec])[i] + (*pLstVec[ipLstvec])[i+1]  ) / 2;
141                                         (*pLstVecOut[ipLstvec]).push_back( result );                            
142                                 }// for size
143                                 (*pLstVecOut[ipLstvec]).push_back( (*pLstVec[ipLstvec])[size-1] );      // Last item            
144                         } // if size>=2
145                 } // for pLstVec                
146         } // Type==2
147         if (bbGetInputType()==3) // Addition  k1[0]
148         {
149                 int             ipLstvec;
150                 int     i;
151                 int     size;
152                 double  result;
153                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
154                 {
155                         size=(*pLstVec[ipLstvec]).size();
156                         for (i=0;i<size;i++)
157                         {
158                                 result= (*pLstVec[ipLstvec])[i] + bbGetInputk1()[0] ;
159                                 (*pLstVecOut[ipLstvec]).push_back( result );                            
160                         }// for size
161                 } // for pLstVec                
162         } // Type==3
163         if (bbGetInputType()==4) // Substraction  k1[0]
164         {
165                 int             ipLstvec;
166                 int     i;
167                 int     size;
168                 double  result;
169                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
170                 {
171                         size=(*pLstVec[ipLstvec]).size();
172                         for (i=0;i<size;i++)
173                         {
174                                 result= (*pLstVec[ipLstvec])[i] - bbGetInputk1()[0] ;
175                                 (*pLstVecOut[ipLstvec]).push_back( result );                            
176                         }// for size
177                 } // for pLstVec                
178         } // Type==4
179         if (bbGetInputType()==5) // Multiplication  k1[0]
180         {
181                 int     ipLstvec;
182                 int     i;
183                 int     size;
184                 double  result;
185                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
186                 {
187                         size=(*pLstVec[ipLstvec]).size();
188                         for (i=0;i<size;i++)
189                         {
190                                 result= (*pLstVec[ipLstvec])[i] * bbGetInputk1()[0] ;
191                                 (*pLstVecOut[ipLstvec]).push_back( result );                            
192                         }// for size
193                 } // for pLstVec                
194         } // Type==5
195         if (bbGetInputType()==6) // Division  k1
196         {
197                 int     ipLstvec;
198                 int     i;
199                 int     size;
200                 double  result;
201                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
202                 {
203                         size=(*pLstVec[ipLstvec]).size();
204                         for (i=0;i<size;i++)
205                         {
206                                 if (bbGetInputk1()[0]!=0)
207                                 {
208                                         result= (*pLstVec[ipLstvec])[i] / bbGetInputk1()[0] ;
209                                         (*pLstVecOut[ipLstvec]).push_back( result );                            
210                                 } else {
211                                         printf("EED VectorFilterDouble::Process  Warning!! Div by 0 in Type 6 \n");
212                                         (*pLstVecOut[ipLstvec]).push_back( -1 );                                
213                                 }
214                         }// for size
215                 } // for pLstVec                
216         } // Type==6
217         if (bbGetInputType()==7) 
218         {
219                 int i,j;
220                 for (j=0;j<In4.size(); j++)
221                 {
222                         Out0.push_back( In4[j] );
223                         Out1.push_back( In5[j] );
224                         Out2.push_back( In6[j] );
225                 } //
226                 int iGeneral=0,sizeI,sizeLst1Indexes=In3.size();
227                 for (i=0; i<sizeLst1Indexes-1;i++) { iGeneral=iGeneral+In3[i]; }
228                 sizeI=iGeneral+In3[ sizeLst1Indexes-1 ];
229                 int jGeneral = 0;
230                 int sizeJ        = In7[ 0 ];
231                 double dist2,dist2Min,dx,dy,dz;
232                 int iBack;
233                 for (j=jGeneral;j<sizeJ;j++) 
234                 {
235                         dist2Min=1000000;
236                         for (i=iGeneral;i<sizeI;i++) 
237                         {       
238                                 dx              = In0[i]-In4[j];
239                                 dy              = In1[i]-In5[j];
240                                 dz              = In2[i]-In6[j];
241                                 dist2           = dx*dx + dy*dy + dz*dz;
242                                 if (dist2<dist2Min) 
243                                 {
244                                                 dist2Min=dist2;
245                                                 iBack=i;
246                                 } // if
247                         } // for j
248                         Out0[j]=In0[iBack]; 
249                         Out1[j]=In1[iBack]; 
250                         Out2[j]=In2[iBack]; 
251                 } // for i
252         }       // Type 7
253         if (bbGetInputType()==8)   // order all vectors using as base the vector In0
254         {
255                 double tmp;
256                 int i,j,ii,sizeII;
257                 sizeII=pLstVec.size();
258                 int size=In0.size();
259                 for (i=0;i<size; i++)
260                 {
261                         for (ii=0;ii<sizeII;ii++)
262                         {
263                                  (*pLstVecOut[ii]).push_back( (*pLstVec[ii])[i] ); 
264                         } // for ii
265                 } // i
266                 for (i=0;i<size;i++)
267                 {
268                         for (j=i;j<size;j++)
269                         {
270                                 if ((*pLstVecOut[0])[j] <= (*pLstVecOut[0])[i] ) 
271                                 {
272                                         for (ii=0;ii<sizeII;ii++)
273                                         {
274                                                 tmp = (*pLstVecOut[ii])[i]; 
275                                                 (*pLstVecOut[ii])[i] = (*pLstVecOut[ii])[j]; 
276                                                 (*pLstVecOut[ii])[j] = tmp; 
277                                         } // for ii
278                                 } // if <
279                         } // for j
280                 } // for i
281         } // Type 8
282         if (bbGetInputType()==9)   // Invert vectors
283         {
284                 double tmp;
285                 int i,j,ii,sizeII;
286                 sizeII=pLstVec.size();
287                 int size=In0.size();
288                 int size2=size/2;
289                 for (i=0;i<size; i++)
290                 {
291                         for (ii=0;ii<sizeII;ii++)
292                         {
293                                  (*pLstVecOut[ii]).push_back( (*pLstVec[ii])[i] ); 
294                         } // for ii
295                 } // i
296                 for (i=0;i<size2;i++)
297                 {
298                         for (ii=0;ii<sizeII;ii++)
299                         {
300                                 j=size-1-i;     
301                                 tmp = (*pLstVecOut[ii])[i]; 
302                                 (*pLstVecOut[ii])[i] = (*pLstVecOut[ii])[ j ]; 
303                                 (*pLstVecOut[ii])[j] = tmp; 
304                         } // for ii
305                 } // for i
306         } // Type 9
307         if (bbGetInputType()==10)   // Nearest point in vector
308         {
309                 int     sizeLstX        =       In0.size();   // lstX
310                 int     sizeLstY        =       In1.size();   // lstY
311                 int     sizeLstZ        =       In2.size();   // lstZ
312                 if ( (sizeLstX==sizeLstY) && (sizeLstY==sizeLstZ) && (sizeLstX>0) && (In3.size()==3) )
313                 {
314                         int i;
315                         double x1               =       In3[0];  // pointX of pointXYZ
316                         double y1               =       In3[1];  // pointY of pointXYZ
317                         double z1               =       In3[2];  // pointZ of pointXYZ
318                         double distMin  =       100000;
319                         double dist;
320                         double x2;
321                         double y2;
322                         double z2;
323                         Out0.push_back( -1 ); 
324                         Out1.push_back( -999 ); 
325                         Out1.push_back( -999 ); 
326                         Out1.push_back( -999 ); 
327                         for (i=0;i<sizeLstX; i++)
328                         {
329                                 x2 = In0[i];    // lstX
330                                 y2 = In1[i];    // lstY
331                                 z2 = In2[i];    // lstZ
332                                 dist = sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2) );
333                                 if (dist<distMin) 
334                                 {
335                                         distMin = dist;
336                                         Out0[0] = i;
337                                         Out1[0] = x2;
338                                         Out1[1] = y2;
339                                         Out1[2] = z2;
340                                 }
341                         } // for i
342                 } else {
343                         printf("EED Warnning VectorFilterDouble::Process()   For Type 10 the size of the vectors are not coherent.\n");
344                 }// if size
345         } // Type 10
346     if (bbGetInputType()==11)   // Nearest point in vector
347     {
348         int i;
349         int    sizeLstX    =    In0.size();   // lstX
350         int    sizeLstY    =    In1.size();   // lstY
351         int    sizeLstZ    =    In2.size();   // lstZ
352         std::vector<double> spc = bbGetInputk1();
353         if (spc.size()>=1)
354         {
355             for (i=0;i<sizeLstX; i++)
356             {
357                 Out0.push_back( In0[i]*spc[0] );
358             } // for i
359         } else {
360             printf("EED Warnning VectorFilterDouble::Process()   For Type 11 the K1 vector (spacing) is not coherent.\n");
361         }// if size
362         if (spc.size()>=2)
363         {
364             for (i=0;i<sizeLstY; i++)
365             {
366                 Out1.push_back( In1[i]*spc[1] );
367             } // for i
368         } else {
369             printf("EED Warnning VectorFilterDouble::Process()   For Type 11 the K1 vector (spacing) is not coherent.\n");
370         }// if size
371         if (spc.size()>=3)
372         {
373             for (i=0;i<sizeLstZ; i++)
374             {
375                 Out2.push_back( In2[i]*spc[2] );
376             } // for i
377         } else {
378             printf("EED Warnning VectorFilterDouble::Process()   For Type 11 the K1 vector (spacing) is not coherent.\n");
379         }// if size
380      } // Type 11
381     if (bbGetInputType()==12)   // Nearest point in vector
382     {
383         int i;
384         int    sizeLstX    =    In0.size();   // lstX
385         int    sizeLstY    =    In1.size();   // lstY
386         int    sizeLstZ    =    In2.size();   // lstZ
387         std::vector<double> spc = bbGetInputk1();
388         if (spc.size()>=1)
389         {
390             if (spc[0]!=0)
391             {
392                 for (i=0;i<sizeLstX; i++)
393                 {
394                     Out0.push_back( In0[i]/spc[0] );
395                 } // for i
396             } // if spc !=0
397         } else {
398             printf("EED Warnning VectorFilterDouble::Process()   For Type 12 the K1 vector (spacing) is not coherent.\n");
399         }// if size
400         if (spc.size()>=2)
401         {
402             if (spc[0]!=0)
403             {
404                 for (i=0;i<sizeLstY; i++)
405                 {
406                     Out1.push_back( In1[i]/spc[1] );
407                 } // for i
408             } // if spc !=0
409         } else {
410             printf("EED Warnning VectorFilterDouble::Process()   For Type 12 the K1 vector (spacing) is not coherent.\n");
411         }// if size
412         if (spc.size()>=3)
413         {
414             if (spc[0]!=0)
415             {
416                 for (i=0;i<sizeLstZ; i++)
417                 {
418                     Out2.push_back( In2[i]/spc[2] );
419                 } // for i
420             } // if spc !=0
421         } else {
422             printf("EED Warnning VectorFilterDouble::Process()   For Type 12 the K1 vector (spacing) is not coherent.\n");
423         }// if size
424      } // Type 12
425     if (bbGetInputType()==13)   // Distance point 3D between In0 and In1
426     {
427         if ( In0.size()==3 and In1.size()==3)
428         {
429             double x = In0[0] - In1[0];
430             double y = In0[1] - In1[1];
431             double z = In0[2] - In1[2];
432             Out0.push_back( sqrt( x*x +y*y +z*z ) );
433         }
434      } // Type 13
435         bbSetOutputOut0( Out0 );
436         bbSetOutputOut1( Out1 );
437         bbSetOutputOut2( Out2 );
438         bbSetOutputOut3( Out3 );
439         bbSetOutputOut4( Out4 );
440         bbSetOutputOut5( Out5 );
441         bbSetOutputOut6( Out6 );
442         bbSetOutputOut7( Out7 );
443         bbSetOutputOut8( Out8 );
444         bbSetOutputOut9( Out9 );
445 }
446
447 //===== 
448 // 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)
449 //===== 
450 void VectorFilterDouble::bbUserSetDefaultValues()
451 {
452 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
453 //    Here we initialize the input 'In' to 0
454     bbSetInputType(0);
455     std::vector<double> k1;
456     k1.push_back(0);
457     bbSetInputk1(k1);
458 }
459
460 //===== 
461 // 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)
462 //===== 
463 void VectorFilterDouble::bbUserInitializeProcessing()
464 {
465 //  THE INITIALIZATION METHOD BODY :
466 //    Here does nothing 
467 //    but this is where you should allocate the internal/output pointers 
468 //    if any
469 }
470
471 //===== 
472 // 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)
473 //===== 
474 void VectorFilterDouble::bbUserFinalizeProcessing()
475 {
476 //  THE FINALIZATION METHOD BODY :
477 //    Here does nothing 
478 //    but this is where you should desallocate the internal/output pointers 
479 //    if any
480 }
481
482 }// EO namespace bbstd
483
484