Back to index

salome-gui  6.5.0
VTKViewer_AppendFilter.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME OBJECT : kernel of SALOME component
00024 //  File   : VTKViewer_GeometryFilter.cxx
00025 //  Author : 
00026 
00027 #include "VTKViewer_AppendFilter.h"
00028 
00029 #include <vtkCell.h>
00030 #include <vtkCellData.h>
00031 #include <vtkDataSetAttributes.h>
00032 #include <vtkDataSetCollection.h>
00033 #include <vtkObjectFactory.h>
00034 #include <vtkPointData.h>
00035 #include <vtkUnstructuredGrid.h>
00036 #include <vtkInformation.h>
00037 #include <vtkInformationVector.h>
00038 
00039 #include <vtkPoints.h>
00040 
00041 #if defined __GNUC__
00042   #if __GNUC__ == 2
00043     #define __GNUC_2__
00044   #endif
00045 #endif
00046 
00047 vtkCxxRevisionMacro(VTKViewer_AppendFilter, "$Revision: 1.7.2.1.8.4.2.1 $");
00048 vtkStandardNewMacro(VTKViewer_AppendFilter);
00049 
00050 VTKViewer_AppendFilter
00051 ::VTKViewer_AppendFilter() 
00052 {
00053   myDoMappingFlag = false;
00054 }
00055 
00056 VTKViewer_AppendFilter
00057 ::~VTKViewer_AppendFilter()
00058 {}
00059 
00060 void
00061 VTKViewer_AppendFilter
00062 ::SetDoMappingFlag(const bool theFlag)
00063 {
00064   if(myDoMappingFlag == theFlag)
00065     return;
00066 
00067   myDoMappingFlag = theFlag;
00068 
00069   this->Modified();
00070 }
00071 
00072 bool 
00073 VTKViewer_AppendFilter
00074 ::DoMappingFlag() const
00075 {
00076   return myDoMappingFlag;
00077 }
00078 
00079 void
00080 VTKViewer_AppendFilter
00081 ::SetSharedPointsDataSet(vtkPointSet* thePointsDataSet)
00082 {
00083   if(GetSharedPointsDataSet() == thePointsDataSet)
00084     return;
00085 
00086   mySharedPointsDataSet = thePointsDataSet;
00087 
00088   Modified();
00089 }
00090 
00091 vtkPointSet*
00092 VTKViewer_AppendFilter
00093 ::GetSharedPointsDataSet()
00094 {
00095   return mySharedPointsDataSet.GetPointer();
00096 }
00097 
00098 int
00099 VTKViewer_AppendFilter
00100 ::RequestData(
00101               vtkInformation *request,
00102               vtkInformationVector **inputVector,
00103               vtkInformationVector *outputVector)
00104 {
00105   int aRet = 0;
00106   if(GetSharedPointsDataSet())
00107     aRet = MakeOutput(request,inputVector,outputVector);
00108   else
00109     aRet = Superclass::RequestData(request,inputVector,outputVector);
00110 
00111   if(myDoMappingFlag)
00112     DoMapping();
00113   
00114   return aRet;
00115 }
00116 
00117 
00118 void 
00119 VTKViewer_AppendFilter
00120 ::DoMapping()
00121 {
00122   myNodeRanges.clear();
00123   myCellRanges.clear();
00124 
00125   vtkIdType aPntStartId = 0;
00126   vtkIdType aCellStartId = 0;
00127 
00128   for(vtkIdType aDataSetId = 0; aDataSetId < this->GetNumberOfInputPorts(); ++aDataSetId){
00129     vtkDataSet* aDataSet = (vtkDataSet *)(this->GetInput(aDataSetId));
00130     // Do mapping of the nodes
00131     if(!GetSharedPointsDataSet()){
00132       vtkIdType aNbPnts = aDataSet->GetNumberOfPoints();
00133       myNodeRanges.push_back(aPntStartId + aNbPnts);
00134       aPntStartId += aNbPnts;
00135     }
00136     // Do mapping of the cells
00137     vtkIdType aNbCells = aDataSet->GetNumberOfCells();
00138     myCellRanges.push_back(aCellStartId + aNbCells);
00139     aCellStartId += aNbCells;
00140   }
00141 }
00142 
00143 namespace
00144 {
00145   inline
00146   vtkIdType
00147   GetOutputID(vtkIdType theInputID,
00148               vtkIdType theInputDataSetID,
00149               const VTKViewer_AppendFilter::TVectorIds& theRanges)
00150   {
00151     theInputID = theInputDataSetID = -1;
00152 
00153     vtkIdType aNbInputs = theRanges.size();
00154     if(theInputDataSetID < 0 || theInputDataSetID >= aNbInputs)
00155       return -1;
00156     
00157     vtkIdType aStartId = theRanges[theInputDataSetID];
00158     return aStartId + theInputID;
00159   }
00160 }
00161 
00162 vtkIdType
00163 VTKViewer_AppendFilter
00164 ::GetPointOutputID(vtkIdType theInputID,
00165                    vtkIdType theInputDataSetID)
00166 {
00167   if(GetSharedPointsDataSet())
00168     return theInputID;
00169 
00170   return GetOutputID(theInputID,theInputDataSetID,myNodeRanges);
00171 }
00172 
00173 
00174 vtkIdType 
00175 VTKViewer_AppendFilter
00176 ::GetCellOutputID(vtkIdType theInputID,
00177                    vtkIdType theInputDataSetID)
00178 {
00179   if(GetSharedPointsDataSet())
00180     return theInputID;
00181 
00182   return GetOutputID(theInputID,theInputDataSetID,myCellRanges);
00183 }
00184 
00185 
00186 namespace
00187 {
00188   void
00189   GetInputID(vtkIdType theOutputID,
00190              vtkIdType& theInputID,
00191              vtkIdType& theStartID,
00192              vtkIdType& theInputDataSetID,
00193              const VTKViewer_AppendFilter::TVectorIds& theRanges)
00194   {
00195     theInputID = theStartID = theInputDataSetID = -1;
00196 
00197     if(theRanges.empty())
00198       return;
00199 
00200     const vtkIdType& aRangeEnd = theRanges.back();
00201     if(theOutputID < 0 ||  theOutputID >= aRangeEnd)
00202       return;
00203 
00204     vtkIdType aStartId = 0;
00205     vtkIdType aNbInputs = theRanges.size();
00206     for(vtkIdType aDataSetId = 0; aDataSetId < aNbInputs; ++aDataSetId){
00207       vtkIdType aRange = theRanges[aDataSetId];
00208       if(aRange > theOutputID){
00209         theInputID = theOutputID - aStartId;
00210         theInputDataSetID = aDataSetId;
00211         theStartID = aStartId;
00212         break;
00213       }
00214       aStartId = aRange;
00215     }
00216   }
00217 }
00218 
00219 void 
00220 VTKViewer_AppendFilter
00221 ::GetPointInputID(vtkIdType theOutputID,
00222                   vtkIdType& theInputID,
00223                   vtkIdType& theStartID,
00224                   vtkIdType& theInputDataSetID)
00225 {
00226   if(GetSharedPointsDataSet()) {
00227     theStartID = theInputDataSetID = 0;
00228     theInputID = theOutputID;
00229     return;
00230   }
00231 
00232   ::GetInputID(theOutputID,
00233                theInputID,
00234                theStartID,
00235                theInputDataSetID,
00236                myNodeRanges);
00237 }
00238 
00239 
00240 void
00241 VTKViewer_AppendFilter
00242 ::GetCellInputID(vtkIdType theOutputID,
00243                  vtkIdType& theInputID,
00244                  vtkIdType& theStartID,
00245                  vtkIdType& theInputDataSetID)
00246 {
00247   ::GetInputID(theOutputID,
00248                theInputID,
00249                theStartID,
00250                theInputDataSetID,
00251                myCellRanges);
00252 }
00253 
00254 
00255 int
00256 VTKViewer_AppendFilter
00257 ::MakeOutput(
00258   vtkInformation *vtkNotUsed(request),
00259   vtkInformationVector **inputVector,
00260   vtkInformationVector *outputVector)
00261 {
00262   int idx;
00263   vtkIdType numPts, numCells, cellId;
00264   // vtkCellData *cd;
00265   vtkIdList *ptIds;
00266   vtkDataSet *ds;
00267   int numInputs = this->GetNumberOfInputConnections(0);
00268   
00269   // get the output info object
00270   vtkInformation *outInfo = outputVector->GetInformationObject(0);
00271 
00272   // get the ouptut
00273   vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
00274     outInfo->Get(vtkDataObject::DATA_OBJECT()));
00275   //
00276   numPts = mySharedPointsDataSet->GetNumberOfPoints();
00277   if (numPts < 1) {
00278     return 0;
00279   }
00280   //
00281   numCells = 0;
00282   vtkInformation *inInfo = 0;
00283   for (idx = 0; idx < numInputs;++idx) {
00284     inInfo = inputVector[0]->GetInformationObject(idx);
00285     ds = 0;
00286     if (inInfo)
00287       {
00288       ds = vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
00289       }
00290     if (ds != NULL)  {
00291       if ( ds->GetNumberOfPoints() <= 0 && ds->GetNumberOfCells() <= 0 )  {
00292         continue; //no input, just skip
00293       }
00294       numCells += ds->GetNumberOfCells();
00295     }//if non-empty dataset
00296   }//for all inputs
00297   if (numCells < 1) {
00298     return 0;
00299   }
00300   //
00301   // Now can allocate memory
00302   output->Allocate(numCells); 
00303   ptIds = vtkIdList::New(); 
00304   ptIds->Allocate(VTK_CELL_SIZE);
00305   //
00306   // Append each input dataset together
00307   //
00308   // 1.points
00309   output->SetPoints(GetSharedPointsDataSet()->GetPoints());
00310   output->GetPointData()->PassData(GetSharedPointsDataSet()->GetPointData());
00311   // 2.cells
00312   for (idx = 0; idx < numInputs; ++idx) {
00313     inInfo = inputVector[0]->GetInformationObject(idx);
00314     ds = 0;
00315     if (inInfo)
00316       {
00317       ds = vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
00318       }
00319     if (ds != NULL) {
00320       
00321       numCells = ds->GetNumberOfCells(); 
00322       // cd = ds->GetCellData();
00323       // copy cell and cell data
00324       for (cellId=0; cellId<numCells; cellId++)  {
00325         ds->GetCellPoints(cellId, ptIds);
00326         output->InsertNextCell(ds->GetCellType(cellId), ptIds);
00327       }
00328     }
00329   }
00330   //
00331   ptIds->Delete();
00332   return 1;
00333 }
00334 
00335 int VTKViewer_AppendFilter::FillInputPortInformation(int, vtkInformation *info)
00336 {
00337   info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
00338   info->Set(vtkAlgorithm::INPUT_IS_REPEATABLE(), 1);
00339   return 1;
00340 }