1 // Created on: 2011-10-14
2 // Created by: Roman KOZLOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <IVtkVTK_View.hxx>
19 // prevent disabling some MSVC warning messages by VTK headers
23 #include <vtkAutoInit.h>
24 #include <vtkCamera.h>
25 #include <vtkRenderer.h>
26 #include <vtkRenderWindow.h>
27 #include <vtkTransform.h>
32 IMPLEMENT_STANDARD_RTTIEXT(IVtkVTK_View,IVtk_IView)
34 // Initialization of VTK object factories.
35 // Since VTK 6 the factory methods require "auto-initialization" depending on
36 // what modules are enabled at VTK configure time.
37 // Some defines are needed in order to make the factories work properly.
38 #ifdef VTK_OPENGL2_BACKEND
39 VTK_MODULE_INIT(vtkRenderingOpenGL2)
41 VTK_MODULE_INIT(vtkRenderingOpenGL)
43 VTK_MODULE_INIT(vtkInteractionStyle)
45 // Handle implementation
48 //================================================================
49 // Function : Constructor
51 //================================================================
52 IVtkVTK_View::IVtkVTK_View (vtkRenderer* theRenderer)
53 : myRenderer (theRenderer)
56 //================================================================
57 // Function : Destructor
59 //================================================================
60 IVtkVTK_View::~IVtkVTK_View()
63 //================================================================
64 // Function : IsPerspective
66 //================================================================
67 bool IVtkVTK_View::IsPerspective() const
69 return !myRenderer->GetActiveCamera()->GetParallelProjection();
72 //================================================================
73 // Function : GetDistance
75 //================================================================
76 double IVtkVTK_View::GetDistance() const
78 return myRenderer->GetActiveCamera()->GetDistance();
81 //================================================================
82 // Function : GetPosition
84 //================================================================
85 void IVtkVTK_View::GetPosition (double& theX, double& theY, double& theZ) const
87 myRenderer->GetActiveCamera()->GetFocalPoint (theX, theY, theZ);
90 //================================================================
91 // Function : GetViewUp
93 //================================================================
94 void IVtkVTK_View::GetViewUp (double& theDx, double& theDy, double& theDz) const
96 myRenderer->GetActiveCamera()->OrthogonalizeViewUp();
97 myRenderer->GetActiveCamera()->GetViewUp (theDx, theDy, theDz);
100 //================================================================
101 // Function : GetDirectionOfProjection
103 //================================================================
104 void IVtkVTK_View::GetDirectionOfProjection (double& theDx,
108 myRenderer->GetActiveCamera()->GetDirectionOfProjection (theDx, theDy, theDz);
114 //================================================================
115 // Function : GetScale
117 //================================================================
118 void IVtkVTK_View::GetScale (double& theX, double& theY, double& theZ) const
121 myRenderer->GetActiveCamera()->GetViewTransformObject()->GetScale (aScale);
127 //================================================================
128 // Function : GetParallelScale
130 //================================================================
131 double IVtkVTK_View::GetParallelScale() const
133 return myRenderer->GetActiveCamera()->GetParallelScale();
136 //================================================================
137 // Function : GetViewAngle
139 //================================================================
140 double IVtkVTK_View::GetViewAngle() const
142 return myRenderer->GetActiveCamera()->GetViewAngle();
145 //================================================================
146 // Function : GetViewCenter
148 //================================================================
149 void IVtkVTK_View::GetViewCenter (double& theX, double& theY) const
151 double* aCenter = myRenderer->GetCenter();
156 //================================================================
157 // Function : DisplayToWorld
159 //================================================================
160 bool IVtkVTK_View::DisplayToWorld (const gp_XY& theDisplayPnt, gp_XYZ& theWorldPnt) const
162 // Convert the selection point into world coordinates.
163 myRenderer->SetDisplayPoint (theDisplayPnt.X(), theDisplayPnt.Y(), 0.0);
164 myRenderer->DisplayToWorld();
166 double* const aCoords = myRenderer->GetWorldPoint();
167 if (aCoords[3] == 0.0) // Point at infinity in homogeneous coordinates
172 theWorldPnt = gp_XYZ (aCoords[0] / aCoords[3],
173 aCoords[1] / aCoords[3], aCoords[2] / aCoords[3]);
178 //================================================================
179 // Function : GetWindowSize
181 //================================================================
182 void IVtkVTK_View::GetWindowSize (int& theX, int& theY) const
184 int* aSize = myRenderer->GetRenderWindow()->GetSize();
189 //================================================================
190 // Function : GetCamera
192 //================================================================
193 void IVtkVTK_View::GetCamera (Graphic3d_Mat4d& theProj,
194 Graphic3d_Mat4d& theOrient,
195 Standard_Boolean& theIsOrtho) const
197 theIsOrtho = !IsPerspective();
199 vtkMatrix4x4* aCompositeProj =
200 myRenderer->GetActiveCamera()->
201 GetCompositeProjectionTransformMatrix (myRenderer->GetTiledAspectRatio(),
204 for (Standard_Integer aRow = 0; aRow < 4; ++aRow)
206 for (Standard_Integer aCol = 0; aCol < 4; ++aCol)
208 theProj.SetValue (aRow, aCol, aCompositeProj->GetElement (aRow, aCol));
212 theOrient.InitIdentity();
215 //================================================================
216 // Function : GetViewport
218 //================================================================
219 void IVtkVTK_View::GetViewport (Standard_Real& theX,
221 Standard_Real& theWidth,
222 Standard_Real& theHeight) const
224 Standard_Real aViewport[4];
225 myRenderer->GetViewport (aViewport);
228 theWidth = aViewport[2];
229 theHeight = aViewport[3];