Salome HOME
Update copyrights
[modules/smesh.git] / src / SMDS / SMDS_MeshNode.cxx
index b7a1cb117ed5544bdc711316938ccecb25327bc5..67f70374c9f3384fe00335c4773f95c68afbaa5f 100644 (file)
@@ -1,91 +1,94 @@
-//  SMESH SMDS : implementaion of Salome mesh data structure
+// Copyright (C) 2007-2019  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
+//
+// 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, 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
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
-// 
-//  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. 
-// 
-//  This library is distributed in the hope that it will be useful, 
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
-//  Lesser General Public License for more details. 
-// 
-//  You should have received a copy of the GNU Lesser General Public 
-//  License along with this library; if not, write to the Free Software 
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
-// 
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
+//  SMESH SMDS : implementation of Salome mesh data structure
+//
 #ifdef _MSC_VER
 #pragma warning(disable:4786)
 #endif
 
 #include "SMDS_MeshNode.hxx"
+
+#include "SMDS_ElementFactory.hxx"
+#include "SMDS_Mesh.hxx"
+#include "SMDS_SetIterator.hxx"
 #include "SMDS_SpacePosition.hxx"
-#include "SMDS_IteratorOfElements.hxx"
 
-using namespace std;
+#include <utilities.h>
+#include <Utils_SALOME_Exception.hxx>
+#include <cassert>
 
-//=======================================================================
-//function : SMDS_MeshNode
-//purpose  : 
-//=======================================================================
+#include <boost/make_shared.hpp>
 
-SMDS_MeshNode::SMDS_MeshNode(double x, double y, double z):
-       myX(x), myY(y), myZ(z),
-       myPosition(SMDS_SpacePosition::originSpacePosition())
+void SMDS_MeshNode::init(double x, double y, double z)
 {
+  SMDS_UnstructuredGrid * grid = getGrid();
+  vtkPoints *points = grid->GetPoints();
+  points->InsertPoint( GetVtkID(), x, y, z );
+  if ( grid->HasLinks() )
+    grid->GetLinks()->ResizeForPoint( GetVtkID() );
 }
 
 //=======================================================================
 //function : RemoveInverseElement
-//purpose  : 
+//purpose  :
 //=======================================================================
 
-void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
+void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * elem)
 {
-  NCollection_List<const SMDS_MeshElement*>::Iterator it(myInverseElements);
-  while (it.More()) {
-    const SMDS_MeshElement* elem = it.Value();
-    if (elem == parent)
-      myInverseElements.Remove(it);
-    else
-      it.Next();
-  }
+  if ( getGrid()->HasLinks() )
+    getGrid()->RemoveReferenceToCell( GetVtkID(), elem->GetVtkID());
 }
 
 //=======================================================================
 //function : Print
-//purpose  : 
+//purpose  :
 //=======================================================================
 
 void SMDS_MeshNode::Print(ostream & OS) const
 {
-       OS << "Node <" << GetID() << "> : X = " << myX << " Y = "
-               << myY << " Z = " << myZ << endl;
+  OS << "Node <" << GetID() << "> : X = " << X() << " Y = "
+     << Y() << " Z = " << Z() << endl;
 }
 
 //=======================================================================
 //function : SetPosition
-//purpose  : 
+//purpose  :
 //=======================================================================
 
-void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
+void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos, int shapeID)
 {
-       myPosition = aPos;
+  myHolder->SetPosition( this, aPos, shapeID );
 }
 
 //=======================================================================
 //function : GetPosition
-//purpose  : 
+//purpose  : Return a position of this node on shape
+//warning  : result is std::unique_ptr !
 //=======================================================================
 
-const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
+SMDS_PositionPtr SMDS_MeshNode::GetPosition() const
 {
-       return myPosition;
+  return myHolder->GetPosition( this );
 }
 
 //=======================================================================
@@ -94,136 +97,173 @@ const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
  */
 //=======================================================================
 
-class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
+namespace
 {
-  NCollection_List<const SMDS_MeshElement*>::Iterator myIterator;
-  SMDSAbs_ElementType                                 myType;
- public:
-  SMDS_MeshNode_MyInvIterator(const NCollection_List<const SMDS_MeshElement*>& s,
-                              SMDSAbs_ElementType type):
-    myIterator(s), myType(type)
-  {}
-
-  bool more()
+  struct InverseIterator: public SMDS_ElemIterator
   {
-    if ( myType != SMDSAbs_All ) {
-      while ( myIterator.More() && myIterator.Value()->GetType() != myType)
-        myIterator.Next();
+    const SMDS_Mesh*       myMesh;
+    size_t                 myIter;
+    std::vector<vtkIdType> myCellList;
+
+    InverseIterator(const SMDS_Mesh *   mesh = 0,
+                    const vtkIdType*    cells = 0,
+                    const int           ncells = 0,
+                    SMDSAbs_ElementType type = SMDSAbs_All)
+      : myMesh(mesh), myIter(0)
+    {
+      if ( ncells )
+      {
+        myCellList.reserve( ncells );
+        if (type == SMDSAbs_All)
+        {
+          myCellList.assign( cells, cells + ncells );
+        }
+        else
+        {
+          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 )
+            {
+              myCellList.push_back(vtkId);
+            }
+          }
+        }
+      }
     }
-    return myIterator.More() != Standard_False;
-  }
 
-  const SMDS_MeshElement* next()
-  {
-    const SMDS_MeshElement* current=myIterator.Value();
-    myIterator.Next();
-    return current;
-  }    
-};
-
-SMDS_ElemIteratorPtr SMDS_MeshNode::
-       GetInverseElementIterator(SMDSAbs_ElementType type) const
-{
-  return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(myInverseElements,type));
-}
+    bool more()
+    {
+      return ( myIter < myCellList.size() );
+    }
 
-// Same as GetInverseElementIterator but the create iterator only return
-// wanted type elements.
-class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
-{
-  NCollection_List<const SMDS_MeshElement*> mySet;
-  NCollection_List<const SMDS_MeshElement*>::Iterator myIterator;
- public:
-  SMDS_MeshNode_MyIterator(SMDSAbs_ElementType type,
-                           const NCollection_List<const SMDS_MeshElement*>& s)
-  {
-    const SMDS_MeshElement * e;
-    bool toInsert;
-    NCollection_List<const SMDS_MeshElement*>::Iterator it(s);
-    for(; it.More(); it.Next())
+    const SMDS_MeshElement* next()
     {
-      e=it.Value();
-      switch(type)
+      int vtkId  = myCellList[ myIter++ ];
+      int smdsId = myMesh->FromVtkToSmds( vtkId );
+      const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
+      if (!elem)
       {
-      case SMDSAbs_Edge: toInsert=true; break;
-      case SMDSAbs_Face: toInsert=(e->GetType()!=SMDSAbs_Edge); break;
-      case SMDSAbs_Volume: toInsert=(e->GetType()==SMDSAbs_Volume); break;
+        MESSAGE("InverseIterator problem Null element");
+        throw SALOME_Exception("InverseIterator problem Null element");
       }
-      if(toInsert) mySet.Append(e);
+      return elem;
     }
-    myIterator.Init(mySet);
-  }
+  };
 
-  bool more()
+  //=======================================================================
+  /*!
+   * \brief Iterator on a node
+   */
+  //=======================================================================
+
+  template< class ELEM_ITERATOR >
+  struct Iterator : public ELEM_ITERATOR
   {
-    return myIterator.More() != Standard_False;
-  }
+    typedef typename ELEM_ITERATOR::value_type element_type;
+    const SMDS_MeshNode* myNode;
+
+    Iterator( const SMDS_MeshNode* n ): myNode( n ) {}
+
+    virtual bool more()
+    {
+      return myNode;
+    }
+    virtual element_type next()
+    {
+      element_type res = static_cast<element_type>( myNode );
+      myNode = 0;
+      return res;
+    }
+  };
+}
 
-  const SMDS_MeshElement* next()
+SMDS_ElemIteratorPtr SMDS_MeshNode::GetInverseElementIterator(SMDSAbs_ElementType type) const
+{
+  if ( GetMesh()->NbElements() > 0 ) // avoid building links
+  {
+    vtkCellLinks::Link& l = getGrid()->GetLinks()->GetLink( GetVtkID() );
+    return boost::make_shared< InverseIterator >( GetMesh(), l.cells, l.ncells, type );
+  }
+  else
   {
-    const SMDS_MeshElement* current=myIterator.Value();
-    myIterator.Next();
-    return current;
+    return boost::make_shared< InverseIterator >();
   }
-};
+}
 
-SMDS_ElemIteratorPtr SMDS_MeshNode::
-       elementsIterator(SMDSAbs_ElementType type) const
+SMDS_ElemIteratorPtr SMDS_MeshNode::nodesIterator() const
 {
-  if(type==SMDSAbs_Node)
-    return SMDS_MeshElement::elementsIterator(SMDSAbs_Node); 
-  else
-    return SMDS_ElemIteratorPtr
-      (new SMDS_IteratorOfElements
-       (this,type,
-        SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(type, myInverseElements))));
+  return boost::make_shared< Iterator< SMDS_ElemIterator > >( this );
 }
 
-int SMDS_MeshNode::NbNodes() const
+SMDS_NodeIteratorPtr SMDS_MeshNode::nodeIterator() const
 {
-       return 1;
+  return boost::make_shared< Iterator< SMDS_NodeIterator > >( this );
+}
+
+const SMDS_MeshNode* SMDS_MeshNode::GetNode(const int ind) const
+{
+  return ind == 0 ? this : 0;
+}
+
+double* SMDS_MeshNode::getCoord() const
+{
+  return getGrid()->GetPoint( GetVtkID() );
 }
 
 double SMDS_MeshNode::X() const
 {
-       return myX;
+  double *coord = getCoord();
+  return coord[0];
 }
 
 double SMDS_MeshNode::Y() const
 {
-       return myY;
+  double *coord = getCoord();
+  return coord[1];
 }
 
 double SMDS_MeshNode::Z() const
 {
-       return myZ;
+  double *coord = getCoord();
+  return coord[2];
 }
 
-void SMDS_MeshNode::setXYZ(double x, double y, double z)
+//================================================================================
+/*!
+ * \brief thread safe getting coords
+ */
+//================================================================================
+
+void SMDS_MeshNode::GetXYZ(double xyz[3]) const
 {
-       myX=x;
-       myY=y;
-       myZ=z;  
+  return getGrid()->GetPoint( GetVtkID(), xyz );
 }
 
-SMDSAbs_ElementType SMDS_MeshNode::GetType() const
+//================================================================================
+void SMDS_MeshNode::setXYZ( double x, double y, double z )
 {
-       return SMDSAbs_Node;
+  vtkPoints *points = getGrid()->GetPoints();
+  points->InsertPoint( GetVtkID(), x, y, z );
+  //GetMesh()->adjustBoundingBox(x, y, z);
+  GetMesh()->setMyModified();
 }
 
 //=======================================================================
 //function : AddInverseElement
 //purpose  :
 //=======================================================================
-void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
+void SMDS_MeshNode::AddInverseElement( const SMDS_MeshElement* elem )
 {
-  NCollection_List<const SMDS_MeshElement*>::Iterator it(myInverseElements);
-  for (; it.More(); it.Next()) {
-    const SMDS_MeshElement* elem = it.Value();
-    if (elem == ME)
-      return;
+  SMDS_UnstructuredGrid* grid = getGrid();
+  if ( grid->HasLinks() )
+  {
+    vtkCellLinks *Links = grid->GetLinks();
+    Links->ResizeCellList( GetVtkID(), 1 );
+    Links->AddCellReference( elem->GetVtkID(), GetVtkID() );
   }
-  myInverseElements.Append(ME);
 }
 
 //=======================================================================
@@ -232,12 +272,7 @@ void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
 //=======================================================================
 void SMDS_MeshNode::ClearInverseElements()
 {
-  myInverseElements.Clear();
-}
-
-bool SMDS_MeshNode::emptyInverseElements()
-{
-  return myInverseElements.IsEmpty() != Standard_False;
+  getGrid()->ResizeCellList( GetVtkID(), 0);
 }
 
 //================================================================================
@@ -246,31 +281,22 @@ bool SMDS_MeshNode::emptyInverseElements()
  */
 //================================================================================
 
-int SMDS_MeshNode::NbInverseNodes(SMDSAbs_ElementType type) const
+int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
 {
-  if ( type == SMDSAbs_All )
-    return myInverseElements.Extent();
   int nb = 0;
-  NCollection_List<const SMDS_MeshElement*>::Iterator it( myInverseElements );
-  for ( ; it.More(); it.Next() )
-    if ( it.Value()->GetType() == type )
-      nb++;
-  return nb;
-}
+  SMDS_Mesh *mesh = GetMesh();
+  if ( mesh->NbElements() > 0 ) // avoid building links
+  {
+    vtkCellLinks::Link& l = mesh->GetGrid()->GetLinks()->GetLink( GetVtkID() );
 
-///////////////////////////////////////////////////////////////////////////////
-/// To be used with STL set
-///////////////////////////////////////////////////////////////////////////////
-bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
-{
-       return e1.GetID()<e2.GetID();
-       /*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;*/
-}
+    if ( type == SMDSAbs_All )
+      return l.ncells;
 
+    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;
+}