Salome HOME
bos #20256: [CEA 18523] Porting SMESH to int 64 bits
[modules/smesh.git] / src / SMDS / SMDS_MeshNode.cxx
index 6a9556031d5cad2a416b9d9ef6aed2bf05becb09..ac99e349df5a81b14ee9a57bd38cdc32ce557779 100644 (file)
-//  SMESH SMDS : implementaion of Salome mesh data structure
+// Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+// 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.
 //
-//  File   : SMDS_MeshNode.cxx
-//  Author : Jean-Michel BOULCOURT
-//  Module : SMESH
+// 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"
 
-using namespace std;
-#include "SMDS_MeshNode.ixx"
-#include "SMDS_ListIteratorOfListOfMeshElement.hxx"
+#include "SMDS_ElementFactory.hxx"
+#include "SMDS_Mesh.hxx"
+#include "SMDS_SetIterator.hxx"
 #include "SMDS_SpacePosition.hxx"
 
-static Handle(SMDS_Position)& StaticInstancePosition()
+#include <utilities.h>
+#include <Utils_SALOME_Exception.hxx>
+#include <cassert>
+#include <smIdType.hxx>
+
+#include <boost/make_shared.hpp>
+
+void SMDS_MeshNode::init(double x, double y, double z)
 {
-  static Handle(SMDS_SpacePosition) staticpos;
-  if (staticpos.IsNull())
-    staticpos = new SMDS_SpacePosition();
+  SMDS_UnstructuredGrid * grid = getGrid();
+  vtkPoints *points = grid->GetPoints();
+  points->InsertPoint( GetVtkID(), x, y, z );
+  if ( grid->HasLinks() )
+    grid->GetLinks()->ResizeForPoint( GetVtkID() );
+}
 
-  return staticpos;
+//=======================================================================
+//function : RemoveInverseElement
+//purpose  :
+//=======================================================================
+
+void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * elem)
+{
+  if ( getGrid()->HasLinks() )
+    getGrid()->RemoveReferenceToCell( GetVtkID(), elem->GetVtkID());
 }
 
 //=======================================================================
-//function : SMDS_MeshNode
-//purpose  : 
+//function : Print
+//purpose  :
 //=======================================================================
 
-SMDS_MeshNode::SMDS_MeshNode(const Standard_Integer ID,
-                            const Standard_Real x, const Standard_Real y, const Standard_Real z) :
-  SMDS_MeshElement(ID,1,SMDSAbs_Node),myPnt(x,y,z),myPosition(StaticInstancePosition())
+void SMDS_MeshNode::Print(ostream & OS) const
 {
+  OS << "Node <" << GetID() << "> : X = " << X() << " Y = "
+     << Y() << " Z = " << Z() << endl;
 }
 
+//=======================================================================
+//function : SetPosition
+//purpose  :
+//=======================================================================
+
+void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos, int shapeID)
+{
+  myHolder->SetPosition( this, aPos, shapeID );
+}
 
 //=======================================================================
-//function : RemoveInverseElement
-//purpose  : 
+//function : GetPosition
+//purpose  : Return a position of this node on shape
+//warning  : result is std::unique_ptr !
+//=======================================================================
+
+SMDS_PositionPtr SMDS_MeshNode::GetPosition() const
+{
+  return myHolder->GetPosition( this );
+}
+
+//=======================================================================
+/*!
+ * \brief Iterator on list of elements
+ */
 //=======================================================================
 
-void SMDS_MeshNode::RemoveInverseElement(const Handle(SMDS_MeshElement)& parent)
+namespace
 {
+  struct InverseIterator: public SMDS_ElemIterator
+  {
+    const SMDS_Mesh*       myMesh;
+    size_t                 myIter;
+    std::vector<vtkIdType> myCellList;
 
-  SMDS_ListIteratorOfListOfMeshElement itLstInvCnx(myInverseElements);
+    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++)
+          {
+            vtkIdType vtkId = cells[i];
+            smIdType smdsId = myMesh->FromVtkToSmds( vtkId );
+            const SMDS_MeshElement* elem = myMesh->FindElement( smdsId );
+            if ( elem->GetType() == type )
+            {
+              myCellList.push_back(vtkId);
+            }
+          }
+        }
+      }
+    }
 
-  for (;itLstInvCnx.More();itLstInvCnx.Next()) {
-    Handle(SMDS_MeshElement)& ME = itLstInvCnx.Value();
-    if (ME->IsSame(parent))
-      myInverseElements.Remove(itLstInvCnx);
-    if (!itLstInvCnx.More())
-      break;
+    bool more()
+    {
+      return ( myIter < myCellList.size() );
+    }
+
+    const SMDS_MeshElement* next()
+    {
+      vtkIdType vtkId = myCellList[ myIter++ ];
+      smIdType smdsId = myMesh->FromVtkToSmds( vtkId );
+      const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
+      if (!elem)
+      {
+        MESSAGE("InverseIterator problem Null element");
+        throw SALOME_Exception("InverseIterator problem Null element");
+      }
+      return elem;
+    }
+  };
+
+  //=======================================================================
+  /*!
+   * \brief Iterator on a node
+   */
+  //=======================================================================
+
+  template< class ELEM_ITERATOR >
+  struct Iterator : public ELEM_ITERATOR
+  {
+    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;
+    }
+  };
+}
+
+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
+  {
+    return boost::make_shared< InverseIterator >();
   }
 }
 
+SMDS_ElemIteratorPtr SMDS_MeshNode::nodesIterator() const
+{
+  return boost::make_shared< Iterator< SMDS_ElemIterator > >( this );
+}
 
-//=======================================================================
-//function : Print
-//purpose  : 
-//=======================================================================
+SMDS_NodeIteratorPtr SMDS_MeshNode::nodeIterator() const
+{
+  return boost::make_shared< Iterator< SMDS_NodeIterator > >( this );
+}
+
+const SMDS_MeshNode* SMDS_MeshNode::GetNode(const int ind) const
+{
+  return ind == 0 ? this : 0;
+}
 
-void SMDS_MeshNode::Print(Standard_OStream& OS) const
+double* SMDS_MeshNode::getCoord() const
 {
-  OS << "Node <" << myID << "> : X = " << myPnt.X() << " Y = " << myPnt.Y() << " Z = " << myPnt.Z() << endl;
+  return getGrid()->GetPoint( GetVtkID() );
 }
 
+double SMDS_MeshNode::X() const
+{
+  double *coord = getCoord();
+  return coord[0];
+}
 
-//=======================================================================
-//function : SetPosition
-//purpose  : 
-//=======================================================================
+double SMDS_MeshNode::Y() const
+{
+  double *coord = getCoord();
+  return coord[1];
+}
+
+double SMDS_MeshNode::Z() const
+{
+  double *coord = getCoord();
+  return coord[2];
+}
+
+//================================================================================
+/*!
+ * \brief thread safe getting coords
+ */
+//================================================================================
 
-void SMDS_MeshNode::SetPosition(const Handle(SMDS_Position)& aPos)
+void SMDS_MeshNode::GetXYZ(double xyz[3]) const
 {
-  myPosition = aPos;
+  return getGrid()->GetPoint( GetVtkID(), xyz );
+}
+
+//================================================================================
+void SMDS_MeshNode::setXYZ( double x, double y, double z )
+{
+  vtkPoints *points = getGrid()->GetPoints();
+  points->InsertPoint( GetVtkID(), x, y, z );
+  //GetMesh()->adjustBoundingBox(x, y, z);
+  GetMesh()->setMyModified();
 }
 
 //=======================================================================
-//function : GetPosition
-//purpose  : 
+//function : AddInverseElement
+//purpose  :
 //=======================================================================
+void SMDS_MeshNode::AddInverseElement( const SMDS_MeshElement* elem )
+{
+  SMDS_UnstructuredGrid* grid = getGrid();
+  if ( grid->HasLinks() )
+  {
+    vtkCellLinks *Links = grid->GetLinks();
+    Links->ResizeCellList( GetVtkID(), 1 );
+    Links->AddCellReference( elem->GetVtkID(), GetVtkID() );
+  }
+}
 
-Handle(SMDS_Position) SMDS_MeshNode::GetPosition() const
+//=======================================================================
+//function : ClearInverseElements
+//purpose  :
+//=======================================================================
+void SMDS_MeshNode::ClearInverseElements()
 {
-  return myPosition;
+  getGrid()->ResizeCellList( GetVtkID(), 0);
 }
 
+//================================================================================
+/*!
+ * \brief Count inverse elements of given type
+ */
+//================================================================================
+
+int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
+{
+  int nb = 0;
+  SMDS_Mesh *mesh = GetMesh();
+  if ( mesh->NbElements() > 0 ) // avoid building links
+  {
+    vtkCellLinks::Link& l = mesh->GetGrid()->GetLinks()->GetLink( GetVtkID() );
+
+    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;
+}