1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : VISU_FieldTransform.cxx
26 #include "VISU_FieldTransform.hxx"
27 #include "VTKViewer_Transform.h"
28 #include "VISU_PipeLineUtils.hxx"
30 #include <vtkObjectFactory.h>
31 #include <vtkPointData.h>
32 #include <vtkCellData.h>
33 #include <vtkDataSet.h>
35 #include <vtkInformation.h>
36 #include <vtkInformationVector.h>
38 static vtkFloatingPointType Tolerance = 1.0 / VTK_LARGE_FLOAT;
40 //----------------------------------------------------------------------------
41 vtkStandardNewMacro(VISU_FieldTransform);
43 //----------------------------------------------------------------------------
46 ::Ident(double theArg)
51 //----------------------------------------------------------------------------
54 ::Log10(double theArg)
57 return -VTK_LARGE_FLOAT;
63 //----------------------------------------------------------------------------
65 ::VISU_FieldTransform()
70 myScalarRange[0] = VTK_LARGE_FLOAT;
71 myScalarRange[1] = -VTK_LARGE_FLOAT;
74 //----------------------------------------------------------------------------
76 ::~VISU_FieldTransform()
78 SetSpaceTransform(NULL);
86 unsigned long aTime = Superclass::GetMTime();
88 aTime = std::max(aTime, myTransform->GetMTime());
93 //----------------------------------------------------------------------------
96 ::SetScalarTransform(TTransformFun theFunction)
98 if(myFunction == theFunction)
101 if(theFunction == NULL)
102 theFunction = &Ident;
104 myFunction = theFunction;
109 //----------------------------------------------------------------------------
112 ::SetSpaceTransform(VTKViewer_Transform* theTransform)
114 if(myTransform == theTransform)
117 if(myTransform != NULL)
118 myTransform->UnRegister(this);
120 myTransform = theTransform;
122 if(theTransform != NULL)
123 theTransform->Register(this);
129 //----------------------------------------------------------------------------
132 ::SetScalarRange(vtkFloatingPointType theScalarRange[2])
134 if(VISU::CheckIsSameRange(theScalarRange, myScalarRange))
137 myScalarRange[0] = theScalarRange[0];
138 myScalarRange[1] = theScalarRange[1];
143 //----------------------------------------------------------------------------
146 ::SetScalarMin(vtkFloatingPointType theValue)
148 vtkFloatingPointType aScalarRange[2] = {theValue, GetScalarRange()[1]};
149 SetScalarRange(aScalarRange);
152 //----------------------------------------------------------------------------
155 ::SetScalarMax(vtkFloatingPointType theValue)
157 vtkFloatingPointType aScalarRange[2] = {GetScalarRange()[0], theValue};
158 SetScalarRange(aScalarRange);
161 //----------------------------------------------------------------------------
162 template<typename TValueType>
164 LinearTransformVectors(TValueType* theInputPtr,
165 TValueType* theOutputPtr,
166 vtkIdType theNbOfTuples,
167 vtkFloatingPointType theScale[3])
169 for(vtkIdType aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++){
170 for(vtkIdType aComponentId = 0; aComponentId < 3; aComponentId++){
171 *theOutputPtr = TValueType(*theInputPtr * theScale[aComponentId]);
179 //----------------------------------------------------------------------------
180 template<typename TValueType>
182 NonLinearTransformVectors(vtkDataArray *theInputVectors,
183 TValueType* theInputPtr,
184 TValueType* theOutputPtr,
185 vtkIdType theNbOfTuples,
186 vtkFloatingPointType theScale[3],
187 VISU_FieldTransform::TTransformFun theFunction,
188 vtkFloatingPointType theModifiedScalarMin,
189 vtkFloatingPointType theModifiedScalarDelta,
190 vtkFloatingPointType theSourceScalarMax)
192 for(vtkIdType aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++){
193 vtkFloatingPointType anInputVector[3];
194 theInputVectors->GetTuple(aTupleId, anInputVector);
195 vtkFloatingPointType aMagnification = vtkMath::Norm(anInputVector);
196 if(aMagnification > Tolerance)
198 ((*theFunction)(aMagnification) - theModifiedScalarMin) /
199 theModifiedScalarDelta * theSourceScalarMax /
201 if(aMagnification < 0.0)
202 aMagnification = 0.0;
203 for(vtkIdType aComponentId = 0; aComponentId < 3; aComponentId++){
204 *theOutputPtr = TValueType(*theInputPtr * aMagnification * theScale[aComponentId]);
212 //----------------------------------------------------------------------------
213 template<typename TDataSetAttributesType>
215 ExecuteVectors(VISU_FieldTransform::TTransformFun theFunction,
216 VTKViewer_Transform* theTransform,
217 vtkFloatingPointType theScalarRange[2],
218 vtkIdType theNbOfTuples,
219 TDataSetAttributesType* theInputData,
220 TDataSetAttributesType* theOutputData)
222 vtkDataArray *anInputVectors = theInputData->GetVectors();
223 if(!anInputVectors || theNbOfTuples < 1)
226 vtkFloatingPointType aScalarRange[2];
227 aScalarRange[0] = (*theFunction)(theScalarRange[0]);
228 aScalarRange[1] = (*theFunction)(theScalarRange[1]);
230 vtkFloatingPointType aScalarDelta = aScalarRange[1] - aScalarRange[0];
231 vtkFloatingPointType aScale[3] = {1.0, 1.0, 1.0};
234 aScale[0] = theTransform->GetScale()[0];
235 aScale[1] = theTransform->GetScale()[1];
236 aScale[2] = theTransform->GetScale()[2];
239 vtkIdType anInputDataType = anInputVectors->GetDataType();
240 vtkDataArray *anOutputVectors = vtkDataArray::CreateDataArray(anInputDataType);
241 anOutputVectors->SetNumberOfComponents(3);
242 anOutputVectors->SetNumberOfTuples(theNbOfTuples);
244 void *anInputPtr = anInputVectors->GetVoidPointer(0);
245 void *anOutputPtr = anOutputVectors->GetVoidPointer(0);
247 if(theFunction == &(VISU_FieldTransform::Ident)){
248 switch(anInputDataType){
249 vtkTemplateMacro4(LinearTransformVectors,
250 (VTK_TT *)(anInputPtr),
251 (VTK_TT *)(anOutputPtr),
258 switch(anInputDataType){
259 vtkTemplateMacro9(NonLinearTransformVectors,
261 (VTK_TT *)(anInputPtr),
262 (VTK_TT *)(anOutputPtr),
274 theOutputData->SetVectors(anOutputVectors);
275 anOutputVectors->Delete();
279 //----------------------------------------------------------------------------
280 template<typename TValueType>
282 NonLinearTransformScalars(vtkDataArray *theInputScalars,
283 TValueType* theInputPtr,
284 TValueType* theOutputPtr,
285 vtkIdType theNbOfTuples,
286 VISU_FieldTransform::TTransformFun theFunction,
287 vtkFloatingPointType theModifiedScalarMin)
289 for(vtkIdType aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++){
290 vtkFloatingPointType aScalar = (*theFunction)(vtkFloatingPointType(*theInputPtr));
291 if(aScalar < theModifiedScalarMin)
292 aScalar = theModifiedScalarMin;
293 *theOutputPtr = TValueType(aScalar);
300 //----------------------------------------------------------------------------
301 template<typename TDataSetAttributesType>
303 ExecuteScalars(VISU_FieldTransform::TTransformFun theFunction,
304 vtkFloatingPointType theScalarRange[2],
305 vtkIdType theNbOfTuples,
306 TDataSetAttributesType* theInputData,
307 TDataSetAttributesType* theOutputData)
309 vtkDataArray *anInputScalars = theInputData->GetScalars();
310 if(!anInputScalars || theNbOfTuples < 1)
313 vtkFloatingPointType aScalarRange[2];
314 aScalarRange[0] = (*theFunction)(theScalarRange[0]);
315 aScalarRange[1] = (*theFunction)(theScalarRange[1]);
317 vtkIdType anInputDataType = anInputScalars->GetDataType();
318 vtkDataArray *anOutputScalars = vtkDataArray::CreateDataArray(anInputDataType);
319 anOutputScalars->SetNumberOfComponents(1);
320 anOutputScalars->SetNumberOfTuples(theNbOfTuples);
322 void *anInputPtr = anInputScalars->GetVoidPointer(0);
323 void *anOutputPtr = anOutputScalars->GetVoidPointer(0);
325 switch(anInputDataType){
326 vtkTemplateMacro6(NonLinearTransformScalars,
328 (VTK_TT *)(anInputPtr),
329 (VTK_TT *)(anOutputPtr),
337 theOutputData->SetScalars(anOutputScalars);
338 anOutputScalars->Delete();
342 //----------------------------------------------------------------------------
345 ::RequestData(vtkInformation *vtkNotUsed(request),
346 vtkInformationVector **inputVector,
347 vtkInformationVector *outputVector)
349 // get the info objects
350 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
351 vtkInformation *outInfo = outputVector->GetInformationObject(0);
353 // get the input and ouptut
354 vtkDataSet *input = vtkDataSet::SafeDownCast(
355 inInfo->Get(vtkDataObject::DATA_OBJECT()));
356 vtkDataSet *output = vtkDataSet::SafeDownCast(
357 outInfo->Get(vtkDataObject::DATA_OBJECT()));
359 output->CopyStructure(input);
360 if(myFunction != &Ident || (myTransform && !myTransform->IsIdentity())){
361 output->GetPointData()->CopyScalarsOff();
362 output->GetPointData()->CopyVectorsOff();
364 output->GetCellData()->CopyScalarsOff();
365 output->GetCellData()->CopyVectorsOff();
367 ExecuteScalars(myFunction,
369 input->GetNumberOfPoints(),
370 input->GetPointData(),
371 output->GetPointData());
373 ExecuteVectors(myFunction,
376 input->GetNumberOfPoints(),
377 input->GetPointData(),
378 output->GetPointData());
380 ExecuteScalars(myFunction,
382 input->GetNumberOfCells(),
383 input->GetCellData(),
384 output->GetCellData());
386 ExecuteVectors(myFunction,
389 input->GetNumberOfCells(),
390 input->GetCellData(),
391 output->GetCellData());
393 output->GetPointData()->CopyAllOn();
394 output->GetCellData()->CopyAllOn();
396 output->GetPointData()->PassData(input->GetPointData());
397 output->GetCellData()->PassData(input->GetCellData());
400 output->GetPointData()->PassData(input->GetPointData());
401 output->GetCellData()->PassData(input->GetCellData());
406 //----------------------------------------------------------------------------