From 3b5a94a165051ebb44e3cf9d099eb9f70a0355cc Mon Sep 17 00:00:00 2001
From: oan <oan@opencascade.com>
Date: Thu, 20 Oct 2022 14:25:26 +0300
Subject: [PATCH] 0033050: [Regression to OCCT 7.3.0] Mesh - meshing deflection
 is not reached

Add parameter EnableControlSurfaceDeflectionAllSurfaces to IMeshTools_Parameters enabling possibility to optimize mesh even on analytical surfaces;
Add corresponding parameter -surf_def_all to incmesh Draw command.
---
 src/BRepMesh/BRepMesh_MeshAlgoFactory.cxx | 28 +++++++++++-----
 src/IMeshTools/IMeshTools_Parameters.hxx  |  5 +++
 src/MeshTest/MeshTest.cxx                 |  6 ++++
 tests/bugs/mesh/bug33050                  | 41 +++++++++++++++++++++++
 4 files changed, 71 insertions(+), 9 deletions(-)
 create mode 100644 tests/bugs/mesh/bug33050

diff --git a/src/BRepMesh/BRepMesh_MeshAlgoFactory.cxx b/src/BRepMesh/BRepMesh_MeshAlgoFactory.cxx
index ce7cb6fba9..917645f823 100644
--- a/src/BRepMesh/BRepMesh_MeshAlgoFactory.cxx
+++ b/src/BRepMesh/BRepMesh_MeshAlgoFactory.cxx
@@ -73,27 +73,37 @@ Handle(IMeshTools_MeshAlgo) BRepMesh_MeshAlgoFactory::GetAlgo(
   switch (theSurfaceType)
   {
   case GeomAbs_Plane:
-    return theParameters.InternalVerticesMode ?
-      new NodeInsertionMeshAlgo<BRepMesh_DefaultRangeSplitter>::Type :
-      new BaseMeshAlgo::Type;
+    return theParameters.EnableControlSurfaceDeflectionAllSurfaces ?
+      new DeflectionControlMeshAlgo<BRepMesh_DefaultRangeSplitter>::Type :
+        (theParameters.InternalVerticesMode ?
+         new NodeInsertionMeshAlgo<BRepMesh_DefaultRangeSplitter>::Type :
+         new BaseMeshAlgo::Type);
     break;
 
   case GeomAbs_Sphere:
-    return new NodeInsertionMeshAlgo<BRepMesh_SphereRangeSplitter>::Type;
+    return theParameters.EnableControlSurfaceDeflectionAllSurfaces ?
+      new DeflectionControlMeshAlgo<BRepMesh_SphereRangeSplitter>::Type :
+      new NodeInsertionMeshAlgo<BRepMesh_SphereRangeSplitter>::Type;
     break;
 
   case GeomAbs_Cylinder:
-    return theParameters.InternalVerticesMode ?
-      new NodeInsertionMeshAlgo<BRepMesh_CylinderRangeSplitter>::Type :
-      new BaseMeshAlgo::Type;
+    return theParameters.EnableControlSurfaceDeflectionAllSurfaces ?
+      new DeflectionControlMeshAlgo<BRepMesh_CylinderRangeSplitter>::Type :
+        (theParameters.InternalVerticesMode ?
+        new NodeInsertionMeshAlgo<BRepMesh_CylinderRangeSplitter>::Type :
+        new BaseMeshAlgo::Type);
     break;
 
   case GeomAbs_Cone:
-    return new NodeInsertionMeshAlgo<BRepMesh_ConeRangeSplitter>::Type;
+    return theParameters.EnableControlSurfaceDeflectionAllSurfaces ? 
+      new DeflectionControlMeshAlgo<BRepMesh_ConeRangeSplitter>::Type :
+      new NodeInsertionMeshAlgo<BRepMesh_ConeRangeSplitter>::Type;
     break;
 
   case GeomAbs_Torus:
-    return new NodeInsertionMeshAlgo<BRepMesh_TorusRangeSplitter>::Type;
+    return theParameters.EnableControlSurfaceDeflectionAllSurfaces ?
+      new DeflectionControlMeshAlgo<BRepMesh_TorusRangeSplitter>::Type :
+      new NodeInsertionMeshAlgo<BRepMesh_TorusRangeSplitter>::Type;
     break;
 
   case GeomAbs_SurfaceOfRevolution:
diff --git a/src/IMeshTools/IMeshTools_Parameters.hxx b/src/IMeshTools/IMeshTools_Parameters.hxx
index 4bbff6a902..be3ea1820b 100644
--- a/src/IMeshTools/IMeshTools_Parameters.hxx
+++ b/src/IMeshTools/IMeshTools_Parameters.hxx
@@ -35,6 +35,7 @@ struct IMeshTools_Parameters {
     Relative (Standard_False),
     InternalVerticesMode (Standard_True),
     ControlSurfaceDeflection (Standard_True),
+    EnableControlSurfaceDeflectionAllSurfaces(Standard_False),
     CleanModel (Standard_True),
     AdjustMinSize (Standard_False),
     ForceFaceDeflection (Standard_False),
@@ -85,6 +86,10 @@ struct IMeshTools_Parameters {
   //! the face
   Standard_Boolean                                 ControlSurfaceDeflection;
 
+  // Enables/disables check triggered by ControlSurfaceDeflection flag 
+  // for all types of surfaces including analytical.
+  Standard_Boolean                                 EnableControlSurfaceDeflectionAllSurfaces;
+
   //! Cleans temporary data model when algorithm is finished.
   Standard_Boolean                                 CleanModel;
 
diff --git a/src/MeshTest/MeshTest.cxx b/src/MeshTest/MeshTest.cxx
index 351b51379c..4c165a8822 100644
--- a/src/MeshTest/MeshTest.cxx
+++ b/src/MeshTest/MeshTest.cxx
@@ -116,6 +116,10 @@ static Standard_Integer incrementalmesh (Draw_Interpretor& theDI,
     {
       aMeshParams.ControlSurfaceDeflection = !Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter);
     }
+    else if (aNameCase == "-surf_def_all")
+    {
+      aMeshParams.EnableControlSurfaceDeflectionAllSurfaces = Draw::ParseOnOffIterator(theNbArgs, theArgVec, anArgIter);
+    }
     else if (aNameCase == "-adjust_min")
     {
       aMeshParams.AdjustMinSize = Draw::ParseOnOffNoIterator (theNbArgs, theArgVec, anArgIter);
@@ -1620,6 +1624,8 @@ void  MeshTest::Commands(Draw_Interpretor& theCommands)
     "\n\t\t:  -ai             angular deflection inside of faces in deg (~57.29 deg = 1 rad by default);"
     "\n\t\t:  -int_vert_off   disables insertion of internal vertices into mesh (enabled by default);"
     "\n\t\t:  -surf_def_off   disables control of deflection of mesh from real surface (enabled by default);"
+    "\n\t\t:  -surf_def_all   enables control of deflection of mesh from real surface for all types of surfaces"
+    "\n\t\t:                  (FALSE by default);"
     "\n\t\t:  -adjust_min     enables local adjustment of min size depending on edge size (FALSE by default);"
     "\n\t\t:  -force_face_def disables usage of shape tolerances for computing face deflection (FALSE by default);"
     "\n\t\t:  -decrease       enforces the meshing of the shape even if current mesh satisfies the new criteria"
diff --git a/tests/bugs/mesh/bug33050 b/tests/bugs/mesh/bug33050
new file mode 100644
index 0000000000..466cfe9a2f
--- /dev/null
+++ b/tests/bugs/mesh/bug33050
@@ -0,0 +1,41 @@
+puts "================"
+puts "0033050: \[Regression to OCCT 7.3.0\] Mesh - meshing deflection is not reached"
+puts "================"
+puts ""
+
+restore [locate_data_file bug33050.brep] result
+
+################## Deflection: 0.001 ##################
+tclean result
+incmesh result 0.001
+
+checktrinfo result -tri 27652 -nod 28016 -defl 0.0027132671526737435 -tol_abs_defl 1e-6
+
+tclean result
+incmesh result 0.001 -surf_def_all
+
+checktrinfo result -tri 30102 -nod 29241 -defl 0.0010301984083541042 -tol_abs_defl 1e-6
+
+################## Deflection: 0.0001 #################
+
+tclean result
+incmesh result 0.0001
+
+checktrinfo result -tri 88140 -nod 84304 -defl 0.00073349807253346574 -tol_abs_defl 1e-6
+
+tclean result
+incmesh result 0.0001 -surf_def_all
+
+checktrinfo result -tri 103376 -nod 91922 -defl 0.00012553867394267057 -tol_abs_defl 1e-6
+
+################## Deflection: 0.00005 ################
+
+tclean result
+incmesh result 0.00005
+
+checktrinfo result -tri 127538 -nod 119773 -defl 0.00043153032084638013 -tol_abs_defl 1e-6
+
+tclean result
+incmesh result 0.00005 -surf_def_all
+
+checktrinfo result -tri 154246 -nod 133127 -defl 5.1157129731636775e-05 -tol_abs_defl 1e-6
\ No newline at end of file