Salome HOME
Using provider for occgeom and nglib
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_NETGEN_2D_ONLY.cxx
index 473b5a00eda15c9f8b08f1fee900b9b41f97ad9e..a735a1fbb9f4f40f198a34ea85fb9edc6c841612 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -25,6 +25,7 @@
 
 #include "NETGENPlugin_Mesher.hxx"
 #include "NETGENPlugin_Hypothesis_2D.hxx"
+#include "NETGENPlugin_Provider.hxx"
 
 #include <SMDS_MeshElement.hxx>
 #include <SMDS_MeshNode.hxx>
@@ -63,16 +64,11 @@ namespace nglib {
 #include <meshing.hpp>
 //#include <meshtype.hpp>
 namespace netgen {
-#ifdef NETGEN_V5
-  extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, MeshingParameters&, int, int);
-#else
-  extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, int, int, char*);
-#endif
-#ifdef NETGEN_V5 && WIN32
-  DLL_HEADER 
-#endif
+  NETGENPLUGIN_DLL_HEADER
   extern MeshingParameters mparam;
+#ifdef NETGEN_V5
   extern void OCCSetLocalMeshSize(OCCGeometry & geom, Mesh & mesh);
+#endif
 }
 
 using namespace std;
@@ -81,17 +77,16 @@ using namespace nglib;
 
 //=============================================================================
 /*!
- *  
+ *
  */
 //=============================================================================
 
 NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY(int        hypId,
-                                                         int        studyId,
                                                          SMESH_Gen* gen)
-  : SMESH_2D_Algo(hypId, studyId, gen)
+  : SMESH_2D_Algo(hypId, gen)
 {
   _name = "NETGEN_2D_ONLY";
-  
+
   _shapeType = (1 << TopAbs_FACE);// 1 bit /shape type
   _onlyUnaryInput = false; // treat all FACEs at once
 
@@ -109,18 +104,18 @@ NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY(int        hypId,
 
 //=============================================================================
 /*!
- *  
+ *
  */
 //=============================================================================
 
 NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY()
 {
-  MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY");
+  //MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY");
 }
 
 //=============================================================================
 /*!
- *  
+ *
  */
 //=============================================================================
 
@@ -134,6 +129,7 @@ bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh&         aMesh,
   _hypParameters = 0;
   _progressByTic = -1;
 
+
   const list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
 
   if (hyps.empty())
@@ -170,7 +166,7 @@ bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh&         aMesh,
 
   int nbHyps = bool(_hypMaxElementArea) + bool(_hypLengthFromEdges) + bool(_hypParameters );
   if ( nbHyps > 1 )
-    aStatus = HYP_CONCURENT;
+    aStatus = HYP_CONCURRENT;
   else if ( hasVL )
     error( StdMeshers_ViscousLayers2D::CheckHypothesis( aMesh, aShape, aStatus ));
   else
@@ -232,6 +228,11 @@ bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh&         aMesh,
 bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
                                           const TopoDS_Shape& aShape)
 {
+  aMesh.Lock();
+  SMESH_Hypothesis::Hypothesis_Status hypStatus;
+  this->CheckHypothesis(aMesh, aShape, hypStatus);
+  aMesh.Unlock();
+
   netgen::multithread.terminate = 0;
   //netgen::multithread.task = "Surface meshing";
 
@@ -239,12 +240,14 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
   SMESH_MesherHelper helper(aMesh);
   helper.SetElementsOnShape( true );
 
-  NETGENPlugin_NetgenLibWrapper ngLib;
-  ngLib._isComputeOk = false;
+  NETGENPlugin_NetgenLibWrapper *ngLib;
+  int id_ngLib = nglib_provider.take(&ngLib);
+  ngLib->_isComputeOk = false;
 
   netgen::Mesh   ngMeshNoLocSize;
-  netgen::Mesh * ngMeshes[2] = { (netgen::Mesh*) ngLib._ngMesh,  & ngMeshNoLocSize };
-  netgen::OCCGeometry occgeoComm;
+  netgen::Mesh * ngMeshes[2] = { (netgen::Mesh*) ngLib->_ngMesh,  & ngMeshNoLocSize };
+  netgen::OCCGeometry *occgeoComm;
+  int id_occgeoComm = occgeom_provider.take(&occgeoComm);
 
   // min / max sizes are set as follows:
   // if ( _hypParameters )
@@ -258,7 +261,6 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
   // else
   //    min = aMesher.GetDefaultMinSize()
   //    max = max segment len of a FACE
-
   NETGENPlugin_Mesher aMesher( &aMesh, aShape, /*isVolume=*/false);
   aMesher.SetParameters( _hypParameters ); // _hypParameters -> netgen::mparam
   const bool toOptimize = _hypParameters ? _hypParameters->GetOptimize() : true;
@@ -276,26 +278,35 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
   if ( isCommonLocalSize ) // compute common local size in ngMeshes[0]
   {
     //list< SMESH_subMesh* > meshedSM[4]; --> all sub-shapes are added to occgeoComm
-    aMesher.PrepareOCCgeometry( occgeoComm, aShape, aMesh );//, meshedSM );
+    aMesher.PrepareOCCgeometry( *occgeoComm, aShape, aMesh );//, meshedSM );
 
     // local size set at MESHCONST_ANALYSE step depends on
     // minh, face_maxh, grading and curvaturesafety; find minh if not set by the user
     if ( !_hypParameters || netgen::mparam.minh < DBL_MIN )
     {
       if ( !_hypParameters )
-        netgen::mparam.maxh = occgeoComm.GetBoundingBox().Diam() / 3.;
+        netgen::mparam.maxh = occgeoComm->GetBoundingBox().Diam() / 3.;
       netgen::mparam.minh = aMesher.GetDefaultMinSize( aShape, netgen::mparam.maxh );
     }
     // set local size depending on curvature and NOT closeness of EDGEs
+#ifdef NETGEN_V6
+    const double factor = 2; //netgen::occparam.resthcloseedgefac;
+#else
+    const double factor = netgen::occparam.resthcloseedgefac;
     netgen::occparam.resthcloseedgeenable = false;
-    //netgen::occparam.resthcloseedgefac = 1.0 + netgen::mparam.grading;
-    occgeoComm.face_maxh = netgen::mparam.maxh;
-    netgen::OCCSetLocalMeshSize( occgeoComm, *ngMeshes[0] );
-    occgeoComm.emap.Clear();
-    occgeoComm.vmap.Clear();
+    netgen::occparam.resthcloseedgefac = 1.0 + netgen::mparam.grading;
+#endif
+    occgeoComm->face_maxh = netgen::mparam.maxh;
+#ifdef NETGEN_V6
+    netgen::OCCParameters occparam;
+    netgen::OCCSetLocalMeshSize( *occgeoComm, *ngMeshes[0], netgen::mparam, occparam );
+#else
+    netgen::OCCSetLocalMeshSize( *occgeoComm, *ngMeshes[0] );
+#endif
+    occgeoComm->emap.Clear();
+    occgeoComm->vmap.Clear();
 
     // set local size according to size of existing segments
-    const double factor = netgen::occparam.resthcloseedgefac;
     TopTools_IndexedMapOfShape edgeMap;
     TopExp::MapShapes( aMesh.GetShapeToMesh(), TopAbs_EDGE, edgeMap );
     for ( int iE = 1; iE <= edgeMap.Extent(); ++iE )
@@ -318,7 +329,8 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
     }
 
     // set local size defined on shapes
-    aMesher.SetLocalSize( occgeoComm, *ngMeshes[0] );
+    aMesher.SetLocalSize( *occgeoComm, *ngMeshes[0] );
+    aMesher.SetLocalSizeForChordalError( *occgeoComm, *ngMeshes[0] );
     try {
       ngMeshes[0]->LoadLocalMeshSize( mparam.meshsizefilename );
     } catch (NgException & ex) {
@@ -326,7 +338,6 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
     }
   }
   netgen::mparam.uselocalh = toOptimize; // restore as it is used at surface optimization
-
   // ==================
   // Loop on all FACEs
   // ==================
@@ -355,10 +366,10 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
     // get all EDGEs of a FACE
     // ------------------------
     TSideVector wires =
-      StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, faceErr, proxyMesh );
+      StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, faceErr, &helper, proxyMesh );
     if ( faceErr && !faceErr->IsOK() )
       continue;
-    int nbWires = wires.size();
+    size_t nbWires = wires.size();
     if ( nbWires == 0 )
     {
       faceErr.reset
@@ -384,21 +395,21 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
       if (_hypLengthFromEdges )
       {
         // compute edgeLength as an average segment length
-        int nbSegments = 0;
-        for ( int iW = 0; iW < nbWires; ++iW )
+        smIdType nbSegments = 0;
+        for ( size_t iW = 0; iW < nbWires; ++iW )
         {
           edgeLength += wires[ iW ]->Length();
           nbSegments += wires[ iW ]->NbSegments();
         }
         if ( nbSegments )
-          edgeLength /= nbSegments;
+          edgeLength /= double( nbSegments );
         netgen::mparam.maxh = edgeLength;
       }
       else if ( isDefaultHyp )
       {
         // set edgeLength by a longest segment
         double maxSeg2 = 0;
-        for ( int iW = 0; iW < nbWires; ++iW )
+        for ( size_t iW = 0; iW < nbWires; ++iW )
         {
           const UVPtStructVec& points = wires[ iW ]->GetUVPtStruct();
           if ( points.empty() )
@@ -415,7 +426,7 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
         netgen::mparam.maxh = edgeLength;
       }
       if ( netgen::mparam.maxh < DBL_MIN )
-        netgen::mparam.maxh = occgeoComm.GetBoundingBox().Diam();
+        netgen::mparam.maxh = occgeoComm->GetBoundingBox().Diam();
 
       if ( !isCommonLocalSize )
       {
@@ -424,16 +435,17 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
     }
 
     // prepare occgeom
-    netgen::OCCGeometry occgeom;
-    occgeom.shape = F;
-    occgeom.fmap.Add( F );
-    occgeom.CalcBoundingBox();
-    occgeom.facemeshstatus.SetSize(1);
-    occgeom.facemeshstatus = 0;
-    occgeom.face_maxh_modified.SetSize(1);
-    occgeom.face_maxh_modified = 0;
-    occgeom.face_maxh.SetSize(1);
-    occgeom.face_maxh = netgen::mparam.maxh;
+    netgen::OCCGeometry *occgeom;
+    int id_occgeom = occgeom_provider.take(&occgeom);
+    occgeom->shape = F;
+    occgeom->fmap.Add( F );
+    occgeom->CalcBoundingBox();
+    occgeom->facemeshstatus.SetSize(1);
+    occgeom->facemeshstatus = 0;
+    occgeom->face_maxh_modified.SetSize(1);
+    occgeom->face_maxh_modified = 0;
+    occgeom->face_maxh.SetSize(1);
+    occgeom->face_maxh = netgen::mparam.maxh;
 
     // -------------------------
     // Fill netgen mesh
@@ -456,10 +468,11 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
       {
         ngMesh->SetGlobalH ( mparam.maxh );
         ngMesh->SetMinimalH( mparam.minh );
-        Box<3> bb = occgeom.GetBoundingBox();
+        Box<3> bb = occgeom->GetBoundingBox();
         bb.Increase (bb.Diam()/10);
         ngMesh->SetLocalH (bb.PMin(), bb.PMax(), mparam.grading);
-        aMesher.SetLocalSize( occgeom, *ngMesh );
+        aMesher.SetLocalSize( *occgeom, *ngMesh );
+        aMesher.SetLocalSizeForChordalError( *occgeoComm, *ngMesh );
         try {
           ngMesh->LoadLocalMeshSize( mparam.meshsizefilename );
         } catch (NgException & ex) {
@@ -468,7 +481,7 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
       }
 
       nodeVec.clear();
-      faceErr = aMesher.AddSegmentsToMesh( *ngMesh, occgeom, wires, helper, nodeVec,
+      faceErr = aMesher.AddSegmentsToMesh( *ngMesh, *occgeom, wires, helper, nodeVec,
                                            /*overrideMinH=*/!_hypParameters);
       if ( faceErr && !faceErr->IsOK() )
         break;
@@ -486,13 +499,7 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
       SMESH_Comment str;
       try {
         OCC_CATCH_SIGNALS;
-
-#ifdef NETGEN_V5
-        err = netgen::OCCGenerateMesh(occgeom, ngMesh, netgen::mparam, startWith, endWith);
-#else
-        char *optstr = 0;
-        err = netgen::OCCGenerateMesh(occgeom, ngMesh, startWith, endWith, optstr);
-#endif
+        err = ngLib->GenerateMesh(*occgeom, startWith, endWith, ngMesh);
         if ( netgen::multithread.terminate )
           return false;
         if ( err )
@@ -514,7 +521,7 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
       }
       if ( err )
       {
-        if ( aMesher.FixFaceMesh( occgeom, *ngMesh, 1 ))
+        if ( aMesher.FixFaceMesh( *occgeom, *ngMesh, 1 ))
           break;
         if ( iLoop == LOC_SIZE )
         {
@@ -545,7 +552,9 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
         }
       }
 
-
+      occgeom_provider.release(id_occgeoComm, true);
+      occgeom_provider.release(id_occgeom, true);
+      aMesh.Lock();
       // ----------------------------------------------------
       // Fill the SMESHDS with the generated nodes and faces
       // ----------------------------------------------------
@@ -553,7 +562,7 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
       int nbNodes = ngMesh->GetNP();
       int nbFaces = ngMesh->GetNSE();
 
-      int nbInputNodes = nodeVec.size()-1;
+      int nbInputNodes = (int) nodeVec.size()-1;
       nodeVec.resize( nbNodes+1, 0 );
 
       // add nodes
@@ -595,6 +604,9 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
       break;
     } // two attempts
   } // loop on FACEs
+  aMesh.Unlock();
+  nglib_provider.release(id_ngLib, true);
+
 
   return true;
 }
@@ -651,7 +663,7 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Evaluate(SMESH_Mesh& aMesh,
     return false;
 
   // collect info from edges
-  int nb0d = 0, nb1d = 0;
+  smIdType nb0d = 0, nb1d = 0;
   bool IsQuadratic = false;
   bool IsFirst = true;
   double fullLen = 0.0;
@@ -669,9 +681,9 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Evaluate(SMESH_Mesh& aMesh,
       smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
       return false;
     }
-    std::vector<int> aVec = (*anIt).second;
+    std::vector<smIdType> aVec = (*anIt).second;
     nb0d += aVec[SMDSEntity_Node];
-    nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
+    nb1d += std::max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
     double aLen = SMESH_Algo::EdgeLength(E);
     fullLen += aLen;
     if(IsFirst) {
@@ -685,7 +697,7 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Evaluate(SMESH_Mesh& aMesh,
   double ELen = 0;
   if (( _hypLengthFromEdges ) || ( !_hypLengthFromEdges && !_hypMaxElementArea )) {
     if ( nb1d > 0 )
-      ELen = fullLen / nb1d;
+      ELen = fullLen / double( nb1d );
   }
   if ( _hypMaxElementArea ) {
     double maxArea = _hypMaxElementArea->GetMaxArea();
@@ -703,10 +715,10 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Evaluate(SMESH_Mesh& aMesh,
     smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated.\nToo small element length",this));
     return false;
   }
-  int nbFaces = (int) ( anArea / ( ELen*ELen*sqrt(3.) / 4 ) );
-  int nbNodes = (int) ( ( nbFaces*3 - (nb1d-1)*2 ) / 6 + 1 );
-  std::vector<int> aVec(SMDSEntity_Last);
-  for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i]=0;
+  smIdType nbFaces = (smIdType) ( anArea / ( ELen*ELen*sqrt(3.) / 4 ) );
+  smIdType nbNodes = (smIdType) ( ( nbFaces*3 - (nb1d-1)*2 ) / 6 + 1 );
+  std::vector<smIdType> aVec(SMDSEntity_Last);
+  for(smIdType i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i]=0;
   if( IsQuadratic ) {
     aVec[SMDSEntity_Node] = nbNodes;
     aVec[SMDSEntity_Quad_Triangle] = nbFaces;