]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuManualContourModel_Box.cxx
9623d4c42448e9bfb84a9c395bc688b142dd0d91
[creaMaracasVisu.git] / bbtk / src / bbcreaMaracasVisuManualContourModel_Box.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 "bbcreaMaracasVisuManualContourModel_Box.h"
5 #include "bbcreaMaracasVisuPackage.h"
6
7 #include <creaContoursFactory.h>
8
9
10 namespace bbcreaMaracasVisu
11 {
12
13 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ManualContourModel_Box)
14 BBTK_BLACK_BOX_IMPLEMENTATION(ManualContourModel_Box,bbtk::AtomicBlackBox);
15 //===== 
16 // 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)
17 //===== 
18
19
20 void ManualContourModel_Box::ProcessBySegment(  
21                         int Type, 
22                         int &iGeneral, int sizeSegment,
23                         std::vector<double> *lstInX,std::vector<double> *lstInY, std::vector<double> *lstInZ,
24                         std::vector<double> *lstOutX,std::vector<double> *lstOutY, std::vector<double> *lstOutZ,
25                         std::vector<int>        *lstIndexsOut, bool open )
26 {
27         creaContoursFactory f;
28         manualContourModel      *m;
29         int i,size=iGeneral+sizeSegment;
30         double x,y,z;
31         m = (manualContourModel*)f.getContourModel( bbGetInputType() );
32         m->SetNumberOfPointsSpline( bbGetInputNbPoints() );
33         m->SetCloseContour( open );
34         for (i=iGeneral;i<size;i++)
35         {
36                 m->AddPoint( (*lstInX)[i] , (*lstInY)[i] , (*lstInZ)[i] );
37         } // for
38         m->UpdateSpline();
39         int sizeContour = bbGetInputNbPoints();
40         for (i=0;i<sizeContour;i++)
41         {
42                 m->GetSpline_i_Point(i,&x,&y,&z);
43                 lstOutX->push_back(x);
44                 lstOutY->push_back(y);
45                 lstOutZ->push_back(z);
46         } // for
47         iGeneral=iGeneral+sizeSegment;
48         lstIndexsOut->push_back( sizeContour );
49         delete m;
50 }
51
52
53 void ManualContourModel_Box::RedistributionPoints(      std::vector<double> *lstOutX,
54                                                                                                         std::vector<double> *lstOutY, 
55                                                                                                         std::vector<double> *lstOutZ,
56                                                                                                         std::vector<int> *lstIndexsOut )
57 {
58         std::vector<double> lstRstX;
59         std::vector<double> lstRstY;
60         std::vector<double> lstRstZ;
61         int iLstIndexOut,sizeLstIndexOut = lstIndexsOut->size();
62         int ii, iGeneral=0;
63         int size;
64         for (iLstIndexOut=0;  iLstIndexOut<sizeLstIndexOut; iLstIndexOut++)
65         {
66                 lstRstX.clear();
67                 lstRstY.clear();
68                 lstRstZ.clear();
69                 size=(*lstIndexsOut)[iLstIndexOut];
70                 if (size>2)
71                 {
72                         double dist=0,dist2,distSeg,delta;
73                         double dd,dx,dy,dz;
74                         int i,k;
75                         for ( i=iGeneral+1 ; i<iGeneral+size ; i++ )
76                         {
77                                 dx      = (*lstOutX)[i]-(*lstOutX)[i-1];
78                                 dy      = (*lstOutY)[i]-(*lstOutY)[i-1];
79                                 dz      = (*lstOutZ)[i]-(*lstOutZ)[i-1];
80                                 dist= dist+sqrt( dx*dx + dy*dy + dz*dz );
81                         } //for
82                         delta = dist/(size-1);
83                         lstRstX.push_back( (*lstOutX)[iGeneral] );
84                         lstRstY.push_back( (*lstOutY)[iGeneral] );
85                         lstRstZ.push_back( (*lstOutZ)[iGeneral] );
86                         for (i=iGeneral+1; i<iGeneral+size;i++)
87                         {
88                                 dist2 = 0;
89                                 for (k=iGeneral+1; k<iGeneral+size;k++)
90                                 {
91                                         dx = (*lstOutX)[k]-(*lstOutX)[k-1];
92                                         dy = (*lstOutY)[k]-(*lstOutY)[k-1];
93                                         dz = (*lstOutZ)[k]-(*lstOutZ)[k-1];
94                                         distSeg = sqrt( dx*dx + dy*dy + dz*dz );
95                                         ii = i-iGeneral;
96                                         if ( dist2+distSeg >= ii*delta )
97                                         {
98                                                 dd=(ii*delta-dist2)/distSeg;
99                                                 if (distSeg==0)
100                                                 {
101                                                         dd = 0;
102                                                 } // if distSeg == 0
103                                                 lstRstX.push_back( (*lstOutX)[k-1] + dd*dx );
104                                                 lstRstY.push_back( (*lstOutY)[k-1] + dd*dy );
105                                                 lstRstZ.push_back( (*lstOutZ)[k-1] + dd*dz );
106                                                 k=iGeneral+size;
107                                         } else {
108                                                 if ( k==iGeneral+size-1 )
109                                                 {
110                                                         dd = 1;
111                                                         lstRstX.push_back( (*lstOutX)[k-1] + dd*dx );
112                                                         lstRstY.push_back( (*lstOutY)[k-1] + dd*dy );
113                                                         lstRstZ.push_back( (*lstOutZ)[k-1] + dd*dz );
114                                                 }
115                                         }// if dist2 
116                                         dist2 = dist2+distSeg;
117                                 } // for k
118                         } //for i   
119                         if (lstRstX.size()!=size) 
120                         {
121                                 printf("EED Warnning!   ManualContourModel_Box::RedistributionPoints  >> This list is not coherent  iLstIndexOut=%d  lstRstX.size()=%d  size=%d\n",iLstIndexOut, lstRstX.size(), size);
122                         }
123                         for (i=iGeneral; i<iGeneral+size;i++)
124                         {
125                                 ii=i-iGeneral;
126                                 (*lstOutX)[i] = lstRstX[ii];
127                                 (*lstOutY)[i] = lstRstY[ii];
128                                 (*lstOutZ)[i] = lstRstZ[ii];
129                         } // for i
130                 } // if size>2
131                 iGeneral=iGeneral+size;
132         }// for iLstIndexOut
133 }
134
135
136 //-------------------------------------------------------------------------------------------------------------
137
138
139 void ManualContourModel_Box::ExtractContour(    std::vector<double> *lstX,
140                                                                                                 std::vector<double> *lstY, 
141                                                                                                 std::vector<double> *lstZ,
142                                                                                                 std::vector<int>        *lstIndexs,
143                                                                                                 int contour,
144                                                                                                 std::vector<double> *lstOutX,
145                                                                                                 std::vector<double> *lstOutY, 
146                                                                                                 std::vector<double> *lstOutZ )
147 {
148         int     i;
149         int     iLstIndex;
150         int     iContour;
151         int     sizeLstIndexslstIndexs;
152         int     iGeneral;
153         int     iGeneralPlusSize;
154         if ( (lstX!=NULL) &&  (lstY!=NULL) && (lstZ!=NULL) && (lstIndexs!=NULL) && (lstOutX!=NULL) && (lstOutY!=NULL) && (lstOutZ!=NULL) )
155         {
156                 sizeLstIndexslstIndexs  = (*lstIndexs).size();
157                 if  ( sizeLstIndexslstIndexs!=0 ) 
158                 {
159                         (*lstOutX).clear();
160                         (*lstOutY).clear();
161                         (*lstOutZ).clear();
162                         iGeneral                                = 0;
163                         iGeneralPlusSize                = (*lstIndexs)[0];
164                         for ( iContour=1 ; iContour<=contour ; iContour++ )
165                         {
166                                 iGeneral                        = iGeneral+(*lstIndexs)[iContour-1];
167                                 iGeneralPlusSize        = iGeneral+(*lstIndexs)[iContour];
168                         } // for iContour
169                         for ( i=iGeneral ; i<iGeneralPlusSize ; i++ )
170                         {
171                                 (*lstOutX).push_back( (*lstX)[i] );
172                                 (*lstOutY).push_back( (*lstY)[i] );
173                                 (*lstOutZ).push_back( (*lstZ)[i] );                             
174                         } //for
175                 } // size
176         } // NULL
177 }
178
179 void ManualContourModel_Box::PutPointsInContour(std::vector<double> *lstTmpX,
180                                                                                                 std::vector<double> *lstTmpY, 
181                                                                                                 std::vector<double> *lstTmpZ,
182                                                                                                 int contour,
183                                                                                                 std::vector<double> *lstOutX,
184                                                                                                 std::vector<double> *lstOutY, 
185                                                                                                 std::vector<double> *lstOutZ,
186                                                                                                 std::vector<int>        *lstOutIndexs )
187 {
188         int     i;
189         int     iLstIndex;
190         int     iContour;
191         int     sizeLstIndexslstIndexs;
192         int     iGeneral;
193         int     iGeneralPlusSize;
194         int     iSize;
195         int     SizeContour;
196         if ( (lstTmpX!=NULL) &&  (lstTmpY!=NULL) && (lstTmpZ!=NULL)  && (lstOutX!=NULL) && (lstOutY!=NULL) && (lstOutZ!=NULL) && (lstOutIndexs!=NULL) )
197         {
198                 sizeLstIndexslstIndexs  = (*lstOutIndexs).size();
199                 if  ( sizeLstIndexslstIndexs!=0 ) 
200                 {
201                         iGeneral                                = 0;
202                         SizeContour                             = (*lstOutIndexs)[0];
203                         iGeneralPlusSize                = iGeneral + SizeContour;
204                         for ( iContour=1 ; iContour<=contour ; iContour++ )
205                         {
206                                 iGeneral                        = iGeneral + (*lstOutIndexs)[iContour-1];
207                                 SizeContour             = (*lstOutIndexs)[iContour];
208                                 iGeneralPlusSize        = iGeneral + SizeContour;
209                         } // for iContour
210                         
211                         if(SizeContour==(*lstTmpX).size() )
212                         {
213                                 int iSize=0;
214                                 for ( i=iGeneral ; i<iGeneralPlusSize ; i++ )
215                                 {
216                                         (*lstOutX)[i] = (*lstTmpX)[iSize] ;
217                                         (*lstOutY)[i] = (*lstTmpY)[iSize] ;
218                                         (*lstOutZ)[i] = (*lstTmpZ)[iSize] ;
219                                         iSize++;                                        
220                                 } //for                 
221                         } else {
222                                 printf("EED Warnning!! ManualContourModel_Box::PutPointsInContour  the lstTmp vector is not of the correct size. SizeContour=%d  lstTmp.size=%d\n"
223                                                                                                                                                                                                                 ,SizeContour, (*lstTmpX).size() );
224                                 for ( i=iGeneral ; i<iGeneralPlusSize ; i++ )
225                                 {
226                                         (*lstOutX)[i] = -9999 ;
227                                         (*lstOutY)[i] = -9999 ;
228                                         (*lstOutZ)[i] = -9999 ;
229                                 } //for                 
230                         }
231                 } // size
232         } // NULL
233 }
234
235 double ManualContourModel_Box::SizeContour(     std::vector<double> *lstX,
236                                                                                         std::vector<double> *lstY, 
237                                                                                         std::vector<double> *lstZ)
238 {
239         int     i;
240         double  dx;
241         double  dy;
242         double  dz;
243         double  dist            = 0;
244         int     iSize           = (*lstX).size()  - 1;
245         for ( i=0 ; i<iSize ; i++ )
246         {
247                 dx      = (*lstX)[i+1]-(*lstX)[i];
248                 dy      = (*lstY)[i+1]-(*lstY)[i];
249                 dz      = (*lstZ)[i+1]-(*lstZ)[i];
250                 dist= dist+sqrt( dx*dx + dy*dy + dz*dz );
251         } //for i
252         return dist;
253 }
254
255
256 void ManualContourModel_Box::Redistribution_SIN(        double alpha,
257                                                                                                         double beta,
258                                                                                                         std::vector<double> *lstC1X,
259                                                                                                         std::vector<double> *lstC1Y, 
260                                                                                                         std::vector<double> *lstC1Z,
261                                                                                                         double sizeOfContour,
262                                                                                                         std::vector<double> *lstC2X,
263                                                                                                         std::vector<double> *lstC2Y, 
264                                                                                                         std::vector<double> *lstC2Z )
265 {
266         std::vector<double> lstRstX;
267         std::vector<double> lstRstY;
268         std::vector<double> lstRstZ;
269         int     ii,iGeneral;
270         int     size,iGeneralPlusSize;
271         int     i,k;
272         int     firstK;
273         double  iiByDelta;
274         double  dist2,distSeg;
275 //      double  delta;
276         double  dd,dx,dy,dz;
277         double  t,tt, PI;
278         double  TwoPI;
279         double  dist;
280         dist            = sizeOfContour;
281         PI                      = 3.14159265;
282         TwoPI           = 2*PI;
283         iGeneral        = 0;
284 //      for (iLstIndexOut=0;  iLstIndexOut<sizeLstIndexOut; iLstIndexOut++)  // For each contour
285 //      {
286 //              lstRstX.clear();
287 //              lstRstY.clear();
288 //              lstRstZ.clear();
289 //              size                            = (*lstIndexsOut)[iLstIndexOut];
290                 size = (*lstC1X).size();
291                 iGeneralPlusSize        = iGeneral+size;
292                 if (size>2)
293                 {
294 //                      dist=0;
295 //                      for ( i=iGeneral ; i<iGeneralPlusSize-1 ; i++ )
296 //                      {
297 //                              dx      = (*lstOutX)[i]-(*lstOutX)[i+1];
298 //                              dy      = (*lstOutY)[i]-(*lstOutY)[i+1];
299 //                              dz      = (*lstOutZ)[i]-(*lstOutZ)[i+1];
300 //                              dist= dist+sqrt( dx*dx + dy*dy + dz*dz );
301 //                      } //for
302
303
304 //                      delta = dist/(size-1);
305                         firstK  = 0;                    
306                         for (i=iGeneral; i<iGeneralPlusSize;i++)                                //  For each point of one contour
307                         {
308                                 ii = i-iGeneral;
309                                 dist2   = 0;
310                                 for (k=iGeneral; k<iGeneralPlusSize;k++)                        // Search inside
311                                 {
312 //if ( 385==(*lstOutY)[k]  )
313 //{ 
314         t               =       ((double)ii) / ((double)(size-1));
315 //      Beta    = 0.20; //  [0..1]
316 //      Alpha   = 0.0;  // [0..1]
317 //      Beta    = bbGetInputParam()[0];
318 //      Alpha   = bbGetInputParam()[1];
319         tt              = t + 0.70710678182*sin(t*TwoPI)*beta + alpha;
320         if (tt>1) { tt=tt-1; }
321         if (tt<0) { tt=tt+1; }
322         iiByDelta = tt * dist;
323 //} else {
324 //  iiByDelta = ii*delta;
325 //}
326
327 //                                      dx = (*lstOutX)[k+1]-(*lstOutX)[k];
328 //                                      dy = (*lstOutY)[k+1]-(*lstOutY)[k];
329 //                                      dz = (*lstOutZ)[k+1]-(*lstOutZ)[k];
330                                         dx = (*lstC1X)[k+1]-(*lstC1X)[k];
331                                         dy = (*lstC1Y)[k+1]-(*lstC1Y)[k];
332                                         dz = (*lstC1Z)[k+1]-(*lstC1Z)[k];
333                                         distSeg = sqrt( dx*dx + dy*dy + dz*dz );
334                                         if ( dist2+distSeg >= iiByDelta )
335                                         {
336                                                 if (distSeg==0)
337                                                 {
338                                                         dd = 0;
339                                                 } else {
340                                                         dd=(iiByDelta-dist2)/distSeg;
341                                                 }// if distSeg == 0
342                                                 lstRstX.push_back( (*lstC1X)[k] + dd*dx );
343                                                 lstRstY.push_back( (*lstC1Y)[k] + dd*dy );
344                                                 lstRstZ.push_back( (*lstC1Z)[k] + dd*dz );
345                                                 if (ii==0) { firstK=k; }
346                                                 k = iGeneralPlusSize-1;
347                                         } else {
348                                                 if ( k==iGeneral+size-2 )
349                                                 {
350                                                         dd = 1;
351                                                         lstRstX.push_back( (*lstC1X)[k] + dd*dx );
352                                                         lstRstY.push_back( (*lstC1Y)[k] + dd*dy );
353                                                         lstRstZ.push_back( (*lstC1Z)[k] + dd*dz );
354                                                 }
355                                         }// if dist2 
356                                         dist2 = dist2 + distSeg;
357                                 } // for k
358                         } //for i                       
359                         if (lstRstX.size()!=size) 
360                         {
361                                 printf("EED Warnning!   ManualContourModel_Box::Redistribution_SIN  >> This list is not coherent lstRstX.size()=%d  size=%d\n", lstRstX.size(), size);
362                         }
363                         (*lstC2X).clear();
364                         (*lstC2Y).clear();
365                         (*lstC2Z).clear();
366                         for (i=iGeneral; i<iGeneralPlusSize;i++)
367                         {
368                                 (*lstC2X).push_back(-1);        
369                                 (*lstC2Y).push_back(-1);        
370                                 (*lstC2Z).push_back(-1);        
371                         } // for i
372                         int iii;
373                         for (i=iGeneral; i<iGeneralPlusSize;i++)
374                         {
375                                 ii                              = i-iGeneral;
376                                 iii                             = iGeneral+ ( (i-iGeneral) + firstK) % size ;
377 //                              (*lstOutX)[iii] = lstRstX[ii];
378 //                              (*lstOutY)[iii] = lstRstY[ii];
379 //                              (*lstOutZ)[iii] = lstRstZ[ii];
380                                 (*lstC2X)[iii]  = lstRstX[ii];
381                                 (*lstC2Y)[iii]  = lstRstY[ii];
382                                 (*lstC2Z)[iii]  = lstRstZ[ii];
383                         } // for i
384                 } else {
385                         for (i=0; i<size ; i++)
386                         {
387                                 (*lstC2X)[i] = (*lstC1X)[i];
388                                 (*lstC2Y)[i] = (*lstC1Y)[i];
389                                 (*lstC2Z)[i] = (*lstC1Z)[i];
390                         } // for i
391                 }// if size>2
392 //              iGeneral=iGeneral+size;
393 //      }// for iLstIndexOut
394 }
395
396 void ManualContourModel_Box::CopyContour2InContour1(
397                                                                                                         std::vector<double> *lstInX,
398                                                                                                         std::vector<double> *lstInY, 
399                                                                                                         std::vector<double> *lstInZ,
400                                                                                                         std::vector<double> *lstOutX,
401                                                                                                         std::vector<double> *lstOutY, 
402                                                                                                         std::vector<double> *lstOutZ )
403 {
404         int i,sizeLstInX=(*lstInX).size();
405         (*lstOutX).clear();     
406         (*lstOutY).clear();     
407         (*lstOutZ).clear();     
408         for ( i=0 ; i<sizeLstInX ; i++ )
409         {
410                 (*lstOutX).push_back( (*lstInX)[i] );
411                 (*lstOutY).push_back( (*lstInY)[i] );
412                 (*lstOutZ).push_back( (*lstInZ)[i] );
413         } // for i
414 }
415
416 double ManualContourModel_Box::IntegralDistanceTwoContours(std::vector<double> *lstTmpAX,
417                                                                                                                         std::vector<double> *lstTmpAY, 
418                                                                                                                         std::vector<double> *lstTmpAZ,
419                                                                                                                         std::vector<double> *lstTmpBX,
420                                                                                                                         std::vector<double> *lstTmpBY, 
421                                                                                                                         std::vector<double> *lstTmpBZ )
422 {
423         int     i;
424         double  dx;
425         double  dy;
426         double  dz;
427         double  dist            = 0;
428         int     iSize           = (*lstTmpAX).size();
429         for ( i=0 ; i<iSize ; i++ )
430         {
431                 dx              = (*lstTmpAX)[i]-(*lstTmpBX)[i];
432                 dy              = (*lstTmpAY)[i]-(*lstTmpBY)[i];
433                 dz              = (*lstTmpAZ)[i]-(*lstTmpBZ)[i];
434                 dist    = dist + sqrt( dx*dx + dy*dy + dz*dz );
435         } //for i
436         return dist;
437 }
438
439
440 void ManualContourModel_Box::RedistributionPointsAllContours_SIN(       std::vector<double> *lstOutX,
441                                                                                                                                         std::vector<double> *lstOutY, 
442                                                                                                                                         std::vector<double> *lstOutZ,
443                                                                                                                                         std::vector<int>        *lstIndexsOut )
444 {
445         std::vector<double> lstTmp1X;
446         std::vector<double> lstTmp1Y;
447         std::vector<double> lstTmp1Z;
448         std::vector<double> lstTmp2X;
449         std::vector<double> lstTmp2Y;
450         std::vector<double> lstTmp2Z;
451         std::vector<double> lstTmp2aX;
452         std::vector<double> lstTmp2aY;
453         std::vector<double> lstTmp2aZ;
454         std::vector<double> lstTmp2bX;
455         std::vector<double> lstTmp2bY;
456         std::vector<double> lstTmp2bZ;
457         std::vector<double> lstTmp2cX;
458         std::vector<double> lstTmp2cY;
459         std::vector<double> lstTmp2cZ;
460         std::vector<double> lstTmp3X;
461         std::vector<double> lstTmp3Y;
462         std::vector<double> lstTmp3Z;
463         std::vector<int>        lstContourExeption;
464         int     iContour;
465         double  nbContours              = (*lstIndexsOut).size();
466         double  alpha,iAlpha;
467         double  beta,iBeta;
468         double  sizeContour1;
469         double  sizeContour2;
470         double  sizeContour3;
471         double  distAcum;
472         double minDistAcum;
473         
474         // ------------ Wave 1 -----------------
475         
476         ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut,0,&lstTmp1X,&lstTmp1Y,&lstTmp1Z);
477         sizeContour1    = SizeContour( &lstTmp1X, &lstTmp1Y, &lstTmp1Z );
478         
479 // Increment    
480         for ( iContour=0; iContour<nbContours-1 ; iContour++ )
481         {
482                 beta    = 0;
483                 alpha   = 0;
484 // find Alpha
485                 ExtractContour( lstOutX,lstOutY,lstOutZ,lstIndexsOut, iContour+1 ,&lstTmp2X,&lstTmp2Y,&lstTmp2Z );              
486                 sizeContour2    = SizeContour( &lstTmp2X, &lstTmp2Y, &lstTmp2Z );
487
488                 if (iContour+2<nbContours)
489                 {
490                         ExtractContour( lstOutX,lstOutY,lstOutZ,lstIndexsOut, iContour+2 ,&lstTmp3X,&lstTmp3Y,&lstTmp3Z );              
491                         sizeContour3    = SizeContour( &lstTmp3X, &lstTmp3Y, &lstTmp3Z );
492                 } else {
493                         sizeContour3=-1;
494                 }
495                 
496                 if ( (sizeContour1>sizeContour2) && (sizeContour2>sizeContour3) )
497                 {
498                         beta    = 0.05;
499                         alpha   = 0;
500                         minDistAcum = 999999999;
501                         for (iAlpha=0 ; iAlpha<1; iAlpha=iAlpha+0.1 ) 
502                         {
503                                 Redistribution_SIN( iAlpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
504                                 distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
505                                 if (distAcum<minDistAcum) 
506                                 {
507                                         alpha           = iAlpha;
508                                         minDistAcum     = distAcum;
509                                 } // if integerDist
510                         } // for alpha
511 // find Beta
512                         minDistAcum = 999999999;
513                         for (iBeta=0.0 ; iBeta<0.2; iBeta=iBeta+0.01) 
514                         {
515                                 Redistribution_SIN( alpha,iBeta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
516                                 distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
517                                 if (distAcum<minDistAcum) 
518                                 {
519                                         beta            = iBeta;
520                                         minDistAcum     = distAcum;
521                                 } // if integerDist
522
523                         } // for beta
524                 } //if sizeContour1>sizeContour2>sizeContour3
525                  else  {
526                         if ( (sizeContour1>sizeContour2) && (sizeContour2<sizeContour3) )
527                         {
528                                 lstContourExeption.push_back( iContour+1 );
529                         } // 
530                  }               
531 // Set Alpha y Beta
532                 Redistribution_SIN( alpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
533
534                 PutPointsInContour(&lstTmp2aX,&lstTmp2aY,&lstTmp2aZ, iContour+1 ,lstOutX,lstOutY,lstOutZ,lstIndexsOut);                         
535                 sizeContour1 = sizeContour2;
536                 CopyContour2InContour1( &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ , &lstTmp1X,&lstTmp1Y,&lstTmp1Z );
537         
538         } // for iContour 
539         
540         
541         
542         
543         
544         // ------------ Wave 2 -----------------
545         
546         ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut,nbContours-1,&lstTmp1X,&lstTmp1Y,&lstTmp1Z);
547         sizeContour1    = SizeContour( &lstTmp1X, &lstTmp1Y, &lstTmp1Z );
548         
549 // Increment    
550         for ( iContour=nbContours-1; iContour>=0 ; iContour-- )
551         {
552                 beta    = 0;
553                 alpha   = 0;
554 // find Alpha
555                 ExtractContour( lstOutX,lstOutY,lstOutZ,lstIndexsOut, iContour-1 ,&lstTmp2X,&lstTmp2Y,&lstTmp2Z );              
556                 sizeContour2    = SizeContour( &lstTmp2X, &lstTmp2Y, &lstTmp2Z );
557                 
558                 
559                 if (iContour-2<=0)
560                 {
561                         ExtractContour( lstOutX,lstOutY,lstOutZ,lstIndexsOut, iContour-2 ,&lstTmp3X,&lstTmp3Y,&lstTmp3Z );              
562                         sizeContour3    = SizeContour( &lstTmp3X, &lstTmp3Y, &lstTmp3Z );
563                 } else {
564                         sizeContour3=-1;
565                 }
566                 
567                 if ( (sizeContour1>sizeContour2) && (sizeContour2>sizeContour3) ){
568                         beta    = 0.05;
569                         alpha   = 0;
570                         minDistAcum = 999999999;
571                         for (iAlpha=0 ; iAlpha<1; iAlpha=iAlpha+0.1 ) 
572                         {
573                                 Redistribution_SIN( iAlpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
574                                 distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
575                                 if (distAcum<minDistAcum) 
576                                 {
577                                         alpha           = iAlpha;
578                                         minDistAcum     = distAcum;
579                                 } // if integerDist
580                         } // for alpha
581 // find Beta
582                         minDistAcum = 999999999;
583                         for (iBeta=0.0 ; iBeta<0.2; iBeta=iBeta+0.01) 
584                         {
585                                 Redistribution_SIN( alpha,iBeta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
586                                 distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
587                                 if (distAcum<minDistAcum) 
588                                 {
589                                         beta            = iBeta;
590                                         minDistAcum     = distAcum;
591                                 } // if integerDist
592
593                         } // for beta
594                 } //if  sizeContour1>sizeContour2>sizeContour3
595 // Set Alpha y Beta
596
597
598                 Redistribution_SIN( alpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
599
600                 PutPointsInContour(&lstTmp2aX,&lstTmp2aY,&lstTmp2aZ, iContour-1 ,lstOutX,lstOutY,lstOutZ,lstIndexsOut);                         
601                 sizeContour1 = sizeContour2;
602                 CopyContour2InContour1( &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ , &lstTmp1X,&lstTmp1Y,&lstTmp1Z );
603         
604         } // for iContour 
605         
606         
607         
608         // ------------ Wave 3 -----------------
609         
610         double alpha1,alpha2;
611         double beta1,beta2;
612         double iExtra,sizeExtra=lstContourExeption.size();
613         for ( iExtra=0 ; iExtra<sizeExtra ; iExtra++ )
614         {
615          
616                 ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut, lstContourExeption[iExtra]-1       , &lstTmp1X,&lstTmp1Y,&lstTmp1Z);
617                 sizeContour1    = SizeContour( &lstTmp1X, &lstTmp1Y, &lstTmp1Z );
618                 ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut, lstContourExeption[iExtra]         , &lstTmp2X,&lstTmp2Y,&lstTmp2Z);
619                 sizeContour2    = SizeContour( &lstTmp2X, &lstTmp2Y, &lstTmp2Z );
620                 ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut, lstContourExeption[iExtra]+1       , &lstTmp3X,&lstTmp3Y,&lstTmp3Z);
621                 sizeContour3    = SizeContour( &lstTmp3X, &lstTmp3Y, &lstTmp3Z );
622          
623                 printf("EED Warnning!  ManualContourModel_Box::RedistributionPointsAllContours_SIN  wave3 posible inconsistent contour y=%f\n", lstTmp2Y[0] );
624          
625 // find Alpha
626                 beta1   = 0.05;
627                 alpha1  = 0;
628                 minDistAcum = 999999999;
629                 for (iAlpha=0 ; iAlpha<1; iAlpha=iAlpha+0.1 ) 
630                 {
631                         Redistribution_SIN( iAlpha,beta1, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
632                         distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
633                         if (distAcum<minDistAcum) 
634                         {
635                                 alpha1          = iAlpha;
636                                 minDistAcum     = distAcum;
637                         } // if integerDist
638                 } // for alpha
639 // find Beta
640                 minDistAcum = 999999999;
641                 for (iBeta=0.0 ; iBeta<0.2; iBeta=iBeta+0.01) 
642                 {
643                         Redistribution_SIN( alpha1,iBeta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
644                         distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
645                         if (distAcum<minDistAcum) 
646                         {
647                                 beta1           = iBeta;
648                                 minDistAcum     = distAcum;
649                         } // if integerDist
650                 } // for beta    
651
652  
653 // find Alpha
654                 beta2   = 0.05;
655                 alpha2  = 0;
656                 minDistAcum = 999999999;
657                 for (iAlpha=0 ; iAlpha<1; iAlpha=iAlpha+0.1 ) 
658                 {
659                         Redistribution_SIN( iAlpha,beta2, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
660                         distAcum = IntegralDistanceTwoContours( &lstTmp3X,&lstTmp3Y,&lstTmp3Z , &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
661                         if (distAcum<minDistAcum) 
662                         {
663                                 alpha2          = iAlpha;
664                                 minDistAcum     = distAcum;
665                         } // if integerDist
666                 } // for alpha
667 // find Beta
668                 minDistAcum = 999999999;
669                 for (iBeta=0.0 ; iBeta<0.2; iBeta=iBeta+0.01) 
670                 {
671                         Redistribution_SIN( alpha2,iBeta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
672                         distAcum = IntegralDistanceTwoContours( &lstTmp3X,&lstTmp3Y,&lstTmp3Z , &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
673                         if (distAcum<minDistAcum) 
674                         {
675                                 beta2           = iBeta;
676                                 minDistAcum     = distAcum;
677                         } // if integerDist
678                 } // for beta    
679
680                 if (beta2>beta1)
681                 {
682                         alpha   = alpha2;
683                 } else {
684                         alpha   = alpha1;
685                 }
686                 beta    = ( beta1 + beta2 ) / 2;
687                 Redistribution_SIN( alpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
688                 PutPointsInContour(&lstTmp2bX,&lstTmp2bY,&lstTmp2bZ,  lstContourExeption[iExtra]  ,lstOutX,lstOutY,lstOutZ,lstIndexsOut);
689
690 //              Redistribution_SIN( alpha1,beta1, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
691 //              Redistribution_SIN( alpha2,beta2, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
692 //              Mixing2DistributionVectors(&lstTmp2aX,&lstTmp2aY,&lstTmp2aZ,&lstTmp2bX,&lstTmp2bY,&lstTmp2bZ, &lstTmp2cX,&lstTmp2cY,&lstTmp2cZ );
693 //              PutPointsInContour(&lstTmp2cX,&lstTmp2cY,&lstTmp2cZ,  lstContourExeption[iExtra]  ,lstOutX,lstOutY,lstOutZ,lstIndexsOut);                               
694
695
696         } // for iExtra
697         
698 }
699  
700  
701  
702 void ManualContourModel_Box::Mixing2DistributionVectors(        std::vector<double> *lstAX,
703                                                                                                                         std::vector<double> *lstAY, 
704                                                                                                                         std::vector<double> *lstAZ,
705                                                                                                                         std::vector<double> *lstBX,
706                                                                                                                         std::vector<double> *lstBY, 
707                                                                                                                         std::vector<double> *lstBZ,
708                                                                                                                         std::vector<double> *lstOutX,
709                                                                                                                         std::vector<double> *lstOutY, 
710                                                                                                                         std::vector<double> *lstOutZ)
711 {
712         (*lstOutX).clear();
713         (*lstOutY).clear();
714         (*lstOutZ).clear();
715         
716         std::vector<double> lstTmpX;
717         std::vector<double> lstTmpY;
718         std::vector<double> lstTmpZ;
719         double  dx,dy,dz;
720         double  distA,distAT;
721         double  distB,distBT;
722         int     i,j,size;
723         int     ii,sizeTmpX;
724         bool    flagA,flagB;
725
726         distAT  = 0;
727         distBT  = 0;
728         i               = 1;
729         j               = 1;
730         size    = (*lstAX).size();      
731         
732         lstTmpX.push_back( (*lstAX)[0] );
733         lstTmpY.push_back( (*lstAY)[0] );
734         lstTmpZ.push_back( (*lstAZ)[0] );
735         lstTmpX.push_back( (*lstBX)[0] );
736         lstTmpY.push_back( (*lstBY)[0] );
737         lstTmpZ.push_back( (*lstBZ)[0] );
738         
739         while ((i<size )&& (j<size))
740         {
741                 dx              = (*lstAX)[i]-(*lstAX)[i-1];
742                 dy              = (*lstAY)[i]-(*lstAY)[i-1];
743                 dz              = (*lstAZ)[i]-(*lstAZ)[i-1];
744                 distA   = sqrt( dx*dx + dy*dy +dz*dz );
745                 dx              = (*lstBX)[j]-(*lstBX)[j-1];
746                 dy              = (*lstBY)[j]-(*lstBY)[j-1];
747                 dz              = (*lstBZ)[j]-(*lstBZ)[j-1];
748                 distB   = sqrt( dx*dx + dy*dy +dz*dz );
749                 flagA   = false;
750                 flagB   = false;
751                 if ((distAT+distA)<(distBT+distB))
752                 {
753                         flagA=true;
754                 } else if ((distAT+distA)>(distBT+distB))
755                 {
756                         flagB=true;
757                 } else {
758                         flagA=true;
759                         flagB=true;
760                 } // if  distAT+distA     distBT+distB
761                 
762                 if (flagA==true)
763                 {
764                         lstTmpX.push_back( (*lstAX)[i] );
765                         lstTmpY.push_back( (*lstAY)[i] );
766                         lstTmpZ.push_back( (*lstAZ)[i] );
767                         distAT = distAT+distA;
768                         i++;
769                 } // if flagA
770
771                 if (flagB==true)
772                 {
773                         lstTmpX.push_back( (*lstBX)[j] );
774                         lstTmpY.push_back( (*lstBY)[j] );
775                         lstTmpZ.push_back( (*lstBZ)[j] );
776                         distBT = distBT+distB;
777                         j++;
778                 } // if flagA
779                 
780         } // while i  j
781         
782         sizeTmpX = lstTmpX.size();
783         for (ii=0; ii<sizeTmpX; ii=ii+2)
784         {
785                 (*lstOutX).push_back( lstTmpX[ii] );
786                 (*lstOutY).push_back( lstTmpY[ii] );
787                 (*lstOutZ).push_back( lstTmpZ[ii] );
788         } // for ii
789 printf("EED ManualContourModel_Box::Mixing2DistributionVectors Y=%f   size=%d  sizeTmpX=%d   sizeOut=%d \n",lstTmpY[0], size,sizeTmpX, (*lstOutX).size()  );    
790         
791 }
792                                                                                                         
793
794  
795
796
797 void ManualContourModel_Box::RedistributionPoints_SIN_iContour(int iContour,    std::vector<double> *lstOutX,
798                                                                                                         std::vector<double> *lstOutY, 
799                                                                                                         std::vector<double> *lstOutZ,
800                                                                                                         std::vector<int> *lstIndexsOut,double alpha, double beta )
801 {
802 printf("EED ManualContourModel_Box::RedistributionPoints_SIN_iContour  Start \n");
803         std::vector<double> lstRstX;
804         std::vector<double> lstRstY;
805         std::vector<double> lstRstZ;
806         int iLstIndexOut,sizeLstIndexOut = lstIndexsOut->size();
807         int ii,iGeneral;
808         double iiByDelta;
809         int size,iGeneralPlusSize;
810         double Alpha,Beta,t,tt, PI=3.14159265;
811         double TwoPI=2*PI;
812         double dist=0,dist2,distSeg,delta;
813         double dd,dx,dy,dz;
814         int i,k;
815         int firstK;
816         double tmpX,tmpY,tmpZ;
817         iGeneral=0;
818         for (iLstIndexOut=0;  iLstIndexOut<sizeLstIndexOut; iLstIndexOut++)  // For each contour
819         {
820                 lstRstX.clear();
821                 lstRstY.clear();
822                 lstRstZ.clear();
823                 size                            = (*lstIndexsOut)[iLstIndexOut];
824                 iGeneralPlusSize        = iGeneral+size;
825                 if (size>2)
826                 {
827                         dist=0;
828                         for ( i=iGeneral ; i<iGeneralPlusSize-1 ; i++ )
829                         {
830                                 dx      = (*lstOutX)[i]-(*lstOutX)[i+1];
831                                 dy      = (*lstOutY)[i]-(*lstOutY)[i+1];
832                                 dz      = (*lstOutZ)[i]-(*lstOutZ)[i+1];
833                                 dist= dist+sqrt( dx*dx + dy*dy + dz*dz );
834                         } //for
835                         delta = dist/(size-1);
836
837                         firstK  = 0;                    
838                         for (i=iGeneral; i<iGeneralPlusSize;i++)                                //  For each point of one contour
839                         {
840                                 ii = i-iGeneral;
841                                 dist2   = 0;
842                                 for (k=iGeneral; k<iGeneralPlusSize-1;k++)                      // Search inside
843                                 {
844 if ( 385==(*lstOutY)[k]  )
845
846         t               =       ((double)ii) / ((double)(size-1));
847         Beta    = 0.20; //  [0..1]
848         Alpha   = 0.0;  // [0..1]
849         Beta    = bbGetInputParam()[0];
850         Alpha   = bbGetInputParam()[1];
851         tt              = t + 0.70710678182*sin(t*TwoPI)*Beta + Alpha;
852         if (tt>1) { tt=tt-1; }
853         if (tt<0) { tt=tt+1; }
854         iiByDelta = tt * dist;
855 } else {
856   iiByDelta = ii*delta;
857 }
858                                         dx = (*lstOutX)[k+1]-(*lstOutX)[k];
859                                         dy = (*lstOutY)[k+1]-(*lstOutY)[k];
860                                         dz = (*lstOutZ)[k+1]-(*lstOutZ)[k];
861                                         distSeg = sqrt( dx*dx + dy*dy + dz*dz );
862                                         if ( dist2+distSeg >= iiByDelta )
863                                         {
864                                                 if (distSeg==0)
865                                                 {
866                                                         dd = 0;
867                                                 } else {
868                                                         dd=(iiByDelta-dist2)/distSeg;
869                                                 }// if distSeg == 0
870                                                 lstRstX.push_back( (*lstOutX)[k] + dd*dx );
871                                                 lstRstY.push_back( (*lstOutY)[k] + dd*dy );
872                                                 lstRstZ.push_back( (*lstOutZ)[k] + dd*dz );
873                                                 if (ii==0) { firstK=k; }
874                                                 k = iGeneralPlusSize-1;
875                                         } else {
876                                                 if ( k==iGeneral+size-2 )
877                                                 {
878                                                         dd = 1;
879                                                         lstRstX.push_back( (*lstOutX)[k] + dd*dx );
880                                                         lstRstY.push_back( (*lstOutY)[k] + dd*dy );
881                                                         lstRstZ.push_back( (*lstOutZ)[k] + dd*dz );
882                                                 }
883                                         }// if dist2 
884                                         dist2 = dist2+distSeg;
885                                 } // for k
886                         } //for i   
887                         
888                         
889                         if (lstRstX.size()!=size) 
890                         {
891                                 printf("EED Warnning!   ManualContourModel_Box::RedistributionPoints  >> This list is not coherent  iLstIndexOut=%d  lstRstX.size()=%d  size=%d\n",iLstIndexOut, lstRstX.size(), size);
892                                 tmpX    = lstRstX[iGeneral];
893                                 tmpY    = lstRstY[iGeneral];
894                                 tmpZ    = lstRstZ[iGeneral];
895                                 lstRstX.push_back( tmpX );
896                                 lstRstY.push_back( tmpY );
897                                 lstRstZ.push_back( tmpZ );
898                         }
899                         int iii;
900                         for (i=iGeneral; i<iGeneralPlusSize;i++)
901                         {
902 //                              ii=( (i-iGeneral) + firstK) % size ;
903                                 ii= i-iGeneral ;
904                                 iii=iGeneral+ ( (i-iGeneral) + firstK) % size ;
905                                 (*lstOutX)[iii] = lstRstX[ii];
906                                 (*lstOutY)[iii] = lstRstY[ii];
907                                 (*lstOutZ)[iii] = lstRstZ[ii];
908                         } // for i
909                 } // if size>2
910                 iGeneral=iGeneral+size;
911         }// for iLstIndexOut
912         
913 printf("EED ManualContourModel_Box::RedistributionPoints_SIN_iContour  End \n");
914         
915 }
916
917
918
919 void ManualContourModel_Box::ClockwisePoints(   std::vector<double> *lstInX,
920                                                                                         std::vector<double> *lstInY, 
921                                                                                         std::vector<double> *lstInZ,
922                                                                                         std::vector<int> *lstIndexsIn )
923 {
924         int     iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
925         int     i,iGeneral=0;
926         int     size,size2;
927         double  cx,cy,cz;
928         double  px,py,pz;
929         double  backpx,backpy,backpz;
930         double  ang;
931         char    dir=-1;
932         bool    dirx,diry,dirz;
933         int     flagAng=0;
934         float   backang;
935         double  tmp;
936
937         // For each contour
938         for (iLstIndexIn=0;  iLstIndexIn<sizeLstIndexIn; iLstIndexIn++)
939         {
940                 // Step 1. Find gravity center and direction
941                 size    = (*lstIndexsIn)[iLstIndexIn];
942                 if (size>2)  // for contour with more than 2 points
943                 {
944                         cx              = 0;
945                         cy              = 0;
946                         cz              = 0;
947                         dirx    = true;
948                         diry    = true;
949                         dirz    = true;
950                         for ( i=0 ; i<size ; i++ )
951                         {
952                                 px=(*lstInX)[iGeneral+i];
953                                 py=(*lstInY)[iGeneral+i];
954                                 pz=(*lstInZ)[iGeneral+i];
955                                 cx      = px + cx;
956                                 cy      = py + cy;
957                                 cz      = pz + cz;
958                                 if (i!=0) 
959                                 { 
960                                         if (backpx!=px) { dirx=false; } 
961                                         if (backpy!=py) { diry=false; } 
962                                         if (backpz!=pz) { dirz=false; } 
963                                         backpx=px;
964                                         backpy=py;
965                                         backpz=pz;
966                                 } // if i!=0
967                                 backpx=px; 
968                                 backpy=py; 
969                                 backpz=pz; 
970                         } // for i 
971                         cx=cx/size;
972                         cy=cy/size;
973                         cz=cz/size;
974                         if (dirx==true) { dir=1; }  // YZ 
975                         if (diry==true) { dir=2; }  // XZ 
976                         if (dirz==true) { dir=0; }  // XZ 
977                         // Step 2. Find angle diference find 
978                         flagAng=0;
979                         for ( i=0 ; i<size ; i++ )
980                         {
981                                 px = (*lstInX)[iGeneral+i]-cx;
982                                 py = (*lstInY)[iGeneral+i]-cy;
983                                 pz = (*lstInZ)[iGeneral+i]-cz;
984                                 if (dir==0) { ang=atan2( py , px ); } // XY
985                                 if (dir==1) { ang=atan2( pz , py ); } // YZ
986                                 if (dir==2) { ang=atan2( pz , px ); } // XZ
987                                 if (i>0) 
988                                 { 
989                                         if (backang<ang) 
990                                         {
991                                                 flagAng++;
992                                         } else {
993                                                 flagAng--;
994                                         }// if backang<ang
995                                 } // if i
996                                 backang=ang; 
997                         } // for i 
998
999
1000                         // Step 3. Invert order of points
1001                         if (flagAng<0)
1002                         {
1003                                 size2 = size/2;
1004                                 for ( i=0 ; i<size2 ; i++ )
1005                                 {
1006                                         tmp                                                     = (*lstInX)[iGeneral+i];
1007                                         (*lstInX)[iGeneral+i]                   = (*lstInX)[iGeneral+size-1-i];
1008                                         (*lstInX)[iGeneral+size-1-i]    = tmp;
1009                                         tmp                                                     = (*lstInY)[iGeneral+i];
1010                                         (*lstInY)[iGeneral+i]                   = (*lstInY)[iGeneral+size-1-i];
1011                                         (*lstInY)[iGeneral+size-1-i]    = tmp;
1012                                         tmp                                                     = (*lstInZ)[iGeneral+i];
1013                                         (*lstInZ)[iGeneral+i]                   = (*lstInZ)[iGeneral+size-1-i];
1014                                         (*lstInZ)[iGeneral+size-1-i]    = tmp;
1015                                 } // for i
1016                         } // flagAng
1017                 } // size>2
1018                 iGeneral = iGeneral+size;
1019         } // for iLstIndexIn
1020 }
1021
1022
1023 /*
1024 void ManualContourModel_Box::ShiftValues(       std::vector<double> *lstInX,
1025                                                                                         std::vector<double> *lstInY, 
1026                                                                                         std::vector<double> *lstInZ,
1027                                                                                         std::vector<int> *lstIndexsIn )
1028 {
1029         int iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
1030         int ii, iGeneral=0;
1031         int size,size2;
1032         double dist,distMin;
1033         int i,iBack;
1034         int ig;
1035         double dx,dy,dz;
1036         std::vector<double> LstTmpX;
1037         std::vector<double> LstTmpY;
1038         std::vector<double> LstTmpZ;
1039         if (sizeLstIndexIn>=2)
1040         {
1041         
1042                 for (iLstIndexIn=0;  iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
1043                 {
1044                         size  = (*lstIndexsIn)[iLstIndexIn];
1045                         size2 = (*lstIndexsIn)[iLstIndexIn+1];
1046                         //find min distance and  iBack
1047                         distMin = 10000000;
1048                         iBack   = 0;
1049                         
1050                         
1051                         for (ig=0; ig<size; ig++)
1052                         {
1053                                 for ( i=0 ; i<size2 ; i++ )
1054                                 {
1055                                         dx      = (*lstInX)[iGeneral+ig]-(*lstInX)[iGeneral+size+i];
1056                                         dy      = (*lstInY)[iGeneral+ig]-(*lstInY)[iGeneral+size+i];
1057                                         dz      = (*lstInZ)[iGeneral+ig]-(*lstInZ)[iGeneral+size+i];
1058                                         dist= sqrt( dx*dx + dy*dy + dz*dz );
1059                                         if ( dist<distMin ) 
1060                                         {
1061                                                 iBack   = i-ig;
1062                                                 distMin = dist;
1063                                         }
1064                                 } // for i size2
1065                         } // for ig size
1066
1067 if (iBack<0) 
1068 {
1069         printf("- "); 
1070         iBack = iBack + size2; 
1071 }
1072                         
1073 printf("EED ManualContourModel_Box::ShiftValues px=%f py=%f pz=%f  iBack=%d/%d  size=%d  distMin=%f \n", (*lstInX)[iGeneral]  , (*lstInY)[iGeneral] , (*lstInZ)[iGeneral], iBack, size2,size, distMin);
1074                         
1075                         if (iBack!=0)
1076                         {
1077                                 LstTmpX.clear();
1078                                 LstTmpY.clear();
1079                                 LstTmpZ.clear();
1080                                 for (i=0 ; i<size2 ; i++) 
1081                                 {
1082                                         ii= (i+iBack)%size2;
1083                                         LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
1084                                         LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
1085                                         LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
1086                                 } // for i                              
1087                                 for (i=0 ; i<size2 ; i++) 
1088                                 {
1089                                         (*lstInX)[iGeneral+size+i] = LstTmpX[i];
1090                                         (*lstInY)[iGeneral+size+i] = LstTmpY[i];
1091                                         (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
1092                                 } // for i                              
1093                         }
1094                         iGeneral=iGeneral+size;
1095                 } // for iLstIndexIn
1096                 
1097 //------------          
1098                 
1099                 iGeneral=0;
1100
1101                 for (iLstIndexIn=0;  iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
1102                 {
1103                         size  = (*lstIndexsIn)[iLstIndexIn];
1104                         size2 = (*lstIndexsIn)[iLstIndexIn+1];
1105                         //find min distance and  iBack
1106                         distMin = 10000000;
1107                         iBack   = 0;
1108                         
1109                         
1110 //                      for (ig=0; ig<size; ig++)
1111 //                      {
1112                                 for ( i=0 ; i<size2 ; i++ )
1113                                 {
1114                                         dx      = (*lstInX)[iGeneral]-(*lstInX)[iGeneral+size+i];
1115                                         dy      = (*lstInY)[iGeneral]-(*lstInY)[iGeneral+size+i];
1116                                         dz      = (*lstInZ)[iGeneral]-(*lstInZ)[iGeneral+size+i];
1117                                         dist= sqrt( dx*dx + dy*dy + dz*dz );
1118                                         if ( dist<distMin ) 
1119                                         {
1120                                                 iBack   = i;
1121                                                 distMin = dist;
1122                                         }
1123                                 } // for i size2
1124 //                      } // for ig size
1125
1126                         
1127 printf("EED ManualContourModel_Box::ShiftValues px=%f py=%f pz=%f  iBack=%d/%d  size=%d  distMin=%f \n", (*lstInX)[iGeneral]  , (*lstInY)[iGeneral] , (*lstInZ)[iGeneral], iBack, size2,size, distMin);
1128                         
1129                         if (iBack!=0)
1130                         {
1131                                 LstTmpX.clear();
1132                                 LstTmpY.clear();
1133                                 LstTmpZ.clear();
1134                                 for (i=0 ; i<size2 ; i++) 
1135                                 {
1136                                         ii= (i+iBack)%size2;
1137                                         LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
1138                                         LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
1139                                         LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
1140                                 } // for i                              
1141                                 for (i=0 ; i<size2 ; i++) 
1142                                 {
1143                                         (*lstInX)[iGeneral+size+i] = LstTmpX[i];
1144                                         (*lstInY)[iGeneral+size+i] = LstTmpY[i];
1145                                         (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
1146                                 } // for i                              
1147                         }
1148                         iGeneral=iGeneral+size;
1149                 } // for iLstIndexIn
1150                 
1151         } // if sizeLstIndexIn
1152         
1153 }
1154 */
1155
1156
1157
1158 void ManualContourModel_Box::ShiftValues(       std::vector<double> *lstInX,
1159                                                                                         std::vector<double> *lstInY, 
1160                                                                                         std::vector<double> *lstInZ,
1161                                                                                         std::vector<int> *lstIndexsIn )
1162 {
1163         int iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
1164         int ii, iGeneral=0;
1165         int size,size2;
1166         double dist,distMin;
1167         int i,iBack;
1168         int ig;
1169         double dx,dy,dz;
1170         std::vector<double> LstTmpX;
1171         std::vector<double> LstTmpY;
1172         std::vector<double> LstTmpZ;
1173         if (sizeLstIndexIn>=2)
1174         {
1175         
1176           
1177          
1178                 for (iLstIndexIn=0;  iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
1179                 {
1180                         size  = (*lstIndexsIn)[iLstIndexIn];
1181                         size2 = (*lstIndexsIn)[iLstIndexIn+1];
1182                         //find min distance and  iBack
1183                         distMin = 10000000;
1184                         iBack   = 0;
1185                         
1186                     // Comparing distance between two contours  
1187                     // Both contours need the same size 
1188                         for (ig=0; ig<size; ig++)
1189                         {
1190                                 dist=0;
1191                                 for ( i=0 ; i<size2 ; i++ )
1192                                 {
1193                                         dx      = (*lstInX)[iGeneral+i]-(*lstInX)[iGeneral+size+(i+ig)%size];
1194                                         dy      = (*lstInY)[iGeneral+i]-(*lstInY)[iGeneral+size+(i+ig)%size];
1195                                         dz      = (*lstInZ)[iGeneral+i]-(*lstInZ)[iGeneral+size+(i+ig)%size];
1196                                         dist= dist + sqrt( dx*dx + dy*dy + dz*dz );
1197                                 } // for i size2
1198
1199                                 if ( dist<distMin ) 
1200                                 {
1201                                         iBack   = ig+size;
1202                                         distMin = dist;
1203                                 }
1204                                 
1205                         } // for ig size
1206
1207                         
1208 //printf("EED ManualContourModel_Box::ShiftValues px=%f py=%f pz=%f  iBack=%d/%d  size=%d  distMin=%f \n", (*lstInX)[iGeneral]  , (*lstInY)[iGeneral] , (*lstInZ)[iGeneral], iBack, size2,size, distMin);
1209                         
1210                         if (iBack!=0)
1211                         {
1212                                 LstTmpX.clear();
1213                                 LstTmpY.clear();
1214                                 LstTmpZ.clear();
1215                                 for (i=0 ; i<size2 ; i++) 
1216                                 {
1217                                         ii = (i+iBack)%size2;
1218                                         if (ii<(size2-1)) // Skip the last item
1219                                         {
1220                                                 LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
1221                                                 LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
1222                                                 LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
1223                                         }
1224                                 } // for i
1225                                 
1226                                 //Repeat the first item at the end to close the contour
1227                                 LstTmpX.push_back( LstTmpX[0] );
1228                                 LstTmpY.push_back( LstTmpY[0] );
1229                                 LstTmpZ.push_back( LstTmpZ[0] );
1230
1231                                 for (i=0 ; i<size2 ; i++) 
1232                                 {
1233                                         (*lstInX)[iGeneral+size+i] = LstTmpX[i];
1234                                         (*lstInY)[iGeneral+size+i] = LstTmpY[i];
1235                                         (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
1236                                 } // for i                              
1237                         }
1238                         iGeneral=iGeneral+size;
1239                 } // for iLstIndexIn
1240                 
1241                 
1242         } // if sizeLstIndexIn
1243         
1244 }
1245
1246
1247
1248
1249
1250
1251 void ManualContourModel_Box::Process()
1252 {
1253 // THE MAIN PROCESSING METHOD BODY
1254 //   Here we simply set the input 'In' value to the output 'Out'
1255 //   And print out the output value
1256 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
1257 //    void bbSet{Input|Output}NAME(const TYPE&)
1258 //    const TYPE& bbGet{Input|Output}NAME() const 
1259 //    Where :
1260 //    * NAME is the name of the input/output
1261 //      (the one provided in the attribute 'name' of the tag 'input')
1262 //    * TYPE is the C++ type of the input/output
1263 //      (the one provided in the attribute 'type' of the tag 'input')
1264
1265 //    bbSetOutputOut( bbGetInputIn() );
1266 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
1267
1268         // First Step  Spline Interpolation
1269         std::vector<double> lstInX=bbGetInputLstControlPointsX();
1270         std::vector<double> lstInY=bbGetInputLstControlPointsY();
1271         std::vector<double> lstInZ=bbGetInputLstControlPointsZ();
1272         if ( (lstInX.size()!=lstInY.size()) || (lstInY.size()!=lstInZ.size()) ) 
1273         { 
1274                 printf("Warnning !!  .. ManualContourModel_Box: The list X Y Z, no have the same number of elements \n");
1275                 return;
1276         }
1277         std::vector<int>        lstIndexsIn=bbGetInputLstIndexsIn();
1278         std::vector<int>        lstIndexsOut;
1279         std::vector<double> lstOutX;
1280         std::vector<double> lstOutY;
1281         std::vector<double> lstOutZ;
1282         if (bbGetInputLstIndexsIn().size()==0)
1283         {
1284                 lstIndexsIn.push_back( lstInX.size() );
1285         }
1286         
1287 // Step 1.  All contours the same clockwise direction (Control Points)
1288         if (bbGetInputDoubleContour()==1)
1289         {
1290                 ClockwisePoints( &lstInX , &lstInY , &lstInZ , &lstIndexsIn );
1291 //              ShiftValues( &lstInX , &lstInY , &lstInZ , &lstIndexsIn );
1292         } // DoubleContour
1293         int i,size=lstIndexsIn.size();
1294         int iGeneral=0;
1295         
1296 // Step 2.  Spline interpolation of control points      
1297         for (i=0;i<size;i++)
1298         {
1299                 ProcessBySegment(       bbGetInputType() , 
1300                                                         iGeneral,  lstIndexsIn[i] ,
1301                                                         &lstInX ,  &lstInY  , &lstInZ,
1302                                                         &lstOutX , &lstOutY , &lstOutZ,
1303                                                         &lstIndexsOut,bbGetInputOpenClose() );
1304         } // for
1305
1306         if (bbGetInputDoubleContour()==0)
1307         {
1308 // Finish if Simple contours
1309                 //////////////////// Set Out   DoubleContour = 0
1310                 bbSetOutputLstContourPointsX(lstOutX);  
1311                 bbSetOutputLstContourPointsY(lstOutY);  
1312                 bbSetOutputLstContourPointsZ(lstOutZ);
1313                 bbSetOutputLstIndexsOut(lstIndexsOut);  
1314         } else {
1315 // Step 3. Interpolation in the other direction
1316 // Step 3.1 Linear Normalice points around contours     
1317                 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
1318 //EED 01/2021           
1319 // Step 3.2 Shift points to find minimun acumulate distance     
1320                 ShiftValues( &lstOutX, &lstOutY, &lstOutZ, &lstIndexsOut );
1321 // Step 3.3. SIN Normalice points around contours       
1322 //              RedistributionPoints_SIN_iContour(-1, &lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut,-1,-1);
1323
1324 if (bbGetInputParam()[0]==1)
1325 {
1326                 RedistributionPointsAllContours_SIN( &lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
1327                 ShiftValues( &lstOutX, &lstOutY, &lstOutZ, &lstIndexsOut );
1328 }
1329 // Step 3.4 Transpose the vectors   
1330                 lstInX.clear();
1331                 lstInY.clear();
1332                 lstInZ.clear();
1333                 lstIndexsIn.clear();
1334                 size  = bbGetInputNbPoints();
1335                 int j,size2 = lstIndexsOut.size();
1336                 for (i=0;i<size;i++)
1337                 {
1338                         for (j=0;j<size2;j++)
1339                         {
1340                                 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
1341                                 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
1342                                 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
1343                         } // for j
1344                         lstIndexsIn.push_back( size2 );
1345                 } // for i
1346                 lstOutX.clear();
1347                 lstOutY.clear();
1348                 lstOutZ.clear();
1349                 lstIndexsOut.clear();
1350 // Step 3.5 Interponation 2
1351                 size=lstIndexsIn.size();
1352                 iGeneral=0;
1353                 for (i=0;i<size;i++)
1354                 {
1355                         ProcessBySegment(       bbGetInputType() , 
1356                                                                 iGeneral, lstIndexsIn[i] ,
1357                                                                 &lstInX,&lstInY,&lstInZ,
1358                                                                 &lstOutX,&lstOutY,&lstOutZ,
1359                                                                 &lstIndexsOut,bbGetInputOpenClose2());
1360                 } // for
1361                 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
1362 // Step 3.6 Transpose the vectors   
1363                 lstInX.clear();
1364                 lstInY.clear();
1365                 lstInZ.clear();
1366                 lstIndexsIn.clear();
1367                 size  = bbGetInputNbPoints();
1368                 size2 = lstIndexsOut.size();
1369                 for (i=0;i<size;i++)
1370                 {
1371                         for (j=0;j<size2;j++)
1372                         {
1373                                 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
1374                                 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
1375                                 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
1376                         } // for j
1377                         lstIndexsIn.push_back( size2 );
1378                 } // for i
1379                 lstOutX.clear();
1380                 lstOutY.clear();
1381                 lstOutZ.clear();
1382                 lstIndexsOut.clear();
1383                 //////////////////// Set Out   DoubleContour = 1
1384                 bbSetOutputLstContourPointsX(lstInX);   
1385                 bbSetOutputLstContourPointsY(lstInY);   
1386                 bbSetOutputLstContourPointsZ(lstInZ);
1387                 bbSetOutputLstIndexsOut(lstIndexsIn);   
1388
1389         } // if DoubleContour 
1390
1391 }
1392 //===== 
1393 // 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)
1394 //===== 
1395 void ManualContourModel_Box::bbUserSetDefaultValues()
1396 {
1397
1398 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
1399 //    Here we initialize the input 'In' to 0
1400    bbSetInputType(1);
1401    bbSetInputDoubleContour(0);
1402    bbSetInputOpenClose(false);
1403    bbSetInputOpenClose2(false);
1404    bbSetInputNbPoints(100);
1405   
1406 }
1407 //===== 
1408 // 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)
1409 //===== 
1410 void ManualContourModel_Box::bbUserInitializeProcessing()
1411 {
1412
1413 //  THE INITIALIZATION METHOD BODY :
1414 //    Here does nothing 
1415 //    but this is where you should allocate the internal/output pointers 
1416 //    if any 
1417
1418   
1419 }
1420 //===== 
1421 // 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)
1422 //===== 
1423 void ManualContourModel_Box::bbUserFinalizeProcessing()
1424 {
1425
1426 //  THE FINALIZATION METHOD BODY :
1427 //    Here does nothing 
1428 //    but this is where you should desallocate the internal/output pointers 
1429 //    if any
1430   
1431 }
1432 }
1433 // EO namespace bbcreaMaracasVisu
1434
1435