Salome HOME
MPV: Merge V1_2d
[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 "SALOME_Transform.h"
31
32 #include <vtkWarpVector.h>
33
34 #ifdef _DEBUG_
35 static int MYDEBUG = 1;
36 #else
37 static int MYDEBUG = 0;
38 #endif
39
40 vtkStandardNewMacro(VISU_DeformedShapePL);
41
42 VISU_DeformedShapePL::VISU_DeformedShapePL(){
43   myWarpVector = vtkWarpVector::New();
44   myCellDataToPointData = vtkCellDataToPointData::New();
45 }
46
47 VISU_DeformedShapePL::~VISU_DeformedShapePL(){
48   myWarpVector->UnRegisterAllOutputs();
49   myWarpVector->Delete();
50
51   myCellDataToPointData->UnRegisterAllOutputs();
52   myCellDataToPointData->Delete();
53 }
54
55 void VISU_DeformedShapePL::ShallowCopy(VISU_PipeLine *thePipeLine){
56   VISU_ScalarMapPL::ShallowCopy(thePipeLine);
57   if(VISU_DeformedShapePL *aPipeLine = dynamic_cast<VISU_DeformedShapePL*>(thePipeLine)){
58     SetScale(aPipeLine->GetScale());
59   }
60 }
61
62 float VISU_DeformedShapePL::GetScaleFactor(vtkDataSet* theDataSet){
63   if(!theDataSet) return 0.0;
64   theDataSet->Update();
65   int aNbCells = theDataSet->GetNumberOfCells();
66   int aNbPoints = theDataSet->GetNumberOfPoints();
67   int aNbElem = aNbCells? aNbCells: aNbPoints;
68   float* aBounds = theDataSet->GetBounds();
69   float aVolume = 1, aVol, idim = 0;
70   for(int i = 0; i < 6; i += 2){
71     aVol = fabs(aBounds[i+1] - aBounds[i]);
72     if(aVol > 0) {
73       idim++; 
74       aVolume *= aVol;
75     }
76   }
77   aVolume /= aNbElem;
78   return pow(aVolume,float(1.0/idim));
79 }
80
81
82 void VISU_DeformedShapePL::SetScale(float theScale) { 
83   if(myScaleFactor == theScale) return;
84   myScaleFactor = theScale;
85   myWarpVector->SetScaleFactor(myScaleFactor);
86   Modified();
87 }
88 float VISU_DeformedShapePL::GetScale() { 
89   return myWarpVector->GetScaleFactor();
90 }
91
92 void VISU_DeformedShapePL::Init(){
93   VISU_ScalarMapPL::Init();
94   float aScalarRange[2];
95   GetSourceRange(aScalarRange);
96   SetScale(GetScaleFactor(myInput)/aScalarRange[1]);
97 }
98
99 VISU_ScalarMapPL::THook* VISU_DeformedShapePL::DoHook(){
100   VISU::CellDataToPoint(myWarpVector,myCellDataToPointData,myInput,myFieldTransform);
101   return myWarpVector->GetOutput();
102 }
103
104 void VISU_DeformedShapePL::Update(){
105   VISU_ScalarMapPL::Update();
106 }
107
108 void VISU_DeformedShapePL::SetMapScale(float theMapScale){
109   VISU_ScalarMapPL::SetMapScale(theMapScale);
110
111   myWarpVector->SetScaleFactor(myScaleFactor*theMapScale);
112   Modified();
113 }