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