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>
18 #include <vtkAutoInit.h>
19 #include <vtkCamera.h>
20 #include <vtkRenderer.h>
21 #include <vtkRenderWindow.h>
22 #include <vtkTransform.h>
24 IMPLEMENT_STANDARD_RTTIEXT(IVtkVTK_View,IVtk_IView)
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)
33 VTK_MODULE_INIT(vtkRenderingOpenGL)
35 VTK_MODULE_INIT(vtkInteractionStyle)
37 // Handle implementation
40 //================================================================
41 // Function : Constructor
43 //================================================================
44 IVtkVTK_View::IVtkVTK_View (vtkRenderer* theRenderer)
45 : myRenderer (theRenderer)
48 //================================================================
49 // Function : Destructor
51 //================================================================
52 IVtkVTK_View::~IVtkVTK_View()
55 //================================================================
56 // Function : IsPerspective
58 //================================================================
59 bool IVtkVTK_View::IsPerspective() const
61 return !myRenderer->GetActiveCamera()->GetParallelProjection();
64 //================================================================
65 // Function : GetDistance
67 //================================================================
68 double IVtkVTK_View::GetDistance() const
70 return myRenderer->GetActiveCamera()->GetDistance();
73 //================================================================
74 // Function : GetPosition
76 //================================================================
77 void IVtkVTK_View::GetPosition (double& theX, double& theY, double& theZ) const
79 myRenderer->GetActiveCamera()->GetFocalPoint (theX, theY, theZ);
82 //================================================================
83 // Function : GetViewUp
85 //================================================================
86 void IVtkVTK_View::GetViewUp (double& theDx, double& theDy, double& theDz) const
88 myRenderer->GetActiveCamera()->OrthogonalizeViewUp();
89 myRenderer->GetActiveCamera()->GetViewUp (theDx, theDy, theDz);
92 //================================================================
93 // Function : GetDirectionOfProjection
95 //================================================================
96 void IVtkVTK_View::GetDirectionOfProjection (double& theDx,
100 myRenderer->GetActiveCamera()->GetDirectionOfProjection (theDx, theDy, theDz);
106 //================================================================
107 // Function : GetScale
109 //================================================================
110 void IVtkVTK_View::GetScale (double& theX, double& theY, double& theZ) const
113 myRenderer->GetActiveCamera()->GetViewTransformObject()->GetScale (aScale);
119 //================================================================
120 // Function : GetParallelScale
122 //================================================================
123 double IVtkVTK_View::GetParallelScale() const
125 return myRenderer->GetActiveCamera()->GetParallelScale();
128 //================================================================
129 // Function : GetViewAngle
131 //================================================================
132 double IVtkVTK_View::GetViewAngle() const
134 return myRenderer->GetActiveCamera()->GetViewAngle();
137 //================================================================
138 // Function : GetViewCenter
140 //================================================================
141 void IVtkVTK_View::GetViewCenter (double& theX, double& theY) const
143 double* aCenter = myRenderer->GetCenter();
148 //================================================================
149 // Function : DisplayToWorld
151 //================================================================
152 bool IVtkVTK_View::DisplayToWorld (const gp_XY& theDisplayPnt, gp_XYZ& theWorldPnt) const
154 // Convert the selection point into world coordinates.
155 myRenderer->SetDisplayPoint (theDisplayPnt.X(), theDisplayPnt.Y(), 0.0);
156 myRenderer->DisplayToWorld();
158 double* const aCoords = myRenderer->GetWorldPoint();
159 if (aCoords[3] == 0.0) // Point at infinity in homogeneous coordinates
164 theWorldPnt = gp_XYZ (aCoords[0] / aCoords[3], aCoords[1] / aCoords[3], aCoords[2] / aCoords[3]);
169 //================================================================
170 // Function : GetWindowSize
172 //================================================================
173 void IVtkVTK_View::GetWindowSize (int& theX, int& theY) const
175 int* aSize = myRenderer->GetRenderWindow()->GetSize();
180 //================================================================
181 // Function : GetCamera
183 //================================================================
184 void IVtkVTK_View::GetCamera (Graphic3d_Mat4d& theProj,
185 Graphic3d_Mat4d& theOrient,
186 Standard_Boolean& theIsOrtho) const
188 theIsOrtho = !IsPerspective();
190 vtkMatrix4x4* aCompositeProj =
191 myRenderer->GetActiveCamera()->GetCompositeProjectionTransformMatrix (myRenderer->GetTiledAspectRatio(),
194 for (Standard_Integer aRow = 0; aRow < 4; ++aRow)
196 for (Standard_Integer aCol = 0; aCol < 4; ++aCol)
198 theProj.SetValue (aRow, aCol, aCompositeProj->GetElement (aRow, aCol));
202 theOrient.InitIdentity();
205 //================================================================
206 // Function : GetViewport
208 //================================================================
209 void IVtkVTK_View::GetViewport (Standard_Real& theX,
211 Standard_Real& theWidth,
212 Standard_Real& theHeight) const
214 Standard_Real aViewport[4];
215 myRenderer->GetViewport (aViewport);
218 theWidth = aViewport[2];
219 theHeight = aViewport[3];