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