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