Salome HOME
Merge from V5_1_main 10/06/2010
[modules/visu.git] / src / PIPELINE / VISU_DeformedShapeAndScalarMapPL.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  VISU DeformedShapeAndScalarMapPL
21 // File:    VISU_DeformedShapeAndScalarMapPL.cxx
22 // Author:  Eugeny Nikolaev
23 // Module : VISU
24 //
25 #include "VISU_DeformedShapeAndScalarMapPL.hxx"
26 #include "VISU_FieldTransform.hxx"
27 #include "VISU_Extractor.hxx"
28 #include "VISU_LookupTable.hxx"
29 #include "VISU_DeformedShapePL.hxx"
30 #include "VTKViewer_TransformFilter.h"
31 #include "VTKViewer_Transform.h"
32 #include "VISU_MergeFilter.hxx"
33 #include "VISU_ElnoDisassembleFilter.hxx"
34 #include "VISU_PipeLineUtils.hxx"
35 #include "SALOME_ExtractGeometry.h"
36
37 #include <vtkPlane.h>
38 #include <vtkWarpVector.h>
39 #include <vtkImplicitBoolean.h>
40 #include <vtkImplicitFunction.h>
41 #include <vtkUnstructuredGrid.h>
42 #include <vtkCellDataToPointData.h>
43 #include <vtkPointDataToCellData.h>
44 #include <vtkImplicitFunctionCollection.h>
45
46 #ifdef WNT
47 #include <float.h>
48 #define isnan _isnan
49 #endif
50
51 //----------------------------------------------------------------------------
52 vtkStandardNewMacro(VISU_DeformedShapeAndScalarMapPL)
53
54 //----------------------------------------------------------------------------
55 /*!
56  * Constructor. Creating new instances of vtkWarpVector,vtkMergeFilter,vtkUnstructuredGrid
57  * Where:
58  * \li myDeformVectors is vtkWarpVector  - deformation vectors data
59  * \li myScalarsMergeFilter   is vtkMergeFilter - merge filter.
60  * Merge filter which unify the deformation and scalars
61  * \li myScalars is vtk shared pointer to vtkUnstructuredGrid - scalars data
62 */
63 VISU_DeformedShapeAndScalarMapPL
64 ::VISU_DeformedShapeAndScalarMapPL():
65   myScaleFactor(1.0),
66   myMapScaleFactor(1.0)
67 {
68   myWarpVector = vtkWarpVector::New();
69
70   myScalarsMergeFilter = VISU_MergeFilter::New();
71   myScalarsMergeFilter->SetMergingInputs(true);
72
73   myScalarsExtractor = VISU_Extractor::New();
74
75   myScalarsFieldTransform = VISU_FieldTransform::New();
76
77   myCellDataToPointData = vtkCellDataToPointData::New();
78   myScalarsElnoDisassembleFilter = VISU_ElnoDisassembleFilter::New();
79
80   vtkImplicitBoolean* anImplicitBoolean = vtkImplicitBoolean::New();
81   anImplicitBoolean->SetOperationTypeToIntersection();
82
83   myExtractGeometry = SALOME_ExtractGeometry::New();
84   myExtractGeometry->SetImplicitFunction(anImplicitBoolean);
85 }
86
87 //----------------------------------------------------------------------------
88 /*!
89  * Destructor.
90  * Delete all fields.
91 */
92 VISU_DeformedShapeAndScalarMapPL
93 ::~VISU_DeformedShapeAndScalarMapPL()
94 {
95   myWarpVector->Delete();
96
97   myScalarsMergeFilter->Delete();
98   
99   myScalarsExtractor->Delete();
100
101   myScalarsFieldTransform->Delete();
102
103   myCellDataToPointData->Delete();
104 }
105
106 //----------------------------------------------------------------------------
107 /*!
108  * Initial method
109  */
110 void
111 VISU_DeformedShapeAndScalarMapPL
112 ::Init()
113 {
114   Superclass::Init();
115   
116   SetScale(VISU_DeformedShapePL::GetDefaultScale(this));
117 }
118
119 //----------------------------------------------------------------------------
120 /*!
121  * Build method
122  * Building of deformation and puts result to merge filter.
123  */
124 void
125 VISU_DeformedShapeAndScalarMapPL
126 ::Build()
127 {
128   Superclass::Build();
129 }
130
131
132 //----------------------------------------------------------------------------
133 vtkDataSet* 
134 VISU_DeformedShapeAndScalarMapPL
135 ::InsertCustomPL()
136 {
137   GetMapper()->SetColorModeToMapScalars();
138   GetMapper()->ScalarVisibilityOn();
139
140   VISU::CellDataToPoint(myWarpVector,
141                         myCellDataToPointData,
142                         GetMergedInput());
143   
144   myScalars = vtkUnstructuredGrid::SafeDownCast(GetMergedInput());
145
146   UpdateScalars();
147
148   myScalarsFieldTransform->SetInput(myScalarsExtractor->GetOutput());
149
150   // Sets geometry for merge filter
151   myScalarsMergeFilter->SetGeometry(myWarpVector->GetUnstructuredGridOutput());
152
153   vtkDataSet* aScalarsDataSet = myScalarsFieldTransform->GetOutput();
154   myScalarsMergeFilter->SetScalars(aScalarsDataSet);
155   myScalarsMergeFilter->AddField("VISU_CELLS_MAPPER", aScalarsDataSet);
156   myScalarsMergeFilter->AddField("VISU_POINTS_MAPPER", aScalarsDataSet);
157
158   return myScalarsMergeFilter->GetOutput();
159 }
160
161
162 //----------------------------------------------------------------------------
163 /*!
164  *  Update method
165  */
166 void
167 VISU_DeformedShapeAndScalarMapPL
168 ::Update()
169 {
170   Superclass::Update();
171   //{
172   //  std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myScalarsExtractor.vtk";
173   //  VISU::WriteToFile(myScalarsExtractor->GetUnstructuredGridOutput(), aFileName);
174   //}
175   //{
176   //  std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myWarpVector.vtk";
177   //  VISU::WriteToFile(myWarpVector->GetUnstructuredGridOutput(), aFileName);
178   //}
179   //{
180   //  std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myScalarsMergeFilter.vtk";
181   //  VISU::WriteToFile(myScalarsMergeFilter->GetUnstructuredGridOutput(), aFileName);
182   //}
183 }
184
185 //----------------------------------------------------------------------------
186 unsigned long int
187 VISU_DeformedShapeAndScalarMapPL
188 ::GetMemorySize()
189 {
190   unsigned long int aSize = Superclass::GetMemorySize();
191
192   if(vtkDataSet* aDataSet = myWarpVector->GetOutput())
193     aSize += aDataSet->GetActualMemorySize() * 1024;
194   
195   if(vtkDataSet* aDataSet = myScalarsExtractor->GetOutput())
196     aSize += aDataSet->GetActualMemorySize() * 1024;
197
198   if(vtkDataSet* aDataSet = myScalarsMergeFilter->GetOutput())
199     aSize += aDataSet->GetActualMemorySize() * 1024;
200
201   if(myCellDataToPointData->GetInput())
202     if(vtkDataSet* aDataSet = myCellDataToPointData->GetOutput())
203       aSize += aDataSet->GetActualMemorySize() * 1024;
204
205   return aSize;
206 }
207
208 //----------------------------------------------------------------------------
209 /*!
210  * Update scalars method.
211  * Put scalars to merge filter.
212  */
213 void
214 VISU_DeformedShapeAndScalarMapPL
215 ::UpdateScalars()
216 {
217   vtkDataSet* aScalars = GetScalars();
218   myScalarsElnoDisassembleFilter->SetInput(aScalars);
219   myExtractGeometry->SetInput(myScalarsElnoDisassembleFilter->GetOutput());
220   myScalarsExtractor->SetInput(myExtractGeometry->GetOutput());
221
222   if(VISU::IsDataOnCells(myScalarsElnoDisassembleFilter->GetOutput()))
223     GetMapper()->SetScalarModeToUseCellData();
224   else
225     GetMapper()->SetScalarModeToUsePointData();
226 }
227
228 //----------------------------------------------------------------------------
229 /*!
230  * Copy information about pipline.
231  * Copy scale and scalars.
232  */
233 void
234 VISU_DeformedShapeAndScalarMapPL
235 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
236                 bool theIsCopyInput)
237 {
238   Superclass::DoShallowCopy(thePipeLine, theIsCopyInput);
239
240   if(VISU_DeformedShapeAndScalarMapPL *aPipeLine = dynamic_cast<VISU_DeformedShapeAndScalarMapPL*>(thePipeLine)){
241      SetImplicitFunction(aPipeLine->GetImplicitFunction());
242      SetScale(aPipeLine->GetScale());
243      SetScalars(aPipeLine->GetScalars());
244   }
245 }
246
247 //----------------------------------------------------------------------------
248 /*!
249  * Set scalars.
250  * Sets vtkDataSet with scalars values to VISU_Extractor filter for scalars extraction.
251  */
252 void
253 VISU_DeformedShapeAndScalarMapPL
254 ::SetScalars(vtkDataSet *theScalars)
255 {
256   if(GetScalars() == theScalars)
257     return;
258   
259   myScalars = vtkUnstructuredGrid::SafeDownCast(theScalars);
260   UpdateScalars();
261 }
262
263 //----------------------------------------------------------------------------
264 /*!
265  * Get pointer to input scalars.
266  */
267 vtkDataSet* 
268 VISU_DeformedShapeAndScalarMapPL
269 ::GetScalars()
270 {
271   return myScalars.GetPointer();
272 }
273
274 //----------------------------------------------------------------------------
275 /*!
276  * Removes all clipping planes (for myScalars)
277  */
278 void
279 VISU_DeformedShapeAndScalarMapPL
280 ::RemoveAllClippingPlanes()
281 {
282   if(vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()){
283     vtkImplicitFunctionCollection* aFunction = aBoolean->GetFunction();
284     aFunction->RemoveAllItems();
285     aBoolean->Modified();
286   }
287   Superclass::RemoveAllClippingPlanes();
288 }
289
290 //----------------------------------------------------------------------------
291 /*!
292  * Removes a clipping plane (for myScalars)
293  */
294 void
295 VISU_DeformedShapeAndScalarMapPL
296 ::RemoveClippingPlane(vtkIdType theID)
297 {
298   if(vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()){
299     vtkImplicitFunctionCollection* aFunction = aBoolean->GetFunction();
300     if(theID >= 0 && theID < aFunction->GetNumberOfItems())
301       aFunction->RemoveItem(theID);
302     aBoolean->Modified();
303   }
304   Superclass::RemoveClippingPlane(theID);
305 }
306
307 //----------------------------------------------------------------------------
308 /*!
309  * Adds a clipping plane (for myScalars)
310  */
311 bool 
312 VISU_DeformedShapeAndScalarMapPL
313 ::AddClippingPlane(vtkPlane* thePlane)
314 {
315   if (thePlane) {
316     if (vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()) {
317       vtkImplicitFunctionCollection* aFunction = aBoolean->GetFunction();
318       aFunction->AddItem(thePlane);
319
320       // Check, that at least one cell present after clipping.
321       // This check was introduced because of bug IPAL8849.
322       vtkDataSet* aClippedDataSet = GetClippedInput();
323       if(aClippedDataSet->GetNumberOfCells() < 1)
324         return false;
325     }
326   }
327   return Superclass::AddClippingPlane(thePlane);
328 }
329
330 //----------------------------------------------------------------------------
331 /*!
332  * Sets implicit function of clipping
333  */
334 void
335 VISU_DeformedShapeAndScalarMapPL
336 ::SetImplicitFunction(vtkImplicitFunction *theFunction)
337 {
338   myExtractGeometry->SetImplicitFunction(theFunction);
339
340
341 //----------------------------------------------------------------------------
342 /*!
343  * Gets implicit function of clipping
344  */
345 vtkImplicitFunction * 
346 VISU_DeformedShapeAndScalarMapPL
347 ::GetImplicitFunction()
348 {
349   return myExtractGeometry->GetImplicitFunction();
350 }
351
352 //----------------------------------------------------------------------------
353 /*!
354  * Sets scale for deformed shape
355  */
356 void
357 VISU_DeformedShapeAndScalarMapPL
358 ::SetScale(vtkFloatingPointType theScale) 
359 {
360   if(VISU::CheckIsSameValue(myScaleFactor, theScale))
361     return;
362
363   myScaleFactor = theScale;
364   myWarpVector->SetScaleFactor(theScale*myMapScaleFactor);
365 }
366
367 //----------------------------------------------------------------------------
368 /*!
369  * Gets scale of deformed shape.
370  */
371 vtkFloatingPointType
372 VISU_DeformedShapeAndScalarMapPL
373 ::GetScale() 
374 {
375   return myScaleFactor;
376 }
377
378 //----------------------------------------------------------------------------
379 /*!
380  * Set scale factor of deformation.
381  */
382 void
383 VISU_DeformedShapeAndScalarMapPL
384 ::SetMapScale(vtkFloatingPointType theMapScale)
385 {
386   myMapScaleFactor = theMapScale;
387   Superclass::SetMapScale(theMapScale);
388   myWarpVector->SetScaleFactor(myScaleFactor*theMapScale);
389 }
390
391 //----------------------------------------------------------------------------
392 /*!
393  * Gets scalar mode.
394  */
395 int
396 VISU_DeformedShapeAndScalarMapPL
397 ::GetScalarMode()
398 {
399   return myScalarsExtractor->GetScalarMode();
400 }
401
402 //----------------------------------------------------------------------------
403 /*!
404  * Sets scalar mode.
405  */
406 void
407 VISU_DeformedShapeAndScalarMapPL
408 ::SetScalarMode(int theScalarMode)
409 {
410   VISU_ScalarMapPL::SetScalarMode(theScalarMode, GetScalars(), myScalarsExtractor);
411 }
412
413 //----------------------------------------------------------------------------
414 void
415 VISU_DeformedShapeAndScalarMapPL
416 ::SetScaling(int theScaling) 
417 {
418   if(GetScaling() == theScaling)
419     return;
420
421   GetBarTable()->SetScale(theScaling);
422
423   if(theScaling == VTK_SCALE_LOG10)
424     myScalarsFieldTransform->SetScalarTransform(&(VISU_FieldTransform::Log10));
425   else
426     myScalarsFieldTransform->SetScalarTransform(&(VISU_FieldTransform::Ident));
427 }
428
429
430 //----------------------------------------------------------------------------
431 void
432 VISU_DeformedShapeAndScalarMapPL
433 ::SetScalarRange(vtkFloatingPointType theRange[2])
434 {
435   if (isnan(theRange[0]) || isnan(theRange[1]))
436     throw std::runtime_error("NAN values in the presentation");
437
438   if(VISU::CheckIsSameRange(theRange, GetScalarRange()))
439     return;
440
441   myScalarsFieldTransform->SetScalarRange(theRange);
442   GetBarTable()->SetRange(theRange);
443 }
444
445
446 //----------------------------------------------------------------------------
447 vtkFloatingPointType* 
448 VISU_DeformedShapeAndScalarMapPL
449 ::GetScalarRange() 
450 {
451   return myScalarsFieldTransform->GetScalarRange();
452 }
453
454
455 //----------------------------------------------------------------------------
456 /*!
457  * Gets ranges of extracted scalars
458  * \param theRange[2] - output values
459  * \li theRange[0] - minimum value
460  * \li theRange[1] - maximum value
461  */
462 void 
463 VISU_DeformedShapeAndScalarMapPL
464 ::GetSourceRange(vtkFloatingPointType theRange[2])
465 {
466   myScalarsExtractor->Update();
467   myScalarsExtractor->GetUnstructuredGridOutput()->GetScalarRange(theRange);
468
469   if (isnan(theRange[0]) || isnan(theRange[1]))
470     throw std::runtime_error("NAN values in the presentation");
471 }
472
473
474 //----------------------------------------------------------------------------
475 void
476 VISU_DeformedShapeAndScalarMapPL
477 ::SetGaussMetric(VISU::TGaussMetric theGaussMetric) 
478 {
479   if(GetGaussMetric() == theGaussMetric)
480     return;
481
482   myScalarsExtractor->SetGaussMetric(theGaussMetric);
483 }
484
485
486 //----------------------------------------------------------------------------
487 VISU::TGaussMetric
488 VISU_DeformedShapeAndScalarMapPL
489 ::GetGaussMetric() 
490 {
491   return myScalarsExtractor->GetGaussMetric();
492 }