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