]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/wxVtkMPR2DView.cxx
5c9562ef3e54d8b2a5793e6ae0978d63efc24d58
[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) {
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) { 
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) { 
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
224
225                 if ((x!=_backX) || (y!=_backY) || (z!=_backZ)) {
226
227                         if (_direction==0) { 
228                                 _imageViewer2XYZ->SetXSlice( (int)(GetVtkmprbasedata()->GetX()) ); 
229                                 _ptsA->SetPoint(0, -x2, y1  , z );
230                                 _ptsA->SetPoint(1, -x2, y2  , z );
231                                 _ptsB->SetPoint(0, -x2, y   , z1);
232                                 _ptsB->SetPoint(1, -x2, y   , z2);
233                         }
234                         if (_direction==1) { 
235                                 _imageViewer2XYZ->SetYSlice( (int)(GetVtkmprbasedata()->GetY()) ); 
236                             _ptsA->SetPoint(0, x1 , y2 , z );
237                                 _ptsA->SetPoint(1, x2 , y2 , z );
238                                 _ptsB->SetPoint(0, x  , y2 , z1);
239                                 _ptsB->SetPoint(1, x  , y2 , z2);
240                         }
241                         if (_direction==2) { 
242                                 _imageViewer2XYZ->SetZSlice( (int)(GetVtkmprbasedata()->GetZ()) ); 
243                                 _imageViewer2XYZ->SetZSlice( (int)(GetVtkmprbasedata()->GetZ()) ); 
244                         //      _ptsA->SetPoint(0, x1 , y , -z2 );
245                         //      _ptsA->SetPoint(1, x2 , y , -z2 );
246                         //      _ptsB->SetPoint(0, x  , y1, -z2 );
247                         //      _ptsB->SetPoint(1, x  , y2, -z2 );
248                                 
249                                 _ptsA->SetPoint(0, x1 , y , z2 );
250                                 _ptsA->SetPoint(1, x2 , y , z2 );
251                                 _ptsB->SetPoint(0, x  , y1, z2 );
252                                 _ptsB->SetPoint(1, x  , y2, z2 );
253                         }
254                         _backX=x;
255                         _backY=y;
256                         _backZ=z;
257                 }
258                 wxVtkBaseView::Refresh();
259         }
260 }
261 //-------------------------------------------------------------------
262 int wxVtkMPR2DView::GetActualSlice()   // virtual
263 {
264         int result;
265         if (_direction==0) 
266         { 
267                 result = (int)(GetVtkmprbasedata()->GetX());
268         }
269         if (_direction==1) 
270         { 
271                 result = (int)(GetVtkmprbasedata()->GetY());
272         }
273         if (_direction==2) 
274         { 
275                 result = (int)(GetVtkmprbasedata()->GetZ());
276         }
277         return result;
278 }
279 //-------------------------------------------------------------------
280 void wxVtkMPR2DView::SetActualSlice(int slice)   // virtual
281 {
282         if (_direction==0) 
283         { 
284                 GetVtkmprbasedata()->SetX(slice);
285         }
286         if (_direction==1) 
287         { 
288                 GetVtkmprbasedata()->SetY(slice);
289         }
290         if (_direction==2) 
291         { 
292                 GetVtkmprbasedata()->SetZ(slice);
293         }
294 }
295 //-------------------------------------------------------------------
296 bool wxVtkMPR2DView::IfMouseTouchX(double x, double y, double z)
297 {
298         double delta=5;
299         bool result=false;
300         if (_direction==0) 
301         { 
302         }
303         if (_direction==1) 
304         { 
305                 if (( x<GetVtkmprbasedata()->GetX()+delta ) && ( x>GetVtkmprbasedata()->GetX()-delta ))
306                 {
307                         result = true;
308                 }
309         }
310         if (_direction==2) 
311         { 
312                 if (( x<GetVtkmprbasedata()->GetX()+delta ) && ( x>GetVtkmprbasedata()->GetX()-delta ))
313                 {
314                         result = true;
315                 }
316         }
317         return result;
318 }
319 //-------------------------------------------------------------------
320 bool wxVtkMPR2DView::IfMouseTouchY(double x, double y, double z)
321 {
322         double delta=5;
323         bool result=false;
324         if (_direction==0) 
325         { 
326                 if (( y<GetVtkmprbasedata()->GetY()+delta ) && ( y>GetVtkmprbasedata()->GetY()-delta ))
327                 {
328                         result = true;
329                 }
330         }
331         if (_direction==1) 
332         { 
333         }
334         if (_direction==2) 
335         { 
336                 if (( y<GetVtkmprbasedata()->GetY()+delta ) && ( y>GetVtkmprbasedata()->GetY()-delta ))
337                 {
338                         result = true;
339                 }
340         }
341         return result;
342 }
343 //-------------------------------------------------------------------
344 bool wxVtkMPR2DView::IfMouseTouchZ(double x, double y, double z)
345 {
346         double delta=5;
347         bool result=false;
348         if (_direction==0) 
349         { 
350                 if (( z<GetVtkmprbasedata()->GetZ()+delta ) && ( z>GetVtkmprbasedata()->GetZ()-delta ))
351                 {
352                         result = true;
353                 }
354         }
355         if (_direction==1) 
356         { 
357                 if (( z<GetVtkmprbasedata()->GetZ()+delta ) && ( z>GetVtkmprbasedata()->GetZ()-delta ))
358                 {
359                         result = true;
360                 }
361         }
362         if (_direction==2) 
363         { 
364         }
365         return result;
366 }
367 //-------------------------------------------------------------------
368 void wxVtkMPR2DView::MoveX(double x, double y, double z)
369 {
370         if (_direction==0) 
371         { 
372         }
373         if (_direction==1) 
374         { 
375                 GetVtkmprbasedata()->SetX(x);
376         }
377         if (_direction==2) 
378         { 
379                 GetVtkmprbasedata()->SetX(x);
380         }
381 }
382 //-------------------------------------------------------------------
383 void wxVtkMPR2DView::MoveY(double x, double y, double z)
384 {
385         if (_direction==0) 
386         { 
387                 GetVtkmprbasedata()->SetY(y);
388         }
389         if (_direction==1) 
390         { 
391         }
392         if (_direction==2) 
393         { 
394                 GetVtkmprbasedata()->SetY(y);
395         }
396 }
397 //-------------------------------------------------------------------
398 void wxVtkMPR2DView::MoveZ(double x, double y, double z)
399 {
400         if (_direction==0) 
401         { 
402                 GetVtkmprbasedata()->SetZ(z);
403         }
404         if (_direction==1) 
405         { 
406                 GetVtkmprbasedata()->SetZ(z);
407         }
408         if (_direction==2) 
409         { 
410         }
411 }
412 //-------------------------------------------------------------------
413 void wxVtkMPR2DView::ChangeAxisColor(double x, double y, double z)
414 {
415         double c1r=1,c1g=1,c1b=0;
416         double c2r=1,c2g=0,c2b=0;
417
418         if (_direction==0) 
419         { 
420                 if (IfMouseTouchY(x,y,z)==true)
421                 {
422                         _lineBActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
423                 } else {
424                         _lineBActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
425                 }
426                 if (IfMouseTouchZ(x,y,z)==true)
427                 {
428                         _lineAActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
429                 } else {
430                         _lineAActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
431                 }
432         }
433
434         if (_direction==1) 
435         { 
436                 if (IfMouseTouchX(x,y,z)==true)
437                 {
438                         _lineBActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
439                 } else {
440                         _lineBActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
441                 }
442                 if (IfMouseTouchZ(x,y,z)==true)
443                 {
444                         _lineAActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
445                 } else {
446                         _lineAActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
447                 }
448         }
449
450         if (_direction==2) 
451         { 
452                 if (IfMouseTouchX(x,y,z)==true)
453                 {
454                         _lineBActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
455                 } else {
456                         _lineBActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
457                 }
458                 if (IfMouseTouchY(x,y,z)==true)
459                 {
460                         _lineAActor->GetProperty()->SetDiffuseColor(c1r,c1g,c1b);
461                 } else {
462                         _lineAActor->GetProperty()->SetDiffuseColor(c2r,c2g,c2b);
463                 }
464         }
465         Refresh();
466 }
467 //-------------------------------------------------------------------
468 void wxVtkMPR2DView::TransfromeCoordViewWorld(double &X, double &Y, double &Z, int type) // virtual 
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 //}