Salome HOME
Replacing ctpl by boost::thread_pool
[modules/smesh.git] / src / SMESH / SMESH_Mesh.hxx
1 // Copyright (C) 2007-2022  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 //  File   : SMESH_Mesh.hxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : SMESH
26 //
27 #ifndef _SMESH_MESH_HXX_
28 #define _SMESH_MESH_HXX_
29
30 #include "SMESH_SMESH.hxx"
31
32 #include "SMDSAbs_ElementType.hxx"
33 #include "SMESH_ComputeError.hxx"
34 #include "SMESH_Controls.hxx"
35 #include "SMESH_Hypothesis.hxx"
36 #include "SMDS_Iterator.hxx"
37
38 #include "Utils_SALOME_Exception.hxx"
39
40 #include <TopoDS_Shape.hxx>
41 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
42 #include <TopTools_ListOfShape.hxx>
43
44 #include "MEDCouplingMemArray.hxx"
45
46 #include <map>
47 #include <list>
48 #include <vector>
49 #include <ostream>
50 #include <boost/filesystem.hpp>
51 #include <boost/asio/thread_pool.hpp>
52
53 #ifdef WIN32
54 #pragma warning(disable:4251) // Warning DLL Interface ...
55 #pragma warning(disable:4290) // Warning Exception ...
56 #endif
57
58 class SMESHDS_Command;
59 class SMESHDS_Document;
60 class SMESHDS_GroupBase;
61 class SMESHDS_Hypothesis;
62 class SMESHDS_Mesh;
63 class SMESH_Gen;
64 class SMESH_Group;
65 class SMESH_HypoFilter;
66 class SMESH_subMesh;
67 class TopoDS_Solid;
68
69 class DriverMED_W_SMESHDS_Mesh;
70
71 typedef std::list<int> TListOfInt;
72 typedef std::list<TListOfInt> TListOfListOfInt;
73
74 class SMESH_EXPORT SMESH_Mesh
75 {
76  public:
77   SMESH_Mesh(int               theLocalId,
78              SMESH_Gen*        theGen,
79              bool              theIsEmbeddedMode,
80              SMESHDS_Document* theDocument);
81
82   virtual ~SMESH_Mesh();
83
84   /*!
85    * \brief Set geometry to be meshed
86    */
87   void ShapeToMesh(const TopoDS_Shape & aShape);
88   /*!
89    * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
90    */
91   TopoDS_Shape GetShapeToMesh() const;
92   /*!
93    * \brief Return true if there is a geometry to be meshed, not PseudoShape()
94    */
95   bool HasShapeToMesh() const { return _isShapeToMesh; }
96
97   void UndefShapeToMesh() { _isShapeToMesh = false; }
98
99   /*!
100    * \brief Return diagonal size of bounding box of shape to mesh.
101    */
102   double GetShapeDiagonalSize() const;
103   /*!
104    * \brief Return diagonal size of bounding box of a shape.
105    */
106   static double GetShapeDiagonalSize(const TopoDS_Shape & aShape);
107   /*!
108    * \brief Return a solid which is returned by GetShapeToMesh() if
109    *        a real geometry to be meshed was not set
110    */
111   static const TopoDS_Solid& PseudoShape();
112
113   /*!
114    * \brief Load mesh from study file
115    */
116   void Load();
117   /*!
118    * \brief Remove all nodes and elements
119    */
120   void Clear();
121   /*!
122    * \brief Remove all nodes and elements of indicated shape
123    */
124   void ClearSubMesh(const int theShapeId);
125
126   /*!
127    * consult DriverMED_R_SMESHDS_Mesh::ReadStatus for returned value
128    */
129   int UNVToMesh(const char* theFileName);
130
131   int MEDToMesh(const char* theFileName, const char* theMeshName);
132
133   std::string STLToMesh(const char* theFileName);
134
135   int CGNSToMesh(const char* theFileName, const int theMeshIndex, std::string& theMeshName);
136
137   SMESH_ComputeErrorPtr GMFToMesh(const char* theFileName,
138                                   bool        theMakeRequiredGroups = true );
139
140   SMESH_Hypothesis::Hypothesis_Status
141   AddHypothesis(const TopoDS_Shape & aSubShape, int anHypId, std::string* error=0);
142
143   SMESH_Hypothesis::Hypothesis_Status
144   RemoveHypothesis(const TopoDS_Shape & aSubShape, int anHypId);
145
146   const std::list <const SMESHDS_Hypothesis * >&
147   GetHypothesisList(const TopoDS_Shape & aSubShape) const;
148
149   const SMESH_Hypothesis * GetHypothesis(const TopoDS_Shape &    aSubShape,
150                                          const SMESH_HypoFilter& aFilter,
151                                          const bool              andAncestors,
152                                          TopoDS_Shape*           assignedTo=0) const;
153
154   int GetHypotheses(const TopoDS_Shape &                     aSubShape,
155                     const SMESH_HypoFilter&                  aFilter,
156                     std::list< const SMESHDS_Hypothesis * >& aHypList,
157                     const bool                               andAncestors,
158                     std::list< TopoDS_Shape > *              assignedTo=0) const;
159
160   const SMESH_Hypothesis * GetHypothesis(const SMESH_subMesh *   aSubMesh,
161                                          const SMESH_HypoFilter& aFilter,
162                                          const bool              andAncestors,
163                                          TopoDS_Shape*           assignedTo=0) const;
164
165   int GetHypotheses(const SMESH_subMesh *                    aSubMesh,
166                     const SMESH_HypoFilter&                  aFilter,
167                     std::list< const SMESHDS_Hypothesis * >& aHypList,
168                     const bool                               andAncestors,
169                     std::list< TopoDS_Shape > *              assignedTo=0) const;
170
171   SMESH_Hypothesis * GetHypothesis(const int aHypID) const;
172
173   const std::list<SMESHDS_Command*> & GetLog();
174
175   void ClearLog();
176
177   int GetId() const          { return _id; }
178
179   bool MeshExists( int meshId ) const;
180
181   SMESH_Mesh* FindMesh( int meshId ) const;
182
183   SMESHDS_Mesh * GetMeshDS() { return _meshDS; }
184
185   const SMESHDS_Mesh * GetMeshDS() const { return _meshDS; }
186
187   SMESH_Gen *GetGen()        { return _gen; }
188
189   SMESH_subMesh *GetSubMesh(const TopoDS_Shape & aSubShape);
190
191   SMESH_subMesh *GetSubMeshContaining(const TopoDS_Shape & aSubShape) const;
192
193   SMESH_subMesh *GetSubMeshContaining(const int aShapeID) const;
194   /*!
195    * \brief Return submeshes of groups containing the given subshape
196    */
197   std::list<SMESH_subMesh*> GetGroupSubMeshesContaining(const TopoDS_Shape & shape) const;
198   /*!
199    * \brief Say all submeshes that theChangedHyp has been modified
200    */
201   void NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* theChangedHyp);
202
203   // const std::list < SMESH_subMesh * >&
204   // GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp) throw(SALOME_Exception);
205   /*!
206    * \brief Return True if anHyp is used to mesh aSubShape
207    */
208   bool IsUsedHypothesis(SMESHDS_Hypothesis *  anHyp,
209                         const SMESH_subMesh * aSubMesh);
210   /*!
211    * \brief check if a hypothesis allowing notconform mesh is present
212    */
213   bool IsNotConformAllowed() const;
214
215   bool IsMainShape(const TopoDS_Shape& theShape) const;
216
217   TopoDS_Shape GetShapeByEntry(const std::string& entry) const;
218
219   /*!
220    * \brief Return list of ancestors of theSubShape in the order
221    *        that lower dimension shapes come first
222    */
223   const TopTools_ListOfShape& GetAncestors(const TopoDS_Shape& theSubShape) const;
224
225   void SetAutoColor(bool theAutoColor);
226
227   bool GetAutoColor();
228
229   /*!
230    * \brief Set the flag meaning that the mesh has been edited "manually".
231    * It is to set to false after Clear() and to set to true by MeshEditor
232    */
233   void SetIsModified(bool isModified);
234
235   bool GetIsModified() const { return _isModified; }
236
237   /*!
238    * \brief Return true if the mesh has been edited since a total re-compute
239    *        and those modifications may prevent successful partial re-compute.
240    *        As a side effect reset _isModified flag if mesh is empty
241    */
242   bool HasModificationsToDiscard() const;
243
244   /*!
245    * \brief Return true if all sub-meshes are computed OK - to update an icon
246    */
247   bool IsComputedOK();
248
249   /*!
250    * \brief Return data map of descendant to ancestor shapes
251    */
252   typedef TopTools_IndexedDataMapOfShapeListOfShape TAncestorMap;
253   const TAncestorMap& GetAncestorMap() const { return _mapAncestors; }
254
255   /*!
256    * \brief Check group names for duplications.
257    *  Consider maximum group name length stored in MED file
258    */
259   bool HasDuplicatedGroupNamesMED();
260
261   /*!
262    * \brief Exception thrown by Export*() in case if a mesh is too large for export
263    *        due to limitation of a format
264    */
265   struct TooLargeForExport : public std::runtime_error
266   {
267     TooLargeForExport(const char* format):runtime_error(format) {}
268   };
269
270   MEDCoupling::MCAuto<MEDCoupling::DataArrayByte>
271     ExportMEDCoupling(const char*         theMeshName = NULL,
272                       bool                theAutoGroups = true,
273                       const SMESHDS_Mesh* theMeshPart = 0,
274                       bool                theAutoDimension = false,
275                       bool                theAddODOnVertices = false,
276                       double              theZTolerance = -1.,
277                       bool                theSaveNumbers = true);
278
279   void ExportMED(const char *        theFile,
280                  const char*         theMeshName = NULL,
281                  bool                theAutoGroups = true,
282                  int                 theVersion = -1,
283                  const SMESHDS_Mesh* theMeshPart = 0,
284                  bool                theAutoDimension = false,
285                  bool                theAddODOnVertices = false,
286                  double              theZTolerance = -1.,
287                  bool                theSaveNumbers = true);
288
289   void ExportDAT(const char *        file,
290                  const SMESHDS_Mesh* meshPart = 0,
291                  const bool          renumber = true);
292   void ExportUNV(const char *        file,
293                  const SMESHDS_Mesh* meshPart = 0,
294                  const bool          renumber = true);
295   void ExportSTL(const char *        file,
296                  const bool          isascii,
297                  const char *        name = 0,
298                  const SMESHDS_Mesh* meshPart = 0);
299   void ExportCGNS(const char *        file,
300                   const SMESHDS_Mesh* mesh,
301                   const char *        meshName = 0,
302                   const bool          groupElemsByType = false);
303   void ExportGMF(const char *        file,
304                  const SMESHDS_Mesh* mesh,
305                  bool                withRequiredGroups = true );
306
307   double GetComputeProgress() const;
308
309   smIdType NbNodes() const;
310   smIdType Nb0DElements() const;
311   smIdType NbBalls() const;
312
313   smIdType NbEdges(SMDSAbs_ElementOrder order = ORDER_ANY) const;
314
315   smIdType NbFaces(SMDSAbs_ElementOrder order = ORDER_ANY) const;
316   smIdType NbTriangles(SMDSAbs_ElementOrder order = ORDER_ANY) const;
317   smIdType NbQuadrangles(SMDSAbs_ElementOrder order = ORDER_ANY) const;
318   smIdType NbBiQuadQuadrangles() const;
319   smIdType NbBiQuadTriangles() const;
320   smIdType NbPolygons(SMDSAbs_ElementOrder order = ORDER_ANY) const;
321
322   smIdType NbVolumes(SMDSAbs_ElementOrder order = ORDER_ANY) const;
323   smIdType NbTetras(SMDSAbs_ElementOrder order = ORDER_ANY) const;
324   smIdType NbHexas(SMDSAbs_ElementOrder order = ORDER_ANY) const;
325   smIdType NbTriQuadraticHexas() const;
326   smIdType NbPyramids(SMDSAbs_ElementOrder order = ORDER_ANY) const;
327   smIdType NbPrisms(SMDSAbs_ElementOrder order = ORDER_ANY) const;
328   smIdType NbQuadPrisms() const;
329   smIdType NbBiQuadPrisms() const;
330   smIdType NbHexagonalPrisms() const;
331   smIdType NbPolyhedrons() const;
332
333   smIdType NbSubMesh() const;
334
335   size_t NbGroup() const { return _mapGroup.size(); }
336
337   int NbMeshes() const; // nb meshes in the Study
338
339   SMESH_Group* AddGroup (const SMDSAbs_ElementType theType,
340                          const char*               theName,
341                          const int                 theId = -1,
342                          const TopoDS_Shape&       theShape = TopoDS_Shape(),
343                          const SMESH_PredicatePtr& thePredicate = SMESH_PredicatePtr());
344
345   SMESH_Group* AddGroup (SMESHDS_GroupBase* groupDS);
346
347   typedef boost::shared_ptr< SMDS_Iterator<SMESH_Group*> > GroupIteratorPtr;
348   GroupIteratorPtr GetGroups() const;
349
350   std::list<int> GetGroupIds() const;
351
352   SMESH_Group* GetGroup (const int theGroupID) const;
353
354   bool RemoveGroup (const int theGroupID);
355
356   SMESH_Group* ConvertToStandalone ( int theGroupID );
357
358   struct TCallUp // callback from SMESH to SMESH_I level
359   {
360     virtual void RemoveGroup( const int theGroupID )=0;
361     virtual void HypothesisModified( int hypID, bool updateIcons )=0;
362     virtual void Load()=0;
363     virtual bool IsLoaded()=0;
364     virtual TopoDS_Shape GetShapeByEntry(const std::string& entry)=0;
365     virtual ~TCallUp() {}
366   };
367   void SetCallUp( TCallUp * upCaller );
368
369   bool SynchronizeGroups();
370
371
372   SMDSAbs_ElementType GetElementType( const smIdType id, const bool iselem );
373
374   void ClearMeshOrder();
375   void SetMeshOrder(const TListOfListOfInt& theOrder );
376   const TListOfListOfInt& GetMeshOrder() const;
377
378   // sort submeshes according to stored mesh order
379   bool SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const;
380
381   // return true if given order of sub-meshes is OK
382   bool IsOrderOK( const SMESH_subMesh* smBefore,
383                   const SMESH_subMesh* smAfter ) const;
384
385   std::ostream& Dump(std::ostream & save);
386
387   // Parallel computation functions
388
389   void Lock() {_my_lock.lock();};
390   void Unlock() {_my_lock.unlock();};
391
392   int GetNbThreads(){return _NbThreads;};
393   void SetNbThreads(int nbThreads){_NbThreads=nbThreads;};
394
395   void InitPoolThreads(){_pool = new boost::asio::thread_pool(_NbThreads);};
396   void DeletePoolThreads(){delete _pool;};
397
398   void wait(){_pool->join(); DeletePoolThreads(); InitPoolThreads(); }
399
400   bool IsParallel(){return _NbThreads > 0;}
401
402   // Temporary folder used during parallel Computation
403   boost::filesystem::path tmp_folder;
404   boost::asio::thread_pool *     _pool = nullptr; //thread pool for computation
405
406
407 private:
408
409   void exportMEDCommmon(DriverMED_W_SMESHDS_Mesh& myWriter,
410                         const char*               theMeshName,
411                         bool                      theAutoGroups,
412                         const SMESHDS_Mesh*       meshPart,
413                         bool                      theAutoDimension,
414                         bool                      theAddODOnVertices,
415                         double                    theZTolerance,
416                         bool                      theSaveNumbers);
417
418  private:
419   void fillAncestorsMap(const TopoDS_Shape& theShape);
420   void getAncestorsSubMeshes(const TopoDS_Shape&            theSubShape,
421                              std::vector< SMESH_subMesh* >& theSubMeshes) const;
422
423 protected:
424   int                        _id;           // id given by creator (unique within the creator instance)
425   int                        _groupId;      // id generator for group objects
426   int                        _nbSubShapes;  // initial nb of subshapes in the shape to mesh
427   bool                       _isShapeToMesh;// set to true when a shape is given (only once)
428   SMESHDS_Document *         _document;
429   SMESHDS_Mesh *             _meshDS;
430   SMESH_Gen *                _gen;
431   std::map <int, SMESH_Group*> _mapGroup;
432
433   class SubMeshHolder;
434   SubMeshHolder*             _subMeshHolder;
435
436   bool                       _isAutoColor;
437   bool                       _isModified; //!< modified since last total re-compute, issue 0020693
438
439   double                     _shapeDiagonal; //!< diagonal size of bounding box of shape to mesh
440
441   TopTools_IndexedDataMapOfShapeListOfShape _mapAncestors;
442
443   mutable std::vector<SMESH_subMesh*> _ancestorSubMeshes; // to speed up GetHypothes[ei]s()
444
445   TListOfListOfInt           _subMeshOrder;
446
447   // Struct calling methods at CORBA API implementation level, used to
448   // 1) make an upper level (SMESH_I) be consistent with a lower one (SMESH)
449   // when group removal is invoked by hyp modification (issue 0020918)
450   // 2) to forget not loaded mesh data at hyp modification
451   TCallUp*                    _callUp;
452
453   // Mutex for multhitreading write in SMESH_Mesh
454   std::mutex _my_lock;
455   int _NbThreads=0;
456
457 protected:
458   SMESH_Mesh();
459   SMESH_Mesh(const SMESH_Mesh&) {};
460 };
461
462 #endif