Salome HOME
Merge branch 'V8_4_BR'
[modules/smesh.git] / src / SMDS / SMDS_MeshNode.cxx
index 80bbf81c84a68f50461f0c76fe506357d3ce0d18..554f0fa7115dd75e0e8e021c97be94f90a051694 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,7 +20,7 @@
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
-//  SMESH SMDS : implementaion of Salome mesh data structure
+//  SMESH SMDS : implementation of Salome mesh data structure
 //
 #ifdef _MSC_VER
 #pragma warning(disable:4786)
@@ -42,7 +42,7 @@ int SMDS_MeshNode::nbNodes =0;
 
 //=======================================================================
 //function : SMDS_MeshNode
-//purpose  : 
+//purpose  :
 //=======================================================================
 SMDS_MeshNode::SMDS_MeshNode() :
   SMDS_MeshElement(-1, -1, 0),
@@ -62,16 +62,13 @@ SMDS_MeshNode::SMDS_MeshNode(int id, int meshId, int shapeId, double x, double y
 void SMDS_MeshNode::init(int id, int meshId, int shapeId, double x, double y, double z)
 {
   SMDS_MeshElement::init(id, meshId, shapeId);
-  myVtkID = id -1;
+  myVtkID = id - 1;
   assert(myVtkID >= 0);
-  //MESSAGE("Node " << myID << " " << myVtkID << " (" << x << ", " << y << ", " << z << ")");
-  SMDS_Mesh* mesh = SMDS_Mesh::_meshList[myMeshId];
-  SMDS_UnstructuredGrid * grid = mesh->getGrid();
+  SMDS_UnstructuredGrid * grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
   vtkPoints *points = grid->GetPoints();
   points->InsertPoint(myVtkID, x, y, z);
-  SMDS_CellLinks *cellLinks = dynamic_cast<SMDS_CellLinks*>(grid->GetCellLinks());
-  assert(cellLinks);
-  cellLinks->ResizeForPoint( myVtkID );
+  if ( grid->HasLinks() )
+    grid->GetLinks()->ResizeForPoint( myVtkID );
 }
 
 SMDS_MeshNode::~SMDS_MeshNode()
@@ -83,31 +80,29 @@ SMDS_MeshNode::~SMDS_MeshNode()
 
 //=======================================================================
 //function : RemoveInverseElement
-//purpose  : 
+//purpose  :
 //=======================================================================
 
-void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
+void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * elem)
 {
-    //MESSAGE("RemoveInverseElement " << myID << " " << parent->GetID());
-    const SMDS_MeshCell* cell = dynamic_cast<const SMDS_MeshCell*>(parent);
-    MYASSERT(cell);
-    SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myVtkID, cell->getVtkId());
+  if ( SMDS_Mesh::_meshList[myMeshId]->getGrid()->HasLinks() )
+    SMDS_Mesh::_meshList[myMeshId]->getGrid()->RemoveReferenceToCell(myVtkID, elem->getVtkId());
 }
 
 //=======================================================================
 //function : Print
-//purpose  : 
+//purpose  :
 //=======================================================================
 
 void SMDS_MeshNode::Print(ostream & OS) const
 {
-        OS << "Node <" << myID << "> : X = " << X() << " Y = "
-                << Y() << " Z = " << Z() << endl;
+  OS << "Node <" << myID << "> : X = " << X() << " Y = "
+     << Y() << " Z = " << Z() << endl;
 }
 
 //=======================================================================
 //function : SetPosition
-//purpose  : 
+//purpose  :
 //=======================================================================
 
 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
@@ -121,12 +116,12 @@ void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
 
 //=======================================================================
 //function : GetPosition
-//purpose  : 
+//purpose  :
 //=======================================================================
 
 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
 {
-        return myPosition;
+  return myPosition;
 }
 
 //=======================================================================
@@ -149,30 +144,33 @@ public:
   SMDS_MeshNode_MyInvIterator(SMDS_Mesh *mesh, vtkIdType* cells, int ncells, SMDSAbs_ElementType type) :
     myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
   {
-    //MESSAGE("SMDS_MeshNode_MyInvIterator : ncells " << myNcells);
-    cellList.clear();
-    if (type == SMDSAbs_All)
-      for (int i = 0; i < ncells; i++)
-        cellList.push_back(cells[i]);
-    else for (int i = 0; i < ncells; i++)
+    if ( ncells )
+    {
+      cellList.reserve( ncells );
+      if (type == SMDSAbs_All)
+      {
+        cellList.assign( cells, cells + ncells );
+      }
+      else
       {
-        int vtkId = cells[i];
-        int smdsId = myMesh->fromVtkToSmds(vtkId);
-        const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
-        if (elem->GetType() == type)
+        for (int i = 0; i < ncells; i++)
+        {
+          int vtkId = cells[i];
+          int smdsId = myMesh->fromVtkToSmds(vtkId);
+          const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
+          if (elem->GetType() == type)
           {
-            //MESSAGE("Add element vtkId " << vtkId << " " << elem->GetType())
             cellList.push_back(vtkId);
           }
+        }
       }
-    myCells = &cellList[0];
-    myNcells = cellList.size();
-    //MESSAGE("myNcells="<<myNcells);
+      myCells = cellList.empty() ? 0 : &cellList[0];
+      myNcells = cellList.size();
+    }
   }
 
   bool more()
   {
-    //MESSAGE("iter " << iter << " ncells " << myNcells);
     return (iter < myNcells);
   }
 
@@ -182,87 +180,39 @@ public:
     int smdsId = myMesh->fromVtkToSmds(vtkId);
     const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
     if (!elem)
-      {
-        MESSAGE("SMDS_MeshNode_MyInvIterator problem Null element");
-        throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
-      }
-    //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId << " " << elem->GetType());
+    {
+      MESSAGE("SMDS_MeshNode_MyInvIterator problem Null element");
+      throw SALOME_Exception("SMDS_MeshNode_MyInvIterator problem Null element");
+    }
     iter++;
     return elem;
   }
 };
 
-SMDS_ElemIteratorPtr SMDS_MeshNode::
-        GetInverseElementIterator(SMDSAbs_ElementType type) const
+SMDS_ElemIteratorPtr SMDS_MeshNode::GetInverseElementIterator(SMDSAbs_ElementType type) const
 {
-    vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
-    //MESSAGE("myID " << myID << " ncells " << l.ncells);
-    return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
-}
-
-// Same as GetInverseElementIterator but the create iterator only return
-// wanted type elements.
-class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
-{
-private:
-  SMDS_Mesh* myMesh;
-  vtkIdType* myCells;
-  int  myNcells;
-  SMDSAbs_ElementType                                 myType;
-  int  iter;
-  vector<SMDS_MeshElement*> myFiltCells;
-
- public:
-  SMDS_MeshNode_MyIterator(SMDS_Mesh *mesh,
-                           vtkIdType* cells,
-                           int ncells,
-                           SMDSAbs_ElementType type):
-    myMesh(mesh), myCells(cells), myNcells(ncells), myType(type), iter(0)
+  if ( SMDS_Mesh::_meshList[myMeshId]->NbElements() > 0 ) // avoid building links
   {
-        //MESSAGE("myNcells " << myNcells);
-       for (; iter<ncells; iter++)
-        {
-           int vtkId = myCells[iter];
-           int smdsId = myMesh->fromVtkToSmds(vtkId);
-           //MESSAGE("vtkId " << vtkId << " smdsId " << smdsId);
-           const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
-           if (elem->GetType() == type)
-               myFiltCells.push_back((SMDS_MeshElement*)elem);
-        }
-        myNcells = myFiltCells.size();
-        //MESSAGE("myNcells " << myNcells);
-       iter = 0;
-        //MESSAGE("SMDS_MeshNode_MyIterator (filter) " << ncells << " " << myNcells);
-  }
-
-  bool more()
-  {
-      return (iter< myNcells);
+    vtkCellLinks::Link& l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetLinks()->GetLink(myVtkID);
+    return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
   }
-
-  const SMDS_MeshElement* next()
+  else
   {
-      const SMDS_MeshElement* elem = myFiltCells[iter];
-      iter++;
-      return elem;
+    return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(SMDS_Mesh::_meshList[myMeshId], 0, 0, type));
   }
-};
+}
 
-SMDS_ElemIteratorPtr SMDS_MeshNode::
-        elementsIterator(SMDSAbs_ElementType type) const
+SMDS_ElemIteratorPtr SMDS_MeshNode::elementsIterator(SMDSAbs_ElementType type) const
 {
-  if(type==SMDSAbs_Node)
-    return SMDS_MeshElement::elementsIterator(SMDSAbs_Node); 
+  if ( type == SMDSAbs_Node )
+    return SMDS_MeshElement::elementsIterator( SMDSAbs_Node );
   else
-  {
-    vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
-    return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(SMDS_Mesh::_meshList[myMeshId], l.cells, l.ncells, type));
-  }
+    return GetInverseElementIterator( type );
 }
 
 int SMDS_MeshNode::NbNodes() const
 {
-        return 1;
+  return 1;
 }
 
 double* SMDS_MeshNode::getCoord() const
@@ -292,12 +242,14 @@ double SMDS_MeshNode::Z() const
 /*!
  * \brief thread safe getting coords
  */
+//================================================================================
+
 void SMDS_MeshNode::GetXYZ(double xyz[3]) const
 {
   return SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetPoint(myVtkID,xyz);
 }
 
-//* resize the vtkPoints structure every SMDS_Mesh::chunkSize points
+//================================================================================
 void SMDS_MeshNode::setXYZ(double x, double y, double z)
 {
   SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
@@ -309,7 +261,7 @@ void SMDS_MeshNode::setXYZ(double x, double y, double z)
 
 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
 {
-        return SMDSAbs_Node;
+  return SMDSAbs_Node;
 }
 
 vtkIdType SMDS_MeshNode::GetVtkType() const
@@ -323,12 +275,13 @@ vtkIdType SMDS_MeshNode::GetVtkType() const
 //=======================================================================
 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
 {
-  const SMDS_MeshCell *cell = dynamic_cast<const SMDS_MeshCell*> (ME);
-  assert(cell);
   SMDS_UnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
-  vtkCellLinks *Links = grid->GetCellLinks();
-  Links->ResizeCellList(myVtkID, 1);
-  Links->AddCellReference(cell->getVtkId(), myVtkID);
+  if ( grid->HasLinks() )
+  {
+    vtkCellLinks *Links = grid->GetLinks();
+    Links->ResizeCellList(myVtkID, 1);
+    Links->AddCellReference(ME->getVtkId(), myVtkID);
+  }
 }
 
 //=======================================================================
@@ -340,12 +293,6 @@ void SMDS_MeshNode::ClearInverseElements()
   SMDS_Mesh::_meshList[myMeshId]->getGrid()->ResizeCellList(myVtkID, 0);
 }
 
-bool SMDS_MeshNode::emptyInverseElements()
-{
-  vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
-  return (l.ncells == 0);
-}
-
 //================================================================================
 /*!
  * \brief Count inverse elements of given type
@@ -354,35 +301,20 @@ bool SMDS_MeshNode::emptyInverseElements()
 
 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
 {
-  vtkCellLinks::Link l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetCellLinks()->GetLink(myVtkID);
+  int nb = 0;
+  if ( SMDS_Mesh::_meshList[myMeshId]->NbElements() > 0 ) // avoid building links
+  {
+    vtkCellLinks::Link& l = SMDS_Mesh::_meshList[myMeshId]->getGrid()->GetLinks()->GetLink(myVtkID);
 
-  if ( type == SMDSAbs_All )
-    return l.ncells;
+    if ( type == SMDSAbs_All )
+      return l.ncells;
 
-  int nb = 0;
-  SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
-  for (int i=0; i<l.ncells; i++)
-        {
-           const SMDS_MeshElement* elem = mesh->FindElement(mesh->fromVtkToSmds(l.cells[i]));
-           if (elem->GetType() == type)
-                nb++;
-        }
+    SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
+    for ( int i = 0; i < l.ncells; i++ )
+    {
+      const SMDS_MeshElement* elem = mesh->FindElement( mesh->fromVtkToSmds( l.cells[i] ));
+      nb += ( elem->GetType() == type );
+    }
+  }
   return nb;
 }
-
-///////////////////////////////////////////////////////////////////////////////
-/// To be used with STL set
-///////////////////////////////////////////////////////////////////////////////
-bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
-{
-        return e1.getVtkId()<e2.getVtkId();
-        /*if(e1.myX<e2.myX) return true;
-        else if(e1.myX==e2.myX)
-        {
-                if(e1.myY<e2.myY) return true;
-                else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);
-                else return false;
-        }
-        else return false;*/
-}
-