Salome HOME
Patch for MacOS (from SALOME forum)
[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   // In each group, the cdr of nodes are substituted by the first one
476   // in all elements.
477
478   typedef std::list< std::list< int > > TListOfListOfElementsID;
479
480   void FindEqualElements(TIDSortedElemSet &        theElements,
481                          TListOfListOfElementsID & theGroupsOfElementsID);
482   // Return list of group of elements build on the same nodes.
483   // Search among theElements or in the whole mesh if theElements is empty.
484
485   void MergeElements(TListOfListOfElementsID & theGroupsOfElementsID);
486   // In each group remove all but first of elements.
487
488   void MergeEqualElements();
489   // Remove all but one of elements built on the same nodes.
490   // Return nb of successfully merged groups.
491
492   int SimplifyFace (const std::vector<const SMDS_MeshNode *>& faceNodes,
493                     std::vector<const SMDS_MeshNode *>&       poly_nodes,
494                     std::vector<int>&                         quantities) const;
495   // Split face, defined by <faceNodes>, into several faces by repeating nodes.
496   // Is used by MergeNodes()
497
498   static bool CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
499                                    const SMDS_MeshNode* theNode2,
500                                    const SMDS_MeshNode* theNode3 = 0);
501   // Return true if the three nodes are on a free border
502
503   static bool FindFreeBorder (const SMDS_MeshNode*                  theFirstNode,
504                               const SMDS_MeshNode*                  theSecondNode,
505                               const SMDS_MeshNode*                  theLastNode,
506                               std::list< const SMDS_MeshNode* > &   theNodes,
507                               std::list< const SMDS_MeshElement* >& theFaces);
508   // Return nodes and faces of a free border if found 
509
510   enum Sew_Error {
511     SEW_OK,
512     // for SewFreeBorder()
513     SEW_BORDER1_NOT_FOUND,
514     SEW_BORDER2_NOT_FOUND,
515     SEW_BOTH_BORDERS_NOT_FOUND,
516     SEW_BAD_SIDE_NODES,
517     SEW_VOLUMES_TO_SPLIT,
518     // for SewSideElements()
519     SEW_DIFF_NB_OF_ELEMENTS,
520     SEW_TOPO_DIFF_SETS_OF_ELEMENTS,
521     SEW_BAD_SIDE1_NODES,
522     SEW_BAD_SIDE2_NODES,
523     SEW_INTERNAL_ERROR
524     };
525     
526
527   Sew_Error SewFreeBorder (const SMDS_MeshNode* theBorderFirstNode,
528                            const SMDS_MeshNode* theBorderSecondNode,
529                            const SMDS_MeshNode* theBorderLastNode,
530                            const SMDS_MeshNode* theSide2FirstNode,
531                            const SMDS_MeshNode* theSide2SecondNode,
532                            const SMDS_MeshNode* theSide2ThirdNode = 0,
533                            const bool           theSide2IsFreeBorder = true,
534                            const bool           toCreatePolygons = false,
535                            const bool           toCreatePolyedrs = false);
536   // Sew the free border to the side2 by replacing nodes in
537   // elements on the free border with nodes of the elements
538   // of the side 2. If nb of links in the free border and
539   // between theSide2FirstNode and theSide2LastNode are different,
540   // additional nodes are inserted on a link provided that no
541   // volume elements share the splitted link.
542   // The side 2 is a free border if theSide2IsFreeBorder == true.
543   // Sewing is peformed between the given first, second and last
544   // nodes on the sides.
545   // theBorderFirstNode is merged with theSide2FirstNode.
546   // if (!theSide2IsFreeBorder) then theSide2SecondNode gives
547   // the last node on the side 2, which will be merged with
548   // theBorderLastNode.
549   // if (theSide2IsFreeBorder) then theSide2SecondNode will
550   // be merged with theBorderSecondNode.
551   // if (theSide2IsFreeBorder && theSide2ThirdNode == 0) then
552   // the 2 free borders are sewn link by link and no additional
553   // nodes are inserted.
554   // Return false, if sewing failed.
555
556   Sew_Error SewSideElements (TIDSortedElemSet&    theSide1,
557                              TIDSortedElemSet&    theSide2,
558                              const SMDS_MeshNode* theFirstNode1ToMerge,
559                              const SMDS_MeshNode* theFirstNode2ToMerge,
560                              const SMDS_MeshNode* theSecondNode1ToMerge,
561                              const SMDS_MeshNode* theSecondNode2ToMerge);
562   // Sew two sides of a mesh. Nodes belonging to theSide1 are
563   // merged with nodes of elements of theSide2.
564   // Number of elements in theSide1 and in theSide2 must be
565   // equal and they should have similar node connectivity.
566   // The nodes to merge should belong to side s borders and
567   // the first node should be linked to the second.
568
569   void InsertNodesIntoLink(const SMDS_MeshElement*          theFace,
570                            const SMDS_MeshNode*             theBetweenNode1,
571                            const SMDS_MeshNode*             theBetweenNode2,
572                            std::list<const SMDS_MeshNode*>& theNodesToInsert,
573                            const bool                       toCreatePoly = false);
574   // insert theNodesToInsert into theFace between theBetweenNode1 and theBetweenNode2.
575   // If toCreatePoly is true, replace theFace by polygon, else split theFace.
576
577   void UpdateVolumes (const SMDS_MeshNode*             theBetweenNode1,
578                       const SMDS_MeshNode*             theBetweenNode2,
579                       std::list<const SMDS_MeshNode*>& theNodesToInsert);
580   // insert theNodesToInsert into all volumes, containing link
581   // theBetweenNode1 - theBetweenNode2, between theBetweenNode1 and theBetweenNode2.
582
583   void ConvertToQuadratic(const bool theForce3d, const bool theToBiQuad);
584   void ConvertToQuadratic(const bool theForce3d,
585                           TIDSortedElemSet& theElements, const bool theToBiQuad);
586   // Converts all mesh to quadratic or bi-quadratic one, deletes old elements, 
587   // replacing them with quadratic or bi-quadratic ones with the same id.
588   // If theForce3d = 1; this results in the medium node lying at the 
589   // middle of the line segments connecting start and end node of a mesh element.
590   // If theForce3d = 0; this results in the medium node lying at the 
591   // geometrical edge from which the mesh element is built.
592
593   bool ConvertFromQuadratic();
594   void ConvertFromQuadratic(TIDSortedElemSet& theElements);
595   // Converts all mesh from quadratic to ordinary ones, deletes old quadratic elements, replacing 
596   // them with ordinary mesh elements with the same id.
597   // Returns true in case of success, false otherwise.
598
599   static void AddToSameGroups (const SMDS_MeshElement* elemToAdd,
600                                const SMDS_MeshElement* elemInGroups,
601                                SMESHDS_Mesh *          aMesh);
602   // Add elemToAdd to the all groups the elemInGroups belongs to
603
604   static void RemoveElemFromGroups (const SMDS_MeshElement* element,
605                                     SMESHDS_Mesh *          aMesh);
606   // remove element from the all groups
607
608   static void ReplaceElemInGroups (const SMDS_MeshElement* elemToRm,
609                                    const SMDS_MeshElement* elemToAdd,
610                                    SMESHDS_Mesh *          aMesh);
611   // replace elemToRm by elemToAdd in the all groups
612
613   static void ReplaceElemInGroups (const SMDS_MeshElement*                     elemToRm,
614                                    const std::vector<const SMDS_MeshElement*>& elemToAdd,
615                                    SMESHDS_Mesh *                              aMesh);
616   // replace elemToRm by elemToAdd in the all groups
617
618   /*!
619    * \brief Return nodes linked to the given one in elements of the type
620    */
621   static void GetLinkedNodes( const SMDS_MeshNode* node,
622                               TIDSortedElemSet &   linkedNodes,
623                               SMDSAbs_ElementType  type = SMDSAbs_All );
624
625   /*!
626    * \brief Find corresponding nodes in two sets of faces 
627     * \param theSide1 - first face set
628     * \param theSide2 - second first face
629     * \param theFirstNode1 - a boundary node of set 1
630     * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
631     * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
632     * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
633     * \param nReplaceMap - output map of corresponding nodes
634     * \return Sew_Error  - is a success or not
635    */
636   static Sew_Error FindMatchingNodes(std::set<const SMDS_MeshElement*>& theSide1,
637                                      std::set<const SMDS_MeshElement*>& theSide2,
638                                      const SMDS_MeshNode*               theFirstNode1,
639                                      const SMDS_MeshNode*               theFirstNode2,
640                                      const SMDS_MeshNode*               theSecondNode1,
641                                      const SMDS_MeshNode*               theSecondNode2,
642                                      TNodeNodeMap &                     theNodeReplaceMap);
643
644   /*!
645    * \brief Returns true if given node is medium
646     * \param n - node to check
647     * \param typeToCheck - type of elements containing the node to ask about node status
648     * \return bool - check result
649    */
650   static bool IsMedium(const SMDS_MeshNode*      node,
651                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
652
653   int FindShape (const SMDS_MeshElement * theElem);
654   // Return an index of the shape theElem is on
655   // or zero if a shape not found
656
657   void DoubleElements( const TIDSortedElemSet& theElements );
658
659   bool DoubleNodes( const std::list< int >& theListOfNodes, 
660                     const std::list< int >& theListOfModifiedElems );
661   
662   bool DoubleNodes( const TIDSortedElemSet& theElems, 
663                     const TIDSortedElemSet& theNodesNot,
664                     const TIDSortedElemSet& theAffectedElems );
665
666   bool AffectedElemGroupsInRegion( const TIDSortedElemSet& theElems,
667                                    const TIDSortedElemSet& theNodesNot,
668                                    const TopoDS_Shape&     theShape,
669                                    TIDSortedElemSet& theAffectedElems);
670
671   bool DoubleNodesInRegion( const TIDSortedElemSet& theElems, 
672                             const TIDSortedElemSet& theNodesNot,
673                             const TopoDS_Shape&     theShape );
674   
675   double OrientedAngle(const gp_Pnt& p0, const gp_Pnt& p1, const gp_Pnt& g1, const gp_Pnt& g2);
676
677   bool DoubleNodesOnGroupBoundaries( const std::vector<TIDSortedElemSet>& theElems,
678                                      bool                                 createJointElems,
679                                      bool                                 onAllBoundaries);
680
681   bool CreateFlatElementsOnFacesGroups( const std::vector<TIDSortedElemSet>& theElems );
682
683   void CreateHoleSkin(double radius,
684                       const TopoDS_Shape& theShape,
685                       SMESH_NodeSearcher* theNodeSearcher,
686                       const char* groupName,
687                       std::vector<double>&   nodesCoords,
688                       std::vector<std::vector<int> >& listOfListOfNodes);
689
690   /*!
691    * \brief Generated skin mesh (containing 2D cells) from 3D mesh
692    * The created 2D mesh elements based on nodes of free faces of boundary volumes
693    * \return TRUE if operation has been completed successfully, FALSE otherwise
694    */
695   bool Make2DMeshFrom3D();
696
697   enum Bnd_Dimension { BND_2DFROM3D, BND_1DFROM3D, BND_1DFROM2D };
698
699   int MakeBoundaryMesh(const TIDSortedElemSet& elements,
700                        Bnd_Dimension           dimension,
701                        SMESH_Group*            group = 0,
702                        SMESH_Mesh*             targetMesh = 0,
703                        bool                    toCopyElements = false,
704                        bool                    toCopyExistingBondary = false,
705                        bool                    toAddExistingBondary = false,
706                        bool                    aroundElements = false);
707
708  private:
709
710   /*!
711    * \brief Convert elements contained in a submesh to quadratic
712    * \return int - nb of checked elements
713    */
714   int convertElemToQuadratic(SMESHDS_SubMesh *   theSm,
715                              SMESH_MesherHelper& theHelper,
716                              const bool          theForce3d);
717
718   /*!
719    * \brief Convert quadratic elements to linear ones and remove quadratic nodes
720    * \return nb of checked elements
721    */
722   int removeQuadElem( SMESHDS_SubMesh *    theSm,
723                       SMDS_ElemIteratorPtr theItr,
724                       const int            theShapeID);
725   /*!
726    * \brief Create groups of elements made during transformation
727    * \param nodeGens - nodes making corresponding myLastCreatedNodes
728    * \param elemGens - elements making corresponding myLastCreatedElems
729    * \param postfix - to append to names of new groups
730    * \param targetMesh - mesh to create groups in
731    * \param topPresent - is there "top" elements that are created by sweeping
732    */
733   PGroupIDs generateGroups(const SMESH_SequenceOfElemPtr& nodeGens,
734                            const SMESH_SequenceOfElemPtr& elemGens,
735                            const std::string&             postfix,
736                            SMESH_Mesh*                    targetMesh=0,
737                            const bool                     topPresent=true);
738   /*!
739    * \brief Create elements by sweeping an element
740    * \param elem - element to sweep
741    * \param newNodesItVec - nodes generated from each node of the element
742    * \param newElems - generated elements
743    * \param nbSteps - number of sweeping steps
744    * \param srcElements - to append elem for each generated element
745    */
746   void sweepElement(const SMDS_MeshElement*                    elem,
747                     const std::vector<TNodeOfNodeListMapItr> & newNodesItVec,
748                     std::list<const SMDS_MeshElement*>&        newElems,
749                     const size_t                               nbSteps,
750                     SMESH_SequenceOfElemPtr&                   srcElements);
751
752   /*!
753    * \brief Create 1D and 2D elements around swept elements
754    * \param mapNewNodes - source nodes and ones generated from them
755    * \param newElemsMap - source elements and ones generated from them
756    * \param elemNewNodesMap - nodes generated from each node of each element
757    * \param elemSet - all swept elements
758    * \param nbSteps - number of sweeping steps
759    * \param srcElements - to append elem for each generated element
760    */
761   void makeWalls (TNodeOfNodeListMap &     mapNewNodes,
762                   TTElemOfElemListMap &    newElemsMap,
763                   TElemOfVecOfNnlmiMap &   elemNewNodesMap,
764                   TIDSortedElemSet&        elemSet,
765                   const int                nbSteps,
766                   SMESH_SequenceOfElemPtr& srcElements);
767
768   struct SMESH_MeshEditor_PathPoint
769   {
770     gp_Pnt myPnt;
771     gp_Dir myTgt;
772     double myAngle, myPrm;
773
774     SMESH_MeshEditor_PathPoint(): myPnt(99., 99., 99.), myTgt(1.,0.,0.), myAngle(0), myPrm(0) {}
775     void          SetPnt      (const gp_Pnt& aP3D)  { myPnt  =aP3D; }
776     void          SetTangent  (const gp_Dir& aTgt)  { myTgt  =aTgt; }
777     void          SetAngle    (const double& aBeta) { myAngle=aBeta; }
778     void          SetParameter(const double& aPrm)  { myPrm  =aPrm; }
779     const gp_Pnt& Pnt         ()const               { return myPnt; }
780     const gp_Dir& Tangent     ()const               { return myTgt; }
781     double        Angle       ()const               { return myAngle; }
782     double        Parameter   ()const               { return myPrm; }
783   };
784   Extrusion_Error MakeEdgePathPoints(std::list<double>&                     aPrms,
785                                      const TopoDS_Edge&                     aTrackEdge,
786                                      bool                                   aFirstIsStart,
787                                      std::list<SMESH_MeshEditor_PathPoint>& aLPP);
788   Extrusion_Error MakeExtrElements(TIDSortedElemSet                       theElements[2],
789                                    std::list<SMESH_MeshEditor_PathPoint>& theFullList,
790                                    const bool                             theHasAngles,
791                                    std::list<double>&                     theAngles,
792                                    const bool                             theLinearVariation,
793                                    const bool                             theHasRefPoint,
794                                    const gp_Pnt&                          theRefPoint,
795                                    const bool                             theMakeGroups);
796   static void LinearAngleVariation(const int          NbSteps,
797                                    std::list<double>& theAngles);
798
799   bool doubleNodes( SMESHDS_Mesh*           theMeshDS,
800                     const TIDSortedElemSet& theElems,
801                     const TIDSortedElemSet& theNodesNot,
802                     TNodeNodeMap&           theNodeNodeMap,
803                     const bool              theIsDoubleElem );
804
805   void copyPosition( const SMDS_MeshNode* from,
806                      const SMDS_MeshNode* to );
807
808 private:
809
810   SMESH_Mesh *            myMesh;
811
812   // Nodes and elements created during last operation
813   SMESH_SequenceOfElemPtr myLastCreatedNodes, myLastCreatedElems;
814
815   // Description of error/warning occured during last operation
816   SMESH_ComputeErrorPtr   myError;
817 };
818
819 #endif