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