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"
42 int SMDS_MeshNode::nbNodes =0;
44 //=======================================================================
45 //function : SMDS_MeshNode
47 //=======================================================================
48 SMDS_MeshNode::SMDS_MeshNode() :
49 SMDS_MeshElement(-1, -1, -1),
50 myPosition(SMDS_SpacePosition::originSpacePosition())
54 SMDS_MeshNode::SMDS_MeshNode(int id, int meshId, int shapeId, double x, double y, double z):
55 SMDS_MeshElement(id, meshId, shapeId),
56 myPosition(SMDS_SpacePosition::originSpacePosition())
58 init(id, meshId, shapeId, x, y ,z);
61 void SMDS_MeshNode::init(int id, int meshId, int shapeId, double x, double y, double z)
68 //MESSAGE("Node " << myID << " (" << x << ", " << y << ", " << z << ")");
69 SMDS_Mesh* mesh = SMDS_Mesh::_meshList[myMeshId];
70 vtkUnstructuredGrid * grid = mesh->getGrid();
71 vtkPoints *points = grid->GetPoints();
72 //int nbp = points->GetNumberOfPoints();
73 points->InsertPoint(myID, x, y, z);
74 if (myID >= mesh->myCellLinksSize)
76 //points->SetNumberOfPoints(myID+SMDS_Mesh::chunkSize);
77 vtkCellLinks *cellLinks = grid->GetCellLinks();
79 // int imax = cellLinks->Size;
80 // for (int i =0; i<imax; i++)
82 // vtkCellLinks::Link &ilink = cellLinks->GetLink(i);
83 // int ncells = ilink.ncells;
84 // int *cells = ilink.cells;
85 // MESSAGE("NODE " << i << " " << cellLinks << " " << cells << " " << ncells);
86 // for (int j=0; j< ncells; j++)
87 // MESSAGE(" " << j << " " << cells[j]);
90 cellLinks->Resize(myID+SMDS_Mesh::chunkSize);
92 // cellLinks = grid->GetCellLinks();
93 // imax = cellLinks->Size;
94 // for (int i =0; i<imax; i++)
96 // vtkCellLinks::Link &ilink = cellLinks->GetLink(i);
97 // int ncells = ilink.ncells;
98 // int *cells = ilink.cells;
99 // MESSAGE("NODE " << i << " " << cellLinks << " " << cells << " " << ncells);
100 // for (int j=0; j< ncells; j++)
101 // MESSAGE(" " << j << " " << cells[j]);
104 mesh->myCellLinksSize = cellLinks->Size;
105 //MESSAGE(" -------------------------------------- resize CellLinks " << myID << " --> " << mesh->myCellLinksSize);
110 SMDS_MeshNode::~SMDS_MeshNode()
115 //=======================================================================
116 //function : RemoveInverseElement
118 //=======================================================================
120 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
122 //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
123 const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
125 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myID, cell->getVtkId());
128 //=======================================================================
131 //=======================================================================
133 void SMDS_MeshNode::Print(ostream & OS) const
135 OS << "Node <" << myID << "> : X = " << X() << " Y = "
136 << Y() << " Z = " << Z() << endl;
139 //=======================================================================
140 //function : SetPosition
142 //=======================================================================
144 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
149 //=======================================================================
150 //function : GetPosition
152 //=======================================================================
154 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
159 //=======================================================================
161 * \brief Iterator on list of elements
163 //=======================================================================
165 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
171 SMDSAbs_ElementType myType;
175 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh,
178 SMDSAbs_ElementType type):
179 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
181 //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
186 return (iter< myNcells);
189 const SMDS_MeshElement* next()
191 int vtkId = myCells[iter];
192 int smdsId = myMesh->fromVtkToSmds(vtkId);
193 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
199 SMDS_ElemIteratorPtr SMDS_MeshNode::
200 GetInverseElementIterator(SMDSAbs_ElementType type) const
202 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
203 //MESSAGE("ncells " << l.ncells);
204 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
207 // Same as GetInverseElementIterator but the create iterator only return
208 // wanted type elements.
209 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
215 SMDSAbs_ElementType myType;
217 vector<SMDS_MeshElement*> myFiltCells;
220 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
223 SMDSAbs_ElementType type):
224 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
226 for (; iter<ncells; iter++)
228 int vtkId = myCells[iter];
229 int smdsId = myMesh->fromVtkToSmds(vtkId);
230 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
231 if (elem->GetType() == type)
232 myFiltCells.push_back((SMDS_MeshElement*)elem);
234 myNcells = myFiltCells.size();
236 //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
241 return (iter< myNcells);
244 const SMDS_MeshElement* next()
246 const SMDS_MeshElement* elem = myFiltCells[iter];
252 SMDS_ElemIteratorPtr SMDS_MeshNode::
253 elementsIterator(SMDSAbs_ElementType type) const
255 if(type==SMDSAbs_Node)
256 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node);
259 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
260 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
264 int SMDS_MeshNode::NbNodes() const
270 double* SMDS_MeshNode::getCoord() const
272 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myID);
275 double SMDS_MeshNode::X() const
277 double *coord = getCoord();
281 double SMDS_MeshNode::Y() const
283 double *coord = getCoord();
287 double SMDS_MeshNode::Z() const
289 double *coord = getCoord();
293 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
294 void SMDS_MeshNode::setXYZ(double x, double y, double z)
296 vtkPoints *points = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoints();
297 points->InsertPoint(myID, x, y, z);
300 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
305 vtkIdType SMDS_MeshNode::GetVtkType() const
310 //=======================================================================
311 //function : AddInverseElement
313 //=======================================================================
314 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
316 const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*>(ME);
318 SMDS_Mesh::_meshList[myMeshId]->getGrid()->AddReferenceToCell(myID, cell->getVtkId());
321 //=======================================================================
322 //function : ClearInverseElements
324 //=======================================================================
325 void SMDS_MeshNode::ClearInverseElements()
327 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myID, 0);
330 bool SMDS_MeshNode::emptyInverseElements()
332 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
333 return (l.ncells == 0);
336 //================================================================================
338 * \brief Count inverse elements of given type
340 //================================================================================
342 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
344 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
346 if ( type == SMDSAbs_All )
350 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
351 for (int i=0; i<l.ncells; i++)
353 const SMDS_MeshElement* elem = mesh->FindNode(l.cells[i]);
354 if (elem->GetType() == type)
360 ///////////////////////////////////////////////////////////////////////////////
361 /// To be used with STL set
362 ///////////////////////////////////////////////////////////////////////////////
363 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
365 return e1.GetID()<e2.GetID();
366 /*if(e1.myX<e2.myX) return true;
367 else if(e1.myX==e2.myX)
369 if(e1.myY<e2.myY) return true;
370 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);