]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/manualContour/manualViewContour.cpp
#3093 creaMaracasVisu Feature New Normal - Contour Information in pixels and image...
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / manualContour / manualViewContour.cpp
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 #include "manualViewContour.h"
27
28 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
31
32 manualViewContour::manualViewContour()
33 {
34         _id_viewPoint_for_text  = 0;
35         _initialConoturModel    = new manualContourModel();
36         
37         _minX                                   = 99999;
38         _minY                                   = 99999;
39         _maxX                                   = -99999;
40         _maxY                                   = -99999;
41         
42 }
43 // ----------------------------------------------------------------------------
44 manualViewContour::~manualViewContour()
45 {
46         delete _initialConoturModel;
47 }
48 // ----------------------------------------------------------------------------
49
50 // ----------------------------------------------------------------------------
51 manualViewContour * manualViewContour :: Clone()
52 {
53         manualViewContour * clone = new manualViewContour();
54         CopyAttributesTo(clone);
55         return clone;
56 }
57
58 // ---------------------------------------------------------------------------
59
60 void manualViewContour::CopyAttributesTo( manualViewContour * cloneObject)
61 {
62         // Call to Fathers object
63         manualViewBaseContour::CopyAttributesTo(cloneObject);
64 }
65
66 // ---------------------------------------------------------------------------
67
68 int manualViewContour::GetType() // virtual
69 {
70         return 1;
71 }
72
73 // ----------------------------------------------------------------------------
74
75 void manualViewContour::Save(FILE *pFile)
76 {
77         manualViewBaseContour::Save(pFile);
78 }
79
80 // ----------------------------------------------------------------------------
81
82 void manualViewContour::Open(FILE *pFile)
83 {
84 }
85
86 // ----------------------------------------------------------------------------
87 void manualViewContour::RefreshContour() // virtual
88 {
89         int i,np,nps;
90
91 //JSTG 25-02-08 --------------------
92         //double t,delta, x,y,z;
93         double x,y,z;
94         double xx,yy,zz;
95 //----------------------------------
96
97     np  = GetNumberOfPoints( );
98
99         if (np==2) 
100         {
101                 SetCellArray(false);
102                 manualPoint *mpA = _manContModel->GetManualPoint(0);
103                 manualPoint *mpB = _manContModel->GetManualPoint(1);            
104                 if ( _pts!=NULL )
105                 {
106                         xx=mpA->GetX() * _spc[0];
107                         yy=mpA->GetY() * _spc[1];
108                         zz=mpA->GetZ() * _spc[2];
109                         _minX=xx;
110                         _minY=yy;
111                         _maxX=xx;
112                         _maxY=yy;
113                         _pts->SetPoint(0 , xx , yy , zz  );
114                         xx=mpB->GetX() * _spc[0];
115                         yy=mpB->GetY() * _spc[1];
116                         zz=mpB->GetZ() * _spc[2];
117                         if (xx<_minX) _minX=xx;
118                         if (yy<_minY) _minY=yy;
119                         if (xx>_maxX) _maxX=xx;
120                         if (yy>_maxY) _maxY=yy;
121                         _pts->SetPoint(1 , xx , yy , zz  );
122                 } // if _pts!=NULL
123         } else {
124                 SetCellArray(true);
125                 _manContModel->UpdateSpline();
126                 //nps = GetNumberOfPointsSpline();
127                 nps = _manContModel->GetNumberOfPointsSpline();
128                 //delta=( double ) ( np  ) / ( double ) ( nps-1  );             //JSTG 25-02-08
129
130                 //EED 27 Juin 2012
131                 //Boundaring box        
132                 _minX=99999;
133                 _minY=99999;
134                 _maxX=-99999;
135                 _maxY=-99999;
136                 if ( _pts!=NULL )
137                 {
138                         if (np>=2  )
139                         {
140                                 for( i = 0; i < nps; i++ )
141                                 {
142         //JSTG 25-02-08 ------------------------------------------------
143                                         //t = delta * (double)i;
144                                         //_manContModel->GetSplinePoint(t,x,y,z);
145                                         _manContModel->GetSpline_i_Point(i,&x,&y,&z);
146         //--------------------------------------------------------------
147                 // EED 27 sep 2006
148                 //                      _pts->SetPoint(i, x,y,z );
149                                         xx=x*_spc[0];
150                                         yy=y*_spc[1];
151                                         zz=z*_spc[2];
152                                         _pts->SetPoint(i,xx,yy,zz );
153
154                             //EED 27 Juin 2012
155                                         //Boundaring box
156                                         if (xx < _minX) { _minX = xx; }
157                                         if (yy < _minY) { _minY = yy; }
158                                         if (xx > _maxX) { _maxX = xx; }
159                                         if (yy > _maxY) { _maxY = yy; }
160                                 
161                                 }// for
162                         }else{
163                                         _pts->SetPoint(0, 0 , 0 , 0);
164                                         _pts->SetPoint(1, 0 , 0 , 0);
165                         } // if
166                 } // _pts!=NULL
167         } //np==2
168
169         _minX=_minX-1.0;
170         _minY=_minY-1.0;
171         _maxX=_maxX+1.0;
172         _maxY=_maxY+1.0;
173
174 //EED 2017-03-02
175         _pts->Modified();
176 }
177
178 // ----------------------------------------------------------------------------
179 void manualViewContour::RefreshText()  // virtual
180 {
181
182         if ((_textActor!=NULL) && ( _textActor->GetProperty()->GetOpacity()!=0 )){
183                 int size = GetNumberOfPoints();
184                 char text[50];
185                 char resultText[250];
186                 strcpy(resultText, "000");
187                 char resultText2[250];
188                 strcpy(resultText2, "000");
189                 double spcP[3];
190                 spcP[0]=1;
191                 spcP[1]=1;
192                 spcP[2]=1;
193
194 //CMRU 19-08-09 ----------------------------------
195                 std::string label;
196                 label = _manContModel->GetLabel();
197 // ------------------------------------
198         
199                 if (size==2)
200                 {
201                         strcpy(resultText,"L= ");
202                         gcvt ( this->_manContModel->GetPathSize( spcP ), 5, text );
203                         strcat(resultText,text);
204                         strcat(resultText,"p");
205
206 //CMRU 19-08-09 ----------------------------------
207                         strcat(resultText," ");
208                         strcat(resultText,label.c_str());
209
210                         strcpy(resultText2,"L= ");
211                         gcvt ( this->_manContModel->GetPathSize( _spc ), 5, text );
212                         strcat(resultText2,text);
213                         strcat(resultText2,"u");
214
215 //------------------------------------------------
216                 }
217                 if (size>2)
218                 {
219                         if (_manContModel->IfCloseContour()==true)
220                         {
221                                 strcpy(resultText,"P= ");
222                                 gcvt ( this->_manContModel->GetPathSize( spcP ), 5, text );
223                                 strcat(resultText,text);
224                                 strcat(resultText,"p");
225
226                                 gcvt ( this->_manContModel->GetPathArea( spcP ), 5, text );
227                                 strcat(resultText,"   A= ");
228                                 strcat(resultText,text);
229                                 strcat(resultText,"p^2 ");
230
231
232                                 strcpy(resultText2,"P= ");
233                                 gcvt ( this->_manContModel->GetPathSize( _spc ), 5, text );
234                                 strcat(resultText2,text);
235                                 strcat(resultText2,"u");
236
237                                 gcvt ( this->_manContModel->GetPathArea( _spc ), 5, text );
238                                 strcat(resultText2,"   A= ");
239                                 strcat(resultText2,text);
240                                 strcat(resultText2,"u^2 ");
241
242                         } else {
243                                 strcpy(resultText,"L= ");
244                                 gcvt ( this->_manContModel->GetPathSize( spcP ), 5, text );
245                                 strcat(resultText,text);
246                                 strcat(resultText,"p");
247
248                                 strcpy(resultText2,"L= ");
249                                 gcvt ( this->_manContModel->GetPathSize( _spc ), 5, text );
250                                 strcat(resultText2,text);
251                                 strcat(resultText2,"u");
252
253                         }
254 //CMRU 19-08-09 ----------------------------------
255                         strcat(resultText,label.c_str());
256 //------------------------------------------------
257                 }
258
259                 _textActor->SetInput(resultText);
260                 _textActor2->SetInput(resultText2);
261                 
262                 if (size>=1){
263
264                         int i;
265                         for (i=0; i<size; i++)
266                         {
267                                 if (_lstViewPoints[i]->GetPosibleSelected()==true)
268                                 {
269                                         _id_viewPoint_for_text = i;
270                                 }
271                         }
272
273                         if (_id_viewPoint_for_text>=size) 
274                         {
275                                 _id_viewPoint_for_text=0;
276                         }
277                         
278                         double px = _manContModel->GetManualPoint(_id_viewPoint_for_text)->GetX();
279                         double py = _manContModel->GetManualPoint(_id_viewPoint_for_text)->GetY();
280
281                         //EED 27 sep 2006
282                         px=px*_spc[0];
283                         py=py*_spc[1];
284
285                         _textActor->SetPosition(px+GetRange()+1,py);
286                         _textActor2->SetPosition(px+GetRange()+1,py+2);
287                 }
288         }
289 }
290
291 // ----------------------------------------------------------------------------
292 bool manualViewContour::ifTouchContour(int x,int y,int z)
293 {
294         bool result=false;
295
296         double xx=x;
297         double yy=y;
298         double zz=z;
299         double ppA[3];
300         double ppB[3];
301         double d1,d2,d3;
302         TransfromCoordViewWorld(xx,yy,zz);
303
304 //EED 27 sep 2006
305         xx = xx * _spc[0];
306         yy = yy * _spc[1];
307         zz = zz * _spc[2];
308
309         if ( (xx>=_minX) &&  (yy>=_minY) && (xx<=_maxX) && (yy<=_maxY)) {  // inside the boundaring box
310         
311
312                 if (_manContModel->GetSizeLstPoints()==2)
313                 {
314                         manualPoint *mpA = _manContModel->GetManualPoint(0);
315                         manualPoint *mpB = _manContModel->GetManualPoint(1);
316                         _pts->GetPoint(0, ppA);
317                         double dirVec[3];
318
319                         // direction of the segment
320             dirVec[0] = mpB->GetX() - mpA->GetX();                      
321             dirVec[1] = mpB->GetY() - mpA->GetY();                      
322             dirVec[2] = mpB->GetZ() - mpA->GetZ();                      
323
324
325                         // proj = mpA + k*dirVec
326                         double num;
327                         double den = 0.0 ;
328                         num     =       dirVec[0]*( xx-mpA->GetX() );
329                         num     =       num + dirVec[1]*( yy-mpA->GetY() );
330                         num     =       num + dirVec[2]*( zz-mpA->GetZ() );
331                         den=dirVec[0]*dirVec[0] + dirVec[1]*dirVec[1] + dirVec[2]*dirVec[2];
332                         double k;
333                         if (den!=0) 
334                         {
335                                 k=num/den;
336                         } else {
337                                 k=99999999;
338                         }
339                         // projection of th point xx,yy,zz  in segment mpA,mpB
340                         double projX = mpA->GetX() + k*dirVec[0];
341                         double projY = mpA->GetY() + k*dirVec[1];
342                         double projZ = mpA->GetZ() + k*dirVec[2];
343
344                         // distance [projX,projY,projZ]   and   (xx,yy,zz]
345                         double d1= sqrt(  (projX-xx)*(projX-xx) + (projY-yy)*(projY-yy) +(projZ-zz)*(projZ-zz) );
346                         if (d1<=1)
347                         { 
348                                 result=true;
349                         } // if d1
350                 } else{
351                         unsigned int i, nps,nps_t;
352                         nps       = _sizePointsContour;
353                         if (this->_manContModel->IfCloseContour()==true)
354                         {
355                                 nps_t = nps;
356                         } else {
357                         nps_t = nps-1;
358                         } // if close
359                         for( i = 0; i < nps_t; i++ ) 
360                         {
361                                 _pts->GetPoint(i%nps, ppA);
362                                 _pts->GetPoint((i+1)%nps, ppB);
363                                 d1 = sqrt( (ppA[0]-xx)*(ppA[0]-xx) + (ppA[1]-yy)*(ppA[1]-yy) + (ppA[2]-zz)*(ppA[2]-zz));
364                                 d2 = sqrt( (ppB[0]-xx)*(ppB[0]-xx) + (ppB[1]-yy)*(ppB[1]-yy) + (ppB[2]-zz)*(ppB[2]-zz));
365                                 d3 = sqrt( (ppB[0]-ppA[0])*(ppB[0]-ppA[0]) + (ppB[1]-ppA[1])*(ppB[1]-ppA[1]) + (ppB[2]-ppA[2])*(ppB[2]-ppA[2]));
366                                 if (  ((d1+d2)>=d3) &&  ((d1+d2)<=d3*1.3) ) 
367                                 {
368                                         result=true;
369                                         i=nps;
370                                 } // if
371                         } // for i
372                 } // if GetSizeLstPoints()==2
373         } // if min max
374
375         return result;
376 }
377
378 // ----------------------------------------------------------------------------
379 void manualViewContour::DeletePoint(int id) // virtual
380 {
381         if (_lstViewPoints.size()>2)
382         {
383                 manualViewBaseContour::DeletePoint( id );
384         }
385 }
386 // ----------------------------------------------------------------------------
387
388 void manualViewContour::ClearPoint(int id)
389 {
390         manualViewBaseContour::DeletePoint( id );
391 }
392
393 //-------------------------------------------------------------------
394 void manualViewContour::InitMove(int x, int y, int z)
395 {
396         _initialConoturModel->DeleteAllPoints();
397
398         manualPoint *mp = NULL;
399         double XX=x;
400         double YY=y;
401         double ZZ=z;
402         TransfromCoordViewWorld(XX,YY,ZZ);
403
404         int i, manualPointsSZ = _manContModel->GetSizeLstPoints();
405         for ( i=0; i<manualPointsSZ; i++ )
406         {
407                 mp = _manContModel->GetManualPoint( i );
408                 this->_initialConoturModel->AddPoint( mp->GetX() - XX, mp->GetY() - YY, mp->GetZ() );
409         }
410 }
411
412 //-------------------------------------------------------------------
413 void manualViewContour::MoveContour(int x, int y, int z)
414 {
415         manualPoint *mpOrigin = NULL;
416         manualPoint *mpMoving = NULL;
417         double XX = x;
418         double YY = y;
419         double ZZ = z;
420
421         TransfromCoordViewWorld(XX,YY,ZZ);
422
423         int i, manualPointsSZ = _manContModel->GetSizeLstPoints();
424         for ( i=0; i<manualPointsSZ; i++ )
425         {
426                 mpOrigin = _manContModel->GetManualPoint( i );
427                 mpMoving = _initialConoturModel->GetManualPoint(i);
428                 mpOrigin->SetPoint( mpMoving->GetX()+XX, mpMoving->GetY() + YY, mpMoving->GetZ() );
429         }
430         UpdateViewPoints();
431 }
432
433 //-------------------------------------------------------------------
434 void manualViewContour::MoveContour(int horizontalUnits, int verticalUnits )
435 {
436         manualPoint *mpOrigin = NULL;
437
438         int i, manualPointsSZ = _manContModel->GetSizeLstPoints();
439         for ( i=0; i<manualPointsSZ; i++ )
440         {
441                 mpOrigin = _manContModel->GetManualPoint( i );
442                 mpOrigin->SetPoint( mpOrigin->GetX()+horizontalUnits, mpOrigin->GetY()+verticalUnits, mpOrigin->GetZ() );
443         }
444         UpdateViewPoints();
445 }