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