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 // 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::SetGeometryConnection(vtkAlgorithmOutput *input)
65 this->Superclass::SetInputConnection(input);
68 //------------------------------------------------------------------------------
69 void VISU_GaussMergeFilter::SetGeometryData(vtkDataSet *input)
71 this->Superclass::SetInputData(input);
74 //------------------------------------------------------------------------------
75 void VISU_GaussMergeFilter::SetScalarsData(vtkDataSet *input)
77 this->SetInputData(1, input);
80 //------------------------------------------------------------------------------
81 void VISU_GaussMergeFilter::SetScalarsConnection(vtkAlgorithmOutput *input)
83 this->SetInputConnection(1, input);
86 //------------------------------------------------------------------------------
87 void VISU_GaussMergeFilter::SetVectorsData(vtkDataSet *input)
89 this->SetInputData(2, input);
92 //------------------------------------------------------------------------------
93 void VISU_GaussMergeFilter::SetVectorsConnection(vtkAlgorithmOutput *input)
95 this->SetInputConnection(2, input);
98 //------------------------------------------------------------------------------
99 void VISU_GaussMergeFilter::SetNormals(vtkAlgorithmOutput *input)
101 this->SetInputConnection(3, input);
104 //------------------------------------------------------------------------------
105 void VISU_GaussMergeFilter::SetTCoords(vtkAlgorithmOutput *input)
107 this->SetInputConnection(4, input);
110 //------------------------------------------------------------------------------
111 void VISU_GaussMergeFilter::SetTensors(vtkAlgorithmOutput *input)
113 this->SetInputConnection(5, input);
116 //------------------------------------------------------------------------------
117 void VISU_GaussMergeFilter::AddField(const char* name, vtkDataSet* input)
119 this->FieldList->Add(name, input);
122 //------------------------------------------------------------------------------
123 void VISU_GaussMergeFilter::RemoveFields()
125 delete this->FieldList;
126 this->FieldList = new VISU::TFieldList;
130 //---------------------------------------------------------------
132 VISU_GaussMergeFilter
133 ::SetMergingInputs(bool theIsMergingInputs)
135 if(myIsMergingInputs == theIsMergingInputs)
138 myIsMergingInputs = theIsMergingInputs;
143 //---------------------------------------------------------------
145 VISU_GaussMergeFilter
148 return myIsMergingInputs;
152 //---------------------------------------------------------------
154 VISU_GaussMergeFilter
155 ::RequestData(vtkInformation *theRequest,
156 vtkInformationVector **theInputVector,
157 vtkInformationVector *theOutputVector)
159 vtkInformation *inInfo0 = theInputVector[0]->GetInformationObject(0);
160 vtkInformation *inInfo1 = theInputVector[1]->GetInformationObject(0);
161 vtkInformation *outInfo = theOutputVector->GetInformationObject(0);
163 // get the input and output
164 vtkDataSet *anInput = vtkDataSet::SafeDownCast(inInfo0->Get(vtkDataObject::DATA_OBJECT()));
165 vtkDataSet *aScalars = theInputVector[1]->GetNumberOfInformationObjects() > 0 ?
166 vtkDataSet::SafeDownCast(inInfo1->Get(vtkDataObject::DATA_OBJECT())) : NULL;
167 vtkDataSet *anOutput = vtkDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
169 if(vtkUnstructuredGrid *anInputUG = dynamic_cast<vtkUnstructuredGrid*>(this->GetInput())){
170 vtkPolyData *anOutputPD = dynamic_cast<vtkPolyData*>(this->GetOutput());
171 return ExecuteGauss(anInputUG,
176 return Superclass::RequestData(theRequest,
181 //----------------------------------------------------------------------------
182 // Trick: Abstract data types that may or may not be the same type
183 // (structured/unstructured), but the points/cells match up.
184 // Output/Geometry may be structured while ScalarInput may be
185 // unstructured (but really have same triagulation/topology as geometry).
186 // Just request all the input. Always generate all of the output (todo).
188 VISU_GaussMergeFilter
189 ::RequestUpdateExtent(vtkInformation *vtkNotUsed(request),
190 vtkInformationVector **inputVector,
191 vtkInformationVector *vtkNotUsed(outputVector))
193 vtkInformation *inputInfo;
196 for (idx = 0; idx < 6; ++idx)
198 inputInfo = inputVector[idx]->GetInformationObject(0);
201 inputInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER(),
203 inputInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES(),
205 inputInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS(),
207 inputInfo->Set(vtkStreamingDemandDrivenPipeline::EXACT_EXTENT(), 1);
214 //----------------------------------------------------------------------------
216 VISU_GaussMergeFilter
217 ::FillInputPortInformation(int port, vtkInformation *info)
219 int retval = this->Superclass::FillInputPortInformation(port, info);
222 info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
227 //----------------------------------------------------------------------------
229 VISU_GaussMergeFilter
230 ::FillOutputPortInformation(int port, vtkInformation *info)
232 info->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkPolyData");
237 VISU_GaussMergeFilter
238 ::SetGaussPtsIDMapper(const VISU::PGaussPtsIDMapper& theIDMapper)
240 myGaussPtsIDMapper = theIDMapper;
244 const VISU::PGaussPtsIDMapper&
245 VISU_GaussMergeFilter
246 ::GetGaussPtsIDMapper()
248 return myGaussPtsIDMapper;
252 VISU_GaussMergeFilter
253 ::ExecuteGauss(vtkUnstructuredGrid* theInput,
254 vtkDataSet* theScalarsDataSet,
255 vtkPolyData* theOutput)
257 if(IsMergingInputs()){
258 vtkCellData *aCellData = theInput->GetCellData();
259 if(vtkDataArray *aCellMapper = aCellData->GetArray("VISU_CELLS_MAPPER")){
260 vtkIntArray *aGeometryCellMapper = dynamic_cast<vtkIntArray*>(aCellMapper);
262 vtkIntArray* aDataPointMapper = GetIDMapper(FieldList,
263 VISU::TGetPointData(),
264 "VISU_POINTS_MAPPER");
266 vtkIntArray* aDataCellIds = vtkIntArray::New();
268 int nbPoints = aDataPointMapper->GetNumberOfTuples();
269 aDataCellIds->SetNumberOfComponents(2);
270 aDataCellIds->SetNumberOfTuples(nbPoints);
271 int* aDataCellPointer = aDataCellIds->GetPointer(0);
273 int nbPoints = aDataPointMapper->GetNumberOfTuples();
274 for(int i=0;i<nbPoints;i++,aDataCellPointer++){
275 VISU::TGaussPointID aGPID = myGaussPtsIDMapper->GetObjID(i);
276 vtkIdType aCellId = aGPID.first;
277 *aDataCellPointer = aCellId;
279 *aDataCellPointer = 3; // it's a entity CELL
283 vtkIntArray* anCellArr = GetIDMapper(FieldList,
284 VISU::TGetCellData(),
285 "VISU_CELLS_MAPPER");
286 vtkIntArray* anPMArr = GetIDMapper(FieldList,
287 VISU::TGetPointData(),
288 "VISU_POINTS_MAPPER");
290 vtkDataArray* anFArr = GetIDMapper(FieldList,
291 VISU::TGetPointData(),
294 if(VISU::IsDifferent(aDataCellIds, aGeometryCellMapper)){
295 VISU::TObjectIdArray anIntersection;
296 VISU::GetIntersection(aDataCellIds,
300 VISU::TObjectId2TupleGaussIdMap aDataCellId2TupleGaussIdMap;
301 VISU::GetObjectId2TupleGaussIdArray(aDataCellIds, aDataCellId2TupleGaussIdMap);
303 vtkIdType aNbTuples = 0;
304 for(vtkIdType i = 0;i < anIntersection.size();i++)
305 aNbTuples += aDataCellId2TupleGaussIdMap[anIntersection[i].first].size();
307 vtkPointSet* aScalarsDataSet = dynamic_cast<vtkPointSet*>(theScalarsDataSet);
308 vtkPoints* aDataPoints = aScalarsDataSet->GetPoints();
309 vtkPoints* anOutputPoints = vtkPoints::New(aDataPoints->GetDataType());
311 anOutputPoints->SetNumberOfPoints(aNbTuples);
312 theOutput->SetPoints(anOutputPoints);
313 anOutputPoints->Delete();
315 vtkCellData* anInputCellData = aScalarsDataSet->GetCellData();
316 vtkPointData* anInputPointData = aScalarsDataSet->GetPointData();
318 theOutput->Allocate(aNbTuples);
319 vtkCellData* anOutputCellData = theOutput->GetCellData();
320 vtkPointData* anOutputPointData = theOutput->GetPointData();
322 anOutputCellData->CopyAllocate(anInputCellData,aNbTuples);
323 anOutputPointData->CopyAllocate(anInputPointData,aNbTuples);
325 vtkIdList *aCellIds = vtkIdList::New();
327 for(int aTupleId=0, aNewTupleId=0; aTupleId<anIntersection.size(); aTupleId++){
328 VISU::TObjectId& anObjectId = anIntersection[aTupleId];
329 VISU::TCellIdArray aCellIdArray = aDataCellId2TupleGaussIdMap[anObjectId.first];
331 for(vtkIdType i = 0; i < aCellIdArray.size();i++) {
332 vtkIdType aCellId = aCellIdArray[i];
333 vtkCell *aCell = theScalarsDataSet->GetCell(aCellId);
336 aCellIds->InsertNextId(aNewTupleId);
339 vtkIdType aCellType = theScalarsDataSet->GetCellType(aCellId);
340 vtkIdType aNewCellId = theOutput->InsertNextCell(aCellType, aCellIds);
342 anOutputCellData->CopyData(anInputCellData, aCellId, aNewCellId);
343 anOutputPointData->CopyData(anInputPointData, aCellId, aNewCellId);
345 aDataPoints->GetPoint(aCellId, aCoords);
346 anOutputPoints->SetPoint(aNewCellId, aCoords);