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