]> Creatis software - bbtk.git/blob - packages/std/src/bbstdVectorFilterDouble.cxx
#3348 BBTK Feature New Normal - VectorFilterDouble (find nearest point) WriteColumn...
[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                 bool okSizeVec=true;
81                 int ipLstvec; 
82                 for (ipLstvec=1;ipLstvec<pLstVec.size();ipLstvec++)
83                 {
84                         if ( (*pLstVec[ipLstvec]).size()!=(*pLstVec[0]).size() )  { okSizeVec=false; }
85                 }
86                 if ( okSizeVec==false)
87                 {
88                         printf("EED VectorFilterDouble::Process WARNING! vectors are not of the same size.\n");
89                 } else {
90                         int iLine;
91                         int ipLstvec2; 
92                         bool okLine;
93                         for (iLine=0 ; iLine < (*pLstVec[0]).size() ; iLine++ ) 
94                         {
95                                 okLine=false;
96                                 for ( ipLstvec2=0 ; ipLstvec2<pLstVec.size() ; ipLstvec2++)
97                                 {
98                                         if ( (*pLstVec[ipLstvec2])[iLine]!=(*pLstVec[ipLstvec2])[iLine-1] )  { okLine=true; }
99                                 } // for ipLstVec2
100                                                 
101                                 if ( (okLine==true) || (iLine==0) ) 
102                                 {
103                                         for (ipLstvec2=0 ; ipLstvec2<pLstVec.size() ; ipLstvec2++)
104                                         {
105                                                 (*pLstVecOut[ipLstvec2]).push_back( (*pLstVec[ipLstvec2])[iLine] );
106                                         } // for
107                                 } // if okLine
108                         } // for iLine
109                 } // if okSizeVec
110         } // Type==0
111
112
113         if (bbGetInputType()==1) // Resize vector
114         {
115                 int             ipLstvec2;
116                 double  iLine;
117                 int     sizeLines       = (*pLstVec[0]).size();
118                 double  step            = (double)sizeLines/((double)bbGetInputk1()-1);
119                 for (iLine=0 ; iLine<sizeLines ; iLine=iLine+step)
120                 {
121                         for (ipLstvec2=0 ; ipLstvec2<pLstVec.size() ; ipLstvec2++)
122                         {
123                                 (*pLstVecOut[ipLstvec2]).push_back( (*pLstVec[ipLstvec2])[(int)iLine] );
124                         } // for                        
125                 } // for ipLstVec
126
127                 // adding the lastone
128                 for (ipLstvec2=0 ; ipLstvec2<pLstVec.size() ; ipLstvec2++)
129                 {
130                         (*pLstVecOut[ipLstvec2]).push_back( (*pLstVec[ipLstvec2])[(int)(sizeLines-1)] );
131                 } // for                        
132
133         } // Type==1
134
135         if (bbGetInputType()==2) // Insert intermediate points
136         {
137                 int             ipLstvec;
138                 int     i;
139                 int     size;
140                 double  result;
141                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
142                 {
143                         size=(*pLstVec[ipLstvec]).size();
144                         if (size>=2)
145                         {
146                                 for (i=0;i<size-1;i++)
147                                 {
148                                         (*pLstVecOut[ipLstvec]).push_back( (*pLstVec[ipLstvec])[i] );   // First item                   
149                                         result=((*pLstVec[ipLstvec])[i] + (*pLstVec[ipLstvec])[i+1]  ) / 2;
150                                         (*pLstVecOut[ipLstvec]).push_back( result );                            
151                                 }// for size
152                                 (*pLstVecOut[ipLstvec]).push_back( (*pLstVec[ipLstvec])[size-1] );      // Last item            
153                         } // if size>=2
154                 } // for pLstVec                
155         } // Type==2
156
157
158         if (bbGetInputType()==3) // Addition  k1
159         {
160                 int             ipLstvec;
161                 int     i;
162                 int     size;
163                 double  result;
164                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
165                 {
166                         size=(*pLstVec[ipLstvec]).size();
167                         for (i=0;i<size;i++)
168                         {
169                                 result= (*pLstVec[ipLstvec])[i] + bbGetInputk1() ;
170                                 (*pLstVecOut[ipLstvec]).push_back( result );                            
171                         }// for size
172                 } // for pLstVec                
173         } // Type==3
174
175         if (bbGetInputType()==4) // Substraction  k1
176         {
177                 int             ipLstvec;
178                 int     i;
179                 int     size;
180                 double  result;
181                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
182                 {
183                         size=(*pLstVec[ipLstvec]).size();
184                         for (i=0;i<size;i++)
185                         {
186                                 result= (*pLstVec[ipLstvec])[i] - bbGetInputk1() ;
187                                 (*pLstVecOut[ipLstvec]).push_back( result );                            
188                         }// for size
189                 } // for pLstVec                
190         } // Type==4
191
192         if (bbGetInputType()==5) // Multiplication  k1
193         {
194                 int     ipLstvec;
195                 int     i;
196                 int     size;
197                 double  result;
198                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
199                 {
200                         size=(*pLstVec[ipLstvec]).size();
201                         for (i=0;i<size;i++)
202                         {
203                                 result= (*pLstVec[ipLstvec])[i] * bbGetInputk1() ;
204                                 (*pLstVecOut[ipLstvec]).push_back( result );                            
205                         }// for size
206                 } // for pLstVec                
207         } // Type==5
208
209         if (bbGetInputType()==6) // Division  k1ng gadget (node check, owner GtkCheckButton)
210
211
212         {
213                 int     ipLstvec;
214                 int     i;
215                 int     size;
216                 double  result;
217                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
218                 {
219                         size=(*pLstVec[ipLstvec]).size();
220                         for (i=0;i<size;i++)
221                         {
222                                 if (bbGetInputk1()!=0)
223                                 {
224                                         result= (*pLstVec[ipLstvec])[i] / bbGetInputk1() ;
225                                         (*pLstVecOut[ipLstvec]).push_back( result );                            
226                                 } else {
227                                         printf("EED VectorFilterDouble::Process  Warning!! Div by 0 in Type 6 \n");
228                                         (*pLstVecOut[ipLstvec]).push_back( -1 );                                
229                                 }
230                         }// for size
231                 } // for pLstVec                
232         } // Type==6
233
234
235
236         if (bbGetInputType()==7) 
237         {
238                 int i,j;
239                 for (j=0;j<In4.size(); j++)
240                 {
241                         Out0.push_back( In4[j] );
242                         Out1.push_back( In5[j] );
243                         Out2.push_back( In6[j] );
244                 } //
245
246
247                 int iGeneral=0,sizeI,sizeLst1Indexes=In3.size();
248                 for (i=0; i<sizeLst1Indexes-1;i++) { iGeneral=iGeneral+In3[i]; }
249                 sizeI=iGeneral+In3[ sizeLst1Indexes-1 ];
250
251                 int jGeneral = 0;
252                 int sizeJ        = In7[ 0 ];
253
254                 double dist2,dist2Min,dx,dy,dz;
255                 int iBack;
256
257                 for (j=jGeneral;j<sizeJ;j++) 
258                 {
259                         dist2Min=1000000;
260                         for (i=iGeneral;i<sizeI;i++) 
261                         {       
262                                 dx              = In0[i]-In4[j];
263                                 dy              = In1[i]-In5[j];
264                                 dz              = In2[i]-In6[j];
265                                 dist2           = dx*dx + dy*dy + dz*dz;
266                                 if (dist2<dist2Min) 
267                                 {
268                                                 dist2Min=dist2;
269                                                 iBack=i;
270                                 } // if
271                         } // for j
272                         Out0[j]=In0[iBack]; 
273                         Out1[j]=In1[iBack]; 
274                         Out2[j]=In2[iBack]; 
275                 } // for i
276
277
278         }       // Type 7
279
280
281
282         if (bbGetInputType()==8)   // order all vectors using as base the vector In0
283         {
284                 double tmp;
285                 int i,j,ii,sizeII;
286                 sizeII=pLstVec.size();
287
288
289                 int size=In0.size();
290                 for (i=0;i<size; i++)
291                 {
292                         for (ii=0;ii<sizeII;ii++)
293                         {
294                                  (*pLstVecOut[ii]).push_back( (*pLstVec[ii])[i] ); 
295                         } // for ii
296                 } // i
297                 for (i=0;i<size;i++)
298                 {
299                         for (j=i;j<size;j++)
300                         {
301                                 if ((*pLstVecOut[0])[j] <= (*pLstVecOut[0])[i] ) 
302                                 {       
303                                         
304                                         for (ii=0;ii<sizeII;ii++)
305                                         {
306                                                 tmp = (*pLstVecOut[ii])[i]; 
307                                                 (*pLstVecOut[ii])[i] = (*pLstVecOut[ii])[j]; 
308                                                 (*pLstVecOut[ii])[j] = tmp; 
309                                         } // for ii
310                                 } // if <
311                         } // for j
312                 } // for i
313         } // Type 8
314
315
316         if (bbGetInputType()==9)   // Invert vectors
317         {
318                 double tmp;
319                 int i,j,ii,sizeII;
320                 sizeII=pLstVec.size();
321
322                 int size=In0.size();
323                 int size2=size/2;
324                 for (i=0;i<size; i++)
325                 {
326                         for (ii=0;ii<sizeII;ii++)
327                         {
328                                  (*pLstVecOut[ii]).push_back( (*pLstVec[ii])[i] ); 
329                         } // for ii
330                 } // i
331
332                 for (i=0;i<size2;i++)
333                 {
334                         for (ii=0;ii<sizeII;ii++)
335                         {
336                                 j=size-1-i;     
337                                 tmp = (*pLstVecOut[ii])[i]; 
338                                 (*pLstVecOut[ii])[i] = (*pLstVecOut[ii])[ j ]; 
339                                 (*pLstVecOut[ii])[j] = tmp; 
340                         } // for ii
341                 } // for i
342         } // Type 9
343
344
345 printf("EED VectorFilterDouble::Process() start\n");
346         if (bbGetInputType()==10)   // Invert vectors
347         {
348                 int     sizeLstX        =       In0.size();   // lstX
349                 int     sizeLstY        =       In1.size();   // lstY
350                 int     sizeLstZ        =       In2.size();   // lstZ
351                 printf("EED Warnning VectorFilterDouble::Process() %d  %d  %d  %d\n", sizeLstX,sizeLstY,sizeLstZ,In3.size() );
352                 if ( (sizeLstX==sizeLstY) && (sizeLstY==sizeLstZ) && (sizeLstX>0) && (In3.size()==3) )
353                 {
354                         int i;
355                         double x1               =       In3[0];  // pointX of pointXYZ
356                         double y1               =       In3[1];  // pointY of pointXYZ
357                         double z1               =       In3[2];  // pointZ of pointXYZ
358                         double distMin  =       100000;
359                         double dist;
360                         double x2;
361                         double y2;
362                         double z2;
363                         Out0.push_back( -1 ); 
364                         Out1.push_back( -999 ); 
365                         Out1.push_back( -999 ); 
366                         Out1.push_back( -999 ); 
367                         for (i=0;i<sizeLstX; i++)
368                         {
369                                 x2 = In0[i];    // lstX
370                                 y2 = In1[i];    // lstY
371                                 z2 = In2[i];    // lstZ
372                                 dist = sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2) );
373                                 if (dist<distMin) 
374                                 {
375                                         distMin = dist;
376                                         Out0[0] = i;
377                                         Out1[0] = x2;
378                                         Out1[1] = y2;
379                                         Out1[2] = z2;
380                                 }
381                         } // for i
382                 } else {
383                         printf("EED Warnning VectorFilterDouble::Process()   For Type 10 the size of the vectors are not coherent.\n");
384                 }// if size
385         } // Type 10
386
387 printf("EED VectorFilterDouble::Process() end\n");
388
389
390         bbSetOutputOut0( Out0 );
391         bbSetOutputOut1( Out1 );
392         bbSetOutputOut2( Out2 );
393         bbSetOutputOut3( Out3 );
394         bbSetOutputOut4( Out4 );
395         bbSetOutputOut5( Out5 );
396         bbSetOutputOut6( Out6 );
397         bbSetOutputOut7( Out7 );
398         bbSetOutputOut8( Out8 );
399         bbSetOutputOut9( Out9 );
400 }
401 //===== 
402 // 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)
403 //===== 
404 void VectorFilterDouble::bbUserSetDefaultValues()
405 {
406
407 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
408 //    Here we initialize the input 'In' to 0
409    bbSetInputType(0);
410    bbSetInputk1(0);
411   
412 }
413 //===== 
414 // 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)
415 //===== 
416 void VectorFilterDouble::bbUserInitializeProcessing()
417 {
418
419 //  THE INITIALIZATION METHOD BODY :
420 //    Here does nothing 
421 //    but this is where you should allocate the internal/output pointers 
422 //    if any 
423
424   
425 }
426 //===== 
427 // 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)
428 //===== 
429 void VectorFilterDouble::bbUserFinalizeProcessing()
430 {
431
432 //  THE FINALIZATION METHOD BODY :
433 //    Here does nothing 
434 //    but this is where you should desallocate the internal/output pointers 
435 //    if any
436   
437 }
438 }
439 // EO namespace bbstd
440
441