]> Creatis software - bbtk.git/blob - packages/std/src/bbstdVectorFilterDouble.cxx
Clean code
[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()-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
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() ;
173                                 (*pLstVecOut[ipLstvec]).push_back( result );                            
174                         }// for size
175                 } // for pLstVec                
176         } // Type==3
177
178         if (bbGetInputType()==4) // Substraction  k1
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() ;
190                                 (*pLstVecOut[ipLstvec]).push_back( result );                            
191                         }// for size
192                 } // for pLstVec                
193         } // Type==4
194
195         if (bbGetInputType()==5) // Multiplication  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                                 result= (*pLstVec[ipLstvec])[i] * bbGetInputk1() ;
207                                 (*pLstVecOut[ipLstvec]).push_back( result );                            
208                         }// for size
209                 } // for pLstVec                
210         } // Type==5
211
212         if (bbGetInputType()==6) // Division  k1ng gadget (node check, owner GtkCheckButton)
213
214
215         {
216                 int     ipLstvec;
217                 int     i;
218                 int     size;
219                 double  result;
220                 for (ipLstvec=0 ; ipLstvec<pLstVec.size() ; ipLstvec++)
221                 {
222                         size=(*pLstVec[ipLstvec]).size();
223                         for (i=0;i<size;i++)
224                         {
225                                 if (bbGetInputk1()!=0)
226                                 {
227                                         result= (*pLstVec[ipLstvec])[i] / bbGetInputk1() ;
228                                         (*pLstVecOut[ipLstvec]).push_back( result );                            
229                                 } else {
230                                         printf("EED VectorFilterDouble::Process  Warning!! Div by 0 in Type 6 \n");
231                                         (*pLstVecOut[ipLstvec]).push_back( -1 );                                
232                                 }
233                         }// for size
234                 } // for pLstVec                
235         } // Type==6
236
237
238
239         if (bbGetInputType()==7) 
240         {
241                 int i,j;
242                 for (j=0;j<In4.size(); j++)
243                 {
244                         Out0.push_back( In4[j] );
245                         Out1.push_back( In5[j] );
246                         Out2.push_back( In6[j] );
247                 } //
248
249
250                 int iGeneral=0,sizeI,sizeLst1Indexes=In3.size();
251                 for (i=0; i<sizeLst1Indexes-1;i++) { iGeneral=iGeneral+In3[i]; }
252                 sizeI=iGeneral+In3[ sizeLst1Indexes-1 ];
253
254                 int jGeneral = 0;
255                 int sizeJ        = In7[ 0 ];
256
257                 double dist2,dist2Min,dx,dy,dz;
258                 int iBack;
259
260                 for (j=jGeneral;j<sizeJ;j++) 
261                 {
262                         dist2Min=1000000;
263                         for (i=iGeneral;i<sizeI;i++) 
264                         {       
265                                 dx              = In0[i]-In4[j];
266                                 dy              = In1[i]-In5[j];
267                                 dz              = In2[i]-In6[j];
268                                 dist2           = dx*dx + dy*dy + dz*dz;
269                                 if (dist2<dist2Min) 
270                                 {
271                                                 dist2Min=dist2;
272                                                 iBack=i;
273                                 } // if
274                         } // for j
275                         Out0[j]=In0[iBack]; 
276                         Out1[j]=In1[iBack]; 
277                         Out2[j]=In2[iBack]; 
278                 } // for i
279
280
281         }       // Type 7
282
283
284
285         if (bbGetInputType()==8)   // order all vectors using as base the vector In0
286         {
287                 double tmp;
288                 int i,j,ii,sizeII;
289                 sizeII=pLstVec.size();
290
291
292                 int size=In0.size();
293                 for (i=0;i<size; i++)
294                 {
295                         for (ii=0;ii<sizeII;ii++)
296                         {
297                                  (*pLstVecOut[ii]).push_back( (*pLstVec[ii])[i] ); 
298                         } // for ii
299                 } // i
300                 for (i=0;i<size;i++)
301                 {
302                         for (j=i;j<size;j++)
303                         {
304                                 if ((*pLstVecOut[0])[j] <= (*pLstVecOut[0])[i] ) 
305                                 {       
306                                         
307                                         for (ii=0;ii<sizeII;ii++)
308                                         {
309                                                 tmp = (*pLstVecOut[ii])[i]; 
310                                                 (*pLstVecOut[ii])[i] = (*pLstVecOut[ii])[j]; 
311                                                 (*pLstVecOut[ii])[j] = tmp; 
312                                         } // for ii
313                                 } // if <
314                         } // for j
315                 } // for i
316         } // Type 8
317
318
319         if (bbGetInputType()==9)   // Invert vectors
320         {
321                 double tmp;
322                 int i,j,ii,sizeII;
323                 sizeII=pLstVec.size();
324
325                 int size=In0.size();
326                 int size2=size/2;
327                 for (i=0;i<size; i++)
328                 {
329                         for (ii=0;ii<sizeII;ii++)
330                         {
331                                  (*pLstVecOut[ii]).push_back( (*pLstVec[ii])[i] ); 
332                         } // for ii
333                 } // i
334
335                 for (i=0;i<size2;i++)
336                 {
337                         for (ii=0;ii<sizeII;ii++)
338                         {
339                                 j=size-1-i;     
340                                 tmp = (*pLstVecOut[ii])[i]; 
341                                 (*pLstVecOut[ii])[i] = (*pLstVecOut[ii])[ j ]; 
342                                 (*pLstVecOut[ii])[j] = tmp; 
343                         } // for ii
344                 } // for i
345         } // Type 9
346
347
348         if (bbGetInputType()==10)   // Nearest point in vector
349         {
350                 int     sizeLstX        =       In0.size();   // lstX
351                 int     sizeLstY        =       In1.size();   // lstY
352                 int     sizeLstZ        =       In2.size();   // lstZ
353                 if ( (sizeLstX==sizeLstY) && (sizeLstY==sizeLstZ) && (sizeLstX>0) && (In3.size()==3) )
354                 {
355                         int i;
356                         double x1               =       In3[0];  // pointX of pointXYZ
357                         double y1               =       In3[1];  // pointY of pointXYZ
358                         double z1               =       In3[2];  // pointZ of pointXYZ
359                         double distMin  =       100000;
360                         double dist;
361                         double x2;
362                         double y2;
363                         double z2;
364                         Out0.push_back( -1 ); 
365                         Out1.push_back( -999 ); 
366                         Out1.push_back( -999 ); 
367                         Out1.push_back( -999 ); 
368                         for (i=0;i<sizeLstX; i++)
369                         {
370                                 x2 = In0[i];    // lstX
371                                 y2 = In1[i];    // lstY
372                                 z2 = In2[i];    // lstZ
373                                 dist = sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2) );
374                                 if (dist<distMin) 
375                                 {
376                                         distMin = dist;
377                                         Out0[0] = i;
378                                         Out1[0] = x2;
379                                         Out1[1] = y2;
380                                         Out1[2] = z2;
381                                 }
382                         } // for i
383                 } else {
384                         printf("EED Warnning VectorFilterDouble::Process()   For Type 10 the size of the vectors are not coherent.\n");
385                 }// if size
386         } // Type 10
387
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