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