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 "SMDS_MeshElement.hxx"
34 #include "SMESH_Controls.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_SequenceOfElemPtr.hxx"
37 #include "SMESH_SequenceOfNode.hxx"
39 #include <TColStd_HSequenceOfReal.hxx>
50 class SMESH_MesherHelper;
53 typedef std::map<const SMDS_MeshElement*,
54 std::list<const SMDS_MeshElement*> > TElemOfElemListMap;
55 typedef std::map<const SMDS_MeshNode*, const SMDS_MeshNode*> TNodeNodeMap;
57 //!< Set of elements sorted by ID, to be used to assure predictability of edition
58 typedef std::set< const SMDS_MeshElement*, TIDCompare > TIDSortedElemSet;
60 typedef pair< const SMDS_MeshNode*, const SMDS_MeshNode* > NLink;
63 //=======================================================================
65 * \brief A sorted pair of nodes
67 //=======================================================================
69 struct SMESH_TLink: public NLink
71 SMESH_TLink(const SMDS_MeshNode* n1, const SMDS_MeshNode* n2 ):NLink( n1, n2 )
72 { if ( n1->GetID() < n2->GetID() ) std::swap( first, second ); }
73 SMESH_TLink(const NLink& link ):NLink( link )
74 { if ( first->GetID() < second->GetID() ) std::swap( first, second ); }
75 const SMDS_MeshNode* node1() const { return first; }
76 const SMDS_MeshNode* node2() const { return second; }
79 // ============================================================
81 * \brief Searcher for the node closest to point
83 // ============================================================
85 struct SMESH_NodeSearcher
87 virtual const SMDS_MeshNode* FindClosestTo( const gp_Pnt& pnt ) = 0;
90 // ============================================================
92 * \brief Editor of a mesh
94 // ============================================================
96 class SMESH_EXPORT SMESH_MeshEditor {
100 SMESH_MeshEditor( SMESH_Mesh* theMesh );
105 SMDS_MeshElement* AddElement(const std::vector<const SMDS_MeshNode*> & nodes,
106 const SMDSAbs_ElementType type,
112 SMDS_MeshElement* AddElement(const std::vector<int> & nodeIDs,
113 const SMDSAbs_ElementType type,
117 bool Remove (const std::list< int >& theElemIDs, const bool isNodes);
118 // Remove a node or an element.
119 // Modify a compute state of sub-meshes which become empty
121 bool InverseDiag (const SMDS_MeshElement * theTria1,
122 const SMDS_MeshElement * theTria2 );
123 // Replace two neighbour triangles with ones built on the same 4 nodes
124 // but having other common link.
125 // Return False if args are improper
127 bool InverseDiag (const SMDS_MeshNode * theNode1,
128 const SMDS_MeshNode * theNode2 );
129 // Replace two neighbour triangles sharing theNode1-theNode2 link
130 // with ones built on the same 4 nodes but having other common link.
131 // Return false if proper faces not found
133 bool DeleteDiag (const SMDS_MeshNode * theNode1,
134 const SMDS_MeshNode * theNode2 );
135 // Replace two neighbour triangles sharing theNode1-theNode2 link
136 // with a quadrangle built on the same 4 nodes.
137 // Return false if proper faces not found
139 bool Reorient (const SMDS_MeshElement * theElement);
140 // Reverse theElement orientation
144 * \brief Fuse neighbour triangles into quadrangles.
145 * \param theElems - The triangles to be fused.
146 * \param theCriterion - Is used to choose a neighbour to fuse with.
147 * \param theMaxAngle - Is a max angle between element normals at which fusion
148 * is still performed; theMaxAngle is mesured in radians.
149 * \retval bool - Success or not.
151 bool TriToQuad (TIDSortedElemSet & theElems,
152 SMESH::Controls::NumericalFunctorPtr theCriterion,
153 const double theMaxAngle);
156 * \brief Split quadrangles into triangles.
157 * \param theElems - The faces to be splitted.
158 * \param theCriterion - Is used to choose a diagonal for splitting.
159 * \retval bool - Success or not.
161 bool QuadToTri (TIDSortedElemSet & theElems,
162 SMESH::Controls::NumericalFunctorPtr theCriterion);
165 * \brief Split quadrangles into triangles.
166 * \param theElems - The faces to be splitted.
167 * \param the13Diag - Is used to choose a diagonal for splitting.
168 * \retval bool - Success or not.
170 bool QuadToTri (TIDSortedElemSet & theElems,
171 const bool the13Diag);
174 * \brief Find better diagonal for splitting.
175 * \param theQuad - The face to find better splitting of.
176 * \param theCriterion - Is used to choose a diagonal for splitting.
177 * \retval int - 1 for 1-3 diagonal, 2 for 2-4, -1 - for errors.
179 int BestSplit (const SMDS_MeshElement* theQuad,
180 SMESH::Controls::NumericalFunctorPtr theCriterion);
183 enum SmoothMethod { LAPLACIAN = 0, CENTROIDAL };
185 void Smooth (TIDSortedElemSet & theElements,
186 std::set<const SMDS_MeshNode*> & theFixedNodes,
187 const SmoothMethod theSmoothMethod,
188 const int theNbIterations,
189 double theTgtAspectRatio = 1.0,
190 const bool the2D = true);
191 // Smooth theElements using theSmoothMethod during theNbIterations
192 // or until a worst element has aspect ratio <= theTgtAspectRatio.
193 // Aspect Ratio varies in range [1.0, inf].
194 // If theElements is empty, the whole mesh is smoothed.
195 // theFixedNodes contains additionally fixed nodes. Nodes built
196 // on edges and boundary nodes are always fixed.
197 // If the2D, smoothing is performed using UV parameters of nodes
198 // on geometrical faces
200 typedef std::auto_ptr< std::list<int> > PGroupIDs;
202 PGroupIDs RotationSweep (TIDSortedElemSet & theElements,
203 const gp_Ax1& theAxis,
204 const double theAngle,
205 const int theNbSteps,
206 const double theToler,
207 const bool theMakeGroups,
208 const bool theMakeWalls=true);
209 // Generate new elements by rotation of theElements around theAxis
210 // by theAngle by theNbSteps
213 * Auxilary flag for advanced extrusion.
214 * BOUNDARY: create or not boundary for result of extrusion
215 * SEW: try to use existing nodes or create new nodes in any case
217 enum ExtrusionFlags {
218 EXTRUSION_FLAG_BOUNDARY = 0x01,
219 EXTRUSION_FLAG_SEW = 0x02
223 * special structire for control of extrusion functionality
226 gp_Dir myDir; // direction of extrusion
227 Handle(TColStd_HSequenceOfReal) mySteps; // magnitudes for each step
228 SMESH_SequenceOfNode myNodes; // nodes for using in sewing
232 * Create new node in the mesh with given coordinates
233 * (auxilary for advanced extrusion)
235 const SMDS_MeshNode* CreateNode(const double x,
238 const double tolnode,
239 SMESH_SequenceOfNode& aNodes);
242 * Generate new elements by extrusion of theElements
243 * It is a method used in .idl file. All functionality
244 * is implemented in the next method (see below) which
245 * is used in the cuurent method.
246 * param theElems - list of elements for extrusion
247 * param newElemsMap returns history of extrusion
248 * param theFlags set flags for performing extrusion (see description
249 * of enum ExtrusionFlags for additional information)
250 * param theTolerance - uses for comparing locations of nodes if flag
251 * EXTRUSION_FLAG_SEW is set
253 PGroupIDs ExtrusionSweep (TIDSortedElemSet & theElems,
254 const gp_Vec& theStep,
255 const int theNbSteps,
256 TElemOfElemListMap& newElemsMap,
257 const bool theMakeGroups,
258 const int theFlags = EXTRUSION_FLAG_BOUNDARY,
259 const double theTolerance = 1.e-6);
262 * Generate new elements by extrusion of theElements
263 * param theElems - list of elements for extrusion
264 * param newElemsMap returns history of extrusion
265 * param theFlags set flags for performing extrusion (see description
266 * of enum ExtrusionFlags for additional information)
267 * param theTolerance - uses for comparing locations of nodes if flag
268 * EXTRUSION_FLAG_SEW is set
269 * param theParams - special structure for manage of extrusion
271 PGroupIDs ExtrusionSweep (TIDSortedElemSet & theElems,
272 ExtrusParam& theParams,
273 TElemOfElemListMap& newElemsMap,
274 const bool theMakeGroups,
276 const double theTolerance);
279 // Generate new elements by extrusion of theElements
280 // by theStep by theNbSteps
282 enum Extrusion_Error {
287 EXTR_BAD_STARTING_NODE,
288 EXTR_BAD_ANGLES_NUMBER,
289 EXTR_CANT_GET_TANGENT
292 Extrusion_Error ExtrusionAlongTrack (TIDSortedElemSet & theElements,
293 SMESH_subMesh* theTrackPattern,
294 const SMDS_MeshNode* theNodeStart,
295 const bool theHasAngles,
296 std::list<double>& theAngles,
297 const bool theHasRefPoint,
298 const gp_Pnt& theRefPoint,
299 const bool theMakeGroups);
300 // Generate new elements by extrusion of theElements along path given by theTrackPattern,
301 // theHasAngles are the rotation angles, base point can be given by theRefPoint
303 PGroupIDs Transform (TIDSortedElemSet & theElements,
304 const gp_Trsf& theTrsf,
306 const bool theMakeGroups,
307 SMESH_Mesh* theTargetMesh=0);
308 // Move or copy theElements applying theTrsf to their nodes
310 typedef std::list< std::list< const SMDS_MeshNode* > > TListOfListOfNodes;
312 void FindCoincidentNodes (std::set<const SMDS_MeshNode*> & theNodes,
313 const double theTolerance,
314 TListOfListOfNodes & theGroupsOfNodes);
315 // Return list of group of nodes close to each other within theTolerance.
316 // Search among theNodes or in the whole mesh if theNodes is empty.
319 * \brief Return SMESH_NodeSearcher
321 SMESH_NodeSearcher* GetNodeSearcher();
323 int SimplifyFace (const std::vector<const SMDS_MeshNode *> faceNodes,
324 std::vector<const SMDS_MeshNode *>& poly_nodes,
325 std::vector<int>& quantities) const;
326 // Split face, defined by <faceNodes>, into several faces by repeating nodes.
327 // Is used by MergeNodes()
329 void MergeNodes (TListOfListOfNodes & theNodeGroups);
330 // In each group, the cdr of nodes are substituted by the first one
333 typedef std::list< std::list< int > > TListOfListOfElementsID;
335 void FindEqualElements(std::set<const SMDS_MeshElement*> & theElements,
336 TListOfListOfElementsID & theGroupsOfElementsID);
337 // Return list of group of elements build on the same nodes.
338 // Search among theElements or in the whole mesh if theElements is empty.
340 void MergeElements(TListOfListOfElementsID & theGroupsOfElementsID);
341 // In each group remove all but first of elements.
343 void MergeEqualElements();
344 // Remove all but one of elements built on the same nodes.
345 // Return nb of successfully merged groups.
347 static bool CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
348 const SMDS_MeshNode* theNode2,
349 const SMDS_MeshNode* theNode3 = 0);
350 // Return true if the three nodes are on a free border
352 static bool FindFreeBorder (const SMDS_MeshNode* theFirstNode,
353 const SMDS_MeshNode* theSecondNode,
354 const SMDS_MeshNode* theLastNode,
355 std::list< const SMDS_MeshNode* > & theNodes,
356 std::list< const SMDS_MeshElement* >& theFaces);
357 // Return nodes and faces of a free border if found
361 // for SewFreeBorder()
362 SEW_BORDER1_NOT_FOUND,
363 SEW_BORDER2_NOT_FOUND,
364 SEW_BOTH_BORDERS_NOT_FOUND,
366 SEW_VOLUMES_TO_SPLIT,
367 // for SewSideElements()
368 SEW_DIFF_NB_OF_ELEMENTS,
369 SEW_TOPO_DIFF_SETS_OF_ELEMENTS,
376 Sew_Error SewFreeBorder (const SMDS_MeshNode* theBorderFirstNode,
377 const SMDS_MeshNode* theBorderSecondNode,
378 const SMDS_MeshNode* theBorderLastNode,
379 const SMDS_MeshNode* theSide2FirstNode,
380 const SMDS_MeshNode* theSide2SecondNode,
381 const SMDS_MeshNode* theSide2ThirdNode = 0,
382 const bool theSide2IsFreeBorder = true,
383 const bool toCreatePolygons = false,
384 const bool toCreatePolyedrs = false);
385 // Sew the free border to the side2 by replacing nodes in
386 // elements on the free border with nodes of the elements
387 // of the side 2. If nb of links in the free border and
388 // between theSide2FirstNode and theSide2LastNode are different,
389 // additional nodes are inserted on a link provided that no
390 // volume elements share the splitted link.
391 // The side 2 is a free border if theSide2IsFreeBorder == true.
392 // Sewing is peformed between the given first, second and last
393 // nodes on the sides.
394 // theBorderFirstNode is merged with theSide2FirstNode.
395 // if (!theSide2IsFreeBorder) then theSide2SecondNode gives
396 // the last node on the side 2, which will be merged with
397 // theBorderLastNode.
398 // if (theSide2IsFreeBorder) then theSide2SecondNode will
399 // be merged with theBorderSecondNode.
400 // if (theSide2IsFreeBorder && theSide2ThirdNode == 0) then
401 // the 2 free borders are sewn link by link and no additional
402 // nodes are inserted.
403 // Return false, if sewing failed.
405 Sew_Error SewSideElements (TIDSortedElemSet& theSide1,
406 TIDSortedElemSet& theSide2,
407 const SMDS_MeshNode* theFirstNode1ToMerge,
408 const SMDS_MeshNode* theFirstNode2ToMerge,
409 const SMDS_MeshNode* theSecondNode1ToMerge,
410 const SMDS_MeshNode* theSecondNode2ToMerge);
411 // Sew two sides of a mesh. Nodes belonging to theSide1 are
412 // merged with nodes of elements of theSide2.
413 // Number of elements in theSide1 and in theSide2 must be
414 // equal and they should have similar node connectivity.
415 // The nodes to merge should belong to side s borders and
416 // the first node should be linked to the second.
418 void InsertNodesIntoLink(const SMDS_MeshElement* theFace,
419 const SMDS_MeshNode* theBetweenNode1,
420 const SMDS_MeshNode* theBetweenNode2,
421 std::list<const SMDS_MeshNode*>& theNodesToInsert,
422 const bool toCreatePoly = false);
423 // insert theNodesToInsert into theFace between theBetweenNode1 and theBetweenNode2.
424 // If toCreatePoly is true, replace theFace by polygon, else split theFace.
426 void UpdateVolumes (const SMDS_MeshNode* theBetweenNode1,
427 const SMDS_MeshNode* theBetweenNode2,
428 std::list<const SMDS_MeshNode*>& theNodesToInsert);
429 // insert theNodesToInsert into all volumes, containing link
430 // theBetweenNode1 - theBetweenNode2, between theBetweenNode1 and theBetweenNode2.
432 void ConvertToQuadratic(const bool theForce3d);
433 //converts all mesh to quadratic one, deletes old elements, replacing
434 //them with quadratic ones with the same id.
436 bool ConvertFromQuadratic();
437 //converts all mesh from quadratic to ordinary ones, deletes old quadratic elements, replacing
438 //them with ordinary mesh elements with the same id.
441 // static int SortQuadNodes (const SMDS_Mesh * theMesh,
442 // int theNodeIds[] );
443 // // Set 4 nodes of a quadrangle face in a good order.
444 // // Swap 1<->2 or 2<->3 nodes and correspondingly return
447 // static bool SortHexaNodes (const SMDS_Mesh * theMesh,
448 // int theNodeIds[] );
449 // // Set 8 nodes of a hexahedron in a good order.
450 // // Return success status
452 static void AddToSameGroups (const SMDS_MeshElement* elemToAdd,
453 const SMDS_MeshElement* elemInGroups,
454 SMESHDS_Mesh * aMesh);
455 // Add elemToAdd to the all groups the elemInGroups belongs to
457 static void RemoveElemFromGroups (const SMDS_MeshElement* element,
458 SMESHDS_Mesh * aMesh);
459 // remove element from the all groups
461 static void ReplaceElemInGroups (const SMDS_MeshElement* elemToRm,
462 const SMDS_MeshElement* elemToAdd,
463 SMESHDS_Mesh * aMesh);
464 // replace elemToRm by elemToAdd in the all groups
467 * \brief Return nodes linked to the given one in elements of the type
469 static void GetLinkedNodes( const SMDS_MeshNode* node,
470 TIDSortedElemSet & linkedNodes,
471 SMDSAbs_ElementType type = SMDSAbs_All );
473 static const SMDS_MeshElement*
474 FindFaceInSet(const SMDS_MeshNode* n1,
475 const SMDS_MeshNode* n2,
476 const TIDSortedElemSet& elemSet,
477 const TIDSortedElemSet& avoidSet);
478 // Return a face having linked nodes n1 and n2 and which is
479 // - not in avoidSet,
480 // - in elemSet provided that !elemSet.empty()
483 * \brief Find corresponding nodes in two sets of faces
484 * \param theSide1 - first face set
485 * \param theSide2 - second first face
486 * \param theFirstNode1 - a boundary node of set 1
487 * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
488 * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
489 * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
490 * \param nReplaceMap - output map of corresponding nodes
491 * \retval Sew_Error - is a success or not
493 static Sew_Error FindMatchingNodes(std::set<const SMDS_MeshElement*>& theSide1,
494 std::set<const SMDS_MeshElement*>& theSide2,
495 const SMDS_MeshNode* theFirstNode1,
496 const SMDS_MeshNode* theFirstNode2,
497 const SMDS_MeshNode* theSecondNode1,
498 const SMDS_MeshNode* theSecondNode2,
499 TNodeNodeMap & nReplaceMap);
502 * \brief Returns true if given node is medium
503 * \param n - node to check
504 * \param typeToCheck - type of elements containing the node to ask about node status
505 * \retval bool - check result
507 static bool IsMedium(const SMDS_MeshNode* node,
508 const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
510 int FindShape (const SMDS_MeshElement * theElem);
511 // Return an index of the shape theElem is on
512 // or zero if a shape not found
514 SMESH_Mesh * GetMesh() { return myMesh; }
516 SMESHDS_Mesh * GetMeshDS() { return myMesh->GetMeshDS(); }
518 const SMESH_SequenceOfElemPtr& GetLastCreatedNodes() const { return myLastCreatedNodes; }
520 const SMESH_SequenceOfElemPtr& GetLastCreatedElems() const { return myLastCreatedElems; }
522 bool DoubleNodes( const std::list< int >& theListOfNodes,
523 const std::list< int >& theListOfModifiedElems );
528 * \brief Convert elements contained in a submesh to quadratic
529 * \retval int - nb of checked elements
531 int convertElemToQuadratic(SMESHDS_SubMesh * theSm,
532 SMESH_MesherHelper& theHelper,
533 const bool theForce3d);
536 * \brief Convert quadratic elements to linear ones and remove quadratic nodes
537 * \retval int - nb of checked elements
539 int removeQuadElem( SMESHDS_SubMesh * theSm,
540 SMDS_ElemIteratorPtr theItr,
541 const int theShapeID);
543 * \brief Create groups of elements made during transformation
544 * \param nodeGens - nodes making corresponding myLastCreatedNodes
545 * \param elemGens - elements making corresponding myLastCreatedElems
546 * \param postfix - to append to names of new groups
548 PGroupIDs generateGroups(const SMESH_SequenceOfElemPtr& nodeGens,
549 const SMESH_SequenceOfElemPtr& elemGens,
550 const std::string& postfix,
551 SMESH_Mesh* targetMesh=0);
554 typedef std::map<const SMDS_MeshNode*, std::list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
555 typedef TNodeOfNodeListMap::iterator TNodeOfNodeListMapItr;
556 typedef std::vector<TNodeOfNodeListMapItr> TVecOfNnlmiMap;
557 typedef std::map<const SMDS_MeshElement*, TVecOfNnlmiMap > TElemOfVecOfNnlmiMap;
560 * \brief Create elements by sweeping an element
561 * \param elem - element to sweep
562 * \param newNodesItVec - nodes generated from each node of the element
563 * \param newElems - generated elements
564 * \param nbSteps - number of sweeping steps
565 * \param srcElements - to append elem for each generated element
567 void sweepElement(const SMDS_MeshElement* elem,
568 const std::vector<TNodeOfNodeListMapItr> & newNodesItVec,
569 std::list<const SMDS_MeshElement*>& newElems,
571 SMESH_SequenceOfElemPtr& srcElements);
574 * \brief Create 1D and 2D elements around swept elements
575 * \param mapNewNodes - source nodes and ones generated from them
576 * \param newElemsMap - source elements and ones generated from them
577 * \param elemNewNodesMap - nodes generated from each node of each element
578 * \param elemSet - all swept elements
579 * \param nbSteps - number of sweeping steps
580 * \param srcElements - to append elem for each generated element
582 void makeWalls (TNodeOfNodeListMap & mapNewNodes,
583 TElemOfElemListMap & newElemsMap,
584 TElemOfVecOfNnlmiMap & elemNewNodesMap,
585 TIDSortedElemSet& elemSet,
587 SMESH_SequenceOfElemPtr& srcElements);
593 * Sequence for keeping nodes created during last operation
595 SMESH_SequenceOfElemPtr myLastCreatedNodes;
598 * Sequence for keeping elements created during last operation
600 SMESH_SequenceOfElemPtr myLastCreatedElems;