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)
67 //MESSAGE("Node " << myID << " (" << x << ", " << y << ", " << z << ")");
68 SMDS_Mesh* mesh = SMDS_Mesh::_meshList[myMeshId];
69 vtkUnstructuredGrid * grid = mesh->getGrid();
70 vtkPoints *points = grid->GetPoints();
71 //int nbp = points->GetNumberOfPoints();
72 points->InsertPoint(myID, x, y, z);
73 if (myID >= mesh->myCellLinksSize)
75 //points->SetNumberOfPoints(myID+SMDS_Mesh::chunkSize);
76 vtkCellLinks *cellLinks = grid->GetCellLinks();
78 // int imax = cellLinks->Size;
79 // for (int i =0; i<imax; i++)
81 // vtkCellLinks::Link &ilink = cellLinks->GetLink(i);
82 // int ncells = ilink.ncells;
83 // int *cells = ilink.cells;
84 // MESSAGE("NODE " << i << " " << cellLinks << " " << cells << " " << ncells);
85 // for (int j=0; j< ncells; j++)
86 // MESSAGE(" " << j << " " << cells[j]);
89 cellLinks->Resize(myID+SMDS_Mesh::chunkSize);
91 // cellLinks = grid->GetCellLinks();
92 // imax = cellLinks->Size;
93 // for (int i =0; i<imax; i++)
95 // vtkCellLinks::Link &ilink = cellLinks->GetLink(i);
96 // int ncells = ilink.ncells;
97 // int *cells = ilink.cells;
98 // MESSAGE("NODE " << i << " " << cellLinks << " " << cells << " " << ncells);
99 // for (int j=0; j< ncells; j++)
100 // MESSAGE(" " << j << " " << cells[j]);
103 mesh->myCellLinksSize = cellLinks->Size;
104 //MESSAGE(" -------------------------------------- resize CellLinks " << myID << " --> " << mesh->myCellLinksSize);
109 SMDS_MeshNode::~SMDS_MeshNode()
114 //=======================================================================
115 //function : RemoveInverseElement
117 //=======================================================================
119 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
121 MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
122 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myID, parent->GetID()); // -PR- GetVtkID ?
125 //=======================================================================
128 //=======================================================================
130 void SMDS_MeshNode::Print(ostream & OS) const
132 OS << "Node <" << myID << "> : X = " << X() << " Y = "
133 << Y() << " Z = " << Z() << endl;
136 //=======================================================================
137 //function : SetPosition
139 //=======================================================================
141 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
146 //=======================================================================
147 //function : GetPosition
149 //=======================================================================
151 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
156 //=======================================================================
158 * \brief Iterator on list of elements
160 //=======================================================================
162 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
168 SMDSAbs_ElementType myType;
172 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh,
175 SMDSAbs_ElementType type):
176 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
178 //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
183 return (iter< myNcells);
186 const SMDS_MeshElement* next()
188 int vtkId = myCells[iter];
189 int smdsId = myMesh->fromVtkToSmds(vtkId);
190 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
196 SMDS_ElemIteratorPtr SMDS_MeshNode::
197 GetInverseElementIterator(SMDSAbs_ElementType type) const
199 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
200 //MESSAGE("ncells " << l.ncells);
201 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
204 // Same as GetInverseElementIterator but the create iterator only return
205 // wanted type elements.
206 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
212 SMDSAbs_ElementType myType;
214 vector<SMDS_MeshElement*> myFiltCells;
217 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
220 SMDSAbs_ElementType type):
221 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
223 for (; iter<ncells; iter++)
225 int vtkId = myCells[iter];
226 int smdsId = myMesh->fromVtkToSmds(vtkId);
227 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
228 if (elem->GetType() == type)
229 myFiltCells.push_back((SMDS_MeshElement*)elem);
231 myNcells = myFiltCells.size();
233 //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
238 return (iter< myNcells);
241 const SMDS_MeshElement* next()
243 const SMDS_MeshElement* elem = myFiltCells[iter];
249 SMDS_ElemIteratorPtr SMDS_MeshNode::
250 elementsIterator(SMDSAbs_ElementType type) const
252 if(type==SMDSAbs_Node)
253 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node);
256 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
257 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
261 int SMDS_MeshNode::NbNodes() const
267 double* SMDS_MeshNode::getCoord() const
269 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myID);
272 double SMDS_MeshNode::X() const
274 double *coord = getCoord();
278 double SMDS_MeshNode::Y() const
280 double *coord = getCoord();
284 double SMDS_MeshNode::Z() const
286 double *coord = getCoord();
290 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
291 void SMDS_MeshNode::setXYZ(double x, double y, double z)
293 vtkPoints *points = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoints();
294 points->InsertPoint(myID, x, y, z);
297 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
302 //=======================================================================
303 //function : AddInverseElement
305 //=======================================================================
306 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
308 const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*>(ME);
310 SMDS_Mesh::_meshList[myMeshId]->getGrid()->AddReferenceToCell(myID, cell->getVtkId());
313 //=======================================================================
314 //function : ClearInverseElements
316 //=======================================================================
317 void SMDS_MeshNode::ClearInverseElements()
319 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myID, 0);
322 bool SMDS_MeshNode::emptyInverseElements()
324 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
325 return (l.ncells == 0);
328 //================================================================================
330 * \brief Count inverse elements of given type
332 //================================================================================
334 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
336 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myID);
338 if ( type == SMDSAbs_All )
342 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
343 for (int i=0; i<l.ncells; i++)
345 const SMDS_MeshElement* elem = mesh->FindNode(l.cells[i]);
346 if (elem->GetType() == type)
352 ///////////////////////////////////////////////////////////////////////////////
353 /// To be used with STL set
354 ///////////////////////////////////////////////////////////////////////////////
355 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
357 return e1.GetID()<e2.GetID();
358 /*if(e1.myX<e2.myX) return true;
359 else if(e1.myX==e2.myX)
361 if(e1.myY<e2.myY) return true;
362 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);