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