--- /dev/null
+diff -Naur freeimage-3.16.0_SRC_orig/Source/LibRawLite/dcraw/dcraw.c freeimage-3.16.0_SRC_patched/Source/LibRawLite/dcraw/dcraw.c
+--- freeimage-3.16.0_SRC_orig/Source/LibRawLite/dcraw/dcraw.c 2014-02-07 23:48:10.000000000 +0400
++++ freeimage-3.16.0_SRC_patched/Source/LibRawLite/dcraw/dcraw.c 2016-10-14 12:05:37.481253676 +0300
+@@ -4072,22 +4072,22 @@
+ -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,\r
+ -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,\r
+ -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,\r
+- -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,\r
+- -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,\r
++ -1,-2,-1,+0,0,(signed char)0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,\r
++ -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,(signed char)0x88, -1,-1,+1,-2,0,0x40,\r
+ -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,\r
+ -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,\r
+ -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,\r
+ -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,\r
+ -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,\r
+ -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,\r
+- +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,\r
++ +0,-2,+0,+0,1,(signed char)0x80, +0,-1,+0,+1,1,(signed char)0x88, +0,-1,+1,-2,0,0x40,\r
+ +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,\r
+ +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,\r
+ +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,\r
+ +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,\r
+ +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,\r
+- +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,\r
+- +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,\r
++ +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,(signed char)0x80,\r
++ +1,-1,+1,+1,0,(signed char)0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,\r
+ +1,+0,+2,+1,0,0x10\r
+ }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };\r
+ ushort (*brow[5])[4], *pix;\r
+diff -Naur freeimage-3.16.0_SRC_orig/Source/LibRawLite/internal/dcraw_common.cpp freeimage-3.16.0_SRC_patched/Source/LibRawLite/internal/dcraw_common.cpp
+--- freeimage-3.16.0_SRC_orig/Source/LibRawLite/internal/dcraw_common.cpp 2014-02-07 23:48:10.000000000 +0400
++++ freeimage-3.16.0_SRC_patched/Source/LibRawLite/internal/dcraw_common.cpp 2016-10-14 12:06:27.540395171 +0300
+@@ -3782,22 +3782,22 @@
+ -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,\r
+ -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,\r
+ -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,\r
+- -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,\r
+- -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,\r
++ -1,-2,-1,+0,0,(signed char)0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,\r
++ -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,(signed char)0x88, -1,-1,+1,-2,0,0x40,\r
+ -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,\r
+ -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,\r
+ -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,\r
+ -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,\r
+ -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,\r
+ -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,\r
+- +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,\r
++ +0,-2,+0,+0,1,(signed char)0x80, +0,-1,+0,+1,1,(signed char)0x88, +0,-1,+1,-2,0,0x40,\r
+ +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,\r
+ +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,\r
+ +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,\r
+ +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,\r
+ +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,\r
+- +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,\r
+- +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,\r
++ +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,(signed char)0x80,\r
++ +1,-1,+1,+1,0,(signed char)0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,\r
+ +1,+0,+2,+1,0,0x10\r
+ }, chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };\r
+ ushort (*brow[5])[4], *pix;\r
+@@ -7380,7 +7380,7 @@
+ { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },\r
+ { "Phase One P65", 0, 0,\r
+ { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },\r
+- { "Red One", 704, 0xffff, /* DJC */\r
++ { "Red One", 704, (short int)0xffff, /* DJC */\r
+ { 21014,-7891,-2613,-3056,12201,856,-2203,5125,8042 } },\r
+ { "Samsung EK-GN120", 0, 0, /* Adobe; Galaxy NX */\r
+ { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },\r
--- /dev/null
+diff -Naur netgen-5.3.1_SRC_orig/Makefile.am netgen-5.3.1_SRC_modif/Makefile.am
+--- netgen-5.3.1_SRC_orig/Makefile.am 2014-08-29 13:55:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/Makefile.am 2017-09-11 18:08:21.217313702 +0300
+@@ -2,7 +2,7 @@
+
+ METASOURCES = AUTO
+
+-SUBDIRS = libsrc ng tutorials doc windows nglib
++SUBDIRS = libsrc nglib #tutorials doc windows nglib
+
+ # TESTS = ng/netgen -batchmode
+
+diff -Naur netgen-5.3.1_SRC_orig/Makefile.in netgen-5.3.1_SRC_modif/Makefile.in
+--- netgen-5.3.1_SRC_orig/Makefile.in 2014-10-06 15:04:37.000000000 +0400
++++ netgen-5.3.1_SRC_modif/Makefile.in 2017-09-11 18:08:21.225312838 +0300
+@@ -280,7 +280,7 @@
+ top_srcdir = @top_srcdir@
+ ACLOCAL_AMFLAGS = -I m4
+ METASOURCES = AUTO
+-SUBDIRS = libsrc ng tutorials doc windows nglib
++SUBDIRS = libsrc nglib #tutorials doc windows nglib
+ all: config.h
+ $(MAKE) $(AM_MAKEFLAGS) all-recursive
+
+diff -Naur netgen-5.3.1_SRC_orig/configure.ac netgen-5.3.1_SRC_modif/configure.ac
+--- netgen-5.3.1_SRC_orig/configure.ac 2014-10-06 15:00:17.000000000 +0400
++++ netgen-5.3.1_SRC_modif/configure.ac 2017-09-11 18:08:21.226312730 +0300
+@@ -20,7 +20,7 @@
+ CXXFLAGS="$CXXFLAGS $OPENMP_CXXFLAGS"
+ # LDFLAGS="$LDFLAGS $OPENMP_CXXFLAGS"
+
+-AM_PROG_AR
++m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
+ AC_PROG_LIBTOOL
+ LT_INIT
+
+@@ -42,8 +42,8 @@
+
+ if test a$occon = atrue ; then
+
+- AC_SUBST([OCCFLAGS], ["-DOCCGEOMETRY -I$occdir/inc -I/usr/include/opencascade"])
+- AC_SUBST([OCCLIBS], ["-L$occdir/lib -lTKernel -lTKGeomBase -lTKMath -lTKG2d -lTKG3d -lTKXSBase -lTKOffset -lTKFillet -lTKShHealing -lTKMesh -lTKMeshVS -lTKTopAlgo -lTKGeomAlgo -lTKBool -lTKPrim -lTKBO -lTKIGES -lTKBRep -lTKSTEPBase -lTKSTEP -lTKSTL -lTKSTEPAttr -lTKSTEP209 -lTKXDESTEP -lTKXDEIGES -lTKXCAF -lTKLCAF -lFWOSPlugin"])
++ AC_SUBST([OCCFLAGS], ["-DOCCGEOMETRY -I$occdir/include/opencascade"])
++ AC_SUBST([OCCLIBS], ["-L$occdir/lib -lTKernel -lTKGeomBase -lTKMath -lTKG2d -lTKG3d -lTKXSBase -lTKOffset -lTKFillet -lTKShHealing -lTKMesh -lTKMeshVS -lTKTopAlgo -lTKGeomAlgo -lTKBool -lTKPrim -lTKBO -lTKIGES -lTKBRep -lTKSTEPBase -lTKSTEP -lTKSTL -lTKSTEPAttr -lTKSTEP209 -lTKXDESTEP -lTKXDEIGES -lTKXCAF -lTKLCAF"])
+
+ # -lTKDCAF
+
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/csg/Makefile.am netgen-5.3.1_SRC_modif/libsrc/csg/Makefile.am
+--- netgen-5.3.1_SRC_orig/libsrc/csg/Makefile.am 2014-08-29 13:54:06.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/csg/Makefile.am 2017-09-11 18:08:21.234311865 +0300
+@@ -8,7 +8,7 @@
+ AM_CPPFLAGS = -I$(top_srcdir)/libsrc/include $(TCL_INCLUDES)
+ METASOURCES = AUTO
+
+-lib_LTLIBRARIES = libcsg.la
++noinst_LTLIBRARIES = libcsg.la
+
+
+ libcsg_la_SOURCES = algprim.cpp brick.cpp \
+@@ -17,12 +17,9 @@
+ manifold.cpp meshsurf.cpp polyhedra.cpp revolution.cpp singularref.cpp \
+ solid.cpp specpoin.cpp spline3d.cpp surface.cpp triapprox.cpp
+
+-libcsg_la_LIBADD = $(top_builddir)/libsrc/meshing/libmesh.la
+-
+-
+
+ if NGGUI
+-lib_LTLIBRARIES += libcsgvis.la
++lib_LTLIBRARIES = libcsgvis.la
+
+ libcsgvis_la_SOURCES = vscsg.cpp csgpkg.cpp
+ libcsgvis_la_LIBADD = libcsg.la
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/geom2d/Makefile.am netgen-5.3.1_SRC_modif/libsrc/geom2d/Makefile.am
+--- netgen-5.3.1_SRC_orig/libsrc/geom2d/Makefile.am 2014-08-29 13:54:06.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/geom2d/Makefile.am 2017-09-11 18:08:21.234311865 +0300
+@@ -4,16 +4,15 @@
+
+ METASOURCES = AUTO
+
+-lib_LTLIBRARIES = libgeom2d.la
++noinst_LTLIBRARIES = libgeom2d.la
+
+ if NGGUI
+-lib_LTLIBRARIES += libgeom2dvis.la
++lib_LTLIBRARIES = libgeom2dvis.la
+ endif
+
+
+
+ libgeom2d_la_SOURCES = genmesh2d.cpp geom2dmesh.cpp geometry2d.cpp
+-libgeom2d_la_LIBADD = $(top_builddir)/libsrc/meshing/libmesh.la
+
+ libgeom2dvis_la_SOURCES = geom2dpkg.cpp vsgeom2d.cpp
+ libgeom2dvis_la_LIBADD = libgeom2d.la
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/interface/Makefile.am netgen-5.3.1_SRC_modif/libsrc/interface/Makefile.am
+--- netgen-5.3.1_SRC_orig/libsrc/interface/Makefile.am 2014-08-29 13:54:02.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/interface/Makefile.am 2017-09-11 18:08:21.234311865 +0300
+@@ -2,14 +2,11 @@
+
+ AM_CPPFLAGS = -I$(top_srcdir)/libsrc/include -I$(top_srcdir)/libsrc/interface $(MPI_INCLUDES) $(TCL_INCLUDES) -DOPENGL
+ METASOURCES = AUTO
+-lib_LTLIBRARIES = libinterface.la
++noinst_LTLIBRARIES = libinterface.la
+ libinterface_la_SOURCES = nginterface.cpp nginterface_v2.cpp \
+ read_fnf_mesh.cpp readtetmesh.cpp readuser.cpp writeabaqus.cpp writediffpack.cpp \
+ writedolfin.cpp writeelmer.cpp writefeap.cpp writefluent.cpp writegmsh.cpp writejcm.cpp \
+ writepermas.cpp writetecplot.cpp writetet.cpp writetochnog.cpp writeuser.cpp \
+ wuchemnitz.cpp writegmsh2.cpp writeOpenFOAM15x.cpp
+
+-
+-libinterface_la_LIBADD = $(top_builddir)/libsrc/meshing/libmesh.la
+-
+ # libinterface_la_LDFLAGS = -rdynamic
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/meshing/Makefile.am netgen-5.3.1_SRC_modif/libsrc/meshing/Makefile.am
+--- netgen-5.3.1_SRC_orig/libsrc/meshing/Makefile.am 2014-08-29 13:54:05.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/meshing/Makefile.am 2017-09-11 18:08:21.234311865 +0300
+@@ -15,7 +15,7 @@
+
+ METASOURCES = AUTO
+
+-lib_LTLIBRARIES = libmesh.la
++noinst_LTLIBRARIES = libmesh.la
+
+ libmesh_la_SOURCES = adfront2.cpp adfront3.cpp bisect.cpp boundarylayer.cpp \
+ clusters.cpp curvedelems.cpp delaunay.cpp delaunay2d.cpp \
+@@ -30,8 +30,5 @@
+ topology.cpp triarls.cpp validate.cpp zrefine.cpp bcfunctions.cpp \
+ parallelmesh.cpp paralleltop.cpp paralleltop.hpp basegeom.cpp
+
+-libmesh_la_LIBADD = $(top_builddir)/libsrc/linalg/libla.la \
+- $(top_builddir)/libsrc/gprim/libgprim.la \
+- $(top_builddir)/libsrc/general/libgen.la \
+- -lz
++libmesh_la_LIBADD = -lz
+
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/meshing/findip.hpp netgen-5.3.1_SRC_modif/libsrc/meshing/findip.hpp
+--- netgen-5.3.1_SRC_orig/libsrc/meshing/findip.hpp 2014-08-29 13:54:05.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/meshing/findip.hpp 2017-09-11 18:08:21.226312730 +0300
+@@ -75,6 +75,9 @@
+ static int timer = NgProfiler::CreateTimer ("FindInnerPoint");
+ NgProfiler::RegionTimer reg (timer);
+
++ if ( points.Size() < 3 )
++ return 0;
++
+ Array<Vec3d> a;
+ Array<double> c;
+ Mat<3> m, inv;
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/meshing/improve3.cpp netgen-5.3.1_SRC_modif/libsrc/meshing/improve3.cpp
+--- netgen-5.3.1_SRC_orig/libsrc/meshing/improve3.cpp 2014-08-29 13:54:05.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/meshing/improve3.cpp 2017-09-11 18:08:21.227312622 +0300
+@@ -1219,6 +1219,7 @@
+
+ tetused = 0;
+ tetused[0] = 1;
++ int nbtetused = 0;
+
+ for (int l = 2; l < nsuround; l++)
+ {
+@@ -1239,10 +1240,12 @@
+
+ tetused[k] = 1;
+ suroundpts[l] = newpi;
++ ++nbtetused;
+ }
+ }
+ }
+-
++ if ( nbtetused < nsuround )
++ continue;
+
+ bad1 = 0;
+ for (int k = 0; k < nsuround; k++)
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/meshing/meshtype.cpp netgen-5.3.1_SRC_modif/libsrc/meshing/meshtype.cpp
+--- netgen-5.3.1_SRC_orig/libsrc/meshing/meshtype.cpp 2014-08-29 13:54:05.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/meshing/meshtype.cpp 2017-09-11 18:08:21.227312622 +0300
+@@ -1,4 +1,5 @@
+ #include <mystdlib.h>
++#include <float.h> // to get DBL_MIN defined
+
+ #include "meshing.hpp"
+
+@@ -666,7 +667,8 @@
+
+ double det = trans.Det();
+
+- if (det <= 0)
++ // if (det <= 0)
++ if (det <= DBL_MIN) // avoid FPE
+ err += 1e12;
+ else
+ err += frob * frob / det;
+@@ -722,7 +724,8 @@
+
+ double det = trans(0,0)*trans(1,1)-trans(1,0)*trans(0,1);
+
+- if (det <= 0)
++ // if (det <= 0)
++ if (det <= DBL_MIN) // avoid FPE
+ {
+ dd = 0;
+ return 1e12;
+@@ -806,7 +809,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 <= 0)
++ if (det <= DBL_MIN) // avoid FPE
+ err += 1e12;
+ else
+ {
+@@ -856,7 +860,8 @@
+ frob /= 2;
+
+ double det = trans.Det();
+- if (det <= 0)
++ //if (det <= 0)
++ if (det <= DBL_MIN) // avoid FPE
+ err += 1e12;
+ else
+ err += frob * frob / det;
+@@ -1864,7 +1869,8 @@
+ case PYRAMID:
+ {
+ double noz = 1-p(2);
+- if (noz == 0.0) 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;
+@@ -2030,7 +2036,8 @@
+
+ double det = -trans.Det();
+
+- if (det <= 0)
++ //if (det <= 0)
++ if (det <= DBL_MIN) // avoid FPE
+ err += 1e12;
+ else
+ err += frob * frob * frob / det;
+@@ -2102,7 +2109,8 @@
+ ddet *= -1;
+
+
+- if (det <= 0)
++ //if (det <= 0)
++ if (det <= DBL_MIN) // avoid FPE
+ err += 1e12;
+ else
+ {
+@@ -2184,7 +2192,7 @@
+
+ det *= -1;
+
+- if (det <= 0)
++ if (det <= DBL_MIN)
+ err += 1e12;
+ else
+ {
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/meshing/meshtype.hpp netgen-5.3.1_SRC_modif/libsrc/meshing/meshtype.hpp
+--- netgen-5.3.1_SRC_orig/libsrc/meshing/meshtype.hpp 2014-08-29 13:54:05.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/meshing/meshtype.hpp 2017-09-11 18:08:21.228312514 +0300
+@@ -15,6 +15,7 @@
+ Classes for NETGEN
+ */
+
++class Mesh; // added due to compilation errors on some platforms
+
+
+ enum ELEMENT_TYPE {
+@@ -360,7 +361,7 @@
+ {
+ #ifdef DEBUG
+ if (typ != QUAD && typ != QUAD6 && typ != QUAD8)
+- PrintSysError ("element2d::GetNV not implemented for typ", typ)
++ PrintSysError ("element2d::GetNV not implemented for typ", typ);
+ #endif
+ return 4;
+ }
+@@ -618,7 +619,7 @@
+ return 8;
+ default:
+ #ifdef DEBUG
+- PrintSysError ("Element3d::GetNV not implemented for typ ", typ)
++ PrintSysError ("Element3d::GetNV not implemented for typ ", typ);
+ #endif
+ ;
+ }
+@@ -682,7 +683,7 @@
+ case PRISM12: return 5;
+ default:
+ #ifdef DEBUG
+- PrintSysError ("element3d::GetNFaces not implemented for typ", typ)
++ PrintSysError ("element3d::GetNFaces not implemented for typ", typ);
+ #endif
+ ;
+ }
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Makefile.am netgen-5.3.1_SRC_modif/libsrc/occ/Makefile.am
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Makefile.am 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Makefile.am 2017-09-11 18:08:21.234311865 +0300
+@@ -14,10 +14,10 @@
+
+ METASOURCES = AUTO
+
+-lib_LTLIBRARIES = libocc.la
++noinst_LTLIBRARIES = libocc.la
+
+ if NGGUI
+-lib_LTLIBRARIES += liboccvis.la
++lib_LTLIBRARIES = liboccvis.la
+ endif
+
+
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Inter2d.cxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Inter2d.cxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Inter2d.cxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Inter2d.cxx 2017-09-11 18:08:21.228312514 +0300
+@@ -47,9 +47,7 @@
+ #include <TopOpeBRep_EdgesIntersector.hxx>
+ #include <TopOpeBRep_Point2d.hxx>
+ #include <TopTools_ListIteratorOfListOfShape.hxx>
+-#include <TopTools_ListOfShape.hxx>
+ #include <TopTools_MapIteratorOfMapOfShape.hxx>
+-#include <TopTools_MapOfShape.hxx>
+ #include <TopoDS.hxx>
+ #include <TopoDS_Edge.hxx>
+ #include <TopoDS_Vertex.hxx>
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Inter2d.hxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Inter2d.hxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Inter2d.hxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Inter2d.hxx 2017-09-11 18:08:21.228312514 +0300
+@@ -27,7 +27,9 @@
+ #ifndef _Partition_Inter2d_HeaderFile
+ #define _Partition_Inter2d_HeaderFile
+
+-#ifndef _Handle_BRepAlgo_AsDes_HeaderFile
++#include <Standard_Version.hxx>
++
++#if OCC_VERSION_MAJOR < 7
+ #include <Handle_BRepAlgo_AsDes.hxx>
+ #endif
+ #ifndef _Standard_Real_HeaderFile
+@@ -36,11 +38,13 @@
+ #ifndef _Standard_Boolean_HeaderFile
+ #include <Standard_Boolean.hxx>
+ #endif
++
++#include <TopTools_MapOfShape.hxx>
++#include <TopTools_ListOfShape.hxx>
++
+ class BRepAlgo_AsDes;
+ class TopoDS_Face;
+-class TopTools_MapOfShape;
+ class TopoDS_Vertex;
+-class TopTools_ListOfShape;
+ class TopoDS_Edge;
+
+
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Inter3d.cxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Inter3d.cxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Inter3d.cxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Inter3d.cxx 2017-09-11 18:08:21.229312406 +0300
+@@ -48,7 +48,6 @@
+ #include <TopOpeBRepTool_BoxSort.hxx>
+ #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
+ #include <TopTools_ListIteratorOfListOfShape.hxx>
+-#include <TopTools_ListOfShape.hxx>
+ #include <TopoDS.hxx>
+ #include <TopoDS_Compound.hxx>
+ #include <TopoDS_Edge.hxx>
+@@ -206,7 +205,7 @@
+ Handle (Geom_Surface) S = BRep_Tool::Surface(F,L);
+
+ if (S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
+- S = (*(Handle_Geom_RectangularTrimmedSurface*)&S)->BasisSurface();
++ S = Handle(Geom_RectangularTrimmedSurface)::DownCast (S)->BasisSurface();
+ }
+ if (!S->IsUPeriodic() && !S->IsVPeriodic())
+ return;
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Inter3d.hxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Inter3d.hxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Inter3d.hxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Inter3d.hxx 2017-09-11 18:08:21.229312406 +0300
+@@ -27,7 +27,9 @@
+ #ifndef _Partition_Inter3d_HeaderFile
+ #define _Partition_Inter3d_HeaderFile
+
+-#ifndef _Handle_BRepAlgo_AsDes_HeaderFile
++#include <Standard_Version.hxx>
++
++#if OCC_VERSION_MAJOR < 7
+ #include <Handle_BRepAlgo_AsDes.hxx>
+ #endif
+ #ifndef _TopTools_DataMapOfShapeListOfShape_HeaderFile
+@@ -36,6 +38,9 @@
+ #ifndef _TopTools_MapOfShape_HeaderFile
+ #include <TopTools_MapOfShape.hxx>
+ #endif
++#ifndef _TopTools_ListOfShape_HeaderFile
++#include <TopTools_ListOfShape.hxx>
++#endif
+ #ifndef _TopTools_DataMapOfShapeShape_HeaderFile
+ #include <TopTools_DataMapOfShapeShape.hxx>
+ #endif
+@@ -43,10 +48,7 @@
+ #include <Standard_Boolean.hxx>
+ #endif
+ class BRepAlgo_AsDes;
+-class TopTools_ListOfShape;
+-class TopTools_DataMapOfShapeShape;
+ class TopoDS_Face;
+-class TopTools_MapOfShape;
+ class TopoDS_Shape;
+ class TopoDS_Vertex;
+ class TopoDS_Edge;
+@@ -83,13 +85,13 @@
+ void FacesPartition(const TopoDS_Face& F1,const TopoDS_Face& F2) ;
+ Standard_Boolean IsDone(const TopoDS_Face& F1,const TopoDS_Face& F2) const;
+ TopTools_MapOfShape& TouchedFaces() ;
+- Handle_BRepAlgo_AsDes AsDes() const;
++ Handle(BRepAlgo_AsDes) AsDes() const;
+ TopTools_MapOfShape& NewEdges() ;
+ Standard_Boolean HasSameDomainF(const TopoDS_Shape& F) const;
+ Standard_Boolean IsSameDomainF(const TopoDS_Shape& F1,const TopoDS_Shape& F2) const;
+ const TopTools_ListOfShape& SameDomain(const TopoDS_Face& F) const;
+ TopoDS_Vertex ReplaceSameDomainV(const TopoDS_Vertex& V,const TopoDS_Edge& E) const;
+- Handle_BRepAlgo_AsDes SectionEdgesAD() const;
++ Handle(BRepAlgo_AsDes) SectionEdgesAD() const;
+ Standard_Boolean IsSectionEdge(const TopoDS_Edge& E) const;
+ Standard_Boolean HasSectionEdge(const TopoDS_Face& F) const;
+ Standard_Boolean IsSplitOn(const TopoDS_Edge& NewE,const TopoDS_Edge& OldE,const TopoDS_Face& F) const;
+@@ -121,11 +123,11 @@
+
+ // Fields PRIVATE
+ //
+- Handle_BRepAlgo_AsDes myAsDes;
++ Handle(BRepAlgo_AsDes) myAsDes;
+ TopTools_DataMapOfShapeListOfShape myDone;
+ TopTools_MapOfShape myTouched;
+ TopTools_MapOfShape myNewEdges;
+- Handle_BRepAlgo_AsDes mySectionEdgesAD;
++ Handle(BRepAlgo_AsDes) mySectionEdgesAD;
+ TopTools_DataMapOfShapeListOfShape mySameDomainFM;
+ TopTools_DataMapOfShapeShape mySameDomainVM;
+
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Loop.hxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Loop.hxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Loop.hxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Loop.hxx 2017-09-11 18:08:21.229312406 +0300
+@@ -38,8 +38,6 @@
+ #endif
+ class TopoDS_Face;
+ class TopoDS_Edge;
+-class TopTools_ListOfShape;
+-
+
+ #ifndef _Standard_HeaderFile
+ #include <Standard.hxx>
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Loop2d.cxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Loop2d.cxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Loop2d.cxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Loop2d.cxx 2017-09-11 18:08:21.229312406 +0300
+@@ -210,7 +210,7 @@
+ Cc->D1(uc, PC, CTg1);
+ if (!isForward) CTg1.Reverse();
+
+- Standard_Real anglemin = 3 * PI, tolAng = 1.e-8;
++ Standard_Real anglemin = 3 * M_PI, tolAng = 1.e-8;
+
+ // select an edge whose first derivative is most left of CTg1
+ // ie an angle between Tg1 and CTg1 is least
+@@ -234,7 +234,7 @@
+ // -PI < angle < PI
+ Standard_Real angle = Tg1.Angle(CTg1);
+
+- if (PI - Abs(angle) <= tolAng)
++ if (M_PI - Abs(angle) <= tolAng)
+ {
+ // an angle is too close to PI; assure that an angle sign really
+ // reflects an edge position: +PI - an edge is worst,
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Loop2d.hxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Loop2d.hxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Loop2d.hxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Loop2d.hxx 2017-09-11 18:08:21.230312298 +0300
+@@ -24,7 +24,6 @@
+ #endif
+ class TopoDS_Face;
+ class TopoDS_Edge;
+-class TopTools_ListOfShape;
+ class BRepAlgo_Image;
+
+
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Loop3d.hxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Loop3d.hxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Loop3d.hxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Loop3d.hxx 2017-09-11 18:08:21.230312298 +0300
+@@ -13,6 +13,9 @@
+ #ifndef _TopTools_ListOfShape_HeaderFile
+ #include <TopTools_ListOfShape.hxx>
+ #endif
++#ifndef _TopTools_MapOfOrientedShape_HeaderFile
++#include <TopTools_MapOfOrientedShape.hxx>
++#endif
+ #ifndef _TopTools_IndexedDataMapOfShapeListOfShape_HeaderFile
+ #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
+ #endif
+@@ -23,8 +26,6 @@
+ #include <Standard_Real.hxx>
+ #endif
+ class TopoDS_Shape;
+-class TopTools_ListOfShape;
+-class TopTools_MapOfOrientedShape;
+ class TopoDS_Edge;
+ class TopoDS_Face;
+ class gp_Vec;
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Spliter.cxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Spliter.cxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Spliter.cxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Spliter.cxx 2017-09-11 18:08:21.230312298 +0300
+@@ -48,7 +48,6 @@
+ #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
+ #include <TopTools_IndexedMapOfShape.hxx>
+ #include <TopTools_ListIteratorOfListOfShape.hxx>
+-#include <TopTools_ListOfShape.hxx>
+ #include <TopTools_MapIteratorOfMapOfShape.hxx>
+ #include <TopTools_SequenceOfShape.hxx>
+
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Spliter.hxx netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Spliter.hxx
+--- netgen-5.3.1_SRC_orig/libsrc/occ/Partition_Spliter.hxx 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/Partition_Spliter.hxx 2017-09-11 18:08:21.231312190 +0300
+@@ -28,9 +28,6 @@
+ #ifndef _TopTools_DataMapOfShapeShape_HeaderFile
+ #include <TopTools_DataMapOfShapeShape.hxx>
+ #endif
+-#ifndef _Handle_BRepAlgo_AsDes_HeaderFile
+-#include <Handle_BRepAlgo_AsDes.hxx>
+-#endif
+ #ifndef _BRepAlgo_Image_HeaderFile
+ #include <BRepAlgo_Image.hxx>
+ #endif
+@@ -45,7 +42,6 @@
+ #endif
+ class BRepAlgo_AsDes;
+ class TopoDS_Shape;
+-class TopTools_ListOfShape;
+ class TopoDS_Edge;
+
+
+@@ -129,7 +125,7 @@
+ TopTools_DataMapOfShapeShape myFaceShapeMap;
+ TopTools_DataMapOfShapeShape myInternalFaces;
+ TopTools_DataMapOfShapeShape myIntNotClFaces;
+- Handle_BRepAlgo_AsDes myAsDes;
++ Handle(BRepAlgo_AsDes) myAsDes;
+ BRepAlgo_Image myImagesFaces;
+ BRepAlgo_Image myImagesEdges;
+ BRepAlgo_Image myImageShape;
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/occconstruction.cpp netgen-5.3.1_SRC_modif/libsrc/occ/occconstruction.cpp
+--- netgen-5.3.1_SRC_orig/libsrc/occ/occconstruction.cpp 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/occconstruction.cpp 2017-09-11 18:08:21.231312190 +0300
+@@ -28,7 +28,7 @@
+ #include <BRepAlgoAPI_Common.hxx>
+ #include <BRepAlgoAPI_Fuse.hxx>
+ #include <BRepAlgoAPI_Section.hxx>
+-#include <BRepOffsetAPI_Sewing.hxx>
++//#include <BRepOffsetAPI_Sewing.hxx>
+ //#include <BRepAlgo_Sewing.hxx>
+ #include <BRepOffsetAPI_MakeOffsetShape.hxx>
+ #include <ShapeFix_Shape.hxx>
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/occgenmesh.cpp netgen-5.3.1_SRC_modif/libsrc/occ/occgenmesh.cpp
+--- netgen-5.3.1_SRC_orig/libsrc/occ/occgenmesh.cpp 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/occgenmesh.cpp 2017-09-11 18:08:21.231312190 +0300
+@@ -171,8 +171,8 @@
+ if(h < 1e-4*maxside)\r
+ return;\r
+ \r
+-\r
+- if (h > 30) return;\r
++ // commented to restrict H on a large sphere for example
++ //if (h > 30) return;
+ }\r
+ \r
+ if (h < maxside && depth < 10)\r
+@@ -250,8 +250,8 @@
+ hvalue[0] = 0;\r
+ pnt = c->Value(s0);\r
+ \r
+- double olddist = 0;\r
+- double dist = 0;\r
++ //double olddist = 0; -- useless variables
++ //double dist = 0;
+ \r
+ int tmpVal = (int)(DIVIDEEDGESECTIONS);\r
+ \r
+@@ -259,15 +259,19 @@
+ {\r
+ oldpnt = pnt;\r
+ pnt = c->Value(s0+(i/double(DIVIDEEDGESECTIONS))*(s1-s0));\r
++ // -- no more than 1 segment per <edge length>/DIVIDEEDGESECTIONS
+ hvalue[i] = hvalue[i-1] +\r
++ // 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()))*\r
+- pnt.Distance(oldpnt);\r
++ pnt.Distance(oldpnt));
+ \r
+ //(*testout) << "mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z())) " << mesh.GetH(Point3d(pnt.X(), pnt.Y(), pnt.Z()))\r
+ // << " pnt.Distance(oldpnt) " << pnt.Distance(oldpnt) << endl;\r
+ \r
+- olddist = dist;\r
+- dist = pnt.Distance(oldpnt);\r
++ //olddist = dist; -- useless variables
++ //dist = pnt.Distance(oldpnt);
+ }\r
+ \r
+ // nsubedges = int(ceil(hvalue[DIVIDEEDGESECTIONS]));\r
+@@ -282,7 +286,10 @@
+ {\r
+ if (hvalue[i1]/hvalue[DIVIDEEDGESECTIONS]*nsubedges >= i)\r
+ {\r
+- params[i] = s0+(i1/double(DIVIDEEDGESECTIONS))*(s1-s0);\r
++ // -- 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]);\r
+ ps[i-1] = MeshPoint (Point3d(pnt.X(), pnt.Y(), pnt.Z()));\r
+ i++;\r
+@@ -326,6 +333,9 @@
+ (*testout) << "nedges = " << nedges << endl;\r
+ \r
+ double eps = 1e-6 * geom.GetBoundingBox().Diam();\r
++ const double eps2 = eps * eps; // -- small optimization
++
++ int first_vp = mesh.GetNP()+1; // -- to support SALOME sub-meshes
+ \r
+ for (int i = 1; i <= nvertices; i++)\r
+ {\r
+@@ -335,7 +345,8 @@
+ bool exists = 0;\r
+ if (merge_solids)\r
+ for (PointIndex pi = 1; pi <= mesh.GetNP(); pi++)\r
+- if ( Dist2 (mesh[pi], Point<3>(mp)) < eps*eps)\r
++ //if ( Dist2 (mesh[pi], Point<3>(mp)) < eps*eps)
++ if ( Dist2 (mesh[pi], Point<3>(mp)) < eps2 ) // -- small optimization
+ {\r
+ exists = 1;\r
+ break;\r
+@@ -365,6 +376,7 @@
+ {\r
+ TopoDS_Face face = TopoDS::Face(exp1.Current());\r
+ int facenr = geom.fmap.FindIndex(face);\r
++ if ( facenr < 1 ) continue; // -- to support SALOME sub-meshes
+ \r
+ if (face2solid[0][facenr-1] == 0)\r
+ face2solid[0][facenr-1] = solidnr;\r
+@@ -384,6 +396,7 @@
+ int facenr = 0;\r
+ int edgenr = 0;\r
+ \r
++ edgenr = mesh.GetNSeg(); // to support SALOME sub-meshes
+ \r
+ (*testout) << "faces = " << geom.fmap.Extent() << endl;\r
+ int curr = 0;\r
+@@ -445,6 +458,7 @@
+ //(*testout) << "ignoring degenerated edge" << endl;\r
+ continue;\r
+ }\r
++ if ( geom.emap.FindIndex(edge) < 1 ) continue; // to support SALOME sub-meshes
+ \r
+ if (geom.vmap.FindIndex(TopExp::FirstVertex (edge)) ==\r
+ geom.vmap.FindIndex(TopExp::LastVertex (edge)))\r
+@@ -477,20 +491,104 @@
+ \r
+ if (!merge_solids)\r
+ {\r
+- pnums[0] = geom.vmap.FindIndex (TopExp::FirstVertex (edge));\r
+- pnums[pnums.Size()-1] = geom.vmap.FindIndex (TopExp::LastVertex (edge));\r
++ //pnums[0] = geom.vmap.FindIndex (TopExp::FirstVertex (edge));
++ //pnums[pnums.Size()-1] = geom.vmap.FindIndex (TopExp::LastVertex (edge));
++ MeshPoint dfltP ( Point<3> ( 0, 0, 0 ) );
++ int *ipp[] = { &pnums[0], &pnums[pnums.Size()-1] };
++ TopoDS_Iterator vIt( edge, false );
++ TopoDS_Vertex v[2];
++ v[0] = TopoDS::Vertex( vIt.Value() ); vIt.Next();
++ v[1] = TopoDS::Vertex( vIt.Value() );
++ if ( v[0].Orientation() == TopAbs_REVERSED )
++ std::swap( v[0], v[1] );
++ for ( int i = 0; i < 2; ++i)
++ {
++ int &ip = *ipp[i];
++ ip = geom.vmap.FindIndex ( v[i] );
++ if ( ip == 0 || ip > nvertices )
++ {
++ int iv = ip;
++ if ( ip == 0 )
++ ip = iv = geom.vmap.Add( v[i] );
++ gp_Pnt pnt = BRep_Tool::Pnt( v[i] );
++ MeshPoint mp( Point<3>(pnt.X(), pnt.Y(), pnt.Z()) );
++ for (PointIndex pi = 1; pi < first_vp; pi++)
++ if ( Dist2 (mesh.Point(pi), Point<3>(mp)) < 1e-100 )
++ {
++ ip = pi;
++ if ( mesh.Point(ip).GetLayer() != dfltP.GetLayer() && mesh.Point(ip).GetLayer() != iv )
++ continue;
++ if ( mesh.Point(ip).GetLayer() == dfltP.GetLayer())
++ mesh.Point(ip) = MeshPoint( mesh.Point(ip), iv );
++ break;
++ }
+ }\r
+ else\r
+ {\r
+- Point<3> fp = occ2ng (BRep_Tool::Pnt (TopExp::FirstVertex (edge)));\r
+- Point<3> lp = occ2ng (BRep_Tool::Pnt (TopExp::LastVertex (edge)));\r
++ ip += first_vp - 1;
++ }
++ }
++ }
++ else
++ {
++ TopoDS_Iterator vIt( edge, false );
++ TopoDS_Vertex v1 = TopoDS::Vertex( vIt.Value() ); vIt.Next();
++ TopoDS_Vertex v2 = TopoDS::Vertex( vIt.Value() );
++ if ( v1.Orientation() == TopAbs_REVERSED )
++ std::swap( v1, v2 );
++ const bool isClosedEdge = v1.IsSame( v2 );
++
++ Point<3> fp = occ2ng (BRep_Tool::Pnt (v1));
++ Point<3> lp = occ2ng (BRep_Tool::Pnt (v2));
++ double tol2 = std::min( eps*eps, 1e-6 * Dist2( fp, lp ));
++ if ( isClosedEdge )
++ tol2 = BRep_Tool::Tolerance( v1 ) * BRep_Tool::Tolerance( v1 );
+ \r
+ pnums[0] = -1;\r
+ pnums.Last() = -1;\r
+ for (PointIndex pi = 1; pi < first_ep; pi++)\r
+ {\r
+- if (Dist2 (mesh[pi], fp) < eps*eps) pnums[0] = pi;\r
+- if (Dist2 (mesh[pi], lp) < eps*eps) pnums.Last() = pi;\r
++ if (Dist2 (mesh[pi], fp) < tol2) pnums[0] = pi;
++ if (Dist2 (mesh[pi], lp) < tol2) pnums.Last() = pi;
++ }
++ if (( isClosedEdge && pnums[0] != pnums.Last() ) ||
++ ( !isClosedEdge && pnums[0] == pnums.Last() ))
++ pnums[0] = pnums.Last() = -1;
++ if ( pnums[0] == -1 || pnums.Last() == -1 )
++ {
++ // take into account a possible large gap between a vertex and an edge curve
++ // end and a large vertex tolerance covering the whole edge
++ if ( pnums[0] == -1 )
++ {
++ double tol = BRep_Tool::Tolerance( v1 );
++ 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] = first_ep-1- nvertices + geom.vmap.FindIndex ( v1 );
++ }
++ if ( isClosedEdge )
++ {
++ pnums.Last() = pnums[0];
++ }
++ else
++ {
++ if ( pnums.Last() == -1 )
++ {
++ double tol = BRep_Tool::Tolerance( v2 );
++ 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() = first_ep-1-nvertices + geom.vmap.FindIndex ( v2 );
++ }
++
++ if ( Dist2( fp, mesh[PointIndex(pnums[0])]) >
++ Dist2( lp, mesh[PointIndex(pnums.Last())]))
++ std::swap( pnums[0], pnums.Last() );
++ }
+ }\r
+ }\r
+ \r
+@@ -500,17 +598,20 @@
+ bool exists = 0;\r
+ int j;\r
+ for (j = first_ep; j <= mesh.GetNP(); j++)\r
++ {
++ if (!merge_solids && mesh.Point(j).GetLayer() != geomedgenr ) continue; // to support SALOME fuse edges
+ if ((mesh.Point(j)-Point<3>(mp[i-1])).Length() < eps)\r
+ {\r
+ exists = 1;\r
+ break;\r
+ }\r
++ }
+ \r
+ if (exists)\r
+ pnums[i] = j;\r
+ else\r
+ {\r
+- mesh.AddPoint (mp[i-1]);\r
++ mesh.AddPoint (mp[i-1], geomedgenr); // to support SALOME fuse edges
+ (*testout) << "add meshpoint " << mp[i-1] << endl;\r
+ pnums[i] = mesh.GetNP();\r
+ }\r
+@@ -594,6 +695,8 @@
+ // (*testout) << "edge " << mesh.LineSegment(i).edgenr << " face " << mesh.LineSegment(i).si\r
+ // << " p1 " << mesh.LineSegment(i)[0] << " p2 " << mesh.LineSegment(i)[1] << endl;\r
+ // exit(10);\r
++ for (int j = 1; j <= mesh.GetNP(); j++) // to support SALOME fuse edges: set level to zero
++ mesh.Point(j) = MeshPoint( (Point<3>&) mesh.Point(j) );
+ \r
+ mesh.CalcSurfacesOfNode();\r
+ multithread.task = savetask;\r
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/occgeom.cpp netgen-5.3.1_SRC_modif/libsrc/occ/occgeom.cpp
+--- netgen-5.3.1_SRC_orig/libsrc/occ/occgeom.cpp 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/occgeom.cpp 2017-09-11 18:08:32.836058020 +0300
+@@ -8,6 +8,8 @@
+ #include "ShapeAnalysis_CheckSmallFace.hxx"\r
+ #include "ShapeAnalysis_DataMapOfShapeListOfReal.hxx"\r
+ #include "ShapeAnalysis_Surface.hxx"\r
++#include <BRepTopAdaptor_FClass2d.hxx> // -- to optimize Project() and FastProject()
++#include <TopAbs_State.hxx>
+ #include "BRepAlgoAPI_Fuse.hxx"\r
+ #include "BRepCheck_Analyzer.hxx"\r
+ #include "BRepLib.hxx"\r
+@@ -16,9 +18,16 @@
+ #include "ShapeFix_FixSmallFace.hxx"\r
+ #include "Partition_Spliter.hxx"\r
+ \r
+-\r
+ namespace netgen\r
+ {\r
++ // free data used to optimize Project() and FastProject()
++ OCCGeometry::~OCCGeometry()
++ {
++ NCollection_DataMap<int,BRepTopAdaptor_FClass2d*>::Iterator it(fclsmap);
++ for (; it.More(); it.Next())
++ delete it.Value();
++ }
++
+ void OCCGeometry :: PrintNrShapes ()\r
+ {\r
+ TopExp_Explorer e;\r
+@@ -112,13 +121,13 @@
+ double surfacecont = 0;\r
+ \r
+ {\r
+- Handle_ShapeBuild_ReShape rebuild = new ShapeBuild_ReShape;\r
++ Handle(ShapeBuild_ReShape) rebuild = new ShapeBuild_ReShape;
+ rebuild->Apply(shape);\r
+ for (exp1.Init (shape, TopAbs_EDGE); exp1.More(); exp1.Next())\r
+ {\r
+ TopoDS_Edge edge = TopoDS::Edge(exp1.Current());\r
+ if ( BRep_Tool::Degenerated(edge) )\r
+- rebuild->Remove(edge, false);\r
++ rebuild->Remove(edge);\r
+ }\r
+ shape = rebuild->Apply(shape);\r
+ }\r
+@@ -143,7 +152,7 @@
+ cout << endl << "- repairing faces" << endl;\r
+ \r
+ Handle(ShapeFix_Face) sff;\r
+- Handle_ShapeBuild_ReShape rebuild = new ShapeBuild_ReShape;\r
++ Handle(ShapeBuild_ReShape) rebuild = new ShapeBuild_ReShape;
+ rebuild->Apply(shape);\r
+ \r
+ \r
+@@ -187,7 +196,7 @@
+ cout << "(natural bounds added)" <<endl;\r
+ TopoDS_Face newface = sff->Face();\r
+ \r
+- rebuild->Replace(face, newface, Standard_False);\r
++ rebuild->Replace(face, newface);\r
+ }\r
+ \r
+ // Set the original colour of the face to the newly created \r
+@@ -200,13 +209,13 @@
+ \r
+ \r
+ {\r
+- Handle_ShapeBuild_ReShape rebuild = new ShapeBuild_ReShape;\r
++ Handle(ShapeBuild_ReShape) rebuild = new ShapeBuild_ReShape;
+ rebuild->Apply(shape);\r
+ for (exp1.Init (shape, TopAbs_EDGE); exp1.More(); exp1.Next())\r
+ {\r
+ TopoDS_Edge edge = TopoDS::Edge(exp1.Current());\r
+ if ( BRep_Tool::Degenerated(edge) )\r
+- rebuild->Remove(edge, false);\r
++ rebuild->Remove(edge);\r
+ }\r
+ shape = rebuild->Apply(shape);\r
+ }\r
+@@ -217,7 +226,7 @@
+ cout << endl << "- fixing small edges" << endl;\r
+ \r
+ Handle(ShapeFix_Wire) sfw;\r
+- Handle_ShapeBuild_ReShape rebuild = new ShapeBuild_ReShape;\r
++ Handle(ShapeBuild_ReShape) rebuild = new ShapeBuild_ReShape;
+ rebuild->Apply(shape);\r
+ \r
+ \r
+@@ -270,7 +279,7 @@
+ if(replace)\r
+ {\r
+ TopoDS_Wire newwire = sfw->Wire();\r
+- rebuild->Replace(oldwire, newwire, Standard_False);\r
++ rebuild->Replace(oldwire, newwire);\r
+ }\r
+ \r
+ //delete sfw; sfw = NULL;\r
+@@ -284,7 +293,7 @@
+ \r
+ {\r
+ BuildFMap();\r
+- Handle_ShapeBuild_ReShape rebuild = new ShapeBuild_ReShape;\r
++ Handle(ShapeBuild_ReShape) rebuild = new ShapeBuild_ReShape;
+ rebuild->Apply(shape);\r
+ \r
+ for (exp1.Init (shape, TopAbs_EDGE); exp1.More(); exp1.Next())\r
+@@ -300,7 +309,7 @@
+ cout << "removing degenerated edge " << emap.FindIndex(edge)\r
+ << " from vertex " << vmap.FindIndex(TopExp::FirstVertex (edge))\r
+ << " to vertex " << vmap.FindIndex(TopExp::LastVertex (edge)) << endl;\r
+- rebuild->Remove(edge, false);\r
++ rebuild->Remove(edge);\r
+ }\r
+ }\r
+ }\r
+@@ -312,13 +321,13 @@
+ \r
+ \r
+ {\r
+- Handle_ShapeBuild_ReShape rebuild = new ShapeBuild_ReShape;\r
++ Handle(ShapeBuild_ReShape) rebuild = new ShapeBuild_ReShape;
+ rebuild->Apply(shape);\r
+ for (exp1.Init (shape, TopAbs_EDGE); exp1.More(); exp1.Next())\r
+ {\r
+ TopoDS_Edge edge = TopoDS::Edge(exp1.Current());\r
+ if ( BRep_Tool::Degenerated(edge) )\r
+- rebuild->Remove(edge, false);\r
++ rebuild->Remove(edge);\r
+ }\r
+ shape = rebuild->Apply(shape);\r
+ }\r
+@@ -438,13 +447,13 @@
+ \r
+ \r
+ {\r
+- Handle_ShapeBuild_ReShape rebuild = new ShapeBuild_ReShape;\r
++ Handle(ShapeBuild_ReShape) rebuild = new ShapeBuild_ReShape;
+ rebuild->Apply(shape);\r
+ for (exp1.Init (shape, TopAbs_EDGE); exp1.More(); exp1.Next())\r
+ {\r
+ TopoDS_Edge edge = TopoDS::Edge(exp1.Current());\r
+ if ( BRep_Tool::Degenerated(edge) )\r
+- rebuild->Remove(edge, false);\r
++ rebuild->Remove(edge);\r
+ }\r
+ shape = rebuild->Apply(shape);\r
+ }\r
+@@ -483,9 +492,9 @@
+ TopoDS_Solid solid = TopoDS::Solid(exp0.Current());\r
+ TopoDS_Solid newsolid = solid;\r
+ BRepLib::OrientClosedSolid (newsolid);\r
+- Handle_ShapeBuild_ReShape rebuild = new ShapeBuild_ReShape;\r
++ Handle(ShapeBuild_ReShape) rebuild = new ShapeBuild_ReShape;
+ // rebuild->Apply(shape);\r
+- rebuild->Replace(solid, newsolid, Standard_False);\r
++ rebuild->Replace(solid, newsolid);\r
+ TopoDS_Shape newshape = rebuild->Apply(shape, TopAbs_COMPSOLID);//, 1);\r
+ // TopoDS_Shape newshape = rebuild->Apply(shape);\r
+ shape = newshape;\r
+@@ -906,8 +915,8 @@
+ TopoDS_Solid solid = TopoDS::Solid(exp0.Current());\r
+ TopoDS_Solid newsolid = solid;\r
+ BRepLib::OrientClosedSolid (newsolid);\r
+- Handle_ShapeBuild_ReShape rebuild = new ShapeBuild_ReShape;\r
+- rebuild->Replace(solid, newsolid, Standard_False);\r
++ Handle(ShapeBuild_ReShape) rebuild = new ShapeBuild_ReShape;
++ rebuild->Replace(solid, newsolid);\r
+ \r
+ TopoDS_Shape newshape = rebuild->Apply(shape, TopAbs_SHAPE, 1);\r
+ shape = newshape;\r
+@@ -951,25 +960,58 @@
+ }\r
+ \r
+ \r
++ // returns a projector and a classifier for the given surface
++ void OCCGeometry::GetFaceTools(int surfi, Handle(ShapeAnalysis_Surface)& proj,
++ BRepTopAdaptor_FClass2d*& cls) const
++ {
++ //MSV: organize caching projector in the map
++ if (fprjmap.IsBound(surfi))
++ {
++ proj = fprjmap.Find(surfi);
++ cls = fclsmap.Find(surfi);
++ }
++ else
++ {
++ const TopoDS_Face& aFace = TopoDS::Face(fmap(surfi));
++ Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
++ proj = new ShapeAnalysis_Surface(aSurf);
++ fprjmap.Bind(surfi, proj);
++ cls = new BRepTopAdaptor_FClass2d(aFace,Precision::Confusion());
++ fclsmap.Bind(surfi, cls);
++ }
++ }
+ \r
+-\r
+- void OCCGeometry :: Project (int surfi, Point<3> & p) const\r
++ // void OCCGeometry :: Project (int surfi, Point<3> & p) const
++ bool OCCGeometry :: Project (int surfi, Point<3> & p, double& u, double& v) const
+ {\r
+ static int cnt = 0;\r
+ if (++cnt % 1000 == 0) cout << "Project cnt = " << cnt << endl;\r
+ \r
+ gp_Pnt pnt(p(0), p(1), p(2));\r
+ \r
+- double u,v;\r
+- Handle( Geom_Surface ) thesurf = BRep_Tool::Surface(TopoDS::Face(fmap(surfi)));\r
+- Handle( ShapeAnalysis_Surface ) su = new ShapeAnalysis_Surface( thesurf );\r
+- gp_Pnt2d suval = su->ValueOfUV ( pnt, BRep_Tool::Tolerance( TopoDS::Face(fmap(surfi)) ) );\r
+- suval.Coord( u, v);\r
+- pnt = thesurf->Value( u, v );\r
+-\r
++ // -- 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);
++\r
++ gp_Pnt2d p2d = proj->ValueOfUV(pnt, Precision::Confusion());
++ if (cls->Perform(p2d) == TopAbs_OUT)
++ {
++ return false;
++ }
++ pnt = proj->Value(p2d);
++ p2d.Coord(u, v);
+ \r
+ p = Point<3> (pnt.X(), pnt.Y(), pnt.Z());\r
+ \r
++ return true;
+ }\r
+ \r
+ \r
+@@ -979,54 +1021,69 @@
+ {\r
+ gp_Pnt p(ap(0), ap(1), ap(2));\r
+ \r
+- Handle(Geom_Surface) surface = BRep_Tool::Surface(TopoDS::Face(fmap(surfi)));\r
+-\r
+- gp_Pnt x = surface->Value (u,v);\r
+-\r
+- if (p.SquareDistance(x) <= sqr(PROJECTION_TOLERANCE)) return true;\r
+-\r
+- gp_Vec du, dv;\r
+-\r
+- surface->D1(u,v,x,du,dv);\r
+-\r
+- int count = 0;\r
+-\r
+- gp_Pnt xold;\r
+- gp_Vec n;\r
+- double det, lambda, mu;\r
+-\r
+- do {\r
+- count++;\r
+-\r
+- n = du^dv;\r
+-\r
+- det = Det3 (n.X(), du.X(), dv.X(),\r
+- n.Y(), du.Y(), dv.Y(),\r
+- n.Z(), du.Z(), dv.Z());\r
+-\r
+- if (det < 1e-15) return false;\r
+-\r
+- lambda = Det3 (n.X(), p.X()-x.X(), dv.X(),\r
+- n.Y(), p.Y()-x.Y(), dv.Y(),\r
+- n.Z(), p.Z()-x.Z(), dv.Z())/det;\r
+-\r
+- mu = Det3 (n.X(), du.X(), p.X()-x.X(),\r
+- n.Y(), du.Y(), p.Y()-x.Y(),\r
+- n.Z(), du.Z(), p.Z()-x.Z())/det;\r
+-\r
+- u += lambda;\r
+- v += mu;\r
+-\r
+- xold = x;\r
+- surface->D1(u,v,x,du,dv);\r
+-\r
+- } while (xold.SquareDistance(x) > sqr(PROJECTION_TOLERANCE) && count < 50);\r
+-\r
+- // (*testout) << "FastProject count: " << count << endl;\r
+-\r
+- if (count == 50) return false;\r
+-\r
+- ap = Point<3> (x.X(), x.Y(), x.Z());\r
++ // -- 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);
++\r
++ gp_Pnt2d p2d = proj->NextValueOfUV(gp_Pnt2d(u,v), p, Precision::Confusion());
++ if (cls->Perform(p2d) == TopAbs_OUT)
++ {
++ //cout << "Projection fails" << endl;
++ return false;
++ }
++\r
++ p = proj->Value(p2d);
++ p2d.Coord(u, v);
++ ap = Point<3> (p.X(), p.Y(), p.Z());
+ \r
+ return true;\r
+ }\r
+@@ -1038,9 +1095,9 @@
+ {\r
+ cout << "writing stl..."; cout.flush();\r
+ StlAPI_Writer writer;\r
+- writer.RelativeMode() = Standard_False;\r
++ //writer.RelativeMode() = Standard_False;
+ \r
+- writer.SetDeflection(0.02);\r
++ //writer.SetDeflection(0.02);
+ writer.Write(shape,filename);\r
+ \r
+ cout << "done" << endl;\r
+@@ -1059,10 +1116,10 @@
+ occgeo = new OCCGeometry;\r
+ \r
+ // Initiate a dummy XCAF Application to handle the IGES XCAF Document\r
+- static Handle_XCAFApp_Application dummy_app = XCAFApp_Application::GetApplication();\r
++ static Handle(XCAFApp_Application) dummy_app = XCAFApp_Application::GetApplication();
+ \r
+ // Create an XCAF Document to contain the IGES file itself\r
+- Handle_TDocStd_Document iges_doc;\r
++ Handle(TDocStd_Document) iges_doc;
+ \r
+ // Check if a IGES File is already open under this handle, if so, close it to prevent\r
+ // Segmentation Faults when trying to create a new document\r
+@@ -1089,8 +1146,8 @@
+ reader.Transfer(iges_doc);\r
+ \r
+ // Read in the shape(s) and the colours present in the IGES File\r
+- Handle_XCAFDoc_ShapeTool iges_shape_contents = XCAFDoc_DocumentTool::ShapeTool(iges_doc->Main());\r
+- Handle_XCAFDoc_ColorTool iges_colour_contents = XCAFDoc_DocumentTool::ColorTool(iges_doc->Main());\r
++ Handle(XCAFDoc_ShapeTool) iges_shape_contents = XCAFDoc_DocumentTool::ShapeTool(iges_doc->Main());
++ Handle(XCAFDoc_ColorTool) iges_colour_contents = XCAFDoc_DocumentTool::ColorTool(iges_doc->Main());
+ \r
+ TDF_LabelSequence iges_shapes;\r
+ iges_shape_contents->GetShapes(iges_shapes);\r
+@@ -1137,10 +1194,10 @@
+ occgeo = new OCCGeometry;\r
+ \r
+ // Initiate a dummy XCAF Application to handle the STEP XCAF Document\r
+- static Handle_XCAFApp_Application dummy_app = XCAFApp_Application::GetApplication();\r
++ static Handle(XCAFApp_Application) dummy_app = XCAFApp_Application::GetApplication();
+ \r
+ // Create an XCAF Document to contain the STEP file itself\r
+- Handle_TDocStd_Document step_doc;\r
++ Handle(TDocStd_Document) step_doc;
+ \r
+ // Check if a STEP File is already open under this handle, if so, close it to prevent\r
+ // Segmentation Faults when trying to create a new document\r
+@@ -1167,8 +1224,8 @@
+ reader.Transfer(step_doc);\r
+ \r
+ // Read in the shape(s) and the colours present in the STEP File\r
+- Handle_XCAFDoc_ShapeTool step_shape_contents = XCAFDoc_DocumentTool::ShapeTool(step_doc->Main());\r
+- Handle_XCAFDoc_ColorTool step_colour_contents = XCAFDoc_DocumentTool::ColorTool(step_doc->Main());\r
++ Handle(XCAFDoc_ShapeTool) step_shape_contents = XCAFDoc_DocumentTool::ShapeTool(step_doc->Main());
++ Handle(XCAFDoc_ColorTool) step_colour_contents = XCAFDoc_DocumentTool::ColorTool(step_doc->Main());
+ \r
+ TDF_LabelSequence step_shapes;\r
+ step_shape_contents->GetShapes(step_shapes);\r
+@@ -1221,7 +1278,7 @@
+ // Fixed a bug in the OpenCascade XDE Colour handling when \r
+ // opening BREP Files, since BREP Files have no colour data.\r
+ // Hence, the face_colours Handle needs to be created as a NULL handle.\r
+- occgeo->face_colours = Handle_XCAFDoc_ColorTool();\r
++ occgeo->face_colours = Handle(XCAFDoc_ColorTool)();
+ occgeo->face_colours.Nullify();\r
+ occgeo->changed = 1;\r
+ occgeo->BuildFMap();\r
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/occgeom.hpp netgen-5.3.1_SRC_modif/libsrc/occ/occgeom.hpp
+--- netgen-5.3.1_SRC_orig/libsrc/occ/occgeom.hpp 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/occgeom.hpp 2017-09-11 18:08:21.233311974 +0300
+@@ -15,8 +15,8 @@
+ #include "Geom_Curve.hxx"\r
+ #include "Geom2d_Curve.hxx"\r
+ #include "Geom_Surface.hxx"\r
+-#include "GeomAPI_ProjectPointOnSurf.hxx"\r
+-#include "GeomAPI_ProjectPointOnCurve.hxx"\r
++// #include "GeomAPI_ProjectPointOnSurf.hxx"
++// #include "GeomAPI_ProjectPointOnCurve.hxx"
+ #include "BRepTools.hxx"\r
+ #include "TopExp.hxx"\r
+ #include "BRepBuilderAPI_MakeVertex.hxx"\r
+@@ -42,8 +42,8 @@
+ #include "Geom_Curve.hxx"\r
+ #include "Geom2d_Curve.hxx"\r
+ #include "Geom_Surface.hxx"\r
+-#include "GeomAPI_ProjectPointOnSurf.hxx"\r
+-#include "GeomAPI_ProjectPointOnCurve.hxx"\r
++// #include "GeomAPI_ProjectPointOnSurf.hxx"
++// #include "GeomAPI_ProjectPointOnCurve.hxx"
+ #include "TopoDS_Wire.hxx"\r
+ #include "BRepTools_WireExplorer.hxx"\r
+ #include "BRepTools.hxx"\r
+@@ -68,18 +68,26 @@
+ #include "IGESToBRep_Reader.hxx"\r
+ #include "Interface_Static.hxx"\r
+ #include "GeomAPI_ExtremaCurveCurve.hxx"\r
+-#include "Standard_ErrorHandler.hxx"\r
++//#include "Standard_ErrorHandler.hxx"
+ #include "Standard_Failure.hxx"\r
+ #include "ShapeUpgrade_ShellSewing.hxx"\r
+ #include "ShapeFix_Shape.hxx"\r
+ #include "ShapeFix_Wireframe.hxx"\r
++#include <Standard_Version.hxx>
++#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) <= 0x060702
++// porting to OCCT6.7.3
+ #include "BRepMesh.hxx"\r
++#endif
+ #include "BRepMesh_IncrementalMesh.hxx"\r
+ #include "BRepBndLib.hxx"\r
+ #include "Bnd_Box.hxx"\r
+ #include "ShapeAnalysis.hxx"\r
+ #include "ShapeBuild_ReShape.hxx"\r
+ \r
++// -- Optimization: to use cached projector and classifier
++#include <NCollection_DataMap.hxx>
++class ShapeAnalysis_Surface;
++class BRepTopAdaptor_FClass2d;
+ \r
+ // Philippose - 29/01/2009\r
+ // OpenCascade XDE Support\r
+@@ -192,6 +200,9 @@
+ class OCCGeometry : public NetgenGeometry\r
+ {\r
+ Point<3> center;\r
++ // -- Optimization: to use cached projector and classifier
++ mutable NCollection_DataMap<int,Handle(ShapeAnalysis_Surface)> fprjmap;
++ mutable NCollection_DataMap<int,BRepTopAdaptor_FClass2d*> fclsmap;
+ \r
+ public:\r
+ TopoDS_Shape shape;\r
+@@ -203,7 +214,7 @@
+ // OpenCascade XDE Support\r
+ // XCAF Handle to make the face colours available to the rest of\r
+ // the system\r
+- Handle_XCAFDoc_ColorTool face_colours;\r
++ Handle(XCAFDoc_ColorTool) face_colours;
+ \r
+ mutable int changed;\r
+ Array<int> facemeshstatus;\r
+@@ -247,6 +258,8 @@
+ virtual void Save (string filename) const;\r
+ \r
+ \r
++ ~OCCGeometry(); // -- to free cached projector and classifier
++
+ void BuildFMap();\r
+ \r
+ Box<3> GetBoundingBox()\r
+@@ -266,9 +279,14 @@
+ Point<3> Center()\r
+ { return center;}\r
+ \r
+- void Project (int surfi, Point<3> & p) const;\r
++ // 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;\r
+ \r
++ // -- Optimization: to use cached projector and classifier
++ void GetFaceTools(int surfi, Handle(ShapeAnalysis_Surface)& proj,
++ BRepTopAdaptor_FClass2d*& cls) const;
++
+ OCCSurface GetSurface (int surfi)\r
+ {\r
+ cout << "OCCGeometry::GetSurface using PLANESPACE" << endl;\r
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/occmeshsurf.cpp netgen-5.3.1_SRC_modif/libsrc/occ/occmeshsurf.cpp
+--- netgen-5.3.1_SRC_orig/libsrc/occ/occmeshsurf.cpp 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/occmeshsurf.cpp 2017-09-11 18:08:21.233311974 +0300
+@@ -6,6 +6,7 @@
+ #include <meshing.hpp>
+ #include <GeomLProp_SLProps.hxx>
+ #include <ShapeAnalysis_Surface.hxx>
++#include <GeomAPI_ProjectPointOnCurve.hxx> // -- moved here from occgeom.hpp
+
+
+ namespace netgen
+@@ -96,13 +97,16 @@
+
+ n.Normalize();
+ }
+- else
++ else if ( lprop.IsNormalDefined() )
+ {
+ n(0)=lprop.Normal().X();
+ n(1)=lprop.Normal().Y();
+ n(2)=lprop.Normal().Z();
+ }
+-
++ else
++ {
++ n = 0;
++ }
+ if(glob_testout)
+ {
+ (*testout) << "u " << geominfo.u << " v " << geominfo.v
+@@ -434,23 +438,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);
+ }
+
+
+ int MeshOptimize2dOCCSurfaces :: ProjectPointGI (INDEX surfind, Point<3> & p, PointGeomInfo & gi) const
+ {
+- 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))
+- {
++ // -- 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);
++ else
++ ok = geometry.Project (surfind, hp, gi.u, gi.v);
+ p = hp;
+- return 1;
+- }
+- ProjectPoint (surfind, p);
+- return CalcPointGeomInfo (surfind, gi, p);
++ return ok;
+ }
+
+
+@@ -680,7 +694,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;
+@@ -689,7 +704,7 @@
+ }
+
+ newp = hnewp;
+- }
++ }//; -- to compile with -Wall -pedantic
+
+
+ void OCCRefinementSurfaces ::
+@@ -708,14 +723,18 @@
+ hnewp = Point<3> (pnt.X(), pnt.Y(), pnt.Z());
+ newp = hnewp;
+ newgi = ap1;
+- };
++ }//; -- to compile with -Wall -pedantic
+
+
+ void OCCRefinementSurfaces :: ProjectToSurface (Point<3> & p, int surfi) const
+ {
+ if (surfi > 0)
+- geometry.Project (surfi, p);
+- };
++ //geometry.Project (surfi, p);
++ {
++ double u, v;
++ geometry.Project (surfi, p, u, v);
++ }
++ }//; -- to compile with -Wall -pedantic
+
+ void OCCRefinementSurfaces :: ProjectToSurface (Point<3> & p, int surfi, PointGeomInfo & gi) const
+ {
+@@ -723,9 +742,10 @@
+ if (!geometry.FastProject (surfi, p, gi.u, gi.v))
+ {
+ cout << "Fast projection to surface fails! Using OCC projection" << endl;
+- geometry.Project (surfi, p);
++ double u, v;
++ geometry.Project (surfi, p, u, v);
++ }
+ }
+- };
+
+
+
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/occpkg.cpp netgen-5.3.1_SRC_modif/libsrc/occ/occpkg.cpp
+--- netgen-5.3.1_SRC_orig/libsrc/occ/occpkg.cpp 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/occpkg.cpp 2017-09-11 18:08:32.837057913 +0300
+@@ -485,7 +485,7 @@
+ if (strcmp (argv[2], "Wire") == 0) sh = occgeometry->wmap(nr);\r
+ if (strcmp (argv[2], "Edge") == 0) sh = occgeometry->emap(nr);\r
+ \r
+- rebuild->Replace(sh, sh.Reversed(), Standard_False);\r
++ rebuild->Replace(sh, sh.Reversed());\r
+ \r
+ TopoDS_Shape newshape = rebuild->Apply(occgeometry->shape, TopAbs_SHELL, 1);\r
+ occgeometry->shape = newshape;\r
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/occ/utilities.h netgen-5.3.1_SRC_modif/libsrc/occ/utilities.h
+--- netgen-5.3.1_SRC_orig/libsrc/occ/utilities.h 2014-08-29 13:54:03.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/occ/utilities.h 2017-09-11 18:08:21.233311974 +0300
+@@ -33,6 +33,7 @@
+
+ #include <string>
+ #include <iostream>
++#include <iomanip>
+ #include <cstdlib>
+ // #include "SALOME_Log.hxx"
+
+diff -Naur netgen-5.3.1_SRC_orig/libsrc/stlgeom/Makefile.am netgen-5.3.1_SRC_modif/libsrc/stlgeom/Makefile.am
+--- netgen-5.3.1_SRC_orig/libsrc/stlgeom/Makefile.am 2014-08-29 13:54:05.000000000 +0400
++++ netgen-5.3.1_SRC_modif/libsrc/stlgeom/Makefile.am 2017-09-11 18:08:21.235311757 +0300
+@@ -4,10 +4,10 @@
+ AM_CPPFLAGS = -I$(top_srcdir)/libsrc/include $(TCL_INCLUDES)
+ METASOURCES = AUTO
+
+-lib_LTLIBRARIES = libstl.la
++noinst_LTLIBRARIES = libstl.la
+
+ if NGGUI
+-lib_LTLIBRARIES += libstlvis.la
++lib_LTLIBRARIES = libstlvis.la
+ endif
+
+ libstl_la_SOURCES = meshstlsurface.cpp stlgeom.cpp stlgeomchart.cpp \
+@@ -16,6 +16,5 @@
+
+ libstlvis_la_SOURCES = stlpkg.cpp vsstl.cpp
+ libstlvis_la_LIBADD = libstl.la
+-libstl_la_LIBADD = $(top_builddir)/libsrc/meshing/libmesh.la
+ # libstlvis_la_LIBADD = libstl.la $(top_builddir)/libsrc/linalg/libla.la
+
+diff -Naur netgen-5.3.1_SRC_orig/nglib/Makefile.am netgen-5.3.1_SRC_modif/nglib/Makefile.am
+--- netgen-5.3.1_SRC_orig/nglib/Makefile.am 2014-08-29 13:54:00.000000000 +0400
++++ netgen-5.3.1_SRC_modif/nglib/Makefile.am 2017-09-11 18:08:21.235311757 +0300
+@@ -14,6 +14,9 @@
+ $(top_builddir)/libsrc/stlgeom/libstl.la \
+ $(top_builddir)/libsrc/occ/libocc.la \
+ $(top_builddir)/libsrc/meshing/libmesh.la \
++ $(top_builddir)/libsrc/general/libgen.la \
++ $(top_builddir)/libsrc/gprim/libgprim.la \
++ $(top_builddir)/libsrc/linalg/libla.la
+ $(OCCLIBS) $(MPI_LIBS)
+
+ libnglib_la_LDFLAGS = -avoid-version
+diff -Naur netgen-5.3.1_SRC_orig/nglib/nglib.h netgen-5.3.1_SRC_modif/nglib/nglib.h
+--- netgen-5.3.1_SRC_orig/nglib/nglib.h 2014-08-29 13:54:00.000000000 +0400
++++ netgen-5.3.1_SRC_modif/nglib/nglib.h 2017-09-11 18:08:21.233311974 +0300
+@@ -24,7 +24,7 @@
+ // Philippose - 14.02.2009\r
+ // Modifications for creating a DLL in Windows\r
+ #ifdef WIN32\r
+- #ifdef NGLIB_EXPORTS || nglib_EXPORTS\r
++ #if defined NGLIB_EXPORTS || defined nglib_EXPORTS
+ #define DLL_HEADER __declspec(dllexport)\r
+ #else\r
+ #define DLL_HEADER __declspec(dllimport)\r