Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / PIPELINE / VISU_DeformedShapePL.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  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 // File:    VISU_PipeLine.cxx
24 // Author:  Alexey PETROV
25 // Module : VISU
26
27
28 #include "VISU_DeformedShapePL.hxx"
29 #include "VISU_PipeLineUtils.hxx"
30 #include "VTKViewer_Transform.h"
31
32 #include <vtkWarpVector.h>
33
34
35 //----------------------------------------------------------------------------
36 vtkStandardNewMacro(VISU_DeformedShapePL);
37
38
39 //----------------------------------------------------------------------------
40 VISU_DeformedShapePL
41 ::VISU_DeformedShapePL():
42   myScaleFactor(0.0)
43 {
44   SetIsShrinkable(true);
45
46   myWarpVector = vtkWarpVector::New();
47   myCellDataToPointData = vtkCellDataToPointData::New();
48 }
49
50
51 //----------------------------------------------------------------------------
52 VISU_DeformedShapePL
53 ::~VISU_DeformedShapePL()
54 {
55   myWarpVector->Delete();
56
57   myCellDataToPointData->Delete();
58 }
59
60
61 //----------------------------------------------------------------------------
62 unsigned long int 
63 VISU_DeformedShapePL
64 ::GetMTime()
65 {
66   unsigned long int aTime = Superclass::GetMTime();
67
68   aTime = std::max(aTime, myWarpVector->GetMTime());
69   aTime = std::max(aTime, myCellDataToPointData->GetMTime());
70
71   return aTime;
72 }
73
74
75 //----------------------------------------------------------------------------
76 void
77 VISU_DeformedShapePL
78 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
79                 bool theIsCopyInput)
80 {
81   Superclass::DoShallowCopy(thePipeLine, theIsCopyInput);
82
83   if(VISU_DeformedShapePL *aPipeLine = dynamic_cast<VISU_DeformedShapePL*>(thePipeLine)){
84     SetScale(aPipeLine->GetScale());
85   }
86 }
87
88
89 //----------------------------------------------------------------------------
90 vtkFloatingPointType
91 VISU_DeformedShapePL
92 ::GetScaleFactor(vtkDataSet* theDataSet)
93 {
94   if(!theDataSet)
95     return 0.0;
96
97   theDataSet->Update();
98
99   int aNbCells = theDataSet->GetNumberOfCells();
100   int aNbPoints = theDataSet->GetNumberOfPoints();
101   int aNbElem = aNbCells? aNbCells: aNbPoints;
102
103   vtkFloatingPointType* aBounds = theDataSet->GetBounds();
104   vtkFloatingPointType aVolume = 1, aVol, idim = 0;
105   for(int i = 0; i < 6; i += 2){
106     aVol = fabs(aBounds[i+1] - aBounds[i]);
107     if(aVol > 0) {
108       idim++;
109       aVolume *= aVol;
110     }
111   }
112   aVolume /= aNbElem;
113   return pow(aVolume, vtkFloatingPointType(1.0/idim));
114 }
115
116
117 //----------------------------------------------------------------------------
118 vtkFloatingPointType
119 VISU_DeformedShapePL
120 ::GetDefaultScale(VISU_ScalarMapPL* theScalarMapPL)
121 {
122   vtkFloatingPointType aSourceRange[2];
123   theScalarMapPL->GetSourceRange(aSourceRange);
124   
125   static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
126   if(fabs(aSourceRange[1]) > EPS){
127     vtkDataSet* aDataSet = theScalarMapPL->GetMergedInput();
128     vtkFloatingPointType aScaleFactor = VISU_DeformedShapePL::GetScaleFactor(aDataSet);
129     return aScaleFactor / aSourceRange[1];
130   }
131   return 0.0;
132 }
133
134
135 //----------------------------------------------------------------------------
136 void
137 VISU_DeformedShapePL
138 ::SetScale(vtkFloatingPointType theScale) 
139 {
140   if(VISU::CheckIsSameValue(myWarpVector->GetScaleFactor(), theScale))
141     return;
142
143   myWarpVector->SetScaleFactor(theScale);
144   myScaleFactor = theScale;
145 }
146
147
148 //----------------------------------------------------------------------------
149 vtkFloatingPointType
150 VISU_DeformedShapePL
151 ::GetScale() 
152 {
153   return myScaleFactor;
154 }
155
156
157 //----------------------------------------------------------------------------
158 void
159 VISU_DeformedShapePL
160 ::Init()
161 {
162   Superclass::Init();
163
164   SetScale(VISU_DeformedShapePL::GetDefaultScale(this));
165 }
166
167
168 //----------------------------------------------------------------------------
169 void
170 VISU_DeformedShapePL
171 ::Update()
172 {
173   Superclass::Update();
174   //{
175   //  std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myWarpVector.vtk";
176   //  VISU::WriteToFile(myWarpVector->GetUnstructuredGridOutput(), aFileName);
177   //}
178 }
179
180
181 //----------------------------------------------------------------------------
182 vtkDataSet* 
183 VISU_DeformedShapePL
184 ::InsertCustomPL()
185 {
186   VISU::CellDataToPoint(myWarpVector,
187                         myCellDataToPointData,
188                         GetMergedInput());
189
190   return myWarpVector->GetOutput();
191 }
192
193
194 //----------------------------------------------------------------------------
195 unsigned long int
196 VISU_DeformedShapePL
197 ::GetMemorySize()
198 {
199   unsigned long int aSize = Superclass::GetMemorySize();
200
201   if(myWarpVector->GetInput())
202     if(vtkDataSet* aDataSet = myWarpVector->GetOutput())
203       aSize += aDataSet->GetActualMemorySize() * 1024;
204   
205   if(myCellDataToPointData->GetInput())
206     if(vtkDataSet* aDataSet = myCellDataToPointData->GetOutput())
207       aSize += aDataSet->GetActualMemorySize() * 1024;
208
209   return aSize;
210 }
211
212
213 //----------------------------------------------------------------------------
214 void
215 VISU_DeformedShapePL
216 ::SetMapScale(vtkFloatingPointType theMapScale)
217 {
218   Superclass::SetMapScale(theMapScale);
219
220   myWarpVector->SetScaleFactor(myScaleFactor*theMapScale);
221 }
222
223
224 //----------------------------------------------------------------------------