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