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