Salome HOME
IPAL22173 TC6.2.0: "Netgen1D-2D" algorithm doesn't work on "flight_solid.brep" V6_3_1_BR V6_3_1_WIN V6_3_BR V6_3_1 V6_3_1rc1 V6_3_1rc2 V6_3_1rc3
authoreap <eap@opencascade.com>
Tue, 28 Jun 2011 08:29:25 +0000 (08:29 +0000)
committereap <eap@opencascade.com>
Tue, 28 Jun 2011 08:29:25 +0000 (08:29 +0000)
   1) fix search of a node for edge extremities
   2) add comments

src/NETGEN/netgen49ForSalome.patch

index 14e44486af421e77cdb60fa7d09d9d7729524068..9b4373818bc5b4a8c0d333d5325922f945bb144a 100644 (file)
@@ -1,76 +1,83 @@
 diff -Naur netgen-4.9.13_orig/libsrc/meshing/meshtype.cpp netgen-4.9.13_new/libsrc/meshing/meshtype.cpp
 --- netgen-4.9.13_orig/libsrc/meshing/meshtype.cpp     2009-09-13 14:28:38.000000000 +0400
-+++ netgen-4.9.13_new/libsrc/meshing/meshtype.cpp      2011-02-18 11:47:33.000000000 +0300
++++ netgen-4.9.13_new/libsrc/meshing/meshtype.cpp      2011-06-03 17:54:39.000000000 +0400
 @@ -1,4 +1,5 @@
  #include <mystdlib.h>
-+#include <float.h>
++#include <float.h> // to get DBL_MIN defined
  
  #include "meshing.hpp"  
  
-@@ -650,7 +651,7 @@
+@@ -650,7 +651,8 @@
  
          double det = trans.Det();
  
 -        if (det <= 0)
-+        if (det <= DBL_MIN)
++        // if (det <= 0)
++        if (det <= DBL_MIN) // avoid FPE
            err += 1e12;
          else
            err += frob * frob / det;
-@@ -706,7 +707,7 @@
+@@ -706,7 +708,8 @@
  
              double det = trans(0,0)*trans(1,1)-trans(1,0)*trans(0,1);
  
 -            if (det <= 0)
-+            if (det <= DBL_MIN)
++            // if (det <= 0)
++            if (det <= DBL_MIN)  // avoid FPE
                {
                  dd = 0;
                  return 1e12;
-@@ -790,7 +791,7 @@
+@@ -790,7 +793,8 @@
            = dtrans(0,0) * trans(1,1) - trans(0,1) * dtrans(1,0)
            + trans(0,0) * dtrans(1,1) - dtrans(0,1) * trans(1,0);
  
 -        if (det <= 0)
-+        if (det <= DBL_MIN)
++        // if (det <= 0)
++        if (det <= DBL_MIN) // avoid FPE
            err += 1e12;
          else
            {
-@@ -840,7 +841,7 @@
+@@ -840,7 +844,8 @@
          frob /= 2;
  
          double det = trans.Det();
 -        if (det <= 0)
-+        if (det <= DBL_MIN)
++        //if (det <= 0)
++        if (det <= DBL_MIN) // avoid FPE
            err += 1e12;
          else
            err += frob * frob / det;
-@@ -1857,7 +1858,7 @@
+@@ -1857,7 +1862,8 @@
        case PYRAMID:
          {
            double noz = 1-p(2);
 -          if (noz == 0.0) noz = 1e-10;
-+          if (noz <= DBL_MIN) noz = 1e-10;
++          //if (noz == 0.0) noz = 1e-10;
++          if (noz <= DBL_MIN) noz = 1e-10; // avoid FPE
  
            double xi  = p(0) / noz;
            double eta = p(1) / noz;
-@@ -2035,7 +2036,7 @@
+@@ -2035,7 +2041,8 @@
  
          double det = -trans.Det();
        
 -        if (det <= 0)
-+        if (det <= DBL_MIN)
++        //if (det <= 0)
++        if (det <= DBL_MIN) // avoid FPE
            err += 1e12;
          else
            err += frob * frob * frob / det;
-@@ -2107,7 +2108,7 @@
+@@ -2107,7 +2114,8 @@
          ddet *= -1;
  
        
 -        if (det <= 0)
-+        if (det <= DBL_MIN)
++        //if (det <= 0)
++        if (det <= DBL_MIN) // avoid FPE
            err += 1e12;
          else
            {
-@@ -2189,7 +2190,7 @@
+@@ -2189,7 +2197,7 @@
        
          det *= -1;
        
@@ -79,40 +86,39 @@ diff -Naur netgen-4.9.13_orig/libsrc/meshing/meshtype.cpp netgen-4.9.13_new/libs
            err += 1e12;
          else
            {
-@@ -2522,10 +2523,10 @@
+@@ -2522,10 +2530,10 @@
  
    MeshingParameters :: MeshingParameters ()
    {
 -    optimize3d = "cmdmustm";
-+    optimize3d = (char*)"cmdmustm";
++    optimize3d = (char*)"cmdmustm"; // optimize3d = "cmdmustm";
      //optimize3d = "cmdmstm";
      optsteps3d = 3;
 -    optimize2d = "smsmsmSmSmSm";
-+    optimize2d = (char*)"smsmsmSmSmSm";
++    optimize2d = (char*)"smsmsmSmSmSm"; // optimize2d = "smsmsmSmSmSm";
      optsteps2d = 3;
      opterrpow = 2;
      blockfill = 1;
 diff -Naur netgen-4.9.13_orig/libsrc/meshing/meshtype.hpp netgen-4.9.13_new/libsrc/meshing/meshtype.hpp
 --- netgen-4.9.13_orig/libsrc/meshing/meshtype.hpp     2009-11-09 13:50:43.000000000 +0300
-+++ netgen-4.9.13_new/libsrc/meshing/meshtype.hpp      2011-02-18 11:24:03.000000000 +0300
-@@ -12,7 +12,7 @@
++++ netgen-4.9.13_new/libsrc/meshing/meshtype.hpp      2011-06-03 17:56:43.000000000 +0400
+@@ -12,6 +12,7 @@
      Classes for NETGEN
  */
  
--
-+class Mesh;
++class Mesh; // added due to compilation errors on some platforms
  enum ELEMENT_TYPE { 
    SEGMENT = 1, SEGMENT3 = 2,
-   TRIG = 10, QUAD=11, TRIG6 = 12, QUAD6 = 13, QUAD8 = 14,
 diff -Naur netgen-4.9.13_orig/libsrc/meshing/smoothing2.cpp netgen-4.9.13_new/libsrc/meshing/smoothing2.cpp
 --- netgen-4.9.13_orig/libsrc/meshing/smoothing2.cpp   2009-11-09 13:47:09.000000000 +0300
-+++ netgen-4.9.13_new/libsrc/meshing/smoothing2.cpp    2011-02-18 16:24:34.000000000 +0300
++++ netgen-4.9.13_new/libsrc/meshing/smoothing2.cpp    2011-06-03 18:04:35.000000000 +0400
 @@ -302,7 +302,8 @@
      vgrad = 0;
      badness = 0;
  
 -    meshthis -> GetNormalVector (surfi, sp1, gi1, n);
-+    //meshthis -> GetNormalVector (surfi, sp1, gi1, n);
++    //normal already computed: meshthis -> GetNormalVector (surfi, sp1, gi1, n);
 +    n = normal;
      pp1 = sp1 + x(0) * t1 + x(1) * t2;
  
@@ -122,7 +128,7 @@ diff -Naur netgen-4.9.13_orig/libsrc/meshing/smoothing2.cpp netgen-4.9.13_new/li
      badness = 0;
  
 -    meshthis -> GetNormalVector (surfi, sp1, gi1, n);
-+    //meshthis -> GetNormalVector (surfi, sp1, gi1, n);
++    //normal already computed: meshthis -> GetNormalVector (surfi, sp1, gi1, n);
 +    n = normal;
  
      pp1 = sp1 + x(0) * t1 + x(1) * t2;
@@ -132,7 +138,7 @@ diff -Naur netgen-4.9.13_orig/libsrc/meshing/smoothing2.cpp netgen-4.9.13_new/li
      badness = 0;
  
 -    meshthis -> GetNormalVector (surfi, sp1, gi1, n);
-+    //meshthis -> GetNormalVector (surfi, sp1, gi1, n);
++    //normal already computed: meshthis -> GetNormalVector (surfi, sp1, gi1, n);
 +    n = normal;
  
      pp1 = sp1 + x(0) * t1 + x(1) * t2;
@@ -142,7 +148,7 @@ diff -Naur netgen-4.9.13_orig/libsrc/meshing/smoothing2.cpp netgen-4.9.13_new/li
      badness = 0;
  
 -    meshthis -> GetNormalVector (surfi, sp1, gi1, n);
-+    //meshthis -> GetNormalVector (surfi, sp1, gi1, n);
++    //normal already computed: meshthis -> GetNormalVector (surfi, sp1, gi1, n);
 +    n = normal;
  
      // pp1 = sp1;
@@ -152,13 +158,13 @@ diff -Naur netgen-4.9.13_orig/libsrc/meshing/smoothing2.cpp netgen-4.9.13_new/li
                  mesh[pi] = Point<3> (origp);
                }
 -          
-+            break;
++            break; // exit as <fact> is not used anymore
            }
        }
  
 diff -Naur netgen-4.9.13_orig/libsrc/occ/occconstruction.cpp netgen-4.9.13_new/libsrc/occ/occconstruction.cpp
 --- netgen-4.9.13_orig/libsrc/occ/occconstruction.cpp  2009-08-24 06:32:47.000000000 +0400
-+++ netgen-4.9.13_new/libsrc/occ/occconstruction.cpp   2011-02-18 14:04:45.000000000 +0300
++++ netgen-4.9.13_new/libsrc/occ/occconstruction.cpp   2011-04-20 13:20:56.000000000 +0400
 @@ -28,7 +28,7 @@
  #include <BRepAlgoAPI_Common.hxx>
  #include <BRepAlgoAPI_Fuse.hxx>
@@ -170,52 +176,56 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occconstruction.cpp netgen-4.9.13_new/l
  #include <ShapeFix_Shape.hxx>
 diff -Naur netgen-4.9.13_orig/libsrc/occ/occgenmesh.cpp netgen-4.9.13_new/libsrc/occ/occgenmesh.cpp
 --- netgen-4.9.13_orig/libsrc/occ/occgenmesh.cpp       2010-03-16 09:30:07.000000000 +0300
-+++ netgen-4.9.13_new/libsrc/occ/occgenmesh.cpp        2011-02-18 17:06:25.000000000 +0300
-@@ -15,6 +15,7 @@
++++ netgen-4.9.13_new/libsrc/occ/occgenmesh.cpp        2011-06-28 12:04:32.000000000 +0400
+@@ -15,6 +15,8 @@
  
  #define DIVIDEEDGESECTIONS 1000
  #define IGNORECURVELENGTH 1e-4
++// a small value used to avoid FPE
 +#define VSMALL 1e-10
  
  
     bool merge_solids = 1;
-@@ -26,7 +27,7 @@
+@@ -26,7 +28,8 @@
        double nq = n*q;
  
        Point<3> p = p0 + 0.5*n;
 -      double lambda = (p-l.p0)*n / nq;
-+      double lambda = (fabs(nq) > 1e-10 ? (p-l.p0)*n / nq : -1);
++      // double lambda = (p-l.p0)*n / nq;  -- avoid FPE
++      double lambda = (fabs(nq) > 1e-10) ? (p-l.p0)*n / nq : -1;
  
        if (lambda >= 0 && lambda <= 1)
        {
-@@ -54,7 +55,7 @@
+@@ -54,6 +57,8 @@
  
  
  
--   double ComputeH (double kappa)
-+   static double ComputeH (double kappa)
++   
++   static // useless out of this file
+    double ComputeH (double kappa)
     {
        double hret;
-       kappa *= mparam.curvaturesafety;
-@@ -62,7 +63,7 @@
+@@ -62,7 +67,8 @@
        if (mparam.maxh * kappa < 1)
           hret = mparam.maxh;
        else
 -         hret = 1 / kappa;
-+         hret = 1 / (kappa + VSMALL);
++        // hret = 1 / kappa; -- avoid FPE
++        hret = 1 / (kappa + VSMALL);
  
        if (mparam.maxh < hret)
           hret = mparam.maxh;
-@@ -73,7 +74,7 @@
+@@ -71,8 +77,7 @@
+    }
  
  
--   void RestrictHTriangle (gp_Pnt2d & par0, gp_Pnt2d & par1, gp_Pnt2d & par2,
-+   static void RestrictHTriangle (gp_Pnt2d & par0, gp_Pnt2d & par1, gp_Pnt2d & par2,
+-
+-
++   static // useless out of this file
+    void RestrictHTriangle (gp_Pnt2d & par0, gp_Pnt2d & par1, gp_Pnt2d & par2,
                             BRepLProp_SLProps * prop, Mesh & mesh, int depth, double h = 0)
     {
-       int ls = -1;
-@@ -168,8 +169,8 @@
+@@ -168,8 +173,8 @@
           if(h < 1e-4*maxside)
              return;
  
@@ -226,176 +236,197 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occgenmesh.cpp netgen-4.9.13_new/libsrc
        }
  
        if (h < maxside && depth < 10)
-@@ -228,7 +229,7 @@
+@@ -228,6 +233,7 @@
  
  
  
--   void DivideEdge (TopoDS_Edge & edge, Array<MeshPoint> & ps,
-+   static void DivideEdge (TopoDS_Edge & edge, Array<MeshPoint> & ps,
++   static // useless out of this file
+    void DivideEdge (TopoDS_Edge & edge, Array<MeshPoint> & ps,
                      Array<double> & params, Mesh & mesh)
     {
-       double s0, s1;
-@@ -247,8 +248,8 @@
+@@ -247,8 +253,8 @@
        hvalue[0] = 0;
        pnt = c->Value(s0);
  
 -      double olddist = 0;
 -      double dist = 0;
-+      //double olddist = 0;
++      //double olddist = 0; -- useless variables
 +      //double dist = 0;
  
        int tmpVal = (int)(DIVIDEEDGESECTIONS);
  
-@@ -256,15 +257,15 @@
+@@ -256,15 +262,19 @@
        {
           oldpnt = pnt;
           pnt = c->Value(s0+(i/double(DIVIDEEDGESECTIONS))*(s1-s0));
--         hvalue[i] = hvalue[i-1] +
++         // -- no more than 1 segment per <edge length>/DIVIDEEDGESECTIONS
+          hvalue[i] = hvalue[i-1] +
 -            1.0/mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))*
 -            pnt.Distance(oldpnt);
-+         hvalue[i] = hvalue[i-1] + min( 1.0,
-+                                        1.0/mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))*
-+                                        pnt.Distance(oldpnt));
++         //   1.0/mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))*
++         //   pnt.Distance(oldpnt);
++           min( 1.0,
++                1.0/mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))*
++                pnt.Distance(oldpnt));
  
           //(*testout) << "mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z())) " << mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))
           //      <<  " pnt.Distance(oldpnt) " << pnt.Distance(oldpnt) << endl;
  
 -         olddist = dist;
 -         dist = pnt.Distance(oldpnt);
-+         //olddist = dist;
++         //olddist = dist; -- useless variables
 +         //dist = pnt.Distance(oldpnt);
        }
  
        //  nsubedges = int(ceil(hvalue[DIVIDEEDGESECTIONS]));
-@@ -279,7 +280,10 @@
+@@ -279,7 +289,10 @@
        {
           if (hvalue[i1]/hvalue[DIVIDEEDGESECTIONS]*nsubedges >= i)
           {
 -            params[i] = s0+(i1/double(DIVIDEEDGESECTIONS))*(s1-s0);
-+            // For nsubedges comparable to DIVIDEEDGESECTIONS (SALOME issue 0021073)
++            // -- for nsubedges comparable to DIVIDEEDGESECTIONS
 +            //params[i] = s0+(i1/double(DIVIDEEDGESECTIONS))*(s1-s0);
 +            double d1 = i1 - (hvalue[i1] - i*hvalue[DIVIDEEDGESECTIONS]/nsubedges)/(hvalue[i1]-hvalue[i1-1]);
 +            params[i] = s0+(d1/double(DIVIDEEDGESECTIONS))*(s1-s0);
              pnt = c->Value(params[i]);
              ps[i-1] = MeshPoint (Point3d(pnt.X(), pnt.Y(), pnt.Z()));
              i++;
-@@ -323,6 +327,7 @@
+@@ -323,6 +336,7 @@
        (*testout) << "nedges = " << nedges << endl;
  
        double eps = 1e-6 * geom.GetBoundingBox().Diam();
-+      const double eps2 = eps * eps;
++      const double eps2 = eps * eps; // -- small optimization
  
        for (int i = 1; i <= nvertices; i++)
        {
-@@ -332,7 +337,7 @@
+@@ -332,7 +346,8 @@
           bool exists = 0;
           if (merge_solids)
              for (PointIndex pi = 1; pi <= mesh.GetNP(); pi++)
 -               if ( Dist2 (mesh[pi], Point<3>(mp)) < eps*eps)
-+               if ( Dist2 (mesh[pi], Point<3>(mp)) < eps2 )
++               //if ( Dist2 (mesh[pi], Point<3>(mp)) < eps*eps)              
++               if ( Dist2 (mesh[pi], Point<3>(mp)) < eps2 ) // -- small optimization
                 {
                    exists = 1;
                    break;
-@@ -362,6 +367,7 @@
+@@ -362,6 +377,7 @@
           {
              TopoDS_Face face = TopoDS::Face(exp1.Current());
              int facenr = geom.fmap.FindIndex(face);
-+            if ( facenr < 1 ) continue; // support of SALOME sub-meshes
++            if ( facenr < 1 ) continue; // -- to support SALOME sub-meshes
  
              if (face2solid[0][facenr-1] == 0)
                 face2solid[0][facenr-1] = solidnr;
-@@ -381,6 +387,7 @@
+@@ -381,6 +397,7 @@
        int facenr = 0;
        int edgenr = 0;
  
-+      edgenr = mesh.GetNSeg(); // support of SALOME sub-meshes
++      edgenr = mesh.GetNSeg(); // to support SALOME sub-meshes
  
        (*testout) << "faces = " << geom.fmap.Extent() << endl;
        int curr = 0;
-@@ -442,6 +449,8 @@
+@@ -442,6 +459,7 @@
                    //(*testout) << "ignoring degenerated edge" << endl;
                    continue;
                 }
-+               if ( geom.emap.FindIndex(edge) < 1 )
-+                 continue; // support SALOME sub-meshes
++               if ( geom.emap.FindIndex(edge) < 1 ) continue; // to support SALOME sub-meshes
  
                 if (geom.vmap.FindIndex(TopExp::FirstVertex (edge)) ==
                    geom.vmap.FindIndex(TopExp::LastVertex (edge)))
-@@ -479,6 +488,8 @@
-                }
-                else
+@@ -481,13 +499,45 @@
                 {
-+                  const double tol = std::max( BRep_Tool::Tolerance( TopExp::FirstVertex (edge)),
-+                                               BRep_Tool::Tolerance( TopExp::LastVertex (edge)));
                    Point<3> fp = occ2ng (BRep_Tool::Pnt (TopExp::FirstVertex (edge)));
                    Point<3> lp = occ2ng (BRep_Tool::Pnt (TopExp::LastVertex (edge)));
++                  double tol2 = std::min( eps*eps, 1e-6 * Dist2( fp, lp ));
  
-@@ -486,8 +497,8 @@
+                   pnums[0] = -1;
                    pnums.Last() = -1;
                    for (PointIndex pi = 1; pi < first_ep; pi++)
                    {
 -                     if (Dist2 (mesh[pi], fp) < eps*eps) pnums[0] = pi;
 -                     if (Dist2 (mesh[pi], lp) < eps*eps) pnums.Last() = pi;
-+                     if (Dist2 (mesh[pi], fp) < 2*tol*tol) pnums[0] = pi;
-+                     if (Dist2 (mesh[pi], lp) < 2*tol*tol) pnums.Last() = pi;
++                    if (Dist2 (mesh[pi], fp) < tol2) pnums[0] = pi;
++                    if (Dist2 (mesh[pi], lp) < tol2) pnums.Last() = pi;
++                  }
++                  if ( pnums[0] == pnums.Last() )
++                    pnums[0] = -1;
++                  if ( pnums[0] == -1 || pnums.Last() == -1 )
++                  {
++                    // take into account a possible large gap between a vertex and an edge curve
++                    // and a large vertex tolerance covering the whole edge
++                    if ( pnums[0] == -1 )
++                    {
++                      double tol = BRep_Tool::Tolerance( TopExp::FirstVertex (edge));
++                      for (PointIndex pi = 1; pi < first_ep; pi++)
++                        if (pi != pnums.Last() && Dist2 (mesh[pi], fp) < 2*tol*tol)
++                          pnums[0] = pi;
++
++                      if ( pnums[0] == -1 )
++                        pnums[0] = geom.vmap.FindIndex (TopExp::FirstVertex (edge));
++                    }
++                    if ( pnums.Last() == -1 )
++                    {
++                      double tol = BRep_Tool::Tolerance( TopExp::LastVertex (edge));
++                      for (PointIndex pi = 1; pi < first_ep; pi++)
++                        if (pi != pnums[0] && Dist2 (mesh[pi], lp) < 2*tol*tol)
++                          pnums.Last() = pi;
++
++                      if ( pnums.Last() == -1 )
++                        pnums.Last() = geom.vmap.FindIndex (TopExp::LastVertex (edge));
++                    }
++
++                    if ( Dist2( fp, mesh[PointIndex(pnums[0])]) >
++                         Dist2( lp, mesh[PointIndex(pnums.Last())]))
++                      std::swap( pnums[0], pnums.Last() );
                    }
                 }
  
-@@ -633,7 +644,7 @@
+@@ -633,7 +683,8 @@
           }
  
           (*testout) << "mesh face " << k << endl;
 -         multithread.percent = 100 * k / (mesh.GetNFD()+1e-10);
++         // multithread.percent = 100 * k / (mesh.GetNFD()+1e-10); -- avoid FPE
 +         multithread.percent = 100 * k / (mesh.GetNFD() + VSMALL);
           geom.facemeshstatus[k-1] = -1;
  
  
-@@ -901,7 +912,7 @@
+@@ -901,7 +952,8 @@
           //      if (k != 36) continue;
  
           //      (*testout) << "optimize face " << k << endl;
 -         multithread.percent = 100 * k / (mesh.GetNFD()+1e-10);
++         //multithread.percent = 100 * k / (mesh.GetNFD()+1e-10); -- avoid FPE
 +         multithread.percent = 100 * k / (mesh.GetNFD() + VSMALL);
  
           FaceDescriptor & fd = mesh.GetFaceDescriptor(k);
  
-@@ -1229,7 +1240,7 @@
-                   mindist = min (mindist, line.Dist(lines[num]));
-                }
--               mindist *= occparam.resthcloseedgefac;
-+               mindist /= (occparam.resthcloseedgefac + VSMALL);
-                if (mindist < 1e-3)
-                {
-@@ -1456,3 +1467,4 @@
+@@ -1456,3 +1508,4 @@
  }
  
  #endif
 +
 diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.cpp netgen-4.9.13_new/libsrc/occ/occgeom.cpp
 --- netgen-4.9.13_orig/libsrc/occ/occgeom.cpp  2010-03-05 16:16:21.000000000 +0300
-+++ netgen-4.9.13_new/libsrc/occ/occgeom.cpp   2011-02-18 15:34:01.000000000 +0300
-@@ -7,7 +7,8 @@
- #include "ShapeAnalysis_ShapeContents.hxx"
++++ netgen-4.9.13_new/libsrc/occ/occgeom.cpp   2011-06-03 19:32:06.000000000 +0400
+@@ -8,6 +8,8 @@
  #include "ShapeAnalysis_CheckSmallFace.hxx"
  #include "ShapeAnalysis_DataMapOfShapeListOfReal.hxx"
--#include "ShapeAnalysis_Surface.hxx"
-+#include <ShapeAnalysis_Surface.hxx>
-+#include <BRepTopAdaptor_FClass2d.hxx>
+ #include "ShapeAnalysis_Surface.hxx"
++#include <BRepTopAdaptor_FClass2d.hxx> // -- to optimize Project() and FastProject()
++#include <TopAbs_State.hxx>
  #include "BRepAlgoAPI_Fuse.hxx"
  #include "BRepCheck_Analyzer.hxx"
  #include "BRepLib.hxx"
-@@ -15,11 +16,18 @@
- #include "ShapeFix.hxx"
+@@ -16,10 +18,17 @@
  #include "ShapeFix_FixSmallFace.hxx"
  #include "Partition_Spliter.hxx"
--
-+#include <TopAbs_State.hxx>
  
+-
  namespace netgen
  {
 -   void OCCGeometry :: PrintNrShapes ()
++  // free data used to optimize Project() and FastProject()
 +  OCCGeometry::~OCCGeometry()
 +  {
 +    NCollection_DataMap<int,BRepTopAdaptor_FClass2d*>::Iterator it(fclsmap);
@@ -407,10 +438,11 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.cpp netgen-4.9.13_new/libsrc/oc
     {
        TopExp_Explorer e;
        int count = 0;
-@@ -952,24 +960,47 @@
+@@ -951,25 +960,58 @@
+    }
  
  
++   // returns a projector and a classifier for the given surface
 +   void OCCGeometry::GetFaceTools(int surfi, Handle(ShapeAnalysis_Surface)& proj,
 +                                  BRepTopAdaptor_FClass2d*& cls) const
 +   {
@@ -431,14 +463,16 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.cpp netgen-4.9.13_new/libsrc/oc
 +     }
 +   }
  
+-
 -   void OCCGeometry :: Project (int surfi, Point<3> & p) const
++   // void OCCGeometry :: Project (int surfi, Point<3> & p) const
 +   bool OCCGeometry :: Project (int surfi, Point<3> & p, double& u, double& v) const
     {
        static int cnt = 0;
        if (++cnt % 1000 == 0) cout << "Project cnt = " << cnt << endl;
  
        gp_Pnt pnt(p(0), p(1), p(2));
--
 -      double u,v;
 -      Handle( Geom_Surface ) thesurf = BRep_Tool::Surface(TopoDS::Face(fmap(surfi)));
 -      Handle( ShapeAnalysis_Surface ) su = new ShapeAnalysis_Surface( thesurf );
@@ -447,7 +481,14 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.cpp netgen-4.9.13_new/libsrc/oc
 -      pnt = thesurf->Value( u, v );
 -
 -
-+  
++      // -- Optimization: use cached projector and classifier
++      // double u,v;
++      // Handle( Geom_Surface ) thesurf = BRep_Tool::Surface(TopoDS::Face(fmap(surfi)));
++      // Handle( ShapeAnalysis_Surface ) su = new ShapeAnalysis_Surface( thesurf );
++      // gp_Pnt2d suval = su->ValueOfUV ( pnt, BRep_Tool::Tolerance( TopoDS::Face(fmap(surfi)) ) );
++      // suval.Coord( u, v);
++      // pnt = thesurf->Value( u, v );  
++
 +      Handle(ShapeAnalysis_Surface) proj;
 +      BRepTopAdaptor_FClass2d *cls;
 +      GetFaceTools(surfi, proj, cls);
@@ -461,12 +502,12 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.cpp netgen-4.9.13_new/libsrc/oc
 +      p2d.Coord(u, v);
 +  
        p = Point<3> (pnt.X(), pnt.Y(), pnt.Z());
--
 +      return true;
     }
  
  
-@@ -979,54 +1010,20 @@
+@@ -979,54 +1021,69 @@
     {
        gp_Pnt p(ap(0), ap(1), ap(2));
  
@@ -518,6 +559,55 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.cpp netgen-4.9.13_new/libsrc/oc
 -      if (count == 50) return false;
 -
 -      ap = Point<3> (x.X(), x.Y(), x.Z());
++      // -- Optimization: use cached projector and classifier
++      // Handle(Geom_Surface) surface = BRep_Tool::Surface(TopoDS::Face(fmap(surfi)));
++      // 
++      // gp_Pnt x = surface->Value (u,v);
++      // 
++      // if (p.SquareDistance(x) <= sqr(PROJECTION_TOLERANCE)) return true;
++      // 
++      // gp_Vec du, dv;
++      // 
++      // surface->D1(u,v,x,du,dv);
++      // 
++      // int count = 0;
++      // 
++      // gp_Pnt xold;
++      // gp_Vec n;
++      // double det, lambda, mu;
++      // 
++      // do {
++      //    count++;
++      // 
++      //    n = du^dv;
++      // 
++      //    det = Det3 (n.X(), du.X(), dv.X(),
++      //       n.Y(), du.Y(), dv.Y(),
++      //       n.Z(), du.Z(), dv.Z());
++      // 
++      //    if (det < 1e-15) return false;
++      // 
++      //    lambda = Det3 (n.X(), p.X()-x.X(), dv.X(),
++      //       n.Y(), p.Y()-x.Y(), dv.Y(),
++      //       n.Z(), p.Z()-x.Z(), dv.Z())/det;
++      // 
++      //    mu     = Det3 (n.X(), du.X(), p.X()-x.X(),
++      //       n.Y(), du.Y(), p.Y()-x.Y(),
++      //       n.Z(), du.Z(), p.Z()-x.Z())/det;
++      // 
++      //    u += lambda;
++      //    v += mu;
++      // 
++      //    xold = x;
++      //    surface->D1(u,v,x,du,dv);
++      // 
++      // } while (xold.SquareDistance(x) > sqr(PROJECTION_TOLERANCE) && count < 50);
++      // 
++      // //    (*testout) << "FastProject count: " << count << endl;
++      // 
++      // if (count == 50) return false;
++      // 
++      // ap = Point<3> (x.X(), x.Y(), x.Z());
 +      Handle(ShapeAnalysis_Surface) proj;
 +      BRepTopAdaptor_FClass2d *cls;
 +      GetFaceTools(surfi, proj, cls);
@@ -537,26 +627,30 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.cpp netgen-4.9.13_new/libsrc/oc
     }
 diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.hpp netgen-4.9.13_new/libsrc/occ/occgeom.hpp
 --- netgen-4.9.13_orig/libsrc/occ/occgeom.hpp  2010-01-14 19:56:19.000000000 +0300
-+++ netgen-4.9.13_new/libsrc/occ/occgeom.hpp   2011-02-18 15:33:10.000000000 +0300
-@@ -15,8 +15,6 @@
++++ netgen-4.9.13_new/libsrc/occ/occgeom.hpp   2011-06-28 11:07:52.000000000 +0400
+@@ -15,8 +15,8 @@
  #include "Geom_Curve.hxx"
  #include "Geom2d_Curve.hxx"
  #include "Geom_Surface.hxx"
 -#include "GeomAPI_ProjectPointOnSurf.hxx"
 -#include "GeomAPI_ProjectPointOnCurve.hxx"
++// #include "GeomAPI_ProjectPointOnSurf.hxx"
++// #include "GeomAPI_ProjectPointOnCurve.hxx"
  #include "BRepTools.hxx"
  #include "TopExp.hxx"
  #include "BRepBuilderAPI_MakeVertex.hxx"
-@@ -42,8 +40,6 @@
+@@ -42,8 +42,8 @@
  #include "Geom_Curve.hxx"
  #include "Geom2d_Curve.hxx"
  #include "Geom_Surface.hxx"
 -#include "GeomAPI_ProjectPointOnSurf.hxx"
 -#include "GeomAPI_ProjectPointOnCurve.hxx"
++// #include "GeomAPI_ProjectPointOnSurf.hxx"
++// #include "GeomAPI_ProjectPointOnCurve.hxx"
  #include "TopoDS_Wire.hxx"
  #include "BRepTools_WireExplorer.hxx"
  #include "BRepTools.hxx"
-@@ -68,7 +64,7 @@
+@@ -68,7 +68,7 @@
  #include "IGESToBRep_Reader.hxx"
  #include "Interface_Static.hxx"
  #include "GeomAPI_ExtremaCurveCurve.hxx"
@@ -565,43 +659,46 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.hpp netgen-4.9.13_new/libsrc/oc
  #include "Standard_Failure.hxx"
  #include "ShapeUpgrade_ShellSewing.hxx"
  #include "ShapeFix_Shape.hxx"
-@@ -80,6 +76,10 @@
+@@ -80,6 +80,10 @@
  #include "ShapeAnalysis.hxx"
  #include "ShapeBuild_ReShape.hxx"
  
++// -- Optimization: to use cached projector and classifier
 +#include <NCollection_DataMap.hxx>
 +class Handle_ShapeAnalysis_Surface;
 +class BRepTopAdaptor_FClass2d;
-+
  
  // Philippose - 29/01/2009
  // OpenCascade XDE Support
-@@ -190,6 +190,8 @@
+@@ -190,6 +194,9 @@
     class OCCGeometry : public NetgenGeometry
     {
        Point<3> center;
++      // -- Optimization: to use cached projector and classifier
 +      mutable NCollection_DataMap<int,Handle_ShapeAnalysis_Surface> fprjmap;
 +      mutable NCollection_DataMap<int,BRepTopAdaptor_FClass2d*> fclsmap;
  
     public:
        TopoDS_Shape shape;
-@@ -241,6 +243,8 @@
+@@ -241,6 +248,8 @@
           vmap.Clear();
        }
  
-+      ~OCCGeometry();
++      ~OCCGeometry();      // -- to free cached projector and classifier
 +
        void BuildFMap();
  
        Box<3> GetBoundingBox()
-@@ -260,9 +264,12 @@
+@@ -260,9 +269,14 @@
        Point<3> Center()
        {  return center;}
  
 -      void Project (int surfi, Point<3> & p) const;
++      // void Project (int surfi, Point<3> & p) const; -- optimization
 +      bool Project (int surfi, Point<3> & p, double& u, double& v) const;
        bool FastProject (int surfi, Point<3> & ap, double& u, double& v) const;
  
++      // -- Optimization: to use cached projector and classifier
 +      void GetFaceTools(int surfi, Handle(ShapeAnalysis_Surface)& proj,
 +                        BRepTopAdaptor_FClass2d*& cls) const;
 +
@@ -610,20 +707,21 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.hpp netgen-4.9.13_new/libsrc/oc
           cout << "OCCGeometry::GetSurface using PLANESPACE" << endl;
 diff -Naur netgen-4.9.13_orig/libsrc/occ/occmeshsurf.cpp netgen-4.9.13_new/libsrc/occ/occmeshsurf.cpp
 --- netgen-4.9.13_orig/libsrc/occ/occmeshsurf.cpp      2009-08-24 06:32:47.000000000 +0400
-+++ netgen-4.9.13_new/libsrc/occ/occmeshsurf.cpp       2011-02-18 16:27:39.000000000 +0300
++++ netgen-4.9.13_new/libsrc/occ/occmeshsurf.cpp       2011-06-03 19:55:14.000000000 +0400
 @@ -6,6 +6,7 @@
  #include <meshing.hpp>
  #include <GeomLProp_SLProps.hxx>
  #include <ShapeAnalysis_Surface.hxx>
-+#include <GeomAPI_ProjectPointOnCurve.hxx>
++#include <GeomAPI_ProjectPointOnCurve.hxx> // -- moved here from occgeom.hpp
  
  
  namespace netgen
-@@ -434,23 +435,21 @@
+@@ -434,23 +435,33 @@
  
    void MeshOptimize2dOCCSurfaces :: ProjectPoint (INDEX surfind, Point<3> & p) const
    {
 -    geometry.Project (surfind, p);
++    // geometry.Project (surfind, p); -- signature of Project() changed for optimization
 +    double u, v;
 +    geometry.Project (surfind, p, u, v);
    }
@@ -633,7 +731,9 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occmeshsurf.cpp netgen-4.9.13_new/libsr
    {
 -    double u = gi.u;
 -    double v = gi.v;
--
++    //double u = gi.u;
++    //double v = gi.v;
      Point<3> hp = p;
 -    if (geometry.FastProject (surfind, hp, u, v))
 -      {
@@ -642,6 +742,14 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occmeshsurf.cpp netgen-4.9.13_new/libsr
 -      }
 -    ProjectPoint (surfind, p); 
 -    return CalcPointGeomInfo (surfind, gi, p); 
++    // -- u and v are computed by FastProject() and Project(), no need to call CalcPointGeomInfo()
++    // if (geometry.FastProject (surfind, hp, u, v))
++    //   {
++    //    p = hp;
++    //    return 1;
++    //   }
++    // ProjectPoint (surfind, p); 
++    // return CalcPointGeomInfo (surfind, gi, p); 
 +    bool ok;
 +    if (gi.trignum > 0)
 +      ok = geometry.FastProject (surfind, hp, gi.u, gi.v);
@@ -652,16 +760,26 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occmeshsurf.cpp netgen-4.9.13_new/libsr
    }
  
  
-@@ -680,7 +679,7 @@
+@@ -680,7 +691,8 @@
        if (!geometry.FastProject (surfi, hnewp, u, v))
          {
          //  cout << "Fast projection to surface fails! Using OCC projection" << endl;
 -          geometry.Project (surfi, hnewp);
++          // geometry.Project (surfi, hnewp); -- Project() changed for optimization
 +          geometry.Project (surfi, hnewp, u, v);
          }
  
        newgi.trignum = 1;
-@@ -708,14 +707,17 @@
+@@ -689,7 +701,7 @@
+       }
+   
+     newp = hnewp;
+-  }
++  }//; -- to compile with -Wall -pedantic
+   void OCCRefinementSurfaces :: 
+@@ -708,14 +720,18 @@
      hnewp = Point<3> (pnt.X(), pnt.Y(), pnt.Z());
      newp = hnewp;
      newgi = ap1;
@@ -674,15 +792,16 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occmeshsurf.cpp netgen-4.9.13_new/libsr
      if (surfi > 0)
 -      geometry.Project (surfi, p);
 -  };
++      // geometry.Project (surfi, p); -- Project() changed for optimization
 +    {
 +      double u, v;
 +      geometry.Project (surfi, p, u, v);
 +    }
-+  }
++  }//; -- to compile with -Wall -pedantic
  
    void OCCRefinementSurfaces :: ProjectToSurface (Point<3> & p, int surfi, PointGeomInfo & gi)
    {
-@@ -723,9 +725,10 @@
+@@ -723,9 +739,10 @@
        if (!geometry.FastProject (surfi, p, gi.u, gi.v))
        {
          cout << "Fast projection to surface fails! Using OCC projection" << endl;
@@ -697,7 +816,7 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/occmeshsurf.cpp netgen-4.9.13_new/libsr
  
 diff -Naur netgen-4.9.13_orig/libsrc/occ/utilities.h netgen-4.9.13_new/libsrc/occ/utilities.h
 --- netgen-4.9.13_orig/libsrc/occ/utilities.h  2009-08-24 06:12:24.000000000 +0400
-+++ netgen-4.9.13_new/libsrc/occ/utilities.h   2011-02-18 11:24:03.000000000 +0300
++++ netgen-4.9.13_new/libsrc/occ/utilities.h   2011-04-20 13:20:56.000000000 +0400
 @@ -33,6 +33,7 @@
  
  #include <string>
@@ -708,30 +827,33 @@ diff -Naur netgen-4.9.13_orig/libsrc/occ/utilities.h netgen-4.9.13_new/libsrc/oc
  
 diff -Naur netgen-4.9.13_orig/libsrc/stlgeom/stlgeommesh.cpp netgen-4.9.13_new/libsrc/stlgeom/stlgeommesh.cpp
 --- netgen-4.9.13_orig/libsrc/stlgeom/stlgeommesh.cpp  2009-08-10 15:40:51.000000000 +0400
-+++ netgen-4.9.13_new/libsrc/stlgeom/stlgeommesh.cpp   2011-02-18 11:24:03.000000000 +0300
-@@ -1435,7 +1435,7 @@
++++ netgen-4.9.13_new/libsrc/stlgeom/stlgeommesh.cpp   2011-06-03 19:57:16.000000000 +0400
+@@ -1435,7 +1435,8 @@
  
          if (!optstring || strlen(optstring) == 0)
            {
 -            mparam.optimize2d = "smcm";
-+            mparam.optimize2d = (char*)"smcm";
++            //mparam.optimize2d = (char*)"smcm";
++              mparam.optimize2d = (char*)"smcm";
            }
          else
            {
-@@ -1451,7 +1451,7 @@
+@@ -1451,7 +1452,8 @@
                                 mparam.grading);
              mesh -> LoadLocalMeshSize (mparam.meshsizefilename);            
              mesh -> CalcLocalHFromSurfaceCurvature (stlparam.resthsurfmeshcurvfac);
 -            mparam.optimize2d = "cmsmSm";
-+            mparam.optimize2d = (char*)"cmsmSm";
++            //mparam.optimize2d = (char*)"cmsmSm";
++              mparam.optimize2d = (char*)"cmsmSm";
              STLSurfaceOptimization (*stlgeometry, *mesh, mparam);
  #ifdef STAT_STREAM
              (*statout) << GetTime() << " & ";
-@@ -1557,7 +1557,7 @@
+@@ -1557,7 +1559,8 @@
  
          if (!optstring || strlen(optstring) == 0)
            {
 -            mparam.optimize3d = "cmdmstm";
++              //mparam.optimize3d = "cmdmstm";
 +            mparam.optimize3d = (char*)"cmdmstm";
            }
          else