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)
70 //MESSAGE("Node " << myID << " " << myVtkID << " (" << x << ", " << y << ", " << z << ")");
71 SMDS_Mesh* mesh = SMDS_Mesh::_meshList[myMeshId];
72 SMDS_UnstructuredGrid * grid = mesh->getGrid();
73 vtkPoints *points = grid->GetPoints();
74 points->InsertPoint(myVtkID, x, y, z);
75 SMDS_CellLinks *cellLinks = dynamic_cast<SMDS_CellLinks*>(grid->GetCellLinks());
77 if (myVtkID >= cellLinks->GetLinksSize())
78 cellLinks->ResizeL(myVtkID+SMDS_Mesh::chunkSize);
81 SMDS_MeshNode::~SMDS_MeshNode()
86 //=======================================================================
87 //function : RemoveInverseElement
89 //=======================================================================
91 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
93 //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
94 const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
96 SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myVtkID, cell->getVtkId());
99 //=======================================================================
102 //=======================================================================
104 void SMDS_MeshNode::Print(ostream & OS) const
106 OS << "Node <" << myID << "> : X = " << X() << " Y = "
107 << Y() << " Z = " << Z() << endl;
110 //=======================================================================
111 //function : SetPosition
113 //=======================================================================
115 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
120 //=======================================================================
121 //function : GetPosition
123 //=======================================================================
125 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
130 //=======================================================================
132 * \brief Iterator on list of elements
134 //=======================================================================
136 class SMDS_MeshNode_MyInvIterator: public SMDS_ElemIterator
142 SMDSAbs_ElementType myType;
144 vector<vtkIdType> cellList;
147 SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh, vtkIdType* cells, int ncells, SMDSAbs_ElementType type) :
148 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
150 //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
152 if (type == SMDSAbs_All)
153 for (int i = 0; i < ncells; i++)
154 cellList.push_back(cells[i]);
155 else for (int i = 0; i < ncells; i++)
157 int vtkId = cells[i];
158 int smdsId = myMesh->fromVtkToSmds(vtkId);
159 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
160 if (elem->GetType() == type)
162 //MESSAGE("Add element vtkId " << vtkId << " " << elem->GetType())
163 cellList.push_back(vtkId);
166 myCells = &cellList[0];
167 myNcells = cellList.size();
168 //MESSAGE("myNcells="<<myNcells);
173 //MESSAGE("iter " << iter << " ncells " << myNcells);
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");
187 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId << " " << elem->GetType());
193 SMDS_ElemIteratorPtr SMDS_MeshNode::
194 GetInverseElementIterator(SMDSAbs_ElementType type) const
196 vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
197 //MESSAGE("myID " << myID << " ncells " << l.ncells);
198 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
201 // Same as GetInverseElementIterator but the create iterator only return
202 // wanted type elements.
203 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
209 SMDSAbs_ElementType myType;
211 vector<SMDS_MeshElement*> myFiltCells;
214 SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
217 SMDSAbs_ElementType type):
218 myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
220 //MESSAGE("myNcells " << myNcells);
221 for (; iter<ncells; iter++)
223 int vtkId = myCells[iter];
224 int smdsId = myMesh->fromVtkToSmds(vtkId);
225 //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId);
226 const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
227 if (elem->GetType() == type)
228 myFiltCells.push_back((SMDS_MeshElement*)elem);
230 myNcells = myFiltCells.size();
231 //MESSAGE("myNcells " << myNcells);
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(myVtkID);
257 return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
261 int SMDS_MeshNode::NbNodes() const
266 double* SMDS_MeshNode::getCoord() const
268 return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myVtkID);
271 double SMDS_MeshNode::X() const
273 double *coord = getCoord();
277 double SMDS_MeshNode::Y() const
279 double *coord = getCoord();
283 double SMDS_MeshNode::Z() const
285 double *coord = getCoord();
289 //* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
290 void SMDS_MeshNode::setXYZ(double x, double y, double z)
292 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
293 vtkPoints *points = mesh->getGrid()->GetPoints();
294 points->InsertPoint(myVtkID, x, y, z);
295 mesh->adjustBoundingBox(x, y, z);
296 mesh->setMyModified();
299 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
304 vtkIdType SMDS_MeshNode::GetVtkType() const
309 //=======================================================================
310 //function : AddInverseElement
312 //=======================================================================
313 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
315 const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*>(ME);
317 SMDS_Mesh::_meshList[myMeshId]->getGrid()->AddReferenceToCell(myVtkID, cell->getVtkId());
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);