1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // VISU DeformedShapeAndScalarMapPL
21 // File: VISU_DeformedShapeAndScalarMapPL.cxx
22 // Author: Eugeny Nikolaev
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"
38 #include <vtkWarpVector.h>
39 #include <vtkImplicitBoolean.h>
40 #include <vtkImplicitFunction.h>
41 #include <vtkUnstructuredGrid.h>
42 #include <vtkPointDataToCellData.h>
43 #include <vtkImplicitFunctionCollection.h>
50 //----------------------------------------------------------------------------
51 vtkStandardNewMacro(VISU_DeformedShapeAndScalarMapPL)
53 //----------------------------------------------------------------------------
55 * Constructor. Creating new instances of vtkWarpVector,vtkMergeFilter,vtkUnstructuredGrid
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
62 VISU_DeformedShapeAndScalarMapPL
63 ::VISU_DeformedShapeAndScalarMapPL():
67 myWarpVector = vtkWarpVector::New();
69 myScalarsMergeFilter = VISU_MergeFilter::New();
70 myScalarsMergeFilter->SetMergingInputs(true);
72 myScalarsExtractor = VISU_Extractor::New();
74 myScalarsFieldTransform = VISU_FieldTransform::New();
76 myCellDataToPointData = VISU_CellDataToPointData::New();
77 myScalarsElnoDisassembleFilter = VISU_ElnoDisassembleFilter::New();
79 vtkImplicitBoolean* anImplicitBoolean = vtkImplicitBoolean::New();
80 anImplicitBoolean->SetOperationTypeToIntersection();
82 myExtractGeometry = SALOME_ExtractGeometry::New();
83 myExtractGeometry->SetImplicitFunction(anImplicitBoolean);
86 //----------------------------------------------------------------------------
91 VISU_DeformedShapeAndScalarMapPL
92 ::~VISU_DeformedShapeAndScalarMapPL()
94 myWarpVector->Delete();
96 myScalarsMergeFilter->Delete();
98 myScalarsExtractor->Delete();
100 myScalarsFieldTransform->Delete();
102 myCellDataToPointData->Delete();
105 //----------------------------------------------------------------------------
110 VISU_DeformedShapeAndScalarMapPL
115 SetScale(VISU_DeformedShapePL::GetDefaultScale(this));
118 //----------------------------------------------------------------------------
121 * Building of deformation and puts result to merge filter.
124 VISU_DeformedShapeAndScalarMapPL
131 //----------------------------------------------------------------------------
133 VISU_DeformedShapeAndScalarMapPL
136 GetMapper()->SetColorModeToMapScalars();
137 GetMapper()->ScalarVisibilityOn();
139 VISU::CellDataToPoint(myWarpVector,
140 myCellDataToPointData,
142 GetMergedInputPort());
144 myScalars = vtkUnstructuredGrid::SafeDownCast(GetMergedInput());
148 myScalarsFieldTransform->SetInputConnection(myScalarsExtractor->GetOutputPort());
150 // Sets geometry for merge filter
151 myScalarsMergeFilter->SetGeometryConnection(myWarpVector->GetOutputPort());
153 vtkDataSet* aScalarsDataSet = myScalarsFieldTransform->GetOutput();
154 myScalarsFieldTransform->Update();
156 vtkAlgorithmOutput* aScalarsConnection = myScalarsFieldTransform->GetOutputPort();
157 myScalarsMergeFilter->SetScalarsConnection(aScalarsConnection);
158 myScalarsMergeFilter->AddField("VISU_CELLS_MAPPER", aScalarsDataSet);
159 myScalarsMergeFilter->AddField("VISU_POINTS_MAPPER", aScalarsDataSet);
161 return myScalarsMergeFilter->GetOutputPort();
165 //----------------------------------------------------------------------------
170 VISU_DeformedShapeAndScalarMapPL
173 Superclass::Update();
175 // std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myScalarsExtractor.vtk";
176 // VISU::WriteToFile(myScalarsExtractor->GetUnstructuredGridOutput(), aFileName);
179 // std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myWarpVector.vtk";
180 // VISU::WriteToFile(myWarpVector->GetUnstructuredGridOutput(), aFileName);
183 // std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myScalarsMergeFilter.vtk";
184 // VISU::WriteToFile(myScalarsMergeFilter->GetUnstructuredGridOutput(), aFileName);
188 //----------------------------------------------------------------------------
190 VISU_DeformedShapeAndScalarMapPL
193 unsigned long int aSize = Superclass::GetMemorySize();
195 if(vtkDataSet* aDataSet = myWarpVector->GetOutput())
196 aSize += aDataSet->GetActualMemorySize() * 1024;
198 if(vtkDataSet* aDataSet = myScalarsExtractor->GetOutput())
199 aSize += aDataSet->GetActualMemorySize() * 1024;
201 if(vtkDataSet* aDataSet = myScalarsMergeFilter->GetOutput())
202 aSize += aDataSet->GetActualMemorySize() * 1024;
204 if(myCellDataToPointData->GetInput())
205 if(vtkDataSet* aDataSet = myCellDataToPointData->GetOutput())
206 aSize += aDataSet->GetActualMemorySize() * 1024;
211 //----------------------------------------------------------------------------
213 * Update scalars method.
214 * Put scalars to merge filter.
217 VISU_DeformedShapeAndScalarMapPL
220 vtkDataSet* aScalars = GetScalars();
221 myScalarsElnoDisassembleFilter->SetInputData(aScalars);
222 myExtractGeometry->SetInputConnection(myScalarsElnoDisassembleFilter->GetOutputPort());
223 myScalarsExtractor->SetInputConnection(myExtractGeometry->GetOutputPort());
225 vtkDataSet* aDataSet = myScalarsElnoDisassembleFilter->GetOutput();
226 myScalarsElnoDisassembleFilter->Update();
227 if(VISU::IsDataOnCells(aDataSet))
228 GetMapper()->SetScalarModeToUseCellData();
230 GetMapper()->SetScalarModeToUsePointData();
233 //----------------------------------------------------------------------------
235 * Copy information about pipline.
236 * Copy scale and scalars.
239 VISU_DeformedShapeAndScalarMapPL
240 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
243 Superclass::DoShallowCopy(thePipeLine, theIsCopyInput);
245 if(VISU_DeformedShapeAndScalarMapPL *aPipeLine = dynamic_cast<VISU_DeformedShapeAndScalarMapPL*>(thePipeLine)){
246 SetImplicitFunction(aPipeLine->GetImplicitFunction());
247 SetScale(aPipeLine->GetScale());
248 SetScalars(aPipeLine->GetScalars());
252 //----------------------------------------------------------------------------
255 * Sets vtkDataSet with scalars values to VISU_Extractor filter for scalars extraction.
258 VISU_DeformedShapeAndScalarMapPL
259 ::SetScalars(vtkDataSet *theScalars)
261 if(GetScalars() == theScalars)
264 myScalars = vtkUnstructuredGrid::SafeDownCast(theScalars);
268 //----------------------------------------------------------------------------
270 * Get pointer to input scalars.
273 VISU_DeformedShapeAndScalarMapPL
276 return myScalars.GetPointer();
279 //----------------------------------------------------------------------------
281 * Removes all clipping planes (for myScalars)
284 VISU_DeformedShapeAndScalarMapPL
285 ::RemoveAllClippingPlanes()
287 if(vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()){
288 vtkImplicitFunctionCollection* aFunction = aBoolean->GetFunction();
289 aFunction->RemoveAllItems();
290 aBoolean->Modified();
292 Superclass::RemoveAllClippingPlanes();
295 //----------------------------------------------------------------------------
297 * Removes a clipping plane (for myScalars)
300 VISU_DeformedShapeAndScalarMapPL
301 ::RemoveClippingPlane(vtkIdType theID)
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();
309 Superclass::RemoveClippingPlane(theID);
312 //----------------------------------------------------------------------------
314 * Adds a clipping plane (for myScalars)
317 VISU_DeformedShapeAndScalarMapPL
318 ::AddClippingPlane(vtkPlane* thePlane)
321 if (vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()) {
322 vtkImplicitFunctionCollection* aFunction = aBoolean->GetFunction();
323 aFunction->AddItem(thePlane);
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)
332 return Superclass::AddClippingPlane(thePlane);
335 //----------------------------------------------------------------------------
337 * Sets implicit function of clipping
340 VISU_DeformedShapeAndScalarMapPL
341 ::SetImplicitFunction(vtkImplicitFunction *theFunction)
343 myExtractGeometry->SetImplicitFunction(theFunction);
346 //----------------------------------------------------------------------------
348 * Gets implicit function of clipping
350 vtkImplicitFunction *
351 VISU_DeformedShapeAndScalarMapPL
352 ::GetImplicitFunction()
354 return myExtractGeometry->GetImplicitFunction();
357 //----------------------------------------------------------------------------
359 * Sets scale for deformed shape
362 VISU_DeformedShapeAndScalarMapPL
363 ::SetScale(double theScale)
365 if(VISU::CheckIsSameValue(myScaleFactor, theScale))
368 myScaleFactor = theScale;
369 myWarpVector->SetScaleFactor(theScale*myMapScaleFactor);
372 //----------------------------------------------------------------------------
374 * Gets scale of deformed shape.
377 VISU_DeformedShapeAndScalarMapPL
380 return myScaleFactor;
383 //----------------------------------------------------------------------------
385 * Set scale factor of deformation.
388 VISU_DeformedShapeAndScalarMapPL
389 ::SetMapScale(double theMapScale)
391 myMapScaleFactor = theMapScale;
392 Superclass::SetMapScale(theMapScale);
393 myWarpVector->SetScaleFactor(myScaleFactor*theMapScale);
396 //----------------------------------------------------------------------------
401 VISU_DeformedShapeAndScalarMapPL
404 return myScalarsExtractor->GetScalarMode();
407 //----------------------------------------------------------------------------
412 VISU_DeformedShapeAndScalarMapPL
413 ::SetScalarMode(int theScalarMode)
415 VISU_ScalarMapPL::SetScalarMode(theScalarMode, GetScalars(), myScalarsExtractor);
418 //----------------------------------------------------------------------------
420 VISU_DeformedShapeAndScalarMapPL
421 ::SetScaling(int theScaling)
423 if(GetScaling() == theScaling)
426 GetBarTable()->SetScale(theScaling);
428 if(theScaling == VTK_SCALE_LOG10)
429 myScalarsFieldTransform->SetScalarTransform(&(VISU_FieldTransform::Log10));
431 myScalarsFieldTransform->SetScalarTransform(&(VISU_FieldTransform::Ident));
435 //----------------------------------------------------------------------------
437 VISU_DeformedShapeAndScalarMapPL
438 ::SetScalarRange(double theRange[2])
440 if (isnan(theRange[0]) || isnan(theRange[1]))
441 throw std::runtime_error("NAN values in the presentation");
443 if(VISU::CheckIsSameRange(theRange, GetScalarRange()))
446 myScalarsFieldTransform->SetScalarRange(theRange);
447 GetBarTable()->SetRange(theRange);
451 //----------------------------------------------------------------------------
453 VISU_DeformedShapeAndScalarMapPL
456 return myScalarsFieldTransform->GetScalarRange();
460 //----------------------------------------------------------------------------
462 * Gets ranges of extracted scalars
463 * \param theRange[2] - output values
464 * \li theRange[0] - minimum value
465 * \li theRange[1] - maximum value
468 VISU_DeformedShapeAndScalarMapPL
469 ::GetSourceRange(double theRange[2])
471 myScalarsExtractor->Update();
472 myScalarsExtractor->GetUnstructuredGridOutput()->GetScalarRange(theRange);
474 if (isnan(theRange[0]) || isnan(theRange[1]))
475 throw std::runtime_error("NAN values in the presentation");
479 //----------------------------------------------------------------------------
481 VISU_DeformedShapeAndScalarMapPL
482 ::SetGaussMetric(VISU::TGaussMetric theGaussMetric)
484 if(GetGaussMetric() == theGaussMetric)
487 myScalarsExtractor->SetGaussMetric(theGaussMetric);
491 //----------------------------------------------------------------------------
493 VISU_DeformedShapeAndScalarMapPL
496 return myScalarsExtractor->GetGaussMetric();