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 VTK_MODULE_INIT(vtkRenderingOpenGL);
31 VTK_MODULE_INIT(vtkInteractionStyle);
33 // Handle implementation
36 //================================================================
37 // Function : Constructor
39 //================================================================
40 IVtkVTK_View::IVtkVTK_View (vtkRenderer* theRenderer)
41 : myRenderer (theRenderer)
44 //================================================================
45 // Function : Destructor
47 //================================================================
48 IVtkVTK_View::~IVtkVTK_View()
51 //================================================================
52 // Function : IsPerspective
54 //================================================================
55 bool IVtkVTK_View::IsPerspective() const
57 return !myRenderer->GetActiveCamera()->GetParallelProjection();
60 //================================================================
61 // Function : GetDistance
63 //================================================================
64 double IVtkVTK_View::GetDistance() const
66 return myRenderer->GetActiveCamera()->GetDistance();
69 //================================================================
70 // Function : GetPosition
72 //================================================================
73 void IVtkVTK_View::GetPosition (double& theX, double& theY, double& theZ) const
75 myRenderer->GetActiveCamera()->GetFocalPoint (theX, theY, theZ);
78 //================================================================
79 // Function : GetViewUp
81 //================================================================
82 void IVtkVTK_View::GetViewUp (double& theDx, double& theDy, double& theDz) const
84 myRenderer->GetActiveCamera()->OrthogonalizeViewUp();
85 myRenderer->GetActiveCamera()->GetViewUp (theDx, theDy, theDz);
88 //================================================================
89 // Function : GetDirectionOfProjection
91 //================================================================
92 void IVtkVTK_View::GetDirectionOfProjection (double& theDx,
96 myRenderer->GetActiveCamera()->GetDirectionOfProjection (theDx, theDy, theDz);
102 //================================================================
103 // Function : GetScale
105 //================================================================
106 void IVtkVTK_View::GetScale (double& theX, double& theY, double& theZ) const
109 myRenderer->GetActiveCamera()->GetViewTransformObject()->GetScale (aScale);
115 //================================================================
116 // Function : GetParallelScale
118 //================================================================
119 double IVtkVTK_View::GetParallelScale() const
121 return myRenderer->GetActiveCamera()->GetParallelScale();
124 //================================================================
125 // Function : GetViewAngle
127 //================================================================
128 double IVtkVTK_View::GetViewAngle() const
130 return myRenderer->GetActiveCamera()->GetViewAngle();
133 //================================================================
134 // Function : GetViewCenter
136 //================================================================
137 void IVtkVTK_View::GetViewCenter (double& theX, double& theY) const
139 double* aCenter = myRenderer->GetCenter();
144 //================================================================
145 // Function : DisplayToWorld
147 //================================================================
148 bool IVtkVTK_View::DisplayToWorld (const gp_XY& theDisplayPnt, gp_XYZ& theWorldPnt) const
150 // Convert the selection point into world coordinates.
151 myRenderer->SetDisplayPoint (theDisplayPnt.X(), theDisplayPnt.Y(), 0.0);
152 myRenderer->DisplayToWorld();
154 double* const aCoords = myRenderer->GetWorldPoint();
155 if (aCoords[3] == 0.0) // Point at infinity in homogeneous coordinates
160 theWorldPnt = gp_XYZ (aCoords[0] / aCoords[3], aCoords[1] / aCoords[3], aCoords[2] / aCoords[3]);
165 //================================================================
166 // Function : GetWindowSize
168 //================================================================
169 void IVtkVTK_View::GetWindowSize (int& theX, int& theY) const
171 int* aSize = myRenderer->GetRenderWindow()->GetSize();
176 //================================================================
177 // Function : GetCamera
179 //================================================================
180 void IVtkVTK_View::GetCamera (Graphic3d_Mat4d& theProj,
181 Graphic3d_Mat4d& theOrient,
182 Standard_Boolean& theIsOrtho) const
184 theIsOrtho = !IsPerspective();
186 vtkMatrix4x4* aCompositeProj =
187 myRenderer->GetActiveCamera()->GetCompositeProjectionTransformMatrix (myRenderer->GetTiledAspectRatio(),
190 for (Standard_Integer aRow = 0; aRow < 4; ++aRow)
192 for (Standard_Integer aCol = 0; aCol < 4; ++aCol)
194 theProj.SetValue (aRow, aCol, aCompositeProj->GetElement (aRow, aCol));
198 theOrient.InitIdentity();
201 //================================================================
202 // Function : GetViewport
204 //================================================================
205 void IVtkVTK_View::GetViewport (Standard_Real& theX,
207 Standard_Real& theWidth,
208 Standard_Real& theHeight) const
210 Standard_Real aViewport[4];
211 myRenderer->GetViewport (aViewport);
214 theWidth = aViewport[2];
215 theHeight = aViewport[3];