1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : VISU_CommonCellsFilter.cxx
21 // Created : Wed Apr 4 08:45:07 2007
22 // Author : Eugeny NIKOLAEV (enk)
24 #include "VISU_CommonCellsFilter.hxx"
25 #include "VISU_ConvertorDef.hxx"
27 // VTK product headers
28 #include <vtkUnstructuredGrid.h>
29 #include <vtkSetGet.h>
30 #include <vtkObjectFactory.h>
31 #include <vtkDataSet.h>
32 #include <vtkCellTypes.h>
33 #include <vtkPointData.h>
34 #include <vtkCellData.h>
35 #include <vtkIdList.h>
36 #include <vtkFloatArray.h>
46 static int MYDEBUG = 0;
48 static int MYDEBUG = 0;
52 #include "VISU_ConvertorUtils.hxx"
56 typedef std::pair<int,int> TPair;// pair first - object id, second - entity
58 typedef std::vector<int> TSortedArrayOne;
59 typedef std::set<TPair> TSortedArrayPair;
60 typedef std::set<int> TIdSet;
61 typedef std::map<int,int> TId2IdMap;
65 GetSortedArrayAsPair(vtkIntArray *theArray,
66 TSortedArrayPair& theSortedArray)
68 TSortedArrayPair aSortedArray;
69 int nbComp = theArray->GetNumberOfComponents();
71 int aMaxId = theArray->GetNumberOfTuples()*theArray->GetNumberOfComponents();
72 int* aPointer = theArray->GetPointer(0);
73 int* anEndPointer = theArray->GetPointer(aMaxId + 1);
74 for(;aPointer<anEndPointer;){
76 aPair.first = *aPointer;
78 aPair.second = *aPointer;
80 aSortedArray.insert(aPair);
82 } else if (nbComp == 1) {
83 int aMaxId = theArray->GetNumberOfTuples();
84 int* aPointer = theArray->GetPointer(0);
85 int* anEndPointer = theArray->GetPointer(aMaxId + 1);
86 for(;aPointer<anEndPointer;){
88 aPair.first = *aPointer;
90 aPair.second = (int)VISU::NODE_ENTITY;
91 aSortedArray.insert(aPair);
95 theSortedArray.swap(aSortedArray);
100 GetSortedArrayOne(vtkIntArray *theArray,
101 TSortedArrayOne& theSortedArray)
103 int aMaxId = theArray->GetMaxId();
104 int* aPointer = theArray->GetPointer(0);
105 int* anEndPointer = theArray->GetPointer(aMaxId + 1);
106 TSortedArrayOne aSortedArray(aPointer, anEndPointer);
107 std::sort(aSortedArray.begin(), aSortedArray.end());
108 theSortedArray.swap(aSortedArray);
113 GetIdsForCopy(vtkUnstructuredGrid *inputUGrid,
114 vtkIntArray* inputPointIds,
115 TSortedArrayOne& outputSortedArray)
118 TSortedArrayOne aSortedPointIds;
119 TSortedArrayOne aOutputCellIds;
120 TIdSet aMapForSearch;
121 int nbTuples = inputPointIds->GetNumberOfTuples();
122 int nbComp = inputPointIds->GetNumberOfComponents();
123 int * aPtr = inputPointIds->GetPointer(0);
124 int * aPtrEnd = inputPointIds->GetPointer(nbTuples*nbComp+1);
127 aMapForSearch.insert(*aPtr);
130 else if (nbComp == 2)
132 aMapForSearch.insert(*aPtr);
135 int nbInputCells = inputUGrid->GetNumberOfCells();
137 for(int idCell=0;idCell<nbInputCells;idCell++){
138 vtkCell* aCell = inputUGrid->GetCell(idCell);
139 vtkIdList* ptIds = aCell->GetPointIds();
140 int nbPointsInCell = ptIds->GetNumberOfIds();
141 bool aGoodCell = true;
142 for(int i=0;i<nbPointsInCell;i++){
143 int aSearchingId = ptIds->GetId(i);
144 TIdSet::iterator aResult = aMapForSearch.find(aSearchingId);
145 if(aResult == aMapForSearch.end()){
151 aOutputCellIds.push_back(idCell);
157 outputSortedArray.swap(aOutputCellIds);
163 CopyElementsToOutput(vtkUnstructuredGrid* theInputUG,
165 TSortedArrayOne& theElementIdsForCopy,
166 TId2IdMap& theOldId2NewIdPointsMap,
167 vtkUnstructuredGrid* theOutputUG)
169 vtkIntArray* theOuputIDSArray = vtkIntArray::New();
170 theOuputIDSArray->SetName("VISU_CELLS_MAPPER");
171 theOuputIDSArray->SetNumberOfComponents(2);
172 theOuputIDSArray->SetNumberOfTuples(theNbElements);
173 int* aOuputIDSPtr = theOuputIDSArray->GetPointer(0);
175 vtkIntArray* aInputCellsMapper =
176 dynamic_cast<vtkIntArray*>(theInputUG->GetCellData()->GetArray("VISU_CELLS_MAPPER"));
177 int* aInputCellsMapperPointer = aInputCellsMapper->GetPointer(0);
178 for(int aCellIndex=0;aCellIndex<theNbElements;aCellIndex++){
179 int aCellId = theElementIdsForCopy[aCellIndex];
180 vtkIdList* aOldPointIds = theInputUG->GetCell(aCellId)->GetPointIds();
181 vtkIdList* aNewPointIds = vtkIdList::New();
182 int nbPointIds = aOldPointIds->GetNumberOfIds();
183 aNewPointIds->SetNumberOfIds(nbPointIds);
184 for(int j=0;j<nbPointIds;j++){
185 int aOldId = aOldPointIds->GetId(j);
186 int aNewId = theOldId2NewIdPointsMap[aOldId];
187 aNewPointIds->SetId(j,aNewId);
189 const int aOldCellId = theElementIdsForCopy[aCellIndex];
190 theOutputUG->InsertNextCell(theInputUG->GetCellType(aOldCellId),
193 *aOuputIDSPtr = aInputCellsMapperPointer[2*aOldCellId];
195 *aOuputIDSPtr = aInputCellsMapperPointer[2*aOldCellId+1];
198 aNewPointIds->Delete();
201 theOutputUG->GetCellData()->AddArray(theOuputIDSArray);
203 theOuputIDSArray->Delete();
207 vtkStandardNewMacro(VISU_CommonCellsFilter);
209 VISU_CommonCellsFilter
210 ::VISU_CommonCellsFilter()
213 VISU_CommonCellsFilter
214 ::~VISU_CommonCellsFilter()
218 VISU_CommonCellsFilter
219 ::SetProfileUG(vtkUnstructuredGrid *input)
221 this->SetInput(input);
225 VISU_CommonCellsFilter
228 return dynamic_cast<vtkUnstructuredGrid*>(this->GetInput());
232 VISU_CommonCellsFilter
233 ::SetCellsUG(vtkUnstructuredGrid *input)
235 this->vtkProcessObject::SetNthInput(1, input);
239 VISU_CommonCellsFilter
242 if (this->NumberOfInputs < 2)
246 return dynamic_cast<vtkUnstructuredGrid*>(this->Inputs[1]);
250 VISU_CommonCellsFilter
253 VISU::TTimerLog aTimerLog(MYDEBUG,"VISU_CommonCellsFilter::Execute");
254 vtkUnstructuredGrid* anInputProfileUG = this->GetProfileUG();
255 vtkUnstructuredGrid* anInputCellsUG = this->GetCellsUG();
257 vtkUnstructuredGrid* anOutput = this->GetOutput();
259 if(anInputCellsUG == NULL){
260 anOutput->ShallowCopy(anInputProfileUG);
263 // check if anInputProfileUG already have cells types not equal VTK_VERTEX
264 vtkCellTypes* aCellTypes = vtkCellTypes::New();
266 anInputProfileUG->GetCellTypes(aCellTypes);
268 if (aCellTypes->GetNumberOfTypes()!=1 )
269 anOutput->ShallowCopy(anInputProfileUG);
271 if(aCellTypes->GetCellType(0) != VTK_VERTEX)
272 anOutput->DeepCopy(anInputProfileUG);
275 vtkCellData* aInputCellData = anInputProfileUG->GetCellData();
278 // Calculate output points
280 vtkIdList* aPointIdsForCopy = vtkIdList::New();
281 vtkPoints* aOutputPointSet = vtkPoints::New();
282 TId2IdMap aOldId2NewIdPointsMap;
284 aOutputPointSet->Reset();
286 vtkIntArray* aPointIDS =
287 dynamic_cast<vtkIntArray*>(aInputCellData->GetArray("VISU_CELLS_MAPPER"));
289 int* aPtr = aPointIDS->GetPointer(0);
290 aPointIdsForCopy->SetNumberOfIds(aPointIDS->GetNumberOfTuples());
291 for(int i=0;i<aPointIDS->GetNumberOfTuples();i++){
292 aPointIdsForCopy->SetId(i,*aPtr);
295 aOutputPointSet->SetNumberOfPoints(aPointIdsForCopy->GetNumberOfIds());
296 // aOutputPointSet copy points from anInputProfileUG to aOutputPointSet, which
297 // in aPointIdsForCopy ids list
298 anInputProfileUG->GetPoints()->GetPoints(aPointIdsForCopy,aOutputPointSet);
299 for(int i=0;i<aPointIdsForCopy->GetNumberOfIds();i++)
300 aOldId2NewIdPointsMap[aPointIdsForCopy->GetId(i)] = i;
301 anOutput->SetPoints(aOutputPointSet);
303 aOutputPointSet->Delete();
304 // applay scalar,vector,normal,tensor ... values
305 anOutput->GetPointData()->CopyFieldOff("VISU_CELLS_MAPPER");
306 anOutput->GetPointData()->CopyFieldOff("VISU_POINTS_MAPPER");
307 anOutput->GetPointData()->PassData(aInputCellData);
308 //anOutput->GetPointData()->GetArray("VISU_CELLS_MAPPER")->SetName("VISU_POINTS_MAPPER");
310 // apply VISU_POINTS_MAPPER
311 int anEntity = int(VISU::NODE_ENTITY);
312 vtkIntArray* aNewPointsIdsArray = vtkIntArray::New();
313 aNewPointsIdsArray->SetName("VISU_POINTS_MAPPER");
314 aNewPointsIdsArray->SetNumberOfComponents(2);
315 aNewPointsIdsArray->SetNumberOfTuples(aPointIdsForCopy->GetNumberOfIds());
316 int *aPtr = aNewPointsIdsArray->GetPointer(0);
317 for(int i = 0; i < aPointIdsForCopy->GetNumberOfIds(); i++){
318 *aPtr++ = aPointIdsForCopy->GetId(i);
321 anOutput->GetPointData()->AddArray(aNewPointsIdsArray);
325 aNewPointsIdsArray->Delete();
328 // Calculate output cells
331 TSortedArrayOne aCellIdsForCopy;
333 GetIdsForCopy(anInputCellsUG,aPointIDS,aCellIdsForCopy);
334 nbCells = aCellIdsForCopy.size();
336 // copy cells to output
337 int aAllocMem = nbCells;
338 anOutput->Allocate(aAllocMem);
340 if(nbCells>0 && anInputCellsUG)
341 CopyElementsToOutput(anInputCellsUG,
344 aOldId2NewIdPointsMap,
348 aPointIdsForCopy->Delete();
354 anOutput->ShallowCopy(anInputProfileUG);