3 #include "SMDS_UnstructuredGrid.hxx"
6 #include <vtkCellArray.h>
7 #include <vtkIdTypeArray.h>
8 #include <vtkUnsignedCharArray.h>
14 SMDS_UnstructuredGrid* SMDS_UnstructuredGrid::New()
16 MESSAGE("SMDS_UnstructuredGrid::New");
17 return new SMDS_UnstructuredGrid();
20 SMDS_UnstructuredGrid::SMDS_UnstructuredGrid() : vtkUnstructuredGrid()
24 SMDS_UnstructuredGrid::~SMDS_UnstructuredGrid()
29 unsigned long SMDS_UnstructuredGrid::GetMTime()
31 unsigned long mtime = vtkUnstructuredGrid::GetMTime();
32 MESSAGE("vtkUnstructuredGrid::GetMTime: " << mtime);
36 void SMDS_UnstructuredGrid::UpdateInformation()
38 MESSAGE("SMDS_UnstructuredGrid::UpdateInformation");
39 return vtkUnstructuredGrid::UpdateInformation();
42 void SMDS_UnstructuredGrid::compactGrid(std::vector<int>& idNodesOldToNew, int newNodeSize,
43 std::vector<int>& idCellsOldToNew, int newCellSize)
45 MESSAGE("------------------------- SMDS_UnstructuredGrid::compactGrid " << newNodeSize << " " << newCellSize);
51 int alreadyCopied = 0;
52 typedef enum {lookHoleStart, lookHoleEnd, lookBlocEnd} enumState;
53 enumState compactState = lookHoleStart;
55 // --- if newNodeSize, create a new compacted vtkPoints
57 vtkPoints *newPoints = 0;
60 MESSAGE("-------------- compactGrid, newNodeSize");
61 newPoints = vtkPoints::New();
62 newPoints->Initialize();
63 newPoints->Allocate(newNodeSize);
64 newPoints->SetNumberOfPoints(newNodeSize);
65 int oldNodeSize = idNodesOldToNew.size();
67 for (int i=0; i< oldNodeSize; i++)
72 if (idNodesOldToNew[i] < 0)
75 compactState = lookHoleEnd;
79 if (idNodesOldToNew[i] >= 0)
83 compactState = lookBlocEnd;
87 if (idNodesOldToNew[i] < 0) endBloc = i; // see nbPoints below
88 else if (i == (oldNodeSize-1)) endBloc = i+1;
91 MESSAGE("-------------- newNodeSize, endbloc");
92 void *target = newPoints->GetVoidPointer(3*alreadyCopied);
93 void *source = this->Points->GetVoidPointer(3*startBloc);
94 int nbPoints = endBloc - startBloc;
95 memcpy(target, source, 3*sizeof(float)*nbPoints);
96 for (int j=startBloc; j<endBloc; j++)
97 idNodesOldToNew[j] = alreadyCopied++;
98 compactState = lookHoleStart;
107 if (!alreadyCopied) // no hole, but shorter, no need to modify idNodesOldToNew
109 MESSAGE("------------- newNodeSize, shorter")
110 void *target = newPoints->GetVoidPointer(0);
111 void *source = this->Points->GetVoidPointer(0);
112 int nbPoints = newNodeSize;
113 memcpy(target, source, 3*sizeof(float)*nbPoints);
117 // --- create new compacted Connectivity, Locations and Types
119 int oldCellSize = this->Types->GetNumberOfTuples();
121 vtkCellArray *newConnectivity = vtkCellArray::New();
122 newConnectivity->Initialize();
123 int oldCellDataSize = this->Connectivity->GetData()->GetSize();
124 newConnectivity->Allocate(oldCellDataSize);
125 MESSAGE("oldCellSize="<< oldCellSize << " oldCellDataSize=" << oldCellDataSize);
127 vtkUnsignedCharArray *newTypes = vtkUnsignedCharArray::New();
128 newTypes->Initialize();
129 //newTypes->Allocate(oldCellSize);
130 newTypes->SetNumberOfValues(newCellSize);
132 vtkIdTypeArray *newLocations = vtkIdTypeArray::New();
133 newLocations->Initialize();
134 //newLocations->Allocate(oldCellSize);
135 newLocations->SetNumberOfValues(newCellSize);
142 compactState = lookHoleStart;
145 vtkIdType *pointsCell =&tmpid[0]; // --- points id to fill a new cell
147 for (int i=0; i<oldCellSize; i++)
152 if (this->Types->GetValue(i) == VTK_EMPTY_CELL)
155 compactState = lookHoleEnd;
159 if (this->Types->GetValue(i) != VTK_EMPTY_CELL)
163 compactState = lookBlocEnd;
167 if (this->Types->GetValue(i) == VTK_EMPTY_CELL) endBloc =i;
168 else if (i == (oldCellSize-1)) endBloc = i+1;
171 MESSAGE(" -------- newCellSize, endBloc");
172 for (int j=startBloc; j<endBloc; j++)
174 newTypes->SetValue(alreadyCopied, this->Types->GetValue(j));
175 idCellsOldToNew[j] = alreadyCopied;
176 int oldLoc = this->Locations->GetValue(j);
178 this->Connectivity->GetCell(oldLoc, nbpts, pointsCell);
179 for (int l=0; l<nbpts; l++)
180 pointsCell[l] = idNodesOldToNew[pointsCell[l]];
181 int newcnt = newConnectivity->InsertNextCell(nbpts, pointsCell);
182 int newLoc = newConnectivity->GetInsertLocation(nbpts);
183 newLocations->SetValue(alreadyCopied, newLoc);
190 if (!alreadyCopied) // no hole, but shorter
192 MESSAGE(" -------- newCellSize, shorter");
193 for (int j=0; j<oldCellSize; j++)
195 newTypes->SetValue(alreadyCopied, this->Types->GetValue(j));
196 idCellsOldToNew[j] = alreadyCopied;
197 int oldLoc = this->Locations->GetValue(j);
199 this->Connectivity->GetCell(oldLoc, nbpts, pointsCell);
200 //MESSAGE(j << " " << alreadyCopied << " " << (int)this->Types->GetValue(j) << " " << oldLoc << " " << nbpts );
201 for (int l=0; l<nbpts; l++)
203 int oldval = pointsCell[l];
204 pointsCell[l] = idNodesOldToNew[oldval];
205 //MESSAGE(" " << oldval << " " << pointsCell[l]);
207 int newcnt = newConnectivity->InsertNextCell(nbpts, pointsCell);
208 int newLoc = newConnectivity->GetInsertLocation(nbpts);
209 //MESSAGE(newcnt << " " << newLoc);
210 newLocations->SetValue(alreadyCopied, newLoc);
215 newConnectivity->Squeeze();
216 //newTypes->Squeeze();
217 //newLocations->Squeeze();
221 MESSAGE("------- newNodeSize, setPoints");
222 this->SetPoints(newPoints);
224 this->SetCells(newTypes, newLocations, newConnectivity);