Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / SMESHUtils / SMESH_Delaunay.cxx
index e53bfd8fcda9257d91bf8ca1026e9f07e372cab1..70890775ca0c570653625cec326ac07526e567be 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  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
 #include <BRepAdaptor_Surface.hxx>
 #include <BRepMesh_Delaun.hxx>
 
+#include <Basics_OCCTVersion.hxx>
+
 //================================================================================
 /*!
  * \brief Construct a Delaunay triangulation of given boundary nodes
  *  \param [in] boundaryNodes - vector of nodes of a wire
  *  \param [in] face - the face
  *  \param [in] faceID - the face ID
- *  \param [in] nbNodesToVisit - nb of non-marked nodes on the face
  */
 //================================================================================
 
@@ -54,6 +55,8 @@ SMESH_Delaunay::SMESH_Delaunay(const std::vector< const UVPtStructVec* > & bound
     const int nbDiv = 100;
     const double uRange = surf.LastUParameter() - surf.FirstUParameter();
     const double vRange = surf.LastVParameter() - surf.FirstVParameter();
+    const double uFixed = surf.FirstUParameter() + 0.5 * uRange;
+    const double vFixed = surf.FirstVParameter() + 0.5 * vRange;
     const double dU = uRange / nbDiv;
     const double dV = vRange / nbDiv;
     double u = surf.FirstUParameter(), v = surf.FirstVParameter();
@@ -61,10 +64,10 @@ SMESH_Delaunay::SMESH_Delaunay(const std::vector< const UVPtStructVec* > & bound
     double lenU = 0, lenV = 0;
     for ( ; u < surf.LastUParameter(); u += dU, v += dV )
     {
-      gp_Pnt p1U = surf.Value( u, surf.FirstVParameter() );
+      gp_Pnt p1U = surf.Value( u, vFixed );
       lenU += p1U.Distance( p0U );
       p0U = p1U;
-      gp_Pnt p1V = surf.Value( surf.FirstUParameter(), v );
+      gp_Pnt p1V = surf.Value( uFixed, v );
       lenV += p1V.Distance( p0V );
       p0V = p1V;
     }
@@ -82,7 +85,11 @@ SMESH_Delaunay::SMESH_Delaunay(const std::vector< const UVPtStructVec* > & bound
   _bndNodes.resize( nbP );
 
   // fill boundary points
+#if OCC_VERSION_LARGE <= 0x07030000
   BRepMesh::Array1OfVertexOfDelaun bndVert( 1, 1 + nbP );
+#else
+  IMeshData::Array1OfVertexOfDelaun bndVert( 1, 1 + nbP );
+#endif
   BRepMesh_Vertex v( 0, 0, BRepMesh_Frontier );
   for ( size_t iW = 0; iW < boundaryNodes.size(); ++iW )
   {
@@ -193,21 +200,22 @@ const BRepMesh_Triangle* SMESH_Delaunay::FindTriangle( const gp_XY&
     nodeUVs[1] = _triaDS->GetNode( nodeIDs[1] ).Coord();
     nodeUVs[2] = _triaDS->GetNode( nodeIDs[2] ).Coord();
 
-    if ( _triaDS->GetNode( nodeIDs[0] ).Movability() == BRepMesh_Frontier &&
-         _triaDS->GetNode( nodeIDs[1] ).Movability() == BRepMesh_Frontier &&
-         _triaDS->GetNode( nodeIDs[2] ).Movability() == BRepMesh_Frontier )
+    SMESH_MeshAlgos::GetBarycentricCoords( uv,
+                                           nodeUVs[0], nodeUVs[1], nodeUVs[2],
+                                           bc[0], bc[1] );
+    if ( bc[0] >= 0 && bc[1] >= 0 && bc[0] + bc[1] <= 1 )
     {
-      SMESH_MeshAlgos::GetBarycentricCoords( uv,
-                                             nodeUVs[0], nodeUVs[1], nodeUVs[2],
-                                             bc[0], bc[1] );
-      if ( bc[0] >= 0 && bc[1] >= 0 && bc[0] + bc[1] <= 1 )
+      if ( _triaDS->GetNode( nodeIDs[0] ).Movability() != BRepMesh_Frontier ||
+           _triaDS->GetNode( nodeIDs[1] ).Movability() != BRepMesh_Frontier ||
+           _triaDS->GetNode( nodeIDs[2] ).Movability() != BRepMesh_Frontier )
       {
-        bc[2] = 1 - bc[0] - bc[1];
-        triaNodes[0] = nodeIDs[0] - 1;
-        triaNodes[1] = nodeIDs[1] - 1;
-        triaNodes[2] = nodeIDs[2] - 1;
-        return tria;
+        return 0;
       }
+      bc[2] = 1 - bc[0] - bc[1];
+      triaNodes[0] = nodeIDs[0] - 1;
+      triaNodes[1] = nodeIDs[1] - 1;
+      triaNodes[2] = nodeIDs[2] - 1;
+      return tria;
     }
 
     // look for a neighbor triangle, which is adjacent to a link intersected
@@ -217,7 +225,8 @@ const BRepMesh_Triangle* SMESH_Delaunay::FindTriangle( const gp_XY&
     gp_XY seg = uv - gc;
 
     tria->Edges( linkIDs, ori );
-    int triaID = _triaDS->IndexOf( *tria );
+
+    const BRepMesh_Triangle* prevTria = tria;
     tria = 0;
 
     for ( int i = 0; i < 3; ++i )
@@ -240,7 +249,9 @@ const BRepMesh_Triangle* SMESH_Delaunay::FindTriangle( const gp_XY&
       double uSeg = ( uv1 - gc ) ^ lin / crossSegLin;
       if ( 0. <= uSeg && uSeg <= 1. )
       {
-        tria = & _triaDS->GetElement( triIDs.Index( 1 + ( triIDs.Index(1) == triaID )));
+        tria = & _triaDS->GetElement( triIDs.Index( 1 ));
+        if ( tria == prevTria )
+          tria = & _triaDS->GetElement( triIDs.Index( 2 ));
         if ( tria->Movability() != BRepMesh_Deleted )
           break;
       }
@@ -259,10 +270,17 @@ const BRepMesh_Triangle* SMESH_Delaunay::FindTriangle( const gp_XY&
 
 const BRepMesh_Triangle* SMESH_Delaunay::GetTriangleNear( int iBndNode )
 {
+  if ( iBndNode >= _triaDS->NbNodes() )
+    return 0;
   int nodeIDs[3];
-  int nbNbNodes = _bndNodes.size();
-  const BRepMesh::ListOfInteger & linkIds = _triaDS->LinksConnectedTo( iBndNode + 1 );
-  BRepMesh::ListOfInteger::const_iterator iLink = linkIds.cbegin();
+  int nbBndNodes = _bndNodes.size();
+#if OCC_VERSION_LARGE <= 0x07030000
+  typedef BRepMesh::ListOfInteger TLinkList;
+#else
+  typedef IMeshData::ListOfInteger TLinkList;
+#endif
+  const TLinkList &       linkIds = _triaDS->LinksConnectedTo( iBndNode + 1 );
+  TLinkList::const_iterator iLink = linkIds.cbegin();
   for ( ; iLink != linkIds.cend(); ++iLink )
   {
     const BRepMesh_PairOfIndex & triaIds = _triaDS->ElementsConnectedTo( *iLink );
@@ -271,9 +289,9 @@ const BRepMesh_Triangle* SMESH_Delaunay::GetTriangleNear( int iBndNode )
       if ( tria.Movability() != BRepMesh_Deleted )
       {
         _triaDS->ElementNodes( tria, nodeIDs );
-        if ( nodeIDs[0]-1 < nbNbNodes &&
-             nodeIDs[1]-1 < nbNbNodes &&
-             nodeIDs[2]-1 < nbNbNodes )
+        if ( nodeIDs[0]-1 < nbBndNodes &&
+             nodeIDs[1]-1 < nbBndNodes &&
+             nodeIDs[2]-1 < nbBndNodes )
           return &tria;
       }
     }
@@ -283,9 +301,9 @@ const BRepMesh_Triangle* SMESH_Delaunay::GetTriangleNear( int iBndNode )
       if ( tria.Movability() != BRepMesh_Deleted )
       {
         _triaDS->ElementNodes( tria, nodeIDs );
-        if ( nodeIDs[0]-1 < nbNbNodes &&
-             nodeIDs[1]-1 < nbNbNodes &&
-             nodeIDs[2]-1 < nbNbNodes )
+        if ( nodeIDs[0]-1 < nbBndNodes &&
+             nodeIDs[1]-1 < nbBndNodes &&
+             nodeIDs[2]-1 < nbBndNodes )
           return &tria;
       }
     }
@@ -344,7 +362,7 @@ void SMESH_Delaunay::ToPython() const
   text << "import salome, SMESH\n";
   text << "salome.salome_init()\n";
   text << "from salome.smesh import smeshBuilder\n";
-  text << "smesh = smeshBuilder.New(salome.myStudy)\n";
+  text << "smesh = smeshBuilder.New()\n";
   text << "mesh=smesh.Mesh()\n";
   const char* endl = "\n";
 
@@ -355,19 +373,34 @@ void SMESH_Delaunay::ToPython() const
   }
 
   int nodeIDs[3];
+  const char* dofName[] = { "Free",
+                            "InVolume",
+                            "OnSurface",
+                            "OnCurve",
+                            "Fixed",
+                            "Frontier",
+                            "Deleted" };
+  text << "# nb elements = " << _triaDS->NbElements() << endl;
+  std::vector< int > deletedElems;
   for ( int i = 0; i < _triaDS->NbElements(); ++i )
   {
     const BRepMesh_Triangle& t = _triaDS->GetElement( i+1 );
     if ( t.Movability() == BRepMesh_Deleted )
-      continue;
+      deletedElems.push_back( i+1 );
+    //   continue;
     _triaDS->ElementNodes( t, nodeIDs );
-    text << "mesh.AddFace([ " << nodeIDs[0] << ", " << nodeIDs[1] << ", " << nodeIDs[2] << " ])" << endl;
+    text << "mesh.AddFace([ " << nodeIDs[0] << ", " << nodeIDs[1] << ", " << nodeIDs[2] << " ]) # "
+         <<  dofName[ t.Movability() ] << endl;
   }
+  text << "mesh.MakeGroupByIds( 'deleted elements', SMESH.FACE, [";
+  for ( int id : deletedElems )
+    text << id << ",";
+  text << "])" << endl;
 
   const char* fileName = "/tmp/Delaunay.py";
   SMESH_File file( fileName, false );
   file.remove();
   file.openForWriting();
   file.write( text.c_str(), text.size() );
-  cout << "execfile( '" << fileName << "')" << endl;
+  std::cout << "exec(open('" << fileName << "', 'rb').read())";
 }