1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 SMDS_UnstructuredGrid * grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
68 vtkPoints *points = grid->GetPoints();
69 points->InsertPoint(myVtkID, x, y, z);
70 if ( grid->HasLinks() )
71 grid->GetLinks()->ResizeForPoint( myVtkID );
74 SMDS_MeshNode::~SMDS_MeshNode()
77 if ( myPosition && myPosition != SMDS_SpacePosition::originSpacePosition() )
78 delete myPosition, myPosition = 0;
81 //=======================================================================
82 //function : RemoveInverseElement
84 //=======================================================================
86 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * elem)
88 if ( SMDS_Mesh::_meshList[myMeshId]->getGrid()->HasLinks() )
89 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myVtkID, elem->getVtkId());
92 //=======================================================================
95 //=======================================================================
97 void SMDS_MeshNode::Print(ostream & OS) const
99 OS << "Node <" << myID << "> : X = " << X() << " Y = "
100 << Y() << " Z = " << Z() << endl;
103 //=======================================================================
104 //function : SetPosition
106 //=======================================================================
108 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
111 myPosition != SMDS_SpacePosition::originSpacePosition() &&
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)
149 cellList.reserve( ncells );
150 if (type == SMDSAbs_All)
152 cellList.assign( cells, cells + ncells );
156 for (int i = 0; i < ncells; i++)
158 int vtkId = cells[i];
159 int smdsId = myMesh->fromVtkToSmds(vtkId);
160 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
161 if (elem->GetType() == type)
163 cellList.push_back(vtkId);
167 myCells = cellList.empty() ? 0 : &cellList[0];
168 myNcells = cellList.size();
174 return (iter < myNcells);
177 const SMDS_MeshElement* next()
179 int vtkId = myCells[iter];
180 int smdsId = myMesh->fromVtkToSmds(vtkId);
181 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
184 MESSAGE("SMDS_MeshNode_MyInvIterator problem Null element");
185 throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
192 SMDS_ElemIteratorPtr SMDS_MeshNode::GetInverseElementIterator(SMDSAbs_ElementType type) const
194 if ( SMDS_Mesh::_meshList[myMeshId]->NbElements() > 0 ) // avoid building links
196 vtkCellLinks::Link& l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetLinks()->GetLink(myVtkID);
197 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
201 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], 0, 0, type));
205 SMDS_ElemIteratorPtr SMDS_MeshNode::elementsIterator(SMDSAbs_ElementType type) const
207 if ( type == SMDSAbs_Node )
208 return SMDS_MeshElement::elementsIterator( SMDSAbs_Node );
210 return GetInverseElementIterator( type );
213 int SMDS_MeshNode::NbNodes() const
218 double* SMDS_MeshNode::getCoord() const
220 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myVtkID);
223 double SMDS_MeshNode::X() const
225 double *coord = getCoord();
229 double SMDS_MeshNode::Y() const
231 double *coord = getCoord();
235 double SMDS_MeshNode::Z() const
237 double *coord = getCoord();
241 //================================================================================
243 * \brief thread safe getting coords
245 //================================================================================
247 void SMDS_MeshNode::GetXYZ(double xyz[3]) const
249 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myVtkID,xyz);
252 //================================================================================
253 void SMDS_MeshNode::setXYZ(double x, double y, double z)
255 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
256 vtkPoints *points = mesh->getGrid()->GetPoints();
257 points->InsertPoint(myVtkID, x, y, z);
258 mesh->adjustBoundingBox(x, y, z);
259 mesh->setMyModified();
262 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
267 vtkIdType SMDS_MeshNode::GetVtkType() const
272 //=======================================================================
273 //function : AddInverseElement
275 //=======================================================================
276 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
278 SMDS_UnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
279 if ( grid->HasLinks() )
281 vtkCellLinks *Links = grid->GetLinks();
282 Links->ResizeCellList(myVtkID, 1);
283 Links->AddCellReference(ME->getVtkId(), myVtkID);
287 //=======================================================================
288 //function : ClearInverseElements
290 //=======================================================================
291 void SMDS_MeshNode::ClearInverseElements()
293 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myVtkID, 0);
296 //================================================================================
298 * \brief Count inverse elements of given type
300 //================================================================================
302 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
305 if ( SMDS_Mesh::_meshList[myMeshId]->NbElements() > 0 ) // avoid building links
307 vtkCellLinks::Link& l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetLinks()->GetLink(myVtkID);
309 if ( type == SMDSAbs_All )
312 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
313 for ( int i = 0; i < l.ncells; i++ )
315 const SMDS_MeshElement* elem = mesh->FindElement( mesh->fromVtkToSmds( l.cells[i] ));
316 nb += ( elem->GetType() == type );