Salome HOME
Working version for run_mesher for netgen 3d
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_Mesher.hxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  NETGENPlugin : C++ implementation
24 // File      : NETGENPlugin_Mesher.hxx
25 // Author    : Michael Sazonov (OCN)
26 // Date      : 31/03/2006
27 // Project   : SALOME
28 //
29 #ifndef _NETGENPlugin_Mesher_HXX_
30 #define _NETGENPlugin_Mesher_HXX_
31
32 #include "NETGENPlugin_Defs.hxx"
33
34 #include <StdMeshers_FaceSide.hxx>
35 #include <SMDS_MeshElement.hxx>
36 #include <SMESH_Algo.hxx>
37 #include <SMESH_ProxyMesh.hxx>
38
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41 namespace nglib {
42 #include <nglib.h>
43 }
44
45 #include <map>
46 #include <vector>
47 #include <set>
48
49 class NETGENPlugin_Hypothesis;
50 class NETGENPlugin_Internals;
51 class NETGENPlugin_SimpleHypothesis_2D;
52 class SMESHDS_Mesh;
53 class SMESH_Comment;
54 class SMESH_Mesh;
55 class SMESH_MesherHelper;
56 class StdMeshers_ViscousLayers;
57 class TopoDS_Shape;
58 namespace netgen {
59   class OCCGeometry;
60   class Mesh;
61   class MeshingParameters;
62   extern MeshingParameters mparam;
63 }
64 //=============================================================================
65 /*!
66  * \brief Struct storing nb of entities in netgen mesh
67  */
68 //=============================================================================
69
70 struct NETGENPlugin_ngMeshInfo
71 {
72   int   _nbNodes, _nbSegments, _nbFaces, _nbVolumes;
73   bool  _elementsRemoved; // case where netgen can remove free nodes
74   char* _copyOfLocalH;
75   NETGENPlugin_ngMeshInfo( netgen::Mesh* ngMesh=0, bool checkRemovedElems=false );
76   void transferLocalH( netgen::Mesh* fromMesh, netgen::Mesh* toMesh );
77   void restoreLocalH ( netgen::Mesh* ngMesh);
78 };
79
80 //================================================================================
81 /*!
82  * \brief It correctly initializes netgen library at constructor and
83  *        correctly finishes using netgen library at destructor
84  */
85 //================================================================================
86
87 struct NETGENPLUGIN_EXPORT NETGENPlugin_NetgenLibWrapper
88 {
89   bool           _isComputeOk;
90   netgen::Mesh * _ngMesh;
91
92   NETGENPlugin_NetgenLibWrapper();
93   ~NETGENPlugin_NetgenLibWrapper();
94   void setMesh( nglib::Ng_Mesh* mesh );
95   nglib::Ng_Mesh* ngMesh() { return (nglib::Ng_Mesh*)(void*)_ngMesh; }
96
97
98
99   static int GenerateMesh(netgen::OCCGeometry& occgeo, int startWith, int endWith,
100                           netgen::Mesh* & ngMesh, netgen::MeshingParameters & mparam);
101   int GenerateMesh(netgen::OCCGeometry& occgeo, int startWith, int endWith )
102   {
103
104     return GenerateMesh( occgeo, startWith, endWith, _ngMesh, netgen::mparam );
105   }
106   static int GenerateMesh(netgen::OCCGeometry& occgeo, int startWith, int endWith,
107                           netgen::Mesh* & ngMesh){
108                             return GenerateMesh(occgeo, startWith, endWith, ngMesh, netgen::mparam);
109                           };
110   static void CalcLocalH( netgen::Mesh * ngMesh );
111
112   static void RemoveTmpFiles();
113   static int& instanceCounter();
114
115  private:
116   std::string getOutputFileName();
117   void        removeOutputFile();
118   std::string _outputFileName;
119
120   ostream *       _ngcout;
121   ostream *       _ngcerr;
122   std::streambuf* _coutBuffer;   // to re-/store cout.rdbuf()
123 };
124
125 //=============================================================================
126 /*!
127  * \brief This class calls the NETGEN mesher of OCC geometry
128  */
129 //=============================================================================
130
131 class NETGENPLUGIN_EXPORT NETGENPlugin_Mesher
132 {
133  public:
134   // ---------- PUBLIC METHODS ----------
135
136   NETGENPlugin_Mesher (SMESH_Mesh* mesh, const TopoDS_Shape& aShape,
137                        const bool isVolume);
138   ~NETGENPlugin_Mesher();
139   void SetSelfPointer( NETGENPlugin_Mesher ** ptr );
140
141   void SetParameters(const NETGENPlugin_Hypothesis*          hyp);
142   void SetParameters(const NETGENPlugin_Hypothesis*          hyp, netgen::MeshingParameters &mparams);
143   void SetParameters(const NETGENPlugin_SimpleHypothesis_2D* hyp);
144   void SetParameters(const StdMeshers_ViscousLayers*         hyp );
145   void SetViscousLayers2DAssigned(bool isAssigned) { _isViscousLayers2D = isAssigned; }
146
147   void SetLocalSizeForChordalError( netgen::OCCGeometry& occgeo, netgen::Mesh& ngMesh );
148   static void SetLocalSize( netgen::OCCGeometry& occgeo, netgen::Mesh& ngMesh );
149
150   bool Compute();
151
152   bool Evaluate(MapShapeNbElems& aResMap);
153
154   double GetProgress(const SMESH_Algo* holder,
155                      const int *       algoProgressTic,
156                      const double *    algoProgress) const;
157
158   static void PrepareOCCgeometry(netgen::OCCGeometry&          occgeom,
159                                  const TopoDS_Shape&           shape,
160                                  SMESH_Mesh&                   mesh,
161                                  std::list< SMESH_subMesh* > * meshedSM=0,
162                                  NETGENPlugin_Internals*       internalShapes=0);
163
164   static double GetDefaultMinSize(const TopoDS_Shape& shape,
165                                   const double        maxSize);
166
167   static void RestrictLocalSize(netgen::Mesh& ngMesh,
168                                 const gp_XYZ& p,
169                                 double        size,
170                                 const bool    overrideMinH=true);
171
172   static int FillSMesh(const netgen::OCCGeometry&          occgeom,
173                        netgen::Mesh&                       ngMesh,
174                        const NETGENPlugin_ngMeshInfo&      initState,
175                        SMESH_Mesh&                         sMesh,
176                        std::vector<const SMDS_MeshNode*>&  nodeVec,
177                        SMESH_Comment&                      comment,
178                        SMESH_MesherHelper*                 quadHelper=0);
179
180   bool FillNgMesh(netgen::OCCGeometry&                occgeom,
181                   netgen::Mesh&                       ngMesh,
182                   std::vector<const SMDS_MeshNode*>&  nodeVec,
183                   const std::list< SMESH_subMesh* > & meshedSM,
184                   SMESH_MesherHelper*                 quadHelper=0,
185                   SMESH_ProxyMesh::Ptr                proxyMesh=SMESH_ProxyMesh::Ptr());
186
187   static void FixIntFaces(const netgen::OCCGeometry& occgeom,
188                           netgen::Mesh&              ngMesh,
189                           NETGENPlugin_Internals&    internalShapes);
190
191   static bool FixFaceMesh(const netgen::OCCGeometry& occgeom,
192                           netgen::Mesh&              ngMesh,
193                           const int                  faceID);
194
195   static void AddIntVerticesInFaces(const netgen::OCCGeometry&          occgeom,
196                                     netgen::Mesh&                       ngMesh,
197                                     std::vector<const SMDS_MeshNode*>&  nodeVec,
198                                     NETGENPlugin_Internals&             internalShapes);
199
200   static void AddIntVerticesInSolids(const netgen::OCCGeometry&         occgeom,
201                                     netgen::Mesh&                       ngMesh,
202                                     std::vector<const SMDS_MeshNode*>&  nodeVec,
203                                     NETGENPlugin_Internals&             internalShapes);
204
205   static SMESH_ComputeErrorPtr
206     AddSegmentsToMesh(netgen::Mesh&                         ngMesh,
207                       netgen::OCCGeometry&                  geom,
208                       const TSideVector&                    wires,
209                       SMESH_MesherHelper&                   helper,
210                       std::vector< const SMDS_MeshNode* > & nodeVec,
211                       const bool                            overrideMinH=true);
212
213   void SetDefaultParameters();
214   void SetDefaultParameters(netgen::MeshingParameters &mparams);
215
216   static SMESH_ComputeErrorPtr ReadErrors(const std::vector< const SMDS_MeshNode* >& nodeVec);
217
218
219   static void toPython( const netgen::Mesh* ngMesh ); // debug
220
221  private:
222
223   SMESH_Mesh*          _mesh;
224   const TopoDS_Shape&  _shape;
225   bool                 _isVolume;
226   bool                 _optimize;
227   int                  _fineness;
228   bool                 _isViscousLayers2D;
229   double               _chordalError;
230   netgen::Mesh*        _ngMesh;
231   netgen::OCCGeometry* _occgeom;
232
233   int                  _curShapeIndex;
234   volatile int         _progressTic;
235   volatile double      _ticTime; // normalized [0,1] compute time per a SMESH_Algo::_progressTic
236   volatile double      _totalTime;
237
238   const NETGENPlugin_SimpleHypothesis_2D * _simpleHyp;
239   const StdMeshers_ViscousLayers*   _viscousLayersHyp;
240
241   // a pointer to NETGENPlugin_Mesher* field of the holder, that will be
242   // nullified at destruction of this
243   NETGENPlugin_Mesher ** _ptrToMe;
244 };
245
246 //=============================================================================
247 /*!
248  * \brief Container of info needed to solve problems with internal shapes.
249  *
250  * Issue 0020676. It is made up as a class to be ready to extract from NETGEN
251  * and put in SMESH as soon as the same solution is needed somewhere else.
252  * The approach is to precompute internal edges in 2D and internal faces in 3D
253  * and put their mesh correctly (twice) into netgen mesh.
254  * In 2D, this class finds internal edges in faces and their vertices.
255  * In 3D, it additionally finds internal faces, their edges shared with other faces,
256  * and their vertices shared by several internal edges. Nodes built on the found
257  * shapes and mesh faces built on the found internal faces are to be doubled in
258  * netgen mesh to emulate a "crack"
259  *
260  * For internal faces a more simple solution is found, which is just to duplicate
261  * mesh faces on internal geom faces without modeling a "real crack". For this
262  * reason findBorderElements() is no more used anywhere.
263  */
264 //=============================================================================
265
266 class NETGENPLUGIN_EXPORT NETGENPlugin_Internals
267 {
268   SMESH_Mesh&       _mesh;
269   bool              _is3D;
270   //2D
271   std::map<int,int> _e2face;//!<edges and their vertices in faces where they are TopAbs_INTERNAL
272   std::map<int,std::list<int> > _f2v;//!<faces with internal vertices
273   // 3D
274   std::set<int>     _intShapes;
275   std::set<int>     _borderFaces; //!< non-internal faces sharing the internal edge
276   std::map<int,std::list<int> > _s2v;//!<solids with internal vertices
277
278 public:
279   NETGENPlugin_Internals( SMESH_Mesh& mesh, const TopoDS_Shape& shape, bool is3D );
280
281   SMESH_Mesh& getMesh() const;
282
283   bool isShapeToPrecompute(const TopoDS_Shape& s);
284
285   // 2D meshing
286   // edges
287   bool hasInternalEdges() const { return !_e2face.empty(); }
288   bool isInternalEdge( int id ) const { return _e2face.count( id ); }
289   const std::map<int,int>& getEdgesAndVerticesWithFaces() const { return _e2face; }
290   void getInternalEdges( TopTools_IndexedMapOfShape&  fmap,
291                          TopTools_IndexedMapOfShape&  emap,
292                          TopTools_IndexedMapOfShape&  vmap,
293                          std::list< SMESH_subMesh* > smToPrecompute[]);
294   // vertices
295   bool hasInternalVertexInFace() const { return !_f2v.empty(); }
296   const std::map<int,std::list<int> >& getFacesWithVertices() const { return _f2v; }
297
298   // 3D meshing
299   // faces
300   bool hasInternalFaces() const { return !_intShapes.empty(); }
301   bool isInternalShape( int id ) const { return _intShapes.count( id ); }
302   void findBorderElements( std::set< const SMDS_MeshElement*, TIDCompare > & borderElems );
303   bool isBorderFace( int faceID ) const { return _borderFaces.count( faceID ); }
304   void getInternalFaces( TopTools_IndexedMapOfShape&  fmap,
305                          TopTools_IndexedMapOfShape&  emap,
306                          std::list< SMESH_subMesh* >& facesSM,
307                          std::list< SMESH_subMesh* >& boundarySM);
308   // vertices
309   bool hasInternalVertexInSolid() const { return !_s2v.empty(); }
310   bool hasInternalVertexInSolid(int soID ) const { return _s2v.count(soID); }
311   const std::map<int,std::list<int> >& getSolidsWithVertices() const { return _s2v; }
312
313
314 };
315
316 #endif