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;
61 typedef enum {lookHoleStart, lookHoleEnd, lookBlocEnd} enumState;
62 enumState compactState = lookHoleStart;
66 // this->Links->UnRegister(this);
70 // --- if newNodeSize, create a new compacted vtkPoints
72 vtkPoints *newPoints = 0;
75 MESSAGE("-------------- compactGrid, newNodeSize " << newNodeSize);
76 newPoints = vtkPoints::New();
77 newPoints->Initialize();
78 newPoints->Allocate(newNodeSize);
79 newPoints->SetNumberOfPoints(newNodeSize);
80 int oldNodeSize = idNodesOldToNew.size();
82 for (int i=0; i< oldNodeSize; i++)
87 if (idNodesOldToNew[i] < 0)
89 MESSAGE("-------------- newNodeSize, startHole " << i << " " << oldNodeSize);
91 if (!alreadyCopied) // copy the first bloc
93 MESSAGE("--------- copy first nodes before hole " << i << " " << oldNodeSize);
94 copyNodes(newPoints, idNodesOldToNew, alreadyCopied, 0, startHole);
96 compactState = lookHoleEnd;
100 if (idNodesOldToNew[i] >= 0)
102 MESSAGE("-------------- newNodeSize, endHole " << i << " " << oldNodeSize);
105 compactState = lookBlocEnd;
109 if (idNodesOldToNew[i] < 0) endBloc = i; // see nbPoints below
110 else if (i == (oldNodeSize-1)) endBloc = i+1;
113 MESSAGE("-------------- newNodeSize, endbloc " << endBloc << " " << oldNodeSize);
114 copyNodes(newPoints, idNodesOldToNew, alreadyCopied, startBloc, endBloc);
115 compactState = lookHoleStart;
124 if (!alreadyCopied) // no hole, but shorter, no need to modify idNodesOldToNew
126 MESSAGE("------------- newNodeSize, shorter " << oldNodeSize);
127 copyNodes(newPoints, idNodesOldToNew, alreadyCopied, 0, newNodeSize);
129 newPoints->Squeeze();
132 // --- create new compacted Connectivity, Locations and Types
134 int oldCellSize = this->Types->GetNumberOfTuples();
136 vtkCellArray *newConnectivity = vtkCellArray::New();
137 newConnectivity->Initialize();
138 int oldCellDataSize = this->Connectivity->GetData()->GetSize();
139 newConnectivity->Allocate(oldCellDataSize);
140 MESSAGE("oldCellSize="<< oldCellSize << " oldCellDataSize=" << oldCellDataSize);
142 vtkUnsignedCharArray *newTypes = vtkUnsignedCharArray::New();
143 newTypes->Initialize();
144 //newTypes->Allocate(oldCellSize);
145 newTypes->SetNumberOfValues(newCellSize);
147 vtkIdTypeArray *newLocations = vtkIdTypeArray::New();
148 newLocations->Initialize();
149 //newLocations->Allocate(oldCellSize);
150 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;
173 if (!alreadyCopied) // copy the first bloc
175 MESSAGE("--------- copy first bloc before hole " << i << " " << oldCellSize);
176 copyBloc(newTypes, idCellsOldToNew, idNodesOldToNew, newConnectivity, newLocations, pointsCell, alreadyCopied, 0, startHole);
181 if (this->Types->GetValue(i) != VTK_EMPTY_CELL)
183 MESSAGE(" -------- newCellSize, EndHole " << i << " " << oldCellSize);
186 compactState = lookBlocEnd;
187 holes += endHole - startHole;
188 //alreadyCopied = startBloc -holes;
193 if (this->Types->GetValue(i) == VTK_EMPTY_CELL) endBloc =i;
194 else if (i == (oldCellSize-1)) endBloc = i+1;
197 MESSAGE(" -------- newCellSize, endBloc " << endBloc << " " << oldCellSize);
198 copyBloc(newTypes, idCellsOldToNew, idNodesOldToNew, newConnectivity, newLocations, pointsCell, alreadyCopied, startBloc, endBloc);
199 compactState = lookHoleStart;
204 if (!alreadyCopied) // no hole, but shorter
206 MESSAGE(" -------- newCellSize, shorter " << oldCellSize);
207 copyBloc(newTypes, idCellsOldToNew, idNodesOldToNew, newConnectivity, newLocations, pointsCell, alreadyCopied, 0, oldCellSize);
210 newConnectivity->Squeeze();
211 //newTypes->Squeeze();
212 //newLocations->Squeeze();
216 MESSAGE("------- newNodeSize, setPoints");
217 this->SetPoints(newPoints);
218 MESSAGE("NumberOfPoints: " << this->GetNumberOfPoints());
220 this->SetCells(newTypes, newLocations, newConnectivity);
224 void SMDS_UnstructuredGrid::copyNodes(vtkPoints *newPoints,
225 std::vector<int>& idNodesOldToNew,
230 MESSAGE("copyNodes " << alreadyCopied << " " << start << " " << end << " size: " << end - start << " total: " << alreadyCopied + end - start);
231 void *target = newPoints->GetVoidPointer(3*alreadyCopied);
232 void *source = this->Points->GetVoidPointer(3*start);
233 int nbPoints = end - start;
236 memcpy(target, source, 3*sizeof(float)*nbPoints);
237 for (int j=start; j<end; j++)
238 idNodesOldToNew[j] = alreadyCopied++;
242 void SMDS_UnstructuredGrid::copyBloc(vtkUnsignedCharArray *newTypes,
243 std::vector<int>& idCellsOldToNew,
244 std::vector<int>& idNodesOldToNew,
245 vtkCellArray* newConnectivity,
246 vtkIdTypeArray* newLocations,
247 vtkIdType* pointsCell,
252 MESSAGE("copyBloc " << alreadyCopied << " " << start << " " << end << " size: " << end - start << " total: " << alreadyCopied + end - start);
253 for (int j=start; j<end; j++)
255 newTypes->SetValue(alreadyCopied, this->Types->GetValue(j));
256 idCellsOldToNew[j] = alreadyCopied;
257 vtkIdType oldLoc = this->Locations->GetValue(j);
259 vtkIdType *oldPtsCell = 0;
260 this->Connectivity->GetCell(oldLoc, nbpts, oldPtsCell);
261 //MESSAGE(j << " " << alreadyCopied << " " << (int)this->Types->GetValue(j) << " " << oldLoc << " " << nbpts );
262 for (int l=0; l<nbpts; l++)
264 int oldval = oldPtsCell[l];
265 pointsCell[l] = idNodesOldToNew[oldval];
266 //MESSAGE(" " << oldval << " " << pointsCell[l]);
268 int newcnt = newConnectivity->InsertNextCell(nbpts, pointsCell);
269 int newLoc = newConnectivity->GetInsertLocation(nbpts);
270 //MESSAGE(newcnt << " " << newLoc);
271 newLocations->SetValue(alreadyCopied, newLoc);