1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
23 // File : SMESH_MeshEditor.hxx
24 // Created : Mon Apr 12 14:56:19 2004
25 // Author : Edward AGAPOV (eap)
28 #ifndef SMESH_MeshEditor_HeaderFile
29 #define SMESH_MeshEditor_HeaderFile
31 #include "SMESH_SMESH.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_Controls.hxx"
35 #include "SMESH_SequenceOfNode.hxx"
36 #include "SMESH_SequenceOfElemPtr.hxx"
37 #include "TColStd_HSequenceOfReal.hxx"
38 #include "SMESH_MesherHelper.hxx"
39 #include "SMDS_MeshElement.hxx"
46 typedef std::map<const SMDS_MeshElement*,
47 std::list<const SMDS_MeshElement*> > TElemOfElemListMap;
48 typedef std::map<const SMDS_MeshNode*, const SMDS_MeshNode*> TNodeNodeMap;
51 typedef pair< const SMDS_MeshNode*, const SMDS_MeshNode* > NLink;
53 //=======================================================================
55 * \brief A sorted pair of nodes
57 //=======================================================================
59 struct SMESH_TLink: public NLink
61 SMESH_TLink(const SMDS_MeshNode* n1, const SMDS_MeshNode* n2 ):NLink( n1, n2 )
62 { if ( n1->GetID() < n2->GetID() ) std::swap( first, second ); }
63 SMESH_TLink(const NLink& link ):NLink( link )
64 { if ( first->GetID() < second->GetID() ) std::swap( first, second ); }
74 // ============================================================
76 * \brief Set of elements sorted by ID, to be used to assure
77 * predictability of edition
79 // ============================================================
81 template < class TMeshElem = SMDS_MeshElement>
83 bool operator () (const TMeshElem* e1, const TMeshElem* e2) const
84 { return e1->GetID() < e2->GetID(); }
86 typedef std::set< const SMDS_MeshElement*, TIDCompare< SMDS_MeshElement> > TIDSortedElemSet;
88 // ============================================================
90 * \brief Searcher for the node closest to point
92 // ============================================================
94 struct SMESH_NodeSearcher
96 virtual const SMDS_MeshNode* FindClosestTo( const gp_Pnt& pnt ) = 0;
99 // ============================================================
101 * \brief Editor of a mesh
103 // ============================================================
105 class SMESH_EXPORT SMESH_MeshEditor {
109 SMESH_MeshEditor( SMESH_Mesh* theMesh );
114 SMDS_MeshElement* AddElement(const std::vector<const SMDS_MeshNode*> & nodes,
115 const SMDSAbs_ElementType type,
121 SMDS_MeshElement* AddElement(const std::vector<int> & nodeIDs,
122 const SMDSAbs_ElementType type,
126 bool Remove (const std::list< int >& theElemIDs, const bool isNodes);
127 // Remove a node or an element.
128 // Modify a compute state of sub-meshes which become empty
130 bool InverseDiag (const SMDS_MeshElement * theTria1,
131 const SMDS_MeshElement * theTria2 );
132 // Replace two neighbour triangles with ones built on the same 4 nodes
133 // but having other common link.
134 // Return False if args are improper
136 bool InverseDiag (const SMDS_MeshNode * theNode1,
137 const SMDS_MeshNode * theNode2 );
138 // Replace two neighbour triangles sharing theNode1-theNode2 link
139 // with ones built on the same 4 nodes but having other common link.
140 // Return false if proper faces not found
142 bool DeleteDiag (const SMDS_MeshNode * theNode1,
143 const SMDS_MeshNode * theNode2 );
144 // Replace two neighbour triangles sharing theNode1-theNode2 link
145 // with a quadrangle built on the same 4 nodes.
146 // Return false if proper faces not found
148 bool Reorient (const SMDS_MeshElement * theElement);
149 // Reverse theElement orientation
153 * \brief Fuse neighbour triangles into quadrangles.
154 * \param theElems - The triangles to be fused.
155 * \param theCriterion - Is used to choose a neighbour to fuse with.
156 * \param theMaxAngle - Is a max angle between element normals at which fusion
157 * is still performed; theMaxAngle is mesured in radians.
158 * \retval bool - Success or not.
160 bool TriToQuad (TIDSortedElemSet & theElems,
161 SMESH::Controls::NumericalFunctorPtr theCriterion,
162 const double theMaxAngle);
165 * \brief Split quadrangles into triangles.
166 * \param theElems - The faces to be splitted.
167 * \param theCriterion - Is used to choose a diagonal for splitting.
168 * \retval bool - Success or not.
170 bool QuadToTri (TIDSortedElemSet & theElems,
171 SMESH::Controls::NumericalFunctorPtr theCriterion);
174 * \brief Split quadrangles into triangles.
175 * \param theElems - The faces to be splitted.
176 * \param the13Diag - Is used to choose a diagonal for splitting.
177 * \retval bool - Success or not.
179 bool QuadToTri (TIDSortedElemSet & theElems,
180 const bool the13Diag);
183 * \brief Find better diagonal for splitting.
184 * \param theQuad - The face to find better splitting of.
185 * \param theCriterion - Is used to choose a diagonal for splitting.
186 * \retval int - 1 for 1-3 diagonal, 2 for 2-4, -1 - for errors.
188 int BestSplit (const SMDS_MeshElement* theQuad,
189 SMESH::Controls::NumericalFunctorPtr theCriterion);
192 enum SmoothMethod { LAPLACIAN = 0, CENTROIDAL };
194 void Smooth (TIDSortedElemSet & theElements,
195 std::set<const SMDS_MeshNode*> & theFixedNodes,
196 const SmoothMethod theSmoothMethod,
197 const int theNbIterations,
198 double theTgtAspectRatio = 1.0,
199 const bool the2D = true);
200 // Smooth theElements using theSmoothMethod during theNbIterations
201 // or until a worst element has aspect ratio <= theTgtAspectRatio.
202 // Aspect Ratio varies in range [1.0, inf].
203 // If theElements is empty, the whole mesh is smoothed.
204 // theFixedNodes contains additionally fixed nodes. Nodes built
205 // on edges and boundary nodes are always fixed.
206 // If the2D, smoothing is performed using UV parameters of nodes
207 // on geometrical faces
209 typedef std::auto_ptr< std::list<int> > PGroupIDs;
211 PGroupIDs RotationSweep (TIDSortedElemSet & theElements,
212 const gp_Ax1& theAxis,
213 const double theAngle,
214 const int theNbSteps,
215 const double theToler,
216 const bool theMakeGroups,
217 const bool theMakeWalls=true);
218 // Generate new elements by rotation of theElements around theAxis
219 // by theAngle by theNbSteps
222 * Auxilary flag for advanced extrusion.
223 * BOUNDARY: create or not boundary for result of extrusion
224 * SEW: try to use existing nodes or create new nodes in any case
226 enum ExtrusionFlags {
227 EXTRUSION_FLAG_BOUNDARY = 0x01,
228 EXTRUSION_FLAG_SEW = 0x02
232 * special structire for control of extrusion functionality
235 gp_Dir myDir; // direction of extrusion
236 Handle(TColStd_HSequenceOfReal) mySteps; // magnitudes for each step
237 SMESH_SequenceOfNode myNodes; // nodes for using in sewing
241 * Create new node in the mesh with given coordinates
242 * (auxilary for advanced extrusion)
244 const SMDS_MeshNode* CreateNode(const double x,
247 const double tolnode,
248 SMESH_SequenceOfNode& aNodes);
251 * Generate new elements by extrusion of theElements
252 * It is a method used in .idl file. All functionality
253 * is implemented in the next method (see below) which
254 * is used in the cuurent method.
255 * param theElems - list of elements for extrusion
256 * param newElemsMap returns history of extrusion
257 * param theFlags set flags for performing extrusion (see description
258 * of enum ExtrusionFlags for additional information)
259 * param theTolerance - uses for comparing locations of nodes if flag
260 * EXTRUSION_FLAG_SEW is set
262 PGroupIDs ExtrusionSweep (TIDSortedElemSet & theElems,
263 const gp_Vec& theStep,
264 const int theNbSteps,
265 TElemOfElemListMap& newElemsMap,
266 const bool theMakeGroups,
267 const int theFlags = EXTRUSION_FLAG_BOUNDARY,
268 const double theTolerance = 1.e-6);
271 * Generate new elements by extrusion of theElements
272 * param theElems - list of elements for extrusion
273 * param newElemsMap returns history of extrusion
274 * param theFlags set flags for performing extrusion (see description
275 * of enum ExtrusionFlags for additional information)
276 * param theTolerance - uses for comparing locations of nodes if flag
277 * EXTRUSION_FLAG_SEW is set
278 * param theParams - special structure for manage of extrusion
280 PGroupIDs ExtrusionSweep (TIDSortedElemSet & theElems,
281 ExtrusParam& theParams,
282 TElemOfElemListMap& newElemsMap,
283 const bool theMakeGroups,
285 const double theTolerance);
288 // Generate new elements by extrusion of theElements
289 // by theStep by theNbSteps
291 enum Extrusion_Error {
296 EXTR_BAD_STARTING_NODE,
297 EXTR_BAD_ANGLES_NUMBER,
298 EXTR_CANT_GET_TANGENT
301 Extrusion_Error ExtrusionAlongTrack (TIDSortedElemSet & theElements,
302 SMESH_subMesh* theTrackPattern,
303 const SMDS_MeshNode* theNodeStart,
304 const bool theHasAngles,
305 std::list<double>& theAngles,
306 const bool theHasRefPoint,
307 const gp_Pnt& theRefPoint,
308 const bool theMakeGroups);
309 // Generate new elements by extrusion of theElements along path given by theTrackPattern,
310 // theHasAngles are the rotation angles, base point can be given by theRefPoint
312 PGroupIDs Transform (TIDSortedElemSet & theElements,
313 const gp_Trsf& theTrsf,
315 const bool theMakeGroups,
316 SMESH_Mesh* theTargetMesh=0);
317 // Move or copy theElements applying theTrsf to their nodes
319 typedef std::list< std::list< const SMDS_MeshNode* > > TListOfListOfNodes;
321 void FindCoincidentNodes (std::set<const SMDS_MeshNode*> & theNodes,
322 const double theTolerance,
323 TListOfListOfNodes & theGroupsOfNodes);
324 // Return list of group of nodes close to each other within theTolerance.
325 // Search among theNodes or in the whole mesh if theNodes is empty.
328 * \brief Return SMESH_NodeSearcher
330 SMESH_NodeSearcher* GetNodeSearcher();
332 int SimplifyFace (const std::vector<const SMDS_MeshNode *> faceNodes,
333 std::vector<const SMDS_MeshNode *>& poly_nodes,
334 std::vector<int>& quantities) const;
335 // Split face, defined by <faceNodes>, into several faces by repeating nodes.
336 // Is used by MergeNodes()
338 void MergeNodes (TListOfListOfNodes & theNodeGroups);
339 // In each group, the cdr of nodes are substituted by the first one
342 typedef std::list< std::list< int > > TListOfListOfElementsID;
344 void FindEqualElements(std::set<const SMDS_MeshElement*> & theElements,
345 TListOfListOfElementsID & theGroupsOfElementsID);
346 // Return list of group of elements build on the same nodes.
347 // Search among theElements or in the whole mesh if theElements is empty.
349 void MergeElements(TListOfListOfElementsID & theGroupsOfElementsID);
350 // In each group remove all but first of elements.
352 void MergeEqualElements();
353 // Remove all but one of elements built on the same nodes.
354 // Return nb of successfully merged groups.
356 static bool CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
357 const SMDS_MeshNode* theNode2,
358 const SMDS_MeshNode* theNode3 = 0);
359 // Return true if the three nodes are on a free border
361 static bool FindFreeBorder (const SMDS_MeshNode* theFirstNode,
362 const SMDS_MeshNode* theSecondNode,
363 const SMDS_MeshNode* theLastNode,
364 std::list< const SMDS_MeshNode* > & theNodes,
365 std::list< const SMDS_MeshElement* >& theFaces);
366 // Return nodes and faces of a free border if found
370 // for SewFreeBorder()
371 SEW_BORDER1_NOT_FOUND,
372 SEW_BORDER2_NOT_FOUND,
373 SEW_BOTH_BORDERS_NOT_FOUND,
375 SEW_VOLUMES_TO_SPLIT,
376 // for SewSideElements()
377 SEW_DIFF_NB_OF_ELEMENTS,
378 SEW_TOPO_DIFF_SETS_OF_ELEMENTS,
385 Sew_Error SewFreeBorder (const SMDS_MeshNode* theBorderFirstNode,
386 const SMDS_MeshNode* theBorderSecondNode,
387 const SMDS_MeshNode* theBorderLastNode,
388 const SMDS_MeshNode* theSide2FirstNode,
389 const SMDS_MeshNode* theSide2SecondNode,
390 const SMDS_MeshNode* theSide2ThirdNode = 0,
391 const bool theSide2IsFreeBorder = true,
392 const bool toCreatePolygons = false,
393 const bool toCreatePolyedrs = false);
394 // Sew the free border to the side2 by replacing nodes in
395 // elements on the free border with nodes of the elements
396 // of the side 2. If nb of links in the free border and
397 // between theSide2FirstNode and theSide2LastNode are different,
398 // additional nodes are inserted on a link provided that no
399 // volume elements share the splitted link.
400 // The side 2 is a free border if theSide2IsFreeBorder == true.
401 // Sewing is peformed between the given first, second and last
402 // nodes on the sides.
403 // theBorderFirstNode is merged with theSide2FirstNode.
404 // if (!theSide2IsFreeBorder) then theSide2SecondNode gives
405 // the last node on the side 2, which will be merged with
406 // theBorderLastNode.
407 // if (theSide2IsFreeBorder) then theSide2SecondNode will
408 // be merged with theBorderSecondNode.
409 // if (theSide2IsFreeBorder && theSide2ThirdNode == 0) then
410 // the 2 free borders are sewn link by link and no additional
411 // nodes are inserted.
412 // Return false, if sewing failed.
414 Sew_Error SewSideElements (TIDSortedElemSet& theSide1,
415 TIDSortedElemSet& theSide2,
416 const SMDS_MeshNode* theFirstNode1ToMerge,
417 const SMDS_MeshNode* theFirstNode2ToMerge,
418 const SMDS_MeshNode* theSecondNode1ToMerge,
419 const SMDS_MeshNode* theSecondNode2ToMerge);
420 // Sew two sides of a mesh. Nodes belonging to theSide1 are
421 // merged with nodes of elements of theSide2.
422 // Number of elements in theSide1 and in theSide2 must be
423 // equal and they should have similar node connectivity.
424 // The nodes to merge should belong to side s borders and
425 // the first node should be linked to the second.
427 void InsertNodesIntoLink(const SMDS_MeshElement* theFace,
428 const SMDS_MeshNode* theBetweenNode1,
429 const SMDS_MeshNode* theBetweenNode2,
430 std::list<const SMDS_MeshNode*>& theNodesToInsert,
431 const bool toCreatePoly = false);
432 // insert theNodesToInsert into theFace between theBetweenNode1 and theBetweenNode2.
433 // If toCreatePoly is true, replace theFace by polygon, else split theFace.
435 void UpdateVolumes (const SMDS_MeshNode* theBetweenNode1,
436 const SMDS_MeshNode* theBetweenNode2,
437 std::list<const SMDS_MeshNode*>& theNodesToInsert);
438 // insert theNodesToInsert into all volumes, containing link
439 // theBetweenNode1 - theBetweenNode2, between theBetweenNode1 and theBetweenNode2.
441 void ConvertToQuadratic(const bool theForce3d);
442 //converts all mesh to quadratic one, deletes old elements, replacing
443 //them with quadratic ones with the same id.
445 bool ConvertFromQuadratic();
446 //converts all mesh from quadratic to ordinary ones, deletes old quadratic elements, replacing
447 //them with ordinary mesh elements with the same id.
450 // static int SortQuadNodes (const SMDS_Mesh * theMesh,
451 // int theNodeIds[] );
452 // // Set 4 nodes of a quadrangle face in a good order.
453 // // Swap 1<->2 or 2<->3 nodes and correspondingly return
456 // static bool SortHexaNodes (const SMDS_Mesh * theMesh,
457 // int theNodeIds[] );
458 // // Set 8 nodes of a hexahedron in a good order.
459 // // Return success status
461 static void AddToSameGroups (const SMDS_MeshElement* elemToAdd,
462 const SMDS_MeshElement* elemInGroups,
463 SMESHDS_Mesh * aMesh);
464 // Add elemToAdd to the all groups the elemInGroups belongs to
466 static void RemoveElemFromGroups (const SMDS_MeshElement* element,
467 SMESHDS_Mesh * aMesh);
468 // remove element from the all groups
470 static void ReplaceElemInGroups (const SMDS_MeshElement* elemToRm,
471 const SMDS_MeshElement* elemToAdd,
472 SMESHDS_Mesh * aMesh);
473 // replace elemToRm by elemToAdd in the all groups
476 * \brief Return nodes linked to the given one in elements of the type
478 static void GetLinkedNodes( const SMDS_MeshNode* node,
479 TIDSortedElemSet & linkedNodes,
480 SMDSAbs_ElementType type = SMDSAbs_All );
482 static const SMDS_MeshElement*
483 FindFaceInSet(const SMDS_MeshNode* n1,
484 const SMDS_MeshNode* n2,
485 const TIDSortedElemSet& elemSet,
486 const TIDSortedElemSet& avoidSet);
487 // Return a face having linked nodes n1 and n2 and which is
488 // - not in avoidSet,
489 // - in elemSet provided that !elemSet.empty()
492 * \brief Find corresponding nodes in two sets of faces
493 * \param theSide1 - first face set
494 * \param theSide2 - second first face
495 * \param theFirstNode1 - a boundary node of set 1
496 * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
497 * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
498 * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
499 * \param nReplaceMap - output map of corresponding nodes
500 * \retval Sew_Error - is a success or not
502 static Sew_Error FindMatchingNodes(std::set<const SMDS_MeshElement*>& theSide1,
503 std::set<const SMDS_MeshElement*>& theSide2,
504 const SMDS_MeshNode* theFirstNode1,
505 const SMDS_MeshNode* theFirstNode2,
506 const SMDS_MeshNode* theSecondNode1,
507 const SMDS_MeshNode* theSecondNode2,
508 TNodeNodeMap & nReplaceMap);
511 * \brief Returns true if given node is medium
512 * \param n - node to check
513 * \param typeToCheck - type of elements containing the node to ask about node status
514 * \retval bool - check result
516 static bool IsMedium(const SMDS_MeshNode* node,
517 const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
519 int FindShape (const SMDS_MeshElement * theElem);
520 // Return an index of the shape theElem is on
521 // or zero if a shape not found
523 SMESH_Mesh * GetMesh() { return myMesh; }
525 SMESHDS_Mesh * GetMeshDS() { return myMesh->GetMeshDS(); }
527 const SMESH_SequenceOfElemPtr& GetLastCreatedNodes() const { return myLastCreatedNodes; }
529 const SMESH_SequenceOfElemPtr& GetLastCreatedElems() const { return myLastCreatedElems; }
531 bool DoubleNodes( const std::list< int >& theListOfNodes,
532 const std::list< int >& theListOfModifiedElems );
537 * \brief Convert elements contained in a submesh to quadratic
538 * \retval int - nb of checked elements
540 int convertElemToQuadratic(SMESHDS_SubMesh * theSm,
541 SMESH_MesherHelper& theHelper,
542 const bool theForce3d);
545 * \brief Convert quadratic elements to linear ones and remove quadratic nodes
546 * \retval int - nb of checked elements
548 int removeQuadElem( SMESHDS_SubMesh * theSm,
549 SMDS_ElemIteratorPtr theItr,
550 const int theShapeID);
552 * \brief Create groups of elements made during transformation
553 * \param nodeGens - nodes making corresponding myLastCreatedNodes
554 * \param elemGens - elements making corresponding myLastCreatedElems
555 * \param postfix - to append to names of new groups
557 PGroupIDs generateGroups(const SMESH_SequenceOfElemPtr& nodeGens,
558 const SMESH_SequenceOfElemPtr& elemGens,
559 const std::string& postfix,
560 SMESH_Mesh* targetMesh=0);
563 typedef std::map<const SMDS_MeshNode*, std::list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
564 typedef TNodeOfNodeListMap::iterator TNodeOfNodeListMapItr;
565 typedef std::vector<TNodeOfNodeListMapItr> TVecOfNnlmiMap;
566 typedef std::map<const SMDS_MeshElement*, TVecOfNnlmiMap > TElemOfVecOfNnlmiMap;
569 * \brief Create elements by sweeping an element
570 * \param elem - element to sweep
571 * \param newNodesItVec - nodes generated from each node of the element
572 * \param newElems - generated elements
573 * \param nbSteps - number of sweeping steps
574 * \param srcElements - to append elem for each generated element
576 void sweepElement(const SMDS_MeshElement* elem,
577 const std::vector<TNodeOfNodeListMapItr> & newNodesItVec,
578 std::list<const SMDS_MeshElement*>& newElems,
580 SMESH_SequenceOfElemPtr& srcElements);
583 * \brief Create 1D and 2D elements around swept elements
584 * \param mapNewNodes - source nodes and ones generated from them
585 * \param newElemsMap - source elements and ones generated from them
586 * \param elemNewNodesMap - nodes generated from each node of each element
587 * \param elemSet - all swept elements
588 * \param nbSteps - number of sweeping steps
589 * \param srcElements - to append elem for each generated element
591 void makeWalls (TNodeOfNodeListMap & mapNewNodes,
592 TElemOfElemListMap & newElemsMap,
593 TElemOfVecOfNnlmiMap & elemNewNodesMap,
594 TIDSortedElemSet& elemSet,
596 SMESH_SequenceOfElemPtr& srcElements);
602 * Sequence for keeping nodes created during last operation
604 SMESH_SequenceOfElemPtr myLastCreatedNodes;
607 * Sequence for keeping elements created during last operation
609 SMESH_SequenceOfElemPtr myLastCreatedElems;