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