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())
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())
62 init(id, meshId, shapeId, x, y ,z);
65 void SMDS_MeshNode::init(int id, int meshId, int shapeId, double x, double y, double z)
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);
81 SMDS_MeshNode::~SMDS_MeshNode()
86 //=======================================================================
87 //function : RemoveInverseElement
89 //=======================================================================
91 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
93 //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
94 const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
96 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myID, cell->getVtkId());
99 //=======================================================================
102 //=======================================================================
104 void SMDS_MeshNode::Print(ostream & OS) const
106 OS << "Node <" << myID << "> : X = " << X() << " Y = "
107 << Y() << " Z = " << Z() << endl;
110 //=======================================================================
111 //function : SetPosition
113 //=======================================================================
115 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
120 //=======================================================================
121 //function : GetPosition
123 //=======================================================================
125 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
130 //=======================================================================
132 * \brief Iterator on list of elements
134 //=======================================================================
136 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
142 SMDSAbs_ElementType myType;
146 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh,
149 SMDSAbs_ElementType type):
150 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
152 //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
157 return (iter< myNcells);
160 const SMDS_MeshElement* next()
162 int vtkId = myCells[iter];
163 int smdsId = myMesh->fromVtkToSmds(vtkId);
164 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
168 throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
170 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId << " " << (elem!=0));
176 SMDS_ElemIteratorPtr SMDS_MeshNode::
177 GetInverseElementIterator(SMDSAbs_ElementType type) const
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));
184 // Same as GetInverseElementIterator but the create iterator only return
185 // wanted type elements.
186 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
192 SMDSAbs_ElementType myType;
194 vector<SMDS_MeshElement*> myFiltCells;
197 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
200 SMDSAbs_ElementType type):
201 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
203 //MESSAGE("myNcells " << myNcells);
204 for (; iter<ncells; iter++)
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);
213 myNcells = myFiltCells.size();
214 //MESSAGE("myNcells " << myNcells);
216 //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
221 return (iter< myNcells);
224 const SMDS_MeshElement* next()
226 const SMDS_MeshElement* elem = myFiltCells[iter];
232 SMDS_ElemIteratorPtr SMDS_MeshNode::
233 elementsIterator(SMDSAbs_ElementType type) const
235 if(type==SMDSAbs_Node)
236 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node);
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));
244 int SMDS_MeshNode::NbNodes() const
250 double* SMDS_MeshNode::getCoord() const
252 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myID);
255 double SMDS_MeshNode::X() const
257 double *coord = getCoord();
261 double SMDS_MeshNode::Y() const
263 double *coord = getCoord();
267 double SMDS_MeshNode::Z() const
269 double *coord = getCoord();
273 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
274 void SMDS_MeshNode::setXYZ(double x, double y, double z)
276 vtkPoints *points = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoints();
277 points->InsertPoint(myID, x, y, z);
280 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
285 vtkIdType SMDS_MeshNode::GetVtkType() const
290 //=======================================================================
291 //function : AddInverseElement
293 //=======================================================================
294 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
296 const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*>(ME);
298 SMDS_Mesh::_meshList[myMeshId]->getGrid()->AddReferenceToCell(myID, cell->getVtkId());
301 //=======================================================================
302 //function : ClearInverseElements
304 //=======================================================================
305 void SMDS_MeshNode::ClearInverseElements()
307 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myID, 0);
310 bool SMDS_MeshNode::emptyInverseElements()
312 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
313 return (l.ncells == 0);
316 //================================================================================
318 * \brief Count inverse elements of given type
320 //================================================================================
322 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
324 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
326 if ( type == SMDSAbs_All )
330 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
331 for (int i=0; i<l.ncells; i++)
333 const SMDS_MeshElement* elem = mesh->FindNode(l.cells[i]);
334 if (elem->GetType() == type)
340 ///////////////////////////////////////////////////////////////////////////////
341 /// To be used with STL set
342 ///////////////////////////////////////////////////////////////////////////////
343 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
345 return e1.GetID()<e2.GetID();
346 /*if(e1.myX<e2.myX) return true;
347 else if(e1.myX==e2.myX)
349 if(e1.myY<e2.myY) return true;
350 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);