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