Salome HOME
Add ConvertFromQuadratic()
[modules/smesh.git] / src / SMESH / SMESH_MeshEditor.hxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 // File      : SMESH_MeshEditor.hxx
25 // Created   : Mon Apr 12 14:56:19 2004
26 // Author    : Edward AGAPOV (eap)
27 // Module    : SMESH
28
29
30 #ifndef SMESH_MeshEditor_HeaderFile
31 #define SMESH_MeshEditor_HeaderFile
32
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_Controls.hxx"
35 #include "SMESH_SequenceOfNode.hxx"
36 #include "gp_Dir.hxx"
37 #include "TColStd_HSequenceOfReal.hxx"
38 #include "SMESH_MesherHelper.hxx"
39
40 #include <list>
41 #include <map>
42
43 typedef map<const SMDS_MeshElement*,
44             list<const SMDS_MeshElement*> > TElemOfElemListMap;
45
46 class SMDS_MeshElement;
47 class SMDS_MeshFace;
48 class SMDS_MeshNode;
49 class gp_Ax1;
50 class gp_Vec;
51 class gp_Pnt;
52
53 class SMESH_MeshEditor {
54  public:
55
56   SMESH_MeshEditor( SMESH_Mesh* theMesh );
57
58   bool Remove (const std::list< int >& theElemIDs, const bool isNodes);
59   // Remove a node or an element.
60   // Modify a compute state of sub-meshes which become empty
61
62   bool InverseDiag (const SMDS_MeshElement * theTria1,
63                     const SMDS_MeshElement * theTria2 );
64   // Replace two neighbour triangles with ones built on the same 4 nodes
65   // but having other common link.
66   // Return False if args are improper
67
68   bool InverseDiag (const SMDS_MeshNode * theNode1,
69                     const SMDS_MeshNode * theNode2 );
70   // Replace two neighbour triangles sharing theNode1-theNode2 link
71   // with ones built on the same 4 nodes but having other common link.
72   // Return false if proper faces not found
73
74   bool DeleteDiag (const SMDS_MeshNode * theNode1,
75                    const SMDS_MeshNode * theNode2 );
76   // Replace two neighbour triangles sharing theNode1-theNode2 link
77   // with a quadrangle built on the same 4 nodes.
78   // Return false if proper faces not found
79
80   bool Reorient (const SMDS_MeshElement * theElement);
81   // Reverse theElement orientation
82
83
84   /*!
85    * \brief Fuse neighbour triangles into quadrangles.
86    * \param theElems     - The triangles to be fused.
87    * \param theCriterion - Is used to choose a neighbour to fuse with.
88    * \param theMaxAngle  - Is a max angle between element normals at which fusion
89    *                       is still performed; theMaxAngle is mesured in radians.
90    * \retval bool - Success or not.
91    */
92   bool TriToQuad (std::set<const SMDS_MeshElement*> &  theElems,
93                   SMESH::Controls::NumericalFunctorPtr theCriterion,
94                   const double                         theMaxAngle);
95
96   /*!
97    * \brief Split quadrangles into triangles.
98    * \param theElems     - The faces to be splitted.
99    * \param theCriterion - Is used to choose a diagonal for splitting.
100    * \retval bool - Success or not.
101    */
102   bool QuadToTri (std::set<const SMDS_MeshElement*> &  theElems,
103                   SMESH::Controls::NumericalFunctorPtr theCriterion);
104
105   /*!
106    * \brief Split quadrangles into triangles.
107    * \param theElems  - The faces to be splitted.
108    * \param the13Diag - Is used to choose a diagonal for splitting.
109    * \retval bool - Success or not.
110    */
111   bool QuadToTri (std::set<const SMDS_MeshElement*> & theElems,
112                   const bool                          the13Diag);
113
114   /*!
115    * \brief Find better diagonal for splitting.
116    * \param theQuad      - The face to find better splitting of.
117    * \param theCriterion - Is used to choose a diagonal for splitting.
118    * \retval int - 1 for 1-3 diagonal, 2 for 2-4, -1 - for errors.
119    */
120   int BestSplit (const SMDS_MeshElement*              theQuad,
121                  SMESH::Controls::NumericalFunctorPtr theCriterion);
122
123
124   enum SmoothMethod { LAPLACIAN = 0, CENTROIDAL };
125
126   void Smooth (std::set<const SMDS_MeshElement*> & theElements,
127                std::set<const SMDS_MeshNode*> &    theFixedNodes,
128                const SmoothMethod                  theSmoothMethod,
129                const int                           theNbIterations,
130                double                              theTgtAspectRatio = 1.0,
131                const bool                          the2D = true);
132   // Smooth theElements using theSmoothMethod during theNbIterations
133   // or until a worst element has aspect ratio <= theTgtAspectRatio.
134   // Aspect Ratio varies in range [1.0, inf].
135   // If theElements is empty, the whole mesh is smoothed.
136   // theFixedNodes contains additionally fixed nodes. Nodes built
137   // on edges and boundary nodes are always fixed.
138   // If the2D, smoothing is performed using UV parameters of nodes
139   // on geometrical faces
140
141
142   void RotationSweep (std::set<const SMDS_MeshElement*> & theElements,
143                       const gp_Ax1&                       theAxis,
144                       const double                        theAngle,
145                       const int                           theNbSteps,
146                       const double                        theToler);
147   // Generate new elements by rotation of theElements around theAxis
148   // by theAngle by theNbSteps
149
150   /*!
151    * Auxilary flag for advanced extrusion.
152    * BOUNDARY: create or not boundary for result of extrusion
153    * SEW:      try to use existing nodes or create new nodes in any case
154    */
155   enum ExtrusionFlags {
156     EXTRUSION_FLAG_BOUNDARY = 0x01,
157     EXTRUSION_FLAG_SEW = 0x02
158   };
159   
160   /*!
161    * special structire for control of extrusion functionality
162    */
163   struct ExtrusParam {
164     gp_Dir myDir; // direction of extrusion
165     Handle(TColStd_HSequenceOfReal) mySteps; // magnitudes for each step
166     SMESH_SequenceOfNode myNodes; // nodes for using in sewing
167   };
168
169   /*!
170    * Create new node in the mesh with given coordinates
171    * (auxilary for advanced extrusion)
172    */
173   const SMDS_MeshNode* CreateNode(const double x,
174                                   const double y,
175                                   const double z,
176                                   const double tolnode,
177                                   SMESH_SequenceOfNode& aNodes);
178
179   /*!
180    * Generate new elements by extrusion of theElements
181    * It is a method used in .idl file. All functionality
182    * is implemented in the next method (see below) which
183    * is used in the cuurent method.
184    * param theElems - list of elements for extrusion
185    * param newElemsMap returns history of extrusion
186    * param theFlags set flags for performing extrusion (see description
187    *   of enum ExtrusionFlags for additional information)
188    * param theTolerance - uses for comparing locations of nodes if flag
189    *   EXTRUSION_FLAG_SEW is set
190    */
191   void ExtrusionSweep
192            (set<const SMDS_MeshElement*> & theElems,
193             const gp_Vec&                  theStep,
194             const int                      theNbSteps,
195             TElemOfElemListMap&            newElemsMap,
196             const int                      theFlags = EXTRUSION_FLAG_BOUNDARY,
197             const double                   theTolerance = 1.e-6);
198   
199   /*!
200    * Generate new elements by extrusion of theElements
201    * param theElems - list of elements for extrusion
202    * param newElemsMap returns history of extrusion
203    * param theFlags set flags for performing extrusion (see description
204    *   of enum ExtrusionFlags for additional information)
205    * param theTolerance - uses for comparing locations of nodes if flag
206    *   EXTRUSION_FLAG_SEW is set
207    * param theParams - special structure for manage of extrusion
208    */
209   void ExtrusionSweep (set<const SMDS_MeshElement*> & theElems,
210                        ExtrusParam&                   theParams,
211                        TElemOfElemListMap&            newElemsMap,
212                        const int                      theFlags,
213                        const double                   theTolerance);
214
215
216   // Generate new elements by extrusion of theElements 
217   // by theStep by theNbSteps
218
219   enum Extrusion_Error {
220     EXTR_OK,
221     EXTR_NO_ELEMENTS, 
222     EXTR_PATH_NOT_EDGE,
223     EXTR_BAD_PATH_SHAPE,
224     EXTR_BAD_STARTING_NODE,
225     EXTR_BAD_ANGLES_NUMBER,
226     EXTR_CANT_GET_TANGENT
227     };
228   
229   Extrusion_Error ExtrusionAlongTrack (std::set<const SMDS_MeshElement*> & theElements,
230                                        SMESH_subMesh*                      theTrackPattern,
231                                        const SMDS_MeshNode*                theNodeStart,
232                                        const bool                          theHasAngles,
233                                        std::list<double>&                  theAngles,
234                                        const bool                          theHasRefPoint,
235                                        const gp_Pnt&                       theRefPoint);
236   // Generate new elements by extrusion of theElements along path given by theTrackPattern,
237   // theHasAngles are the rotation angles, base point can be given by theRefPoint
238
239   void Transform (std::set<const SMDS_MeshElement*> & theElements,
240                   const gp_Trsf&                      theTrsf,
241                   const bool                          theCopy);
242   // Move or copy theElements applying theTrsf to their nodes
243
244   typedef std::list< std::list< const SMDS_MeshNode* > > TListOfListOfNodes;
245
246   void FindCoincidentNodes (std::set<const SMDS_MeshNode*> & theNodes,
247                             const double                     theTolerance,
248                             TListOfListOfNodes &             theGroupsOfNodes);
249   // Return list of group of nodes close to each other within theTolerance.
250   // Search among theNodes or in the whole mesh if theNodes is empty.
251
252   int SimplifyFace (const vector<const SMDS_MeshNode *> faceNodes,
253                     vector<const SMDS_MeshNode *>&      poly_nodes,
254                     vector<int>&                        quantities) const;
255   // Split face, defined by <faceNodes>, into several faces by repeating nodes.
256   // Is used by MergeNodes()
257
258   void MergeNodes (TListOfListOfNodes & theNodeGroups);
259   // In each group, the cdr of nodes are substituted by the first one
260   // in all elements.
261
262   void MergeEqualElements();
263   // Remove all but one of elements built on the same nodes.
264   // Return nb of successfully merged groups.
265
266   static bool CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
267                                    const SMDS_MeshNode* theNode2,
268                                    const SMDS_MeshNode* theNode3 = 0);
269   // Return true if the three nodes are on a free border
270
271   enum Sew_Error {
272     SEW_OK,
273     // for SewFreeBorder()
274     SEW_BORDER1_NOT_FOUND,
275     SEW_BORDER2_NOT_FOUND,
276     SEW_BOTH_BORDERS_NOT_FOUND,
277     SEW_BAD_SIDE_NODES,
278     SEW_VOLUMES_TO_SPLIT,
279     // for SewSideElements()
280     SEW_DIFF_NB_OF_ELEMENTS,
281     SEW_TOPO_DIFF_SETS_OF_ELEMENTS,
282     SEW_BAD_SIDE1_NODES,
283     SEW_BAD_SIDE2_NODES
284     };
285     
286
287   Sew_Error SewFreeBorder (const SMDS_MeshNode* theBorderFirstNode,
288                            const SMDS_MeshNode* theBorderSecondNode,
289                            const SMDS_MeshNode* theBorderLastNode,
290                            const SMDS_MeshNode* theSide2FirstNode,
291                            const SMDS_MeshNode* theSide2SecondNode,
292                            const SMDS_MeshNode* theSide2ThirdNode = 0,
293                            const bool           theSide2IsFreeBorder = true,
294                            const bool           toCreatePolygons = false,
295                            const bool           toCreatePolyedrs = false);
296   // Sew the free border to the side2 by replacing nodes in
297   // elements on the free border with nodes of the elements
298   // of the side 2. If nb of links in the free border and
299   // between theSide2FirstNode and theSide2LastNode are different,
300   // additional nodes are inserted on a link provided that no
301   // volume elements share the splitted link.
302   // The side 2 is a free border if theSide2IsFreeBorder == true.
303   // Sewing is peformed between the given first, second and last
304   // nodes on the sides.
305   // theBorderFirstNode is merged with theSide2FirstNode.
306   // if (!theSide2IsFreeBorder) then theSide2SecondNode gives
307   // the last node on the side 2, which will be merged with
308   // theBorderLastNode.
309   // if (theSide2IsFreeBorder) then theSide2SecondNode will
310   // be merged with theBorderSecondNode.
311   // if (theSide2IsFreeBorder && theSide2ThirdNode == 0) then
312   // the 2 free borders are sewn link by link and no additional
313   // nodes are inserted.
314   // Return false, if sewing failed.
315
316   Sew_Error SewSideElements (std::set<const SMDS_MeshElement*>& theSide1,
317                              std::set<const SMDS_MeshElement*>& theSide2,
318                              const SMDS_MeshNode*               theFirstNode1ToMerge,
319                              const SMDS_MeshNode*               theFirstNode2ToMerge,
320                              const SMDS_MeshNode*               theSecondNode1ToMerge,
321                              const SMDS_MeshNode*               theSecondNode2ToMerge);
322   // Sew two sides of a mesh. Nodes belonging to theSide1 are
323   // merged with nodes of elements of theSide2.
324   // Number of elements in theSide1 and in theSide2 must be
325   // equal and they should have similar node connectivity.
326   // The nodes to merge should belong to side s borders and
327   // the first node should be linked to the second.
328
329   void InsertNodesIntoLink(const SMDS_MeshElement*          theFace,
330                            const SMDS_MeshNode*             theBetweenNode1,
331                            const SMDS_MeshNode*             theBetweenNode2,
332                            std::list<const SMDS_MeshNode*>& theNodesToInsert,
333                            const bool                       toCreatePoly = false);
334   // insert theNodesToInsert into theFace between theBetweenNode1 and theBetweenNode2.
335   // If toCreatePoly is true, replace theFace by polygon, else split theFace.
336
337   void UpdateVolumes (const SMDS_MeshNode*             theBetweenNode1,
338                       const SMDS_MeshNode*             theBetweenNode2,
339                       std::list<const SMDS_MeshNode*>& theNodesToInsert);
340   // insert theNodesToInsert into all volumes, containing link
341   // theBetweenNode1 - theBetweenNode2, between theBetweenNode1 and theBetweenNode2.
342
343   void ConvertToQuadratic(const bool theForce3d);
344   //converts all mesh to quadratic one, deletes old elements, replacing 
345   //them with quadratic ones with the same id.
346
347   bool ConvertFromQuadratic();
348   //converts all mesh from quadratic to ordinary ones, deletes old quadratic elements, replacing 
349   //them with ordinary mesh elements with the same id.
350
351
352 //  static int SortQuadNodes (const SMDS_Mesh * theMesh,
353 //                            int               theNodeIds[] );
354 //  // Set 4 nodes of a quadrangle face in a good order.
355 //  // Swap 1<->2 or 2<->3 nodes and correspondingly return
356 //  // 1 or 2 else 0.
357 //
358 //  static bool SortHexaNodes (const SMDS_Mesh * theMesh,
359 //                             int               theNodeIds[] );
360 //  // Set 8 nodes of a hexahedron in a good order.
361 //  // Return success status
362
363   static void AddToSameGroups (const SMDS_MeshElement* elemToAdd,
364                                const SMDS_MeshElement* elemInGroups,
365                                SMESHDS_Mesh *          aMesh);
366   // Add elemToAdd to the groups the elemInGroups belongs to
367
368   static void RemoveElemFromGroups (const SMDS_MeshElement* removeelem,
369                                     SMESHDS_Mesh *          aMesh);
370   // remove elemToAdd from the groups 
371
372   static const SMDS_MeshElement*
373     FindFaceInSet(const SMDS_MeshNode*                     n1,
374                   const SMDS_MeshNode*                     n2,
375                   const std::set<const SMDS_MeshElement*>& elemSet,
376                   const std::set<const SMDS_MeshElement*>& avoidSet);
377   // Return a face having linked nodes n1 and n2 and which is
378   // - not in avoidSet,
379   // - in elemSet provided that !elemSet.empty()
380
381   /*!
382    * \brief Returns true if given node is medium
383     * \param n - node to check
384     * \param typeToCheck - type of elements containing the node to ask about node status
385     * \retval bool - check result
386    */
387   static bool IsMedium(const SMDS_MeshNode*      node,
388                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
389
390   int FindShape (const SMDS_MeshElement * theElem);
391   // Return an index of the shape theElem is on
392   // or zero if a shape not found
393
394   SMESH_Mesh * GetMesh() { return myMesh; }
395
396   SMESHDS_Mesh * GetMeshDS() { return myMesh->GetMeshDS(); }
397 private:
398
399   void ConvertElemToQuadratic(SMESHDS_SubMesh *theSm,
400                               SMESH_MesherHelper* theHelper,
401                               const bool theForce3d);
402   //Auxiliary function for "ConvertToQuadratic" is intended to convert
403   //elements contained in submesh to quadratic
404
405  private:
406
407   SMESH_Mesh * myMesh;
408
409 };
410
411 #endif