3 #include "SMDS_UnstructuredGrid.hxx"
6 #include <vtkCellArray.h>
7 #include <vtkCellLinks.h>
8 #include <vtkIdTypeArray.h>
9 #include <vtkUnsignedCharArray.h>
15 SMDS_UnstructuredGrid* SMDS_UnstructuredGrid::New()
17 MESSAGE("SMDS_UnstructuredGrid::New");
18 return new SMDS_UnstructuredGrid();
21 SMDS_UnstructuredGrid::SMDS_UnstructuredGrid() : vtkUnstructuredGrid()
25 SMDS_UnstructuredGrid::~SMDS_UnstructuredGrid()
30 unsigned long SMDS_UnstructuredGrid::GetMTime()
32 unsigned long mtime = vtkUnstructuredGrid::GetMTime();
33 MESSAGE("vtkUnstructuredGrid::GetMTime: " << mtime);
37 void SMDS_UnstructuredGrid::Update()
39 MESSAGE("SMDS_UnstructuredGrid::Update");
40 return vtkUnstructuredGrid::Update();
43 void SMDS_UnstructuredGrid::UpdateInformation()
45 MESSAGE("SMDS_UnstructuredGrid::UpdateInformation");
46 return vtkUnstructuredGrid::UpdateInformation();
49 void SMDS_UnstructuredGrid::compactGrid(std::vector<int>& idNodesOldToNew, int newNodeSize,
50 std::vector<int>& idCellsOldToNew, int newCellSize)
52 MESSAGE("------------------------- SMDS_UnstructuredGrid::compactGrid " << newNodeSize << " " << newCellSize);
58 int alreadyCopied = 0;
60 typedef enum {lookHoleStart, lookHoleEnd, lookBlocEnd} enumState;
61 enumState compactState = lookHoleStart;
65 // this->Links->UnRegister(this);
69 // --- if newNodeSize, create a new compacted vtkPoints
71 vtkPoints *newPoints = 0;
74 MESSAGE("-------------- compactGrid, newNodeSize");
75 newPoints = vtkPoints::New();
76 newPoints->Initialize();
77 newPoints->Allocate(newNodeSize);
78 newPoints->SetNumberOfPoints(newNodeSize);
79 int oldNodeSize = idNodesOldToNew.size();
81 for (int i=0; i< oldNodeSize; i++)
86 if (idNodesOldToNew[i] < 0)
88 MESSAGE("-------------- newNodeSize, startHole " << i << " " << oldNodeSize);
90 compactState = lookHoleEnd;
94 if (idNodesOldToNew[i] >= 0)
96 MESSAGE("-------------- newNodeSize, endHole " << i << " " << oldNodeSize);
99 compactState = lookBlocEnd;
103 if (idNodesOldToNew[i] < 0) endBloc = i; // see nbPoints below
104 else if (i == (oldNodeSize-1)) endBloc = i+1;
107 MESSAGE("-------------- newNodeSize, endbloc " << endBloc << " " << oldNodeSize);
108 void *target = newPoints->GetVoidPointer(3*alreadyCopied);
109 void *source = this->Points->GetVoidPointer(3*startBloc);
110 int nbPoints = endBloc - startBloc;
111 memcpy(target, source, 3*sizeof(float)*nbPoints);
112 for (int j=startBloc; j<endBloc; j++)
113 idNodesOldToNew[j] = alreadyCopied++;
114 compactState = lookHoleStart;
123 if (!alreadyCopied) // no hole, but shorter, no need to modify idNodesOldToNew
125 MESSAGE("------------- newNodeSize, shorter " << oldNodeSize)
126 void *target = newPoints->GetVoidPointer(0);
127 void *source = this->Points->GetVoidPointer(0);
128 int nbPoints = newNodeSize;
129 memcpy(target, source, 3*sizeof(float)*nbPoints);
133 // --- create new compacted Connectivity, Locations and Types
135 int oldCellSize = this->Types->GetNumberOfTuples();
137 vtkCellArray *newConnectivity = vtkCellArray::New();
138 newConnectivity->Initialize();
139 int oldCellDataSize = this->Connectivity->GetData()->GetSize();
140 newConnectivity->Allocate(oldCellDataSize);
141 MESSAGE("oldCellSize="<< oldCellSize << " oldCellDataSize=" << oldCellDataSize);
143 vtkUnsignedCharArray *newTypes = vtkUnsignedCharArray::New();
144 newTypes->Initialize();
145 //newTypes->Allocate(oldCellSize);
146 newTypes->SetNumberOfValues(newCellSize);
148 vtkIdTypeArray *newLocations = vtkIdTypeArray::New();
149 newLocations->Initialize();
150 //newLocations->Allocate(oldCellSize);
151 newLocations->SetNumberOfValues(newCellSize);
158 compactState = lookHoleStart;
161 vtkIdType *pointsCell =&tmpid[0]; // --- points id to fill a new cell
163 for (int i=0; i<oldCellSize; i++)
168 if (this->Types->GetValue(i) == VTK_EMPTY_CELL)
170 MESSAGE(" -------- newCellSize, startHole " << i << " " << oldCellSize);
172 compactState = lookHoleEnd;
176 if (this->Types->GetValue(i) != VTK_EMPTY_CELL)
178 MESSAGE(" -------- newCellSize, EndHole " << i << " " << oldCellSize);
181 compactState = lookBlocEnd;
185 if (this->Types->GetValue(i) == VTK_EMPTY_CELL) endBloc =i;
186 else if (i == (oldCellSize-1)) endBloc = i+1;
189 MESSAGE(" -------- newCellSize, endBloc " << endBloc << " " << oldCellSize);
190 for (int j=startBloc; j<endBloc; j++)
192 newTypes->SetValue(alreadyCopied, this->Types->GetValue(j));
193 idCellsOldToNew[j] = alreadyCopied;
194 vtkIdType oldLoc = this->Locations->GetValue(j);
196 vtkIdType *oldPtsCell = 0;
197 this->Connectivity->GetCell(oldLoc, nbpts, oldPtsCell);
198 for (int l=0; l<nbpts; l++)
200 int oldval = oldPtsCell[l];
201 pointsCell[l] = idNodesOldToNew[oldval];
203 int newcnt = newConnectivity->InsertNextCell(nbpts, pointsCell);
204 int newLoc = newConnectivity->GetInsertLocation(nbpts);
205 newLocations->SetValue(alreadyCopied, newLoc);
208 compactState = lookHoleStart;
213 if (!alreadyCopied) // no hole, but shorter
215 MESSAGE(" -------- newCellSize, shorter " << oldCellSize);
216 for (int j=0; j<oldCellSize; j++)
218 newTypes->SetValue(alreadyCopied, this->Types->GetValue(j));
219 idCellsOldToNew[j] = alreadyCopied;
220 vtkIdType oldLoc = this->Locations->GetValue(j);
222 vtkIdType *oldPtsCell = 0;
223 this->Connectivity->GetCell(oldLoc, nbpts, oldPtsCell);
224 //MESSAGE(j << " " << alreadyCopied << " " << (int)this->Types->GetValue(j) << " " << oldLoc << " " << nbpts );
225 for (int l=0; l<nbpts; l++)
227 int oldval = oldPtsCell[l];
228 pointsCell[l] = idNodesOldToNew[oldval];
229 //MESSAGE(" " << oldval << " " << pointsCell[l]);
231 int newcnt = newConnectivity->InsertNextCell(nbpts, pointsCell);
232 int newLoc = newConnectivity->GetInsertLocation(nbpts);
233 //MESSAGE(newcnt << " " << newLoc);
234 newLocations->SetValue(alreadyCopied, newLoc);
239 newConnectivity->Squeeze();
240 //newTypes->Squeeze();
241 //newLocations->Squeeze();
245 MESSAGE("------- newNodeSize, setPoints");
246 this->SetPoints(newPoints);
248 this->SetCells(newTypes, newLocations, newConnectivity);