Salome HOME
Changed for test bug PAL14022.
[modules/smesh.git] / src / SMESH / SMESH_MeshEditor.hxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  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 //
24 // File      : SMESH_MeshEditor.hxx
25 // Created   : Mon Apr 12 14:56:19 2004
26 // Author    : Edward AGAPOV (eap)
27 // Module    : SMESH
28
29
30 #ifndef SMESH_MeshEditor_HeaderFile
31 #define SMESH_MeshEditor_HeaderFile
32
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_Controls.hxx"
35 #include "SMESH_SequenceOfNode.hxx"
36 #include "SMESH_SequenceOfElemPtr.hxx"
37 #include "gp_Dir.hxx"
38 #include "TColStd_HSequenceOfReal.hxx"
39 #include "SMESH_MesherHelper.hxx"
40 #include "SMDS_MeshElement.hxx"
41
42 #include <list>
43 #include <map>
44
45 typedef map<const SMDS_MeshElement*,
46             list<const SMDS_MeshElement*> > TElemOfElemListMap;
47 typedef map<const SMDS_MeshNode*, const SMDS_MeshNode*> TNodeNodeMap;
48
49 typedef map<const SMDS_MeshNode*, SMESHDS_SubMesh*>           RemoveQuadNodeMap;
50 typedef map<const SMDS_MeshNode*, SMESHDS_SubMesh*>::iterator ItRemoveQuadNodeMap;
51
52 class SMDS_MeshFace;
53 class SMDS_MeshNode;
54 class gp_Ax1;
55 class gp_Vec;
56 class gp_Pnt;
57
58 class SMESH_MeshEditor {
59 public:
60
61   // define a set of elements sorted by ID, to be used to assure
62   // predictability of edition
63   struct TIDCompare {
64     bool operator () (const SMDS_MeshElement* e1, const SMDS_MeshElement* e2)
65     { return e1->GetID() < e2->GetID(); }
66   };
67   typedef set< const SMDS_MeshElement*, TIDCompare > TIDSortedElemSet;
68
69   /*!
70    * \brief Insert element in a map of elements sorted by ID
71     * \param elem - element to insert
72     * \param elemMap - the map to fill in
73    */
74   static void Insert(const SMDS_MeshElement*                 elem,
75                      std::map<int,const SMDS_MeshElement*> & elemMap) {
76     elemMap.insert( make_pair( elem->GetID(), elem ));
77   }
78   
79 public:
80
81   SMESH_MeshEditor( SMESH_Mesh* theMesh );
82
83   bool Remove (const std::list< int >& theElemIDs, const bool isNodes);
84   // Remove a node or an element.
85   // Modify a compute state of sub-meshes which become empty
86
87   bool InverseDiag (const SMDS_MeshElement * theTria1,
88                     const SMDS_MeshElement * theTria2 );
89   // Replace two neighbour triangles with ones built on the same 4 nodes
90   // but having other common link.
91   // Return False if args are improper
92
93   bool InverseDiag (const SMDS_MeshNode * theNode1,
94                     const SMDS_MeshNode * theNode2 );
95   // Replace two neighbour triangles sharing theNode1-theNode2 link
96   // with ones built on the same 4 nodes but having other common link.
97   // Return false if proper faces not found
98
99   bool DeleteDiag (const SMDS_MeshNode * theNode1,
100                    const SMDS_MeshNode * theNode2 );
101   // Replace two neighbour triangles sharing theNode1-theNode2 link
102   // with a quadrangle built on the same 4 nodes.
103   // Return false if proper faces not found
104
105   bool Reorient (const SMDS_MeshElement * theElement);
106   // Reverse theElement orientation
107
108
109   /*!
110    * \brief Fuse neighbour triangles into quadrangles.
111    * \param theElems     - The triangles to be fused.
112    * \param theCriterion - Is used to choose a neighbour to fuse with.
113    * \param theMaxAngle  - Is a max angle between element normals at which fusion
114    *                       is still performed; theMaxAngle is mesured in radians.
115    * \retval bool - Success or not.
116    */
117   bool TriToQuad (std::map<int,const SMDS_MeshElement*> & theElems,
118                   SMESH::Controls::NumericalFunctorPtr theCriterion,
119                   const double                         theMaxAngle);
120
121   /*!
122    * \brief Split quadrangles into triangles.
123    * \param theElems     - The faces to be splitted.
124    * \param theCriterion - Is used to choose a diagonal for splitting.
125    * \retval bool - Success or not.
126    */
127   bool QuadToTri (std::map<int,const SMDS_MeshElement*> &  theElems,
128                   SMESH::Controls::NumericalFunctorPtr theCriterion);
129
130   /*!
131    * \brief Split quadrangles into triangles.
132    * \param theElems  - The faces to be splitted.
133    * \param the13Diag - Is used to choose a diagonal for splitting.
134    * \retval bool - Success or not.
135    */
136   bool QuadToTri (std::map<int,const SMDS_MeshElement*> & theElems,
137                   const bool                          the13Diag);
138
139   /*!
140    * \brief Find better diagonal for splitting.
141    * \param theQuad      - The face to find better splitting of.
142    * \param theCriterion - Is used to choose a diagonal for splitting.
143    * \retval int - 1 for 1-3 diagonal, 2 for 2-4, -1 - for errors.
144    */
145   int BestSplit (const SMDS_MeshElement*              theQuad,
146                  SMESH::Controls::NumericalFunctorPtr theCriterion);
147
148
149   enum SmoothMethod { LAPLACIAN = 0, CENTROIDAL };
150
151   void Smooth (std::map<int,const SMDS_MeshElement*> & theElements,
152                std::set<const SMDS_MeshNode*> &    theFixedNodes,
153                const SmoothMethod                  theSmoothMethod,
154                const int                           theNbIterations,
155                double                              theTgtAspectRatio = 1.0,
156                const bool                          the2D = true);
157   // Smooth theElements using theSmoothMethod during theNbIterations
158   // or until a worst element has aspect ratio <= theTgtAspectRatio.
159   // Aspect Ratio varies in range [1.0, inf].
160   // If theElements is empty, the whole mesh is smoothed.
161   // theFixedNodes contains additionally fixed nodes. Nodes built
162   // on edges and boundary nodes are always fixed.
163   // If the2D, smoothing is performed using UV parameters of nodes
164   // on geometrical faces
165
166
167   void RotationSweep (std::map<int,const SMDS_MeshElement*> & theElements,
168                       const gp_Ax1&                       theAxis,
169                       const double                        theAngle,
170                       const int                           theNbSteps,
171                       const double                        theToler);
172   // Generate new elements by rotation of theElements around theAxis
173   // by theAngle by theNbSteps
174
175   /*!
176    * Auxilary flag for advanced extrusion.
177    * BOUNDARY: create or not boundary for result of extrusion
178    * SEW:      try to use existing nodes or create new nodes in any case
179    */
180   enum ExtrusionFlags {
181     EXTRUSION_FLAG_BOUNDARY = 0x01,
182     EXTRUSION_FLAG_SEW = 0x02
183   };
184   
185   /*!
186    * special structire for control of extrusion functionality
187    */
188   struct ExtrusParam {
189     gp_Dir myDir; // direction of extrusion
190     Handle(TColStd_HSequenceOfReal) mySteps; // magnitudes for each step
191     SMESH_SequenceOfNode myNodes; // nodes for using in sewing
192   };
193
194   /*!
195    * Create new node in the mesh with given coordinates
196    * (auxilary for advanced extrusion)
197    */
198   const SMDS_MeshNode* CreateNode(const double x,
199                                   const double y,
200                                   const double z,
201                                   const double tolnode,
202                                   SMESH_SequenceOfNode& aNodes);
203
204   /*!
205    * Generate new elements by extrusion of theElements
206    * It is a method used in .idl file. All functionality
207    * is implemented in the next method (see below) which
208    * is used in the cuurent method.
209    * param theElems - list of elements for extrusion
210    * param newElemsMap returns history of extrusion
211    * param theFlags set flags for performing extrusion (see description
212    *   of enum ExtrusionFlags for additional information)
213    * param theTolerance - uses for comparing locations of nodes if flag
214    *   EXTRUSION_FLAG_SEW is set
215    */
216   void ExtrusionSweep
217            (map<int,const SMDS_MeshElement*> & theElems,
218             const gp_Vec&                  theStep,
219             const int                      theNbSteps,
220             TElemOfElemListMap&            newElemsMap,
221             const int                      theFlags = EXTRUSION_FLAG_BOUNDARY,
222             const double                   theTolerance = 1.e-6);
223   
224   /*!
225    * Generate new elements by extrusion of theElements
226    * param theElems - list of elements for extrusion
227    * param newElemsMap returns history of extrusion
228    * param theFlags set flags for performing extrusion (see description
229    *   of enum ExtrusionFlags for additional information)
230    * param theTolerance - uses for comparing locations of nodes if flag
231    *   EXTRUSION_FLAG_SEW is set
232    * param theParams - special structure for manage of extrusion
233    */
234   void ExtrusionSweep (map<int,const SMDS_MeshElement*> & theElems,
235                        ExtrusParam&                   theParams,
236                        TElemOfElemListMap&            newElemsMap,
237                        const int                      theFlags,
238                        const double                   theTolerance);
239
240
241   // Generate new elements by extrusion of theElements 
242   // by theStep by theNbSteps
243
244   enum Extrusion_Error {
245     EXTR_OK,
246     EXTR_NO_ELEMENTS, 
247     EXTR_PATH_NOT_EDGE,
248     EXTR_BAD_PATH_SHAPE,
249     EXTR_BAD_STARTING_NODE,
250     EXTR_BAD_ANGLES_NUMBER,
251     EXTR_CANT_GET_TANGENT
252     };
253   
254   Extrusion_Error ExtrusionAlongTrack (std::map<int,const SMDS_MeshElement*> & theElements,
255                                        SMESH_subMesh*                      theTrackPattern,
256                                        const SMDS_MeshNode*                theNodeStart,
257                                        const bool                          theHasAngles,
258                                        std::list<double>&                  theAngles,
259                                        const bool                          theHasRefPoint,
260                                        const gp_Pnt&                       theRefPoint);
261   // Generate new elements by extrusion of theElements along path given by theTrackPattern,
262   // theHasAngles are the rotation angles, base point can be given by theRefPoint
263
264   void Transform (std::map<int,const SMDS_MeshElement*> & theElements,
265                   const gp_Trsf&                      theTrsf,
266                   const bool                          theCopy);
267   // Move or copy theElements applying theTrsf to their nodes
268
269   typedef std::list< std::list< const SMDS_MeshNode* > > TListOfListOfNodes;
270
271   void FindCoincidentNodes (std::set<const SMDS_MeshNode*> & theNodes,
272                             const double                     theTolerance,
273                             TListOfListOfNodes &             theGroupsOfNodes);
274   // Return list of group of nodes close to each other within theTolerance.
275   // Search among theNodes or in the whole mesh if theNodes is empty.
276
277   int SimplifyFace (const vector<const SMDS_MeshNode *> faceNodes,
278                     vector<const SMDS_MeshNode *>&      poly_nodes,
279                     vector<int>&                        quantities) const;
280   // Split face, defined by <faceNodes>, into several faces by repeating nodes.
281   // Is used by MergeNodes()
282
283   void MergeNodes (TListOfListOfNodes & theNodeGroups);
284   // In each group, the cdr of nodes are substituted by the first one
285   // in all elements.
286
287   void MergeEqualElements();
288   // Remove all but one of elements built on the same nodes.
289   // Return nb of successfully merged groups.
290
291   static bool CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
292                                    const SMDS_MeshNode* theNode2,
293                                    const SMDS_MeshNode* theNode3 = 0);
294   // Return true if the three nodes are on a free border
295
296   static bool FindFreeBorder (const SMDS_MeshNode*                  theFirstNode,
297                               const SMDS_MeshNode*                  theSecondNode,
298                               const SMDS_MeshNode*                  theLastNode,
299                               std::list< const SMDS_MeshNode* > &   theNodes,
300                               std::list< const SMDS_MeshElement* >& theFaces);
301   // Return nodes and faces of a free border if found 
302
303   enum Sew_Error {
304     SEW_OK,
305     // for SewFreeBorder()
306     SEW_BORDER1_NOT_FOUND,
307     SEW_BORDER2_NOT_FOUND,
308     SEW_BOTH_BORDERS_NOT_FOUND,
309     SEW_BAD_SIDE_NODES,
310     SEW_VOLUMES_TO_SPLIT,
311     // for SewSideElements()
312     SEW_DIFF_NB_OF_ELEMENTS,
313     SEW_TOPO_DIFF_SETS_OF_ELEMENTS,
314     SEW_BAD_SIDE1_NODES,
315     SEW_BAD_SIDE2_NODES,
316     SEW_INTERNAL_ERROR
317     };
318     
319
320   Sew_Error SewFreeBorder (const SMDS_MeshNode* theBorderFirstNode,
321                            const SMDS_MeshNode* theBorderSecondNode,
322                            const SMDS_MeshNode* theBorderLastNode,
323                            const SMDS_MeshNode* theSide2FirstNode,
324                            const SMDS_MeshNode* theSide2SecondNode,
325                            const SMDS_MeshNode* theSide2ThirdNode = 0,
326                            const bool           theSide2IsFreeBorder = true,
327                            const bool           toCreatePolygons = false,
328                            const bool           toCreatePolyedrs = false);
329   // Sew the free border to the side2 by replacing nodes in
330   // elements on the free border with nodes of the elements
331   // of the side 2. If nb of links in the free border and
332   // between theSide2FirstNode and theSide2LastNode are different,
333   // additional nodes are inserted on a link provided that no
334   // volume elements share the splitted link.
335   // The side 2 is a free border if theSide2IsFreeBorder == true.
336   // Sewing is peformed between the given first, second and last
337   // nodes on the sides.
338   // theBorderFirstNode is merged with theSide2FirstNode.
339   // if (!theSide2IsFreeBorder) then theSide2SecondNode gives
340   // the last node on the side 2, which will be merged with
341   // theBorderLastNode.
342   // if (theSide2IsFreeBorder) then theSide2SecondNode will
343   // be merged with theBorderSecondNode.
344   // if (theSide2IsFreeBorder && theSide2ThirdNode == 0) then
345   // the 2 free borders are sewn link by link and no additional
346   // nodes are inserted.
347   // Return false, if sewing failed.
348
349   Sew_Error SewSideElements (std::map<int,const SMDS_MeshElement*>& theSide1,
350                              std::map<int,const SMDS_MeshElement*>& theSide2,
351                              const SMDS_MeshNode*               theFirstNode1ToMerge,
352                              const SMDS_MeshNode*               theFirstNode2ToMerge,
353                              const SMDS_MeshNode*               theSecondNode1ToMerge,
354                              const SMDS_MeshNode*               theSecondNode2ToMerge);
355   // Sew two sides of a mesh. Nodes belonging to theSide1 are
356   // merged with nodes of elements of theSide2.
357   // Number of elements in theSide1 and in theSide2 must be
358   // equal and they should have similar node connectivity.
359   // The nodes to merge should belong to side s borders and
360   // the first node should be linked to the second.
361
362   void InsertNodesIntoLink(const SMDS_MeshElement*          theFace,
363                            const SMDS_MeshNode*             theBetweenNode1,
364                            const SMDS_MeshNode*             theBetweenNode2,
365                            std::list<const SMDS_MeshNode*>& theNodesToInsert,
366                            const bool                       toCreatePoly = false);
367   // insert theNodesToInsert into theFace between theBetweenNode1 and theBetweenNode2.
368   // If toCreatePoly is true, replace theFace by polygon, else split theFace.
369
370   void UpdateVolumes (const SMDS_MeshNode*             theBetweenNode1,
371                       const SMDS_MeshNode*             theBetweenNode2,
372                       std::list<const SMDS_MeshNode*>& theNodesToInsert);
373   // insert theNodesToInsert into all volumes, containing link
374   // theBetweenNode1 - theBetweenNode2, between theBetweenNode1 and theBetweenNode2.
375
376   void ConvertToQuadratic(const bool theForce3d);
377   //converts all mesh to quadratic one, deletes old elements, replacing 
378   //them with quadratic ones with the same id.
379
380   bool ConvertFromQuadratic();
381   //converts all mesh from quadratic to ordinary ones, deletes old quadratic elements, replacing 
382   //them with ordinary mesh elements with the same id.
383
384
385 //  static int SortQuadNodes (const SMDS_Mesh * theMesh,
386 //                            int               theNodeIds[] );
387 //  // Set 4 nodes of a quadrangle face in a good order.
388 //  // Swap 1<->2 or 2<->3 nodes and correspondingly return
389 //  // 1 or 2 else 0.
390 //
391 //  static bool SortHexaNodes (const SMDS_Mesh * theMesh,
392 //                             int               theNodeIds[] );
393 //  // Set 8 nodes of a hexahedron in a good order.
394 //  // Return success status
395
396   static void AddToSameGroups (const SMDS_MeshElement* elemToAdd,
397                                const SMDS_MeshElement* elemInGroups,
398                                SMESHDS_Mesh *          aMesh);
399   // Add elemToAdd to the groups the elemInGroups belongs to
400
401   static void RemoveElemFromGroups (const SMDS_MeshElement* removeelem,
402                                     SMESHDS_Mesh *          aMesh);
403   // remove elemToAdd from the groups 
404
405   static const SMDS_MeshElement*
406     FindFaceInSet(const SMDS_MeshNode*                     n1,
407                   const SMDS_MeshNode*                     n2,
408                   const std::map<int,const SMDS_MeshElement*>& elemSet,
409                   const std::map<int,const SMDS_MeshElement*>& avoidSet);
410   // Return a face having linked nodes n1 and n2 and which is
411   // - not in avoidSet,
412   // - in elemSet provided that !elemSet.empty()
413
414   /*!
415    * \brief Find corresponding nodes in two sets of faces 
416     * \param theSide1 - first face set
417     * \param theSide2 - second first face
418     * \param theFirstNode1 - a boundary node of set 1
419     * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
420     * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
421     * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
422     * \param nReplaceMap - output map of corresponding nodes
423     * \retval Sew_Error  - is a success or not
424    */
425   static Sew_Error FindMatchingNodes(set<const SMDS_MeshElement*>& theSide1,
426                                      set<const SMDS_MeshElement*>& theSide2,
427                                      const SMDS_MeshNode*          theFirstNode1,
428                                      const SMDS_MeshNode*          theFirstNode2,
429                                      const SMDS_MeshNode*          theSecondNode1,
430                                      const SMDS_MeshNode*          theSecondNode2,
431                                      TNodeNodeMap &                nReplaceMap);
432
433   /*!
434    * \brief Returns true if given node is medium
435     * \param n - node to check
436     * \param typeToCheck - type of elements containing the node to ask about node status
437     * \retval bool - check result
438    */
439   static bool IsMedium(const SMDS_MeshNode*      node,
440                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
441
442   int FindShape (const SMDS_MeshElement * theElem);
443   // Return an index of the shape theElem is on
444   // or zero if a shape not found
445
446   SMESH_Mesh * GetMesh() { return myMesh; }
447
448   SMESHDS_Mesh * GetMeshDS() { return myMesh->GetMeshDS(); }
449
450   SMESH_SequenceOfElemPtr GetLastCreatedNodes() { return myLastCreatedNodes; }
451
452   SMESH_SequenceOfElemPtr GetLastCreatedElems() { return myLastCreatedElems; }
453
454 private:
455
456   void ConvertElemToQuadratic(SMESHDS_SubMesh *theSm,
457                               SMESH_MesherHelper* theHelper,
458                               const bool theForce3d);
459   //Auxiliary function for "ConvertToQuadratic" is intended to convert
460   //elements contained in submesh to quadratic
461
462   void RemoveQuadElem( SMESHDS_SubMesh *theSm,
463                        SMDS_ElemIteratorPtr theItr,
464                        RemoveQuadNodeMap& theRemoveNodeMap);
465   //Auxiliary function for "ConvertFromQuadratic" is intended to convert quadratic
466   //element to ordinary and for removing quadratic nodes
467
468 private:
469
470   SMESH_Mesh * myMesh;
471
472   /*!
473    * Sequence for keeping nodes created during last operation
474    */
475   SMESH_SequenceOfElemPtr myLastCreatedNodes;
476
477   /*!
478    * Sequence for keeping elements created during last operation
479    */
480   SMESH_SequenceOfElemPtr myLastCreatedElems;
481
482 };
483
484 #endif