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