1 // Created on: 2011-10-27
2 // Created by: Roman KOZLOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <IVtkTools_SubPolyDataFilter.hxx>
17 #include <IVtkVTK_ShapeData.hxx>
18 #include <vtkCellArray.h>
19 #include <vtkInformation.h>
20 #include <vtkInformationVector.h>
21 #include <vtkObjectFactory.h>
22 #include <vtkCellData.h>
23 #include <vtkIdTypeArray.h>
26 vtkStandardNewMacro(IVtkTools_SubPolyDataFilter);
28 //================================================================
29 // Function : Constructor
31 //================================================================
32 IVtkTools_SubPolyDataFilter::IVtkTools_SubPolyDataFilter()
34 myIdsArrayName = IVtkVTK_ShapeData::ARRNAME_SUBSHAPE_IDS;
38 //================================================================
39 // Function : Destructor
41 //================================================================
42 IVtkTools_SubPolyDataFilter::~IVtkTools_SubPolyDataFilter() { }
44 //================================================================
45 // Function : RequestData
46 // Purpose : Filter cells according to the given set of ids.
47 //================================================================
48 int IVtkTools_SubPolyDataFilter::RequestData (vtkInformation *vtkNotUsed(theRequest),
49 vtkInformationVector **theInputVector,
50 vtkInformationVector *theOutputVector)
52 // get the input and output
53 vtkInformation *anInInfo = theInputVector[0]->GetInformationObject(0);
54 vtkInformation *anOutInfo = theOutputVector->GetInformationObject(0);
56 vtkPolyData *anInput = vtkPolyData::SafeDownCast(
57 anInInfo->Get (vtkDataObject::DATA_OBJECT()));
59 vtkPolyData *anOutput = vtkPolyData::SafeDownCast(
60 anOutInfo->Get (vtkDataObject::DATA_OBJECT()));
62 vtkIdList *anIdList = vtkIdList::New(); // List of cell ids to be passed
63 anIdList->Allocate(myIdsSet.Extent()); // Allocate the list of ids
69 vtkCellData* aCellData = anInput->GetCellData();
71 vtkIdTypeArray* aDataArray = vtkIdTypeArray::SafeDownCast (aCellData->GetArray (myIdsArrayName));
73 if(aDataArray != NULL)
75 aSize = aDataArray->GetNumberOfTuples();
76 anIdList->Allocate (aSize); // Allocate the list of ids
79 // Prepare the list of ids from the set of ids.
80 // Iterate on input cells.
81 if (!myIdsSet.IsEmpty())
83 for (vtkIdType anI = 0; anI < aSize; anI++)
85 if (myIdsSet.Contains (aDataArray->GetValue (anI)))
87 // Add a cell id to output if it's value is in the set.
88 anIdList->InsertNextId (anI);
93 // Copy cells with their points according to the prepared list of cell ids.
94 anOutput->GetCellData()->AllocateArrays(anInput->GetCellData()->GetNumberOfArrays());
95 anOutput->Allocate(anInput, anIdList->GetNumberOfIds()); // Allocate output cells
97 // Create new arrays for output data
98 vtkCellData *const anInData = anInput->GetCellData();
99 vtkCellData *const anOutData = anOutput->GetCellData();
100 vtkDataArray *anOutArr, *anInArr;
102 for (Standard_Integer anI = 0; anI < anInData->GetNumberOfArrays(); anI++)
104 anInArr = anInData->GetArray (anI);
105 anOutArr = vtkDataArray::CreateDataArray(anInArr->GetDataType());
106 anOutArr->SetName(anInArr->GetName());
107 anOutArr->Allocate(anIdList->GetNumberOfIds() * anInArr->GetNumberOfComponents());
108 anOutArr->SetNumberOfTuples (anIdList->GetNumberOfIds());
109 anOutArr->SetNumberOfComponents (anInArr->GetNumberOfComponents());
110 anOutData->AddArray(anOutArr);
113 // Copy cells with ids from our list.
114 anOutput->CopyCells (anInput, anIdList);
116 // Copy filtered arrays data
117 vtkIdType anOutId, anInId;
119 for (Standard_Integer anI = 0; anI < anInData->GetNumberOfArrays(); anI++)
121 anInArr = anInData->GetArray (anI);
122 anOutArr = anOutData->GetArray (anI);
123 for (anOutId = 0; anOutId < anIdList->GetNumberOfIds(); anOutId++)
125 anInId = anIdList->GetId (anOutId);
126 anOutArr->SetTuple (anOutId, anInId, anInArr);
135 anOutput->CopyStructure (anInput); // Copy points and cells
136 anOutput->CopyAttributes (anInput); // Copy data arrays (sub-shapes ids)
139 return 1; // Return non-zero value if success and pipeline is not failed.
142 //================================================================
143 // Function : SetDoFiltering
145 //================================================================
146 void IVtkTools_SubPolyDataFilter::SetDoFiltering (const bool theDoFiltering)
148 myDoFiltering = theDoFiltering;
151 //================================================================
152 // Function : PrintSelf
154 //================================================================
155 void IVtkTools_SubPolyDataFilter::PrintSelf (std::ostream& theOs, vtkIndent theIndent)
157 this->Superclass::PrintSelf (theOs,theIndent);
158 theOs << theIndent << "SubPolyData: " << "\n";
159 theOs << theIndent << " Number of cells to pass: " << myIdsSet.Extent() << "\n";
160 theOs << theIndent << " Cells ids to pass: {" ;
161 // Print the content of the set of ids.
162 IVtk_IdTypeMap::Iterator anIter(myIdsSet);
163 while (anIter.More())
165 theOs << " " << anIter.Value();
172 theOs << "}" << "\n";
175 //================================================================
177 // Purpose : Clear ids set to be passed through this filter.
178 //================================================================
179 void IVtkTools_SubPolyDataFilter::Clear()
184 //================================================================
185 // Function : SetData
186 // Purpose : Set ids to be passed through this filter.
187 //================================================================
188 void IVtkTools_SubPolyDataFilter::SetData (const IVtk_IdTypeMap theSet)
193 //================================================================
194 // Function : AddData
195 // Purpose : Add ids to be passed through this filter.
196 //================================================================
197 void IVtkTools_SubPolyDataFilter::AddData (const IVtk_IdTypeMap theSet)
199 IVtk_IdTypeMap::Iterator anIt (theSet);
200 for (; anIt.More(); anIt.Next())
202 if (!myIdsSet.Contains (anIt.Value()))
204 myIdsSet.Add (anIt.Value());
209 //================================================================
210 // Function : SetData
211 // Purpose : Set ids to be passed through this filter.
212 //================================================================
213 void IVtkTools_SubPolyDataFilter::SetData (const IVtk_ShapeIdList theIdList)
219 //================================================================
220 // Function : AddData
221 // Purpose : Add ids to be passed through this filter.
222 //================================================================
223 void IVtkTools_SubPolyDataFilter::AddData (const IVtk_ShapeIdList theIdList)
225 IVtk_ShapeIdList::Iterator anIt (theIdList);
226 for (; anIt.More(); anIt.Next())
228 if (!myIdsSet.Contains (anIt.Value()))
230 myIdsSet.Add (anIt.Value());
235 //! Set ids to be passed through this filter.
236 //================================================================
237 // Function : SetIdsArrayName
239 //================================================================
240 void IVtkTools_SubPolyDataFilter::SetIdsArrayName (const char* theArrayName)
242 myIdsArrayName = theArrayName;