1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMDS : implementaion of Salome mesh data structure
25 #pragma warning(disable:4786)
28 #include "SMDS_MeshNode.hxx"
29 #include "SMDS_SpacePosition.hxx"
30 #include "SMDS_IteratorOfElements.hxx"
31 #include "SMDS_Mesh.hxx"
32 #include <vtkUnstructuredGrid.h>
34 #define protected public
35 #include <vtkCellLinks.h>
36 #define protected protected
38 #include "utilities.h"
39 #include "Utils_SALOME_Exception.hxx"
44 int SMDS_MeshNode::nbNodes =0;
46 //=======================================================================
47 //function : SMDS_MeshNode
49 //=======================================================================
50 SMDS_MeshNode::SMDS_MeshNode() :
51 SMDS_MeshElement(-1, -1, -1),
52 myPosition(SMDS_SpacePosition::originSpacePosition())
56 SMDS_MeshNode::SMDS_MeshNode(int id, int meshId, int shapeId, double x, double y, double z):
57 SMDS_MeshElement(id, meshId, shapeId),
58 myPosition(SMDS_SpacePosition::originSpacePosition())
60 init(id, meshId, shapeId, x, y ,z);
63 void SMDS_MeshNode::init(int id, int meshId, int shapeId, double x, double y, double z)
70 //MESSAGE("Node " << myID << " (" << x << ", " << y << ", " << z << ")");
71 SMDS_Mesh* mesh = SMDS_Mesh::_meshList[myMeshId];
72 vtkUnstructuredGrid * grid = mesh->getGrid();
73 vtkPoints *points = grid->GetPoints();
74 //int nbp = points->GetNumberOfPoints();
75 points->InsertPoint(myID, x, y, z);
76 if (myID >= mesh->myCellLinksSize)
78 //points->SetNumberOfPoints(myID+SMDS_Mesh::chunkSize);
79 vtkCellLinks *cellLinks = grid->GetCellLinks();
81 // int imax = cellLinks->Size;
82 // for (int i =0; i<imax; i++)
84 // vtkCellLinks::Link &ilink = cellLinks->GetLink(i);
85 // int ncells = ilink.ncells;
86 // int *cells = ilink.cells;
87 // MESSAGE("NODE " << i << " " << cellLinks << " " << cells << " " << ncells);
88 // for (int j=0; j< ncells; j++)
89 // MESSAGE(" " << j << " " << cells[j]);
92 cellLinks->Resize(myID+SMDS_Mesh::chunkSize);
94 // cellLinks = grid->GetCellLinks();
95 // imax = cellLinks->Size;
96 // for (int i =0; i<imax; i++)
98 // vtkCellLinks::Link &ilink = cellLinks->GetLink(i);
99 // int ncells = ilink.ncells;
100 // int *cells = ilink.cells;
101 // MESSAGE("NODE " << i << " " << cellLinks << " " << cells << " " << ncells);
102 // for (int j=0; j< ncells; j++)
103 // MESSAGE(" " << j << " " << cells[j]);
106 mesh->myCellLinksSize = cellLinks->Size;
107 //MESSAGE(" -------------------------------------- resize CellLinks " << myID << " --> " << mesh->myCellLinksSize);
112 SMDS_MeshNode::~SMDS_MeshNode()
117 //=======================================================================
118 //function : RemoveInverseElement
120 //=======================================================================
122 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
124 //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
125 const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
127 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myID, cell->getVtkId());
130 //=======================================================================
133 //=======================================================================
135 void SMDS_MeshNode::Print(ostream & OS) const
137 OS << "Node <" << myID << "> : X = " << X() << " Y = "
138 << Y() << " Z = " << Z() << endl;
141 //=======================================================================
142 //function : SetPosition
144 //=======================================================================
146 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
151 //=======================================================================
152 //function : GetPosition
154 //=======================================================================
156 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
161 //=======================================================================
163 * \brief Iterator on list of elements
165 //=======================================================================
167 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
173 SMDSAbs_ElementType myType;
177 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh,
180 SMDSAbs_ElementType type):
181 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
183 //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
188 return (iter< myNcells);
191 const SMDS_MeshElement* next()
193 int vtkId = myCells[iter];
194 int smdsId = myMesh->fromVtkToSmds(vtkId);
195 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
199 throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
201 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId << " " << (elem!=0));
207 SMDS_ElemIteratorPtr SMDS_MeshNode::
208 GetInverseElementIterator(SMDSAbs_ElementType type) const
210 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
211 //MESSAGE("myID " << myID << " ncells " << l.ncells);
212 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
215 // Same as GetInverseElementIterator but the create iterator only return
216 // wanted type elements.
217 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
223 SMDSAbs_ElementType myType;
225 vector<SMDS_MeshElement*> myFiltCells;
228 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
231 SMDSAbs_ElementType type):
232 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
234 //MESSAGE("myNcells " << myNcells);
235 for (; iter<ncells; iter++)
237 int vtkId = myCells[iter];
238 int smdsId = myMesh->fromVtkToSmds(vtkId);
239 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId);
240 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
241 if (elem->GetType() == type)
242 myFiltCells.push_back((SMDS_MeshElement*)elem);
244 myNcells = myFiltCells.size();
245 //MESSAGE("myNcells " << myNcells);
247 //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
252 return (iter< myNcells);
255 const SMDS_MeshElement* next()
257 const SMDS_MeshElement* elem = myFiltCells[iter];
263 SMDS_ElemIteratorPtr SMDS_MeshNode::
264 elementsIterator(SMDSAbs_ElementType type) const
266 if(type==SMDSAbs_Node)
267 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node);
270 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
271 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
275 int SMDS_MeshNode::NbNodes() const
281 double* SMDS_MeshNode::getCoord() const
283 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myID);
286 double SMDS_MeshNode::X() const
288 double *coord = getCoord();
292 double SMDS_MeshNode::Y() const
294 double *coord = getCoord();
298 double SMDS_MeshNode::Z() const
300 double *coord = getCoord();
304 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
305 void SMDS_MeshNode::setXYZ(double x, double y, double z)
307 vtkPoints *points = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoints();
308 points->InsertPoint(myID, x, y, z);
311 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
316 vtkIdType SMDS_MeshNode::GetVtkType() const
321 //=======================================================================
322 //function : AddInverseElement
324 //=======================================================================
325 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
327 const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*>(ME);
329 SMDS_Mesh::_meshList[myMeshId]->getGrid()->AddReferenceToCell(myID, cell->getVtkId());
332 //=======================================================================
333 //function : ClearInverseElements
335 //=======================================================================
336 void SMDS_MeshNode::ClearInverseElements()
338 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myID, 0);
341 bool SMDS_MeshNode::emptyInverseElements()
343 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
344 return (l.ncells == 0);
347 //================================================================================
349 * \brief Count inverse elements of given type
351 //================================================================================
353 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
355 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
357 if ( type == SMDSAbs_All )
361 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
362 for (int i=0; i<l.ncells; i++)
364 const SMDS_MeshElement* elem = mesh->FindNode(l.cells[i]);
365 if (elem->GetType() == type)
371 ///////////////////////////////////////////////////////////////////////////////
372 /// To be used with STL set
373 ///////////////////////////////////////////////////////////////////////////////
374 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
376 return e1.GetID()<e2.GetID();
377 /*if(e1.myX<e2.myX) return true;
378 else if(e1.myX==e2.myX)
380 if(e1.myY<e2.myY) return true;
381 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);