1 // Copyright (C) 2007-2012 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 cellLinks->ResizeForPoint( myVtkID );
77 SMDS_MeshNode::~SMDS_MeshNode()
80 if ( myPosition && myPosition != SMDS_SpacePosition::originSpacePosition() )
81 delete myPosition, myPosition = 0;
84 //=======================================================================
85 //function : RemoveInverseElement
87 //=======================================================================
89 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
91 //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
92 const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
94 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myVtkID, cell->getVtkId());
97 //=======================================================================
100 //=======================================================================
102 void SMDS_MeshNode::Print(ostream & OS) const
104 OS << "Node <" << myID << "> : X = " << X() << " Y = "
105 << Y() << " Z = " << Z() << endl;
108 //=======================================================================
109 //function : SetPosition
111 //=======================================================================
113 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
116 myPosition != SMDS_SpacePosition::originSpacePosition() &&
122 //=======================================================================
123 //function : GetPosition
125 //=======================================================================
127 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
132 //=======================================================================
134 * \brief Iterator on list of elements
136 //=======================================================================
138 class SMDS_MeshNode_MyInvIterator: public SMDS_ElemIterator
144 SMDSAbs_ElementType myType;
146 vector<vtkIdType> cellList;
149 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh, vtkIdType* cells, int ncells, SMDSAbs_ElementType type) :
150 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
152 //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
154 if (type == SMDSAbs_All)
155 for (int i = 0; i < ncells; i++)
156 cellList.push_back(cells[i]);
157 else for (int i = 0; i < ncells; i++)
159 int vtkId = cells[i];
160 int smdsId = myMesh->fromVtkToSmds(vtkId);
161 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
162 if (elem->GetType() == type)
164 //MESSAGE("Add element vtkId " << vtkId << " " << elem->GetType())
165 cellList.push_back(vtkId);
168 myCells = &cellList[0];
169 myNcells = cellList.size();
170 //MESSAGE("myNcells="<<myNcells);
175 //MESSAGE("iter " << iter << " ncells " << myNcells);
176 return (iter < myNcells);
179 const SMDS_MeshElement* next()
181 int vtkId = myCells[iter];
182 int smdsId = myMesh->fromVtkToSmds(vtkId);
183 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
186 MESSAGE("SMDS_MeshNode_MyInvIterator problem Null element");
187 throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
189 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId << " " << elem->GetType());
195 SMDS_ElemIteratorPtr SMDS_MeshNode::
196 GetInverseElementIterator(SMDSAbs_ElementType type) const
198 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
199 //MESSAGE("myID " << myID << " ncells " << l.ncells);
200 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
203 // Same as GetInverseElementIterator but the create iterator only return
204 // wanted type elements.
205 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
211 SMDSAbs_ElementType myType;
213 vector<SMDS_MeshElement*> myFiltCells;
216 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
219 SMDSAbs_ElementType type):
220 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
222 //MESSAGE("myNcells " << myNcells);
223 for (; iter<ncells; iter++)
225 int vtkId = myCells[iter];
226 int smdsId = myMesh->fromVtkToSmds(vtkId);
227 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId);
228 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
229 if (elem->GetType() == type)
230 myFiltCells.push_back((SMDS_MeshElement*)elem);
232 myNcells = myFiltCells.size();
233 //MESSAGE("myNcells " << myNcells);
235 //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
240 return (iter< myNcells);
243 const SMDS_MeshElement* next()
245 const SMDS_MeshElement* elem = myFiltCells[iter];
251 SMDS_ElemIteratorPtr SMDS_MeshNode::
252 elementsIterator(SMDSAbs_ElementType type) const
254 if(type==SMDSAbs_Node)
255 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node);
258 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
259 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
263 int SMDS_MeshNode::NbNodes() const
268 double* SMDS_MeshNode::getCoord() const
270 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myVtkID);
273 double SMDS_MeshNode::X() const
275 double *coord = getCoord();
279 double SMDS_MeshNode::Y() const
281 double *coord = getCoord();
285 double SMDS_MeshNode::Z() const
287 double *coord = getCoord();
291 //================================================================================
293 * \brief thread safe getting coords
295 void SMDS_MeshNode::GetXYZ(double xyz[3]) const
297 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myVtkID,xyz);
300 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
301 void SMDS_MeshNode::setXYZ(double x, double y, double z)
303 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
304 vtkPoints *points = mesh->getGrid()->GetPoints();
305 points->InsertPoint(myVtkID, x, y, z);
306 mesh->adjustBoundingBox(x, y, z);
307 mesh->setMyModified();
310 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
315 vtkIdType SMDS_MeshNode::GetVtkType() const
320 //=======================================================================
321 //function : AddInverseElement
323 //=======================================================================
324 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
326 const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*> (ME);
328 SMDS_UnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
329 vtkCellLinks *Links = grid->GetCellLinks();
330 Links->ResizeCellList(myVtkID, 1);
331 Links->AddCellReference(cell->getVtkId(), myVtkID);
334 //=======================================================================
335 //function : ClearInverseElements
337 //=======================================================================
338 void SMDS_MeshNode::ClearInverseElements()
340 SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myVtkID, 0);
343 bool SMDS_MeshNode::emptyInverseElements()
345 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
346 return (l.ncells == 0);
349 //================================================================================
351 * \brief Count inverse elements of given type
353 //================================================================================
355 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
357 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
359 if ( type == SMDSAbs_All )
363 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
364 for (int i=0; i<l.ncells; i++)
366 const SMDS_MeshElement* elem = mesh->FindElement(mesh->fromVtkToSmds(l.cells[i]));
367 if (elem->GetType() == type)
373 ///////////////////////////////////////////////////////////////////////////////
374 /// To be used with STL set
375 ///////////////////////////////////////////////////////////////////////////////
376 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
378 return e1.getVtkId()<e2.getVtkId();
379 /*if(e1.myX<e2.myX) return true;
380 else if(e1.myX==e2.myX)
382 if(e1.myY<e2.myY) return true;
383 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);