Salome HOME
Merge from V5_1_4_BR 07/05/2010
[modules/smesh.git] / src / StdMeshers / StdMeshers_FaceSide.cxx
index 969dc1ce56b828a629f9519fbe1c3ee38478cbb2..c0cef600d0d97a5eb6b05dcc61fc3ba5223ee4a0 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//  Copyright (C) 2007-2010  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
@@ -19,6 +19,7 @@
 //
 //  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
@@ -91,9 +92,12 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
   int nbEdges = theEdges.size();
   myEdge.resize( nbEdges );
   myC2d.resize( nbEdges );
+  myC3dAdaptor.resize( nbEdges );
   myFirst.resize( nbEdges );
   myLast.resize( nbEdges );
   myNormPar.resize( nbEdges );
+  myEdgeLength.resize( nbEdges );
+  myIsUniform.resize( nbEdges, true );
   myLength = 0;
   myNbPonits = myNbSegments = 0;
   myMesh = theMesh;
@@ -103,7 +107,6 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
   if ( nbEdges == 0 ) return;
 
   SMESHDS_Mesh* meshDS = theMesh->GetMeshDS();
-  vector<double> len( nbEdges );
 
   int nbDegen = 0;
   list<TopoDS_Edge>::iterator edge = theEdges.begin();
@@ -111,9 +114,9 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
   for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
   {
     int i = theIsForward ? index : nbEdges - index - 1;
-    len[i] = SMESH_Algo::EdgeLength( *edge );
-    if ( len[i] < DBL_MIN ) nbDegen++;
-    myLength += len[i];
+    myEdgeLength[i] = SMESH_Algo::EdgeLength( *edge );
+    if ( myEdgeLength[i] < DBL_MIN ) nbDegen++;
+    myLength += myEdgeLength[i];
     myEdge[i] = *edge;
     if ( !theIsForward ) myEdge[i].Reverse();
 
@@ -141,6 +144,24 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
       myNbPonits += 1; // for the first end
     else
       myMissingVertexNodes = true;
+
+    // check if edge has non-uniform parametrization (issue 0020705)
+    if ( !myC2d[i].IsNull() && myEdgeLength[i] > DBL_MIN)
+    {
+      Geom2dAdaptor_Curve A2dC( myC2d[i] );
+      double p2 = myFirst[i]+(myLast[i]-myFirst[i])/2., p4 = myFirst[i]+(myLast[i]-myFirst[i])/4.;
+      double d2 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p2 );
+      double d4 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p4 );
+      //cout<<"len = "<<len<<"  d2 = "<<d2<<"  fabs(2*d2/len-1.0) = "<<fabs(2*d2/len-1.0)<<endl;
+      myIsUniform[i] = !( fabs(2*d2/myEdgeLength[i]-1.0) > 0.01 || fabs(2*d4/d2-1.0) > 0.01 );
+      if ( !myIsUniform[i] )
+      {
+        double fp,lp;
+        TopLoc_Location L;
+        Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],L,fp,lp);
+        myC3dAdaptor[i].Load( C3d, fp,lp );
+      }
+    }
   }
   vExp.Initialize( theEdges.back() );
   if ( vExp.Value().Orientation() != TopAbs_REVERSED ) vExp.Next();
@@ -158,9 +179,9 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
       totLength += myLength * degenNormLen * nbDegen;
     double prevNormPar = 0;
     for ( int i = 0; i < nbEdges; ++i ) {
-      if ( len[ i ] < DBL_MIN )
-        len[ i ] = myLength * degenNormLen;
-      myNormPar[ i ] = prevNormPar + len[i]/totLength;
+      if ( myEdgeLength[ i ] < DBL_MIN )
+        myEdgeLength[ i ] = myLength * degenNormLen;
+      myNormPar[ i ] = prevNormPar + myEdgeLength[i]/totLength;
       prevNormPar = myNormPar[ i ];
     }
   }
@@ -196,28 +217,6 @@ StdMeshers_FaceSide::StdMeshers_FaceSide(const SMDS_MeshNode* 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
@@ -233,13 +232,16 @@ const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool   isXConst,
     if ( NbEdges() == 0 ) return myPoints;
 
     SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
+    SMESH_MesherHelper helper(*myMesh);
+    bool paramOK;
 
     // sort nodes of all edges putting them into a map
 
     map< double, const SMDS_MeshNode*> u2node;
     //int nbOnDegen = 0;
-    for ( int i = 0; i < myEdge.size(); ++i ) {
-      // put 1st vertex node
+    for ( int i = 0; i < myEdge.size(); ++i )
+    {
+      // Put 1st vertex node of a current edge
       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());
@@ -254,52 +256,54 @@ const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool   isXConst,
         return myPoints;
       }
 
-      // put 2nd vertex node for a last edge
-      if ( i+1 == myEdge.size() ) {
-        node = SMESH_Algo::VertexNode( VV[1], meshDS );
-        if ( !node ) {
-          MESSAGE(" NO NODE on VERTEX" );
-          return myPoints;
-        }
-        u2node.insert( make_pair( 1., node ));
-      }
-
-      bool IsUni = IsUniform( myC2d[i], myFirst[i], myLast[i] );
-
-      // put internal nodes
+      // Put internal nodes
       SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
       if ( !sm ) continue;
+      vector< pair< double, const SMDS_MeshNode*> > u2nodeVec;
+      u2nodeVec.reserve( sm->NbNodes() );
       SMDS_NodeIteratorPtr nItr = sm->GetNodes();
       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 ))
-          continue;
-        const SMDS_EdgePosition* epos =
-          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;
+      if ( !myIsUniform[i] )
+        while ( nItr->more() )
+        {
+          const SMDS_MeshNode* node = nItr->next();
+          if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
+            continue;
+          double u = helper.GetNodeU( myEdge[i], node, &paramOK );
+          double aLenU = GCPnts_AbscissaPoint::Length
+            ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), myFirst[i], u );
+          if ( myEdgeLength[i] < aLenU ) // nonregression test "3D_mesh_NETGEN/G6"
+          {
+            u2nodeVec.clear();
+            break;
+          }
+          double normPar = prevNormPar + r*aLenU/myEdgeLength[i];
+          u2nodeVec.push_back( make_pair( normPar, node ));
         }
-#ifdef _DEBUG_
-        if ( normPar > 1 || normPar < 0) {
-          dump("DEBUG");
-          MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
-                    << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
-                    << " paramSize="<<paramSize );
+      nItr = sm->GetNodes();
+      if ( u2nodeVec.empty() )
+        while ( nItr->more() )
+        {
+          const SMDS_MeshNode* node = nItr->next();
+          if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
+            continue;
+          double u = helper.GetNodeU( myEdge[i], node, &paramOK );
+
+          // paramSize is signed so orientation is taken into account
+          double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
+          u2nodeVec.push_back( make_pair( normPar, node ));
         }
-#endif
-        u2node.insert( make_pair( normPar, node ));
+      u2node.insert( u2nodeVec.begin(), u2nodeVec.end() );
+
+      // Put 2nd vertex node for a last edge
+      if ( i+1 == myEdge.size() ) {
+        node = SMESH_Algo::VertexNode( VV[1], meshDS );
+        if ( !node ) {
+          MESSAGE(" NO NODE on VERTEX" );
+          return myPoints;
+        }
+        u2node.insert( make_pair( 1., node ));
       }
     }
     if ( u2node.size() != myNbPonits ) {
@@ -441,9 +445,12 @@ void StdMeshers_FaceSide::Reverse()
   if ( nbEdges > 1 ) {
     reverse( myEdge );
     reverse( myC2d );
+    reverse( myC3dAdaptor );
     reverse( myFirst );
     reverse( myLast );
     reverse( myNormPar );
+    reverse( myEdgeLength );
+    reverse( myIsUniform );
   }
   myNormPar[nbEdges-1]=1.;
   myPoints.clear();
@@ -551,19 +558,11 @@ gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
     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( !myIsUniform[i] )
+    {
+      double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
+      GCPnts_AbscissaPoint AbPnt
+        ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
       if( AbPnt.IsDone() ) {
         par = AbPnt.Parameter();
       }
@@ -571,7 +570,6 @@ gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
     return myC2d[ i ]->Value(par);
 
   }
-  //return gp_Pnt2d( 1e+100, 1e+100 );
   return myDefaultPnt2d;
 }