]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/wxVtk3DBaseView.cxx
Clean code
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / wxVtk3DBaseView.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 #include "wxVtk3DBaseView.h"
27
28 #include "vtkInteractorStyleBaseView3D.h"
29
30 #ifdef _DEBUG
31 #define new DEBUG_NEW
32 #endif
33 wxVtk3DBaseView::wxVtk3DBaseView(wxWindow *parent, vtkBaseData* vtkbasedata,bool TDxWindow)
34 :wxVtkBaseView( parent , TDxWindow)
35 {
36         SetVtkBaseData( vtkbasedata );
37         _configure      = false;
38         _aRenderer      = NULL;
39         _renWin         = NULL;
40         _aCamera        = NULL;
41 }
42 //-------------------------------------------------------------------
43 wxVtk3DBaseView::~wxVtk3DBaseView()
44 {
45         if (_aCamera!=NULL)             { _aCamera              -> Delete(); _aCamera=NULL;}
46         if (_aRenderer!=NULL)   { _aRenderer    -> Delete(); _aRenderer=NULL;}
47         if (_renWin!=NULL)              
48         { 
49                 if(_renWin->GetReferenceCount()==0)
50                 {
51                         _renWin->Delete(); 
52                         _renWin=NULL;
53                 }
54         }
55 }
56 //-------------------------------------------------------------------
57 vtkCamera* wxVtk3DBaseView::GetCamera()
58 {
59         return _aCamera;
60 }
61 //-------------------------------------------------------------------
62 vtkRenderer* wxVtk3DBaseView::GetRenderer()  // virtual
63 {
64         return _aRenderer;
65 }
66 //-------------------------------------------------------------------
67 vtkRenderWindow* wxVtk3DBaseView::GetRenWin()  // virtual
68 {
69         return _renWin;
70 }
71 //-------------------------------------------------------------------
72 void wxVtk3DBaseView::Refresh()
73 {
74 //      _renWin->Render();
75         vtkRenderWindowInteractor *vri = GetWxVTKRenderWindowInteractor();
76         vri->vtkRenderWindowInteractor::Render();
77 }
78
79 //-------------------------------------------------------------------
80 // EED 27/05/2013
81 void wxVtk3DBaseView::ResetCamera(int* ext, double* spc)
82 {
83     GetRenderer()->ResetCameraClippingRange();
84         if(ext == NULL)
85         {
86                 GetRenderer()->ResetCamera ();
87                 GetCamera()->Dolly(1.2);
88         }else{  
89                 /*double x = (spc[0])*(origin[0]+(((double)ext[1]-(double)ext[0])/2.0));
90                 double y = (spc[1])*(origin[1]+(double)ext[3]);
91                 double z = (spc[2])*(origin[2]+(((double)ext[5]-(double)ext[4])/2.0));*/
92                 /*double x0=(spc[0])*((double)ext[0]+origin[0]);
93                 double x1=(spc[0])*((double)ext[1]+origin[0]);
94                 double y0=(spc[1])*((double)ext[2]+origin[1]);
95                 double y1=(spc[1])*((double)ext[3]+origin[1]);
96                 double z0=(spc[2])*((double)ext[4]+origin[2]);
97                 double z1=(spc[2])*((double)ext[5]+origin[2]);*/
98                 double x0=(spc[0])*((double)ext[0]);
99                 double x1=(spc[0])*((double)ext[1]);
100                 double y0=(spc[1])*((double)ext[2]);
101                 double y1=(spc[1])*((double)ext[3]);
102                 double z0=(spc[2])*((double)ext[4]);
103                 double z1=(spc[2])*((double)ext[5]);
104                 GetRenderer()->ResetCamera(x0,x1,y0,y1,z0,z1);
105                 //_wxvtk3Dbaseview->GetCamera()->SetPosition(x,y,z);
106                 GetCamera()->Dolly(1.5);        
107         }// if ext
108 }
109
110 //-------------------------------------------------------------------
111 void wxVtk3DBaseView::SetStereo(int type)
112 {
113         //EED 02/06/2012  
114         if (_renWin!=NULL)
115         {
116                 if (type==0)
117                 {
118                         _renWin->SetStereoRender(0); // OFF
119                 } else {
120                         _renWin->SetStereoRender(1); // ON
121                         _renWin->SetStereoCapableWindow(1);
122                         _renWin->SetAnaglyphColorMask(4,3);
123                         _renWin->SetAnaglyphColorSaturation(0.65);
124                 
125                         // VTK_STEREO_CRYSTAL_EYES 1
126                         // VTK_STEREO_RED_BLUE     2
127                         // VTK_STEREO_INTERLACED   3
128                         // VTK_STEREO_LEFT         4
129                         // VTK_STEREO_RIGHT        5
130                         // VTK_STEREO_DRESDEN      6
131                         // VTK_STEREO_ANAGLYPH     7
132             // VTK_STEREO_CHECKERBOARD 8
133             // VTK_STEREO_SPLITVIEWPORT_HORIZONTAL 9
134             // VTK_STEREO_FAKE 10
135                         // VTK_STEREO_EMULATE 11
136             
137                         _renWin->SetStereoType(type);
138
139                 } // if type
140         } // _renWin
141 }
142
143
144 //-------------------------------------------------------------------
145 void wxVtk3DBaseView::Configure()
146 {
147         if (_configure==false)
148         {
149                 _configure=true;
150                 //wxVtkBaseView::Configure();
151                 _aRenderer      = vtkRenderer::New();
152                 _renWin         = vtkRenderWindow::New();
153         
154         
155         printf("EED wxVtk3DBaseView::Configure >>>>> vtkRenderWindow IsDirect       %d\n", _renWin->IsDirect() );
156         
157         /* EED 2022-07-26  Segmentation fault with VTK9.2 Macos
158         int supportopengl=_renWin->SupportsOpenGL();
159         printf("EED wxVtk3DBaseView::Configure >>>>> vtkRenderWindow SupportsOpenGL %d\n", supportopengl );
160         if (supportopengl==0)
161         {
162 #if defined(_WIN32)
163             printf("   OpenGL 3 not detected.\n    Try to install opengl drivers or use de opengl-mesa version.\n    Copy from <ProgramsFiles>\\CreaTools\\crea_TPdlls-4.0.0\\bin\\opengl-mesa\n    the file: opengl32.dll\n to    <ProgramsFiles>\\CreaTools\\CreaTools-3.0.0\\bin \n");
164 #else
165             printf("   OpenGL 3 not detected.\n    Try to install opengl drivers or use de opengl-mesa version.\n");
166 #endif
167             exit(0);
168         }
169         */
170
171         
172         
173                 _renWin->AddRenderer(_aRenderer);
174
175                 _aRenderer->GradientBackgroundOn();
176                 _aRenderer->SetBackground( 0.33 , 0.33 , 0.33 );
177                 _aRenderer->SetBackground2( 0.66 , 0.66 , 0.66 );
178
179 //              _aRenderer->GradientBackgroundOff();
180 //              _aRenderer->SetBackground(0,0,0 );
181                 
182                 SetStereo(0);
183                 
184                 GetWxVTKRenderWindowInteractor()->SetRenderWindow(_renWin);
185
186                 vtkInteractorStyleBaseView3D *interactorStyleBaseView3D = vtkInteractorStyleBaseView3D::New(); 
187
188                 SetInteractorStyleBaseView( interactorStyleBaseView3D );
189                 interactorStyleBaseView3D->SetInteractor (  GetWxVTKRenderWindowInteractor() );
190                 GetWxVTKRenderWindowInteractor()->SetInteractorStyle( interactorStyleBaseView3D );
191                 interactorStyleBaseView3D->SetwxVtkBaseView(this);      
192
193                 // It is convenient to create an initial view of the data. The
194                 // FocalPoint and Position form a vector direction. Later on
195                 // (ResetCamera() method) this vector is used to position the camera
196                 // to look at the data in this direction.
197                 _aCamera = vtkCamera::New();
198
199 //EED 17Avril2009       
200 /*
201                 _aCamera->SetViewUp (0, 0, -1);
202                 _aCamera->SetPosition (0, 1, 0);
203  */
204                 _aCamera->SetViewUp (0, 1, 0);
205                 _aCamera->SetPosition (0, 0, 1);
206                 
207                 _aCamera->SetFocalPoint (0, 0, 0);
208                 _aCamera->ComputeViewPlaneNormal();
209
210 //EED 27/05/2013
211                 GetRenderer()->SetActiveCamera(GetCamera());
212                 
213 //EED 21 mars 2012  FLIP probleme  ..PLOP..
214 //EED 13 may 2013  FLIP probleme  ..PLOP..
215                 GetCamera()->Roll(180);
216                 GetCamera()->Yaw(180+30);
217                 GetCamera()->Pitch(-22);
218                                 
219                 //https://stackoverflow.com/questions/47528086/problems-with-rendering-transparent-objects-in-vtk
220                 //https://itk.org/Wiki/VTK/Depth_Peeling
221
222 // EED 2022-08-24
223 //              _aRenderer->SetUseDepthPeeling(1);
224 //              _aRenderer->SetOcclusionRatio(0.1);
225 //        _aRenderer->SetMaximumNumberOfPeels(100);
226 //              _aRenderer->SetMaximumNumberOfPeels(4);
227         
228         _aRenderer->UseDepthPeelingOn();
229         _aRenderer->UseDepthPeelingForVolumesOn();
230
231                 _renWin->SetMultiSamples(0);
232                 _renWin->SetAlphaBitPlanes(1);
233         
234 //EED 2021-11-3
235 // The Depth Peeling method have a bug in MacOs
236 // In vtkDualDepthPeelingPass::Peel()   TranslucentWrittenPixels are incoherent  TOO BIG  some times. Why ??
237 // So the comparition in vtkDualDepthPeelingPass::PeelingDone()  is not coherent.
238 //   while (!this->PeelingDone())
239 //        {
240 //          this->Peel();
241 //        }
242 // Or CurrentPeel >= MaximumNumberOfPeels || TranslucentWrittenPixels <= OcclusionThreshold not coherent. Why ??
243
244  //EED 2021-07-25
245 //        _aRenderer->UseDepthPeelingOff();
246
247         
248 /*
249 _renWin -> StereoRenderOn ();
250 _renWin -> StereoCapableWindowOn ();
251 _renWin -> SetStereoTypeToSplitViewportHorizontal();
252 */
253         
254         
255         } // if _configure
256     
257 }
258
259 //-------------------------------------------------------------------
260 //EED 27 sep 2006
261 void wxVtk3DBaseView::GetSpacing(double spc[3])
262 {
263         spc[0]=1;
264         spc[1]=1;
265         spc[2]=1;
266 }
267