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