Salome HOME
0021484: EDF 2087 SMESH: Problem projection
[modules/smesh.git] / src / StdMeshers / StdMeshers_Quadrangle_2D.cxx
index b75ca0b7aa21ec309edea410b2642a89d6190863..23be5d62faaab0306318ae709e11d59422abe35a 100644 (file)
@@ -1,23 +1,24 @@
-//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011  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-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.
+// 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.
+// 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
+// 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
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 
 //  File   : StdMeshers_Quadrangle_2D.cxx
 //  Author : Paul RASCLE, EDF
@@ -894,9 +895,9 @@ FaceQuadStruct* StdMeshers_Quadrangle_2D::CheckNbEdges(SMESH_Mesh &         aMes
 
       for ( int i = degenSides.size()-1; i > -1; --i )
       {
-        StdMeshers_FaceSide * & degenSide = quad->side[ degenSides[ i ]];
+        StdMeshers_FaceSide* degenSide = quad->side[ degenSides[ i ]];
         delete degenSide;
-        quad->side.erase( vector<StdMeshers_FaceSide*>::iterator( & degenSide ));
+        quad->side.erase( quad->side.begin() + degenSides[ i ] );
       }
       for ( unsigned i = TOP_SIDE; i < quad->side.size(); ++i )
         quad->side[i]->Reverse();
@@ -2191,7 +2192,6 @@ bool StdMeshers_Quadrangle_2D::ComputeReduced (SMESH_Mesh &        aMesh,
     int nb1 = nb;
     int nr1 = nr;
     int nt1 = nt;
-    int nl1 = nl;
 
     if (nr == nl) {
       if (nb < nt) {
@@ -2200,7 +2200,6 @@ bool StdMeshers_Quadrangle_2D::ComputeReduced (SMESH_Mesh &        aMesh,
       }
     }
     else if (nb == nt) {
-      nl1 = nb; // and == nt
       nr1 = nb; // and == nt
       if (nl < nr) {
         nt1 = nl;
@@ -2216,12 +2215,12 @@ bool StdMeshers_Quadrangle_2D::ComputeReduced (SMESH_Mesh &        aMesh,
     }
 
     // number of rows and columns
-    int nrows = nr1 - 1; // and also == nl1 - 1
+    int nrows = nr1 - 1;
     int ncol_top = nt1 - 1;
     int ncol_bot = nb1 - 1;
-    // maximum number of bottom elements for "tree" simple reduce 3->1
-    int max_tree31 = ncol_top * pow(3.0, nrows);
-    if (ncol_bot > max_tree31)
+    // number of rows needed to reduce ncol_bot to ncol_top using simple 3->1 "tree" (see below)
+    int nrows_tree31 = int( log( (double)(ncol_bot / ncol_top) ) / log((double) 3 )); // = log x base 3
+    if ( nrows < nrows_tree31 )
       MultipleReduce = true;
   }
 
@@ -2544,9 +2543,18 @@ bool StdMeshers_Quadrangle_2D::ComputeReduced (SMESH_Mesh &        aMesh,
     // maximum number of bottom elements for "linear" simple reduce 4->2
     int max_lin31 = ncol_top + ncol_top * 2 * nrows;
     // maximum number of bottom elements for "tree" simple reduce 4->2
-    int max_tree42 = npair_top * pow(2.0, nrows + 1);
-    if (ncol_top > npair_top * 2) {
-      int delta = ncol_bot - max_tree42;
+    int max_tree42 = 0;
+    // number of rows needed to reduce ncol_bot to ncol_top using simple 4->2 "tree"
+#ifdef WIN32
+    //<cmath> of the MSVC doesn't contain log2
+    int nrows_tree42 = int( log( (double)(ncol_bot / ncol_top) )/log((double)2)  ); // needed to avoid overflow at pow(2)
+#else
+    int nrows_tree42 = int( log2( ncol_bot / ncol_top )); // needed to avoid overflow at pow(2)
+#endif
+
+    if (ncol_top > npair_top * 2 && nrows_tree42 < nrows) {
+      max_tree42 = npair_top * pow(2.0, nrows + 1);
+      int delta = ncol_bot - int( max_tree42 );
       for (int irow = 1; irow < nrows; irow++) {
         int nfour = delta / 4;
         delta -= nfour * 2;
@@ -3791,7 +3799,7 @@ namespace // data for smoothing
     TSmoothNode* _n2;
     TTriangle( TSmoothNode* n1=0, TSmoothNode* n2=0 ): _n1(n1), _n2(n2) {}
 
-    inline bool IsForward( gp_UV uv );
+    inline bool IsForward( gp_UV uv ) const;
   };
   // --------------------------------------------------------------------------------
   /*!
@@ -3803,7 +3811,7 @@ namespace // data for smoothing
     vector< TTriangle > _triangles; // if empty, then node is not movable
   };
   // --------------------------------------------------------------------------------
-  inline bool TTriangle::IsForward( gp_UV uv )
+  inline bool TTriangle::IsForward( gp_UV uv ) const
   {
     gp_Vec2d v1( uv, _n1->_uv ), v2( uv, _n2->_uv );
     double d = v1 ^ v2;
@@ -3909,12 +3917,20 @@ void StdMeshers_Quadrangle_2D::Smooth (FaceQuadStruct* quad)
     }
   }
 
+  // define refernce orientation in 2D
+  TNo2SmooNoMap::iterator n2sn = smooNoMap.begin();
+  for ( ; n2sn != smooNoMap.end(); ++n2sn )
+    if ( !n2sn->second._triangles.empty() )
+      break;
+  if ( n2sn == smooNoMap.end() ) return;
+  const TSmoothNode & sampleNode = n2sn->second;
+  const bool refForward = ( sampleNode._triangles[0].IsForward( sampleNode._uv ));
+
   // Smoothing
 
   for ( int iLoop = 0; iLoop < 5; ++iLoop )
   {
-    TNo2SmooNoMap::iterator n2sn = smooNoMap.begin();
-    for ( ; n2sn != smooNoMap.end(); ++n2sn )
+    for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
     {
       TSmoothNode& sNode = n2sn->second;
       if ( sNode._triangles.empty() )
@@ -3929,7 +3945,7 @@ void StdMeshers_Quadrangle_2D::Smooth (FaceQuadStruct* quad)
       // check validity of the newUV
       bool isValid = true;
       for ( unsigned i = 0; i < sNode._triangles.size() && isValid; ++i )
-        isValid = sNode._triangles[i].IsForward( newUV );
+        isValid = ( sNode._triangles[i].IsForward( newUV ) == refForward );
 
       if ( isValid )
         sNode._uv = newUV;
@@ -3940,8 +3956,7 @@ void StdMeshers_Quadrangle_2D::Smooth (FaceQuadStruct* quad)
 
   Handle(Geom_Surface) surface = BRep_Tool::Surface( geomFace );
 
-  TNo2SmooNoMap::iterator n2sn = smooNoMap.begin();
-  for ( ; n2sn != smooNoMap.end(); ++n2sn )
+  for ( n2sn = smooNoMap.begin(); n2sn != smooNoMap.end(); ++n2sn )
   {
     TSmoothNode& sNode = n2sn->second;
     if ( sNode._triangles.empty() )