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