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 #include <IVtkVTK_View.hxx>
17 
18 // prevent disabling some MSVC warning messages by VTK headers
19 #ifdef _MSC_VER
20 #pragma warning(push)
21 #endif
22 #include <vtkAutoInit.h>
23 #include <vtkCamera.h>
24 #include <vtkRenderer.h>
25 #include <vtkRenderWindow.h>
26 #include <vtkTransform.h>
27 #ifdef _MSC_VER
28 #pragma warning(pop)
29 #endif
30 
IMPLEMENT_STANDARD_RTTIEXT(IVtkVTK_View,IVtk_IView)31 IMPLEMENT_STANDARD_RTTIEXT(IVtkVTK_View,IVtk_IView)
32 
33 // Initialization of VTK object factories.
34 // Since VTK 6 the factory methods require "auto-initialization" depending on
35 // what modules are enabled at VTK configure time.
36 // Some defines are needed in order to make the factories work properly.
37 #ifdef VTK_OPENGL2_BACKEND
38 VTK_MODULE_INIT(vtkRenderingOpenGL2)
39 #else
40 VTK_MODULE_INIT(vtkRenderingOpenGL)
41 #endif
42 VTK_MODULE_INIT(vtkInteractionStyle)
43 
44 // Handle implementation
45 
46 
47 //================================================================
48 // Function : Constructor
49 // Purpose  :
50 //================================================================
51 IVtkVTK_View::IVtkVTK_View (vtkRenderer* theRenderer)
52 : myRenderer (theRenderer)
53 { }
54 
55 //================================================================
56 // Function : Destructor
57 // Purpose  :
58 //================================================================
~IVtkVTK_View()59 IVtkVTK_View::~IVtkVTK_View()
60 { }
61 
62 //================================================================
63 // Function : IsPerspective
64 // Purpose  :
65 //================================================================
IsPerspective() const66 bool IVtkVTK_View::IsPerspective() const
67 {
68   return !myRenderer->GetActiveCamera()->GetParallelProjection();
69 }
70 
71 //================================================================
72 // Function : GetDistance
73 // Purpose  :
74 //================================================================
GetDistance() const75 double IVtkVTK_View::GetDistance() const
76 {
77   return myRenderer->GetActiveCamera()->GetDistance();
78 }
79 
80 //================================================================
81 // Function : GetEyePosition
82 // Purpose  :
83 //================================================================
GetEyePosition(double & theX,double & theY,double & theZ) const84 void IVtkVTK_View::GetEyePosition (double& theX, double& theY, double& theZ) const
85 {
86   myRenderer->GetActiveCamera()->GetPosition (theX, theY, theZ);
87 }
88 
89 //================================================================
90 // Function : GetPosition
91 // Purpose  :
92 //================================================================
GetPosition(double & theX,double & theY,double & theZ) const93 void IVtkVTK_View::GetPosition (double& theX, double& theY, double& theZ) const
94 {
95   myRenderer->GetActiveCamera()->GetFocalPoint (theX, theY, theZ);
96 }
97 
98 //================================================================
99 // Function : GetViewUp
100 // Purpose  :
101 //================================================================
GetViewUp(double & theDx,double & theDy,double & theDz) const102 void IVtkVTK_View::GetViewUp (double& theDx, double& theDy, double& theDz) const
103 {
104   myRenderer->GetActiveCamera()->OrthogonalizeViewUp();
105   myRenderer->GetActiveCamera()->GetViewUp (theDx, theDy, theDz);
106 }
107 
108 //================================================================
109 // Function : GetDirectionOfProjection
110 // Purpose  :
111 //================================================================
GetDirectionOfProjection(double & theDx,double & theDy,double & theDz) const112 void IVtkVTK_View::GetDirectionOfProjection (double& theDx,
113                                              double& theDy,
114                                              double& theDz) const
115 {
116   myRenderer->GetActiveCamera()->GetDirectionOfProjection (theDx, theDy, theDz);
117   theDx = -theDx;
118   theDy = -theDy;
119   theDz = -theDz;
120 }
121 
122 //================================================================
123 // Function : GetScale
124 // Purpose  :
125 //================================================================
GetScale(double & theX,double & theY,double & theZ) const126 void IVtkVTK_View::GetScale (double& theX, double& theY, double& theZ) const
127 {
128   double aScale[3];
129   myRenderer->GetActiveCamera()->GetViewTransformObject()->GetScale (aScale);
130   theX = aScale[0];
131   theY = aScale[1];
132   theZ = aScale[2];
133 }
134 
135 //================================================================
136 // Function : GetParallelScale
137 // Purpose  :
138 //================================================================
GetParallelScale() const139 double IVtkVTK_View::GetParallelScale() const
140 {
141   return myRenderer->GetActiveCamera()->GetParallelScale();
142 }
143 
144 //================================================================
145 // Function : GetViewAngle
146 // Purpose  :
147 //================================================================
GetViewAngle() const148 double IVtkVTK_View::GetViewAngle() const
149 {
150   return myRenderer->GetActiveCamera()->GetViewAngle();
151 }
152 
153 //================================================================
154 // Function : GetAspectRatio
155 // Purpose  :
156 //================================================================
GetAspectRatio() const157 double IVtkVTK_View::GetAspectRatio() const
158 {
159   return myRenderer->GetTiledAspectRatio();
160 }
161 
162 //================================================================
163 // Function : GetClippingRange
164 // Purpose  :
165 //================================================================
GetClippingRange(double & theZNear,double & theZFar) const166 void IVtkVTK_View::GetClippingRange (double& theZNear, double& theZFar) const
167 {
168   myRenderer->GetActiveCamera()->GetClippingRange (theZNear, theZFar);
169 }
170 
171 //================================================================
172 // Function : GetViewCenter
173 // Purpose  :
174 //================================================================
GetViewCenter(double & theX,double & theY) const175 void IVtkVTK_View::GetViewCenter (double& theX, double& theY) const
176 {
177   double* aCenter = myRenderer->GetCenter();
178   theX = aCenter[0];
179   theY = aCenter[1];
180 }
181 
182 //================================================================
183 // Function : DisplayToWorld
184 // Purpose  :
185 //================================================================
DisplayToWorld(const gp_XY & theDisplayPnt,gp_XYZ & theWorldPnt) const186 bool IVtkVTK_View::DisplayToWorld (const gp_XY& theDisplayPnt, gp_XYZ& theWorldPnt) const
187 {
188   // Convert the selection point into world coordinates.
189   myRenderer->SetDisplayPoint (theDisplayPnt.X(), theDisplayPnt.Y(), 0.0);
190   myRenderer->DisplayToWorld();
191 
192   double* const aCoords = myRenderer->GetWorldPoint();
193   if (aCoords[3] == 0.0) // Point at infinity in homogeneous coordinates
194   {
195     return false;
196   }
197 
198   theWorldPnt = gp_XYZ (aCoords[0] / aCoords[3],
199     aCoords[1] / aCoords[3], aCoords[2] / aCoords[3]);
200 
201   return true;
202 }
203 
204 //================================================================
205 // Function : GetWindowSize
206 // Purpose  :
207 //================================================================
GetWindowSize(int & theX,int & theY) const208 void IVtkVTK_View::GetWindowSize (int& theX, int& theY) const
209 {
210   int* aSize = myRenderer->GetRenderWindow()->GetSize();
211   theX = aSize[0];
212   theY = aSize[1];
213 }
214 
215 //================================================================
216 // Function : GetCamera
217 // Purpose  :
218 //================================================================
GetCamera(Graphic3d_Mat4d & theProj,Graphic3d_Mat4d & theOrient,Standard_Boolean & theIsOrtho) const219 void IVtkVTK_View::GetCamera (Graphic3d_Mat4d& theProj,
220                               Graphic3d_Mat4d& theOrient,
221                               Standard_Boolean& theIsOrtho) const
222 {
223   theIsOrtho = !IsPerspective();
224 
225   vtkMatrix4x4* aCompositeProj =
226     myRenderer->GetActiveCamera()->
227     GetCompositeProjectionTransformMatrix (myRenderer->GetTiledAspectRatio(),
228                                            0,
229                                            1);
230   for (Standard_Integer aRow = 0; aRow < 4; ++aRow)
231   {
232     for (Standard_Integer aCol = 0; aCol < 4; ++aCol)
233     {
234       theProj.SetValue (aRow, aCol, aCompositeProj->GetElement (aRow, aCol));
235     }
236   }
237 
238   theOrient.InitIdentity();
239 }
240 
241 //================================================================
242 // Function : GetViewport
243 // Purpose  :
244 //================================================================
GetViewport(Standard_Real & theX,Standard_Real & theY,Standard_Real & theWidth,Standard_Real & theHeight) const245 void IVtkVTK_View::GetViewport (Standard_Real& theX,
246                                 Standard_Real& theY,
247                                 Standard_Real& theWidth,
248                                 Standard_Real& theHeight) const
249 {
250   Standard_Real aViewport[4];
251   myRenderer->GetViewport (aViewport);
252   theX = aViewport[0];
253   theY = aViewport[1];
254   theWidth  = aViewport[2];
255   theHeight = aViewport[3];
256 }
257