1 // Copyright (C) 2007-2008 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
22 // File : VISU_CommonCellsFilter.cxx
23 // Created : Wed Apr 4 08:45:07 2007
24 // Author : Eugeny NIKOLAEV (enk)
26 #include "VISU_CommonCellsFilter.hxx"
27 #include "VISU_ConvertorDef.hxx"
29 // VTK product headers
30 #include <vtkUnstructuredGrid.h>
31 #include <vtkSetGet.h>
32 #include <vtkObjectFactory.h>
33 #include <vtkDataSet.h>
34 #include <vtkCellTypes.h>
35 #include <vtkPointData.h>
36 #include <vtkCellData.h>
37 #include <vtkIdList.h>
38 #include <vtkFloatArray.h>
48 static int MYDEBUG = 0;
50 static int MYDEBUG = 0;
54 #include "VISU_ConvertorUtils.hxx"
58 typedef std::pair<int,int> TPair;// pair first - object id, second - entity
60 typedef std::vector<int> TSortedArrayOne;
61 typedef std::set<TPair> TSortedArrayPair;
62 typedef std::set<int> TIdSet;
63 typedef std::map<int,int> TId2IdMap;
67 GetSortedArrayAsPair(vtkIntArray *theArray,
68 TSortedArrayPair& theSortedArray)
70 TSortedArrayPair aSortedArray;
71 int nbComp = theArray->GetNumberOfComponents();
73 int aMaxId = theArray->GetNumberOfTuples()*theArray->GetNumberOfComponents();
74 int* aPointer = theArray->GetPointer(0);
75 int* anEndPointer = theArray->GetPointer(aMaxId + 1);
76 for(;aPointer<anEndPointer;){
78 aPair.first = *aPointer;
80 aPair.second = *aPointer;
82 aSortedArray.insert(aPair);
84 } else if (nbComp == 1) {
85 int aMaxId = theArray->GetNumberOfTuples();
86 int* aPointer = theArray->GetPointer(0);
87 int* anEndPointer = theArray->GetPointer(aMaxId + 1);
88 for(;aPointer<anEndPointer;){
90 aPair.first = *aPointer;
92 aPair.second = (int)VISU::NODE_ENTITY;
93 aSortedArray.insert(aPair);
97 theSortedArray.swap(aSortedArray);
102 GetSortedArrayOne(vtkIntArray *theArray,
103 TSortedArrayOne& theSortedArray)
105 int aMaxId = theArray->GetMaxId();
106 int* aPointer = theArray->GetPointer(0);
107 int* anEndPointer = theArray->GetPointer(aMaxId + 1);
108 TSortedArrayOne aSortedArray(aPointer, anEndPointer);
109 std::sort(aSortedArray.begin(), aSortedArray.end());
110 theSortedArray.swap(aSortedArray);
115 GetIdsForCopy(vtkUnstructuredGrid *inputUGrid,
116 vtkIntArray* inputPointIds,
117 TSortedArrayOne& outputSortedArray)
120 TSortedArrayOne aSortedPointIds;
121 TSortedArrayOne aOutputCellIds;
122 TIdSet aMapForSearch;
123 int nbTuples = inputPointIds->GetNumberOfTuples();
124 int nbComp = inputPointIds->GetNumberOfComponents();
125 int * aPtr = inputPointIds->GetPointer(0);
126 int * aPtrEnd = inputPointIds->GetPointer(nbTuples*nbComp+1);
129 aMapForSearch.insert(*aPtr);
132 else if (nbComp == 2)
134 aMapForSearch.insert(*aPtr);
137 int nbInputCells = inputUGrid->GetNumberOfCells();
139 for(int idCell=0;idCell<nbInputCells;idCell++){
140 vtkCell* aCell = inputUGrid->GetCell(idCell);
141 vtkIdList* ptIds = aCell->GetPointIds();
142 int nbPointsInCell = ptIds->GetNumberOfIds();
143 bool aGoodCell = true;
144 for(int i=0;i<nbPointsInCell;i++){
145 int aSearchingId = ptIds->GetId(i);
146 TIdSet::iterator aResult = aMapForSearch.find(aSearchingId);
147 if(aResult == aMapForSearch.end()){
153 aOutputCellIds.push_back(idCell);
159 outputSortedArray.swap(aOutputCellIds);
165 CopyElementsToOutput(vtkUnstructuredGrid* theInputUG,
167 TSortedArrayOne& theElementIdsForCopy,
168 TId2IdMap& theOldId2NewIdPointsMap,
169 vtkUnstructuredGrid* theOutputUG)
171 vtkIntArray* theOuputIDSArray = vtkIntArray::New();
172 theOuputIDSArray->SetName("VISU_CELLS_MAPPER");
173 theOuputIDSArray->SetNumberOfComponents(2);
174 theOuputIDSArray->SetNumberOfTuples(theNbElements);
175 int* aOuputIDSPtr = theOuputIDSArray->GetPointer(0);
177 vtkIntArray* aInputCellsMapper =
178 dynamic_cast<vtkIntArray*>(theInputUG->GetCellData()->GetArray("VISU_CELLS_MAPPER"));
179 int* aInputCellsMapperPointer = aInputCellsMapper->GetPointer(0);
180 for(int aCellIndex=0;aCellIndex<theNbElements;aCellIndex++){
181 int aCellId = theElementIdsForCopy[aCellIndex];
182 vtkIdList* aOldPointIds = theInputUG->GetCell(aCellId)->GetPointIds();
183 vtkIdList* aNewPointIds = vtkIdList::New();
184 int nbPointIds = aOldPointIds->GetNumberOfIds();
185 aNewPointIds->SetNumberOfIds(nbPointIds);
186 for(int j=0;j<nbPointIds;j++){
187 int aOldId = aOldPointIds->GetId(j);
188 int aNewId = theOldId2NewIdPointsMap[aOldId];
189 aNewPointIds->SetId(j,aNewId);
191 const int aOldCellId = theElementIdsForCopy[aCellIndex];
192 theOutputUG->InsertNextCell(theInputUG->GetCellType(aOldCellId),
195 *aOuputIDSPtr = aInputCellsMapperPointer[2*aOldCellId];
197 *aOuputIDSPtr = aInputCellsMapperPointer[2*aOldCellId+1];
200 aNewPointIds->Delete();
203 theOutputUG->GetCellData()->AddArray(theOuputIDSArray);
205 theOuputIDSArray->Delete();
209 vtkStandardNewMacro(VISU_CommonCellsFilter);
211 VISU_CommonCellsFilter
212 ::VISU_CommonCellsFilter()
215 VISU_CommonCellsFilter
216 ::~VISU_CommonCellsFilter()
220 VISU_CommonCellsFilter
221 ::SetProfileUG(vtkUnstructuredGrid *input)
223 this->SetInput(input);
227 VISU_CommonCellsFilter
230 return dynamic_cast<vtkUnstructuredGrid*>(this->GetInput());
234 VISU_CommonCellsFilter
235 ::SetCellsUG(vtkUnstructuredGrid *input)
237 this->vtkProcessObject::SetNthInput(1, input);
241 VISU_CommonCellsFilter
244 if (this->NumberOfInputs < 2)
248 return dynamic_cast<vtkUnstructuredGrid*>(this->Inputs[1]);
252 VISU_CommonCellsFilter
255 VISU::TTimerLog aTimerLog(MYDEBUG,"VISU_CommonCellsFilter::Execute");
256 vtkUnstructuredGrid* anInputProfileUG = this->GetProfileUG();
257 vtkUnstructuredGrid* anInputCellsUG = this->GetCellsUG();
259 vtkUnstructuredGrid* anOutput = this->GetOutput();
261 if(anInputCellsUG == NULL){
262 anOutput->ShallowCopy(anInputProfileUG);
265 // check if anInputProfileUG already have cells types not equal VTK_VERTEX
266 vtkCellTypes* aCellTypes = vtkCellTypes::New();
268 anInputProfileUG->GetCellTypes(aCellTypes);
270 if (aCellTypes->GetNumberOfTypes()!=1 )
271 anOutput->ShallowCopy(anInputProfileUG);
273 if(aCellTypes->GetCellType(0) != VTK_VERTEX)
274 anOutput->DeepCopy(anInputProfileUG);
277 vtkCellData* aInputCellData = anInputProfileUG->GetCellData();
280 // Calculate output points
282 vtkIdList* aPointIdsForCopy = vtkIdList::New();
283 vtkPoints* aOutputPointSet = vtkPoints::New();
284 TId2IdMap aOldId2NewIdPointsMap;
286 aOutputPointSet->Reset();
288 vtkIntArray* aPointIDS =
289 dynamic_cast<vtkIntArray*>(aInputCellData->GetArray("VISU_CELLS_MAPPER"));
291 int* aPtr = aPointIDS->GetPointer(0);
292 aPointIdsForCopy->SetNumberOfIds(aPointIDS->GetNumberOfTuples());
293 for(int i=0;i<aPointIDS->GetNumberOfTuples();i++){
294 aPointIdsForCopy->SetId(i,*aPtr);
297 aOutputPointSet->SetNumberOfPoints(aPointIdsForCopy->GetNumberOfIds());
298 // aOutputPointSet copy points from anInputProfileUG to aOutputPointSet, which
299 // in aPointIdsForCopy ids list
300 anInputProfileUG->GetPoints()->GetPoints(aPointIdsForCopy,aOutputPointSet);
301 for(int i=0;i<aPointIdsForCopy->GetNumberOfIds();i++)
302 aOldId2NewIdPointsMap[aPointIdsForCopy->GetId(i)] = i;
303 anOutput->SetPoints(aOutputPointSet);
305 aOutputPointSet->Delete();
306 // applay scalar,vector,normal,tensor ... values
307 anOutput->GetPointData()->CopyFieldOff("VISU_CELLS_MAPPER");
308 anOutput->GetPointData()->CopyFieldOff("VISU_POINTS_MAPPER");
309 anOutput->GetPointData()->PassData(aInputCellData);
310 //anOutput->GetPointData()->GetArray("VISU_CELLS_MAPPER")->SetName("VISU_POINTS_MAPPER");
312 // apply VISU_POINTS_MAPPER
313 int anEntity = int(VISU::NODE_ENTITY);
314 vtkIntArray* aNewPointsIdsArray = vtkIntArray::New();
315 aNewPointsIdsArray->SetName("VISU_POINTS_MAPPER");
316 aNewPointsIdsArray->SetNumberOfComponents(2);
317 aNewPointsIdsArray->SetNumberOfTuples(aPointIdsForCopy->GetNumberOfIds());
318 int *aPtr = aNewPointsIdsArray->GetPointer(0);
319 for(int i = 0; i < aPointIdsForCopy->GetNumberOfIds(); i++){
320 *aPtr++ = aPointIdsForCopy->GetId(i);
323 anOutput->GetPointData()->AddArray(aNewPointsIdsArray);
327 aNewPointsIdsArray->Delete();
330 // Calculate output cells
333 TSortedArrayOne aCellIdsForCopy;
335 GetIdsForCopy(anInputCellsUG,aPointIDS,aCellIdsForCopy);
336 nbCells = aCellIdsForCopy.size();
338 // copy cells to output
339 int aAllocMem = nbCells;
340 anOutput->Allocate(aAllocMem);
342 if(nbCells>0 && anInputCellsUG)
343 CopyElementsToOutput(anInputCellsUG,
346 aOldId2NewIdPointsMap,
350 aPointIdsForCopy->Delete();
356 anOutput->ShallowCopy(anInputProfileUG);