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