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