1 // Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // NETGENPlugin : C++ implementation
24 // File : NETGENPlugin_Mesher.hxx
25 // Author : Michael Sazonov (OCN)
29 #ifndef _NETGENPlugin_Mesher_HXX_
30 #define _NETGENPlugin_Mesher_HXX_
32 #include "NETGENPlugin_Defs.hxx"
34 #include <StdMeshers_FaceSide.hxx>
35 #include <SMDS_MeshElement.hxx>
36 #include <SMESH_Algo.hxx>
37 #include <SMESH_ProxyMesh.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
49 class NETGENPlugin_Hypothesis;
50 class NETGENPlugin_Internals;
51 class NETGENPlugin_SimpleHypothesis_2D;
55 class SMESH_MesherHelper;
56 class StdMeshers_ViscousLayers;
62 //=============================================================================
64 * \brief Struct storing nb of entities in netgen mesh
66 //=============================================================================
68 struct NETGENPlugin_ngMeshInfo
70 int _nbNodes, _nbSegments, _nbFaces, _nbVolumes;
71 bool _elementsRemoved; // case where netgen can remove free nodes
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);
78 //================================================================================
80 * \brief It correctly initializes netgen library at constructor and
81 * correctly finishes using netgen library at destructor
83 //================================================================================
85 struct NETGENPLUGIN_EXPORT NETGENPlugin_NetgenLibWrapper
88 netgen::Mesh * _ngMesh;
90 NETGENPlugin_NetgenLibWrapper();
91 ~NETGENPlugin_NetgenLibWrapper();
92 void setMesh( nglib::Ng_Mesh* mesh );
93 nglib::Ng_Mesh* ngMesh() { return (nglib::Ng_Mesh*)(void*)_ngMesh; }
97 static int GenerateMesh(netgen::OCCGeometry& occgeo, int startWith, int endWith,
98 netgen::Mesh* & ngMesh);
99 int GenerateMesh(netgen::OCCGeometry& occgeo, int startWith, int endWith )
101 return GenerateMesh( occgeo, startWith, endWith, _ngMesh );
104 static void CalcLocalH( netgen::Mesh * ngMesh );
106 static void RemoveTmpFiles();
107 static int& instanceCounter();
108 void setOutputFile(std::string);
111 std::string getOutputFileName();
112 void removeOutputFile();
113 std::string _outputFileName;
117 std::streambuf* _coutBuffer; // to re-/store cout.rdbuf()
120 //=============================================================================
122 * \brief This class calls the NETGEN mesher of OCC geometry
124 //=============================================================================
126 class NETGENPLUGIN_EXPORT NETGENPlugin_Mesher
129 // ---------- PUBLIC METHODS ----------
131 NETGENPlugin_Mesher (SMESH_Mesh* mesh, const TopoDS_Shape& aShape,
132 const bool isVolume);
133 ~NETGENPlugin_Mesher();
134 void SetSelfPointer( NETGENPlugin_Mesher ** ptr );
136 void SetParameters(const NETGENPlugin_Hypothesis* hyp);
137 void SetParameters(const NETGENPlugin_SimpleHypothesis_2D* hyp);
138 void SetParameters(const StdMeshers_ViscousLayers* hyp );
139 void SetViscousLayers2DAssigned(bool isAssigned) { _isViscousLayers2D = isAssigned; }
141 void SetLocalSizeForChordalError( netgen::OCCGeometry& occgeo, netgen::Mesh& ngMesh );
142 static void SetLocalSize( netgen::OCCGeometry& occgeo, netgen::Mesh& ngMesh );
146 bool Evaluate(MapShapeNbElems& aResMap);
148 double GetProgress(const SMESH_Algo* holder,
149 const int * algoProgressTic,
150 const double * algoProgress) const;
152 static void PrepareOCCgeometry(netgen::OCCGeometry& occgeom,
153 const TopoDS_Shape& shape,
155 std::list< SMESH_subMesh* > * meshedSM=0,
156 NETGENPlugin_Internals* internalShapes=0);
158 static double GetDefaultMinSize(const TopoDS_Shape& shape,
159 const double maxSize);
161 static void RestrictLocalSize(netgen::Mesh& ngMesh,
164 const bool overrideMinH=true);
166 static int FillSMesh(const netgen::OCCGeometry& occgeom,
167 netgen::Mesh& ngMesh,
168 const NETGENPlugin_ngMeshInfo& initState,
170 std::vector<const SMDS_MeshNode*>& nodeVec,
171 SMESH_Comment& comment,
172 SMESH_MesherHelper* quadHelper=0);
174 bool FillNgMesh(netgen::OCCGeometry& occgeom,
175 netgen::Mesh& ngMesh,
176 std::vector<const SMDS_MeshNode*>& nodeVec,
177 const std::list< SMESH_subMesh* > & meshedSM,
178 SMESH_MesherHelper* quadHelper=0,
179 SMESH_ProxyMesh::Ptr proxyMesh=SMESH_ProxyMesh::Ptr());
181 static void FixIntFaces(const netgen::OCCGeometry& occgeom,
182 netgen::Mesh& ngMesh,
183 NETGENPlugin_Internals& internalShapes);
185 static bool FixFaceMesh(const netgen::OCCGeometry& occgeom,
186 netgen::Mesh& ngMesh,
189 static void AddIntVerticesInFaces(const netgen::OCCGeometry& occgeom,
190 netgen::Mesh& ngMesh,
191 std::vector<const SMDS_MeshNode*>& nodeVec,
192 NETGENPlugin_Internals& internalShapes);
194 static void AddIntVerticesInSolids(const netgen::OCCGeometry& occgeom,
195 netgen::Mesh& ngMesh,
196 std::vector<const SMDS_MeshNode*>& nodeVec,
197 NETGENPlugin_Internals& internalShapes);
199 static SMESH_ComputeErrorPtr
200 AddSegmentsToMesh(netgen::Mesh& ngMesh,
201 netgen::OCCGeometry& geom,
202 const TSideVector& wires,
203 SMESH_MesherHelper& helper,
204 std::vector< const SMDS_MeshNode* > & nodeVec,
205 const bool overrideMinH=true);
207 void SetDefaultParameters();
209 static SMESH_ComputeErrorPtr ReadErrors(const std::vector< const SMDS_MeshNode* >& nodeVec);
212 static void toPython( const netgen::Mesh* ngMesh ); // debug
217 const TopoDS_Shape& _shape;
221 bool _isViscousLayers2D;
222 double _chordalError;
223 netgen::Mesh* _ngMesh;
224 netgen::OCCGeometry* _occgeom;
227 volatile int _progressTic;
228 volatile double _ticTime; // normalized [0,1] compute time per a SMESH_Algo::_progressTic
229 volatile double _totalTime;
231 const NETGENPlugin_SimpleHypothesis_2D * _simpleHyp;
232 const StdMeshers_ViscousLayers* _viscousLayersHyp;
234 // a pointer to NETGENPlugin_Mesher* field of the holder, that will be
235 // nullified at destruction of this
236 NETGENPlugin_Mesher ** _ptrToMe;
239 //=============================================================================
241 * \brief Container of info needed to solve problems with internal shapes.
243 * Issue 0020676. It is made up as a class to be ready to extract from NETGEN
244 * and put in SMESH as soon as the same solution is needed somewhere else.
245 * The approach is to precompute internal edges in 2D and internal faces in 3D
246 * and put their mesh correctly (twice) into netgen mesh.
247 * In 2D, this class finds internal edges in faces and their vertices.
248 * In 3D, it additionally finds internal faces, their edges shared with other faces,
249 * and their vertices shared by several internal edges. Nodes built on the found
250 * shapes and mesh faces built on the found internal faces are to be doubled in
251 * netgen mesh to emulate a "crack"
253 * For internal faces a more simple solution is found, which is just to duplicate
254 * mesh faces on internal geom faces without modeling a "real crack". For this
255 * reason findBorderElements() is no more used anywhere.
257 //=============================================================================
259 class NETGENPLUGIN_EXPORT NETGENPlugin_Internals
264 std::map<int,int> _e2face;//!<edges and their vertices in faces where they are TopAbs_INTERNAL
265 std::map<int,std::list<int> > _f2v;//!<faces with internal vertices
267 std::set<int> _intShapes;
268 std::set<int> _borderFaces; //!< non-internal faces sharing the internal edge
269 std::map<int,std::list<int> > _s2v;//!<solids with internal vertices
272 NETGENPlugin_Internals( SMESH_Mesh& mesh, const TopoDS_Shape& shape, bool is3D );
274 SMESH_Mesh& getMesh() const;
276 bool isShapeToPrecompute(const TopoDS_Shape& s);
280 bool hasInternalEdges() const { return !_e2face.empty(); }
281 bool isInternalEdge( int id ) const { return _e2face.count( id ); }
282 const std::map<int,int>& getEdgesAndVerticesWithFaces() const { return _e2face; }
283 void getInternalEdges( TopTools_IndexedMapOfShape& fmap,
284 TopTools_IndexedMapOfShape& emap,
285 TopTools_IndexedMapOfShape& vmap,
286 std::list< SMESH_subMesh* > smToPrecompute[]);
288 bool hasInternalVertexInFace() const { return !_f2v.empty(); }
289 const std::map<int,std::list<int> >& getFacesWithVertices() const { return _f2v; }
293 bool hasInternalFaces() const { return !_intShapes.empty(); }
294 bool isInternalShape( int id ) const { return _intShapes.count( id ); }
295 void findBorderElements( std::set< const SMDS_MeshElement*, TIDCompare > & borderElems );
296 bool isBorderFace( int faceID ) const { return _borderFaces.count( faceID ); }
297 void getInternalFaces( TopTools_IndexedMapOfShape& fmap,
298 TopTools_IndexedMapOfShape& emap,
299 std::list< SMESH_subMesh* >& facesSM,
300 std::list< SMESH_subMesh* >& boundarySM);
302 bool hasInternalVertexInSolid() const { return !_s2v.empty(); }
303 bool hasInternalVertexInSolid(int soID ) const { return _s2v.count(soID); }
304 const std::map<int,std::list<int> >& getSolidsWithVertices() const { return _s2v; }