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 grid->GetLinks()->ResizeForPoint( myVtkID );
73 SMDS_MeshNode::~SMDS_MeshNode()
76 if ( myPosition && myPosition != SMDS_SpacePosition::originSpacePosition() )
77 delete myPosition, myPosition = 0;
80 //=======================================================================
81 //function : RemoveInverseElement
83 //=======================================================================
85 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
87 //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
88 const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
90 if ( SMDS_Mesh::_meshList[myMeshId]->getGrid()->HasLinks() )
91 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myVtkID, cell->getVtkId());
94 //=======================================================================
97 //=======================================================================
99 void SMDS_MeshNode::Print(ostream & OS) const
101 OS << "Node <" << myID << "> : X = " << X() << " Y = "
102 << Y() << " Z = " << Z() << endl;
105 //=======================================================================
106 //function : SetPosition
108 //=======================================================================
110 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
113 myPosition != SMDS_SpacePosition::originSpacePosition() &&
119 //=======================================================================
120 //function : GetPosition
122 //=======================================================================
124 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
129 //=======================================================================
131 * \brief Iterator on list of elements
133 //=======================================================================
135 class SMDS_MeshNode_MyInvIterator: public SMDS_ElemIterator
141 SMDSAbs_ElementType myType;
143 vector<vtkIdType> cellList;
146 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh, vtkIdType* cells, int ncells, SMDSAbs_ElementType type) :
147 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
149 cellList.reserve( ncells );
150 if (type == SMDSAbs_All)
151 cellList.assign( cells, cells + ncells );
153 for (int i = 0; i < ncells; i++)
155 int vtkId = cells[i];
156 int smdsId = myMesh->fromVtkToSmds(vtkId);
157 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
158 if (elem->GetType() == type)
160 cellList.push_back(vtkId);
163 myCells = cellList.empty() ? 0 : &cellList[0];
164 myNcells = cellList.size();
169 return (iter < myNcells);
172 const SMDS_MeshElement* next()
174 int vtkId = myCells[iter];
175 int smdsId = myMesh->fromVtkToSmds(vtkId);
176 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
179 MESSAGE("SMDS_MeshNode_MyInvIterator problem Null element");
180 throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
187 SMDS_ElemIteratorPtr SMDS_MeshNode::GetInverseElementIterator(SMDSAbs_ElementType type) const
189 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetLinks()->GetLink(myVtkID);
190 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
193 // Same as GetInverseElementIterator but the create iterator only return
194 // wanted type elements.
195 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
201 SMDSAbs_ElementType myType;
203 vector<SMDS_MeshElement*> myFiltCells;
206 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
209 SMDSAbs_ElementType type):
210 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
212 //MESSAGE("myNcells " << myNcells);
213 for (; iter<ncells; iter++)
215 int vtkId = myCells[iter];
216 int smdsId = myMesh->fromVtkToSmds(vtkId);
217 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId);
218 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
219 if (elem->GetType() == type)
220 myFiltCells.push_back((SMDS_MeshElement*)elem);
222 myNcells = myFiltCells.size();
223 //MESSAGE("myNcells " << myNcells);
225 //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
230 return (iter< myNcells);
233 const SMDS_MeshElement* next()
235 const SMDS_MeshElement* elem = myFiltCells[iter];
241 SMDS_ElemIteratorPtr SMDS_MeshNode::
242 elementsIterator(SMDSAbs_ElementType type) const
244 if(type==SMDSAbs_Node)
245 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node);
248 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetLinks()->GetLink(myVtkID);
249 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
253 int SMDS_MeshNode::NbNodes() const
258 double* SMDS_MeshNode::getCoord() const
260 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myVtkID);
263 double SMDS_MeshNode::X() const
265 double *coord = getCoord();
269 double SMDS_MeshNode::Y() const
271 double *coord = getCoord();
275 double SMDS_MeshNode::Z() const
277 double *coord = getCoord();
281 //================================================================================
283 * \brief thread safe getting coords
285 void SMDS_MeshNode::GetXYZ(double xyz[3]) const
287 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myVtkID,xyz);
290 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
291 void SMDS_MeshNode::setXYZ(double x, double y, double z)
293 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
294 vtkPoints *points = mesh->getGrid()->GetPoints();
295 points->InsertPoint(myVtkID, x, y, z);
296 mesh->adjustBoundingBox(x, y, z);
297 mesh->setMyModified();
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_UnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
319 vtkCellLinks *Links = grid->GetLinks();
320 Links->ResizeCellList(myVtkID, 1);
321 Links->AddCellReference(cell->getVtkId(), myVtkID);
324 //=======================================================================
325 //function : ClearInverseElements
327 //=======================================================================
328 void SMDS_MeshNode::ClearInverseElements()
330 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myVtkID, 0);
333 bool SMDS_MeshNode::emptyInverseElements()
335 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetLinks()->GetLink(myVtkID);
336 return (l.ncells == 0);
339 //================================================================================
341 * \brief Count inverse elements of given type
343 //================================================================================
345 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
347 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetLinks()->GetLink(myVtkID);
349 if ( type == SMDSAbs_All )
353 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
354 for (int i=0; i<l.ncells; i++)
356 const SMDS_MeshElement* elem = mesh->FindElement(mesh->fromVtkToSmds(l.cells[i]));
357 if (elem->GetType() == type)
363 ///////////////////////////////////////////////////////////////////////////////
364 /// To be used with STL set
365 ///////////////////////////////////////////////////////////////////////////////
366 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
368 return e1.getVtkId()<e2.getVtkId();
369 /*if(e1.myX<e2.myX) return true;
370 else if(e1.myX==e2.myX)
372 if(e1.myY<e2.myY) return true;
373 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);