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