Salome HOME
Changes for bug 0020705.
[modules/smesh.git] / src / StdMeshers / StdMeshers_FaceSide.cxx
index dbf7a3675b8220988fbc0424d03e863321d959fb..e464d0f12e215b5b950d71008f0ffee97f2675a2 100644 (file)
 #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>
 
+#include <GCPnts_AbscissaPoint.hxx>
+#include <Geom2dAdaptor_Curve.hxx>
+
 #include <map>
 
 #include "utilities.h"
@@ -95,6 +99,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 +107,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 +134,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 +165,56 @@ 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;
+  }
+}
+
+
+//=======================================================================
+//function : IsUniform
+//purpose  : auxilary function
+//=======================================================================
+bool IsUniform(const Handle(Geom2d_Curve)& C2d, double fp, double lp)
+{
+  //cout<<"IsUniform  fp = "<<fp<<"  lp = "<<lp<<endl;
+  if(C2d.IsNull())
+    return true;
+  Geom2dAdaptor_Curve A2dC(C2d);
+  double d1 = GCPnts_AbscissaPoint::Length( A2dC, fp, lp );
+  double d2 = GCPnts_AbscissaPoint::Length( A2dC, fp, fp+(lp-fp)/2. );
+  double d4 = GCPnts_AbscissaPoint::Length( A2dC, fp, fp+(lp-fp)/4. );
+  //cout<<"d1 = "<<d1<<"  d2 = "<<d2<<"  fabs(2*d2/d1-1.0) = "<<fabs(2*d2/d1-1.0)<<endl;
+  if( fabs(2*d2/d1-1.0) > 0.01 || fabs(2*d4/d2-1.0) > 0.01 )
+    return false;
+
+  return true;
+}
+
+
 //================================================================================
 /*!
  * \brief Return info on nodes on the side
@@ -175,23 +235,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;
@@ -199,11 +261,14 @@ const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool   isXConst,
         u2node.insert( make_pair( 1., node ));
       }
 
+      bool IsUni = IsUniform( myC2d[i], myFirst[i], myLast[i] );
+
       // put internal nodes
       SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
       if ( !sm ) continue;
       SMDS_NodeIteratorPtr nItr = sm->GetNodes();
-      double paramSize = myLast[i] - myFirst[i], r = myNormPar[i] - prevNormPar;
+      double paramSize = myLast[i] - myFirst[i];
+      double r = myNormPar[i] - prevNormPar;
       while ( nItr->more() ) {
         const SMDS_MeshNode* node = nItr->next();
         if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
@@ -212,7 +277,17 @@ const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool   isXConst,
           static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
         double u = epos->GetUParameter();
         // paramSize is signed so orientation is taken into account
+
         double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
+        if(!IsUni) {
+          double fp,lp;
+          TopLoc_Location L;
+          Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],L,fp,lp);
+          GeomAdaptor_Curve A3dC( C3d );
+          double aLen = GCPnts_AbscissaPoint::Length( A3dC, myFirst[i], myLast[i] );
+          double aLenU = GCPnts_AbscissaPoint::Length( A3dC, myFirst[i], u );
+          normPar = prevNormPar + r*aLenU/aLen;
+        }
 #ifdef _DEBUG_
         if ( normPar > 1 || normPar < 0) {
           dump("DEBUG");
@@ -454,6 +529,7 @@ BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
   return new BRepAdaptor_CompCurve( aWire );
 }
 
+
 //================================================================================
 /*!
  * \brief Return 2D point by normalized parameter
@@ -468,9 +544,32 @@ gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
     int i = EdgeIndex( U );
     double prevU = i ? myNormPar[ i-1 ] : 0;
     double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
-    return myC2d[ i ]->Value( myFirst[i] * ( 1 - r ) + myLast[i] * r );
+
+    double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
+    
+    // check parametrization of curve
+    if( !IsUniform( myC2d[i], myFirst[i], myLast[i] ) ) {
+      double fp,lp;
+      TopLoc_Location L;
+      Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],L,fp,lp);
+      fp = myFirst[i];
+      lp = myLast[i];
+      GeomAdaptor_Curve A3dC( C3d );
+      double aLen3d = GCPnts_AbscissaPoint::Length( A3dC, fp, lp );
+      double aLen3dU = aLen3d*r;
+      if(fp>lp) {
+        aLen3dU = -aLen3dU;
+      }
+      GCPnts_AbscissaPoint AbPnt( A3dC, aLen3dU, fp );
+      if( AbPnt.IsDone() ) {
+        par = AbPnt.Parameter();
+      }
+    }
+    return myC2d[ i ]->Value(par);
+
   }
-  return gp_Pnt2d( 1e+100, 1e+100 );
+  //return gp_Pnt2d( 1e+100, 1e+100 );
+  return myDefaultPnt2d;
 }
 
 //================================================================================
@@ -497,29 +596,29 @@ 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);
+        }
       }
-    }
-    // find out side orientation, which is important if there are several wires (PAL19080) 
-    bool isForward = true;
-    if ( nbWires > 1 ) {
-      TopExp_Explorer e( theFace, TopAbs_EDGE );
-      while ( ! e.Current().IsSame( wireEdges.back() ))
-        e.Next();
-      isForward = ( e.Current().Orientation() == wireEdges.back().Orientation() );
-    }
-
+    const bool isForward = true;
     StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
                                                          isForward, theIgnoreMediumNodes);
     wires[ iW ] = StdMeshers_FaceSidePtr( wire );
@@ -528,3 +627,42 @@ TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
   return wires;
 }
 
+//================================================================================
+/*!
+ * \brief Return 1st vertex of the i-the edge
+ */
+//================================================================================
+
+TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
+{
+  TopoDS_Vertex v;
+  if ( i < NbEdges() )
+  {
+    v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
+        TopExp::FirstVertex( myEdge[i], 1 )        :
+        TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
+  }
+  return v;
+}
+
+//================================================================================
+/*!
+ * \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;
+}
+