Back to index

salome-paravis  6.5.0
vtkScaleVectorFilter.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "vtkScaleVectorFilter.h"
00021 #include "vtkInformation.h"
00022 #include "vtkInformationVector.h"
00023 #include "vtkObjectFactory.h"
00024 #include "vtkCellArray.h"
00025 #include "vtkPointData.h"
00026 #include "vtkDataSet.h"
00027 
00028 #include <string>
00029 
00030 using namespace std;
00031 
00032 vtkCxxRevisionMacro(vtkScaleVectorFilter, "$Revision: 1.1.4.2.12.1 $");
00033 vtkStandardNewMacro(vtkScaleVectorFilter);
00034 
00035 vtkScaleVectorFilter::vtkScaleVectorFilter()
00036 {
00037   this->ScaleFactor = 0.5;
00038 }
00039 
00040 vtkScaleVectorFilter::~vtkScaleVectorFilter()
00041 {
00042 }
00043 
00044 //----------------------------------------------------------------------------
00045 int vtkScaleVectorFilter::FillInputPortInformation(
00046   int vtkNotUsed(port), vtkInformation* info)
00047 {
00048   // now add our info
00049   info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
00050   return 1;
00051 }
00052 
00053 
00054 int vtkScaleVectorFilter::RequestData(vtkInformation *vtkNotUsed(request),
00055     vtkInformationVector **input, vtkInformationVector *output)
00056 {
00057   vtkDataSet *dsIn = vtkDataSet::SafeDownCast(
00058       input[0]->GetInformationObject(0)->Get(vtkDataObject::DATA_OBJECT()));
00059 
00060   vtkDataSet *dsOut = vtkDataSet::SafeDownCast(
00061       output->GetInformationObject(0)->Get(vtkDataObject::DATA_OBJECT()));
00062 
00063   vtkDataArray* array = this->GetInputArrayToProcess(0, input);
00064 
00065   if(dsIn == NULL || array == NULL || dsOut == NULL
00066      || array->GetNumberOfComponents() != 3 ||
00067      this->GetInputArrayAssociation(0, input) != vtkDataObject::FIELD_ASSOCIATION_POINTS)
00068     {
00069     vtkDebugMacro("vtkScaleVectorFilter no correctly configured");
00070     return 1;
00071     }
00072 
00073   dsOut->ShallowCopy(dsIn);
00074 
00075   vtkPointData* inPD = dsIn->GetPointData();
00076   vtkPointData* outPD = dsOut->GetPointData();
00077 
00078   outPD->RemoveArray(array->GetName());
00079 
00080   vtkDataArray* outArray = array->NewInstance();
00081   outArray->SetName(array->GetName());
00082 
00083   string name = array->GetName();
00084   size_t pos1 = name.find_last_of("[");
00085   if(pos1 != string::npos)
00086     {
00087     string name1 = name.substr(0, pos1);
00088     size_t noblankpos = name1.find_last_not_of(" ");
00089     name1 = name1.substr(0, noblankpos+1);
00090 
00091     outArray->SetName(name1.c_str());
00092 
00093     for(int arrayId = 0; arrayId < inPD->GetNumberOfArrays(); arrayId++)
00094       {
00095       vtkDataArray* thearray = inPD->GetArray(arrayId);
00096       if(thearray == NULL)
00097         continue;
00098 
00099       if(strncmp(name1.c_str(), thearray->GetName(), name1.size()) == 0)
00100         {
00101         outPD->RemoveArray(thearray->GetName());
00102         }
00103       }
00104     }
00105 
00106   outPD->AddArray(outArray);
00107   outArray->Delete();
00108 
00109   outArray->SetNumberOfComponents(array->GetNumberOfComponents());
00110   outArray->SetNumberOfTuples(array->GetNumberOfTuples());
00111 
00112   if(array->HasAComponentName())
00113     {
00114     outArray->CopyComponentNames(array);
00115     }
00116 
00117   if(array->HasInformation())
00118     {
00119     outArray->CopyInformation(array->GetInformation());
00120     }
00121 
00122   for(vtkIdType id=0; id < outArray->GetNumberOfTuples(); id++)
00123     {
00124     double tuple[3];
00125     array->GetTuple(id, tuple);
00126     tuple[0] *= this->ScaleFactor;
00127     tuple[1] *= this->ScaleFactor;
00128     tuple[2] *= this->ScaleFactor;
00129     outArray->SetTuple(id, tuple);
00130     }
00131   // fix the range keys to match the full range, so that the
00132   // gui automatically sets the range.
00133   double range[2];
00134   vtkInformation* info = outArray->GetInformation();
00135 
00136   array->GetRange(range, -1);
00137   range[0] = 0;
00138   info->Set(vtkDataArray::L2_NORM_RANGE(), range, 2);
00139 
00140   if(!outArray->GetInformation()->Has(vtkDataArray::PER_COMPONENT()))
00141     {
00142     vtkInformationVector* infoVec = vtkInformationVector::New();
00143     info->Set( vtkDataArray::PER_COMPONENT(), infoVec );
00144     infoVec->FastDelete();
00145     }
00146   vtkInformationVector* per_comp = info->Get(vtkDataArray::PER_COMPONENT());
00147   per_comp->SetNumberOfInformationObjects(outArray->GetNumberOfComponents());
00148   for(int comp = 0; comp < outArray->GetNumberOfComponents(); comp++)
00149     {
00150     array->GetRange(range, comp);
00151     range[0] = fabs(range[0]);
00152     range[1] = fabs(range[1]);
00153     double r = (range[0] > range[1] ? range[0] : range[1]);
00154     range[0] = -r;
00155     range[1] = r;
00156     per_comp->GetInformationObject(comp)->Set(
00157         vtkDataArray::COMPONENT_RANGE(), range, 2 );
00158     }
00159 
00160   outPD->SetActiveVectors(outArray->GetName());
00161 
00162   return 1;
00163 }
00164 
00165 void vtkScaleVectorFilter::PrintSelf(ostream& os, vtkIndent indent)
00166 {
00167   this->Superclass::PrintSelf(os, indent);
00168 
00169   os << indent << "ScaleFactor : " << this->ScaleFactor << endl;
00170 }