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 <vtkCellData.h>
19 #include <vtkIdList.h>
20 #include <vtkIdTypeArray.h>
21 #include <vtkInformation.h>
22 #include <vtkInformationVector.h>
23 #include <vtkObjectFactory.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 vtkSmartPointer<vtkInformation> anInInfo = theInputVector[0]->GetInformationObject(0);
54 vtkSmartPointer<vtkInformation> anOutInfo = theOutputVector->GetInformationObject(0);
56 vtkSmartPointer<vtkPolyData> anInput = vtkPolyData::SafeDownCast(
57 anInInfo->Get (vtkDataObject::DATA_OBJECT()));
59 vtkSmartPointer<vtkPolyData> anOutput = vtkPolyData::SafeDownCast(
60 anOutInfo->Get (vtkDataObject::DATA_OBJECT()));
66 vtkSmartPointer<vtkCellData> aCellData = anInput->GetCellData();
68 vtkSmartPointer<vtkIdTypeArray> aDataArray =
69 vtkIdTypeArray::SafeDownCast (aCellData->GetArray (myIdsArrayName));
71 // List of cell ids to be passed
72 vtkSmartPointer<vtkIdList> anIdList = vtkSmartPointer<vtkIdList>::New();
73 anIdList->Allocate(myIdsSet.Extent()); // Allocate the list of ids
75 if (aDataArray.GetPointer() != NULL)
77 aSize = aDataArray->GetNumberOfTuples();
78 anIdList->Allocate (aSize); // Allocate the list of ids
81 // Prepare the list of ids from the set of ids.
82 // Iterate on input cells.
83 if (!myIdsSet.IsEmpty())
85 for (vtkIdType anI = 0; anI < aSize; anI++)
87 if (myIdsSet.Contains (aDataArray->GetValue (anI)))
89 // Add a cell id to output if it's value is in the set.
90 anIdList->InsertNextId (anI);
95 // Copy cells with their points according to the prepared list of cell ids.
96 anOutput->GetCellData()->AllocateArrays(anInput->GetCellData()->GetNumberOfArrays());
97 anOutput->Allocate(anInput, anIdList->GetNumberOfIds()); // Allocate output cells
99 // Create new arrays for output data
100 vtkSmartPointer<vtkCellData> anInData = anInput->GetCellData();
101 vtkSmartPointer<vtkCellData> anOutData = anOutput->GetCellData();
102 vtkSmartPointer<vtkDataArray> anInArr, anOutArr;
104 for (Standard_Integer anI = 0; anI < anInData->GetNumberOfArrays(); anI++)
106 anInArr = anInData->GetArray (anI);
107 anOutArr = vtkSmartPointer<vtkDataArray>::Take(
108 vtkDataArray::CreateDataArray(anInArr->GetDataType()));
109 anOutArr->SetName(anInArr->GetName());
110 anOutArr->Allocate(anIdList->GetNumberOfIds() * anInArr->GetNumberOfComponents());
111 anOutArr->SetNumberOfTuples (anIdList->GetNumberOfIds());
112 anOutArr->SetNumberOfComponents (anInArr->GetNumberOfComponents());
113 anOutData->AddArray(anOutArr);
116 // Copy cells with ids from our list.
117 anOutput->CopyCells (anInput, anIdList);
119 // Copy filtered arrays data
120 vtkIdType anOutId, anInId;
122 for (Standard_Integer anI = 0; anI < anInData->GetNumberOfArrays(); anI++)
124 anInArr = anInData->GetArray (anI);
125 anOutArr = anOutData->GetArray(anI);
126 for (anOutId = 0; anOutId < anIdList->GetNumberOfIds(); anOutId++)
128 anInId = anIdList->GetId (anOutId);
129 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;