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