Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / PIPELINE / VISU_DeformedShapePL.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 // File:    VISU_PipeLine.cxx
24 // Author:  Alexey PETROV
25 // Module : VISU
26
27
28 #include "VISU_DeformedShapePL.hxx"
29 #include "VISU_PipeLineUtils.hxx"
30 #include "VTKViewer_Transform.h"
31
32 #include <vtkWarpVector.h>
33
34 vtkStandardNewMacro(VISU_DeformedShapePL);
35
36 VISU_DeformedShapePL::VISU_DeformedShapePL(){
37   myWarpVector = vtkWarpVector::New();
38   myCellDataToPointData = vtkCellDataToPointData::New();
39 }
40
41 VISU_DeformedShapePL::~VISU_DeformedShapePL(){
42   myWarpVector->UnRegisterAllOutputs();
43   myWarpVector->Delete();
44
45   myCellDataToPointData->UnRegisterAllOutputs();
46   myCellDataToPointData->Delete();
47 }
48
49 void VISU_DeformedShapePL::ShallowCopy(VISU_PipeLine *thePipeLine){
50   if(VISU_DeformedShapePL *aPipeLine = dynamic_cast<VISU_DeformedShapePL*>(thePipeLine)){
51     SetScale(aPipeLine->GetScale());
52   }
53   VISU_ScalarMapPL::ShallowCopy(thePipeLine);
54 }
55
56 float VISU_DeformedShapePL::GetScaleFactor(vtkDataSet* theDataSet){
57   if(!theDataSet) return 0.0;
58   theDataSet->Update();
59   int aNbCells = theDataSet->GetNumberOfCells();
60   int aNbPoints = theDataSet->GetNumberOfPoints();
61   int aNbElem = aNbCells? aNbCells: aNbPoints;
62   float* aBounds = theDataSet->GetBounds();
63   float aVolume = 1, aVol, idim = 0;
64   for(int i = 0; i < 6; i += 2){
65     aVol = fabs(aBounds[i+1] - aBounds[i]);
66     if(aVol > 0) {
67       idim++;
68       aVolume *= aVol;
69     }
70   }
71   aVolume /= aNbElem;
72   return pow(aVolume,float(1.0/idim));
73 }
74
75
76 void VISU_DeformedShapePL::SetScale(float theScale) {
77   myWarpVector->SetScaleFactor(theScale);
78   myScaleFactor = theScale;
79   Modified();
80 }
81 float VISU_DeformedShapePL::GetScale() {
82   return myScaleFactor;
83 }
84
85 void VISU_DeformedShapePL::Init(){
86   VISU_ScalarMapPL::Init();
87   float aScalarRange[2];
88   GetSourceRange(aScalarRange);
89
90   vtkDataSet* aDataSet = GetInput2();
91   float aScaleFactor = VISU_DeformedShapePL::GetScaleFactor( aDataSet );
92
93   static double EPS = 1.0 / VTK_LARGE_FLOAT;
94   if(fabs(aScalarRange[1]) > EPS)
95     SetScale(aScaleFactor / aScalarRange[1]);
96   else
97     SetScale(0.0);
98 }
99
100 VISU_ScalarMapPL::THook* VISU_DeformedShapePL::DoHook(){
101   VISU::CellDataToPoint(myWarpVector,myCellDataToPointData,GetInput2(),myFieldTransform);
102   return myWarpVector->GetOutput();
103 }
104
105 void VISU_DeformedShapePL::Update(){
106   VISU_ScalarMapPL::Update();
107 }
108
109 void VISU_DeformedShapePL::SetMapScale(float theMapScale){
110   VISU_ScalarMapPL::SetMapScale(theMapScale);
111
112   myWarpVector->SetScaleFactor(myScaleFactor*theMapScale);
113   Modified();
114 }