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)
4 #include "bbcreaMaracasVisuManualContourModel_Box.h"
5 #include "bbcreaMaracasVisuPackage.h"
7 #include <creaContoursFactory.h>
10 namespace bbcreaMaracasVisu
13 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ManualContourModel_Box)
14 BBTK_BLACK_BOX_IMPLEMENTATION(ManualContourModel_Box,bbtk::AtomicBlackBox);
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)
20 void ManualContourModel_Box::ProcessBySegment(
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 )
27 creaContoursFactory f;
28 manualContourModel *m;
29 int i,size=iGeneral+sizeSegment;
31 m = (manualContourModel*)f.getContourModel( bbGetInputType() );
32 m->SetNumberOfPointsSpline( bbGetInputNbPoints() );
33 m->SetCloseContour( open );
34 for (i=iGeneral;i<size;i++)
36 m->AddPoint( (*lstInX)[i] , (*lstInY)[i] , (*lstInZ)[i] );
39 int sizeContour = bbGetInputNbPoints();
40 for (i=0;i<sizeContour;i++)
42 m->GetSpline_i_Point(i,&x,&y,&z);
43 lstOutX->push_back(x);
44 lstOutY->push_back(y);
45 lstOutZ->push_back(z);
47 iGeneral=iGeneral+sizeSegment;
48 lstIndexsOut->push_back( sizeContour );
53 void ManualContourModel_Box::RedistributionPoints( std::vector<double> *lstOutX,
54 std::vector<double> *lstOutY,
55 std::vector<double> *lstOutZ,
56 std::vector<int> *lstIndexsOut )
58 std::vector<double> lstRstX;
59 std::vector<double> lstRstY;
60 std::vector<double> lstRstZ;
61 int iLstIndexOut,sizeLstIndexOut = lstIndexsOut->size();
64 for (iLstIndexOut=0; iLstIndexOut<sizeLstIndexOut; iLstIndexOut++)
66 //printf("EED ManualContourModel_Box::RedistributionPoints iLstIndexOut=%d \n", iLstIndexOut);
70 size=(*lstIndexsOut)[iLstIndexOut];
73 double dist=0,dist2,distSeg,delta;
76 for ( i=iGeneral+1 ; i<iGeneral+size ; i++ )
78 dx = (*lstOutX)[i]-(*lstOutX)[i-1];
79 dy = (*lstOutY)[i]-(*lstOutY)[i-1];
80 dz = (*lstOutZ)[i]-(*lstOutZ)[i-1];
81 dist= dist+sqrt( dx*dx + dy*dy + dz*dz );
83 delta = dist/(size-1);
84 lstRstX.push_back( (*lstOutX)[iGeneral] );
85 lstRstY.push_back( (*lstOutY)[iGeneral] );
86 lstRstZ.push_back( (*lstOutZ)[iGeneral] );
87 for (i=iGeneral+1; i<iGeneral+size;i++)
89 //printf("EED ManualContourModel_Box::RedistributionPointsi=%d \n", i-iGeneral );
91 for (k=iGeneral+1; k<iGeneral+size;k++)
93 dx = (*lstOutX)[k]-(*lstOutX)[k-1];
94 dy = (*lstOutY)[k]-(*lstOutY)[k-1];
95 dz = (*lstOutZ)[k]-(*lstOutZ)[k-1];
96 distSeg = sqrt( dx*dx + dy*dy + dz*dz );
98 if ( dist2+distSeg >= ii*delta )
100 dd=(ii*delta-dist2)/distSeg;
105 lstRstX.push_back( (*lstOutX)[k-1] + dd*dx );
106 lstRstY.push_back( (*lstOutY)[k-1] + dd*dy );
107 lstRstZ.push_back( (*lstOutZ)[k-1] + dd*dz );
110 if ( k==iGeneral+size-1 )
113 lstRstX.push_back( (*lstOutX)[k-1] + dd*dx );
114 lstRstY.push_back( (*lstOutY)[k-1] + dd*dy );
115 lstRstZ.push_back( (*lstOutZ)[k-1] + dd*dz );
116 // printf("EED ManualContourModel_Box::RedistributionPoints iLstIndexOut=%d i=%d k=%d dist2+distSeg=%f ii*delta=%f dif=%f \n", iLstIndexOut,i-iGeneral, k-iGeneral, dist2+distSeg , ii*delta , (dist2+distSeg) - ii*delta);
119 dist2 = dist2+distSeg;
122 if (lstRstX.size()!=size)
124 printf("EED Warnning! ManualContourModel_Box::RedistributionPoints >> This list is not coherent iLstIndexOut=%d lstRstX.size()=%d size=%d\n",iLstIndexOut, lstRstX.size(), size);
126 for (i=iGeneral; i<iGeneral+size;i++)
129 (*lstOutX)[i] = lstRstX[ii];
130 (*lstOutY)[i] = lstRstY[ii];
131 (*lstOutZ)[i] = lstRstZ[ii];
134 iGeneral=iGeneral+size;
139 void ManualContourModel_Box::ClockwisePoints( std::vector<double> *lstInX,
140 std::vector<double> *lstInY,
141 std::vector<double> *lstInZ,
142 std::vector<int> *lstIndexsIn )
144 int iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
149 double backpx,backpy,backpz;
158 for (iLstIndexIn=0; iLstIndexIn<sizeLstIndexIn; iLstIndexIn++)
160 // Step 1. Find gravity center and direction
161 size = (*lstIndexsIn)[iLstIndexIn];
162 if (size>2) // for contour with more than 2 points
170 for ( i=0 ; i<size ; i++ )
172 px=(*lstInX)[iGeneral+i];
173 py=(*lstInY)[iGeneral+i];
174 pz=(*lstInZ)[iGeneral+i];
180 if (backpx!=px) { dirx=false; }
181 if (backpy!=py) { diry=false; }
182 if (backpz!=pz) { dirz=false; }
194 if (dirx==true) { dir=1; } // YZ
195 if (diry==true) { dir=2; } // XZ
196 if (dirz==true) { dir=0; } // XZ
197 // Step 2. Find angle diference find
199 for ( i=0 ; i<size ; i++ )
201 px = (*lstInX)[iGeneral+i]-cx;
202 py = (*lstInY)[iGeneral+i]-cy;
203 pz = (*lstInZ)[iGeneral+i]-cz;
204 if (dir==0) { ang=atan2( py , px ); } // XY
205 if (dir==1) { ang=atan2( pz , py ); } // YZ
206 if (dir==2) { ang=atan2( pz , px ); } // XZ
220 // Step 3. Invert order of points
224 for ( i=0 ; i<size2 ; i++ )
226 tmp = (*lstInX)[iGeneral+i];
227 (*lstInX)[iGeneral+i] = (*lstInX)[iGeneral+size-1-i];
228 (*lstInX)[iGeneral+size-1-i] = tmp;
229 tmp = (*lstInY)[iGeneral+i];
230 (*lstInY)[iGeneral+i] = (*lstInY)[iGeneral+size-1-i];
231 (*lstInY)[iGeneral+size-1-i] = tmp;
232 tmp = (*lstInZ)[iGeneral+i];
233 (*lstInZ)[iGeneral+i] = (*lstInZ)[iGeneral+size-1-i];
234 (*lstInZ)[iGeneral+size-1-i] = tmp;
238 iGeneral = iGeneral+size;
244 void ManualContourModel_Box::ShiftValues( std::vector<double> *lstInX,
245 std::vector<double> *lstInY,
246 std::vector<double> *lstInZ,
247 std::vector<int> *lstIndexsIn )
249 int iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
256 std::vector<double> LstTmpX;
257 std::vector<double> LstTmpY;
258 std::vector<double> LstTmpZ;
259 if (sizeLstIndexIn>=2)
262 for (iLstIndexIn=0; iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
264 size = (*lstIndexsIn)[iLstIndexIn];
265 size2 = (*lstIndexsIn)[iLstIndexIn+1];
266 //find min distance and iBack
271 for (ig=0; ig<size; ig++)
273 for ( i=0 ; i<size2 ; i++ )
275 dx = (*lstInX)[iGeneral+ig]-(*lstInX)[iGeneral+size+i];
276 dy = (*lstInY)[iGeneral+ig]-(*lstInY)[iGeneral+size+i];
277 dz = (*lstInZ)[iGeneral+ig]-(*lstInZ)[iGeneral+size+i];
278 dist= sqrt( dx*dx + dy*dy + dz*dz );
290 iBack = iBack + size2;
293 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);
300 for (i=0 ; i<size2 ; i++)
303 LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
304 LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
305 LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
307 for (i=0 ; i<size2 ; i++)
309 (*lstInX)[iGeneral+size+i] = LstTmpX[i];
310 (*lstInY)[iGeneral+size+i] = LstTmpY[i];
311 (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
314 iGeneral=iGeneral+size;
321 for (iLstIndexIn=0; iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
323 size = (*lstIndexsIn)[iLstIndexIn];
324 size2 = (*lstIndexsIn)[iLstIndexIn+1];
325 //find min distance and iBack
330 // for (ig=0; ig<size; ig++)
332 for ( i=0 ; i<size2 ; i++ )
334 dx = (*lstInX)[iGeneral]-(*lstInX)[iGeneral+size+i];
335 dy = (*lstInY)[iGeneral]-(*lstInY)[iGeneral+size+i];
336 dz = (*lstInZ)[iGeneral]-(*lstInZ)[iGeneral+size+i];
337 dist= sqrt( dx*dx + dy*dy + dz*dz );
347 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);
354 for (i=0 ; i<size2 ; i++)
357 LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
358 LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
359 LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
361 for (i=0 ; i<size2 ; i++)
363 (*lstInX)[iGeneral+size+i] = LstTmpX[i];
364 (*lstInY)[iGeneral+size+i] = LstTmpY[i];
365 (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
368 iGeneral=iGeneral+size;
371 } // if sizeLstIndexIn
378 void ManualContourModel_Box::ShiftValues( std::vector<double> *lstInX,
379 std::vector<double> *lstInY,
380 std::vector<double> *lstInZ,
381 std::vector<int> *lstIndexsIn )
383 int iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
390 std::vector<double> LstTmpX;
391 std::vector<double> LstTmpY;
392 std::vector<double> LstTmpZ;
393 if (sizeLstIndexIn>=2)
398 for (iLstIndexIn=0; iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
400 size = (*lstIndexsIn)[iLstIndexIn];
401 size2 = (*lstIndexsIn)[iLstIndexIn+1];
402 //find min distance and iBack
406 // Comparing distance between two contours
407 // Both contours need the same size
408 for (ig=0; ig<size; ig++)
411 for ( i=0 ; i<size2 ; i++ )
413 dx = (*lstInX)[iGeneral+i]-(*lstInX)[iGeneral+size+(i+ig)%size];
414 dy = (*lstInY)[iGeneral+i]-(*lstInY)[iGeneral+size+(i+ig)%size];
415 dz = (*lstInZ)[iGeneral+i]-(*lstInZ)[iGeneral+size+(i+ig)%size];
416 dist= dist + sqrt( dx*dx + dy*dy + dz*dz );
428 //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);
435 for (i=0 ; i<size2 ; i++)
437 ii = (i+iBack)%size2;
438 if (ii<(size2-1)) // Skip the last item
440 LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
441 LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
442 LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
446 //Repeat the first item at the end to close the contour
447 LstTmpX.push_back( LstTmpX[0] );
448 LstTmpY.push_back( LstTmpY[0] );
449 LstTmpZ.push_back( LstTmpZ[0] );
451 for (i=0 ; i<size2 ; i++)
453 (*lstInX)[iGeneral+size+i] = LstTmpX[i];
454 (*lstInY)[iGeneral+size+i] = LstTmpY[i];
455 (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
458 iGeneral=iGeneral+size;
462 } // if sizeLstIndexIn
471 void ManualContourModel_Box::Process()
473 // THE MAIN PROCESSING METHOD BODY
474 // Here we simply set the input 'In' value to the output 'Out'
475 // And print out the output value
476 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
477 // void bbSet{Input|Output}NAME(const TYPE&)
478 // const TYPE& bbGet{Input|Output}NAME() const
480 // * NAME is the name of the input/output
481 // (the one provided in the attribute 'name' of the tag 'input')
482 // * TYPE is the C++ type of the input/output
483 // (the one provided in the attribute 'type' of the tag 'input')
485 // bbSetOutputOut( bbGetInputIn() );
486 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
488 // First Step Spline Interpolation
489 std::vector<double> lstInX=bbGetInputLstControlPointsX();
490 std::vector<double> lstInY=bbGetInputLstControlPointsY();
491 std::vector<double> lstInZ=bbGetInputLstControlPointsZ();
492 if ( (lstInX.size()!=lstInY.size()) || (lstInY.size()!=lstInZ.size()) )
494 printf("Warnning !! .. ManualContourModel_Box: The list X Y Z, no have the same number of elements \n");
497 std::vector<int> lstIndexsIn=bbGetInputLstIndexsIn();
498 std::vector<int> lstIndexsOut;
499 std::vector<double> lstOutX;
500 std::vector<double> lstOutY;
501 std::vector<double> lstOutZ;
502 if (bbGetInputLstIndexsIn().size()==0)
504 lstIndexsIn.push_back( lstInX.size() );
506 if (bbGetInputDoubleContour()==1)
508 ClockwisePoints( &lstInX , &lstInY , &lstInZ , &lstIndexsIn );
509 // ShiftValues( &lstInX , &lstInY , &lstInZ , &lstIndexsIn );
511 int i,size=lstIndexsIn.size();
515 ProcessBySegment( bbGetInputType() ,
516 iGeneral, lstIndexsIn[i] ,
517 &lstInX , &lstInY , &lstInZ,
518 &lstOutX , &lstOutY , &lstOutZ,
519 &lstIndexsOut,bbGetInputOpenClose() );
522 if (bbGetInputDoubleContour()==0)
524 //////////////////// Set Out DoubleContour = 0
525 bbSetOutputLstContourPointsX(lstOutX);
526 bbSetOutputLstContourPointsY(lstOutY);
527 bbSetOutputLstContourPointsZ(lstOutZ);
528 bbSetOutputLstIndexsOut(lstIndexsOut);
530 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
532 ShiftValues( &lstOutX, &lstOutY, &lstOutZ, &lstIndexsOut );
533 ///////////// Second Step Transpose the vectors
538 size = bbGetInputNbPoints();
539 int j,size2 = lstIndexsOut.size();
542 for (j=0;j<size2;j++)
544 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
545 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
546 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
548 lstIndexsIn.push_back( size2 );
553 lstIndexsOut.clear();
554 ///////////////////// Third step Interponation 2
555 size=lstIndexsIn.size();
559 ProcessBySegment( bbGetInputType() ,
560 iGeneral, lstIndexsIn[i] ,
561 &lstInX,&lstInY,&lstInZ,
562 &lstOutX,&lstOutY,&lstOutZ,
563 &lstIndexsOut,bbGetInputOpenClose2());
565 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
566 //////////////////// Forth step Transpose the vectors
571 size = bbGetInputNbPoints();
572 size2 = lstIndexsOut.size();
575 for (j=0;j<size2;j++)
577 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
578 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
579 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
581 lstIndexsIn.push_back( size2 );
586 lstIndexsOut.clear();
587 //////////////////// Set Out DoubleContour = 1
588 bbSetOutputLstContourPointsX(lstInX);
589 bbSetOutputLstContourPointsY(lstInY);
590 bbSetOutputLstContourPointsZ(lstInZ);
591 bbSetOutputLstIndexsOut(lstIndexsIn);
593 } // if DoubleContour
597 // 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)
599 void ManualContourModel_Box::bbUserSetDefaultValues()
602 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
603 // Here we initialize the input 'In' to 0
605 bbSetInputDoubleContour(0);
606 bbSetInputOpenClose(false);
607 bbSetInputOpenClose2(false);
608 bbSetInputNbPoints(100);
612 // 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)
614 void ManualContourModel_Box::bbUserInitializeProcessing()
617 // THE INITIALIZATION METHOD BODY :
619 // but this is where you should allocate the internal/output pointers
625 // 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)
627 void ManualContourModel_Box::bbUserFinalizeProcessing()
630 // THE FINALIZATION METHOD BODY :
632 // but this is where you should desallocate the internal/output pointers
637 // EO namespace bbcreaMaracasVisu