Salome HOME
b364e3809b2a53baae73e7e8b91a7199c1b3bb9e
[modules/visu.git] / src / PIPELINE / VISU_ImplicitFunctionWidget.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME VTKViewer : build VTK viewer into Salome desktop
23 //  File   : VVTK_ImplicitFunctionWidget.cxx
24 //  Author : Peter KURNEV
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "VISU_ImplicitFunctionWidget.hxx"
29 //
30 #include <vtkFollower.h>
31 #include <vtkObjectFactory.h>
32 #include <vtkDataSet.h>
33 #include <vtkRenderWindow.h>
34 #include <vtkRenderer.h>
35 #include <vtkDataSet.h>
36 #include <vtkImplicitFunction.h>
37 #include <vtkMapper.h>
38 //
39 vtkStandardNewMacro(VISU_UnScaledActor);
40
41 //====================================================================
42 // function: VISU_UnScaledActor
43 // purpose:
44 //====================================================================
45 VISU_UnScaledActor::VISU_UnScaledActor() 
46 {
47   for (int i=0; i<3; ++i){
48     myCenter[i]=0.;
49   }
50   mySize=24;
51 }
52 //====================================================================
53 // function: SetCenter
54 // purpose:
55 //====================================================================
56 void VISU_UnScaledActor::SetCenter(vtkFloatingPointType *pC)
57 {
58   for (int i=0; i<3; ++i){
59     myCenter[i]=pC[i];
60   }
61 }
62 //====================================================================
63 // function:GetBounds
64 // purpose:
65 //====================================================================
66 vtkFloatingPointType* VISU_UnScaledActor::GetBounds()
67 {
68   Superclass::GetBounds();
69   //
70   for (int i=0; i<3; ++i){
71     Bounds[2*i]=myCenter[i];
72     Bounds[2*i+1]=myCenter[i];
73   }
74   return Bounds;
75 }
76 //====================================================================
77 // function:Render
78 // purpose:
79 //====================================================================
80 void VISU_UnScaledActor::Render(vtkRenderer *theRenderer)
81 {
82   if(theRenderer){
83     vtkFloatingPointType P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
84     theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
85     theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
86     vtkFloatingPointType aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
87                                            (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
88                                            (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
89     int* aSize = theRenderer->GetRenderWindow()->GetSize();
90     vtkFloatingPointType aWinDiag = sqrt(vtkFloatingPointType(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
91     vtkDataSet* aDataSet = GetMapper()->GetInput();
92     vtkFloatingPointType aLength = aDataSet->GetLength();
93     vtkFloatingPointType aPrecision = 1.e-3;
94     vtkFloatingPointType anOldScale = GetScale()[0];
95     vtkFloatingPointType aScale = 
96       mySize*aWorldDiag/aWinDiag/aLength*sqrt(vtkFloatingPointType(aSize[0])/vtkFloatingPointType(aSize[1]));
97
98     SetOrigin(myCenter);  
99     //
100     if(fabs(aScale - anOldScale)/aScale > aPrecision){
101       SetScale(aScale);
102     }
103   }
104   vtkFollower::Render(theRenderer);
105   
106 }
107 //====================================================================
108 // function:SetSize
109 // purpose:
110 //====================================================================
111 void VISU_UnScaledActor::SetSize(int theSize)
112 {
113   mySize = theSize;
114 }
115
116 //==================================================================
117 // class: VISU_ImplicitFunctionWidget
118 //
119 vtkCxxRevisionMacro(VISU_ImplicitFunctionWidget, "$Revision$");
120 //==================================================================
121 // function: VISU_ImplicitFunctionWidget
122 // purpose :
123 //==================================================================
124 VISU_ImplicitFunctionWidget::VISU_ImplicitFunctionWidget() 
125
126   vtk3DWidget()
127 {
128 }
129 //==================================================================
130 // function: ~
131 // purpose :
132 //==================================================================
133 VISU_ImplicitFunctionWidget::~VISU_ImplicitFunctionWidget()
134 {  
135 }