]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuManualContourModel_Box.cxx
#3266 creaMaracasVisu Feature New Normal - ManualContour_Box by segments
[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         
267         if (bbGetInputDoubleContour()==1)
268         {
269                 ShiftValues( &lstInX , &lstInY , &lstInZ , &lstIndexsIn );
270         } // DoubleContour
271
272         int i,size=lstIndexsIn.size();
273         int iGeneral=0;
274         for (i=0;i<size;i++)
275         {
276                 ProcessBySegment(       bbGetInputType() , 
277                                                         iGeneral,  lstIndexsIn[i] ,
278                                                         &lstInX ,  &lstInY  , &lstInZ,
279                                                         &lstOutX , &lstOutY , &lstOutZ,
280                                                         &lstIndexsOut,bbGetInputOpenClose() );
281         } // for
282
283         if (bbGetInputDoubleContour()==0)
284         {
285                 //////////////////// Set Out   DoubleContour = 0
286                 bbSetOutputLstContourPointsX(lstOutX);  
287                 bbSetOutputLstContourPointsY(lstOutY);  
288                 bbSetOutputLstContourPointsZ(lstOutZ);
289                 bbSetOutputLstIndexsOut(lstIndexsOut);  
290         } else {
291                 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
292                 ///////////// Second Step Transpose the vectors   
293                 lstInX.clear();
294                 lstInY.clear();
295                 lstInZ.clear();
296                 lstIndexsIn.clear();
297                 size  = bbGetInputNbPoints();
298                 int j,size2 = lstIndexsOut.size();
299                 for (i=0;i<size;i++)
300                 {
301                         for (j=0;j<size2;j++)
302                         {
303                                 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
304                                 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
305                                 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
306                         } // for j
307                         lstIndexsIn.push_back( size2 );
308                 } // for i
309                 lstOutX.clear();
310                 lstOutY.clear();
311                 lstOutZ.clear();
312                 lstIndexsOut.clear();
313                 ///////////////////// Third step Interponation 2
314                 size=lstIndexsIn.size();
315                 iGeneral=0;
316                 for (i=0;i<size;i++)
317                 {
318                         ProcessBySegment(       bbGetInputType() , 
319                                                                 iGeneral, lstIndexsIn[i] ,
320                                                                 &lstInX,&lstInY,&lstInZ,
321                                                                 &lstOutX,&lstOutY,&lstOutZ,
322                                                                 &lstIndexsOut,bbGetInputOpenClose2());
323                 } // for
324                 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
325                 //////////////////// Forth step Transpose the vectors   
326                 lstInX.clear();
327                 lstInY.clear();
328                 lstInZ.clear();
329                 lstIndexsIn.clear();
330                 size  = bbGetInputNbPoints();
331                 size2 = lstIndexsOut.size();
332                 for (i=0;i<size;i++)
333                 {
334                         for (j=0;j<size2;j++)
335                         {
336                                 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
337                                 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
338                                 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
339                         } // for j
340                         lstIndexsIn.push_back( size2 );
341                 } // for i
342                 lstOutX.clear();
343                 lstOutY.clear();
344                 lstOutZ.clear();
345                 lstIndexsOut.clear();
346
347                 //////////////////// Set Out   DoubleContour = 1
348                 bbSetOutputLstContourPointsX(lstInX);   
349                 bbSetOutputLstContourPointsY(lstInY);   
350                 bbSetOutputLstContourPointsZ(lstInZ);
351                 bbSetOutputLstIndexsOut(lstIndexsIn);   
352
353         } // if DoubleContour 
354
355 }
356 //===== 
357 // 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)
358 //===== 
359 void ManualContourModel_Box::bbUserSetDefaultValues()
360 {
361
362 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
363 //    Here we initialize the input 'In' to 0
364    bbSetInputType(1);
365    bbSetInputDoubleContour(0);
366    bbSetInputOpenClose(false);
367    bbSetInputOpenClose2(false);
368    bbSetInputNbPoints(100);
369   
370 }
371 //===== 
372 // 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)
373 //===== 
374 void ManualContourModel_Box::bbUserInitializeProcessing()
375 {
376
377 //  THE INITIALIZATION METHOD BODY :
378 //    Here does nothing 
379 //    but this is where you should allocate the internal/output pointers 
380 //    if any 
381
382   
383 }
384 //===== 
385 // 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)
386 //===== 
387 void ManualContourModel_Box::bbUserFinalizeProcessing()
388 {
389
390 //  THE FINALIZATION METHOD BODY :
391 //    Here does nothing 
392 //    but this is where you should desallocate the internal/output pointers 
393 //    if any
394   
395 }
396 }
397 // EO namespace bbcreaMaracasVisu
398
399