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 // SALOME OBJECT : kernel of SALOME component
23 // File : VISU_GeometryFilter.cxx
28 #include "VISU_AppendFilterUtilities.hxx"
29 #include "VISU_ConvertorUtils.hxx"
32 #include <vtkCellData.h>
33 #include <vtkPointData.h>
35 #include <vtkDataSetCollection.h>
36 #include <vtkObjectFactory.h>
38 #include <vtkUnstructuredGrid.h>
39 #include <vtkPolyData.h>
41 #include <vtkInformationVector.h>
42 #include <vtkInformation.h>
43 #include <vtkExecutive.h>
45 #include <vtkPoints.h>
46 #include <vtkIntArray.h>
54 //---------------------------------------------------------------
55 typedef vtkIdType TCellId;
56 typedef vtkIdType TInputId;
57 typedef std::pair<TInputId, TCellId> TInputCellId;
59 typedef std::pair<vtkIdType, vtkIdType> TObjectId;
60 typedef std::map<TObjectId, TInputCellId> TObject2InputIdMap;
63 //---------------------------------------------------------------
65 DoMergingInputs(vtkCellData *theCellData,
67 TObject2InputIdMap& theResult)
69 if(vtkDataArray *aDataArray = theCellData->GetArray("VISU_CELLS_MAPPER")){
70 if(vtkIntArray *anIntArray = dynamic_cast<vtkIntArray*>(aDataArray)){
71 int *aPointer = anIntArray->GetPointer(0);
72 int aNbCells = anIntArray->GetNumberOfTuples();
73 for(vtkIdType aCellId = 0; aCellId < aNbCells; aCellId++){
74 int aObjId = *aPointer++;
75 int anEntity = *aPointer++;
76 TObjectId anObjectId(aObjId, anEntity);
77 TObject2InputIdMap::iterator anIter = theResult.find(anObjectId);
78 if(anIter != theResult.end())
80 TInputCellId anInputCellId(theInputId, aCellId);
81 theResult.insert(anIter, TObject2InputIdMap::value_type(anObjectId, anInputCellId));
88 //---------------------------------------------------------------
91 vtkDataSetAttributes::FieldList myFieldList;
92 bool myIsFirstCellData;
94 TFillFieldList(vtkIdType theNbInputs):
95 myFieldList(theNbInputs),
96 myIsFirstCellData(true)
100 operator()(TInputId theInputId, vtkDataSet* theDataSet)
102 vtkCellData *aCellData = theDataSet->GetCellData();
103 if(myIsFirstCellData){
104 myFieldList.InitializeFieldList(aCellData);
105 myIsFirstCellData = false;
107 myFieldList.IntersectFieldList(aCellData);
113 GetNbCells() const = 0;
117 //---------------------------------------------------------------
118 struct TCellCounter: TFillFieldList
122 TCellCounter(vtkIdType theNbInputs):
123 TFillFieldList(theNbInputs),
128 operator()(TInputId theInputId, vtkDataSet* theDataSet)
130 TFillFieldList::operator()(theInputId, theDataSet);
131 myNbCells += theDataSet->GetNumberOfCells();
143 //---------------------------------------------------------------
144 struct TCellIdMerger: TFillFieldList
146 TObject2InputIdMap myObject2InputIdMap;
148 TCellIdMerger(vtkIdType theNbInputs):
149 TFillFieldList(theNbInputs)
153 operator()(TInputId theInputId, vtkDataSet* theDataSet)
155 TFillFieldList::operator()(theInputId, theDataSet);
156 vtkCellData *aCellData = theDataSet->GetCellData();
157 DoMergingInputs(aCellData, theInputId, myObject2InputIdMap);
164 return myObject2InputIdMap.size();
169 //---------------------------------------------------------------
170 template<class TFunctor>
172 ForEachInput(vtkInformationVector **theInputVector,
173 vtkIdType theNumberOfInputConnections,
174 TFunctor& theFunctor)
176 for(vtkIdType anInputId = 0; anInputId < theNumberOfInputConnections; anInputId++)
177 if(vtkDataSet *aDataSet = VISU::GetInput(theInputVector, anInputId))
178 if(aDataSet->GetNumberOfPoints() > 0 && aDataSet->GetNumberOfCells() > 0)
179 theFunctor(anInputId, aDataSet);
183 //---------------------------------------------------------------
184 template<class TDataSet>
186 RequestData(vtkInformationVector **theInputVector,
187 vtkIdType theNumberOfInputConnections,
188 vtkInformationVector *theOutputVector,
189 vtkPointSet* theSharedPointSet,
190 bool theIsMergingInputs,
191 bool theIsMappingInputs)
193 if ( theNumberOfInputConnections == 1 ) {
194 // get the input and ouptut
195 vtkDataSet *anInput = VISU::GetInput( theInputVector, 0 );
196 vtkDataSet* anOutput = VISU::GetOutput( theOutputVector );
198 if ( anInput->GetDataObjectType() != anOutput->GetDataObjectType() )
201 // This has to be here because it initialized all field datas.
202 anOutput->CopyStructure( anInput );
204 // Pass all. (data object's field data is passed by the
205 // superclass after this method)
206 anOutput->GetPointData()->PassData( anInput->GetPointData() );
207 anOutput->GetCellData()->PassData( anInput->GetCellData() );
212 if ( theSharedPointSet ) {
213 vtkPoints* aPoints = theSharedPointSet->GetPoints();
214 if(aPoints->GetNumberOfPoints() < 1)
217 TDataSet* anOutput = TDataSet::SafeDownCast(VISU::GetOutput(theOutputVector));
218 vtkIdType anNbInputs = theNumberOfInputConnections;
219 if ( theIsMergingInputs ) {
220 TCellIdMerger aFunctor(anNbInputs);
221 ForEachInput<TCellIdMerger>(theInputVector, anNbInputs, aFunctor);
223 vtkDataSetAttributes::FieldList& aFieldList = aFunctor.myFieldList;
224 TObject2InputIdMap& anObject2InputIdMap = aFunctor.myObject2InputIdMap;
225 vtkIdType aNbCells = aFunctor.GetNbCells();
229 // Now can allocate memory
230 anOutput->Allocate(aNbCells);
231 vtkCellData *anOutputCellData = anOutput->GetCellData();
232 anOutputCellData->CopyAllocate(aFieldList, aNbCells);
234 // Append each input dataset together
236 anOutput->SetPoints(theSharedPointSet->GetPoints());
237 anOutput->GetPointData()->PassData(theSharedPointSet->GetPointData());
240 vtkIdList *anIdList = vtkIdList::New();
241 anIdList->Allocate(VTK_CELL_SIZE);
242 TObject2InputIdMap::const_iterator anIter = anObject2InputIdMap.begin();
243 TObject2InputIdMap::const_iterator anEndIter = anObject2InputIdMap.end();
244 for(; anIter != anEndIter; anIter++){
245 //TObjectId anObjectId = anIter->first;
246 const TInputCellId& anInputCellId = anIter->second;
247 TInputId anInputId = anInputCellId.first;
248 if(vtkDataSet *aDataSet = VISU::GetInput(theInputVector, anInputId)){
249 TCellId aCellId = anInputCellId.second;
250 aDataSet->GetCellPoints(aCellId, anIdList);
252 vtkIdType aCellType = aDataSet->GetCellType(aCellId);
253 vtkIdType aNewCellId = anOutput->InsertNextCell(aCellType, anIdList);
255 vtkCellData *aCellData = aDataSet->GetCellData();
256 anOutputCellData->CopyData(aFieldList, aCellData, anInputId, aCellId, aNewCellId);
261 if(theIsMappingInputs){
262 vtkIntArray *aDataArray = vtkIntArray::New();
263 aDataArray->SetName("VISU_INPUTS_MAPPER");
264 aDataArray->SetNumberOfComponents(2);
265 aDataArray->SetNumberOfTuples(aNbCells);
267 vtkIdType aTupleId = 0;
268 TObject2InputIdMap::const_iterator anIter = anObject2InputIdMap.begin();
269 TObject2InputIdMap::const_iterator anEndIter = anObject2InputIdMap.end();
270 for(vtkIdType aCellId = 0; anIter != anEndIter; anIter++, aCellId++){
271 const TInputCellId& anInputCellId = anIter->second;
272 TInputId anInputId = anInputCellId.first;
273 /*TCellId*/ aCellId = anInputCellId.second;
274 aDataArray->SetValue(aTupleId++, anInputId);
275 aDataArray->SetValue(aTupleId++, aCellId);
278 anOutputCellData->AddArray(aDataArray);
279 aDataArray->Delete();
284 TCellCounter aFunctor(anNbInputs);
285 ForEachInput<TCellCounter>(theInputVector, anNbInputs, aFunctor);
287 vtkDataSetAttributes::FieldList& aFieldList = aFunctor.myFieldList;
288 vtkIdType aNbCells = aFunctor.GetNbCells();
292 // Now can allocate memory
293 anOutput->Allocate(aNbCells);
294 vtkCellData *anOutputCellData = anOutput->GetCellData();
295 anOutputCellData->CopyAllocate(aFieldList, aNbCells);
297 // Append each input dataset together
299 anOutput->SetPoints(theSharedPointSet->GetPoints());
300 anOutput->GetPointData()->PassData(theSharedPointSet->GetPointData());
303 vtkIdList *anIdList = vtkIdList::New();
304 anIdList->Allocate(VTK_CELL_SIZE);
305 for(vtkIdType anInputId = 0; anInputId < anNbInputs; anInputId++){
306 if(vtkDataSet *aDataSet = VISU::GetInput(theInputVector, anInputId)){
307 vtkIdType aNbCells = aDataSet->GetNumberOfCells();
308 vtkCellData *aCellData = aDataSet->GetCellData();
309 // copy cell and cell data
310 for(vtkIdType aCellId = 0; aCellId < aNbCells; aCellId++){
311 aDataSet->GetCellPoints(aCellId, anIdList);
313 vtkIdType aCellType = aDataSet->GetCellType(aCellId);
314 vtkIdType aNewCellId = anOutput->InsertNextCell(aCellType, anIdList);
316 anOutputCellData->CopyData(aFieldList, aCellData, anInputId, aCellId, aNewCellId);
322 if(theIsMappingInputs){
323 vtkIntArray *aDataArray = vtkIntArray::New();
324 aDataArray->SetName("VISU_INPUTS_MAPPER");
325 aDataArray->SetNumberOfComponents(2);
326 aDataArray->SetNumberOfTuples(aNbCells);
328 vtkIdType aTupleId = 0;
329 for(vtkIdType anInputId = 0; anInputId < anNbInputs; anInputId++){
330 if(vtkDataSet *aDataSet = VISU::GetInput(theInputVector, anInputId)){
331 vtkIdType aNbCells = aDataSet->GetNumberOfCells();
332 for(vtkIdType aCellId = 0; aCellId < aNbCells; aCellId++){
333 aDataArray->SetValue(aTupleId++, aCellId);
334 aDataArray->SetValue(aTupleId++, anInputId);
339 anOutputCellData->AddArray(aDataArray);
340 aDataArray->Delete();
350 //---------------------------------------------------------------
356 //---------------------------------------------------------------
358 ::TAppendFilterHelper(vtkObject* theParent):
359 myIsMergingInputs(false),
360 myIsMappingInputs(false),
365 //---------------------------------------------------------------
368 ::SetSharedPointSet(vtkPointSet* thePointSet)
370 if(GetSharedPointSet() == thePointSet)
373 mySharedPointSet = thePointSet;
379 //---------------------------------------------------------------
382 ::GetSharedPointSet()
384 return mySharedPointSet.GetPointer();
388 //---------------------------------------------------------------
391 ::SetMappingInputs(bool theIsMappingInputs)
393 if(myIsMappingInputs == theIsMappingInputs)
396 myIsMappingInputs = theIsMappingInputs;
401 //---------------------------------------------------------------
406 return myIsMappingInputs;
410 //---------------------------------------------------------------
413 ::SetMergingInputs(bool theIsMergingInputs)
415 if(myIsMergingInputs == theIsMergingInputs)
418 myIsMergingInputs = theIsMergingInputs;
423 //---------------------------------------------------------------
428 return myIsMergingInputs;
432 //---------------------------------------------------------------
434 UnstructuredGridRequestData(vtkInformationVector **theInputVector,
435 vtkIdType theNumberOfInputConnections,
436 vtkInformationVector *theOutputVector,
437 vtkPointSet* theSharedPointSet,
438 bool theIsMergingInputs,
439 bool theIsMappingInputs)
441 return RequestData<vtkUnstructuredGrid>(theInputVector,
442 theNumberOfInputConnections,
450 //---------------------------------------------------------------
452 PolyDataRequestData(vtkInformationVector **theInputVector,
453 vtkIdType theNumberOfInputConnections,
454 vtkInformationVector *theOutputVector,
455 vtkPointSet* theSharedPointSet,
456 bool theIsMergingInputs,
457 bool theIsMappingInputs)
459 return RequestData<vtkPolyData>(theInputVector,
460 theNumberOfInputConnections,
468 //---------------------------------------------------------------