VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataBranchUtilities.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPolyDataBranchUtilities.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.3 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
23#include "vtkCleanPolyData.h"
24#include "vtkPolyData.h"
25#include "vtkPointData.h"
26#include "vtkCellArray.h"
27#include "vtkIdList.h"
28#include "vtkObjectFactory.h"
29#include "vtkVersion.h"
30
31
33
34void vtkvmtkPolyDataBranchUtilities::GetGroupsIdList(vtkPolyData* surface, const char* groupIdsArrayName, vtkIdList* groupIds)
35{
36 vtkDataArray* groupIdsArray = surface->GetPointData()->GetArray(groupIdsArrayName);
37
38 int numberOfPoints = surface->GetNumberOfPoints();
39
40 groupIds->Initialize();
41
42 int maxGroupId = 0;
43 int i;
44 for (i=0; i<numberOfPoints; i++)
45 {
46 vtkIdType groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
47 if (groupId > maxGroupId)
48 {
49 maxGroupId = groupId;
50 }
51 }
52
53 vtkIdList* isGroupList = vtkIdList::New();
54 isGroupList->SetNumberOfIds(maxGroupId+1);
55
56 for (i=0; i<maxGroupId+1; i++)
57 {
58 isGroupList->SetId(i,0);
59 }
60
61 for (i=0; i<numberOfPoints; i++)
62 {
63 vtkIdType groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
64 isGroupList->SetId(groupId,1);
65 }
66
67 for (i=0; i<maxGroupId+1; i++)
68 {
69 int isGroup = isGroupList->GetId(i);
70 if (isGroup == 1)
71 {
72 groupIds->InsertNextId(i);
73 }
74 }
75
76 isGroupList->Delete();
77}
78
79void vtkvmtkPolyDataBranchUtilities::ExtractGroup(vtkPolyData* surface, const char* groupIdsArrayName, vtkIdType groupId, bool cleanGroupSurface, vtkPolyData* groupSurface)
80{
81 vtkDataArray* groupIdsArray = surface->GetPointData()->GetArray(groupIdsArrayName);
82
83 surface->BuildCells();
84
85 int numberOfCells = surface->GetPolys()->GetNumberOfCells();
86
87 vtkCellArray* groupPolys = vtkCellArray::New();
88 vtkIdType npts;
89 const vtkIdType *pts;
90 for (int j=0; j<numberOfCells; j++)
91 {
92 surface->GetCellPoints(j,npts,pts);
93 bool insertCell = true;
94 for (int k=0; k<npts; k++)
95 {
96 if (static_cast<int>(groupIdsArray->GetComponent(pts[k],0) != groupId))
97 {
98 insertCell = false;
99 break;
100 }
101 }
102 if (!insertCell)
103 {
104 continue;
105 }
106 groupPolys->InsertNextCell(npts,pts);
107 }
108 groupPolys->Squeeze();
109
110 // Assemble the group surface from scratch instead of deep copying the input
111 // and swapping out its polys. Once the input has its cell map built,
112 // DeepCopy brings a copy of that map along, and mutating the polys array
113 // afterwards leaves the map out of sync with the actual cells - downstream
114 // filters then read cells through the stale map and crash (observed with
115 // VTK >= 9.6 inside vtkCleanPolyData and vtkPolyDataConnectivityFilter).
116 // This also no longer copies the input's cell data, which referred to the
117 // original full set of cells and was meaningless for the extracted subset.
118 groupSurface->Initialize();
119 vtkPoints* groupPoints = vtkPoints::New();
120 groupPoints->DeepCopy(surface->GetPoints());
121 groupSurface->SetPoints(groupPoints);
122 groupPoints->Delete();
123 groupSurface->GetPointData()->DeepCopy(surface->GetPointData());
124 groupSurface->SetPolys(groupPolys);
125 groupPolys->Delete();
126
127 if (cleanGroupSurface)
128 {
129 vtkCleanPolyData* cleaner = vtkCleanPolyData::New();
130 cleaner->SetInputData(groupSurface);
131 cleaner->Update();
132
133 groupSurface->DeepCopy(cleaner->GetOutput());
134
135 cleaner->Delete();
136 }
137}
Utility functions to ease working with branches and surfaces.
static void ExtractGroup(vtkPolyData *surface, const char *groupIdsArrayName, vtkIdType groupId, bool cleanGroupSurface, vtkPolyData *groupSurface)
static void GetGroupsIdList(vtkPolyData *surface, const char *groupIdsArrayName, vtkIdList *groupIds)
vtkStandardNewMacro(vtkvmtkPolyDataBranchUtilities)