Salome HOME
Update copyright info (2010->2011)
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_Mesher.hxx
1 //  Copyright (C) 2007-2011  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 #include "StdMeshers_FaceSide.hxx"
34 #include "SMDS_MeshElement.hxx"
35 #include "SMESH_Algo.hxx"
36
37 namespace nglib {
38 #include <nglib.h>
39 }
40
41 #include <map>
42 #include <vector>
43 #include <set>
44
45 class SMESH_Mesh;
46 class SMESH_Comment;
47 class SMESHDS_Mesh;
48 class TopoDS_Shape;
49 class TopTools_DataMapOfShapeShape;
50 class TopTools_IndexedMapOfShape;
51 class NETGENPlugin_Hypothesis;
52 class NETGENPlugin_SimpleHypothesis_2D;
53 class NETGENPlugin_Internals;
54 namespace netgen {
55   class OCCGeometry;
56   class Mesh;
57 }
58 //=============================================================================
59 /*!
60  * \brief Struct storing nb of entities in netgen mesh
61  */
62 //=============================================================================
63
64 struct NETGENPlugin_ngMeshInfo
65 {
66   int _nbNodes, _nbSegments, _nbFaces, _nbVolumes;
67   NETGENPlugin_ngMeshInfo( netgen::Mesh* ngMesh=0);
68 };
69
70 //=============================================================================
71 /*!
72  * \brief This class calls the NETGEN mesher of OCC geometry
73  */
74 //=============================================================================
75
76 class NETGENPLUGIN_EXPORT NETGENPlugin_Mesher 
77 {
78  public:
79   // ---------- PUBLIC METHODS ----------
80
81   NETGENPlugin_Mesher (SMESH_Mesh* mesh, const TopoDS_Shape& aShape,
82                        const bool isVolume);
83
84   void SetParameters(const NETGENPlugin_Hypothesis*          hyp);
85   void SetParameters(const NETGENPlugin_SimpleHypothesis_2D* hyp);
86
87   bool Compute();
88
89   bool Evaluate(MapShapeNbElems& aResMap);
90
91   static void PrepareOCCgeometry(netgen::OCCGeometry&          occgeom,
92                                  const TopoDS_Shape&           shape,
93                                  SMESH_Mesh&                   mesh,
94                                  std::list< SMESH_subMesh* > * meshedSM=0,
95                                  NETGENPlugin_Internals*       internalShapes=0);
96
97   static int FillSMesh(const netgen::OCCGeometry&          occgeom,
98                        const netgen::Mesh&                 ngMesh,
99                        const NETGENPlugin_ngMeshInfo&      initState,
100                        SMESH_Mesh&                         sMesh,
101                        std::vector<const SMDS_MeshNode*>&  nodeVec,
102                        SMESH_Comment&                      comment);
103
104   bool fillNgMesh(const netgen::OCCGeometry&          occgeom,
105                   netgen::Mesh&                       ngMesh,
106                   std::vector<const SMDS_MeshNode*>&  nodeVec,
107                   const std::list< SMESH_subMesh* > & meshedSM);
108
109   static void fixIntFaces(const netgen::OCCGeometry& occgeom,
110                           netgen::Mesh&              ngMesh,
111                           NETGENPlugin_Internals&    internalShapes);
112
113   static void addIntVerticesInFaces(const netgen::OCCGeometry&          occgeom,
114                                     netgen::Mesh&                       ngMesh,
115                                     std::vector<const SMDS_MeshNode*>&  nodeVec,
116                                     NETGENPlugin_Internals&             internalShapes);
117
118   static void addIntVerticesInSolids(const netgen::OCCGeometry&         occgeom,
119                                     netgen::Mesh&                       ngMesh,
120                                     std::vector<const SMDS_MeshNode*>&  nodeVec,
121                                     NETGENPlugin_Internals&             internalShapes);
122
123   void defaultParameters();
124
125   static void RemoveTmpFiles();
126
127   static SMESH_ComputeErrorPtr readErrors(const std::vector< const SMDS_MeshNode* >& nodeVec);
128
129  private:
130   SMESH_Mesh*          _mesh;
131   const TopoDS_Shape&  _shape;
132   bool                 _isVolume;
133   bool                 _optimize;
134
135   const NETGENPlugin_SimpleHypothesis_2D * _simpleHyp;
136   std::map< int, std::pair<int,int> >      _faceDescriptors;
137 };
138
139 //=============================================================================
140 /*!
141  * \brief Container of info needed to solve problems with internal shapes.
142  *
143  * Issue 0020676. It is made up as a class to be ready to extract from NETGEN
144  * and put in SMESH as soon as the same solution is needed somewhere else.
145  * The approach is to precompute internal edges in 2D and internal faces in 3D
146  * and put their mesh correctly (twice) into netgen mesh.
147  * In 2D, this class finds internal edges in faces and their vertices.
148  * In 3D, it additionally finds internal faces, their edges shared with other faces,
149  * and their vertices shared by several internal edges. Nodes built on the found
150  * shapes and mesh faces built on the found internal faces are to be doubled in
151  * netgen mesh to emulate a "crack"
152  *
153  * For internal faces a more simple solution is found, which is just to duplicate
154  * mesh faces on internal geom faces without modeling a "real crack". For this
155  * reason findBorderElements() is no more used anywhere.
156  */
157 //=============================================================================
158
159 class NETGENPLUGIN_EXPORT NETGENPlugin_Internals
160 {
161   SMESH_Mesh&       _mesh;
162   bool              _is3D;
163   //2D
164   std::map<int,int> _e2face;//!<edges and their vertices in faces where they are TopAbs_INTERNAL
165   std::map<int,std::list<int> > _f2v;//!<faces with internal vertices
166   // 3D
167   std::set<int>     _intShapes;
168   std::set<int>     _borderFaces; //!< non-intrnal faces sharing the internal edge
169   std::map<int,std::list<int> > _s2v;//!<solids with internal vertices
170
171 public:
172   NETGENPlugin_Internals( SMESH_Mesh& mesh, const TopoDS_Shape& shape, bool is3D );
173
174   SMESH_Mesh& getMesh() const;
175
176   bool isShapeToPrecompute(const TopoDS_Shape& s);
177
178   // 2D meshing
179   // edges 
180   bool hasInternalEdges() const { return !_e2face.empty(); }
181   bool isInternalEdge( int id ) const { return _e2face.count( id ); }
182   const std::map<int,int>& getEdgesAndVerticesWithFaces() const { return _e2face; }
183   void getInternalEdges( TopTools_IndexedMapOfShape&  fmap,
184                          TopTools_IndexedMapOfShape&  emap,
185                          TopTools_IndexedMapOfShape&  vmap,
186                          std::list< SMESH_subMesh* >& smToPrecompute);
187   // vertices
188   bool hasInternalVertexInFace() const { return !_f2v.empty(); }
189   const std::map<int,std::list<int> >& getFacesWithVertices() const { return _f2v; }
190
191   // 3D meshing
192   // faces
193   bool hasInternalFaces() const { return !_intShapes.empty(); }
194   bool isInternalShape( int id ) const { return _intShapes.count( id ); }
195   void findBorderElements( std::set< const SMDS_MeshElement*, TIDCompare > & borderElems );
196   bool isBorderFace( int faceID ) const { return _borderFaces.count( faceID ); }
197   void getInternalFaces( TopTools_IndexedMapOfShape&  fmap,
198                          TopTools_IndexedMapOfShape&  emap,
199                          std::list< SMESH_subMesh* >& facesSM,
200                          std::list< SMESH_subMesh* >& boundarySM);
201   // vertices
202   bool hasInternalVertexInSolid() const { return !_s2v.empty(); }
203   bool hasInternalVertexInSolid(int soID ) const { return _s2v.count(soID); }
204   const std::map<int,std::list<int> >& getSolidsWithVertices() const { return _s2v; }
205
206
207 };
208
209 //================================================================================
210 /*!
211  * \brief It correctly initializes netgen library at constructor and
212  *        correctly finishes using netgen library at destructor
213  */
214 //================================================================================
215
216 struct NETGENPLUGIN_EXPORT NETGENPlugin_NetgenLibWrapper
217 {
218   nglib::Ng_Mesh * _ngMesh;
219   NETGENPlugin_NetgenLibWrapper();
220   ~NETGENPlugin_NetgenLibWrapper();
221   void setMesh( nglib::Ng_Mesh* mesh );
222 };
223
224 #endif