054b30421cc1dda14451d948b999a4292f14f6cb
[occt.git] / src / IVtkTools / IVtkTools_SubPolyDataFilter.cxx
1 // Created on: 2011-10-27 
2 // Created by: Roman KOZLOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS 
4 // 
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
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>
24
25
26 vtkStandardNewMacro(IVtkTools_SubPolyDataFilter);
27
28 //================================================================
29 // Function : Constructor
30 // Purpose  : 
31 //================================================================
32 IVtkTools_SubPolyDataFilter::IVtkTools_SubPolyDataFilter()
33 {
34   myIdsArrayName = IVtkVTK_ShapeData::ARRNAME_SUBSHAPE_IDS;
35   myDoFiltering = true;
36 }
37
38 //================================================================
39 // Function : Destructor
40 // Purpose  : 
41 //================================================================
42 IVtkTools_SubPolyDataFilter::~IVtkTools_SubPolyDataFilter() { }
43
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)
51 {
52   // get the input and output
53   vtkInformation *anInInfo = theInputVector[0]->GetInformationObject(0);
54   vtkInformation *anOutInfo = theOutputVector->GetInformationObject(0);
55
56   vtkPolyData *anInput = vtkPolyData::SafeDownCast(
57     anInInfo->Get (vtkDataObject::DATA_OBJECT()));
58
59   vtkPolyData *anOutput = vtkPolyData::SafeDownCast(
60     anOutInfo->Get (vtkDataObject::DATA_OBJECT()));
61
62   vtkIdList *anIdList = vtkIdList::New(); // List of cell ids to be passed
63   anIdList->Allocate(myIdsSet.Extent());  // Allocate the list of ids
64
65   anInput->Modified();
66
67   if (myDoFiltering)
68   {
69     vtkCellData* aCellData = anInput->GetCellData();
70     int aSize = 0;
71     vtkIdTypeArray* aDataArray = vtkIdTypeArray::SafeDownCast (aCellData->GetArray (myIdsArrayName));
72
73     if(aDataArray != NULL)
74     {
75       aSize = aDataArray->GetNumberOfTuples();
76       anIdList->Allocate (aSize);  // Allocate the list of ids
77     }
78
79     // Prepare the list of ids from the set of ids.
80     // Iterate on input cells.
81     if (!myIdsSet.IsEmpty())
82     {
83       for (vtkIdType anI = 0; anI < aSize; anI++)
84       {
85         if (myIdsSet.Contains (aDataArray->GetValue (anI)))
86         {
87           // Add a cell id to output if it's value is in the set.
88           anIdList->InsertNextId (anI);
89         }
90       }
91     }
92
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
96     // Pass data arrays.
97     // Create new arrays for output data 
98     vtkCellData *const anInData = anInput->GetCellData();
99     vtkCellData *const anOutData = anOutput->GetCellData();
100     vtkDataArray *anOutArr, *anInArr;
101
102     for (Standard_Integer anI = 0; anI < anInData->GetNumberOfArrays(); anI++)
103     {
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);
111     }
112
113     // Copy cells with ids from our list.
114     anOutput->CopyCells (anInput, anIdList);
115
116     // Copy filtered arrays data
117     vtkIdType anOutId, anInId;
118
119     for (Standard_Integer anI = 0; anI < anInData->GetNumberOfArrays(); anI++)
120     {
121       anInArr = anInData->GetArray (anI);
122       anOutArr = anOutData->GetArray (anI);
123       for (anOutId = 0; anOutId < anIdList->GetNumberOfIds(); anOutId++)
124       {
125         anInId = anIdList->GetId (anOutId);
126         anOutArr->SetTuple (anOutId, anInId, anInArr);
127       }
128     }
129
130     anIdList->Delete();
131
132   }
133   else
134   {
135     anOutput->CopyStructure (anInput);  // Copy points and cells
136     anOutput->CopyAttributes (anInput); // Copy data arrays (sub-shapes ids)
137   }
138
139   return 1; // Return non-zero value if success and pipeline is not failed.
140 }
141
142 //================================================================
143 // Function : SetDoFiltering
144 // Purpose  : 
145 //================================================================
146 void IVtkTools_SubPolyDataFilter::SetDoFiltering (const bool theDoFiltering)
147 {
148   myDoFiltering = theDoFiltering;
149 }
150
151 //================================================================
152 // Function : PrintSelf
153 // Purpose  : 
154 //================================================================
155 void IVtkTools_SubPolyDataFilter::PrintSelf (std::ostream& theOs, vtkIndent theIndent)
156 {
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())
164   {
165       theOs << " " << anIter.Value();
166       anIter.Next();
167       if (anIter.More())
168       {
169           theOs << "; ";
170       }
171   }
172   theOs << "}" << "\n";
173 }
174
175 //================================================================
176 // Function : Clear
177 // Purpose  : Clear ids set to be passed through this filter.
178 //================================================================
179 void IVtkTools_SubPolyDataFilter::Clear()
180 {
181   myIdsSet.Clear();
182 }
183
184 //================================================================
185 // Function : SetData
186 // Purpose  : Set ids to be passed through this filter.
187 //================================================================
188 void IVtkTools_SubPolyDataFilter::SetData (const IVtk_IdTypeMap theSet)
189 {
190   myIdsSet = theSet;
191 }
192
193 //================================================================
194 // Function : AddData
195 // Purpose  : Add ids to be passed through this filter.
196 //================================================================
197 void IVtkTools_SubPolyDataFilter::AddData (const IVtk_IdTypeMap theSet)
198 {
199   IVtk_IdTypeMap::Iterator anIt (theSet);
200   for (; anIt.More(); anIt.Next())
201   {
202     if (!myIdsSet.Contains (anIt.Value()))
203     {
204       myIdsSet.Add (anIt.Value());
205     }
206   }
207 }
208
209 //================================================================
210 // Function : SetData
211 // Purpose  : Set ids to be passed through this filter.
212 //================================================================
213 void IVtkTools_SubPolyDataFilter::SetData (const IVtk_ShapeIdList theIdList)
214 {
215   myIdsSet.Clear();
216   AddData (theIdList);
217 }
218
219 //================================================================
220 // Function : AddData
221 // Purpose  : Add ids to be passed through this filter.
222 //================================================================
223 void IVtkTools_SubPolyDataFilter::AddData (const IVtk_ShapeIdList theIdList)
224 {
225   IVtk_ShapeIdList::Iterator anIt (theIdList);
226   for (; anIt.More(); anIt.Next())
227   {
228     if (!myIdsSet.Contains (anIt.Value()))
229     {
230       myIdsSet.Add (anIt.Value());
231     }
232   }
233 }
234
235 //! Set ids to be passed through this filter.
236 //================================================================
237 // Function : SetIdsArrayName
238 // Purpose  : 
239 //================================================================
240 void IVtkTools_SubPolyDataFilter::SetIdsArrayName (const char* theArrayName)
241 {
242   myIdsArrayName = theArrayName;
243 }