]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/manualContour/manualViewContour.cpp
0c082a20c818ea232c9fca2db8a25d3d966f970d
[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 //JSTG 25-02-08 --------------------
91         //double t,delta, x,y,z;
92         double x,y,z;
93         double xx,yy,zz;
94 //----------------------------------
95
96     np  = GetNumberOfPoints( );
97
98         if (np==2) 
99         {
100                 SetCellArray(false);
101                 manualPoint *mpA = _manContModel->GetManualPoint(0);
102                 manualPoint *mpB = _manContModel->GetManualPoint(1);            
103                 if ( _pts!=NULL )
104                 {
105                         xx=mpA->GetX() * _spc[0];
106                         yy=mpA->GetY() * _spc[1];
107                         zz=mpA->GetZ() * _spc[2];
108                         _minX=xx;
109                         _minY=yy;
110                         _maxX=xx;
111                         _maxY=yy;
112                         _pts->SetPoint(0 , xx , yy , zz  );
113                         xx=mpB->GetX() * _spc[0];
114                         yy=mpB->GetY() * _spc[1];
115                         zz=mpB->GetZ() * _spc[2];
116                         if (xx<_minX) _minX=xx;
117                         if (yy<_minY) _minY=yy;
118                         if (xx>_maxX) _maxX=xx;
119                         if (yy>_maxY) _maxY=yy;
120                         _pts->SetPoint(1 , xx , yy , zz  );
121                 } // if _pts!=NULL
122         } else {
123                 SetCellArray(true);
124                 _manContModel->UpdateSpline();
125                 //nps = GetNumberOfPointsSpline();
126                 nps = _manContModel->GetNumberOfPointsSpline();
127                 //delta=( double ) ( np  ) / ( double ) ( nps-1  );             //JSTG 25-02-08
128
129                 //EED 27 Juin 2012
130                 //Boundaring box        
131                 _minX=99999;
132                 _minY=99999;
133                 _maxX=-99999;
134                 _maxY=-99999;
135                 if ( _pts!=NULL )
136                 {
137                         if (np>=2  )
138                         {
139                                 for( i = 0; i < nps; i++ )
140                                 {
141         //JSTG 25-02-08 ------------------------------------------------
142                                         //t = delta * (double)i;
143                                         //_manContModel->GetSplinePoint(t,x,y,z);
144                                         _manContModel->GetSpline_i_Point(i,&x,&y,&z);
145         //--------------------------------------------------------------
146                 // EED 27 sep 2006
147                 //                      _pts->SetPoint(i, x,y,z );
148                                         xx = x*_spc[0];
149                                         yy = y*_spc[1];
150                                         zz = z*_spc[2];
151                                         _pts->SetPoint(i,xx,yy,zz );
152
153                             //EED 27 Juin 2012
154                                         //Boundaring box
155                                         if (xx < _minX) { _minX = xx; }
156                                         if (yy < _minY) { _minY = yy; }
157                                         if (xx > _maxX) { _maxX = xx; }
158                                         if (yy > _maxY) { _maxY = yy; }
159                                 }// for
160                         }else{
161                                         _pts->SetPoint(0, 0 , 0 , 0);
162                                         _pts->SetPoint(1, 0 , 0 , 0);
163                         } // if
164                 } // _pts!=NULL
165         } //np==2
166
167         _minX=_minX-1.0;
168         _minY=_minY-1.0;
169         _maxX=_maxX+1.0;
170         _maxY=_maxY+1.0;
171
172 //EED 2017-03-02
173         if (_pts!=NULL) 
174         {
175                 _pts->Modified();
176         }
177 }
178
179 // ----------------------------------------------------------------------------
180 void manualViewContour::RefreshText()  // virtual
181 {
182
183         if ((_textActor!=NULL) && ( _textActor->GetProperty()->GetOpacity()!=0 )){
184                 int size = GetNumberOfPoints();
185                 char text[50];
186                 char resultText[250];
187                 strcpy(resultText, "000");
188                 char resultText2[250];
189                 strcpy(resultText2, "000");
190                 double spcP[3];
191                 spcP[0]=1;
192                 spcP[1]=1;
193                 spcP[2]=1;
194
195 //CMRU 19-08-09 ----------------------------------
196                 std::string label;
197                 label = _manContModel->GetLabel();
198 // ------------------------------------
199         
200                 if (size==2)
201                 {
202                         strcpy(resultText,"L= ");
203                         gcvt ( this->_manContModel->GetPathSize( spcP ), 5, text );
204                         strcat(resultText,text);
205                         strcat(resultText,"p");
206
207 //CMRU 19-08-09 ----------------------------------
208                         strcat(resultText," ");
209                         strcat(resultText,label.c_str());
210
211                         strcpy(resultText2,"L= ");
212                         gcvt ( this->_manContModel->GetPathSize( _spc ), 5, text );
213                         strcat(resultText2,text);
214                         strcat(resultText2,"u");
215
216 //------------------------------------------------
217                 }
218                 if (size>2)
219                 {
220                         if (_manContModel->IfCloseContour()==true)
221                         {
222                                 strcpy(resultText,"P= ");
223                                 gcvt ( this->_manContModel->GetPathSize( spcP ), 5, text );
224                                 strcat(resultText,text);
225                                 strcat(resultText,"p");
226
227                                 gcvt ( this->_manContModel->GetPathArea( spcP ), 5, text );
228                                 strcat(resultText,"   A= ");
229                                 strcat(resultText,text);
230                                 strcat(resultText,"p^2 ");
231
232
233                                 strcpy(resultText2,"P= ");
234                                 gcvt ( this->_manContModel->GetPathSize( _spc ), 5, text );
235                                 strcat(resultText2,text);
236                                 strcat(resultText2,"u");
237
238                                 gcvt ( this->_manContModel->GetPathArea( _spc ), 5, text );
239                                 strcat(resultText2,"   A= ");
240                                 strcat(resultText2,text);
241                                 strcat(resultText2,"u^2 ");
242
243                         } else {
244                                 strcpy(resultText,"L= ");
245                                 gcvt ( this->_manContModel->GetPathSize( spcP ), 5, text );
246                                 strcat(resultText,text);
247                                 strcat(resultText,"p");
248
249                                 strcpy(resultText2,"L= ");
250                                 gcvt ( this->_manContModel->GetPathSize( _spc ), 5, text );
251                                 strcat(resultText2,text);
252                                 strcat(resultText2,"u");
253
254                         }
255 //CMRU 19-08-09 ----------------------------------
256                         strcat(resultText,label.c_str());
257 //------------------------------------------------
258                 }
259
260                 _textActor->SetInput(resultText);
261                 _textActor2->SetInput(resultText2);
262                 
263                 if (size>=1){
264
265                         int i;
266                         for (i=0; i<size; i++)
267                         {
268                                 if (_lstViewPoints[i]->GetPosibleSelected()==true)
269                                 {
270                                         _id_viewPoint_for_text = i;
271                                 }
272                         }
273
274                         if (_id_viewPoint_for_text>=size) 
275                         {
276                                 _id_viewPoint_for_text=0;
277                         }
278                         
279                         double px = _manContModel->GetManualPoint(_id_viewPoint_for_text)->GetX();
280                         double py = _manContModel->GetManualPoint(_id_viewPoint_for_text)->GetY();
281
282                         //EED 27 sep 2006
283                         px=px*_spc[0];
284                         py=py*_spc[1];
285
286                         _textActor->SetPosition(px+GetRange()+1,py);
287                         _textActor2->SetPosition(px+GetRange()+1,py+2);
288                 }
289         }
290 }
291
292 // ----------------------------------------------------------------------------
293 bool manualViewContour::ifTouchContour(int x,int y,int z)
294 {
295         bool result=false;
296
297         double xx=x;
298         double yy=y;
299         double zz=z;
300         double ppA[3];
301         double ppB[3];
302         double d1,d2,d3;
303         TransfromCoordViewWorld(xx,yy,zz);
304
305 //EED 27 sep 2006
306         xx = xx * _spc[0];
307         yy = yy * _spc[1];
308         zz = zz * _spc[2];
309
310         if ( (xx>=_minX) &&  (yy>=_minY) && (xx<=_maxX) && (yy<=_maxY)) {  // inside the boundaring box
311         
312
313                 if (_manContModel->GetSizeLstPoints()==2)
314                 {
315                         manualPoint *mpA = _manContModel->GetManualPoint(0);
316                         manualPoint *mpB = _manContModel->GetManualPoint(1);
317                         _pts->GetPoint(0, ppA);
318                         double dirVec[3];
319
320                         // direction of the segment
321             dirVec[0] = mpB->GetX() - mpA->GetX();                      
322             dirVec[1] = mpB->GetY() - mpA->GetY();                      
323             dirVec[2] = mpB->GetZ() - mpA->GetZ();                      
324
325
326                         // proj = mpA + k*dirVec
327                         double num;
328                         double den = 0.0 ;
329                         num     =       dirVec[0]*( xx-mpA->GetX() );
330                         num     =       num + dirVec[1]*( yy-mpA->GetY() );
331                         num     =       num + dirVec[2]*( zz-mpA->GetZ() );
332                         den=dirVec[0]*dirVec[0] + dirVec[1]*dirVec[1] + dirVec[2]*dirVec[2];
333                         double k;
334                         if (den!=0) 
335                         {
336                                 k=num/den;
337                         } else {
338                                 k=99999999;
339                         }
340                         // projection of th point xx,yy,zz  in segment mpA,mpB
341                         double projX = mpA->GetX() + k*dirVec[0];
342                         double projY = mpA->GetY() + k*dirVec[1];
343                         double projZ = mpA->GetZ() + k*dirVec[2];
344
345                         // distance [projX,projY,projZ]   and   (xx,yy,zz]
346                         double d1= sqrt(  (projX-xx)*(projX-xx) + (projY-yy)*(projY-yy) +(projZ-zz)*(projZ-zz) );
347                         if (d1<=1)
348                         { 
349                                 result=true;
350                         } // if d1
351                 } else{
352                         unsigned int i, nps,nps_t;
353                         nps       = _sizePointsContour;
354                         if (this->_manContModel->IfCloseContour()==true)
355                         {
356                                 nps_t = nps;
357                         } else {
358                         nps_t = nps-1;
359                         } // if close
360                         for( i = 0; i < nps_t; i++ ) 
361                         {
362                                 _pts->GetPoint(i%nps, ppA);
363                                 _pts->GetPoint((i+1)%nps, ppB);
364                                 d1 = sqrt( (ppA[0]-xx)*(ppA[0]-xx) + (ppA[1]-yy)*(ppA[1]-yy) + (ppA[2]-zz)*(ppA[2]-zz));
365                                 d2 = sqrt( (ppB[0]-xx)*(ppB[0]-xx) + (ppB[1]-yy)*(ppB[1]-yy) + (ppB[2]-zz)*(ppB[2]-zz));
366                                 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]));
367                                 if (  ((d1+d2)>=d3) &&  ((d1+d2)<=d3*1.3) ) 
368                                 {
369                                         result=true;
370                                         i=nps;
371                                 } // if
372                         } // for i
373                 } // if GetSizeLstPoints()==2
374         } // if min max
375
376         return result;
377 }
378
379 // ----------------------------------------------------------------------------
380 void manualViewContour::DeletePoint(int id) // virtual
381 {
382         if (_lstViewPoints.size()>2)
383         {
384                 manualViewBaseContour::DeletePoint( id );
385         }
386 }
387 // ----------------------------------------------------------------------------
388
389 void manualViewContour::ClearPoint(int id)
390 {
391         manualViewBaseContour::DeletePoint( id );
392 }
393
394 //-------------------------------------------------------------------
395 void manualViewContour::InitMove(int x, int y, int z)
396 {
397         _initialConoturModel->DeleteAllPoints();
398
399         manualPoint *mp = NULL;
400         double XX=x;
401         double YY=y;
402         double ZZ=z;
403         TransfromCoordViewWorld(XX,YY,ZZ);
404
405         int i, manualPointsSZ = _manContModel->GetSizeLstPoints();
406         for ( i=0; i<manualPointsSZ; i++ )
407         {
408                 mp = _manContModel->GetManualPoint( i );
409                 this->_initialConoturModel->AddPoint( mp->GetX() - XX, mp->GetY() - YY, mp->GetZ() );
410         }
411 }
412
413 //-------------------------------------------------------------------
414 void manualViewContour::MoveContour(int x, int y, int z)
415 {
416         manualPoint *mpOrigin = NULL;
417         manualPoint *mpMoving = NULL;
418         double XX = x;
419         double YY = y;
420         double ZZ = z;
421
422         TransfromCoordViewWorld(XX,YY,ZZ);
423
424         int i, manualPointsSZ = _manContModel->GetSizeLstPoints();
425         for ( i=0; i<manualPointsSZ; i++ )
426         {
427                 mpOrigin = _manContModel->GetManualPoint( i );
428                 mpMoving = _initialConoturModel->GetManualPoint(i);
429                 mpOrigin->SetPoint( mpMoving->GetX()+XX, mpMoving->GetY() + YY, mpMoving->GetZ() );
430         }
431         UpdateViewPoints();
432 }
433
434 //-------------------------------------------------------------------
435 void manualViewContour::MoveContour(int horizontalUnits, int verticalUnits )
436 {
437         manualPoint *mpOrigin = NULL;
438
439         int i, manualPointsSZ = _manContModel->GetSizeLstPoints();
440         for ( i=0; i<manualPointsSZ; i++ )
441         {
442                 mpOrigin = _manContModel->GetManualPoint( i );
443                 mpOrigin->SetPoint( mpOrigin->GetX()+horizontalUnits, mpOrigin->GetY()+verticalUnits, mpOrigin->GetZ() );
444         }
445         UpdateViewPoints();
446 }