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 //=======================================================================
43 //function : SMDS_MeshNode
45 //=======================================================================
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())
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)
59 //points->SetNumberOfPoints(myID+SMDS_Mesh::chunkSize);
60 vtkCellLinks *cellLinks = grid->GetCellLinks();
62 // int imax = cellLinks->Size;
63 // for (int i =0; i<imax; i++)
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]);
73 cellLinks->Resize(myID+SMDS_Mesh::chunkSize);
75 // cellLinks = grid->GetCellLinks();
76 // imax = cellLinks->Size;
77 // for (int i =0; i<imax; i++)
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]);
87 mesh->myCellLinksSize = cellLinks->Size;
88 //MESSAGE(" -------------------------------------- resize CellLinks " << myID << " --> " << mesh->myCellLinksSize);
93 //=======================================================================
94 //function : RemoveInverseElement
96 //=======================================================================
98 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
100 MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
101 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myID, parent->GetID()); // -PR- GetVtkID ?
104 //=======================================================================
107 //=======================================================================
109 void SMDS_MeshNode::Print(ostream & OS) const
111 OS << "Node <" << myID << "> : X = " << X() << " Y = "
112 << Y() << " Z = " << Z() << endl;
115 //=======================================================================
116 //function : SetPosition
118 //=======================================================================
120 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
125 //=======================================================================
126 //function : GetPosition
128 //=======================================================================
130 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
135 //=======================================================================
137 * \brief Iterator on list of elements
139 //=======================================================================
141 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
147 SMDSAbs_ElementType myType;
151 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh,
154 SMDSAbs_ElementType type):
155 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
157 //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
162 return (iter< myNcells);
165 const SMDS_MeshElement* next()
167 int vtkId = myCells[iter];
168 int smdsId = myMesh->fromVtkToSmds(vtkId);
169 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
175 SMDS_ElemIteratorPtr SMDS_MeshNode::
176 GetInverseElementIterator(SMDSAbs_ElementType type) const
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));
183 // Same as GetInverseElementIterator but the create iterator only return
184 // wanted type elements.
185 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
191 SMDSAbs_ElementType myType;
193 vector<SMDS_MeshElement*> myFiltCells;
196 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
199 SMDSAbs_ElementType type):
200 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
202 for (; iter<ncells; iter++)
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);
210 myNcells = myFiltCells.size();
212 //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
217 return (iter< myNcells);
220 const SMDS_MeshElement* next()
222 const SMDS_MeshElement* elem = myFiltCells[iter];
228 SMDS_ElemIteratorPtr SMDS_MeshNode::
229 elementsIterator(SMDSAbs_ElementType type) const
231 if(type==SMDSAbs_Node)
232 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node);
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));
240 int SMDS_MeshNode::NbNodes() const
246 double* SMDS_MeshNode::getCoord() const
248 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myID);
251 double SMDS_MeshNode::X() const
253 double *coord = getCoord();
257 double SMDS_MeshNode::Y() const
259 double *coord = getCoord();
263 double SMDS_MeshNode::Z() const
265 double *coord = getCoord();
269 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
270 void SMDS_MeshNode::setXYZ(double x, double y, double z)
272 vtkPoints *points = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoints();
273 points->InsertPoint(myID, x, y, z);
276 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
281 //=======================================================================
282 //function : AddInverseElement
284 //=======================================================================
285 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
287 const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*>(ME);
289 SMDS_Mesh::_meshList[myMeshId]->getGrid()->AddReferenceToCell(myID, cell->getVtkId());
292 //=======================================================================
293 //function : ClearInverseElements
295 //=======================================================================
296 void SMDS_MeshNode::ClearInverseElements()
298 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myID, 0);
301 bool SMDS_MeshNode::emptyInverseElements()
303 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
304 return (l.ncells == 0);
307 //================================================================================
309 * \brief Count inverse elements of given type
311 //================================================================================
313 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
315 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
317 if ( type == SMDSAbs_All )
321 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
322 for (int i=0; i<l.ncells; i++)
324 const SMDS_MeshElement* elem = mesh->FindNode(l.cells[i]);
325 if (elem->GetType() == type)
331 ///////////////////////////////////////////////////////////////////////////////
332 /// To be used with STL set
333 ///////////////////////////////////////////////////////////////////////////////
334 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
336 return e1.GetID()<e2.GetID();
337 /*if(e1.myX<e2.myX) return true;
338 else if(e1.myX==e2.myX)
340 if(e1.myY<e2.myY) return true;
341 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);