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