0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / IVtkVTK / IVtkVTK_View.cxx
CommitLineData
913a4c4a 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
17#include <IVtkVTK_View.hxx>
a9660929 18
19// prevent disabling some MSVC warning messages by VTK headers
20#ifdef _MSC_VER
21#pragma warning(push)
22#endif
913a4c4a 23#include <vtkAutoInit.h>
24#include <vtkCamera.h>
25#include <vtkRenderer.h>
f751596e 26#include <vtkRenderWindow.h>
913a4c4a 27#include <vtkTransform.h>
a9660929 28#ifdef _MSC_VER
29#pragma warning(pop)
30#endif
913a4c4a 31
92efcf78 32IMPLEMENT_STANDARD_RTTIEXT(IVtkVTK_View,IVtk_IView)
33
913a4c4a 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.
88d533be 38#ifdef VTK_OPENGL2_BACKEND
39VTK_MODULE_INIT(vtkRenderingOpenGL2)
40#else
68858c7d 41VTK_MODULE_INIT(vtkRenderingOpenGL)
88d533be 42#endif
68858c7d 43VTK_MODULE_INIT(vtkInteractionStyle)
913a4c4a 44
45// Handle implementation
ec357c5c 46
913a4c4a 47
48//================================================================
49// Function : Constructor
50// Purpose :
51//================================================================
52IVtkVTK_View::IVtkVTK_View (vtkRenderer* theRenderer)
53: myRenderer (theRenderer)
54{ }
55
56//================================================================
57// Function : Destructor
58// Purpose :
59//================================================================
60IVtkVTK_View::~IVtkVTK_View()
61{ }
62
63//================================================================
64// Function : IsPerspective
65// Purpose :
66//================================================================
67bool IVtkVTK_View::IsPerspective() const
68{
69 return !myRenderer->GetActiveCamera()->GetParallelProjection();
70}
71
72//================================================================
73// Function : GetDistance
74// Purpose :
75//================================================================
76double IVtkVTK_View::GetDistance() const
77{
78 return myRenderer->GetActiveCamera()->GetDistance();
79}
80
81//================================================================
82// Function : GetPosition
83// Purpose :
84//================================================================
85void IVtkVTK_View::GetPosition (double& theX, double& theY, double& theZ) const
86{
87 myRenderer->GetActiveCamera()->GetFocalPoint (theX, theY, theZ);
88}
89
90//================================================================
91// Function : GetViewUp
92// Purpose :
93//================================================================
94void IVtkVTK_View::GetViewUp (double& theDx, double& theDy, double& theDz) const
95{
96 myRenderer->GetActiveCamera()->OrthogonalizeViewUp();
97 myRenderer->GetActiveCamera()->GetViewUp (theDx, theDy, theDz);
98}
99
100//================================================================
101// Function : GetDirectionOfProjection
102// Purpose :
103//================================================================
104void IVtkVTK_View::GetDirectionOfProjection (double& theDx,
105 double& theDy,
106 double& theDz) const
107{
108 myRenderer->GetActiveCamera()->GetDirectionOfProjection (theDx, theDy, theDz);
109 theDx = -theDx;
110 theDy = -theDy;
111 theDz = -theDz;
112}
113
114//================================================================
115// Function : GetScale
116// Purpose :
117//================================================================
118void IVtkVTK_View::GetScale (double& theX, double& theY, double& theZ) const
119{
120 double aScale[3];
121 myRenderer->GetActiveCamera()->GetViewTransformObject()->GetScale (aScale);
122 theX = aScale[0];
123 theY = aScale[1];
124 theZ = aScale[2];
125}
126
127//================================================================
128// Function : GetParallelScale
129// Purpose :
130//================================================================
131double IVtkVTK_View::GetParallelScale() const
132{
133 return myRenderer->GetActiveCamera()->GetParallelScale();
134}
135
136//================================================================
137// Function : GetViewAngle
138// Purpose :
139//================================================================
140double IVtkVTK_View::GetViewAngle() const
141{
142 return myRenderer->GetActiveCamera()->GetViewAngle();
143}
144
145//================================================================
146// Function : GetViewCenter
147// Purpose :
148//================================================================
149void IVtkVTK_View::GetViewCenter (double& theX, double& theY) const
150{
151 double* aCenter = myRenderer->GetCenter();
152 theX = aCenter[0];
153 theY = aCenter[1];
154}
155
156//================================================================
157// Function : DisplayToWorld
158// Purpose :
159//================================================================
160bool IVtkVTK_View::DisplayToWorld (const gp_XY& theDisplayPnt, gp_XYZ& theWorldPnt) const
161{
162 // Convert the selection point into world coordinates.
163 myRenderer->SetDisplayPoint (theDisplayPnt.X(), theDisplayPnt.Y(), 0.0);
164 myRenderer->DisplayToWorld();
165
5ca413ce 166 double* const aCoords = myRenderer->GetWorldPoint();
913a4c4a 167 if (aCoords[3] == 0.0) // Point at infinity in homogeneous coordinates
168 {
169 return false;
170 }
171
a2f76b15 172 theWorldPnt = gp_XYZ (aCoords[0] / aCoords[3],
173 aCoords[1] / aCoords[3], aCoords[2] / aCoords[3]);
913a4c4a 174
175 return true;
176}
f751596e 177
178//================================================================
179// Function : GetWindowSize
180// Purpose :
181//================================================================
182void IVtkVTK_View::GetWindowSize (int& theX, int& theY) const
183{
184 int* aSize = myRenderer->GetRenderWindow()->GetSize();
185 theX = aSize[0];
186 theY = aSize[1];
187}
188
189//================================================================
190// Function : GetCamera
191// Purpose :
192//================================================================
193void IVtkVTK_View::GetCamera (Graphic3d_Mat4d& theProj,
194 Graphic3d_Mat4d& theOrient,
195 Standard_Boolean& theIsOrtho) const
196{
197 theIsOrtho = !IsPerspective();
198
199 vtkMatrix4x4* aCompositeProj =
a2f76b15 200 myRenderer->GetActiveCamera()->
201 GetCompositeProjectionTransformMatrix (myRenderer->GetTiledAspectRatio(),
202 0,
203 1);
f751596e 204 for (Standard_Integer aRow = 0; aRow < 4; ++aRow)
205 {
206 for (Standard_Integer aCol = 0; aCol < 4; ++aCol)
207 {
208 theProj.SetValue (aRow, aCol, aCompositeProj->GetElement (aRow, aCol));
209 }
210 }
211
212 theOrient.InitIdentity();
213}
214
215//================================================================
216// Function : GetViewport
217// Purpose :
218//================================================================
219void IVtkVTK_View::GetViewport (Standard_Real& theX,
220 Standard_Real& theY,
221 Standard_Real& theWidth,
222 Standard_Real& theHeight) const
223{
224 Standard_Real aViewport[4];
225 myRenderer->GetViewport (aViewport);
226 theX = aViewport[0];
227 theY = aViewport[1];
228 theWidth = aViewport[2];
229 theHeight = aViewport[3];
230}