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