1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : VISU_FieldTransform.cxx
8 #include "VISU_FieldTransform.hxx"
9 #include "VTKViewer_Transform.h"
11 #include <vtkObjectFactory.h>
12 #include <vtkFloatArray.h>
13 #include <vtkPointData.h>
14 #include <vtkCellData.h>
15 #include <vtkDataSet.h>
18 static vtkFloatingPointType Tolerance = 1.0 / VTK_LARGE_FLOAT;
22 vtkStandardNewMacro(VISU_FieldTransform);
26 ::Ident(double theArg)
33 ::Log10(double theArg)
36 return -VTK_LARGE_FLOAT;
43 ::VISU_FieldTransform()
48 myScalarRange[0] = VTK_LARGE_FLOAT;
49 myScalarRange[1] = -VTK_LARGE_FLOAT;
53 ::~VISU_FieldTransform()
55 SetSpaceTransform(NULL);
70 unsigned long aTime = Superclass::GetMTime();
72 aTime = max(aTime,myTransform->GetMTime());
79 ::SetScalarTransform(TTransformFun theFunction)
81 if(myFunction == theFunction)
84 if(theFunction == NULL)
87 myFunction = theFunction;
92 void VISU_FieldTransform::SetSpaceTransform(VTKViewer_Transform* theTransform){
93 if(myTransform == theTransform)
96 if(myTransform != NULL)
97 myTransform->UnRegister(this);
99 myTransform = theTransform;
101 if(theTransform != NULL)
102 theTransform->Register(this);
110 ::SetScalarRange(vtkFloatingPointType theScalarRange[2])
112 vtkFloatingPointType aDelta =
113 fabs(myScalarRange[0] - theScalarRange[0]) +
114 fabs(myScalarRange[1] - theScalarRange[1]);
115 if(aDelta < Tolerance)
118 myScalarRange[0] = theScalarRange[0];
119 myScalarRange[1] = theScalarRange[1];
126 ::SetScalarMin(vtkFloatingPointType theValue)
128 vtkFloatingPointType aScalarRange[2] = {theValue, GetScalarRange()[1]};
129 SetScalarRange(aScalarRange);
134 ::SetScalarMax(vtkFloatingPointType theValue)
136 vtkFloatingPointType aScalarRange[2] = {GetScalarRange()[0], theValue};
137 SetScalarRange(aScalarRange);
141 template<typename TypeData> void
142 ExecVectors(VISU_FieldTransform::TTransformFun theFunction,
143 VTKViewer_Transform* theTransform,
144 vtkFloatingPointType theScalarRange[2],
146 TypeData* theInputData,
147 TypeData* theOutputData)
149 vtkDataArray *anInVectors = theInputData->GetVectors();
150 if ( !anInVectors || theNbOfTuples < 1 )
152 vtkFloatArray *aNewVectors = vtkFloatArray::New();
153 aNewVectors->SetNumberOfComponents(3);
154 aNewVectors->SetNumberOfTuples(theNbOfTuples);
155 vtkFloatingPointType aScalarRange[2] = {(*theFunction)(theScalarRange[0]),(*theFunction)(theScalarRange[1])};
156 vtkFloatingPointType aDelta = aScalarRange[1] - aScalarRange[0];
157 vtkFloatingPointType aScale[3] = {1.0, 1.0, 1.0};
158 static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
160 aScale[0] = theTransform->GetScale()[0];
161 aScale[1] = theTransform->GetScale()[1];
162 aScale[2] = theTransform->GetScale()[2];
164 if(theFunction == &(VISU_FieldTransform::Ident)){
165 for (int aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++) {
166 vtkFloatingPointType anInVec[3];
167 anInVectors->GetTuple(aTupleId,anInVec);
168 vtkFloatingPointType anNewVec[3];
169 anNewVec[0] = anInVec[0]*aScale[0];
170 anNewVec[1] = anInVec[1]*aScale[1];
171 anNewVec[2] = anInVec[2]*aScale[2];
172 aNewVectors->SetTuple(aTupleId,anNewVec);
175 for (int aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++) {
176 vtkFloatingPointType anInVec[3];
177 anInVectors->GetTuple(aTupleId,anInVec);
178 vtkFloatingPointType aMagn = vtkMath::Norm(anInVec);
180 aMagn = ((*theFunction)(aMagn) - aScalarRange[0]) / aDelta * theScalarRange[1] / aMagn;
183 vtkFloatingPointType anNewVec[3];
184 anNewVec[0] = anInVec[0]*aMagn*aScale[0];
185 anNewVec[1] = anInVec[1]*aMagn*aScale[1];
186 anNewVec[2] = anInVec[2]*aMagn*aScale[2];
187 aNewVectors->SetTuple(aTupleId,anNewVec);
190 theOutputData->SetVectors(aNewVectors);
191 aNewVectors->Delete();
194 template<typename TypeData> void
195 ExecScalars(VISU_FieldTransform::TTransformFun theFunction,
196 vtkFloatingPointType theScalarRange[2],
198 TypeData* theInputData,
199 TypeData* theOutputData)
201 vtkDataArray *anInScalars = theInputData->GetScalars();
202 if ( !anInScalars || theNbOfTuples < 1 )
204 vtkFloatArray *aNewScalars = vtkFloatArray::New();
205 aNewScalars->SetNumberOfComponents(1);
206 aNewScalars->SetNumberOfTuples(theNbOfTuples);
207 vtkFloatingPointType aScalarRange[2] = {(*theFunction)(theScalarRange[0]),(*theFunction)(theScalarRange[1])};
208 for (int aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++) {
209 vtkFloatingPointType aScalar = (*theFunction)(anInScalars->GetTuple1(aTupleId));
210 if(aScalar < aScalarRange[0])
211 aScalar = aScalarRange[0];
212 aNewScalars->SetTuple1(aTupleId,aScalar);
214 theOutputData->SetScalars(aNewScalars);
215 aNewScalars->Delete();
222 vtkDataSet *input = this->GetInput(), *output = this->GetOutput();
223 output->CopyStructure(input);
224 if(myFunction != &Ident || (myTransform && !myTransform->IsIdentity())){
225 output->GetPointData()->CopyAllOff();
226 output->GetCellData()->CopyAllOff();
228 ExecScalars(myFunction,myScalarRange,input->GetNumberOfPoints(),input->GetPointData(),output->GetPointData());
229 ExecVectors(myFunction,myTransform,myScalarRange,input->GetNumberOfPoints(),input->GetPointData(),output->GetPointData());
231 ExecScalars(myFunction,myScalarRange,input->GetNumberOfCells(),input->GetCellData(),output->GetCellData());
232 ExecVectors(myFunction,myTransform,myScalarRange,input->GetNumberOfCells(),input->GetCellData(),output->GetCellData());
234 output->GetPointData()->CopyAllOn();
235 output->GetCellData()->CopyAllOn();
237 output->GetPointData()->PassData(input->GetPointData());
238 output->GetCellData()->PassData(input->GetCellData());