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