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 "SMESHDS_Group.hxx"
34 #include "SMESHDS_Mesh.hxx"
35 #include "SMESH_subMesh.hxx"
36 #include "SMESH_ControlsDef.hxx"
38 #include "utilities.h"
40 #include <TopTools_ListIteratorOfListOfShape.hxx>
41 #include <TopTools_ListOfShape.hxx>
45 #include <gp_Trsf.hxx>
50 #include <BRep_Tool.hxx>
51 #include <SMDS_EdgePosition.hxx>
52 #include <Geom_Curve.hxx>
59 typedef map<const SMDS_MeshNode*, const SMDS_MeshNode*> TNodeNodeMap;
60 typedef map<const SMDS_MeshElement*, list<const SMDS_MeshNode*> > TElemOfNodeListMap;
61 typedef map<const SMDS_MeshElement*, list<const SMDS_MeshElement*> > TElemOfElemListMap;
62 typedef map<const SMDS_MeshNode*, list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
63 typedef TNodeOfNodeListMap::iterator TNodeOfNodeListMapItr;
64 typedef map<const SMDS_MeshElement*, vector<TNodeOfNodeListMapItr> > TElemOfVecOfNnlmiMap;
66 //=======================================================================
67 //function : SMESH_MeshEditor
69 //=======================================================================
71 SMESH_MeshEditor::SMESH_MeshEditor( SMESH_Mesh* theMesh ):
76 //=======================================================================
78 //purpose : Remove a node or an element.
79 // Modify a compute state of sub-meshes which become empty
80 //=======================================================================
82 bool SMESH_MeshEditor::Remove (const list< int >& theIDs,
86 SMESHDS_Mesh* aMesh = GetMeshDS();
87 set< SMESH_subMesh *> smmap;
89 list<int>::const_iterator it = theIDs.begin();
90 for ( ; it != theIDs.end(); it++ )
92 const SMDS_MeshElement * elem;
94 elem = aMesh->FindNode( *it );
96 elem = aMesh->FindElement( *it );
100 // Find sub-meshes to notify about modification
101 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
102 while ( nodeIt->more() )
104 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
105 const SMDS_PositionPtr& aPosition = node->GetPosition();
106 if ( aPosition.get() ) {
107 int aShapeID = aPosition->GetShapeId();
109 TopoDS_Shape aShape = aMesh->IndexToShape( aShapeID );
110 SMESH_subMesh * sm = GetMesh()->GetSubMeshContaining( aShape );
119 aMesh->RemoveNode( static_cast< const SMDS_MeshNode* >( elem ));
121 aMesh->RemoveElement( elem );
124 // Notify sub-meshes about modification
125 if ( !smmap.empty() ) {
126 set< SMESH_subMesh *>::iterator smIt;
127 for ( smIt = smmap.begin(); smIt != smmap.end(); smIt++ )
128 (*smIt)->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
133 //=======================================================================
134 //function : FindShape
135 //purpose : Return an index of the shape theElem is on
136 // or zero if a shape not found
137 //=======================================================================
139 int SMESH_MeshEditor::FindShape (const SMDS_MeshElement * theElem)
141 SMESHDS_Mesh * aMesh = GetMeshDS();
142 if ( aMesh->ShapeToMesh().IsNull() )
145 if ( theElem->GetType() == SMDSAbs_Node )
147 const SMDS_PositionPtr& aPosition =
148 static_cast<const SMDS_MeshNode*>( theElem )->GetPosition();
149 if ( aPosition.get() )
150 return aPosition->GetShapeId();
155 TopoDS_Shape aShape; // the shape a node is on
156 SMDS_ElemIteratorPtr nodeIt = theElem->nodesIterator();
157 while ( nodeIt->more() )
159 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
160 const SMDS_PositionPtr& aPosition = node->GetPosition();
161 if ( aPosition.get() ) {
162 int aShapeID = aPosition->GetShapeId();
163 SMESHDS_SubMesh * sm = aMesh->MeshElements( aShapeID );
166 if ( sm->Contains( theElem ))
168 if ( aShape.IsNull() )
169 aShape = aMesh->IndexToShape( aShapeID );
173 //MESSAGE ( "::FindShape() No SubShape for aShapeID " << aShapeID );
178 // None of nodes is on a proper shape,
179 // find the shape among ancestors of aShape on which a node is
180 if ( aShape.IsNull() ) {
181 //MESSAGE ("::FindShape() - NONE node is on shape")
184 TopTools_ListIteratorOfListOfShape ancIt( GetMesh()->GetAncestors( aShape ));
185 for ( ; ancIt.More(); ancIt.Next() )
187 SMESHDS_SubMesh * sm = aMesh->MeshElements( ancIt.Value() );
188 if ( sm && sm->Contains( theElem ))
189 return aMesh->ShapeToIndex( ancIt.Value() );
192 //MESSAGE ("::FindShape() - SHAPE NOT FOUND")
196 //=======================================================================
197 //function : InverseDiag
198 //purpose : Replace two neighbour triangles with ones built on the same 4 nodes
199 // but having other common link.
200 // Return False if args are improper
201 //=======================================================================
203 bool SMESH_MeshEditor::InverseDiag (const SMDS_MeshElement * theTria1,
204 const SMDS_MeshElement * theTria2 )
206 if (!theTria1 || !theTria2)
208 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( theTria1 );
209 if (!F1) return false;
210 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( theTria2 );
211 if (!F2) return false;
213 // 1 +--+ A theTria1: ( 1 A B ) A->2 ( 1 2 B ) 1 +--+ A
214 // | /| theTria2: ( B A 2 ) B->1 ( 1 A 2 ) |\ |
218 // put nodes in array and find out indices of the same ones
219 const SMDS_MeshNode* aNodes [6];
220 int sameInd [] = { 0, 0, 0, 0, 0, 0 };
222 SMDS_ElemIteratorPtr it = theTria1->nodesIterator();
225 aNodes[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
227 if ( i > 2 ) // theTria2
228 // find same node of theTria1
229 for ( int j = 0; j < 3; j++ )
230 if ( aNodes[ i ] == aNodes[ j ]) {
239 return false; // theTria1 is not a triangle
240 it = theTria2->nodesIterator();
242 if ( i == 6 && it->more() )
243 return false; // theTria2 is not a triangle
246 // find indices of 1,2 and of A,B in theTria1
247 int iA = 0, iB = 0, i1 = 0, i2 = 0;
248 for ( i = 0; i < 6; i++ )
250 if ( sameInd [ i ] == 0 )
257 // nodes 1 and 2 should not be the same
258 if ( aNodes[ i1 ] == aNodes[ i2 ] )
263 aNodes[ iA ] = aNodes[ i2 ];
265 aNodes[ sameInd[ iB ]] = aNodes[ i1 ];
267 //MESSAGE( theTria1 << theTria2 );
269 GetMeshDS()->ChangeElementNodes( theTria1, aNodes, 3 );
270 GetMeshDS()->ChangeElementNodes( theTria2, &aNodes[ 3 ], 3 );
272 //MESSAGE( theTria1 << theTria2 );
277 //=======================================================================
278 //function : findTriangles
279 //purpose : find triangles sharing theNode1-theNode2 link
280 //=======================================================================
282 static bool findTriangles(const SMDS_MeshNode * theNode1,
283 const SMDS_MeshNode * theNode2,
284 const SMDS_MeshElement*& theTria1,
285 const SMDS_MeshElement*& theTria2)
287 if ( !theNode1 || !theNode2 ) return false;
289 theTria1 = theTria2 = 0;
291 set< const SMDS_MeshElement* > emap;
292 SMDS_ElemIteratorPtr it = theNode1->GetInverseElementIterator();
294 const SMDS_MeshElement* elem = it->next();
295 if ( elem->GetType() == SMDSAbs_Face && elem->NbNodes() == 3 )
298 it = theNode2->GetInverseElementIterator();
300 const SMDS_MeshElement* elem = it->next();
301 if ( elem->GetType() == SMDSAbs_Face &&
302 emap.find( elem ) != emap.end() )
310 return ( theTria1 && theTria2 );
313 //=======================================================================
314 //function : InverseDiag
315 //purpose : Replace two neighbour triangles sharing theNode1-theNode2 link
316 // with ones built on the same 4 nodes but having other common link.
317 // Return false if proper faces not found
318 //=======================================================================
320 bool SMESH_MeshEditor::InverseDiag (const SMDS_MeshNode * theNode1,
321 const SMDS_MeshNode * theNode2)
323 MESSAGE( "::InverseDiag()" );
325 const SMDS_MeshElement *tr1, *tr2;
326 if ( !findTriangles( theNode1, theNode2, tr1, tr2 ))
329 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( tr1 );
330 if (!F1) return false;
331 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( tr2 );
332 if (!F2) return false;
334 // 1 +--+ A tr1: ( 1 A B ) A->2 ( 1 2 B ) 1 +--+ A
335 // | /| tr2: ( B A 2 ) B->1 ( 1 A 2 ) |\ |
339 // put nodes in array
340 // and find indices of 1,2 and of A in tr1 and of B in tr2
341 int i, iA1 = 0, i1 = 0;
342 const SMDS_MeshNode* aNodes1 [3];
343 SMDS_ElemIteratorPtr it;
344 for (i = 0, it = tr1->nodesIterator(); it->more(); i++ ) {
345 aNodes1[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
346 if ( aNodes1[ i ] == theNode1 )
347 iA1 = i; // node A in tr1
348 else if ( aNodes1[ i ] != theNode2 )
352 const SMDS_MeshNode* aNodes2 [3];
353 for (i = 0, it = tr2->nodesIterator(); it->more(); i++ ) {
354 aNodes2[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
355 if ( aNodes2[ i ] == theNode2 )
356 iB2 = i; // node B in tr2
357 else if ( aNodes2[ i ] != theNode1 )
361 // nodes 1 and 2 should not be the same
362 if ( aNodes1[ i1 ] == aNodes2[ i2 ] )
366 aNodes1[ iA1 ] = aNodes2[ i2 ];
368 aNodes2[ iB2 ] = aNodes1[ i1 ];
370 //MESSAGE( tr1 << tr2 );
372 GetMeshDS()->ChangeElementNodes( tr1, aNodes1, 3 );
373 GetMeshDS()->ChangeElementNodes( tr2, aNodes2, 3 );
375 //MESSAGE( tr1 << tr2 );
381 //=======================================================================
382 //function : getQuadrangleNodes
383 //purpose : fill theQuadNodes - nodes of a quadrangle resulting from
384 // fusion of triangles tr1 and tr2 having shared link on
385 // theNode1 and theNode2
386 //=======================================================================
388 bool getQuadrangleNodes(const SMDS_MeshNode * theQuadNodes [],
389 const SMDS_MeshNode * theNode1,
390 const SMDS_MeshNode * theNode2,
391 const SMDS_MeshElement * tr1,
392 const SMDS_MeshElement * tr2 )
394 // find the 4-th node to insert into tr1
395 const SMDS_MeshNode* n4 = 0;
396 SMDS_ElemIteratorPtr it = tr2->nodesIterator();
397 while ( !n4 && it->more() )
399 const SMDS_MeshNode * n = static_cast<const SMDS_MeshNode*>( it->next() );
400 bool isDiag = ( n == theNode1 || n == theNode2 );
404 // Make an array of nodes to be in a quadrangle
405 int iNode = 0, iFirstDiag = -1;
406 it = tr1->nodesIterator();
409 const SMDS_MeshNode * n = static_cast<const SMDS_MeshNode*>( it->next() );
410 bool isDiag = ( n == theNode1 || n == theNode2 );
413 if ( iFirstDiag < 0 )
415 else if ( iNode - iFirstDiag == 1 )
416 theQuadNodes[ iNode++ ] = n4; // insert the 4-th node between diagonal nodes
420 return false; // tr1 and tr2 should not have all the same nodes
422 theQuadNodes[ iNode++ ] = n;
424 if ( iNode == 3 ) // diagonal nodes have 0 and 2 indices
425 theQuadNodes[ iNode ] = n4;
430 //=======================================================================
431 //function : DeleteDiag
432 //purpose : Replace two neighbour triangles sharing theNode1-theNode2 link
433 // with a quadrangle built on the same 4 nodes.
434 // Return false if proper faces not found
435 //=======================================================================
437 bool SMESH_MeshEditor::DeleteDiag (const SMDS_MeshNode * theNode1,
438 const SMDS_MeshNode * theNode2)
440 MESSAGE( "::DeleteDiag()" );
442 const SMDS_MeshElement *tr1, *tr2;
443 if ( !findTriangles( theNode1, theNode2, tr1, tr2 ))
446 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( tr1 );
447 if (!F1) return false;
448 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( tr2 );
449 if (!F2) return false;
451 const SMDS_MeshNode* aNodes [ 4 ];
452 if ( ! getQuadrangleNodes( aNodes, theNode1, theNode2, tr1, tr2 ))
455 //MESSAGE( endl << tr1 << tr2 );
457 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 4 );
458 GetMeshDS()->RemoveElement( tr2 );
460 //MESSAGE( endl << tr1 );
465 //=======================================================================
466 //function : Reorient
467 //purpose : Reverse the normal of theFace
468 // Return false if theFace is null
469 //=======================================================================
471 bool SMESH_MeshEditor::Reorient (const SMDS_MeshElement * theFace)
473 if (!theFace) return false;
474 const SMDS_FaceOfNodes* F = dynamic_cast<const SMDS_FaceOfNodes*>( theFace );
475 if (!F) return false;
477 const SMDS_MeshNode* aNodes [4], *tmpNode;
479 SMDS_ElemIteratorPtr it = theFace->nodesIterator();
481 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( it->next() );
483 // exchange nodes with indeces 0 and 2
484 tmpNode = aNodes[ 0 ];
485 aNodes[ 0 ] = aNodes[ 2 ];
486 aNodes[ 2 ] = tmpNode;
488 //MESSAGE( theFace );
490 GetMeshDS()->ChangeElementNodes( theFace, aNodes, theFace->NbNodes() );
492 //MESSAGE( theFace );
497 //=======================================================================
498 //function : getBadRate
500 //=======================================================================
502 static double getBadRate (const SMDS_MeshElement* theElem,
503 SMESH::Controls::NumericalFunctorPtr& theCrit)
505 SMESH::Controls::TSequenceOfXYZ P;
506 if ( !theElem || !theCrit->GetPoints( theElem, P ))
508 return theCrit->GetBadRate( theCrit->GetValue( P ), theElem->NbNodes() );
511 //=======================================================================
512 //function : QuadToTri
513 //purpose : Cut quadrangles into triangles.
514 // theCrit is used to select a diagonal to cut
515 //=======================================================================
517 bool SMESH_MeshEditor::QuadToTri (set<const SMDS_MeshElement*> & theElems,
518 SMESH::Controls::NumericalFunctorPtr theCrit)
520 MESSAGE( "::QuadToTri()" );
522 if ( !theCrit.get() )
525 SMESHDS_Mesh * aMesh = GetMeshDS();
527 set< const SMDS_MeshElement * >::iterator itElem;
528 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
530 const SMDS_MeshElement* elem = (*itElem);
531 if ( !elem || elem->GetType() != SMDSAbs_Face || elem->NbNodes() != 4 )
534 // retrieve element nodes
535 const SMDS_MeshNode* aNodes [4];
536 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
538 while ( itN->more() )
539 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
541 // compare two sets of possible triangles
542 double aBadRate1, aBadRate2; // to what extent a set is bad
543 SMDS_FaceOfNodes tr1 ( aNodes[0], aNodes[1], aNodes[2] );
544 SMDS_FaceOfNodes tr2 ( aNodes[2], aNodes[3], aNodes[0] );
545 aBadRate1 = getBadRate( &tr1, theCrit ) + getBadRate( &tr2, theCrit );
547 SMDS_FaceOfNodes tr3 ( aNodes[1], aNodes[2], aNodes[3] );
548 SMDS_FaceOfNodes tr4 ( aNodes[3], aNodes[0], aNodes[1] );
549 aBadRate2 = getBadRate( &tr3, theCrit ) + getBadRate( &tr4, theCrit );
551 int aShapeId = FindShape( elem );
552 //MESSAGE( "aBadRate1 = " << aBadRate1 << "; aBadRate2 = " << aBadRate2
553 // << " ShapeID = " << aShapeId << endl << elem );
555 if ( aBadRate1 <= aBadRate2 ) {
556 // tr1 + tr2 is better
557 aMesh->ChangeElementNodes( elem, aNodes, 3 );
558 //MESSAGE( endl << elem );
560 elem = aMesh->AddFace( aNodes[2], aNodes[3], aNodes[0] );
563 // tr3 + tr4 is better
564 aMesh->ChangeElementNodes( elem, &aNodes[1], 3 );
565 //MESSAGE( endl << elem );
567 elem = aMesh->AddFace( aNodes[3], aNodes[0], aNodes[1] );
569 //MESSAGE( endl << elem );
571 // put a new triangle on the same shape
573 aMesh->SetMeshElementOnShape( elem, aShapeId );
579 //=======================================================================
580 //function : AddToSameGroups
581 //purpose : add elemToAdd to the groups the elemInGroups belongs to
582 //=======================================================================
584 void SMESH_MeshEditor::AddToSameGroups (const SMDS_MeshElement* elemToAdd,
585 const SMDS_MeshElement* elemInGroups,
586 SMESHDS_Mesh * aMesh)
588 const set<SMESHDS_GroupBase*>& groups = aMesh->GetGroups();
589 set<SMESHDS_GroupBase*>::const_iterator grIt = groups.begin();
590 for ( ; grIt != groups.end(); grIt++ ) {
591 SMESHDS_Group* group = dynamic_cast<SMESHDS_Group*>( *grIt );
592 if ( group && group->SMDSGroup().Contains( elemInGroups ))
593 group->SMDSGroup().Add( elemToAdd );
597 //=======================================================================
598 //function : QuadToTri
599 //purpose : Cut quadrangles into triangles.
600 // theCrit is used to select a diagonal to cut
601 //=======================================================================
603 bool SMESH_MeshEditor::QuadToTri (std::set<const SMDS_MeshElement*> & theElems,
604 const bool the13Diag)
606 MESSAGE( "::QuadToTri()" );
608 SMESHDS_Mesh * aMesh = GetMeshDS();
610 set< const SMDS_MeshElement * >::iterator itElem;
611 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
613 const SMDS_MeshElement* elem = (*itElem);
614 if ( !elem || elem->GetType() != SMDSAbs_Face || elem->NbNodes() != 4 )
617 // retrieve element nodes
618 const SMDS_MeshNode* aNodes [4];
619 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
621 while ( itN->more() )
622 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
624 int aShapeId = FindShape( elem );
625 const SMDS_MeshElement* newElem = 0;
628 aMesh->ChangeElementNodes( elem, aNodes, 3 );
629 newElem = aMesh->AddFace( aNodes[2], aNodes[3], aNodes[0] );
633 aMesh->ChangeElementNodes( elem, &aNodes[1], 3 );
634 newElem = aMesh->AddFace( aNodes[3], aNodes[0], aNodes[1] );
637 // put a new triangle on the same shape and add to the same groups
640 aMesh->SetMeshElementOnShape( newElem, aShapeId );
642 AddToSameGroups( newElem, elem, aMesh );
648 //=======================================================================
649 //function : getAngle
651 //=======================================================================
653 double getAngle(const SMDS_MeshElement * tr1,
654 const SMDS_MeshElement * tr2,
655 const SMDS_MeshNode * n1,
656 const SMDS_MeshNode * n2)
658 double angle = 2*PI; // bad angle
661 SMESH::Controls::TSequenceOfXYZ P1, P2;
662 if ( !SMESH::Controls::NumericalFunctor::GetPoints( tr1, P1 ) ||
663 !SMESH::Controls::NumericalFunctor::GetPoints( tr2, P2 ))
665 gp_Vec N1 = gp_Vec( P1(2) - P1(1) ) ^ gp_Vec( P1(3) - P1(1) );
666 if ( N1.SquareMagnitude() <= gp::Resolution() )
668 gp_Vec N2 = gp_Vec( P2(2) - P2(1) ) ^ gp_Vec( P2(3) - P2(1) );
669 if ( N2.SquareMagnitude() <= gp::Resolution() )
672 // find the first diagonal node n1 in the triangles:
673 // take in account a diagonal link orientation
674 const SMDS_MeshElement *nFirst[2], *tr[] = { tr1, tr2 };
675 for ( int t = 0; t < 2; t++ )
677 SMDS_ElemIteratorPtr it = tr[ t ]->nodesIterator();
678 int i = 0, iDiag = -1;
679 while ( it->more()) {
680 const SMDS_MeshElement *n = it->next();
681 if ( n == n1 || n == n2 )
685 if ( i - iDiag == 1 )
686 nFirst[ t ] = ( n == n1 ? n2 : n1 );
694 if ( nFirst[ 0 ] == nFirst[ 1 ] )
697 angle = N1.Angle( N2 );
702 // =================================================
703 // class generating a unique ID for a pair of nodes
704 // and able to return nodes by that ID
705 // =================================================
710 LinkID_Gen( const SMESHDS_Mesh* theMesh )
711 :myMesh( theMesh ), myMaxID( theMesh->MaxNodeID() + 1)
714 long GetLinkID (const SMDS_MeshNode * n1,
715 const SMDS_MeshNode * n2) const
717 return ( Min(n1->GetID(),n2->GetID()) * myMaxID + Max(n1->GetID(),n2->GetID()));
720 bool GetNodes (const long theLinkID,
721 const SMDS_MeshNode* & theNode1,
722 const SMDS_MeshNode* & theNode2) const
724 theNode1 = myMesh->FindNode( theLinkID / myMaxID );
725 if ( !theNode1 ) return false;
726 theNode2 = myMesh->FindNode( theLinkID % myMaxID );
727 if ( !theNode2 ) return false;
733 const SMESHDS_Mesh* myMesh;
737 //=======================================================================
738 //function : TriToQuad
739 //purpose : Fuse neighbour triangles into quadrangles.
740 // theCrit is used to select a neighbour to fuse with.
741 // theMaxAngle is a max angle between element normals at which
742 // fusion is still performed.
743 //=======================================================================
745 bool SMESH_MeshEditor::TriToQuad (set<const SMDS_MeshElement*> & theElems,
746 SMESH::Controls::NumericalFunctorPtr theCrit,
747 const double theMaxAngle)
749 MESSAGE( "::TriToQuad()" );
751 if ( !theCrit.get() )
754 SMESHDS_Mesh * aMesh = GetMeshDS();
755 LinkID_Gen aLinkID_Gen( aMesh );
758 // Prepare data for algo: build
759 // 1. map of elements with their linkIDs
760 // 2. map of linkIDs with their elements
762 map< long, list< const SMDS_MeshElement* > > mapLi_listEl;
763 map< long, list< const SMDS_MeshElement* > >::iterator itLE;
764 map< const SMDS_MeshElement*, set< long > > mapEl_setLi;
765 map< const SMDS_MeshElement*, set< long > >::iterator itEL;
767 set<const SMDS_MeshElement*>::iterator itElem;
768 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
770 const SMDS_MeshElement* elem = (*itElem);
771 if ( !elem || elem->NbNodes() != 3 )
774 // retrieve element nodes
775 const SMDS_MeshNode* aNodes [4];
776 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
778 while ( itN->more() )
779 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
781 aNodes[ 3 ] = aNodes[ 0 ];
784 for ( i = 0; i < 3; i++ )
786 long linkID = aLinkID_Gen.GetLinkID( aNodes[ i ], aNodes[ i+1 ] );
787 // check if elements sharing a link can be fused
788 itLE = mapLi_listEl.find( linkID );
789 if ( itLE != mapLi_listEl.end() )
791 if ((*itLE).second.size() > 1 ) // consider only 2 elems adjacent by a link
793 const SMDS_MeshElement* elem2 = (*itLE).second.front();
794 // if ( FindShape( elem ) != FindShape( elem2 ))
795 // continue; // do not fuse triangles laying on different shapes
796 if ( getAngle( elem, elem2, aNodes[i], aNodes[i+1] ) > theMaxAngle )
797 continue; // avoid making badly shaped quads
798 (*itLE).second.push_back( elem );
801 mapLi_listEl[ linkID ].push_back( elem );
802 mapEl_setLi [ elem ].insert( linkID );
805 // Clean the maps from the links shared by a sole element, ie
806 // links to which only one element is bound in mapLi_listEl
808 for ( itLE = mapLi_listEl.begin(); itLE != mapLi_listEl.end(); itLE++ )
810 int nbElems = (*itLE).second.size();
812 const SMDS_MeshElement* elem = (*itLE).second.front();
813 long link = (*itLE).first;
814 mapEl_setLi[ elem ].erase( link );
815 if ( mapEl_setLi[ elem ].empty() )
816 mapEl_setLi.erase( elem );
820 // Algo: fuse triangles into quadrangles
822 while ( ! mapEl_setLi.empty() )
824 // Look for the start element:
825 // the element having the least nb of shared links
827 const SMDS_MeshElement* startElem = 0;
829 for ( itEL = mapEl_setLi.begin(); itEL != mapEl_setLi.end(); itEL++ )
831 int nbLinks = (*itEL).second.size();
832 if ( nbLinks < minNbLinks )
834 startElem = (*itEL).first;
835 minNbLinks = nbLinks;
836 if ( minNbLinks == 1 )
841 // search elements to fuse starting from startElem or links of elements
842 // fused earlyer - startLinks
843 list< long > startLinks;
844 while ( startElem || !startLinks.empty() )
846 while ( !startElem && !startLinks.empty() )
848 // Get an element to start, by a link
849 long linkId = startLinks.front();
850 startLinks.pop_front();
851 itLE = mapLi_listEl.find( linkId );
852 if ( itLE != mapLi_listEl.end() )
854 list< const SMDS_MeshElement* > & listElem = (*itLE).second;
855 list< const SMDS_MeshElement* >::iterator itE = listElem.begin();
856 for ( ; itE != listElem.end() ; itE++ )
857 if ( mapEl_setLi.find( (*itE) ) != mapEl_setLi.end() )
859 mapLi_listEl.erase( itLE );
865 // Get candidates to be fused
867 const SMDS_MeshElement *tr1 = startElem, *tr2 = 0, *tr3 = 0;
870 ASSERT( mapEl_setLi.find( tr1 ) != mapEl_setLi.end() );
871 set< long >& setLi = mapEl_setLi[ tr1 ];
872 ASSERT( !setLi.empty() );
873 set< long >::iterator itLi;
874 for ( itLi = setLi.begin(); itLi != setLi.end(); itLi++ )
876 long linkID = (*itLi);
877 itLE = mapLi_listEl.find( linkID );
878 if ( itLE == mapLi_listEl.end() )
880 const SMDS_MeshElement* elem = (*itLE).second.front();
882 elem = (*itLE).second.back();
883 mapLi_listEl.erase( itLE );
884 if ( mapEl_setLi.find( elem ) == mapEl_setLi.end())
897 // add other links of elem to list of links to re-start from
898 set< long >& links = mapEl_setLi[ elem ];
899 set< long >::iterator it;
900 for ( it = links.begin(); it != links.end(); it++ )
902 long linkID2 = (*it);
903 if ( linkID2 != linkID )
904 startLinks.push_back( linkID2 );
908 // Get nodes of possible quadrangles
910 const SMDS_MeshNode *n12 [4], *n13 [4];
911 bool Ok12 = false, Ok13 = false;
912 const SMDS_MeshNode *linkNode1, *linkNode2;
914 aLinkID_Gen.GetNodes( link12, linkNode1, linkNode2 ) &&
915 getQuadrangleNodes( n12, linkNode1, linkNode2, tr1, tr2 ))
918 aLinkID_Gen.GetNodes( link13, linkNode1, linkNode2 ) &&
919 getQuadrangleNodes( n13, linkNode1, linkNode2, tr1, tr3 ))
922 // Choose a pair to fuse
926 SMDS_FaceOfNodes quad12 ( n12[ 0 ], n12[ 1 ], n12[ 2 ], n12[ 3 ] );
927 SMDS_FaceOfNodes quad13 ( n13[ 0 ], n13[ 1 ], n13[ 2 ], n13[ 3 ] );
928 double aBadRate12 = getBadRate( &quad12, theCrit );
929 double aBadRate13 = getBadRate( &quad13, theCrit );
930 if ( aBadRate13 < aBadRate12 )
938 // and remove fused elems and removed links from the maps
940 mapEl_setLi.erase( tr1 );
943 mapEl_setLi.erase( tr2 );
944 mapLi_listEl.erase( link12 );
945 aMesh->ChangeElementNodes( tr1, n12, 4 );
946 aMesh->RemoveElement( tr2 );
950 mapEl_setLi.erase( tr3 );
951 mapLi_listEl.erase( link13 );
952 aMesh->ChangeElementNodes( tr1, n13, 4 );
953 aMesh->RemoveElement( tr3 );
956 // Next element to fuse: the rejected one
958 startElem = Ok12 ? tr3 : tr2;
960 } // if ( startElem )
961 } // while ( startElem || !startLinks.empty() )
962 } // while ( ! mapEl_setLi.empty() )
968 #define DUMPSO(txt) \
969 // cout << txt << endl;
970 //=============================================================================
974 //=============================================================================
975 static void swap( int i1, int i2, int idNodes[], gp_Pnt P[] )
979 int tmp = idNodes[ i1 ];
980 idNodes[ i1 ] = idNodes[ i2 ];
982 gp_Pnt Ptmp = P[ i1 ];
985 DUMPSO( i1 << "(" << idNodes[ i2 ] << ") <-> " << i2 << "(" << idNodes[ i1 ] << ")");
988 //=======================================================================
989 //function : SortQuadNodes
990 //purpose : Set 4 nodes of a quadrangle face in a good order.
991 // Swap 1<->2 or 2<->3 nodes and correspondingly return
993 //=======================================================================
995 int SMESH_MeshEditor::SortQuadNodes (const SMDS_Mesh * theMesh,
1000 for ( i = 0; i < 4; i++ ) {
1001 const SMDS_MeshNode *n = theMesh->FindNode( idNodes[i] );
1003 P[ i ].SetCoord( n->X(), n->Y(), n->Z() );
1006 gp_Vec V1(P[0], P[1]);
1007 gp_Vec V2(P[0], P[2]);
1008 gp_Vec V3(P[0], P[3]);
1010 gp_Vec Cross1 = V1 ^ V2;
1011 gp_Vec Cross2 = V2 ^ V3;
1014 if (Cross1.Dot(Cross2) < 0)
1019 if (Cross1.Dot(Cross2) < 0)
1023 swap ( i, i + 1, idNodes, P );
1025 // for ( int ii = 0; ii < 4; ii++ ) {
1026 // const SMDS_MeshNode *n = theMesh->FindNode( idNodes[ii] );
1027 // DUMPSO( ii << "(" << idNodes[ii] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1033 //=======================================================================
1034 //function : SortHexaNodes
1035 //purpose : Set 8 nodes of a hexahedron in a good order.
1036 // Return success status
1037 //=======================================================================
1039 bool SMESH_MeshEditor::SortHexaNodes (const SMDS_Mesh * theMesh,
1044 DUMPSO( "INPUT: ========================================");
1045 for ( i = 0; i < 8; i++ ) {
1046 const SMDS_MeshNode *n = theMesh->FindNode( idNodes[i] );
1047 if ( !n ) return false;
1048 P[ i ].SetCoord( n->X(), n->Y(), n->Z() );
1049 DUMPSO( i << "(" << idNodes[i] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1051 DUMPSO( "========================================");
1054 set<int> faceNodes; // ids of bottom face nodes, to be found
1055 set<int> checkedId1; // ids of tried 2-nd nodes
1056 Standard_Real leastDist = DBL_MAX; // dist of the 4-th node from 123 plane
1057 const Standard_Real tol = 1.e-6; // tolerance to find nodes in plane
1058 int iMin, iLoop1 = 0;
1060 // Loop to try the 2-nd nodes
1062 while ( leastDist > DBL_MIN && ++iLoop1 < 8 )
1064 // Find not checked 2-nd node
1065 for ( i = 1; i < 8; i++ )
1066 if ( checkedId1.find( idNodes[i] ) == checkedId1.end() ) {
1067 int id1 = idNodes[i];
1068 swap ( 1, i, idNodes, P );
1069 checkedId1.insert ( id1 );
1073 // Find the 3-d node so that 1-2-3 triangle to be on a hexa face,
1074 // ie that all but meybe one (id3 which is on the same face) nodes
1075 // lay on the same side from the triangle plane.
1077 bool manyInPlane = false; // more than 4 nodes lay in plane
1079 while ( ++iLoop2 < 6 ) {
1081 // get 1-2-3 plane coeffs
1082 Standard_Real A, B, C, D;
1083 gp_Vec N = gp_Vec (P[0], P[1]).Crossed( gp_Vec (P[0], P[2]) );
1084 if ( N.SquareMagnitude() > gp::Resolution() )
1086 gp_Pln pln ( P[0], N );
1087 pln.Coefficients( A, B, C, D );
1089 // find the node (iMin) closest to pln
1090 Standard_Real dist[ 8 ], minDist = DBL_MAX;
1092 for ( i = 3; i < 8; i++ ) {
1093 dist[i] = A * P[i].X() + B * P[i].Y() + C * P[i].Z() + D;
1094 if ( fabs( dist[i] ) < minDist ) {
1095 minDist = fabs( dist[i] );
1098 if ( fabs( dist[i] ) <= tol )
1099 idInPln.insert( idNodes[i] );
1102 // there should not be more than 4 nodes in bottom plane
1103 if ( idInPln.size() > 1 )
1105 DUMPSO( "### idInPln.size() = " << idInPln.size());
1106 // idInPlane does not contain the first 3 nodes
1107 if ( manyInPlane || idInPln.size() == 5)
1108 return false; // all nodes in one plane
1111 // set the 1-st node to be not in plane
1112 for ( i = 3; i < 8; i++ ) {
1113 if ( idInPln.find( idNodes[ i ] ) == idInPln.end() ) {
1114 DUMPSO( "### Reset 0-th node");
1115 swap( 0, i, idNodes, P );
1120 // reset to re-check second nodes
1121 leastDist = DBL_MAX;
1125 break; // from iLoop2;
1128 // check that the other 4 nodes are on the same side
1129 bool sameSide = true;
1130 bool isNeg = dist[ iMin == 3 ? 4 : 3 ] <= 0.;
1131 for ( i = 3; sameSide && i < 8; i++ ) {
1133 sameSide = ( isNeg == dist[i] <= 0.);
1136 // keep best solution
1137 if ( sameSide && minDist < leastDist ) {
1138 leastDist = minDist;
1140 faceNodes.insert( idNodes[ 1 ] );
1141 faceNodes.insert( idNodes[ 2 ] );
1142 faceNodes.insert( idNodes[ iMin ] );
1143 DUMPSO( "loop " << iLoop2 << " id2 " << idNodes[ 1 ] << " id3 " << idNodes[ 2 ]
1144 << " leastDist = " << leastDist);
1145 if ( leastDist <= DBL_MIN )
1150 // set next 3-d node to check
1151 int iNext = 2 + iLoop2;
1153 DUMPSO( "Try 2-nd");
1154 swap ( 2, iNext, idNodes, P );
1156 } // while ( iLoop2 < 6 )
1159 if ( faceNodes.empty() ) return false;
1161 // Put the faceNodes in proper places
1162 for ( i = 4; i < 8; i++ ) {
1163 if ( faceNodes.find( idNodes[ i ] ) != faceNodes.end() ) {
1164 // find a place to put
1166 while ( faceNodes.find( idNodes[ iTo ] ) != faceNodes.end() )
1168 DUMPSO( "Set faceNodes");
1169 swap ( iTo, i, idNodes, P );
1174 // Set nodes of the found bottom face in good order
1175 DUMPSO( " Found bottom face: ");
1176 i = SortQuadNodes( theMesh, idNodes );
1178 gp_Pnt Ptmp = P[ i ];
1183 // for ( int ii = 0; ii < 4; ii++ ) {
1184 // const SMDS_MeshNode *n = theMesh->FindNode( idNodes[ii] );
1185 // DUMPSO( ii << "(" << idNodes[ii] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1188 // Gravity center of the top and bottom faces
1189 gp_Pnt aGCb = ( P[0].XYZ() + P[1].XYZ() + P[2].XYZ() + P[3].XYZ() ) / 4.;
1190 gp_Pnt aGCt = ( P[4].XYZ() + P[5].XYZ() + P[6].XYZ() + P[7].XYZ() ) / 4.;
1192 // Get direction from the bottom to the top face
1193 gp_Vec upDir ( aGCb, aGCt );
1194 Standard_Real upDirSize = upDir.Magnitude();
1195 if ( upDirSize <= gp::Resolution() ) return false;
1198 // Assure that the bottom face normal points up
1199 gp_Vec Nb = gp_Vec (P[0], P[1]).Crossed( gp_Vec (P[0], P[2]) );
1200 Nb += gp_Vec (P[0], P[2]).Crossed( gp_Vec (P[0], P[3]) );
1201 if ( Nb.Dot( upDir ) < 0 ) {
1202 DUMPSO( "Reverse bottom face");
1203 swap( 1, 3, idNodes, P );
1206 // Find 5-th node - the one closest to the 1-st among the last 4 nodes.
1207 Standard_Real minDist = DBL_MAX;
1208 for ( i = 4; i < 8; i++ ) {
1209 // projection of P[i] to the plane defined by P[0] and upDir
1210 gp_Pnt Pp = P[i].Translated( upDir * ( upDir.Dot( gp_Vec( P[i], P[0] ))));
1211 Standard_Real sqDist = P[0].SquareDistance( Pp );
1212 if ( sqDist < minDist ) {
1217 DUMPSO( "Set 4-th");
1218 swap ( 4, iMin, idNodes, P );
1220 // Set nodes of the top face in good order
1221 DUMPSO( "Sort top face");
1222 i = SortQuadNodes( theMesh, &idNodes[4] );
1225 gp_Pnt Ptmp = P[ i ];
1230 // Assure that direction of the top face normal is from the bottom face
1231 gp_Vec Nt = gp_Vec (P[4], P[5]).Crossed( gp_Vec (P[4], P[6]) );
1232 Nt += gp_Vec (P[4], P[6]).Crossed( gp_Vec (P[4], P[7]) );
1233 if ( Nt.Dot( upDir ) < 0 ) {
1234 DUMPSO( "Reverse top face");
1235 swap( 5, 7, idNodes, P );
1238 // DUMPSO( "OUTPUT: ========================================");
1239 // for ( i = 0; i < 8; i++ ) {
1240 // float *p = ugrid->GetPoint(idNodes[i]);
1241 // DUMPSO( i << "(" << idNodes[i] << ") : " << p[0] << " " << p[1] << " " << p[2]);
1247 //=======================================================================
1248 //function : laplacianSmooth
1249 //purpose : pulls theNode toward the center of surrounding nodes directly
1250 // connected to that node along an element edge
1251 //=======================================================================
1253 void laplacianSmooth(SMESHDS_Mesh * theMesh,
1254 const SMDS_MeshNode* theNode,
1255 const set<const SMDS_MeshElement*> & theElems,
1256 const set<const SMDS_MeshNode*> & theFixedNodes)
1258 // find surrounding nodes
1259 set< const SMDS_MeshNode* > nodeSet;
1260 SMDS_ElemIteratorPtr elemIt = theNode->GetInverseElementIterator();
1261 while ( elemIt->more() )
1263 const SMDS_MeshElement* elem = elemIt->next();
1264 if ( theElems.find( elem ) == theElems.end() )
1267 int i = 0, iNode = 0;
1268 const SMDS_MeshNode* aNodes [4];
1269 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1270 while ( itN->more() )
1272 aNodes[ i ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1273 if ( aNodes[ i ] == theNode )
1276 nodeSet.insert( aNodes[ i ] );
1279 if ( elem->NbNodes() == 4 ) { // remove an opposite node
1280 iNode += ( iNode < 2 ) ? 2 : -2;
1281 nodeSet.erase( aNodes[ iNode ]);
1285 // compute new coodrs
1286 double coord[] = { 0., 0., 0. };
1287 set< const SMDS_MeshNode* >::iterator nodeSetIt = nodeSet.begin();
1288 for ( ; nodeSetIt != nodeSet.end(); nodeSetIt++ ) {
1289 const SMDS_MeshNode* node = (*nodeSetIt);
1290 coord[0] += node->X();
1291 coord[1] += node->Y();
1292 coord[2] += node->Z();
1294 double nbNodes = nodeSet.size();
1295 theMesh->MoveNode (theNode,
1301 //=======================================================================
1302 //function : centroidalSmooth
1303 //purpose : pulls theNode toward the element-area-weighted centroid of the
1304 // surrounding elements
1305 //=======================================================================
1307 void centroidalSmooth(SMESHDS_Mesh * theMesh,
1308 const SMDS_MeshNode* theNode,
1309 const set<const SMDS_MeshElement*> & theElems,
1310 const set<const SMDS_MeshNode*> & theFixedNodes)
1312 gp_XYZ aNewXYZ(0.,0.,0.);
1313 SMESH::Controls::Area anAreaFunc;
1314 double totalArea = 0.;
1317 SMDS_ElemIteratorPtr elemIt = theNode->GetInverseElementIterator();
1318 while ( elemIt->more() )
1320 const SMDS_MeshElement* elem = elemIt->next();
1321 if ( theElems.find( elem ) == theElems.end() )
1326 gp_XYZ elemCenter(0.,0.,0.);
1327 SMESH::Controls::TSequenceOfXYZ aNodePoints;
1328 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1329 while ( itN->more() )
1331 const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>( itN->next() );
1332 gp_XYZ aP( aNode->X(), aNode->Y(), aNode->Z() );
1333 aNodePoints.push_back( aP );
1336 double elemArea = anAreaFunc.GetValue( aNodePoints );
1337 totalArea += elemArea;
1338 elemCenter /= elem->NbNodes();
1339 aNewXYZ += elemCenter * elemArea;
1341 aNewXYZ /= totalArea;
1342 theMesh->MoveNode (theNode,
1348 //=======================================================================
1350 //purpose : Smooth theElements during theNbIterations or until a worst
1351 // element has aspect ratio <= theTgtAspectRatio.
1352 // Aspect Ratio varies in range [1.0, inf].
1353 // If theElements is empty, the whole mesh is smoothed.
1354 // theFixedNodes contains additionally fixed nodes. Nodes built
1355 // on edges and boundary nodes are always fixed.
1356 //=======================================================================
1358 void SMESH_MeshEditor::Smooth (set<const SMDS_MeshElement*> & theElems,
1359 set<const SMDS_MeshNode*> & theFixedNodes,
1360 const SmoothMethod theSmoothMethod,
1361 const int theNbIterations,
1362 double theTgtAspectRatio)
1364 MESSAGE((theSmoothMethod==LAPLACIAN ? "LAPLACIAN" : "CENTROIDAL") << "--::Smooth()");
1366 SMESHDS_Mesh* aMesh = GetMeshDS();
1367 if ( theElems.empty() ) {
1369 SMDS_FaceIteratorPtr fIt = aMesh->facesIterator();
1370 while ( fIt->more() )
1371 theElems.insert( fIt->next() );
1374 set<const SMDS_MeshNode*> setMovableNodes;
1376 // Fill setMovableNodes
1378 map< const SMDS_MeshNode*, int > mapNodeNbFaces;
1379 set< const SMDS_MeshElement* >::iterator itElem;
1380 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1382 const SMDS_MeshElement* elem = (*itElem);
1383 if ( !elem || elem->GetType() != SMDSAbs_Face )
1386 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1387 while ( itN->more() ) {
1388 const SMDS_MeshNode* node =
1389 static_cast<const SMDS_MeshNode*>( itN->next() );
1391 if ( theFixedNodes.find( node ) != theFixedNodes.end() )
1394 // if node is on edge => it is fixed
1395 SMDS_PositionPtr aPositionPtr = node->GetPosition();
1396 if ( aPositionPtr.get() &&
1397 (aPositionPtr->GetTypeOfPosition() == SMDS_TOP_EDGE ||
1398 aPositionPtr->GetTypeOfPosition() == SMDS_TOP_VERTEX)) {
1399 theFixedNodes.insert( node );
1402 // fill mapNodeNbFaces in order to detect fixed boundary nodes
1403 map<const SMDS_MeshNode*,int>::iterator nodeNbFacesIt =
1404 mapNodeNbFaces.find ( node );
1405 if ( nodeNbFacesIt == mapNodeNbFaces.end() )
1406 mapNodeNbFaces.insert( map<const SMDS_MeshNode*,int>::value_type( node, 1 ));
1408 (*nodeNbFacesIt).second++;
1411 // put not fixed nodes in setMovableNodes
1412 map<const SMDS_MeshNode*,int>::iterator nodeNbFacesIt =
1413 mapNodeNbFaces.begin();
1414 for ( ; nodeNbFacesIt != mapNodeNbFaces.end(); nodeNbFacesIt++ ) {
1415 const SMDS_MeshNode* node = (*nodeNbFacesIt).first;
1416 // a node is on free boundary if it is shared by 1-2 faces
1417 if ( (*nodeNbFacesIt).second > 2 )
1418 setMovableNodes.insert( node );
1420 theFixedNodes.insert( node );
1425 if ( theTgtAspectRatio < 1.0 )
1426 theTgtAspectRatio = 1.0;
1428 SMESH::Controls::AspectRatio aQualityFunc;
1430 for ( int it = 0; it < theNbIterations; it++ )
1432 Standard_Real maxDisplacement = 0.;
1433 set<const SMDS_MeshNode*>::iterator movableNodesIt
1434 = setMovableNodes.begin();
1435 for ( ; movableNodesIt != setMovableNodes.end(); movableNodesIt++ )
1437 const SMDS_MeshNode* node = (*movableNodesIt);
1438 gp_XYZ aPrevPos ( node->X(), node->Y(), node->Z() );
1441 if ( theSmoothMethod == LAPLACIAN )
1442 laplacianSmooth( aMesh, node, theElems, theFixedNodes );
1444 centroidalSmooth( aMesh, node, theElems, theFixedNodes );
1447 gp_XYZ aNewPos ( node->X(), node->Y(), node->Z() );
1448 Standard_Real aDispl = (aPrevPos - aNewPos).SquareModulus();
1449 if ( aDispl > maxDisplacement )
1450 maxDisplacement = aDispl;
1452 // no node movement => exit
1453 if ( maxDisplacement < 1.e-16 ) {
1454 MESSAGE("-- no node movement -- maxDisplacement: " << maxDisplacement << " it "<< it);
1458 // check elements quality
1459 double maxRatio = 0;
1460 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1462 const SMDS_MeshElement* elem = (*itElem);
1463 if ( !elem || elem->GetType() != SMDSAbs_Face )
1465 SMESH::Controls::TSequenceOfXYZ aPoints;
1466 if ( aQualityFunc.GetPoints( elem, aPoints )) {
1467 double aValue = aQualityFunc.GetValue( aPoints );
1468 if ( aValue > maxRatio )
1472 if ( maxRatio <= theTgtAspectRatio ) {
1473 MESSAGE("-- quality achived -- maxRatio " << maxRatio << " it "<< it);
1476 if (it+1 == theNbIterations) {
1477 MESSAGE("-- Iteration limit exceeded --");
1482 //=======================================================================
1483 //function : isReverse
1484 //purpose : Return true if normal of prevNodes is not co-directied with
1485 // gp_Vec(prevNodes[iNotSame],nextNodes[iNotSame]).
1486 // iNotSame is where prevNodes and nextNodes are different
1487 //=======================================================================
1489 static bool isReverse(const SMDS_MeshNode* prevNodes[],
1490 const SMDS_MeshNode* nextNodes[],
1494 int iBeforeNotSame = ( iNotSame == 0 ? nbNodes - 1 : iNotSame - 1 );
1495 int iAfterNotSame = ( iNotSame + 1 == nbNodes ? 0 : iNotSame + 1 );
1497 const SMDS_MeshNode* nB = prevNodes[ iBeforeNotSame ];
1498 const SMDS_MeshNode* nA = prevNodes[ iAfterNotSame ];
1499 const SMDS_MeshNode* nP = prevNodes[ iNotSame ];
1500 const SMDS_MeshNode* nN = nextNodes[ iNotSame ];
1502 gp_Pnt pB ( nB->X(), nB->Y(), nB->Z() );
1503 gp_Pnt pA ( nA->X(), nA->Y(), nA->Z() );
1504 gp_Pnt pP ( nP->X(), nP->Y(), nP->Z() );
1505 gp_Pnt pN ( nN->X(), nN->Y(), nN->Z() );
1507 gp_Vec vB ( pP, pB ), vA ( pP, pA ), vN ( pP, pN );
1509 return (vA ^ vB) * vN < 0.0;
1512 //=======================================================================
1513 //function : sweepElement
1515 //=======================================================================
1517 static void sweepElement(SMESHDS_Mesh* aMesh,
1518 const SMDS_MeshElement* elem,
1519 const vector<TNodeOfNodeListMapItr> & newNodesItVec,
1520 list<const SMDS_MeshElement*>& newElems)
1522 // Loop on elem nodes:
1523 // find new nodes and detect same nodes indices
1524 int nbNodes = elem->NbNodes();
1525 list<const SMDS_MeshNode*>::const_iterator itNN[ nbNodes ];
1526 const SMDS_MeshNode* prevNod[ nbNodes ], *nextNod[ nbNodes ];
1527 int iNode, nbSame = 0, iNotSameNode = 0, iSameNode = 0;
1529 for ( iNode = 0; iNode < nbNodes; iNode++ )
1531 TNodeOfNodeListMapItr nnIt = newNodesItVec[ iNode ];
1532 const SMDS_MeshNode* node = nnIt->first;
1533 const list< const SMDS_MeshNode* > & listNewNodes = nnIt->second;
1534 if ( listNewNodes.empty() )
1537 itNN[ iNode ] = listNewNodes.begin();
1538 prevNod[ iNode ] = node;
1539 nextNod[ iNode ] = listNewNodes.front();
1540 if ( prevNod[ iNode ] != nextNod [ iNode ])
1541 iNotSameNode = iNode;
1547 if ( nbSame == nbNodes || nbSame > 2) {
1548 MESSAGE( " Too many same nodes of element " << elem->GetID() );
1552 int iBeforeSame = 0, iAfterSame = 0, iOpposSame = 0;
1554 iBeforeSame = ( iSameNode == 0 ? nbNodes - 1 : iSameNode - 1 );
1555 iAfterSame = ( iSameNode + 1 == nbNodes ? 0 : iSameNode + 1 );
1556 iOpposSame = ( iSameNode - 2 < 0 ? iSameNode + 2 : iSameNode - 2 );
1559 // check element orientation
1561 if ( nbNodes > 2 && !isReverse( prevNod, nextNod, nbNodes, iNotSameNode )) {
1562 //MESSAGE("Reversed elem " << elem );
1566 int iAB = iAfterSame + iBeforeSame;
1567 iBeforeSame = iAB - iBeforeSame;
1568 iAfterSame = iAB - iAfterSame;
1572 // make new elements
1573 int iStep, nbSteps = newNodesItVec[ 0 ]->second.size();
1574 for (iStep = 0; iStep < nbSteps; iStep++ )
1577 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
1578 nextNod[ iNode ] = *itNN[ iNode ];
1581 SMDS_MeshElement* aNewElem = 0;
1586 aNewElem = aMesh->AddEdge( prevNod[ 0 ], nextNod[ 0 ] );
1592 aNewElem = aMesh->AddFace(prevNod[ 0 ], prevNod[ 1 ],
1593 nextNod[ 1 ], nextNod[ 0 ] );
1595 aNewElem = aMesh->AddFace(prevNod[ 0 ], prevNod[ 1 ],
1596 nextNod[ iNotSameNode ] );
1599 case 3: { // TRIANGLE
1601 if ( nbSame == 0 ) // --- pentahedron
1602 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ],
1603 nextNod[ i0 ], nextNod[ 1 ], nextNod[ i2 ] );
1605 else if ( nbSame == 1 ) // --- pyramid
1606 aNewElem = aMesh->AddVolume (prevNod[ iAfterSame ], prevNod[ iBeforeSame ],
1607 nextNod[ iBeforeSame ], nextNod[ iAfterSame ],
1608 nextNod[ iSameNode ]);
1610 else // 2 same nodes: --- tetrahedron
1611 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ],
1612 nextNod[ iNotSameNode ]);
1615 case 4: { // QUADRANGLE
1617 if ( nbSame == 0 ) // --- hexahedron
1618 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ], prevNod[ 3 ],
1619 nextNod[ i0 ], nextNod[ 1 ], nextNod[ i2 ], nextNod[ 3 ]);
1621 else if ( nbSame == 1 ) // --- pyramid + pentahedron
1623 aNewElem = aMesh->AddVolume (prevNod[ iAfterSame ], prevNod[ iBeforeSame ],
1624 nextNod[ iBeforeSame ], nextNod[ iAfterSame ],
1625 nextNod[ iSameNode ]);
1626 newElems.push_back( aNewElem );
1627 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iOpposSame ],
1628 prevNod[ iAfterSame ], nextNod[ iBeforeSame ],
1629 nextNod[ iOpposSame ], nextNod[ iAfterSame ] );
1631 else if ( nbSame == 2 ) // pentahedron
1633 if ( prevNod[ iBeforeSame ] == nextNod[ iBeforeSame ] )
1634 // iBeforeSame is same too
1635 aNewElem = aMesh->AddVolume (prevNod[ iOpposSame ], prevNod[ iBeforeSame ],
1636 nextNod[ iOpposSame ], prevNod[ iAfterSame ],
1637 prevNod[ iSameNode ], nextNod[ iAfterSame ]);
1639 // iAfterSame is same too
1640 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iSameNode ],
1641 nextNod[ iBeforeSame ], prevNod[ iOpposSame ],
1642 prevNod[ iAfterSame ], nextNod[ iOpposSame ]);
1650 newElems.push_back( aNewElem );
1652 // set new prev nodes
1653 for ( iNode = 0; iNode < nbNodes; iNode++ )
1654 prevNod[ iNode ] = nextNod[ iNode ];
1659 //=======================================================================
1660 //function : makeWalls
1661 //purpose : create 1D and 2D elements around swept elements
1662 //=======================================================================
1664 static void makeWalls (SMESHDS_Mesh* aMesh,
1665 TNodeOfNodeListMap & mapNewNodes,
1666 TElemOfElemListMap & newElemsMap,
1667 TElemOfVecOfNnlmiMap & elemNewNodesMap,
1668 set<const SMDS_MeshElement*>& elemSet)
1670 ASSERT( newElemsMap.size() == elemNewNodesMap.size() );
1672 // Find nodes belonging to only one initial element - sweep them to get edges.
1674 TNodeOfNodeListMapItr nList = mapNewNodes.begin();
1675 for ( ; nList != mapNewNodes.end(); nList++ )
1677 const SMDS_MeshNode* node =
1678 static_cast<const SMDS_MeshNode*>( nList->first );
1679 SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator();
1680 int nbInitElems = 0;
1681 while ( eIt->more() && nbInitElems < 2 )
1682 if ( elemSet.find( eIt->next() ) != elemSet.end() )
1684 if ( nbInitElems < 2 ) {
1685 vector<TNodeOfNodeListMapItr> newNodesItVec( 1, nList );
1686 list<const SMDS_MeshElement*> newEdges;
1687 sweepElement( aMesh, node, newNodesItVec, newEdges );
1691 // Make a ceiling for each element ie an equal element of last new nodes.
1692 // Find free links of faces - make edges and sweep them into faces.
1694 TElemOfElemListMap::iterator itElem = newElemsMap.begin();
1695 TElemOfVecOfNnlmiMap::iterator itElemNodes = elemNewNodesMap.begin();
1696 for ( ; itElem != newElemsMap.end(); itElem++, itElemNodes++ )
1698 const SMDS_MeshElement* elem = itElem->first;
1699 vector<TNodeOfNodeListMapItr>& vecNewNodes = itElemNodes->second;
1701 if ( elem->GetType() == SMDSAbs_Edge )
1703 // create a ceiling edge
1704 aMesh->AddEdge(vecNewNodes[ 0 ]->second.back(),
1705 vecNewNodes[ 1 ]->second.back() );
1707 if ( elem->GetType() != SMDSAbs_Face )
1710 bool hasFreeLinks = false;
1712 set<const SMDS_MeshElement*> avoidSet;
1713 avoidSet.insert( elem );
1715 // loop on a face nodes
1716 set<const SMDS_MeshNode*> aFaceLastNodes;
1717 int iNode, nbNodes = vecNewNodes.size();
1718 for ( iNode = 0; iNode < nbNodes; iNode++ )
1720 aFaceLastNodes.insert( vecNewNodes[ iNode ]->second.back() );
1721 // look for free links of a face
1722 int iNext = ( iNode + 1 == nbNodes ) ? 0 : iNode + 1;
1723 const SMDS_MeshNode* n1 = vecNewNodes[ iNode ]->first;
1724 const SMDS_MeshNode* n2 = vecNewNodes[ iNext ]->first;
1725 // check if a link is free
1726 if ( ! SMESH_MeshEditor::FindFaceInSet ( n1, n2, elemSet, avoidSet ))
1728 hasFreeLinks = true;
1729 // make an edge and a ceiling for a new edge
1730 if ( !aMesh->FindEdge( n1, n2 ))
1731 aMesh->AddEdge( n1, n2 );
1732 n1 = vecNewNodes[ iNode ]->second.back();
1733 n2 = vecNewNodes[ iNext ]->second.back();
1734 if ( !aMesh->FindEdge( n1, n2 ))
1735 aMesh->AddEdge( n1, n2 );
1738 // sweep free links into faces
1742 list<const SMDS_MeshElement*> & newVolumes = itElem->second;
1743 int iStep, nbSteps = vecNewNodes[0]->second.size();
1744 int iVol, volNb, nbVolumesByStep = newVolumes.size() / nbSteps;
1746 set<const SMDS_MeshNode*> initNodeSet, faceNodeSet;
1747 for ( iNode = 0; iNode < nbNodes; iNode++ )
1748 initNodeSet.insert( vecNewNodes[ iNode ]->first );
1750 for ( volNb = 0; volNb < nbVolumesByStep; volNb++ )
1752 list<const SMDS_MeshElement*>::iterator v = newVolumes.begin();
1754 while ( iVol++ < volNb ) v++;
1755 // find indices of free faces of a volume
1757 SMDS_VolumeTool vTool( *v );
1758 int iF, nbF = vTool.NbFaces();
1759 for ( iF = 0; iF < nbF; iF ++ )
1760 if (vTool.IsFreeFace( iF ) &&
1761 vTool.GetFaceNodes( iF, faceNodeSet ) &&
1762 initNodeSet != faceNodeSet) // except an initial face
1763 fInd.push_back( iF );
1767 // create faces for all steps
1768 for ( iStep = 0; iStep < nbSteps; iStep++ )
1771 vTool.SetExternalNormal();
1772 list< int >::iterator ind = fInd.begin();
1773 for ( ; ind != fInd.end(); ind++ )
1775 const SMDS_MeshNode** nodes = vTool.GetFaceNodes( *ind );
1776 switch ( vTool.NbFaceNodes( *ind ) ) {
1778 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] ); break;
1780 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ] ); break;
1783 // go to the next volume
1785 while ( iVol++ < nbVolumesByStep ) v++;
1788 } // sweep free links into faces
1790 // make a ceiling face with a normal external to a volume
1792 SMDS_VolumeTool lastVol( itElem->second.back() );
1793 int iF = lastVol.GetFaceIndex( aFaceLastNodes );
1796 lastVol.SetExternalNormal();
1797 const SMDS_MeshNode** nodes = lastVol.GetFaceNodes( iF );
1798 switch ( lastVol.NbFaceNodes( iF ) ) {
1800 if (!hasFreeLinks ||
1801 !aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ]))
1802 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] );
1805 if (!hasFreeLinks ||
1806 !aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ]))
1807 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ] );
1812 } // loop on swept elements
1815 //=======================================================================
1816 //function : RotationSweep
1818 //=======================================================================
1820 void SMESH_MeshEditor::RotationSweep(set<const SMDS_MeshElement*> & theElems,
1821 const gp_Ax1& theAxis,
1822 const double theAngle,
1823 const int theNbSteps,
1824 const double theTol)
1826 MESSAGE( "RotationSweep()");
1828 aTrsf.SetRotation( theAxis, theAngle );
1830 gp_Lin aLine( theAxis );
1831 double aSqTol = theTol * theTol;
1833 SMESHDS_Mesh* aMesh = GetMeshDS();
1835 TNodeOfNodeListMap mapNewNodes;
1836 TElemOfVecOfNnlmiMap mapElemNewNodes;
1837 TElemOfElemListMap newElemsMap;
1840 set< const SMDS_MeshElement* >::iterator itElem;
1841 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1843 const SMDS_MeshElement* elem = (*itElem);
1846 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
1847 newNodesItVec.reserve( elem->NbNodes() );
1849 // loop on elem nodes
1850 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1851 while ( itN->more() ) {
1853 // check if a node has been already sweeped
1854 const SMDS_MeshNode* node =
1855 static_cast<const SMDS_MeshNode*>( itN->next() );
1856 TNodeOfNodeListMapItr nIt = mapNewNodes.find( node );
1857 if ( nIt == mapNewNodes.end() )
1859 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
1860 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
1863 gp_XYZ aXYZ( node->X(), node->Y(), node->Z() );
1865 aXYZ.Coord( coord[0], coord[1], coord[2] );
1866 bool isOnAxis = ( aLine.SquareDistance( aXYZ ) <= aSqTol );
1867 const SMDS_MeshNode * newNode = node;
1868 for ( int i = 0; i < theNbSteps; i++ ) {
1870 aTrsf.Transforms( coord[0], coord[1], coord[2] );
1871 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
1873 listNewNodes.push_back( newNode );
1876 newNodesItVec.push_back( nIt );
1878 // make new elements
1879 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem] );
1882 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElems );
1885 //=======================================================================
1886 //function : ExtrusionSweep
1888 //=======================================================================
1890 void SMESH_MeshEditor::ExtrusionSweep(set<const SMDS_MeshElement*> & theElems,
1891 const gp_Vec& theStep,
1892 const int theNbSteps)
1895 aTrsf.SetTranslation( theStep );
1897 SMESHDS_Mesh* aMesh = GetMeshDS();
1899 TNodeOfNodeListMap mapNewNodes;
1900 TElemOfVecOfNnlmiMap mapElemNewNodes;
1901 TElemOfElemListMap newElemsMap;
1904 set< const SMDS_MeshElement* >::iterator itElem;
1905 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1907 // check element type
1908 const SMDS_MeshElement* elem = (*itElem);
1912 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
1913 newNodesItVec.reserve( elem->NbNodes() );
1915 // loop on elem nodes
1916 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1917 while ( itN->more() ) {
1919 // check if a node has been already sweeped
1920 const SMDS_MeshNode* node =
1921 static_cast<const SMDS_MeshNode*>( itN->next() );
1922 TNodeOfNodeListMap::iterator nIt = mapNewNodes.find( node );
1923 if ( nIt == mapNewNodes.end() )
1925 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
1926 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
1929 double coord[] = { node->X(), node->Y(), node->Z() };
1930 for ( int i = 0; i < theNbSteps; i++ ) {
1931 aTrsf.Transforms( coord[0], coord[1], coord[2] );
1932 const SMDS_MeshNode * newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
1933 listNewNodes.push_back( newNode );
1936 newNodesItVec.push_back( nIt );
1938 // make new elements
1939 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem] );
1942 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElems );
1946 class SMESH_MeshEditor_PathPoint {
1948 SMESH_MeshEditor_PathPoint() {
1949 myPnt.SetCoord(99., 99., 99.);
1950 myTgt.SetCoord(1.,0.,0.);
1954 void SetPnt(const gp_Pnt& aP3D){
1957 void SetTangent(const gp_Dir& aTgt){
1960 void SetAngle(const double& aBeta){
1963 void SetParameter(const double& aPrm){
1966 const gp_Pnt& Pnt()const{
1969 const gp_Dir& Tangent()const{
1972 double Angle()const{
1975 double Parameter()const{
1986 //=======================================================================
1987 //function : ExtrusionAlongTrack
1989 //=======================================================================
1990 int SMESH_MeshEditor::ExtrusionAlongTrack (std::set<const SMDS_MeshElement*> & theElements,
1991 SMESH_subMesh* theTrack,
1992 const SMDS_MeshNode* theN1,
1993 const bool theHasAngles,
1994 std::list<double>& theAngles,
1995 const bool theHasRefPoint,
1996 const gp_Pnt& theRefPoint)
1998 MESSAGE("SMESH_MeshEditor::ExtrusionAlongTrack")
1999 int j, iErr, aNbTP, aNbAngles, aNbE, aNb;
2000 double aT1, aT2, aT, aAngle, aX, aY, aZ;
2001 std::list<double> aPrms;
2002 std::list<double>::iterator aItD;
2003 std::set< const SMDS_MeshElement* >::iterator itElem;
2005 Standard_Real aTx1, aTx2, aL2, aTolVec, aTolVec2;
2009 Handle(Geom_Curve) aC3D;
2010 TopoDS_Edge aTrackEdge;
2011 TopoDS_Vertex aV1, aV2;
2013 SMDS_ElemIteratorPtr aItE;
2014 SMDS_NodeIteratorPtr aItN;
2015 SMDSAbs_ElementType aTypeE;
2017 TNodeOfNodeListMap mapNewNodes;
2018 TElemOfVecOfNnlmiMap mapElemNewNodes;
2019 TElemOfElemListMap newElemsMap;
2022 aTolVec2=aTolVec*aTolVec;
2026 aNbE=theElements.size();
2028 iErr = 10; // nothing to do
2032 // 1.1 Track Pattern
2035 SMESHDS_SubMesh* pSubMeshDS=theTrack->GetSubMeshDS();
2037 if ( !pSubMeshDS->Contains( theN1 ) ) {
2038 iErr = 2; // No match found for start node
2042 aItE = pSubMeshDS->GetElements();
2043 while ( aItE->more() ) {
2044 const SMDS_MeshElement* pE = aItE->next();
2045 aTypeE = pE->GetType();
2046 if ( aTypeE != SMDSAbs_Edge ) {
2047 iErr = 3; // Pattern must contain links only
2052 const TopoDS_Shape& aS = theTrack->GetSubShape();
2053 if ( aS.ShapeType() != TopAbs_EDGE) {
2054 iErr = 3; // Sub shape for the Pattern must be an Edge
2056 aTrackEdge = TopoDS::Edge( aS );
2057 if ( BRep_Tool::Degenerated( aTrackEdge ) ) {
2058 iErr = 4; // the Edge must not be degenerated
2063 TopExp::Vertices( aTrackEdge, aV1, aV2 );
2064 aT1=BRep_Tool::Parameter( aV1, aTrackEdge );
2065 aT2=BRep_Tool::Parameter( aV2, aTrackEdge );
2067 aItN = myMesh->GetSubMesh( aV1 )->GetSubMeshDS()->GetNodes();
2068 const SMDS_MeshNode* aN1 = aItN->next();
2070 aItN = myMesh->GetSubMesh( aV2 )->GetSubMeshDS()->GetNodes();
2071 const SMDS_MeshNode* aN2 = aItN->next();
2073 if ( !( aN1 == theN1 || aN2 == theN1 ) ) {
2074 iErr = 5; // starting node must be aN1 or aN2
2078 aNbTP = pSubMeshDS->NbNodes() + 2;
2081 vector<double> aAngles( aNbTP );
2083 if ( theHasAngles ) {
2084 aNbAngles = theAngles.size();
2085 if ( aNbTP != aNbAngles ) {
2086 iErr = 6; // number of Angles does not match to the number of track points
2089 aItD = theAngles.begin();
2090 for ( j=0; aItD != aPrms.end(); ++aItD, ++j ) {
2092 aAngles[j] = aAngle;
2096 for ( j=0; j < aNbTP; ++j ) {
2101 // 2. Collect parameters on the track edge
2102 aPrms.push_back( aT1 );
2103 aPrms.push_back( aT2 );
2105 aItN = pSubMeshDS->GetNodes();
2106 while ( aItN->more() ) {
2107 const SMDS_MeshNode* pNode = aItN->next();
2108 const SMDS_EdgePosition* pEPos =
2109 static_cast<const SMDS_EdgePosition*>( pNode->GetPosition().get() );
2110 aT = pEPos->GetUParameter();
2111 aPrms.push_back( aT );
2116 if ( aN1 == theN1 ) {
2128 SMESH_MeshEditor_PathPoint aPP;
2129 vector<SMESH_MeshEditor_PathPoint> aPPs( aNbTP );
2131 aC3D = BRep_Tool::Curve( aTrackEdge, aTx1, aTx2 );
2133 aItD = aPrms.begin();
2134 for ( j=0; aItD != aPrms.end(); ++aItD, ++j ) {
2136 aC3D->D1( aT, aP3D, aVec );
2137 aL2 = aVec.SquareMagnitude();
2138 if ( aL2 < aTolVec2 ) {
2139 iErr = 20; // can not obtain the tangent;
2142 gp_Dir aTgt( aVec );
2143 aAngle = aAngles[j];
2146 aPP.SetTangent( aTgt );
2147 aPP.SetAngle( aAngle );
2148 aPP.SetParameter( aT );
2152 // 3. Center of rotation aV0
2154 if ( !theHasRefPoint ) {
2156 aGC.SetCoord( 0.,0.,0. );
2158 itElem = theElements.begin();
2159 for ( ; itElem != theElements.end(); itElem++ ) {
2160 const SMDS_MeshElement* elem = (*itElem);
2162 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2163 while ( itN->more() ) {
2164 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( itN->next() );
2169 if ( mapNewNodes.find( node ) == mapNewNodes.end() ) {
2170 list<const SMDS_MeshNode*> aLNx;
2171 mapNewNodes[node] = aLNx;
2173 gp_XYZ aXYZ( aX, aY, aZ );
2181 } // if (!theHasRefPoint) {
2182 mapNewNodes.clear();
2184 // 4. Processing the elements
2185 SMESHDS_Mesh* aMesh = GetMeshDS();
2187 for ( itElem = theElements.begin(); itElem != theElements.end(); itElem++ ) {
2188 // check element type
2189 const SMDS_MeshElement* elem = (*itElem);
2190 aTypeE = elem->GetType();
2191 if ( !elem || ( aTypeE != SMDSAbs_Face && aTypeE != SMDSAbs_Edge ) )
2194 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
2195 newNodesItVec.reserve( elem->NbNodes() );
2197 // loop on elem nodes
2198 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2199 while ( itN->more() ) {
2201 // check if a node has been already processed
2202 const SMDS_MeshNode* node =
2203 static_cast<const SMDS_MeshNode*>( itN->next() );
2204 TNodeOfNodeListMap::iterator nIt = mapNewNodes.find( node );
2205 if ( nIt == mapNewNodes.end() ) {
2206 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
2207 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
2210 aX = node->X(); aY = node->Y(); aZ = node->Z();
2212 Standard_Real aAngle1x;
2213 gp_Pnt aP0x, aP1x, aPN0, aPN1, aV0x, aV1x;
2217 aPN0.SetCoord(aX, aY, aZ);
2219 const SMESH_MeshEditor_PathPoint& aPP0 = aPPs[0];
2222 for ( j = 1; j < aNbTP; ++j ) {
2223 const SMESH_MeshEditor_PathPoint& aPP1 = aPPs[j];
2225 const gp_Dir& aDT1x = aPP1.Tangent();
2226 aAngle1x = aPP1.Angle();
2228 gp_Trsf aTrsf, aTrsfRot;
2230 gp_Vec aV01x( aP0x, aP1x );
2231 aTrsf.SetTranslation( aV01x );
2234 aV1x = aV0x.Transformed( aTrsf );
2235 aPN1 = aPN0.Transformed( aTrsf );
2237 if ( theHasAngles ) {
2238 anAx1.SetLocation( aV1x );
2239 anAx1.SetDirection( aDT1x );
2240 aTrsfRot.SetRotation( anAx1, aAngle1x );
2242 aPN1 = aPN1.Transformed( aTrsfRot );
2249 const SMDS_MeshNode* newNode = aMesh->AddNode( aX, aY, aZ );
2250 listNewNodes.push_back( newNode );
2257 newNodesItVec.push_back( nIt );
2259 // make new elements
2260 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem] );
2263 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElements );
2267 //=======================================================================
2268 //function : Transform
2270 //=======================================================================
2272 void SMESH_MeshEditor::Transform (set<const SMDS_MeshElement*> & theElems,
2273 const gp_Trsf& theTrsf,
2277 switch ( theTrsf.Form() ) {
2283 needReverse = false;
2286 SMESHDS_Mesh* aMesh = GetMeshDS();
2288 // map old node to new one
2289 TNodeNodeMap nodeMap;
2291 // elements sharing moved nodes; those of them which have all
2292 // nodes mirrored but are not in theElems are to be reversed
2293 set<const SMDS_MeshElement*> inverseElemSet;
2296 set< const SMDS_MeshElement* >::iterator itElem;
2297 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
2299 const SMDS_MeshElement* elem = (*itElem);
2303 // loop on elem nodes
2304 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2305 while ( itN->more() ) {
2307 // check if a node has been already transormed
2308 const SMDS_MeshNode* node =
2309 static_cast<const SMDS_MeshNode*>( itN->next() );
2310 if (nodeMap.find( node ) != nodeMap.end() )
2314 coord[0] = node->X();
2315 coord[1] = node->Y();
2316 coord[2] = node->Z();
2317 theTrsf.Transforms( coord[0], coord[1], coord[2] );
2318 const SMDS_MeshNode * newNode = node;
2320 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
2322 aMesh->MoveNode( node, coord[0], coord[1], coord[2] );
2323 nodeMap.insert( TNodeNodeMap::value_type( node, newNode ));
2325 // keep inverse elements
2326 if ( !theCopy && needReverse ) {
2327 SMDS_ElemIteratorPtr invElemIt = node->GetInverseElementIterator();
2328 while ( invElemIt->more() )
2329 inverseElemSet.insert( invElemIt->next() );
2334 // either new elements are to be created
2335 // or a mirrored element are to be reversed
2336 if ( !theCopy && !needReverse)
2339 if ( !inverseElemSet.empty()) {
2340 set<const SMDS_MeshElement*>::iterator invElemIt = inverseElemSet.begin();
2341 for ( ; invElemIt != inverseElemSet.end(); invElemIt++ )
2342 theElems.insert( *invElemIt );
2345 // replicate or reverse elements
2348 REV_TETRA = 0, // = nbNodes - 4
2349 REV_PYRAMID = 1, // = nbNodes - 4
2350 REV_PENTA = 2, // = nbNodes - 4
2352 REV_HEXA = 4, // = nbNodes - 4
2356 { 2, 1, 0, 3, 4, 0, 0, 0 }, // REV_TETRA
2357 { 2, 1, 0, 3, 4, 0, 0, 0 }, // REV_PYRAMID
2358 { 2, 1, 0, 5, 4, 3, 0, 0 }, // REV_PENTA
2359 { 2, 1, 0, 3, 0, 0, 0, 0 }, // REV_FACE
2360 { 2, 1, 0, 3, 6, 5, 4, 7 }, // REV_HEXA
2361 { 0, 1, 2, 3, 4, 5, 6, 7 } // FORWARD
2364 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
2366 const SMDS_MeshElement* elem = (*itElem);
2367 if ( !elem || elem->GetType() == SMDSAbs_Node )
2370 int nbNodes = elem->NbNodes();
2371 int elemType = elem->GetType();
2373 int* i = index[ FORWARD ];
2374 if ( needReverse && nbNodes > 2) // reverse mirrored faces and volumes
2375 if ( elemType == SMDSAbs_Face )
2376 i = index[ REV_FACE ];
2378 i = index[ nbNodes - 4 ];
2380 // find transformed nodes
2381 const SMDS_MeshNode* nodes[8];
2383 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2384 while ( itN->more() )
2386 const SMDS_MeshNode* node =
2387 static_cast<const SMDS_MeshNode*>( itN->next() );
2388 TNodeNodeMap::iterator nodeMapIt = nodeMap.find( node );
2389 if ( nodeMapIt == nodeMap.end() )
2390 break; // not all nodes transformed
2391 nodes[ i [ iNode++ ]] = (*nodeMapIt).second;
2393 if ( iNode != nbNodes )
2394 continue; // not all nodes transformed
2398 // add a new element
2399 switch ( elemType ) {
2401 aMesh->AddEdge( nodes[ 0 ], nodes[ 1 ] );
2405 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] );
2407 aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ]);
2409 case SMDSAbs_Volume:
2411 aMesh->AddVolume( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ] );
2412 else if ( nbNodes == 8 )
2413 aMesh->AddVolume( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ],
2414 nodes[ 4 ], nodes[ 5 ], nodes[ 6 ] , nodes[ 7 ]);
2415 else if ( nbNodes == 6 )
2416 aMesh->AddVolume( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ],
2417 nodes[ 4 ], nodes[ 5 ]);
2418 else if ( nbNodes == 5 )
2419 aMesh->AddVolume( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] , nodes[ 3 ],
2427 // reverse element as it was reversed by transformation
2429 aMesh->ChangeElementNodes( elem, nodes, nbNodes );
2434 //=======================================================================
2435 //function : FindCoincidentNodes
2436 //purpose : Return list of group of nodes close to each other within theTolerance
2437 // Search among theNodes or in the whole mesh if theNodes is empty.
2438 //=======================================================================
2440 void SMESH_MeshEditor::FindCoincidentNodes (set<const SMDS_MeshNode*> & theNodes,
2441 const double theTolerance,
2442 TListOfListOfNodes & theGroupsOfNodes)
2444 double tol2 = theTolerance * theTolerance;
2446 list<const SMDS_MeshNode*> nodes;
2447 if ( theNodes.empty() )
2448 { // get all nodes in the mesh
2449 SMDS_NodeIteratorPtr nIt = GetMeshDS()->nodesIterator();
2450 while ( nIt->more() )
2451 nodes.push_back( nIt->next() );
2455 nodes.insert( nodes.end(), theNodes.begin(), theNodes.end() );
2458 list<const SMDS_MeshNode*>::iterator it2, it1 = nodes.begin();
2459 for ( ; it1 != nodes.end(); it1++ )
2461 const SMDS_MeshNode* n1 = *it1;
2462 gp_Pnt p1( n1->X(), n1->Y(), n1->Z() );
2464 list<const SMDS_MeshNode*> * groupPtr = 0;
2466 for ( it2++; it2 != nodes.end(); it2++ )
2468 const SMDS_MeshNode* n2 = *it2;
2469 gp_Pnt p2( n2->X(), n2->Y(), n2->Z() );
2470 if ( p1.SquareDistance( p2 ) <= tol2 )
2473 theGroupsOfNodes.push_back( list<const SMDS_MeshNode*>() );
2474 groupPtr = & theGroupsOfNodes.back();
2475 groupPtr->push_back( n1 );
2477 groupPtr->push_back( n2 );
2478 it2 = nodes.erase( it2 );
2485 //=======================================================================
2486 //function : MergeNodes
2487 //purpose : In each group, the cdr of nodes are substituted by the first one
2489 //=======================================================================
2491 void SMESH_MeshEditor::MergeNodes (TListOfListOfNodes & theGroupsOfNodes)
2493 SMESHDS_Mesh* aMesh = GetMeshDS();
2495 TNodeNodeMap nodeNodeMap; // node to replace - new node
2496 set<const SMDS_MeshElement*> elems; // all elements with changed nodes
2497 list< int > rmElemIds, rmNodeIds;
2499 // Fill nodeNodeMap and elems
2501 TListOfListOfNodes::iterator grIt = theGroupsOfNodes.begin();
2502 for ( ; grIt != theGroupsOfNodes.end(); grIt++ )
2504 list<const SMDS_MeshNode*>& nodes = *grIt;
2505 list<const SMDS_MeshNode*>::iterator nIt = nodes.begin();
2506 const SMDS_MeshNode* nToKeep = *nIt;
2507 for ( ; nIt != nodes.end(); nIt++ )
2509 const SMDS_MeshNode* nToRemove = *nIt;
2510 nodeNodeMap.insert( TNodeNodeMap::value_type( nToRemove, nToKeep ));
2511 if ( nToRemove != nToKeep ) {
2512 rmNodeIds.push_back( nToRemove->GetID() );
2513 AddToSameGroups( nToKeep, nToRemove, aMesh );
2516 SMDS_ElemIteratorPtr invElemIt = nToRemove->GetInverseElementIterator();
2517 while ( invElemIt->more() )
2518 elems.insert( invElemIt->next() );
2521 // Change element nodes or remove an element
2523 set<const SMDS_MeshElement*>::iterator eIt = elems.begin();
2524 for ( ; eIt != elems.end(); eIt++ )
2526 const SMDS_MeshElement* elem = *eIt;
2527 int nbNodes = elem->NbNodes();
2528 int aShapeId = FindShape( elem );
2530 set<const SMDS_MeshNode*> nodeSet;
2531 const SMDS_MeshNode* curNodes[ nbNodes ], *uniqueNodes[ nbNodes ];
2532 int iUnique = 0, iCur = 0, nbRepl = 0, iRepl [ nbNodes ];
2534 // get new seq of nodes
2535 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2536 while ( itN->more() )
2538 const SMDS_MeshNode* n =
2539 static_cast<const SMDS_MeshNode*>( itN->next() );
2541 TNodeNodeMap::iterator nnIt = nodeNodeMap.find( n );
2542 if ( nnIt != nodeNodeMap.end() ) { // n sticks
2544 iRepl[ nbRepl++ ] = iCur;
2546 curNodes[ iCur ] = n;
2547 bool isUnique = nodeSet.insert( n ).second;
2549 uniqueNodes[ iUnique++ ] = n;
2553 // Analyse element topology after replacement
2556 int nbUniqueNodes = nodeSet.size();
2557 if ( nbNodes != nbUniqueNodes ) // some nodes stick
2559 switch ( nbNodes ) {
2560 case 2: ///////////////////////////////////// EDGE
2561 isOk = false; break;
2562 case 3: ///////////////////////////////////// TRIANGLE
2563 isOk = false; break;
2565 if ( elem->GetType() == SMDSAbs_Volume ) // TETRAHEDRON
2567 else { //////////////////////////////////// QUADRANGLE
2568 if ( nbUniqueNodes < 3 )
2570 else if ( nbRepl == 2 && iRepl[ 1 ] - iRepl[ 0 ] == 2 )
2571 isOk = false; // opposite nodes stick
2574 case 6: ///////////////////////////////////// PENTAHEDRON
2575 if ( nbUniqueNodes == 4 ) {
2576 // ---------------------------------> tetrahedron
2578 iRepl[ 0 ] > 2 && iRepl[ 1 ] > 2 && iRepl[ 2 ] > 2 ) {
2579 // all top nodes stick: reverse a bottom
2580 uniqueNodes[ 0 ] = curNodes [ 1 ];
2581 uniqueNodes[ 1 ] = curNodes [ 0 ];
2583 else if (nbRepl == 3 &&
2584 iRepl[ 0 ] < 3 && iRepl[ 1 ] < 3 && iRepl[ 2 ] < 3 ) {
2585 // all bottom nodes stick: set a top before
2586 uniqueNodes[ 3 ] = uniqueNodes [ 0 ];
2587 uniqueNodes[ 0 ] = curNodes [ 3 ];
2588 uniqueNodes[ 1 ] = curNodes [ 4 ];
2589 uniqueNodes[ 2 ] = curNodes [ 5 ];
2591 else if (nbRepl == 4 &&
2592 iRepl[ 2 ] - iRepl [ 0 ] == 3 && iRepl[ 3 ] - iRepl [ 1 ] == 3 ) {
2593 // a lateral face turns into a line: reverse a bottom
2594 uniqueNodes[ 0 ] = curNodes [ 1 ];
2595 uniqueNodes[ 1 ] = curNodes [ 0 ];
2600 else if ( nbUniqueNodes == 5 ) {
2601 // PENTAHEDRON --------------------> 2 tetrahedrons
2602 if ( nbRepl == 2 && iRepl[ 1 ] - iRepl [ 0 ] == 3 ) {
2603 // a bottom node sticks with a linked top one
2605 SMDS_MeshElement* newElem =
2606 aMesh->AddVolume(curNodes[ 3 ],
2609 curNodes[ iRepl[ 0 ] == 2 ? 1 : 2 ]);
2611 aMesh->SetMeshElementOnShape( newElem, aShapeId );
2612 // 2. : reverse a bottom
2613 uniqueNodes[ 0 ] = curNodes [ 1 ];
2614 uniqueNodes[ 1 ] = curNodes [ 0 ];
2623 case 8: { //////////////////////////////////// HEXAHEDRON
2625 SMDS_VolumeTool hexa (elem);
2626 hexa.SetExternalNormal();
2627 if ( nbUniqueNodes == 4 && nbRepl == 6 ) {
2628 //////////////////////// ---> tetrahedron
2629 for ( int iFace = 0; iFace < 6; iFace++ ) {
2630 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
2631 if (curNodes[ind[ 0 ]] == curNodes[ind[ 1 ]] &&
2632 curNodes[ind[ 0 ]] == curNodes[ind[ 2 ]] &&
2633 curNodes[ind[ 0 ]] == curNodes[ind[ 3 ]] ) {
2634 // one face turns into a point ...
2635 int iOppFace = hexa.GetOppFaceIndex( iFace );
2636 ind = hexa.GetFaceNodesIndices( iOppFace );
2638 iUnique = 2; // reverse a tetrahedron bottom
2639 for ( iCur = 0; iCur < 4 && nbStick < 2; iCur++ ) {
2640 if ( curNodes[ind[ iCur ]] == curNodes[ind[ iCur + 1 ]] )
2642 else if ( iUnique >= 0 )
2643 uniqueNodes[ iUnique-- ] = curNodes[ind[ iCur ]];
2645 if ( nbStick == 1 ) {
2646 // ... and the opposite one - into a triangle.
2648 ind = hexa.GetFaceNodesIndices( iFace );
2649 uniqueNodes[ 3 ] = curNodes[ind[ 0 ]];
2656 else if (nbUniqueNodes == 5 && nbRepl == 4 ) {
2657 //////////////////// HEXAHEDRON ---> 2 tetrahedrons
2658 for ( int iFace = 0; iFace < 6; iFace++ ) {
2659 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
2660 if (curNodes[ind[ 0 ]] == curNodes[ind[ 1 ]] &&
2661 curNodes[ind[ 0 ]] == curNodes[ind[ 2 ]] &&
2662 curNodes[ind[ 0 ]] == curNodes[ind[ 3 ]] ) {
2663 // one face turns into a point ...
2664 int iOppFace = hexa.GetOppFaceIndex( iFace );
2665 ind = hexa.GetFaceNodesIndices( iOppFace );
2667 iUnique = 2; // reverse a tetrahedron 1 bottom
2668 for ( iCur = 0; iCur < 4 && nbStick == 0; iCur++ ) {
2669 if ( curNodes[ind[ iCur ]] == curNodes[ind[ iCur + 1 ]] )
2671 else if ( iUnique >= 0 )
2672 uniqueNodes[ iUnique-- ] = curNodes[ind[ iCur ]];
2674 if ( nbStick == 0 ) {
2675 // ... and the opposite one is a quadrangle
2677 const int* indTop = hexa.GetFaceNodesIndices( iFace );
2678 uniqueNodes[ 3 ] = curNodes[indTop[ 0 ]];
2681 SMDS_MeshElement* newElem =
2682 aMesh->AddVolume(curNodes[ind[ 0 ]],
2685 curNodes[indTop[ 0 ]]);
2687 aMesh->SetMeshElementOnShape( newElem, aShapeId );
2694 else if ( nbUniqueNodes == 6 && nbRepl == 4 ) {
2695 ////////////////// HEXAHEDRON ---> 2 tetrahedrons or 1 prism
2696 // find indices of quad and tri faces
2697 int iQuadFace[ 6 ], iTriFace[ 6 ], nbQuad = 0, nbTri = 0, iFace;
2698 for ( iFace = 0; iFace < 6; iFace++ ) {
2699 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
2701 for ( iCur = 0; iCur < 4; iCur++ )
2702 nodeSet.insert( curNodes[ind[ iCur ]] );
2703 nbUniqueNodes = nodeSet.size();
2704 if ( nbUniqueNodes == 3 )
2705 iTriFace[ nbTri++ ] = iFace;
2706 else if ( nbUniqueNodes == 4 )
2707 iQuadFace[ nbQuad++ ] = iFace;
2709 if (nbQuad == 2 && nbTri == 4 &&
2710 hexa.GetOppFaceIndex( iQuadFace[ 0 ] ) == iQuadFace[ 1 ]) {
2711 // 2 opposite quadrangles stuck with a diagonal;
2712 // sample groups of merged indices: (0-4)(2-6)
2713 // --------------------------------------------> 2 tetrahedrons
2714 const int *ind1 = hexa.GetFaceNodesIndices( iQuadFace[ 0 ]); // indices of quad1 nodes
2715 const int *ind2 = hexa.GetFaceNodesIndices( iQuadFace[ 1 ]);
2716 int i0, i1d, i2, i3d, i0t, i2t; // d-daigonal, t-top
2717 if (curNodes[ind1[ 0 ]] == curNodes[ind2[ 0 ]] &&
2718 curNodes[ind1[ 2 ]] == curNodes[ind2[ 2 ]]) {
2719 // stuck with 0-2 diagonal
2727 else if (curNodes[ind1[ 1 ]] == curNodes[ind2[ 3 ]] &&
2728 curNodes[ind1[ 3 ]] == curNodes[ind2[ 1 ]]) {
2729 // stuck with 1-3 diagonal
2741 uniqueNodes[ 0 ] = curNodes [ i0 ];
2742 uniqueNodes[ 1 ] = curNodes [ i1d ];
2743 uniqueNodes[ 2 ] = curNodes [ i3d ];
2744 uniqueNodes[ 3 ] = curNodes [ i0t ];
2747 SMDS_MeshElement* newElem = aMesh->AddVolume(curNodes[ i1d ],
2752 aMesh->SetMeshElementOnShape( newElem, aShapeId );
2755 else if (( nbTri == 2 && nbQuad == 3 ) || // merged (0-4)(1-5)
2756 ( nbTri == 4 && nbQuad == 2 )) { // merged (7-4)(1-5)
2757 // --------------------------------------------> prism
2758 // find 2 opposite triangles
2760 for ( iFace = 0; iFace + 1 < nbTri; iFace++ ) {
2761 if ( hexa.GetOppFaceIndex( iTriFace[ iFace ] ) == iTriFace[ iFace + 1 ]) {
2762 // find indices of kept and replaced nodes
2763 // and fill unique nodes of 2 opposite triangles
2764 const int *ind1 = hexa.GetFaceNodesIndices( iTriFace[ iFace ]);
2765 const int *ind2 = hexa.GetFaceNodesIndices( iTriFace[ iFace + 1 ]);
2766 const SMDS_MeshNode** hexanodes = hexa.GetNodes();
2767 // fill unique nodes
2770 for ( iCur = 0; iCur < 4 && isOk; iCur++ ) {
2771 const SMDS_MeshNode* n = curNodes[ind1[ iCur ]];
2772 const SMDS_MeshNode* nInit = hexanodes[ind1[ iCur ]];
2774 // iCur of a linked node of the opposite face (make normals co-directed):
2775 int iCurOpp = ( iCur == 1 || iCur == 3 ) ? 4 - iCur : iCur;
2776 // check that correspondent corners of triangles are linked
2777 if ( !hexa.IsLinked( ind1[ iCur ], ind2[ iCurOpp ] ))
2780 uniqueNodes[ iUnique ] = n;
2781 uniqueNodes[ iUnique + 3 ] = curNodes[ind2[ iCurOpp ]];
2790 } // if ( nbUniqueNodes == 6 && nbRepl == 4 )
2796 } // switch ( nbNodes )
2798 } // if ( nbNodes != nbUniqueNodes ) // some nodes stick
2801 aMesh->ChangeElementNodes( elem, uniqueNodes, nbUniqueNodes );
2803 rmElemIds.push_back( elem->GetID() );
2805 } // loop on elements
2807 // Remove equal nodes and bad elements
2809 Remove( rmNodeIds, true );
2810 Remove( rmElemIds, false );
2814 //=======================================================================
2815 //function : MergeEqualElements
2816 //purpose : Remove all but one of elements built on the same nodes.
2817 //=======================================================================
2819 void SMESH_MeshEditor::MergeEqualElements()
2821 SMESHDS_Mesh* aMesh = GetMeshDS();
2823 SMDS_EdgeIteratorPtr eIt = aMesh->edgesIterator();
2824 SMDS_FaceIteratorPtr fIt = aMesh->facesIterator();
2825 SMDS_VolumeIteratorPtr vIt = aMesh->volumesIterator();
2827 list< int > rmElemIds; // IDs of elems to remove
2829 for ( int iDim = 1; iDim <= 3; iDim++ ) {
2831 set< set <const SMDS_MeshElement*> > setOfNodeSet;
2835 const SMDS_MeshElement* elem = 0;
2837 if ( eIt->more() ) elem = eIt->next();
2838 } else if ( iDim == 2 ) {
2839 if ( fIt->more() ) elem = fIt->next();
2841 if ( vIt->more() ) elem = vIt->next();
2846 set <const SMDS_MeshElement*> nodeSet;
2847 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
2848 while ( nodeIt->more() )
2849 nodeSet.insert( nodeIt->next() );
2852 bool isUnique = setOfNodeSet.insert( nodeSet ).second;
2854 rmElemIds.push_back( elem->GetID() );
2858 Remove( rmElemIds, false );
2861 //=======================================================================
2862 //function : FindFaceInSet
2863 //purpose : Return a face having linked nodes n1 and n2 and which is
2864 // - not in avoidSet,
2865 // - in elemSet provided that !elemSet.empty()
2866 //=======================================================================
2868 const SMDS_MeshElement*
2869 SMESH_MeshEditor::FindFaceInSet(const SMDS_MeshNode* n1,
2870 const SMDS_MeshNode* n2,
2871 const set<const SMDS_MeshElement*>& elemSet,
2872 const set<const SMDS_MeshElement*>& avoidSet)
2875 SMDS_ElemIteratorPtr invElemIt = n1->GetInverseElementIterator();
2876 while ( invElemIt->more() ) { // loop on inverse elements of n1
2877 const SMDS_MeshElement* elem = invElemIt->next();
2878 if (elem->GetType() != SMDSAbs_Face ||
2879 avoidSet.find( elem ) != avoidSet.end() )
2881 if ( !elemSet.empty() && elemSet.find( elem ) == elemSet.end())
2883 // get face nodes and find index of n1
2884 int i1, nbN = elem->NbNodes(), iNode = 0;
2885 const SMDS_MeshNode* faceNodes[ nbN ], *n;
2886 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
2887 while ( nIt->more() ) {
2888 faceNodes[ iNode ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
2889 if ( faceNodes[ iNode++ ] == n1 )
2892 // find a n2 linked to n1
2893 for ( iNode = 0; iNode < 2; iNode++ ) {
2894 if ( iNode ) // node before n1
2895 n = faceNodes[ i1 == 0 ? nbN - 1 : i1 - 1 ];
2896 else // node after n1
2897 n = faceNodes[ i1 + 1 == nbN ? 0 : i1 + 1 ];
2905 //=======================================================================
2906 //function : findAdjacentFace
2908 //=======================================================================
2910 static const SMDS_MeshElement* findAdjacentFace(const SMDS_MeshNode* n1,
2911 const SMDS_MeshNode* n2,
2912 const SMDS_MeshElement* elem)
2914 set<const SMDS_MeshElement*> elemSet, avoidSet;
2916 avoidSet.insert ( elem );
2917 SMESH_MeshEditor::FindFaceInSet( n1, n2, elemSet, avoidSet );
2920 //=======================================================================
2921 //function : findFreeBorder
2923 //=======================================================================
2925 #define ControlFreeBorder SMESH::Controls::FreeEdges::IsFreeEdge
2927 static bool findFreeBorder (const SMDS_MeshNode* theFirstNode,
2928 const SMDS_MeshNode* theSecondNode,
2929 const SMDS_MeshNode* theLastNode,
2930 list< const SMDS_MeshNode* > & theNodes,
2931 list< const SMDS_MeshElement* > & theFaces)
2933 if ( !theFirstNode || !theSecondNode )
2935 // find border face between theFirstNode and theSecondNode
2936 const SMDS_MeshElement* curElem = findAdjacentFace( theFirstNode, theSecondNode, 0 );
2940 theFaces.push_back( curElem );
2941 theNodes.push_back( theFirstNode );
2942 theNodes.push_back( theSecondNode );
2944 const SMDS_MeshNode* nodes [5], *nIgnore = theFirstNode, * nStart = theSecondNode;
2945 set < const SMDS_MeshElement* > foundElems;
2946 bool needTheLast = ( theLastNode != 0 );
2948 while ( nStart != theLastNode )
2950 if ( nStart == theFirstNode )
2951 return !needTheLast;
2953 // find all free border faces sharing form nStart
2955 list< const SMDS_MeshElement* > curElemList;
2956 list< const SMDS_MeshNode* > nStartList;
2957 SMDS_ElemIteratorPtr invElemIt = nStart->facesIterator();
2958 while ( invElemIt->more() ) {
2959 const SMDS_MeshElement* e = invElemIt->next();
2960 if ( e == curElem || foundElems.insert( e ).second )
2963 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
2964 int iNode = 0, nbNodes = e->NbNodes();
2965 while ( nIt->more() )
2966 nodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
2967 nodes[ iNode ] = nodes[ 0 ];
2969 for ( iNode = 0; iNode < nbNodes; iNode++ )
2970 if (((nodes[ iNode ] == nStart && nodes[ iNode + 1] != nIgnore ) ||
2971 (nodes[ iNode + 1] == nStart && nodes[ iNode ] != nIgnore )) &&
2972 ControlFreeBorder( &nodes[ iNode ], e->GetID() ))
2974 nStartList.push_back( nodes[ iNode + ( nodes[ iNode ] == nStart ? 1 : 0 )]);
2975 curElemList.push_back( e );
2979 // analyse the found
2981 int nbNewBorders = curElemList.size();
2982 if ( nbNewBorders == 0 ) {
2983 // no free border furthermore
2984 return !needTheLast;
2986 else if ( nbNewBorders == 1 ) {
2987 // one more element found
2989 nStart = nStartList.front();
2990 curElem = curElemList.front();
2991 theFaces.push_back( curElem );
2992 theNodes.push_back( nStart );
2995 // several continuations found
2996 list< const SMDS_MeshElement* >::iterator curElemIt;
2997 list< const SMDS_MeshNode* >::iterator nStartIt;
2998 // check if one of them reached the last node
2999 if ( needTheLast ) {
3000 for (curElemIt = curElemList.begin(), nStartIt = nStartList.begin();
3001 curElemIt!= curElemList.end();
3002 curElemIt++, nStartIt++ )
3003 if ( *nStartIt == theLastNode ) {
3004 theFaces.push_back( *curElemIt );
3005 theNodes.push_back( *nStartIt );
3009 // find the best free border by the continuations
3010 list<const SMDS_MeshNode*> contNodes[ 2 ], *cNL;
3011 list<const SMDS_MeshElement*> contFaces[ 2 ], *cFL;
3012 for (curElemIt = curElemList.begin(), nStartIt = nStartList.begin();
3013 curElemIt!= curElemList.end();
3014 curElemIt++, nStartIt++ )
3016 cNL = & contNodes[ contNodes[0].empty() ? 0 : 1 ];
3017 cFL = & contFaces[ contFaces[0].empty() ? 0 : 1 ];
3018 // find one more free border
3019 if ( ! findFreeBorder( nIgnore, nStart, theLastNode, *cNL, *cFL )) {
3023 else if ( !contNodes[0].empty() && !contNodes[1].empty() ) {
3024 // choice: clear a worse one
3025 int iLongest = ( contNodes[0].size() < contNodes[1].size() ? 1 : 0 );
3026 int iWorse = ( needTheLast ? 1 - iLongest : iLongest );
3027 contNodes[ iWorse ].clear();
3028 contFaces[ iWorse ].clear();
3031 if ( contNodes[0].empty() && contNodes[1].empty() )
3034 // append the best free border
3035 cNL = & contNodes[ contNodes[0].empty() ? 1 : 0 ];
3036 cFL = & contFaces[ contFaces[0].empty() ? 1 : 0 ];
3037 theNodes.pop_back(); // remove nIgnore
3038 theNodes.pop_back(); // remove nStart
3039 theFaces.pop_back(); // remove curElem
3040 list< const SMDS_MeshNode* >::iterator nIt = cNL->begin();
3041 list< const SMDS_MeshElement* >::iterator fIt = cFL->begin();
3042 for ( ; nIt != cNL->end(); nIt++ ) theNodes.push_back( *nIt );
3043 for ( ; fIt != cFL->end(); fIt++ ) theFaces.push_back( *fIt );
3046 } // several continuations found
3047 } // while ( nStart != theLastNode )
3052 //=======================================================================
3053 //function : CheckFreeBorderNodes
3054 //purpose : Return true if the tree nodes are on a free border
3055 //=======================================================================
3057 bool SMESH_MeshEditor::CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
3058 const SMDS_MeshNode* theNode2,
3059 const SMDS_MeshNode* theNode3)
3061 list< const SMDS_MeshNode* > nodes;
3062 list< const SMDS_MeshElement* > faces;
3063 return findFreeBorder( theNode1, theNode2, theNode3, nodes, faces);
3066 //=======================================================================
3067 //function : SewFreeBorder
3069 //=======================================================================
3071 SMESH_MeshEditor::Sew_Error
3072 SMESH_MeshEditor::SewFreeBorder (const SMDS_MeshNode* theBordFirstNode,
3073 const SMDS_MeshNode* theBordSecondNode,
3074 const SMDS_MeshNode* theBordLastNode,
3075 const SMDS_MeshNode* theSideFirstNode,
3076 const SMDS_MeshNode* theSideSecondNode,
3077 const SMDS_MeshNode* theSideThirdNode,
3078 bool theSideIsFreeBorder)
3080 MESSAGE("::SewFreeBorder()");
3081 Sew_Error aResult = SEW_OK;
3083 // ====================================
3084 // find side nodes and elements
3085 // ====================================
3087 list< const SMDS_MeshNode* > nSide[ 2 ];
3088 list< const SMDS_MeshElement* > eSide[ 2 ];
3089 list< const SMDS_MeshNode* >::iterator nIt[ 2 ];
3090 list< const SMDS_MeshElement* >::iterator eIt[ 2 ];
3094 if (!findFreeBorder(theBordFirstNode,theBordSecondNode,theBordLastNode,
3095 nSide[0], eSide[0])) {
3096 MESSAGE(" Free Border 1 not found " );
3097 aResult = SEW_BORDER1_NOT_FOUND;
3099 if (theSideIsFreeBorder)
3103 if (!findFreeBorder(theSideFirstNode, theSideSecondNode, theSideThirdNode,
3104 nSide[1], eSide[1])) {
3105 MESSAGE(" Free Border 2 not found " );
3106 aResult = ( aResult != SEW_OK ? SEW_BOTH_BORDERS_NOT_FOUND : SEW_BORDER2_NOT_FOUND );
3109 if ( aResult != SEW_OK )
3112 if (!theSideIsFreeBorder)
3117 // -------------------------------------------------------------------------
3119 // 1. If nodes to merge are not coincident, move nodes of the free border
3120 // from the coord sys defined by the direction from the first to last
3121 // nodes of the border to the correspondent sys of the side 2
3122 // 2. On the side 2, find the links most co-directed with the correspondent
3123 // links of the free border
3124 // -------------------------------------------------------------------------
3126 // 1. Since sewing may brake if there are volumes to split on the side 2,
3127 // we wont move nodes but just compute new coordinates for them
3128 typedef map<const SMDS_MeshNode*, gp_XYZ> TNodeXYZMap;
3129 TNodeXYZMap nBordXYZ;
3130 list< const SMDS_MeshNode* >& bordNodes = nSide[ 0 ];
3131 list< const SMDS_MeshNode* >::iterator nBordIt;
3133 gp_XYZ Pb1( theBordFirstNode->X(), theBordFirstNode->Y(), theBordFirstNode->Z() );
3134 gp_XYZ Pb2( theBordLastNode->X(), theBordLastNode->Y(), theBordLastNode->Z() );
3135 gp_XYZ Ps1( theSideFirstNode->X(), theSideFirstNode->Y(), theSideFirstNode->Z() );
3136 gp_XYZ Ps2( theSideSecondNode->X(), theSideSecondNode->Y(), theSideSecondNode->Z() );
3137 double tol2 = 1.e-8;
3138 gp_Vec Vbs1( Pb1 - Ps1 ),Vbs2( Pb2 - Ps2 );
3139 if ( Vbs1.SquareMagnitude() > tol2 || Vbs2.SquareMagnitude() > tol2 )
3141 // Need node movement.
3143 // find X and Z axes to create trsf
3144 gp_Vec Zb( Pb1 - Pb2 ), Zs( Ps1 - Ps2 );
3146 if ( X.SquareMagnitude() <= gp::Resolution() * gp::Resolution() )
3148 X = gp_Ax2( gp::Origin(), Zb ).XDirection();
3151 gp_Ax3 toBordAx( Pb1, Zb, X );
3152 gp_Ax3 fromSideAx( Ps1, Zs, X );
3153 gp_Ax3 toGlobalAx( gp::Origin(), gp::DZ(), gp::DX() );
3155 gp_Trsf toBordSys, fromSide2Sys;
3156 toBordSys.SetTransformation( toBordAx );
3157 fromSide2Sys.SetTransformation( fromSideAx, toGlobalAx );
3158 fromSide2Sys.SetScaleFactor( Zs.Magnitude() / Zb.Magnitude() );
3161 for ( nBordIt = bordNodes.begin(); nBordIt != bordNodes.end(); nBordIt++ ) {
3162 const SMDS_MeshNode* n = *nBordIt;
3163 gp_XYZ xyz( n->X(),n->Y(),n->Z() );
3164 toBordSys.Transforms( xyz );
3165 fromSide2Sys.Transforms( xyz );
3166 nBordXYZ.insert( TNodeXYZMap::value_type( n, xyz ));
3171 // just insert nodes XYZ in the nBordXYZ map
3172 for ( nBordIt = bordNodes.begin(); nBordIt != bordNodes.end(); nBordIt++ ) {
3173 const SMDS_MeshNode* n = *nBordIt;
3174 nBordXYZ.insert( TNodeXYZMap::value_type( n, gp_XYZ( n->X(),n->Y(),n->Z() )));
3178 // 2. On the side 2, find the links most co-directed with the correspondent
3179 // links of the free border
3181 list< const SMDS_MeshElement* >& sideElems = eSide[ 1 ];
3182 list< const SMDS_MeshNode* >& sideNodes = nSide[ 1 ];
3183 sideNodes.push_back( theSideFirstNode );
3185 bool hasVolumes = false;
3186 LinkID_Gen aLinkID_Gen( GetMeshDS() );
3187 set<long> foundSideLinkIDs, checkedLinkIDs;
3188 SMDS_VolumeTool volume;
3189 const SMDS_MeshNode* faceNodes[ 4 ];
3191 const SMDS_MeshNode* sideNode;
3192 const SMDS_MeshElement* sideElem;
3193 const SMDS_MeshNode* prevSideNode = theSideFirstNode;
3194 const SMDS_MeshNode* prevBordNode = theBordFirstNode;
3195 nBordIt = bordNodes.begin();
3197 // border node position and border link direction to compare with
3198 gp_XYZ bordPos = nBordXYZ[ *nBordIt ];
3199 gp_XYZ bordDir = bordPos - nBordXYZ[ prevBordNode ];
3200 // choose next side node by link direction or by closeness to
3201 // the current border node:
3202 bool searchByDir = ( *nBordIt != theBordLastNode );
3204 // find the next node on the Side 2
3206 double maxDot = -DBL_MAX, minDist = DBL_MAX;
3208 checkedLinkIDs.clear();
3209 gp_XYZ prevXYZ( prevSideNode->X(), prevSideNode->Y(), prevSideNode->Z() );
3211 SMDS_ElemIteratorPtr invElemIt
3212 = prevSideNode->GetInverseElementIterator();
3213 while ( invElemIt->more() ) { // loop on inverse elements on the Side 2
3214 const SMDS_MeshElement* elem = invElemIt->next();
3215 // prepare data for a loop on links, of a face or a volume
3216 int iPrevNode, iNode = 0, nbNodes = elem->NbNodes();
3217 bool isVolume = volume.Set( elem );
3218 const SMDS_MeshNode** nodes = isVolume ? volume.GetNodes() : faceNodes;
3219 if ( isVolume ) // --volume
3221 else if ( nbNodes > 2 ) { // --face
3222 // retrieve all face nodes and find iPrevNode - an index of the prevSideNode
3223 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
3224 while ( nIt->more() ) {
3225 nodes[ iNode ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
3226 if ( nodes[ iNode++ ] == prevSideNode )
3227 iPrevNode = iNode - 1;
3229 // there are 2 links to check
3234 // loop on links, to be precise, on the second node of links
3235 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
3236 const SMDS_MeshNode* n = nodes[ iNode ];
3238 if ( !volume.IsLinked( n, prevSideNode ))
3241 if ( iNode ) // a node before prevSideNode
3242 n = nodes[ iPrevNode == 0 ? elem->NbNodes() - 1 : iPrevNode - 1 ];
3243 else // a node after prevSideNode
3244 n = nodes[ iPrevNode + 1 == elem->NbNodes() ? 0 : iPrevNode + 1 ];
3246 // check if this link was already used
3247 long iLink = aLinkID_Gen.GetLinkID( prevSideNode, n );
3248 bool isJustChecked = !checkedLinkIDs.insert( iLink ).second;
3249 if (!isJustChecked &&
3250 foundSideLinkIDs.find( iLink ) == foundSideLinkIDs.end() ) {
3251 // test a link geometrically
3252 gp_XYZ nextXYZ ( n->X(), n->Y(), n->Z() );
3253 bool linkIsBetter = false;
3255 if ( searchByDir ) { // choose most co-directed link
3256 dot = bordDir * ( nextXYZ - prevXYZ ).Normalized();
3257 linkIsBetter = ( dot > maxDot );
3259 else { // choose link with the node closest to bordPos
3260 dist = ( nextXYZ - bordPos ).SquareModulus();
3261 linkIsBetter = ( dist < minDist );
3263 if ( linkIsBetter ) {
3272 } // loop on inverse elements of prevSideNode
3275 MESSAGE(" Cant find path by links of the Side 2 ");
3276 return SEW_BAD_SIDE_NODES;
3278 sideNodes.push_back( sideNode );
3279 sideElems.push_back( sideElem );
3280 foundSideLinkIDs.insert ( linkID );
3281 prevSideNode = sideNode;
3283 if ( *nBordIt == theBordLastNode )
3284 searchByDir = false;
3286 // find the next border link to compare with
3287 gp_XYZ sidePos( sideNode->X(), sideNode->Y(), sideNode->Z() );
3288 searchByDir = ( bordDir * ( sidePos - bordPos ) <= 0 );
3289 while ( *nBordIt != theBordLastNode && !searchByDir ) {
3290 prevBordNode = *nBordIt;
3292 bordPos = nBordXYZ[ *nBordIt ];
3293 bordDir = bordPos - nBordXYZ[ prevBordNode ];
3294 searchByDir = ( bordDir * ( sidePos - bordPos ) <= 0 );
3298 while ( sideNode != theSideSecondNode );
3300 if ( hasVolumes && sideNodes.size () != bordNodes.size() ) {
3301 MESSAGE("VOLUME SPLITTING IS FORBIDDEN");
3302 return SEW_VOLUMES_TO_SPLIT; // volume splitting is forbidden
3304 } // end nodes search on the side 2
3306 // ============================
3307 // sew the border to the side 2
3308 // ============================
3310 int nbNodes[] = { nSide[0].size(), nSide[1].size() };
3311 int maxNbNodes = Max( nbNodes[0], nbNodes[1] );
3313 TListOfListOfNodes nodeGroupsToMerge;
3314 if ( nbNodes[0] == nbNodes[1] ||
3315 ( theSideIsFreeBorder && !theSideThirdNode)) {
3317 // all nodes are to be merged
3319 for (nIt[0] = nSide[0].begin(), nIt[1] = nSide[1].begin();
3320 nIt[0] != nSide[0].end() && nIt[1] != nSide[1].end();
3321 nIt[0]++, nIt[1]++ )
3323 nodeGroupsToMerge.push_back( list<const SMDS_MeshNode*>() );
3324 nodeGroupsToMerge.back().push_back( *nIt[1] ); // to keep
3325 nodeGroupsToMerge.back().push_back( *nIt[0] ); // tp remove
3330 // insert new nodes into the border and the side to get equal nb of segments
3332 // get normalized parameters of nodes on the borders
3333 double param[ 2 ][ maxNbNodes ];
3335 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
3336 list< const SMDS_MeshNode* >& nodes = nSide[ iBord ];
3337 list< const SMDS_MeshNode* >::iterator nIt = nodes.begin();
3338 const SMDS_MeshNode* nPrev = *nIt;
3339 double bordLength = 0;
3340 for ( iNode = 0; nIt != nodes.end(); nIt++, iNode++ ) { // loop on border nodes
3341 const SMDS_MeshNode* nCur = *nIt;
3342 gp_XYZ segment (nCur->X() - nPrev->X(),
3343 nCur->Y() - nPrev->Y(),
3344 nCur->Z() - nPrev->Z());
3345 double segmentLen = segment.Modulus();
3346 bordLength += segmentLen;
3347 param[ iBord ][ iNode ] = bordLength;
3350 // normalize within [0,1]
3351 for ( iNode = 0; iNode < nbNodes[ iBord ]; iNode++ ) {
3352 param[ iBord ][ iNode ] /= bordLength;
3356 // loop on border segments
3357 const SMDS_MeshNode *nPrev[ 2 ] = { 0, 0 };
3358 int i[ 2 ] = { 0, 0 };
3359 nIt[0] = nSide[0].begin(); eIt[0] = eSide[0].begin();
3360 nIt[1] = nSide[1].begin(); eIt[1] = eSide[1].begin();
3362 TElemOfNodeListMap insertMap;
3363 TElemOfNodeListMap::iterator insertMapIt;
3365 // key: elem to insert nodes into
3366 // value: 2 nodes to insert between + nodes to be inserted
3368 bool next[ 2 ] = { false, false };
3370 // find min adjacent segment length after sewing
3371 double nextParam = 10., prevParam = 0;
3372 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
3373 if ( i[ iBord ] + 1 < nbNodes[ iBord ])
3374 nextParam = Min( nextParam, param[iBord][ i[iBord] + 1 ]);
3375 if ( i[ iBord ] > 0 )
3376 prevParam = Max( prevParam, param[iBord][ i[iBord] - 1 ]);
3378 double minParam = Min( param[ 0 ][ i[0] ], param[ 1 ][ i[1] ]);
3379 double maxParam = Max( param[ 0 ][ i[0] ], param[ 1 ][ i[1] ]);
3380 double minSegLen = Min( nextParam - minParam, maxParam - prevParam );
3382 // choose to insert or to merge nodes
3383 double du = param[ 1 ][ i[1] ] - param[ 0 ][ i[0] ];
3384 if ( Abs( du ) <= minSegLen * 0.2 ) {
3387 nodeGroupsToMerge.push_back( list<const SMDS_MeshNode*>() );
3388 const SMDS_MeshNode* n0 = *nIt[0];
3389 const SMDS_MeshNode* n1 = *nIt[1];
3390 nodeGroupsToMerge.back().push_back( n1 );
3391 nodeGroupsToMerge.back().push_back( n0 );
3392 // position of node of the border changes due to merge
3393 param[ 0 ][ i[0] ] += du;
3394 // move n1 for the sake of elem shape evaluation during insertion.
3395 // n1 will be removed by MergeNodes() anyway
3396 const_cast<SMDS_MeshNode*>( n0 )->setXYZ( n1->X(), n1->Y(), n1->Z() );
3397 next[0] = next[1] = true;
3402 int intoBord = ( du < 0 ) ? 0 : 1;
3403 const SMDS_MeshElement* elem = *eIt[ intoBord ];
3404 const SMDS_MeshNode* n1 = nPrev[ intoBord ];
3405 const SMDS_MeshNode* n2 = *nIt[ intoBord ];
3406 const SMDS_MeshNode* nIns = *nIt[ 1 - intoBord ];
3407 if ( intoBord == 1 ) {
3408 // move node of the border to be on a link of elem of the side
3409 gp_XYZ p1 (n1->X(), n1->Y(), n1->Z());
3410 gp_XYZ p2 (n2->X(), n2->Y(), n2->Z());
3411 double ratio = du / ( param[ 1 ][ i[1] ] - param[ 1 ][ i[1]-1 ]);
3412 gp_XYZ p = p2 * ( 1 - ratio ) + p1 * ratio;
3413 GetMeshDS()->MoveNode( nIns, p.X(), p.Y(), p.Z() );
3415 insertMapIt = insertMap.find( elem );
3416 bool notFound = ( insertMapIt == insertMap.end() );
3417 bool otherLink = ( !notFound && (*insertMapIt).second.front() != n1 );
3419 // insert into another link of the same element:
3420 // 1. perform insertion into the other link of the elem
3421 list<const SMDS_MeshNode*> & nodeList = (*insertMapIt).second;
3422 const SMDS_MeshNode* n12 = nodeList.front(); nodeList.pop_front();
3423 const SMDS_MeshNode* n22 = nodeList.front(); nodeList.pop_front();
3424 InsertNodesIntoLink( elem, n12, n22, nodeList );
3425 // 2. perform insertion into the link of adjacent faces
3427 const SMDS_MeshElement* adjElem = findAdjacentFace( n12, n22, elem );
3429 InsertNodesIntoLink( adjElem, n12, n22, nodeList );
3433 // 3. find an element appeared on n1 and n2 after the insertion
3434 insertMap.erase( elem );
3435 elem = findAdjacentFace( n1, n2, 0 );
3437 if ( notFound || otherLink ) {
3438 // add element and nodes of the side into the insertMap
3439 insertMapIt = insertMap.insert
3440 ( TElemOfNodeListMap::value_type( elem, list<const SMDS_MeshNode*>() )).first;
3441 (*insertMapIt).second.push_back( n1 );
3442 (*insertMapIt).second.push_back( n2 );
3444 // add node to be inserted into elem
3445 (*insertMapIt).second.push_back( nIns );
3446 next[ 1 - intoBord ] = true;
3449 // go to the next segment
3450 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
3451 if ( next[ iBord ] ) {
3452 if ( i[ iBord ] != 0 && eIt[ iBord ] != eSide[ iBord ].end())
3454 nPrev[ iBord ] = *nIt[ iBord ];
3455 nIt[ iBord ]++; i[ iBord ]++;
3459 while ( nIt[0] != nSide[0].end() && nIt[1] != nSide[1].end());
3461 // perform insertion of nodes into elements
3463 for (insertMapIt = insertMap.begin();
3464 insertMapIt != insertMap.end();
3467 const SMDS_MeshElement* elem = (*insertMapIt).first;
3468 list<const SMDS_MeshNode*> & nodeList = (*insertMapIt).second;
3469 const SMDS_MeshNode* n1 = nodeList.front(); nodeList.pop_front();
3470 const SMDS_MeshNode* n2 = nodeList.front(); nodeList.pop_front();
3472 InsertNodesIntoLink( elem, n1, n2, nodeList );
3474 if ( !theSideIsFreeBorder ) {
3475 // look for and insert nodes into the faces adjacent to elem
3477 const SMDS_MeshElement* adjElem = findAdjacentFace( n1, n2, elem );
3479 InsertNodesIntoLink( adjElem, n1, n2, nodeList );
3486 } // end: insert new nodes
3488 MergeNodes ( nodeGroupsToMerge );
3493 //=======================================================================
3494 //function : InsertNodesIntoLink
3495 //purpose : insert theNodesToInsert into theFace between theBetweenNode1
3496 // and theBetweenNode2 and split theElement
3497 //=======================================================================
3499 void SMESH_MeshEditor::InsertNodesIntoLink(const SMDS_MeshElement* theFace,
3500 const SMDS_MeshNode* theBetweenNode1,
3501 const SMDS_MeshNode* theBetweenNode2,
3502 list<const SMDS_MeshNode*>& theNodesToInsert)
3504 if ( theFace->GetType() != SMDSAbs_Face ) return;
3506 // find indices of 2 link nodes and of the rest nodes
3507 int iNode = 0, il1, il2, i3, i4;
3508 il1 = il2 = i3 = i4 = -1;
3509 const SMDS_MeshNode* nodes[ 8 ];
3510 SMDS_ElemIteratorPtr nodeIt = theFace->nodesIterator();
3511 while ( nodeIt->more() ) {
3512 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3513 if ( n == theBetweenNode1 )
3515 else if ( n == theBetweenNode2 )
3521 nodes[ iNode++ ] = n;
3523 if ( il1 < 0 || il2 < 0 || i3 < 0 )
3526 // arrange link nodes to go one after another regarding the face orientation
3527 bool reverse = ( Abs( il2 - il1 ) == 1 ? il2 < il1 : il1 < il2 );
3532 theNodesToInsert.reverse();
3534 // check that not link nodes of a quadrangles are in good order
3535 int nbFaceNodes = theFace->NbNodes();
3536 if ( nbFaceNodes == 4 && i4 - i3 != 1 ) {
3542 // put theNodesToInsert between theBetweenNode1 and theBetweenNode2
3543 int nbLinkNodes = 2 + theNodesToInsert.size();
3544 const SMDS_MeshNode* linkNodes[ nbLinkNodes ];
3545 linkNodes[ 0 ] = nodes[ il1 ];
3546 linkNodes[ nbLinkNodes - 1 ] = nodes[ il2 ];
3547 list<const SMDS_MeshNode*>::iterator nIt = theNodesToInsert.begin();
3548 for ( iNode = 1; nIt != theNodesToInsert.end(); nIt++ ) {
3549 linkNodes[ iNode++ ] = *nIt;
3551 // decide how to split a quadrangle: compare possible variants
3552 // and choose which of splits to be a quadrangle
3553 int i1, i2, iSplit, nbSplits = nbLinkNodes - 1, iBestQuad;
3554 if ( nbFaceNodes == 3 )
3556 iBestQuad = nbSplits;
3559 else if ( nbFaceNodes == 4 )
3561 SMESH::Controls::NumericalFunctorPtr aCrit( new SMESH::Controls::AspectRatio);
3562 double aBestRate = DBL_MAX;
3563 for ( int iQuad = 0; iQuad < nbSplits; iQuad++ ) {
3565 double aBadRate = 0;
3566 // evaluate elements quality
3567 for ( iSplit = 0; iSplit < nbSplits; iSplit++ ) {
3568 if ( iSplit == iQuad ) {
3569 SMDS_FaceOfNodes quad (linkNodes[ i1++ ],
3573 aBadRate += getBadRate( &quad, aCrit );
3576 SMDS_FaceOfNodes tria (linkNodes[ i1++ ],
3578 nodes[ iSplit < iQuad ? i4 : i3 ]);
3579 aBadRate += getBadRate( &tria, aCrit );
3583 if ( aBadRate < aBestRate ) {
3585 aBestRate = aBadRate;
3590 // create new elements
3591 SMESHDS_Mesh *aMesh = GetMeshDS();
3592 int aShapeId = FindShape( theFace );
3595 for ( iSplit = 0; iSplit < nbSplits - 1; iSplit++ ) {
3596 SMDS_MeshElement* newElem = 0;
3597 if ( iSplit == iBestQuad )
3598 newElem = aMesh->AddFace (linkNodes[ i1++ ],
3603 newElem = aMesh->AddFace (linkNodes[ i1++ ],
3605 nodes[ iSplit < iBestQuad ? i4 : i3 ]);
3606 if ( aShapeId && newElem )
3607 aMesh->SetMeshElementOnShape( newElem, aShapeId );
3610 // change nodes of theFace
3611 const SMDS_MeshNode* newNodes[ 4 ];
3612 newNodes[ 0 ] = linkNodes[ i1 ];
3613 newNodes[ 1 ] = linkNodes[ i2 ];
3614 newNodes[ 2 ] = nodes[ iSplit >= iBestQuad ? i3 : i4 ];
3615 newNodes[ 3 ] = nodes[ i4 ];
3616 aMesh->ChangeElementNodes( theFace, newNodes, iSplit == iBestQuad ? 4 : 3 );
3619 //=======================================================================
3620 //function : SewSideElements
3622 //=======================================================================
3624 SMESH_MeshEditor::Sew_Error
3625 SMESH_MeshEditor::SewSideElements (set<const SMDS_MeshElement*>& theSide1,
3626 set<const SMDS_MeshElement*>& theSide2,
3627 const SMDS_MeshNode* theFirstNode1,
3628 const SMDS_MeshNode* theFirstNode2,
3629 const SMDS_MeshNode* theSecondNode1,
3630 const SMDS_MeshNode* theSecondNode2)
3632 MESSAGE ("::::SewSideElements()");
3633 if ( theSide1.size() != theSide2.size() )
3634 return SEW_DIFF_NB_OF_ELEMENTS;
3636 Sew_Error aResult = SEW_OK;
3638 // 1. Build set of faces representing each side
3639 // 2. Find which nodes of the side 1 to merge with ones on the side 2
3640 // 3. Replace nodes in elements of the side 1 and remove replaced nodes
3642 // =======================================================================
3643 // 1. Build set of faces representing each side:
3644 // =======================================================================
3645 // a. build set of nodes belonging to faces
3646 // b. complete set of faces: find missing fices whose nodes are in set of nodes
3647 // c. create temporary faces representing side of volumes if correspondent
3648 // face does not exist
3650 SMESHDS_Mesh* aMesh = GetMeshDS();
3651 SMDS_Mesh aTmpFacesMesh;
3652 set<const SMDS_MeshElement*> faceSet1, faceSet2;
3653 set<const SMDS_MeshElement*> volSet1, volSet2;
3654 set<const SMDS_MeshNode*> nodeSet1, nodeSet2;
3655 set<const SMDS_MeshElement*> * faceSetPtr[] = { &faceSet1, &faceSet2 };
3656 set<const SMDS_MeshElement*> * volSetPtr[] = { &volSet1, &volSet2 };
3657 set<const SMDS_MeshNode*> * nodeSetPtr[] = { &nodeSet1, &nodeSet2 };
3658 set<const SMDS_MeshElement*> * elemSetPtr[] = { &theSide1, &theSide2 };
3659 int iSide, iFace, iNode;
3661 for ( iSide = 0; iSide < 2; iSide++ ) {
3662 set<const SMDS_MeshNode*> * nodeSet = nodeSetPtr[ iSide ];
3663 set<const SMDS_MeshElement*> * elemSet = elemSetPtr[ iSide ];
3664 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
3665 set<const SMDS_MeshElement*> * volSet = volSetPtr [ iSide ];
3666 set<const SMDS_MeshElement*>::iterator vIt, eIt;
3667 set<const SMDS_MeshNode*>::iterator nIt;
3669 // -----------------------------------------------------------
3670 // 1a. Collect nodes of existing faces
3671 // and build set of face nodes in order to detect missing
3672 // faces corresponing to sides of volumes
3673 // -----------------------------------------------------------
3675 set< set <const SMDS_MeshNode*> > setOfFaceNodeSet;
3677 // loop on the given element of a side
3678 for (eIt = elemSet->begin(); eIt != elemSet->end(); eIt++ ) {
3679 const SMDS_MeshElement* elem = *eIt;
3680 if ( elem->GetType() == SMDSAbs_Face ) {
3681 faceSet->insert( elem );
3682 set <const SMDS_MeshNode*> faceNodeSet;
3683 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
3684 while ( nodeIt->more() ) {
3685 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3686 nodeSet->insert( n );
3687 faceNodeSet.insert( n );
3689 setOfFaceNodeSet.insert( faceNodeSet );
3691 else if ( elem->GetType() == SMDSAbs_Volume )
3692 volSet->insert( elem );
3694 // ------------------------------------------------------------------------------
3695 // 1b. Complete set of faces: find missing fices whose nodes are in set of nodes
3696 // ------------------------------------------------------------------------------
3698 for ( nIt = nodeSet->begin(); nIt != nodeSet->end(); nIt++ ) { // loop on nodes of iSide
3699 SMDS_ElemIteratorPtr fIt = (*nIt)->facesIterator();
3700 while ( fIt->more() ) { // loop on faces sharing a node
3701 const SMDS_MeshElement* f = fIt->next();
3702 if ( faceSet->find( f ) == faceSet->end() ) {
3703 // check if all nodes are in nodeSet and
3704 // complete setOfFaceNodeSet if they are
3705 set <const SMDS_MeshNode*> faceNodeSet;
3706 SMDS_ElemIteratorPtr nodeIt = f->nodesIterator();
3707 bool allInSet = true;
3708 while ( nodeIt->more() && allInSet ) { // loop on nodes of a face
3709 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3710 if ( nodeSet->find( n ) == nodeSet->end() )
3713 faceNodeSet.insert( n );
3716 faceSet->insert( f );
3717 setOfFaceNodeSet.insert( faceNodeSet );
3723 // -------------------------------------------------------------------------
3724 // 1c. Create temporary faces representing sides of volumes if correspondent
3725 // face does not exist
3726 // -------------------------------------------------------------------------
3728 if ( !volSet->empty() )
3730 //int nodeSetSize = nodeSet->size();
3732 // loop on given volumes
3733 for ( vIt = volSet->begin(); vIt != volSet->end(); vIt++ ) {
3734 SMDS_VolumeTool vol (*vIt);
3735 // loop on volume faces: find free faces
3736 // --------------------------------------
3737 list<const SMDS_MeshElement* > freeFaceList;
3738 for ( iFace = 0; iFace < vol.NbFaces(); iFace++ ) {
3739 if ( !vol.IsFreeFace( iFace ))
3741 // check if there is already a face with same nodes in a face set
3742 const SMDS_MeshElement* aFreeFace = 0;
3743 const SMDS_MeshNode** fNodes = vol.GetFaceNodes( iFace );
3744 int nbNodes = vol.NbFaceNodes( iFace );
3745 set <const SMDS_MeshNode*> faceNodeSet;
3746 vol.GetFaceNodes( iFace, faceNodeSet );
3747 bool isNewFace = setOfFaceNodeSet.insert( faceNodeSet ).second;
3749 // no such a face is given but it still can exist, check it
3751 aFreeFace = aMesh->FindFace( fNodes[0],fNodes[1],fNodes[2] );
3753 aFreeFace = aMesh->FindFace( fNodes[0],fNodes[1],fNodes[2],fNodes[3] );
3756 // create a temporary face
3758 aFreeFace = aTmpFacesMesh.AddFace( fNodes[0],fNodes[1],fNodes[2] );
3760 aFreeFace = aTmpFacesMesh.AddFace( fNodes[0],fNodes[1],fNodes[2],fNodes[3] );
3763 freeFaceList.push_back( aFreeFace );
3765 } // loop on faces of a volume
3767 // choose one of several free faces
3768 // --------------------------------------
3769 if ( freeFaceList.size() > 1 ) {
3770 // choose a face having max nb of nodes shared by other elems of a side
3771 int maxNbNodes = -1/*, nbExcludedFaces = 0*/;
3772 list<const SMDS_MeshElement* >::iterator fIt = freeFaceList.begin();
3773 while ( fIt != freeFaceList.end() ) { // loop on free faces
3774 int nbSharedNodes = 0;
3775 SMDS_ElemIteratorPtr nodeIt = (*fIt)->nodesIterator();
3776 while ( nodeIt->more() ) { // loop on free face nodes
3777 const SMDS_MeshNode* n =
3778 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3779 SMDS_ElemIteratorPtr invElemIt = n->GetInverseElementIterator();
3780 while ( invElemIt->more() ) {
3781 const SMDS_MeshElement* e = invElemIt->next();
3782 if ( faceSet->find( e ) != faceSet->end() )
3784 if ( elemSet->find( e ) != elemSet->end() )
3788 if ( nbSharedNodes >= maxNbNodes ) {
3789 maxNbNodes = nbSharedNodes;
3793 freeFaceList.erase( fIt++ ); // here fIt++ occures before erase
3795 if ( freeFaceList.size() > 1 )
3797 // could not choose one face, use another way
3798 // choose a face most close to the bary center of the opposite side
3799 gp_XYZ aBC( 0., 0., 0. );
3800 set <const SMDS_MeshNode*> addedNodes;
3801 set<const SMDS_MeshElement*> * elemSet2 = elemSetPtr[ 1 - iSide ];
3802 eIt = elemSet2->begin();
3803 for ( eIt = elemSet2->begin(); eIt != elemSet2->end(); eIt++ ) {
3804 SMDS_ElemIteratorPtr nodeIt = (*eIt)->nodesIterator();
3805 while ( nodeIt->more() ) { // loop on free face nodes
3806 const SMDS_MeshNode* n =
3807 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3808 if ( addedNodes.insert( n ).second )
3809 aBC += gp_XYZ( n->X(),n->Y(),n->Z() );
3812 aBC /= addedNodes.size();
3813 double minDist = DBL_MAX;
3814 fIt = freeFaceList.begin();
3815 while ( fIt != freeFaceList.end() ) { // loop on free faces
3817 SMDS_ElemIteratorPtr nodeIt = (*fIt)->nodesIterator();
3818 while ( nodeIt->more() ) { // loop on free face nodes
3819 const SMDS_MeshNode* n =
3820 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3821 gp_XYZ p( n->X(),n->Y(),n->Z() );
3822 dist += ( aBC - p ).SquareModulus();
3824 if ( dist < minDist ) {
3826 freeFaceList.erase( freeFaceList.begin(), fIt++ );
3829 fIt = freeFaceList.erase( fIt++ );
3832 } // choose one of several free faces of a volume
3834 if ( freeFaceList.size() == 1 ) {
3835 const SMDS_MeshElement* aFreeFace = freeFaceList.front();
3836 faceSet->insert( aFreeFace );
3837 // complete a node set with nodes of a found free face
3838 // for ( iNode = 0; iNode < ; iNode++ )
3839 // nodeSet->insert( fNodes[ iNode ] );
3842 } // loop on volumes of a side
3844 // // complete a set of faces if new nodes in a nodeSet appeared
3845 // // ----------------------------------------------------------
3846 // if ( nodeSetSize != nodeSet->size() ) {
3847 // for ( ; nIt != nodeSet->end(); nIt++ ) { // loop on nodes of iSide
3848 // SMDS_ElemIteratorPtr fIt = (*nIt)->facesIterator();
3849 // while ( fIt->more() ) { // loop on faces sharing a node
3850 // const SMDS_MeshElement* f = fIt->next();
3851 // if ( faceSet->find( f ) == faceSet->end() ) {
3852 // // check if all nodes are in nodeSet and
3853 // // complete setOfFaceNodeSet if they are
3854 // set <const SMDS_MeshNode*> faceNodeSet;
3855 // SMDS_ElemIteratorPtr nodeIt = f->nodesIterator();
3856 // bool allInSet = true;
3857 // while ( nodeIt->more() && allInSet ) { // loop on nodes of a face
3858 // const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
3859 // if ( nodeSet->find( n ) == nodeSet->end() )
3860 // allInSet = false;
3862 // faceNodeSet.insert( n );
3864 // if ( allInSet ) {
3865 // faceSet->insert( f );
3866 // setOfFaceNodeSet.insert( faceNodeSet );
3872 } // Create temporary faces, if there are volumes given
3875 if ( faceSet1.size() != faceSet2.size() ) {
3876 // delete temporary faces: they are in reverseElements of actual nodes
3877 SMDS_FaceIteratorPtr tmpFaceIt = aTmpFacesMesh.facesIterator();
3878 while ( tmpFaceIt->more() )
3879 aTmpFacesMesh.RemoveElement( tmpFaceIt->next() );
3880 MESSAGE("Diff nb of faces");
3881 return SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
3884 // ============================================================
3885 // 2. Find nodes to merge:
3886 // bind a node to remove to a node to put instead
3887 // ============================================================
3889 TNodeNodeMap nReplaceMap; // bind a node to remove to a node to put instead
3890 if ( theFirstNode1 != theFirstNode2 )
3891 nReplaceMap.insert( TNodeNodeMap::value_type( theFirstNode1, theFirstNode2 ));
3892 if ( theSecondNode1 != theSecondNode2 )
3893 nReplaceMap.insert( TNodeNodeMap::value_type( theSecondNode1, theSecondNode2 ));
3895 LinkID_Gen aLinkID_Gen( GetMeshDS() );
3896 set< long > linkIdSet; // links to process
3897 linkIdSet.insert( aLinkID_Gen.GetLinkID( theFirstNode1, theSecondNode1 ));
3899 typedef pair< const SMDS_MeshNode*, const SMDS_MeshNode* > TPairOfNodes;
3900 list< TPairOfNodes > linkList[2];
3901 linkList[0].push_back( TPairOfNodes( theFirstNode1, theSecondNode1 ));
3902 linkList[1].push_back( TPairOfNodes( theFirstNode2, theSecondNode2 ));
3903 // loop on links in linkList; find faces by links and append links
3904 // of the found faces to linkList
3905 list< TPairOfNodes >::iterator linkIt[] = { linkList[0].begin(), linkList[1].begin() } ;
3906 for ( ; linkIt[0] != linkList[0].end(); linkIt[0]++, linkIt[1]++ )
3908 TPairOfNodes link[] = { *linkIt[0], *linkIt[1] };
3909 long linkID = aLinkID_Gen.GetLinkID( link[0].first, link[0].second );
3910 if ( linkIdSet.find( linkID ) == linkIdSet.end() )
3913 // by links, find faces in the face sets,
3914 // and find indices of link nodes in the found faces;
3915 // in a face set, there is only one or no face sharing a link
3916 // ---------------------------------------------------------------
3918 const SMDS_MeshElement* face[] = { 0, 0 };
3919 const SMDS_MeshNode* faceNodes[ 2 ][ 5 ];
3920 const SMDS_MeshNode* notLinkNodes[ 2 ][ 2 ] = {{ 0, 0 },{ 0, 0 }} ;
3921 int iLinkNode[2][2];
3922 for ( iSide = 0; iSide < 2; iSide++ ) { // loop on 2 sides
3923 const SMDS_MeshNode* n1 = link[iSide].first;
3924 const SMDS_MeshNode* n2 = link[iSide].second;
3925 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
3926 set< const SMDS_MeshElement* > fMap;
3927 for ( int i = 0; i < 2; i++ ) { // loop on 2 nodes of a link
3928 const SMDS_MeshNode* n = i ? n1 : n2; // a node of a link
3929 SMDS_ElemIteratorPtr fIt = n->facesIterator();
3930 while ( fIt->more() ) { // loop on faces sharing a node
3931 const SMDS_MeshElement* f = fIt->next();
3932 if (faceSet->find( f ) != faceSet->end() && // f is in face set
3933 ! fMap.insert( f ).second ) // f encounters twice
3935 if ( face[ iSide ] ) {
3936 MESSAGE( "2 faces per link " );
3937 aResult = iSide ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES;
3941 faceSet->erase( f );
3942 // get face nodes and find ones of a link
3944 SMDS_ElemIteratorPtr nIt = f->nodesIterator();
3945 while ( nIt->more() ) {
3946 const SMDS_MeshNode* n =
3947 static_cast<const SMDS_MeshNode*>( nIt->next() );
3949 iLinkNode[ iSide ][ 0 ] = iNode;
3951 iLinkNode[ iSide ][ 1 ] = iNode;
3952 else if ( notLinkNodes[ iSide ][ 0 ] )
3953 notLinkNodes[ iSide ][ 1 ] = n;
3955 notLinkNodes[ iSide ][ 0 ] = n;
3956 faceNodes[ iSide ][ iNode++ ] = n;
3958 faceNodes[ iSide ][ iNode ] = faceNodes[ iSide ][ 0 ];
3963 // check similarity of elements of the sides
3964 if (aResult == SEW_OK && ( face[0] && !face[1] ) || ( !face[0] && face[1] )) {
3965 MESSAGE("Correspondent face not found on side " << ( face[0] ? 1 : 0 ));
3966 if ( nReplaceMap.size() == 2 ) // faces on input nodes not found
3967 aResult = ( face[0] ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES );
3969 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
3970 break; // do not return because it s necessary to remove tmp faces
3973 // set nodes to merge
3974 // -------------------
3976 if ( face[0] && face[1] )
3978 int nbNodes = face[0]->NbNodes();
3979 if ( nbNodes != face[1]->NbNodes() ) {
3980 MESSAGE("Diff nb of face nodes");
3981 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
3982 break; // do not return because it s necessary to remove tmp faces
3984 bool reverse[] = { false, false }; // order of notLinkNodes of quadrangle
3986 nReplaceMap.insert( TNodeNodeMap::value_type
3987 ( notLinkNodes[0][0], notLinkNodes[1][0] ));
3989 for ( iSide = 0; iSide < 2; iSide++ ) { // loop on 2 sides
3990 // analyse link orientation in faces
3991 int i1 = iLinkNode[ iSide ][ 0 ];
3992 int i2 = iLinkNode[ iSide ][ 1 ];
3993 reverse[ iSide ] = Abs( i1 - i2 ) == 1 ? i1 > i2 : i2 > i1;
3994 // if notLinkNodes are the first and the last ones, then
3995 // their order does not correspond to the link orientation
3996 if (( i1 == 1 && i2 == 2 ) ||
3997 ( i1 == 2 && i2 == 1 ))
3998 reverse[ iSide ] = !reverse[ iSide ];
4000 if ( reverse[0] == reverse[1] ) {
4001 nReplaceMap.insert( TNodeNodeMap::value_type
4002 ( notLinkNodes[0][0], notLinkNodes[1][0] ));
4003 nReplaceMap.insert( TNodeNodeMap::value_type
4004 ( notLinkNodes[0][1], notLinkNodes[1][1] ));
4007 nReplaceMap.insert( TNodeNodeMap::value_type
4008 ( notLinkNodes[0][0], notLinkNodes[1][1] ));
4009 nReplaceMap.insert( TNodeNodeMap::value_type
4010 ( notLinkNodes[0][1], notLinkNodes[1][0] ));
4014 // add other links of the faces to linkList
4015 // -----------------------------------------
4017 const SMDS_MeshNode** nodes = faceNodes[ 0 ];
4018 for ( iNode = 0; iNode < nbNodes; iNode++ )
4020 linkID = aLinkID_Gen.GetLinkID( nodes[iNode], nodes[iNode+1] );
4021 pair< set<long>::iterator, bool > iter_isnew = linkIdSet.insert( linkID );
4022 if ( !iter_isnew.second ) { // already in a set: no need to process
4023 linkIdSet.erase( iter_isnew.first );
4025 else // new in set == encountered for the first time: add
4027 const SMDS_MeshNode* n1 = nodes[ iNode ];
4028 const SMDS_MeshNode* n2 = nodes[ iNode + 1];
4029 linkList[0].push_back ( TPairOfNodes( n1, n2 ));
4030 linkList[1].push_back ( TPairOfNodes( nReplaceMap[n1], nReplaceMap[n2] ));
4034 } // loop on link lists
4036 if ( aResult == SEW_OK &&
4037 ( linkIt[0] != linkList[0].end() ||
4038 !faceSetPtr[0]->empty() || !faceSetPtr[1]->empty() )) {
4039 MESSAGE( (linkIt[0] != linkList[0].end()) <<" "<< (faceSetPtr[0]->empty()) <<
4040 " " << (faceSetPtr[1]->empty()));
4041 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
4044 // ====================================================================
4045 // 3. Replace nodes in elements of the side 1 and remove replaced nodes
4046 // ====================================================================
4048 // delete temporary faces: they are in reverseElements of actual nodes
4049 SMDS_FaceIteratorPtr tmpFaceIt = aTmpFacesMesh.facesIterator();
4050 while ( tmpFaceIt->more() )
4051 aTmpFacesMesh.RemoveElement( tmpFaceIt->next() );
4053 if ( aResult != SEW_OK)
4056 list< int > nodeIDsToRemove/*, elemIDsToRemove*/;
4057 // loop on nodes replacement map
4058 TNodeNodeMap::iterator nReplaceMapIt = nReplaceMap.begin(), nnIt;
4059 for ( ; nReplaceMapIt != nReplaceMap.end(); nReplaceMapIt++ )
4060 if ( (*nReplaceMapIt).first != (*nReplaceMapIt).second )
4062 const SMDS_MeshNode* nToRemove = (*nReplaceMapIt).first;
4063 nodeIDsToRemove.push_back( nToRemove->GetID() );
4064 // loop on elements sharing nToRemove
4065 SMDS_ElemIteratorPtr invElemIt = nToRemove->GetInverseElementIterator();
4066 while ( invElemIt->more() ) {
4067 const SMDS_MeshElement* e = invElemIt->next();
4068 // get a new suite of nodes: make replacement
4069 int nbReplaced = 0, i = 0, nbNodes = e->NbNodes();
4070 const SMDS_MeshNode* nodes[ 8 ];
4071 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
4072 while ( nIt->more() ) {
4073 const SMDS_MeshNode* n =
4074 static_cast<const SMDS_MeshNode*>( nIt->next() );
4075 nnIt = nReplaceMap.find( n );
4076 if ( nnIt != nReplaceMap.end() ) {
4082 // if ( nbReplaced == nbNodes && e->GetType() == SMDSAbs_Face )
4083 // elemIDsToRemove.push_back( e->GetID() );
4086 aMesh->ChangeElementNodes( e, nodes, nbNodes );
4090 Remove( nodeIDsToRemove, true );