0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / IVtkVTK / IVtkVTK_View.cxx
1 // Created on: 2011-10-14 
2 // Created by: Roman KOZLOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS 
4 // 
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <IVtkVTK_View.hxx>
18 #include <vtkAutoInit.h>
19 #include <vtkCamera.h>
20 #include <vtkRenderer.h>
21 #include <vtkRenderWindow.h>
22 #include <vtkTransform.h>
23
24 IMPLEMENT_STANDARD_RTTIEXT(IVtkVTK_View,IVtk_IView)
25
26 // Initialization of VTK object factories.
27 // Since VTK 6 the factory methods require "auto-initialization" depending on
28 // what modules are enabled at VTK configure time.
29 // Some defines are needed in order to make the factories work properly.
30 #ifdef VTK_OPENGL2_BACKEND
31 VTK_MODULE_INIT(vtkRenderingOpenGL2)
32 #else
33 VTK_MODULE_INIT(vtkRenderingOpenGL)
34 #endif
35 VTK_MODULE_INIT(vtkInteractionStyle)
36
37 // Handle implementation
38
39
40 //================================================================
41 // Function : Constructor
42 // Purpose  : 
43 //================================================================
44 IVtkVTK_View::IVtkVTK_View (vtkRenderer* theRenderer)
45 : myRenderer (theRenderer)
46 { }
47
48 //================================================================
49 // Function : Destructor
50 // Purpose  : 
51 //================================================================
52 IVtkVTK_View::~IVtkVTK_View()
53 { }
54
55 //================================================================
56 // Function : IsPerspective
57 // Purpose  : 
58 //================================================================
59 bool IVtkVTK_View::IsPerspective() const
60 {
61   return !myRenderer->GetActiveCamera()->GetParallelProjection();
62 }
63
64 //================================================================
65 // Function : GetDistance
66 // Purpose  : 
67 //================================================================
68 double IVtkVTK_View::GetDistance() const
69 {
70   return myRenderer->GetActiveCamera()->GetDistance();
71 }
72
73 //================================================================
74 // Function : GetPosition
75 // Purpose  : 
76 //================================================================
77 void IVtkVTK_View::GetPosition (double& theX, double& theY, double& theZ) const
78 {
79   myRenderer->GetActiveCamera()->GetFocalPoint (theX, theY, theZ);
80 }
81
82 //================================================================
83 // Function : GetViewUp
84 // Purpose  : 
85 //================================================================
86 void IVtkVTK_View::GetViewUp (double& theDx, double& theDy, double& theDz) const
87 {
88   myRenderer->GetActiveCamera()->OrthogonalizeViewUp();
89   myRenderer->GetActiveCamera()->GetViewUp (theDx, theDy, theDz);
90 }
91
92 //================================================================
93 // Function : GetDirectionOfProjection
94 // Purpose  : 
95 //================================================================
96 void IVtkVTK_View::GetDirectionOfProjection (double& theDx,
97                                              double& theDy,
98                                              double& theDz) const
99 {
100   myRenderer->GetActiveCamera()->GetDirectionOfProjection (theDx, theDy, theDz);
101   theDx = -theDx;
102   theDy = -theDy;
103   theDz = -theDz;
104 }
105
106 //================================================================
107 // Function : GetScale
108 // Purpose  : 
109 //================================================================
110 void IVtkVTK_View::GetScale (double& theX, double& theY, double& theZ) const
111 {
112   double aScale[3];
113   myRenderer->GetActiveCamera()->GetViewTransformObject()->GetScale (aScale);
114   theX = aScale[0];
115   theY = aScale[1];
116   theZ = aScale[2];
117 }
118
119 //================================================================
120 // Function : GetParallelScale
121 // Purpose  : 
122 //================================================================
123 double IVtkVTK_View::GetParallelScale() const
124 {
125   return myRenderer->GetActiveCamera()->GetParallelScale();
126 }
127
128 //================================================================
129 // Function : GetViewAngle
130 // Purpose  : 
131 //================================================================
132 double IVtkVTK_View::GetViewAngle() const
133 {
134   return myRenderer->GetActiveCamera()->GetViewAngle();
135 }
136
137 //================================================================
138 // Function : GetViewCenter
139 // Purpose  : 
140 //================================================================
141 void IVtkVTK_View::GetViewCenter (double& theX, double& theY) const
142 {
143   double* aCenter = myRenderer->GetCenter();
144   theX = aCenter[0];
145   theY = aCenter[1];
146 }
147
148 //================================================================
149 // Function : DisplayToWorld
150 // Purpose  : 
151 //================================================================
152 bool IVtkVTK_View::DisplayToWorld (const gp_XY& theDisplayPnt, gp_XYZ& theWorldPnt) const
153 {
154   // Convert the selection point into world coordinates.
155   myRenderer->SetDisplayPoint (theDisplayPnt.X(), theDisplayPnt.Y(), 0.0);
156   myRenderer->DisplayToWorld();
157
158   double* const aCoords = myRenderer->GetWorldPoint();
159   if (aCoords[3] == 0.0) // Point at infinity in homogeneous coordinates
160   {
161     return false;
162   }
163
164   theWorldPnt = gp_XYZ (aCoords[0] / aCoords[3], 
165     aCoords[1] / aCoords[3], aCoords[2] / aCoords[3]);
166
167   return true;
168 }
169
170 //================================================================
171 // Function : GetWindowSize
172 // Purpose  :
173 //================================================================
174 void IVtkVTK_View::GetWindowSize (int& theX, int& theY) const
175 {
176   int* aSize = myRenderer->GetRenderWindow()->GetSize();
177   theX = aSize[0];
178   theY = aSize[1];
179 }
180
181 //================================================================
182 // Function : GetCamera
183 // Purpose  :
184 //================================================================
185 void IVtkVTK_View::GetCamera (Graphic3d_Mat4d& theProj,
186                               Graphic3d_Mat4d& theOrient,
187                               Standard_Boolean& theIsOrtho) const
188 {
189   theIsOrtho = !IsPerspective();
190
191   vtkMatrix4x4* aCompositeProj =
192     myRenderer->GetActiveCamera()->
193     GetCompositeProjectionTransformMatrix (myRenderer->GetTiledAspectRatio(),
194                                            0,
195                                            1);
196   for (Standard_Integer aRow = 0; aRow < 4; ++aRow)
197   {
198     for (Standard_Integer aCol = 0; aCol < 4; ++aCol)
199     {
200       theProj.SetValue (aRow, aCol, aCompositeProj->GetElement (aRow, aCol));
201     }
202   }
203
204   theOrient.InitIdentity();
205 }
206
207 //================================================================
208 // Function : GetViewport
209 // Purpose  :
210 //================================================================
211 void IVtkVTK_View::GetViewport (Standard_Real& theX,
212                                 Standard_Real& theY,
213                                 Standard_Real& theWidth,
214                                 Standard_Real& theHeight) const
215 {
216   Standard_Real aViewport[4];
217   myRenderer->GetViewport (aViewport);
218   theX = aViewport[0];
219   theY = aViewport[1];
220   theWidth  = aViewport[2];
221   theHeight = aViewport[3];
222 }