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;
53 typedef pair< const SMDS_MeshNode*, const SMDS_MeshNode* > NLink;
55 //=======================================================================
57 * \brief A sorted pair of nodes
59 //=======================================================================
61 struct SMESH_TLink: public NLink
63 SMESH_TLink(const SMDS_MeshNode* n1, const SMDS_MeshNode* n2 ):NLink( n1, n2 )
64 { if ( n1->GetID() < n2->GetID() ) std::swap( first, second ); }
65 SMESH_TLink(const NLink& link ):NLink( link )
66 { if ( first->GetID() < second->GetID() ) std::swap( first, second ); }
76 // ============================================================
78 * \brief Set of elements sorted by ID, to be used to assure
79 * predictability of edition
81 // ============================================================
83 template < class TMeshElem = SMDS_MeshElement>
85 bool operator () (const TMeshElem* e1, const TMeshElem* e2) const
86 { return e1->GetID() < e2->GetID(); }
88 typedef std::set< const SMDS_MeshElement*, TIDCompare< SMDS_MeshElement> > TIDSortedElemSet;
90 // ============================================================
92 * \brief Searcher for the node closest to point
94 // ============================================================
96 struct SMESH_NodeSearcher
98 virtual const SMDS_MeshNode* FindClosestTo( const gp_Pnt& pnt ) = 0;
101 // ============================================================
103 * \brief Editor of a mesh
105 // ============================================================
107 class SMESH_EXPORT SMESH_MeshEditor {
111 SMESH_MeshEditor( SMESH_Mesh* theMesh );
116 SMDS_MeshElement* AddElement(const std::vector<const SMDS_MeshNode*> & nodes,
117 const SMDSAbs_ElementType type,
123 SMDS_MeshElement* AddElement(const std::vector<int> & nodeIDs,
124 const SMDSAbs_ElementType type,
128 bool Remove (const std::list< int >& theElemIDs, const bool isNodes);
129 // Remove a node or an element.
130 // Modify a compute state of sub-meshes which become empty
132 bool InverseDiag (const SMDS_MeshElement * theTria1,
133 const SMDS_MeshElement * theTria2 );
134 // Replace two neighbour triangles with ones built on the same 4 nodes
135 // but having other common link.
136 // Return False if args are improper
138 bool InverseDiag (const SMDS_MeshNode * theNode1,
139 const SMDS_MeshNode * theNode2 );
140 // Replace two neighbour triangles sharing theNode1-theNode2 link
141 // with ones built on the same 4 nodes but having other common link.
142 // Return false if proper faces not found
144 bool DeleteDiag (const SMDS_MeshNode * theNode1,
145 const SMDS_MeshNode * theNode2 );
146 // Replace two neighbour triangles sharing theNode1-theNode2 link
147 // with a quadrangle built on the same 4 nodes.
148 // Return false if proper faces not found
150 bool Reorient (const SMDS_MeshElement * theElement);
151 // Reverse theElement orientation
155 * \brief Fuse neighbour triangles into quadrangles.
156 * \param theElems - The triangles to be fused.
157 * \param theCriterion - Is used to choose a neighbour to fuse with.
158 * \param theMaxAngle - Is a max angle between element normals at which fusion
159 * is still performed; theMaxAngle is mesured in radians.
160 * \retval bool - Success or not.
162 bool TriToQuad (TIDSortedElemSet & theElems,
163 SMESH::Controls::NumericalFunctorPtr theCriterion,
164 const double theMaxAngle);
167 * \brief Split quadrangles into triangles.
168 * \param theElems - The faces to be splitted.
169 * \param theCriterion - Is used to choose a diagonal for splitting.
170 * \retval bool - Success or not.
172 bool QuadToTri (TIDSortedElemSet & theElems,
173 SMESH::Controls::NumericalFunctorPtr theCriterion);
176 * \brief Split quadrangles into triangles.
177 * \param theElems - The faces to be splitted.
178 * \param the13Diag - Is used to choose a diagonal for splitting.
179 * \retval bool - Success or not.
181 bool QuadToTri (TIDSortedElemSet & theElems,
182 const bool the13Diag);
185 * \brief Find better diagonal for splitting.
186 * \param theQuad - The face to find better splitting of.
187 * \param theCriterion - Is used to choose a diagonal for splitting.
188 * \retval int - 1 for 1-3 diagonal, 2 for 2-4, -1 - for errors.
190 int BestSplit (const SMDS_MeshElement* theQuad,
191 SMESH::Controls::NumericalFunctorPtr theCriterion);
194 enum SmoothMethod { LAPLACIAN = 0, CENTROIDAL };
196 void Smooth (TIDSortedElemSet & theElements,
197 std::set<const SMDS_MeshNode*> & theFixedNodes,
198 const SmoothMethod theSmoothMethod,
199 const int theNbIterations,
200 double theTgtAspectRatio = 1.0,
201 const bool the2D = true);
202 // Smooth theElements using theSmoothMethod during theNbIterations
203 // or until a worst element has aspect ratio <= theTgtAspectRatio.
204 // Aspect Ratio varies in range [1.0, inf].
205 // If theElements is empty, the whole mesh is smoothed.
206 // theFixedNodes contains additionally fixed nodes. Nodes built
207 // on edges and boundary nodes are always fixed.
208 // If the2D, smoothing is performed using UV parameters of nodes
209 // on geometrical faces
211 typedef std::auto_ptr< std::list<int> > PGroupIDs;
213 PGroupIDs RotationSweep (TIDSortedElemSet & theElements,
214 const gp_Ax1& theAxis,
215 const double theAngle,
216 const int theNbSteps,
217 const double theToler,
218 const bool theMakeGroups,
219 const bool theMakeWalls=true);
220 // Generate new elements by rotation of theElements around theAxis
221 // by theAngle by theNbSteps
224 * Auxilary flag for advanced extrusion.
225 * BOUNDARY: create or not boundary for result of extrusion
226 * SEW: try to use existing nodes or create new nodes in any case
228 enum ExtrusionFlags {
229 EXTRUSION_FLAG_BOUNDARY = 0x01,
230 EXTRUSION_FLAG_SEW = 0x02
234 * special structire for control of extrusion functionality
237 gp_Dir myDir; // direction of extrusion
238 Handle(TColStd_HSequenceOfReal) mySteps; // magnitudes for each step
239 SMESH_SequenceOfNode myNodes; // nodes for using in sewing
243 * Create new node in the mesh with given coordinates
244 * (auxilary for advanced extrusion)
246 const SMDS_MeshNode* CreateNode(const double x,
249 const double tolnode,
250 SMESH_SequenceOfNode& aNodes);
253 * Generate new elements by extrusion of theElements
254 * It is a method used in .idl file. All functionality
255 * is implemented in the next method (see below) which
256 * is used in the cuurent method.
257 * param theElems - list of elements for extrusion
258 * param newElemsMap returns history of extrusion
259 * param theFlags set flags for performing extrusion (see description
260 * of enum ExtrusionFlags for additional information)
261 * param theTolerance - uses for comparing locations of nodes if flag
262 * EXTRUSION_FLAG_SEW is set
264 PGroupIDs ExtrusionSweep (TIDSortedElemSet & theElems,
265 const gp_Vec& theStep,
266 const int theNbSteps,
267 TElemOfElemListMap& newElemsMap,
268 const bool theMakeGroups,
269 const int theFlags = EXTRUSION_FLAG_BOUNDARY,
270 const double theTolerance = 1.e-6);
273 * Generate new elements by extrusion of theElements
274 * param theElems - list of elements for extrusion
275 * param newElemsMap returns history of extrusion
276 * param theFlags set flags for performing extrusion (see description
277 * of enum ExtrusionFlags for additional information)
278 * param theTolerance - uses for comparing locations of nodes if flag
279 * EXTRUSION_FLAG_SEW is set
280 * param theParams - special structure for manage of extrusion
282 PGroupIDs ExtrusionSweep (TIDSortedElemSet & theElems,
283 ExtrusParam& theParams,
284 TElemOfElemListMap& newElemsMap,
285 const bool theMakeGroups,
287 const double theTolerance);
290 // Generate new elements by extrusion of theElements
291 // by theStep by theNbSteps
293 enum Extrusion_Error {
298 EXTR_BAD_STARTING_NODE,
299 EXTR_BAD_ANGLES_NUMBER,
300 EXTR_CANT_GET_TANGENT
303 Extrusion_Error ExtrusionAlongTrack (TIDSortedElemSet & theElements,
304 SMESH_subMesh* theTrackPattern,
305 const SMDS_MeshNode* theNodeStart,
306 const bool theHasAngles,
307 std::list<double>& theAngles,
308 const bool theHasRefPoint,
309 const gp_Pnt& theRefPoint,
310 const bool theMakeGroups);
311 // Generate new elements by extrusion of theElements along path given by theTrackPattern,
312 // theHasAngles are the rotation angles, base point can be given by theRefPoint
314 PGroupIDs Transform (TIDSortedElemSet & theElements,
315 const gp_Trsf& theTrsf,
317 const bool theMakeGroups,
318 SMESH_Mesh* theTargetMesh=0);
319 // Move or copy theElements applying theTrsf to their nodes
321 typedef std::list< std::list< const SMDS_MeshNode* > > TListOfListOfNodes;
323 void FindCoincidentNodes (std::set<const SMDS_MeshNode*> & theNodes,
324 const double theTolerance,
325 TListOfListOfNodes & theGroupsOfNodes);
326 // Return list of group of nodes close to each other within theTolerance.
327 // Search among theNodes or in the whole mesh if theNodes is empty.
330 * \brief Return SMESH_NodeSearcher
332 SMESH_NodeSearcher* GetNodeSearcher();
334 int SimplifyFace (const std::vector<const SMDS_MeshNode *> faceNodes,
335 std::vector<const SMDS_MeshNode *>& poly_nodes,
336 std::vector<int>& quantities) const;
337 // Split face, defined by <faceNodes>, into several faces by repeating nodes.
338 // Is used by MergeNodes()
340 void MergeNodes (TListOfListOfNodes & theNodeGroups);
341 // In each group, the cdr of nodes are substituted by the first one
344 typedef std::list< std::list< int > > TListOfListOfElementsID;
346 void FindEqualElements(std::set<const SMDS_MeshElement*> & theElements,
347 TListOfListOfElementsID & theGroupsOfElementsID);
348 // Return list of group of elements build on the same nodes.
349 // Search among theElements or in the whole mesh if theElements is empty.
351 void MergeElements(TListOfListOfElementsID & theGroupsOfElementsID);
352 // In each group remove all but first of elements.
354 void MergeEqualElements();
355 // Remove all but one of elements built on the same nodes.
356 // Return nb of successfully merged groups.
358 static bool CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
359 const SMDS_MeshNode* theNode2,
360 const SMDS_MeshNode* theNode3 = 0);
361 // Return true if the three nodes are on a free border
363 static bool FindFreeBorder (const SMDS_MeshNode* theFirstNode,
364 const SMDS_MeshNode* theSecondNode,
365 const SMDS_MeshNode* theLastNode,
366 std::list< const SMDS_MeshNode* > & theNodes,
367 std::list< const SMDS_MeshElement* >& theFaces);
368 // Return nodes and faces of a free border if found
372 // for SewFreeBorder()
373 SEW_BORDER1_NOT_FOUND,
374 SEW_BORDER2_NOT_FOUND,
375 SEW_BOTH_BORDERS_NOT_FOUND,
377 SEW_VOLUMES_TO_SPLIT,
378 // for SewSideElements()
379 SEW_DIFF_NB_OF_ELEMENTS,
380 SEW_TOPO_DIFF_SETS_OF_ELEMENTS,
387 Sew_Error SewFreeBorder (const SMDS_MeshNode* theBorderFirstNode,
388 const SMDS_MeshNode* theBorderSecondNode,
389 const SMDS_MeshNode* theBorderLastNode,
390 const SMDS_MeshNode* theSide2FirstNode,
391 const SMDS_MeshNode* theSide2SecondNode,
392 const SMDS_MeshNode* theSide2ThirdNode = 0,
393 const bool theSide2IsFreeBorder = true,
394 const bool toCreatePolygons = false,
395 const bool toCreatePolyedrs = false);
396 // Sew the free border to the side2 by replacing nodes in
397 // elements on the free border with nodes of the elements
398 // of the side 2. If nb of links in the free border and
399 // between theSide2FirstNode and theSide2LastNode are different,
400 // additional nodes are inserted on a link provided that no
401 // volume elements share the splitted link.
402 // The side 2 is a free border if theSide2IsFreeBorder == true.
403 // Sewing is peformed between the given first, second and last
404 // nodes on the sides.
405 // theBorderFirstNode is merged with theSide2FirstNode.
406 // if (!theSide2IsFreeBorder) then theSide2SecondNode gives
407 // the last node on the side 2, which will be merged with
408 // theBorderLastNode.
409 // if (theSide2IsFreeBorder) then theSide2SecondNode will
410 // be merged with theBorderSecondNode.
411 // if (theSide2IsFreeBorder && theSide2ThirdNode == 0) then
412 // the 2 free borders are sewn link by link and no additional
413 // nodes are inserted.
414 // Return false, if sewing failed.
416 Sew_Error SewSideElements (TIDSortedElemSet& theSide1,
417 TIDSortedElemSet& theSide2,
418 const SMDS_MeshNode* theFirstNode1ToMerge,
419 const SMDS_MeshNode* theFirstNode2ToMerge,
420 const SMDS_MeshNode* theSecondNode1ToMerge,
421 const SMDS_MeshNode* theSecondNode2ToMerge);
422 // Sew two sides of a mesh. Nodes belonging to theSide1 are
423 // merged with nodes of elements of theSide2.
424 // Number of elements in theSide1 and in theSide2 must be
425 // equal and they should have similar node connectivity.
426 // The nodes to merge should belong to side s borders and
427 // the first node should be linked to the second.
429 void InsertNodesIntoLink(const SMDS_MeshElement* theFace,
430 const SMDS_MeshNode* theBetweenNode1,
431 const SMDS_MeshNode* theBetweenNode2,
432 std::list<const SMDS_MeshNode*>& theNodesToInsert,
433 const bool toCreatePoly = false);
434 // insert theNodesToInsert into theFace between theBetweenNode1 and theBetweenNode2.
435 // If toCreatePoly is true, replace theFace by polygon, else split theFace.
437 void UpdateVolumes (const SMDS_MeshNode* theBetweenNode1,
438 const SMDS_MeshNode* theBetweenNode2,
439 std::list<const SMDS_MeshNode*>& theNodesToInsert);
440 // insert theNodesToInsert into all volumes, containing link
441 // theBetweenNode1 - theBetweenNode2, between theBetweenNode1 and theBetweenNode2.
443 void ConvertToQuadratic(const bool theForce3d);
444 //converts all mesh to quadratic one, deletes old elements, replacing
445 //them with quadratic ones with the same id.
447 bool ConvertFromQuadratic();
448 //converts all mesh from quadratic to ordinary ones, deletes old quadratic elements, replacing
449 //them with ordinary mesh elements with the same id.
452 // static int SortQuadNodes (const SMDS_Mesh * theMesh,
453 // int theNodeIds[] );
454 // // Set 4 nodes of a quadrangle face in a good order.
455 // // Swap 1<->2 or 2<->3 nodes and correspondingly return
458 // static bool SortHexaNodes (const SMDS_Mesh * theMesh,
459 // int theNodeIds[] );
460 // // Set 8 nodes of a hexahedron in a good order.
461 // // Return success status
463 static void AddToSameGroups (const SMDS_MeshElement* elemToAdd,
464 const SMDS_MeshElement* elemInGroups,
465 SMESHDS_Mesh * aMesh);
466 // Add elemToAdd to the groups the elemInGroups belongs to
468 static void RemoveElemFromGroups (const SMDS_MeshElement* removeelem,
469 SMESHDS_Mesh * aMesh);
470 // remove elemToAdd from the groups
473 * \brief Return nodes linked to the given one in elements of the type
475 static void GetLinkedNodes( const SMDS_MeshNode* node,
476 TIDSortedElemSet & linkedNodes,
477 SMDSAbs_ElementType type = SMDSAbs_All );
479 static const SMDS_MeshElement*
480 FindFaceInSet(const SMDS_MeshNode* n1,
481 const SMDS_MeshNode* n2,
482 const TIDSortedElemSet& elemSet,
483 const TIDSortedElemSet& avoidSet);
484 // Return a face having linked nodes n1 and n2 and which is
485 // - not in avoidSet,
486 // - in elemSet provided that !elemSet.empty()
489 * \brief Find corresponding nodes in two sets of faces
490 * \param theSide1 - first face set
491 * \param theSide2 - second first face
492 * \param theFirstNode1 - a boundary node of set 1
493 * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
494 * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
495 * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
496 * \param nReplaceMap - output map of corresponding nodes
497 * \retval Sew_Error - is a success or not
499 static Sew_Error FindMatchingNodes(std::set<const SMDS_MeshElement*>& theSide1,
500 std::set<const SMDS_MeshElement*>& theSide2,
501 const SMDS_MeshNode* theFirstNode1,
502 const SMDS_MeshNode* theFirstNode2,
503 const SMDS_MeshNode* theSecondNode1,
504 const SMDS_MeshNode* theSecondNode2,
505 TNodeNodeMap & nReplaceMap);
508 * \brief Returns true if given node is medium
509 * \param n - node to check
510 * \param typeToCheck - type of elements containing the node to ask about node status
511 * \retval bool - check result
513 static bool IsMedium(const SMDS_MeshNode* node,
514 const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
516 int FindShape (const SMDS_MeshElement * theElem);
517 // Return an index of the shape theElem is on
518 // or zero if a shape not found
520 SMESH_Mesh * GetMesh() { return myMesh; }
522 SMESHDS_Mesh * GetMeshDS() { return myMesh->GetMeshDS(); }
524 const SMESH_SequenceOfElemPtr& GetLastCreatedNodes() const { return myLastCreatedNodes; }
526 const SMESH_SequenceOfElemPtr& GetLastCreatedElems() const { return myLastCreatedElems; }
528 bool DoubleNodes( const std::list< int >& theListOfNodes,
529 const std::list< int >& theListOfModifiedElems );
534 * \brief Convert elements contained in a submesh to quadratic
535 * \retval int - nb of checked elements
537 int convertElemToQuadratic(SMESHDS_SubMesh * theSm,
538 SMESH_MesherHelper& theHelper,
539 const bool theForce3d);
542 * \brief Convert quadratic elements to linear ones and remove quadratic nodes
543 * \retval int - nb of checked elements
545 int removeQuadElem( SMESHDS_SubMesh * theSm,
546 SMDS_ElemIteratorPtr theItr,
547 const int theShapeID);
549 * \brief Create groups of elements made during transformation
550 * \param nodeGens - nodes making corresponding myLastCreatedNodes
551 * \param elemGens - elements making corresponding myLastCreatedElems
552 * \param postfix - to append to names of new groups
554 PGroupIDs generateGroups(const SMESH_SequenceOfElemPtr& nodeGens,
555 const SMESH_SequenceOfElemPtr& elemGens,
556 const std::string& postfix,
557 SMESH_Mesh* targetMesh=0);
560 typedef std::map<const SMDS_MeshNode*, std::list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
561 typedef TNodeOfNodeListMap::iterator TNodeOfNodeListMapItr;
562 typedef std::vector<TNodeOfNodeListMapItr> TVecOfNnlmiMap;
563 typedef std::map<const SMDS_MeshElement*, TVecOfNnlmiMap > TElemOfVecOfNnlmiMap;
566 * \brief Create elements by sweeping an element
567 * \param elem - element to sweep
568 * \param newNodesItVec - nodes generated from each node of the element
569 * \param newElems - generated elements
570 * \param nbSteps - number of sweeping steps
571 * \param srcElements - to append elem for each generated element
573 void sweepElement(const SMDS_MeshElement* elem,
574 const std::vector<TNodeOfNodeListMapItr> & newNodesItVec,
575 std::list<const SMDS_MeshElement*>& newElems,
577 SMESH_SequenceOfElemPtr& srcElements);
580 * \brief Create 1D and 2D elements around swept elements
581 * \param mapNewNodes - source nodes and ones generated from them
582 * \param newElemsMap - source elements and ones generated from them
583 * \param elemNewNodesMap - nodes generated from each node of each element
584 * \param elemSet - all swept elements
585 * \param nbSteps - number of sweeping steps
586 * \param srcElements - to append elem for each generated element
588 void makeWalls (TNodeOfNodeListMap & mapNewNodes,
589 TElemOfElemListMap & newElemsMap,
590 TElemOfVecOfNnlmiMap & elemNewNodesMap,
591 TIDSortedElemSet& elemSet,
593 SMESH_SequenceOfElemPtr& srcElements);
599 * Sequence for keeping nodes created during last operation
601 SMESH_SequenceOfElemPtr myLastCreatedNodes;
604 * Sequence for keeping elements created during last operation
606 SMESH_SequenceOfElemPtr myLastCreatedElems;