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