Salome HOME
Merge from V6_main 01/04/2013
[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_DataMapOfShapeShape;
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 This class calls the NETGEN mesher of OCC geometry
78  */
79 //=============================================================================
80
81 class NETGENPLUGIN_EXPORT NETGENPlugin_Mesher 
82 {
83  public:
84   // ---------- PUBLIC METHODS ----------
85
86   NETGENPlugin_Mesher (SMESH_Mesh* mesh, const TopoDS_Shape& aShape,
87                        const bool isVolume);
88
89   void SetParameters(const NETGENPlugin_Hypothesis*          hyp);
90   void SetParameters(const NETGENPlugin_SimpleHypothesis_2D* hyp);
91   void SetViscousLayers2DAssigned(bool isAssigned) { _isViscousLayers2D = isAssigned; }
92
93   bool Compute();
94
95   bool Evaluate(MapShapeNbElems& aResMap);
96
97   static void PrepareOCCgeometry(netgen::OCCGeometry&          occgeom,
98                                  const TopoDS_Shape&           shape,
99                                  SMESH_Mesh&                   mesh,
100                                  std::list< SMESH_subMesh* > * meshedSM=0,
101                                  NETGENPlugin_Internals*       internalShapes=0);
102
103   static double GetDefaultMinSize(const TopoDS_Shape& shape,
104                                   const double        maxSize);
105
106   static void RestrictLocalSize(netgen::Mesh& ngMesh, const gp_XYZ& p, const double  size);
107
108   static int FillSMesh(const netgen::OCCGeometry&          occgeom,
109                        netgen::Mesh&                       ngMesh,
110                        const NETGENPlugin_ngMeshInfo&      initState,
111                        SMESH_Mesh&                         sMesh,
112                        std::vector<const SMDS_MeshNode*>&  nodeVec,
113                        SMESH_Comment&                      comment);
114
115   bool FillNgMesh(netgen::OCCGeometry&                occgeom,
116                   netgen::Mesh&                       ngMesh,
117                   std::vector<const SMDS_MeshNode*>&  nodeVec,
118                   const std::list< SMESH_subMesh* > & meshedSM,
119                   SMESH_ProxyMesh::Ptr                proxyMesh=SMESH_ProxyMesh::Ptr());
120
121   static void FixIntFaces(const netgen::OCCGeometry& occgeom,
122                           netgen::Mesh&              ngMesh,
123                           NETGENPlugin_Internals&    internalShapes);
124
125   static void AddIntVerticesInFaces(const netgen::OCCGeometry&          occgeom,
126                                     netgen::Mesh&                       ngMesh,
127                                     std::vector<const SMDS_MeshNode*>&  nodeVec,
128                                     NETGENPlugin_Internals&             internalShapes);
129
130   static void AddIntVerticesInSolids(const netgen::OCCGeometry&         occgeom,
131                                     netgen::Mesh&                       ngMesh,
132                                     std::vector<const SMDS_MeshNode*>&  nodeVec,
133                                     NETGENPlugin_Internals&             internalShapes);
134
135   static SMESH_ComputeErrorPtr
136     AddSegmentsToMesh(netgen::Mesh&                         ngMesh,
137                       netgen::OCCGeometry&                  geom,
138                       const TSideVector&                    wires,
139                       SMESH_MesherHelper&                   helper,
140                       std::vector< const SMDS_MeshNode* > & nodeVec);
141
142   void SetDefaultParameters();
143
144   static void RemoveTmpFiles();
145
146   static SMESH_ComputeErrorPtr ReadErrors(const std::vector< const SMDS_MeshNode* >& nodeVec);
147
148
149   static void toPython( const netgen::Mesh* ngMesh,
150                         const std::string&  pyFile); // debug
151
152  private:
153   SMESH_Mesh*          _mesh;
154   const TopoDS_Shape&  _shape;
155   bool                 _isVolume;
156   bool                 _optimize;
157   int                  _fineness;
158   bool                 _isViscousLayers2D;
159
160   const NETGENPlugin_SimpleHypothesis_2D * _simpleHyp;
161 };
162
163 //=============================================================================
164 /*!
165  * \brief Container of info needed to solve problems with internal shapes.
166  *
167  * Issue 0020676. It is made up as a class to be ready to extract from NETGEN
168  * and put in SMESH as soon as the same solution is needed somewhere else.
169  * The approach is to precompute internal edges in 2D and internal faces in 3D
170  * and put their mesh correctly (twice) into netgen mesh.
171  * In 2D, this class finds internal edges in faces and their vertices.
172  * In 3D, it additionally finds internal faces, their edges shared with other faces,
173  * and their vertices shared by several internal edges. Nodes built on the found
174  * shapes and mesh faces built on the found internal faces are to be doubled in
175  * netgen mesh to emulate a "crack"
176  *
177  * For internal faces a more simple solution is found, which is just to duplicate
178  * mesh faces on internal geom faces without modeling a "real crack". For this
179  * reason findBorderElements() is no more used anywhere.
180  */
181 //=============================================================================
182
183 class NETGENPLUGIN_EXPORT NETGENPlugin_Internals
184 {
185   SMESH_Mesh&       _mesh;
186   bool              _is3D;
187   //2D
188   std::map<int,int> _e2face;//!<edges and their vertices in faces where they are TopAbs_INTERNAL
189   std::map<int,std::list<int> > _f2v;//!<faces with internal vertices
190   // 3D
191   std::set<int>     _intShapes;
192   std::set<int>     _borderFaces; //!< non-internal faces sharing the internal edge
193   std::map<int,std::list<int> > _s2v;//!<solids with internal vertices
194
195 public:
196   NETGENPlugin_Internals( SMESH_Mesh& mesh, const TopoDS_Shape& shape, bool is3D );
197
198   SMESH_Mesh& getMesh() const;
199
200   bool isShapeToPrecompute(const TopoDS_Shape& s);
201
202   // 2D meshing
203   // edges 
204   bool hasInternalEdges() const { return !_e2face.empty(); }
205   bool isInternalEdge( int id ) const { return _e2face.count( id ); }
206   const std::map<int,int>& getEdgesAndVerticesWithFaces() const { return _e2face; }
207   void getInternalEdges( TopTools_IndexedMapOfShape&  fmap,
208                          TopTools_IndexedMapOfShape&  emap,
209                          TopTools_IndexedMapOfShape&  vmap,
210                          std::list< SMESH_subMesh* > smToPrecompute[]);
211   // vertices
212   bool hasInternalVertexInFace() const { return !_f2v.empty(); }
213   const std::map<int,std::list<int> >& getFacesWithVertices() const { return _f2v; }
214
215   // 3D meshing
216   // faces
217   bool hasInternalFaces() const { return !_intShapes.empty(); }
218   bool isInternalShape( int id ) const { return _intShapes.count( id ); }
219   void findBorderElements( std::set< const SMDS_MeshElement*, TIDCompare > & borderElems );
220   bool isBorderFace( int faceID ) const { return _borderFaces.count( faceID ); }
221   void getInternalFaces( TopTools_IndexedMapOfShape&  fmap,
222                          TopTools_IndexedMapOfShape&  emap,
223                          std::list< SMESH_subMesh* >& facesSM,
224                          std::list< SMESH_subMesh* >& boundarySM);
225   // vertices
226   bool hasInternalVertexInSolid() const { return !_s2v.empty(); }
227   bool hasInternalVertexInSolid(int soID ) const { return _s2v.count(soID); }
228   const std::map<int,std::list<int> >& getSolidsWithVertices() const { return _s2v; }
229
230
231 };
232
233 //================================================================================
234 /*!
235  * \brief It correctly initializes netgen library at constructor and
236  *        correctly finishes using netgen library at destructor
237  */
238 //================================================================================
239
240 struct NETGENPLUGIN_EXPORT NETGENPlugin_NetgenLibWrapper
241 {
242   nglib::Ng_Mesh * _ngMesh;
243   NETGENPlugin_NetgenLibWrapper();
244   ~NETGENPlugin_NetgenLibWrapper();
245   void setMesh( nglib::Ng_Mesh* mesh );
246 };
247
248 #endif