]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuManualContourModel_Box.cxx
#3306 creaMaracasVisu Feature New Normal - Clean Code
[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 //printf("EED ManualContourModel_Box::RedistributionPoints iLstIndexOut=%d   \n", iLstIndexOut);
67                 lstRstX.clear();
68                 lstRstY.clear();
69                 lstRstZ.clear();
70                 size=(*lstIndexsOut)[iLstIndexOut];
71                 if (size>2)
72                 {
73                         double dist=0,dist2,distSeg,delta;
74                         double dd,dx,dy,dz;
75                         int i,k;
76                         for (i=iGeneral+1; i<iGeneral+size;i++)
77                         {
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 );
82                         } //for
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++)
88                         {
89 //printf("EED ManualContourModel_Box::RedistributionPointsi=%d \n", i-iGeneral );
90
91                                 dist2 = 0;
92                                 for (k=iGeneral+1; k<iGeneral+size;k++)
93                                 {
94                                         dx=(*lstOutX)[k]-(*lstOutX)[k-1];
95                                         dy=(*lstOutY)[k]-(*lstOutY)[k-1];
96                                         dz=(*lstOutZ)[k]-(*lstOutZ)[k-1];
97                                         distSeg = sqrt( dx*dx + dy*dy + dz*dz );
98                                         ii=i-iGeneral;
99                                         if ( dist2+distSeg>=ii*delta) 
100                                         {
101                                                 dd=(ii*delta-dist2)/distSeg;
102                                                 if (distSeg==0)
103                                                 {
104                                                         dd=0;
105                                                 } // if distSeg == 0
106                                                 lstRstX.push_back( (*lstOutX)[k-1] + dd*dx );
107                                                 lstRstY.push_back( (*lstOutY)[k-1] + dd*dy );
108                                                 lstRstZ.push_back( (*lstOutZ)[k-1] + dd*dz );
109                                                 k=iGeneral+size;
110                                         } else {
111                                                 if (k==iGeneral+size-1) 
112                                                 {
113                                                         dd=1;
114                                                         lstRstX.push_back( (*lstOutX)[k-1] + dd*dx );
115                                                         lstRstY.push_back( (*lstOutY)[k-1] + dd*dy );
116                                                         lstRstZ.push_back( (*lstOutZ)[k-1] + dd*dz );
117 //                                                      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);
118                                                 }
119                                         }// if dist2 
120                                         dist2=dist2+distSeg;
121                                 } // for k
122                         } //for i   
123                         if (lstRstX.size()!=size) 
124                         {
125                                 printf("EED Warnning!   ManualContourModel_Box::RedistributionPoints  >> This list is not coherent  iLstIndexOut=%d  lstRstX.size()=%d  size=%d\n",iLstIndexOut, lstRstX.size(), size);
126                         }
127                         for (i=iGeneral; i<iGeneral+size;i++)
128                         {
129                                 ii=i-iGeneral;
130                                 (*lstOutX)[i] = lstRstX[ii];
131                                 (*lstOutY)[i] = lstRstY[ii];
132                                 (*lstOutZ)[i] = lstRstZ[ii];
133                         } // for i
134                 } // if size>2
135                 iGeneral=iGeneral+size;
136         }// for iLstIndexOut
137 }
138
139
140
141 void ManualContourModel_Box::ShiftValues(       std::vector<double> *lstInX,
142                                                                                         std::vector<double> *lstInY, 
143                                                                                         std::vector<double> *lstInZ,
144                                                                                         std::vector<int> *lstIndexsIn )
145 {
146         int iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
147         int ii, iGeneral=0;
148         int size,size2;
149         double dist,distMin;
150         int i,iBack;
151         double dx,dy,dz;
152         std::vector<double> LstTmpX;
153         std::vector<double> LstTmpY;
154         std::vector<double> LstTmpZ;
155         if (sizeLstIndexIn>=2)
156         {
157                 for (iLstIndexIn=0;  iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
158                 {
159                         size  = (*lstIndexsIn)[iLstIndexIn];
160                         size2 = (*lstIndexsIn)[iLstIndexIn+1];
161                         //find min distance and  iBack
162                         distMin = 10000000;
163                         iBack   = 0;
164                         for ( i=0 ; i<size2 ; i++ )
165                         {
166                                 dx      = (*lstInX)[iGeneral]-(*lstInX)[iGeneral+size+i];
167                                 dy      = (*lstInY)[iGeneral]-(*lstInY)[iGeneral+size+i];
168                                 dz      = (*lstInZ)[iGeneral]-(*lstInZ)[iGeneral+size+i];
169                                 dist= sqrt( dx*dx + dy*dy + dz*dz );
170                                 if ( dist<distMin ) 
171                                 {
172                                         iBack   = i;
173                                         distMin = dist;
174                                 }
175                         } // for dist
176                         if (iBack!=0)
177                         {
178                                 LstTmpX.clear();
179                                 LstTmpY.clear();
180                                 LstTmpZ.clear();
181                                 for (i=0 ; i<size2 ; i++) 
182                                 {
183                                         ii= (i+iBack)%size2;
184                                         LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
185                                         LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
186                                         LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
187                                 } // for i                              
188                                 for (i=0 ; i<size2 ; i++) 
189                                 {
190                                         (*lstInX)[iGeneral+size+i] = LstTmpX[i];
191                                         (*lstInY)[iGeneral+size+i] = LstTmpY[i];
192                                         (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
193                                 } // for i                              
194                         }
195                         iGeneral=iGeneral+size;
196                 } // for iLstIndexIn
197         } // sizeLstIndexIn
198 }
199
200
201
202 void ManualContourModel_Box::Process()
203 {
204 // THE MAIN PROCESSING METHOD BODY
205 //   Here we simply set the input 'In' value to the output 'Out'
206 //   And print out the output value
207 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
208 //    void bbSet{Input|Output}NAME(const TYPE&)
209 //    const TYPE& bbGet{Input|Output}NAME() const 
210 //    Where :
211 //    * NAME is the name of the input/output
212 //      (the one provided in the attribute 'name' of the tag 'input')
213 //    * TYPE is the C++ type of the input/output
214 //      (the one provided in the attribute 'type' of the tag 'input')
215
216 //    bbSetOutputOut( bbGetInputIn() );
217 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
218
219         // First Step  Spline Interpolation
220         std::vector<double> lstInX=bbGetInputLstControlPointsX();
221         std::vector<double> lstInY=bbGetInputLstControlPointsY();
222         std::vector<double> lstInZ=bbGetInputLstControlPointsZ();
223         if ( (lstInX.size()!=lstInY.size()) || (lstInY.size()!=lstInZ.size()) ) 
224         { 
225                 printf("Warnning !!  .. ManualContourModel_Box: The list X Y Z, no have the same number of elements \n");
226                 return;
227         }
228         std::vector<int>        lstIndexsIn=bbGetInputLstIndexsIn();
229         std::vector<int>        lstIndexsOut;
230         std::vector<double> lstOutX;
231         std::vector<double> lstOutY;
232         std::vector<double> lstOutZ;
233         if (bbGetInputLstIndexsIn().size()==0)
234         {
235                 lstIndexsIn.push_back( lstInX.size() );
236         }
237         if (bbGetInputDoubleContour()==1)
238         {
239                 ShiftValues( &lstInX , &lstInY , &lstInZ , &lstIndexsIn );
240         } // DoubleContour
241         int i,size=lstIndexsIn.size();
242         int iGeneral=0;
243         for (i=0;i<size;i++)
244         {
245                 ProcessBySegment(       bbGetInputType() , 
246                                                         iGeneral,  lstIndexsIn[i] ,
247                                                         &lstInX ,  &lstInY  , &lstInZ,
248                                                         &lstOutX , &lstOutY , &lstOutZ,
249                                                         &lstIndexsOut,bbGetInputOpenClose() );
250         } // for
251
252         if (bbGetInputDoubleContour()==0)
253         {
254                 //////////////////// Set Out   DoubleContour = 0
255                 bbSetOutputLstContourPointsX(lstOutX);  
256                 bbSetOutputLstContourPointsY(lstOutY);  
257                 bbSetOutputLstContourPointsZ(lstOutZ);
258                 bbSetOutputLstIndexsOut(lstIndexsOut);  
259         } else {
260                 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
261                 ///////////// Second Step Transpose the vectors   
262                 lstInX.clear();
263                 lstInY.clear();
264                 lstInZ.clear();
265                 lstIndexsIn.clear();
266                 size  = bbGetInputNbPoints();
267                 int j,size2 = lstIndexsOut.size();
268                 for (i=0;i<size;i++)
269                 {
270                         for (j=0;j<size2;j++)
271                         {
272                                 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
273                                 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
274                                 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
275                         } // for j
276                         lstIndexsIn.push_back( size2 );
277                 } // for i
278                 lstOutX.clear();
279                 lstOutY.clear();
280                 lstOutZ.clear();
281                 lstIndexsOut.clear();
282                 ///////////////////// Third step Interponation 2
283                 size=lstIndexsIn.size();
284                 iGeneral=0;
285                 for (i=0;i<size;i++)
286                 {
287                         ProcessBySegment(       bbGetInputType() , 
288                                                                 iGeneral, lstIndexsIn[i] ,
289                                                                 &lstInX,&lstInY,&lstInZ,
290                                                                 &lstOutX,&lstOutY,&lstOutZ,
291                                                                 &lstIndexsOut,bbGetInputOpenClose2());
292                 } // for
293                 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
294                 //////////////////// Forth step Transpose the vectors   
295                 lstInX.clear();
296                 lstInY.clear();
297                 lstInZ.clear();
298                 lstIndexsIn.clear();
299                 size  = bbGetInputNbPoints();
300                 size2 = lstIndexsOut.size();
301                 for (i=0;i<size;i++)
302                 {
303                         for (j=0;j<size2;j++)
304                         {
305                                 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
306                                 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
307                                 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
308                         } // for j
309                         lstIndexsIn.push_back( size2 );
310                 } // for i
311                 lstOutX.clear();
312                 lstOutY.clear();
313                 lstOutZ.clear();
314                 lstIndexsOut.clear();
315                 //////////////////// Set Out   DoubleContour = 1
316                 bbSetOutputLstContourPointsX(lstInX);   
317                 bbSetOutputLstContourPointsY(lstInY);   
318                 bbSetOutputLstContourPointsZ(lstInZ);
319                 bbSetOutputLstIndexsOut(lstIndexsIn);   
320
321         } // if DoubleContour 
322
323 }
324 //===== 
325 // 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)
326 //===== 
327 void ManualContourModel_Box::bbUserSetDefaultValues()
328 {
329
330 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
331 //    Here we initialize the input 'In' to 0
332    bbSetInputType(1);
333    bbSetInputDoubleContour(0);
334    bbSetInputOpenClose(false);
335    bbSetInputOpenClose2(false);
336    bbSetInputNbPoints(100);
337   
338 }
339 //===== 
340 // 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)
341 //===== 
342 void ManualContourModel_Box::bbUserInitializeProcessing()
343 {
344
345 //  THE INITIALIZATION METHOD BODY :
346 //    Here does nothing 
347 //    but this is where you should allocate the internal/output pointers 
348 //    if any 
349
350   
351 }
352 //===== 
353 // 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)
354 //===== 
355 void ManualContourModel_Box::bbUserFinalizeProcessing()
356 {
357
358 //  THE FINALIZATION METHOD BODY :
359 //    Here does nothing 
360 //    but this is where you should desallocate the internal/output pointers 
361 //    if any
362   
363 }
364 }
365 // EO namespace bbcreaMaracasVisu
366
367