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 // Initialization of VTK object factories.
25 // Since VTK 6 the factory methods require "auto-initialization" depending on
26 // what modules are enabled at VTK configure time.
27 // Some defines are needed in order to make the factories work properly.
28 VTK_MODULE_INIT(vtkRenderingOpenGL);
29 VTK_MODULE_INIT(vtkInteractionStyle);
31 // Handle implementation
34 //================================================================
35 // Function : Constructor
37 //================================================================
38 IVtkVTK_View::IVtkVTK_View (vtkRenderer* theRenderer)
39 : myRenderer (theRenderer)
42 //================================================================
43 // Function : Destructor
45 //================================================================
46 IVtkVTK_View::~IVtkVTK_View()
49 //================================================================
50 // Function : IsPerspective
52 //================================================================
53 bool IVtkVTK_View::IsPerspective() const
55 return !myRenderer->GetActiveCamera()->GetParallelProjection();
58 //================================================================
59 // Function : GetDistance
61 //================================================================
62 double IVtkVTK_View::GetDistance() const
64 return myRenderer->GetActiveCamera()->GetDistance();
67 //================================================================
68 // Function : GetPosition
70 //================================================================
71 void IVtkVTK_View::GetPosition (double& theX, double& theY, double& theZ) const
73 myRenderer->GetActiveCamera()->GetFocalPoint (theX, theY, theZ);
76 //================================================================
77 // Function : GetViewUp
79 //================================================================
80 void IVtkVTK_View::GetViewUp (double& theDx, double& theDy, double& theDz) const
82 myRenderer->GetActiveCamera()->OrthogonalizeViewUp();
83 myRenderer->GetActiveCamera()->GetViewUp (theDx, theDy, theDz);
86 //================================================================
87 // Function : GetDirectionOfProjection
89 //================================================================
90 void IVtkVTK_View::GetDirectionOfProjection (double& theDx,
94 myRenderer->GetActiveCamera()->GetDirectionOfProjection (theDx, theDy, theDz);
100 //================================================================
101 // Function : GetScale
103 //================================================================
104 void IVtkVTK_View::GetScale (double& theX, double& theY, double& theZ) const
107 myRenderer->GetActiveCamera()->GetViewTransformObject()->GetScale (aScale);
113 //================================================================
114 // Function : GetParallelScale
116 //================================================================
117 double IVtkVTK_View::GetParallelScale() const
119 return myRenderer->GetActiveCamera()->GetParallelScale();
122 //================================================================
123 // Function : GetViewAngle
125 //================================================================
126 double IVtkVTK_View::GetViewAngle() const
128 return myRenderer->GetActiveCamera()->GetViewAngle();
131 //================================================================
132 // Function : GetViewCenter
134 //================================================================
135 void IVtkVTK_View::GetViewCenter (double& theX, double& theY) const
137 double* aCenter = myRenderer->GetCenter();
142 //================================================================
143 // Function : DisplayToWorld
145 //================================================================
146 bool IVtkVTK_View::DisplayToWorld (const gp_XY& theDisplayPnt, gp_XYZ& theWorldPnt) const
148 // Convert the selection point into world coordinates.
149 myRenderer->SetDisplayPoint (theDisplayPnt.X(), theDisplayPnt.Y(), 0.0);
150 myRenderer->DisplayToWorld();
152 vtkFloatingPointType* const aCoords = myRenderer->GetWorldPoint();
153 if (aCoords[3] == 0.0) // Point at infinity in homogeneous coordinates
158 theWorldPnt = gp_XYZ (aCoords[0] / aCoords[3], aCoords[1] / aCoords[3], aCoords[2] / aCoords[3]);
163 //================================================================
164 // Function : GetWindowSize
166 //================================================================
167 void IVtkVTK_View::GetWindowSize (int& theX, int& theY) const
169 int* aSize = myRenderer->GetRenderWindow()->GetSize();
174 //================================================================
175 // Function : GetCamera
177 //================================================================
178 void IVtkVTK_View::GetCamera (Graphic3d_Mat4d& theProj,
179 Graphic3d_Mat4d& theOrient,
180 Standard_Boolean& theIsOrtho) const
182 theIsOrtho = !IsPerspective();
184 vtkMatrix4x4* aCompositeProj =
185 myRenderer->GetActiveCamera()->GetCompositeProjectionTransformMatrix (myRenderer->GetTiledAspectRatio(),
188 for (Standard_Integer aRow = 0; aRow < 4; ++aRow)
190 for (Standard_Integer aCol = 0; aCol < 4; ++aCol)
192 theProj.SetValue (aRow, aCol, aCompositeProj->GetElement (aRow, aCol));
196 theOrient.InitIdentity();
199 //================================================================
200 // Function : GetViewport
202 //================================================================
203 void IVtkVTK_View::GetViewport (Standard_Real& theX,
205 Standard_Real& theWidth,
206 Standard_Real& theHeight) const
208 Standard_Real aViewport[4];
209 myRenderer->GetViewport (aViewport);
212 theWidth = aViewport[2];
213 theHeight = aViewport[3];