Salome HOME
PR: debug SMDS memimp
[modules/smesh.git] / src / SMDS / SMDS_UnstructuredGrid.cxx
index 99bd56fb6611af1fde43220ea2295bb289eac848..143d54334de7988cf8a1fd00aca95cc8cdd3448f 100644 (file)
 
 
 #include "SMDS_UnstructuredGrid.hxx"
+#include "utilities.h"
 
-using namespace std;
-
-vtkCellLinks::Link* SMDS_CellLinks::AdjustSize(vtkIdType sz)
-{
-  vtkIdType i;
-  vtkCellLinks::Link *newArray;
-  vtkIdType newSize = sz;
-  vtkCellLinks::Link linkInit = {0,NULL};
-
-  newArray = new vtkCellLinks::Link[newSize];
-
-  for (i=0; i<sz && i<this->Size; i++)
-    {
-    newArray[i] = this->Array[i];
-    }
-
-  for (i=this->Size; i < newSize ; i++)
-    {
-    newArray[i] = linkInit;
-    }
+#include <vtkCellArray.h>
+#include <vtkCellLinks.h>
+#include <vtkIdTypeArray.h>
+#include <vtkUnsignedCharArray.h>
 
-  this->Size = newSize;
-  delete [] this->Array;
-  this->Array = newArray;
+#include <list>
 
-  return this->Array;
-}
+using namespace std;
 
-SMDS_CellLinks* SMDS_CellLinks::New()
+SMDS_UnstructuredGrid* SMDS_UnstructuredGrid::New()
 {
-  return new SMDS_CellLinks();
+       MESSAGE("SMDS_UnstructuredGrid::New");
+       return new SMDS_UnstructuredGrid();
 }
 
-SMDS_CellLinks::SMDS_CellLinks() : vtkCellLinks()
+SMDS_UnstructuredGrid::SMDS_UnstructuredGrid() : vtkUnstructuredGrid()
 {
 }
 
-SMDS_CellLinks::~SMDS_CellLinks()
+SMDS_UnstructuredGrid::~SMDS_UnstructuredGrid()
 {
 }
 
-void SMDS_UnstructuredGrid::BuildLinks()
-{
-  // Remove the old links if they are already built
-  if (this->Links)
-    {
-    this->Links->UnRegister(this);
-    }
-
-  this->Links = SMDS_CellLinks::New();
-  this->Links->Allocate(this->GetNumberOfPoints());
-  this->Links->Register(this);
-  this->Links->BuildLinks(this, this->Connectivity);
-  this->Links->Delete();
-}
 
-SMDS_CellLinks* SMDS_UnstructuredGrid::GetCellLinks()
+unsigned long SMDS_UnstructuredGrid::GetMTime()
 {
-  return static_cast<SMDS_CellLinks*>(this->Links);
+       unsigned long mtime = vtkUnstructuredGrid::GetMTime();
+       MESSAGE("vtkUnstructuredGrid::GetMTime: " << mtime);
+       return mtime;
 }
 
-SMDS_UnstructuredGrid* SMDS_UnstructuredGrid::New()
+void SMDS_UnstructuredGrid::Update()
 {
-  return new SMDS_UnstructuredGrid();
+       MESSAGE("SMDS_UnstructuredGrid::Update");
+       return vtkUnstructuredGrid::Update();
 }
 
-SMDS_UnstructuredGrid::SMDS_UnstructuredGrid() : vtkUnstructuredGrid()
+void SMDS_UnstructuredGrid::UpdateInformation()
 {
+       MESSAGE("SMDS_UnstructuredGrid::UpdateInformation");
+       return vtkUnstructuredGrid::UpdateInformation();
 }
 
-SMDS_UnstructuredGrid::~SMDS_UnstructuredGrid()
+void SMDS_UnstructuredGrid::compactGrid(std::vector<int>& idNodesOldToNew, int newNodeSize,
+                                                                               std::vector<int>& idCellsOldToNew, int newCellSize)
 {
-}
+       MESSAGE("------------------------- SMDS_UnstructuredGrid::compactGrid " << newNodeSize << " " << newCellSize);
+
+       int startHole = 0;
+       int endHole = 0;
+       int startBloc = 0;
+       int endBloc = 0;
+       int alreadyCopied = 0;
+
+       typedef enum {lookHoleStart, lookHoleEnd, lookBlocEnd} enumState;
+       enumState compactState = lookHoleStart;
+
+//     if (this->Links)
+//     {
+//             this->Links->UnRegister(this);
+//             this->Links = 0;
+//     }
+
+       // --- if newNodeSize, create a new compacted vtkPoints
+
+       vtkPoints *newPoints = 0;
+       if (newNodeSize)
+       {
+               MESSAGE("-------------- compactGrid, newNodeSize");
+               newPoints = vtkPoints::New();
+               newPoints->Initialize();
+               newPoints->Allocate(newNodeSize);
+               newPoints->SetNumberOfPoints(newNodeSize);
+               int oldNodeSize = idNodesOldToNew.size();
+
+               for (int i=0; i< oldNodeSize; i++)
+               {
+                       switch(compactState)
+                       {
+                       case lookHoleStart:
+                               if (idNodesOldToNew[i] < 0)
+                               {
+                                       MESSAGE("-------------- newNodeSize, startHole " << i <<  " " << oldNodeSize);
+                                       startHole = i;
+                                       compactState = lookHoleEnd;
+                               }
+                               break;
+                       case lookHoleEnd:
+                               if (idNodesOldToNew[i] >= 0)
+                               {
+                                       MESSAGE("-------------- newNodeSize, endHole " << i <<  " " << oldNodeSize);
+                                       endHole = i;
+                                       startBloc = i;
+                                       compactState = lookBlocEnd;
+                               }
+                               break;
+                       case lookBlocEnd:
+                               if (idNodesOldToNew[i] < 0) endBloc = i; // see nbPoints below
+                               else if (i == (oldNodeSize-1)) endBloc = i+1;
+                               if (endBloc)
+                               {
+                                       MESSAGE("-------------- newNodeSize, endbloc " << endBloc <<  " " << oldNodeSize);
+                                       void *target = newPoints->GetVoidPointer(3*alreadyCopied);
+                                       void *source = this->Points->GetVoidPointer(3*startBloc);
+                                       int nbPoints = endBloc - startBloc;
+                                       memcpy(target, source, 3*sizeof(float)*nbPoints);
+                                       for (int j=startBloc; j<endBloc; j++)
+                                               idNodesOldToNew[j] = alreadyCopied++;
+                                       compactState = lookHoleStart;
+                                       startHole = i;
+                                       endHole = 0;
+                                       startBloc = 0;
+                                       endBloc = 0;
+                               }
+                               break;
+                       }
+               }
+               if (!alreadyCopied) // no hole, but shorter, no need to modify idNodesOldToNew
+               {
+                       MESSAGE("------------- newNodeSize, shorter " << oldNodeSize)
+                       void *target = newPoints->GetVoidPointer(0);
+                       void *source = this->Points->GetVoidPointer(0);
+                       int nbPoints = newNodeSize;
+                       memcpy(target, source, 3*sizeof(float)*nbPoints);
+               }
+       }
+
+       // --- create new compacted Connectivity, Locations and Types
+
+    int oldCellSize = this->Types->GetNumberOfTuples();
+
+       vtkCellArray *newConnectivity = vtkCellArray::New();
+       newConnectivity->Initialize();
+       int oldCellDataSize = this->Connectivity->GetData()->GetSize();
+       newConnectivity->Allocate(oldCellDataSize);
+       MESSAGE("oldCellSize="<< oldCellSize << " oldCellDataSize=" << oldCellDataSize);
+
+       vtkUnsignedCharArray *newTypes = vtkUnsignedCharArray::New();
+       newTypes->Initialize();
+       //newTypes->Allocate(oldCellSize);
+       newTypes->SetNumberOfValues(newCellSize);
+
+       vtkIdTypeArray *newLocations = vtkIdTypeArray::New();
+       newLocations->Initialize();
+       //newLocations->Allocate(oldCellSize);
+       newLocations->SetNumberOfValues(newCellSize);
+
+       startHole = 0;
+       endHole = 0;
+       startBloc = 0;
+       endBloc = 0;
+       alreadyCopied = 0;
+       compactState = lookHoleStart;
+
+       vtkIdType tmpid[50];
+       vtkIdType *pointsCell =&tmpid[0]; // --- points id to fill a new cell
+
+    for (int i=0; i<oldCellSize; i++)
+    {
+               switch(compactState)
+               {
+               case lookHoleStart:
+                       if (this->Types->GetValue(i) == VTK_EMPTY_CELL)
+                       {
+                       MESSAGE(" -------- newCellSize, startHole " << i << " " << oldCellSize);
+                               startHole = i;
+                               compactState = lookHoleEnd;
+                       }
+                       break;
+               case lookHoleEnd:
+                       if (this->Types->GetValue(i) != VTK_EMPTY_CELL)
+                       {
+                       MESSAGE(" -------- newCellSize, EndHole " << i << " " << oldCellSize);
+                               endHole = i;
+                               startBloc = i;
+                               compactState = lookBlocEnd;
+                       }
+                       break;
+               case lookBlocEnd:
+                       if (this->Types->GetValue(i) == VTK_EMPTY_CELL) endBloc =i;
+                       else if (i == (oldCellSize-1)) endBloc = i+1;
+                       if (endBloc)
+                       {
+                       MESSAGE(" -------- newCellSize, endBloc " << endBloc << " " << oldCellSize);
+                               for (int j=startBloc; j<endBloc; j++)
+                               {
+                                       newTypes->SetValue(alreadyCopied, this->Types->GetValue(j));
+                                       idCellsOldToNew[j] = alreadyCopied;
+                                       vtkIdType oldLoc = this->Locations->GetValue(j);
+                                       vtkIdType nbpts;
+                                       vtkIdType *oldPtsCell = 0;
+                                       this->Connectivity->GetCell(oldLoc, nbpts, oldPtsCell);
+                                       for (int l=0; l<nbpts; l++)
+                                       {
+                                               int oldval = oldPtsCell[l];
+                                               pointsCell[l] = idNodesOldToNew[oldval];
+                                       }
+                                       int newcnt = newConnectivity->InsertNextCell(nbpts, pointsCell);
+                                       int newLoc = newConnectivity->GetInsertLocation(nbpts);
+                                       newLocations->SetValue(alreadyCopied, newLoc);
+                                       alreadyCopied++;
+                               }
+                       compactState = lookHoleStart;
+                       }
+                       break;
+               }
+    }
+    if (!alreadyCopied) // no hole, but shorter
+    {
+       MESSAGE(" -------- newCellSize, shorter " << oldCellSize);
+               for (int j=0; j<oldCellSize; j++)
+               {
+                       newTypes->SetValue(alreadyCopied, this->Types->GetValue(j));
+                       idCellsOldToNew[j] = alreadyCopied;
+                       vtkIdType oldLoc = this->Locations->GetValue(j);
+                       vtkIdType nbpts;
+                       vtkIdType *oldPtsCell = 0;
+                       this->Connectivity->GetCell(oldLoc, nbpts, oldPtsCell);
+                       //MESSAGE(j << " " << alreadyCopied << " " << (int)this->Types->GetValue(j) << " " << oldLoc << " " << nbpts );
+                       for (int l=0; l<nbpts; l++)
+                       {
+                               int oldval = oldPtsCell[l];
+                               pointsCell[l] = idNodesOldToNew[oldval];
+                               //MESSAGE("   " << oldval << " " << pointsCell[l]);
+                       }
+                       int newcnt = newConnectivity->InsertNextCell(nbpts, pointsCell);
+                       int newLoc = newConnectivity->GetInsertLocation(nbpts);
+                       //MESSAGE(newcnt << " " << newLoc);
+                       newLocations->SetValue(alreadyCopied, newLoc);
+                       alreadyCopied++;
+               }
+    }
 
+    newConnectivity->Squeeze();
+    //newTypes->Squeeze();
+    //newLocations->Squeeze();
 
+    if (newNodeSize)
+    {
+       MESSAGE("------- newNodeSize, setPoints");
+       this->SetPoints(newPoints);
+    }
+    this->SetCells(newTypes, newLocations, newConnectivity);
+    this->BuildLinks();
+}