Salome HOME
Update copyright info (2010->2011)
[modules/smesh.git] / src / SMESH / SMESH_MeshEditor.hxx
1 //  Copyright (C) 2007-2011  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 = 0);
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 = 0);
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   /*!
411    * \brief Return true if the point is IN or ON of the element
412    */
413   static bool isOut( const SMDS_MeshElement* element, const gp_Pnt& point, double tol );
414
415
416   int SimplifyFace (const std::vector<const SMDS_MeshNode *> faceNodes,
417                     std::vector<const SMDS_MeshNode *>&      poly_nodes,
418                     std::vector<int>&                        quantities) const;
419   // Split face, defined by <faceNodes>, into several faces by repeating nodes.
420   // Is used by MergeNodes()
421
422   void MergeNodes (TListOfListOfNodes & theNodeGroups);
423   // In each group, the cdr of nodes are substituted by the first one
424   // in all elements.
425
426   typedef std::list< std::list< int > > TListOfListOfElementsID;
427
428   void FindEqualElements(std::set<const SMDS_MeshElement*> & theElements,
429                          TListOfListOfElementsID &           theGroupsOfElementsID);
430   // Return list of group of elements build on the same nodes.
431   // Search among theElements or in the whole mesh if theElements is empty.
432
433   void MergeElements(TListOfListOfElementsID & theGroupsOfElementsID);
434   // In each group remove all but first of elements.
435
436   void MergeEqualElements();
437   // Remove all but one of elements built on the same nodes.
438   // Return nb of successfully merged groups.
439
440   static bool CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
441                                    const SMDS_MeshNode* theNode2,
442                                    const SMDS_MeshNode* theNode3 = 0);
443   // Return true if the three nodes are on a free border
444
445   static bool FindFreeBorder (const SMDS_MeshNode*                  theFirstNode,
446                               const SMDS_MeshNode*                  theSecondNode,
447                               const SMDS_MeshNode*                  theLastNode,
448                               std::list< const SMDS_MeshNode* > &   theNodes,
449                               std::list< const SMDS_MeshElement* >& theFaces);
450   // Return nodes and faces of a free border if found 
451
452   enum Sew_Error {
453     SEW_OK,
454     // for SewFreeBorder()
455     SEW_BORDER1_NOT_FOUND,
456     SEW_BORDER2_NOT_FOUND,
457     SEW_BOTH_BORDERS_NOT_FOUND,
458     SEW_BAD_SIDE_NODES,
459     SEW_VOLUMES_TO_SPLIT,
460     // for SewSideElements()
461     SEW_DIFF_NB_OF_ELEMENTS,
462     SEW_TOPO_DIFF_SETS_OF_ELEMENTS,
463     SEW_BAD_SIDE1_NODES,
464     SEW_BAD_SIDE2_NODES,
465     SEW_INTERNAL_ERROR
466     };
467     
468
469   Sew_Error SewFreeBorder (const SMDS_MeshNode* theBorderFirstNode,
470                            const SMDS_MeshNode* theBorderSecondNode,
471                            const SMDS_MeshNode* theBorderLastNode,
472                            const SMDS_MeshNode* theSide2FirstNode,
473                            const SMDS_MeshNode* theSide2SecondNode,
474                            const SMDS_MeshNode* theSide2ThirdNode = 0,
475                            const bool           theSide2IsFreeBorder = true,
476                            const bool           toCreatePolygons = false,
477                            const bool           toCreatePolyedrs = false);
478   // Sew the free border to the side2 by replacing nodes in
479   // elements on the free border with nodes of the elements
480   // of the side 2. If nb of links in the free border and
481   // between theSide2FirstNode and theSide2LastNode are different,
482   // additional nodes are inserted on a link provided that no
483   // volume elements share the splitted link.
484   // The side 2 is a free border if theSide2IsFreeBorder == true.
485   // Sewing is peformed between the given first, second and last
486   // nodes on the sides.
487   // theBorderFirstNode is merged with theSide2FirstNode.
488   // if (!theSide2IsFreeBorder) then theSide2SecondNode gives
489   // the last node on the side 2, which will be merged with
490   // theBorderLastNode.
491   // if (theSide2IsFreeBorder) then theSide2SecondNode will
492   // be merged with theBorderSecondNode.
493   // if (theSide2IsFreeBorder && theSide2ThirdNode == 0) then
494   // the 2 free borders are sewn link by link and no additional
495   // nodes are inserted.
496   // Return false, if sewing failed.
497
498   Sew_Error SewSideElements (TIDSortedElemSet&    theSide1,
499                              TIDSortedElemSet&    theSide2,
500                              const SMDS_MeshNode* theFirstNode1ToMerge,
501                              const SMDS_MeshNode* theFirstNode2ToMerge,
502                              const SMDS_MeshNode* theSecondNode1ToMerge,
503                              const SMDS_MeshNode* theSecondNode2ToMerge);
504   // Sew two sides of a mesh. Nodes belonging to theSide1 are
505   // merged with nodes of elements of theSide2.
506   // Number of elements in theSide1 and in theSide2 must be
507   // equal and they should have similar node connectivity.
508   // The nodes to merge should belong to side s borders and
509   // the first node should be linked to the second.
510
511   void InsertNodesIntoLink(const SMDS_MeshElement*          theFace,
512                            const SMDS_MeshNode*             theBetweenNode1,
513                            const SMDS_MeshNode*             theBetweenNode2,
514                            std::list<const SMDS_MeshNode*>& theNodesToInsert,
515                            const bool                       toCreatePoly = false);
516   // insert theNodesToInsert into theFace between theBetweenNode1 and theBetweenNode2.
517   // If toCreatePoly is true, replace theFace by polygon, else split theFace.
518
519   void UpdateVolumes (const SMDS_MeshNode*             theBetweenNode1,
520                       const SMDS_MeshNode*             theBetweenNode2,
521                       std::list<const SMDS_MeshNode*>& theNodesToInsert);
522   // insert theNodesToInsert into all volumes, containing link
523   // theBetweenNode1 - theBetweenNode2, between theBetweenNode1 and theBetweenNode2.
524
525   void ConvertToQuadratic(const bool theForce3d);
526   // Converts all mesh to quadratic one, deletes old elements, replacing 
527   // them with quadratic ones with the same id.
528   // If theForce3d = 1; this results in the medium node lying at the 
529   // middle of the line segments connecting start and end node of a mesh 
530   // element
531   // If theForce3d = 0; this results in the medium node lying at the 
532   // geometrical edge from which the mesh element is built
533
534   bool ConvertFromQuadratic();
535   // Converts all mesh from quadratic to ordinary ones, deletes old quadratic elements, replacing 
536   // them with ordinary mesh elements with the same id.
537   // Returns true in case of success, false otherwise.
538
539   static void AddToSameGroups (const SMDS_MeshElement* elemToAdd,
540                                const SMDS_MeshElement* elemInGroups,
541                                SMESHDS_Mesh *          aMesh);
542   // Add elemToAdd to the all groups the elemInGroups belongs to
543
544   static void RemoveElemFromGroups (const SMDS_MeshElement* element,
545                                     SMESHDS_Mesh *          aMesh);
546   // remove element from the all groups
547
548   static void ReplaceElemInGroups (const SMDS_MeshElement* elemToRm,
549                                    const SMDS_MeshElement* elemToAdd,
550                                    SMESHDS_Mesh *          aMesh);
551   // replace elemToRm by elemToAdd in the all groups
552
553   static void ReplaceElemInGroups (const SMDS_MeshElement*                     elemToRm,
554                                    const std::vector<const SMDS_MeshElement*>& elemToAdd,
555                                    SMESHDS_Mesh *                              aMesh);
556   // replace elemToRm by elemToAdd in the all groups
557
558   /*!
559    * \brief Return nodes linked to the given one in elements of the type
560    */
561   static void GetLinkedNodes( const SMDS_MeshNode* node,
562                               TIDSortedElemSet &   linkedNodes,
563                               SMDSAbs_ElementType  type = SMDSAbs_All );
564
565   static const SMDS_MeshElement* FindFaceInSet(const SMDS_MeshNode*    n1,
566                                                const SMDS_MeshNode*    n2,
567                                                const TIDSortedElemSet& elemSet,
568                                                const TIDSortedElemSet& avoidSet,
569                                                int*                    i1=0,
570                                                int*                    i2=0);
571   // Return a face having linked nodes n1 and n2 and which is
572   // - not in avoidSet,
573   // - in elemSet provided that !elemSet.empty()
574   // i1 and i2 optionally returns indices of n1 and n2
575
576   /*!
577    * \brief Find corresponding nodes in two sets of faces 
578     * \param theSide1 - first face set
579     * \param theSide2 - second first face
580     * \param theFirstNode1 - a boundary node of set 1
581     * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
582     * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
583     * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
584     * \param nReplaceMap - output map of corresponding nodes
585     * \retval Sew_Error  - is a success or not
586    */
587   static Sew_Error FindMatchingNodes(std::set<const SMDS_MeshElement*>& theSide1,
588                                      std::set<const SMDS_MeshElement*>& theSide2,
589                                      const SMDS_MeshNode*               theFirstNode1,
590                                      const SMDS_MeshNode*               theFirstNode2,
591                                      const SMDS_MeshNode*               theSecondNode1,
592                                      const SMDS_MeshNode*               theSecondNode2,
593                                      TNodeNodeMap &                     theNodeReplaceMap);
594
595   /*!
596    * \brief Returns true if given node is medium
597     * \param n - node to check
598     * \param typeToCheck - type of elements containing the node to ask about node status
599     * \retval bool - check result
600    */
601   static bool IsMedium(const SMDS_MeshNode*      node,
602                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
603
604   int FindShape (const SMDS_MeshElement * theElem);
605   // Return an index of the shape theElem is on
606   // or zero if a shape not found
607
608   SMESH_Mesh * GetMesh() { return myMesh; }
609
610   SMESHDS_Mesh * GetMeshDS() { return myMesh->GetMeshDS(); }
611
612   const SMESH_SequenceOfElemPtr& GetLastCreatedNodes() const { return myLastCreatedNodes; }
613
614   const SMESH_SequenceOfElemPtr& GetLastCreatedElems() const { return myLastCreatedElems; }
615
616   bool DoubleNodes( const std::list< int >& theListOfNodes, 
617                     const std::list< int >& theListOfModifiedElems );
618   
619   bool DoubleNodes( const TIDSortedElemSet& theElems, 
620                     const TIDSortedElemSet& theNodesNot,
621                     const TIDSortedElemSet& theAffectedElems );
622
623   bool DoubleNodesInRegion( const TIDSortedElemSet& theElems, 
624                             const TIDSortedElemSet& theNodesNot,
625                             const TopoDS_Shape&     theShape );
626   
627   bool Make2DMeshFrom3D();
628
629   enum Bnd_Dimension { BND_2DFROM3D, BND_1DFROM3D, BND_1DFROM2D };
630
631   void MakeBoundaryMesh(const TIDSortedElemSet& elements,
632                         Bnd_Dimension           dimension,
633                         SMESH_Group*            group = 0,
634                         SMESH_Mesh*             targetMesh = 0,
635                         bool                    toCopyElements = false,
636                         bool                    toCopyExistingBondary = false);
637
638 private:
639
640   /*!
641    * \brief Convert elements contained in a submesh to quadratic
642     * \retval int - nb of checked elements
643    */
644   int convertElemToQuadratic(SMESHDS_SubMesh *   theSm,
645                              SMESH_MesherHelper& theHelper,
646                              const bool          theForce3d);
647
648   /*!
649    * \brief Convert quadratic elements to linear ones and remove quadratic nodes
650     * \retval int - nb of checked elements
651    */
652   int removeQuadElem( SMESHDS_SubMesh *    theSm,
653                       SMDS_ElemIteratorPtr theItr,
654                       const int            theShapeID);
655   /*!
656    * \brief Create groups of elements made during transformation
657    * \param nodeGens - nodes making corresponding myLastCreatedNodes
658    * \param elemGens - elements making corresponding myLastCreatedElems
659    * \param postfix - to append to names of new groups
660    */
661   PGroupIDs generateGroups(const SMESH_SequenceOfElemPtr& nodeGens,
662                            const SMESH_SequenceOfElemPtr& elemGens,
663                            const std::string&             postfix,
664                            SMESH_Mesh*                    targetMesh=0);
665
666
667   typedef std::map<const SMDS_MeshNode*, std::list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
668   typedef TNodeOfNodeListMap::iterator                                     TNodeOfNodeListMapItr;
669   typedef std::vector<TNodeOfNodeListMapItr>                               TVecOfNnlmiMap;
670   typedef std::map<const SMDS_MeshElement*, TVecOfNnlmiMap >               TElemOfVecOfNnlmiMap;
671
672   /*!
673    * \brief Create elements by sweeping an element
674     * \param elem - element to sweep
675     * \param newNodesItVec - nodes generated from each node of the element
676     * \param newElems - generated elements
677     * \param nbSteps - number of sweeping steps
678     * \param srcElements - to append elem for each generated element
679    */
680   void sweepElement(const SMDS_MeshElement*                    elem,
681                     const std::vector<TNodeOfNodeListMapItr> & newNodesItVec,
682                     std::list<const SMDS_MeshElement*>&        newElems,
683                     const int                                  nbSteps,
684                     SMESH_SequenceOfElemPtr&                   srcElements);
685
686   /*!
687    * \brief Create 1D and 2D elements around swept elements
688     * \param mapNewNodes - source nodes and ones generated from them
689     * \param newElemsMap - source elements and ones generated from them
690     * \param elemNewNodesMap - nodes generated from each node of each element
691     * \param elemSet - all swept elements
692     * \param nbSteps - number of sweeping steps
693     * \param srcElements - to append elem for each generated element
694    */
695   void makeWalls (TNodeOfNodeListMap &     mapNewNodes,
696                   TElemOfElemListMap &     newElemsMap,
697                   TElemOfVecOfNnlmiMap &   elemNewNodesMap,
698                   TIDSortedElemSet&        elemSet,
699                   const int                nbSteps,
700                   SMESH_SequenceOfElemPtr& srcElements);
701
702   struct SMESH_MeshEditor_PathPoint
703   {
704     gp_Pnt myPnt;
705     gp_Dir myTgt;
706     double myAngle, myPrm;
707
708     SMESH_MeshEditor_PathPoint(): myPnt(99., 99., 99.), myTgt(1.,0.,0.), myAngle(0), myPrm(0) {}
709     void          SetPnt      (const gp_Pnt& aP3D)  { myPnt  =aP3D; }
710     void          SetTangent  (const gp_Dir& aTgt)  { myTgt  =aTgt; }
711     void          SetAngle    (const double& aBeta) { myAngle=aBeta; }
712     void          SetParameter(const double& aPrm)  { myPrm  =aPrm; }
713     const gp_Pnt& Pnt         ()const               { return myPnt; }
714     const gp_Dir& Tangent     ()const               { return myTgt; }
715     double        Angle       ()const               { return myAngle; }
716     double        Parameter   ()const               { return myPrm; }
717   };
718   Extrusion_Error MakeEdgePathPoints(std::list<double>&                     aPrms,
719                                      const TopoDS_Edge&                     aTrackEdge,
720                                      bool                                   aFirstIsStart,
721                                      std::list<SMESH_MeshEditor_PathPoint>& aLPP);
722   Extrusion_Error MakeExtrElements(TIDSortedElemSet&                      theElements,
723                                    std::list<SMESH_MeshEditor_PathPoint>& theFullList,
724                                    const bool                             theHasAngles,
725                                    std::list<double>&                     theAngles,
726                                    const bool                             theLinearVariation,
727                                    const bool                             theHasRefPoint,
728                                    const gp_Pnt&                          theRefPoint,
729                                    const bool                             theMakeGroups);
730   void LinearAngleVariation(const int     NbSteps,
731                             list<double>& theAngles);
732
733   bool doubleNodes( SMESHDS_Mesh*                                           theMeshDS,
734                     const TIDSortedElemSet&                                 theElems,
735                     const TIDSortedElemSet&                                 theNodesNot,
736                     std::map< const SMDS_MeshNode*, const SMDS_MeshNode* >& theNodeNodeMap,
737                     const bool                                              theIsDoubleElem );
738
739 private:
740
741   SMESH_Mesh * myMesh;
742
743   /*!
744    * Sequence for keeping nodes created during last operation
745    */
746   SMESH_SequenceOfElemPtr myLastCreatedNodes;
747
748   /*!
749    * Sequence for keeping elements created during last operation
750    */
751   SMESH_SequenceOfElemPtr myLastCreatedElems;
752
753 };
754
755 #endif