1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : VISU_FieldTransform.cxx
8 #include "VISU_FieldTransform.hxx"
9 #include "SALOME_Transform.h"
11 #include <vtkObjectFactory.h>
12 #include <vtkFloatArray.h>
13 #include <vtkPointData.h>
14 #include <vtkCellData.h>
15 #include <vtkDataSet.h>
21 static int MYDEBUG = 0;
23 static int MYDEBUG = 0;
26 vtkStandardNewMacro(VISU_FieldTransform);
28 double VISU_FieldTransform::Ident(double theArg){
31 double VISU_FieldTransform::Log10(double theArg){
32 if(theArg <= 0.0) return -VTK_LARGE_FLOAT;
37 VISU_FieldTransform::VISU_FieldTransform(){
42 VISU_FieldTransform::~VISU_FieldTransform() {}
45 void VISU_FieldTransform::Update(){
46 if(myTransform && myTransform->GetMTime() > vtkSource::GetMTime())
51 unsigned long VISU_FieldTransform::GetMTime(){
52 if(myTransform && myTransform->GetMTime() > vtkSource::GetMTime())
53 return myTransform->GetMTime();
54 return vtkSource::GetMTime();
57 void VISU_FieldTransform::SetScalarTransform(TTransformFun theFunction) {
58 myFunction = theFunction;
59 if(myFunction == NULL) myFunction = &Ident;
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);
73 void VISU_FieldTransform::SetScalarRange(float theScalarRange[2]) {
74 myScalarRange[0] = theScalarRange[0];
75 myScalarRange[1] = theScalarRange[1];
78 void VISU_FieldTransform::SetScalarMin(float theValue){
79 float aScalarRange[2] = {theValue, GetScalarRange()[1]};
80 SetScalarRange(aScalarRange);
82 void VISU_FieldTransform::SetScalarMax(float theValue){
83 float aScalarRange[2] = {GetScalarRange()[0], theValue};
84 SetScalarRange(aScalarRange);
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)
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};
102 aScale[0] = theTransform->GetScale()[0];
103 aScale[1] = theTransform->GetScale()[1];
104 aScale[2] = theTransform->GetScale()[2];
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]);
116 theOutputData->SetVectors(newVectors);
117 newVectors->Delete();
120 template<typename TypeData> void
121 ExecScalars(VISU_FieldTransform::TTransformFun theFunction, float theScalarRange[2],
122 int theNbComponent, TypeData* theInputData, TypeData* theOutputData)
124 vtkDataArray *inScalars = theInputData->GetScalars();
125 if ( !inScalars || theNbComponent < 1 )
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);
136 theOutputData->SetScalars(newScalars);
137 newScalars->Delete();
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();
147 ExecScalars(myFunction,myScalarRange,input->GetNumberOfPoints(),input->GetPointData(),output->GetPointData());
148 ExecVectors(myFunction,myTransform,myScalarRange,input->GetNumberOfPoints(),input->GetPointData(),output->GetPointData());
150 ExecScalars(myFunction,myScalarRange,input->GetNumberOfCells(),input->GetCellData(),output->GetCellData());
151 ExecVectors(myFunction,myTransform,myScalarRange,input->GetNumberOfCells(),input->GetCellData(),output->GetCellData());
153 output->GetPointData()->CopyAllOn();
154 output->GetCellData()->CopyAllOn();
156 output->GetPointData()->PassData(input->GetPointData());
157 output->GetCellData()->PassData(input->GetCellData());