]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/wxVtkMPR2DView.cxx
Bug #1373
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / wxVtkMPR2DView.cxx
1 #include "wxVtkMPR2DView.h"
2
3 #include "vtkInteractorStyleBaseView.h"
4
5 #include "vtkCellArray.h"
6 #ifdef WIN32
7 #include <mathdefs.h>
8 using namespace gtm;
9 #endif
10
11 wxVtkMPR2DView::wxVtkMPR2DView( wxWindow *parent, int direction)
12  :wxVtk2DBaseView(parent)
13 {
14         _backX                  = -99999;
15         _backY                  = -99999;
16         _backZ                  = -99999;
17         _direction              = direction;
18         _ptsA                   = NULL;
19         _lineAActor             = NULL;
20         _lineAMapper    = NULL;
21         _pdA                    = NULL;
22         _ptsB                   = NULL;
23         _lineBActor             = NULL;
24         _lineBMapper    = NULL;
25         _pdB                    = NULL;
26         _interactorstylemprview = NULL;
27 }
28
29 //-------------------------------------------------------------------
30 wxVtkMPR2DView::~wxVtkMPR2DView()
31 {
32         if (_ptsA!=NULL)            { _ptsA                 -> Delete(); }
33         if (_lineAActor!=NULL)  { _lineAActor   -> Delete(); }
34         if (_lineAMapper!=NULL) { _lineAMapper  -> Delete(); }
35         if (_pdA!=NULL)             { _pdA                  -> Delete(); }
36         if (_ptsB!=NULL)            { _ptsB                 -> Delete(); }
37         if (_lineBActor!=NULL)  { _lineBActor   -> Delete(); }
38         if (_lineBMapper!=NULL) { _lineBMapper  -> Delete(); }
39         if (_pdB!=NULL)             { _pdB                  -> Delete(); }
40 }
41 //-------------------------------------------------------------------
42 vtkMPRBaseData *wxVtkMPR2DView::GetVtkmprbasedata()
43 {
44         return (vtkMPRBaseData*)GetVtkBaseData();
45 }
46 //-------------------------------------------------------------------
47 void wxVtkMPR2DView::Configure()
48 {
49         wxVtk2DBaseView::Configure();
50         int x1,x2,y1,y2,z1,z2;
51         GetVtkmprbasedata()     -> GetDimensionExtention(&x1,&x2,&y1,&y2,&z1,&z2);
52         double spc[3];
53         vtkImageData* img =  GetVtkmprbasedata()->GetImageData();
54         if(_interactorstylemprview==NULL)
55         {
56                 _interactorstylemprview = new vtkInteractorStyleMPRView();
57                 ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _interactorstylemprview );
58         }
59
60         if(img!=NULL){
61                 double* origin = img->GetOrigin();
62                 img->GetExtent(x1,x2,y1,y2,z1,z2);
63                 img->GetSpacing(spc);
64                 x1 += origin[0];
65                 x2 += origin[0];
66                 y1 += origin[1];
67                 y2 += origin[1];
68                 z1 += origin[2];
69                 z2 += origin[2];
70                 x1 = (int)(x1*spc[0]);
71                 y1 = (int)(y1*spc[1]);
72                 z1 = (int)(z1*spc[2]);
73
74                 x2 = (int)(x2*spc[0]);
75                 y2 = (int)(y2*spc[1]);
76                 z2 = (int)(z2*spc[2]);
77
78                 _visibleAxis = true;
79         }
80
81         // Axe A
82         if(_lineAActor==NULL){
83                 _lineAActor = vtkActor::New();
84                 _lineAActor->GetProperty()->SetDiffuseColor(1,0,0);
85                 _lineAActor->GetProperty()->SetLineWidth(0.5);
86
87                 _ptsA = vtkPoints::New();
88                 _ptsA->SetNumberOfPoints(2);
89                 _ptsA->SetPoint(0, -1000        , -1000 , -1000 );
90                 _ptsA->SetPoint(1,  1000        ,  1000 ,  1000 );
91                 _pdA = vtkPolyData::New();
92                 _lineAMapper = vtkPolyDataMapper::New();
93                 _lineAMapper->SetInput(_pdA);
94                 _lineAMapper->ImmediateModeRenderingOn();
95                 _lineAActor->SetMapper(_lineAMapper);
96         }
97
98         vtkCellArray *linesA = vtkCellArray::New();
99         linesA->InsertNextCell(2);
100         linesA->InsertCellPoint(0);
101         linesA->InsertCellPoint(1);
102         _pdA->SetPoints( _ptsA );
103         _pdA->SetLines( linesA );
104         linesA->Delete();  //do not delete lines ??
105 //              _lineAActor->GetProperty()->BackfaceCullingOn();
106
107
108 // Axe B
109         if(_lineBActor==NULL){
110                 _lineBActor                                             =       vtkActor::New();
111                 _lineBActor->GetProperty()->SetDiffuseColor(1,0,0);
112                 _lineBActor->GetProperty()->SetLineWidth(0.5);
113                 _lineBMapper                                    =       vtkPolyDataMapper::New();
114                 _lineBActor->SetMapper(_lineBMapper);
115                 _ptsB = vtkPoints::New();
116                 _ptsB->SetNumberOfPoints(2);
117                 _ptsB->SetPoint(0, -1000        , -1000 , -1000 );
118                 _ptsB->SetPoint(1,  1000        ,  1000 ,  1000 );
119 //              _lineBActor->GetProperty()->BackfaceCullingOn();
120         }
121
122         vtkCellArray *linesB;
123         linesB = vtkCellArray::New();
124         linesB->InsertNextCell(2);
125         linesB->InsertCellPoint(0);
126         linesB->InsertCellPoint(1);
127         _pdB = vtkPolyData::New();
128         _pdB->SetPoints( _ptsB );
129         _pdB->SetLines( linesB );
130         linesB->Delete();  //do not delete lines ??
131         _lineBMapper->SetInput(_pdB);
132         _lineBMapper->ImmediateModeRenderingOn();
133         if(_imageViewer2XYZ){
134                 _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->AddActor( _lineAActor );
135                 _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->AddActor( _lineBActor );
136                 vtkCamera *camera =_imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->GetActiveCamera();
137
138         //EED 17Avril2009
139     //EED 21 mars 2012  FLIP problem  ..PLOP..
140
141                 if (_direction==0) {  // YZ
142                         camera->SetViewUp               (   0   ,    0          ,     1         );
143                         camera->SetPosition             ( 10000,(y1+y2)/2       , (z1+z2)/2     );
144                         camera->SetFocalPoint   (   0   , (y1+y2)/2     , (z1+z2)/2     );
145                         camera->SetParallelScale( (z2-z1)/3.0 );
146                 }
147
148                 if (_direction==1) { // XZ
149                         camera->SetViewUp               (       0               ,       0       ,       1               );
150                         camera->SetPosition             ((x1+x2)/2      , -10000        , (z1+z2)/2     );
151                         camera->SetFocalPoint   ((x1+x2)/2      ,   0   , (z1+z2)/2     );
152                         camera->SetParallelScale( (x2-x1)/3.0 );
153                 }
154
155                 if (_direction==2) {  // XY
156                         camera->SetViewUp               (       0               ,       -1              ,       0       );
157                         camera->SetPosition             ((x1+x2)/2      , (y1+y2)/2     , -10000);
158                         camera->SetFocalPoint   ((x1+x2)/2      , (y1+y2)/2     ,       0       );
159                         camera->SetParallelScale( (x2-x1)/3.0 );
160                 }
161
162 /*
163                 if (_direction==0) {    // YZ
164                         camera->SetViewUp               (   0   ,     1         ,     0         );
165                         camera->SetPosition             (  10000,(y1+y2)/2      , (z1+z2)/2     );
166                         camera->SetFocalPoint   (   0   , (y1+y2)/2     , (z1+z2)/2     );
167                         camera->SetParallelScale( (z2-z1)/3.0 );
168                 }
169
170                 if (_direction==1) {    // XZ
171                         camera->SetViewUp               (       0               ,       0       ,       -1              );
172                         camera->SetPosition             ((x1+x2)/2      , 10000 , (z1+z2)/2     );
173                         camera->SetFocalPoint   ((x1+x2)/2      ,   0   , (z1+z2)/2     );
174                         camera->SetParallelScale( (x2-x1)/3.0 );
175                 }
176
177                 if (_direction==2) {    // XY
178                         camera->SetViewUp               (       0               ,       1               ,       0       );
179                         camera->SetPosition             ((x1+x2)/2      , (y1+y2)/2     ,  10000);
180                         camera->SetFocalPoint   ((x1+x2)/2      , (y1+y2)/2     ,       0       );
181                         camera->SetParallelScale( (x2-x1)/3.0 );
182                 }
183 */
184
185         //      _imageViewer2XYZ->GetVtkImageViewer2()->SetColorWindow (160);
186         //      _imageViewer2XYZ->GetVtkImageViewer2()->SetColorLevel (800);
187         }
188 }
189
190 void wxVtkMPR2DView::SetVisibleAxis(bool ok)
191 {
192         if (ok!=_visibleAxis)
193         {
194                 _visibleAxis=ok;
195                 if (_visibleAxis==true)
196                 {
197                         _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->AddActor( _lineAActor );
198                         _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->AddActor( _lineBActor );
199                 }
200                 if (_visibleAxis==false)
201                 {
202                         _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->RemoveActor( _lineAActor );
203                         _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->RemoveActor( _lineBActor );
204                 }
205
206         }
207 }
208
209 //-------------------------------------------------------------------
210 void wxVtkMPR2DView::Refresh()
211 {
212         //wxVtk2DBaseView::Refresh();
213         int x1 = 0,x2 = 0,y1 = 0,y2 = 0,z1 = 0,z2 = 0;
214         int x = 0, y = 0, z = 0;
215         int xx = 0, yy = 0, zz = 0;
216         double *spc     = 0;
217         double *origin  = 0;
218         
219         double xx1,yy1,zz1,xx2,yy2,zz2;
220         
221         vtkImageData* img = GetVtkmprbasedata()->GetImageData();
222         if(img!=NULL){
223                 origin = img->GetOrigin();
224                 img->GetExtent(x1,x2,y1,y2,z1,z2);
225                 spc = img->GetSpacing();
226                 x1 += origin[0];
227                 x2 += origin[0];
228                 y1 += origin[1];
229                 y2 += origin[1];
230                 z1 += origin[2];
231                 z2 += origin[2];
232                 
233                 xx1 =           x1*spc[0];
234                 yy1 =           y1*spc[1];
235                 zz1 =           z1*spc[2];
236                 
237                 xx2 =           x2*spc[0];
238                 yy2 =           y2*spc[1];
239                 zz2 =           z2*spc[2];
240                 
241                 x1 = (int)(x1*spc[0]);
242                 y1 = (int)(y1*spc[1]);
243                 z1 = (int)(z1*spc[2]);
244                 
245                 x2      = (int)(x2*spc[0]);
246                 y2      = (int)(y2*spc[1]);
247                 z2      = (int)(z2*spc[2]);
248
249                 xx = (int)(GetVtkmprbasedata()->GetX());
250                 yy = (int)(GetVtkmprbasedata()->GetY());
251                 zz = (int)(GetVtkmprbasedata()->GetZ());
252
253                 x =  round(xx*spc[0]);
254                 y =  round(yy*spc[1]);
255                 z =  round(zz*spc[2]);
256                 
257                 if ((xx!=_backX) || (yy!=_backY) || (zz!=_backZ)) {
258
259                         if (_direction==0) {    // YZ
260                                 if(_imageViewer2XYZ)
261                                 {
262                                         _imageViewer2XYZ->SetXSlice( (int)(GetVtkmprbasedata()->GetX()) );
263                                 }
264 //EED 21 mars 2012  FLIP probleme  ..PLOP..
265                                 //_ptsA->SetPoint(0, x2, y1  , z );
266                                 //_ptsA->SetPoint(1, x2, y2  , z );
267                                 //_ptsB->SetPoint(0, x2, y   , z1);
268                                 //_ptsB->SetPoint(1, x2, y   , z2);
269                                 _ptsA->SetPoint(0, xx2, yy1  , z  );
270                                 _ptsA->SetPoint(1, xx2, yy2  , z  );
271                                 _ptsB->SetPoint(0, xx2, y    , zz1);
272                                 _ptsB->SetPoint(1, xx2, y    , zz2);
273                         }
274                         if (_direction==1) {    // XZ
275                                 if(_imageViewer2XYZ)
276                                 {
277                                         _imageViewer2XYZ->SetYSlice( (int)(GetVtkmprbasedata()->GetY()) );
278                                 }
279 //EED 21 mars 2012  FLIP probleme  ..PLOP..
280                             //_ptsA->SetPoint(0, x1 , y2 , z );
281                                 //_ptsA->SetPoint(1, x2 , y2 , z );
282                                 //_ptsB->SetPoint(0, x  , y2 , z1);
283                                 //_ptsB->SetPoint(1, x  , y2 , z2);
284                             _ptsA->SetPoint(0, xx1 , y1 , z  );
285                                 _ptsA->SetPoint(1, xx2 , y1 , z  );
286                                 _ptsB->SetPoint(0, x   , y1 , zz1);
287                                 _ptsB->SetPoint(1, x   , y1 , zz2);
288                         }
289                         if (_direction==2) {    // XY
290                                 if(_imageViewer2XYZ)
291                                 {
292                                         _imageViewer2XYZ->SetZSlice( (int)(GetVtkmprbasedata()->GetZ()) );
293                                 }
294                         //      _ptsA->SetPoint(0, x1 , y , -z2 );
295                         //      _ptsA->SetPoint(1, x2 , y , -z2 );
296                         //      _ptsB->SetPoint(0, x  , y1, -z2 );
297                         //      _ptsB->SetPoint(1, x  , y2, -z2 );
298
299                                 
300 //EED 21 mars 2012  FLIP probleme  ..PLOP..
301                                 //_ptsA->SetPoint(0, x1 , y , z2 );
302                                 //_ptsA->SetPoint(1, x2 , y , z2 );
303                                 //_ptsB->SetPoint(0, x  , y1, z2 );
304                                 //_ptsB->SetPoint(1, x  , y2, z2 );
305                                 
306                                 _ptsA->SetPoint(0, xx1 , y  , z1 );
307                                 _ptsA->SetPoint(1, xx2 , y  , z1 );
308                                 _ptsB->SetPoint(0, x   , yy1, z1 );
309                                 _ptsB->SetPoint(1, x   , yy2, z1 );
310                                 
311                                 
312                         }
313                         _backX=xx;
314                         _backY=yy;
315                         _backZ=zz;
316                 }
317
318 //EED 01nov2012
319                 UpdateColorWindowLevel();
320                 wxVtkBaseView::Refresh();
321         }
322 }
323 //-------------------------------------------------------------------
324 int wxVtkMPR2DView::GetActualSlice()   // virtual
325 {
326         int result;
327         if (_direction==0)
328         {
329                 result = (int)(GetVtkmprbasedata()->GetX());
330         }
331         if (_direction==1)
332         {
333                 result = (int)(GetVtkmprbasedata()->GetY());
334         }
335         if (_direction==2)
336         {
337                 result = (int)(GetVtkmprbasedata()->GetZ());
338         }
339         return result;
340 }
341 //-------------------------------------------------------------------
342 void wxVtkMPR2DView::SetActualSlice(int slice)   // virtual
343 {
344         if (_direction==0)
345         {
346                 GetVtkmprbasedata()->SetX(slice);
347         }
348         if (_direction==1)
349         {
350                 GetVtkmprbasedata()->SetY(slice);
351         }
352         if (_direction==2)
353         {
354                 GetVtkmprbasedata()->SetZ(slice);
355         }
356 }
357 //-------------------------------------------------------------------
358 bool wxVtkMPR2DView::IfMouseTouchX(double x, double y, double z)
359 {
360         double delta=5;
361         bool result=false;
362         if (_direction==0)
363         {
364         }
365         if (_direction==1)
366         {
367                 if (( x<GetVtkmprbasedata()->GetX()+delta ) && ( x>GetVtkmprbasedata()->GetX()-delta ))
368                 {
369                         result = true;
370                 }
371         }
372         if (_direction==2)
373         {
374                 if (( x<GetVtkmprbasedata()->GetX()+delta ) && ( x>GetVtkmprbasedata()->GetX()-delta ))
375                 {
376                         result = true;
377                 }
378         }
379         return result;
380 }
381 //-------------------------------------------------------------------
382 bool wxVtkMPR2DView::IfMouseTouchY(double x, double y, double z)
383 {
384         double delta=5;
385         bool result=false;
386         if (_direction==0)
387         {
388                 if (( y<GetVtkmprbasedata()->GetY()+delta ) && ( y>GetVtkmprbasedata()->GetY()-delta ))
389                 {
390                         result = true;
391                 }
392         }
393         if (_direction==1)
394         {
395         }
396         if (_direction==2)
397         {
398                 if (( y<GetVtkmprbasedata()->GetY()+delta ) && ( y>GetVtkmprbasedata()->GetY()-delta ))
399                 {
400                         result = true;
401                 }
402         }
403         return result;
404 }
405 //-------------------------------------------------------------------
406 bool wxVtkMPR2DView::IfMouseTouchZ(double x, double y, double z)
407 {
408         double delta=5;
409         bool result=false;
410         if (_direction==0)
411         {
412                 if (( z<GetVtkmprbasedata()->GetZ()+delta ) && ( z>GetVtkmprbasedata()->GetZ()-delta ))
413                 {
414                         result = true;
415                 }
416         }
417         if (_direction==1)
418         {
419                 if (( z<GetVtkmprbasedata()->GetZ()+delta ) && ( z>GetVtkmprbasedata()->GetZ()-delta ))
420                 {
421                         result = true;
422                 }
423         }
424         if (_direction==2)
425         {
426         }
427         return result;
428 }
429 //-------------------------------------------------------------------
430 void wxVtkMPR2DView::MoveX(double x, double y, double z)
431 {
432         if (_direction==0)
433         {
434         }
435         if (_direction==1)
436         {
437                 GetVtkmprbasedata()->SetX(x);
438         }
439         if (_direction==2)
440         {
441                 GetVtkmprbasedata()->SetX(x);
442         }
443 }
444 //-------------------------------------------------------------------
445 void wxVtkMPR2DView::MoveY(double x, double y, double z)
446 {
447         if (_direction==0)
448         {
449                 GetVtkmprbasedata()->SetY(y);
450         }
451         if (_direction==1)
452         {
453         }
454         if (_direction==2)
455         {
456                 GetVtkmprbasedata()->SetY(y);
457         }
458 }
459 //-------------------------------------------------------------------
460 void wxVtkMPR2DView::MoveZ(double x, double y, double z)
461 {
462         if (_direction==0)
463         {
464                 GetVtkmprbasedata()->SetZ(z);
465         }
466         if (_direction==1)
467         {
468                 GetVtkmprbasedata()->SetZ(z);
469         }
470         if (_direction==2)
471         {
472         }
473 }
474 //-------------------------------------------------------------------
475 void wxVtkMPR2DView::ChangeAxisColor(double x, double y, double z)
476 {
477         double c1r=1,c1g=1,c1b=0;
478         double c2r=1,c2g=0,c2b=0;
479
480         if (_direction==0)
481         {
482                 if (IfMouseTouchY(x,y,z)==true)
483                 {
484                         _lineBActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
485                 } else {
486                         _lineBActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
487                 }
488                 if (IfMouseTouchZ(x,y,z)==true)
489                 {
490                         _lineAActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
491                 } else {
492                         _lineAActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
493                 }
494         }
495
496         if (_direction==1)
497         {
498                 if (IfMouseTouchX(x,y,z)==true)
499                 {
500                         _lineBActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
501                 } else {
502                         _lineBActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
503                 }
504                 if (IfMouseTouchZ(x,y,z)==true)
505                 {
506                         _lineAActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
507                 } else {
508                         _lineAActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
509                 }
510         }
511
512         if (_direction==2)
513         {
514                 if (IfMouseTouchX(x,y,z)==true)
515                 {
516                         _lineBActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
517                 } else {
518                         _lineBActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
519                 }
520                 if (IfMouseTouchY(x,y,z)==true)
521                 {
522                         _lineAActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
523                 } else {
524                         _lineAActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
525                 }
526         }
527         Refresh();
528 }
529
530
531 //EED 5 juin 2009
532 //void wxVtkMPR2DView::TransfromCoordViewWorld(double &X, double &Y, double &Z, int type) // virtual
533 void wxVtkMPR2DView::TransFromCoordScreenToWorld(double &X, double &Y, double &Z, bool keepNormalDirection, int type) //virtual //keepNormalDirection=false, type=2
534 {
535
536         wxVtkBaseView::TransFromCoordScreenToWorld(X,Y,Z,keepNormalDirection,_direction);
537
538         if ((_direction==0) && (keepNormalDirection==true) )
539         {
540                 X = ((vtkMPRBaseData*)GetVtkBaseData())->GetX();
541         }
542
543         if ((_direction==1) && (keepNormalDirection==true) )
544         {
545                 Y = ((vtkMPRBaseData*)GetVtkBaseData())->GetY();
546         }
547
548         if ((_direction==2) && (keepNormalDirection==true) )
549         {
550                 Z = ((vtkMPRBaseData*)GetVtkBaseData())->GetZ();
551         }
552
553 }
554
555 //-------------------------------------------------------------------
556 int wxVtkMPR2DView::GetDirection() // virtual
557 {
558     return _direction;
559 }
560
561
562 //-------------------------------------------------------------------
563 //-------------------------------------------------------------------
564 //-------------------------------------------------------------------
565 //void boxVolumeObserver::Execute(vtkObject *wdg, unsigned long eventId, void* calldata) {  // virtual
566 //      if (eventId==vtkCommand::StartInteractionEvent){
567 //              _renWin->SetDesiredUpdateRate(10);
568 //      }
569 //      if (eventId==vtkCommand::InteractionEvent){
570 //              _renWin->SetDesiredUpdateRate(0.001);
571 //      }
572 //      if (eventId==vtkCommand::EndInteractionEvent){
573 //              vtkPlanes *planes = vtkPlanes::New();
574 //              vtkBoxWidget *boxwidget = reinterpret_cast<vtkBoxWidget*>(wdg);
575 //              boxwidget->GetPlanes(planes);
576 //              _volumeMapper->SetClippingPlanes(planes);
577 //              planes -> Delete();
578 //      }
579 //}
580
581 //-------------------------------------------------------------------
582 //void boxVolumeObserver::SetRenWin( vtkRenderWindow *renWin ){
583 //      _renWin = renWin;
584 //}
585 //-------------------------------------------------------------------
586 //void boxVolumeObserver::SetVolumeMapper(vtkVolumeRayCastMapper *volumeMapper){
587 //      _volumeMapper = volumeMapper;
588 //}