Salome HOME
Copyrights update 2015.
[plugins/hybridplugin.git] / src / HYBRIDPlugin / HYBRIDPlugin_Hypothesis.hxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // ---
21 // File   : HYBRIDPlugin_Hypothesis.hxx
22 // Author : Christian VAN WAMBEKE (CEA)
23 // ---
24 //
25 #ifndef HYBRIDPlugin_Hypothesis_HeaderFile
26 #define HYBRIDPlugin_Hypothesis_HeaderFile
27
28 #include "HYBRIDPlugin_Defs.hxx"
29
30 #include <SMDS_MeshNode.hxx>
31
32 #include "SMESH_Hypothesis.hxx"
33 #include "SMESH_Mesh_i.hxx"
34 #include "SMESH_Gen_i.hxx"
35 #include "SMESH_TypeDefs.hxx"
36 #include "utilities.h"
37
38 #include <stdexcept>
39 #include <map>
40 #include <vector>
41 #include <cstdio>
42
43 class HYBRIDPLUGIN_EXPORT HYBRIDPlugin_Hypothesis: public SMESH_Hypothesis
44 {
45 public:
46
47   HYBRIDPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen);
48
49   typedef std::map<std::vector<double>,double> THYBRIDEnforcedVertexCoordsValues;
50   typedef std::map<std::string,double> THYBRIDEnforcedVertexEntryValues;
51   
52   struct THYBRIDEnforcedVertex {
53     std::string name;
54     std::string geomEntry;
55     bool isCompound;
56     std::vector<double> coords;
57     std::string groupName;
58     double size;
59   };
60   
61   struct CompareHYBRIDEnforcedVertex {
62     bool operator () (const THYBRIDEnforcedVertex* e1, const THYBRIDEnforcedVertex* e2) const {
63       if (e1 && e2) {
64         if (e1->coords.size() && e2->coords.size())
65           return (e1->coords < e2->coords);
66         else
67           return (e1->geomEntry < e2->geomEntry);
68       }
69       return false;
70     }
71   };
72   typedef std::set< THYBRIDEnforcedVertex*, CompareHYBRIDEnforcedVertex > THYBRIDEnforcedVertexList;
73   // Map Coords / Enforced node
74   typedef std::map< std::vector<double>, THYBRIDEnforcedVertex* > TCoordsHYBRIDEnforcedVertexMap;
75   // Map geom entry / Enforced node
76   typedef std::map< std::string, THYBRIDEnforcedVertex* > TGeomEntryHYBRIDEnforcedVertexMap;
77   // Map groupName / Enforced node
78   typedef std::map< std::string, THYBRIDEnforcedVertexList > TGroupNameHYBRIDEnforcedVertexMap;
79   
80   ////////////////////
81   // Enforced meshes
82   ////////////////////
83   
84   struct THYBRIDEnforcedMesh {
85     int         persistID;
86     std::string name;
87     std::string entry;
88     std::string groupName;
89     SMESH::ElementType elementType;
90   };
91   
92   struct CompareHYBRIDEnforcedMesh {
93     bool operator () (const THYBRIDEnforcedMesh* e1, const THYBRIDEnforcedMesh* e2) const {
94       if (e1 && e2) {
95         if (e1->entry == e2->entry)
96           return (e1->elementType < e2->elementType);
97         else
98           return (e1->entry < e2->entry);
99       }
100       else
101         return false;
102     }
103   };
104   typedef std::set< THYBRIDEnforcedMesh*, CompareHYBRIDEnforcedMesh > THYBRIDEnforcedMeshList;
105   // Map mesh entry / Enforced mesh list
106   // ex: 0:1:2:1 -> [ ("Mesh_1", "0:1:2:1", TopAbs_NODE, ""),
107   //                  ("Mesh_1", "0:1:2:1", TopAbs_EDGE, "edge group")]
108   typedef std::map< std::string, THYBRIDEnforcedMeshList > TEntryHYBRIDEnforcedMeshListMap;
109   
110   typedef std::map<int,double> TID2SizeMap;
111
112   struct TIDMeshIDCompare {
113     bool operator () (const SMDS_MeshElement* e1, const SMDS_MeshElement* e2) const
114     { return e1->getMeshId() == e2->getMeshId() ? e1->GetID() < e2->GetID() : e1->getMeshId() < e2->getMeshId() ; }
115   };
116   
117   typedef std::map<const SMDS_MeshElement*, std::string, TIDMeshIDCompare > TIDSortedElemGroupMap;
118   typedef std::map<const SMDS_MeshNode*, std::string, TIDMeshIDCompare > TIDSortedNodeGroupMap;
119   typedef std::set<std::string> TSetStrings;
120
121   static const char* GetHypType() { return "HYBRID_Parameters"; }
122   /*!
123    * To mesh "holes" in a solid or not. Default is to mesh.
124    */
125   void SetToMeshHoles(bool toMesh);
126   bool GetToMeshHoles(bool checkFreeOption = false) const;
127   /*!
128    * To mesh layers on all wrap. Default is yes.
129    */
130   void SetLayersOnAllWrap(bool toMesh);
131   bool GetLayersOnAllWrap(bool checkFreeOption = false) const;
132   /*!
133    * To make groups of volumes of different domains when mesh is generated from skin.
134    * Default is to make groups.
135    * This option works only (1) for the mesh w/o shape and (2) if GetToMeshHoles() == true
136    */
137   void SetToMakeGroupsOfDomains(bool toMakeGroups);
138   bool GetToMakeGroupsOfDomains() const;
139   /*!
140    * Maximal size of memory to be used by the algorithm (in Megabytes)
141    */
142   void SetMaximumMemory(double MB);
143   double GetMaximumMemory() const;
144   /*!
145    * Initial size of memory to be used by the algorithm (in Megabytes) in
146    * automatic memory adjustment mode. Default is zero
147    */
148   void SetInitialMemory(double MB);
149   double GetInitialMemory() const;
150   /*!
151    * Optimization level: 0-none, 1-light, 2-medium, 3-standard+, 4-strong. Default is medium
152    */
153   enum OptimizationLevel { None = 0, Light, Medium, StandardPlus, Strong };
154   void SetOptimizationLevel(OptimizationLevel level);
155   OptimizationLevel GetOptimizationLevel() const;
156
157   /*!
158    * Collision Mode: 0-decrease, 1-stop. Default is decrease
159    */
160   enum CollisionMode { Decrease = 0, Stop };
161   void SetCollisionMode(CollisionMode level);
162   CollisionMode GetCollisionMode() const;
163   /*!
164    * BoundaryLayersGrowth: 0-Layer_Growth_Inward, 1-Layer_Growth_Outward. Default is Layer_Growth_Inward
165    */
166   enum BoundaryLayersGrowth { Layer_Growth_Inward = 0, Layer_Growth_Outward };
167   void SetBoundaryLayersGrowth(BoundaryLayersGrowth level);
168   BoundaryLayersGrowth GetBoundaryLayersGrowth() const;
169   /*!
170    * ElementGeneration: 0-Generation_Tetra_Dominant, 1-Generation_Hexa_Dominant. Default is Generation_Tetra_Dominant
171    */
172   enum ElementGeneration { Generation_Tetra_Dominant = 0, Generation_Hexa_Dominant };
173   void SetElementGeneration(ElementGeneration level);
174   ElementGeneration GetElementGeneration() const;
175     /*!
176      * To mesh adding extra normals at opening ridges and corners.
177      * Default is no.
178      */
179     void SetAddMultinormals(bool toAddMultinormals);
180     bool GetAddMultinormals() const;
181     /*!
182      * To mesh smoothing normals at closed ridges and corners.
183      * Default is no.
184      */
185     void SetSmoothNormals(bool toSmoothNormals);
186     bool GetSmoothNormals() const;
187     /*!
188      * To set height of the first layer.
189      */
190     void SetHeightFirstLayer(double HFL);
191     double GetHeightFirstLayer() const;
192     /*!
193      * To set boundary layers coefficient of geometric progression.
194      * Default is 1.0
195      */
196     void SetBoundaryLayersProgression(double BLP);
197     double GetBoundaryLayersProgression() const;
198     /*!
199      * To set multinormals angle threshold at opening ridges.
200      * Default is 30.0
201      */
202     void SetMultinormalsAngle(double MNA);
203     double GetMultinormalsAngle() const;
204     /*!
205      * To set number of boundary layers.
206      * Default is 1
207      */
208     void SetNbOfBoundaryLayers(short NBL);
209     short GetNbOfBoundaryLayers() const;
210
211
212   /*!
213    * Path to working directory
214    */
215   void SetWorkingDirectory(const std::string& path);
216   std::string GetWorkingDirectory() const;
217   /*!
218    * To keep working files or remove them. Log file remains in case of errors anyway.
219    */
220   void SetKeepFiles(bool toKeep);
221   bool GetKeepFiles() const;
222   /*!
223    * Verbose level [0-10]
224    *  0 - no standard output,
225    *  2 - prints the data, quality statistics of the skin and final meshes and
226    *     indicates when the final mesh is being saved. In addition the software
227    *     gives indication regarding the CPU time.
228    * 10 - same as 2 plus the main steps in the computation, quality statistics
229    *     histogram of the skin mesh, quality statistics histogram together with
230    *     the characteristics of the final mesh.
231    */
232   void SetVerboseLevel(short level);
233   short GetVerboseLevel() const;
234   /*!
235    * To create new nodes
236    */
237   void SetToCreateNewNodes(bool toCreate);
238   bool GetToCreateNewNodes() const;
239   /*!
240    * To use boundary recovery version which tries to create mesh on a very poor
241    * quality surface mesh
242    */
243   void SetToUseBoundaryRecoveryVersion(bool toUse);
244   bool GetToUseBoundaryRecoveryVersion() const;
245   /*!
246    * Applies finite-element correction by replacing overconstrained elements where
247    * it is possible. The process is cutting first the overconstrained edges and
248    * second the overconstrained facets. This insure that no edges have two boundary
249    * vertices and that no facets have three boundary vertices.
250    */
251   void SetFEMCorrection(bool toUseFem);
252   bool GetFEMCorrection() const;
253   /*!
254    * To removes initial central point.
255    */
256   void SetToRemoveCentralPoint(bool toRemove);
257   bool GetToRemoveCentralPoint() const;
258   /*!
259    * To set hiden/undocumented/advanced options
260    */
261   void SetTextOption(const std::string& option);
262   std::string GetTextOption() const;
263   /*!
264   * To define the volumic gradation
265   */
266   void SetGradation(double gradation);
267   double GetGradation() const ;
268   /*!
269   * Print log in standard output
270   */
271   void SetStandardOutputLog(bool logInStandardOutput);
272   bool GetStandardOutputLog() const ;
273   /*!
274   * Remove log file on success
275   */
276   void SetRemoveLogOnSuccess(bool removeLogOnSuccess);
277   bool GetRemoveLogOnSuccess() const ;
278     
279   
280 //   struct TEnforcedEdge {
281 //     long ID;
282 //     long node1;
283 //     long node2;
284 //     std::string groupName;
285 //   };
286   
287
288   /*!
289    * \brief Return command to run hybrid mesher excluding file prefix (-f)
290    */
291   static std::string CommandToRun(const HYBRIDPlugin_Hypothesis* hyp,
292                                   const bool                    hasShapeToMesh=true);
293   /*!
294    * \brief Return a unique file name
295    */
296   static std::string GetFileName(const HYBRIDPlugin_Hypothesis* hyp);
297   /*!
298    * \brief Return the name of executable
299    */
300   static std::string GetExeName();
301
302   /*!
303    * To set an enforced vertex
304    */
305   bool SetEnforcedVertex(std::string aName, std::string anEntry, std::string aGroupName,
306                          double size, double x=0.0, double y=0.0, double z=0.0, bool isCompound = false);
307   THYBRIDEnforcedVertex* GetEnforcedVertex(double x, double y, double z) throw (std::invalid_argument);
308   THYBRIDEnforcedVertex* GetEnforcedVertex(const std::string anEntry) throw (std::invalid_argument);
309   bool RemoveEnforcedVertex(double x=0.0, double y=0.0, double z=0.0, const std::string anEntry="" ) throw (std::invalid_argument);
310   const THYBRIDEnforcedVertexCoordsValues _GetEnforcedVerticesCoordsSize() const {return _enfVertexCoordsSizeList; }
311   const THYBRIDEnforcedVertexEntryValues  _GetEnforcedVerticesEntrySize() const {return _enfVertexEntrySizeList; }
312   const THYBRIDEnforcedVertexList         _GetEnforcedVertices() const { return _enfVertexList; }
313   const TCoordsHYBRIDEnforcedVertexMap    _GetEnforcedVerticesByCoords() const { return _coordsEnfVertexMap; }
314   const TGeomEntryHYBRIDEnforcedVertexMap _GetEnforcedVerticesByEntry() const { return _geomEntryEnfVertexMap; }
315   void ClearEnforcedVertices();
316
317   /*!
318    * To set enforced elements
319    */
320   bool SetEnforcedMesh(SMESH_Mesh& theMesh, SMESH::ElementType elementType, std::string name, std::string entry, std::string groupName = "");
321   bool SetEnforcedGroup(const SMESHDS_Mesh* theMeshDS, SMESH::long_array_var theIDs, SMESH::ElementType elementType, std::string name, std::string entry, std::string groupName = "");
322   bool SetEnforcedElements(TIDSortedElemSet theElemSet, SMESH::ElementType elementType, std::string groupName = "");
323   const THYBRIDEnforcedMeshList _GetEnforcedMeshes() const { return _enfMeshList; }
324   const TEntryHYBRIDEnforcedMeshListMap _GetEnforcedMeshesByEntry() const { return _entryEnfMeshMap; }
325   void ClearEnforcedMeshes();
326   const TIDSortedNodeGroupMap _GetEnforcedNodes() const { return _enfNodes; }
327   const TIDSortedElemGroupMap _GetEnforcedEdges() const { return _enfEdges; }
328   const TIDSortedElemGroupMap _GetEnforcedTriangles() const { return _enfTriangles; }
329   const TID2SizeMap _GetNodeIDToSizeMap() const {return _nodeIDToSizeMap; }
330   const TSetStrings _GetGroupsToRemove() const {return _groupsToRemove; }
331   void RestoreEnfElemsByMeshes(); // persistence
332   /*!
333    * \brief Return the enforced vertices
334    */
335   static THYBRIDEnforcedVertexList GetEnforcedVertices(const HYBRIDPlugin_Hypothesis* hyp);
336   static THYBRIDEnforcedVertexCoordsValues GetEnforcedVerticesCoordsSize(const HYBRIDPlugin_Hypothesis* hyp);
337   static THYBRIDEnforcedVertexEntryValues  GetEnforcedVerticesEntrySize(const HYBRIDPlugin_Hypothesis* hyp);
338   static TCoordsHYBRIDEnforcedVertexMap GetEnforcedVerticesByCoords(const HYBRIDPlugin_Hypothesis* hyp);
339   static TGeomEntryHYBRIDEnforcedVertexMap GetEnforcedVerticesByEntry(const HYBRIDPlugin_Hypothesis* hyp);
340   
341   static THYBRIDEnforcedMeshList GetEnforcedMeshes(const HYBRIDPlugin_Hypothesis* hyp);
342   static TEntryHYBRIDEnforcedMeshListMap GetEnforcedMeshesByEntry(const HYBRIDPlugin_Hypothesis* hyp);
343   static TIDSortedNodeGroupMap GetEnforcedNodes(const HYBRIDPlugin_Hypothesis* hyp);
344   static TIDSortedElemGroupMap GetEnforcedEdges(const HYBRIDPlugin_Hypothesis* hyp);
345   static TIDSortedElemGroupMap GetEnforcedTriangles(const HYBRIDPlugin_Hypothesis* hyp);
346   static TID2SizeMap GetNodeIDToSizeMap(const HYBRIDPlugin_Hypothesis* hyp);
347   static TSetStrings GetGroupsToRemove(const HYBRIDPlugin_Hypothesis* hyp);
348   static bool GetToMakeGroupsOfDomains(const HYBRIDPlugin_Hypothesis* hyp);
349   void ClearGroupsToRemove();
350   
351   static bool   DefaultMeshHoles();
352   static bool   DefaultLayersOnAllWrap();
353   static bool   DefaultToMakeGroupsOfDomains();
354   static double DefaultMaximumMemory();
355   static double DefaultInitialMemory();
356   static short  DefaultOptimizationLevel();
357   static short  DefaultCollisionMode();
358   static short  DefaultBoundaryLayersGrowth();
359   static short  DefaultElementGeneration();
360   static std::string DefaultWorkingDirectory();
361   static bool   DefaultKeepFiles();
362   static short  DefaultVerboseLevel();
363   static bool   DefaultToCreateNewNodes();
364   static bool   DefaultToUseBoundaryRecoveryVersion();
365   static bool   DefaultToUseFEMCorrection();
366   static bool   DefaultToRemoveCentralPoint();
367   static bool   DefaultStandardOutputLog();
368   static bool   DefaultRemoveLogOnSuccess();
369   static double DefaultGradation();
370   static bool   DefaultAddMultinormals();
371   static bool   DefaultSmoothNormals();
372   static short  DefaultNbOfBoundaryLayers();
373   static double DefaultHeightFirstLayer();
374   static double DefaultBoundaryLayersProgression();
375   static double DefaultMultinormalsAngle();
376     
377   static THYBRIDEnforcedVertex DefaultHYBRIDEnforcedVertex() {return THYBRIDEnforcedVertex();}
378   static THYBRIDEnforcedVertexList DefaultHYBRIDEnforcedVertexList() {return THYBRIDEnforcedVertexList();}
379   static THYBRIDEnforcedVertexCoordsValues DefaultHYBRIDEnforcedVertexCoordsValues() {return THYBRIDEnforcedVertexCoordsValues();}
380   static THYBRIDEnforcedVertexEntryValues DefaultHYBRIDEnforcedVertexEntryValues() {return THYBRIDEnforcedVertexEntryValues();}
381   static TCoordsHYBRIDEnforcedVertexMap DefaultCoordsHYBRIDEnforcedVertexMap() {return TCoordsHYBRIDEnforcedVertexMap();}
382   static TGeomEntryHYBRIDEnforcedVertexMap DefaultGeomEntryHYBRIDEnforcedVertexMap() {return TGeomEntryHYBRIDEnforcedVertexMap();}
383   static TGroupNameHYBRIDEnforcedVertexMap DefaultGroupNameHYBRIDEnforcedVertexMap() {return TGroupNameHYBRIDEnforcedVertexMap();}
384   
385   static THYBRIDEnforcedMesh        DefaultHYBRIDEnforcedMesh() {return THYBRIDEnforcedMesh();}
386   static THYBRIDEnforcedMeshList    DefaultHYBRIDEnforcedMeshList() {return THYBRIDEnforcedMeshList();}
387   static TEntryHYBRIDEnforcedMeshListMap DefaultEntryHYBRIDEnforcedMeshListMap() {return TEntryHYBRIDEnforcedMeshListMap();}
388   static TIDSortedNodeGroupMap      DefaultIDSortedNodeGroupMap() {return TIDSortedNodeGroupMap();}
389   static TIDSortedElemGroupMap      DefaultIDSortedElemGroupMap() {return TIDSortedElemGroupMap();}
390   static TID2SizeMap                DefaultID2SizeMap() {return TID2SizeMap();}
391   static TSetStrings                DefaultGroupsToRemove() {return TSetStrings();}
392   
393   // Persistence
394   virtual std::ostream & SaveTo(std::ostream & save);
395   virtual std::istream & LoadFrom(std::istream & load);
396   friend HYBRIDPLUGIN_EXPORT std::ostream & operator <<(std::ostream & save, HYBRIDPlugin_Hypothesis & hyp);
397   friend HYBRIDPLUGIN_EXPORT std::istream & operator >>(std::istream & load, HYBRIDPlugin_Hypothesis & hyp);
398
399   /*!
400    * \brief Does nothing
401    */
402   virtual bool SetParametersByMesh(const SMESH_Mesh* theMesh, const TopoDS_Shape& theShape);
403
404   /*!
405    * \brief Sets myToMakeGroupsOfDomains depending on whether theMesh is on shape or not
406    */
407   virtual bool SetParametersByDefaults(const TDefaults& dflts, const SMESH_Mesh* theMesh=0);
408
409 private:
410
411   bool   myToMeshHoles;
412   bool   myLayersOnAllWrap;
413   bool   myToMakeGroupsOfDomains;
414   double myMaximumMemory;
415   double myInitialMemory;
416   short  myOptimizationLevel;
417   short  myCollisionMode;
418   short  myBoundaryLayersGrowth;
419   short  myElementGeneration;
420   bool   myKeepFiles;
421   std::string myWorkingDirectory;
422   short  myVerboseLevel;
423   bool   myToCreateNewNodes;
424   bool   myToUseBoundaryRecoveryVersion;
425   bool   myToUseFemCorrection;
426   bool   myToRemoveCentralPoint;
427   bool   myLogInStandardOutput;
428   bool   myRemoveLogOnSuccess;
429   std::string myTextOption;
430   double myGradation;
431
432   bool    myAddMultinormals;
433   bool    mySmoothNormals;
434   double  myHeightFirstLayer;
435   double  myBoundaryLayersProgression;
436   double  myMultinormalsAngle;
437   short   myNbOfBoundaryLayers;
438    
439   THYBRIDEnforcedVertexList _enfVertexList;
440   THYBRIDEnforcedVertexCoordsValues _enfVertexCoordsSizeList;
441   THYBRIDEnforcedVertexEntryValues _enfVertexEntrySizeList;
442   // map to get "manual" enf vertex (through the coordinates)
443   TCoordsHYBRIDEnforcedVertexMap _coordsEnfVertexMap;
444   // map to get "geom" enf vertex (through the geom entries)
445   TGeomEntryHYBRIDEnforcedVertexMap _geomEntryEnfVertexMap;
446   
447   
448   THYBRIDEnforcedMeshList _enfMeshList;
449   // map to get enf meshes through the entries
450   TEntryHYBRIDEnforcedMeshListMap _entryEnfMeshMap;
451   TIDSortedNodeGroupMap _enfNodes;
452   TIDSortedElemGroupMap _enfEdges;
453   TIDSortedElemGroupMap _enfTriangles;
454   TID2SizeMap _nodeIDToSizeMap;
455   std::map<std::string, TIDSortedElemSet > _entryToElemsMap;
456   
457   TSetStrings _groupsToRemove;
458 };
459
460
461 #endif