Salome HOME
e251e4f9c36952e26bbe456333bfe3016ed6c762
[modules/smesh.git] / src / SMESH / SMESH_MeshEditor.hxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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 // File      : SMESH_MeshEditor.hxx
24 // Created   : Mon Apr 12 14:56:19 2004
25 // Author    : Edward AGAPOV (eap)
26 // Module    : SMESH
27 //
28 #ifndef SMESH_MeshEditor_HeaderFile
29 #define SMESH_MeshEditor_HeaderFile
30
31 #include "SMESH_SMESH.hxx"
32
33 #include "SMESH_Controls.hxx"
34 #include "SMESH_TypeDefs.hxx"
35 #include "SMESH_ComputeError.hxx"
36
37 #include <utilities.h>
38
39 #include <TColStd_HSequenceOfReal.hxx>
40 #include <gp_Dir.hxx>
41
42 #include <list>
43 #include <map>
44 #include <set>
45
46 class SMDS_MeshElement;
47 class SMDS_MeshFace;
48 class SMDS_MeshNode;
49 class SMESHDS_Mesh;
50 class SMESHDS_SubMesh;
51 class SMESH_Group;
52 class SMESH_Mesh;
53 class SMESH_MesherHelper;
54 class SMESH_NodeSearcher;
55 class SMESH_subMesh;
56 class TopoDS_Edge;
57 class TopoDS_Shape;
58 class TopoDS_Vertex;
59 class gp_Ax1;
60 class gp_Pnt;
61 class gp_Vec;
62
63 // ============================================================
64 /*!
65  * \brief Editor of a mesh
66  */
67 // ============================================================
68
69 class SMESH_EXPORT SMESH_MeshEditor
70 {
71 public:
72
73   SMESH_MeshEditor( SMESH_Mesh* theMesh );
74
75   SMESH_Mesh   *                 GetMesh() { return myMesh; }
76   SMESHDS_Mesh *                 GetMeshDS();
77
78   const SMESH_SequenceOfElemPtr& GetLastCreatedNodes() const { return myLastCreatedNodes; }
79   const SMESH_SequenceOfElemPtr& GetLastCreatedElems() const { return myLastCreatedElems; }
80   void                           ClearLastCreated();
81   SMESH_ComputeErrorPtr &        GetError() { return myError; }
82
83   // --------------------------------------------------------------------------------
84   struct ElemFeatures //!< Features of element to create
85   {
86     SMDSAbs_ElementType myType;
87     bool                myIsPoly, myIsQuad;
88     int                 myID;
89     double              myBallDiameter;
90     std::vector<int>    myPolyhedQuantities;
91
92     SMESH_EXPORT ElemFeatures( SMDSAbs_ElementType type=SMDSAbs_All, bool isPoly=false, bool isQuad=false )
93       :myType( type ), myIsPoly(isPoly), myIsQuad(isQuad), myID(-1), myBallDiameter(0) {}
94
95     SMESH_EXPORT ElemFeatures& Init( SMDSAbs_ElementType type, bool isPoly=false, bool isQuad=false )
96     { myType = type; myIsPoly = isPoly; myIsQuad = isQuad; return *this; }
97
98     SMESH_EXPORT ElemFeatures& Init( const SMDS_MeshElement* elem, bool basicOnly=true );
99
100     SMESH_EXPORT ElemFeatures& Init( double diameter )
101     { myType = SMDSAbs_Ball; myBallDiameter = diameter; return *this; }
102
103     SMESH_EXPORT ElemFeatures& Init( std::vector<int>& quanities, bool isQuad=false )
104     { myType = SMDSAbs_Volume; myIsPoly = 1; myIsQuad = isQuad;
105       myPolyhedQuantities.swap( quanities ); return *this; }
106
107     SMESH_EXPORT ElemFeatures& Init( const std::vector<int>& quanities, bool isQuad=false )
108     { myType = SMDSAbs_Volume; myIsPoly = 1; myIsQuad = isQuad;
109       myPolyhedQuantities = quanities; return *this; }
110
111     SMESH_EXPORT ElemFeatures& SetPoly(bool isPoly) { myIsPoly = isPoly; return *this; }
112     SMESH_EXPORT ElemFeatures& SetQuad(bool isQuad) { myIsQuad = isQuad; return *this; }
113     SMESH_EXPORT ElemFeatures& SetID  (int ID)      { myID = ID; return *this; }
114   };
115
116   /*!
117    * \brief Add element
118    */
119   SMDS_MeshElement* AddElement(const std::vector<const SMDS_MeshNode*> & nodes,
120                                const ElemFeatures&                       features);
121   /*!
122    * \brief Add element
123    */
124   SMDS_MeshElement* AddElement(const std::vector<int> & nodeIDs,
125                                const ElemFeatures&      features);
126
127   int Remove (const std::list< int >& theElemIDs, const bool isNodes);
128   // Remove a node or an element.
129   // Modify a compute state of sub-meshes which become empty
130
131   void Create0DElementsOnAllNodes( const TIDSortedElemSet& elements,
132                                    TIDSortedElemSet&       all0DElems,
133                                    const bool              duplicateElements);
134   // Create 0D elements on all nodes of the given. \a all0DElems returns
135   // all 0D elements found or created on nodes of \a elements
136
137   bool InverseDiag (const SMDS_MeshElement * theTria1,
138                     const SMDS_MeshElement * theTria2 );
139   // Replace two neighbour triangles with ones built on the same 4 nodes
140   // but having other common link.
141   // Return False if args are improper
142
143   bool InverseDiag (const SMDS_MeshNode * theNode1,
144                     const SMDS_MeshNode * theNode2 );
145   // Replace two neighbour triangles sharing theNode1-theNode2 link
146   // with ones built on the same 4 nodes but having other common link.
147   // Return false if proper faces not found
148
149   bool DeleteDiag (const SMDS_MeshNode * theNode1,
150                    const SMDS_MeshNode * theNode2 );
151   // Replace two neighbour triangles sharing theNode1-theNode2 link
152   // with a quadrangle built on the same 4 nodes.
153   // Return false if proper faces not found
154
155   bool Reorient (const SMDS_MeshElement * theElement);
156   // Reverse theElement orientation
157
158   int Reorient2D (TIDSortedElemSet &       theFaces,
159                   const gp_Dir&            theDirection,
160                   const SMDS_MeshElement * theFace);
161   // Reverse theFaces whose orientation to be same as that of theFace
162   // oriented according to theDirection. Return nb of reoriented faces
163
164   int Reorient2DBy3D (TIDSortedElemSet & theFaces,
165                       TIDSortedElemSet & theVolumes,
166                       const bool         theOutsideNormal);
167   // Reorient faces basing on orientation of adjacent volumes.
168   // Return nb of reoriented faces
169
170   /*!
171    * \brief Fuse neighbour triangles into quadrangles.
172    * \param theElems     - The triangles to be fused.
173    * \param theCriterion - Is used to choose a neighbour to fuse with.
174    * \param theMaxAngle  - Is a max angle between element normals at which fusion
175    *                       is still performed; theMaxAngle is mesured in radians.
176    * \return bool - Success or not.
177    */
178   bool TriToQuad (TIDSortedElemSet &                   theElems,
179                   SMESH::Controls::NumericalFunctorPtr theCriterion,
180                   const double                         theMaxAngle);
181   /*!
182    * \brief Split quadrangles into triangles.
183    * \param theElems     - The faces to be splitted.
184    * \param theCriterion - Is used to choose a diagonal for splitting.
185    * \return bool - Success or not.
186    */
187   bool QuadToTri (TIDSortedElemSet &                   theElems,
188                   SMESH::Controls::NumericalFunctorPtr theCriterion);
189   /*!
190    * \brief Split quadrangles into triangles.
191    * \param theElems  - The faces to be splitted.
192    * \param the13Diag - Is used to choose a diagonal for splitting.
193    * \return bool - Success or not.
194    */
195   bool QuadToTri (TIDSortedElemSet & theElems,
196                   const bool         the13Diag);
197   /*!
198    * \brief Split each of given quadrangles into 4 triangles.
199    * \param theElems - The faces to be splitted. If empty all faces are split.
200    */
201   void QuadTo4Tri (TIDSortedElemSet & theElems);
202
203   /*!
204    * \brief Find better diagonal for splitting.
205    * \param theQuad      - The face to find better splitting of.
206    * \param theCriterion - Is used to choose a diagonal for splitting.
207    * \return int - 1 for 1-3 diagonal, 2 for 2-4, -1 - for errors.
208    */
209   int BestSplit (const SMDS_MeshElement*              theQuad,
210                  SMESH::Controls::NumericalFunctorPtr theCriterion);
211
212
213   typedef std::map < const SMDS_MeshElement*, int, TIDCompare > TFacetOfElem;
214
215     //!<2nd arg of SplitVolumes()
216   enum SplitVolumToTetraFlags { HEXA_TO_5 = 1, // split into tetrahedra
217                                 HEXA_TO_6,
218                                 HEXA_TO_24,
219                                 HEXA_TO_2_PRISMS, // split into prisms
220                                 HEXA_TO_4_PRISMS };
221   /*!
222    * \brief Split volumic elements into tetrahedra or prisms.
223    *        If facet ID < 0, element is split into tetrahedra,
224    *        else a hexahedron is split into prisms so that the given facet is
225    *        split into triangles
226    */
227   void SplitVolumes (const TFacetOfElem & theElems, const int theMethodFlags);
228
229   /*!
230    * \brief For hexahedra that will be split into prisms, finds facets to
231    *        split into triangles
232    *  \param [in,out] theHexas - the hexahedra
233    *  \param [in]     theFacetNormal - facet normal
234    *  \param [out]    theFacets - the hexahedra and found facet IDs
235    */
236   void GetHexaFacetsToSplit( TIDSortedElemSet& theHexas,
237                              const gp_Ax1&     theFacetNormal,
238                              TFacetOfElem &    theFacets);
239
240   /*!
241    * \brief Split bi-quadratic elements into linear ones without creation of additional nodes
242    *   - bi-quadratic triangle will be split into 3 linear quadrangles;
243    *   - bi-quadratic quadrangle will be split into 4 linear quadrangles;
244    *   - tri-quadratic hexahedron will be split into 8 linear hexahedra;
245    *   Quadratic elements of lower dimension  adjacent to the split bi-quadratic element
246    *   will be split in order to keep the mesh conformal.
247    *  \param elems - elements to split
248    */
249   void SplitBiQuadraticIntoLinear(TIDSortedElemSet& theElems);
250
251   enum SmoothMethod { LAPLACIAN = 0, CENTROIDAL };
252
253   void Smooth (TIDSortedElemSet &               theElements,
254                std::set<const SMDS_MeshNode*> & theFixedNodes,
255                const SmoothMethod               theSmoothMethod,
256                const int                        theNbIterations,
257                double                           theTgtAspectRatio = 1.0,
258                const bool                       the2D = true);
259   // Smooth theElements using theSmoothMethod during theNbIterations
260   // or until a worst element has aspect ratio <= theTgtAspectRatio.
261   // Aspect Ratio varies in range [1.0, inf].
262   // If theElements is empty, the whole mesh is smoothed.
263   // theFixedNodes contains additionally fixed nodes. Nodes built
264   // on edges and boundary nodes are always fixed.
265   // If the2D, smoothing is performed using UV parameters of nodes
266   // on geometrical faces
267
268   typedef TIDTypeCompare TElemSort;
269   typedef std::map < const SMDS_MeshElement*,
270     std::list<const SMDS_MeshElement*>, TElemSort >                        TTElemOfElemListMap;
271   typedef std::map<const SMDS_MeshNode*, std::list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
272   typedef TNodeOfNodeListMap::iterator                                     TNodeOfNodeListMapItr;
273   typedef std::vector<TNodeOfNodeListMapItr>                               TVecOfNnlmiMap;
274   typedef std::map<const SMDS_MeshElement*, TVecOfNnlmiMap, TElemSort >    TElemOfVecOfNnlmiMap;
275   typedef std::auto_ptr< std::list<int> > PGroupIDs;
276
277   PGroupIDs RotationSweep (TIDSortedElemSet   theElements[2],
278                            const gp_Ax1&      theAxis,
279                            const double       theAngle,
280                            const int          theNbSteps,
281                            const double       theToler,
282                            const bool         theMakeGroups,
283                            const bool         theMakeWalls=true);
284   // Generate new elements by rotation of theElements around theAxis
285   // by theAngle by theNbSteps
286
287   /*!
288    * Flags of extrusion.
289    * BOUNDARY: create or not boundary for result of extrusion
290    * SEW:      try to use existing nodes or create new nodes in any case
291    * GROUPS:   to create groups
292    * BY_AVG_NORMAL: step size is measured along average normal to elements,
293    *                else step size is measured along average normal of any element
294    * USE_INPUT_ELEMS_ONLY: to use only input elements to compute extrusion direction
295    *                       for ExtrusionByNormal()
296    * SCALE_LINEAR_VARIATION: to make linear variation of scale factors
297    */
298   enum ExtrusionFlags {
299     EXTRUSION_FLAG_BOUNDARY = 0x01,
300     EXTRUSION_FLAG_SEW = 0x02,
301     EXTRUSION_FLAG_GROUPS = 0x04,
302     EXTRUSION_FLAG_BY_AVG_NORMAL = 0x08,
303     EXTRUSION_FLAG_USE_INPUT_ELEMS_ONLY = 0x10,
304     EXTRUSION_FLAG_SCALE_LINEAR_VARIATION = 0x20
305   };
306
307   /*!
308    * Generator of nodes for extrusion functionality
309    */
310   class SMESH_EXPORT ExtrusParam
311   {
312     gp_Dir                          myDir;   // direction of extrusion
313     Handle(TColStd_HSequenceOfReal) mySteps; // magnitudes for each step
314     std::vector<double>             myScales, myMediumScales;// scale factors
315     gp_XYZ                          myBaseP; // scaling center
316     SMESH_SequenceOfNode            myNodes; // nodes for using in sewing
317     int                             myFlags; // see ExtrusionFlags
318     double                          myTolerance; // tolerance for sewing nodes
319     const TIDSortedElemSet*         myElemsToUse; // elements to use for extrusion by normal
320
321     int (ExtrusParam::*myMakeNodesFun)(SMESHDS_Mesh*                     mesh,
322                                        const SMDS_MeshNode*              srcNode,
323                                        std::list<const SMDS_MeshNode*> & newNodes,
324                                        const bool                        makeMediumNodes);
325
326   public:
327     ExtrusParam( const gp_Vec&                   theStep,
328                  const int                       theNbSteps,
329                  const std::list<double>&        theScales,
330                  const gp_XYZ*                   theBaseP,
331                  const int                       theFlags = 0,
332                  const double                    theTolerance = 1e-6);
333     ExtrusParam( const gp_Dir&                   theDir,
334                  Handle(TColStd_HSequenceOfReal) theSteps,
335                  const int                       theFlags = 0,
336                  const double                    theTolerance = 1e-6);
337     ExtrusParam( const double                    theStep,
338                  const int                       theNbSteps,
339                  const int                       theFlags,
340                  const int                       theDim); // for extrusion by normal
341
342     SMESH_SequenceOfNode& ChangeNodes() { return myNodes; }
343     int& Flags()                   { return myFlags; }
344     bool ToMakeBoundary()    const { return myFlags & EXTRUSION_FLAG_BOUNDARY; }
345     bool ToMakeGroups()      const { return myFlags & EXTRUSION_FLAG_GROUPS; }
346     bool ToUseInpElemsOnly() const { return myFlags & EXTRUSION_FLAG_USE_INPUT_ELEMS_ONLY; }
347     bool IsLinearVariation() const { return myFlags & EXTRUSION_FLAG_SCALE_LINEAR_VARIATION; }
348     int  NbSteps()           const { return mySteps->Length(); }
349
350     // stores elements to use for extrusion by normal, depending on
351     // state of EXTRUSION_FLAG_USE_INPUT_ELEMS_ONLY flag;
352     // define myBaseP for scaling
353     void SetElementsToUse( const TIDSortedElemSet& elems, const TIDSortedElemSet& nodes );
354
355     // creates nodes and returns number of nodes added in \a newNodes
356     int MakeNodes( SMESHDS_Mesh*                     mesh,
357                    const SMDS_MeshNode*              srcNode,
358                    std::list<const SMDS_MeshNode*> & newNodes,
359                    const bool                        makeMediumNodes)
360     {
361       return (this->*myMakeNodesFun)( mesh, srcNode, newNodes, makeMediumNodes );
362     }
363   private:
364
365     int makeNodesByDir( SMESHDS_Mesh*                     mesh,
366                         const SMDS_MeshNode*              srcNode,
367                         std::list<const SMDS_MeshNode*> & newNodes,
368                         const bool                        makeMediumNodes);
369     int makeNodesByDirAndSew( SMESHDS_Mesh*                     mesh,
370                               const SMDS_MeshNode*              srcNode,
371                               std::list<const SMDS_MeshNode*> & newNodes,
372                               const bool                        makeMediumNodes);
373     int makeNodesByNormal2D( SMESHDS_Mesh*                     mesh,
374                              const SMDS_MeshNode*              srcNode,
375                              std::list<const SMDS_MeshNode*> & newNodes,
376                              const bool                        makeMediumNodes);
377     int makeNodesByNormal1D( SMESHDS_Mesh*                     mesh,
378                              const SMDS_MeshNode*              srcNode,
379                              std::list<const SMDS_MeshNode*> & newNodes,
380                              const bool                        makeMediumNodes);
381     // step iteration
382     void   beginStepIter( bool withMediumNodes );
383     bool   moreSteps();
384     double nextStep();
385     std::vector< double > myCurSteps;
386     bool                  myWithMediumNodes;
387     int                   myNextStep;
388   };
389
390   /*!
391    * Generate new elements by extrusion of theElements
392    * It is a method used in .idl file. All functionality
393    * is implemented in the next method (see below) which
394    * is used in the current method.
395    * @param theElems - list of elements for extrusion
396    * @param newElemsMap returns history of extrusion
397    * @param theFlags set flags for performing extrusion (see description
398    *   of enum ExtrusionFlags for additional information)
399    * @param theTolerance - uses for comparing locations of nodes if flag
400    *   EXTRUSION_FLAG_SEW is set
401    */
402   PGroupIDs ExtrusionSweep (TIDSortedElemSet     theElems[2],
403                             const gp_Vec&        theStep,
404                             const int            theNbSteps,
405                             TTElemOfElemListMap& newElemsMap,
406                             const int            theFlags,
407                             const double         theTolerance = 1.e-6);
408   
409   /*!
410    * Generate new elements by extrusion of theElements
411    * @param theElems - list of elements for extrusion
412    * @param newElemsMap returns history of extrusion
413    * @param theFlags set flags for performing extrusion (see description
414    *   of enum ExtrusionFlags for additional information)
415    * @param theTolerance - uses for comparing locations of nodes if flag
416    *   EXTRUSION_FLAG_SEW is set
417    * @param theParams - special structure for manage of extrusion
418    */
419   PGroupIDs ExtrusionSweep (TIDSortedElemSet     theElems[2],
420                             ExtrusParam&         theParams,
421                             TTElemOfElemListMap& newElemsMap);
422
423
424   // Generate new elements by extrusion of theElements 
425   // by theStep by theNbSteps
426
427   enum Extrusion_Error {
428     EXTR_OK,
429     EXTR_NO_ELEMENTS, 
430     EXTR_PATH_NOT_EDGE,
431     EXTR_BAD_PATH_SHAPE,
432     EXTR_BAD_STARTING_NODE,
433     EXTR_BAD_ANGLES_NUMBER,
434     EXTR_CANT_GET_TANGENT
435     };
436   
437   Extrusion_Error ExtrusionAlongTrack (TIDSortedElemSet     theElements[2],
438                                        SMESH_subMesh*       theTrackPattern,
439                                        const SMDS_MeshNode* theNodeStart,
440                                        const bool           theHasAngles,
441                                        std::list<double>&   theAngles,
442                                        const bool           theLinearVariation,
443                                        const bool           theHasRefPoint,
444                                        const gp_Pnt&        theRefPoint,
445                                        const bool           theMakeGroups);
446   Extrusion_Error ExtrusionAlongTrack (TIDSortedElemSet     theElements[2],
447                                        SMESH_Mesh*          theTrackPattern,
448                                        const SMDS_MeshNode* theNodeStart,
449                                        const bool           theHasAngles,
450                                        std::list<double>&   theAngles,
451                                        const bool           theLinearVariation,
452                                        const bool           theHasRefPoint,
453                                        const gp_Pnt&        theRefPoint,
454                                        const bool           theMakeGroups);
455   // Generate new elements by extrusion of theElements along path given by theTrackPattern,
456   // theHasAngles are the rotation angles, base point can be given by theRefPoint
457
458   PGroupIDs Transform (TIDSortedElemSet & theElements,
459                        const gp_Trsf&     theTrsf,
460                        const bool         theCopy,
461                        const bool         theMakeGroups,
462                        SMESH_Mesh*        theTargetMesh=0);
463   // Move or copy theElements applying theTrsf to their nodes
464
465   typedef std::list< std::list< const SMDS_MeshNode* > > TListOfListOfNodes;
466
467   void FindCoincidentNodes (TIDSortedNodeSet &   theNodes,
468                             const double         theTolerance,
469                             TListOfListOfNodes & theGroupsOfNodes,
470                             bool                 theSeparateCornersAndMedium);
471   // Return list of group of nodes close to each other within theTolerance.
472   // Search among theNodes or in the whole mesh if theNodes is empty.
473
474   void MergeNodes (TListOfListOfNodes & theNodeGroups,
475                    const bool           theAvoidMakingHoles = false);
476   // In each group, the cdr of nodes are substituted by the first one
477   // in all elements.
478
479   typedef std::list< std::list< int > > TListOfListOfElementsID;
480
481   void FindEqualElements(TIDSortedElemSet &        theElements,
482                          TListOfListOfElementsID & theGroupsOfElementsID);
483   // Return list of group of elements build on the same nodes.
484   // Search among theElements or in the whole mesh if theElements is empty.
485
486   void MergeElements(TListOfListOfElementsID & theGroupsOfElementsID);
487   // In each group remove all but first of elements.
488
489   void MergeEqualElements();
490   // Remove all but one of elements built on the same nodes.
491   // Return nb of successfully merged groups.
492
493   int SimplifyFace (const std::vector<const SMDS_MeshNode *>& faceNodes,
494                     std::vector<const SMDS_MeshNode *>&       poly_nodes,
495                     std::vector<int>&                         quantities) const;
496   // Split face, defined by <faceNodes>, into several faces by repeating nodes.
497   // Is used by MergeNodes()
498
499   static bool CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
500                                    const SMDS_MeshNode* theNode2,
501                                    const SMDS_MeshNode* theNode3 = 0);
502   // Return true if the three nodes are on a free border
503
504   static bool FindFreeBorder (const SMDS_MeshNode*                  theFirstNode,
505                               const SMDS_MeshNode*                  theSecondNode,
506                               const SMDS_MeshNode*                  theLastNode,
507                               std::list< const SMDS_MeshNode* > &   theNodes,
508                               std::list< const SMDS_MeshElement* >& theFaces);
509   // Return nodes and faces of a free border if found 
510
511   enum Sew_Error {
512     SEW_OK,
513     // for SewFreeBorder()
514     SEW_BORDER1_NOT_FOUND,
515     SEW_BORDER2_NOT_FOUND,
516     SEW_BOTH_BORDERS_NOT_FOUND,
517     SEW_BAD_SIDE_NODES,
518     SEW_VOLUMES_TO_SPLIT,
519     // for SewSideElements()
520     SEW_DIFF_NB_OF_ELEMENTS,
521     SEW_TOPO_DIFF_SETS_OF_ELEMENTS,
522     SEW_BAD_SIDE1_NODES,
523     SEW_BAD_SIDE2_NODES,
524     SEW_INTERNAL_ERROR
525     };
526     
527
528   Sew_Error SewFreeBorder (const SMDS_MeshNode* theBorderFirstNode,
529                            const SMDS_MeshNode* theBorderSecondNode,
530                            const SMDS_MeshNode* theBorderLastNode,
531                            const SMDS_MeshNode* theSide2FirstNode,
532                            const SMDS_MeshNode* theSide2SecondNode,
533                            const SMDS_MeshNode* theSide2ThirdNode = 0,
534                            const bool           theSide2IsFreeBorder = true,
535                            const bool           toCreatePolygons = false,
536                            const bool           toCreatePolyedrs = false);
537   // Sew the free border to the side2 by replacing nodes in
538   // elements on the free border with nodes of the elements
539   // of the side 2. If nb of links in the free border and
540   // between theSide2FirstNode and theSide2LastNode are different,
541   // additional nodes are inserted on a link provided that no
542   // volume elements share the splitted link.
543   // The side 2 is a free border if theSide2IsFreeBorder == true.
544   // Sewing is peformed between the given first, second and last
545   // nodes on the sides.
546   // theBorderFirstNode is merged with theSide2FirstNode.
547   // if (!theSide2IsFreeBorder) then theSide2SecondNode gives
548   // the last node on the side 2, which will be merged with
549   // theBorderLastNode.
550   // if (theSide2IsFreeBorder) then theSide2SecondNode will
551   // be merged with theBorderSecondNode.
552   // if (theSide2IsFreeBorder && theSide2ThirdNode == 0) then
553   // the 2 free borders are sewn link by link and no additional
554   // nodes are inserted.
555   // Return false, if sewing failed.
556
557   Sew_Error SewSideElements (TIDSortedElemSet&    theSide1,
558                              TIDSortedElemSet&    theSide2,
559                              const SMDS_MeshNode* theFirstNode1ToMerge,
560                              const SMDS_MeshNode* theFirstNode2ToMerge,
561                              const SMDS_MeshNode* theSecondNode1ToMerge,
562                              const SMDS_MeshNode* theSecondNode2ToMerge);
563   // Sew two sides of a mesh. Nodes belonging to theSide1 are
564   // merged with nodes of elements of theSide2.
565   // Number of elements in theSide1 and in theSide2 must be
566   // equal and they should have similar node connectivity.
567   // The nodes to merge should belong to side s borders and
568   // the first node should be linked to the second.
569
570   void InsertNodesIntoLink(const SMDS_MeshElement*          theFace,
571                            const SMDS_MeshNode*             theBetweenNode1,
572                            const SMDS_MeshNode*             theBetweenNode2,
573                            std::list<const SMDS_MeshNode*>& theNodesToInsert,
574                            const bool                       toCreatePoly = false);
575   // insert theNodesToInsert into theFace between theBetweenNode1 and theBetweenNode2.
576   // If toCreatePoly is true, replace theFace by polygon, else split theFace.
577
578   void UpdateVolumes (const SMDS_MeshNode*             theBetweenNode1,
579                       const SMDS_MeshNode*             theBetweenNode2,
580                       std::list<const SMDS_MeshNode*>& theNodesToInsert);
581   // insert theNodesToInsert into all volumes, containing link
582   // theBetweenNode1 - theBetweenNode2, between theBetweenNode1 and theBetweenNode2.
583
584   void ConvertToQuadratic(const bool theForce3d, const bool theToBiQuad);
585   void ConvertToQuadratic(const bool theForce3d,
586                           TIDSortedElemSet& theElements, const bool theToBiQuad);
587   // Converts all mesh to quadratic or bi-quadratic one, deletes old elements, 
588   // replacing them with quadratic or bi-quadratic ones with the same id.
589   // If theForce3d = 1; this results in the medium node lying at the 
590   // middle of the line segments connecting start and end node of a mesh element.
591   // If theForce3d = 0; this results in the medium node lying at the 
592   // geometrical edge from which the mesh element is built.
593
594   bool ConvertFromQuadratic();
595   void ConvertFromQuadratic(TIDSortedElemSet& theElements);
596   // Converts all mesh from quadratic to ordinary ones, deletes old quadratic elements, replacing 
597   // them with ordinary mesh elements with the same id.
598   // Returns true in case of success, false otherwise.
599
600   static void AddToSameGroups (const SMDS_MeshElement* elemToAdd,
601                                const SMDS_MeshElement* elemInGroups,
602                                SMESHDS_Mesh *          aMesh);
603   // Add elemToAdd to the all groups the elemInGroups belongs to
604
605   static void RemoveElemFromGroups (const SMDS_MeshElement* element,
606                                     SMESHDS_Mesh *          aMesh);
607   // remove element from the all groups
608
609   static void ReplaceElemInGroups (const SMDS_MeshElement* elemToRm,
610                                    const SMDS_MeshElement* elemToAdd,
611                                    SMESHDS_Mesh *          aMesh);
612   // replace elemToRm by elemToAdd in the all groups
613
614   static void ReplaceElemInGroups (const SMDS_MeshElement*                     elemToRm,
615                                    const std::vector<const SMDS_MeshElement*>& elemToAdd,
616                                    SMESHDS_Mesh *                              aMesh);
617   // replace elemToRm by elemToAdd in the all groups
618
619   /*!
620    * \brief Return nodes linked to the given one in elements of the type
621    */
622   static void GetLinkedNodes( const SMDS_MeshNode* node,
623                               TIDSortedElemSet &   linkedNodes,
624                               SMDSAbs_ElementType  type = SMDSAbs_All );
625
626   /*!
627    * \brief Find corresponding nodes in two sets of faces 
628     * \param theSide1 - first face set
629     * \param theSide2 - second first face
630     * \param theFirstNode1 - a boundary node of set 1
631     * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
632     * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
633     * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
634     * \param nReplaceMap - output map of corresponding nodes
635     * \return Sew_Error  - is a success or not
636    */
637   static Sew_Error FindMatchingNodes(std::set<const SMDS_MeshElement*>& theSide1,
638                                      std::set<const SMDS_MeshElement*>& theSide2,
639                                      const SMDS_MeshNode*               theFirstNode1,
640                                      const SMDS_MeshNode*               theFirstNode2,
641                                      const SMDS_MeshNode*               theSecondNode1,
642                                      const SMDS_MeshNode*               theSecondNode2,
643                                      TNodeNodeMap &                     theNodeReplaceMap);
644
645   /*!
646    * \brief Returns true if given node is medium
647     * \param n - node to check
648     * \param typeToCheck - type of elements containing the node to ask about node status
649     * \return bool - check result
650    */
651   static bool IsMedium(const SMDS_MeshNode*      node,
652                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
653
654   int FindShape (const SMDS_MeshElement * theElem);
655   // Return an index of the shape theElem is on
656   // or zero if a shape not found
657
658   void DoubleElements( const TIDSortedElemSet& theElements );
659
660   bool DoubleNodes( const std::list< int >& theListOfNodes, 
661                     const std::list< int >& theListOfModifiedElems );
662   
663   bool DoubleNodes( const TIDSortedElemSet& theElems, 
664                     const TIDSortedElemSet& theNodesNot,
665                     const TIDSortedElemSet& theAffectedElems );
666
667   bool AffectedElemGroupsInRegion( const TIDSortedElemSet& theElems,
668                                    const TIDSortedElemSet& theNodesNot,
669                                    const TopoDS_Shape&     theShape,
670                                    TIDSortedElemSet& theAffectedElems);
671
672   bool DoubleNodesInRegion( const TIDSortedElemSet& theElems, 
673                             const TIDSortedElemSet& theNodesNot,
674                             const TopoDS_Shape&     theShape );
675   
676   double OrientedAngle(const gp_Pnt& p0, const gp_Pnt& p1, const gp_Pnt& g1, const gp_Pnt& g2);
677
678   bool DoubleNodesOnGroupBoundaries( const std::vector<TIDSortedElemSet>& theElems,
679                                      bool                                 createJointElems,
680                                      bool                                 onAllBoundaries);
681
682   bool CreateFlatElementsOnFacesGroups( const std::vector<TIDSortedElemSet>& theElems );
683
684   void CreateHoleSkin(double radius,
685                       const TopoDS_Shape& theShape,
686                       SMESH_NodeSearcher* theNodeSearcher,
687                       const char* groupName,
688                       std::vector<double>&   nodesCoords,
689                       std::vector<std::vector<int> >& listOfListOfNodes);
690
691   /*!
692    * \brief Generated skin mesh (containing 2D cells) from 3D mesh
693    * The created 2D mesh elements based on nodes of free faces of boundary volumes
694    * \return TRUE if operation has been completed successfully, FALSE otherwise
695    */
696   bool Make2DMeshFrom3D();
697
698   enum Bnd_Dimension { BND_2DFROM3D, BND_1DFROM3D, BND_1DFROM2D };
699
700   int MakeBoundaryMesh(const TIDSortedElemSet& elements,
701                        Bnd_Dimension           dimension,
702                        SMESH_Group*            group = 0,
703                        SMESH_Mesh*             targetMesh = 0,
704                        bool                    toCopyElements = false,
705                        bool                    toCopyExistingBondary = false,
706                        bool                    toAddExistingBondary = false,
707                        bool                    aroundElements = false);
708
709  private:
710
711   /*!
712    * \brief Convert elements contained in a submesh to quadratic
713    * \return int - nb of checked elements
714    */
715   int convertElemToQuadratic(SMESHDS_SubMesh *   theSm,
716                              SMESH_MesherHelper& theHelper,
717                              const bool          theForce3d);
718
719   /*!
720    * \brief Convert quadratic elements to linear ones and remove quadratic nodes
721    * \return nb of checked elements
722    */
723   int removeQuadElem( SMESHDS_SubMesh *    theSm,
724                       SMDS_ElemIteratorPtr theItr,
725                       const int            theShapeID);
726   /*!
727    * \brief Create groups of elements made during transformation
728    * \param nodeGens - nodes making corresponding myLastCreatedNodes
729    * \param elemGens - elements making corresponding myLastCreatedElems
730    * \param postfix - to append to names of new groups
731    * \param targetMesh - mesh to create groups in
732    * \param topPresent - is there "top" elements that are created by sweeping
733    */
734   PGroupIDs generateGroups(const SMESH_SequenceOfElemPtr& nodeGens,
735                            const SMESH_SequenceOfElemPtr& elemGens,
736                            const std::string&             postfix,
737                            SMESH_Mesh*                    targetMesh=0,
738                            const bool                     topPresent=true);
739   /*!
740    * \brief Create elements by sweeping an element
741    * \param elem - element to sweep
742    * \param newNodesItVec - nodes generated from each node of the element
743    * \param newElems - generated elements
744    * \param nbSteps - number of sweeping steps
745    * \param srcElements - to append elem for each generated element
746    */
747   void sweepElement(const SMDS_MeshElement*                    elem,
748                     const std::vector<TNodeOfNodeListMapItr> & newNodesItVec,
749                     std::list<const SMDS_MeshElement*>&        newElems,
750                     const size_t                               nbSteps,
751                     SMESH_SequenceOfElemPtr&                   srcElements);
752
753   /*!
754    * \brief Create 1D and 2D elements around swept elements
755    * \param mapNewNodes - source nodes and ones generated from them
756    * \param newElemsMap - source elements and ones generated from them
757    * \param elemNewNodesMap - nodes generated from each node of each element
758    * \param elemSet - all swept elements
759    * \param nbSteps - number of sweeping steps
760    * \param srcElements - to append elem for each generated element
761    */
762   void makeWalls (TNodeOfNodeListMap &     mapNewNodes,
763                   TTElemOfElemListMap &    newElemsMap,
764                   TElemOfVecOfNnlmiMap &   elemNewNodesMap,
765                   TIDSortedElemSet&        elemSet,
766                   const int                nbSteps,
767                   SMESH_SequenceOfElemPtr& srcElements);
768
769   struct SMESH_MeshEditor_PathPoint
770   {
771     gp_Pnt myPnt;
772     gp_Dir myTgt;
773     double myAngle, myPrm;
774
775     SMESH_MeshEditor_PathPoint(): myPnt(99., 99., 99.), myTgt(1.,0.,0.), myAngle(0), myPrm(0) {}
776     void          SetPnt      (const gp_Pnt& aP3D)  { myPnt  =aP3D; }
777     void          SetTangent  (const gp_Dir& aTgt)  { myTgt  =aTgt; }
778     void          SetAngle    (const double& aBeta) { myAngle=aBeta; }
779     void          SetParameter(const double& aPrm)  { myPrm  =aPrm; }
780     const gp_Pnt& Pnt         ()const               { return myPnt; }
781     const gp_Dir& Tangent     ()const               { return myTgt; }
782     double        Angle       ()const               { return myAngle; }
783     double        Parameter   ()const               { return myPrm; }
784   };
785   Extrusion_Error MakeEdgePathPoints(std::list<double>&                     aPrms,
786                                      const TopoDS_Edge&                     aTrackEdge,
787                                      bool                                   aFirstIsStart,
788                                      std::list<SMESH_MeshEditor_PathPoint>& aLPP);
789   Extrusion_Error MakeExtrElements(TIDSortedElemSet                       theElements[2],
790                                    std::list<SMESH_MeshEditor_PathPoint>& theFullList,
791                                    const bool                             theHasAngles,
792                                    std::list<double>&                     theAngles,
793                                    const bool                             theLinearVariation,
794                                    const bool                             theHasRefPoint,
795                                    const gp_Pnt&                          theRefPoint,
796                                    const bool                             theMakeGroups);
797   static void LinearAngleVariation(const int          NbSteps,
798                                    std::list<double>& theAngles);
799
800   bool doubleNodes( SMESHDS_Mesh*           theMeshDS,
801                     const TIDSortedElemSet& theElems,
802                     const TIDSortedElemSet& theNodesNot,
803                     TNodeNodeMap&           theNodeNodeMap,
804                     const bool              theIsDoubleElem );
805
806   void copyPosition( const SMDS_MeshNode* from,
807                      const SMDS_MeshNode* to );
808
809 private:
810
811   SMESH_Mesh *            myMesh;
812
813   // Nodes and elements created during last operation
814   SMESH_SequenceOfElemPtr myLastCreatedNodes, myLastCreatedElems;
815
816   // Description of error/warning occured during last operation
817   SMESH_ComputeErrorPtr   myError;
818 };
819
820 #endif