Salome HOME
Regression of SALOME_TESTS/Grids/smesh/viscous_layers_01/B2
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_Mesher.hxx
1 // Copyright (C) 2007-2016  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 namespace nglib {
40 #include <nglib.h>
41 }
42
43 #include <map>
44 #include <vector>
45 #include <set>
46
47 class SMESHDS_Mesh;
48 class SMESH_Comment;
49 class SMESH_Mesh;
50 class SMESH_MesherHelper;
51 class TopoDS_Shape;
52 class TopTools_IndexedMapOfShape;
53 class NETGENPlugin_Hypothesis;
54 class NETGENPlugin_SimpleHypothesis_2D;
55 class NETGENPlugin_Internals;
56 namespace netgen {
57   class OCCGeometry;
58   class Mesh;
59 }
60 //=============================================================================
61 /*!
62  * \brief Struct storing nb of entities in netgen mesh
63  */
64 //=============================================================================
65
66 struct NETGENPlugin_ngMeshInfo
67 {
68   int _nbNodes, _nbSegments, _nbFaces, _nbVolumes;
69   char* _copyOfLocalH;
70   NETGENPlugin_ngMeshInfo( netgen::Mesh* ngMesh=0);
71   void transferLocalH( netgen::Mesh* fromMesh, netgen::Mesh* toMesh );
72   void restoreLocalH ( netgen::Mesh* ngMesh);
73 };
74
75 //================================================================================
76 /*!
77  * \brief It correctly initializes netgen library at constructor and
78  *        correctly finishes using netgen library at destructor
79  */
80 //================================================================================
81
82 struct NETGENPLUGIN_EXPORT NETGENPlugin_NetgenLibWrapper
83 {
84   bool             _isComputeOk;
85   nglib::Ng_Mesh * _ngMesh;
86
87   NETGENPlugin_NetgenLibWrapper();
88   ~NETGENPlugin_NetgenLibWrapper();
89   void setMesh( nglib::Ng_Mesh* mesh );
90
91   static void RemoveTmpFiles();
92   static int& instanceCounter();
93
94  private:
95   std::string getOutputFileName();
96   void        removeOutputFile();
97   std::string _outputFileName;
98
99   ostream *       _ngcout;
100   ostream *       _ngcerr;
101   std::streambuf* _coutBuffer;   // to re-/store cout.rdbuf()
102 };
103
104 //=============================================================================
105 /*!
106  * \brief This class calls the NETGEN mesher of OCC geometry
107  */
108 //=============================================================================
109
110 class NETGENPLUGIN_EXPORT NETGENPlugin_Mesher 
111 {
112  public:
113   // ---------- PUBLIC METHODS ----------
114
115   NETGENPlugin_Mesher (SMESH_Mesh* mesh, const TopoDS_Shape& aShape,
116                        const bool isVolume);
117   ~NETGENPlugin_Mesher();
118   void SetSelfPointer( NETGENPlugin_Mesher ** ptr );
119
120   void SetParameters(const NETGENPlugin_Hypothesis*          hyp);
121   void SetParameters(const NETGENPlugin_SimpleHypothesis_2D* hyp);
122   void SetViscousLayers2DAssigned(bool isAssigned) { _isViscousLayers2D = isAssigned; }
123
124   bool Compute();
125
126   bool Evaluate(MapShapeNbElems& aResMap);
127
128   double GetProgress(const SMESH_Algo* holder,
129                      const int *       algoProgressTic,
130                      const double *    algoProgress) const;
131
132   static void PrepareOCCgeometry(netgen::OCCGeometry&          occgeom,
133                                  const TopoDS_Shape&           shape,
134                                  SMESH_Mesh&                   mesh,
135                                  std::list< SMESH_subMesh* > * meshedSM=0,
136                                  NETGENPlugin_Internals*       internalShapes=0);
137
138   static double GetDefaultMinSize(const TopoDS_Shape& shape,
139                                   const double        maxSize);
140
141   static void RestrictLocalSize(netgen::Mesh& ngMesh,
142                                 const gp_XYZ& p,
143                                 double        size,
144                                 const bool    overrideMinH=true);
145
146   static int FillSMesh(const netgen::OCCGeometry&          occgeom,
147                        netgen::Mesh&                       ngMesh,
148                        const NETGENPlugin_ngMeshInfo&      initState,
149                        SMESH_Mesh&                         sMesh,
150                        std::vector<const SMDS_MeshNode*>&  nodeVec,
151                        SMESH_Comment&                      comment,
152                        SMESH_MesherHelper*                 quadHelper=0);
153
154   bool FillNgMesh(netgen::OCCGeometry&                occgeom,
155                   netgen::Mesh&                       ngMesh,
156                   std::vector<const SMDS_MeshNode*>&  nodeVec,
157                   const std::list< SMESH_subMesh* > & meshedSM,
158                   SMESH_MesherHelper*                 quadHelper=0,
159                   SMESH_ProxyMesh::Ptr                proxyMesh=SMESH_ProxyMesh::Ptr());
160
161   static void FixIntFaces(const netgen::OCCGeometry& occgeom,
162                           netgen::Mesh&              ngMesh,
163                           NETGENPlugin_Internals&    internalShapes);
164
165   static bool FixFaceMesh(const netgen::OCCGeometry& occgeom,
166                           netgen::Mesh&              ngMesh,
167                           const int                  faceID);
168
169   static void AddIntVerticesInFaces(const netgen::OCCGeometry&          occgeom,
170                                     netgen::Mesh&                       ngMesh,
171                                     std::vector<const SMDS_MeshNode*>&  nodeVec,
172                                     NETGENPlugin_Internals&             internalShapes);
173
174   static void AddIntVerticesInSolids(const netgen::OCCGeometry&         occgeom,
175                                     netgen::Mesh&                       ngMesh,
176                                     std::vector<const SMDS_MeshNode*>&  nodeVec,
177                                     NETGENPlugin_Internals&             internalShapes);
178
179   static SMESH_ComputeErrorPtr
180     AddSegmentsToMesh(netgen::Mesh&                         ngMesh,
181                       netgen::OCCGeometry&                  geom,
182                       const TSideVector&                    wires,
183                       SMESH_MesherHelper&                   helper,
184                       std::vector< const SMDS_MeshNode* > & nodeVec,
185                       const bool                            overrideMinH=true);
186
187   void SetDefaultParameters();
188
189   static SMESH_ComputeErrorPtr ReadErrors(const std::vector< const SMDS_MeshNode* >& nodeVec);
190
191
192   static void toPython( const netgen::Mesh* ngMesh,
193                         const std::string&  pyFile); // debug
194
195  private:
196
197   SMESH_Mesh*          _mesh;
198   const TopoDS_Shape&  _shape;
199   bool                 _isVolume;
200   bool                 _optimize;
201   int                  _fineness;
202   bool                 _isViscousLayers2D;
203   netgen::Mesh*        _ngMesh;
204   netgen::OCCGeometry* _occgeom;
205
206   int                  _curShapeIndex;
207   volatile int         _progressTic;
208   volatile double      _ticTime; // normalized [0,1] compute time per a SMESH_Algo::_progressTic
209   volatile double      _totalTime;
210
211   const NETGENPlugin_SimpleHypothesis_2D * _simpleHyp;
212
213   // a pointer to NETGENPlugin_Mesher* field of the holder, that will be
214   // nullified at destruction of this
215   NETGENPlugin_Mesher ** _ptrToMe; 
216 };
217
218 //=============================================================================
219 /*!
220  * \brief Container of info needed to solve problems with internal shapes.
221  *
222  * Issue 0020676. It is made up as a class to be ready to extract from NETGEN
223  * and put in SMESH as soon as the same solution is needed somewhere else.
224  * The approach is to precompute internal edges in 2D and internal faces in 3D
225  * and put their mesh correctly (twice) into netgen mesh.
226  * In 2D, this class finds internal edges in faces and their vertices.
227  * In 3D, it additionally finds internal faces, their edges shared with other faces,
228  * and their vertices shared by several internal edges. Nodes built on the found
229  * shapes and mesh faces built on the found internal faces are to be doubled in
230  * netgen mesh to emulate a "crack"
231  *
232  * For internal faces a more simple solution is found, which is just to duplicate
233  * mesh faces on internal geom faces without modeling a "real crack". For this
234  * reason findBorderElements() is no more used anywhere.
235  */
236 //=============================================================================
237
238 class NETGENPLUGIN_EXPORT NETGENPlugin_Internals
239 {
240   SMESH_Mesh&       _mesh;
241   bool              _is3D;
242   //2D
243   std::map<int,int> _e2face;//!<edges and their vertices in faces where they are TopAbs_INTERNAL
244   std::map<int,std::list<int> > _f2v;//!<faces with internal vertices
245   // 3D
246   std::set<int>     _intShapes;
247   std::set<int>     _borderFaces; //!< non-internal faces sharing the internal edge
248   std::map<int,std::list<int> > _s2v;//!<solids with internal vertices
249
250 public:
251   NETGENPlugin_Internals( SMESH_Mesh& mesh, const TopoDS_Shape& shape, bool is3D );
252
253   SMESH_Mesh& getMesh() const;
254
255   bool isShapeToPrecompute(const TopoDS_Shape& s);
256
257   // 2D meshing
258   // edges 
259   bool hasInternalEdges() const { return !_e2face.empty(); }
260   bool isInternalEdge( int id ) const { return _e2face.count( id ); }
261   const std::map<int,int>& getEdgesAndVerticesWithFaces() const { return _e2face; }
262   void getInternalEdges( TopTools_IndexedMapOfShape&  fmap,
263                          TopTools_IndexedMapOfShape&  emap,
264                          TopTools_IndexedMapOfShape&  vmap,
265                          std::list< SMESH_subMesh* > smToPrecompute[]);
266   // vertices
267   bool hasInternalVertexInFace() const { return !_f2v.empty(); }
268   const std::map<int,std::list<int> >& getFacesWithVertices() const { return _f2v; }
269
270   // 3D meshing
271   // faces
272   bool hasInternalFaces() const { return !_intShapes.empty(); }
273   bool isInternalShape( int id ) const { return _intShapes.count( id ); }
274   void findBorderElements( std::set< const SMDS_MeshElement*, TIDCompare > & borderElems );
275   bool isBorderFace( int faceID ) const { return _borderFaces.count( faceID ); }
276   void getInternalFaces( TopTools_IndexedMapOfShape&  fmap,
277                          TopTools_IndexedMapOfShape&  emap,
278                          std::list< SMESH_subMesh* >& facesSM,
279                          std::list< SMESH_subMesh* >& boundarySM);
280   // vertices
281   bool hasInternalVertexInSolid() const { return !_s2v.empty(); }
282   bool hasInternalVertexInSolid(int soID ) const { return _s2v.count(soID); }
283   const std::map<int,std::list<int> >& getSolidsWithVertices() const { return _s2v; }
284
285
286 };
287
288 #endif