Salome HOME
0020676: EDF 1212 GEOM: Partition operation creates vertices which causes mesh comput...
[modules/smesh.git] / src / StdMeshers / StdMeshers_FaceSide.cxx
index 5850cb74ea5523b2b9922e542490b845c7488303..305c1fc9e9c980f965cca62924f6fb87341b64a4 100644 (file)
@@ -1,31 +1,30 @@
-//  SMESH SMESH : implementaion of SMESH idl descriptions
+//  Copyright (C) 2007-2008  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
 //
-//  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
+//  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 SMESH : implementaion of SMESH idl descriptions
 // File      : StdMeshers_FaceSide.hxx
 // Created   : Wed Jan 31 18:41:25 2007
 // Author    : Edward AGAPOV (eap)
 // Module    : SMESH
-
+//
 #include "StdMeshers_FaceSide.hxx"
 
 #include "SMDS_EdgePosition.hxx"
@@ -34,7 +33,7 @@
 #include "SMESHDS_SubMesh.hxx"
 //#include "SMESH_Algo.hxx"
 #include "SMESH_Mesh.hxx"
-#include "SMESH_MeshEditor.hxx"
+#include "SMESH_MesherHelper.hxx"
 #include "SMESH_ComputeError.hxx"
 #include "SMESH_Block.hxx"
 
@@ -44,6 +43,8 @@
 #include <BRep_Builder.hxx>
 #include <BRep_Tool.hxx>
 #include <TopExp.hxx>
+#include <TopExp_Explorer.hxx>
+#include <TopoDS.hxx>
 #include <TopoDS_Face.hxx>
 #include <TopoDS_Vertex.hxx>
 #include <TopoDS_Wire.hxx>
@@ -95,6 +96,7 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
   myMesh = theMesh;
   myMissingVertexNodes = false;
   myIgnoreMediumNodes = theIgnoreMediumNodes;
+  myDefaultPnt2d = gp_Pnt2d( 1e+100, 1e+100 );
   if ( nbEdges == 0 ) return;
 
   SMESHDS_Mesh* meshDS = theMesh->GetMeshDS();
@@ -102,6 +104,7 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
 
   int nbDegen = 0;
   list<TopoDS_Edge>::iterator edge = theEdges.begin();
+  TopoDS_Iterator vExp;
   for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
   {
     int i = theIsForward ? index : nbEdges - index - 1;
@@ -128,16 +131,20 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
       myNbPonits += nbN;
       myNbSegments += sm->NbElements();
     }
-    if ( SMESH_Algo::VertexNode( TopExp::FirstVertex( *edge, 1), meshDS ))
+    vExp.Initialize( *edge );
+    if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
       myNbPonits += 1; // for the first end
     else
       myMissingVertexNodes = true;
   }
-  if ( SMESH_Algo::VertexNode( TopExp::LastVertex( theEdges.back(), 1), meshDS ))
-    myNbPonits++; // for the last end
-  else
-    myMissingVertexNodes = true;
-
+  vExp.Next();
+  if ( vExp.More() )
+  {
+    if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
+      myNbPonits++; // for the last end
+    else
+      myMissingVertexNodes = true;
+  }
   if ( nbEdges > 1 && myLength > DBL_MIN ) {
     const double degenNormLen = 1.e-5;
     double totLength = myLength;
@@ -155,6 +162,34 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
   //dump();
 }
 
+//================================================================================
+/*!
+ * \brief Constructor of a side for vertex using data from other FaceSide
+  * \param theVertex - the vertex
+  * \param theSide - the side
+ */
+//================================================================================
+
+StdMeshers_FaceSide::StdMeshers_FaceSide(const SMDS_MeshNode* theNode,
+                                         const gp_Pnt2d thePnt2d,
+                                         const StdMeshers_FaceSide* theSide)
+{
+  myC2d.resize(1);
+  myLength = 0;
+  myMesh = theSide->GetMesh();
+  myDefaultPnt2d = thePnt2d;
+
+  myPoints = theSide->GetUVPtStruct();
+  myNbPonits = myNbSegments = myPoints.size();
+  std::vector<uvPtStruct>::iterator it = myPoints.begin();
+  for(; it!=myPoints.end(); it++) {
+    (*it).u = thePnt2d.X();
+    (*it).v = thePnt2d.Y();
+    (*it).y = 0.0;
+    (*it).node = theNode;
+  }
+}
+
 //================================================================================
 /*!
  * \brief Return info on nodes on the side
@@ -175,23 +210,25 @@ const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool   isXConst,
 
     map< double, const SMDS_MeshNode*> u2node;
     //int nbOnDegen = 0;
-    for ( int i = 0; i < myEdge.size(); ++i )
-    {
+    for ( int i = 0; i < myEdge.size(); ++i ) {
       // put 1st vertex node
-      TopoDS_Vertex VFirst, VLast;
-      TopExp::Vertices( myEdge[i], VFirst, VLast, true);
-      const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VFirst, meshDS );
+      TopoDS_Vertex VV[2]; // TopExp::FirstVertex() returns NULL for INTERNAL edge
+      for ( TopoDS_Iterator vIt(myEdge[i]); vIt.More(); vIt.Next() )
+        VV[ VV[0].IsNull() ? 0 : 1 ] = TopoDS::Vertex(vIt.Value());
+      if ( VV[0].Orientation() == TopAbs_REVERSED ) std::swap ( VV[0], VV[1] );
+      const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VV[0], meshDS );
       double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
       if ( node ) { // internal nodes may be missing
         u2node.insert( make_pair( prevNormPar, node ));
-      } else if ( i == 0 ) {
+      }
+      else if ( i == 0 ) {
         MESSAGE(" NO NODE on VERTEX" );
         return myPoints;
       }
 
       // put 2nd vertex node for a last edge
       if ( i+1 == myEdge.size() ) {
-        node = SMESH_Algo::VertexNode( VLast, meshDS );
+        node = SMESH_Algo::VertexNode( VV[1], meshDS );
         if ( !node ) {
           MESSAGE(" NO NODE on VERTEX" );
           return myPoints;
@@ -216,9 +253,9 @@ const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool   isXConst,
 #ifdef _DEBUG_
         if ( normPar > 1 || normPar < 0) {
           dump("DEBUG");
-          cout << "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
-               << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
-               << " paramSize="<<paramSize<<endl;
+          MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
+                    << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
+                    << " paramSize="<<paramSize );
         }
 #endif
         u2node.insert( make_pair( normPar, node ));
@@ -250,10 +287,10 @@ const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool   isXConst,
 #ifdef _DEBUG_
         if ( EdgeIndex >= myEdge.size() ) {
           dump("DEBUG");
-          cout << "WRONg EdgeIndex " << 1+EdgeIndex
-               << " myNormPar.size()="<<myNormPar.size()
-               << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
-               << " uvPt.normParam="<<uvPt.normParam <<endl;
+          MESSAGE ( "WRONg EdgeIndex " << 1+EdgeIndex
+                    << " myNormPar.size()="<<myNormPar.size()
+                    << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
+                    << " uvPt.normParam="<<uvPt.normParam );
         }
 #endif
         paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
@@ -381,26 +418,32 @@ void StdMeshers_FaceSide::Reverse()
 void StdMeshers_FaceSide::dump(const char* msg) const
 {
 #ifdef _DEBUG_
-  cout << endl;
-  if (msg) cout << msg <<endl;
-  cout<<"NB EDGES: "<< myEdge.size() <<endl;
-  cout << "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() <<endl;
+  if (msg) MESSAGE ( std::endl << msg );
+  MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
+  MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
   for ( int i=0; i<myEdge.size(); ++i)
   {
-    cout << "\t"<<i+1<<endl;
-    cout << "\tEDGE: ";
-    if (myEdge[i].IsNull())
-      cout<<"NULL"<<endl;
+    MESSAGE_ADD ( "\t"<<i+1 );
+    MESSAGE_ADD ( "\tEDGE: " );
+    if (myEdge[i].IsNull()) {
+      MESSAGE_ADD ( "NULL" );
+    }
     else {
       TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
-      cout << "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
-           << "  V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() << endl;
+      MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
+                 << "  V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
+    }
+    MESSAGE_ADD ( "\tC2d: ");
+    
+    if (myC2d[i].IsNull()) {
+      MESSAGE_ADD ( "NULL" );
     }
-    cout << "\tC2d: ";
-    if (myC2d[i].IsNull()) cout<<"NULL"<<endl;
-    else cout << myC2d[i].operator->()<<endl;
-    cout << "\tF: "<<myFirst[i]<< " L: "<< myLast[i]<<endl;
-    cout << "\tnormPar: "<<myNormPar[i]<<endl;
+    else {
+      MESSAGE_ADD ( myC2d[i].operator->() );
+    }
+      
+    MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
+    MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
   }
 #endif
 }
@@ -464,7 +507,8 @@ gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
     double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
     return myC2d[ i ]->Value( myFirst[i] * ( 1 - r ) + myLast[i] * r );
   }
-  return gp_Pnt2d( 1e+100, 1e+100 );
+  //return gp_Pnt2d( 1e+100, 1e+100 );
+  return myDefaultPnt2d;
 }
 
 //================================================================================
@@ -491,26 +535,71 @@ TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
   for ( int iW = 0; iW < nbWires; ++iW )
   {
     std::advance( to, *nbE++ );
+    if ( *nbE == 0 ) // Issue 0020676
+    {
+      --nbWires;
+      --iW;
+      wires.resize( nbWires );
+      continue;
+    }
     list< TopoDS_Edge > wireEdges( from, to );
     // assure that there is a node on the first vertex
     // as StdMeshers_FaceSide::GetUVPtStruct() requires
-    while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
-                                     theMesh.GetMeshDS()))
-    {
-      wireEdges.splice(wireEdges.end(), wireEdges,
-                       wireEdges.begin(), ++wireEdges.begin());
-      if ( from->IsSame( wireEdges.front() )) {
-        theError = TError
-          ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
-        return TSideVector(0);
+    if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
+      while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
+                                       theMesh.GetMeshDS()))
+      {
+        wireEdges.splice(wireEdges.end(), wireEdges,
+                         wireEdges.begin(), ++wireEdges.begin());
+        if ( from->IsSame( wireEdges.front() )) {
+          theError = TError
+            ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
+          return TSideVector(0);
+        }
       }
-    }
+    const bool isForward = true;
     StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
-                                                         true, theIgnoreMediumNodes);
+                                                         isForward, theIgnoreMediumNodes);
     wires[ iW ] = StdMeshers_FaceSidePtr( wire );
     from = to;
   }
   return wires;
 }
 
+//================================================================================
+/*!
+ * \brief Return 1st vertex of the i-the edge
+ */
+//================================================================================
+
+TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
+{
+  return (i >= NbEdges()) ? (TopoDS_Vertex()) :
+    (
+     myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
+     TopExp::FirstVertex( myEdge[i], 1 )        :
+     TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value())
+     );
+}
+
+//================================================================================
+/*!
+ * \brief Return last vertex of the i-the edge
+ */
+//================================================================================
+
+TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
+{
+  TopoDS_Vertex v;
+  if ( i < NbEdges() )
+  {
+    const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
+    if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
+      v = TopExp::LastVertex( edge, 1 );
+    else
+      for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
+        v = TopoDS::Vertex( vIt.Value() );
+  }
+  return v;
+}