]> SALOME platform Git repositories - modules/visu.git/blob - src/PIPELINE/VISU_FieldTransform.cxx
Salome HOME
sources v1.2c
[modules/visu.git] / src / PIPELINE / VISU_FieldTransform.cxx
1 //  Copyright (C) 2003  CEA/DEN, EDF R&D
2 //
3 //
4 //
5 //  File   : VISU_FieldTransform.cxx
6 //  Module : VISU
7
8 #include "VISU_FieldTransform.hxx"   
9 #include "SALOME_Transform.h"
10
11 #include <vtkObjectFactory.h>
12 #include <vtkFloatArray.h>
13 #include <vtkPointData.h>
14 #include <vtkCellData.h>
15 #include <vtkDataSet.h>
16 #include <vtkMath.h>
17
18 using namespace std;
19
20 #ifdef DEBUG
21 static int MYDEBUG = 0;
22 #else
23 static int MYDEBUG = 0;
24 #endif
25
26 vtkStandardNewMacro(VISU_FieldTransform);
27
28 double VISU_FieldTransform::Ident(double theArg){ 
29   return theArg;
30 }
31 double VISU_FieldTransform::Log10(double theArg){ 
32   if(theArg <= 0.0) return -VTK_LARGE_FLOAT;
33   return log10(theArg);
34 }
35
36
37 VISU_FieldTransform::VISU_FieldTransform(){
38   myFunction = &Ident;
39   myTransform = NULL;
40 }
41
42 VISU_FieldTransform::~VISU_FieldTransform() {}
43
44
45 void VISU_FieldTransform::Update(){
46   if(myTransform && myTransform->GetMTime() > vtkSource::GetMTime())
47     Modified();
48   vtkSource::Update();
49 }
50
51 unsigned long VISU_FieldTransform::GetMTime(){
52   if(myTransform && myTransform->GetMTime() > vtkSource::GetMTime())
53     return myTransform->GetMTime();
54   return vtkSource::GetMTime();
55 }
56
57 void VISU_FieldTransform::SetScalarTransform(TTransformFun theFunction) { 
58   myFunction = theFunction;
59   if(myFunction == NULL) myFunction = &Ident;
60   Modified();
61 }
62
63 void VISU_FieldTransform::SetSpaceTransform(SALOME_Transform* theTransform){
64   if(myTransform != theTransform){
65     if (myTransform != NULL) myTransform->UnRegister(this);
66     myTransform = theTransform;
67     if (myTransform != NULL) myTransform->Register(this);
68     this->Modified();
69   }
70 }
71
72
73 void VISU_FieldTransform::SetScalarRange(float theScalarRange[2]) { 
74   myScalarRange[0] = theScalarRange[0];
75   myScalarRange[1] = theScalarRange[1];
76   Modified();
77 }
78 void VISU_FieldTransform::SetScalarMin(float theValue){
79   float aScalarRange[2] = {theValue, GetScalarRange()[1]};
80   SetScalarRange(aScalarRange);
81 }
82 void VISU_FieldTransform::SetScalarMax(float theValue){
83   float aScalarRange[2] = {GetScalarRange()[0], theValue};
84   SetScalarRange(aScalarRange);
85 }
86
87 template<typename TypeData> void
88 ExecVectors(VISU_FieldTransform::TTransformFun theFunction, 
89             SALOME_Transform* theTransform,
90             float theScalarRange[2], int theNbComponent, 
91             TypeData* theInputData, TypeData* theOutputData)
92 {
93   vtkDataArray *inVectors = theInputData->GetVectors();
94   if ( !inVectors || theNbComponent < 1 ) return; 
95   vtkFloatArray *newVectors = vtkFloatArray::New();
96   newVectors->SetNumberOfComponents(3);
97   newVectors->SetNumberOfTuples(theNbComponent);
98   float aScalarRange[2] = {(*theFunction)(theScalarRange[0]),(*theFunction)(theScalarRange[1])};
99   float *V, v[3], vMag, aDelta = aScalarRange[1] - aScalarRange[0];
100   float aScale[3] = {1.0, 1.0, 1.0};
101   if(theTransform){
102     aScale[0] = theTransform->GetScale()[0];
103     aScale[1] = theTransform->GetScale()[1];
104     aScale[2] = theTransform->GetScale()[2];
105   }
106   for (int ptId = 0; ptId < theNbComponent; ptId++) {
107     V = inVectors->GetTuple3(ptId);
108     vMag = vtkMath::Norm(V);
109     vMag = ((*theFunction)(vMag) - aScalarRange[0]) / aDelta * theScalarRange[1] / vMag;
110     if(vMag <= 0.0) vMag = 0.0;
111     v[0] = V[0]*vMag*aScale[0];
112     v[1] = V[1]*vMag*aScale[1];
113     v[2] = V[2]*vMag*aScale[2];
114     newVectors->SetTuple3(ptId, v[0], v[1], v[2]);
115   }
116   theOutputData->SetVectors(newVectors);
117   newVectors->Delete();
118 }
119
120 template<typename TypeData> void
121 ExecScalars(VISU_FieldTransform::TTransformFun theFunction, float theScalarRange[2],
122             int theNbComponent, TypeData* theInputData, TypeData* theOutputData)
123 {
124   vtkDataArray *inScalars = theInputData->GetScalars();
125   if ( !inScalars || theNbComponent < 1 ) 
126     return; 
127   vtkFloatArray *newScalars = vtkFloatArray::New();
128   newScalars->SetNumberOfComponents(1);
129   newScalars->SetNumberOfTuples(theNbComponent);
130   float aScalarRange[2] = {(*theFunction)(theScalarRange[0]),(*theFunction)(theScalarRange[1])};
131   for (int ptId = 0; ptId < theNbComponent; ptId++) {
132     float s = (*theFunction)(inScalars->GetTuple1(ptId ));
133     if(s < aScalarRange[0]) s = aScalarRange[0];
134     newScalars->SetTuple1(ptId, s);
135   }
136   theOutputData->SetScalars(newScalars);
137   newScalars->Delete();
138 }
139
140 void VISU_FieldTransform::Execute(){
141   vtkDataSet *input = this->GetInput(), *output = this->GetOutput();
142   output->CopyStructure(input);
143   if(myFunction != &Ident || (myTransform && !myTransform->IsIdentity())){
144     output->GetPointData()->CopyAllOff();
145     output->GetCellData()->CopyAllOff();
146
147     ExecScalars(myFunction,myScalarRange,input->GetNumberOfPoints(),input->GetPointData(),output->GetPointData());
148     ExecVectors(myFunction,myTransform,myScalarRange,input->GetNumberOfPoints(),input->GetPointData(),output->GetPointData());
149
150     ExecScalars(myFunction,myScalarRange,input->GetNumberOfCells(),input->GetCellData(),output->GetCellData());
151     ExecVectors(myFunction,myTransform,myScalarRange,input->GetNumberOfCells(),input->GetCellData(),output->GetCellData());
152   }else{
153     output->GetPointData()->CopyAllOn();
154     output->GetCellData()->CopyAllOn();
155
156     output->GetPointData()->PassData(input->GetPointData());
157     output->GetCellData()->PassData(input->GetCellData());
158   }
159 }