Salome HOME
Merge from V6_main_20120808 08Aug12
[plugins/netgenplugin.git] / src / NETGEN / netgen49ForSalome.patch
1 diff -Naur netgen-4.9.13_orig/libsrc/meshing/meshtype.cpp netgen-4.9.13_new/libsrc/meshing/meshtype.cpp
2 --- netgen-4.9.13_orig/libsrc/meshing/meshtype.cpp      2009-09-13 14:28:38.000000000 +0400
3 +++ netgen-4.9.13_new/libsrc/meshing/meshtype.cpp       2011-12-20 14:50:26.000000000 +0400
4 @@ -1,4 +1,5 @@
5  #include <mystdlib.h>
6 +#include <float.h> // to get DBL_MIN defined
7  
8  #include "meshing.hpp"  
9  
10 @@ -650,7 +651,8 @@
11  
12          double det = trans.Det();
13  
14 -        if (det <= 0)
15 +        // if (det <= 0)
16 +        if (det <= DBL_MIN) // avoid FPE
17            err += 1e12;
18          else
19            err += frob * frob / det;
20 @@ -706,7 +708,8 @@
21  
22              double det = trans(0,0)*trans(1,1)-trans(1,0)*trans(0,1);
23  
24 -            if (det <= 0)
25 +            // if (det <= 0)
26 +            if (det <= DBL_MIN)  // avoid FPE
27                {
28                  dd = 0;
29                  return 1e12;
30 @@ -790,7 +793,8 @@
31            = dtrans(0,0) * trans(1,1) - trans(0,1) * dtrans(1,0)
32            + trans(0,0) * dtrans(1,1) - dtrans(0,1) * trans(1,0);
33  
34 -        if (det <= 0)
35 +        // if (det <= 0)
36 +        if (det <= DBL_MIN) // avoid FPE
37            err += 1e12;
38          else
39            {
40 @@ -840,7 +844,8 @@
41          frob /= 2;
42  
43          double det = trans.Det();
44 -        if (det <= 0)
45 +        //if (det <= 0)
46 +        if (det <= DBL_MIN) // avoid FPE
47            err += 1e12;
48          else
49            err += frob * frob / det;
50 @@ -1857,7 +1862,8 @@
51        case PYRAMID:
52          {
53            double noz = 1-p(2);
54 -          if (noz == 0.0) noz = 1e-10;
55 +          //if (noz == 0.0) noz = 1e-10;
56 +          if (noz <= DBL_MIN) noz = 1e-10; // avoid FPE
57  
58            double xi  = p(0) / noz;
59            double eta = p(1) / noz;
60 @@ -2035,7 +2041,8 @@
61  
62          double det = -trans.Det();
63        
64 -        if (det <= 0)
65 +        //if (det <= 0)
66 +        if (det <= DBL_MIN) // avoid FPE
67            err += 1e12;
68          else
69            err += frob * frob * frob / det;
70 @@ -2107,7 +2114,8 @@
71          ddet *= -1;
72  
73        
74 -        if (det <= 0)
75 +        //if (det <= 0)
76 +        if (det <= DBL_MIN) // avoid FPE
77            err += 1e12;
78          else
79            {
80 @@ -2189,7 +2197,7 @@
81        
82          det *= -1;
83        
84 -        if (det <= 0)
85 +        if (det <= DBL_MIN)
86            err += 1e12;
87          else
88            {
89 @@ -2522,10 +2530,10 @@
90  
91    MeshingParameters :: MeshingParameters ()
92    {
93 -    optimize3d = "cmdmustm";
94 +    optimize3d = (char*)"cmdmustm"; // optimize3d = "cmdmustm";
95      //optimize3d = "cmdmstm";
96      optsteps3d = 3;
97 -    optimize2d = "smsmsmSmSmSm";
98 +    optimize2d = (char*)"smsmsmSmSmSm"; // optimize2d = "smsmsmSmSmSm";
99      optsteps2d = 3;
100      opterrpow = 2;
101      blockfill = 1;
102 diff -Naur netgen-4.9.13_orig/libsrc/meshing/meshtype.hpp netgen-4.9.13_new/libsrc/meshing/meshtype.hpp
103 --- netgen-4.9.13_orig/libsrc/meshing/meshtype.hpp      2009-11-09 13:50:43.000000000 +0300
104 +++ netgen-4.9.13_new/libsrc/meshing/meshtype.hpp       2011-12-20 14:50:26.000000000 +0400
105 @@ -12,6 +12,7 @@
106      Classes for NETGEN
107  */
108  
109 +class Mesh; // added due to compilation errors on some platforms
110  
111  enum ELEMENT_TYPE { 
112    SEGMENT = 1, SEGMENT3 = 2,
113 diff -Naur netgen-4.9.13_orig/libsrc/meshing/smoothing2.cpp netgen-4.9.13_new/libsrc/meshing/smoothing2.cpp
114 --- netgen-4.9.13_orig/libsrc/meshing/smoothing2.cpp    2009-11-09 13:47:09.000000000 +0300
115 +++ netgen-4.9.13_new/libsrc/meshing/smoothing2.cpp     2011-12-20 14:50:26.000000000 +0400
116 @@ -302,7 +302,8 @@
117      vgrad = 0;
118      badness = 0;
119  
120 -    meshthis -> GetNormalVector (surfi, sp1, gi1, n);
121 +    //normal already computed: meshthis -> GetNormalVector (surfi, sp1, gi1, n);
122 +    n = normal;
123      pp1 = sp1 + x(0) * t1 + x(1) * t2;
124  
125      //  meshthis -> ProjectPoint (surfi, pp1);
126 @@ -360,7 +361,8 @@
127      vgrad = 0;
128      badness = 0;
129  
130 -    meshthis -> GetNormalVector (surfi, sp1, gi1, n);
131 +    //normal already computed: meshthis -> GetNormalVector (surfi, sp1, gi1, n);
132 +    n = normal;
133  
134      pp1 = sp1 + x(0) * t1 + x(1) * t2;
135  
136 @@ -514,7 +516,8 @@
137      vgrad = 0;
138      badness = 0;
139  
140 -    meshthis -> GetNormalVector (surfi, sp1, gi1, n);
141 +    //normal already computed: meshthis -> GetNormalVector (surfi, sp1, gi1, n);
142 +    n = normal;
143  
144      pp1 = sp1 + x(0) * t1 + x(1) * t2;
145  
146 @@ -586,7 +589,8 @@
147      vgrad = 0;
148      badness = 0;
149  
150 -    meshthis -> GetNormalVector (surfi, sp1, gi1, n);
151 +    //normal already computed: meshthis -> GetNormalVector (surfi, sp1, gi1, n);
152 +    n = normal;
153  
154      // pp1 = sp1;
155      //    pp1.Add2 (x.Get(1), t1, x.Get(2), t2);
156 @@ -973,7 +977,7 @@
157                 {
158                   mesh[pi] = Point<3> (origp);
159                 }
160 -           
161 +             break; // exit as <fact> is not used anymore
162             }
163         }
164  
165 diff -Naur netgen-4.9.13_orig/libsrc/occ/occconstruction.cpp netgen-4.9.13_new/libsrc/occ/occconstruction.cpp
166 --- netgen-4.9.13_orig/libsrc/occ/occconstruction.cpp   2009-08-24 06:32:47.000000000 +0400
167 +++ netgen-4.9.13_new/libsrc/occ/occconstruction.cpp    2011-12-20 14:50:26.000000000 +0400
168 @@ -28,7 +28,7 @@
169  #include <BRepAlgoAPI_Common.hxx>
170  #include <BRepAlgoAPI_Fuse.hxx>
171  #include <BRepAlgoAPI_Section.hxx>
172 -#include <BRepOffsetAPI_Sewing.hxx>
173 +//#include <BRepOffsetAPI_Sewing.hxx>
174  //#include <BRepAlgo_Sewing.hxx>
175  #include <BRepOffsetAPI_MakeOffsetShape.hxx>
176  #include <ShapeFix_Shape.hxx>
177 diff -Naur netgen-4.9.13_orig/libsrc/occ/occgenmesh.cpp netgen-4.9.13_new/libsrc/occ/occgenmesh.cpp
178 --- netgen-4.9.13_orig/libsrc/occ/occgenmesh.cpp        2010-03-16 09:30:07.000000000 +0300
179 +++ netgen-4.9.13_new/libsrc/occ/occgenmesh.cpp 2011-12-20 14:50:26.000000000 +0400
180 @@ -15,6 +15,8 @@
181  
182  #define DIVIDEEDGESECTIONS 1000
183  #define IGNORECURVELENGTH 1e-4
184 +// a small value used to avoid FPE
185 +#define VSMALL 1e-10
186  
187  
188     bool merge_solids = 1;
189 @@ -26,7 +28,8 @@
190        double nq = n*q;
191  
192        Point<3> p = p0 + 0.5*n;
193 -      double lambda = (p-l.p0)*n / nq;
194 +      // double lambda = (p-l.p0)*n / nq;  -- avoid FPE
195 +      double lambda = (fabs(nq) > 1e-10) ? (p-l.p0)*n / nq : -1;
196  
197        if (lambda >= 0 && lambda <= 1)
198        {
199 @@ -54,6 +57,8 @@
200  
201  
202  
203 +   
204 +   static // useless out of this file
205     double ComputeH (double kappa)
206     {
207        double hret;
208 @@ -62,7 +67,8 @@
209        if (mparam.maxh * kappa < 1)
210           hret = mparam.maxh;
211        else
212 -         hret = 1 / kappa;
213 +        // hret = 1 / kappa; -- avoid FPE
214 +        hret = 1 / (kappa + VSMALL);
215  
216        if (mparam.maxh < hret)
217           hret = mparam.maxh;
218 @@ -71,8 +77,7 @@
219     }
220  
221  
222 -
223 -
224 +   static // useless out of this file
225     void RestrictHTriangle (gp_Pnt2d & par0, gp_Pnt2d & par1, gp_Pnt2d & par2,
226                             BRepLProp_SLProps * prop, Mesh & mesh, int depth, double h = 0)
227     {
228 @@ -168,8 +173,8 @@
229           if(h < 1e-4*maxside)
230              return;
231  
232 -
233 -         if (h > 30) return;
234 +         // commented to restrict H on a large sphere for example
235 +         //if (h > 30) return;
236        }
237  
238        if (h < maxside && depth < 10)
239 @@ -228,6 +233,7 @@
240  
241  
242  
243 +   static // useless out of this file
244     void DivideEdge (TopoDS_Edge & edge, Array<MeshPoint> & ps,
245                      Array<double> & params, Mesh & mesh)
246     {
247 @@ -247,8 +253,8 @@
248        hvalue[0] = 0;
249        pnt = c->Value(s0);
250  
251 -      double olddist = 0;
252 -      double dist = 0;
253 +      //double olddist = 0; -- useless variables
254 +      //double dist = 0;
255  
256        int tmpVal = (int)(DIVIDEEDGESECTIONS);
257  
258 @@ -256,15 +262,19 @@
259        {
260           oldpnt = pnt;
261           pnt = c->Value(s0+(i/double(DIVIDEEDGESECTIONS))*(s1-s0));
262 +         // -- no more than 1 segment per <edge length>/DIVIDEEDGESECTIONS
263           hvalue[i] = hvalue[i-1] +
264 -            1.0/mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))*
265 -            pnt.Distance(oldpnt);
266 +         //   1.0/mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))*
267 +         //   pnt.Distance(oldpnt);
268 +           min( 1.0,
269 +                1.0/mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))*
270 +                pnt.Distance(oldpnt));
271  
272           //(*testout) << "mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z())) " << mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))
273           //       <<  " pnt.Distance(oldpnt) " << pnt.Distance(oldpnt) << endl;
274  
275 -         olddist = dist;
276 -         dist = pnt.Distance(oldpnt);
277 +         //olddist = dist; -- useless variables
278 +         //dist = pnt.Distance(oldpnt);
279        }
280  
281        //  nsubedges = int(ceil(hvalue[DIVIDEEDGESECTIONS]));
282 @@ -279,7 +289,10 @@
283        {
284           if (hvalue[i1]/hvalue[DIVIDEEDGESECTIONS]*nsubedges >= i)
285           {
286 -            params[i] = s0+(i1/double(DIVIDEEDGESECTIONS))*(s1-s0);
287 +            // -- for nsubedges comparable to DIVIDEEDGESECTIONS
288 +            //params[i] = s0+(i1/double(DIVIDEEDGESECTIONS))*(s1-s0);
289 +            double d1 = i1 - (hvalue[i1] - i*hvalue[DIVIDEEDGESECTIONS]/nsubedges)/(hvalue[i1]-hvalue[i1-1]);
290 +            params[i] = s0+(d1/double(DIVIDEEDGESECTIONS))*(s1-s0);
291              pnt = c->Value(params[i]);
292              ps[i-1] = MeshPoint (Point3d(pnt.X(), pnt.Y(), pnt.Z()));
293              i++;
294 @@ -323,6 +336,7 @@
295        (*testout) << "nedges = " << nedges << endl;
296  
297        double eps = 1e-6 * geom.GetBoundingBox().Diam();
298 +      const double eps2 = eps * eps; // -- small optimization
299  
300        for (int i = 1; i <= nvertices; i++)
301        {
302 @@ -332,7 +346,8 @@
303           bool exists = 0;
304           if (merge_solids)
305              for (PointIndex pi = 1; pi <= mesh.GetNP(); pi++)
306 -               if ( Dist2 (mesh[pi], Point<3>(mp)) < eps*eps)
307 +               //if ( Dist2 (mesh[pi], Point<3>(mp)) < eps*eps)              
308 +               if ( Dist2 (mesh[pi], Point<3>(mp)) < eps2 ) // -- small optimization
309                 {
310                    exists = 1;
311                    break;
312 @@ -362,6 +377,7 @@
313           {
314              TopoDS_Face face = TopoDS::Face(exp1.Current());
315              int facenr = geom.fmap.FindIndex(face);
316 +            if ( facenr < 1 ) continue; // -- to support SALOME sub-meshes
317  
318              if (face2solid[0][facenr-1] == 0)
319                 face2solid[0][facenr-1] = solidnr;
320 @@ -381,6 +397,7 @@
321        int facenr = 0;
322        int edgenr = 0;
323  
324 +      edgenr = mesh.GetNSeg(); // to support SALOME sub-meshes
325  
326        (*testout) << "faces = " << geom.fmap.Extent() << endl;
327        int curr = 0;
328 @@ -442,6 +459,7 @@
329                    //(*testout) << "ignoring degenerated edge" << endl;
330                    continue;
331                 }
332 +               if ( geom.emap.FindIndex(edge) < 1 ) continue; // to support SALOME sub-meshes
333  
334                 if (geom.vmap.FindIndex(TopExp::FirstVertex (edge)) ==
335                    geom.vmap.FindIndex(TopExp::LastVertex (edge)))
336 @@ -479,15 +497,64 @@
337                 }
338                 else
339                 {
340 -                  Point<3> fp = occ2ng (BRep_Tool::Pnt (TopExp::FirstVertex (edge)));
341 -                  Point<3> lp = occ2ng (BRep_Tool::Pnt (TopExp::LastVertex (edge)));
342 +                 TopoDS_Iterator vIt( edge, false );
343 +                 TopoDS_Vertex v1 = TopoDS::Vertex( vIt.Value() ); vIt.Next();
344 +                 TopoDS_Vertex v2 = TopoDS::Vertex( vIt.Value() );
345 +                 if ( v1.Orientation() == TopAbs_REVERSED )
346 +                   std::swap( v1, v2 );
347 +                 const bool isClosedEdge = v1.IsSame( v2 );
348 +                 
349 +                  Point<3> fp = occ2ng (BRep_Tool::Pnt (v1));
350 +                  Point<3> lp = occ2ng (BRep_Tool::Pnt (v2));
351 +                  double tol2 = std::min( eps*eps, 1e-6 * Dist2( fp, lp ));
352 +                  if ( isClosedEdge )
353 +                    tol2 = BRep_Tool::Tolerance( v1 ) * BRep_Tool::Tolerance( v1 );
354  
355                    pnums[0] = -1;
356                    pnums.Last() = -1;
357                    for (PointIndex pi = 1; pi < first_ep; pi++)
358                    {
359 -                     if (Dist2 (mesh[pi], fp) < eps*eps) pnums[0] = pi;
360 -                     if (Dist2 (mesh[pi], lp) < eps*eps) pnums.Last() = pi;
361 +                    if (Dist2 (mesh[pi], fp) < tol2) pnums[0] = pi;
362 +                    if (Dist2 (mesh[pi], lp) < tol2) pnums.Last() = pi;
363 +                  }
364 +                  if (( isClosedEdge && pnums[0] != pnums.Last() ) ||
365 +                      ( !isClosedEdge && pnums[0] == pnums.Last() ))
366 +                    pnums[0] = pnums.Last() = -1;
367 +                  if ( pnums[0] == -1 || pnums.Last() == -1 )
368 +                  {
369 +                    // take into account a possible large gap between a vertex and an edge curve
370 +                    // end and a large vertex tolerance covering the whole edge
371 +                    if ( pnums[0] == -1 )
372 +                    {
373 +                      double tol = BRep_Tool::Tolerance( v1 );
374 +                      for (PointIndex pi = 1; pi < first_ep; pi++)
375 +                        if (pi != pnums.Last() && Dist2 (mesh[pi], fp) < 2*tol*tol)
376 +                          pnums[0] = pi;
377 +
378 +                      if ( pnums[0] == -1 )
379 +                        pnums[0] = first_ep-1- nvertices + geom.vmap.FindIndex ( v1 );
380 +                    }
381 +                    if ( isClosedEdge )
382 +                    {
383 +                      pnums.Last() = pnums[0];
384 +                    }
385 +                    else
386 +                    {
387 +                      if ( pnums.Last() == -1 )
388 +                      {
389 +                        double tol = BRep_Tool::Tolerance( v2 );
390 +                        for (PointIndex pi = 1; pi < first_ep; pi++)
391 +                          if (pi != pnums[0] && Dist2 (mesh[pi], lp) < 2*tol*tol)
392 +                            pnums.Last() = pi;
393 +
394 +                        if ( pnums.Last() == -1 )
395 +                          pnums.Last() = first_ep-1-nvertices + geom.vmap.FindIndex ( v2 );
396 +                      }
397 +
398 +                      if ( Dist2( fp, mesh[PointIndex(pnums[0])]) >
399 +                           Dist2( lp, mesh[PointIndex(pnums.Last())]))
400 +                      std::swap( pnums[0], pnums.Last() );
401 +                    }
402                    }
403                 }
404  
405 @@ -633,7 +700,8 @@
406           }
407  
408           (*testout) << "mesh face " << k << endl;
409 -         multithread.percent = 100 * k / (mesh.GetNFD()+1e-10);
410 +         // multithread.percent = 100 * k / (mesh.GetNFD()+1e-10); -- avoid FPE
411 +         multithread.percent = 100 * k / (mesh.GetNFD() + VSMALL);
412           geom.facemeshstatus[k-1] = -1;
413  
414  
415 @@ -901,7 +969,8 @@
416           //      if (k != 36) continue;
417  
418           //      (*testout) << "optimize face " << k << endl;
419 -         multithread.percent = 100 * k / (mesh.GetNFD()+1e-10);
420 +         //multithread.percent = 100 * k / (mesh.GetNFD()+1e-10); -- avoid FPE
421 +         multithread.percent = 100 * k / (mesh.GetNFD() + VSMALL);
422  
423           FaceDescriptor & fd = mesh.GetFaceDescriptor(k);
424  
425 @@ -1456,3 +1525,4 @@
426  }
427  
428  #endif
429 +
430 diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.cpp netgen-4.9.13_new/libsrc/occ/occgeom.cpp
431 --- netgen-4.9.13_orig/libsrc/occ/occgeom.cpp   2010-03-05 16:16:21.000000000 +0300
432 +++ netgen-4.9.13_new/libsrc/occ/occgeom.cpp    2011-12-20 14:50:26.000000000 +0400
433 @@ -8,6 +8,8 @@
434  #include "ShapeAnalysis_CheckSmallFace.hxx"
435  #include "ShapeAnalysis_DataMapOfShapeListOfReal.hxx"
436  #include "ShapeAnalysis_Surface.hxx"
437 +#include <BRepTopAdaptor_FClass2d.hxx> // -- to optimize Project() and FastProject()
438 +#include <TopAbs_State.hxx>
439  #include "BRepAlgoAPI_Fuse.hxx"
440  #include "BRepCheck_Analyzer.hxx"
441  #include "BRepLib.hxx"
442 @@ -16,10 +18,17 @@
443  #include "ShapeFix_FixSmallFace.hxx"
444  #include "Partition_Spliter.hxx"
445  
446 -
447  namespace netgen
448  {
449 -   void OCCGeometry :: PrintNrShapes ()
450 +  // free data used to optimize Project() and FastProject()
451 +  OCCGeometry::~OCCGeometry()
452 +  {
453 +    NCollection_DataMap<int,BRepTopAdaptor_FClass2d*>::Iterator it(fclsmap);
454 +    for (; it.More(); it.Next())
455 +      delete it.Value();
456 +  }
457 +
458 +  void OCCGeometry :: PrintNrShapes ()
459     {
460        TopExp_Explorer e;
461        int count = 0;
462 @@ -951,25 +960,58 @@
463     }
464  
465  
466 +   // returns a projector and a classifier for the given surface
467 +   void OCCGeometry::GetFaceTools(int surfi, Handle(ShapeAnalysis_Surface)& proj,
468 +                                  BRepTopAdaptor_FClass2d*& cls) const
469 +   {
470 +     //MSV: organize caching projector in the map
471 +     if (fprjmap.IsBound(surfi))
472 +     {
473 +       proj = fprjmap.Find(surfi);
474 +       cls = fclsmap.Find(surfi);
475 +     }
476 +     else
477 +     {
478 +       const TopoDS_Face& aFace = TopoDS::Face(fmap(surfi));
479 +       Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
480 +       proj = new ShapeAnalysis_Surface(aSurf);
481 +       fprjmap.Bind(surfi, proj);
482 +       cls = new BRepTopAdaptor_FClass2d(aFace,Precision::Confusion());
483 +       fclsmap.Bind(surfi, cls);
484 +     }
485 +   }
486  
487 -
488 -   void OCCGeometry :: Project (int surfi, Point<3> & p) const
489 +   // void OCCGeometry :: Project (int surfi, Point<3> & p) const
490 +   bool OCCGeometry :: Project (int surfi, Point<3> & p, double& u, double& v) const
491     {
492        static int cnt = 0;
493        if (++cnt % 1000 == 0) cout << "Project cnt = " << cnt << endl;
494  
495        gp_Pnt pnt(p(0), p(1), p(2));
496  
497 -      double u,v;
498 -      Handle( Geom_Surface ) thesurf = BRep_Tool::Surface(TopoDS::Face(fmap(surfi)));
499 -      Handle( ShapeAnalysis_Surface ) su = new ShapeAnalysis_Surface( thesurf );
500 -      gp_Pnt2d suval = su->ValueOfUV ( pnt, BRep_Tool::Tolerance( TopoDS::Face(fmap(surfi)) ) );
501 -      suval.Coord( u, v);
502 -      pnt = thesurf->Value( u, v );
503 -
504 -
505 +      // -- Optimization: use cached projector and classifier
506 +      // double u,v;
507 +      // Handle( Geom_Surface ) thesurf = BRep_Tool::Surface(TopoDS::Face(fmap(surfi)));
508 +      // Handle( ShapeAnalysis_Surface ) su = new ShapeAnalysis_Surface( thesurf );
509 +      // gp_Pnt2d suval = su->ValueOfUV ( pnt, BRep_Tool::Tolerance( TopoDS::Face(fmap(surfi)) ) );
510 +      // suval.Coord( u, v);
511 +      // pnt = thesurf->Value( u, v );  
512 +
513 +      Handle(ShapeAnalysis_Surface) proj;
514 +      BRepTopAdaptor_FClass2d *cls;
515 +      GetFaceTools(surfi, proj, cls);
516 +  
517 +      gp_Pnt2d p2d = proj->ValueOfUV(pnt, Precision::Confusion());
518 +      if (cls->Perform(p2d) == TopAbs_OUT)
519 +      {
520 +        return false;
521 +      }
522 +      pnt = proj->Value(p2d);
523 +      p2d.Coord(u, v);
524 +  
525        p = Point<3> (pnt.X(), pnt.Y(), pnt.Z());
526  
527 +      return true;
528     }
529  
530  
531 @@ -979,54 +1021,69 @@
532     {
533        gp_Pnt p(ap(0), ap(1), ap(2));
534  
535 -      Handle(Geom_Surface) surface = BRep_Tool::Surface(TopoDS::Face(fmap(surfi)));
536 -
537 -      gp_Pnt x = surface->Value (u,v);
538 -
539 -      if (p.SquareDistance(x) <= sqr(PROJECTION_TOLERANCE)) return true;
540 -
541 -      gp_Vec du, dv;
542 -
543 -      surface->D1(u,v,x,du,dv);
544 -
545 -      int count = 0;
546 -
547 -      gp_Pnt xold;
548 -      gp_Vec n;
549 -      double det, lambda, mu;
550 -
551 -      do {
552 -         count++;
553 -
554 -         n = du^dv;
555 -
556 -         det = Det3 (n.X(), du.X(), dv.X(),
557 -            n.Y(), du.Y(), dv.Y(),
558 -            n.Z(), du.Z(), dv.Z());
559 -
560 -         if (det < 1e-15) return false;
561 -
562 -         lambda = Det3 (n.X(), p.X()-x.X(), dv.X(),
563 -            n.Y(), p.Y()-x.Y(), dv.Y(),
564 -            n.Z(), p.Z()-x.Z(), dv.Z())/det;
565 -
566 -         mu     = Det3 (n.X(), du.X(), p.X()-x.X(),
567 -            n.Y(), du.Y(), p.Y()-x.Y(),
568 -            n.Z(), du.Z(), p.Z()-x.Z())/det;
569 -
570 -         u += lambda;
571 -         v += mu;
572 -
573 -         xold = x;
574 -         surface->D1(u,v,x,du,dv);
575 -
576 -      } while (xold.SquareDistance(x) > sqr(PROJECTION_TOLERANCE) && count < 50);
577 -
578 -      //    (*testout) << "FastProject count: " << count << endl;
579 -
580 -      if (count == 50) return false;
581 -
582 -      ap = Point<3> (x.X(), x.Y(), x.Z());
583 +      // -- Optimization: use cached projector and classifier
584 +      // Handle(Geom_Surface) surface = BRep_Tool::Surface(TopoDS::Face(fmap(surfi)));
585 +      // 
586 +      // gp_Pnt x = surface->Value (u,v);
587 +      // 
588 +      // if (p.SquareDistance(x) <= sqr(PROJECTION_TOLERANCE)) return true;
589 +      // 
590 +      // gp_Vec du, dv;
591 +      // 
592 +      // surface->D1(u,v,x,du,dv);
593 +      // 
594 +      // int count = 0;
595 +      // 
596 +      // gp_Pnt xold;
597 +      // gp_Vec n;
598 +      // double det, lambda, mu;
599 +      // 
600 +      // do {
601 +      //    count++;
602 +      // 
603 +      //    n = du^dv;
604 +      // 
605 +      //    det = Det3 (n.X(), du.X(), dv.X(),
606 +      //       n.Y(), du.Y(), dv.Y(),
607 +      //       n.Z(), du.Z(), dv.Z());
608 +      // 
609 +      //    if (det < 1e-15) return false;
610 +      // 
611 +      //    lambda = Det3 (n.X(), p.X()-x.X(), dv.X(),
612 +      //       n.Y(), p.Y()-x.Y(), dv.Y(),
613 +      //       n.Z(), p.Z()-x.Z(), dv.Z())/det;
614 +      // 
615 +      //    mu     = Det3 (n.X(), du.X(), p.X()-x.X(),
616 +      //       n.Y(), du.Y(), p.Y()-x.Y(),
617 +      //       n.Z(), du.Z(), p.Z()-x.Z())/det;
618 +      // 
619 +      //    u += lambda;
620 +      //    v += mu;
621 +      // 
622 +      //    xold = x;
623 +      //    surface->D1(u,v,x,du,dv);
624 +      // 
625 +      // } while (xold.SquareDistance(x) > sqr(PROJECTION_TOLERANCE) && count < 50);
626 +      // 
627 +      // //    (*testout) << "FastProject count: " << count << endl;
628 +      // 
629 +      // if (count == 50) return false;
630 +      // 
631 +      // ap = Point<3> (x.X(), x.Y(), x.Z());
632 +      Handle(ShapeAnalysis_Surface) proj;
633 +      BRepTopAdaptor_FClass2d *cls;
634 +      GetFaceTools(surfi, proj, cls);
635 +    
636 +      gp_Pnt2d p2d = proj->NextValueOfUV(gp_Pnt2d(u,v), p, Precision::Confusion());
637 +      if (cls->Perform(p2d) == TopAbs_OUT)
638 +      {
639 +        //cout << "Projection fails" << endl;
640 +        return false;
641 +      }
642 +    
643 +      p = proj->Value(p2d);
644 +      p2d.Coord(u, v);
645 +      ap = Point<3> (p.X(), p.Y(), p.Z());
646  
647        return true;
648     }
649 diff -Naur netgen-4.9.13_orig/libsrc/occ/occgeom.hpp netgen-4.9.13_new/libsrc/occ/occgeom.hpp
650 --- netgen-4.9.13_orig/libsrc/occ/occgeom.hpp   2010-01-14 19:56:19.000000000 +0300
651 +++ netgen-4.9.13_new/libsrc/occ/occgeom.hpp    2011-12-20 14:50:26.000000000 +0400
652 @@ -15,8 +15,8 @@
653  #include "Geom_Curve.hxx"
654  #include "Geom2d_Curve.hxx"
655  #include "Geom_Surface.hxx"
656 -#include "GeomAPI_ProjectPointOnSurf.hxx"
657 -#include "GeomAPI_ProjectPointOnCurve.hxx"
658 +// #include "GeomAPI_ProjectPointOnSurf.hxx"
659 +// #include "GeomAPI_ProjectPointOnCurve.hxx"
660  #include "BRepTools.hxx"
661  #include "TopExp.hxx"
662  #include "BRepBuilderAPI_MakeVertex.hxx"
663 @@ -42,8 +42,8 @@
664  #include "Geom_Curve.hxx"
665  #include "Geom2d_Curve.hxx"
666  #include "Geom_Surface.hxx"
667 -#include "GeomAPI_ProjectPointOnSurf.hxx"
668 -#include "GeomAPI_ProjectPointOnCurve.hxx"
669 +// #include "GeomAPI_ProjectPointOnSurf.hxx"
670 +// #include "GeomAPI_ProjectPointOnCurve.hxx"
671  #include "TopoDS_Wire.hxx"
672  #include "BRepTools_WireExplorer.hxx"
673  #include "BRepTools.hxx"
674 @@ -68,7 +68,7 @@
675  #include "IGESToBRep_Reader.hxx"
676  #include "Interface_Static.hxx"
677  #include "GeomAPI_ExtremaCurveCurve.hxx"
678 -#include "Standard_ErrorHandler.hxx"
679 +//#include "Standard_ErrorHandler.hxx"
680  #include "Standard_Failure.hxx"
681  #include "ShapeUpgrade_ShellSewing.hxx"
682  #include "ShapeFix_Shape.hxx"
683 @@ -80,6 +80,10 @@
684  #include "ShapeAnalysis.hxx"
685  #include "ShapeBuild_ReShape.hxx"
686  
687 +// -- Optimization: to use cached projector and classifier
688 +#include <NCollection_DataMap.hxx>
689 +class Handle_ShapeAnalysis_Surface;
690 +class BRepTopAdaptor_FClass2d;
691  
692  // Philippose - 29/01/2009
693  // OpenCascade XDE Support
694 @@ -190,6 +194,9 @@
695     class OCCGeometry : public NetgenGeometry
696     {
697        Point<3> center;
698 +      // -- Optimization: to use cached projector and classifier
699 +      mutable NCollection_DataMap<int,Handle_ShapeAnalysis_Surface> fprjmap;
700 +      mutable NCollection_DataMap<int,BRepTopAdaptor_FClass2d*> fclsmap;
701  
702     public:
703        TopoDS_Shape shape;
704 @@ -241,6 +248,8 @@
705           vmap.Clear();
706        }
707  
708 +      ~OCCGeometry();      // -- to free cached projector and classifier
709 +
710        void BuildFMap();
711  
712        Box<3> GetBoundingBox()
713 @@ -260,9 +269,14 @@
714        Point<3> Center()
715        {  return center;}
716  
717 -      void Project (int surfi, Point<3> & p) const;
718 +      // void Project (int surfi, Point<3> & p) const; -- optimization
719 +      bool Project (int surfi, Point<3> & p, double& u, double& v) const;
720        bool FastProject (int surfi, Point<3> & ap, double& u, double& v) const;
721  
722 +      // -- Optimization: to use cached projector and classifier
723 +      void GetFaceTools(int surfi, Handle(ShapeAnalysis_Surface)& proj,
724 +                        BRepTopAdaptor_FClass2d*& cls) const;
725 +
726        OCCSurface GetSurface (int surfi)
727        {
728           cout << "OCCGeometry::GetSurface using PLANESPACE" << endl;
729 diff -Naur netgen-4.9.13_orig/libsrc/occ/occmeshsurf.cpp netgen-4.9.13_new/libsrc/occ/occmeshsurf.cpp
730 --- netgen-4.9.13_orig/libsrc/occ/occmeshsurf.cpp       2009-08-24 06:32:47.000000000 +0400
731 +++ netgen-4.9.13_new/libsrc/occ/occmeshsurf.cpp        2011-12-20 14:50:26.000000000 +0400
732 @@ -6,6 +6,7 @@
733  #include <meshing.hpp>
734  #include <GeomLProp_SLProps.hxx>
735  #include <ShapeAnalysis_Surface.hxx>
736 +#include <GeomAPI_ProjectPointOnCurve.hxx> // -- moved here from occgeom.hpp
737  
738  
739  namespace netgen
740 @@ -434,23 +435,33 @@
741  
742    void MeshOptimize2dOCCSurfaces :: ProjectPoint (INDEX surfind, Point<3> & p) const
743    {
744 -    geometry.Project (surfind, p);
745 +    // geometry.Project (surfind, p); -- signature of Project() changed for optimization
746 +    double u, v;
747 +    geometry.Project (surfind, p, u, v);
748    }
749  
750  
751    int MeshOptimize2dOCCSurfaces :: ProjectPointGI (INDEX surfind, Point<3> & p, PointGeomInfo & gi) const
752    {
753 -    double u = gi.u;
754 -    double v = gi.v;
755 +    //double u = gi.u;
756 +    //double v = gi.v;
757  
758      Point<3> hp = p;
759 -    if (geometry.FastProject (surfind, hp, u, v))
760 -      {
761 -       p = hp;
762 -       return 1;
763 -      }
764 -    ProjectPoint (surfind, p); 
765 -    return CalcPointGeomInfo (surfind, gi, p); 
766 +    // -- u and v are computed by FastProject() and Project(), no need to call CalcPointGeomInfo()
767 +    // if (geometry.FastProject (surfind, hp, u, v))
768 +    //   {
769 +    //    p = hp;
770 +    //    return 1;
771 +    //   }
772 +    // ProjectPoint (surfind, p); 
773 +    // return CalcPointGeomInfo (surfind, gi, p); 
774 +    bool ok;
775 +    if (gi.trignum > 0)
776 +      ok = geometry.FastProject (surfind, hp, gi.u, gi.v);
777 +    else
778 +      ok = geometry.Project (surfind, hp, gi.u, gi.v);
779 +    p = hp;
780 +    return ok;
781    }
782  
783  
784 @@ -680,7 +691,8 @@
785         if (!geometry.FastProject (surfi, hnewp, u, v))
786           {
787           //  cout << "Fast projection to surface fails! Using OCC projection" << endl;
788 -           geometry.Project (surfi, hnewp);
789 +           // geometry.Project (surfi, hnewp); -- Project() changed for optimization
790 +           geometry.Project (surfi, hnewp, u, v);
791           }
792  
793         newgi.trignum = 1;
794 @@ -689,7 +701,7 @@
795        }
796    
797      newp = hnewp;
798 -  }
799 +  }//; -- to compile with -Wall -pedantic
800  
801  
802    void OCCRefinementSurfaces :: 
803 @@ -708,14 +720,18 @@
804      hnewp = Point<3> (pnt.X(), pnt.Y(), pnt.Z());
805      newp = hnewp;
806      newgi = ap1;
807 -  };
808 +  }
809  
810  
811    void OCCRefinementSurfaces :: ProjectToSurface (Point<3> & p, int surfi)
812    {
813      if (surfi > 0)
814 -      geometry.Project (surfi, p);
815 -  };
816 +      // geometry.Project (surfi, p); -- Project() changed for optimization
817 +    {
818 +      double u, v;
819 +      geometry.Project (surfi, p, u, v);
820 +    }
821 +  }//; -- to compile with -Wall -pedantic
822  
823    void OCCRefinementSurfaces :: ProjectToSurface (Point<3> & p, int surfi, PointGeomInfo & gi)
824    {
825 @@ -723,9 +739,10 @@
826        if (!geometry.FastProject (surfi, p, gi.u, gi.v))
827         {
828           cout << "Fast projection to surface fails! Using OCC projection" << endl;
829 -         geometry.Project (surfi, p);
830 +          double u, v;
831 +         geometry.Project (surfi, p, u, v);
832         }
833 -  };
834 +  }
835  
836  
837  
838 diff -Naur netgen-4.9.13_orig/libsrc/occ/Partition_Inter3d.cxx netgen-4.9.13_new/libsrc/occ/Partition_Inter3d.cxx
839 --- netgen-4.9.13_orig/libsrc/occ/Partition_Inter3d.cxx 2009-08-24 06:12:24.000000000 +0400
840 +++ netgen-4.9.13_new/libsrc/occ/Partition_Inter3d.cxx  2011-12-20 14:50:26.000000000 +0400
841 @@ -86,6 +86,9 @@
842  #include <TopOpeBRepTool_OutCurveType.hxx>
843  #include <TopOpeBRep_DSFiller.hxx>
844  #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
845 +
846 +#include <Standard_Version.hxx>
847 +
848  #include <stdio.h>
849  
850  //=======================================================================
851 @@ -243,7 +246,12 @@
852        Standard_Integer i, nbExt = anExtPS.NbExt();
853        Extrema_POnSurf aPOnSurf;
854        for (i = 1; i <= nbExt; ++i )
855 +#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060400
856 +// porting to OCCT6.5.1
857 +        if (anExtPS.SquareDistance( i ) <= TolE * TolE) {
858 +#else
859          if (anExtPS.Value( i ) <= TolE) {
860 +#endif
861            aPOnSurf = anExtPS.Point( i );
862            break;
863          }
864 diff -Naur netgen-4.9.13_orig/libsrc/occ/Partition_Loop2d.cxx netgen-4.9.13_new/libsrc/occ/Partition_Loop2d.cxx
865 --- netgen-4.9.13_orig/libsrc/occ/Partition_Loop2d.cxx  2009-08-24 06:12:24.000000000 +0400
866 +++ netgen-4.9.13_new/libsrc/occ/Partition_Loop2d.cxx   2011-12-20 14:53:39.000000000 +0400
867 @@ -22,7 +22,6 @@
868  #include <BRepAdaptor_Surface.hxx>
869  #include <BRepAlgo_AsDes.hxx>
870  #include <BRepAlgo_FaceRestrictor.hxx>
871 -#include <BRepOffset_DataMapOfShapeReal.hxx>
872  #include <BRepTopAdaptor_FClass2d.hxx>
873  #include <BRep_Builder.hxx>
874  #include <BRep_Tool.hxx>
875 @@ -51,6 +50,15 @@
876  #include <gp_Pnt.hxx>
877  #include <gp_Pnt2d.hxx>
878  
879 +#include <Standard_Version.hxx>
880 +
881 +#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060400
882 +// porting to OCCT6.5.1
883 +#include <TopTools_DataMapOfShapeReal.hxx>
884 +#else
885 +#include <BRepOffset_DataMapOfShapeReal.hxx>
886 +#endif
887 +
888  //=======================================================================
889  //function : Partition_Loop2d
890  //purpose  :
891 @@ -209,7 +217,7 @@
892      Cc->D1(uc, PC, CTg1);
893      if (!isForward) CTg1.Reverse();
894  
895 -    Standard_Real anglemin = 3 * PI, tolAng = 1.e-8;
896 +    Standard_Real anglemin = 3 * M_PI, tolAng = 1.e-8;
897  
898      // select an edge whose first derivative is most left of CTg1
899      // ie an angle between Tg1 and CTg1 is least
900 @@ -233,7 +241,7 @@
901        // -PI < angle < PI
902        Standard_Real angle = Tg1.Angle(CTg1);
903  
904 -      if (PI - Abs(angle) <= tolAng)
905 +      if (M_PI - Abs(angle) <= tolAng)
906        {
907          // an angle is too close to PI; assure that an angle sign really
908          // reflects an edge position: +PI - an edge is worst,
909 @@ -519,7 +527,12 @@
910      DC.Initialize( DegEdge, F );
911  
912    // avoid intersecting twice the same edge
913 +#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060400
914 +// porting to OCCT6.5.1
915 +  TopTools_DataMapOfShapeReal EUMap ( EdgesList.Extent() );
916 +#else
917    BRepOffset_DataMapOfShapeReal EUMap ( EdgesList.Extent() );
918 +#endif
919  
920    Standard_Real U, f, l;
921    BRep_Tool::Range (DegEdge, f, l);
922 diff -Naur netgen-4.9.13_orig/libsrc/occ/Partition_Loop.cxx netgen-4.9.13_new/libsrc/occ/Partition_Loop.cxx
923 --- netgen-4.9.13_orig/libsrc/occ/Partition_Loop.cxx    2009-08-24 06:12:24.000000000 +0400
924 +++ netgen-4.9.13_new/libsrc/occ/Partition_Loop.cxx     2011-12-20 14:53:05.000000000 +0400
925 @@ -178,7 +178,7 @@
926        }
927      }
928  
929 -    Standard_Real anglemax = - PI;
930 +    Standard_Real anglemax = - M_PI;
931      TopoDS_Edge   SelectedEdge;        
932      for ( itl.Initialize(LE); itl.More(); itl.Next()) {
933        const TopoDS_Edge& E = TopoDS::Edge(itl.Value());
934 diff -Naur netgen-4.9.13_orig/libsrc/occ/Partition_Spliter.cxx netgen-4.9.13_new/libsrc/occ/Partition_Spliter.cxx
935 --- netgen-4.9.13_orig/libsrc/occ/Partition_Spliter.cxx 2009-08-24 06:12:24.000000000 +0400
936 +++ netgen-4.9.13_new/libsrc/occ/Partition_Spliter.cxx  2011-12-20 14:50:26.000000000 +0400
937 @@ -79,6 +79,8 @@
938  #include <GeomAdaptor_Curve.hxx>
939  #include <TopOpeBRepTool_CurveTool.hxx>
940  
941 +#include <Standard_Version.hxx>
942 +
943  #ifdef DEB
944  //# define PART_PERF
945  #endif
946 @@ -1169,7 +1171,12 @@
947            for (; j<=nbj && ok; ++j) {
948              if (Extrema.IsMin(j)) {
949               hasMin = Standard_True;
950 +#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060400
951 +// porting to OCCT6.5.1
952 +              ok = Extrema.SquareDistance(j) <= tol * tol;
953 +#else
954                ok = Extrema.Value(j) <= tol;
955 +#endif
956             }
957            }
958          }
959 diff -Naur netgen-4.9.13_orig/libsrc/occ/utilities.h netgen-4.9.13_new/libsrc/occ/utilities.h
960 --- netgen-4.9.13_orig/libsrc/occ/utilities.h   2009-08-24 06:12:24.000000000 +0400
961 +++ netgen-4.9.13_new/libsrc/occ/utilities.h    2011-12-20 14:50:26.000000000 +0400
962 @@ -33,6 +33,7 @@
963  
964  #include <string>
965  #include <iostream>
966 +#include <iomanip>
967  #include <cstdlib>
968  // #include "SALOME_Log.hxx"
969  
970 diff -Naur netgen-4.9.13_orig/libsrc/stlgeom/stlgeommesh.cpp netgen-4.9.13_new/libsrc/stlgeom/stlgeommesh.cpp
971 --- netgen-4.9.13_orig/libsrc/stlgeom/stlgeommesh.cpp   2009-08-10 15:40:51.000000000 +0400
972 +++ netgen-4.9.13_new/libsrc/stlgeom/stlgeommesh.cpp    2011-12-20 14:50:26.000000000 +0400
973 @@ -1435,7 +1435,8 @@
974  
975           if (!optstring || strlen(optstring) == 0)
976             {
977 -             mparam.optimize2d = "smcm";
978 +             //mparam.optimize2d = (char*)"smcm";
979 +              mparam.optimize2d = (char*)"smcm";
980             }
981           else
982             {
983 @@ -1451,7 +1452,8 @@
984                                  mparam.grading);
985               mesh -> LoadLocalMeshSize (mparam.meshsizefilename);            
986               mesh -> CalcLocalHFromSurfaceCurvature (stlparam.resthsurfmeshcurvfac);
987 -             mparam.optimize2d = "cmsmSm";
988 +             //mparam.optimize2d = (char*)"cmsmSm";
989 +              mparam.optimize2d = (char*)"cmsmSm";
990               STLSurfaceOptimization (*stlgeometry, *mesh, mparam);
991  #ifdef STAT_STREAM
992               (*statout) << GetTime() << " & ";
993 @@ -1557,7 +1559,8 @@
994  
995           if (!optstring || strlen(optstring) == 0)
996             {
997 -             mparam.optimize3d = "cmdmstm";
998 +              //mparam.optimize3d = "cmdmstm";
999 +             mparam.optimize3d = (char*)"cmdmstm";
1000             }
1001           else
1002             {
1003 diff -Naur netgen-4.9.13_orig/nglib/nglib.h netgen-4.9.13_new/nglib/nglib.h
1004 --- netgen-4.9.13_orig/nglib/nglib.h    2010-05-18 15:20:25.000000000 +0400
1005 +++ netgen-4.9.13_new/nglib/nglib.h     2011-12-20 14:50:26.000000000 +0400
1006 @@ -24,7 +24,7 @@
1007  // Philippose - 14.02.2009
1008  // Modifications for creating a DLL in Windows
1009  #ifdef WIN32
1010 -   #ifdef NGLIB_EXPORTS || nglib_EXPORTS
1011 +   #if defined NGLIB_EXPORTS || defined nglib_EXPORTS
1012        #define DLL_HEADER   __declspec(dllexport)
1013     #else
1014        #define DLL_HEADER   __declspec(dllimport)