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"
39 #include "TColStd_HSequenceOfReal.hxx"
40 #include "SMESH_MesherHelper.hxx"
41 #include "SMDS_MeshElement.hxx"
48 typedef std::map<const SMDS_MeshElement*,
49 std::list<const SMDS_MeshElement*> > TElemOfElemListMap;
50 typedef std::map<const SMDS_MeshNode*, const SMDS_MeshNode*> TNodeNodeMap;
58 // ============================================================
60 * \brief Set of elements sorted by ID, to be used to assure
61 * predictability of edition
63 // ============================================================
65 template < class TMeshElem = SMDS_MeshElement>
67 bool operator () (const TMeshElem* e1, const TMeshElem* e2) const
68 { return e1->GetID() < e2->GetID(); }
70 typedef std::set< const SMDS_MeshElement*, TIDCompare< SMDS_MeshElement> > TIDSortedElemSet;
72 // ============================================================
74 * \brief Searcher for the node closest to point
76 // ============================================================
78 struct SMESH_NodeSearcher
80 virtual const SMDS_MeshNode* FindClosestTo( const gp_Pnt& pnt ) = 0;
83 // ============================================================
85 * \brief Editor of a mesh
87 // ============================================================
89 class SMESH_EXPORT SMESH_MeshEditor {
93 SMESH_MeshEditor( SMESH_Mesh* theMesh );
98 SMDS_MeshElement* AddElement(const std::vector<const SMDS_MeshNode*> & nodes,
99 const SMDSAbs_ElementType type,
105 SMDS_MeshElement* AddElement(const std::vector<int> & nodeIDs,
106 const SMDSAbs_ElementType type,
110 bool Remove (const std::list< int >& theElemIDs, const bool isNodes);
111 // Remove a node or an element.
112 // Modify a compute state of sub-meshes which become empty
114 bool InverseDiag (const SMDS_MeshElement * theTria1,
115 const SMDS_MeshElement * theTria2 );
116 // Replace two neighbour triangles with ones built on the same 4 nodes
117 // but having other common link.
118 // Return False if args are improper
120 bool InverseDiag (const SMDS_MeshNode * theNode1,
121 const SMDS_MeshNode * theNode2 );
122 // Replace two neighbour triangles sharing theNode1-theNode2 link
123 // with ones built on the same 4 nodes but having other common link.
124 // Return false if proper faces not found
126 bool DeleteDiag (const SMDS_MeshNode * theNode1,
127 const SMDS_MeshNode * theNode2 );
128 // Replace two neighbour triangles sharing theNode1-theNode2 link
129 // with a quadrangle built on the same 4 nodes.
130 // Return false if proper faces not found
132 bool Reorient (const SMDS_MeshElement * theElement);
133 // Reverse theElement orientation
137 * \brief Fuse neighbour triangles into quadrangles.
138 * \param theElems - The triangles to be fused.
139 * \param theCriterion - Is used to choose a neighbour to fuse with.
140 * \param theMaxAngle - Is a max angle between element normals at which fusion
141 * is still performed; theMaxAngle is mesured in radians.
142 * \retval bool - Success or not.
144 bool TriToQuad (TIDSortedElemSet & theElems,
145 SMESH::Controls::NumericalFunctorPtr theCriterion,
146 const double theMaxAngle);
149 * \brief Split quadrangles into triangles.
150 * \param theElems - The faces to be splitted.
151 * \param theCriterion - Is used to choose a diagonal for splitting.
152 * \retval bool - Success or not.
154 bool QuadToTri (TIDSortedElemSet & theElems,
155 SMESH::Controls::NumericalFunctorPtr theCriterion);
158 * \brief Split quadrangles into triangles.
159 * \param theElems - The faces to be splitted.
160 * \param the13Diag - Is used to choose a diagonal for splitting.
161 * \retval bool - Success or not.
163 bool QuadToTri (TIDSortedElemSet & theElems,
164 const bool the13Diag);
167 * \brief Find better diagonal for splitting.
168 * \param theQuad - The face to find better splitting of.
169 * \param theCriterion - Is used to choose a diagonal for splitting.
170 * \retval int - 1 for 1-3 diagonal, 2 for 2-4, -1 - for errors.
172 int BestSplit (const SMDS_MeshElement* theQuad,
173 SMESH::Controls::NumericalFunctorPtr theCriterion);
176 enum SmoothMethod { LAPLACIAN = 0, CENTROIDAL };
178 void Smooth (TIDSortedElemSet & theElements,
179 std::set<const SMDS_MeshNode*> & theFixedNodes,
180 const SmoothMethod theSmoothMethod,
181 const int theNbIterations,
182 double theTgtAspectRatio = 1.0,
183 const bool the2D = true);
184 // Smooth theElements using theSmoothMethod during theNbIterations
185 // or until a worst element has aspect ratio <= theTgtAspectRatio.
186 // Aspect Ratio varies in range [1.0, inf].
187 // If theElements is empty, the whole mesh is smoothed.
188 // theFixedNodes contains additionally fixed nodes. Nodes built
189 // on edges and boundary nodes are always fixed.
190 // If the2D, smoothing is performed using UV parameters of nodes
191 // on geometrical faces
193 typedef std::auto_ptr< std::list<int> > PGroupIDs;
195 PGroupIDs RotationSweep (TIDSortedElemSet & theElements,
196 const gp_Ax1& theAxis,
197 const double theAngle,
198 const int theNbSteps,
199 const double theToler,
200 const bool theMakeGroups,
201 const bool theMakeWalls=true);
202 // Generate new elements by rotation of theElements around theAxis
203 // by theAngle by theNbSteps
206 * Auxilary flag for advanced extrusion.
207 * BOUNDARY: create or not boundary for result of extrusion
208 * SEW: try to use existing nodes or create new nodes in any case
210 enum ExtrusionFlags {
211 EXTRUSION_FLAG_BOUNDARY = 0x01,
212 EXTRUSION_FLAG_SEW = 0x02
216 * special structire for control of extrusion functionality
219 gp_Dir myDir; // direction of extrusion
220 Handle(TColStd_HSequenceOfReal) mySteps; // magnitudes for each step
221 SMESH_SequenceOfNode myNodes; // nodes for using in sewing
225 * Create new node in the mesh with given coordinates
226 * (auxilary for advanced extrusion)
228 const SMDS_MeshNode* CreateNode(const double x,
231 const double tolnode,
232 SMESH_SequenceOfNode& aNodes);
235 * Generate new elements by extrusion of theElements
236 * It is a method used in .idl file. All functionality
237 * is implemented in the next method (see below) which
238 * is used in the cuurent method.
239 * param theElems - list of elements for extrusion
240 * param newElemsMap returns history of extrusion
241 * param theFlags set flags for performing extrusion (see description
242 * of enum ExtrusionFlags for additional information)
243 * param theTolerance - uses for comparing locations of nodes if flag
244 * EXTRUSION_FLAG_SEW is set
246 PGroupIDs ExtrusionSweep (TIDSortedElemSet & theElems,
247 const gp_Vec& theStep,
248 const int theNbSteps,
249 TElemOfElemListMap& newElemsMap,
250 const bool theMakeGroups,
251 const int theFlags = EXTRUSION_FLAG_BOUNDARY,
252 const double theTolerance = 1.e-6);
255 * Generate new elements by extrusion of theElements
256 * param theElems - list of elements for extrusion
257 * param newElemsMap returns history of extrusion
258 * param theFlags set flags for performing extrusion (see description
259 * of enum ExtrusionFlags for additional information)
260 * param theTolerance - uses for comparing locations of nodes if flag
261 * EXTRUSION_FLAG_SEW is set
262 * param theParams - special structure for manage of extrusion
264 PGroupIDs ExtrusionSweep (TIDSortedElemSet & theElems,
265 ExtrusParam& theParams,
266 TElemOfElemListMap& newElemsMap,
267 const bool theMakeGroups,
269 const double theTolerance);
272 // Generate new elements by extrusion of theElements
273 // by theStep by theNbSteps
275 enum Extrusion_Error {
280 EXTR_BAD_STARTING_NODE,
281 EXTR_BAD_ANGLES_NUMBER,
282 EXTR_CANT_GET_TANGENT
285 Extrusion_Error ExtrusionAlongTrack (TIDSortedElemSet & theElements,
286 SMESH_subMesh* theTrackPattern,
287 const SMDS_MeshNode* theNodeStart,
288 const bool theHasAngles,
289 std::list<double>& theAngles,
290 const bool theHasRefPoint,
291 const gp_Pnt& theRefPoint,
292 const bool theMakeGroups);
293 // Generate new elements by extrusion of theElements along path given by theTrackPattern,
294 // theHasAngles are the rotation angles, base point can be given by theRefPoint
296 PGroupIDs Transform (TIDSortedElemSet & theElements,
297 const gp_Trsf& theTrsf,
299 const bool theMakeGroups,
300 SMESH_Mesh* theTargetMesh=0);
301 // Move or copy theElements applying theTrsf to their nodes
303 typedef std::list< std::list< const SMDS_MeshNode* > > TListOfListOfNodes;
305 void FindCoincidentNodes (std::set<const SMDS_MeshNode*> & theNodes,
306 const double theTolerance,
307 TListOfListOfNodes & theGroupsOfNodes);
308 // Return list of group of nodes close to each other within theTolerance.
309 // Search among theNodes or in the whole mesh if theNodes is empty.
312 * \brief Return SMESH_NodeSearcher
314 SMESH_NodeSearcher* GetNodeSearcher();
316 int SimplifyFace (const std::vector<const SMDS_MeshNode *> faceNodes,
317 std::vector<const SMDS_MeshNode *>& poly_nodes,
318 std::vector<int>& quantities) const;
319 // Split face, defined by <faceNodes>, into several faces by repeating nodes.
320 // Is used by MergeNodes()
322 void MergeNodes (TListOfListOfNodes & theNodeGroups);
323 // In each group, the cdr of nodes are substituted by the first one
326 typedef std::list< std::list< int > > TListOfListOfElementsID;
328 void FindEqualElements(std::set<const SMDS_MeshElement*> & theElements,
329 TListOfListOfElementsID & theGroupsOfElementsID);
330 // Return list of group of elements build on the same nodes.
331 // Search among theElements or in the whole mesh if theElements is empty.
333 void MergeElements(TListOfListOfElementsID & theGroupsOfElementsID);
334 // In each group remove all but first of elements.
336 void MergeEqualElements();
337 // Remove all but one of elements built on the same nodes.
338 // Return nb of successfully merged groups.
340 static bool CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
341 const SMDS_MeshNode* theNode2,
342 const SMDS_MeshNode* theNode3 = 0);
343 // Return true if the three nodes are on a free border
345 static bool FindFreeBorder (const SMDS_MeshNode* theFirstNode,
346 const SMDS_MeshNode* theSecondNode,
347 const SMDS_MeshNode* theLastNode,
348 std::list< const SMDS_MeshNode* > & theNodes,
349 std::list< const SMDS_MeshElement* >& theFaces);
350 // Return nodes and faces of a free border if found
354 // for SewFreeBorder()
355 SEW_BORDER1_NOT_FOUND,
356 SEW_BORDER2_NOT_FOUND,
357 SEW_BOTH_BORDERS_NOT_FOUND,
359 SEW_VOLUMES_TO_SPLIT,
360 // for SewSideElements()
361 SEW_DIFF_NB_OF_ELEMENTS,
362 SEW_TOPO_DIFF_SETS_OF_ELEMENTS,
369 Sew_Error SewFreeBorder (const SMDS_MeshNode* theBorderFirstNode,
370 const SMDS_MeshNode* theBorderSecondNode,
371 const SMDS_MeshNode* theBorderLastNode,
372 const SMDS_MeshNode* theSide2FirstNode,
373 const SMDS_MeshNode* theSide2SecondNode,
374 const SMDS_MeshNode* theSide2ThirdNode = 0,
375 const bool theSide2IsFreeBorder = true,
376 const bool toCreatePolygons = false,
377 const bool toCreatePolyedrs = false);
378 // Sew the free border to the side2 by replacing nodes in
379 // elements on the free border with nodes of the elements
380 // of the side 2. If nb of links in the free border and
381 // between theSide2FirstNode and theSide2LastNode are different,
382 // additional nodes are inserted on a link provided that no
383 // volume elements share the splitted link.
384 // The side 2 is a free border if theSide2IsFreeBorder == true.
385 // Sewing is peformed between the given first, second and last
386 // nodes on the sides.
387 // theBorderFirstNode is merged with theSide2FirstNode.
388 // if (!theSide2IsFreeBorder) then theSide2SecondNode gives
389 // the last node on the side 2, which will be merged with
390 // theBorderLastNode.
391 // if (theSide2IsFreeBorder) then theSide2SecondNode will
392 // be merged with theBorderSecondNode.
393 // if (theSide2IsFreeBorder && theSide2ThirdNode == 0) then
394 // the 2 free borders are sewn link by link and no additional
395 // nodes are inserted.
396 // Return false, if sewing failed.
398 Sew_Error SewSideElements (TIDSortedElemSet& theSide1,
399 TIDSortedElemSet& theSide2,
400 const SMDS_MeshNode* theFirstNode1ToMerge,
401 const SMDS_MeshNode* theFirstNode2ToMerge,
402 const SMDS_MeshNode* theSecondNode1ToMerge,
403 const SMDS_MeshNode* theSecondNode2ToMerge);
404 // Sew two sides of a mesh. Nodes belonging to theSide1 are
405 // merged with nodes of elements of theSide2.
406 // Number of elements in theSide1 and in theSide2 must be
407 // equal and they should have similar node connectivity.
408 // The nodes to merge should belong to side s borders and
409 // the first node should be linked to the second.
411 void InsertNodesIntoLink(const SMDS_MeshElement* theFace,
412 const SMDS_MeshNode* theBetweenNode1,
413 const SMDS_MeshNode* theBetweenNode2,
414 std::list<const SMDS_MeshNode*>& theNodesToInsert,
415 const bool toCreatePoly = false);
416 // insert theNodesToInsert into theFace between theBetweenNode1 and theBetweenNode2.
417 // If toCreatePoly is true, replace theFace by polygon, else split theFace.
419 void UpdateVolumes (const SMDS_MeshNode* theBetweenNode1,
420 const SMDS_MeshNode* theBetweenNode2,
421 std::list<const SMDS_MeshNode*>& theNodesToInsert);
422 // insert theNodesToInsert into all volumes, containing link
423 // theBetweenNode1 - theBetweenNode2, between theBetweenNode1 and theBetweenNode2.
425 void ConvertToQuadratic(const bool theForce3d);
426 //converts all mesh to quadratic one, deletes old elements, replacing
427 //them with quadratic ones with the same id.
429 bool ConvertFromQuadratic();
430 //converts all mesh from quadratic to ordinary ones, deletes old quadratic elements, replacing
431 //them with ordinary mesh elements with the same id.
434 // static int SortQuadNodes (const SMDS_Mesh * theMesh,
435 // int theNodeIds[] );
436 // // Set 4 nodes of a quadrangle face in a good order.
437 // // Swap 1<->2 or 2<->3 nodes and correspondingly return
440 // static bool SortHexaNodes (const SMDS_Mesh * theMesh,
441 // int theNodeIds[] );
442 // // Set 8 nodes of a hexahedron in a good order.
443 // // Return success status
445 static void AddToSameGroups (const SMDS_MeshElement* elemToAdd,
446 const SMDS_MeshElement* elemInGroups,
447 SMESHDS_Mesh * aMesh);
448 // Add elemToAdd to the groups the elemInGroups belongs to
450 static void RemoveElemFromGroups (const SMDS_MeshElement* removeelem,
451 SMESHDS_Mesh * aMesh);
452 // remove elemToAdd from the groups
455 * \brief Return nodes linked to the given one in elements of the type
457 static void GetLinkedNodes( const SMDS_MeshNode* node,
458 TIDSortedElemSet & linkedNodes,
459 SMDSAbs_ElementType type = SMDSAbs_All );
461 static const SMDS_MeshElement*
462 FindFaceInSet(const SMDS_MeshNode* n1,
463 const SMDS_MeshNode* n2,
464 const TIDSortedElemSet& elemSet,
465 const TIDSortedElemSet& avoidSet);
466 // Return a face having linked nodes n1 and n2 and which is
467 // - not in avoidSet,
468 // - in elemSet provided that !elemSet.empty()
471 * \brief Find corresponding nodes in two sets of faces
472 * \param theSide1 - first face set
473 * \param theSide2 - second first face
474 * \param theFirstNode1 - a boundary node of set 1
475 * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
476 * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
477 * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
478 * \param nReplaceMap - output map of corresponding nodes
479 * \retval Sew_Error - is a success or not
481 static Sew_Error FindMatchingNodes(std::set<const SMDS_MeshElement*>& theSide1,
482 std::set<const SMDS_MeshElement*>& theSide2,
483 const SMDS_MeshNode* theFirstNode1,
484 const SMDS_MeshNode* theFirstNode2,
485 const SMDS_MeshNode* theSecondNode1,
486 const SMDS_MeshNode* theSecondNode2,
487 TNodeNodeMap & nReplaceMap);
490 * \brief Returns true if given node is medium
491 * \param n - node to check
492 * \param typeToCheck - type of elements containing the node to ask about node status
493 * \retval bool - check result
495 static bool IsMedium(const SMDS_MeshNode* node,
496 const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
498 int FindShape (const SMDS_MeshElement * theElem);
499 // Return an index of the shape theElem is on
500 // or zero if a shape not found
502 SMESH_Mesh * GetMesh() { return myMesh; }
504 SMESHDS_Mesh * GetMeshDS() { return myMesh->GetMeshDS(); }
506 const SMESH_SequenceOfElemPtr& GetLastCreatedNodes() const { return myLastCreatedNodes; }
508 const SMESH_SequenceOfElemPtr& GetLastCreatedElems() const { return myLastCreatedElems; }
513 * \brief Convert elements contained in a submesh to quadratic
514 * \retval int - nb of checked elements
516 int convertElemToQuadratic(SMESHDS_SubMesh * theSm,
517 SMESH_MesherHelper& theHelper,
518 const bool theForce3d);
521 * \brief Convert quadratic elements to linear ones and remove quadratic nodes
522 * \retval int - nb of checked elements
524 int removeQuadElem( SMESHDS_SubMesh * theSm,
525 SMDS_ElemIteratorPtr theItr,
526 const int theShapeID);
528 * \brief Create groups of elements made during transformation
529 * \param nodeGens - nodes making corresponding myLastCreatedNodes
530 * \param elemGens - elements making corresponding myLastCreatedElems
531 * \param postfix - to append to names of new groups
533 PGroupIDs generateGroups(const SMESH_SequenceOfElemPtr& nodeGens,
534 const SMESH_SequenceOfElemPtr& elemGens,
535 const std::string& postfix,
536 SMESH_Mesh* targetMesh=0);
539 typedef std::map<const SMDS_MeshNode*, std::list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
540 typedef TNodeOfNodeListMap::iterator TNodeOfNodeListMapItr;
541 typedef std::vector<TNodeOfNodeListMapItr> TVecOfNnlmiMap;
542 typedef std::map<const SMDS_MeshElement*, TVecOfNnlmiMap > TElemOfVecOfNnlmiMap;
545 * \brief Create elements by sweeping an element
546 * \param elem - element to sweep
547 * \param newNodesItVec - nodes generated from each node of the element
548 * \param newElems - generated elements
549 * \param nbSteps - number of sweeping steps
550 * \param srcElements - to append elem for each generated element
552 void sweepElement(const SMDS_MeshElement* elem,
553 const std::vector<TNodeOfNodeListMapItr> & newNodesItVec,
554 std::list<const SMDS_MeshElement*>& newElems,
556 SMESH_SequenceOfElemPtr& srcElements);
559 * \brief Create 1D and 2D elements around swept elements
560 * \param mapNewNodes - source nodes and ones generated from them
561 * \param newElemsMap - source elements and ones generated from them
562 * \param elemNewNodesMap - nodes generated from each node of each element
563 * \param elemSet - all swept elements
564 * \param nbSteps - number of sweeping steps
565 * \param srcElements - to append elem for each generated element
567 void makeWalls (TNodeOfNodeListMap & mapNewNodes,
568 TElemOfElemListMap & newElemsMap,
569 TElemOfVecOfNnlmiMap & elemNewNodesMap,
570 TIDSortedElemSet& elemSet,
572 SMESH_SequenceOfElemPtr& srcElements);
578 * Sequence for keeping nodes created during last operation
580 SMESH_SequenceOfElemPtr myLastCreatedNodes;
583 * Sequence for keeping elements created during last operation
585 SMESH_SequenceOfElemPtr myLastCreatedElems;