Salome HOME
Update copyright information
[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 vtkFloatingPointType
93 VISU_DeformedShapePL
94 ::GetScaleFactor(vtkDataSet* theDataSet)
95 {
96   if(!theDataSet)
97     return 0.0;
98
99   theDataSet->Update();
100
101   int aNbCells = theDataSet->GetNumberOfCells();
102   int aNbPoints = theDataSet->GetNumberOfPoints();
103   int aNbElem = aNbCells? aNbCells: aNbPoints;
104
105   vtkFloatingPointType* aBounds = theDataSet->GetBounds();
106   vtkFloatingPointType aVolume = 1, aVol, idim = 0;
107   for(int i = 0; i < 6; i += 2){
108     aVol = fabs(aBounds[i+1] - aBounds[i]);
109     if(aVol > 0) {
110       idim++;
111       aVolume *= aVol;
112     }
113   }
114   if( aNbElem == 0 || fabs(idim) < 1.0 / VTK_LARGE_FLOAT )
115     return 0.0; // to avoid division by zero
116   aVolume /= aNbElem;
117   return pow(aVolume, vtkFloatingPointType(1.0/idim));
118 }
119
120
121 //----------------------------------------------------------------------------
122 vtkFloatingPointType
123 VISU_DeformedShapePL
124 ::GetDefaultScale(VISU_ScalarMapPL* theScalarMapPL)
125 {
126   vtkFloatingPointType aSourceRange[2];
127   theScalarMapPL->GetSourceRange(aSourceRange);
128   
129   static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
130   if(fabs(aSourceRange[1]) > EPS){
131     vtkDataSet* aDataSet = theScalarMapPL->GetMergedInput();
132     vtkFloatingPointType aScaleFactor = VISU_DeformedShapePL::GetScaleFactor(aDataSet);
133     return aScaleFactor / aSourceRange[1];
134   }
135   return 0.0;
136 }
137
138
139 //----------------------------------------------------------------------------
140 void
141 VISU_DeformedShapePL
142 ::SetScale(vtkFloatingPointType theScale) 
143 {
144   if(VISU::CheckIsSameValue(myWarpVector->GetScaleFactor(), theScale))
145     return;
146   
147   myWarpVector->SetScaleFactor(theScale*myMapScaleFactor);
148   myScaleFactor = theScale;
149 }
150
151
152 //----------------------------------------------------------------------------
153 vtkFloatingPointType
154 VISU_DeformedShapePL
155 ::GetScale() 
156 {
157   return myScaleFactor;
158 }
159
160
161 //----------------------------------------------------------------------------
162 void
163 VISU_DeformedShapePL
164 ::Init()
165 {
166   Superclass::Init();
167
168   SetScale(VISU_DeformedShapePL::GetDefaultScale(this));
169 }
170
171
172 //----------------------------------------------------------------------------
173 void
174 VISU_DeformedShapePL
175 ::Update()
176 {
177   Superclass::Update();
178   //{
179   //  std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myWarpVector.vtk";
180   //  VISU::WriteToFile(myWarpVector->GetUnstructuredGridOutput(), aFileName);
181   //}
182 }
183
184
185 //----------------------------------------------------------------------------
186 vtkDataSet* 
187 VISU_DeformedShapePL
188 ::InsertCustomPL()
189 {
190   VISU::CellDataToPoint(myWarpVector,
191                         myCellDataToPointData,
192                         GetMergedInput());
193
194   return myWarpVector->GetOutput();
195 }
196
197
198 //----------------------------------------------------------------------------
199 unsigned long int
200 VISU_DeformedShapePL
201 ::GetMemorySize()
202 {
203   unsigned long int aSize = Superclass::GetMemorySize();
204
205   if(myWarpVector->GetInput())
206     if(vtkDataSet* aDataSet = myWarpVector->GetOutput())
207       aSize += aDataSet->GetActualMemorySize() * 1024;
208   
209   if(myCellDataToPointData->GetInput())
210     if(vtkDataSet* aDataSet = myCellDataToPointData->GetOutput())
211       aSize += aDataSet->GetActualMemorySize() * 1024;
212
213   return aSize;
214 }
215
216
217 //----------------------------------------------------------------------------
218 void
219 VISU_DeformedShapePL
220 ::SetMapScale(vtkFloatingPointType theMapScale)
221 {
222   myMapScaleFactor = theMapScale;
223   Superclass::SetMapScale(theMapScale);
224
225   vtkFloatingPointType aMapScale = myScaleFactor * theMapScale;
226   if(VISU::CheckIsSameValue(myWarpVector->GetScaleFactor(), aMapScale))
227     return;
228
229   myWarpVector->SetScaleFactor( aMapScale );
230 }
231
232
233 //----------------------------------------------------------------------------