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