Salome HOME
Merge from V5_1_main 14/05/2010
[modules/visu.git] / src / PIPELINE / VISU_ImplicitFunctionWidget.cxx
1 //  Copyright (C) 2007-2010  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
23 //  SALOME VTKViewer : build VTK viewer into Salome desktop
24 //  File   : VVTK_ImplicitFunctionWidget.cxx
25 //  Author : Peter KURNEV
26 //  Module : SALOME
27 //  $Header$
28 //
29 #include "VISU_ImplicitFunctionWidget.hxx"
30 //
31 #include <vtkFollower.h>
32 #include <vtkObjectFactory.h>
33 #include <vtkDataSet.h>
34 #include <vtkRenderWindow.h>
35 #include <vtkRenderer.h>
36 #include <vtkDataSet.h>
37 #include <vtkImplicitFunction.h>
38 #include <vtkMapper.h>
39 //
40 vtkStandardNewMacro(VISU_UnScaledActor);
41
42 //====================================================================
43 // function: VISU_UnScaledActor
44 // purpose:
45 //====================================================================
46 VISU_UnScaledActor::VISU_UnScaledActor() 
47 {
48   for (int i=0; i<3; ++i){
49     myCenter[i]=0.;
50   }
51   mySize=24;
52 }
53 //====================================================================
54 // function: SetCenter
55 // purpose:
56 //====================================================================
57 void VISU_UnScaledActor::SetCenter(vtkFloatingPointType *pC)
58 {
59   for (int i=0; i<3; ++i){
60     myCenter[i]=pC[i];
61   }
62 }
63 //====================================================================
64 // function:GetBounds
65 // purpose:
66 //====================================================================
67 vtkFloatingPointType* VISU_UnScaledActor::GetBounds()
68 {
69   Superclass::GetBounds();
70   //
71   for (int i=0; i<3; ++i){
72     Bounds[2*i]=myCenter[i];
73     Bounds[2*i+1]=myCenter[i];
74   }
75   return Bounds;
76 }
77 //====================================================================
78 // function:Render
79 // purpose:
80 //====================================================================
81 void VISU_UnScaledActor::Render(vtkRenderer *theRenderer)
82 {
83   if(theRenderer){
84     vtkFloatingPointType P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
85     theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
86     theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
87     vtkFloatingPointType aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
88                                            (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
89                                            (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
90     int* aSize = theRenderer->GetRenderWindow()->GetSize();
91     vtkFloatingPointType aWinDiag = sqrt(vtkFloatingPointType(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
92     vtkDataSet* aDataSet = GetMapper()->GetInput();
93     vtkFloatingPointType aLength = aDataSet->GetLength();
94     vtkFloatingPointType aPrecision = 1.e-3;
95     vtkFloatingPointType anOldScale = GetScale()[0];
96     vtkFloatingPointType aScale = 
97       mySize*aWorldDiag/aWinDiag/aLength*sqrt(vtkFloatingPointType(aSize[0])/vtkFloatingPointType(aSize[1]));
98
99     SetOrigin(myCenter);  
100     //
101     if(fabs(aScale - anOldScale)/aScale > aPrecision){
102       SetScale(aScale);
103     }
104   }
105   vtkFollower::Render(theRenderer);
106   
107 }
108 //====================================================================
109 // function:SetSize
110 // purpose:
111 //====================================================================
112 void VISU_UnScaledActor::SetSize(int theSize)
113 {
114   mySize = theSize;
115 }
116
117 //==================================================================
118 // class: VISU_ImplicitFunctionWidget
119 //
120 vtkCxxRevisionMacro(VISU_ImplicitFunctionWidget, "$Revision$");
121 //==================================================================
122 // function: VISU_ImplicitFunctionWidget
123 // purpose :
124 //==================================================================
125 VISU_ImplicitFunctionWidget::VISU_ImplicitFunctionWidget() 
126
127   vtk3DWidget()
128 {
129 }
130 //==================================================================
131 // function: ~
132 // purpose :
133 //==================================================================
134 VISU_ImplicitFunctionWidget::~VISU_ImplicitFunctionWidget()
135 {  
136 }