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