Salome HOME
52566]: TC7.5.0: Empty group of Balls at Diameter Equal to filter
[modules/smesh.git] / src / SMDS / SMDS_MeshElementIDFactory.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMDS : implementaion of Salome mesh data structure
24 //  File   : SMDS_MeshElementIDFactory.cxx
25 //  Author : Jean-Michel BOULCOURT
26 //  Module : SMESH
27 //
28 #ifdef _MSC_VER
29 #pragma warning(disable:4786)
30 #endif
31
32 #include "SMDS_MeshElementIDFactory.hxx"
33 #include "SMDS_MeshElement.hxx"
34 #include "SMDS_Mesh.hxx"
35
36 #include "utilities.h"
37
38 #include "SMDS_UnstructuredGrid.hxx"
39 #include <vtkCellType.h>
40
41 #include <climits>
42
43 using namespace std;
44
45 //=======================================================================
46 //function : SMDS_MeshElementIDFactory
47 //purpose  : 
48 //=======================================================================
49 SMDS_MeshElementIDFactory::SMDS_MeshElementIDFactory():
50   SMDS_MeshNodeIDFactory()
51 {
52 }
53
54 int SMDS_MeshElementIDFactory::SetInVtkGrid(SMDS_MeshElement * elem)
55 {
56    // --- retrieve nodes ID
57
58   SMDS_MeshCell *cell = dynamic_cast<SMDS_MeshCell*>(elem);
59   assert(cell);
60   vector<vtkIdType> nodeIds;
61   SMDS_ElemIteratorPtr it = elem->nodesIterator();
62   while(it->more())
63   {
64       int nodeId = (static_cast<const SMDS_MeshNode*>(it->next()))->getVtkId();
65       MESSAGE("   node in cell " << cell->getVtkId() << " : " << nodeId)
66       nodeIds.push_back(nodeId);
67   }
68
69   // --- insert cell in vtkUnstructuredGrid
70
71   vtkUnstructuredGrid * grid = myMesh->getGrid();
72   //int locType = elem->GetType();
73   int typ = VTK_VERTEX;//GetVtkCellType(locType);
74   int cellId = grid->InsertNextLinkedCell(typ, nodeIds.size(), &nodeIds[0]);
75   cell->setVtkId(cellId); 
76   //MESSAGE("SMDS_MeshElementIDFactory::SetInVtkGrid " << cellId);
77   return cellId;
78 }
79
80 //=======================================================================
81 //function : BindID
82 //purpose  : 
83 //=======================================================================
84
85 bool SMDS_MeshElementIDFactory::BindID(int ID, SMDS_MeshElement * elem)
86 {
87   MESSAGE("SMDS_MeshElementIDFactory::BindID " << ID);
88   SetInVtkGrid(elem);
89   return myMesh->registerElement(ID, elem);
90 }
91
92 //=======================================================================
93 //function : MeshElement
94 //purpose  : 
95 //=======================================================================
96 SMDS_MeshElement* SMDS_MeshElementIDFactory::MeshElement(int ID)
97 {
98   if ((ID<1) || (ID>=myMesh->myCells.size()))
99     return NULL;
100   const SMDS_MeshElement* elem = GetMesh()->FindElement(ID);
101   return (SMDS_MeshElement*)(elem);
102 }
103
104 //=======================================================================
105 //function : GetFreeID
106 //purpose  : 
107 //=======================================================================
108
109 int SMDS_MeshElementIDFactory::GetFreeID()
110 {
111   int ID;
112   do {
113     ID = SMDS_MeshIDFactory::GetFreeID();
114   } while ( MeshElement( ID ));
115   return ID;
116 }
117
118 //=======================================================================
119 //function : ReleaseID
120 //purpose  : 
121 //=======================================================================
122 void SMDS_MeshElementIDFactory::ReleaseID(int ID, int vtkId)
123 {
124   if (ID < 1) // TODO check case ID == O
125     {
126       MESSAGE("~~~~~~~~~~~~~~ SMDS_MeshElementIDFactory::ReleaseID ID = " << ID);
127       return;
128     }
129   //MESSAGE("~~~~~~~~~~~~~~ SMDS_MeshElementIDFactory::ReleaseID smdsId vtkId " << ID << " " << vtkId);
130   if (vtkId >= 0)
131     {
132       assert(vtkId < myMesh->myCellIdVtkToSmds.size());
133       myMesh->myCellIdVtkToSmds[vtkId] = -1;
134       myMesh->setMyModified();
135     }
136   SMDS_MeshIDFactory::ReleaseID(ID);
137   if (ID == myMax)
138     myMax = 0;
139   if (ID == myMin)
140     myMax = 0;
141 }
142
143 //=======================================================================
144 //function : updateMinMax
145 //purpose  : 
146 //=======================================================================
147
148 void SMDS_MeshElementIDFactory::updateMinMax() const
149 {
150   myMin = INT_MAX;
151   myMax = 0;
152   for (int i = 0; i < myMesh->myCells.size(); i++)
153     {
154       if (myMesh->myCells[i])
155         {
156           int id = myMesh->myCells[i]->GetID();
157           if (id > myMax)
158             myMax = id;
159           if (id < myMin)
160             myMin = id;
161         }
162     }
163   if (myMin == INT_MAX)
164     myMin = 0;
165 }
166
167 //=======================================================================
168 //function : elementsIterator
169 //purpose  : Return an iterator on elements of the factory
170 //=======================================================================
171
172 SMDS_ElemIteratorPtr SMDS_MeshElementIDFactory::elementsIterator() const
173 {
174     return myMesh->elementsIterator(SMDSAbs_All);
175 }
176
177 void SMDS_MeshElementIDFactory::Clear()
178 {
179   //myMesh->myCellIdSmdsToVtk.clear();
180   myMesh->myCellIdVtkToSmds.clear();
181   myMin = myMax = 0;
182   SMDS_MeshIDFactory::Clear();
183 }