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