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