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 #include "utilities.h"
36 #include "Utils_SALOME_Exception.hxx"
41 int SMDS_MeshNode::nbNodes =0;
43 //=======================================================================
44 //function : SMDS_MeshNode
46 //=======================================================================
47 SMDS_MeshNode::SMDS_MeshNode() :
48 SMDS_MeshElement(-1, -1, 0),
49 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())
59 init(id, meshId, shapeId, x, y ,z);
62 void SMDS_MeshNode::init(int id, int meshId, int shapeId, double x, double y, double z)
64 SMDS_MeshElement::init(id, meshId, shapeId);
67 //MESSAGE("Node " << myID << " " << myVtkID << " (" << x << ", " << y << ", " << z << ")");
68 SMDS_Mesh* mesh = SMDS_Mesh::_meshList[myMeshId];
69 SMDS_UnstructuredGrid * grid = mesh->getGrid();
70 vtkPoints *points = grid->GetPoints();
71 points->InsertPoint(myVtkID, x, y, z);
72 SMDS_CellLinks *cellLinks = dynamic_cast<SMDS_CellLinks*>(grid->GetCellLinks());
74 if (myVtkID >= cellLinks->GetLinksSize())
75 cellLinks->ResizeL(myVtkID+SMDS_Mesh::chunkSize);
78 SMDS_MeshNode::~SMDS_MeshNode()
83 //=======================================================================
84 //function : RemoveInverseElement
86 //=======================================================================
88 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
90 //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
91 const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
93 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myVtkID, cell->getVtkId());
96 //=======================================================================
99 //=======================================================================
101 void SMDS_MeshNode::Print(ostream & OS) const
103 OS << "Node <" << myID << "> : X = " << X() << " Y = "
104 << Y() << " Z = " << Z() << endl;
107 //=======================================================================
108 //function : SetPosition
110 //=======================================================================
112 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
117 //=======================================================================
118 //function : GetPosition
120 //=======================================================================
122 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
127 //=======================================================================
129 * \brief Iterator on list of elements
131 //=======================================================================
133 class SMDS_MeshNode_MyInvIterator: public SMDS_ElemIterator
139 SMDSAbs_ElementType myType;
141 vector<vtkIdType> cellList;
144 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh, vtkIdType* cells, int ncells, SMDSAbs_ElementType type) :
145 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
147 //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
149 if (type == SMDSAbs_All)
150 for (int i = 0; i < ncells; i++)
151 cellList.push_back(cells[i]);
152 else for (int i = 0; i < ncells; i++)
154 int vtkId = cells[i];
155 int smdsId = myMesh->fromVtkToSmds(vtkId);
156 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
157 if (elem->GetType() == type)
159 //MESSAGE("Add element vtkId " << vtkId << " " << elem->GetType())
160 cellList.push_back(vtkId);
163 myCells = &cellList[0];
164 myNcells = cellList.size();
165 //MESSAGE("myNcells="<<myNcells);
170 //MESSAGE("iter " << iter << " ncells " << myNcells);
171 return (iter < myNcells);
174 const SMDS_MeshElement* next()
176 int vtkId = myCells[iter];
177 int smdsId = myMesh->fromVtkToSmds(vtkId);
178 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
181 MESSAGE("SMDS_MeshNode_MyInvIterator problem Null element");
182 throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
184 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId << " " << elem->GetType());
190 SMDS_ElemIteratorPtr SMDS_MeshNode::
191 GetInverseElementIterator(SMDSAbs_ElementType type) const
193 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
194 //MESSAGE("myID " << myID << " ncells " << l.ncells);
195 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
198 // Same as GetInverseElementIterator but the create iterator only return
199 // wanted type elements.
200 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
206 SMDSAbs_ElementType myType;
208 vector<SMDS_MeshElement*> myFiltCells;
211 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
214 SMDSAbs_ElementType type):
215 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
217 //MESSAGE("myNcells " << myNcells);
218 for (; iter<ncells; iter++)
220 int vtkId = myCells[iter];
221 int smdsId = myMesh->fromVtkToSmds(vtkId);
222 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId);
223 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
224 if (elem->GetType() == type)
225 myFiltCells.push_back((SMDS_MeshElement*)elem);
227 myNcells = myFiltCells.size();
228 //MESSAGE("myNcells " << myNcells);
230 //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
235 return (iter< myNcells);
238 const SMDS_MeshElement* next()
240 const SMDS_MeshElement* elem = myFiltCells[iter];
246 SMDS_ElemIteratorPtr SMDS_MeshNode::
247 elementsIterator(SMDSAbs_ElementType type) const
249 if(type==SMDSAbs_Node)
250 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node);
253 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
254 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
258 int SMDS_MeshNode::NbNodes() const
263 double* SMDS_MeshNode::getCoord() const
265 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myVtkID);
268 double SMDS_MeshNode::X() const
270 double *coord = getCoord();
274 double SMDS_MeshNode::Y() const
276 double *coord = getCoord();
280 double SMDS_MeshNode::Z() const
282 double *coord = getCoord();
286 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
287 void SMDS_MeshNode::setXYZ(double x, double y, double z)
289 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
290 vtkPoints *points = mesh->getGrid()->GetPoints();
291 points->InsertPoint(myVtkID, x, y, z);
292 mesh->adjustBoundingBox(x, y, z);
293 mesh->setMyModified();
296 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
301 vtkIdType SMDS_MeshNode::GetVtkType() const
306 //=======================================================================
307 //function : AddInverseElement
309 //=======================================================================
310 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
312 const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*> (ME);
314 SMDS_UnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
315 vtkCellLinks *Links = grid->GetCellLinks();
316 Links->ResizeCellList(myVtkID, 1);
317 Links->AddCellReference(cell->getVtkId(), myVtkID);
320 //=======================================================================
321 //function : ClearInverseElements
323 //=======================================================================
324 void SMDS_MeshNode::ClearInverseElements()
326 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myVtkID, 0);
329 bool SMDS_MeshNode::emptyInverseElements()
331 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
332 return (l.ncells == 0);
335 //================================================================================
337 * \brief Count inverse elements of given type
339 //================================================================================
341 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
343 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
345 if ( type == SMDSAbs_All )
349 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
350 for (int i=0; i<l.ncells; i++)
352 const SMDS_MeshElement* elem = mesh->FindElement(mesh->fromVtkToSmds(l.cells[i]));
353 if (elem->GetType() == type)
359 ///////////////////////////////////////////////////////////////////////////////
360 /// To be used with STL set
361 ///////////////////////////////////////////////////////////////////////////////
362 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
364 return e1.getVtkId()<e2.getVtkId();
365 /*if(e1.myX<e2.myX) return true;
366 else if(e1.myX==e2.myX)
368 if(e1.myY<e2.myY) return true;
369 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);