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