Back to index

salome-smesh  6.5.0
SMDS_MeshElementIDFactory.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SMESH SMDS : implementaion of Salome mesh data structure
00024 //  File   : SMDS_MeshElementIDFactory.cxx
00025 //  Author : Jean-Michel BOULCOURT
00026 //  Module : SMESH
00027 //
00028 #ifdef _MSC_VER
00029 #pragma warning(disable:4786)
00030 #endif
00031 
00032 #include "SMDS_MeshElementIDFactory.hxx"
00033 #include "SMDS_MeshElement.hxx"
00034 #include "SMDS_Mesh.hxx"
00035 
00036 #include "utilities.h"
00037 
00038 #include "SMDS_UnstructuredGrid.hxx"
00039 #include <vtkCellType.h>
00040 
00041 #include <climits>
00042 
00043 using namespace std;
00044 
00045 //=======================================================================
00046 //function : SMDS_MeshElementIDFactory
00047 //purpose  : 
00048 //=======================================================================
00049 SMDS_MeshElementIDFactory::SMDS_MeshElementIDFactory():
00050   SMDS_MeshNodeIDFactory()
00051 {
00052 }
00053 
00054 int SMDS_MeshElementIDFactory::SetInVtkGrid(SMDS_MeshElement * elem)
00055 {
00056    // --- retrieve nodes ID
00057 
00058   SMDS_MeshCell *cell = dynamic_cast<SMDS_MeshCell*>(elem);
00059   assert(cell);
00060   vector<vtkIdType> nodeIds;
00061   SMDS_ElemIteratorPtr it = elem->nodesIterator();
00062   while(it->more())
00063   {
00064       int nodeId = (static_cast<const SMDS_MeshNode*>(it->next()))->getVtkId();
00065       MESSAGE("   node in cell " << cell->getVtkId() << " : " << nodeId)
00066       nodeIds.push_back(nodeId);
00067   }
00068 
00069   // --- insert cell in vtkUnstructuredGrid
00070 
00071   vtkUnstructuredGrid * grid = myMesh->getGrid();
00072   //int locType = elem->GetType();
00073   int typ = VTK_VERTEX;//GetVtkCellType(locType);
00074   int cellId = grid->InsertNextLinkedCell(typ, nodeIds.size(), &nodeIds[0]);
00075   cell->setVtkId(cellId); 
00076   //MESSAGE("SMDS_MeshElementIDFactory::SetInVtkGrid " << cellId);
00077   return cellId;
00078 }
00079 
00080 //=======================================================================
00081 //function : BindID
00082 //purpose  : 
00083 //=======================================================================
00084 
00085 bool SMDS_MeshElementIDFactory::BindID(int ID, SMDS_MeshElement * elem)
00086 {
00087   MESSAGE("SMDS_MeshElementIDFactory::BindID " << ID);
00088   SetInVtkGrid(elem);
00089   return myMesh->registerElement(ID, elem);
00090 }
00091 
00092 //=======================================================================
00093 //function : MeshElement
00094 //purpose  : 
00095 //=======================================================================
00096 SMDS_MeshElement* SMDS_MeshElementIDFactory::MeshElement(int ID)
00097 {
00098   if ((ID<1) || (ID>=myMesh->myCells.size()))
00099     return NULL;
00100   const SMDS_MeshElement* elem = GetMesh()->FindElement(ID);
00101   return (SMDS_MeshElement*)(elem);
00102 }
00103 
00104 //=======================================================================
00105 //function : GetFreeID
00106 //purpose  : 
00107 //=======================================================================
00108 
00109 int SMDS_MeshElementIDFactory::GetFreeID()
00110 {
00111   int ID;
00112   do {
00113     ID = SMDS_MeshIDFactory::GetFreeID();
00114   } while ( MeshElement( ID ));
00115   return ID;
00116 }
00117 
00118 //=======================================================================
00119 //function : ReleaseID
00120 //purpose  : 
00121 //=======================================================================
00122 void SMDS_MeshElementIDFactory::ReleaseID(int ID, int vtkId)
00123 {
00124   if (ID < 1) // TODO check case ID == O
00125     {
00126       MESSAGE("~~~~~~~~~~~~~~ SMDS_MeshElementIDFactory::ReleaseID ID = " << ID);
00127       return;
00128     }
00129   //MESSAGE("~~~~~~~~~~~~~~ SMDS_MeshElementIDFactory::ReleaseID smdsId vtkId " << ID << " " << vtkId);
00130   if (vtkId >= 0)
00131     {
00132       assert(vtkId < myMesh->myCellIdVtkToSmds.size());
00133       myMesh->myCellIdVtkToSmds[vtkId] = -1;
00134       myMesh->setMyModified();
00135     }
00136   SMDS_MeshIDFactory::ReleaseID(ID);
00137   if (ID == myMax)
00138     myMax = 0;
00139   if (ID == myMin)
00140     myMax = 0;
00141 }
00142 
00143 //=======================================================================
00144 //function : updateMinMax
00145 //purpose  : 
00146 //=======================================================================
00147 
00148 void SMDS_MeshElementIDFactory::updateMinMax() const
00149 {
00150   myMin = INT_MAX;
00151   myMax = 0;
00152   for (int i = 0; i < myMesh->myCells.size(); i++)
00153     {
00154       if (myMesh->myCells[i])
00155         {
00156           int id = myMesh->myCells[i]->GetID();
00157           if (id > myMax)
00158             myMax = id;
00159           if (id < myMin)
00160             myMin = id;
00161         }
00162     }
00163   if (myMin == INT_MAX)
00164     myMin = 0;
00165 }
00166 
00167 //=======================================================================
00168 //function : elementsIterator
00169 //purpose  : Return an iterator on elements of the factory
00170 //=======================================================================
00171 
00172 SMDS_ElemIteratorPtr SMDS_MeshElementIDFactory::elementsIterator() const
00173 {
00174     return myMesh->elementsIterator(SMDSAbs_All);
00175 }
00176 
00177 void SMDS_MeshElementIDFactory::Clear()
00178 {
00179   //myMesh->myCellIdSmdsToVtk.clear();
00180   myMesh->myCellIdVtkToSmds.clear();
00181   myMin = myMax = 0;
00182   SMDS_MeshIDFactory::Clear();
00183 }