1 // Copyright (C) 2007-2010 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 // SALOME VTKViewer : build VTK viewer into Salome desktop
26 #include "VISU_GaussMergeFilter.hxx"
27 #include "VISU_MergeFilterUtilities.hxx"
29 #include <vtkObjectFactory.h>
30 #include <vtkUnstructuredGrid.h>
31 #include <vtkPolyData.h>
32 #include <vtkCellData.h>
33 #include <vtkPointData.h>
34 #include <vtkIdList.h>
36 #include <vtkFloatArray.h>
38 #include <vtkExecutive.h>
39 #include <vtkInformation.h>
40 #include <vtkInformationVector.h>
41 #include <vtkStreamingDemandDrivenPipeline.h>
44 //------------------------------------------------------------------------------
45 vtkStandardNewMacro(VISU_GaussMergeFilter);
47 //------------------------------------------------------------------------------
49 ::VISU_GaussMergeFilter():
50 myIsMergingInputs(false)
52 this->FieldList = new VISU::TFieldList;
53 this->SetNumberOfInputPorts(6);
56 //------------------------------------------------------------------------------
57 VISU_GaussMergeFilter::~VISU_GaussMergeFilter()
59 delete this->FieldList;
62 //------------------------------------------------------------------------------
63 void VISU_GaussMergeFilter::SetGeometry(vtkDataSet *input)
65 this->Superclass::SetInput(input);
68 //------------------------------------------------------------------------------
69 vtkDataSet *VISU_GaussMergeFilter::GetGeometry()
71 if (this->GetNumberOfInputConnections(0) < 1)
75 return vtkDataSet::SafeDownCast(
76 this->GetExecutive()->GetInputData(0, 0));
79 //------------------------------------------------------------------------------
80 void VISU_GaussMergeFilter::SetScalars(vtkDataSet *input)
82 this->SetInput(1, input);
85 //------------------------------------------------------------------------------
86 vtkDataSet *VISU_GaussMergeFilter::GetScalars()
88 if (this->GetNumberOfInputConnections(1) < 1)
92 return vtkDataSet::SafeDownCast(
93 this->GetExecutive()->GetInputData(1, 0));
96 //------------------------------------------------------------------------------
97 void VISU_GaussMergeFilter::SetVectors(vtkDataSet *input)
99 this->SetInput(2, input);
102 //------------------------------------------------------------------------------
103 vtkDataSet *VISU_GaussMergeFilter::GetVectors()
105 if (this->GetNumberOfInputConnections(2) < 1)
109 return vtkDataSet::SafeDownCast(
110 this->GetExecutive()->GetInputData(2, 0));
113 //------------------------------------------------------------------------------
114 void VISU_GaussMergeFilter::SetNormals(vtkDataSet *input)
116 this->SetInput(3, input);
119 //------------------------------------------------------------------------------
120 vtkDataSet *VISU_GaussMergeFilter::GetNormals()
122 if (this->GetNumberOfInputConnections(3) < 1)
126 return vtkDataSet::SafeDownCast(
127 this->GetExecutive()->GetInputData(3, 0));
130 //------------------------------------------------------------------------------
131 void VISU_GaussMergeFilter::SetTCoords(vtkDataSet *input)
133 this->SetInput(4, input);
136 //------------------------------------------------------------------------------
137 vtkDataSet *VISU_GaussMergeFilter::GetTCoords()
139 if (this->GetNumberOfInputConnections(4) < 1)
143 return vtkDataSet::SafeDownCast(
144 this->GetExecutive()->GetInputData(4, 0));
147 //------------------------------------------------------------------------------
148 void VISU_GaussMergeFilter::SetTensors(vtkDataSet *input)
150 this->SetInput(5, input);
153 //------------------------------------------------------------------------------
154 vtkDataSet *VISU_GaussMergeFilter::GetTensors()
156 if (this->GetNumberOfInputConnections(5) < 1)
160 return vtkDataSet::SafeDownCast(
161 this->GetExecutive()->GetInputData(5, 0));
164 //------------------------------------------------------------------------------
165 void VISU_GaussMergeFilter::AddField(const char* name, vtkDataSet* input)
167 this->FieldList->Add(name, input);
170 //------------------------------------------------------------------------------
171 void VISU_GaussMergeFilter::RemoveFields()
173 delete this->FieldList;
174 this->FieldList = new VISU::TFieldList;
178 //---------------------------------------------------------------
180 VISU_GaussMergeFilter
181 ::SetMergingInputs(bool theIsMergingInputs)
183 if(myIsMergingInputs == theIsMergingInputs)
186 myIsMergingInputs = theIsMergingInputs;
191 //---------------------------------------------------------------
193 VISU_GaussMergeFilter
196 return myIsMergingInputs;
200 //---------------------------------------------------------------
202 VISU_GaussMergeFilter
203 ::RequestData(vtkInformation *theRequest,
204 vtkInformationVector **theInputVector,
205 vtkInformationVector *theOutputVector)
207 if(vtkUnstructuredGrid *anInput = dynamic_cast<vtkUnstructuredGrid*>(this->GetInput())){
208 vtkPolyData *anOutput = dynamic_cast<vtkPolyData*>(this->GetOutput());
209 return ExecuteGauss(anInput,
213 return Superclass::RequestData(theRequest,
218 //----------------------------------------------------------------------------
219 // Trick: Abstract data types that may or may not be the same type
220 // (structured/unstructured), but the points/cells match up.
221 // Output/Geometry may be structured while ScalarInput may be
222 // unstructured (but really have same triagulation/topology as geometry).
223 // Just request all the input. Always generate all of the output (todo).
225 VISU_GaussMergeFilter
226 ::RequestUpdateExtent(vtkInformation *vtkNotUsed(request),
227 vtkInformationVector **inputVector,
228 vtkInformationVector *vtkNotUsed(outputVector))
230 vtkInformation *inputInfo;
233 for (idx = 0; idx < 6; ++idx)
235 inputInfo = inputVector[idx]->GetInformationObject(0);
238 inputInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER(),
240 inputInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES(),
242 inputInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS(),
244 inputInfo->Set(vtkStreamingDemandDrivenPipeline::EXACT_EXTENT(), 1);
251 //----------------------------------------------------------------------------
253 VISU_GaussMergeFilter
254 ::FillInputPortInformation(int port, vtkInformation *info)
256 int retval = this->Superclass::FillInputPortInformation(port, info);
259 info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
264 //----------------------------------------------------------------------------
266 VISU_GaussMergeFilter
267 ::FillOutputPortInformation(int port, vtkInformation *info)
269 info->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkPolyData");
274 VISU_GaussMergeFilter
275 ::SetGaussPtsIDMapper(const VISU::PGaussPtsIDMapper& theIDMapper)
277 myGaussPtsIDMapper = theIDMapper;
281 const VISU::PGaussPtsIDMapper&
282 VISU_GaussMergeFilter
283 ::GetGaussPtsIDMapper()
285 return myGaussPtsIDMapper;
289 VISU_GaussMergeFilter
290 ::ExecuteGauss(vtkUnstructuredGrid* theInput,
291 vtkPolyData* theOutput)
293 if(IsMergingInputs()){
294 vtkCellData *aCellData = theInput->GetCellData();
295 if(vtkDataArray *aCellMapper = aCellData->GetArray("VISU_CELLS_MAPPER")){
296 vtkIntArray *aGeometryCellMapper = dynamic_cast<vtkIntArray*>(aCellMapper);
298 vtkIntArray* aDataPointMapper = GetIDMapper(FieldList,
299 VISU::TGetPointData(),
300 "VISU_POINTS_MAPPER");
302 vtkIntArray* aDataCellIds = vtkIntArray::New();
304 int nbPoints = aDataPointMapper->GetNumberOfTuples();
305 aDataCellIds->SetNumberOfComponents(2);
306 aDataCellIds->SetNumberOfTuples(nbPoints);
307 int* aDataCellPointer = aDataCellIds->GetPointer(0);
309 int nbPoints = aDataPointMapper->GetNumberOfTuples();
310 for(int i=0;i<nbPoints;i++,aDataCellPointer++){
311 VISU::TGaussPointID aGPID = myGaussPtsIDMapper->GetObjID(i);
312 vtkIdType aCellId = aGPID.first;
313 *aDataCellPointer = aCellId;
315 *aDataCellPointer = 3; // it's a entity CELL
319 vtkIntArray* anCellArr = GetIDMapper(FieldList,
320 VISU::TGetCellData(),
321 "VISU_CELLS_MAPPER");
322 vtkIntArray* anPMArr = GetIDMapper(FieldList,
323 VISU::TGetPointData(),
324 "VISU_POINTS_MAPPER");
326 vtkDataArray* anFArr = GetIDMapper(FieldList,
327 VISU::TGetPointData(),
330 if(VISU::IsDifferent(aDataCellIds, aGeometryCellMapper)){
331 VISU::TObjectIdArray anIntersection;
332 VISU::GetIntersection(aDataCellIds,
336 VISU::TObjectId2TupleGaussIdMap aDataCellId2TupleGaussIdMap;
337 VISU::GetObjectId2TupleGaussIdArray(aDataCellIds, aDataCellId2TupleGaussIdMap);
339 vtkIdType aNbTuples = 0;
340 for(vtkIdType i = 0;i < anIntersection.size();i++)
341 aNbTuples += aDataCellId2TupleGaussIdMap[anIntersection[i].first].size();
343 vtkPointSet* aScalarsDataSet = dynamic_cast<vtkPointSet*>(GetScalars());
344 vtkPoints* aDataPoints = aScalarsDataSet->GetPoints();
345 vtkPoints* anOutputPoints = vtkPoints::New(aDataPoints->GetDataType());
347 anOutputPoints->SetNumberOfPoints(aNbTuples);
348 theOutput->SetPoints(anOutputPoints);
349 anOutputPoints->Delete();
351 vtkCellData* anInputCellData = aScalarsDataSet->GetCellData();
352 vtkPointData* anInputPointData = aScalarsDataSet->GetPointData();
354 theOutput->Allocate(aNbTuples);
355 vtkCellData* anOutputCellData = theOutput->GetCellData();
356 vtkPointData* anOutputPointData = theOutput->GetPointData();
358 anOutputCellData->CopyAllocate(anInputCellData,aNbTuples);
359 anOutputPointData->CopyAllocate(anInputPointData,aNbTuples);
361 vtkIdList *aCellIds = vtkIdList::New();
362 vtkFloatingPointType aCoords[3];
363 for(int aTupleId=0, aNewTupleId=0; aTupleId<anIntersection.size(); aTupleId++){
364 VISU::TObjectId& anObjectId = anIntersection[aTupleId];
365 VISU::TCellIdArray aCellIdArray = aDataCellId2TupleGaussIdMap[anObjectId.first];
367 for(vtkIdType i = 0; i < aCellIdArray.size();i++) {
368 vtkIdType aCellId = aCellIdArray[i];
369 vtkCell *aCell = GetScalars()->GetCell(aCellId);
372 aCellIds->InsertNextId(aNewTupleId);
375 vtkIdType aCellType = GetScalars()->GetCellType(aCellId);
376 vtkIdType aNewCellId = theOutput->InsertNextCell(aCellType, aCellIds);
378 anOutputCellData->CopyData(anInputCellData, aCellId, aNewCellId);
379 anOutputPointData->CopyData(anInputPointData, aCellId, aNewCellId);
381 aDataPoints->GetPoint(aCellId, aCoords);
382 anOutputPoints->SetPoint(aNewCellId, aCoords);