Salome HOME
PR: debug SMDS memimp
[modules/smesh.git] / src / SMDS / SMDS_MeshNode.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  SMESH SMDS : implementaion of Salome mesh data structure
23 //
24 #ifdef _MSC_VER
25 #pragma warning(disable:4786)
26 #endif
27
28 #include "SMDS_MeshNode.hxx"
29 #include "SMDS_SpacePosition.hxx"
30 #include "SMDS_IteratorOfElements.hxx"
31 #include "SMDS_Mesh.hxx"
32 #include <vtkUnstructuredGrid.h>
33
34 #define protected public
35 #include <vtkCellLinks.h>
36 #define protected protected
37
38 #include "utilities.h"
39 #include "Utils_SALOME_Exception.hxx"
40 #include <cassert>
41
42 using namespace std;
43
44 int SMDS_MeshNode::nbNodes =0;
45
46 //=======================================================================
47 //function : SMDS_MeshNode
48 //purpose  : 
49 //=======================================================================
50 SMDS_MeshNode::SMDS_MeshNode() :
51   SMDS_MeshElement(-1, -1, -1),
52   myPosition(SMDS_SpacePosition::originSpacePosition())
53 {
54   nbNodes++;
55 }
56
57 SMDS_MeshNode::SMDS_MeshNode(int id, int meshId, int shapeId, double x, double y, double z):
58   SMDS_MeshElement(id, meshId, shapeId),
59   myPosition(SMDS_SpacePosition::originSpacePosition())
60 {
61   nbNodes++;
62   init(id, meshId, shapeId, x, y ,z);
63 }
64
65 void SMDS_MeshNode::init(int id, int meshId, int shapeId, double x, double y, double z)
66 {
67   myID = id;
68   myMeshId = meshId;
69   myShapeId = shapeId;
70   myIdInShape = -1;
71   //MESSAGE("Node " << myID << " (" << x << ", " << y << ", " << z << ")");
72   SMDS_Mesh* mesh = SMDS_Mesh::_meshList[myMeshId];
73   vtkUnstructuredGrid * grid = mesh->getGrid();
74   vtkPoints *points = grid->GetPoints();
75   points->InsertPoint(myID, x, y, z);
76   vtkCellLinks *cellLinks = grid->GetCellLinks();
77   if (myID >=cellLinks->Size)
78           cellLinks->Resize(myID+SMDS_Mesh::chunkSize);
79 }
80
81 SMDS_MeshNode::~SMDS_MeshNode()
82 {
83   nbNodes--;
84 }
85
86 //=======================================================================
87 //function : RemoveInverseElement
88 //purpose  : 
89 //=======================================================================
90
91 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
92 {
93     //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
94     const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
95     MYASSERT(cell);
96     SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myID, cell->getVtkId());
97 }
98
99 //=======================================================================
100 //function : Print
101 //purpose  : 
102 //=======================================================================
103
104 void SMDS_MeshNode::Print(ostream & OS) const
105 {
106         OS << "Node <" << myID << "> : X = " << X() << " Y = "
107                 << Y() << " Z = " << Z() << endl;
108 }
109
110 //=======================================================================
111 //function : SetPosition
112 //purpose  : 
113 //=======================================================================
114
115 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
116 {
117         myPosition = aPos;
118 }
119
120 //=======================================================================
121 //function : GetPosition
122 //purpose  : 
123 //=======================================================================
124
125 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
126 {
127         return myPosition;
128 }
129
130 //=======================================================================
131 /*!
132  * \brief Iterator on list of elements
133  */
134 //=======================================================================
135
136 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
137 {
138 private:
139   SMDS_Mesh* myMesh;
140   vtkIdType* myCells;
141   int  myNcells;
142   SMDSAbs_ElementType                                 myType;
143   int  iter;
144
145  public:
146   SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh,
147                               vtkIdType* cells,
148                               int ncells,
149                               SMDSAbs_ElementType type):
150     myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
151   {
152      //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
153   }
154
155   bool more()
156   {
157       return (iter< myNcells);
158   }
159
160   const SMDS_MeshElement* next()
161   {
162       int vtkId = myCells[iter];
163       int smdsId = myMesh->fromVtkToSmds(vtkId);
164       const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
165       if (!elem)
166       {
167           assert(0);
168           throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
169       }
170       //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId << " " << (elem!=0));
171       iter++;
172       return elem;
173   }     
174 };
175
176 SMDS_ElemIteratorPtr SMDS_MeshNode::
177         GetInverseElementIterator(SMDSAbs_ElementType type) const
178 {
179     vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
180     //MESSAGE("myID " << myID << " ncells " << l.ncells);
181     return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
182 }
183
184 // Same as GetInverseElementIterator but the create iterator only return
185 // wanted type elements.
186 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
187 {
188 private:
189   SMDS_Mesh* myMesh;
190   vtkIdType* myCells;
191   int  myNcells;
192   SMDSAbs_ElementType                                 myType;
193   int  iter;
194   vector<SMDS_MeshElement*> myFiltCells;
195
196  public:
197   SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
198                            vtkIdType* cells,
199                            int ncells,
200                            SMDSAbs_ElementType type):
201     myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
202   {
203         //MESSAGE("myNcells " << myNcells);
204        for (; iter<ncells; iter++)
205         {
206            int vtkId = myCells[iter];
207            int smdsId = myMesh->fromVtkToSmds(vtkId);
208            //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId);
209            const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
210            if (elem->GetType() == type)
211                myFiltCells.push_back((SMDS_MeshElement*)elem);
212         }
213         myNcells = myFiltCells.size();
214         //MESSAGE("myNcells " << myNcells);
215        iter = 0;
216         //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
217   }
218
219   bool more()
220   {
221       return (iter< myNcells);
222   }
223
224   const SMDS_MeshElement* next()
225   {
226       const SMDS_MeshElement* elem = myFiltCells[iter];
227       iter++;
228       return elem;
229   }
230 };
231
232 SMDS_ElemIteratorPtr SMDS_MeshNode::
233         elementsIterator(SMDSAbs_ElementType type) const
234 {
235   if(type==SMDSAbs_Node)
236     return SMDS_MeshElement::elementsIterator(SMDSAbs_Node); 
237   else
238   {
239     vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
240     return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
241   }
242 }
243
244 int SMDS_MeshNode::NbNodes() const
245 {
246         return 1;
247 }
248
249
250 double* SMDS_MeshNode::getCoord() const
251 {
252   return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myID);
253 }
254
255 double SMDS_MeshNode::X() const
256 {
257   double *coord = getCoord();
258   return coord[0];
259 }
260
261 double SMDS_MeshNode::Y() const
262 {
263   double *coord = getCoord();
264   return coord[1];
265 }
266
267 double SMDS_MeshNode::Z() const
268 {
269   double *coord = getCoord();
270   return coord[2];
271 }
272
273 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
274 void SMDS_MeshNode::setXYZ(double x, double y, double z)
275 {
276   vtkPoints *points = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoints();
277   points->InsertPoint(myID, x, y, z);
278 }
279
280 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
281 {
282         return SMDSAbs_Node;
283 }
284
285 vtkIdType SMDS_MeshNode::GetVtkType() const
286 {
287   return VTK_VERTEX;
288 }
289
290 //=======================================================================
291 //function : AddInverseElement
292 //purpose  :
293 //=======================================================================
294 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
295 {
296     const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*>(ME);
297     assert(cell);
298     SMDS_Mesh::_meshList[myMeshId]->getGrid()->AddReferenceToCell(myID, cell->getVtkId());
299 }
300
301 //=======================================================================
302 //function : ClearInverseElements
303 //purpose  :
304 //=======================================================================
305 void SMDS_MeshNode::ClearInverseElements()
306 {
307   SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myID, 0);
308 }
309
310 bool SMDS_MeshNode::emptyInverseElements()
311 {
312   vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
313   return (l.ncells == 0);
314 }
315
316 //================================================================================
317 /*!
318  * \brief Count inverse elements of given type
319  */
320 //================================================================================
321
322 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
323 {
324   vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
325
326   if ( type == SMDSAbs_All )
327     return l.ncells;
328
329   int nb = 0;
330   SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
331   for (int i=0; i<l.ncells; i++)
332         {
333            const SMDS_MeshElement* elem = mesh->FindNode(l.cells[i]);
334            if (elem->GetType() == type)
335                 nb++;
336         }
337   return nb;
338 }
339
340 ///////////////////////////////////////////////////////////////////////////////
341 /// To be used with STL set
342 ///////////////////////////////////////////////////////////////////////////////
343 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
344 {
345         return e1.GetID()<e2.GetID();
346         /*if(e1.myX<e2.myX) return true;
347         else if(e1.myX==e2.myX)
348         {
349                 if(e1.myY<e2.myY) return true;
350                 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);
351                 else return false;
352         }
353         else return false;*/
354 }
355