1 // Copyright (C) 2007-2010 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
23 // SMESH SMDS : implementaion of Salome mesh data structure
26 #pragma warning(disable:4786)
29 #include "SMDS_MeshNode.hxx"
30 #include "SMDS_SpacePosition.hxx"
31 #include "SMDS_IteratorOfElements.hxx"
32 #include "SMDS_Mesh.hxx"
33 #include <vtkUnstructuredGrid.h>
35 #define protected public
36 #include <vtkCellLinks.h>
37 #define protected protected
39 #include "utilities.h"
40 #include "Utils_SALOME_Exception.hxx"
45 int SMDS_MeshNode::nbNodes =0;
47 //=======================================================================
48 //function : SMDS_MeshNode
50 //=======================================================================
51 SMDS_MeshNode::SMDS_MeshNode() :
52 SMDS_MeshElement(-1, -1, -1),
53 myPosition(SMDS_SpacePosition::originSpacePosition())
58 SMDS_MeshNode::SMDS_MeshNode(int id, int meshId, int shapeId, double x, double y, double z):
59 SMDS_MeshElement(id, meshId, shapeId),
60 myPosition(SMDS_SpacePosition::originSpacePosition())
63 init(id, meshId, shapeId, x, y ,z);
66 void SMDS_MeshNode::init(int id, int meshId, int shapeId, double x, double y, double z)
72 //MESSAGE("Node " << myID << " (" << x << ", " << y << ", " << z << ")");
73 SMDS_Mesh* mesh = SMDS_Mesh::_meshList[myMeshId];
74 vtkUnstructuredGrid * grid = mesh->getGrid();
75 vtkPoints *points = grid->GetPoints();
76 points->InsertPoint(myID, x, y, z);
77 vtkCellLinks *cellLinks = grid->GetCellLinks();
78 if (myID >=cellLinks->Size)
79 cellLinks->Resize(myID+SMDS_Mesh::chunkSize);
82 SMDS_MeshNode::~SMDS_MeshNode()
87 //=======================================================================
88 //function : RemoveInverseElement
90 //=======================================================================
92 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
94 //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
95 const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
97 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myID, cell->getVtkId());
100 //=======================================================================
103 //=======================================================================
105 void SMDS_MeshNode::Print(ostream & OS) const
107 OS << "Node <" << myID << "> : X = " << X() << " Y = "
108 << Y() << " Z = " << Z() << endl;
111 //=======================================================================
112 //function : SetPosition
114 //=======================================================================
116 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
121 //=======================================================================
122 //function : GetPosition
124 //=======================================================================
126 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
131 //=======================================================================
133 * \brief Iterator on list of elements
135 //=======================================================================
137 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
143 SMDSAbs_ElementType myType;
147 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh,
150 SMDSAbs_ElementType type):
151 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
153 //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
158 return (iter< myNcells);
161 const SMDS_MeshElement* next()
163 int vtkId = myCells[iter];
164 int smdsId = myMesh->fromVtkToSmds(vtkId);
165 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
169 throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
171 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId << " " << (elem!=0));
177 SMDS_ElemIteratorPtr SMDS_MeshNode::
178 GetInverseElementIterator(SMDSAbs_ElementType type) const
180 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
181 //MESSAGE("myID " << myID << " ncells " << l.ncells);
182 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
185 // Same as GetInverseElementIterator but the create iterator only return
186 // wanted type elements.
187 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
193 SMDSAbs_ElementType myType;
195 vector<SMDS_MeshElement*> myFiltCells;
198 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
201 SMDSAbs_ElementType type):
202 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
204 //MESSAGE("myNcells " << myNcells);
205 for (; iter<ncells; iter++)
207 int vtkId = myCells[iter];
208 int smdsId = myMesh->fromVtkToSmds(vtkId);
209 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId);
210 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
211 if (elem->GetType() == type)
212 myFiltCells.push_back((SMDS_MeshElement*)elem);
214 myNcells = myFiltCells.size();
215 //MESSAGE("myNcells " << myNcells);
217 //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
222 return (iter< myNcells);
225 const SMDS_MeshElement* next()
227 const SMDS_MeshElement* elem = myFiltCells[iter];
233 SMDS_ElemIteratorPtr SMDS_MeshNode::
234 elementsIterator(SMDSAbs_ElementType type) const
236 if(type==SMDSAbs_Node)
237 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node);
240 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
241 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
245 int SMDS_MeshNode::NbNodes() const
251 double* SMDS_MeshNode::getCoord() const
253 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myID);
256 double SMDS_MeshNode::X() const
258 double *coord = getCoord();
262 double SMDS_MeshNode::Y() const
264 double *coord = getCoord();
268 double SMDS_MeshNode::Z() const
270 double *coord = getCoord();
274 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
275 void SMDS_MeshNode::setXYZ(double x, double y, double z)
277 vtkPoints *points = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoints();
278 points->InsertPoint(myID, x, y, z);
281 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
286 vtkIdType SMDS_MeshNode::GetVtkType() const
291 //=======================================================================
292 //function : AddInverseElement
294 //=======================================================================
295 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
297 const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*>(ME);
299 SMDS_Mesh::_meshList[myMeshId]->getGrid()->AddReferenceToCell(myID, cell->getVtkId());
302 //=======================================================================
303 //function : ClearInverseElements
305 //=======================================================================
306 void SMDS_MeshNode::ClearInverseElements()
308 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myID, 0);
311 bool SMDS_MeshNode::emptyInverseElements()
313 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
314 return (l.ncells == 0);
317 //================================================================================
319 * \brief Count inverse elements of given type
321 //================================================================================
323 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
325 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
327 if ( type == SMDSAbs_All )
331 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
332 for (int i=0; i<l.ncells; i++)
334 const SMDS_MeshElement* elem = mesh->FindNode(l.cells[i]);
335 if (elem->GetType() == type)
341 ///////////////////////////////////////////////////////////////////////////////
342 /// To be used with STL set
343 ///////////////////////////////////////////////////////////////////////////////
344 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
346 return e1.GetID()<e2.GetID();
347 /*if(e1.myX<e2.myX) return true;
348 else if(e1.myX==e2.myX)
350 if(e1.myY<e2.myY) return true;
351 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);