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