1 // SMESH SMESH : idl implementation based on 'SMESH' unit's classes
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESH_MeshEditor.cxx
25 // Created : Mon Apr 12 16:10:22 2004
26 // Author : Edward AGAPOV (eap)
29 #include "SMESH_MeshEditor.hxx"
31 #include "SMDS_FaceOfNodes.hxx"
32 #include "SMDS_VolumeTool.hxx"
33 #include "SMDS_EdgePosition.hxx"
34 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
36 #include "SMESHDS_Group.hxx"
37 #include "SMESHDS_Mesh.hxx"
39 #include "SMESH_subMesh.hxx"
40 #include "SMESH_ControlsDef.hxx"
42 #include "utilities.h"
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <TopTools_ListOfShape.hxx>
50 #include <gp_Trsf.hxx>
55 #include <BRep_Tool.hxx>
56 #include <Geom_Curve.hxx>
61 using namespace SMESH::Controls;
63 typedef map<const SMDS_MeshNode*, const SMDS_MeshNode*> TNodeNodeMap;
64 typedef map<const SMDS_MeshElement*, list<const SMDS_MeshNode*> > TElemOfNodeListMap;
65 typedef map<const SMDS_MeshElement*, list<const SMDS_MeshElement*> > TElemOfElemListMap;
66 typedef map<const SMDS_MeshNode*, list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
67 typedef TNodeOfNodeListMap::iterator TNodeOfNodeListMapItr;
68 typedef map<const SMDS_MeshElement*, vector<TNodeOfNodeListMapItr> > TElemOfVecOfNnlmiMap;
70 //=======================================================================
71 //function : SMESH_MeshEditor
73 //=======================================================================
75 SMESH_MeshEditor::SMESH_MeshEditor( SMESH_Mesh* theMesh ):
80 //=======================================================================
82 //purpose : Remove a node or an element.
83 // Modify a compute state of sub-meshes which become empty
84 //=======================================================================
86 bool SMESH_MeshEditor::Remove (const list< int >& theIDs,
90 SMESHDS_Mesh* aMesh = GetMeshDS();
91 set< SMESH_subMesh *> smmap;
93 list<int>::const_iterator it = theIDs.begin();
94 for ( ; it != theIDs.end(); it++ )
96 const SMDS_MeshElement * elem;
98 elem = aMesh->FindNode( *it );
100 elem = aMesh->FindElement( *it );
104 // Find sub-meshes to notify about modification
105 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
106 while ( nodeIt->more() )
108 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
109 const SMDS_PositionPtr& aPosition = node->GetPosition();
110 if ( aPosition.get() ) {
111 int aShapeID = aPosition->GetShapeId();
113 TopoDS_Shape aShape = aMesh->IndexToShape( aShapeID );
114 SMESH_subMesh * sm = GetMesh()->GetSubMeshContaining( aShape );
123 aMesh->RemoveNode( static_cast< const SMDS_MeshNode* >( elem ));
125 aMesh->RemoveElement( elem );
128 // Notify sub-meshes about modification
129 if ( !smmap.empty() ) {
130 set< SMESH_subMesh *>::iterator smIt;
131 for ( smIt = smmap.begin(); smIt != smmap.end(); smIt++ )
132 (*smIt)->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
137 //=======================================================================
138 //function : FindShape
139 //purpose : Return an index of the shape theElem is on
140 // or zero if a shape not found
141 //=======================================================================
143 int SMESH_MeshEditor::FindShape (const SMDS_MeshElement * theElem)
145 SMESHDS_Mesh * aMesh = GetMeshDS();
146 if ( aMesh->ShapeToMesh().IsNull() )
149 if ( theElem->GetType() == SMDSAbs_Node )
151 const SMDS_PositionPtr& aPosition =
152 static_cast<const SMDS_MeshNode*>( theElem )->GetPosition();
153 if ( aPosition.get() )
154 return aPosition->GetShapeId();
159 TopoDS_Shape aShape; // the shape a node is on
160 SMDS_ElemIteratorPtr nodeIt = theElem->nodesIterator();
161 while ( nodeIt->more() )
163 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
164 const SMDS_PositionPtr& aPosition = node->GetPosition();
165 if ( aPosition.get() ) {
166 int aShapeID = aPosition->GetShapeId();
167 SMESHDS_SubMesh * sm = aMesh->MeshElements( aShapeID );
170 if ( sm->Contains( theElem ))
172 if ( aShape.IsNull() )
173 aShape = aMesh->IndexToShape( aShapeID );
177 //MESSAGE ( "::FindShape() No SubShape for aShapeID " << aShapeID );
182 // None of nodes is on a proper shape,
183 // find the shape among ancestors of aShape on which a node is
184 if ( aShape.IsNull() ) {
185 //MESSAGE ("::FindShape() - NONE node is on shape")
188 TopTools_ListIteratorOfListOfShape ancIt( GetMesh()->GetAncestors( aShape ));
189 for ( ; ancIt.More(); ancIt.Next() )
191 SMESHDS_SubMesh * sm = aMesh->MeshElements( ancIt.Value() );
192 if ( sm && sm->Contains( theElem ))
193 return aMesh->ShapeToIndex( ancIt.Value() );
196 //MESSAGE ("::FindShape() - SHAPE NOT FOUND")
200 //=======================================================================
201 //function : InverseDiag
202 //purpose : Replace two neighbour triangles with ones built on the same 4 nodes
203 // but having other common link.
204 // Return False if args are improper
205 //=======================================================================
207 bool SMESH_MeshEditor::InverseDiag (const SMDS_MeshElement * theTria1,
208 const SMDS_MeshElement * theTria2 )
210 if (!theTria1 || !theTria2)
212 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( theTria1 );
213 if (!F1) return false;
214 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( theTria2 );
215 if (!F2) return false;
217 // 1 +--+ A theTria1: ( 1 A B ) A->2 ( 1 2 B ) 1 +--+ A
218 // | /| theTria2: ( B A 2 ) B->1 ( 1 A 2 ) |\ |
222 // put nodes in array and find out indices of the same ones
223 const SMDS_MeshNode* aNodes [6];
224 int sameInd [] = { 0, 0, 0, 0, 0, 0 };
226 SMDS_ElemIteratorPtr it = theTria1->nodesIterator();
229 aNodes[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
231 if ( i > 2 ) // theTria2
232 // find same node of theTria1
233 for ( int j = 0; j < 3; j++ )
234 if ( aNodes[ i ] == aNodes[ j ]) {
243 return false; // theTria1 is not a triangle
244 it = theTria2->nodesIterator();
246 if ( i == 6 && it->more() )
247 return false; // theTria2 is not a triangle
250 // find indices of 1,2 and of A,B in theTria1
251 int iA = 0, iB = 0, i1 = 0, i2 = 0;
252 for ( i = 0; i < 6; i++ )
254 if ( sameInd [ i ] == 0 )
261 // nodes 1 and 2 should not be the same
262 if ( aNodes[ i1 ] == aNodes[ i2 ] )
267 aNodes[ iA ] = aNodes[ i2 ];
269 aNodes[ sameInd[ iB ]] = aNodes[ i1 ];
271 //MESSAGE( theTria1 << theTria2 );
273 GetMeshDS()->ChangeElementNodes( theTria1, aNodes, 3 );
274 GetMeshDS()->ChangeElementNodes( theTria2, &aNodes[ 3 ], 3 );
276 //MESSAGE( theTria1 << theTria2 );
281 //=======================================================================
282 //function : findTriangles
283 //purpose : find triangles sharing theNode1-theNode2 link
284 //=======================================================================
286 static bool findTriangles(const SMDS_MeshNode * theNode1,
287 const SMDS_MeshNode * theNode2,
288 const SMDS_MeshElement*& theTria1,
289 const SMDS_MeshElement*& theTria2)
291 if ( !theNode1 || !theNode2 ) return false;
293 theTria1 = theTria2 = 0;
295 set< const SMDS_MeshElement* > emap;
296 SMDS_ElemIteratorPtr it = theNode1->GetInverseElementIterator();
298 const SMDS_MeshElement* elem = it->next();
299 if ( elem->GetType() == SMDSAbs_Face && elem->NbNodes() == 3 )
302 it = theNode2->GetInverseElementIterator();
304 const SMDS_MeshElement* elem = it->next();
305 if ( elem->GetType() == SMDSAbs_Face &&
306 emap.find( elem ) != emap.end() )
314 return ( theTria1 && theTria2 );
317 //=======================================================================
318 //function : InverseDiag
319 //purpose : Replace two neighbour triangles sharing theNode1-theNode2 link
320 // with ones built on the same 4 nodes but having other common link.
321 // Return false if proper faces not found
322 //=======================================================================
324 bool SMESH_MeshEditor::InverseDiag (const SMDS_MeshNode * theNode1,
325 const SMDS_MeshNode * theNode2)
327 MESSAGE( "::InverseDiag()" );
329 const SMDS_MeshElement *tr1, *tr2;
330 if ( !findTriangles( theNode1, theNode2, tr1, tr2 ))
333 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( tr1 );
334 if (!F1) return false;
335 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( tr2 );
336 if (!F2) return false;
338 // 1 +--+ A tr1: ( 1 A B ) A->2 ( 1 2 B ) 1 +--+ A
339 // | /| tr2: ( B A 2 ) B->1 ( 1 A 2 ) |\ |
343 // put nodes in array
344 // and find indices of 1,2 and of A in tr1 and of B in tr2
345 int i, iA1 = 0, i1 = 0;
346 const SMDS_MeshNode* aNodes1 [3];
347 SMDS_ElemIteratorPtr it;
348 for (i = 0, it = tr1->nodesIterator(); it->more(); i++ ) {
349 aNodes1[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
350 if ( aNodes1[ i ] == theNode1 )
351 iA1 = i; // node A in tr1
352 else if ( aNodes1[ i ] != theNode2 )
356 const SMDS_MeshNode* aNodes2 [3];
357 for (i = 0, it = tr2->nodesIterator(); it->more(); i++ ) {
358 aNodes2[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
359 if ( aNodes2[ i ] == theNode2 )
360 iB2 = i; // node B in tr2
361 else if ( aNodes2[ i ] != theNode1 )
365 // nodes 1 and 2 should not be the same
366 if ( aNodes1[ i1 ] == aNodes2[ i2 ] )
370 aNodes1[ iA1 ] = aNodes2[ i2 ];
372 aNodes2[ iB2 ] = aNodes1[ i1 ];
374 //MESSAGE( tr1 << tr2 );
376 GetMeshDS()->ChangeElementNodes( tr1, aNodes1, 3 );
377 GetMeshDS()->ChangeElementNodes( tr2, aNodes2, 3 );
379 //MESSAGE( tr1 << tr2 );
385 //=======================================================================
386 //function : getQuadrangleNodes
387 //purpose : fill theQuadNodes - nodes of a quadrangle resulting from
388 // fusion of triangles tr1 and tr2 having shared link on
389 // theNode1 and theNode2
390 //=======================================================================
392 bool getQuadrangleNodes(const SMDS_MeshNode * theQuadNodes [],
393 const SMDS_MeshNode * theNode1,
394 const SMDS_MeshNode * theNode2,
395 const SMDS_MeshElement * tr1,
396 const SMDS_MeshElement * tr2 )
398 // find the 4-th node to insert into tr1
399 const SMDS_MeshNode* n4 = 0;
400 SMDS_ElemIteratorPtr it = tr2->nodesIterator();
401 while ( !n4 && it->more() )
403 const SMDS_MeshNode * n = static_cast<const SMDS_MeshNode*>( it->next() );
404 bool isDiag = ( n == theNode1 || n == theNode2 );
408 // Make an array of nodes to be in a quadrangle
409 int iNode = 0, iFirstDiag = -1;
410 it = tr1->nodesIterator();
413 const SMDS_MeshNode * n = static_cast<const SMDS_MeshNode*>( it->next() );
414 bool isDiag = ( n == theNode1 || n == theNode2 );
417 if ( iFirstDiag < 0 )
419 else if ( iNode - iFirstDiag == 1 )
420 theQuadNodes[ iNode++ ] = n4; // insert the 4-th node between diagonal nodes
424 return false; // tr1 and tr2 should not have all the same nodes
426 theQuadNodes[ iNode++ ] = n;
428 if ( iNode == 3 ) // diagonal nodes have 0 and 2 indices
429 theQuadNodes[ iNode ] = n4;
434 //=======================================================================
435 //function : DeleteDiag
436 //purpose : Replace two neighbour triangles sharing theNode1-theNode2 link
437 // with a quadrangle built on the same 4 nodes.
438 // Return false if proper faces not found
439 //=======================================================================
441 bool SMESH_MeshEditor::DeleteDiag (const SMDS_MeshNode * theNode1,
442 const SMDS_MeshNode * theNode2)
444 MESSAGE( "::DeleteDiag()" );
446 const SMDS_MeshElement *tr1, *tr2;
447 if ( !findTriangles( theNode1, theNode2, tr1, tr2 ))
450 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( tr1 );
451 if (!F1) return false;
452 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( tr2 );
453 if (!F2) return false;
455 const SMDS_MeshNode* aNodes [ 4 ];
456 if ( ! getQuadrangleNodes( aNodes, theNode1, theNode2, tr1, tr2 ))
459 //MESSAGE( endl << tr1 << tr2 );
461 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 4 );
462 GetMeshDS()->RemoveElement( tr2 );
464 //MESSAGE( endl << tr1 );
469 //=======================================================================
470 //function : Reorient
471 //purpose : Reverse theElement orientation
472 //=======================================================================
474 bool SMESH_MeshEditor::Reorient (const SMDS_MeshElement * theElem)
478 SMDS_ElemIteratorPtr it = theElem->nodesIterator();
479 if ( !it || !it->more() )
482 switch ( theElem->GetType() ) {
487 int i = theElem->NbNodes();
488 vector<const SMDS_MeshNode*> aNodes( i );
490 aNodes[ --i ]= static_cast<const SMDS_MeshNode*>( it->next() );
491 return GetMeshDS()->ChangeElementNodes( theElem, &aNodes[0], theElem->NbNodes() );
495 SMDS_VolumeTool vTool;
496 if ( !vTool.Set( theElem ))
499 return GetMeshDS()->ChangeElementNodes( theElem, vTool.GetNodes(), vTool.NbNodes() );
507 //=======================================================================
508 //function : getBadRate
510 //=======================================================================
512 static double getBadRate (const SMDS_MeshElement* theElem,
513 SMESH::Controls::NumericalFunctorPtr& theCrit)
515 SMESH::Controls::TSequenceOfXYZ P;
516 if ( !theElem || !theCrit->GetPoints( theElem, P ))
518 return theCrit->GetBadRate( theCrit->GetValue( P ), theElem->NbNodes() );
521 //=======================================================================
522 //function : QuadToTri
523 //purpose : Cut quadrangles into triangles.
524 // theCrit is used to select a diagonal to cut
525 //=======================================================================
527 bool SMESH_MeshEditor::QuadToTri (set<const SMDS_MeshElement*> & theElems,
528 SMESH::Controls::NumericalFunctorPtr theCrit)
530 MESSAGE( "::QuadToTri()" );
532 if ( !theCrit.get() )
535 SMESHDS_Mesh * aMesh = GetMeshDS();
537 set< const SMDS_MeshElement * >::iterator itElem;
538 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
540 const SMDS_MeshElement* elem = (*itElem);
541 if ( !elem || elem->GetType() != SMDSAbs_Face || elem->NbNodes() != 4 )
544 // retrieve element nodes
545 const SMDS_MeshNode* aNodes [4];
546 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
548 while ( itN->more() )
549 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
551 // compare two sets of possible triangles
552 double aBadRate1, aBadRate2; // to what extent a set is bad
553 SMDS_FaceOfNodes tr1 ( aNodes[0], aNodes[1], aNodes[2] );
554 SMDS_FaceOfNodes tr2 ( aNodes[2], aNodes[3], aNodes[0] );
555 aBadRate1 = getBadRate( &tr1, theCrit ) + getBadRate( &tr2, theCrit );
557 SMDS_FaceOfNodes tr3 ( aNodes[1], aNodes[2], aNodes[3] );
558 SMDS_FaceOfNodes tr4 ( aNodes[3], aNodes[0], aNodes[1] );
559 aBadRate2 = getBadRate( &tr3, theCrit ) + getBadRate( &tr4, theCrit );
561 int aShapeId = FindShape( elem );
562 //MESSAGE( "aBadRate1 = " << aBadRate1 << "; aBadRate2 = " << aBadRate2
563 // << " ShapeID = " << aShapeId << endl << elem );
565 if ( aBadRate1 <= aBadRate2 ) {
566 // tr1 + tr2 is better
567 aMesh->ChangeElementNodes( elem, aNodes, 3 );
568 //MESSAGE( endl << elem );
570 elem = aMesh->AddFace( aNodes[2], aNodes[3], aNodes[0] );
573 // tr3 + tr4 is better
574 aMesh->ChangeElementNodes( elem, &aNodes[1], 3 );
575 //MESSAGE( endl << elem );
577 elem = aMesh->AddFace( aNodes[3], aNodes[0], aNodes[1] );
579 //MESSAGE( endl << elem );
581 // put a new triangle on the same shape
583 aMesh->SetMeshElementOnShape( elem, aShapeId );
589 //=======================================================================
590 //function : AddToSameGroups
591 //purpose : add elemToAdd to the groups the elemInGroups belongs to
592 //=======================================================================
594 void SMESH_MeshEditor::AddToSameGroups (const SMDS_MeshElement* elemToAdd,
595 const SMDS_MeshElement* elemInGroups,
596 SMESHDS_Mesh * aMesh)
598 const set<SMESHDS_GroupBase*>& groups = aMesh->GetGroups();
599 set<SMESHDS_GroupBase*>::const_iterator grIt = groups.begin();
600 for ( ; grIt != groups.end(); grIt++ ) {
601 SMESHDS_Group* group = dynamic_cast<SMESHDS_Group*>( *grIt );
602 if ( group && group->SMDSGroup().Contains( elemInGroups ))
603 group->SMDSGroup().Add( elemToAdd );
607 //=======================================================================
608 //function : QuadToTri
609 //purpose : Cut quadrangles into triangles.
610 // theCrit is used to select a diagonal to cut
611 //=======================================================================
613 bool SMESH_MeshEditor::QuadToTri (std::set<const SMDS_MeshElement*> & theElems,
614 const bool the13Diag)
616 MESSAGE( "::QuadToTri()" );
618 SMESHDS_Mesh * aMesh = GetMeshDS();
620 set< const SMDS_MeshElement * >::iterator itElem;
621 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
623 const SMDS_MeshElement* elem = (*itElem);
624 if ( !elem || elem->GetType() != SMDSAbs_Face || elem->NbNodes() != 4 )
627 // retrieve element nodes
628 const SMDS_MeshNode* aNodes [4];
629 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
631 while ( itN->more() )
632 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
634 int aShapeId = FindShape( elem );
635 const SMDS_MeshElement* newElem = 0;
638 aMesh->ChangeElementNodes( elem, aNodes, 3 );
639 newElem = aMesh->AddFace( aNodes[2], aNodes[3], aNodes[0] );
643 aMesh->ChangeElementNodes( elem, &aNodes[1], 3 );
644 newElem = aMesh->AddFace( aNodes[3], aNodes[0], aNodes[1] );
647 // put a new triangle on the same shape and add to the same groups
650 aMesh->SetMeshElementOnShape( newElem, aShapeId );
652 AddToSameGroups( newElem, elem, aMesh );
658 //=======================================================================
659 //function : getAngle
661 //=======================================================================
663 double getAngle(const SMDS_MeshElement * tr1,
664 const SMDS_MeshElement * tr2,
665 const SMDS_MeshNode * n1,
666 const SMDS_MeshNode * n2)
668 double angle = 2*PI; // bad angle
671 SMESH::Controls::TSequenceOfXYZ P1, P2;
672 if ( !SMESH::Controls::NumericalFunctor::GetPoints( tr1, P1 ) ||
673 !SMESH::Controls::NumericalFunctor::GetPoints( tr2, P2 ))
675 gp_Vec N1 = gp_Vec( P1(2) - P1(1) ) ^ gp_Vec( P1(3) - P1(1) );
676 if ( N1.SquareMagnitude() <= gp::Resolution() )
678 gp_Vec N2 = gp_Vec( P2(2) - P2(1) ) ^ gp_Vec( P2(3) - P2(1) );
679 if ( N2.SquareMagnitude() <= gp::Resolution() )
682 // find the first diagonal node n1 in the triangles:
683 // take in account a diagonal link orientation
684 const SMDS_MeshElement *nFirst[2], *tr[] = { tr1, tr2 };
685 for ( int t = 0; t < 2; t++ )
687 SMDS_ElemIteratorPtr it = tr[ t ]->nodesIterator();
688 int i = 0, iDiag = -1;
689 while ( it->more()) {
690 const SMDS_MeshElement *n = it->next();
691 if ( n == n1 || n == n2 )
695 if ( i - iDiag == 1 )
696 nFirst[ t ] = ( n == n1 ? n2 : n1 );
704 if ( nFirst[ 0 ] == nFirst[ 1 ] )
707 angle = N1.Angle( N2 );
712 // =================================================
713 // class generating a unique ID for a pair of nodes
714 // and able to return nodes by that ID
715 // =================================================
720 LinkID_Gen( const SMESHDS_Mesh* theMesh )
721 :myMesh( theMesh ), myMaxID( theMesh->MaxNodeID() + 1)
724 long GetLinkID (const SMDS_MeshNode * n1,
725 const SMDS_MeshNode * n2) const
727 return ( Min(n1->GetID(),n2->GetID()) * myMaxID + Max(n1->GetID(),n2->GetID()));
730 bool GetNodes (const long theLinkID,
731 const SMDS_MeshNode* & theNode1,
732 const SMDS_MeshNode* & theNode2) const
734 theNode1 = myMesh->FindNode( theLinkID / myMaxID );
735 if ( !theNode1 ) return false;
736 theNode2 = myMesh->FindNode( theLinkID % myMaxID );
737 if ( !theNode2 ) return false;
743 const SMESHDS_Mesh* myMesh;
747 //=======================================================================
748 //function : TriToQuad
749 //purpose : Fuse neighbour triangles into quadrangles.
750 // theCrit is used to select a neighbour to fuse with.
751 // theMaxAngle is a max angle between element normals at which
752 // fusion is still performed.
753 //=======================================================================
755 bool SMESH_MeshEditor::TriToQuad (set<const SMDS_MeshElement*> & theElems,
756 SMESH::Controls::NumericalFunctorPtr theCrit,
757 const double theMaxAngle)
759 MESSAGE( "::TriToQuad()" );
761 if ( !theCrit.get() )
764 SMESHDS_Mesh * aMesh = GetMeshDS();
765 LinkID_Gen aLinkID_Gen( aMesh );
768 // Prepare data for algo: build
769 // 1. map of elements with their linkIDs
770 // 2. map of linkIDs with their elements
772 map< long, list< const SMDS_MeshElement* > > mapLi_listEl;
773 map< long, list< const SMDS_MeshElement* > >::iterator itLE;
774 map< const SMDS_MeshElement*, set< long > > mapEl_setLi;
775 map< const SMDS_MeshElement*, set< long > >::iterator itEL;
777 set<const SMDS_MeshElement*>::iterator itElem;
778 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
780 const SMDS_MeshElement* elem = (*itElem);
781 if ( !elem || elem->NbNodes() != 3 )
784 // retrieve element nodes
785 const SMDS_MeshNode* aNodes [4];
786 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
788 while ( itN->more() )
789 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
791 aNodes[ 3 ] = aNodes[ 0 ];
794 for ( i = 0; i < 3; i++ )
796 long linkID = aLinkID_Gen.GetLinkID( aNodes[ i ], aNodes[ i+1 ] );
797 // check if elements sharing a link can be fused
798 itLE = mapLi_listEl.find( linkID );
799 if ( itLE != mapLi_listEl.end() )
801 if ((*itLE).second.size() > 1 ) // consider only 2 elems adjacent by a link
803 const SMDS_MeshElement* elem2 = (*itLE).second.front();
804 // if ( FindShape( elem ) != FindShape( elem2 ))
805 // continue; // do not fuse triangles laying on different shapes
806 if ( getAngle( elem, elem2, aNodes[i], aNodes[i+1] ) > theMaxAngle )
807 continue; // avoid making badly shaped quads
808 (*itLE).second.push_back( elem );
811 mapLi_listEl[ linkID ].push_back( elem );
812 mapEl_setLi [ elem ].insert( linkID );
815 // Clean the maps from the links shared by a sole element, ie
816 // links to which only one element is bound in mapLi_listEl
818 for ( itLE = mapLi_listEl.begin(); itLE != mapLi_listEl.end(); itLE++ )
820 int nbElems = (*itLE).second.size();
822 const SMDS_MeshElement* elem = (*itLE).second.front();
823 long link = (*itLE).first;
824 mapEl_setLi[ elem ].erase( link );
825 if ( mapEl_setLi[ elem ].empty() )
826 mapEl_setLi.erase( elem );
830 // Algo: fuse triangles into quadrangles
832 while ( ! mapEl_setLi.empty() )
834 // Look for the start element:
835 // the element having the least nb of shared links
837 const SMDS_MeshElement* startElem = 0;
839 for ( itEL = mapEl_setLi.begin(); itEL != mapEl_setLi.end(); itEL++ )
841 int nbLinks = (*itEL).second.size();
842 if ( nbLinks < minNbLinks )
844 startElem = (*itEL).first;
845 minNbLinks = nbLinks;
846 if ( minNbLinks == 1 )
851 // search elements to fuse starting from startElem or links of elements
852 // fused earlyer - startLinks
853 list< long > startLinks;
854 while ( startElem || !startLinks.empty() )
856 while ( !startElem && !startLinks.empty() )
858 // Get an element to start, by a link
859 long linkId = startLinks.front();
860 startLinks.pop_front();
861 itLE = mapLi_listEl.find( linkId );
862 if ( itLE != mapLi_listEl.end() )
864 list< const SMDS_MeshElement* > & listElem = (*itLE).second;
865 list< const SMDS_MeshElement* >::iterator itE = listElem.begin();
866 for ( ; itE != listElem.end() ; itE++ )
867 if ( mapEl_setLi.find( (*itE) ) != mapEl_setLi.end() )
869 mapLi_listEl.erase( itLE );
875 // Get candidates to be fused
877 const SMDS_MeshElement *tr1 = startElem, *tr2 = 0, *tr3 = 0;
880 ASSERT( mapEl_setLi.find( tr1 ) != mapEl_setLi.end() );
881 set< long >& setLi = mapEl_setLi[ tr1 ];
882 ASSERT( !setLi.empty() );
883 set< long >::iterator itLi;
884 for ( itLi = setLi.begin(); itLi != setLi.end(); itLi++ )
886 long linkID = (*itLi);
887 itLE = mapLi_listEl.find( linkID );
888 if ( itLE == mapLi_listEl.end() )
890 const SMDS_MeshElement* elem = (*itLE).second.front();
892 elem = (*itLE).second.back();
893 mapLi_listEl.erase( itLE );
894 if ( mapEl_setLi.find( elem ) == mapEl_setLi.end())
907 // add other links of elem to list of links to re-start from
908 set< long >& links = mapEl_setLi[ elem ];
909 set< long >::iterator it;
910 for ( it = links.begin(); it != links.end(); it++ )
912 long linkID2 = (*it);
913 if ( linkID2 != linkID )
914 startLinks.push_back( linkID2 );
918 // Get nodes of possible quadrangles
920 const SMDS_MeshNode *n12 [4], *n13 [4];
921 bool Ok12 = false, Ok13 = false;
922 const SMDS_MeshNode *linkNode1, *linkNode2;
924 aLinkID_Gen.GetNodes( link12, linkNode1, linkNode2 ) &&
925 getQuadrangleNodes( n12, linkNode1, linkNode2, tr1, tr2 ))
928 aLinkID_Gen.GetNodes( link13, linkNode1, linkNode2 ) &&
929 getQuadrangleNodes( n13, linkNode1, linkNode2, tr1, tr3 ))
932 // Choose a pair to fuse
936 SMDS_FaceOfNodes quad12 ( n12[ 0 ], n12[ 1 ], n12[ 2 ], n12[ 3 ] );
937 SMDS_FaceOfNodes quad13 ( n13[ 0 ], n13[ 1 ], n13[ 2 ], n13[ 3 ] );
938 double aBadRate12 = getBadRate( &quad12, theCrit );
939 double aBadRate13 = getBadRate( &quad13, theCrit );
940 if ( aBadRate13 < aBadRate12 )
948 // and remove fused elems and removed links from the maps
950 mapEl_setLi.erase( tr1 );
953 mapEl_setLi.erase( tr2 );
954 mapLi_listEl.erase( link12 );
955 aMesh->ChangeElementNodes( tr1, n12, 4 );
956 aMesh->RemoveElement( tr2 );
960 mapEl_setLi.erase( tr3 );
961 mapLi_listEl.erase( link13 );
962 aMesh->ChangeElementNodes( tr1, n13, 4 );
963 aMesh->RemoveElement( tr3 );
966 // Next element to fuse: the rejected one
968 startElem = Ok12 ? tr3 : tr2;
970 } // if ( startElem )
971 } // while ( startElem || !startLinks.empty() )
972 } // while ( ! mapEl_setLi.empty() )
978 /*#define DUMPSO(txt) \
979 // cout << txt << endl;
980 //=============================================================================
984 //=============================================================================
985 static void swap( int i1, int i2, int idNodes[], gp_Pnt P[] )
989 int tmp = idNodes[ i1 ];
990 idNodes[ i1 ] = idNodes[ i2 ];
992 gp_Pnt Ptmp = P[ i1 ];
995 DUMPSO( i1 << "(" << idNodes[ i2 ] << ") <-> " << i2 << "(" << idNodes[ i1 ] << ")");
998 //=======================================================================
999 //function : SortQuadNodes
1000 //purpose : Set 4 nodes of a quadrangle face in a good order.
1001 // Swap 1<->2 or 2<->3 nodes and correspondingly return
1003 //=======================================================================
1005 int SMESH_MeshEditor::SortQuadNodes (const SMDS_Mesh * theMesh,
1010 for ( i = 0; i < 4; i++ ) {
1011 const SMDS_MeshNode *n = theMesh->FindNode( idNodes[i] );
1013 P[ i ].SetCoord( n->X(), n->Y(), n->Z() );
1016 gp_Vec V1(P[0], P[1]);
1017 gp_Vec V2(P[0], P[2]);
1018 gp_Vec V3(P[0], P[3]);
1020 gp_Vec Cross1 = V1 ^ V2;
1021 gp_Vec Cross2 = V2 ^ V3;
1024 if (Cross1.Dot(Cross2) < 0)
1029 if (Cross1.Dot(Cross2) < 0)
1033 swap ( i, i + 1, idNodes, P );
1035 // for ( int ii = 0; ii < 4; ii++ ) {
1036 // const SMDS_MeshNode *n = theMesh->FindNode( idNodes[ii] );
1037 // DUMPSO( ii << "(" << idNodes[ii] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1043 //=======================================================================
1044 //function : SortHexaNodes
1045 //purpose : Set 8 nodes of a hexahedron in a good order.
1046 // Return success status
1047 //=======================================================================
1049 bool SMESH_MeshEditor::SortHexaNodes (const SMDS_Mesh * theMesh,
1054 DUMPSO( "INPUT: ========================================");
1055 for ( i = 0; i < 8; i++ ) {
1056 const SMDS_MeshNode *n = theMesh->FindNode( idNodes[i] );
1057 if ( !n ) return false;
1058 P[ i ].SetCoord( n->X(), n->Y(), n->Z() );
1059 DUMPSO( i << "(" << idNodes[i] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1061 DUMPSO( "========================================");
1064 set<int> faceNodes; // ids of bottom face nodes, to be found
1065 set<int> checkedId1; // ids of tried 2-nd nodes
1066 Standard_Real leastDist = DBL_MAX; // dist of the 4-th node from 123 plane
1067 const Standard_Real tol = 1.e-6; // tolerance to find nodes in plane
1068 int iMin, iLoop1 = 0;
1070 // Loop to try the 2-nd nodes
1072 while ( leastDist > DBL_MIN && ++iLoop1 < 8 )
1074 // Find not checked 2-nd node
1075 for ( i = 1; i < 8; i++ )
1076 if ( checkedId1.find( idNodes[i] ) == checkedId1.end() ) {
1077 int id1 = idNodes[i];
1078 swap ( 1, i, idNodes, P );
1079 checkedId1.insert ( id1 );
1083 // Find the 3-d node so that 1-2-3 triangle to be on a hexa face,
1084 // ie that all but meybe one (id3 which is on the same face) nodes
1085 // lay on the same side from the triangle plane.
1087 bool manyInPlane = false; // more than 4 nodes lay in plane
1089 while ( ++iLoop2 < 6 ) {
1091 // get 1-2-3 plane coeffs
1092 Standard_Real A, B, C, D;
1093 gp_Vec N = gp_Vec (P[0], P[1]).Crossed( gp_Vec (P[0], P[2]) );
1094 if ( N.SquareMagnitude() > gp::Resolution() )
1096 gp_Pln pln ( P[0], N );
1097 pln.Coefficients( A, B, C, D );
1099 // find the node (iMin) closest to pln
1100 Standard_Real dist[ 8 ], minDist = DBL_MAX;
1102 for ( i = 3; i < 8; i++ ) {
1103 dist[i] = A * P[i].X() + B * P[i].Y() + C * P[i].Z() + D;
1104 if ( fabs( dist[i] ) < minDist ) {
1105 minDist = fabs( dist[i] );
1108 if ( fabs( dist[i] ) <= tol )
1109 idInPln.insert( idNodes[i] );
1112 // there should not be more than 4 nodes in bottom plane
1113 if ( idInPln.size() > 1 )
1115 DUMPSO( "### idInPln.size() = " << idInPln.size());
1116 // idInPlane does not contain the first 3 nodes
1117 if ( manyInPlane || idInPln.size() == 5)
1118 return false; // all nodes in one plane
1121 // set the 1-st node to be not in plane
1122 for ( i = 3; i < 8; i++ ) {
1123 if ( idInPln.find( idNodes[ i ] ) == idInPln.end() ) {
1124 DUMPSO( "### Reset 0-th node");
1125 swap( 0, i, idNodes, P );
1130 // reset to re-check second nodes
1131 leastDist = DBL_MAX;
1135 break; // from iLoop2;
1138 // check that the other 4 nodes are on the same side
1139 bool sameSide = true;
1140 bool isNeg = dist[ iMin == 3 ? 4 : 3 ] <= 0.;
1141 for ( i = 3; sameSide && i < 8; i++ ) {
1143 sameSide = ( isNeg == dist[i] <= 0.);
1146 // keep best solution
1147 if ( sameSide && minDist < leastDist ) {
1148 leastDist = minDist;
1150 faceNodes.insert( idNodes[ 1 ] );
1151 faceNodes.insert( idNodes[ 2 ] );
1152 faceNodes.insert( idNodes[ iMin ] );
1153 DUMPSO( "loop " << iLoop2 << " id2 " << idNodes[ 1 ] << " id3 " << idNodes[ 2 ]
1154 << " leastDist = " << leastDist);
1155 if ( leastDist <= DBL_MIN )
1160 // set next 3-d node to check
1161 int iNext = 2 + iLoop2;
1163 DUMPSO( "Try 2-nd");
1164 swap ( 2, iNext, idNodes, P );
1166 } // while ( iLoop2 < 6 )
1169 if ( faceNodes.empty() ) return false;
1171 // Put the faceNodes in proper places
1172 for ( i = 4; i < 8; i++ ) {
1173 if ( faceNodes.find( idNodes[ i ] ) != faceNodes.end() ) {
1174 // find a place to put
1176 while ( faceNodes.find( idNodes[ iTo ] ) != faceNodes.end() )
1178 DUMPSO( "Set faceNodes");
1179 swap ( iTo, i, idNodes, P );
1184 // Set nodes of the found bottom face in good order
1185 DUMPSO( " Found bottom face: ");
1186 i = SortQuadNodes( theMesh, idNodes );
1188 gp_Pnt Ptmp = P[ i ];
1193 // for ( int ii = 0; ii < 4; ii++ ) {
1194 // const SMDS_MeshNode *n = theMesh->FindNode( idNodes[ii] );
1195 // DUMPSO( ii << "(" << idNodes[ii] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1198 // Gravity center of the top and bottom faces
1199 gp_Pnt aGCb = ( P[0].XYZ() + P[1].XYZ() + P[2].XYZ() + P[3].XYZ() ) / 4.;
1200 gp_Pnt aGCt = ( P[4].XYZ() + P[5].XYZ() + P[6].XYZ() + P[7].XYZ() ) / 4.;
1202 // Get direction from the bottom to the top face
1203 gp_Vec upDir ( aGCb, aGCt );
1204 Standard_Real upDirSize = upDir.Magnitude();
1205 if ( upDirSize <= gp::Resolution() ) return false;
1208 // Assure that the bottom face normal points up
1209 gp_Vec Nb = gp_Vec (P[0], P[1]).Crossed( gp_Vec (P[0], P[2]) );
1210 Nb += gp_Vec (P[0], P[2]).Crossed( gp_Vec (P[0], P[3]) );
1211 if ( Nb.Dot( upDir ) < 0 ) {
1212 DUMPSO( "Reverse bottom face");
1213 swap( 1, 3, idNodes, P );
1216 // Find 5-th node - the one closest to the 1-st among the last 4 nodes.
1217 Standard_Real minDist = DBL_MAX;
1218 for ( i = 4; i < 8; i++ ) {
1219 // projection of P[i] to the plane defined by P[0] and upDir
1220 gp_Pnt Pp = P[i].Translated( upDir * ( upDir.Dot( gp_Vec( P[i], P[0] ))));
1221 Standard_Real sqDist = P[0].SquareDistance( Pp );
1222 if ( sqDist < minDist ) {
1227 DUMPSO( "Set 4-th");
1228 swap ( 4, iMin, idNodes, P );
1230 // Set nodes of the top face in good order
1231 DUMPSO( "Sort top face");
1232 i = SortQuadNodes( theMesh, &idNodes[4] );
1235 gp_Pnt Ptmp = P[ i ];
1240 // Assure that direction of the top face normal is from the bottom face
1241 gp_Vec Nt = gp_Vec (P[4], P[5]).Crossed( gp_Vec (P[4], P[6]) );
1242 Nt += gp_Vec (P[4], P[6]).Crossed( gp_Vec (P[4], P[7]) );
1243 if ( Nt.Dot( upDir ) < 0 ) {
1244 DUMPSO( "Reverse top face");
1245 swap( 5, 7, idNodes, P );
1248 // DUMPSO( "OUTPUT: ========================================");
1249 // for ( i = 0; i < 8; i++ ) {
1250 // float *p = ugrid->GetPoint(idNodes[i]);
1251 // DUMPSO( i << "(" << idNodes[i] << ") : " << p[0] << " " << p[1] << " " << p[2]);
1257 //=======================================================================
1258 //function : laplacianSmooth
1259 //purpose : pulls theNode toward the center of surrounding nodes directly
1260 // connected to that node along an element edge
1261 //=======================================================================
1263 void laplacianSmooth(SMESHDS_Mesh * theMesh,
1264 const SMDS_MeshNode* theNode,
1265 const set<const SMDS_MeshElement*> & theElems,
1266 const set<const SMDS_MeshNode*> & theFixedNodes)
1268 // find surrounding nodes
1269 set< const SMDS_MeshNode* > nodeSet;
1270 SMDS_ElemIteratorPtr elemIt = theNode->GetInverseElementIterator();
1271 while ( elemIt->more() )
1273 const SMDS_MeshElement* elem = elemIt->next();
1274 if ( theElems.find( elem ) == theElems.end() )
1279 int i = 0, iNode = 0;
1280 const SMDS_MeshNode* aNodes [4];
1281 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1282 while ( itN->more() )
1284 aNodes[ i ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1285 if ( aNodes[ i ] == theNode )
1288 nodeSet.insert( aNodes[ i ] );
1291 if ( elem->NbNodes() == 4 ) { // remove an opposite node
1292 iNode += ( iNode < 2 ) ? 2 : -2;
1293 nodeSet.erase( aNodes[ iNode ]);
1297 // compute new coodrs
1298 double coord[] = { 0., 0., 0. };
1299 set< const SMDS_MeshNode* >::iterator nodeSetIt = nodeSet.begin();
1300 for ( ; nodeSetIt != nodeSet.end(); nodeSetIt++ ) {
1301 const SMDS_MeshNode* node = (*nodeSetIt);
1302 coord[0] += node->X();
1303 coord[1] += node->Y();
1304 coord[2] += node->Z();
1306 double nbNodes = nodeSet.size();
1307 theMesh->MoveNode (theNode,
1313 //=======================================================================
1314 //function : centroidalSmooth
1315 //purpose : pulls theNode toward the element-area-weighted centroid of the
1316 // surrounding elements
1317 //=======================================================================
1319 void centroidalSmooth(SMESHDS_Mesh * theMesh,
1320 const SMDS_MeshNode* theNode,
1321 const set<const SMDS_MeshElement*> & theElems,
1322 const set<const SMDS_MeshNode*> & theFixedNodes)
1324 gp_XYZ aNewXYZ(0.,0.,0.);
1325 SMESH::Controls::Area anAreaFunc;
1326 double totalArea = 0.;
1329 SMDS_ElemIteratorPtr elemIt = theNode->GetInverseElementIterator();
1330 while ( elemIt->more() )
1332 const SMDS_MeshElement* elem = elemIt->next();
1333 if ( theElems.find( elem ) == theElems.end() )
1338 gp_XYZ elemCenter(0.,0.,0.);
1339 SMESH::Controls::TSequenceOfXYZ aNodePoints;
1340 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1341 while ( itN->more() )
1343 const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>( itN->next() );
1344 gp_XYZ aP( aNode->X(), aNode->Y(), aNode->Z() );
1345 aNodePoints.push_back( aP );
1348 double elemArea = anAreaFunc.GetValue( aNodePoints );
1349 totalArea += elemArea;
1350 elemCenter /= elem->NbNodes();
1351 aNewXYZ += elemCenter * elemArea;
1353 aNewXYZ /= totalArea;
1354 theMesh->MoveNode (theNode,
1360 //=======================================================================
1362 //purpose : Smooth theElements during theNbIterations or until a worst
1363 // element has aspect ratio <= theTgtAspectRatio.
1364 // Aspect Ratio varies in range [1.0, inf].
1365 // If theElements is empty, the whole mesh is smoothed.
1366 // theFixedNodes contains additionally fixed nodes. Nodes built
1367 // on edges and boundary nodes are always fixed.
1368 //=======================================================================
1370 void SMESH_MeshEditor::Smooth (set<const SMDS_MeshElement*> & theElems,
1371 set<const SMDS_MeshNode*> & theFixedNodes,
1372 const SmoothMethod theSmoothMethod,
1373 const int theNbIterations,
1374 double theTgtAspectRatio)
1376 MESSAGE((theSmoothMethod==LAPLACIAN ? "LAPLACIAN" : "CENTROIDAL") << "--::Smooth()");
1378 SMESHDS_Mesh* aMesh = GetMeshDS();
1379 if ( theElems.empty() ) {
1381 SMDS_FaceIteratorPtr fIt = aMesh->facesIterator();
1382 while ( fIt->more() )
1383 theElems.insert( fIt->next() );
1386 set<const SMDS_MeshNode*> setMovableNodes;
1388 // Fill setMovableNodes
1390 map< const SMDS_MeshNode*, int > mapNodeNbFaces;
1391 set< const SMDS_MeshElement* >::iterator itElem;
1392 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1394 const SMDS_MeshElement* elem = (*itElem);
1395 if ( !elem || elem->GetType() != SMDSAbs_Face )
1398 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1399 while ( itN->more() ) {
1400 const SMDS_MeshNode* node =
1401 static_cast<const SMDS_MeshNode*>( itN->next() );
1403 if ( theFixedNodes.find( node ) != theFixedNodes.end() )
1406 // if node is on edge => it is fixed
1407 SMDS_PositionPtr aPositionPtr = node->GetPosition();
1408 if ( aPositionPtr.get() &&
1409 (aPositionPtr->GetTypeOfPosition() == SMDS_TOP_EDGE ||
1410 aPositionPtr->GetTypeOfPosition() == SMDS_TOP_VERTEX)) {
1411 theFixedNodes.insert( node );
1414 // fill mapNodeNbFaces in order to detect fixed boundary nodes
1415 map<const SMDS_MeshNode*,int>::iterator nodeNbFacesIt =
1416 mapNodeNbFaces.find ( node );
1417 if ( nodeNbFacesIt == mapNodeNbFaces.end() )
1418 mapNodeNbFaces.insert( map<const SMDS_MeshNode*,int>::value_type( node, 1 ));
1420 (*nodeNbFacesIt).second++;
1423 // put not fixed nodes in setMovableNodes
1424 map<const SMDS_MeshNode*,int>::iterator nodeNbFacesIt =
1425 mapNodeNbFaces.begin();
1426 for ( ; nodeNbFacesIt != mapNodeNbFaces.end(); nodeNbFacesIt++ ) {
1427 const SMDS_MeshNode* node = (*nodeNbFacesIt).first;
1428 // a node is on free boundary if it is shared by 1-2 faces
1429 if ( (*nodeNbFacesIt).second > 2 )
1430 setMovableNodes.insert( node );
1432 theFixedNodes.insert( node );
1437 if ( theTgtAspectRatio < 1.0 )
1438 theTgtAspectRatio = 1.0;
1440 SMESH::Controls::AspectRatio aQualityFunc;
1442 for ( int it = 0; it < theNbIterations; it++ )
1444 Standard_Real maxDisplacement = 0.;
1445 set<const SMDS_MeshNode*>::iterator movableNodesIt
1446 = setMovableNodes.begin();
1447 for ( ; movableNodesIt != setMovableNodes.end(); movableNodesIt++ )
1449 const SMDS_MeshNode* node = (*movableNodesIt);
1450 gp_XYZ aPrevPos ( node->X(), node->Y(), node->Z() );
1453 if ( theSmoothMethod == LAPLACIAN )
1454 laplacianSmooth( aMesh, node, theElems, theFixedNodes );
1456 centroidalSmooth( aMesh, node, theElems, theFixedNodes );
1459 gp_XYZ aNewPos ( node->X(), node->Y(), node->Z() );
1460 Standard_Real aDispl = (aPrevPos - aNewPos).SquareModulus();
1461 if ( aDispl > maxDisplacement )
1462 maxDisplacement = aDispl;
1464 // no node movement => exit
1465 if ( maxDisplacement < 1.e-16 ) {
1466 MESSAGE("-- no node movement -- maxDisplacement: " << maxDisplacement << " it "<< it);
1470 // check elements quality
1471 double maxRatio = 0;
1472 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1474 const SMDS_MeshElement* elem = (*itElem);
1475 if ( !elem || elem->GetType() != SMDSAbs_Face )
1477 SMESH::Controls::TSequenceOfXYZ aPoints;
1478 if ( aQualityFunc.GetPoints( elem, aPoints )) {
1479 double aValue = aQualityFunc.GetValue( aPoints );
1480 if ( aValue > maxRatio )
1484 if ( maxRatio <= theTgtAspectRatio ) {
1485 MESSAGE("-- quality achived -- maxRatio " << maxRatio << " it "<< it);
1488 if (it+1 == theNbIterations) {
1489 MESSAGE("-- Iteration limit exceeded --");
1494 //=======================================================================
1495 //function : isReverse
1496 //purpose : Return true if normal of prevNodes is not co-directied with
1497 // gp_Vec(prevNodes[iNotSame],nextNodes[iNotSame]).
1498 // iNotSame is where prevNodes and nextNodes are different
1499 //=======================================================================
1501 static bool isReverse(const SMDS_MeshNode* prevNodes[],
1502 const SMDS_MeshNode* nextNodes[],
1506 int iBeforeNotSame = ( iNotSame == 0 ? nbNodes - 1 : iNotSame - 1 );
1507 int iAfterNotSame = ( iNotSame + 1 == nbNodes ? 0 : iNotSame + 1 );
1509 const SMDS_MeshNode* nB = prevNodes[ iBeforeNotSame ];
1510 const SMDS_MeshNode* nA = prevNodes[ iAfterNotSame ];
1511 const SMDS_MeshNode* nP = prevNodes[ iNotSame ];
1512 const SMDS_MeshNode* nN = nextNodes[ iNotSame ];
1514 gp_Pnt pB ( nB->X(), nB->Y(), nB->Z() );
1515 gp_Pnt pA ( nA->X(), nA->Y(), nA->Z() );
1516 gp_Pnt pP ( nP->X(), nP->Y(), nP->Z() );
1517 gp_Pnt pN ( nN->X(), nN->Y(), nN->Z() );
1519 gp_Vec vB ( pP, pB ), vA ( pP, pA ), vN ( pP, pN );
1521 return (vA ^ vB) * vN < 0.0;
1524 //=======================================================================
1525 //function : sweepElement
1527 //=======================================================================
1529 static void sweepElement(SMESHDS_Mesh* aMesh,
1530 const SMDS_MeshElement* elem,
1531 const vector<TNodeOfNodeListMapItr> & newNodesItVec,
1532 list<const SMDS_MeshElement*>& newElems)
1534 // Loop on elem nodes:
1535 // find new nodes and detect same nodes indices
1536 int nbNodes = elem->NbNodes();
1537 list<const SMDS_MeshNode*>::const_iterator itNN[ nbNodes ];
1538 const SMDS_MeshNode* prevNod[ nbNodes ], *nextNod[ nbNodes ];
1539 int iNode, nbSame = 0, iNotSameNode = 0, iSameNode = 0;
1541 for ( iNode = 0; iNode < nbNodes; iNode++ )
1543 TNodeOfNodeListMapItr nnIt = newNodesItVec[ iNode ];
1544 const SMDS_MeshNode* node = nnIt->first;
1545 const list< const SMDS_MeshNode* > & listNewNodes = nnIt->second;
1546 if ( listNewNodes.empty() )
1549 itNN[ iNode ] = listNewNodes.begin();
1550 prevNod[ iNode ] = node;
1551 nextNod[ iNode ] = listNewNodes.front();
1552 if ( prevNod[ iNode ] != nextNod [ iNode ])
1553 iNotSameNode = iNode;
1559 if ( nbSame == nbNodes || nbSame > 2) {
1560 MESSAGE( " Too many same nodes of element " << elem->GetID() );
1564 int iBeforeSame = 0, iAfterSame = 0, iOpposSame = 0;
1566 iBeforeSame = ( iSameNode == 0 ? nbNodes - 1 : iSameNode - 1 );
1567 iAfterSame = ( iSameNode + 1 == nbNodes ? 0 : iSameNode + 1 );
1568 iOpposSame = ( iSameNode - 2 < 0 ? iSameNode + 2 : iSameNode - 2 );
1571 // check element orientation
1573 if ( nbNodes > 2 && !isReverse( prevNod, nextNod, nbNodes, iNotSameNode )) {
1574 //MESSAGE("Reversed elem " << elem );
1578 int iAB = iAfterSame + iBeforeSame;
1579 iBeforeSame = iAB - iBeforeSame;
1580 iAfterSame = iAB - iAfterSame;
1584 // make new elements
1585 int iStep, nbSteps = newNodesItVec[ 0 ]->second.size();
1586 for (iStep = 0; iStep < nbSteps; iStep++ )
1589 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
1590 nextNod[ iNode ] = *itNN[ iNode ];
1593 SMDS_MeshElement* aNewElem = 0;
1600 aNewElem = aMesh->AddEdge( prevNod[ 0 ], nextNod[ 0 ] );
1606 aNewElem = aMesh->AddFace(prevNod[ 0 ], prevNod[ 1 ],
1607 nextNod[ 1 ], nextNod[ 0 ] );
1609 aNewElem = aMesh->AddFace(prevNod[ 0 ], prevNod[ 1 ],
1610 nextNod[ iNotSameNode ] );
1613 case 3: { // TRIANGLE
1615 if ( nbSame == 0 ) // --- pentahedron
1616 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ],
1617 nextNod[ i0 ], nextNod[ 1 ], nextNod[ i2 ] );
1619 else if ( nbSame == 1 ) // --- pyramid
1620 aNewElem = aMesh->AddVolume (prevNod[ iAfterSame ], prevNod[ iBeforeSame ],
1621 nextNod[ iBeforeSame ], nextNod[ iAfterSame ],
1622 nextNod[ iSameNode ]);
1624 else // 2 same nodes: --- tetrahedron
1625 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ],
1626 nextNod[ iNotSameNode ]);
1629 case 4: { // QUADRANGLE
1631 if ( nbSame == 0 ) // --- hexahedron
1632 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ], prevNod[ 3 ],
1633 nextNod[ i0 ], nextNod[ 1 ], nextNod[ i2 ], nextNod[ 3 ]);
1635 else if ( nbSame == 1 ) // --- pyramid + pentahedron
1637 aNewElem = aMesh->AddVolume (prevNod[ iAfterSame ], prevNod[ iBeforeSame ],
1638 nextNod[ iBeforeSame ], nextNod[ iAfterSame ],
1639 nextNod[ iSameNode ]);
1640 newElems.push_back( aNewElem );
1641 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iOpposSame ],
1642 prevNod[ iAfterSame ], nextNod[ iBeforeSame ],
1643 nextNod[ iOpposSame ], nextNod[ iAfterSame ] );
1645 else if ( nbSame == 2 ) // pentahedron
1647 if ( prevNod[ iBeforeSame ] == nextNod[ iBeforeSame ] )
1648 // iBeforeSame is same too
1649 aNewElem = aMesh->AddVolume (prevNod[ iOpposSame ], prevNod[ iBeforeSame ],
1650 nextNod[ iOpposSame ], prevNod[ iAfterSame ],
1651 prevNod[ iSameNode ], nextNod[ iAfterSame ]);
1653 // iAfterSame is same too
1654 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iSameNode ],
1655 nextNod[ iBeforeSame ], prevNod[ iOpposSame ],
1656 prevNod[ iAfterSame ], nextNod[ iOpposSame ]);
1661 // realized for extrusion only
1662 vector<const SMDS_MeshNode*> polyedre_nodes (nbNodes*2 + 4*nbNodes);
1663 vector<int> quantities (nbNodes + 2);
1665 quantities[0] = nbNodes; // bottom of prism
1666 for (int inode = 0; inode < nbNodes; inode++) {
1667 polyedre_nodes[inode] = prevNod[inode];
1670 quantities[1] = nbNodes; // top of prism
1671 for (int inode = 0; inode < nbNodes; inode++) {
1672 polyedre_nodes[nbNodes + inode] = nextNod[inode];
1675 for (int iface = 0; iface < nbNodes; iface++) {
1676 quantities[iface + 2] = 4;
1677 int inextface = (iface == nbNodes - 1) ? 0 : iface + 1;
1678 polyedre_nodes[2*nbNodes + 4*iface + 0] = prevNod[iface];
1679 polyedre_nodes[2*nbNodes + 4*iface + 1] = prevNod[inextface];
1680 polyedre_nodes[2*nbNodes + 4*iface + 2] = nextNod[inextface];
1681 polyedre_nodes[2*nbNodes + 4*iface + 3] = nextNod[iface];
1683 aNewElem = aMesh->AddPolyhedralVolume (polyedre_nodes, quantities);
1687 newElems.push_back( aNewElem );
1689 // set new prev nodes
1690 for ( iNode = 0; iNode < nbNodes; iNode++ )
1691 prevNod[ iNode ] = nextNod[ iNode ];
1696 //=======================================================================
1697 //function : makeWalls
1698 //purpose : create 1D and 2D elements around swept elements
1699 //=======================================================================
1701 static void makeWalls (SMESHDS_Mesh* aMesh,
1702 TNodeOfNodeListMap & mapNewNodes,
1703 TElemOfElemListMap & newElemsMap,
1704 TElemOfVecOfNnlmiMap & elemNewNodesMap,
1705 set<const SMDS_MeshElement*>& elemSet)
1707 ASSERT( newElemsMap.size() == elemNewNodesMap.size() );
1709 // Find nodes belonging to only one initial element - sweep them to get edges.
1711 TNodeOfNodeListMapItr nList = mapNewNodes.begin();
1712 for ( ; nList != mapNewNodes.end(); nList++ )
1714 const SMDS_MeshNode* node =
1715 static_cast<const SMDS_MeshNode*>( nList->first );
1716 SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator();
1717 int nbInitElems = 0;
1718 while ( eIt->more() && nbInitElems < 2 )
1719 if ( elemSet.find( eIt->next() ) != elemSet.end() )
1721 if ( nbInitElems < 2 ) {
1722 vector<TNodeOfNodeListMapItr> newNodesItVec( 1, nList );
1723 list<const SMDS_MeshElement*> newEdges;
1724 sweepElement( aMesh, node, newNodesItVec, newEdges );
1728 // Make a ceiling for each element ie an equal element of last new nodes.
1729 // Find free links of faces - make edges and sweep them into faces.
1731 TElemOfElemListMap::iterator itElem = newElemsMap.begin();
1732 TElemOfVecOfNnlmiMap::iterator itElemNodes = elemNewNodesMap.begin();
1733 for ( ; itElem != newElemsMap.end(); itElem++, itElemNodes++ )
1735 const SMDS_MeshElement* elem = itElem->first;
1736 vector<TNodeOfNodeListMapItr>& vecNewNodes = itElemNodes->second;
1738 if ( elem->GetType() == SMDSAbs_Edge )
1740 // create a ceiling edge
1741 aMesh->AddEdge(vecNewNodes[ 0 ]->second.back(),
1742 vecNewNodes[ 1 ]->second.back() );
1744 if ( elem->GetType() != SMDSAbs_Face )
1747 bool hasFreeLinks = false;
1749 set<const SMDS_MeshElement*> avoidSet;
1750 avoidSet.insert( elem );
1752 // loop on a face nodes
1753 set<const SMDS_MeshNode*> aFaceLastNodes;
1754 int iNode, nbNodes = vecNewNodes.size();
1755 for ( iNode = 0; iNode < nbNodes; iNode++ )
1757 aFaceLastNodes.insert( vecNewNodes[ iNode ]->second.back() );
1758 // look for free links of a face
1759 int iNext = ( iNode + 1 == nbNodes ) ? 0 : iNode + 1;
1760 const SMDS_MeshNode* n1 = vecNewNodes[ iNode ]->first;
1761 const SMDS_MeshNode* n2 = vecNewNodes[ iNext ]->first;
1762 // check if a link is free
1763 if ( ! SMESH_MeshEditor::FindFaceInSet ( n1, n2, elemSet, avoidSet ))
1765 hasFreeLinks = true;
1766 // make an edge and a ceiling for a new edge
1767 if ( !aMesh->FindEdge( n1, n2 ))
1768 aMesh->AddEdge( n1, n2 );
1769 n1 = vecNewNodes[ iNode ]->second.back();
1770 n2 = vecNewNodes[ iNext ]->second.back();
1771 if ( !aMesh->FindEdge( n1, n2 ))
1772 aMesh->AddEdge( n1, n2 );
1775 // sweep free links into faces
1779 list<const SMDS_MeshElement*> & newVolumes = itElem->second;
1780 int iStep, nbSteps = vecNewNodes[0]->second.size();
1781 int iVol, volNb, nbVolumesByStep = newVolumes.size() / nbSteps;
1783 set<const SMDS_MeshNode*> initNodeSet, faceNodeSet;
1784 for ( iNode = 0; iNode < nbNodes; iNode++ )
1785 initNodeSet.insert( vecNewNodes[ iNode ]->first );
1787 for ( volNb = 0; volNb < nbVolumesByStep; volNb++ )
1789 list<const SMDS_MeshElement*>::iterator v = newVolumes.begin();
1791 while ( iVol++ < volNb ) v++;
1792 // find indices of free faces of a volume
1794 SMDS_VolumeTool vTool( *v );
1795 int iF, nbF = vTool.NbFaces();
1796 for ( iF = 0; iF < nbF; iF ++ )
1797 if (vTool.IsFreeFace( iF ) &&
1798 vTool.GetFaceNodes( iF, faceNodeSet ) &&
1799 initNodeSet != faceNodeSet) // except an initial face
1800 fInd.push_back( iF );
1804 // create faces for all steps
1805 for ( iStep = 0; iStep < nbSteps; iStep++ )
1808 vTool.SetExternalNormal();
1809 list< int >::iterator ind = fInd.begin();
1810 for ( ; ind != fInd.end(); ind++ )
1812 const SMDS_MeshNode** nodes = vTool.GetFaceNodes( *ind );
1813 switch ( vTool.NbFaceNodes( *ind ) ) {
1815 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] ); break;
1817 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ] ); break;
1820 int nbPolygonNodes = vTool.NbFaceNodes( *ind );
1821 vector<const SMDS_MeshNode*> polygon_nodes (nbPolygonNodes);
1822 for (int inode = 0; inode < nbPolygonNodes; inode++) {
1823 polygon_nodes[inode] = nodes[inode];
1825 aMesh->AddPolygonalFace(polygon_nodes);
1830 // go to the next volume
1832 while ( iVol++ < nbVolumesByStep ) v++;
1835 } // sweep free links into faces
1837 // make a ceiling face with a normal external to a volume
1839 SMDS_VolumeTool lastVol( itElem->second.back() );
1840 int iF = lastVol.GetFaceIndex( aFaceLastNodes );
1843 lastVol.SetExternalNormal();
1844 const SMDS_MeshNode** nodes = lastVol.GetFaceNodes( iF );
1845 switch ( lastVol.NbFaceNodes( iF ) ) {
1847 if (!hasFreeLinks ||
1848 !aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ]))
1849 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] );
1852 if (!hasFreeLinks ||
1853 !aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ]))
1854 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ] );
1858 int nbPolygonNodes = lastVol.NbFaceNodes( iF );
1859 vector<const SMDS_MeshNode*> polygon_nodes (nbPolygonNodes);
1860 for (int inode = 0; inode < nbPolygonNodes; inode++) {
1861 polygon_nodes[inode] = nodes[inode];
1863 if (!hasFreeLinks || !aMesh->FindFace(polygon_nodes))
1864 aMesh->AddPolygonalFace(polygon_nodes);
1870 } // loop on swept elements
1873 //=======================================================================
1874 //function : RotationSweep
1876 //=======================================================================
1878 void SMESH_MeshEditor::RotationSweep(set<const SMDS_MeshElement*> & theElems,
1879 const gp_Ax1& theAxis,
1880 const double theAngle,
1881 const int theNbSteps,
1882 const double theTol)
1884 MESSAGE( "RotationSweep()");
1886 aTrsf.SetRotation( theAxis, theAngle );
1888 gp_Lin aLine( theAxis );
1889 double aSqTol = theTol * theTol;
1891 SMESHDS_Mesh* aMesh = GetMeshDS();
1893 TNodeOfNodeListMap mapNewNodes;
1894 TElemOfVecOfNnlmiMap mapElemNewNodes;
1895 TElemOfElemListMap newElemsMap;
1898 set< const SMDS_MeshElement* >::iterator itElem;
1899 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1901 const SMDS_MeshElement* elem = (*itElem);
1904 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
1905 newNodesItVec.reserve( elem->NbNodes() );
1907 // loop on elem nodes
1908 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1909 while ( itN->more() ) {
1911 // check if a node has been already sweeped
1912 const SMDS_MeshNode* node =
1913 static_cast<const SMDS_MeshNode*>( itN->next() );
1914 TNodeOfNodeListMapItr nIt = mapNewNodes.find( node );
1915 if ( nIt == mapNewNodes.end() )
1917 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
1918 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
1921 gp_XYZ aXYZ( node->X(), node->Y(), node->Z() );
1923 aXYZ.Coord( coord[0], coord[1], coord[2] );
1924 bool isOnAxis = ( aLine.SquareDistance( aXYZ ) <= aSqTol );
1925 const SMDS_MeshNode * newNode = node;
1926 for ( int i = 0; i < theNbSteps; i++ ) {
1928 aTrsf.Transforms( coord[0], coord[1], coord[2] );
1929 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
1931 listNewNodes.push_back( newNode );
1934 newNodesItVec.push_back( nIt );
1936 // make new elements
1937 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem] );
1940 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElems );
1943 //=======================================================================
1944 //function : ExtrusionSweep
1946 //=======================================================================
1948 void SMESH_MeshEditor::ExtrusionSweep(set<const SMDS_MeshElement*> & theElems,
1949 const gp_Vec& theStep,
1950 const int theNbSteps)
1953 aTrsf.SetTranslation( theStep );
1955 SMESHDS_Mesh* aMesh = GetMeshDS();
1957 TNodeOfNodeListMap mapNewNodes;
1958 TElemOfVecOfNnlmiMap mapElemNewNodes;
1959 TElemOfElemListMap newElemsMap;
1962 set< const SMDS_MeshElement* >::iterator itElem;
1963 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1965 // check element type
1966 const SMDS_MeshElement* elem = (*itElem);
1970 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
1971 newNodesItVec.reserve( elem->NbNodes() );
1973 // loop on elem nodes
1974 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1975 while ( itN->more() ) {
1977 // check if a node has been already sweeped
1978 const SMDS_MeshNode* node =
1979 static_cast<const SMDS_MeshNode*>( itN->next() );
1980 TNodeOfNodeListMap::iterator nIt = mapNewNodes.find( node );
1981 if ( nIt == mapNewNodes.end() )
1983 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
1984 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
1987 double coord[] = { node->X(), node->Y(), node->Z() };
1988 for ( int i = 0; i < theNbSteps; i++ ) {
1989 aTrsf.Transforms( coord[0], coord[1], coord[2] );
1990 const SMDS_MeshNode * newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
1991 listNewNodes.push_back( newNode );
1994 newNodesItVec.push_back( nIt );
1996 // make new elements
1997 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem] );
1999 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElems );
2002 //=======================================================================
2003 //class : SMESH_MeshEditor_PathPoint
2004 //purpose : auxiliary class
2005 //=======================================================================
2006 class SMESH_MeshEditor_PathPoint {
2008 SMESH_MeshEditor_PathPoint() {
2009 myPnt.SetCoord(99., 99., 99.);
2010 myTgt.SetCoord(1.,0.,0.);
2014 void SetPnt(const gp_Pnt& aP3D){
2017 void SetTangent(const gp_Dir& aTgt){
2020 void SetAngle(const double& aBeta){
2023 void SetParameter(const double& aPrm){
2026 const gp_Pnt& Pnt()const{
2029 const gp_Dir& Tangent()const{
2032 double Angle()const{
2035 double Parameter()const{
2046 //=======================================================================
2047 //function : ExtrusionAlongTrack
2049 //=======================================================================
2050 SMESH_MeshEditor::Extrusion_Error
2051 SMESH_MeshEditor::ExtrusionAlongTrack (std::set<const SMDS_MeshElement*> & theElements,
2052 SMESH_subMesh* theTrack,
2053 const SMDS_MeshNode* theN1,
2054 const bool theHasAngles,
2055 std::list<double>& theAngles,
2056 const bool theHasRefPoint,
2057 const gp_Pnt& theRefPoint)
2059 MESSAGE("SMESH_MeshEditor::ExtrusionAlongTrack")
2060 int j, aNbTP, aNbE, aNb;
2061 double aT1, aT2, aT, aAngle, aX, aY, aZ;
2062 std::list<double> aPrms;
2063 std::list<double>::iterator aItD;
2064 std::set< const SMDS_MeshElement* >::iterator itElem;
2066 Standard_Real aTx1, aTx2, aL2, aTolVec, aTolVec2;
2070 Handle(Geom_Curve) aC3D;
2071 TopoDS_Edge aTrackEdge;
2072 TopoDS_Vertex aV1, aV2;
2074 SMDS_ElemIteratorPtr aItE;
2075 SMDS_NodeIteratorPtr aItN;
2076 SMDSAbs_ElementType aTypeE;
2078 TNodeOfNodeListMap mapNewNodes;
2079 TElemOfVecOfNnlmiMap mapElemNewNodes;
2080 TElemOfElemListMap newElemsMap;
2083 aTolVec2=aTolVec*aTolVec;
2086 aNbE = theElements.size();
2089 return EXTR_NO_ELEMENTS;
2091 // 1.1 Track Pattern
2094 SMESHDS_SubMesh* pSubMeshDS=theTrack->GetSubMeshDS();
2096 aItE = pSubMeshDS->GetElements();
2097 while ( aItE->more() ) {
2098 const SMDS_MeshElement* pE = aItE->next();
2099 aTypeE = pE->GetType();
2100 // Pattern must contain links only
2101 if ( aTypeE != SMDSAbs_Edge )
2102 return EXTR_PATH_NOT_EDGE;
2105 const TopoDS_Shape& aS = theTrack->GetSubShape();
2106 // Sub shape for the Pattern must be an Edge
2107 if ( aS.ShapeType() != TopAbs_EDGE )
2108 return EXTR_BAD_PATH_SHAPE;
2110 aTrackEdge = TopoDS::Edge( aS );
2111 // the Edge must not be degenerated
2112 if ( BRep_Tool::Degenerated( aTrackEdge ) )
2113 return EXTR_BAD_PATH_SHAPE;
2115 TopExp::Vertices( aTrackEdge, aV1, aV2 );
2116 aT1=BRep_Tool::Parameter( aV1, aTrackEdge );
2117 aT2=BRep_Tool::Parameter( aV2, aTrackEdge );
2119 aItN = theTrack->GetFather()->GetSubMesh( aV1 )->GetSubMeshDS()->GetNodes();
2120 const SMDS_MeshNode* aN1 = aItN->next();
2122 aItN = theTrack->GetFather()->GetSubMesh( aV2 )->GetSubMeshDS()->GetNodes();
2123 const SMDS_MeshNode* aN2 = aItN->next();
2125 // starting node must be aN1 or aN2
2126 if ( !( aN1 == theN1 || aN2 == theN1 ) )
2127 return EXTR_BAD_STARTING_NODE;
2129 aNbTP = pSubMeshDS->NbNodes() + 2;
2132 vector<double> aAngles( aNbTP );
2134 for ( j=0; j < aNbTP; ++j ) {
2138 if ( theHasAngles ) {
2139 aItD = theAngles.begin();
2140 for ( j=1; (aItD != theAngles.end()) && (j<aNbTP); ++aItD, ++j ) {
2142 aAngles[j] = aAngle;
2146 // 2. Collect parameters on the track edge
2147 aPrms.push_back( aT1 );
2148 aPrms.push_back( aT2 );
2150 aItN = pSubMeshDS->GetNodes();
2151 while ( aItN->more() ) {
2152 const SMDS_MeshNode* pNode = aItN->next();
2153 const SMDS_EdgePosition* pEPos =
2154 static_cast<const SMDS_EdgePosition*>( pNode->GetPosition().get() );
2155 aT = pEPos->GetUParameter();
2156 aPrms.push_back( aT );
2161 if ( aN1 == theN1 ) {
2173 SMESH_MeshEditor_PathPoint aPP;
2174 vector<SMESH_MeshEditor_PathPoint> aPPs( aNbTP );
2176 aC3D = BRep_Tool::Curve( aTrackEdge, aTx1, aTx2 );
2178 aItD = aPrms.begin();
2179 for ( j=0; aItD != aPrms.end(); ++aItD, ++j ) {
2181 aC3D->D1( aT, aP3D, aVec );
2182 aL2 = aVec.SquareMagnitude();
2183 if ( aL2 < aTolVec2 )
2184 return EXTR_CANT_GET_TANGENT;
2186 gp_Dir aTgt( aVec );
2187 aAngle = aAngles[j];
2190 aPP.SetTangent( aTgt );
2191 aPP.SetAngle( aAngle );
2192 aPP.SetParameter( aT );
2196 // 3. Center of rotation aV0
2198 if ( !theHasRefPoint ) {
2200 aGC.SetCoord( 0.,0.,0. );
2202 itElem = theElements.begin();
2203 for ( ; itElem != theElements.end(); itElem++ ) {
2204 const SMDS_MeshElement* elem = (*itElem);
2206 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2207 while ( itN->more() ) {
2208 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( itN->next() );
2213 if ( mapNewNodes.find( node ) == mapNewNodes.end() ) {
2214 list<const SMDS_MeshNode*> aLNx;
2215 mapNewNodes[node] = aLNx;
2217 gp_XYZ aXYZ( aX, aY, aZ );
2225 } // if (!theHasRefPoint) {
2226 mapNewNodes.clear();
2228 // 4. Processing the elements
2229 SMESHDS_Mesh* aMesh = GetMeshDS();
2231 for ( itElem = theElements.begin(); itElem != theElements.end(); itElem++ ) {
2232 // check element type
2233 const SMDS_MeshElement* elem = (*itElem);
2234 aTypeE = elem->GetType();
2235 if ( !elem || ( aTypeE != SMDSAbs_Face && aTypeE != SMDSAbs_Edge ) )
2238 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
2239 newNodesItVec.reserve( elem->NbNodes() );
2241 // loop on elem nodes
2242 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2243 while ( itN->more() ) {
2245 // check if a node has been already processed
2246 const SMDS_MeshNode* node =
2247 static_cast<const SMDS_MeshNode*>( itN->next() );
2248 TNodeOfNodeListMap::iterator nIt = mapNewNodes.find( node );
2249 if ( nIt == mapNewNodes.end() ) {
2250 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
2251 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
2254 aX = node->X(); aY = node->Y(); aZ = node->Z();
2256 Standard_Real aAngle1x, aAngleT1T0, aTolAng;
2257 gp_Pnt aP0x, aP1x, aPN0, aPN1, aV0x, aV1x;
2258 gp_Ax1 anAx1, anAxT1T0;
2259 gp_Dir aDT1x, aDT0x, aDT1T0;
2264 aPN0.SetCoord(aX, aY, aZ);
2266 const SMESH_MeshEditor_PathPoint& aPP0 = aPPs[0];
2268 aDT0x= aPP0.Tangent();
2270 for ( j = 1; j < aNbTP; ++j ) {
2271 const SMESH_MeshEditor_PathPoint& aPP1 = aPPs[j];
2273 aDT1x = aPP1.Tangent();
2274 aAngle1x = aPP1.Angle();
2276 gp_Trsf aTrsf, aTrsfRot, aTrsfRotT1T0;
2278 gp_Vec aV01x( aP0x, aP1x );
2279 aTrsf.SetTranslation( aV01x );
2282 aV1x = aV0x.Transformed( aTrsf );
2283 aPN1 = aPN0.Transformed( aTrsf );
2285 // rotation 1 [ T1,T0 ]
2286 aAngleT1T0=-aDT1x.Angle( aDT0x );
2287 if (fabs(aAngleT1T0) > aTolAng) {
2289 anAxT1T0.SetLocation( aV1x );
2290 anAxT1T0.SetDirection( aDT1T0 );
2291 aTrsfRotT1T0.SetRotation( anAxT1T0, aAngleT1T0 );
2293 aPN1 = aPN1.Transformed( aTrsfRotT1T0 );
2297 if ( theHasAngles ) {
2298 anAx1.SetLocation( aV1x );
2299 anAx1.SetDirection( aDT1x );
2300 aTrsfRot.SetRotation( anAx1, aAngle1x );
2302 aPN1 = aPN1.Transformed( aTrsfRot );
2309 const SMDS_MeshNode* newNode = aMesh->AddNode( aX, aY, aZ );
2310 listNewNodes.push_back( newNode );
2318 newNodesItVec.push_back( nIt );
2320 // make new elements
2321 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem] );
2324 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElements );
2329 //=======================================================================
2330 //function : Transform
2332 //=======================================================================
2334 void SMESH_MeshEditor::Transform (set<const SMDS_MeshElement*> & theElems,
2335 const gp_Trsf& theTrsf,
2339 switch ( theTrsf.Form() ) {
2345 needReverse = false;
2348 SMESHDS_Mesh* aMesh = GetMeshDS();
2350 // map old node to new one
2351 TNodeNodeMap nodeMap;
2353 // elements sharing moved nodes; those of them which have all
2354 // nodes mirrored but are not in theElems are to be reversed
2355 set<const SMDS_MeshElement*> inverseElemSet;
2358 set< const SMDS_MeshElement* >::iterator itElem;
2359 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
2361 const SMDS_MeshElement* elem = (*itElem);
2365 // loop on elem nodes
2366 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2367 while ( itN->more() ) {
2369 // check if a node has been already transformed
2370 const SMDS_MeshNode* node =
2371 static_cast<const SMDS_MeshNode*>( itN->next() );
2372 if (nodeMap.find( node ) != nodeMap.end() )
2376 coord[0] = node->X();
2377 coord[1] = node->Y();
2378 coord[2] = node->Z();
2379 theTrsf.Transforms( coord[0], coord[1], coord[2] );
2380 const SMDS_MeshNode * newNode = node;
2382 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
2384 aMesh->MoveNode( node, coord[0], coord[1], coord[2] );
2385 nodeMap.insert( TNodeNodeMap::value_type( node, newNode ));
2387 // keep inverse elements
2388 if ( !theCopy && needReverse ) {
2389 SMDS_ElemIteratorPtr invElemIt = node->GetInverseElementIterator();
2390 while ( invElemIt->more() )
2391 inverseElemSet.insert( invElemIt->next() );
2396 // either new elements are to be created
2397 // or a mirrored element are to be reversed
2398 if ( !theCopy && !needReverse)
2401 if ( !inverseElemSet.empty()) {
2402 set<const SMDS_MeshElement*>::iterator invElemIt = inverseElemSet.begin();
2403 for ( ; invElemIt != inverseElemSet.end(); invElemIt++ )
2404 theElems.insert( *invElemIt );
2407 // replicate or reverse elements
2410 REV_TETRA = 0, // = nbNodes - 4
2411 REV_PYRAMID = 1, // = nbNodes - 4
2412 REV_PENTA = 2, // = nbNodes - 4
2414 REV_HEXA = 4, // = nbNodes - 4
2418 { 2, 1, 0, 3, 4, 0, 0, 0 }, // REV_TETRA
2419 { 2, 1, 0, 3, 4, 0, 0, 0 }, // REV_PYRAMID
2420 { 2, 1, 0, 5, 4, 3, 0, 0 }, // REV_PENTA
2421 { 2, 1, 0, 3, 0, 0, 0, 0 }, // REV_FACE
2422 { 2, 1, 0, 3, 6, 5, 4, 7 }, // REV_HEXA
2423 { 0, 1, 2, 3, 4, 5, 6, 7 } // FORWARD
2426 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
2428 const SMDS_MeshElement* elem = (*itElem);
2429 if ( !elem || elem->GetType() == SMDSAbs_Node )
2432 int nbNodes = elem->NbNodes();
2433 int elemType = elem->GetType();
2435 if (elem->IsPoly()) {
2436 // Polygon or Polyhedral Volume
2437 switch ( elemType ) {
2440 vector<const SMDS_MeshNode*> poly_nodes (nbNodes);
2442 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2443 while (itN->more()) {
2444 const SMDS_MeshNode* node =
2445 static_cast<const SMDS_MeshNode*>(itN->next());
2446 TNodeNodeMap::iterator nodeMapIt = nodeMap.find(node);
2447 if (nodeMapIt == nodeMap.end())
2448 break; // not all nodes transformed
2450 // reverse mirrored faces and volumes
2451 poly_nodes[nbNodes - iNode - 1] = (*nodeMapIt).second;
2453 poly_nodes[iNode] = (*nodeMapIt).second;
2457 if ( iNode != nbNodes )
2458 continue; // not all nodes transformed
2461 aMesh->AddPolygonalFace(poly_nodes);
2463 aMesh->ChangePolygonNodes(elem, poly_nodes);
2467 case SMDSAbs_Volume:
2469 // ATTENTION: Reversing is not yet done!!!
2470 const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
2471 (const SMDS_PolyhedralVolumeOfNodes*) elem;
2473 MESSAGE("Warning: bad volumic element");
2477 vector<const SMDS_MeshNode*> poly_nodes;
2478 vector<int> quantities;
2480 bool allTransformed = true;
2481 int nbFaces = aPolyedre->NbFaces();
2482 for (int iface = 1; iface <= nbFaces && allTransformed; iface++) {
2483 int nbFaceNodes = aPolyedre->NbFaceNodes(iface);
2484 for (int inode = 1; inode <= nbFaceNodes && allTransformed; inode++) {
2485 const SMDS_MeshNode* node = aPolyedre->GetFaceNode(iface, inode);
2486 TNodeNodeMap::iterator nodeMapIt = nodeMap.find(node);
2487 if (nodeMapIt == nodeMap.end()) {
2488 allTransformed = false; // not all nodes transformed
2490 poly_nodes.push_back((*nodeMapIt).second);
2493 quantities.push_back(nbFaceNodes);
2495 if ( !allTransformed )
2496 continue; // not all nodes transformed
2499 aMesh->AddPolyhedralVolume(poly_nodes, quantities);
2501 aMesh->ChangePolyhedronNodes(elem, poly_nodes, quantities);
2511 int* i = index[ FORWARD ];
2512 if ( needReverse && nbNodes > 2) // reverse mirrored faces and volumes
2513 if ( elemType == SMDSAbs_Face )
2514 i = index[ REV_FACE ];
2516 i = index[ nbNodes - 4 ];
2518 // find transformed nodes
2519 const SMDS_MeshNode* nodes[8];
2521 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2522 while ( itN->more() )
2524 const SMDS_MeshNode* node =
2525 static_cast<const SMDS_MeshNode*>( itN->next() );
2526 TNodeNodeMap::iterator nodeMapIt = nodeMap.find( node );
2527 if ( nodeMapIt == nodeMap.end() )
2528 break; // not all nodes transformed
2529 nodes[ i [ iNode++ ]] = (*nodeMapIt).second;
2531 if ( iNode != nbNodes )
2532 continue; // not all nodes transformed
2536 // add a new element
2537 switch ( elemType ) {
2539 aMesh->AddEdge( nodes[ 0 ], nodes[ 1 ] );
2543 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] );
2545 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ]);
2547 case SMDSAbs_Volume:
2549 aMesh->AddVolume( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ] );
2550 else if ( nbNodes == 8 )
2551 aMesh->AddVolume( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ],
2552 nodes[ 4 ], nodes[ 5 ], nodes[ 6 ] , nodes[ 7 ]);
2553 else if ( nbNodes == 6 )
2554 aMesh->AddVolume( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ],
2555 nodes[ 4 ], nodes[ 5 ]);
2556 else if ( nbNodes == 5 )
2557 aMesh->AddVolume( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ],
2565 // reverse element as it was reversed by transformation
2567 aMesh->ChangeElementNodes( elem, nodes, nbNodes );
2572 //=======================================================================
2573 //function : FindCoincidentNodes
2574 //purpose : Return list of group of nodes close to each other within theTolerance
2575 // Search among theNodes or in the whole mesh if theNodes is empty.
2576 //=======================================================================
2578 void SMESH_MeshEditor::FindCoincidentNodes (set<const SMDS_MeshNode*> & theNodes,
2579 const double theTolerance,
2580 TListOfListOfNodes & theGroupsOfNodes)
2582 double tol2 = theTolerance * theTolerance;
2584 list<const SMDS_MeshNode*> nodes;
2585 if ( theNodes.empty() )
2586 { // get all nodes in the mesh
2587 SMDS_NodeIteratorPtr nIt = GetMeshDS()->nodesIterator();
2588 while ( nIt->more() )
2589 nodes.push_back( nIt->next() );
2593 nodes.insert( nodes.end(), theNodes.begin(), theNodes.end() );
2596 list<const SMDS_MeshNode*>::iterator it2, it1 = nodes.begin();
2597 for ( ; it1 != nodes.end(); it1++ )
2599 const SMDS_MeshNode* n1 = *it1;
2600 gp_Pnt p1( n1->X(), n1->Y(), n1->Z() );
2602 list<const SMDS_MeshNode*> * groupPtr = 0;
2604 for ( it2++; it2 != nodes.end(); it2++ )
2606 const SMDS_MeshNode* n2 = *it2;
2607 gp_Pnt p2( n2->X(), n2->Y(), n2->Z() );
2608 if ( p1.SquareDistance( p2 ) <= tol2 )
2611 theGroupsOfNodes.push_back( list<const SMDS_MeshNode*>() );
2612 groupPtr = & theGroupsOfNodes.back();
2613 groupPtr->push_back( n1 );
2615 groupPtr->push_back( n2 );
2616 it2 = nodes.erase( it2 );
2623 //=======================================================================
2624 //function : MergeNodes
2625 //purpose : In each group, the cdr of nodes are substituted by the first one
2627 //=======================================================================
2629 void SMESH_MeshEditor::MergeNodes (TListOfListOfNodes & theGroupsOfNodes)
2631 SMESHDS_Mesh* aMesh = GetMeshDS();
2633 TNodeNodeMap nodeNodeMap; // node to replace - new node
2634 set<const SMDS_MeshElement*> elems; // all elements with changed nodes
2635 list< int > rmElemIds, rmNodeIds;
2637 // Fill nodeNodeMap and elems
2639 TListOfListOfNodes::iterator grIt = theGroupsOfNodes.begin();
2640 for ( ; grIt != theGroupsOfNodes.end(); grIt++ )
2642 list<const SMDS_MeshNode*>& nodes = *grIt;
2643 list<const SMDS_MeshNode*>::iterator nIt = nodes.begin();
2644 const SMDS_MeshNode* nToKeep = *nIt;
2645 for ( ; nIt != nodes.end(); nIt++ )
2647 const SMDS_MeshNode* nToRemove = *nIt;
2648 nodeNodeMap.insert( TNodeNodeMap::value_type( nToRemove, nToKeep ));
2649 if ( nToRemove != nToKeep ) {
2650 rmNodeIds.push_back( nToRemove->GetID() );
2651 AddToSameGroups( nToKeep, nToRemove, aMesh );
2654 SMDS_ElemIteratorPtr invElemIt = nToRemove->GetInverseElementIterator();
2655 while ( invElemIt->more() )
2656 elems.insert( invElemIt->next() );
2659 // Change element nodes or remove an element
2661 set<const SMDS_MeshElement*>::iterator eIt = elems.begin();
2662 for ( ; eIt != elems.end(); eIt++ )
2664 const SMDS_MeshElement* elem = *eIt;
2665 int nbNodes = elem->NbNodes();
2666 int aShapeId = FindShape( elem );
2668 set<const SMDS_MeshNode*> nodeSet;
2669 const SMDS_MeshNode* curNodes[ nbNodes ], *uniqueNodes[ nbNodes ];
2670 int iUnique = 0, iCur = 0, nbRepl = 0, iRepl [ nbNodes ];
2672 // get new seq of nodes
2673 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2674 while ( itN->more() )
2676 const SMDS_MeshNode* n =
2677 static_cast<const SMDS_MeshNode*>( itN->next() );
2679 TNodeNodeMap::iterator nnIt = nodeNodeMap.find( n );
2680 if ( nnIt != nodeNodeMap.end() ) { // n sticks
2682 iRepl[ nbRepl++ ] = iCur;
2684 curNodes[ iCur ] = n;
2685 bool isUnique = nodeSet.insert( n ).second;
2687 uniqueNodes[ iUnique++ ] = n;
2691 // Analyse element topology after replacement
2694 int nbUniqueNodes = nodeSet.size();
2695 if ( nbNodes != nbUniqueNodes ) // some nodes stick
2697 // Polygons and Polyhedral volumes
2698 if (elem->IsPoly()) {
2700 if (elem->GetType() == SMDSAbs_Face) {
2702 if (nbUniqueNodes < 3) {
2705 // get simple seq of nodes
2706 const SMDS_MeshNode* simpleNodes[ nbNodes ];
2709 simpleNodes[iSimple++] = curNodes[0];
2710 for (iCur = 1; iCur < nbNodes; iCur++) {
2711 if (curNodes[iCur] != simpleNodes[iSimple - 1]) {
2712 simpleNodes[iSimple++] = curNodes[iCur];
2715 int nbSimple = iSimple;
2716 if (simpleNodes[nbSimple - 1] == simpleNodes[0]) {
2721 bool foundCycle = (nbSimple > nbUniqueNodes);
2722 while (foundCycle) {
2724 set<const SMDS_MeshNode*> cycleSet;
2725 for (iSimple = 0; iSimple < nbSimple && !foundCycle; iSimple++) {
2726 const SMDS_MeshNode* n = simpleNodes[iSimple];
2727 if (!cycleSet.insert( n ).second) {
2731 int iC = 0, curLast = iSimple;
2732 for (; iC < curLast; iC++) {
2733 if (simpleNodes[iC] == n) break;
2735 int cycleLen = curLast - iC;
2737 // create sub-element
2738 vector<const SMDS_MeshNode *> poly_nodes (cycleLen);
2739 for (int ii = 0; iC < curLast; iC++) {
2740 poly_nodes[ii++] = simpleNodes[iC];
2742 SMDS_MeshElement* newElem = aMesh->AddPolygonalFace(poly_nodes);
2744 aMesh->SetMeshElementOnShape(newElem, aShapeId);
2746 // put the rest nodes from the first cycle position
2747 for (iC = curLast + 1; iC < nbSimple; iC++) {
2748 simpleNodes[iC - cycleLen] = simpleNodes[iC];
2750 nbSimple -= cycleLen;
2752 } // for (iSimple = 0; iSimple < nbSimple; iSimple++)
2753 } // while (foundCycle)
2756 aMesh->ChangeElementNodes(elem, simpleNodes, nbSimple);
2762 } else if (elem->GetType() == SMDSAbs_Volume) {
2763 // Polyhedral volume
2764 if (nbUniqueNodes < 4) {
2767 // each face has to be analized in order to check volume validity
2768 //aMesh->ChangeElementNodes(elem, uniqueNodes, nbUniqueNodes);
2777 rmElemIds.push_back(elem->GetID());
2783 switch ( nbNodes ) {
2784 case 2: ///////////////////////////////////// EDGE
2785 isOk = false; break;
2786 case 3: ///////////////////////////////////// TRIANGLE
2787 isOk = false; break;
2789 if ( elem->GetType() == SMDSAbs_Volume ) // TETRAHEDRON
2791 else { //////////////////////////////////// QUADRANGLE
2792 if ( nbUniqueNodes < 3 )
2794 else if ( nbRepl == 2 && iRepl[ 1 ] - iRepl[ 0 ] == 2 )
2795 isOk = false; // opposite nodes stick
2798 case 6: ///////////////////////////////////// PENTAHEDRON
2799 if ( nbUniqueNodes == 4 ) {
2800 // ---------------------------------> tetrahedron
2802 iRepl[ 0 ] > 2 && iRepl[ 1 ] > 2 && iRepl[ 2 ] > 2 ) {
2803 // all top nodes stick: reverse a bottom
2804 uniqueNodes[ 0 ] = curNodes [ 1 ];
2805 uniqueNodes[ 1 ] = curNodes [ 0 ];
2807 else if (nbRepl == 3 &&
2808 iRepl[ 0 ] < 3 && iRepl[ 1 ] < 3 && iRepl[ 2 ] < 3 ) {
2809 // all bottom nodes stick: set a top before
2810 uniqueNodes[ 3 ] = uniqueNodes [ 0 ];
2811 uniqueNodes[ 0 ] = curNodes [ 3 ];
2812 uniqueNodes[ 1 ] = curNodes [ 4 ];
2813 uniqueNodes[ 2 ] = curNodes [ 5 ];
2815 else if (nbRepl == 4 &&
2816 iRepl[ 2 ] - iRepl [ 0 ] == 3 && iRepl[ 3 ] - iRepl [ 1 ] == 3 ) {
2817 // a lateral face turns into a line: reverse a bottom
2818 uniqueNodes[ 0 ] = curNodes [ 1 ];
2819 uniqueNodes[ 1 ] = curNodes [ 0 ];
2824 else if ( nbUniqueNodes == 5 ) {
2825 // PENTAHEDRON --------------------> 2 tetrahedrons
2826 if ( nbRepl == 2 && iRepl[ 1 ] - iRepl [ 0 ] == 3 ) {
2827 // a bottom node sticks with a linked top one
2829 SMDS_MeshElement* newElem =
2830 aMesh->AddVolume(curNodes[ 3 ],
2833 curNodes[ iRepl[ 0 ] == 2 ? 1 : 2 ]);
2835 aMesh->SetMeshElementOnShape( newElem, aShapeId );
2836 // 2. : reverse a bottom
2837 uniqueNodes[ 0 ] = curNodes [ 1 ];
2838 uniqueNodes[ 1 ] = curNodes [ 0 ];
2847 case 8: { //////////////////////////////////// HEXAHEDRON
2849 SMDS_VolumeTool hexa (elem);
2850 hexa.SetExternalNormal();
2851 if ( nbUniqueNodes == 4 && nbRepl == 6 ) {
2852 //////////////////////// ---> tetrahedron
2853 for ( int iFace = 0; iFace < 6; iFace++ ) {
2854 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
2855 if (curNodes[ind[ 0 ]] == curNodes[ind[ 1 ]] &&
2856 curNodes[ind[ 0 ]] == curNodes[ind[ 2 ]] &&
2857 curNodes[ind[ 0 ]] == curNodes[ind[ 3 ]] ) {
2858 // one face turns into a point ...
2859 int iOppFace = hexa.GetOppFaceIndex( iFace );
2860 ind = hexa.GetFaceNodesIndices( iOppFace );
2862 iUnique = 2; // reverse a tetrahedron bottom
2863 for ( iCur = 0; iCur < 4 && nbStick < 2; iCur++ ) {
2864 if ( curNodes[ind[ iCur ]] == curNodes[ind[ iCur + 1 ]] )
2866 else if ( iUnique >= 0 )
2867 uniqueNodes[ iUnique-- ] = curNodes[ind[ iCur ]];
2869 if ( nbStick == 1 ) {
2870 // ... and the opposite one - into a triangle.
2872 ind = hexa.GetFaceNodesIndices( iFace );
2873 uniqueNodes[ 3 ] = curNodes[ind[ 0 ]];
2880 else if (nbUniqueNodes == 5 && nbRepl == 4 ) {
2881 //////////////////// HEXAHEDRON ---> 2 tetrahedrons
2882 for ( int iFace = 0; iFace < 6; iFace++ ) {
2883 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
2884 if (curNodes[ind[ 0 ]] == curNodes[ind[ 1 ]] &&
2885 curNodes[ind[ 0 ]] == curNodes[ind[ 2 ]] &&
2886 curNodes[ind[ 0 ]] == curNodes[ind[ 3 ]] ) {
2887 // one face turns into a point ...
2888 int iOppFace = hexa.GetOppFaceIndex( iFace );
2889 ind = hexa.GetFaceNodesIndices( iOppFace );
2891 iUnique = 2; // reverse a tetrahedron 1 bottom
2892 for ( iCur = 0; iCur < 4 && nbStick == 0; iCur++ ) {
2893 if ( curNodes[ind[ iCur ]] == curNodes[ind[ iCur + 1 ]] )
2895 else if ( iUnique >= 0 )
2896 uniqueNodes[ iUnique-- ] = curNodes[ind[ iCur ]];
2898 if ( nbStick == 0 ) {
2899 // ... and the opposite one is a quadrangle
2901 const int* indTop = hexa.GetFaceNodesIndices( iFace );
2902 uniqueNodes[ 3 ] = curNodes[indTop[ 0 ]];
2905 SMDS_MeshElement* newElem =
2906 aMesh->AddVolume(curNodes[ind[ 0 ]],
2909 curNodes[indTop[ 0 ]]);
2911 aMesh->SetMeshElementOnShape( newElem, aShapeId );
2918 else if ( nbUniqueNodes == 6 && nbRepl == 4 ) {
2919 ////////////////// HEXAHEDRON ---> 2 tetrahedrons or 1 prism
2920 // find indices of quad and tri faces
2921 int iQuadFace[ 6 ], iTriFace[ 6 ], nbQuad = 0, nbTri = 0, iFace;
2922 for ( iFace = 0; iFace < 6; iFace++ ) {
2923 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
2925 for ( iCur = 0; iCur < 4; iCur++ )
2926 nodeSet.insert( curNodes[ind[ iCur ]] );
2927 nbUniqueNodes = nodeSet.size();
2928 if ( nbUniqueNodes == 3 )
2929 iTriFace[ nbTri++ ] = iFace;
2930 else if ( nbUniqueNodes == 4 )
2931 iQuadFace[ nbQuad++ ] = iFace;
2933 if (nbQuad == 2 && nbTri == 4 &&
2934 hexa.GetOppFaceIndex( iQuadFace[ 0 ] ) == iQuadFace[ 1 ]) {
2935 // 2 opposite quadrangles stuck with a diagonal;
2936 // sample groups of merged indices: (0-4)(2-6)
2937 // --------------------------------------------> 2 tetrahedrons
2938 const int *ind1 = hexa.GetFaceNodesIndices( iQuadFace[ 0 ]); // indices of quad1 nodes
2939 const int *ind2 = hexa.GetFaceNodesIndices( iQuadFace[ 1 ]);
2940 int i0, i1d, i2, i3d, i0t, i2t; // d-daigonal, t-top
2941 if (curNodes[ind1[ 0 ]] == curNodes[ind2[ 0 ]] &&
2942 curNodes[ind1[ 2 ]] == curNodes[ind2[ 2 ]]) {
2943 // stuck with 0-2 diagonal
2951 else if (curNodes[ind1[ 1 ]] == curNodes[ind2[ 3 ]] &&
2952 curNodes[ind1[ 3 ]] == curNodes[ind2[ 1 ]]) {
2953 // stuck with 1-3 diagonal
2965 uniqueNodes[ 0 ] = curNodes [ i0 ];
2966 uniqueNodes[ 1 ] = curNodes [ i1d ];
2967 uniqueNodes[ 2 ] = curNodes [ i3d ];
2968 uniqueNodes[ 3 ] = curNodes [ i0t ];
2971 SMDS_MeshElement* newElem = aMesh->AddVolume(curNodes[ i1d ],
2976 aMesh->SetMeshElementOnShape( newElem, aShapeId );
2979 else if (( nbTri == 2 && nbQuad == 3 ) || // merged (0-4)(1-5)
2980 ( nbTri == 4 && nbQuad == 2 )) { // merged (7-4)(1-5)
2981 // --------------------------------------------> prism
2982 // find 2 opposite triangles
2984 for ( iFace = 0; iFace + 1 < nbTri; iFace++ ) {
2985 if ( hexa.GetOppFaceIndex( iTriFace[ iFace ] ) == iTriFace[ iFace + 1 ]) {
2986 // find indices of kept and replaced nodes
2987 // and fill unique nodes of 2 opposite triangles
2988 const int *ind1 = hexa.GetFaceNodesIndices( iTriFace[ iFace ]);
2989 const int *ind2 = hexa.GetFaceNodesIndices( iTriFace[ iFace + 1 ]);
2990 const SMDS_MeshNode** hexanodes = hexa.GetNodes();
2991 // fill unique nodes
2994 for ( iCur = 0; iCur < 4 && isOk; iCur++ ) {
2995 const SMDS_MeshNode* n = curNodes[ind1[ iCur ]];
2996 const SMDS_MeshNode* nInit = hexanodes[ind1[ iCur ]];
2998 // iCur of a linked node of the opposite face (make normals co-directed):
2999 int iCurOpp = ( iCur == 1 || iCur == 3 ) ? 4 - iCur : iCur;
3000 // check that correspondent corners of triangles are linked
3001 if ( !hexa.IsLinked( ind1[ iCur ], ind2[ iCurOpp ] ))
3004 uniqueNodes[ iUnique ] = n;
3005 uniqueNodes[ iUnique + 3 ] = curNodes[ind2[ iCurOpp ]];
3014 } // if ( nbUniqueNodes == 6 && nbRepl == 4 )
3020 } // switch ( nbNodes )
3022 } // if ( nbNodes != nbUniqueNodes ) // some nodes stick
3025 aMesh->ChangeElementNodes( elem, uniqueNodes, nbUniqueNodes );
3027 rmElemIds.push_back( elem->GetID() );
3029 } // loop on elements
3031 // Remove equal nodes and bad elements
3033 Remove( rmNodeIds, true );
3034 Remove( rmElemIds, false );
3038 //=======================================================================
3039 //function : MergeEqualElements
3040 //purpose : Remove all but one of elements built on the same nodes.
3041 //=======================================================================
3043 void SMESH_MeshEditor::MergeEqualElements()
3045 SMESHDS_Mesh* aMesh = GetMeshDS();
3047 SMDS_EdgeIteratorPtr eIt = aMesh->edgesIterator();
3048 SMDS_FaceIteratorPtr fIt = aMesh->facesIterator();
3049 SMDS_VolumeIteratorPtr vIt = aMesh->volumesIterator();
3051 list< int > rmElemIds; // IDs of elems to remove
3053 for ( int iDim = 1; iDim <= 3; iDim++ ) {
3055 set< set <const SMDS_MeshElement*> > setOfNodeSet;
3059 const SMDS_MeshElement* elem = 0;
3061 if ( eIt->more() ) elem = eIt->next();
3062 } else if ( iDim == 2 ) {
3063 if ( fIt->more() ) elem = fIt->next();
3065 if ( vIt->more() ) elem = vIt->next();
3070 set <const SMDS_MeshElement*> nodeSet;
3071 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
3072 while ( nodeIt->more() )
3073 nodeSet.insert( nodeIt->next() );
3076 bool isUnique = setOfNodeSet.insert( nodeSet ).second;
3078 rmElemIds.push_back( elem->GetID() );
3082 Remove( rmElemIds, false );
3085 //=======================================================================
3086 //function : FindFaceInSet
3087 //purpose : Return a face having linked nodes n1 and n2 and which is
3088 // - not in avoidSet,
3089 // - in elemSet provided that !elemSet.empty()
3090 //=======================================================================
3092 const SMDS_MeshElement*
3093 SMESH_MeshEditor::FindFaceInSet(const SMDS_MeshNode* n1,
3094 const SMDS_MeshNode* n2,
3095 const set<const SMDS_MeshElement*>& elemSet,
3096 const set<const SMDS_MeshElement*>& avoidSet)
3099 SMDS_ElemIteratorPtr invElemIt = n1->GetInverseElementIterator();
3100 while ( invElemIt->more() ) { // loop on inverse elements of n1
3101 const SMDS_MeshElement* elem = invElemIt->next();
3102 if (elem->GetType() != SMDSAbs_Face ||
3103 avoidSet.find( elem ) != avoidSet.end() )
3105 if ( !elemSet.empty() && elemSet.find( elem ) == elemSet.end())
3107 // get face nodes and find index of n1
3108 int i1, nbN = elem->NbNodes(), iNode = 0;
3109 const SMDS_MeshNode* faceNodes[ nbN ], *n;
3110 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
3111 while ( nIt->more() ) {
3112 faceNodes[ iNode ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
3113 if ( faceNodes[ iNode++ ] == n1 )
3116 // find a n2 linked to n1
3117 for ( iNode = 0; iNode < 2; iNode++ ) {
3118 if ( iNode ) // node before n1
3119 n = faceNodes[ i1 == 0 ? nbN - 1 : i1 - 1 ];
3120 else // node after n1
3121 n = faceNodes[ i1 + 1 == nbN ? 0 : i1 + 1 ];
3129 //=======================================================================
3130 //function : findAdjacentFace
3132 //=======================================================================
3134 static const SMDS_MeshElement* findAdjacentFace(const SMDS_MeshNode* n1,
3135 const SMDS_MeshNode* n2,
3136 const SMDS_MeshElement* elem)
3138 set<const SMDS_MeshElement*> elemSet, avoidSet;
3140 avoidSet.insert ( elem );
3141 return SMESH_MeshEditor::FindFaceInSet( n1, n2, elemSet, avoidSet );
3144 //=======================================================================
3145 //function : findFreeBorder
3147 //=======================================================================
3149 #define ControlFreeBorder SMESH::Controls::FreeEdges::IsFreeEdge
3151 static bool findFreeBorder (const SMDS_MeshNode* theFirstNode,
3152 const SMDS_MeshNode* theSecondNode,
3153 const SMDS_MeshNode* theLastNode,
3154 list< const SMDS_MeshNode* > & theNodes,
3155 list< const SMDS_MeshElement* > & theFaces)
3157 if ( !theFirstNode || !theSecondNode )
3159 // find border face between theFirstNode and theSecondNode
3160 const SMDS_MeshElement* curElem = findAdjacentFace( theFirstNode, theSecondNode, 0 );
3164 theFaces.push_back( curElem );
3165 theNodes.push_back( theFirstNode );
3166 theNodes.push_back( theSecondNode );
3168 const SMDS_MeshNode* nodes [5], *nIgnore = theFirstNode, * nStart = theSecondNode;
3169 set < const SMDS_MeshElement* > foundElems;
3170 bool needTheLast = ( theLastNode != 0 );
3172 while ( nStart != theLastNode )
3174 if ( nStart == theFirstNode )
3175 return !needTheLast;
3177 // find all free border faces sharing form nStart
3179 list< const SMDS_MeshElement* > curElemList;
3180 list< const SMDS_MeshNode* > nStartList;
3181 SMDS_ElemIteratorPtr invElemIt = nStart->facesIterator();
3182 while ( invElemIt->more() ) {
3183 const SMDS_MeshElement* e = invElemIt->next();
3184 if ( e == curElem || foundElems.insert( e ).second )
3187 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
3188 int iNode = 0, nbNodes = e->NbNodes();
3189 while ( nIt->more() )
3190 nodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
3191 nodes[ iNode ] = nodes[ 0 ];
3193 for ( iNode = 0; iNode < nbNodes; iNode++ )
3194 if (((nodes[ iNode ] == nStart && nodes[ iNode + 1] != nIgnore ) ||
3195 (nodes[ iNode + 1] == nStart && nodes[ iNode ] != nIgnore )) &&
3196 ControlFreeBorder( &nodes[ iNode ], e->GetID() ))
3198 nStartList.push_back( nodes[ iNode + ( nodes[ iNode ] == nStart ? 1 : 0 )]);
3199 curElemList.push_back( e );
3203 // analyse the found
3205 int nbNewBorders = curElemList.size();
3206 if ( nbNewBorders == 0 ) {
3207 // no free border furthermore
3208 return !needTheLast;
3210 else if ( nbNewBorders == 1 ) {
3211 // one more element found
3213 nStart = nStartList.front();
3214 curElem = curElemList.front();
3215 theFaces.push_back( curElem );
3216 theNodes.push_back( nStart );
3219 // several continuations found
3220 list< const SMDS_MeshElement* >::iterator curElemIt;
3221 list< const SMDS_MeshNode* >::iterator nStartIt;
3222 // check if one of them reached the last node
3223 if ( needTheLast ) {
3224 for (curElemIt = curElemList.begin(), nStartIt = nStartList.begin();
3225 curElemIt!= curElemList.end();
3226 curElemIt++, nStartIt++ )
3227 if ( *nStartIt == theLastNode ) {
3228 theFaces.push_back( *curElemIt );
3229 theNodes.push_back( *nStartIt );
3233 // find the best free border by the continuations
3234 list<const SMDS_MeshNode*> contNodes[ 2 ], *cNL;
3235 list<const SMDS_MeshElement*> contFaces[ 2 ], *cFL;
3236 for (curElemIt = curElemList.begin(), nStartIt = nStartList.begin();
3237 curElemIt!= curElemList.end();
3238 curElemIt++, nStartIt++ )
3240 cNL = & contNodes[ contNodes[0].empty() ? 0 : 1 ];
3241 cFL = & contFaces[ contFaces[0].empty() ? 0 : 1 ];
3242 // find one more free border
3243 if ( ! findFreeBorder( nIgnore, nStart, theLastNode, *cNL, *cFL )) {
3247 else if ( !contNodes[0].empty() && !contNodes[1].empty() ) {
3248 // choice: clear a worse one
3249 int iLongest = ( contNodes[0].size() < contNodes[1].size() ? 1 : 0 );
3250 int iWorse = ( needTheLast ? 1 - iLongest : iLongest );
3251 contNodes[ iWorse ].clear();
3252 contFaces[ iWorse ].clear();
3255 if ( contNodes[0].empty() && contNodes[1].empty() )
3258 // append the best free border
3259 cNL = & contNodes[ contNodes[0].empty() ? 1 : 0 ];
3260 cFL = & contFaces[ contFaces[0].empty() ? 1 : 0 ];
3261 theNodes.pop_back(); // remove nIgnore
3262 theNodes.pop_back(); // remove nStart
3263 theFaces.pop_back(); // remove curElem
3264 list< const SMDS_MeshNode* >::iterator nIt = cNL->begin();
3265 list< const SMDS_MeshElement* >::iterator fIt = cFL->begin();
3266 for ( ; nIt != cNL->end(); nIt++ ) theNodes.push_back( *nIt );
3267 for ( ; fIt != cFL->end(); fIt++ ) theFaces.push_back( *fIt );
3270 } // several continuations found
3271 } // while ( nStart != theLastNode )
3276 //=======================================================================
3277 //function : CheckFreeBorderNodes
3278 //purpose : Return true if the tree nodes are on a free border
3279 //=======================================================================
3281 bool SMESH_MeshEditor::CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
3282 const SMDS_MeshNode* theNode2,
3283 const SMDS_MeshNode* theNode3)
3285 list< const SMDS_MeshNode* > nodes;
3286 list< const SMDS_MeshElement* > faces;
3287 return findFreeBorder( theNode1, theNode2, theNode3, nodes, faces);
3290 //=======================================================================
3291 //function : SewFreeBorder
3293 //=======================================================================
3295 SMESH_MeshEditor::Sew_Error
3296 SMESH_MeshEditor::SewFreeBorder (const SMDS_MeshNode* theBordFirstNode,
3297 const SMDS_MeshNode* theBordSecondNode,
3298 const SMDS_MeshNode* theBordLastNode,
3299 const SMDS_MeshNode* theSideFirstNode,
3300 const SMDS_MeshNode* theSideSecondNode,
3301 const SMDS_MeshNode* theSideThirdNode,
3302 const bool theSideIsFreeBorder,
3303 const bool toCreatePoly)
3305 MESSAGE("::SewFreeBorder()");
3306 Sew_Error aResult = SEW_OK;
3308 // ====================================
3309 // find side nodes and elements
3310 // ====================================
3312 list< const SMDS_MeshNode* > nSide[ 2 ];
3313 list< const SMDS_MeshElement* > eSide[ 2 ];
3314 list< const SMDS_MeshNode* >::iterator nIt[ 2 ];
3315 list< const SMDS_MeshElement* >::iterator eIt[ 2 ];
3319 if (!findFreeBorder(theBordFirstNode,theBordSecondNode,theBordLastNode,
3320 nSide[0], eSide[0])) {
3321 MESSAGE(" Free Border 1 not found " );
3322 aResult = SEW_BORDER1_NOT_FOUND;
3324 if (theSideIsFreeBorder)
3328 if (!findFreeBorder(theSideFirstNode, theSideSecondNode, theSideThirdNode,
3329 nSide[1], eSide[1])) {
3330 MESSAGE(" Free Border 2 not found " );
3331 aResult = ( aResult != SEW_OK ? SEW_BOTH_BORDERS_NOT_FOUND : SEW_BORDER2_NOT_FOUND );
3334 if ( aResult != SEW_OK )
3337 if (!theSideIsFreeBorder)
3342 // -------------------------------------------------------------------------
3344 // 1. If nodes to merge are not coincident, move nodes of the free border
3345 // from the coord sys defined by the direction from the first to last
3346 // nodes of the border to the correspondent sys of the side 2
3347 // 2. On the side 2, find the links most co-directed with the correspondent
3348 // links of the free border
3349 // -------------------------------------------------------------------------
3351 // 1. Since sewing may brake if there are volumes to split on the side 2,
3352 // we wont move nodes but just compute new coordinates for them
3353 typedef map<const SMDS_MeshNode*, gp_XYZ> TNodeXYZMap;
3354 TNodeXYZMap nBordXYZ;
3355 list< const SMDS_MeshNode* >& bordNodes = nSide[ 0 ];
3356 list< const SMDS_MeshNode* >::iterator nBordIt;
3358 gp_XYZ Pb1( theBordFirstNode->X(), theBordFirstNode->Y(), theBordFirstNode->Z() );
3359 gp_XYZ Pb2( theBordLastNode->X(), theBordLastNode->Y(), theBordLastNode->Z() );
3360 gp_XYZ Ps1( theSideFirstNode->X(), theSideFirstNode->Y(), theSideFirstNode->Z() );
3361 gp_XYZ Ps2( theSideSecondNode->X(), theSideSecondNode->Y(), theSideSecondNode->Z() );
3362 double tol2 = 1.e-8;
3363 gp_Vec Vbs1( Pb1 - Ps1 ),Vbs2( Pb2 - Ps2 );
3364 if ( Vbs1.SquareMagnitude() > tol2 || Vbs2.SquareMagnitude() > tol2 )
3366 // Need node movement.
3368 // find X and Z axes to create trsf
3369 gp_Vec Zb( Pb1 - Pb2 ), Zs( Ps1 - Ps2 );
3371 if ( X.SquareMagnitude() <= gp::Resolution() * gp::Resolution() )
3373 X = gp_Ax2( gp::Origin(), Zb ).XDirection();
3376 gp_Ax3 toBordAx( Pb1, Zb, X );
3377 gp_Ax3 fromSideAx( Ps1, Zs, X );
3378 gp_Ax3 toGlobalAx( gp::Origin(), gp::DZ(), gp::DX() );
3380 gp_Trsf toBordSys, fromSide2Sys;
3381 toBordSys.SetTransformation( toBordAx );
3382 fromSide2Sys.SetTransformation( fromSideAx, toGlobalAx );
3383 fromSide2Sys.SetScaleFactor( Zs.Magnitude() / Zb.Magnitude() );
3386 for ( nBordIt = bordNodes.begin(); nBordIt != bordNodes.end(); nBordIt++ ) {
3387 const SMDS_MeshNode* n = *nBordIt;
3388 gp_XYZ xyz( n->X(),n->Y(),n->Z() );
3389 toBordSys.Transforms( xyz );
3390 fromSide2Sys.Transforms( xyz );
3391 nBordXYZ.insert( TNodeXYZMap::value_type( n, xyz ));
3396 // just insert nodes XYZ in the nBordXYZ map
3397 for ( nBordIt = bordNodes.begin(); nBordIt != bordNodes.end(); nBordIt++ ) {
3398 const SMDS_MeshNode* n = *nBordIt;
3399 nBordXYZ.insert( TNodeXYZMap::value_type( n, gp_XYZ( n->X(),n->Y(),n->Z() )));
3403 // 2. On the side 2, find the links most co-directed with the correspondent
3404 // links of the free border
3406 list< const SMDS_MeshElement* >& sideElems = eSide[ 1 ];
3407 list< const SMDS_MeshNode* >& sideNodes = nSide[ 1 ];
3408 sideNodes.push_back( theSideFirstNode );
3410 bool hasVolumes = false;
3411 LinkID_Gen aLinkID_Gen( GetMeshDS() );
3412 set<long> foundSideLinkIDs, checkedLinkIDs;
3413 SMDS_VolumeTool volume;
3414 //const SMDS_MeshNode* faceNodes[ 4 ];
3416 const SMDS_MeshNode* sideNode;
3417 const SMDS_MeshElement* sideElem;
3418 const SMDS_MeshNode* prevSideNode = theSideFirstNode;
3419 const SMDS_MeshNode* prevBordNode = theBordFirstNode;
3420 nBordIt = bordNodes.begin();
3422 // border node position and border link direction to compare with
3423 gp_XYZ bordPos = nBordXYZ[ *nBordIt ];
3424 gp_XYZ bordDir = bordPos - nBordXYZ[ prevBordNode ];
3425 // choose next side node by link direction or by closeness to
3426 // the current border node:
3427 bool searchByDir = ( *nBordIt != theBordLastNode );
3429 // find the next node on the Side 2
3431 double maxDot = -DBL_MAX, minDist = DBL_MAX;
3433 checkedLinkIDs.clear();
3434 gp_XYZ prevXYZ( prevSideNode->X(), prevSideNode->Y(), prevSideNode->Z() );
3436 SMDS_ElemIteratorPtr invElemIt
3437 = prevSideNode->GetInverseElementIterator();
3438 while ( invElemIt->more() ) { // loop on inverse elements on the Side 2
3439 const SMDS_MeshElement* elem = invElemIt->next();
3440 // prepare data for a loop on links, of a face or a volume
3441 int iPrevNode, iNode = 0, nbNodes = elem->NbNodes();
3442 const SMDS_MeshNode* faceNodes[ nbNodes ];
3443 bool isVolume = volume.Set( elem );
3444 const SMDS_MeshNode** nodes = isVolume ? volume.GetNodes() : faceNodes;
3445 if ( isVolume ) // --volume
3447 else if ( nbNodes > 2 ) { // --face
3448 // retrieve all face nodes and find iPrevNode - an index of the prevSideNode
3449 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
3450 while ( nIt->more() ) {
3451 nodes[ iNode ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
3452 if ( nodes[ iNode++ ] == prevSideNode )
3453 iPrevNode = iNode - 1;
3455 // there are 2 links to check
3460 // loop on links, to be precise, on the second node of links
3461 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
3462 const SMDS_MeshNode* n = nodes[ iNode ];
3464 if ( !volume.IsLinked( n, prevSideNode ))
3467 if ( iNode ) // a node before prevSideNode
3468 n = nodes[ iPrevNode == 0 ? elem->NbNodes() - 1 : iPrevNode - 1 ];
3469 else // a node after prevSideNode
3470 n = nodes[ iPrevNode + 1 == elem->NbNodes() ? 0 : iPrevNode + 1 ];
3472 // check if this link was already used
3473 long iLink = aLinkID_Gen.GetLinkID( prevSideNode, n );
3474 bool isJustChecked = !checkedLinkIDs.insert( iLink ).second;
3475 if (!isJustChecked &&
3476 foundSideLinkIDs.find( iLink ) == foundSideLinkIDs.end() ) {
3477 // test a link geometrically
3478 gp_XYZ nextXYZ ( n->X(), n->Y(), n->Z() );
3479 bool linkIsBetter = false;
3481 if ( searchByDir ) { // choose most co-directed link
3482 dot = bordDir * ( nextXYZ - prevXYZ ).Normalized();
3483 linkIsBetter = ( dot > maxDot );
3485 else { // choose link with the node closest to bordPos
3486 dist = ( nextXYZ - bordPos ).SquareModulus();
3487 linkIsBetter = ( dist < minDist );
3489 if ( linkIsBetter ) {
3498 } // loop on inverse elements of prevSideNode
3501 MESSAGE(" Cant find path by links of the Side 2 ");
3502 return SEW_BAD_SIDE_NODES;
3504 sideNodes.push_back( sideNode );
3505 sideElems.push_back( sideElem );
3506 foundSideLinkIDs.insert ( linkID );
3507 prevSideNode = sideNode;
3509 if ( *nBordIt == theBordLastNode )
3510 searchByDir = false;
3512 // find the next border link to compare with
3513 gp_XYZ sidePos( sideNode->X(), sideNode->Y(), sideNode->Z() );
3514 searchByDir = ( bordDir * ( sidePos - bordPos ) <= 0 );
3515 while ( *nBordIt != theBordLastNode && !searchByDir ) {
3516 prevBordNode = *nBordIt;
3518 bordPos = nBordXYZ[ *nBordIt ];
3519 bordDir = bordPos - nBordXYZ[ prevBordNode ];
3520 searchByDir = ( bordDir * ( sidePos - bordPos ) <= 0 );
3524 while ( sideNode != theSideSecondNode );
3526 if ( hasVolumes && sideNodes.size () != bordNodes.size() ) {
3527 MESSAGE("VOLUME SPLITTING IS FORBIDDEN");
3528 return SEW_VOLUMES_TO_SPLIT; // volume splitting is forbidden
3530 } // end nodes search on the side 2
3532 // ============================
3533 // sew the border to the side 2
3534 // ============================
3536 int nbNodes[] = { nSide[0].size(), nSide[1].size() };
3537 int maxNbNodes = Max( nbNodes[0], nbNodes[1] );
3539 TListOfListOfNodes nodeGroupsToMerge;
3540 if ( nbNodes[0] == nbNodes[1] ||
3541 ( theSideIsFreeBorder && !theSideThirdNode)) {
3543 // all nodes are to be merged
3545 for (nIt[0] = nSide[0].begin(), nIt[1] = nSide[1].begin();
3546 nIt[0] != nSide[0].end() && nIt[1] != nSide[1].end();
3547 nIt[0]++, nIt[1]++ )
3549 nodeGroupsToMerge.push_back( list<const SMDS_MeshNode*>() );
3550 nodeGroupsToMerge.back().push_back( *nIt[1] ); // to keep
3551 nodeGroupsToMerge.back().push_back( *nIt[0] ); // tp remove
3556 // insert new nodes into the border and the side to get equal nb of segments
3558 // get normalized parameters of nodes on the borders
3559 double param[ 2 ][ maxNbNodes ];
3561 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
3562 list< const SMDS_MeshNode* >& nodes = nSide[ iBord ];
3563 list< const SMDS_MeshNode* >::iterator nIt = nodes.begin();
3564 const SMDS_MeshNode* nPrev = *nIt;
3565 double bordLength = 0;
3566 for ( iNode = 0; nIt != nodes.end(); nIt++, iNode++ ) { // loop on border nodes
3567 const SMDS_MeshNode* nCur = *nIt;
3568 gp_XYZ segment (nCur->X() - nPrev->X(),
3569 nCur->Y() - nPrev->Y(),
3570 nCur->Z() - nPrev->Z());
3571 double segmentLen = segment.Modulus();
3572 bordLength += segmentLen;
3573 param[ iBord ][ iNode ] = bordLength;
3576 // normalize within [0,1]
3577 for ( iNode = 0; iNode < nbNodes[ iBord ]; iNode++ ) {
3578 param[ iBord ][ iNode ] /= bordLength;
3582 // loop on border segments
3583 const SMDS_MeshNode *nPrev[ 2 ] = { 0, 0 };
3584 int i[ 2 ] = { 0, 0 };
3585 nIt[0] = nSide[0].begin(); eIt[0] = eSide[0].begin();
3586 nIt[1] = nSide[1].begin(); eIt[1] = eSide[1].begin();
3588 TElemOfNodeListMap insertMap;
3589 TElemOfNodeListMap::iterator insertMapIt;
3591 // key: elem to insert nodes into
3592 // value: 2 nodes to insert between + nodes to be inserted
3594 bool next[ 2 ] = { false, false };
3596 // find min adjacent segment length after sewing
3597 double nextParam = 10., prevParam = 0;
3598 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
3599 if ( i[ iBord ] + 1 < nbNodes[ iBord ])
3600 nextParam = Min( nextParam, param[iBord][ i[iBord] + 1 ]);
3601 if ( i[ iBord ] > 0 )
3602 prevParam = Max( prevParam, param[iBord][ i[iBord] - 1 ]);
3604 double minParam = Min( param[ 0 ][ i[0] ], param[ 1 ][ i[1] ]);
3605 double maxParam = Max( param[ 0 ][ i[0] ], param[ 1 ][ i[1] ]);
3606 double minSegLen = Min( nextParam - minParam, maxParam - prevParam );
3608 // choose to insert or to merge nodes
3609 double du = param[ 1 ][ i[1] ] - param[ 0 ][ i[0] ];
3610 if ( Abs( du ) <= minSegLen * 0.2 ) {
3613 nodeGroupsToMerge.push_back( list<const SMDS_MeshNode*>() );
3614 const SMDS_MeshNode* n0 = *nIt[0];
3615 const SMDS_MeshNode* n1 = *nIt[1];
3616 nodeGroupsToMerge.back().push_back( n1 );
3617 nodeGroupsToMerge.back().push_back( n0 );
3618 // position of node of the border changes due to merge
3619 param[ 0 ][ i[0] ] += du;
3620 // move n1 for the sake of elem shape evaluation during insertion.
3621 // n1 will be removed by MergeNodes() anyway
3622 const_cast<SMDS_MeshNode*>( n0 )->setXYZ( n1->X(), n1->Y(), n1->Z() );
3623 next[0] = next[1] = true;
3628 int intoBord = ( du < 0 ) ? 0 : 1;
3629 const SMDS_MeshElement* elem = *eIt[ intoBord ];
3630 const SMDS_MeshNode* n1 = nPrev[ intoBord ];
3631 const SMDS_MeshNode* n2 = *nIt[ intoBord ];
3632 const SMDS_MeshNode* nIns = *nIt[ 1 - intoBord ];
3633 if ( intoBord == 1 ) {
3634 // move node of the border to be on a link of elem of the side
3635 gp_XYZ p1 (n1->X(), n1->Y(), n1->Z());
3636 gp_XYZ p2 (n2->X(), n2->Y(), n2->Z());
3637 double ratio = du / ( param[ 1 ][ i[1] ] - param[ 1 ][ i[1]-1 ]);
3638 gp_XYZ p = p2 * ( 1 - ratio ) + p1 * ratio;
3639 GetMeshDS()->MoveNode( nIns, p.X(), p.Y(), p.Z() );
3641 insertMapIt = insertMap.find( elem );
3642 bool notFound = ( insertMapIt == insertMap.end() );
3643 bool otherLink = ( !notFound && (*insertMapIt).second.front() != n1 );
3645 // insert into another link of the same element:
3646 // 1. perform insertion into the other link of the elem
3647 list<const SMDS_MeshNode*> & nodeList = (*insertMapIt).second;
3648 const SMDS_MeshNode* n12 = nodeList.front(); nodeList.pop_front();
3649 const SMDS_MeshNode* n22 = nodeList.front(); nodeList.pop_front();
3650 InsertNodesIntoLink( elem, n12, n22, nodeList, toCreatePoly );
3651 // 2. perform insertion into the link of adjacent faces
3653 const SMDS_MeshElement* adjElem = findAdjacentFace( n12, n22, elem );
3655 InsertNodesIntoLink( adjElem, n12, n22, nodeList, toCreatePoly );
3659 // 3. find an element appeared on n1 and n2 after the insertion
3660 insertMap.erase( elem );
3661 elem = findAdjacentFace( n1, n2, 0 );
3663 if ( notFound || otherLink ) {
3664 // add element and nodes of the side into the insertMap
3665 insertMapIt = insertMap.insert
3666 ( TElemOfNodeListMap::value_type( elem, list<const SMDS_MeshNode*>() )).first;
3667 (*insertMapIt).second.push_back( n1 );
3668 (*insertMapIt).second.push_back( n2 );
3670 // add node to be inserted into elem
3671 (*insertMapIt).second.push_back( nIns );
3672 next[ 1 - intoBord ] = true;
3675 // go to the next segment
3676 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
3677 if ( next[ iBord ] ) {
3678 if ( i[ iBord ] != 0 && eIt[ iBord ] != eSide[ iBord ].end())
3680 nPrev[ iBord ] = *nIt[ iBord ];
3681 nIt[ iBord ]++; i[ iBord ]++;
3685 while ( nIt[0] != nSide[0].end() && nIt[1] != nSide[1].end());
3687 // perform insertion of nodes into elements
3689 for (insertMapIt = insertMap.begin();
3690 insertMapIt != insertMap.end();
3693 const SMDS_MeshElement* elem = (*insertMapIt).first;
3694 list<const SMDS_MeshNode*> & nodeList = (*insertMapIt).second;
3695 const SMDS_MeshNode* n1 = nodeList.front(); nodeList.pop_front();
3696 const SMDS_MeshNode* n2 = nodeList.front(); nodeList.pop_front();
3698 InsertNodesIntoLink( elem, n1, n2, nodeList, toCreatePoly );
3700 if ( !theSideIsFreeBorder ) {
3701 // look for and insert nodes into the faces adjacent to elem
3703 const SMDS_MeshElement* adjElem = findAdjacentFace( n1, n2, elem );
3705 InsertNodesIntoLink( adjElem, n1, n2, nodeList, toCreatePoly );
3712 } // end: insert new nodes
3714 MergeNodes ( nodeGroupsToMerge );
3719 //=======================================================================
3720 //function : InsertNodesIntoLink
3721 //purpose : insert theNodesToInsert into theFace between theBetweenNode1
3722 // and theBetweenNode2 and split theElement
3723 //=======================================================================
3725 void SMESH_MeshEditor::InsertNodesIntoLink(const SMDS_MeshElement* theFace,
3726 const SMDS_MeshNode* theBetweenNode1,
3727 const SMDS_MeshNode* theBetweenNode2,
3728 list<const SMDS_MeshNode*>& theNodesToInsert,
3729 const bool toCreatePoly)
3731 if ( theFace->GetType() != SMDSAbs_Face ) return;
3733 // find indices of 2 link nodes and of the rest nodes
3734 int iNode = 0, il1, il2, i3, i4;
3735 il1 = il2 = i3 = i4 = -1;
3736 const SMDS_MeshNode* nodes[ theFace->NbNodes() ];
3737 SMDS_ElemIteratorPtr nodeIt = theFace->nodesIterator();
3738 while ( nodeIt->more() ) {
3739 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3740 if ( n == theBetweenNode1 )
3742 else if ( n == theBetweenNode2 )
3748 nodes[ iNode++ ] = n;
3750 if ( il1 < 0 || il2 < 0 || i3 < 0 )
3753 // arrange link nodes to go one after another regarding the face orientation
3754 bool reverse = ( Abs( il2 - il1 ) == 1 ? il2 < il1 : il1 < il2 );
3755 list<const SMDS_MeshNode *> aNodesToInsert = theNodesToInsert;
3760 aNodesToInsert.reverse();
3762 // check that not link nodes of a quadrangles are in good order
3763 int nbFaceNodes = theFace->NbNodes();
3764 if ( nbFaceNodes == 4 && i4 - i3 != 1 ) {
3770 if (toCreatePoly || theFace->IsPoly()) {
3773 vector<const SMDS_MeshNode *> poly_nodes (nbFaceNodes + aNodesToInsert.size());
3775 // add nodes of face up to first node of link
3777 nodeIt = theFace->nodesIterator();
3778 while ( nodeIt->more() && !isFLN ) {
3779 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3780 poly_nodes[iNode++] = n;
3781 if (n == nodes[il1]) {
3786 // add nodes to insert
3787 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
3788 for (; nIt != aNodesToInsert.end(); nIt++) {
3789 poly_nodes[iNode++] = *nIt;
3792 // add nodes of face starting from last node of link
3793 while ( nodeIt->more() ) {
3794 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3795 poly_nodes[iNode++] = n;
3798 // edit or replace the face
3799 SMESHDS_Mesh *aMesh = GetMeshDS();
3801 if (theFace->IsPoly()) {
3802 aMesh->ChangePolygonNodes(theFace, poly_nodes);
3805 int aShapeId = FindShape( theFace );
3807 SMDS_MeshElement* newElem = aMesh->AddPolygonalFace(poly_nodes);
3808 if ( aShapeId && newElem )
3809 aMesh->SetMeshElementOnShape( newElem, aShapeId );
3811 aMesh->RemoveElement(theFace);
3816 // put aNodesToInsert between theBetweenNode1 and theBetweenNode2
3817 int nbLinkNodes = 2 + aNodesToInsert.size();
3818 const SMDS_MeshNode* linkNodes[ nbLinkNodes ];
3819 linkNodes[ 0 ] = nodes[ il1 ];
3820 linkNodes[ nbLinkNodes - 1 ] = nodes[ il2 ];
3821 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
3822 for ( iNode = 1; nIt != aNodesToInsert.end(); nIt++ ) {
3823 linkNodes[ iNode++ ] = *nIt;
3825 // decide how to split a quadrangle: compare possible variants
3826 // and choose which of splits to be a quadrangle
3827 int i1, i2, iSplit, nbSplits = nbLinkNodes - 1, iBestQuad;
3828 if ( nbFaceNodes == 3 )
3830 iBestQuad = nbSplits;
3833 else if ( nbFaceNodes == 4 )
3835 SMESH::Controls::NumericalFunctorPtr aCrit( new SMESH::Controls::AspectRatio);
3836 double aBestRate = DBL_MAX;
3837 for ( int iQuad = 0; iQuad < nbSplits; iQuad++ ) {
3839 double aBadRate = 0;
3840 // evaluate elements quality
3841 for ( iSplit = 0; iSplit < nbSplits; iSplit++ ) {
3842 if ( iSplit == iQuad ) {
3843 SMDS_FaceOfNodes quad (linkNodes[ i1++ ],
3847 aBadRate += getBadRate( &quad, aCrit );
3850 SMDS_FaceOfNodes tria (linkNodes[ i1++ ],
3852 nodes[ iSplit < iQuad ? i4 : i3 ]);
3853 aBadRate += getBadRate( &tria, aCrit );
3857 if ( aBadRate < aBestRate ) {
3859 aBestRate = aBadRate;
3864 // create new elements
3865 SMESHDS_Mesh *aMesh = GetMeshDS();
3866 int aShapeId = FindShape( theFace );
3869 for ( iSplit = 0; iSplit < nbSplits - 1; iSplit++ ) {
3870 SMDS_MeshElement* newElem = 0;
3871 if ( iSplit == iBestQuad )
3872 newElem = aMesh->AddFace (linkNodes[ i1++ ],
3877 newElem = aMesh->AddFace (linkNodes[ i1++ ],
3879 nodes[ iSplit < iBestQuad ? i4 : i3 ]);
3880 if ( aShapeId && newElem )
3881 aMesh->SetMeshElementOnShape( newElem, aShapeId );
3884 // change nodes of theFace
3885 const SMDS_MeshNode* newNodes[ 4 ];
3886 newNodes[ 0 ] = linkNodes[ i1 ];
3887 newNodes[ 1 ] = linkNodes[ i2 ];
3888 newNodes[ 2 ] = nodes[ iSplit >= iBestQuad ? i3 : i4 ];
3889 newNodes[ 3 ] = nodes[ i4 ];
3890 aMesh->ChangeElementNodes( theFace, newNodes, iSplit == iBestQuad ? 4 : 3 );
3893 //=======================================================================
3894 //function : SewSideElements
3896 //=======================================================================
3898 SMESH_MeshEditor::Sew_Error
3899 SMESH_MeshEditor::SewSideElements (set<const SMDS_MeshElement*>& theSide1,
3900 set<const SMDS_MeshElement*>& theSide2,
3901 const SMDS_MeshNode* theFirstNode1,
3902 const SMDS_MeshNode* theFirstNode2,
3903 const SMDS_MeshNode* theSecondNode1,
3904 const SMDS_MeshNode* theSecondNode2)
3906 MESSAGE ("::::SewSideElements()");
3907 if ( theSide1.size() != theSide2.size() )
3908 return SEW_DIFF_NB_OF_ELEMENTS;
3910 Sew_Error aResult = SEW_OK;
3912 // 1. Build set of faces representing each side
3913 // 2. Find which nodes of the side 1 to merge with ones on the side 2
3914 // 3. Replace nodes in elements of the side 1 and remove replaced nodes
3916 // =======================================================================
3917 // 1. Build set of faces representing each side:
3918 // =======================================================================
3919 // a. build set of nodes belonging to faces
3920 // b. complete set of faces: find missing fices whose nodes are in set of nodes
3921 // c. create temporary faces representing side of volumes if correspondent
3922 // face does not exist
3924 SMESHDS_Mesh* aMesh = GetMeshDS();
3925 SMDS_Mesh aTmpFacesMesh;
3926 set<const SMDS_MeshElement*> faceSet1, faceSet2;
3927 set<const SMDS_MeshElement*> volSet1, volSet2;
3928 set<const SMDS_MeshNode*> nodeSet1, nodeSet2;
3929 set<const SMDS_MeshElement*> * faceSetPtr[] = { &faceSet1, &faceSet2 };
3930 set<const SMDS_MeshElement*> * volSetPtr[] = { &volSet1, &volSet2 };
3931 set<const SMDS_MeshNode*> * nodeSetPtr[] = { &nodeSet1, &nodeSet2 };
3932 set<const SMDS_MeshElement*> * elemSetPtr[] = { &theSide1, &theSide2 };
3933 int iSide, iFace, iNode;
3935 for ( iSide = 0; iSide < 2; iSide++ ) {
3936 set<const SMDS_MeshNode*> * nodeSet = nodeSetPtr[ iSide ];
3937 set<const SMDS_MeshElement*> * elemSet = elemSetPtr[ iSide ];
3938 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
3939 set<const SMDS_MeshElement*> * volSet = volSetPtr [ iSide ];
3940 set<const SMDS_MeshElement*>::iterator vIt, eIt;
3941 set<const SMDS_MeshNode*>::iterator nIt;
3943 // -----------------------------------------------------------
3944 // 1a. Collect nodes of existing faces
3945 // and build set of face nodes in order to detect missing
3946 // faces corresponing to sides of volumes
3947 // -----------------------------------------------------------
3949 set< set <const SMDS_MeshNode*> > setOfFaceNodeSet;
3951 // loop on the given element of a side
3952 for (eIt = elemSet->begin(); eIt != elemSet->end(); eIt++ ) {
3953 const SMDS_MeshElement* elem = *eIt;
3954 if ( elem->GetType() == SMDSAbs_Face ) {
3955 faceSet->insert( elem );
3956 set <const SMDS_MeshNode*> faceNodeSet;
3957 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
3958 while ( nodeIt->more() ) {
3959 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3960 nodeSet->insert( n );
3961 faceNodeSet.insert( n );
3963 setOfFaceNodeSet.insert( faceNodeSet );
3965 else if ( elem->GetType() == SMDSAbs_Volume )
3966 volSet->insert( elem );
3968 // ------------------------------------------------------------------------------
3969 // 1b. Complete set of faces: find missing fices whose nodes are in set of nodes
3970 // ------------------------------------------------------------------------------
3972 for ( nIt = nodeSet->begin(); nIt != nodeSet->end(); nIt++ ) { // loop on nodes of iSide
3973 SMDS_ElemIteratorPtr fIt = (*nIt)->facesIterator();
3974 while ( fIt->more() ) { // loop on faces sharing a node
3975 const SMDS_MeshElement* f = fIt->next();
3976 if ( faceSet->find( f ) == faceSet->end() ) {
3977 // check if all nodes are in nodeSet and
3978 // complete setOfFaceNodeSet if they are
3979 set <const SMDS_MeshNode*> faceNodeSet;
3980 SMDS_ElemIteratorPtr nodeIt = f->nodesIterator();
3981 bool allInSet = true;
3982 while ( nodeIt->more() && allInSet ) { // loop on nodes of a face
3983 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3984 if ( nodeSet->find( n ) == nodeSet->end() )
3987 faceNodeSet.insert( n );
3990 faceSet->insert( f );
3991 setOfFaceNodeSet.insert( faceNodeSet );
3997 // -------------------------------------------------------------------------
3998 // 1c. Create temporary faces representing sides of volumes if correspondent
3999 // face does not exist
4000 // -------------------------------------------------------------------------
4002 if ( !volSet->empty() )
4004 //int nodeSetSize = nodeSet->size();
4006 // loop on given volumes
4007 for ( vIt = volSet->begin(); vIt != volSet->end(); vIt++ ) {
4008 SMDS_VolumeTool vol (*vIt);
4009 // loop on volume faces: find free faces
4010 // --------------------------------------
4011 list<const SMDS_MeshElement* > freeFaceList;
4012 for ( iFace = 0; iFace < vol.NbFaces(); iFace++ ) {
4013 if ( !vol.IsFreeFace( iFace ))
4015 // check if there is already a face with same nodes in a face set
4016 const SMDS_MeshElement* aFreeFace = 0;
4017 const SMDS_MeshNode** fNodes = vol.GetFaceNodes( iFace );
4018 int nbNodes = vol.NbFaceNodes( iFace );
4019 set <const SMDS_MeshNode*> faceNodeSet;
4020 vol.GetFaceNodes( iFace, faceNodeSet );
4021 bool isNewFace = setOfFaceNodeSet.insert( faceNodeSet ).second;
4023 // no such a face is given but it still can exist, check it
4024 if ( nbNodes == 3 ) {
4025 aFreeFace = aMesh->FindFace( fNodes[0],fNodes[1],fNodes[2] );
4026 } else if ( nbNodes == 4 ) {
4027 aFreeFace = aMesh->FindFace( fNodes[0],fNodes[1],fNodes[2],fNodes[3] );
4029 vector<const SMDS_MeshNode *> poly_nodes (nbNodes);
4030 for (int inode = 0; inode < nbNodes; inode++) {
4031 poly_nodes[inode] = fNodes[inode];
4033 aFreeFace = aMesh->FindFace(poly_nodes);
4037 // create a temporary face
4038 if ( nbNodes == 3 ) {
4039 aFreeFace = aTmpFacesMesh.AddFace( fNodes[0],fNodes[1],fNodes[2] );
4040 } else if ( nbNodes == 4 ) {
4041 aFreeFace = aTmpFacesMesh.AddFace( fNodes[0],fNodes[1],fNodes[2],fNodes[3] );
4043 vector<const SMDS_MeshNode *> poly_nodes (nbNodes);
4044 for (int inode = 0; inode < nbNodes; inode++) {
4045 poly_nodes[inode] = fNodes[inode];
4047 aFreeFace = aTmpFacesMesh.AddPolygonalFace(poly_nodes);
4051 freeFaceList.push_back( aFreeFace );
4053 } // loop on faces of a volume
4055 // choose one of several free faces
4056 // --------------------------------------
4057 if ( freeFaceList.size() > 1 ) {
4058 // choose a face having max nb of nodes shared by other elems of a side
4059 int maxNbNodes = -1/*, nbExcludedFaces = 0*/;
4060 list<const SMDS_MeshElement* >::iterator fIt = freeFaceList.begin();
4061 while ( fIt != freeFaceList.end() ) { // loop on free faces
4062 int nbSharedNodes = 0;
4063 SMDS_ElemIteratorPtr nodeIt = (*fIt)->nodesIterator();
4064 while ( nodeIt->more() ) { // loop on free face nodes
4065 const SMDS_MeshNode* n =
4066 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
4067 SMDS_ElemIteratorPtr invElemIt = n->GetInverseElementIterator();
4068 while ( invElemIt->more() ) {
4069 const SMDS_MeshElement* e = invElemIt->next();
4070 if ( faceSet->find( e ) != faceSet->end() )
4072 if ( elemSet->find( e ) != elemSet->end() )
4076 if ( nbSharedNodes >= maxNbNodes ) {
4077 maxNbNodes = nbSharedNodes;
4081 freeFaceList.erase( fIt++ ); // here fIt++ occures before erase
4083 if ( freeFaceList.size() > 1 )
4085 // could not choose one face, use another way
4086 // choose a face most close to the bary center of the opposite side
4087 gp_XYZ aBC( 0., 0., 0. );
4088 set <const SMDS_MeshNode*> addedNodes;
4089 set<const SMDS_MeshElement*> * elemSet2 = elemSetPtr[ 1 - iSide ];
4090 eIt = elemSet2->begin();
4091 for ( eIt = elemSet2->begin(); eIt != elemSet2->end(); eIt++ ) {
4092 SMDS_ElemIteratorPtr nodeIt = (*eIt)->nodesIterator();
4093 while ( nodeIt->more() ) { // loop on free face nodes
4094 const SMDS_MeshNode* n =
4095 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
4096 if ( addedNodes.insert( n ).second )
4097 aBC += gp_XYZ( n->X(),n->Y(),n->Z() );
4100 aBC /= addedNodes.size();
4101 double minDist = DBL_MAX;
4102 fIt = freeFaceList.begin();
4103 while ( fIt != freeFaceList.end() ) { // loop on free faces
4105 SMDS_ElemIteratorPtr nodeIt = (*fIt)->nodesIterator();
4106 while ( nodeIt->more() ) { // loop on free face nodes
4107 const SMDS_MeshNode* n =
4108 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
4109 gp_XYZ p( n->X(),n->Y(),n->Z() );
4110 dist += ( aBC - p ).SquareModulus();
4112 if ( dist < minDist ) {
4114 freeFaceList.erase( freeFaceList.begin(), fIt++ );
4117 fIt = freeFaceList.erase( fIt++ );
4120 } // choose one of several free faces of a volume
4122 if ( freeFaceList.size() == 1 ) {
4123 const SMDS_MeshElement* aFreeFace = freeFaceList.front();
4124 faceSet->insert( aFreeFace );
4125 // complete a node set with nodes of a found free face
4126 // for ( iNode = 0; iNode < ; iNode++ )
4127 // nodeSet->insert( fNodes[ iNode ] );
4130 } // loop on volumes of a side
4132 // // complete a set of faces if new nodes in a nodeSet appeared
4133 // // ----------------------------------------------------------
4134 // if ( nodeSetSize != nodeSet->size() ) {
4135 // for ( ; nIt != nodeSet->end(); nIt++ ) { // loop on nodes of iSide
4136 // SMDS_ElemIteratorPtr fIt = (*nIt)->facesIterator();
4137 // while ( fIt->more() ) { // loop on faces sharing a node
4138 // const SMDS_MeshElement* f = fIt->next();
4139 // if ( faceSet->find( f ) == faceSet->end() ) {
4140 // // check if all nodes are in nodeSet and
4141 // // complete setOfFaceNodeSet if they are
4142 // set <const SMDS_MeshNode*> faceNodeSet;
4143 // SMDS_ElemIteratorPtr nodeIt = f->nodesIterator();
4144 // bool allInSet = true;
4145 // while ( nodeIt->more() && allInSet ) { // loop on nodes of a face
4146 // const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
4147 // if ( nodeSet->find( n ) == nodeSet->end() )
4148 // allInSet = false;
4150 // faceNodeSet.insert( n );
4152 // if ( allInSet ) {
4153 // faceSet->insert( f );
4154 // setOfFaceNodeSet.insert( faceNodeSet );
4160 } // Create temporary faces, if there are volumes given
4163 if ( faceSet1.size() != faceSet2.size() ) {
4164 // delete temporary faces: they are in reverseElements of actual nodes
4165 SMDS_FaceIteratorPtr tmpFaceIt = aTmpFacesMesh.facesIterator();
4166 while ( tmpFaceIt->more() )
4167 aTmpFacesMesh.RemoveElement( tmpFaceIt->next() );
4168 MESSAGE("Diff nb of faces");
4169 return SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
4172 // ============================================================
4173 // 2. Find nodes to merge:
4174 // bind a node to remove to a node to put instead
4175 // ============================================================
4177 TNodeNodeMap nReplaceMap; // bind a node to remove to a node to put instead
4178 if ( theFirstNode1 != theFirstNode2 )
4179 nReplaceMap.insert( TNodeNodeMap::value_type( theFirstNode1, theFirstNode2 ));
4180 if ( theSecondNode1 != theSecondNode2 )
4181 nReplaceMap.insert( TNodeNodeMap::value_type( theSecondNode1, theSecondNode2 ));
4183 LinkID_Gen aLinkID_Gen( GetMeshDS() );
4184 set< long > linkIdSet; // links to process
4185 linkIdSet.insert( aLinkID_Gen.GetLinkID( theFirstNode1, theSecondNode1 ));
4187 typedef pair< const SMDS_MeshNode*, const SMDS_MeshNode* > TPairOfNodes;
4188 list< TPairOfNodes > linkList[2];
4189 linkList[0].push_back( TPairOfNodes( theFirstNode1, theSecondNode1 ));
4190 linkList[1].push_back( TPairOfNodes( theFirstNode2, theSecondNode2 ));
4191 // loop on links in linkList; find faces by links and append links
4192 // of the found faces to linkList
4193 list< TPairOfNodes >::iterator linkIt[] = { linkList[0].begin(), linkList[1].begin() } ;
4194 for ( ; linkIt[0] != linkList[0].end(); linkIt[0]++, linkIt[1]++ )
4196 TPairOfNodes link[] = { *linkIt[0], *linkIt[1] };
4197 long linkID = aLinkID_Gen.GetLinkID( link[0].first, link[0].second );
4198 if ( linkIdSet.find( linkID ) == linkIdSet.end() )
4201 // by links, find faces in the face sets,
4202 // and find indices of link nodes in the found faces;
4203 // in a face set, there is only one or no face sharing a link
4204 // ---------------------------------------------------------------
4206 const SMDS_MeshElement* face[] = { 0, 0 };
4207 const SMDS_MeshNode* faceNodes[ 2 ][ 5 ];
4208 const SMDS_MeshNode* notLinkNodes[ 2 ][ 2 ] = {{ 0, 0 },{ 0, 0 }} ;
4209 int iLinkNode[2][2];
4210 for ( iSide = 0; iSide < 2; iSide++ ) { // loop on 2 sides
4211 const SMDS_MeshNode* n1 = link[iSide].first;
4212 const SMDS_MeshNode* n2 = link[iSide].second;
4213 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
4214 set< const SMDS_MeshElement* > fMap;
4215 for ( int i = 0; i < 2; i++ ) { // loop on 2 nodes of a link
4216 const SMDS_MeshNode* n = i ? n1 : n2; // a node of a link
4217 SMDS_ElemIteratorPtr fIt = n->facesIterator();
4218 while ( fIt->more() ) { // loop on faces sharing a node
4219 const SMDS_MeshElement* f = fIt->next();
4220 if (faceSet->find( f ) != faceSet->end() && // f is in face set
4221 ! fMap.insert( f ).second ) // f encounters twice
4223 if ( face[ iSide ] ) {
4224 MESSAGE( "2 faces per link " );
4225 aResult = iSide ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES;
4229 faceSet->erase( f );
4230 // get face nodes and find ones of a link
4232 SMDS_ElemIteratorPtr nIt = f->nodesIterator();
4233 while ( nIt->more() ) {
4234 const SMDS_MeshNode* n =
4235 static_cast<const SMDS_MeshNode*>( nIt->next() );
4237 iLinkNode[ iSide ][ 0 ] = iNode;
4239 iLinkNode[ iSide ][ 1 ] = iNode;
4240 else if ( notLinkNodes[ iSide ][ 0 ] )
4241 notLinkNodes[ iSide ][ 1 ] = n;
4243 notLinkNodes[ iSide ][ 0 ] = n;
4244 faceNodes[ iSide ][ iNode++ ] = n;
4246 faceNodes[ iSide ][ iNode ] = faceNodes[ iSide ][ 0 ];
4251 // check similarity of elements of the sides
4252 if (aResult == SEW_OK && ( face[0] && !face[1] ) || ( !face[0] && face[1] )) {
4253 MESSAGE("Correspondent face not found on side " << ( face[0] ? 1 : 0 ));
4254 if ( nReplaceMap.size() == 2 ) // faces on input nodes not found
4255 aResult = ( face[0] ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES );
4257 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
4258 break; // do not return because it s necessary to remove tmp faces
4261 // set nodes to merge
4262 // -------------------
4264 if ( face[0] && face[1] )
4266 int nbNodes = face[0]->NbNodes();
4267 if ( nbNodes != face[1]->NbNodes() ) {
4268 MESSAGE("Diff nb of face nodes");
4269 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
4270 break; // do not return because it s necessary to remove tmp faces
4272 bool reverse[] = { false, false }; // order of notLinkNodes of quadrangle
4274 nReplaceMap.insert( TNodeNodeMap::value_type
4275 ( notLinkNodes[0][0], notLinkNodes[1][0] ));
4277 for ( iSide = 0; iSide < 2; iSide++ ) { // loop on 2 sides
4278 // analyse link orientation in faces
4279 int i1 = iLinkNode[ iSide ][ 0 ];
4280 int i2 = iLinkNode[ iSide ][ 1 ];
4281 reverse[ iSide ] = Abs( i1 - i2 ) == 1 ? i1 > i2 : i2 > i1;
4282 // if notLinkNodes are the first and the last ones, then
4283 // their order does not correspond to the link orientation
4284 if (( i1 == 1 && i2 == 2 ) ||
4285 ( i1 == 2 && i2 == 1 ))
4286 reverse[ iSide ] = !reverse[ iSide ];
4288 if ( reverse[0] == reverse[1] ) {
4289 nReplaceMap.insert( TNodeNodeMap::value_type
4290 ( notLinkNodes[0][0], notLinkNodes[1][0] ));
4291 nReplaceMap.insert( TNodeNodeMap::value_type
4292 ( notLinkNodes[0][1], notLinkNodes[1][1] ));
4295 nReplaceMap.insert( TNodeNodeMap::value_type
4296 ( notLinkNodes[0][0], notLinkNodes[1][1] ));
4297 nReplaceMap.insert( TNodeNodeMap::value_type
4298 ( notLinkNodes[0][1], notLinkNodes[1][0] ));
4302 // add other links of the faces to linkList
4303 // -----------------------------------------
4305 const SMDS_MeshNode** nodes = faceNodes[ 0 ];
4306 for ( iNode = 0; iNode < nbNodes; iNode++ )
4308 linkID = aLinkID_Gen.GetLinkID( nodes[iNode], nodes[iNode+1] );
4309 pair< set<long>::iterator, bool > iter_isnew = linkIdSet.insert( linkID );
4310 if ( !iter_isnew.second ) { // already in a set: no need to process
4311 linkIdSet.erase( iter_isnew.first );
4313 else // new in set == encountered for the first time: add
4315 const SMDS_MeshNode* n1 = nodes[ iNode ];
4316 const SMDS_MeshNode* n2 = nodes[ iNode + 1];
4317 linkList[0].push_back ( TPairOfNodes( n1, n2 ));
4318 linkList[1].push_back ( TPairOfNodes( nReplaceMap[n1], nReplaceMap[n2] ));
4322 } // loop on link lists
4324 if ( aResult == SEW_OK &&
4325 ( linkIt[0] != linkList[0].end() ||
4326 !faceSetPtr[0]->empty() || !faceSetPtr[1]->empty() )) {
4327 MESSAGE( (linkIt[0] != linkList[0].end()) <<" "<< (faceSetPtr[0]->empty()) <<
4328 " " << (faceSetPtr[1]->empty()));
4329 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
4332 // ====================================================================
4333 // 3. Replace nodes in elements of the side 1 and remove replaced nodes
4334 // ====================================================================
4336 // delete temporary faces: they are in reverseElements of actual nodes
4337 SMDS_FaceIteratorPtr tmpFaceIt = aTmpFacesMesh.facesIterator();
4338 while ( tmpFaceIt->more() )
4339 aTmpFacesMesh.RemoveElement( tmpFaceIt->next() );
4341 if ( aResult != SEW_OK)
4344 list< int > nodeIDsToRemove/*, elemIDsToRemove*/;
4345 // loop on nodes replacement map
4346 TNodeNodeMap::iterator nReplaceMapIt = nReplaceMap.begin(), nnIt;
4347 for ( ; nReplaceMapIt != nReplaceMap.end(); nReplaceMapIt++ )
4348 if ( (*nReplaceMapIt).first != (*nReplaceMapIt).second )
4350 const SMDS_MeshNode* nToRemove = (*nReplaceMapIt).first;
4351 nodeIDsToRemove.push_back( nToRemove->GetID() );
4352 // loop on elements sharing nToRemove
4353 SMDS_ElemIteratorPtr invElemIt = nToRemove->GetInverseElementIterator();
4354 while ( invElemIt->more() ) {
4355 const SMDS_MeshElement* e = invElemIt->next();
4356 // get a new suite of nodes: make replacement
4357 int nbReplaced = 0, i = 0, nbNodes = e->NbNodes();
4358 const SMDS_MeshNode* nodes[ 8 ];
4359 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
4360 while ( nIt->more() ) {
4361 const SMDS_MeshNode* n =
4362 static_cast<const SMDS_MeshNode*>( nIt->next() );
4363 nnIt = nReplaceMap.find( n );
4364 if ( nnIt != nReplaceMap.end() ) {
4370 // if ( nbReplaced == nbNodes && e->GetType() == SMDSAbs_Face )
4371 // elemIDsToRemove.push_back( e->GetID() );
4374 aMesh->ChangeElementNodes( e, nodes, nbNodes );
4378 Remove( nodeIDsToRemove, true );