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