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.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : SMESH_MeshEditor.cxx
25 // Created : Mon Apr 12 16:10:22 2004
26 // Author : Edward AGAPOV (eap)
29 #include "SMESH_MeshEditor.hxx"
31 #include "SMDS_FaceOfNodes.hxx"
32 #include "SMDS_VolumeTool.hxx"
33 #include "SMDS_EdgePosition.hxx"
34 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
35 #include "SMDS_FacePosition.hxx"
36 #include "SMDS_SpacePosition.hxx"
37 #include "SMDS_QuadraticFaceOfNodes.hxx"
39 #include "SMESHDS_Group.hxx"
40 #include "SMESHDS_Mesh.hxx"
42 #include "SMESH_subMesh.hxx"
43 #include "SMESH_ControlsDef.hxx"
44 #include "SMESH_MesherHelper.hxx"
45 #include "SMESH_OctreeNode.hxx"
47 #include "utilities.h"
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_ListOfShape.hxx>
55 #include <gp_Trsf.hxx>
61 #include <BRep_Tool.hxx>
62 #include <Geom_Curve.hxx>
63 #include <Geom_Surface.hxx>
64 #include <Geom2d_Curve.hxx>
65 #include <Extrema_GenExtPS.hxx>
66 #include <Extrema_POnSurf.hxx>
67 #include <GeomAdaptor_Surface.hxx>
69 #include <TColStd_ListOfInteger.hxx>
70 #include <TopoDS_Face.hxx>
76 using namespace SMESH::Controls;
78 typedef map<const SMDS_MeshElement*, list<const SMDS_MeshNode*> > TElemOfNodeListMap;
79 typedef map<const SMDS_MeshElement*, list<const SMDS_MeshElement*> > TElemOfElemListMap;
80 typedef map<const SMDS_MeshNode*, list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
81 typedef TNodeOfNodeListMap::iterator TNodeOfNodeListMapItr;
82 //typedef map<const SMDS_MeshNode*, vector<const SMDS_MeshNode*> > TNodeOfNodeVecMap;
83 //typedef TNodeOfNodeVecMap::iterator TNodeOfNodeVecMapItr;
84 typedef map<const SMDS_MeshElement*, vector<TNodeOfNodeListMapItr> > TElemOfVecOfNnlmiMap;
85 //typedef map<const SMDS_MeshElement*, vector<TNodeOfNodeVecMapItr> > TElemOfVecOfMapNodesMap;
87 typedef pair< const SMDS_MeshNode*, const SMDS_MeshNode* > NLink;
89 struct TNodeXYZ : public gp_XYZ {
90 TNodeXYZ( const SMDS_MeshNode* n ):gp_XYZ( n->X(), n->Y(), n->Z() ) {}
93 //=======================================================================
94 //function : SMESH_MeshEditor
96 //=======================================================================
98 SMESH_MeshEditor::SMESH_MeshEditor( SMESH_Mesh* theMesh ):
103 //=======================================================================
107 //=======================================================================
110 SMESH_MeshEditor::AddElement(const vector<const SMDS_MeshNode*> & node,
111 const SMDSAbs_ElementType type,
115 SMDS_MeshElement* e = 0;
116 int nbnode = node.size();
117 SMESHDS_Mesh* mesh = GetMeshDS();
121 if ( ID ) e = mesh->AddEdgeWithID(node[0], node[1], ID);
122 else e = mesh->AddEdge (node[0], node[1] );
123 else if ( nbnode == 3 )
124 if ( ID ) e = mesh->AddEdgeWithID(node[0], node[1], node[2], ID);
125 else e = mesh->AddEdge (node[0], node[1], node[2] );
130 if ( ID ) e = mesh->AddFaceWithID(node[0], node[1], node[2], ID);
131 else e = mesh->AddFace (node[0], node[1], node[2] );
132 else if (nbnode == 4)
133 if ( ID ) e = mesh->AddFaceWithID(node[0], node[1], node[2], node[3], ID);
134 else e = mesh->AddFace (node[0], node[1], node[2], node[3] );
135 else if (nbnode == 6)
136 if ( ID ) e = mesh->AddFaceWithID(node[0], node[1], node[2], node[3],
137 node[4], node[5], ID);
138 else e = mesh->AddFace (node[0], node[1], node[2], node[3],
140 else if (nbnode == 8)
141 if ( ID ) e = mesh->AddFaceWithID(node[0], node[1], node[2], node[3],
142 node[4], node[5], node[6], node[7], ID);
143 else e = mesh->AddFace (node[0], node[1], node[2], node[3],
144 node[4], node[5], node[6], node[7] );
146 if ( ID ) e = mesh->AddPolygonalFaceWithID(node, ID);
147 else e = mesh->AddPolygonalFace (node );
153 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3], ID);
154 else e = mesh->AddVolume (node[0], node[1], node[2], node[3] );
155 else if (nbnode == 5)
156 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
158 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
160 else if (nbnode == 6)
161 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
162 node[4], node[5], ID);
163 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
165 else if (nbnode == 8)
166 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
167 node[4], node[5], node[6], node[7], ID);
168 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
169 node[4], node[5], node[6], node[7] );
170 else if (nbnode == 10)
171 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
172 node[4], node[5], node[6], node[7],
173 node[8], node[9], ID);
174 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
175 node[4], node[5], node[6], node[7],
177 else if (nbnode == 13)
178 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
179 node[4], node[5], node[6], node[7],
180 node[8], node[9], node[10],node[11],
182 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
183 node[4], node[5], node[6], node[7],
184 node[8], node[9], node[10],node[11],
186 else if (nbnode == 15)
187 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
188 node[4], node[5], node[6], node[7],
189 node[8], node[9], node[10],node[11],
190 node[12],node[13],node[14],ID);
191 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
192 node[4], node[5], node[6], node[7],
193 node[8], node[9], node[10],node[11],
194 node[12],node[13],node[14] );
195 else if (nbnode == 20)
196 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
197 node[4], node[5], node[6], node[7],
198 node[8], node[9], node[10],node[11],
199 node[12],node[13],node[14],node[15],
200 node[16],node[17],node[18],node[19],ID);
201 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
202 node[4], node[5], node[6], node[7],
203 node[8], node[9], node[10],node[11],
204 node[12],node[13],node[14],node[15],
205 node[16],node[17],node[18],node[19] );
211 //=======================================================================
215 //=======================================================================
217 SMDS_MeshElement* SMESH_MeshEditor::AddElement(const vector<int> & nodeIDs,
218 const SMDSAbs_ElementType type,
222 vector<const SMDS_MeshNode*> nodes;
223 nodes.reserve( nodeIDs.size() );
224 vector<int>::const_iterator id = nodeIDs.begin();
225 while ( id != nodeIDs.end() ) {
226 if ( const SMDS_MeshNode* node = GetMeshDS()->FindNode( *id++ ))
227 nodes.push_back( node );
231 return AddElement( nodes, type, isPoly, ID );
234 //=======================================================================
236 //purpose : Remove a node or an element.
237 // Modify a compute state of sub-meshes which become empty
238 //=======================================================================
240 bool SMESH_MeshEditor::Remove (const list< int >& theIDs,
243 myLastCreatedElems.Clear();
244 myLastCreatedNodes.Clear();
246 SMESHDS_Mesh* aMesh = GetMeshDS();
247 set< SMESH_subMesh *> smmap;
249 list<int>::const_iterator it = theIDs.begin();
250 for ( ; it != theIDs.end(); it++ ) {
251 const SMDS_MeshElement * elem;
253 elem = aMesh->FindNode( *it );
255 elem = aMesh->FindElement( *it );
259 // Find sub-meshes to notify about modification
260 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
261 while ( nodeIt->more() ) {
262 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
263 const SMDS_PositionPtr& aPosition = node->GetPosition();
264 if ( aPosition.get() ) {
265 if ( int aShapeID = aPosition->GetShapeId() ) {
266 if ( SMESH_subMesh * sm = GetMesh()->GetSubMeshContaining( aShapeID ) )
274 aMesh->RemoveNode( static_cast< const SMDS_MeshNode* >( elem ));
276 aMesh->RemoveElement( elem );
279 // Notify sub-meshes about modification
280 if ( !smmap.empty() ) {
281 set< SMESH_subMesh *>::iterator smIt;
282 for ( smIt = smmap.begin(); smIt != smmap.end(); smIt++ )
283 (*smIt)->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
286 // Check if the whole mesh becomes empty
287 if ( SMESH_subMesh * sm = GetMesh()->GetSubMeshContaining( 1 ) )
288 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
293 //=======================================================================
294 //function : FindShape
295 //purpose : Return an index of the shape theElem is on
296 // or zero if a shape not found
297 //=======================================================================
299 int SMESH_MeshEditor::FindShape (const SMDS_MeshElement * theElem)
301 myLastCreatedElems.Clear();
302 myLastCreatedNodes.Clear();
304 SMESHDS_Mesh * aMesh = GetMeshDS();
305 if ( aMesh->ShapeToMesh().IsNull() )
308 if ( theElem->GetType() == SMDSAbs_Node ) {
309 const SMDS_PositionPtr& aPosition =
310 static_cast<const SMDS_MeshNode*>( theElem )->GetPosition();
311 if ( aPosition.get() )
312 return aPosition->GetShapeId();
317 TopoDS_Shape aShape; // the shape a node is on
318 SMDS_ElemIteratorPtr nodeIt = theElem->nodesIterator();
319 while ( nodeIt->more() ) {
320 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
321 const SMDS_PositionPtr& aPosition = node->GetPosition();
322 if ( aPosition.get() ) {
323 int aShapeID = aPosition->GetShapeId();
324 SMESHDS_SubMesh * sm = aMesh->MeshElements( aShapeID );
326 if ( sm->Contains( theElem ))
328 if ( aShape.IsNull() )
329 aShape = aMesh->IndexToShape( aShapeID );
332 //MESSAGE ( "::FindShape() No SubShape for aShapeID " << aShapeID );
337 // None of nodes is on a proper shape,
338 // find the shape among ancestors of aShape on which a node is
339 if ( aShape.IsNull() ) {
340 //MESSAGE ("::FindShape() - NONE node is on shape")
343 TopTools_ListIteratorOfListOfShape ancIt( GetMesh()->GetAncestors( aShape ));
344 for ( ; ancIt.More(); ancIt.Next() ) {
345 SMESHDS_SubMesh * sm = aMesh->MeshElements( ancIt.Value() );
346 if ( sm && sm->Contains( theElem ))
347 return aMesh->ShapeToIndex( ancIt.Value() );
350 //MESSAGE ("::FindShape() - SHAPE NOT FOUND")
354 //=======================================================================
355 //function : IsMedium
357 //=======================================================================
359 bool SMESH_MeshEditor::IsMedium(const SMDS_MeshNode* node,
360 const SMDSAbs_ElementType typeToCheck)
362 bool isMedium = false;
363 SMDS_ElemIteratorPtr it = node->GetInverseElementIterator(typeToCheck);
365 const SMDS_MeshElement* elem = it->next();
366 isMedium = elem->IsMediumNode(node);
371 //=======================================================================
372 //function : ShiftNodesQuadTria
374 // Shift nodes in the array corresponded to quadratic triangle
375 // example: (0,1,2,3,4,5) -> (1,2,0,4,5,3)
376 //=======================================================================
377 static void ShiftNodesQuadTria(const SMDS_MeshNode* aNodes[])
379 const SMDS_MeshNode* nd1 = aNodes[0];
380 aNodes[0] = aNodes[1];
381 aNodes[1] = aNodes[2];
383 const SMDS_MeshNode* nd2 = aNodes[3];
384 aNodes[3] = aNodes[4];
385 aNodes[4] = aNodes[5];
389 //=======================================================================
390 //function : GetNodesFromTwoTria
392 // Shift nodes in the array corresponded to quadratic triangle
393 // example: (0,1,2,3,4,5) -> (1,2,0,4,5,3)
394 //=======================================================================
395 static bool GetNodesFromTwoTria(const SMDS_MeshElement * theTria1,
396 const SMDS_MeshElement * theTria2,
397 const SMDS_MeshNode* N1[],
398 const SMDS_MeshNode* N2[])
400 SMDS_ElemIteratorPtr it = theTria1->nodesIterator();
403 N1[i] = static_cast<const SMDS_MeshNode*>( it->next() );
406 if(it->more()) return false;
407 it = theTria2->nodesIterator();
410 N2[i] = static_cast<const SMDS_MeshNode*>( it->next() );
413 if(it->more()) return false;
415 int sames[3] = {-1,-1,-1};
427 if(nbsames!=2) return false;
429 ShiftNodesQuadTria(N1);
431 ShiftNodesQuadTria(N1);
434 i = sames[0] + sames[1] + sames[2];
436 ShiftNodesQuadTria(N2);
438 // now we receive following N1 and N2 (using numeration as above image)
439 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
440 // i.e. first nodes from both arrays determ new diagonal
444 //=======================================================================
445 //function : InverseDiag
446 //purpose : Replace two neighbour triangles with ones built on the same 4 nodes
447 // but having other common link.
448 // Return False if args are improper
449 //=======================================================================
451 bool SMESH_MeshEditor::InverseDiag (const SMDS_MeshElement * theTria1,
452 const SMDS_MeshElement * theTria2 )
454 myLastCreatedElems.Clear();
455 myLastCreatedNodes.Clear();
457 if (!theTria1 || !theTria2)
460 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( theTria1 );
461 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( theTria2 );
464 // 1 +--+ A theTria1: ( 1 A B ) A->2 ( 1 2 B ) 1 +--+ A
465 // | /| theTria2: ( B A 2 ) B->1 ( 1 A 2 ) |\ |
469 // put nodes in array and find out indices of the same ones
470 const SMDS_MeshNode* aNodes [6];
471 int sameInd [] = { 0, 0, 0, 0, 0, 0 };
473 SMDS_ElemIteratorPtr it = theTria1->nodesIterator();
474 while ( it->more() ) {
475 aNodes[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
477 if ( i > 2 ) // theTria2
478 // find same node of theTria1
479 for ( int j = 0; j < 3; j++ )
480 if ( aNodes[ i ] == aNodes[ j ]) {
489 return false; // theTria1 is not a triangle
490 it = theTria2->nodesIterator();
492 if ( i == 6 && it->more() )
493 return false; // theTria2 is not a triangle
496 // find indices of 1,2 and of A,B in theTria1
497 int iA = 0, iB = 0, i1 = 0, i2 = 0;
498 for ( i = 0; i < 6; i++ ) {
499 if ( sameInd [ i ] == 0 )
506 // nodes 1 and 2 should not be the same
507 if ( aNodes[ i1 ] == aNodes[ i2 ] )
511 aNodes[ iA ] = aNodes[ i2 ];
513 aNodes[ sameInd[ iB ]] = aNodes[ i1 ];
515 //MESSAGE( theTria1 << theTria2 );
517 GetMeshDS()->ChangeElementNodes( theTria1, aNodes, 3 );
518 GetMeshDS()->ChangeElementNodes( theTria2, &aNodes[ 3 ], 3 );
520 //MESSAGE( theTria1 << theTria2 );
524 } // end if(F1 && F2)
526 // check case of quadratic faces
527 const SMDS_QuadraticFaceOfNodes* QF1 =
528 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (theTria1);
529 if(!QF1) return false;
530 const SMDS_QuadraticFaceOfNodes* QF2 =
531 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (theTria2);
532 if(!QF2) return false;
535 // 1 +--+--+ 2 theTria1: (1 2 4 5 9 7) or (2 4 1 9 7 5) or (4 1 2 7 5 9)
536 // | /| theTria2: (2 3 4 6 8 9) or (3 4 2 8 9 6) or (4 2 3 9 6 8)
544 const SMDS_MeshNode* N1 [6];
545 const SMDS_MeshNode* N2 [6];
546 if(!GetNodesFromTwoTria(theTria1,theTria2,N1,N2))
548 // now we receive following N1 and N2 (using numeration as above image)
549 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
550 // i.e. first nodes from both arrays determ new diagonal
552 const SMDS_MeshNode* N1new [6];
553 const SMDS_MeshNode* N2new [6];
566 // replaces nodes in faces
567 GetMeshDS()->ChangeElementNodes( theTria1, N1new, 6 );
568 GetMeshDS()->ChangeElementNodes( theTria2, N2new, 6 );
573 //=======================================================================
574 //function : findTriangles
575 //purpose : find triangles sharing theNode1-theNode2 link
576 //=======================================================================
578 static bool findTriangles(const SMDS_MeshNode * theNode1,
579 const SMDS_MeshNode * theNode2,
580 const SMDS_MeshElement*& theTria1,
581 const SMDS_MeshElement*& theTria2)
583 if ( !theNode1 || !theNode2 ) return false;
585 theTria1 = theTria2 = 0;
587 set< const SMDS_MeshElement* > emap;
588 SMDS_ElemIteratorPtr it = theNode1->GetInverseElementIterator(SMDSAbs_Face);
590 const SMDS_MeshElement* elem = it->next();
591 if ( elem->NbNodes() == 3 )
594 it = theNode2->GetInverseElementIterator(SMDSAbs_Face);
596 const SMDS_MeshElement* elem = it->next();
597 if ( emap.find( elem ) != emap.end() )
599 // theTria1 must be element with minimum ID
600 if( theTria1->GetID() < elem->GetID() ) {
613 return ( theTria1 && theTria2 );
616 //=======================================================================
617 //function : InverseDiag
618 //purpose : Replace two neighbour triangles sharing theNode1-theNode2 link
619 // with ones built on the same 4 nodes but having other common link.
620 // Return false if proper faces not found
621 //=======================================================================
623 bool SMESH_MeshEditor::InverseDiag (const SMDS_MeshNode * theNode1,
624 const SMDS_MeshNode * theNode2)
626 myLastCreatedElems.Clear();
627 myLastCreatedNodes.Clear();
629 MESSAGE( "::InverseDiag()" );
631 const SMDS_MeshElement *tr1, *tr2;
632 if ( !findTriangles( theNode1, theNode2, tr1, tr2 ))
635 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( tr1 );
636 //if (!F1) return false;
637 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( tr2 );
638 //if (!F2) return false;
641 // 1 +--+ A tr1: ( 1 A B ) A->2 ( 1 2 B ) 1 +--+ A
642 // | /| tr2: ( B A 2 ) B->1 ( 1 A 2 ) |\ |
646 // put nodes in array
647 // and find indices of 1,2 and of A in tr1 and of B in tr2
648 int i, iA1 = 0, i1 = 0;
649 const SMDS_MeshNode* aNodes1 [3];
650 SMDS_ElemIteratorPtr it;
651 for (i = 0, it = tr1->nodesIterator(); it->more(); i++ ) {
652 aNodes1[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
653 if ( aNodes1[ i ] == theNode1 )
654 iA1 = i; // node A in tr1
655 else if ( aNodes1[ i ] != theNode2 )
659 const SMDS_MeshNode* aNodes2 [3];
660 for (i = 0, it = tr2->nodesIterator(); it->more(); i++ ) {
661 aNodes2[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
662 if ( aNodes2[ i ] == theNode2 )
663 iB2 = i; // node B in tr2
664 else if ( aNodes2[ i ] != theNode1 )
668 // nodes 1 and 2 should not be the same
669 if ( aNodes1[ i1 ] == aNodes2[ i2 ] )
673 aNodes1[ iA1 ] = aNodes2[ i2 ];
675 aNodes2[ iB2 ] = aNodes1[ i1 ];
677 //MESSAGE( tr1 << tr2 );
679 GetMeshDS()->ChangeElementNodes( tr1, aNodes1, 3 );
680 GetMeshDS()->ChangeElementNodes( tr2, aNodes2, 3 );
682 //MESSAGE( tr1 << tr2 );
687 // check case of quadratic faces
688 const SMDS_QuadraticFaceOfNodes* QF1 =
689 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (tr1);
690 if(!QF1) return false;
691 const SMDS_QuadraticFaceOfNodes* QF2 =
692 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (tr2);
693 if(!QF2) return false;
694 return InverseDiag(tr1,tr2);
697 //=======================================================================
698 //function : getQuadrangleNodes
699 //purpose : fill theQuadNodes - nodes of a quadrangle resulting from
700 // fusion of triangles tr1 and tr2 having shared link on
701 // theNode1 and theNode2
702 //=======================================================================
704 bool getQuadrangleNodes(const SMDS_MeshNode * theQuadNodes [],
705 const SMDS_MeshNode * theNode1,
706 const SMDS_MeshNode * theNode2,
707 const SMDS_MeshElement * tr1,
708 const SMDS_MeshElement * tr2 )
710 if( tr1->NbNodes() != tr2->NbNodes() )
712 // find the 4-th node to insert into tr1
713 const SMDS_MeshNode* n4 = 0;
714 SMDS_ElemIteratorPtr it = tr2->nodesIterator();
716 //while ( !n4 && it->more() ) {
717 while ( !n4 && i<3 ) {
718 const SMDS_MeshNode * n = static_cast<const SMDS_MeshNode*>( it->next() );
720 bool isDiag = ( n == theNode1 || n == theNode2 );
724 // Make an array of nodes to be in a quadrangle
725 int iNode = 0, iFirstDiag = -1;
726 it = tr1->nodesIterator();
728 //while ( it->more() ) {
730 const SMDS_MeshNode * n = static_cast<const SMDS_MeshNode*>( it->next() );
732 bool isDiag = ( n == theNode1 || n == theNode2 );
734 if ( iFirstDiag < 0 )
736 else if ( iNode - iFirstDiag == 1 )
737 theQuadNodes[ iNode++ ] = n4; // insert the 4-th node between diagonal nodes
739 else if ( n == n4 ) {
740 return false; // tr1 and tr2 should not have all the same nodes
742 theQuadNodes[ iNode++ ] = n;
744 if ( iNode == 3 ) // diagonal nodes have 0 and 2 indices
745 theQuadNodes[ iNode ] = n4;
750 //=======================================================================
751 //function : DeleteDiag
752 //purpose : Replace two neighbour triangles sharing theNode1-theNode2 link
753 // with a quadrangle built on the same 4 nodes.
754 // Return false if proper faces not found
755 //=======================================================================
757 bool SMESH_MeshEditor::DeleteDiag (const SMDS_MeshNode * theNode1,
758 const SMDS_MeshNode * theNode2)
760 myLastCreatedElems.Clear();
761 myLastCreatedNodes.Clear();
763 MESSAGE( "::DeleteDiag()" );
765 const SMDS_MeshElement *tr1, *tr2;
766 if ( !findTriangles( theNode1, theNode2, tr1, tr2 ))
769 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( tr1 );
770 //if (!F1) return false;
771 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( tr2 );
772 //if (!F2) return false;
775 const SMDS_MeshNode* aNodes [ 4 ];
776 if ( ! getQuadrangleNodes( aNodes, theNode1, theNode2, tr1, tr2 ))
779 //MESSAGE( endl << tr1 << tr2 );
781 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 4 );
782 myLastCreatedElems.Append(tr1);
783 GetMeshDS()->RemoveElement( tr2 );
785 //MESSAGE( endl << tr1 );
790 // check case of quadratic faces
791 const SMDS_QuadraticFaceOfNodes* QF1 =
792 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (tr1);
793 if(!QF1) return false;
794 const SMDS_QuadraticFaceOfNodes* QF2 =
795 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (tr2);
796 if(!QF2) return false;
799 // 1 +--+--+ 2 tr1: (1 2 4 5 9 7) or (2 4 1 9 7 5) or (4 1 2 7 5 9)
800 // | /| tr2: (2 3 4 6 8 9) or (3 4 2 8 9 6) or (4 2 3 9 6 8)
808 const SMDS_MeshNode* N1 [6];
809 const SMDS_MeshNode* N2 [6];
810 if(!GetNodesFromTwoTria(tr1,tr2,N1,N2))
812 // now we receive following N1 and N2 (using numeration as above image)
813 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
814 // i.e. first nodes from both arrays determ new diagonal
816 const SMDS_MeshNode* aNodes[8];
826 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 8 );
827 myLastCreatedElems.Append(tr1);
828 GetMeshDS()->RemoveElement( tr2 );
830 // remove middle node (9)
831 GetMeshDS()->RemoveNode( N1[4] );
836 //=======================================================================
837 //function : Reorient
838 //purpose : Reverse theElement orientation
839 //=======================================================================
841 bool SMESH_MeshEditor::Reorient (const SMDS_MeshElement * theElem)
843 myLastCreatedElems.Clear();
844 myLastCreatedNodes.Clear();
848 SMDS_ElemIteratorPtr it = theElem->nodesIterator();
849 if ( !it || !it->more() )
852 switch ( theElem->GetType() ) {
856 if(!theElem->IsQuadratic()) {
857 int i = theElem->NbNodes();
858 vector<const SMDS_MeshNode*> aNodes( i );
860 aNodes[ --i ]= static_cast<const SMDS_MeshNode*>( it->next() );
861 return GetMeshDS()->ChangeElementNodes( theElem, &aNodes[0], theElem->NbNodes() );
864 // quadratic elements
865 if(theElem->GetType()==SMDSAbs_Edge) {
866 vector<const SMDS_MeshNode*> aNodes(3);
867 aNodes[1]= static_cast<const SMDS_MeshNode*>( it->next() );
868 aNodes[0]= static_cast<const SMDS_MeshNode*>( it->next() );
869 aNodes[2]= static_cast<const SMDS_MeshNode*>( it->next() );
870 return GetMeshDS()->ChangeElementNodes( theElem, &aNodes[0], 3 );
873 int nbn = theElem->NbNodes();
874 vector<const SMDS_MeshNode*> aNodes(nbn);
875 aNodes[0]= static_cast<const SMDS_MeshNode*>( it->next() );
877 for(; i<nbn/2; i++) {
878 aNodes[nbn/2-i]= static_cast<const SMDS_MeshNode*>( it->next() );
880 for(i=0; i<nbn/2; i++) {
881 aNodes[nbn-i-1]= static_cast<const SMDS_MeshNode*>( it->next() );
883 return GetMeshDS()->ChangeElementNodes( theElem, &aNodes[0], nbn );
887 case SMDSAbs_Volume: {
888 if (theElem->IsPoly()) {
889 const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
890 static_cast<const SMDS_PolyhedralVolumeOfNodes*>( theElem );
892 MESSAGE("Warning: bad volumic element");
896 int nbFaces = aPolyedre->NbFaces();
897 vector<const SMDS_MeshNode *> poly_nodes;
898 vector<int> quantities (nbFaces);
900 // reverse each face of the polyedre
901 for (int iface = 1; iface <= nbFaces; iface++) {
902 int inode, nbFaceNodes = aPolyedre->NbFaceNodes(iface);
903 quantities[iface - 1] = nbFaceNodes;
905 for (inode = nbFaceNodes; inode >= 1; inode--) {
906 const SMDS_MeshNode* curNode = aPolyedre->GetFaceNode(iface, inode);
907 poly_nodes.push_back(curNode);
911 return GetMeshDS()->ChangePolyhedronNodes( theElem, poly_nodes, quantities );
915 SMDS_VolumeTool vTool;
916 if ( !vTool.Set( theElem ))
919 return GetMeshDS()->ChangeElementNodes( theElem, vTool.GetNodes(), vTool.NbNodes() );
928 //=======================================================================
929 //function : getBadRate
931 //=======================================================================
933 static double getBadRate (const SMDS_MeshElement* theElem,
934 SMESH::Controls::NumericalFunctorPtr& theCrit)
936 SMESH::Controls::TSequenceOfXYZ P;
937 if ( !theElem || !theCrit->GetPoints( theElem, P ))
939 return theCrit->GetBadRate( theCrit->GetValue( P ), theElem->NbNodes() );
940 //return theCrit->GetBadRate( theCrit->GetValue( theElem->GetID() ), theElem->NbNodes() );
943 //=======================================================================
944 //function : QuadToTri
945 //purpose : Cut quadrangles into triangles.
946 // theCrit is used to select a diagonal to cut
947 //=======================================================================
949 bool SMESH_MeshEditor::QuadToTri (TIDSortedElemSet & theElems,
950 SMESH::Controls::NumericalFunctorPtr theCrit)
952 myLastCreatedElems.Clear();
953 myLastCreatedNodes.Clear();
955 MESSAGE( "::QuadToTri()" );
957 if ( !theCrit.get() )
960 SMESHDS_Mesh * aMesh = GetMeshDS();
962 Handle(Geom_Surface) surface;
963 SMESH_MesherHelper helper( *GetMesh() );
965 TIDSortedElemSet::iterator itElem;
966 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
967 const SMDS_MeshElement* elem = *itElem;
968 if ( !elem || elem->GetType() != SMDSAbs_Face )
970 if ( elem->NbNodes() != ( elem->IsQuadratic() ? 8 : 4 ))
973 // retrieve element nodes
974 const SMDS_MeshNode* aNodes [8];
975 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
977 while ( itN->more() )
978 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
980 // compare two sets of possible triangles
981 double aBadRate1, aBadRate2; // to what extent a set is bad
982 SMDS_FaceOfNodes tr1 ( aNodes[0], aNodes[1], aNodes[2] );
983 SMDS_FaceOfNodes tr2 ( aNodes[2], aNodes[3], aNodes[0] );
984 aBadRate1 = getBadRate( &tr1, theCrit ) + getBadRate( &tr2, theCrit );
986 SMDS_FaceOfNodes tr3 ( aNodes[1], aNodes[2], aNodes[3] );
987 SMDS_FaceOfNodes tr4 ( aNodes[3], aNodes[0], aNodes[1] );
988 aBadRate2 = getBadRate( &tr3, theCrit ) + getBadRate( &tr4, theCrit );
990 int aShapeId = FindShape( elem );
991 const SMDS_MeshElement* newElem = 0;
993 if( !elem->IsQuadratic() ) {
995 // split liner quadrangle
997 if ( aBadRate1 <= aBadRate2 ) {
998 // tr1 + tr2 is better
999 aMesh->ChangeElementNodes( elem, aNodes, 3 );
1000 newElem = aMesh->AddFace( aNodes[2], aNodes[3], aNodes[0] );
1003 // tr3 + tr4 is better
1004 aMesh->ChangeElementNodes( elem, &aNodes[1], 3 );
1005 newElem = aMesh->AddFace( aNodes[3], aNodes[0], aNodes[1] );
1010 // split quadratic quadrangle
1012 // get surface elem is on
1013 if ( aShapeId != helper.GetSubShapeID() ) {
1017 shape = aMesh->IndexToShape( aShapeId );
1018 if ( !shape.IsNull() && shape.ShapeType() == TopAbs_FACE ) {
1019 TopoDS_Face face = TopoDS::Face( shape );
1020 surface = BRep_Tool::Surface( face );
1021 if ( !surface.IsNull() )
1022 helper.SetSubShape( shape );
1026 const SMDS_MeshNode* aNodes [8];
1027 const SMDS_MeshNode* inFaceNode = 0;
1028 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1030 while ( itN->more() ) {
1031 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1032 if ( !inFaceNode && helper.GetNodeUVneedInFaceNode() &&
1033 aNodes[ i-1 ]->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE )
1035 inFaceNode = aNodes[ i-1 ];
1038 // find middle point for (0,1,2,3)
1039 // and create a node in this point;
1041 if ( surface.IsNull() ) {
1043 p += gp_XYZ(aNodes[i]->X(), aNodes[i]->Y(), aNodes[i]->Z() );
1047 TopoDS_Face face = TopoDS::Face( helper.GetSubShape() );
1050 uv += helper.GetNodeUV( face, aNodes[i], inFaceNode );
1052 p = surface->Value( uv.X(), uv.Y() ).XYZ();
1054 const SMDS_MeshNode* newN = aMesh->AddNode( p.X(), p.Y(), p.Z() );
1055 myLastCreatedNodes.Append(newN);
1057 // create a new element
1058 const SMDS_MeshNode* N[6];
1059 if ( aBadRate1 <= aBadRate2 ) {
1066 newElem = aMesh->AddFace(aNodes[2], aNodes[3], aNodes[0],
1067 aNodes[6], aNodes[7], newN );
1076 newElem = aMesh->AddFace(aNodes[3], aNodes[0], aNodes[1],
1077 aNodes[7], aNodes[4], newN );
1079 aMesh->ChangeElementNodes( elem, N, 6 );
1083 // care of a new element
1085 myLastCreatedElems.Append(newElem);
1086 AddToSameGroups( newElem, elem, aMesh );
1088 // put a new triangle on the same shape
1090 aMesh->SetMeshElementOnShape( newElem, aShapeId );
1095 //=======================================================================
1096 //function : BestSplit
1097 //purpose : Find better diagonal for cutting.
1098 //=======================================================================
1099 int SMESH_MeshEditor::BestSplit (const SMDS_MeshElement* theQuad,
1100 SMESH::Controls::NumericalFunctorPtr theCrit)
1102 myLastCreatedElems.Clear();
1103 myLastCreatedNodes.Clear();
1108 if (!theQuad || theQuad->GetType() != SMDSAbs_Face )
1111 if( theQuad->NbNodes()==4 ||
1112 (theQuad->NbNodes()==8 && theQuad->IsQuadratic()) ) {
1114 // retrieve element nodes
1115 const SMDS_MeshNode* aNodes [4];
1116 SMDS_ElemIteratorPtr itN = theQuad->nodesIterator();
1118 //while (itN->more())
1120 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1122 // compare two sets of possible triangles
1123 double aBadRate1, aBadRate2; // to what extent a set is bad
1124 SMDS_FaceOfNodes tr1 ( aNodes[0], aNodes[1], aNodes[2] );
1125 SMDS_FaceOfNodes tr2 ( aNodes[2], aNodes[3], aNodes[0] );
1126 aBadRate1 = getBadRate( &tr1, theCrit ) + getBadRate( &tr2, theCrit );
1128 SMDS_FaceOfNodes tr3 ( aNodes[1], aNodes[2], aNodes[3] );
1129 SMDS_FaceOfNodes tr4 ( aNodes[3], aNodes[0], aNodes[1] );
1130 aBadRate2 = getBadRate( &tr3, theCrit ) + getBadRate( &tr4, theCrit );
1132 if (aBadRate1 <= aBadRate2) // tr1 + tr2 is better
1133 return 1; // diagonal 1-3
1135 return 2; // diagonal 2-4
1140 //=======================================================================
1141 //function : AddToSameGroups
1142 //purpose : add elemToAdd to the groups the elemInGroups belongs to
1143 //=======================================================================
1145 void SMESH_MeshEditor::AddToSameGroups (const SMDS_MeshElement* elemToAdd,
1146 const SMDS_MeshElement* elemInGroups,
1147 SMESHDS_Mesh * aMesh)
1149 const set<SMESHDS_GroupBase*>& groups = aMesh->GetGroups();
1150 set<SMESHDS_GroupBase*>::const_iterator grIt = groups.begin();
1151 for ( ; grIt != groups.end(); grIt++ ) {
1152 SMESHDS_Group* group = dynamic_cast<SMESHDS_Group*>( *grIt );
1153 if ( group && group->SMDSGroup().Contains( elemInGroups ))
1154 group->SMDSGroup().Add( elemToAdd );
1159 //=======================================================================
1160 //function : RemoveElemFromGroups
1161 //purpose : Remove removeelem to the groups the elemInGroups belongs to
1162 //=======================================================================
1163 void SMESH_MeshEditor::RemoveElemFromGroups (const SMDS_MeshElement* removeelem,
1164 SMESHDS_Mesh * aMesh)
1166 const set<SMESHDS_GroupBase*>& groups = aMesh->GetGroups();
1167 if (!groups.empty())
1169 set<SMESHDS_GroupBase*>::const_iterator GrIt = groups.begin();
1170 for (; GrIt != groups.end(); GrIt++)
1172 SMESHDS_Group* grp = dynamic_cast<SMESHDS_Group*>(*GrIt);
1173 if (!grp || grp->IsEmpty()) continue;
1174 grp->SMDSGroup().Remove(removeelem);
1180 //=======================================================================
1181 //function : QuadToTri
1182 //purpose : Cut quadrangles into triangles.
1183 // theCrit is used to select a diagonal to cut
1184 //=======================================================================
1186 bool SMESH_MeshEditor::QuadToTri (TIDSortedElemSet & theElems,
1187 const bool the13Diag)
1189 myLastCreatedElems.Clear();
1190 myLastCreatedNodes.Clear();
1192 MESSAGE( "::QuadToTri()" );
1194 SMESHDS_Mesh * aMesh = GetMeshDS();
1196 Handle(Geom_Surface) surface;
1197 SMESH_MesherHelper helper( *GetMesh() );
1199 TIDSortedElemSet::iterator itElem;
1200 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
1201 const SMDS_MeshElement* elem = *itElem;
1202 if ( !elem || elem->GetType() != SMDSAbs_Face )
1204 bool isquad = elem->NbNodes()==4 || elem->NbNodes()==8;
1205 if(!isquad) continue;
1207 if(elem->NbNodes()==4) {
1208 // retrieve element nodes
1209 const SMDS_MeshNode* aNodes [4];
1210 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1212 while ( itN->more() )
1213 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1215 int aShapeId = FindShape( elem );
1216 const SMDS_MeshElement* newElem = 0;
1218 aMesh->ChangeElementNodes( elem, aNodes, 3 );
1219 newElem = aMesh->AddFace( aNodes[2], aNodes[3], aNodes[0] );
1222 aMesh->ChangeElementNodes( elem, &aNodes[1], 3 );
1223 newElem = aMesh->AddFace( aNodes[3], aNodes[0], aNodes[1] );
1225 myLastCreatedElems.Append(newElem);
1226 // put a new triangle on the same shape and add to the same groups
1228 aMesh->SetMeshElementOnShape( newElem, aShapeId );
1229 AddToSameGroups( newElem, elem, aMesh );
1232 // Quadratic quadrangle
1234 if( elem->NbNodes()==8 && elem->IsQuadratic() ) {
1236 // get surface elem is on
1237 int aShapeId = FindShape( elem );
1238 if ( aShapeId != helper.GetSubShapeID() ) {
1242 shape = aMesh->IndexToShape( aShapeId );
1243 if ( !shape.IsNull() && shape.ShapeType() == TopAbs_FACE ) {
1244 TopoDS_Face face = TopoDS::Face( shape );
1245 surface = BRep_Tool::Surface( face );
1246 if ( !surface.IsNull() )
1247 helper.SetSubShape( shape );
1251 const SMDS_MeshNode* aNodes [8];
1252 const SMDS_MeshNode* inFaceNode = 0;
1253 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1255 while ( itN->more() ) {
1256 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1257 if ( !inFaceNode && helper.GetNodeUVneedInFaceNode() &&
1258 aNodes[ i-1 ]->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE )
1260 inFaceNode = aNodes[ i-1 ];
1264 // find middle point for (0,1,2,3)
1265 // and create a node in this point;
1267 if ( surface.IsNull() ) {
1269 p += gp_XYZ(aNodes[i]->X(), aNodes[i]->Y(), aNodes[i]->Z() );
1273 TopoDS_Face geomFace = TopoDS::Face( helper.GetSubShape() );
1276 uv += helper.GetNodeUV( geomFace, aNodes[i], inFaceNode );
1278 p = surface->Value( uv.X(), uv.Y() ).XYZ();
1280 const SMDS_MeshNode* newN = aMesh->AddNode( p.X(), p.Y(), p.Z() );
1281 myLastCreatedNodes.Append(newN);
1283 // create a new element
1284 const SMDS_MeshElement* newElem = 0;
1285 const SMDS_MeshNode* N[6];
1293 newElem = aMesh->AddFace(aNodes[2], aNodes[3], aNodes[0],
1294 aNodes[6], aNodes[7], newN );
1303 newElem = aMesh->AddFace(aNodes[3], aNodes[0], aNodes[1],
1304 aNodes[7], aNodes[4], newN );
1306 myLastCreatedElems.Append(newElem);
1307 aMesh->ChangeElementNodes( elem, N, 6 );
1308 // put a new triangle on the same shape and add to the same groups
1310 aMesh->SetMeshElementOnShape( newElem, aShapeId );
1311 AddToSameGroups( newElem, elem, aMesh );
1318 //=======================================================================
1319 //function : getAngle
1321 //=======================================================================
1323 double getAngle(const SMDS_MeshElement * tr1,
1324 const SMDS_MeshElement * tr2,
1325 const SMDS_MeshNode * n1,
1326 const SMDS_MeshNode * n2)
1328 double angle = 2*PI; // bad angle
1331 SMESH::Controls::TSequenceOfXYZ P1, P2;
1332 if ( !SMESH::Controls::NumericalFunctor::GetPoints( tr1, P1 ) ||
1333 !SMESH::Controls::NumericalFunctor::GetPoints( tr2, P2 ))
1336 if(!tr1->IsQuadratic())
1337 N1 = gp_Vec( P1(2) - P1(1) ) ^ gp_Vec( P1(3) - P1(1) );
1339 N1 = gp_Vec( P1(3) - P1(1) ) ^ gp_Vec( P1(5) - P1(1) );
1340 if ( N1.SquareMagnitude() <= gp::Resolution() )
1342 if(!tr2->IsQuadratic())
1343 N2 = gp_Vec( P2(2) - P2(1) ) ^ gp_Vec( P2(3) - P2(1) );
1345 N2 = gp_Vec( P2(3) - P2(1) ) ^ gp_Vec( P2(5) - P2(1) );
1346 if ( N2.SquareMagnitude() <= gp::Resolution() )
1349 // find the first diagonal node n1 in the triangles:
1350 // take in account a diagonal link orientation
1351 const SMDS_MeshElement *nFirst[2], *tr[] = { tr1, tr2 };
1352 for ( int t = 0; t < 2; t++ ) {
1353 SMDS_ElemIteratorPtr it = tr[ t ]->nodesIterator();
1354 int i = 0, iDiag = -1;
1355 while ( it->more()) {
1356 const SMDS_MeshElement *n = it->next();
1357 if ( n == n1 || n == n2 )
1361 if ( i - iDiag == 1 )
1362 nFirst[ t ] = ( n == n1 ? n2 : n1 );
1370 if ( nFirst[ 0 ] == nFirst[ 1 ] )
1373 angle = N1.Angle( N2 );
1378 // =================================================
1379 // class generating a unique ID for a pair of nodes
1380 // and able to return nodes by that ID
1381 // =================================================
1385 LinkID_Gen( const SMESHDS_Mesh* theMesh )
1386 :myMesh( theMesh ), myMaxID( theMesh->MaxNodeID() + 1)
1389 long GetLinkID (const SMDS_MeshNode * n1,
1390 const SMDS_MeshNode * n2) const
1392 return ( Min(n1->GetID(),n2->GetID()) * myMaxID + Max(n1->GetID(),n2->GetID()));
1395 bool GetNodes (const long theLinkID,
1396 const SMDS_MeshNode* & theNode1,
1397 const SMDS_MeshNode* & theNode2) const
1399 theNode1 = myMesh->FindNode( theLinkID / myMaxID );
1400 if ( !theNode1 ) return false;
1401 theNode2 = myMesh->FindNode( theLinkID % myMaxID );
1402 if ( !theNode2 ) return false;
1408 const SMESHDS_Mesh* myMesh;
1413 //=======================================================================
1414 //function : TriToQuad
1415 //purpose : Fuse neighbour triangles into quadrangles.
1416 // theCrit is used to select a neighbour to fuse with.
1417 // theMaxAngle is a max angle between element normals at which
1418 // fusion is still performed.
1419 //=======================================================================
1421 bool SMESH_MeshEditor::TriToQuad (TIDSortedElemSet & theElems,
1422 SMESH::Controls::NumericalFunctorPtr theCrit,
1423 const double theMaxAngle)
1425 myLastCreatedElems.Clear();
1426 myLastCreatedNodes.Clear();
1428 MESSAGE( "::TriToQuad()" );
1430 if ( !theCrit.get() )
1433 SMESHDS_Mesh * aMesh = GetMeshDS();
1434 //LinkID_Gen aLinkID_Gen( aMesh );
1436 // Prepare data for algo: build
1437 // 1. map of elements with their linkIDs
1438 // 2. map of linkIDs with their elements
1440 //map< long, list< const SMDS_MeshElement* > > mapLi_listEl;
1441 //map< long, list< const SMDS_MeshElement* > >::iterator itLE;
1442 //map< const SMDS_MeshElement*, set< long > > mapEl_setLi;
1443 //map< const SMDS_MeshElement*, set< long > >::iterator itEL;
1445 map< NLink, list< const SMDS_MeshElement* > > mapLi_listEl;
1446 map< NLink, list< const SMDS_MeshElement* > >::iterator itLE;
1447 map< const SMDS_MeshElement*, set< NLink > > mapEl_setLi;
1448 map< const SMDS_MeshElement*, set< NLink > >::iterator itEL;
1450 TIDSortedElemSet::iterator itElem;
1451 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
1452 const SMDS_MeshElement* elem = *itElem;
1453 //if ( !elem || elem->NbNodes() != 3 )
1455 if(!elem || elem->GetType() != SMDSAbs_Face ) continue;
1456 bool IsTria = elem->NbNodes()==3 || (elem->NbNodes()==6 && elem->IsQuadratic());
1457 if(!IsTria) continue;
1459 // retrieve element nodes
1460 const SMDS_MeshNode* aNodes [4];
1461 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1463 //while ( itN->more() )
1465 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1467 aNodes[ 3 ] = aNodes[ 0 ];
1470 for ( i = 0; i < 3; i++ ) {
1471 //long linkID = aLinkID_Gen.GetLinkID( aNodes[ i ], aNodes[ i+1 ] );
1472 NLink link(( aNodes[i] < aNodes[i+1] ? aNodes[i] : aNodes[i+1] ),
1473 ( aNodes[i] < aNodes[i+1] ? aNodes[i+1] : aNodes[i] ));
1474 // check if elements sharing a link can be fused
1475 //itLE = mapLi_listEl.find( linkID );
1476 itLE = mapLi_listEl.find( link );
1477 if ( itLE != mapLi_listEl.end() ) {
1478 if ((*itLE).second.size() > 1 ) // consider only 2 elems adjacent by a link
1480 const SMDS_MeshElement* elem2 = (*itLE).second.front();
1481 //if ( FindShape( elem ) != FindShape( elem2 ))
1482 // continue; // do not fuse triangles laying on different shapes
1483 if ( getAngle( elem, elem2, aNodes[i], aNodes[i+1] ) > theMaxAngle )
1484 continue; // avoid making badly shaped quads
1485 (*itLE).second.push_back( elem );
1488 //mapLi_listEl[ linkID ].push_back( elem );
1489 mapLi_listEl[ link ].push_back( elem );
1491 //mapEl_setLi [ elem ].insert( linkID );
1492 mapEl_setLi [ elem ].insert( link );
1495 // Clean the maps from the links shared by a sole element, ie
1496 // links to which only one element is bound in mapLi_listEl
1498 for ( itLE = mapLi_listEl.begin(); itLE != mapLi_listEl.end(); itLE++ ) {
1499 int nbElems = (*itLE).second.size();
1500 if ( nbElems < 2 ) {
1501 const SMDS_MeshElement* elem = (*itLE).second.front();
1502 //long link = (*itLE).first;
1503 NLink link = (*itLE).first;
1504 mapEl_setLi[ elem ].erase( link );
1505 if ( mapEl_setLi[ elem ].empty() )
1506 mapEl_setLi.erase( elem );
1510 // Algo: fuse triangles into quadrangles
1512 while ( ! mapEl_setLi.empty() ) {
1513 // Look for the start element:
1514 // the element having the least nb of shared links
1516 const SMDS_MeshElement* startElem = 0;
1518 for ( itEL = mapEl_setLi.begin(); itEL != mapEl_setLi.end(); itEL++ ) {
1519 int nbLinks = (*itEL).second.size();
1520 if ( nbLinks < minNbLinks ) {
1521 startElem = (*itEL).first;
1522 minNbLinks = nbLinks;
1523 if ( minNbLinks == 1 )
1528 // search elements to fuse starting from startElem or links of elements
1529 // fused earlyer - startLinks
1530 //list< long > startLinks;
1531 list< NLink > startLinks;
1532 while ( startElem || !startLinks.empty() ) {
1533 while ( !startElem && !startLinks.empty() ) {
1534 // Get an element to start, by a link
1535 //long linkId = startLinks.front();
1536 NLink linkId = startLinks.front();
1537 startLinks.pop_front();
1538 itLE = mapLi_listEl.find( linkId );
1539 if ( itLE != mapLi_listEl.end() ) {
1540 list< const SMDS_MeshElement* > & listElem = (*itLE).second;
1541 list< const SMDS_MeshElement* >::iterator itE = listElem.begin();
1542 for ( ; itE != listElem.end() ; itE++ )
1543 if ( mapEl_setLi.find( (*itE) ) != mapEl_setLi.end() )
1545 mapLi_listEl.erase( itLE );
1550 // Get candidates to be fused
1551 const SMDS_MeshElement *tr1 = startElem, *tr2 = 0, *tr3 = 0;
1552 //long link12, link13;
1553 NLink link12, link13;
1555 ASSERT( mapEl_setLi.find( tr1 ) != mapEl_setLi.end() );
1556 //set< long >& setLi = mapEl_setLi[ tr1 ];
1557 set< NLink >& setLi = mapEl_setLi[ tr1 ];
1558 ASSERT( !setLi.empty() );
1559 //set< long >::iterator itLi;
1560 set< NLink >::iterator itLi;
1561 for ( itLi = setLi.begin(); itLi != setLi.end(); itLi++ ) {
1562 //long linkID = (*itLi);
1563 NLink linkID = (*itLi);
1564 itLE = mapLi_listEl.find( linkID );
1565 if ( itLE == mapLi_listEl.end() )
1568 const SMDS_MeshElement* elem = (*itLE).second.front();
1570 elem = (*itLE).second.back();
1571 mapLi_listEl.erase( itLE );
1572 if ( mapEl_setLi.find( elem ) == mapEl_setLi.end())
1583 // add other links of elem to list of links to re-start from
1584 //set< long >& links = mapEl_setLi[ elem ];
1585 //set< long >::iterator it;
1586 set< NLink >& links = mapEl_setLi[ elem ];
1587 set< NLink >::iterator it;
1588 for ( it = links.begin(); it != links.end(); it++ ) {
1589 //long linkID2 = (*it);
1590 NLink linkID2 = (*it);
1591 if ( linkID2 != linkID )
1592 startLinks.push_back( linkID2 );
1596 // Get nodes of possible quadrangles
1597 const SMDS_MeshNode *n12 [4], *n13 [4];
1598 bool Ok12 = false, Ok13 = false;
1599 //const SMDS_MeshNode *linkNode1, *linkNode2;
1600 const SMDS_MeshNode *linkNode1, *linkNode2;
1602 //const SMDS_MeshNode *linkNode1 = link12.first;
1603 //const SMDS_MeshNode *linkNode2 = link12.second;
1604 linkNode1 = link12.first;
1605 linkNode2 = link12.second;
1607 // aLinkID_Gen.GetNodes( link12, linkNode1, linkNode2 ) &&
1608 // getQuadrangleNodes( n12, linkNode1, linkNode2, tr1, tr2 ))
1610 if ( tr2 && getQuadrangleNodes( n12, linkNode1, linkNode2, tr1, tr2 ))
1614 linkNode1 = link13.first;
1615 linkNode2 = link13.second;
1617 // aLinkID_Gen.GetNodes( link13, linkNode1, linkNode2 ) &&
1618 // getQuadrangleNodes( n13, linkNode1, linkNode2, tr1, tr3 ))
1620 if ( tr3 && getQuadrangleNodes( n13, linkNode1, linkNode2, tr1, tr3 ))
1624 // Choose a pair to fuse
1625 if ( Ok12 && Ok13 ) {
1626 SMDS_FaceOfNodes quad12 ( n12[ 0 ], n12[ 1 ], n12[ 2 ], n12[ 3 ] );
1627 SMDS_FaceOfNodes quad13 ( n13[ 0 ], n13[ 1 ], n13[ 2 ], n13[ 3 ] );
1628 double aBadRate12 = getBadRate( &quad12, theCrit );
1629 double aBadRate13 = getBadRate( &quad13, theCrit );
1630 if ( aBadRate13 < aBadRate12 )
1637 // and remove fused elems and removed links from the maps
1638 mapEl_setLi.erase( tr1 );
1640 mapEl_setLi.erase( tr2 );
1641 mapLi_listEl.erase( link12 );
1642 if(tr1->NbNodes()==3) {
1643 if( tr1->GetID() < tr2->GetID() ) {
1644 aMesh->ChangeElementNodes( tr1, n12, 4 );
1645 myLastCreatedElems.Append(tr1);
1646 aMesh->RemoveElement( tr2 );
1649 aMesh->ChangeElementNodes( tr2, n12, 4 );
1650 myLastCreatedElems.Append(tr2);
1651 aMesh->RemoveElement( tr1);
1655 const SMDS_MeshNode* N1 [6];
1656 const SMDS_MeshNode* N2 [6];
1657 GetNodesFromTwoTria(tr1,tr2,N1,N2);
1658 // now we receive following N1 and N2 (using numeration as above image)
1659 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
1660 // i.e. first nodes from both arrays determ new diagonal
1661 const SMDS_MeshNode* aNodes[8];
1670 if( tr1->GetID() < tr2->GetID() ) {
1671 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 8 );
1672 myLastCreatedElems.Append(tr1);
1673 GetMeshDS()->RemoveElement( tr2 );
1676 GetMeshDS()->ChangeElementNodes( tr2, aNodes, 8 );
1677 myLastCreatedElems.Append(tr2);
1678 GetMeshDS()->RemoveElement( tr1 );
1680 // remove middle node (9)
1681 GetMeshDS()->RemoveNode( N1[4] );
1685 mapEl_setLi.erase( tr3 );
1686 mapLi_listEl.erase( link13 );
1687 if(tr1->NbNodes()==3) {
1688 if( tr1->GetID() < tr2->GetID() ) {
1689 aMesh->ChangeElementNodes( tr1, n13, 4 );
1690 myLastCreatedElems.Append(tr1);
1691 aMesh->RemoveElement( tr3 );
1694 aMesh->ChangeElementNodes( tr3, n13, 4 );
1695 myLastCreatedElems.Append(tr3);
1696 aMesh->RemoveElement( tr1 );
1700 const SMDS_MeshNode* N1 [6];
1701 const SMDS_MeshNode* N2 [6];
1702 GetNodesFromTwoTria(tr1,tr3,N1,N2);
1703 // now we receive following N1 and N2 (using numeration as above image)
1704 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
1705 // i.e. first nodes from both arrays determ new diagonal
1706 const SMDS_MeshNode* aNodes[8];
1715 if( tr1->GetID() < tr2->GetID() ) {
1716 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 8 );
1717 myLastCreatedElems.Append(tr1);
1718 GetMeshDS()->RemoveElement( tr3 );
1721 GetMeshDS()->ChangeElementNodes( tr3, aNodes, 8 );
1722 myLastCreatedElems.Append(tr3);
1723 GetMeshDS()->RemoveElement( tr1 );
1725 // remove middle node (9)
1726 GetMeshDS()->RemoveNode( N1[4] );
1730 // Next element to fuse: the rejected one
1732 startElem = Ok12 ? tr3 : tr2;
1734 } // if ( startElem )
1735 } // while ( startElem || !startLinks.empty() )
1736 } // while ( ! mapEl_setLi.empty() )
1742 /*#define DUMPSO(txt) \
1743 // cout << txt << endl;
1744 //=============================================================================
1748 //=============================================================================
1749 static void swap( int i1, int i2, int idNodes[], gp_Pnt P[] )
1753 int tmp = idNodes[ i1 ];
1754 idNodes[ i1 ] = idNodes[ i2 ];
1755 idNodes[ i2 ] = tmp;
1756 gp_Pnt Ptmp = P[ i1 ];
1759 DUMPSO( i1 << "(" << idNodes[ i2 ] << ") <-> " << i2 << "(" << idNodes[ i1 ] << ")");
1762 //=======================================================================
1763 //function : SortQuadNodes
1764 //purpose : Set 4 nodes of a quadrangle face in a good order.
1765 // Swap 1<->2 or 2<->3 nodes and correspondingly return
1767 //=======================================================================
1769 int SMESH_MeshEditor::SortQuadNodes (const SMDS_Mesh * theMesh,
1774 for ( i = 0; i < 4; i++ ) {
1775 const SMDS_MeshNode *n = theMesh->FindNode( idNodes[i] );
1777 P[ i ].SetCoord( n->X(), n->Y(), n->Z() );
1780 gp_Vec V1(P[0], P[1]);
1781 gp_Vec V2(P[0], P[2]);
1782 gp_Vec V3(P[0], P[3]);
1784 gp_Vec Cross1 = V1 ^ V2;
1785 gp_Vec Cross2 = V2 ^ V3;
1788 if (Cross1.Dot(Cross2) < 0)
1793 if (Cross1.Dot(Cross2) < 0)
1797 swap ( i, i + 1, idNodes, P );
1799 // for ( int ii = 0; ii < 4; ii++ ) {
1800 // const SMDS_MeshNode *n = theMesh->FindNode( idNodes[ii] );
1801 // DUMPSO( ii << "(" << idNodes[ii] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1807 //=======================================================================
1808 //function : SortHexaNodes
1809 //purpose : Set 8 nodes of a hexahedron in a good order.
1810 // Return success status
1811 //=======================================================================
1813 bool SMESH_MeshEditor::SortHexaNodes (const SMDS_Mesh * theMesh,
1818 DUMPSO( "INPUT: ========================================");
1819 for ( i = 0; i < 8; i++ ) {
1820 const SMDS_MeshNode *n = theMesh->FindNode( idNodes[i] );
1821 if ( !n ) return false;
1822 P[ i ].SetCoord( n->X(), n->Y(), n->Z() );
1823 DUMPSO( i << "(" << idNodes[i] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1825 DUMPSO( "========================================");
1828 set<int> faceNodes; // ids of bottom face nodes, to be found
1829 set<int> checkedId1; // ids of tried 2-nd nodes
1830 Standard_Real leastDist = DBL_MAX; // dist of the 4-th node from 123 plane
1831 const Standard_Real tol = 1.e-6; // tolerance to find nodes in plane
1832 int iMin, iLoop1 = 0;
1834 // Loop to try the 2-nd nodes
1836 while ( leastDist > DBL_MIN && ++iLoop1 < 8 )
1838 // Find not checked 2-nd node
1839 for ( i = 1; i < 8; i++ )
1840 if ( checkedId1.find( idNodes[i] ) == checkedId1.end() ) {
1841 int id1 = idNodes[i];
1842 swap ( 1, i, idNodes, P );
1843 checkedId1.insert ( id1 );
1847 // Find the 3-d node so that 1-2-3 triangle to be on a hexa face,
1848 // ie that all but meybe one (id3 which is on the same face) nodes
1849 // lay on the same side from the triangle plane.
1851 bool manyInPlane = false; // more than 4 nodes lay in plane
1853 while ( ++iLoop2 < 6 ) {
1855 // get 1-2-3 plane coeffs
1856 Standard_Real A, B, C, D;
1857 gp_Vec N = gp_Vec (P[0], P[1]).Crossed( gp_Vec (P[0], P[2]) );
1858 if ( N.SquareMagnitude() > gp::Resolution() )
1860 gp_Pln pln ( P[0], N );
1861 pln.Coefficients( A, B, C, D );
1863 // find the node (iMin) closest to pln
1864 Standard_Real dist[ 8 ], minDist = DBL_MAX;
1866 for ( i = 3; i < 8; i++ ) {
1867 dist[i] = A * P[i].X() + B * P[i].Y() + C * P[i].Z() + D;
1868 if ( fabs( dist[i] ) < minDist ) {
1869 minDist = fabs( dist[i] );
1872 if ( fabs( dist[i] ) <= tol )
1873 idInPln.insert( idNodes[i] );
1876 // there should not be more than 4 nodes in bottom plane
1877 if ( idInPln.size() > 1 )
1879 DUMPSO( "### idInPln.size() = " << idInPln.size());
1880 // idInPlane does not contain the first 3 nodes
1881 if ( manyInPlane || idInPln.size() == 5)
1882 return false; // all nodes in one plane
1885 // set the 1-st node to be not in plane
1886 for ( i = 3; i < 8; i++ ) {
1887 if ( idInPln.find( idNodes[ i ] ) == idInPln.end() ) {
1888 DUMPSO( "### Reset 0-th node");
1889 swap( 0, i, idNodes, P );
1894 // reset to re-check second nodes
1895 leastDist = DBL_MAX;
1899 break; // from iLoop2;
1902 // check that the other 4 nodes are on the same side
1903 bool sameSide = true;
1904 bool isNeg = dist[ iMin == 3 ? 4 : 3 ] <= 0.;
1905 for ( i = 3; sameSide && i < 8; i++ ) {
1907 sameSide = ( isNeg == dist[i] <= 0.);
1910 // keep best solution
1911 if ( sameSide && minDist < leastDist ) {
1912 leastDist = minDist;
1914 faceNodes.insert( idNodes[ 1 ] );
1915 faceNodes.insert( idNodes[ 2 ] );
1916 faceNodes.insert( idNodes[ iMin ] );
1917 DUMPSO( "loop " << iLoop2 << " id2 " << idNodes[ 1 ] << " id3 " << idNodes[ 2 ]
1918 << " leastDist = " << leastDist);
1919 if ( leastDist <= DBL_MIN )
1924 // set next 3-d node to check
1925 int iNext = 2 + iLoop2;
1927 DUMPSO( "Try 2-nd");
1928 swap ( 2, iNext, idNodes, P );
1930 } // while ( iLoop2 < 6 )
1933 if ( faceNodes.empty() ) return false;
1935 // Put the faceNodes in proper places
1936 for ( i = 4; i < 8; i++ ) {
1937 if ( faceNodes.find( idNodes[ i ] ) != faceNodes.end() ) {
1938 // find a place to put
1940 while ( faceNodes.find( idNodes[ iTo ] ) != faceNodes.end() )
1942 DUMPSO( "Set faceNodes");
1943 swap ( iTo, i, idNodes, P );
1948 // Set nodes of the found bottom face in good order
1949 DUMPSO( " Found bottom face: ");
1950 i = SortQuadNodes( theMesh, idNodes );
1952 gp_Pnt Ptmp = P[ i ];
1957 // for ( int ii = 0; ii < 4; ii++ ) {
1958 // const SMDS_MeshNode *n = theMesh->FindNode( idNodes[ii] );
1959 // DUMPSO( ii << "(" << idNodes[ii] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1962 // Gravity center of the top and bottom faces
1963 gp_Pnt aGCb = ( P[0].XYZ() + P[1].XYZ() + P[2].XYZ() + P[3].XYZ() ) / 4.;
1964 gp_Pnt aGCt = ( P[4].XYZ() + P[5].XYZ() + P[6].XYZ() + P[7].XYZ() ) / 4.;
1966 // Get direction from the bottom to the top face
1967 gp_Vec upDir ( aGCb, aGCt );
1968 Standard_Real upDirSize = upDir.Magnitude();
1969 if ( upDirSize <= gp::Resolution() ) return false;
1972 // Assure that the bottom face normal points up
1973 gp_Vec Nb = gp_Vec (P[0], P[1]).Crossed( gp_Vec (P[0], P[2]) );
1974 Nb += gp_Vec (P[0], P[2]).Crossed( gp_Vec (P[0], P[3]) );
1975 if ( Nb.Dot( upDir ) < 0 ) {
1976 DUMPSO( "Reverse bottom face");
1977 swap( 1, 3, idNodes, P );
1980 // Find 5-th node - the one closest to the 1-st among the last 4 nodes.
1981 Standard_Real minDist = DBL_MAX;
1982 for ( i = 4; i < 8; i++ ) {
1983 // projection of P[i] to the plane defined by P[0] and upDir
1984 gp_Pnt Pp = P[i].Translated( upDir * ( upDir.Dot( gp_Vec( P[i], P[0] ))));
1985 Standard_Real sqDist = P[0].SquareDistance( Pp );
1986 if ( sqDist < minDist ) {
1991 DUMPSO( "Set 4-th");
1992 swap ( 4, iMin, idNodes, P );
1994 // Set nodes of the top face in good order
1995 DUMPSO( "Sort top face");
1996 i = SortQuadNodes( theMesh, &idNodes[4] );
1999 gp_Pnt Ptmp = P[ i ];
2004 // Assure that direction of the top face normal is from the bottom face
2005 gp_Vec Nt = gp_Vec (P[4], P[5]).Crossed( gp_Vec (P[4], P[6]) );
2006 Nt += gp_Vec (P[4], P[6]).Crossed( gp_Vec (P[4], P[7]) );
2007 if ( Nt.Dot( upDir ) < 0 ) {
2008 DUMPSO( "Reverse top face");
2009 swap( 5, 7, idNodes, P );
2012 // DUMPSO( "OUTPUT: ========================================");
2013 // for ( i = 0; i < 8; i++ ) {
2014 // float *p = ugrid->GetPoint(idNodes[i]);
2015 // DUMPSO( i << "(" << idNodes[i] << ") : " << p[0] << " " << p[1] << " " << p[2]);
2021 //=======================================================================
2022 //function : laplacianSmooth
2023 //purpose : pulls theNode toward the center of surrounding nodes directly
2024 // connected to that node along an element edge
2025 //=======================================================================
2027 void laplacianSmooth(const SMDS_MeshNode* theNode,
2028 const Handle(Geom_Surface)& theSurface,
2029 map< const SMDS_MeshNode*, gp_XY* >& theUVMap)
2031 // find surrounding nodes
2033 set< const SMDS_MeshNode* > nodeSet;
2034 SMDS_ElemIteratorPtr elemIt = theNode->GetInverseElementIterator(SMDSAbs_Face);
2035 while ( elemIt->more() )
2037 const SMDS_MeshElement* elem = elemIt->next();
2039 for ( int i = 0; i < elem->NbNodes(); ++i ) {
2040 if ( elem->GetNode( i ) == theNode ) {
2042 int iBefore = i - 1;
2044 if ( elem->IsQuadratic() ) {
2045 int nbCorners = elem->NbNodes() / 2;
2046 if ( iAfter >= nbCorners )
2047 iAfter = 0; // elem->GetNode() wraps index
2048 if ( iBefore == -1 )
2049 iBefore = nbCorners - 1;
2051 nodeSet.insert( elem->GetNode( iAfter ));
2052 nodeSet.insert( elem->GetNode( iBefore ));
2058 // compute new coodrs
2060 double coord[] = { 0., 0., 0. };
2061 set< const SMDS_MeshNode* >::iterator nodeSetIt = nodeSet.begin();
2062 for ( ; nodeSetIt != nodeSet.end(); nodeSetIt++ ) {
2063 const SMDS_MeshNode* node = (*nodeSetIt);
2064 if ( theSurface.IsNull() ) { // smooth in 3D
2065 coord[0] += node->X();
2066 coord[1] += node->Y();
2067 coord[2] += node->Z();
2069 else { // smooth in 2D
2070 ASSERT( theUVMap.find( node ) != theUVMap.end() );
2071 gp_XY* uv = theUVMap[ node ];
2072 coord[0] += uv->X();
2073 coord[1] += uv->Y();
2076 int nbNodes = nodeSet.size();
2079 coord[0] /= nbNodes;
2080 coord[1] /= nbNodes;
2082 if ( !theSurface.IsNull() ) {
2083 ASSERT( theUVMap.find( theNode ) != theUVMap.end() );
2084 theUVMap[ theNode ]->SetCoord( coord[0], coord[1] );
2085 gp_Pnt p3d = theSurface->Value( coord[0], coord[1] );
2091 coord[2] /= nbNodes;
2095 const_cast< SMDS_MeshNode* >( theNode )->setXYZ(coord[0],coord[1],coord[2]);
2098 //=======================================================================
2099 //function : centroidalSmooth
2100 //purpose : pulls theNode toward the element-area-weighted centroid of the
2101 // surrounding elements
2102 //=======================================================================
2104 void centroidalSmooth(const SMDS_MeshNode* theNode,
2105 const Handle(Geom_Surface)& theSurface,
2106 map< const SMDS_MeshNode*, gp_XY* >& theUVMap)
2108 gp_XYZ aNewXYZ(0.,0.,0.);
2109 SMESH::Controls::Area anAreaFunc;
2110 double totalArea = 0.;
2115 SMDS_ElemIteratorPtr elemIt = theNode->GetInverseElementIterator(SMDSAbs_Face);
2116 while ( elemIt->more() )
2118 const SMDS_MeshElement* elem = elemIt->next();
2121 gp_XYZ elemCenter(0.,0.,0.);
2122 SMESH::Controls::TSequenceOfXYZ aNodePoints;
2123 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2124 int nn = elem->NbNodes();
2125 if(elem->IsQuadratic()) nn = nn/2;
2127 //while ( itN->more() ) {
2129 const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>( itN->next() );
2131 gp_XYZ aP( aNode->X(), aNode->Y(), aNode->Z() );
2132 aNodePoints.push_back( aP );
2133 if ( !theSurface.IsNull() ) { // smooth in 2D
2134 ASSERT( theUVMap.find( aNode ) != theUVMap.end() );
2135 gp_XY* uv = theUVMap[ aNode ];
2136 aP.SetCoord( uv->X(), uv->Y(), 0. );
2140 double elemArea = anAreaFunc.GetValue( aNodePoints );
2141 totalArea += elemArea;
2143 aNewXYZ += elemCenter * elemArea;
2145 aNewXYZ /= totalArea;
2146 if ( !theSurface.IsNull() ) {
2147 theUVMap[ theNode ]->SetCoord( aNewXYZ.X(), aNewXYZ.Y() );
2148 aNewXYZ = theSurface->Value( aNewXYZ.X(), aNewXYZ.Y() ).XYZ();
2153 const_cast< SMDS_MeshNode* >( theNode )->setXYZ(aNewXYZ.X(),aNewXYZ.Y(),aNewXYZ.Z());
2156 //=======================================================================
2157 //function : getClosestUV
2158 //purpose : return UV of closest projection
2159 //=======================================================================
2161 static bool getClosestUV (Extrema_GenExtPS& projector,
2162 const gp_Pnt& point,
2165 projector.Perform( point );
2166 if ( projector.IsDone() ) {
2167 double u, v, minVal = DBL_MAX;
2168 for ( int i = projector.NbExt(); i > 0; i-- )
2169 if ( projector.Value( i ) < minVal ) {
2170 minVal = projector.Value( i );
2171 projector.Point( i ).Parameter( u, v );
2173 result.SetCoord( u, v );
2179 //=======================================================================
2181 //purpose : Smooth theElements during theNbIterations or until a worst
2182 // element has aspect ratio <= theTgtAspectRatio.
2183 // Aspect Ratio varies in range [1.0, inf].
2184 // If theElements is empty, the whole mesh is smoothed.
2185 // theFixedNodes contains additionally fixed nodes. Nodes built
2186 // on edges and boundary nodes are always fixed.
2187 //=======================================================================
2189 void SMESH_MeshEditor::Smooth (TIDSortedElemSet & theElems,
2190 set<const SMDS_MeshNode*> & theFixedNodes,
2191 const SmoothMethod theSmoothMethod,
2192 const int theNbIterations,
2193 double theTgtAspectRatio,
2196 myLastCreatedElems.Clear();
2197 myLastCreatedNodes.Clear();
2199 MESSAGE((theSmoothMethod==LAPLACIAN ? "LAPLACIAN" : "CENTROIDAL") << "--::Smooth()");
2201 if ( theTgtAspectRatio < 1.0 )
2202 theTgtAspectRatio = 1.0;
2204 const double disttol = 1.e-16;
2206 SMESH::Controls::AspectRatio aQualityFunc;
2208 SMESHDS_Mesh* aMesh = GetMeshDS();
2210 if ( theElems.empty() ) {
2211 // add all faces to theElems
2212 SMDS_FaceIteratorPtr fIt = aMesh->facesIterator();
2213 while ( fIt->more() ) {
2214 const SMDS_MeshElement* face = fIt->next();
2215 theElems.insert( face );
2218 // get all face ids theElems are on
2219 set< int > faceIdSet;
2220 TIDSortedElemSet::iterator itElem;
2222 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
2223 int fId = FindShape( *itElem );
2224 // check that corresponding submesh exists and a shape is face
2226 faceIdSet.find( fId ) == faceIdSet.end() &&
2227 aMesh->MeshElements( fId )) {
2228 TopoDS_Shape F = aMesh->IndexToShape( fId );
2229 if ( !F.IsNull() && F.ShapeType() == TopAbs_FACE )
2230 faceIdSet.insert( fId );
2233 faceIdSet.insert( 0 ); // to smooth elements that are not on any TopoDS_Face
2235 // ===============================================
2236 // smooth elements on each TopoDS_Face separately
2237 // ===============================================
2239 set< int >::reverse_iterator fId = faceIdSet.rbegin(); // treate 0 fId at the end
2240 for ( ; fId != faceIdSet.rend(); ++fId ) {
2241 // get face surface and submesh
2242 Handle(Geom_Surface) surface;
2243 SMESHDS_SubMesh* faceSubMesh = 0;
2245 double fToler2 = 0, vPeriod = 0., uPeriod = 0., f,l;
2246 double u1 = 0, u2 = 0, v1 = 0, v2 = 0;
2247 bool isUPeriodic = false, isVPeriodic = false;
2249 face = TopoDS::Face( aMesh->IndexToShape( *fId ));
2250 surface = BRep_Tool::Surface( face );
2251 faceSubMesh = aMesh->MeshElements( *fId );
2252 fToler2 = BRep_Tool::Tolerance( face );
2253 fToler2 *= fToler2 * 10.;
2254 isUPeriodic = surface->IsUPeriodic();
2256 vPeriod = surface->UPeriod();
2257 isVPeriodic = surface->IsVPeriodic();
2259 uPeriod = surface->VPeriod();
2260 surface->Bounds( u1, u2, v1, v2 );
2262 // ---------------------------------------------------------
2263 // for elements on a face, find movable and fixed nodes and
2264 // compute UV for them
2265 // ---------------------------------------------------------
2266 bool checkBoundaryNodes = false;
2267 bool isQuadratic = false;
2268 set<const SMDS_MeshNode*> setMovableNodes;
2269 map< const SMDS_MeshNode*, gp_XY* > uvMap, uvMap2;
2270 list< gp_XY > listUV; // uvs the 2 uvMaps refer to
2271 list< const SMDS_MeshElement* > elemsOnFace;
2273 Extrema_GenExtPS projector;
2274 GeomAdaptor_Surface surfAdaptor;
2275 if ( !surface.IsNull() ) {
2276 surfAdaptor.Load( surface );
2277 projector.Initialize( surfAdaptor, 20,20, 1e-5,1e-5 );
2279 int nbElemOnFace = 0;
2280 itElem = theElems.begin();
2281 // loop on not yet smoothed elements: look for elems on a face
2282 while ( itElem != theElems.end() ) {
2283 if ( faceSubMesh && nbElemOnFace == faceSubMesh->NbElements() )
2284 break; // all elements found
2286 const SMDS_MeshElement* elem = *itElem;
2287 if ( !elem || elem->GetType() != SMDSAbs_Face || elem->NbNodes() < 3 ||
2288 ( faceSubMesh && !faceSubMesh->Contains( elem ))) {
2292 elemsOnFace.push_back( elem );
2293 theElems.erase( itElem++ );
2297 isQuadratic = elem->IsQuadratic();
2299 // get movable nodes of elem
2300 const SMDS_MeshNode* node;
2301 SMDS_TypeOfPosition posType;
2302 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2303 int nn = 0, nbn = elem->NbNodes();
2304 if(elem->IsQuadratic())
2306 while ( nn++ < nbn ) {
2307 node = static_cast<const SMDS_MeshNode*>( itN->next() );
2308 const SMDS_PositionPtr& pos = node->GetPosition();
2309 posType = pos.get() ? pos->GetTypeOfPosition() : SMDS_TOP_3DSPACE;
2310 if (posType != SMDS_TOP_EDGE &&
2311 posType != SMDS_TOP_VERTEX &&
2312 theFixedNodes.find( node ) == theFixedNodes.end())
2314 // check if all faces around the node are on faceSubMesh
2315 // because a node on edge may be bound to face
2316 SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator(SMDSAbs_Face);
2318 if ( faceSubMesh ) {
2319 while ( eIt->more() && all ) {
2320 const SMDS_MeshElement* e = eIt->next();
2321 all = faceSubMesh->Contains( e );
2325 setMovableNodes.insert( node );
2327 checkBoundaryNodes = true;
2329 if ( posType == SMDS_TOP_3DSPACE )
2330 checkBoundaryNodes = true;
2333 if ( surface.IsNull() )
2336 // get nodes to check UV
2337 list< const SMDS_MeshNode* > uvCheckNodes;
2338 itN = elem->nodesIterator();
2339 nn = 0; nbn = elem->NbNodes();
2340 if(elem->IsQuadratic())
2342 while ( nn++ < nbn ) {
2343 node = static_cast<const SMDS_MeshNode*>( itN->next() );
2344 if ( uvMap.find( node ) == uvMap.end() )
2345 uvCheckNodes.push_back( node );
2346 // add nodes of elems sharing node
2347 // SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator(SMDSAbs_Face);
2348 // while ( eIt->more() ) {
2349 // const SMDS_MeshElement* e = eIt->next();
2350 // if ( e != elem ) {
2351 // SMDS_ElemIteratorPtr nIt = e->nodesIterator();
2352 // while ( nIt->more() ) {
2353 // const SMDS_MeshNode* n =
2354 // static_cast<const SMDS_MeshNode*>( nIt->next() );
2355 // if ( uvMap.find( n ) == uvMap.end() )
2356 // uvCheckNodes.push_back( n );
2362 list< const SMDS_MeshNode* >::iterator n = uvCheckNodes.begin();
2363 for ( ; n != uvCheckNodes.end(); ++n ) {
2366 const SMDS_PositionPtr& pos = node->GetPosition();
2367 posType = pos.get() ? pos->GetTypeOfPosition() : SMDS_TOP_3DSPACE;
2369 switch ( posType ) {
2370 case SMDS_TOP_FACE: {
2371 SMDS_FacePosition* fPos = ( SMDS_FacePosition* ) pos.get();
2372 uv.SetCoord( fPos->GetUParameter(), fPos->GetVParameter() );
2375 case SMDS_TOP_EDGE: {
2376 TopoDS_Shape S = aMesh->IndexToShape( pos->GetShapeId() );
2377 Handle(Geom2d_Curve) pcurve;
2378 if ( !S.IsNull() && S.ShapeType() == TopAbs_EDGE )
2379 pcurve = BRep_Tool::CurveOnSurface( TopoDS::Edge( S ), face, f,l );
2380 if ( !pcurve.IsNull() ) {
2381 double u = (( SMDS_EdgePosition* ) pos.get() )->GetUParameter();
2382 uv = pcurve->Value( u ).XY();
2386 case SMDS_TOP_VERTEX: {
2387 TopoDS_Shape S = aMesh->IndexToShape( pos->GetShapeId() );
2388 if ( !S.IsNull() && S.ShapeType() == TopAbs_VERTEX )
2389 uv = BRep_Tool::Parameters( TopoDS::Vertex( S ), face ).XY();
2394 // check existing UV
2395 bool project = true;
2396 gp_Pnt pNode ( node->X(), node->Y(), node->Z() );
2397 double dist1 = DBL_MAX, dist2 = 0;
2398 if ( posType != SMDS_TOP_3DSPACE ) {
2399 dist1 = pNode.SquareDistance( surface->Value( uv.X(), uv.Y() ));
2400 project = dist1 > fToler2;
2402 if ( project ) { // compute new UV
2404 if ( !getClosestUV( projector, pNode, newUV )) {
2405 MESSAGE("Node Projection Failed " << node);
2409 newUV.SetX( ElCLib::InPeriod( newUV.X(), u1, u2 ));
2411 newUV.SetY( ElCLib::InPeriod( newUV.Y(), v1, v2 ));
2413 if ( posType != SMDS_TOP_3DSPACE )
2414 dist2 = pNode.SquareDistance( surface->Value( newUV.X(), newUV.Y() ));
2415 if ( dist2 < dist1 )
2419 // store UV in the map
2420 listUV.push_back( uv );
2421 uvMap.insert( make_pair( node, &listUV.back() ));
2423 } // loop on not yet smoothed elements
2425 if ( !faceSubMesh || nbElemOnFace != faceSubMesh->NbElements() )
2426 checkBoundaryNodes = true;
2428 // fix nodes on mesh boundary
2430 if ( checkBoundaryNodes ) {
2431 typedef pair<const SMDS_MeshNode*, const SMDS_MeshNode*> TLink;
2432 map< TLink, int > linkNbMap; // how many times a link encounters in elemsOnFace
2433 map< TLink, int >::iterator link_nb;
2434 // put all elements links to linkNbMap
2435 list< const SMDS_MeshElement* >::iterator elemIt = elemsOnFace.begin();
2436 for ( ; elemIt != elemsOnFace.end(); ++elemIt ) {
2437 const SMDS_MeshElement* elem = (*elemIt);
2438 int nbn = elem->NbNodes();
2439 if(elem->IsQuadratic())
2441 // loop on elem links: insert them in linkNbMap
2442 const SMDS_MeshNode* curNode, *prevNode = elem->GetNode( nbn );
2443 for ( int iN = 0; iN < nbn; ++iN ) {
2444 curNode = elem->GetNode( iN );
2446 if ( curNode < prevNode ) link = make_pair( curNode , prevNode );
2447 else link = make_pair( prevNode , curNode );
2449 link_nb = linkNbMap.find( link );
2450 if ( link_nb == linkNbMap.end() )
2451 linkNbMap.insert( make_pair ( link, 1 ));
2456 // remove nodes that are in links encountered only once from setMovableNodes
2457 for ( link_nb = linkNbMap.begin(); link_nb != linkNbMap.end(); ++link_nb ) {
2458 if ( link_nb->second == 1 ) {
2459 setMovableNodes.erase( link_nb->first.first );
2460 setMovableNodes.erase( link_nb->first.second );
2465 // -----------------------------------------------------
2466 // for nodes on seam edge, compute one more UV ( uvMap2 );
2467 // find movable nodes linked to nodes on seam and which
2468 // are to be smoothed using the second UV ( uvMap2 )
2469 // -----------------------------------------------------
2471 set<const SMDS_MeshNode*> nodesNearSeam; // to smooth using uvMap2
2472 if ( !surface.IsNull() ) {
2473 TopExp_Explorer eExp( face, TopAbs_EDGE );
2474 for ( ; eExp.More(); eExp.Next() ) {
2475 TopoDS_Edge edge = TopoDS::Edge( eExp.Current() );
2476 if ( !BRep_Tool::IsClosed( edge, face ))
2478 SMESHDS_SubMesh* sm = aMesh->MeshElements( edge );
2479 if ( !sm ) continue;
2480 // find out which parameter varies for a node on seam
2483 Handle(Geom2d_Curve) pcurve = BRep_Tool::CurveOnSurface( edge, face, f, l );
2484 if ( pcurve.IsNull() ) continue;
2485 uv1 = pcurve->Value( f );
2487 pcurve = BRep_Tool::CurveOnSurface( edge, face, f, l );
2488 if ( pcurve.IsNull() ) continue;
2489 uv2 = pcurve->Value( f );
2490 int iPar = Abs( uv1.X() - uv2.X() ) > Abs( uv1.Y() - uv2.Y() ) ? 1 : 2;
2492 if ( uv1.Coord( iPar ) > uv2.Coord( iPar )) {
2493 gp_Pnt2d tmp = uv1; uv1 = uv2; uv2 = tmp;
2495 // get nodes on seam and its vertices
2496 list< const SMDS_MeshNode* > seamNodes;
2497 SMDS_NodeIteratorPtr nSeamIt = sm->GetNodes();
2498 while ( nSeamIt->more() ) {
2499 const SMDS_MeshNode* node = nSeamIt->next();
2500 if ( !isQuadratic || !IsMedium( node ))
2501 seamNodes.push_back( node );
2503 TopExp_Explorer vExp( edge, TopAbs_VERTEX );
2504 for ( ; vExp.More(); vExp.Next() ) {
2505 sm = aMesh->MeshElements( vExp.Current() );
2507 nSeamIt = sm->GetNodes();
2508 while ( nSeamIt->more() )
2509 seamNodes.push_back( nSeamIt->next() );
2512 // loop on nodes on seam
2513 list< const SMDS_MeshNode* >::iterator noSeIt = seamNodes.begin();
2514 for ( ; noSeIt != seamNodes.end(); ++noSeIt ) {
2515 const SMDS_MeshNode* nSeam = *noSeIt;
2516 map< const SMDS_MeshNode*, gp_XY* >::iterator n_uv = uvMap.find( nSeam );
2517 if ( n_uv == uvMap.end() )
2520 n_uv->second->SetCoord( iPar, uv1.Coord( iPar ));
2521 // set the second UV
2522 listUV.push_back( *n_uv->second );
2523 listUV.back().SetCoord( iPar, uv2.Coord( iPar ));
2524 if ( uvMap2.empty() )
2525 uvMap2 = uvMap; // copy the uvMap contents
2526 uvMap2[ nSeam ] = &listUV.back();
2528 // collect movable nodes linked to ones on seam in nodesNearSeam
2529 SMDS_ElemIteratorPtr eIt = nSeam->GetInverseElementIterator(SMDSAbs_Face);
2530 while ( eIt->more() ) {
2531 const SMDS_MeshElement* e = eIt->next();
2532 int nbUseMap1 = 0, nbUseMap2 = 0;
2533 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
2534 int nn = 0, nbn = e->NbNodes();
2535 if(e->IsQuadratic()) nbn = nbn/2;
2536 while ( nn++ < nbn )
2538 const SMDS_MeshNode* n =
2539 static_cast<const SMDS_MeshNode*>( nIt->next() );
2541 setMovableNodes.find( n ) == setMovableNodes.end() )
2543 // add only nodes being closer to uv2 than to uv1
2544 gp_Pnt pMid (0.5 * ( n->X() + nSeam->X() ),
2545 0.5 * ( n->Y() + nSeam->Y() ),
2546 0.5 * ( n->Z() + nSeam->Z() ));
2548 getClosestUV( projector, pMid, uv );
2549 if ( uv.Coord( iPar ) > uvMap[ n ]->Coord( iPar ) ) {
2550 nodesNearSeam.insert( n );
2556 // for centroidalSmooth all element nodes must
2557 // be on one side of a seam
2558 if ( theSmoothMethod == CENTROIDAL && nbUseMap1 && nbUseMap2 ) {
2559 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
2561 while ( nn++ < nbn ) {
2562 const SMDS_MeshNode* n =
2563 static_cast<const SMDS_MeshNode*>( nIt->next() );
2564 setMovableNodes.erase( n );
2568 } // loop on nodes on seam
2569 } // loop on edge of a face
2570 } // if ( !face.IsNull() )
2572 if ( setMovableNodes.empty() ) {
2573 MESSAGE( "Face id : " << *fId << " - NO SMOOTHING: no nodes to move!!!");
2574 continue; // goto next face
2582 double maxRatio = -1., maxDisplacement = -1.;
2583 set<const SMDS_MeshNode*>::iterator nodeToMove;
2584 for ( it = 0; it < theNbIterations; it++ ) {
2585 maxDisplacement = 0.;
2586 nodeToMove = setMovableNodes.begin();
2587 for ( ; nodeToMove != setMovableNodes.end(); nodeToMove++ ) {
2588 const SMDS_MeshNode* node = (*nodeToMove);
2589 gp_XYZ aPrevPos ( node->X(), node->Y(), node->Z() );
2592 bool map2 = ( nodesNearSeam.find( node ) != nodesNearSeam.end() );
2593 if ( theSmoothMethod == LAPLACIAN )
2594 laplacianSmooth( node, surface, map2 ? uvMap2 : uvMap );
2596 centroidalSmooth( node, surface, map2 ? uvMap2 : uvMap );
2598 // node displacement
2599 gp_XYZ aNewPos ( node->X(), node->Y(), node->Z() );
2600 Standard_Real aDispl = (aPrevPos - aNewPos).SquareModulus();
2601 if ( aDispl > maxDisplacement )
2602 maxDisplacement = aDispl;
2604 // no node movement => exit
2605 //if ( maxDisplacement < 1.e-16 ) {
2606 if ( maxDisplacement < disttol ) {
2607 MESSAGE("-- no node movement --");
2611 // check elements quality
2613 list< const SMDS_MeshElement* >::iterator elemIt = elemsOnFace.begin();
2614 for ( ; elemIt != elemsOnFace.end(); ++elemIt ) {
2615 const SMDS_MeshElement* elem = (*elemIt);
2616 if ( !elem || elem->GetType() != SMDSAbs_Face )
2618 SMESH::Controls::TSequenceOfXYZ aPoints;
2619 if ( aQualityFunc.GetPoints( elem, aPoints )) {
2620 double aValue = aQualityFunc.GetValue( aPoints );
2621 if ( aValue > maxRatio )
2625 if ( maxRatio <= theTgtAspectRatio ) {
2626 MESSAGE("-- quality achived --");
2629 if (it+1 == theNbIterations) {
2630 MESSAGE("-- Iteration limit exceeded --");
2632 } // smoothing iterations
2634 MESSAGE(" Face id: " << *fId <<
2635 " Nb iterstions: " << it <<
2636 " Displacement: " << maxDisplacement <<
2637 " Aspect Ratio " << maxRatio);
2639 // ---------------------------------------
2640 // new nodes positions are computed,
2641 // record movement in DS and set new UV
2642 // ---------------------------------------
2643 nodeToMove = setMovableNodes.begin();
2644 for ( ; nodeToMove != setMovableNodes.end(); nodeToMove++ ) {
2645 SMDS_MeshNode* node = const_cast< SMDS_MeshNode* > (*nodeToMove);
2646 aMesh->MoveNode( node, node->X(), node->Y(), node->Z() );
2647 map< const SMDS_MeshNode*, gp_XY* >::iterator node_uv = uvMap.find( node );
2648 if ( node_uv != uvMap.end() ) {
2649 gp_XY* uv = node_uv->second;
2651 ( SMDS_PositionPtr( new SMDS_FacePosition( *fId, uv->X(), uv->Y() )));
2655 // move medium nodes of quadratic elements
2658 SMESH_MesherHelper helper( *GetMesh() );
2659 if ( !face.IsNull() )
2660 helper.SetSubShape( face );
2661 list< const SMDS_MeshElement* >::iterator elemIt = elemsOnFace.begin();
2662 for ( ; elemIt != elemsOnFace.end(); ++elemIt ) {
2663 const SMDS_QuadraticFaceOfNodes* QF =
2664 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (*elemIt);
2666 vector<const SMDS_MeshNode*> Ns;
2667 Ns.reserve(QF->NbNodes()+1);
2668 SMDS_NodeIteratorPtr anIter = QF->interlacedNodesIterator();
2669 while ( anIter->more() )
2670 Ns.push_back( anIter->next() );
2671 Ns.push_back( Ns[0] );
2673 for(int i=0; i<QF->NbNodes(); i=i+2) {
2674 if ( !surface.IsNull() ) {
2675 gp_XY uv1 = helper.GetNodeUV( face, Ns[i], Ns[i+2] );
2676 gp_XY uv2 = helper.GetNodeUV( face, Ns[i+2], Ns[i] );
2677 gp_XY uv = ( uv1 + uv2 ) / 2.;
2678 gp_Pnt xyz = surface->Value( uv.X(), uv.Y() );
2679 x = xyz.X(); y = xyz.Y(); z = xyz.Z();
2682 x = (Ns[i]->X() + Ns[i+2]->X())/2;
2683 y = (Ns[i]->Y() + Ns[i+2]->Y())/2;
2684 z = (Ns[i]->Z() + Ns[i+2]->Z())/2;
2686 if( fabs( Ns[i+1]->X() - x ) > disttol ||
2687 fabs( Ns[i+1]->Y() - y ) > disttol ||
2688 fabs( Ns[i+1]->Z() - z ) > disttol ) {
2689 // we have to move i+1 node
2690 aMesh->MoveNode( Ns[i+1], x, y, z );
2697 } // loop on face ids
2701 //=======================================================================
2702 //function : isReverse
2703 //purpose : Return true if normal of prevNodes is not co-directied with
2704 // gp_Vec(prevNodes[iNotSame],nextNodes[iNotSame]).
2705 // iNotSame is where prevNodes and nextNodes are different
2706 //=======================================================================
2708 static bool isReverse(const SMDS_MeshNode* prevNodes[],
2709 const SMDS_MeshNode* nextNodes[],
2713 int iBeforeNotSame = ( iNotSame == 0 ? nbNodes - 1 : iNotSame - 1 );
2714 int iAfterNotSame = ( iNotSame + 1 == nbNodes ? 0 : iNotSame + 1 );
2716 const SMDS_MeshNode* nB = prevNodes[ iBeforeNotSame ];
2717 const SMDS_MeshNode* nA = prevNodes[ iAfterNotSame ];
2718 const SMDS_MeshNode* nP = prevNodes[ iNotSame ];
2719 const SMDS_MeshNode* nN = nextNodes[ iNotSame ];
2721 gp_Pnt pB ( nB->X(), nB->Y(), nB->Z() );
2722 gp_Pnt pA ( nA->X(), nA->Y(), nA->Z() );
2723 gp_Pnt pP ( nP->X(), nP->Y(), nP->Z() );
2724 gp_Pnt pN ( nN->X(), nN->Y(), nN->Z() );
2726 gp_Vec vB ( pP, pB ), vA ( pP, pA ), vN ( pP, pN );
2728 return (vA ^ vB) * vN < 0.0;
2731 //=======================================================================
2732 //function : sweepElement
2734 //=======================================================================
2736 static void sweepElement(SMESHDS_Mesh* aMesh,
2737 const SMDS_MeshElement* elem,
2738 const vector<TNodeOfNodeListMapItr> & newNodesItVec,
2739 list<const SMDS_MeshElement*>& newElems,
2741 SMESH_SequenceOfElemPtr& myLastCreatedElems)
2743 // Loop on elem nodes:
2744 // find new nodes and detect same nodes indices
2745 int nbNodes = elem->NbNodes();
2746 list<const SMDS_MeshNode*>::const_iterator itNN[ nbNodes ];
2747 const SMDS_MeshNode* prevNod[ nbNodes ], *nextNod[ nbNodes ], *midlNod[ nbNodes ];
2748 int iNode, nbSame = 0, iNotSameNode = 0, iSameNode = 0;
2749 vector<int> sames(nbNodes);
2751 bool issimple[nbNodes];
2753 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
2754 TNodeOfNodeListMapItr nnIt = newNodesItVec[ iNode ];
2755 const SMDS_MeshNode* node = nnIt->first;
2756 const list< const SMDS_MeshNode* > & listNewNodes = nnIt->second;
2757 if ( listNewNodes.empty() )
2760 if(listNewNodes.size()==nbSteps) {
2761 issimple[iNode] = true;
2764 issimple[iNode] = false;
2767 itNN[ iNode ] = listNewNodes.begin();
2768 prevNod[ iNode ] = node;
2769 nextNod[ iNode ] = listNewNodes.front();
2770 //cout<<"iNode="<<iNode<<endl;
2771 //cout<<" prevNod[iNode]="<< prevNod[iNode]<<" nextNod[iNode]="<< nextNod[iNode]<<endl;
2772 if ( prevNod[ iNode ] != nextNod [ iNode ])
2773 iNotSameNode = iNode;
2777 sames[nbSame++] = iNode;
2780 //cout<<"1 nbSame="<<nbSame<<endl;
2781 if ( nbSame == nbNodes || nbSame > 2) {
2782 MESSAGE( " Too many same nodes of element " << elem->GetID() );
2786 // if( elem->IsQuadratic() && nbSame>0 ) {
2787 // MESSAGE( "Can not rotate quadratic element " << elem->GetID() );
2791 int iBeforeSame = 0, iAfterSame = 0, iOpposSame = 0;
2793 iBeforeSame = ( iSameNode == 0 ? nbNodes - 1 : iSameNode - 1 );
2794 iAfterSame = ( iSameNode + 1 == nbNodes ? 0 : iSameNode + 1 );
2795 iOpposSame = ( iSameNode - 2 < 0 ? iSameNode + 2 : iSameNode - 2 );
2799 //cout<<" prevNod[0]="<< prevNod[0]<<" prevNod[1]="<< prevNod[1]
2800 // <<" prevNod[2]="<< prevNod[2]<<" prevNod[3]="<< prevNod[4]
2801 // <<" prevNod[4]="<< prevNod[4]<<" prevNod[5]="<< prevNod[5]
2802 // <<" prevNod[6]="<< prevNod[6]<<" prevNod[7]="<< prevNod[7]<<endl;
2804 // check element orientation
2806 if ( nbNodes > 2 && !isReverse( prevNod, nextNod, nbNodes, iNotSameNode )) {
2807 //MESSAGE("Reversed elem " << elem );
2811 int iAB = iAfterSame + iBeforeSame;
2812 iBeforeSame = iAB - iBeforeSame;
2813 iAfterSame = iAB - iAfterSame;
2817 // make new elements
2818 int iStep;//, nbSteps = newNodesItVec[ 0 ]->second.size();
2819 for (iStep = 0; iStep < nbSteps; iStep++ ) {
2821 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
2822 if(issimple[iNode]) {
2823 nextNod[ iNode ] = *itNN[ iNode ];
2827 if( elem->GetType()==SMDSAbs_Node ) {
2828 // we have to use two nodes
2829 midlNod[ iNode ] = *itNN[ iNode ];
2831 nextNod[ iNode ] = *itNN[ iNode ];
2834 else if(!elem->IsQuadratic() ||
2835 elem->IsQuadratic() && elem->IsMediumNode(prevNod[iNode]) ) {
2836 // we have to use each second node
2838 nextNod[ iNode ] = *itNN[ iNode ];
2842 // we have to use two nodes
2843 midlNod[ iNode ] = *itNN[ iNode ];
2845 nextNod[ iNode ] = *itNN[ iNode ];
2850 SMDS_MeshElement* aNewElem = 0;
2851 if(!elem->IsPoly()) {
2852 switch ( nbNodes ) {
2856 if ( nbSame == 0 ) {
2858 aNewElem = aMesh->AddEdge( prevNod[ 0 ], nextNod[ 0 ] );
2860 aNewElem = aMesh->AddEdge( prevNod[ 0 ], nextNod[ 0 ], midlNod[ 0 ] );
2866 aNewElem = aMesh->AddFace(prevNod[ 0 ], prevNod[ 1 ],
2867 nextNod[ 1 ], nextNod[ 0 ] );
2869 aNewElem = aMesh->AddFace(prevNod[ 0 ], prevNod[ 1 ],
2870 nextNod[ iNotSameNode ] );
2874 case 3: { // TRIANGLE or quadratic edge
2875 if(elem->GetType() == SMDSAbs_Face) { // TRIANGLE
2877 if ( nbSame == 0 ) // --- pentahedron
2878 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ],
2879 nextNod[ i0 ], nextNod[ 1 ], nextNod[ i2 ] );
2881 else if ( nbSame == 1 ) // --- pyramid
2882 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iAfterSame ],
2883 nextNod[ iAfterSame ], nextNod[ iBeforeSame ],
2884 nextNod[ iSameNode ]);
2886 else // 2 same nodes: --- tetrahedron
2887 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ],
2888 nextNod[ iNotSameNode ]);
2890 else { // quadratic edge
2891 if(nbSame==0) { // quadratic quadrangle
2892 aNewElem = aMesh->AddFace(prevNod[0], nextNod[0], nextNod[1], prevNod[1],
2893 midlNod[0], nextNod[2], midlNod[1], prevNod[2]);
2895 else if(nbSame==1) { // quadratic triangle
2897 return; // medium node on axis
2898 else if(sames[0]==0) {
2899 aNewElem = aMesh->AddFace(prevNod[0], nextNod[1], prevNod[1],
2900 nextNod[2], midlNod[1], prevNod[2]);
2902 else { // sames[0]==1
2903 aNewElem = aMesh->AddFace(prevNod[0], nextNod[0], prevNod[1],
2904 midlNod[0], nextNod[2], prevNod[2]);
2912 case 4: { // QUADRANGLE
2914 if ( nbSame == 0 ) // --- hexahedron
2915 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ], prevNod[ 3 ],
2916 nextNod[ i0 ], nextNod[ 1 ], nextNod[ i2 ], nextNod[ 3 ]);
2918 else if ( nbSame == 1 ) { // --- pyramid + pentahedron
2919 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iAfterSame ],
2920 nextNod[ iAfterSame ], nextNod[ iBeforeSame ],
2921 nextNod[ iSameNode ]);
2922 newElems.push_back( aNewElem );
2923 aNewElem = aMesh->AddVolume (prevNod[ iAfterSame ], prevNod[ iOpposSame ],
2924 prevNod[ iBeforeSame ], nextNod[ iAfterSame ],
2925 nextNod[ iOpposSame ], nextNod[ iBeforeSame ] );
2927 else if ( nbSame == 2 ) { // pentahedron
2928 if ( prevNod[ iBeforeSame ] == nextNod[ iBeforeSame ] )
2929 // iBeforeSame is same too
2930 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iOpposSame ],
2931 nextNod[ iOpposSame ], prevNod[ iSameNode ],
2932 prevNod[ iAfterSame ], nextNod[ iAfterSame ]);
2934 // iAfterSame is same too
2935 aNewElem = aMesh->AddVolume (prevNod[ iSameNode ], prevNod[ iBeforeSame ],
2936 nextNod[ iBeforeSame ], prevNod[ iAfterSame ],
2937 prevNod[ iOpposSame ], nextNod[ iOpposSame ]);
2941 case 6: { // quadratic triangle
2942 // create pentahedron with 15 nodes
2943 if(i0>0) { // reversed case
2944 aNewElem = aMesh->AddVolume (prevNod[0], prevNod[2], prevNod[1],
2945 nextNod[0], nextNod[2], nextNod[1],
2946 prevNod[5], prevNod[4], prevNod[3],
2947 nextNod[5], nextNod[4], nextNod[3],
2948 midlNod[0], midlNod[2], midlNod[1]);
2950 else { // not reversed case
2951 aNewElem = aMesh->AddVolume (prevNod[0], prevNod[1], prevNod[2],
2952 nextNod[0], nextNod[1], nextNod[2],
2953 prevNod[3], prevNod[4], prevNod[5],
2954 nextNod[3], nextNod[4], nextNod[5],
2955 midlNod[0], midlNod[1], midlNod[2]);
2959 case 8: { // quadratic quadrangle
2960 // create hexahedron with 20 nodes
2961 if(i0>0) { // reversed case
2962 aNewElem = aMesh->AddVolume (prevNod[0], prevNod[3], prevNod[2], prevNod[1],
2963 nextNod[0], nextNod[3], nextNod[2], nextNod[1],
2964 prevNod[7], prevNod[6], prevNod[5], prevNod[4],
2965 nextNod[7], nextNod[6], nextNod[5], nextNod[4],
2966 midlNod[0], midlNod[3], midlNod[2], midlNod[1]);
2968 else { // not reversed case
2969 aNewElem = aMesh->AddVolume (prevNod[0], prevNod[1], prevNod[2], prevNod[3],
2970 nextNod[0], nextNod[1], nextNod[2], nextNod[3],
2971 prevNod[4], prevNod[5], prevNod[6], prevNod[7],
2972 nextNod[4], nextNod[5], nextNod[6], nextNod[7],
2973 midlNod[0], midlNod[1], midlNod[2], midlNod[3]);
2978 // realized for extrusion only
2979 //vector<const SMDS_MeshNode*> polyedre_nodes (nbNodes*2 + 4*nbNodes);
2980 //vector<int> quantities (nbNodes + 2);
2982 //quantities[0] = nbNodes; // bottom of prism
2983 //for (int inode = 0; inode < nbNodes; inode++) {
2984 // polyedre_nodes[inode] = prevNod[inode];
2987 //quantities[1] = nbNodes; // top of prism
2988 //for (int inode = 0; inode < nbNodes; inode++) {
2989 // polyedre_nodes[nbNodes + inode] = nextNod[inode];
2992 //for (int iface = 0; iface < nbNodes; iface++) {
2993 // quantities[iface + 2] = 4;
2994 // int inextface = (iface == nbNodes - 1) ? 0 : iface + 1;
2995 // polyedre_nodes[2*nbNodes + 4*iface + 0] = prevNod[iface];
2996 // polyedre_nodes[2*nbNodes + 4*iface + 1] = prevNod[inextface];
2997 // polyedre_nodes[2*nbNodes + 4*iface + 2] = nextNod[inextface];
2998 // polyedre_nodes[2*nbNodes + 4*iface + 3] = nextNod[iface];
3000 //aNewElem = aMesh->AddPolyhedralVolume (polyedre_nodes, quantities);
3007 // realized for extrusion only
3008 vector<const SMDS_MeshNode*> polyedre_nodes (nbNodes*2 + 4*nbNodes);
3009 vector<int> quantities (nbNodes + 2);
3011 quantities[0] = nbNodes; // bottom of prism
3012 for (int inode = 0; inode < nbNodes; inode++) {
3013 polyedre_nodes[inode] = prevNod[inode];
3016 quantities[1] = nbNodes; // top of prism
3017 for (int inode = 0; inode < nbNodes; inode++) {
3018 polyedre_nodes[nbNodes + inode] = nextNod[inode];
3021 for (int iface = 0; iface < nbNodes; iface++) {
3022 quantities[iface + 2] = 4;
3023 int inextface = (iface == nbNodes - 1) ? 0 : iface + 1;
3024 polyedre_nodes[2*nbNodes + 4*iface + 0] = prevNod[iface];
3025 polyedre_nodes[2*nbNodes + 4*iface + 1] = prevNod[inextface];
3026 polyedre_nodes[2*nbNodes + 4*iface + 2] = nextNod[inextface];
3027 polyedre_nodes[2*nbNodes + 4*iface + 3] = nextNod[iface];
3029 aNewElem = aMesh->AddPolyhedralVolume (polyedre_nodes, quantities);
3033 newElems.push_back( aNewElem );
3034 myLastCreatedElems.Append(aNewElem);
3037 // set new prev nodes
3038 for ( iNode = 0; iNode < nbNodes; iNode++ )
3039 prevNod[ iNode ] = nextNod[ iNode ];
3044 //=======================================================================
3045 //function : makeWalls
3046 //purpose : create 1D and 2D elements around swept elements
3047 //=======================================================================
3049 static void makeWalls (SMESHDS_Mesh* aMesh,
3050 TNodeOfNodeListMap & mapNewNodes,
3051 TElemOfElemListMap & newElemsMap,
3052 TElemOfVecOfNnlmiMap & elemNewNodesMap,
3053 TIDSortedElemSet& elemSet,
3055 SMESH_SequenceOfElemPtr& myLastCreatedElems)
3057 ASSERT( newElemsMap.size() == elemNewNodesMap.size() );
3059 // Find nodes belonging to only one initial element - sweep them to get edges.
3061 TNodeOfNodeListMapItr nList = mapNewNodes.begin();
3062 for ( ; nList != mapNewNodes.end(); nList++ ) {
3063 const SMDS_MeshNode* node =
3064 static_cast<const SMDS_MeshNode*>( nList->first );
3065 SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator();
3066 int nbInitElems = 0;
3067 const SMDS_MeshElement* el = 0;
3068 SMDSAbs_ElementType highType = SMDSAbs_Edge; // count most complex elements only
3069 while ( eIt->more() && nbInitElems < 2 ) {
3071 SMDSAbs_ElementType type = el->GetType();
3072 if ( type == SMDSAbs_Volume || type < highType ) continue;
3073 if ( type > highType ) {
3077 if ( elemSet.find(el) != elemSet.end() )
3080 if ( nbInitElems < 2 ) {
3081 bool NotCreateEdge = el && el->IsQuadratic() && el->IsMediumNode(node);
3082 if(!NotCreateEdge) {
3083 vector<TNodeOfNodeListMapItr> newNodesItVec( 1, nList );
3084 list<const SMDS_MeshElement*> newEdges;
3085 sweepElement( aMesh, node, newNodesItVec, newEdges, nbSteps, myLastCreatedElems );
3090 // Make a ceiling for each element ie an equal element of last new nodes.
3091 // Find free links of faces - make edges and sweep them into faces.
3093 TElemOfElemListMap::iterator itElem = newElemsMap.begin();
3094 TElemOfVecOfNnlmiMap::iterator itElemNodes = elemNewNodesMap.begin();
3095 for ( ; itElem != newElemsMap.end(); itElem++, itElemNodes++ ) {
3096 const SMDS_MeshElement* elem = itElem->first;
3097 vector<TNodeOfNodeListMapItr>& vecNewNodes = itElemNodes->second;
3099 if ( elem->GetType() == SMDSAbs_Edge ) {
3100 // create a ceiling edge
3101 if (!elem->IsQuadratic()) {
3102 if ( !aMesh->FindEdge( vecNewNodes[ 0 ]->second.back(),
3103 vecNewNodes[ 1 ]->second.back()))
3104 myLastCreatedElems.Append(aMesh->AddEdge(vecNewNodes[ 0 ]->second.back(),
3105 vecNewNodes[ 1 ]->second.back()));
3108 if ( !aMesh->FindEdge( vecNewNodes[ 0 ]->second.back(),
3109 vecNewNodes[ 1 ]->second.back(),
3110 vecNewNodes[ 2 ]->second.back()))
3111 myLastCreatedElems.Append(aMesh->AddEdge(vecNewNodes[ 0 ]->second.back(),
3112 vecNewNodes[ 1 ]->second.back(),
3113 vecNewNodes[ 2 ]->second.back()));
3116 if ( elem->GetType() != SMDSAbs_Face )
3119 if(itElem->second.size()==0) continue;
3121 bool hasFreeLinks = false;
3123 TIDSortedElemSet avoidSet;
3124 avoidSet.insert( elem );
3126 set<const SMDS_MeshNode*> aFaceLastNodes;
3127 int iNode, nbNodes = vecNewNodes.size();
3128 if(!elem->IsQuadratic()) {
3129 // loop on the face nodes
3130 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
3131 aFaceLastNodes.insert( vecNewNodes[ iNode ]->second.back() );
3132 // look for free links of the face
3133 int iNext = ( iNode + 1 == nbNodes ) ? 0 : iNode + 1;
3134 const SMDS_MeshNode* n1 = vecNewNodes[ iNode ]->first;
3135 const SMDS_MeshNode* n2 = vecNewNodes[ iNext ]->first;
3136 // check if a link is free
3137 if ( ! SMESH_MeshEditor::FindFaceInSet ( n1, n2, elemSet, avoidSet )) {
3138 hasFreeLinks = true;
3139 // make an edge and a ceiling for a new edge
3140 if ( !aMesh->FindEdge( n1, n2 )) {
3141 myLastCreatedElems.Append(aMesh->AddEdge( n1, n2 ));
3143 n1 = vecNewNodes[ iNode ]->second.back();
3144 n2 = vecNewNodes[ iNext ]->second.back();
3145 if ( !aMesh->FindEdge( n1, n2 )) {
3146 myLastCreatedElems.Append(aMesh->AddEdge( n1, n2 ));
3151 else { // elem is quadratic face
3152 int nbn = nbNodes/2;
3153 for ( iNode = 0; iNode < nbn; iNode++ ) {
3154 aFaceLastNodes.insert( vecNewNodes[ iNode ]->second.back() );
3155 int iNext = ( iNode + 1 == nbn ) ? 0 : iNode + 1;
3156 const SMDS_MeshNode* n1 = vecNewNodes[ iNode ]->first;
3157 const SMDS_MeshNode* n2 = vecNewNodes[ iNext ]->first;
3158 // check if a link is free
3159 if ( ! SMESH_MeshEditor::FindFaceInSet ( n1, n2, elemSet, avoidSet )) {
3160 hasFreeLinks = true;
3161 // make an edge and a ceiling for a new edge
3163 const SMDS_MeshNode* n3 = vecNewNodes[ iNode+nbn ]->first;
3164 if ( !aMesh->FindEdge( n1, n2, n3 )) {
3165 myLastCreatedElems.Append(aMesh->AddEdge( n1, n2, n3 ));
3167 n1 = vecNewNodes[ iNode ]->second.back();
3168 n2 = vecNewNodes[ iNext ]->second.back();
3169 n3 = vecNewNodes[ iNode+nbn ]->second.back();
3170 if ( !aMesh->FindEdge( n1, n2, n3 )) {
3171 myLastCreatedElems.Append(aMesh->AddEdge( n1, n2, n3 ));
3175 for ( iNode = nbn; iNode < 2*nbn; iNode++ ) {
3176 aFaceLastNodes.insert( vecNewNodes[ iNode ]->second.back() );
3180 // sweep free links into faces
3182 if ( hasFreeLinks ) {
3183 list<const SMDS_MeshElement*> & newVolumes = itElem->second;
3184 int iStep; //, nbSteps = vecNewNodes[0]->second.size();
3185 int iVol, volNb, nbVolumesByStep = newVolumes.size() / nbSteps;
3187 set<const SMDS_MeshNode*> initNodeSet, faceNodeSet;
3188 for ( iNode = 0; iNode < nbNodes; iNode++ )
3189 initNodeSet.insert( vecNewNodes[ iNode ]->first );
3191 for ( volNb = 0; volNb < nbVolumesByStep; volNb++ ) {
3192 list<const SMDS_MeshElement*>::iterator v = newVolumes.begin();
3194 while ( iVol++ < volNb ) v++;
3195 // find indices of free faces of a volume
3197 SMDS_VolumeTool vTool( *v );
3198 int iF, nbF = vTool.NbFaces();
3199 for ( iF = 0; iF < nbF; iF ++ ) {
3200 if (vTool.IsFreeFace( iF ) &&
3201 vTool.GetFaceNodes( iF, faceNodeSet ) &&
3202 initNodeSet != faceNodeSet) // except an initial face
3203 fInd.push_back( iF );
3208 // create faces for all steps
3209 // if such a face has been already created by sweep of edge, assure that its orientation is OK
3210 for ( iStep = 0; iStep < nbSteps; iStep++ ) {
3212 vTool.SetExternalNormal();
3213 list< int >::iterator ind = fInd.begin();
3214 for ( ; ind != fInd.end(); ind++ ) {
3215 const SMDS_MeshNode** nodes = vTool.GetFaceNodes( *ind );
3216 int nbn = vTool.NbFaceNodes( *ind );
3218 case 3: { ///// triangle
3219 const SMDS_MeshFace * f = aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ]);
3221 myLastCreatedElems.Append(aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] ));
3222 else if ( nodes[ 1 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3223 aMesh->ChangeElementNodes( f, nodes, nbn );
3226 case 4: { ///// quadrangle
3227 const SMDS_MeshFace * f = aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ]);
3229 myLastCreatedElems.Append(aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ] ));
3230 else if ( nodes[ 1 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3231 aMesh->ChangeElementNodes( f, nodes, nbn );
3235 if( (*v)->IsQuadratic() ) {
3236 if(nbn==6) { /////// quadratic triangle
3237 const SMDS_MeshFace * f = aMesh->FindFace( nodes[0], nodes[2], nodes[4],
3238 nodes[1], nodes[3], nodes[5] );
3240 myLastCreatedElems.Append(aMesh->AddFace(nodes[0], nodes[2], nodes[4],
3241 nodes[1], nodes[3], nodes[5]));
3242 else if ( nodes[ 2 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3243 aMesh->ChangeElementNodes( f, nodes, nbn );
3245 else { /////// quadratic quadrangle
3246 const SMDS_MeshFace * f = aMesh->FindFace( nodes[0], nodes[2], nodes[4], nodes[6],
3247 nodes[1], nodes[3], nodes[5], nodes[7] );
3249 myLastCreatedElems.Append(aMesh->AddFace(nodes[0], nodes[2], nodes[4], nodes[6],
3250 nodes[1], nodes[3], nodes[5], nodes[7]));
3251 else if ( nodes[ 2 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3252 aMesh->ChangeElementNodes( f, nodes, nbn );
3255 else { //////// polygon
3256 vector<const SMDS_MeshNode*> polygon_nodes ( nodes, &nodes[nbn] );
3257 const SMDS_MeshFace * f = aMesh->FindFace( polygon_nodes );
3259 myLastCreatedElems.Append(aMesh->AddPolygonalFace(polygon_nodes));
3260 else if ( nodes[ 1 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3261 aMesh->ChangeElementNodes( f, nodes, nbn );
3265 // go to the next volume
3267 while ( iVol++ < nbVolumesByStep ) v++;
3270 } // sweep free links into faces
3272 // make a ceiling face with a normal external to a volume
3274 SMDS_VolumeTool lastVol( itElem->second.back() );
3276 int iF = lastVol.GetFaceIndex( aFaceLastNodes );
3278 lastVol.SetExternalNormal();
3279 const SMDS_MeshNode** nodes = lastVol.GetFaceNodes( iF );
3280 int nbn = lastVol.NbFaceNodes( iF );
3283 if (!hasFreeLinks ||
3284 !aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ]))
3285 myLastCreatedElems.Append(aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] ));
3288 if (!hasFreeLinks ||
3289 !aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ]))
3290 myLastCreatedElems.Append(aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ] ));
3293 if(itElem->second.back()->IsQuadratic()) {
3295 if (!hasFreeLinks ||
3296 !aMesh->FindFace(nodes[0], nodes[2], nodes[4],
3297 nodes[1], nodes[3], nodes[5]) ) {
3298 myLastCreatedElems.Append(aMesh->AddFace(nodes[0], nodes[2], nodes[4],
3299 nodes[1], nodes[3], nodes[5]));
3303 if (!hasFreeLinks ||
3304 !aMesh->FindFace(nodes[0], nodes[2], nodes[4], nodes[6],
3305 nodes[1], nodes[3], nodes[5], nodes[7]) )
3306 myLastCreatedElems.Append(aMesh->AddFace(nodes[0], nodes[2], nodes[4], nodes[6],
3307 nodes[1], nodes[3], nodes[5], nodes[7]));
3311 vector<const SMDS_MeshNode*> polygon_nodes ( nodes, &nodes[nbn] );
3312 if (!hasFreeLinks || !aMesh->FindFace(polygon_nodes))
3313 myLastCreatedElems.Append(aMesh->AddPolygonalFace(polygon_nodes));
3317 } // loop on swept elements
3320 //=======================================================================
3321 //function : RotationSweep
3323 //=======================================================================
3325 void SMESH_MeshEditor::RotationSweep(TIDSortedElemSet & theElems,
3326 const gp_Ax1& theAxis,
3327 const double theAngle,
3328 const int theNbSteps,
3329 const double theTol,
3330 const bool theMakeWalls)
3332 myLastCreatedElems.Clear();
3333 myLastCreatedNodes.Clear();
3335 MESSAGE( "RotationSweep()");
3337 aTrsf.SetRotation( theAxis, theAngle );
3339 aTrsf2.SetRotation( theAxis, theAngle/2. );
3341 gp_Lin aLine( theAxis );
3342 double aSqTol = theTol * theTol;
3344 SMESHDS_Mesh* aMesh = GetMeshDS();
3346 TNodeOfNodeListMap mapNewNodes;
3347 TElemOfVecOfNnlmiMap mapElemNewNodes;
3348 TElemOfElemListMap newElemsMap;
3351 TIDSortedElemSet::iterator itElem;
3352 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
3353 const SMDS_MeshElement* elem = *itElem;
3354 if ( !elem || elem->GetType() == SMDSAbs_Volume )
3356 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
3357 newNodesItVec.reserve( elem->NbNodes() );
3359 // loop on elem nodes
3360 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
3361 while ( itN->more() ) {
3363 // check if a node has been already sweeped
3364 const SMDS_MeshNode* node =
3365 static_cast<const SMDS_MeshNode*>( itN->next() );
3366 TNodeOfNodeListMapItr nIt = mapNewNodes.find( node );
3367 if ( nIt == mapNewNodes.end() ) {
3368 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
3369 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
3372 gp_XYZ aXYZ( node->X(), node->Y(), node->Z() );
3374 aXYZ.Coord( coord[0], coord[1], coord[2] );
3375 bool isOnAxis = ( aLine.SquareDistance( aXYZ ) <= aSqTol );
3376 const SMDS_MeshNode * newNode = node;
3377 for ( int i = 0; i < theNbSteps; i++ ) {
3379 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3381 aTrsf2.Transforms( coord[0], coord[1], coord[2] );
3382 //aTrsf.Transforms( coord[0], coord[1], coord[2] );
3383 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3384 myLastCreatedNodes.Append(newNode);
3385 listNewNodes.push_back( newNode );
3386 aTrsf2.Transforms( coord[0], coord[1], coord[2] );
3387 //aTrsf.Transforms( coord[0], coord[1], coord[2] );
3390 aTrsf.Transforms( coord[0], coord[1], coord[2] );
3392 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3393 myLastCreatedNodes.Append(newNode);
3395 listNewNodes.push_back( newNode );
3399 // if current elem is quadratic and current node is not medium
3400 // we have to check - may be it is needed to insert additional nodes
3401 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3402 list< const SMDS_MeshNode* > & listNewNodes = nIt->second;
3403 if(listNewNodes.size()==theNbSteps) {
3404 listNewNodes.clear();
3406 gp_XYZ aXYZ( node->X(), node->Y(), node->Z() );
3408 aXYZ.Coord( coord[0], coord[1], coord[2] );
3409 const SMDS_MeshNode * newNode = node;
3410 for(int i = 0; i<theNbSteps; i++) {
3411 aTrsf2.Transforms( coord[0], coord[1], coord[2] );
3412 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3413 myLastCreatedNodes.Append(newNode);
3414 listNewNodes.push_back( newNode );
3415 aTrsf2.Transforms( coord[0], coord[1], coord[2] );
3416 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3417 myLastCreatedNodes.Append(newNode);
3418 listNewNodes.push_back( newNode );
3423 newNodesItVec.push_back( nIt );
3425 // make new elements
3426 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem], theNbSteps, myLastCreatedElems );
3430 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes,
3431 theElems, theNbSteps, myLastCreatedElems );
3435 //=======================================================================
3436 //function : CreateNode
3438 //=======================================================================
3439 const SMDS_MeshNode* SMESH_MeshEditor::CreateNode(const double x,
3442 const double tolnode,
3443 SMESH_SequenceOfNode& aNodes)
3445 myLastCreatedElems.Clear();
3446 myLastCreatedNodes.Clear();
3449 SMESHDS_Mesh * aMesh = myMesh->GetMeshDS();
3451 // try to search in sequence of existing nodes
3452 // if aNodes.Length()>0 we 'nave to use given sequence
3453 // else - use all nodes of mesh
3454 if(aNodes.Length()>0) {
3456 for(i=1; i<=aNodes.Length(); i++) {
3457 gp_Pnt P2(aNodes.Value(i)->X(),aNodes.Value(i)->Y(),aNodes.Value(i)->Z());
3458 if(P1.Distance(P2)<tolnode)
3459 return aNodes.Value(i);
3463 SMDS_NodeIteratorPtr itn = aMesh->nodesIterator();
3464 while(itn->more()) {
3465 const SMDS_MeshNode* aN = static_cast<const SMDS_MeshNode*> (itn->next());
3466 gp_Pnt P2(aN->X(),aN->Y(),aN->Z());
3467 if(P1.Distance(P2)<tolnode)
3472 // create new node and return it
3473 const SMDS_MeshNode* NewNode = aMesh->AddNode(x,y,z);
3474 myLastCreatedNodes.Append(NewNode);
3479 //=======================================================================
3480 //function : ExtrusionSweep
3482 //=======================================================================
3484 void SMESH_MeshEditor::ExtrusionSweep (TIDSortedElemSet & theElems,
3485 const gp_Vec& theStep,
3486 const int theNbSteps,
3487 TElemOfElemListMap& newElemsMap,
3489 const double theTolerance)
3491 ExtrusParam aParams;
3492 aParams.myDir = gp_Dir(theStep);
3493 aParams.myNodes.Clear();
3494 aParams.mySteps = new TColStd_HSequenceOfReal;
3496 for(i=1; i<=theNbSteps; i++)
3497 aParams.mySteps->Append(theStep.Magnitude());
3499 ExtrusionSweep(theElems,aParams,newElemsMap,theFlags,theTolerance);
3504 //=======================================================================
3505 //function : ExtrusionSweep
3507 //=======================================================================
3509 void SMESH_MeshEditor::ExtrusionSweep (TIDSortedElemSet & theElems,
3510 ExtrusParam& theParams,
3511 TElemOfElemListMap& newElemsMap,
3513 const double theTolerance)
3515 myLastCreatedElems.Clear();
3516 myLastCreatedNodes.Clear();
3518 SMESHDS_Mesh* aMesh = GetMeshDS();
3520 int nbsteps = theParams.mySteps->Length();
3522 TNodeOfNodeListMap mapNewNodes;
3523 //TNodeOfNodeVecMap mapNewNodes;
3524 TElemOfVecOfNnlmiMap mapElemNewNodes;
3525 //TElemOfVecOfMapNodesMap mapElemNewNodes;
3528 TIDSortedElemSet::iterator itElem;
3529 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
3530 // check element type
3531 const SMDS_MeshElement* elem = *itElem;
3532 if ( !elem || elem->GetType() == SMDSAbs_Volume )
3535 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
3536 //vector<TNodeOfNodeVecMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
3537 newNodesItVec.reserve( elem->NbNodes() );
3539 // loop on elem nodes
3540 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
3541 while ( itN->more() ) {
3543 // check if a node has been already sweeped
3544 const SMDS_MeshNode* node =
3545 static_cast<const SMDS_MeshNode*>( itN->next() );
3546 TNodeOfNodeListMap::iterator nIt = mapNewNodes.find( node );
3547 //TNodeOfNodeVecMap::iterator nIt = mapNewNodes.find( node );
3548 if ( nIt == mapNewNodes.end() ) {
3549 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
3550 //nIt = mapNewNodes.insert( make_pair( node, vector<const SMDS_MeshNode*>() )).first;
3551 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
3552 //vector<const SMDS_MeshNode*>& vecNewNodes = nIt->second;
3553 //vecNewNodes.reserve(nbsteps);
3556 double coord[] = { node->X(), node->Y(), node->Z() };
3557 //int nbsteps = theParams.mySteps->Length();
3558 for ( int i = 0; i < nbsteps; i++ ) {
3559 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3560 // create additional node
3561 double x = coord[0] + theParams.myDir.X()*theParams.mySteps->Value(i+1)/2.;
3562 double y = coord[1] + theParams.myDir.Y()*theParams.mySteps->Value(i+1)/2.;
3563 double z = coord[2] + theParams.myDir.Z()*theParams.mySteps->Value(i+1)/2.;
3564 if( theFlags & EXTRUSION_FLAG_SEW ) {
3565 const SMDS_MeshNode * newNode = CreateNode(x, y, z,
3566 theTolerance, theParams.myNodes);
3567 listNewNodes.push_back( newNode );
3570 const SMDS_MeshNode * newNode = aMesh->AddNode(x, y, z);
3571 myLastCreatedNodes.Append(newNode);
3572 listNewNodes.push_back( newNode );
3575 //aTrsf.Transforms( coord[0], coord[1], coord[2] );
3576 coord[0] = coord[0] + theParams.myDir.X()*theParams.mySteps->Value(i+1);
3577 coord[1] = coord[1] + theParams.myDir.Y()*theParams.mySteps->Value(i+1);
3578 coord[2] = coord[2] + theParams.myDir.Z()*theParams.mySteps->Value(i+1);
3579 if( theFlags & EXTRUSION_FLAG_SEW ) {
3580 const SMDS_MeshNode * newNode = CreateNode(coord[0], coord[1], coord[2],
3581 theTolerance, theParams.myNodes);
3582 listNewNodes.push_back( newNode );
3583 //vecNewNodes[i]=newNode;
3586 const SMDS_MeshNode * newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3587 myLastCreatedNodes.Append(newNode);
3588 listNewNodes.push_back( newNode );
3589 //vecNewNodes[i]=newNode;
3594 // if current elem is quadratic and current node is not medium
3595 // we have to check - may be it is needed to insert additional nodes
3596 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3597 list< const SMDS_MeshNode* > & listNewNodes = nIt->second;
3598 if(listNewNodes.size()==nbsteps) {
3599 listNewNodes.clear();
3600 double coord[] = { node->X(), node->Y(), node->Z() };
3601 for ( int i = 0; i < nbsteps; i++ ) {
3602 double x = coord[0] + theParams.myDir.X()*theParams.mySteps->Value(i+1);
3603 double y = coord[1] + theParams.myDir.Y()*theParams.mySteps->Value(i+1);
3604 double z = coord[2] + theParams.myDir.Z()*theParams.mySteps->Value(i+1);
3605 if( theFlags & EXTRUSION_FLAG_SEW ) {
3606 const SMDS_MeshNode * newNode = CreateNode(x, y, z,
3607 theTolerance, theParams.myNodes);
3608 listNewNodes.push_back( newNode );
3611 const SMDS_MeshNode * newNode = aMesh->AddNode(x, y, z);
3612 myLastCreatedNodes.Append(newNode);
3613 listNewNodes.push_back( newNode );
3615 coord[0] = coord[0] + theParams.myDir.X()*theParams.mySteps->Value(i+1);
3616 coord[1] = coord[1] + theParams.myDir.Y()*theParams.mySteps->Value(i+1);
3617 coord[2] = coord[2] + theParams.myDir.Z()*theParams.mySteps->Value(i+1);
3618 if( theFlags & EXTRUSION_FLAG_SEW ) {
3619 const SMDS_MeshNode * newNode = CreateNode(coord[0], coord[1], coord[2],
3620 theTolerance, theParams.myNodes);
3621 listNewNodes.push_back( newNode );
3624 const SMDS_MeshNode * newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3625 myLastCreatedNodes.Append(newNode);
3626 listNewNodes.push_back( newNode );
3632 newNodesItVec.push_back( nIt );
3634 // make new elements
3635 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem], nbsteps, myLastCreatedElems );
3638 if( theFlags & EXTRUSION_FLAG_BOUNDARY ) {
3639 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElems, nbsteps, myLastCreatedElems );
3644 //=======================================================================
3645 //class : SMESH_MeshEditor_PathPoint
3646 //purpose : auxiliary class
3647 //=======================================================================
3648 class SMESH_MeshEditor_PathPoint {
3650 SMESH_MeshEditor_PathPoint() {
3651 myPnt.SetCoord(99., 99., 99.);
3652 myTgt.SetCoord(1.,0.,0.);
3656 void SetPnt(const gp_Pnt& aP3D){
3659 void SetTangent(const gp_Dir& aTgt){
3662 void SetAngle(const double& aBeta){
3665 void SetParameter(const double& aPrm){
3668 const gp_Pnt& Pnt()const{
3671 const gp_Dir& Tangent()const{
3674 double Angle()const{
3677 double Parameter()const{
3688 //=======================================================================
3689 //function : ExtrusionAlongTrack
3691 //=======================================================================
3692 SMESH_MeshEditor::Extrusion_Error
3693 SMESH_MeshEditor::ExtrusionAlongTrack (TIDSortedElemSet & theElements,
3694 SMESH_subMesh* theTrack,
3695 const SMDS_MeshNode* theN1,
3696 const bool theHasAngles,
3697 list<double>& theAngles,
3698 const bool theHasRefPoint,
3699 const gp_Pnt& theRefPoint)
3701 myLastCreatedElems.Clear();
3702 myLastCreatedNodes.Clear();
3704 MESSAGE("SMESH_MeshEditor::ExtrusionAlongTrack")
3705 int j, aNbTP, aNbE, aNb;
3706 double aT1, aT2, aT, aAngle, aX, aY, aZ;
3707 std::list<double> aPrms;
3708 std::list<double>::iterator aItD;
3709 TIDSortedElemSet::iterator itElem;
3711 Standard_Real aTx1, aTx2, aL2, aTolVec, aTolVec2;
3715 Handle(Geom_Curve) aC3D;
3716 TopoDS_Edge aTrackEdge;
3717 TopoDS_Vertex aV1, aV2;
3719 SMDS_ElemIteratorPtr aItE;
3720 SMDS_NodeIteratorPtr aItN;
3721 SMDSAbs_ElementType aTypeE;
3723 TNodeOfNodeListMap mapNewNodes;
3724 TElemOfVecOfNnlmiMap mapElemNewNodes;
3725 TElemOfElemListMap newElemsMap;
3728 aTolVec2=aTolVec*aTolVec;
3731 aNbE = theElements.size();
3734 return EXTR_NO_ELEMENTS;
3736 // 1.1 Track Pattern
3739 SMESHDS_SubMesh* pSubMeshDS=theTrack->GetSubMeshDS();
3741 aItE = pSubMeshDS->GetElements();
3742 while ( aItE->more() ) {
3743 const SMDS_MeshElement* pE = aItE->next();
3744 aTypeE = pE->GetType();
3745 // Pattern must contain links only
3746 if ( aTypeE != SMDSAbs_Edge )
3747 return EXTR_PATH_NOT_EDGE;
3750 const TopoDS_Shape& aS = theTrack->GetSubShape();
3751 // Sub shape for the Pattern must be an Edge
3752 if ( aS.ShapeType() != TopAbs_EDGE )
3753 return EXTR_BAD_PATH_SHAPE;
3755 aTrackEdge = TopoDS::Edge( aS );
3756 // the Edge must not be degenerated
3757 if ( BRep_Tool::Degenerated( aTrackEdge ) )
3758 return EXTR_BAD_PATH_SHAPE;
3760 TopExp::Vertices( aTrackEdge, aV1, aV2 );
3761 aT1=BRep_Tool::Parameter( aV1, aTrackEdge );
3762 aT2=BRep_Tool::Parameter( aV2, aTrackEdge );
3764 aItN = theTrack->GetFather()->GetSubMesh( aV1 )->GetSubMeshDS()->GetNodes();
3765 const SMDS_MeshNode* aN1 = aItN->next();
3767 aItN = theTrack->GetFather()->GetSubMesh( aV2 )->GetSubMeshDS()->GetNodes();
3768 const SMDS_MeshNode* aN2 = aItN->next();
3770 // starting node must be aN1 or aN2
3771 if ( !( aN1 == theN1 || aN2 == theN1 ) )
3772 return EXTR_BAD_STARTING_NODE;
3774 aNbTP = pSubMeshDS->NbNodes() + 2;
3777 vector<double> aAngles( aNbTP );
3779 for ( j=0; j < aNbTP; ++j ) {
3783 if ( theHasAngles ) {
3784 aItD = theAngles.begin();
3785 for ( j=1; (aItD != theAngles.end()) && (j<aNbTP); ++aItD, ++j ) {
3787 aAngles[j] = aAngle;
3791 // 2. Collect parameters on the track edge
3792 aPrms.push_back( aT1 );
3793 aPrms.push_back( aT2 );
3795 aItN = pSubMeshDS->GetNodes();
3796 while ( aItN->more() ) {
3797 const SMDS_MeshNode* pNode = aItN->next();
3798 const SMDS_EdgePosition* pEPos =
3799 static_cast<const SMDS_EdgePosition*>( pNode->GetPosition().get() );
3800 aT = pEPos->GetUParameter();
3801 aPrms.push_back( aT );
3806 if ( aN1 == theN1 ) {
3818 SMESH_MeshEditor_PathPoint aPP;
3819 vector<SMESH_MeshEditor_PathPoint> aPPs( aNbTP );
3821 aC3D = BRep_Tool::Curve( aTrackEdge, aTx1, aTx2 );
3823 aItD = aPrms.begin();
3824 for ( j=0; aItD != aPrms.end(); ++aItD, ++j ) {
3826 aC3D->D1( aT, aP3D, aVec );
3827 aL2 = aVec.SquareMagnitude();
3828 if ( aL2 < aTolVec2 )
3829 return EXTR_CANT_GET_TANGENT;
3831 gp_Dir aTgt( aVec );
3832 aAngle = aAngles[j];
3835 aPP.SetTangent( aTgt );
3836 aPP.SetAngle( aAngle );
3837 aPP.SetParameter( aT );
3841 // 3. Center of rotation aV0
3843 if ( !theHasRefPoint ) {
3845 aGC.SetCoord( 0.,0.,0. );
3847 itElem = theElements.begin();
3848 for ( ; itElem != theElements.end(); itElem++ ) {
3849 const SMDS_MeshElement* elem = *itElem;
3851 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
3852 while ( itN->more() ) {
3853 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( itN->next() );
3858 if ( mapNewNodes.find( node ) == mapNewNodes.end() ) {
3859 list<const SMDS_MeshNode*> aLNx;
3860 mapNewNodes[node] = aLNx;
3862 gp_XYZ aXYZ( aX, aY, aZ );
3870 } // if (!theHasRefPoint) {
3871 mapNewNodes.clear();
3873 // 4. Processing the elements
3874 SMESHDS_Mesh* aMesh = GetMeshDS();
3876 for ( itElem = theElements.begin(); itElem != theElements.end(); itElem++ ) {
3877 // check element type
3878 const SMDS_MeshElement* elem = *itElem;
3879 aTypeE = elem->GetType();
3880 if ( !elem || ( aTypeE != SMDSAbs_Face && aTypeE != SMDSAbs_Edge ) )
3883 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
3884 newNodesItVec.reserve( elem->NbNodes() );
3886 // loop on elem nodes
3887 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
3888 while ( itN->more() ) {
3890 // check if a node has been already processed
3891 const SMDS_MeshNode* node =
3892 static_cast<const SMDS_MeshNode*>( itN->next() );
3893 TNodeOfNodeListMap::iterator nIt = mapNewNodes.find( node );
3894 if ( nIt == mapNewNodes.end() ) {
3895 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
3896 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
3899 aX = node->X(); aY = node->Y(); aZ = node->Z();
3901 Standard_Real aAngle1x, aAngleT1T0, aTolAng;
3902 gp_Pnt aP0x, aP1x, aPN0, aPN1, aV0x, aV1x;
3903 gp_Ax1 anAx1, anAxT1T0;
3904 gp_Dir aDT1x, aDT0x, aDT1T0;
3909 aPN0.SetCoord(aX, aY, aZ);
3911 const SMESH_MeshEditor_PathPoint& aPP0 = aPPs[0];
3913 aDT0x= aPP0.Tangent();
3915 for ( j = 1; j < aNbTP; ++j ) {
3916 const SMESH_MeshEditor_PathPoint& aPP1 = aPPs[j];
3918 aDT1x = aPP1.Tangent();
3919 aAngle1x = aPP1.Angle();
3921 gp_Trsf aTrsf, aTrsfRot, aTrsfRotT1T0;
3923 gp_Vec aV01x( aP0x, aP1x );
3924 aTrsf.SetTranslation( aV01x );
3927 aV1x = aV0x.Transformed( aTrsf );
3928 aPN1 = aPN0.Transformed( aTrsf );
3930 // rotation 1 [ T1,T0 ]
3931 aAngleT1T0=-aDT1x.Angle( aDT0x );
3932 if (fabs(aAngleT1T0) > aTolAng) {
3934 anAxT1T0.SetLocation( aV1x );
3935 anAxT1T0.SetDirection( aDT1T0 );
3936 aTrsfRotT1T0.SetRotation( anAxT1T0, aAngleT1T0 );
3938 aPN1 = aPN1.Transformed( aTrsfRotT1T0 );
3942 if ( theHasAngles ) {
3943 anAx1.SetLocation( aV1x );
3944 anAx1.SetDirection( aDT1x );
3945 aTrsfRot.SetRotation( anAx1, aAngle1x );
3947 aPN1 = aPN1.Transformed( aTrsfRot );
3951 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3952 // create additional node
3953 double x = ( aPN1.X() + aPN0.X() )/2.;
3954 double y = ( aPN1.Y() + aPN0.Y() )/2.;
3955 double z = ( aPN1.Z() + aPN0.Z() )/2.;
3956 const SMDS_MeshNode* newNode = aMesh->AddNode(x,y,z);
3957 myLastCreatedNodes.Append(newNode);
3958 listNewNodes.push_back( newNode );
3963 const SMDS_MeshNode* newNode = aMesh->AddNode( aX, aY, aZ );
3964 myLastCreatedNodes.Append(newNode);
3965 listNewNodes.push_back( newNode );
3975 // if current elem is quadratic and current node is not medium
3976 // we have to check - may be it is needed to insert additional nodes
3977 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3978 list< const SMDS_MeshNode* > & listNewNodes = nIt->second;
3979 if(listNewNodes.size()==aNbTP-1) {
3980 vector<const SMDS_MeshNode*> aNodes(2*(aNbTP-1));
3981 gp_XYZ P(node->X(), node->Y(), node->Z());
3982 list< const SMDS_MeshNode* >::iterator it = listNewNodes.begin();
3984 for(i=0; i<aNbTP-1; i++) {
3985 const SMDS_MeshNode* N = *it;
3986 double x = ( N->X() + P.X() )/2.;
3987 double y = ( N->Y() + P.Y() )/2.;
3988 double z = ( N->Z() + P.Z() )/2.;
3989 const SMDS_MeshNode* newN = aMesh->AddNode(x,y,z);
3990 myLastCreatedNodes.Append(newN);
3993 P = gp_XYZ(N->X(),N->Y(),N->Z());
3995 listNewNodes.clear();
3996 for(i=0; i<2*(aNbTP-1); i++) {
3997 listNewNodes.push_back(aNodes[i]);
4003 newNodesItVec.push_back( nIt );
4005 // make new elements
4006 //sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem],
4007 // newNodesItVec[0]->second.size(), myLastCreatedElems );
4008 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem],
4009 aNbTP-1, myLastCreatedElems );
4012 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElements,
4013 aNbTP-1, myLastCreatedElems );
4018 //=======================================================================
4019 //function : Transform
4021 //=======================================================================
4023 void SMESH_MeshEditor::Transform (TIDSortedElemSet & theElems,
4024 const gp_Trsf& theTrsf,
4027 myLastCreatedElems.Clear();
4028 myLastCreatedNodes.Clear();
4031 switch ( theTrsf.Form() ) {
4037 needReverse = false;
4040 SMESHDS_Mesh* aMesh = GetMeshDS();
4042 // map old node to new one
4043 TNodeNodeMap nodeMap;
4045 // elements sharing moved nodes; those of them which have all
4046 // nodes mirrored but are not in theElems are to be reversed
4047 TIDSortedElemSet inverseElemSet;
4050 TIDSortedElemSet::iterator itElem;
4051 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
4052 const SMDS_MeshElement* elem = *itElem;
4056 // loop on elem nodes
4057 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
4058 while ( itN->more() ) {
4060 // check if a node has been already transformed
4061 const SMDS_MeshNode* node =
4062 static_cast<const SMDS_MeshNode*>( itN->next() );
4063 if (nodeMap.find( node ) != nodeMap.end() )
4067 coord[0] = node->X();
4068 coord[1] = node->Y();
4069 coord[2] = node->Z();
4070 theTrsf.Transforms( coord[0], coord[1], coord[2] );
4071 const SMDS_MeshNode * newNode = node;
4073 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
4074 myLastCreatedNodes.Append(newNode);
4077 aMesh->MoveNode( node, coord[0], coord[1], coord[2] );
4078 // node position on shape becomes invalid
4079 const_cast< SMDS_MeshNode* > ( node )->SetPosition
4080 ( SMDS_SpacePosition::originSpacePosition() );
4082 nodeMap.insert( TNodeNodeMap::value_type( node, newNode ));
4084 // keep inverse elements
4085 if ( !theCopy && needReverse ) {
4086 SMDS_ElemIteratorPtr invElemIt = node->GetInverseElementIterator();
4087 while ( invElemIt->more() ) {
4088 const SMDS_MeshElement* iel = invElemIt->next();
4089 inverseElemSet.insert( iel );
4095 // either new elements are to be created
4096 // or a mirrored element are to be reversed
4097 if ( !theCopy && !needReverse)
4100 if ( !inverseElemSet.empty()) {
4101 TIDSortedElemSet::iterator invElemIt = inverseElemSet.begin();
4102 for ( ; invElemIt != inverseElemSet.end(); invElemIt++ )
4103 theElems.insert( *invElemIt );
4106 // replicate or reverse elements
4109 REV_TETRA = 0, // = nbNodes - 4
4110 REV_PYRAMID = 1, // = nbNodes - 4
4111 REV_PENTA = 2, // = nbNodes - 4
4113 REV_HEXA = 4, // = nbNodes - 4
4117 { 2, 1, 0, 3, 4, 0, 0, 0 }, // REV_TETRA
4118 { 2, 1, 0, 3, 4, 0, 0, 0 }, // REV_PYRAMID
4119 { 2, 1, 0, 5, 4, 3, 0, 0 }, // REV_PENTA
4120 { 2, 1, 0, 3, 0, 0, 0, 0 }, // REV_FACE
4121 { 2, 1, 0, 3, 6, 5, 4, 7 }, // REV_HEXA
4122 { 0, 1, 2, 3, 4, 5, 6, 7 } // FORWARD
4125 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
4126 const SMDS_MeshElement* elem = *itElem;
4127 if ( !elem || elem->GetType() == SMDSAbs_Node )
4130 int nbNodes = elem->NbNodes();
4131 int elemType = elem->GetType();
4133 if (elem->IsPoly()) {
4134 // Polygon or Polyhedral Volume
4135 switch ( elemType ) {
4138 vector<const SMDS_MeshNode*> poly_nodes (nbNodes);
4140 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
4141 while (itN->more()) {
4142 const SMDS_MeshNode* node =
4143 static_cast<const SMDS_MeshNode*>(itN->next());
4144 TNodeNodeMap::iterator nodeMapIt = nodeMap.find(node);
4145 if (nodeMapIt == nodeMap.end())
4146 break; // not all nodes transformed
4148 // reverse mirrored faces and volumes
4149 poly_nodes[nbNodes - iNode - 1] = (*nodeMapIt).second;
4151 poly_nodes[iNode] = (*nodeMapIt).second;
4155 if ( iNode != nbNodes )
4156 continue; // not all nodes transformed
4159 myLastCreatedElems.Append(aMesh->AddPolygonalFace(poly_nodes));
4162 aMesh->ChangePolygonNodes(elem, poly_nodes);
4166 case SMDSAbs_Volume:
4168 // ATTENTION: Reversing is not yet done!!!
4169 const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
4170 (const SMDS_PolyhedralVolumeOfNodes*) elem;
4172 MESSAGE("Warning: bad volumic element");
4176 vector<const SMDS_MeshNode*> poly_nodes;
4177 vector<int> quantities;
4179 bool allTransformed = true;
4180 int nbFaces = aPolyedre->NbFaces();
4181 for (int iface = 1; iface <= nbFaces && allTransformed; iface++) {
4182 int nbFaceNodes = aPolyedre->NbFaceNodes(iface);
4183 for (int inode = 1; inode <= nbFaceNodes && allTransformed; inode++) {
4184 const SMDS_MeshNode* node = aPolyedre->GetFaceNode(iface, inode);
4185 TNodeNodeMap::iterator nodeMapIt = nodeMap.find(node);
4186 if (nodeMapIt == nodeMap.end()) {
4187 allTransformed = false; // not all nodes transformed
4189 poly_nodes.push_back((*nodeMapIt).second);
4192 quantities.push_back(nbFaceNodes);
4194 if ( !allTransformed )
4195 continue; // not all nodes transformed
4198 myLastCreatedElems.Append(aMesh->AddPolyhedralVolume(poly_nodes, quantities));
4201 aMesh->ChangePolyhedronNodes(elem, poly_nodes, quantities);
4211 int* i = index[ FORWARD ];
4212 if ( needReverse && nbNodes > 2) // reverse mirrored faces and volumes
4213 if ( elemType == SMDSAbs_Face )
4214 i = index[ REV_FACE ];
4216 i = index[ nbNodes - 4 ];
4218 if(elem->IsQuadratic()) {
4219 static int anIds[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
4222 if(nbNodes==3) { // quadratic edge
4223 static int anIds[] = {1,0,2};
4226 else if(nbNodes==6) { // quadratic triangle
4227 static int anIds[] = {0,2,1,5,4,3};
4230 else if(nbNodes==8) { // quadratic quadrangle
4231 static int anIds[] = {0,3,2,1,7,6,5,4};
4234 else if(nbNodes==10) { // quadratic tetrahedron of 10 nodes
4235 static int anIds[] = {0,2,1,3,6,5,4,7,9,8};
4238 else if(nbNodes==13) { // quadratic pyramid of 13 nodes
4239 static int anIds[] = {0,3,2,1,4,8,7,6,5,9,12,11,10};
4242 else if(nbNodes==15) { // quadratic pentahedron with 15 nodes
4243 static int anIds[] = {0,2,1,3,5,4,8,7,6,11,10,9,12,14,13};
4246 else { // nbNodes==20 - quadratic hexahedron with 20 nodes
4247 static int anIds[] = {0,3,2,1,4,7,6,5,11,10,9,8,15,14,13,12,16,19,18,17};
4253 // find transformed nodes
4254 vector<const SMDS_MeshNode*> nodes(nbNodes);
4256 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
4257 while ( itN->more() ) {
4258 const SMDS_MeshNode* node =
4259 static_cast<const SMDS_MeshNode*>( itN->next() );
4260 TNodeNodeMap::iterator nodeMapIt = nodeMap.find( node );
4261 if ( nodeMapIt == nodeMap.end() )
4262 break; // not all nodes transformed
4263 nodes[ i [ iNode++ ]] = (*nodeMapIt).second;
4265 if ( iNode != nbNodes )
4266 continue; // not all nodes transformed
4269 if ( SMDS_MeshElement* elem = AddElement( nodes, elem->GetType(), elem->IsPoly() ))
4270 myLastCreatedElems.Append( elem );
4274 // reverse element as it was reversed by transformation
4276 aMesh->ChangeElementNodes( elem, &nodes[0], nbNodes );
4281 //=======================================================================
4282 //function : FindCoincidentNodes
4283 //purpose : Return list of group of nodes close to each other within theTolerance
4284 // Search among theNodes or in the whole mesh if theNodes is empty using
4285 // an Octree algorithm
4286 //=======================================================================
4288 void SMESH_MeshEditor::FindCoincidentNodes (set<const SMDS_MeshNode*> & theNodes,
4289 const double theTolerance,
4290 TListOfListOfNodes & theGroupsOfNodes)
4292 myLastCreatedElems.Clear();
4293 myLastCreatedNodes.Clear();
4295 set<const SMDS_MeshNode*> nodes;
4296 if ( theNodes.empty() )
4297 { // get all nodes in the mesh
4298 SMDS_NodeIteratorPtr nIt = GetMeshDS()->nodesIterator();
4299 while ( nIt->more() )
4300 nodes.insert( nodes.end(),nIt->next());
4304 SMESH_OctreeNode::FindCoincidentNodes ( nodes, &theGroupsOfNodes, theTolerance);
4308 //=======================================================================
4310 * \brief Implementation of search for the node closest to point
4312 //=======================================================================
4314 struct SMESH_NodeSearcherImpl: public SMESH_NodeSearcher
4316 SMESH_NodeSearcherImpl( const SMESHDS_Mesh* theMesh )
4318 set<const SMDS_MeshNode*> nodes;
4320 SMDS_NodeIteratorPtr nIt = theMesh->nodesIterator();
4321 while ( nIt->more() )
4322 nodes.insert( nodes.end(), nIt->next() );
4324 myOctreeNode = new SMESH_OctreeNode(nodes) ;
4326 const SMDS_MeshNode* FindClosestTo( const gp_Pnt& thePnt )
4328 SMDS_MeshNode tgtNode( thePnt.X(), thePnt.Y(), thePnt.Z() );
4329 list<const SMDS_MeshNode*> nodes;
4330 myOctreeNode->NodesAround( &tgtNode, &nodes, 1e-7);
4331 const SMDS_MeshNode* closestNode = 0;
4332 double minSqDist = DBL_MAX;
4333 list<const SMDS_MeshNode*>::iterator nIt = nodes.begin();
4334 for ( ; nIt != nodes.end(); ++nIt ) {
4335 double sqDist = thePnt.SquareDistance( TNodeXYZ( *nIt ) );
4336 if ( minSqDist > sqDist ) {
4343 ~SMESH_NodeSearcherImpl() { delete myOctreeNode; }
4345 SMESH_OctreeNode* myOctreeNode;
4348 //=======================================================================
4350 * \brief Return SMESH_NodeSearcher
4352 //=======================================================================
4354 SMESH_NodeSearcher* SMESH_MeshEditor::GetNodeSearcher()
4356 return new SMESH_NodeSearcherImpl( GetMeshDS() );
4359 //=======================================================================
4360 //function : SimplifyFace
4362 //=======================================================================
4363 int SMESH_MeshEditor::SimplifyFace (const vector<const SMDS_MeshNode *> faceNodes,
4364 vector<const SMDS_MeshNode *>& poly_nodes,
4365 vector<int>& quantities) const
4367 int nbNodes = faceNodes.size();
4372 set<const SMDS_MeshNode*> nodeSet;
4374 // get simple seq of nodes
4375 const SMDS_MeshNode* simpleNodes[ nbNodes ];
4376 int iSimple = 0, nbUnique = 0;
4378 simpleNodes[iSimple++] = faceNodes[0];
4380 for (int iCur = 1; iCur < nbNodes; iCur++) {
4381 if (faceNodes[iCur] != simpleNodes[iSimple - 1]) {
4382 simpleNodes[iSimple++] = faceNodes[iCur];
4383 if (nodeSet.insert( faceNodes[iCur] ).second)
4387 int nbSimple = iSimple;
4388 if (simpleNodes[nbSimple - 1] == simpleNodes[0]) {
4398 bool foundLoop = (nbSimple > nbUnique);
4401 set<const SMDS_MeshNode*> loopSet;
4402 for (iSimple = 0; iSimple < nbSimple && !foundLoop; iSimple++) {
4403 const SMDS_MeshNode* n = simpleNodes[iSimple];
4404 if (!loopSet.insert( n ).second) {
4408 int iC = 0, curLast = iSimple;
4409 for (; iC < curLast; iC++) {
4410 if (simpleNodes[iC] == n) break;
4412 int loopLen = curLast - iC;
4414 // create sub-element
4416 quantities.push_back(loopLen);
4417 for (; iC < curLast; iC++) {
4418 poly_nodes.push_back(simpleNodes[iC]);
4421 // shift the rest nodes (place from the first loop position)
4422 for (iC = curLast + 1; iC < nbSimple; iC++) {
4423 simpleNodes[iC - loopLen] = simpleNodes[iC];
4425 nbSimple -= loopLen;
4428 } // for (iSimple = 0; iSimple < nbSimple; iSimple++)
4429 } // while (foundLoop)
4433 quantities.push_back(iSimple);
4434 for (int i = 0; i < iSimple; i++)
4435 poly_nodes.push_back(simpleNodes[i]);
4441 //=======================================================================
4442 //function : MergeNodes
4443 //purpose : In each group, the cdr of nodes are substituted by the first one
4445 //=======================================================================
4447 void SMESH_MeshEditor::MergeNodes (TListOfListOfNodes & theGroupsOfNodes)
4449 myLastCreatedElems.Clear();
4450 myLastCreatedNodes.Clear();
4452 SMESHDS_Mesh* aMesh = GetMeshDS();
4454 TNodeNodeMap nodeNodeMap; // node to replace - new node
4455 set<const SMDS_MeshElement*> elems; // all elements with changed nodes
4456 list< int > rmElemIds, rmNodeIds;
4458 // Fill nodeNodeMap and elems
4460 TListOfListOfNodes::iterator grIt = theGroupsOfNodes.begin();
4461 for ( ; grIt != theGroupsOfNodes.end(); grIt++ ) {
4462 list<const SMDS_MeshNode*>& nodes = *grIt;
4463 list<const SMDS_MeshNode*>::iterator nIt = nodes.begin();
4464 const SMDS_MeshNode* nToKeep = *nIt;
4465 for ( ++nIt; nIt != nodes.end(); nIt++ ) {
4466 const SMDS_MeshNode* nToRemove = *nIt;
4467 nodeNodeMap.insert( TNodeNodeMap::value_type( nToRemove, nToKeep ));
4468 if ( nToRemove != nToKeep ) {
4469 rmNodeIds.push_back( nToRemove->GetID() );
4470 AddToSameGroups( nToKeep, nToRemove, aMesh );
4473 SMDS_ElemIteratorPtr invElemIt = nToRemove->GetInverseElementIterator();
4474 while ( invElemIt->more() ) {
4475 const SMDS_MeshElement* elem = invElemIt->next();
4480 // Change element nodes or remove an element
4482 set<const SMDS_MeshElement*>::iterator eIt = elems.begin();
4483 for ( ; eIt != elems.end(); eIt++ ) {
4484 const SMDS_MeshElement* elem = *eIt;
4485 int nbNodes = elem->NbNodes();
4486 int aShapeId = FindShape( elem );
4488 set<const SMDS_MeshNode*> nodeSet;
4489 const SMDS_MeshNode* curNodes[ nbNodes ], *uniqueNodes[ nbNodes ];
4490 int iUnique = 0, iCur = 0, nbRepl = 0, iRepl [ nbNodes ];
4492 // get new seq of nodes
4493 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
4494 while ( itN->more() ) {
4495 const SMDS_MeshNode* n =
4496 static_cast<const SMDS_MeshNode*>( itN->next() );
4498 TNodeNodeMap::iterator nnIt = nodeNodeMap.find( n );
4499 if ( nnIt != nodeNodeMap.end() ) { // n sticks
4501 iRepl[ nbRepl++ ] = iCur;
4503 curNodes[ iCur ] = n;
4504 bool isUnique = nodeSet.insert( n ).second;
4506 uniqueNodes[ iUnique++ ] = n;
4510 // Analyse element topology after replacement
4513 int nbUniqueNodes = nodeSet.size();
4514 if ( nbNodes != nbUniqueNodes ) { // some nodes stick
4515 // Polygons and Polyhedral volumes
4516 if (elem->IsPoly()) {
4518 if (elem->GetType() == SMDSAbs_Face) {
4520 vector<const SMDS_MeshNode *> face_nodes (nbNodes);
4522 for (; inode < nbNodes; inode++) {
4523 face_nodes[inode] = curNodes[inode];
4526 vector<const SMDS_MeshNode *> polygons_nodes;
4527 vector<int> quantities;
4528 int nbNew = SimplifyFace(face_nodes, polygons_nodes, quantities);
4532 for (int iface = 0; iface < nbNew - 1; iface++) {
4533 int nbNodes = quantities[iface];
4534 vector<const SMDS_MeshNode *> poly_nodes (nbNodes);
4535 for (int ii = 0; ii < nbNodes; ii++, inode++) {
4536 poly_nodes[ii] = polygons_nodes[inode];
4538 SMDS_MeshElement* newElem = aMesh->AddPolygonalFace(poly_nodes);
4539 myLastCreatedElems.Append(newElem);
4541 aMesh->SetMeshElementOnShape(newElem, aShapeId);
4543 aMesh->ChangeElementNodes(elem, &polygons_nodes[inode], quantities[nbNew - 1]);
4546 rmElemIds.push_back(elem->GetID());
4550 else if (elem->GetType() == SMDSAbs_Volume) {
4551 // Polyhedral volume
4552 if (nbUniqueNodes < 4) {
4553 rmElemIds.push_back(elem->GetID());
4556 // each face has to be analized in order to check volume validity
4557 const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
4558 static_cast<const SMDS_PolyhedralVolumeOfNodes*>( elem );
4560 int nbFaces = aPolyedre->NbFaces();
4562 vector<const SMDS_MeshNode *> poly_nodes;
4563 vector<int> quantities;
4565 for (int iface = 1; iface <= nbFaces; iface++) {
4566 int nbFaceNodes = aPolyedre->NbFaceNodes(iface);
4567 vector<const SMDS_MeshNode *> faceNodes (nbFaceNodes);
4569 for (int inode = 1; inode <= nbFaceNodes; inode++) {
4570 const SMDS_MeshNode * faceNode = aPolyedre->GetFaceNode(iface, inode);
4571 TNodeNodeMap::iterator nnIt = nodeNodeMap.find(faceNode);
4572 if (nnIt != nodeNodeMap.end()) { // faceNode sticks
4573 faceNode = (*nnIt).second;
4575 faceNodes[inode - 1] = faceNode;
4578 SimplifyFace(faceNodes, poly_nodes, quantities);
4581 if (quantities.size() > 3) {
4582 // to be done: remove coincident faces
4585 if (quantities.size() > 3)
4586 aMesh->ChangePolyhedronNodes(elem, poly_nodes, quantities);
4588 rmElemIds.push_back(elem->GetID());
4592 rmElemIds.push_back(elem->GetID());
4603 switch ( nbNodes ) {
4604 case 2: ///////////////////////////////////// EDGE
4605 isOk = false; break;
4606 case 3: ///////////////////////////////////// TRIANGLE
4607 isOk = false; break;
4609 if ( elem->GetType() == SMDSAbs_Volume ) // TETRAHEDRON
4611 else { //////////////////////////////////// QUADRANGLE
4612 if ( nbUniqueNodes < 3 )
4614 else if ( nbRepl == 2 && iRepl[ 1 ] - iRepl[ 0 ] == 2 )
4615 isOk = false; // opposite nodes stick
4618 case 6: ///////////////////////////////////// PENTAHEDRON
4619 if ( nbUniqueNodes == 4 ) {
4620 // ---------------------------------> tetrahedron
4622 iRepl[ 0 ] > 2 && iRepl[ 1 ] > 2 && iRepl[ 2 ] > 2 ) {
4623 // all top nodes stick: reverse a bottom
4624 uniqueNodes[ 0 ] = curNodes [ 1 ];
4625 uniqueNodes[ 1 ] = curNodes [ 0 ];
4627 else if (nbRepl == 3 &&
4628 iRepl[ 0 ] < 3 && iRepl[ 1 ] < 3 && iRepl[ 2 ] < 3 ) {
4629 // all bottom nodes stick: set a top before
4630 uniqueNodes[ 3 ] = uniqueNodes [ 0 ];
4631 uniqueNodes[ 0 ] = curNodes [ 3 ];
4632 uniqueNodes[ 1 ] = curNodes [ 4 ];
4633 uniqueNodes[ 2 ] = curNodes [ 5 ];
4635 else if (nbRepl == 4 &&
4636 iRepl[ 2 ] - iRepl [ 0 ] == 3 && iRepl[ 3 ] - iRepl [ 1 ] == 3 ) {
4637 // a lateral face turns into a line: reverse a bottom
4638 uniqueNodes[ 0 ] = curNodes [ 1 ];
4639 uniqueNodes[ 1 ] = curNodes [ 0 ];
4644 else if ( nbUniqueNodes == 5 ) {
4645 // PENTAHEDRON --------------------> 2 tetrahedrons
4646 if ( nbRepl == 2 && iRepl[ 1 ] - iRepl [ 0 ] == 3 ) {
4647 // a bottom node sticks with a linked top one
4649 SMDS_MeshElement* newElem =
4650 aMesh->AddVolume(curNodes[ 3 ],
4653 curNodes[ iRepl[ 0 ] == 2 ? 1 : 2 ]);
4654 myLastCreatedElems.Append(newElem);
4656 aMesh->SetMeshElementOnShape( newElem, aShapeId );
4657 // 2. : reverse a bottom
4658 uniqueNodes[ 0 ] = curNodes [ 1 ];
4659 uniqueNodes[ 1 ] = curNodes [ 0 ];
4669 if(elem->IsQuadratic()) { // Quadratic quadrangle
4682 if( iRepl[0]==0 && iRepl[1]==1 && iRepl[2]==4 ) {
4683 uniqueNodes[0] = curNodes[0];
4684 uniqueNodes[1] = curNodes[2];
4685 uniqueNodes[2] = curNodes[3];
4686 uniqueNodes[3] = curNodes[5];
4687 uniqueNodes[4] = curNodes[6];
4688 uniqueNodes[5] = curNodes[7];
4691 if( iRepl[0]==0 && iRepl[1]==3 && iRepl[2]==7 ) {
4692 uniqueNodes[0] = curNodes[0];
4693 uniqueNodes[1] = curNodes[1];
4694 uniqueNodes[2] = curNodes[2];
4695 uniqueNodes[3] = curNodes[4];
4696 uniqueNodes[4] = curNodes[5];
4697 uniqueNodes[5] = curNodes[6];
4700 if( iRepl[0]==0 && iRepl[1]==4 && iRepl[2]==7 ) {
4701 uniqueNodes[0] = curNodes[1];
4702 uniqueNodes[1] = curNodes[2];
4703 uniqueNodes[2] = curNodes[3];
4704 uniqueNodes[3] = curNodes[5];
4705 uniqueNodes[4] = curNodes[6];
4706 uniqueNodes[5] = curNodes[0];
4709 if( iRepl[0]==1 && iRepl[1]==2 && iRepl[2]==5 ) {
4710 uniqueNodes[0] = curNodes[0];
4711 uniqueNodes[1] = curNodes[1];
4712 uniqueNodes[2] = curNodes[3];
4713 uniqueNodes[3] = curNodes[4];
4714 uniqueNodes[4] = curNodes[6];
4715 uniqueNodes[5] = curNodes[7];
4718 if( iRepl[0]==1 && iRepl[1]==4 && iRepl[2]==5 ) {
4719 uniqueNodes[0] = curNodes[0];
4720 uniqueNodes[1] = curNodes[2];
4721 uniqueNodes[2] = curNodes[3];
4722 uniqueNodes[3] = curNodes[1];
4723 uniqueNodes[4] = curNodes[6];
4724 uniqueNodes[5] = curNodes[7];
4727 if( iRepl[0]==2 && iRepl[1]==3 && iRepl[2]==6 ) {
4728 uniqueNodes[0] = curNodes[0];
4729 uniqueNodes[1] = curNodes[1];
4730 uniqueNodes[2] = curNodes[2];
4731 uniqueNodes[3] = curNodes[4];
4732 uniqueNodes[4] = curNodes[5];
4733 uniqueNodes[5] = curNodes[7];
4736 if( iRepl[0]==2 && iRepl[1]==5 && iRepl[2]==6 ) {
4737 uniqueNodes[0] = curNodes[0];
4738 uniqueNodes[1] = curNodes[1];
4739 uniqueNodes[2] = curNodes[3];
4740 uniqueNodes[3] = curNodes[4];
4741 uniqueNodes[4] = curNodes[2];
4742 uniqueNodes[5] = curNodes[7];
4745 if( iRepl[0]==3 && iRepl[1]==6 && iRepl[2]==7 ) {
4746 uniqueNodes[0] = curNodes[0];
4747 uniqueNodes[1] = curNodes[1];
4748 uniqueNodes[2] = curNodes[2];
4749 uniqueNodes[3] = curNodes[4];
4750 uniqueNodes[4] = curNodes[5];
4751 uniqueNodes[5] = curNodes[3];
4757 //////////////////////////////////// HEXAHEDRON
4759 SMDS_VolumeTool hexa (elem);
4760 hexa.SetExternalNormal();
4761 if ( nbUniqueNodes == 4 && nbRepl == 6 ) {
4762 //////////////////////// ---> tetrahedron
4763 for ( int iFace = 0; iFace < 6; iFace++ ) {
4764 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
4765 if (curNodes[ind[ 0 ]] == curNodes[ind[ 1 ]] &&
4766 curNodes[ind[ 0 ]] == curNodes[ind[ 2 ]] &&
4767 curNodes[ind[ 0 ]] == curNodes[ind[ 3 ]] ) {
4768 // one face turns into a point ...
4769 int iOppFace = hexa.GetOppFaceIndex( iFace );
4770 ind = hexa.GetFaceNodesIndices( iOppFace );
4772 iUnique = 2; // reverse a tetrahedron bottom
4773 for ( iCur = 0; iCur < 4 && nbStick < 2; iCur++ ) {
4774 if ( curNodes[ind[ iCur ]] == curNodes[ind[ iCur + 1 ]] )
4776 else if ( iUnique >= 0 )
4777 uniqueNodes[ iUnique-- ] = curNodes[ind[ iCur ]];
4779 if ( nbStick == 1 ) {
4780 // ... and the opposite one - into a triangle.
4782 ind = hexa.GetFaceNodesIndices( iFace );
4783 uniqueNodes[ 3 ] = curNodes[ind[ 0 ]];
4790 else if (nbUniqueNodes == 5 && nbRepl == 4 ) {
4791 //////////////////// HEXAHEDRON ---> 2 tetrahedrons
4792 for ( int iFace = 0; iFace < 6; iFace++ ) {
4793 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
4794 if (curNodes[ind[ 0 ]] == curNodes[ind[ 1 ]] &&
4795 curNodes[ind[ 0 ]] == curNodes[ind[ 2 ]] &&
4796 curNodes[ind[ 0 ]] == curNodes[ind[ 3 ]] ) {
4797 // one face turns into a point ...
4798 int iOppFace = hexa.GetOppFaceIndex( iFace );
4799 ind = hexa.GetFaceNodesIndices( iOppFace );
4801 iUnique = 2; // reverse a tetrahedron 1 bottom
4802 for ( iCur = 0; iCur < 4 && nbStick == 0; iCur++ ) {
4803 if ( curNodes[ind[ iCur ]] == curNodes[ind[ iCur + 1 ]] )
4805 else if ( iUnique >= 0 )
4806 uniqueNodes[ iUnique-- ] = curNodes[ind[ iCur ]];
4808 if ( nbStick == 0 ) {
4809 // ... and the opposite one is a quadrangle
4811 const int* indTop = hexa.GetFaceNodesIndices( iFace );
4812 uniqueNodes[ 3 ] = curNodes[indTop[ 0 ]];
4815 SMDS_MeshElement* newElem =
4816 aMesh->AddVolume(curNodes[ind[ 0 ]],
4819 curNodes[indTop[ 0 ]]);
4820 myLastCreatedElems.Append(newElem);
4822 aMesh->SetMeshElementOnShape( newElem, aShapeId );
4829 else if ( nbUniqueNodes == 6 && nbRepl == 4 ) {
4830 ////////////////// HEXAHEDRON ---> 2 tetrahedrons or 1 prism
4831 // find indices of quad and tri faces
4832 int iQuadFace[ 6 ], iTriFace[ 6 ], nbQuad = 0, nbTri = 0, iFace;
4833 for ( iFace = 0; iFace < 6; iFace++ ) {
4834 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
4836 for ( iCur = 0; iCur < 4; iCur++ )
4837 nodeSet.insert( curNodes[ind[ iCur ]] );
4838 nbUniqueNodes = nodeSet.size();
4839 if ( nbUniqueNodes == 3 )
4840 iTriFace[ nbTri++ ] = iFace;
4841 else if ( nbUniqueNodes == 4 )
4842 iQuadFace[ nbQuad++ ] = iFace;
4844 if (nbQuad == 2 && nbTri == 4 &&
4845 hexa.GetOppFaceIndex( iQuadFace[ 0 ] ) == iQuadFace[ 1 ]) {
4846 // 2 opposite quadrangles stuck with a diagonal;
4847 // sample groups of merged indices: (0-4)(2-6)
4848 // --------------------------------------------> 2 tetrahedrons
4849 const int *ind1 = hexa.GetFaceNodesIndices( iQuadFace[ 0 ]); // indices of quad1 nodes
4850 const int *ind2 = hexa.GetFaceNodesIndices( iQuadFace[ 1 ]);
4851 int i0, i1d, i2, i3d, i0t, i2t; // d-daigonal, t-top
4852 if (curNodes[ind1[ 0 ]] == curNodes[ind2[ 0 ]] &&
4853 curNodes[ind1[ 2 ]] == curNodes[ind2[ 2 ]]) {
4854 // stuck with 0-2 diagonal
4862 else if (curNodes[ind1[ 1 ]] == curNodes[ind2[ 3 ]] &&
4863 curNodes[ind1[ 3 ]] == curNodes[ind2[ 1 ]]) {
4864 // stuck with 1-3 diagonal
4876 uniqueNodes[ 0 ] = curNodes [ i0 ];
4877 uniqueNodes[ 1 ] = curNodes [ i1d ];
4878 uniqueNodes[ 2 ] = curNodes [ i3d ];
4879 uniqueNodes[ 3 ] = curNodes [ i0t ];
4882 SMDS_MeshElement* newElem = aMesh->AddVolume(curNodes[ i1d ],
4886 myLastCreatedElems.Append(newElem);
4888 aMesh->SetMeshElementOnShape( newElem, aShapeId );
4891 else if (( nbTri == 2 && nbQuad == 3 ) || // merged (0-4)(1-5)
4892 ( nbTri == 4 && nbQuad == 2 )) { // merged (7-4)(1-5)
4893 // --------------------------------------------> prism
4894 // find 2 opposite triangles
4896 for ( iFace = 0; iFace + 1 < nbTri; iFace++ ) {
4897 if ( hexa.GetOppFaceIndex( iTriFace[ iFace ] ) == iTriFace[ iFace + 1 ]) {
4898 // find indices of kept and replaced nodes
4899 // and fill unique nodes of 2 opposite triangles
4900 const int *ind1 = hexa.GetFaceNodesIndices( iTriFace[ iFace ]);
4901 const int *ind2 = hexa.GetFaceNodesIndices( iTriFace[ iFace + 1 ]);
4902 const SMDS_MeshNode** hexanodes = hexa.GetNodes();
4903 // fill unique nodes
4906 for ( iCur = 0; iCur < 4 && isOk; iCur++ ) {
4907 const SMDS_MeshNode* n = curNodes[ind1[ iCur ]];
4908 const SMDS_MeshNode* nInit = hexanodes[ind1[ iCur ]];
4910 // iCur of a linked node of the opposite face (make normals co-directed):
4911 int iCurOpp = ( iCur == 1 || iCur == 3 ) ? 4 - iCur : iCur;
4912 // check that correspondent corners of triangles are linked
4913 if ( !hexa.IsLinked( ind1[ iCur ], ind2[ iCurOpp ] ))
4916 uniqueNodes[ iUnique ] = n;
4917 uniqueNodes[ iUnique + 3 ] = curNodes[ind2[ iCurOpp ]];
4926 } // if ( nbUniqueNodes == 6 && nbRepl == 4 )
4932 } // switch ( nbNodes )
4934 } // if ( nbNodes != nbUniqueNodes ) // some nodes stick
4937 if (elem->IsPoly() && elem->GetType() == SMDSAbs_Volume) {
4938 // Change nodes of polyedre
4939 const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
4940 static_cast<const SMDS_PolyhedralVolumeOfNodes*>( elem );
4942 int nbFaces = aPolyedre->NbFaces();
4944 vector<const SMDS_MeshNode *> poly_nodes;
4945 vector<int> quantities (nbFaces);
4947 for (int iface = 1; iface <= nbFaces; iface++) {
4948 int inode, nbFaceNodes = aPolyedre->NbFaceNodes(iface);
4949 quantities[iface - 1] = nbFaceNodes;
4951 for (inode = 1; inode <= nbFaceNodes; inode++) {
4952 const SMDS_MeshNode* curNode = aPolyedre->GetFaceNode(iface, inode);
4954 TNodeNodeMap::iterator nnIt = nodeNodeMap.find( curNode );
4955 if (nnIt != nodeNodeMap.end()) { // curNode sticks
4956 curNode = (*nnIt).second;
4958 poly_nodes.push_back(curNode);
4961 aMesh->ChangePolyhedronNodes( elem, poly_nodes, quantities );
4965 // Change regular element or polygon
4966 aMesh->ChangeElementNodes( elem, uniqueNodes, nbUniqueNodes );
4970 // Remove invalid regular element or invalid polygon
4971 rmElemIds.push_back( elem->GetID() );
4974 } // loop on elements
4976 // Remove equal nodes and bad elements
4978 Remove( rmNodeIds, true );
4979 Remove( rmElemIds, false );
4984 // =================================================
4985 // class : SortableElement
4986 // purpose : auxilary
4987 // =================================================
4988 class SortableElement : public set <const SMDS_MeshElement*>
4992 SortableElement( const SMDS_MeshElement* theElem )
4994 myID = theElem->GetID();
4995 SMDS_ElemIteratorPtr nodeIt = theElem->nodesIterator();
4996 while ( nodeIt->more() )
4997 this->insert( nodeIt->next() );
5000 const long GetID() const
5003 void SetID(const long anID) const
5012 //=======================================================================
5013 //function : MergeEqualElements
5014 //purpose : Remove all but one of elements built on the same nodes.
5015 //=======================================================================
5017 void SMESH_MeshEditor::MergeEqualElements()
5019 myLastCreatedElems.Clear();
5020 myLastCreatedNodes.Clear();
5022 SMESHDS_Mesh* aMesh = GetMeshDS();
5024 SMDS_EdgeIteratorPtr eIt = aMesh->edgesIterator();
5025 SMDS_FaceIteratorPtr fIt = aMesh->facesIterator();
5026 SMDS_VolumeIteratorPtr vIt = aMesh->volumesIterator();
5028 list< int > rmElemIds; // IDs of elems to remove
5030 for ( int iDim = 1; iDim <= 3; iDim++ ) {
5032 set< SortableElement > setOfNodeSet;
5035 const SMDS_MeshElement* elem = 0;
5037 if ( eIt->more() ) elem = eIt->next();
5038 } else if ( iDim == 2 ) {
5039 if ( fIt->more() ) elem = fIt->next();
5041 if ( vIt->more() ) elem = vIt->next();
5045 SortableElement SE(elem);
5048 pair< set<SortableElement>::iterator, bool> pp = setOfNodeSet.insert(SE);
5049 if( !(pp.second) ) {
5050 set<SortableElement>::iterator itSE = pp.first;
5051 SortableElement SEold = *itSE;
5052 if( SEold.GetID() > SE.GetID() ) {
5053 rmElemIds.push_back( SEold.GetID() );
5054 (*itSE).SetID(SE.GetID());
5057 rmElemIds.push_back( SE.GetID() );
5063 Remove( rmElemIds, false );
5066 //=======================================================================
5067 //function : FindFaceInSet
5068 //purpose : Return a face having linked nodes n1 and n2 and which is
5069 // - not in avoidSet,
5070 // - in elemSet provided that !elemSet.empty()
5071 //=======================================================================
5073 const SMDS_MeshElement*
5074 SMESH_MeshEditor::FindFaceInSet(const SMDS_MeshNode* n1,
5075 const SMDS_MeshNode* n2,
5076 const TIDSortedElemSet& elemSet,
5077 const TIDSortedElemSet& avoidSet)
5080 SMDS_ElemIteratorPtr invElemIt = n1->GetInverseElementIterator(SMDSAbs_Face);
5081 while ( invElemIt->more() ) { // loop on inverse elements of n1
5082 const SMDS_MeshElement* elem = invElemIt->next();
5083 if (avoidSet.find( elem ) != avoidSet.end() )
5085 if ( !elemSet.empty() && elemSet.find( elem ) == elemSet.end())
5087 // get face nodes and find index of n1
5088 int i1, nbN = elem->NbNodes(), iNode = 0;
5089 const SMDS_MeshNode* faceNodes[ nbN ], *n;
5090 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
5091 while ( nIt->more() ) {
5092 faceNodes[ iNode ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
5093 if ( faceNodes[ iNode++ ] == n1 )
5096 // find a n2 linked to n1
5097 if(!elem->IsQuadratic()) {
5098 for ( iNode = 0; iNode < 2; iNode++ ) {
5099 if ( iNode ) // node before n1
5100 n = faceNodes[ i1 == 0 ? nbN - 1 : i1 - 1 ];
5101 else // node after n1
5102 n = faceNodes[ i1 + 1 == nbN ? 0 : i1 + 1 ];
5107 else { // analysis for quadratic elements
5108 bool IsFind = false;
5109 // check using only corner nodes
5110 for ( iNode = 0; iNode < 2; iNode++ ) {
5111 if ( iNode ) // node before n1
5112 n = faceNodes[ i1 == 0 ? nbN/2 - 1 : i1 - 1 ];
5113 else // node after n1
5114 n = faceNodes[ i1 + 1 == nbN/2 ? 0 : i1 + 1 ];
5122 // check using all nodes
5123 const SMDS_QuadraticFaceOfNodes* F =
5124 static_cast<const SMDS_QuadraticFaceOfNodes*>(elem);
5125 // use special nodes iterator
5127 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5128 while ( anIter->more() ) {
5129 faceNodes[iNode] = static_cast<const SMDS_MeshNode*>(anIter->next());
5130 if ( faceNodes[ iNode++ ] == n1 )
5133 for ( iNode = 0; iNode < 2; iNode++ ) {
5134 if ( iNode ) // node before n1
5135 n = faceNodes[ i1 == 0 ? nbN - 1 : i1 - 1 ];
5136 else // node after n1
5137 n = faceNodes[ i1 + 1 == nbN ? 0 : i1 + 1 ];
5143 } // end analysis for quadratic elements
5148 //=======================================================================
5149 //function : findAdjacentFace
5151 //=======================================================================
5153 static const SMDS_MeshElement* findAdjacentFace(const SMDS_MeshNode* n1,
5154 const SMDS_MeshNode* n2,
5155 const SMDS_MeshElement* elem)
5157 TIDSortedElemSet elemSet, avoidSet;
5159 avoidSet.insert ( elem );
5160 return SMESH_MeshEditor::FindFaceInSet( n1, n2, elemSet, avoidSet );
5163 //=======================================================================
5164 //function : FindFreeBorder
5166 //=======================================================================
5168 #define ControlFreeBorder SMESH::Controls::FreeEdges::IsFreeEdge
5170 bool SMESH_MeshEditor::FindFreeBorder (const SMDS_MeshNode* theFirstNode,
5171 const SMDS_MeshNode* theSecondNode,
5172 const SMDS_MeshNode* theLastNode,
5173 list< const SMDS_MeshNode* > & theNodes,
5174 list< const SMDS_MeshElement* >& theFaces)
5176 if ( !theFirstNode || !theSecondNode )
5178 // find border face between theFirstNode and theSecondNode
5179 const SMDS_MeshElement* curElem = findAdjacentFace( theFirstNode, theSecondNode, 0 );
5183 theFaces.push_back( curElem );
5184 theNodes.push_back( theFirstNode );
5185 theNodes.push_back( theSecondNode );
5187 //vector<const SMDS_MeshNode*> nodes;
5188 const SMDS_MeshNode *nIgnore = theFirstNode, *nStart = theSecondNode;
5189 set < const SMDS_MeshElement* > foundElems;
5190 bool needTheLast = ( theLastNode != 0 );
5192 while ( nStart != theLastNode ) {
5193 if ( nStart == theFirstNode )
5194 return !needTheLast;
5196 // find all free border faces sharing form nStart
5198 list< const SMDS_MeshElement* > curElemList;
5199 list< const SMDS_MeshNode* > nStartList;
5200 SMDS_ElemIteratorPtr invElemIt = nStart->GetInverseElementIterator(SMDSAbs_Face);
5201 while ( invElemIt->more() ) {
5202 const SMDS_MeshElement* e = invElemIt->next();
5203 if ( e == curElem || foundElems.insert( e ).second ) {
5205 int iNode = 0, nbNodes = e->NbNodes();
5206 const SMDS_MeshNode* nodes[nbNodes+1];
5207 if(e->IsQuadratic()) {
5208 const SMDS_QuadraticFaceOfNodes* F =
5209 static_cast<const SMDS_QuadraticFaceOfNodes*>(e);
5210 // use special nodes iterator
5211 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5212 while( anIter->more() ) {
5213 nodes[ iNode++ ] = anIter->next();
5217 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
5218 while ( nIt->more() )
5219 nodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
5221 nodes[ iNode ] = nodes[ 0 ];
5223 for ( iNode = 0; iNode < nbNodes; iNode++ )
5224 if (((nodes[ iNode ] == nStart && nodes[ iNode + 1] != nIgnore ) ||
5225 (nodes[ iNode + 1] == nStart && nodes[ iNode ] != nIgnore )) &&
5226 ControlFreeBorder( &nodes[ iNode ], e->GetID() ))
5228 nStartList.push_back( nodes[ iNode + ( nodes[ iNode ] == nStart ? 1 : 0 )]);
5229 curElemList.push_back( e );
5233 // analyse the found
5235 int nbNewBorders = curElemList.size();
5236 if ( nbNewBorders == 0 ) {
5237 // no free border furthermore
5238 return !needTheLast;
5240 else if ( nbNewBorders == 1 ) {
5241 // one more element found
5243 nStart = nStartList.front();
5244 curElem = curElemList.front();
5245 theFaces.push_back( curElem );
5246 theNodes.push_back( nStart );
5249 // several continuations found
5250 list< const SMDS_MeshElement* >::iterator curElemIt;
5251 list< const SMDS_MeshNode* >::iterator nStartIt;
5252 // check if one of them reached the last node
5253 if ( needTheLast ) {
5254 for (curElemIt = curElemList.begin(), nStartIt = nStartList.begin();
5255 curElemIt!= curElemList.end();
5256 curElemIt++, nStartIt++ )
5257 if ( *nStartIt == theLastNode ) {
5258 theFaces.push_back( *curElemIt );
5259 theNodes.push_back( *nStartIt );
5263 // find the best free border by the continuations
5264 list<const SMDS_MeshNode*> contNodes[ 2 ], *cNL;
5265 list<const SMDS_MeshElement*> contFaces[ 2 ], *cFL;
5266 for (curElemIt = curElemList.begin(), nStartIt = nStartList.begin();
5267 curElemIt!= curElemList.end();
5268 curElemIt++, nStartIt++ )
5270 cNL = & contNodes[ contNodes[0].empty() ? 0 : 1 ];
5271 cFL = & contFaces[ contFaces[0].empty() ? 0 : 1 ];
5272 // find one more free border
5273 if ( ! FindFreeBorder( nStart, *nStartIt, theLastNode, *cNL, *cFL )) {
5277 else if ( !contNodes[0].empty() && !contNodes[1].empty() ) {
5278 // choice: clear a worse one
5279 int iLongest = ( contNodes[0].size() < contNodes[1].size() ? 1 : 0 );
5280 int iWorse = ( needTheLast ? 1 - iLongest : iLongest );
5281 contNodes[ iWorse ].clear();
5282 contFaces[ iWorse ].clear();
5285 if ( contNodes[0].empty() && contNodes[1].empty() )
5288 // append the best free border
5289 cNL = & contNodes[ contNodes[0].empty() ? 1 : 0 ];
5290 cFL = & contFaces[ contFaces[0].empty() ? 1 : 0 ];
5291 theNodes.pop_back(); // remove nIgnore
5292 theNodes.pop_back(); // remove nStart
5293 theFaces.pop_back(); // remove curElem
5294 list< const SMDS_MeshNode* >::iterator nIt = cNL->begin();
5295 list< const SMDS_MeshElement* >::iterator fIt = cFL->begin();
5296 for ( ; nIt != cNL->end(); nIt++ ) theNodes.push_back( *nIt );
5297 for ( ; fIt != cFL->end(); fIt++ ) theFaces.push_back( *fIt );
5300 } // several continuations found
5301 } // while ( nStart != theLastNode )
5306 //=======================================================================
5307 //function : CheckFreeBorderNodes
5308 //purpose : Return true if the tree nodes are on a free border
5309 //=======================================================================
5311 bool SMESH_MeshEditor::CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
5312 const SMDS_MeshNode* theNode2,
5313 const SMDS_MeshNode* theNode3)
5315 list< const SMDS_MeshNode* > nodes;
5316 list< const SMDS_MeshElement* > faces;
5317 return FindFreeBorder( theNode1, theNode2, theNode3, nodes, faces);
5320 //=======================================================================
5321 //function : SewFreeBorder
5323 //=======================================================================
5325 SMESH_MeshEditor::Sew_Error
5326 SMESH_MeshEditor::SewFreeBorder (const SMDS_MeshNode* theBordFirstNode,
5327 const SMDS_MeshNode* theBordSecondNode,
5328 const SMDS_MeshNode* theBordLastNode,
5329 const SMDS_MeshNode* theSideFirstNode,
5330 const SMDS_MeshNode* theSideSecondNode,
5331 const SMDS_MeshNode* theSideThirdNode,
5332 const bool theSideIsFreeBorder,
5333 const bool toCreatePolygons,
5334 const bool toCreatePolyedrs)
5336 myLastCreatedElems.Clear();
5337 myLastCreatedNodes.Clear();
5339 MESSAGE("::SewFreeBorder()");
5340 Sew_Error aResult = SEW_OK;
5342 // ====================================
5343 // find side nodes and elements
5344 // ====================================
5346 list< const SMDS_MeshNode* > nSide[ 2 ];
5347 list< const SMDS_MeshElement* > eSide[ 2 ];
5348 list< const SMDS_MeshNode* >::iterator nIt[ 2 ];
5349 list< const SMDS_MeshElement* >::iterator eIt[ 2 ];
5353 if (!FindFreeBorder(theBordFirstNode,theBordSecondNode,theBordLastNode,
5354 nSide[0], eSide[0])) {
5355 MESSAGE(" Free Border 1 not found " );
5356 aResult = SEW_BORDER1_NOT_FOUND;
5358 if (theSideIsFreeBorder) {
5361 if (!FindFreeBorder(theSideFirstNode, theSideSecondNode, theSideThirdNode,
5362 nSide[1], eSide[1])) {
5363 MESSAGE(" Free Border 2 not found " );
5364 aResult = ( aResult != SEW_OK ? SEW_BOTH_BORDERS_NOT_FOUND : SEW_BORDER2_NOT_FOUND );
5367 if ( aResult != SEW_OK )
5370 if (!theSideIsFreeBorder) {
5374 // -------------------------------------------------------------------------
5376 // 1. If nodes to merge are not coincident, move nodes of the free border
5377 // from the coord sys defined by the direction from the first to last
5378 // nodes of the border to the correspondent sys of the side 2
5379 // 2. On the side 2, find the links most co-directed with the correspondent
5380 // links of the free border
5381 // -------------------------------------------------------------------------
5383 // 1. Since sewing may brake if there are volumes to split on the side 2,
5384 // we wont move nodes but just compute new coordinates for them
5385 typedef map<const SMDS_MeshNode*, gp_XYZ> TNodeXYZMap;
5386 TNodeXYZMap nBordXYZ;
5387 list< const SMDS_MeshNode* >& bordNodes = nSide[ 0 ];
5388 list< const SMDS_MeshNode* >::iterator nBordIt;
5390 gp_XYZ Pb1( theBordFirstNode->X(), theBordFirstNode->Y(), theBordFirstNode->Z() );
5391 gp_XYZ Pb2( theBordLastNode->X(), theBordLastNode->Y(), theBordLastNode->Z() );
5392 gp_XYZ Ps1( theSideFirstNode->X(), theSideFirstNode->Y(), theSideFirstNode->Z() );
5393 gp_XYZ Ps2( theSideSecondNode->X(), theSideSecondNode->Y(), theSideSecondNode->Z() );
5394 double tol2 = 1.e-8;
5395 gp_Vec Vbs1( Pb1 - Ps1 ),Vbs2( Pb2 - Ps2 );
5396 if ( Vbs1.SquareMagnitude() > tol2 || Vbs2.SquareMagnitude() > tol2 ) {
5397 // Need node movement.
5399 // find X and Z axes to create trsf
5400 gp_Vec Zb( Pb1 - Pb2 ), Zs( Ps1 - Ps2 );
5402 if ( X.SquareMagnitude() <= gp::Resolution() * gp::Resolution() )
5404 X = gp_Ax2( gp::Origin(), Zb ).XDirection();
5407 gp_Ax3 toBordAx( Pb1, Zb, X );
5408 gp_Ax3 fromSideAx( Ps1, Zs, X );
5409 gp_Ax3 toGlobalAx( gp::Origin(), gp::DZ(), gp::DX() );
5411 gp_Trsf toBordSys, fromSide2Sys;
5412 toBordSys.SetTransformation( toBordAx );
5413 fromSide2Sys.SetTransformation( fromSideAx, toGlobalAx );
5414 fromSide2Sys.SetScaleFactor( Zs.Magnitude() / Zb.Magnitude() );
5417 for ( nBordIt = bordNodes.begin(); nBordIt != bordNodes.end(); nBordIt++ ) {
5418 const SMDS_MeshNode* n = *nBordIt;
5419 gp_XYZ xyz( n->X(),n->Y(),n->Z() );
5420 toBordSys.Transforms( xyz );
5421 fromSide2Sys.Transforms( xyz );
5422 nBordXYZ.insert( TNodeXYZMap::value_type( n, xyz ));
5426 // just insert nodes XYZ in the nBordXYZ map
5427 for ( nBordIt = bordNodes.begin(); nBordIt != bordNodes.end(); nBordIt++ ) {
5428 const SMDS_MeshNode* n = *nBordIt;
5429 nBordXYZ.insert( TNodeXYZMap::value_type( n, gp_XYZ( n->X(),n->Y(),n->Z() )));
5433 // 2. On the side 2, find the links most co-directed with the correspondent
5434 // links of the free border
5436 list< const SMDS_MeshElement* >& sideElems = eSide[ 1 ];
5437 list< const SMDS_MeshNode* >& sideNodes = nSide[ 1 ];
5438 sideNodes.push_back( theSideFirstNode );
5440 bool hasVolumes = false;
5441 LinkID_Gen aLinkID_Gen( GetMeshDS() );
5442 set<long> foundSideLinkIDs, checkedLinkIDs;
5443 SMDS_VolumeTool volume;
5444 //const SMDS_MeshNode* faceNodes[ 4 ];
5446 const SMDS_MeshNode* sideNode;
5447 const SMDS_MeshElement* sideElem;
5448 const SMDS_MeshNode* prevSideNode = theSideFirstNode;
5449 const SMDS_MeshNode* prevBordNode = theBordFirstNode;
5450 nBordIt = bordNodes.begin();
5452 // border node position and border link direction to compare with
5453 gp_XYZ bordPos = nBordXYZ[ *nBordIt ];
5454 gp_XYZ bordDir = bordPos - nBordXYZ[ prevBordNode ];
5455 // choose next side node by link direction or by closeness to
5456 // the current border node:
5457 bool searchByDir = ( *nBordIt != theBordLastNode );
5459 // find the next node on the Side 2
5461 double maxDot = -DBL_MAX, minDist = DBL_MAX;
5463 checkedLinkIDs.clear();
5464 gp_XYZ prevXYZ( prevSideNode->X(), prevSideNode->Y(), prevSideNode->Z() );
5466 // loop on inverse elements of current node (prevSideNode) on the Side 2
5467 SMDS_ElemIteratorPtr invElemIt = prevSideNode->GetInverseElementIterator();
5468 while ( invElemIt->more() )
5470 const SMDS_MeshElement* elem = invElemIt->next();
5471 // prepare data for a loop on links coming to prevSideNode, of a face or a volume
5472 int iPrevNode, iNode = 0, nbNodes = elem->NbNodes();
5473 const SMDS_MeshNode* faceNodes[ nbNodes ];
5474 bool isVolume = volume.Set( elem );
5475 const SMDS_MeshNode** nodes = isVolume ? volume.GetNodes() : faceNodes;
5476 if ( isVolume ) // --volume
5478 //else if ( nbNodes > 2 ) { // --face
5479 else if ( elem->GetType()==SMDSAbs_Face ) { // --face
5480 // retrieve all face nodes and find iPrevNode - an index of the prevSideNode
5481 if(elem->IsQuadratic()) {
5482 const SMDS_QuadraticFaceOfNodes* F =
5483 static_cast<const SMDS_QuadraticFaceOfNodes*>(elem);
5484 // use special nodes iterator
5485 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5486 while( anIter->more() ) {
5487 nodes[ iNode ] = anIter->next();
5488 if ( nodes[ iNode++ ] == prevSideNode )
5489 iPrevNode = iNode - 1;
5493 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
5494 while ( nIt->more() ) {
5495 nodes[ iNode ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
5496 if ( nodes[ iNode++ ] == prevSideNode )
5497 iPrevNode = iNode - 1;
5500 // there are 2 links to check
5505 // loop on links, to be precise, on the second node of links
5506 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
5507 const SMDS_MeshNode* n = nodes[ iNode ];
5509 if ( !volume.IsLinked( n, prevSideNode ))
5513 if ( iNode ) // a node before prevSideNode
5514 n = nodes[ iPrevNode == 0 ? elem->NbNodes() - 1 : iPrevNode - 1 ];
5515 else // a node after prevSideNode
5516 n = nodes[ iPrevNode + 1 == elem->NbNodes() ? 0 : iPrevNode + 1 ];
5518 // check if this link was already used
5519 long iLink = aLinkID_Gen.GetLinkID( prevSideNode, n );
5520 bool isJustChecked = !checkedLinkIDs.insert( iLink ).second;
5521 if (!isJustChecked &&
5522 foundSideLinkIDs.find( iLink ) == foundSideLinkIDs.end() )
5524 // test a link geometrically
5525 gp_XYZ nextXYZ ( n->X(), n->Y(), n->Z() );
5526 bool linkIsBetter = false;
5528 if ( searchByDir ) { // choose most co-directed link
5529 dot = bordDir * ( nextXYZ - prevXYZ ).Normalized();
5530 linkIsBetter = ( dot > maxDot );
5532 else { // choose link with the node closest to bordPos
5533 dist = ( nextXYZ - bordPos ).SquareModulus();
5534 linkIsBetter = ( dist < minDist );
5536 if ( linkIsBetter ) {
5545 } // loop on inverse elements of prevSideNode
5548 MESSAGE(" Cant find path by links of the Side 2 ");
5549 return SEW_BAD_SIDE_NODES;
5551 sideNodes.push_back( sideNode );
5552 sideElems.push_back( sideElem );
5553 foundSideLinkIDs.insert ( linkID );
5554 prevSideNode = sideNode;
5556 if ( *nBordIt == theBordLastNode )
5557 searchByDir = false;
5559 // find the next border link to compare with
5560 gp_XYZ sidePos( sideNode->X(), sideNode->Y(), sideNode->Z() );
5561 searchByDir = ( bordDir * ( sidePos - bordPos ) <= 0 );
5562 // move to next border node if sideNode is before forward border node (bordPos)
5563 while ( *nBordIt != theBordLastNode && !searchByDir ) {
5564 prevBordNode = *nBordIt;
5566 bordPos = nBordXYZ[ *nBordIt ];
5567 bordDir = bordPos - nBordXYZ[ prevBordNode ];
5568 searchByDir = ( bordDir * ( sidePos - bordPos ) <= 0 );
5572 while ( sideNode != theSideSecondNode );
5574 if ( hasVolumes && sideNodes.size () != bordNodes.size() && !toCreatePolyedrs) {
5575 MESSAGE("VOLUME SPLITTING IS FORBIDDEN");
5576 return SEW_VOLUMES_TO_SPLIT; // volume splitting is forbidden
5578 } // end nodes search on the side 2
5580 // ============================
5581 // sew the border to the side 2
5582 // ============================
5584 int nbNodes[] = { nSide[0].size(), nSide[1].size() };
5585 int maxNbNodes = Max( nbNodes[0], nbNodes[1] );
5587 TListOfListOfNodes nodeGroupsToMerge;
5588 if ( nbNodes[0] == nbNodes[1] ||
5589 ( theSideIsFreeBorder && !theSideThirdNode)) {
5591 // all nodes are to be merged
5593 for (nIt[0] = nSide[0].begin(), nIt[1] = nSide[1].begin();
5594 nIt[0] != nSide[0].end() && nIt[1] != nSide[1].end();
5595 nIt[0]++, nIt[1]++ )
5597 nodeGroupsToMerge.push_back( list<const SMDS_MeshNode*>() );
5598 nodeGroupsToMerge.back().push_back( *nIt[1] ); // to keep
5599 nodeGroupsToMerge.back().push_back( *nIt[0] ); // tp remove
5604 // insert new nodes into the border and the side to get equal nb of segments
5606 // get normalized parameters of nodes on the borders
5607 double param[ 2 ][ maxNbNodes ];
5609 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
5610 list< const SMDS_MeshNode* >& nodes = nSide[ iBord ];
5611 list< const SMDS_MeshNode* >::iterator nIt = nodes.begin();
5612 const SMDS_MeshNode* nPrev = *nIt;
5613 double bordLength = 0;
5614 for ( iNode = 0; nIt != nodes.end(); nIt++, iNode++ ) { // loop on border nodes
5615 const SMDS_MeshNode* nCur = *nIt;
5616 gp_XYZ segment (nCur->X() - nPrev->X(),
5617 nCur->Y() - nPrev->Y(),
5618 nCur->Z() - nPrev->Z());
5619 double segmentLen = segment.Modulus();
5620 bordLength += segmentLen;
5621 param[ iBord ][ iNode ] = bordLength;
5624 // normalize within [0,1]
5625 for ( iNode = 0; iNode < nbNodes[ iBord ]; iNode++ ) {
5626 param[ iBord ][ iNode ] /= bordLength;
5630 // loop on border segments
5631 const SMDS_MeshNode *nPrev[ 2 ] = { 0, 0 };
5632 int i[ 2 ] = { 0, 0 };
5633 nIt[0] = nSide[0].begin(); eIt[0] = eSide[0].begin();
5634 nIt[1] = nSide[1].begin(); eIt[1] = eSide[1].begin();
5636 TElemOfNodeListMap insertMap;
5637 TElemOfNodeListMap::iterator insertMapIt;
5639 // key: elem to insert nodes into
5640 // value: 2 nodes to insert between + nodes to be inserted
5642 bool next[ 2 ] = { false, false };
5644 // find min adjacent segment length after sewing
5645 double nextParam = 10., prevParam = 0;
5646 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
5647 if ( i[ iBord ] + 1 < nbNodes[ iBord ])
5648 nextParam = Min( nextParam, param[iBord][ i[iBord] + 1 ]);
5649 if ( i[ iBord ] > 0 )
5650 prevParam = Max( prevParam, param[iBord][ i[iBord] - 1 ]);
5652 double minParam = Min( param[ 0 ][ i[0] ], param[ 1 ][ i[1] ]);
5653 double maxParam = Max( param[ 0 ][ i[0] ], param[ 1 ][ i[1] ]);
5654 double minSegLen = Min( nextParam - minParam, maxParam - prevParam );
5656 // choose to insert or to merge nodes
5657 double du = param[ 1 ][ i[1] ] - param[ 0 ][ i[0] ];
5658 if ( Abs( du ) <= minSegLen * 0.2 ) {
5661 nodeGroupsToMerge.push_back( list<const SMDS_MeshNode*>() );
5662 const SMDS_MeshNode* n0 = *nIt[0];
5663 const SMDS_MeshNode* n1 = *nIt[1];
5664 nodeGroupsToMerge.back().push_back( n1 );
5665 nodeGroupsToMerge.back().push_back( n0 );
5666 // position of node of the border changes due to merge
5667 param[ 0 ][ i[0] ] += du;
5668 // move n1 for the sake of elem shape evaluation during insertion.
5669 // n1 will be removed by MergeNodes() anyway
5670 const_cast<SMDS_MeshNode*>( n0 )->setXYZ( n1->X(), n1->Y(), n1->Z() );
5671 next[0] = next[1] = true;
5676 int intoBord = ( du < 0 ) ? 0 : 1;
5677 const SMDS_MeshElement* elem = *eIt[ intoBord ];
5678 const SMDS_MeshNode* n1 = nPrev[ intoBord ];
5679 const SMDS_MeshNode* n2 = *nIt[ intoBord ];
5680 const SMDS_MeshNode* nIns = *nIt[ 1 - intoBord ];
5681 if ( intoBord == 1 ) {
5682 // move node of the border to be on a link of elem of the side
5683 gp_XYZ p1 (n1->X(), n1->Y(), n1->Z());
5684 gp_XYZ p2 (n2->X(), n2->Y(), n2->Z());
5685 double ratio = du / ( param[ 1 ][ i[1] ] - param[ 1 ][ i[1]-1 ]);
5686 gp_XYZ p = p2 * ( 1 - ratio ) + p1 * ratio;
5687 GetMeshDS()->MoveNode( nIns, p.X(), p.Y(), p.Z() );
5689 insertMapIt = insertMap.find( elem );
5690 bool notFound = ( insertMapIt == insertMap.end() );
5691 bool otherLink = ( !notFound && (*insertMapIt).second.front() != n1 );
5693 // insert into another link of the same element:
5694 // 1. perform insertion into the other link of the elem
5695 list<const SMDS_MeshNode*> & nodeList = (*insertMapIt).second;
5696 const SMDS_MeshNode* n12 = nodeList.front(); nodeList.pop_front();
5697 const SMDS_MeshNode* n22 = nodeList.front(); nodeList.pop_front();
5698 InsertNodesIntoLink( elem, n12, n22, nodeList, toCreatePolygons );
5699 // 2. perform insertion into the link of adjacent faces
5701 const SMDS_MeshElement* adjElem = findAdjacentFace( n12, n22, elem );
5703 InsertNodesIntoLink( adjElem, n12, n22, nodeList, toCreatePolygons );
5707 if (toCreatePolyedrs) {
5708 // perform insertion into the links of adjacent volumes
5709 UpdateVolumes(n12, n22, nodeList);
5711 // 3. find an element appeared on n1 and n2 after the insertion
5712 insertMap.erase( elem );
5713 elem = findAdjacentFace( n1, n2, 0 );
5715 if ( notFound || otherLink ) {
5716 // add element and nodes of the side into the insertMap
5717 insertMapIt = insertMap.insert
5718 ( TElemOfNodeListMap::value_type( elem, list<const SMDS_MeshNode*>() )).first;
5719 (*insertMapIt).second.push_back( n1 );
5720 (*insertMapIt).second.push_back( n2 );
5722 // add node to be inserted into elem
5723 (*insertMapIt).second.push_back( nIns );
5724 next[ 1 - intoBord ] = true;
5727 // go to the next segment
5728 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
5729 if ( next[ iBord ] ) {
5730 if ( i[ iBord ] != 0 && eIt[ iBord ] != eSide[ iBord ].end())
5732 nPrev[ iBord ] = *nIt[ iBord ];
5733 nIt[ iBord ]++; i[ iBord ]++;
5737 while ( nIt[0] != nSide[0].end() && nIt[1] != nSide[1].end());
5739 // perform insertion of nodes into elements
5741 for (insertMapIt = insertMap.begin();
5742 insertMapIt != insertMap.end();
5745 const SMDS_MeshElement* elem = (*insertMapIt).first;
5746 list<const SMDS_MeshNode*> & nodeList = (*insertMapIt).second;
5747 const SMDS_MeshNode* n1 = nodeList.front(); nodeList.pop_front();
5748 const SMDS_MeshNode* n2 = nodeList.front(); nodeList.pop_front();
5750 InsertNodesIntoLink( elem, n1, n2, nodeList, toCreatePolygons );
5752 if ( !theSideIsFreeBorder ) {
5753 // look for and insert nodes into the faces adjacent to elem
5755 const SMDS_MeshElement* adjElem = findAdjacentFace( n1, n2, elem );
5757 InsertNodesIntoLink( adjElem, n1, n2, nodeList, toCreatePolygons );
5762 if (toCreatePolyedrs) {
5763 // perform insertion into the links of adjacent volumes
5764 UpdateVolumes(n1, n2, nodeList);
5768 } // end: insert new nodes
5770 MergeNodes ( nodeGroupsToMerge );
5775 //=======================================================================
5776 //function : InsertNodesIntoLink
5777 //purpose : insert theNodesToInsert into theFace between theBetweenNode1
5778 // and theBetweenNode2 and split theElement
5779 //=======================================================================
5781 void SMESH_MeshEditor::InsertNodesIntoLink(const SMDS_MeshElement* theFace,
5782 const SMDS_MeshNode* theBetweenNode1,
5783 const SMDS_MeshNode* theBetweenNode2,
5784 list<const SMDS_MeshNode*>& theNodesToInsert,
5785 const bool toCreatePoly)
5787 if ( theFace->GetType() != SMDSAbs_Face ) return;
5789 // find indices of 2 link nodes and of the rest nodes
5790 int iNode = 0, il1, il2, i3, i4;
5791 il1 = il2 = i3 = i4 = -1;
5792 const SMDS_MeshNode* nodes[ theFace->NbNodes() ];
5794 if(theFace->IsQuadratic()) {
5795 const SMDS_QuadraticFaceOfNodes* F =
5796 static_cast<const SMDS_QuadraticFaceOfNodes*>(theFace);
5797 // use special nodes iterator
5798 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5799 while( anIter->more() ) {
5800 const SMDS_MeshNode* n = anIter->next();
5801 if ( n == theBetweenNode1 )
5803 else if ( n == theBetweenNode2 )
5809 nodes[ iNode++ ] = n;
5813 SMDS_ElemIteratorPtr nodeIt = theFace->nodesIterator();
5814 while ( nodeIt->more() ) {
5815 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
5816 if ( n == theBetweenNode1 )
5818 else if ( n == theBetweenNode2 )
5824 nodes[ iNode++ ] = n;
5827 if ( il1 < 0 || il2 < 0 || i3 < 0 )
5830 // arrange link nodes to go one after another regarding the face orientation
5831 bool reverse = ( Abs( il2 - il1 ) == 1 ? il2 < il1 : il1 < il2 );
5832 list<const SMDS_MeshNode *> aNodesToInsert = theNodesToInsert;
5837 aNodesToInsert.reverse();
5839 // check that not link nodes of a quadrangles are in good order
5840 int nbFaceNodes = theFace->NbNodes();
5841 if ( nbFaceNodes == 4 && i4 - i3 != 1 ) {
5847 if (toCreatePoly || theFace->IsPoly()) {
5850 vector<const SMDS_MeshNode *> poly_nodes (nbFaceNodes + aNodesToInsert.size());
5852 // add nodes of face up to first node of link
5855 if(theFace->IsQuadratic()) {
5856 const SMDS_QuadraticFaceOfNodes* F =
5857 static_cast<const SMDS_QuadraticFaceOfNodes*>(theFace);
5858 // use special nodes iterator
5859 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5860 while( anIter->more() && !isFLN ) {
5861 const SMDS_MeshNode* n = anIter->next();
5862 poly_nodes[iNode++] = n;
5863 if (n == nodes[il1]) {
5867 // add nodes to insert
5868 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
5869 for (; nIt != aNodesToInsert.end(); nIt++) {
5870 poly_nodes[iNode++] = *nIt;
5872 // add nodes of face starting from last node of link
5873 while ( anIter->more() ) {
5874 poly_nodes[iNode++] = anIter->next();
5878 SMDS_ElemIteratorPtr nodeIt = theFace->nodesIterator();
5879 while ( nodeIt->more() && !isFLN ) {
5880 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
5881 poly_nodes[iNode++] = n;
5882 if (n == nodes[il1]) {
5886 // add nodes to insert
5887 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
5888 for (; nIt != aNodesToInsert.end(); nIt++) {
5889 poly_nodes[iNode++] = *nIt;
5891 // add nodes of face starting from last node of link
5892 while ( nodeIt->more() ) {
5893 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
5894 poly_nodes[iNode++] = n;
5898 // edit or replace the face
5899 SMESHDS_Mesh *aMesh = GetMeshDS();
5901 if (theFace->IsPoly()) {
5902 aMesh->ChangePolygonNodes(theFace, poly_nodes);
5905 int aShapeId = FindShape( theFace );
5907 SMDS_MeshElement* newElem = aMesh->AddPolygonalFace(poly_nodes);
5908 myLastCreatedElems.Append(newElem);
5909 if ( aShapeId && newElem )
5910 aMesh->SetMeshElementOnShape( newElem, aShapeId );
5912 aMesh->RemoveElement(theFace);
5917 if( !theFace->IsQuadratic() ) {
5919 // put aNodesToInsert between theBetweenNode1 and theBetweenNode2
5920 int nbLinkNodes = 2 + aNodesToInsert.size();
5921 const SMDS_MeshNode* linkNodes[ nbLinkNodes ];
5922 linkNodes[ 0 ] = nodes[ il1 ];
5923 linkNodes[ nbLinkNodes - 1 ] = nodes[ il2 ];
5924 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
5925 for ( iNode = 1; nIt != aNodesToInsert.end(); nIt++ ) {
5926 linkNodes[ iNode++ ] = *nIt;
5928 // decide how to split a quadrangle: compare possible variants
5929 // and choose which of splits to be a quadrangle
5930 int i1, i2, iSplit, nbSplits = nbLinkNodes - 1, iBestQuad;
5931 if ( nbFaceNodes == 3 ) {
5932 iBestQuad = nbSplits;
5935 else if ( nbFaceNodes == 4 ) {
5936 SMESH::Controls::NumericalFunctorPtr aCrit( new SMESH::Controls::AspectRatio);
5937 double aBestRate = DBL_MAX;
5938 for ( int iQuad = 0; iQuad < nbSplits; iQuad++ ) {
5940 double aBadRate = 0;
5941 // evaluate elements quality
5942 for ( iSplit = 0; iSplit < nbSplits; iSplit++ ) {
5943 if ( iSplit == iQuad ) {
5944 SMDS_FaceOfNodes quad (linkNodes[ i1++ ],
5948 aBadRate += getBadRate( &quad, aCrit );
5951 SMDS_FaceOfNodes tria (linkNodes[ i1++ ],
5953 nodes[ iSplit < iQuad ? i4 : i3 ]);
5954 aBadRate += getBadRate( &tria, aCrit );
5958 if ( aBadRate < aBestRate ) {
5960 aBestRate = aBadRate;
5965 // create new elements
5966 SMESHDS_Mesh *aMesh = GetMeshDS();
5967 int aShapeId = FindShape( theFace );
5970 for ( iSplit = 0; iSplit < nbSplits - 1; iSplit++ ) {
5971 SMDS_MeshElement* newElem = 0;
5972 if ( iSplit == iBestQuad )
5973 newElem = aMesh->AddFace (linkNodes[ i1++ ],
5978 newElem = aMesh->AddFace (linkNodes[ i1++ ],
5980 nodes[ iSplit < iBestQuad ? i4 : i3 ]);
5981 myLastCreatedElems.Append(newElem);
5982 if ( aShapeId && newElem )
5983 aMesh->SetMeshElementOnShape( newElem, aShapeId );
5986 // change nodes of theFace
5987 const SMDS_MeshNode* newNodes[ 4 ];
5988 newNodes[ 0 ] = linkNodes[ i1 ];
5989 newNodes[ 1 ] = linkNodes[ i2 ];
5990 newNodes[ 2 ] = nodes[ iSplit >= iBestQuad ? i3 : i4 ];
5991 newNodes[ 3 ] = nodes[ i4 ];
5992 aMesh->ChangeElementNodes( theFace, newNodes, iSplit == iBestQuad ? 4 : 3 );
5993 } // end if(!theFace->IsQuadratic())
5994 else { // theFace is quadratic
5995 // we have to split theFace on simple triangles and one simple quadrangle
5997 int nbshift = tmp*2;
5998 // shift nodes in nodes[] by nbshift
6000 for(i=0; i<nbshift; i++) {
6001 const SMDS_MeshNode* n = nodes[0];
6002 for(j=0; j<nbFaceNodes-1; j++) {
6003 nodes[j] = nodes[j+1];
6005 nodes[nbFaceNodes-1] = n;
6007 il1 = il1 - nbshift;
6008 // now have to insert nodes between n0 and n1 or n1 and n2 (see below)
6009 // n0 n1 n2 n0 n1 n2
6010 // +-----+-----+ +-----+-----+
6019 // create new elements
6020 SMESHDS_Mesh *aMesh = GetMeshDS();
6021 int aShapeId = FindShape( theFace );
6024 if(nbFaceNodes==6) { // quadratic triangle
6025 SMDS_MeshElement* newElem =
6026 aMesh->AddFace(nodes[3],nodes[4],nodes[5]);
6027 myLastCreatedElems.Append(newElem);
6028 if ( aShapeId && newElem )
6029 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6030 if(theFace->IsMediumNode(nodes[il1])) {
6031 // create quadrangle
6032 newElem = aMesh->AddFace(nodes[0],nodes[1],nodes[3],nodes[5]);
6033 myLastCreatedElems.Append(newElem);
6034 if ( aShapeId && newElem )
6035 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6041 // create quadrangle
6042 newElem = aMesh->AddFace(nodes[1],nodes[2],nodes[3],nodes[5]);
6043 myLastCreatedElems.Append(newElem);
6044 if ( aShapeId && newElem )
6045 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6051 else { // nbFaceNodes==8 - quadratic quadrangle
6052 SMDS_MeshElement* newElem =
6053 aMesh->AddFace(nodes[3],nodes[4],nodes[5]);
6054 myLastCreatedElems.Append(newElem);
6055 if ( aShapeId && newElem )
6056 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6057 newElem = aMesh->AddFace(nodes[5],nodes[6],nodes[7]);
6058 myLastCreatedElems.Append(newElem);
6059 if ( aShapeId && newElem )
6060 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6061 newElem = aMesh->AddFace(nodes[5],nodes[7],nodes[3]);
6062 myLastCreatedElems.Append(newElem);
6063 if ( aShapeId && newElem )
6064 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6065 if(theFace->IsMediumNode(nodes[il1])) {
6066 // create quadrangle
6067 newElem = aMesh->AddFace(nodes[0],nodes[1],nodes[3],nodes[7]);
6068 myLastCreatedElems.Append(newElem);
6069 if ( aShapeId && newElem )
6070 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6076 // create quadrangle
6077 newElem = aMesh->AddFace(nodes[1],nodes[2],nodes[3],nodes[7]);
6078 myLastCreatedElems.Append(newElem);
6079 if ( aShapeId && newElem )
6080 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6086 // create needed triangles using n1,n2,n3 and inserted nodes
6087 int nbn = 2 + aNodesToInsert.size();
6088 const SMDS_MeshNode* aNodes[nbn];
6089 aNodes[0] = nodes[n1];
6090 aNodes[nbn-1] = nodes[n2];
6091 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
6092 for ( iNode = 1; nIt != aNodesToInsert.end(); nIt++ ) {
6093 aNodes[iNode++] = *nIt;
6095 for(i=1; i<nbn; i++) {
6096 SMDS_MeshElement* newElem =
6097 aMesh->AddFace(aNodes[i-1],aNodes[i],nodes[n3]);
6098 myLastCreatedElems.Append(newElem);
6099 if ( aShapeId && newElem )
6100 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6102 // remove old quadratic face
6103 aMesh->RemoveElement(theFace);
6107 //=======================================================================
6108 //function : UpdateVolumes
6110 //=======================================================================
6111 void SMESH_MeshEditor::UpdateVolumes (const SMDS_MeshNode* theBetweenNode1,
6112 const SMDS_MeshNode* theBetweenNode2,
6113 list<const SMDS_MeshNode*>& theNodesToInsert)
6115 myLastCreatedElems.Clear();
6116 myLastCreatedNodes.Clear();
6118 SMDS_ElemIteratorPtr invElemIt = theBetweenNode1->GetInverseElementIterator(SMDSAbs_Volume);
6119 while (invElemIt->more()) { // loop on inverse elements of theBetweenNode1
6120 const SMDS_MeshElement* elem = invElemIt->next();
6122 // check, if current volume has link theBetweenNode1 - theBetweenNode2
6123 SMDS_VolumeTool aVolume (elem);
6124 if (!aVolume.IsLinked(theBetweenNode1, theBetweenNode2))
6127 // insert new nodes in all faces of the volume, sharing link theBetweenNode1 - theBetweenNode2
6128 int iface, nbFaces = aVolume.NbFaces();
6129 vector<const SMDS_MeshNode *> poly_nodes;
6130 vector<int> quantities (nbFaces);
6132 for (iface = 0; iface < nbFaces; iface++) {
6133 int nbFaceNodes = aVolume.NbFaceNodes(iface), nbInserted = 0;
6134 // faceNodes will contain (nbFaceNodes + 1) nodes, last = first
6135 const SMDS_MeshNode** faceNodes = aVolume.GetFaceNodes(iface);
6137 for (int inode = 0; inode < nbFaceNodes; inode++) {
6138 poly_nodes.push_back(faceNodes[inode]);
6140 if (nbInserted == 0) {
6141 if (faceNodes[inode] == theBetweenNode1) {
6142 if (faceNodes[inode + 1] == theBetweenNode2) {
6143 nbInserted = theNodesToInsert.size();
6145 // add nodes to insert
6146 list<const SMDS_MeshNode*>::iterator nIt = theNodesToInsert.begin();
6147 for (; nIt != theNodesToInsert.end(); nIt++) {
6148 poly_nodes.push_back(*nIt);
6152 else if (faceNodes[inode] == theBetweenNode2) {
6153 if (faceNodes[inode + 1] == theBetweenNode1) {
6154 nbInserted = theNodesToInsert.size();
6156 // add nodes to insert in reversed order
6157 list<const SMDS_MeshNode*>::iterator nIt = theNodesToInsert.end();
6159 for (; nIt != theNodesToInsert.begin(); nIt--) {
6160 poly_nodes.push_back(*nIt);
6162 poly_nodes.push_back(*nIt);
6169 quantities[iface] = nbFaceNodes + nbInserted;
6172 // Replace or update the volume
6173 SMESHDS_Mesh *aMesh = GetMeshDS();
6175 if (elem->IsPoly()) {
6176 aMesh->ChangePolyhedronNodes(elem, poly_nodes, quantities);
6180 int aShapeId = FindShape( elem );
6182 SMDS_MeshElement* newElem =
6183 aMesh->AddPolyhedralVolume(poly_nodes, quantities);
6184 myLastCreatedElems.Append(newElem);
6185 if (aShapeId && newElem)
6186 aMesh->SetMeshElementOnShape(newElem, aShapeId);
6188 aMesh->RemoveElement(elem);
6193 //=======================================================================
6194 //function : ConvertElemToQuadratic
6196 //=======================================================================
6197 void SMESH_MeshEditor::ConvertElemToQuadratic(SMESHDS_SubMesh * theSm,
6198 SMESH_MesherHelper& theHelper,
6199 const bool theForce3d)
6201 if( !theSm ) return;
6202 SMESHDS_Mesh* meshDS = GetMeshDS();
6203 SMDS_ElemIteratorPtr ElemItr = theSm->GetElements();
6204 while(ElemItr->more())
6206 const SMDS_MeshElement* elem = ElemItr->next();
6207 if( !elem || elem->IsQuadratic() ) continue;
6209 int id = elem->GetID();
6210 int nbNodes = elem->NbNodes();
6211 vector<const SMDS_MeshNode *> aNds (nbNodes);
6213 for(int i = 0; i < nbNodes; i++)
6215 aNds[i] = elem->GetNode(i);
6217 SMDSAbs_ElementType aType = elem->GetType();
6219 theSm->RemoveElement(elem);
6220 meshDS->SMDS_Mesh::RemoveFreeElement(elem);
6222 const SMDS_MeshElement* NewElem = 0;
6228 NewElem = theHelper.AddEdge(aNds[0], aNds[1], id, theForce3d);
6236 NewElem = theHelper.AddFace(aNds[0], aNds[1], aNds[2], id, theForce3d);
6239 NewElem = theHelper.AddFace(aNds[0], aNds[1], aNds[2], aNds[3], id, theForce3d);
6246 case SMDSAbs_Volume :
6251 NewElem = theHelper.AddVolume(aNds[0], aNds[1], aNds[2], aNds[3], id, true);
6254 NewElem = theHelper.AddVolume(aNds[0], aNds[1], aNds[2], aNds[3], aNds[4], aNds[5], id, true);
6257 NewElem = theHelper.AddVolume(aNds[0], aNds[1], aNds[2], aNds[3],
6258 aNds[4], aNds[5], aNds[6], aNds[7], id, true);
6270 AddToSameGroups( NewElem, elem, meshDS);
6271 theSm->AddElement( NewElem );
6273 if ( NewElem != elem )
6274 RemoveElemFromGroups (elem, meshDS);
6278 //=======================================================================
6279 //function : ConvertToQuadratic
6281 //=======================================================================
6282 void SMESH_MeshEditor::ConvertToQuadratic(const bool theForce3d)
6284 SMESHDS_Mesh* meshDS = GetMeshDS();
6286 SMESH_MesherHelper aHelper(*myMesh);
6287 aHelper.SetIsQuadratic( true );
6288 const TopoDS_Shape& aShape = meshDS->ShapeToMesh();
6290 if ( !aShape.IsNull() && GetMesh()->GetSubMeshContaining(aShape) )
6292 SMESH_subMesh *aSubMesh = GetMesh()->GetSubMeshContaining(aShape);
6294 const map < int, SMESH_subMesh * >& aMapSM = aSubMesh->DependsOn();
6295 map < int, SMESH_subMesh * >::const_iterator itsub;
6296 for (itsub = aMapSM.begin(); itsub != aMapSM.end(); itsub++)
6298 SMESHDS_SubMesh *sm = ((*itsub).second)->GetSubMeshDS();
6299 aHelper.SetSubShape( (*itsub).second->GetSubShape() );
6300 ConvertElemToQuadratic(sm, aHelper, theForce3d);
6302 aHelper.SetSubShape( aSubMesh->GetSubShape() );
6303 ConvertElemToQuadratic(aSubMesh->GetSubMeshDS(), aHelper, theForce3d);
6307 SMDS_EdgeIteratorPtr aEdgeItr = meshDS->edgesIterator();
6308 while(aEdgeItr->more())
6310 const SMDS_MeshEdge* edge = aEdgeItr->next();
6311 if(edge && !edge->IsQuadratic())
6313 int id = edge->GetID();
6314 const SMDS_MeshNode* n1 = edge->GetNode(0);
6315 const SMDS_MeshNode* n2 = edge->GetNode(1);
6317 meshDS->SMDS_Mesh::RemoveFreeElement(edge);
6319 const SMDS_MeshEdge* NewEdge = aHelper.AddEdge(n1, n2, id, theForce3d);
6321 AddToSameGroups(NewEdge, edge, meshDS);
6322 if ( NewEdge != edge )
6323 RemoveElemFromGroups (edge, meshDS);
6326 SMDS_FaceIteratorPtr aFaceItr = meshDS->facesIterator();
6327 while(aFaceItr->more())
6329 const SMDS_MeshFace* face = aFaceItr->next();
6330 if(!face || face->IsQuadratic() ) continue;
6332 int id = face->GetID();
6333 int nbNodes = face->NbNodes();
6334 vector<const SMDS_MeshNode *> aNds (nbNodes);
6336 for(int i = 0; i < nbNodes; i++)
6338 aNds[i] = face->GetNode(i);
6341 meshDS->SMDS_Mesh::RemoveFreeElement(face);
6343 SMDS_MeshFace * NewFace = 0;
6347 NewFace = aHelper.AddFace(aNds[0], aNds[1], aNds[2], id, theForce3d);
6350 NewFace = aHelper.AddFace(aNds[0], aNds[1], aNds[2], aNds[3], id, theForce3d);
6356 AddToSameGroups(NewFace, face, meshDS);
6357 if ( NewFace != face )
6358 RemoveElemFromGroups (face, meshDS);
6360 SMDS_VolumeIteratorPtr aVolumeItr = meshDS->volumesIterator();
6361 while(aVolumeItr->more())
6363 const SMDS_MeshVolume* volume = aVolumeItr->next();
6364 if(!volume || volume->IsQuadratic() ) continue;
6366 int id = volume->GetID();
6367 int nbNodes = volume->NbNodes();
6368 vector<const SMDS_MeshNode *> aNds (nbNodes);
6370 for(int i = 0; i < nbNodes; i++)
6372 aNds[i] = volume->GetNode(i);
6375 meshDS->SMDS_Mesh::RemoveFreeElement(volume);
6377 SMDS_MeshVolume * NewVolume = 0;
6381 NewVolume = aHelper.AddVolume(aNds[0], aNds[1], aNds[2],
6382 aNds[3], id, true );
6385 NewVolume = aHelper.AddVolume(aNds[0], aNds[1], aNds[2],
6386 aNds[3], aNds[4], aNds[5], id, true);
6389 NewVolume = aHelper.AddVolume(aNds[0], aNds[1], aNds[2], aNds[3],
6390 aNds[4], aNds[5], aNds[6], aNds[7], id, true);
6396 AddToSameGroups(NewVolume, volume, meshDS);
6397 if ( NewVolume != volume )
6398 RemoveElemFromGroups (volume, meshDS);
6403 //=======================================================================
6404 //function : RemoveQuadElem
6406 //=======================================================================
6407 void SMESH_MeshEditor::RemoveQuadElem(SMESHDS_SubMesh * theSm,
6408 SMDS_ElemIteratorPtr theItr,
6409 const int theShapeID)
6411 SMESHDS_Mesh* meshDS = GetMeshDS();
6412 while( theItr->more() )
6414 const SMDS_MeshElement* elem = theItr->next();
6415 if( elem && elem->IsQuadratic())
6417 int id = elem->GetID();
6418 int nbNodes = elem->NbNodes();
6419 vector<const SMDS_MeshNode *> aNds, mediumNodes;
6420 aNds.reserve( nbNodes );
6421 mediumNodes.reserve( nbNodes );
6423 for(int i = 0; i < nbNodes; i++)
6425 const SMDS_MeshNode* n = elem->GetNode(i);
6427 if( elem->IsMediumNode( n ) )
6428 mediumNodes.push_back( n );
6430 aNds.push_back( n );
6432 if( aNds.empty() ) continue;
6433 SMDSAbs_ElementType aType = elem->GetType();
6435 //remove old quadratic elements
6436 meshDS->SMDS_Mesh::RemoveFreeElement( elem );
6438 theSm->RemoveElement( elem );
6440 SMDS_MeshElement * NewElem = AddElement( aNds, aType, false, id );
6442 AddToSameGroups(NewElem, elem, meshDS);
6443 if ( NewElem != elem )
6444 RemoveElemFromGroups (elem, meshDS);
6445 if( theSm && NewElem )
6446 theSm->AddElement( NewElem );
6448 // remove medium nodes
6449 vector<const SMDS_MeshNode*>::iterator nIt = mediumNodes.begin();
6450 for ( ; nIt != mediumNodes.end(); ++nIt ) {
6451 const SMDS_MeshNode* n = *nIt;
6452 if ( n->NbInverseNodes() == 0 ) {
6453 if ( n->GetPosition()->GetShapeId() != theShapeID )
6454 meshDS->RemoveFreeNode( n, meshDS->MeshElements
6455 ( n->GetPosition()->GetShapeId() ));
6457 meshDS->RemoveFreeNode( n, theSm );
6464 //=======================================================================
6465 //function : ConvertFromQuadratic
6467 //=======================================================================
6468 bool SMESH_MeshEditor::ConvertFromQuadratic()
6470 SMESHDS_Mesh* meshDS = GetMeshDS();
6471 const TopoDS_Shape& aShape = meshDS->ShapeToMesh();
6473 if ( !aShape.IsNull() && GetMesh()->GetSubMeshContaining(aShape) )
6475 SMESH_subMesh *aSubMesh = GetMesh()->GetSubMeshContaining(aShape);
6477 const map < int, SMESH_subMesh * >& aMapSM = aSubMesh->DependsOn();
6478 map < int, SMESH_subMesh * >::const_iterator itsub;
6479 for (itsub = aMapSM.begin(); itsub != aMapSM.end(); itsub++)
6481 SMESHDS_SubMesh *sm = ((*itsub).second)->GetSubMeshDS();
6483 RemoveQuadElem( sm, sm->GetElements(), itsub->second->GetId() );
6485 SMESHDS_SubMesh *Sm = aSubMesh->GetSubMeshDS();
6487 RemoveQuadElem( Sm, Sm->GetElements(), aSubMesh->GetId() );
6491 SMESHDS_SubMesh *aSM = 0;
6492 RemoveQuadElem( aSM, meshDS->elementsIterator(), 0 );
6498 //=======================================================================
6499 //function : SewSideElements
6501 //=======================================================================
6503 SMESH_MeshEditor::Sew_Error
6504 SMESH_MeshEditor::SewSideElements (TIDSortedElemSet& theSide1,
6505 TIDSortedElemSet& theSide2,
6506 const SMDS_MeshNode* theFirstNode1,
6507 const SMDS_MeshNode* theFirstNode2,
6508 const SMDS_MeshNode* theSecondNode1,
6509 const SMDS_MeshNode* theSecondNode2)
6511 myLastCreatedElems.Clear();
6512 myLastCreatedNodes.Clear();
6514 MESSAGE ("::::SewSideElements()");
6515 if ( theSide1.size() != theSide2.size() )
6516 return SEW_DIFF_NB_OF_ELEMENTS;
6518 Sew_Error aResult = SEW_OK;
6520 // 1. Build set of faces representing each side
6521 // 2. Find which nodes of the side 1 to merge with ones on the side 2
6522 // 3. Replace nodes in elements of the side 1 and remove replaced nodes
6524 // =======================================================================
6525 // 1. Build set of faces representing each side:
6526 // =======================================================================
6527 // a. build set of nodes belonging to faces
6528 // b. complete set of faces: find missing fices whose nodes are in set of nodes
6529 // c. create temporary faces representing side of volumes if correspondent
6530 // face does not exist
6532 SMESHDS_Mesh* aMesh = GetMeshDS();
6533 SMDS_Mesh aTmpFacesMesh;
6534 set<const SMDS_MeshElement*> faceSet1, faceSet2;
6535 set<const SMDS_MeshElement*> volSet1, volSet2;
6536 set<const SMDS_MeshNode*> nodeSet1, nodeSet2;
6537 set<const SMDS_MeshElement*> * faceSetPtr[] = { &faceSet1, &faceSet2 };
6538 set<const SMDS_MeshElement*> * volSetPtr[] = { &volSet1, &volSet2 };
6539 set<const SMDS_MeshNode*> * nodeSetPtr[] = { &nodeSet1, &nodeSet2 };
6540 TIDSortedElemSet * elemSetPtr[] = { &theSide1, &theSide2 };
6541 int iSide, iFace, iNode;
6543 for ( iSide = 0; iSide < 2; iSide++ ) {
6544 set<const SMDS_MeshNode*> * nodeSet = nodeSetPtr[ iSide ];
6545 TIDSortedElemSet * elemSet = elemSetPtr[ iSide ];
6546 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
6547 set<const SMDS_MeshElement*> * volSet = volSetPtr [ iSide ];
6548 set<const SMDS_MeshElement*>::iterator vIt;
6549 TIDSortedElemSet::iterator eIt;
6550 set<const SMDS_MeshNode*>::iterator nIt;
6552 // check that given nodes belong to given elements
6553 const SMDS_MeshNode* n1 = ( iSide == 0 ) ? theFirstNode1 : theFirstNode2;
6554 const SMDS_MeshNode* n2 = ( iSide == 0 ) ? theSecondNode1 : theSecondNode2;
6555 int firstIndex = -1, secondIndex = -1;
6556 for (eIt = elemSet->begin(); eIt != elemSet->end(); eIt++ ) {
6557 const SMDS_MeshElement* elem = *eIt;
6558 if ( firstIndex < 0 ) firstIndex = elem->GetNodeIndex( n1 );
6559 if ( secondIndex < 0 ) secondIndex = elem->GetNodeIndex( n2 );
6560 if ( firstIndex > -1 && secondIndex > -1 ) break;
6562 if ( firstIndex < 0 || secondIndex < 0 ) {
6563 // we can simply return until temporary faces created
6564 return (iSide == 0 ) ? SEW_BAD_SIDE1_NODES : SEW_BAD_SIDE2_NODES;
6567 // -----------------------------------------------------------
6568 // 1a. Collect nodes of existing faces
6569 // and build set of face nodes in order to detect missing
6570 // faces corresponing to sides of volumes
6571 // -----------------------------------------------------------
6573 set< set <const SMDS_MeshNode*> > setOfFaceNodeSet;
6575 // loop on the given element of a side
6576 for (eIt = elemSet->begin(); eIt != elemSet->end(); eIt++ ) {
6577 //const SMDS_MeshElement* elem = *eIt;
6578 const SMDS_MeshElement* elem = *eIt;
6579 if ( elem->GetType() == SMDSAbs_Face ) {
6580 faceSet->insert( elem );
6581 set <const SMDS_MeshNode*> faceNodeSet;
6582 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
6583 while ( nodeIt->more() ) {
6584 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6585 nodeSet->insert( n );
6586 faceNodeSet.insert( n );
6588 setOfFaceNodeSet.insert( faceNodeSet );
6590 else if ( elem->GetType() == SMDSAbs_Volume )
6591 volSet->insert( elem );
6593 // ------------------------------------------------------------------------------
6594 // 1b. Complete set of faces: find missing fices whose nodes are in set of nodes
6595 // ------------------------------------------------------------------------------
6597 for ( nIt = nodeSet->begin(); nIt != nodeSet->end(); nIt++ ) { // loop on nodes of iSide
6598 SMDS_ElemIteratorPtr fIt = (*nIt)->GetInverseElementIterator(SMDSAbs_Face);
6599 while ( fIt->more() ) { // loop on faces sharing a node
6600 const SMDS_MeshElement* f = fIt->next();
6601 if ( faceSet->find( f ) == faceSet->end() ) {
6602 // check if all nodes are in nodeSet and
6603 // complete setOfFaceNodeSet if they are
6604 set <const SMDS_MeshNode*> faceNodeSet;
6605 SMDS_ElemIteratorPtr nodeIt = f->nodesIterator();
6606 bool allInSet = true;
6607 while ( nodeIt->more() && allInSet ) { // loop on nodes of a face
6608 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6609 if ( nodeSet->find( n ) == nodeSet->end() )
6612 faceNodeSet.insert( n );
6615 faceSet->insert( f );
6616 setOfFaceNodeSet.insert( faceNodeSet );
6622 // -------------------------------------------------------------------------
6623 // 1c. Create temporary faces representing sides of volumes if correspondent
6624 // face does not exist
6625 // -------------------------------------------------------------------------
6627 if ( !volSet->empty() ) {
6628 //int nodeSetSize = nodeSet->size();
6630 // loop on given volumes
6631 for ( vIt = volSet->begin(); vIt != volSet->end(); vIt++ ) {
6632 SMDS_VolumeTool vol (*vIt);
6633 // loop on volume faces: find free faces
6634 // --------------------------------------
6635 list<const SMDS_MeshElement* > freeFaceList;
6636 for ( iFace = 0; iFace < vol.NbFaces(); iFace++ ) {
6637 if ( !vol.IsFreeFace( iFace ))
6639 // check if there is already a face with same nodes in a face set
6640 const SMDS_MeshElement* aFreeFace = 0;
6641 const SMDS_MeshNode** fNodes = vol.GetFaceNodes( iFace );
6642 int nbNodes = vol.NbFaceNodes( iFace );
6643 set <const SMDS_MeshNode*> faceNodeSet;
6644 vol.GetFaceNodes( iFace, faceNodeSet );
6645 bool isNewFace = setOfFaceNodeSet.insert( faceNodeSet ).second;
6647 // no such a face is given but it still can exist, check it
6648 if ( nbNodes == 3 ) {
6649 aFreeFace = aMesh->FindFace( fNodes[0],fNodes[1],fNodes[2] );
6651 else if ( nbNodes == 4 ) {
6652 aFreeFace = aMesh->FindFace( fNodes[0],fNodes[1],fNodes[2],fNodes[3] );
6655 vector<const SMDS_MeshNode *> poly_nodes ( fNodes, & fNodes[nbNodes]);
6656 aFreeFace = aMesh->FindFace(poly_nodes);
6660 // create a temporary face
6661 if ( nbNodes == 3 ) {
6662 aFreeFace = aTmpFacesMesh.AddFace( fNodes[0],fNodes[1],fNodes[2] );
6664 else if ( nbNodes == 4 ) {
6665 aFreeFace = aTmpFacesMesh.AddFace( fNodes[0],fNodes[1],fNodes[2],fNodes[3] );
6668 vector<const SMDS_MeshNode *> poly_nodes ( fNodes, & fNodes[nbNodes]);
6669 aFreeFace = aTmpFacesMesh.AddPolygonalFace(poly_nodes);
6673 freeFaceList.push_back( aFreeFace );
6675 } // loop on faces of a volume
6677 // choose one of several free faces
6678 // --------------------------------------
6679 if ( freeFaceList.size() > 1 ) {
6680 // choose a face having max nb of nodes shared by other elems of a side
6681 int maxNbNodes = -1/*, nbExcludedFaces = 0*/;
6682 list<const SMDS_MeshElement* >::iterator fIt = freeFaceList.begin();
6683 while ( fIt != freeFaceList.end() ) { // loop on free faces
6684 int nbSharedNodes = 0;
6685 SMDS_ElemIteratorPtr nodeIt = (*fIt)->nodesIterator();
6686 while ( nodeIt->more() ) { // loop on free face nodes
6687 const SMDS_MeshNode* n =
6688 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6689 SMDS_ElemIteratorPtr invElemIt = n->GetInverseElementIterator();
6690 while ( invElemIt->more() ) {
6691 const SMDS_MeshElement* e = invElemIt->next();
6692 if ( faceSet->find( e ) != faceSet->end() )
6694 if ( elemSet->find( e ) != elemSet->end() )
6698 if ( nbSharedNodes >= maxNbNodes ) {
6699 maxNbNodes = nbSharedNodes;
6703 freeFaceList.erase( fIt++ ); // here fIt++ occures before erase
6705 if ( freeFaceList.size() > 1 )
6707 // could not choose one face, use another way
6708 // choose a face most close to the bary center of the opposite side
6709 gp_XYZ aBC( 0., 0., 0. );
6710 set <const SMDS_MeshNode*> addedNodes;
6711 TIDSortedElemSet * elemSet2 = elemSetPtr[ 1 - iSide ];
6712 eIt = elemSet2->begin();
6713 for ( eIt = elemSet2->begin(); eIt != elemSet2->end(); eIt++ ) {
6714 SMDS_ElemIteratorPtr nodeIt = (*eIt)->nodesIterator();
6715 while ( nodeIt->more() ) { // loop on free face nodes
6716 const SMDS_MeshNode* n =
6717 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6718 if ( addedNodes.insert( n ).second )
6719 aBC += gp_XYZ( n->X(),n->Y(),n->Z() );
6722 aBC /= addedNodes.size();
6723 double minDist = DBL_MAX;
6724 fIt = freeFaceList.begin();
6725 while ( fIt != freeFaceList.end() ) { // loop on free faces
6727 SMDS_ElemIteratorPtr nodeIt = (*fIt)->nodesIterator();
6728 while ( nodeIt->more() ) { // loop on free face nodes
6729 const SMDS_MeshNode* n =
6730 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6731 gp_XYZ p( n->X(),n->Y(),n->Z() );
6732 dist += ( aBC - p ).SquareModulus();
6734 if ( dist < minDist ) {
6736 freeFaceList.erase( freeFaceList.begin(), fIt++ );
6739 fIt = freeFaceList.erase( fIt++ );
6742 } // choose one of several free faces of a volume
6744 if ( freeFaceList.size() == 1 ) {
6745 const SMDS_MeshElement* aFreeFace = freeFaceList.front();
6746 faceSet->insert( aFreeFace );
6747 // complete a node set with nodes of a found free face
6748 // for ( iNode = 0; iNode < ; iNode++ )
6749 // nodeSet->insert( fNodes[ iNode ] );
6752 } // loop on volumes of a side
6754 // // complete a set of faces if new nodes in a nodeSet appeared
6755 // // ----------------------------------------------------------
6756 // if ( nodeSetSize != nodeSet->size() ) {
6757 // for ( ; nIt != nodeSet->end(); nIt++ ) { // loop on nodes of iSide
6758 // SMDS_ElemIteratorPtr fIt = (*nIt)->GetInverseElementIterator(SMDSAbs_Face);
6759 // while ( fIt->more() ) { // loop on faces sharing a node
6760 // const SMDS_MeshElement* f = fIt->next();
6761 // if ( faceSet->find( f ) == faceSet->end() ) {
6762 // // check if all nodes are in nodeSet and
6763 // // complete setOfFaceNodeSet if they are
6764 // set <const SMDS_MeshNode*> faceNodeSet;
6765 // SMDS_ElemIteratorPtr nodeIt = f->nodesIterator();
6766 // bool allInSet = true;
6767 // while ( nodeIt->more() && allInSet ) { // loop on nodes of a face
6768 // const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6769 // if ( nodeSet->find( n ) == nodeSet->end() )
6770 // allInSet = false;
6772 // faceNodeSet.insert( n );
6774 // if ( allInSet ) {
6775 // faceSet->insert( f );
6776 // setOfFaceNodeSet.insert( faceNodeSet );
6782 } // Create temporary faces, if there are volumes given
6785 if ( faceSet1.size() != faceSet2.size() ) {
6786 // delete temporary faces: they are in reverseElements of actual nodes
6787 SMDS_FaceIteratorPtr tmpFaceIt = aTmpFacesMesh.facesIterator();
6788 while ( tmpFaceIt->more() )
6789 aTmpFacesMesh.RemoveElement( tmpFaceIt->next() );
6790 MESSAGE("Diff nb of faces");
6791 return SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
6794 // ============================================================
6795 // 2. Find nodes to merge:
6796 // bind a node to remove to a node to put instead
6797 // ============================================================
6799 TNodeNodeMap nReplaceMap; // bind a node to remove to a node to put instead
6800 if ( theFirstNode1 != theFirstNode2 )
6801 nReplaceMap.insert( TNodeNodeMap::value_type( theFirstNode1, theFirstNode2 ));
6802 if ( theSecondNode1 != theSecondNode2 )
6803 nReplaceMap.insert( TNodeNodeMap::value_type( theSecondNode1, theSecondNode2 ));
6805 LinkID_Gen aLinkID_Gen( GetMeshDS() );
6806 set< long > linkIdSet; // links to process
6807 linkIdSet.insert( aLinkID_Gen.GetLinkID( theFirstNode1, theSecondNode1 ));
6809 typedef pair< const SMDS_MeshNode*, const SMDS_MeshNode* > NLink;
6810 list< NLink > linkList[2];
6811 linkList[0].push_back( NLink( theFirstNode1, theSecondNode1 ));
6812 linkList[1].push_back( NLink( theFirstNode2, theSecondNode2 ));
6813 // loop on links in linkList; find faces by links and append links
6814 // of the found faces to linkList
6815 list< NLink >::iterator linkIt[] = { linkList[0].begin(), linkList[1].begin() } ;
6816 for ( ; linkIt[0] != linkList[0].end(); linkIt[0]++, linkIt[1]++ ) {
6817 NLink link[] = { *linkIt[0], *linkIt[1] };
6818 long linkID = aLinkID_Gen.GetLinkID( link[0].first, link[0].second );
6819 if ( linkIdSet.find( linkID ) == linkIdSet.end() )
6822 // by links, find faces in the face sets,
6823 // and find indices of link nodes in the found faces;
6824 // in a face set, there is only one or no face sharing a link
6825 // ---------------------------------------------------------------
6827 const SMDS_MeshElement* face[] = { 0, 0 };
6828 //const SMDS_MeshNode* faceNodes[ 2 ][ 5 ];
6829 vector<const SMDS_MeshNode*> fnodes1(9);
6830 vector<const SMDS_MeshNode*> fnodes2(9);
6831 //const SMDS_MeshNode* notLinkNodes[ 2 ][ 2 ] = {{ 0, 0 },{ 0, 0 }} ;
6832 vector<const SMDS_MeshNode*> notLinkNodes1(6);
6833 vector<const SMDS_MeshNode*> notLinkNodes2(6);
6834 int iLinkNode[2][2];
6835 for ( iSide = 0; iSide < 2; iSide++ ) { // loop on 2 sides
6836 const SMDS_MeshNode* n1 = link[iSide].first;
6837 const SMDS_MeshNode* n2 = link[iSide].second;
6838 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
6839 set< const SMDS_MeshElement* > fMap;
6840 for ( int i = 0; i < 2; i++ ) { // loop on 2 nodes of a link
6841 const SMDS_MeshNode* n = i ? n1 : n2; // a node of a link
6842 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
6843 while ( fIt->more() ) { // loop on faces sharing a node
6844 const SMDS_MeshElement* f = fIt->next();
6845 if (faceSet->find( f ) != faceSet->end() && // f is in face set
6846 ! fMap.insert( f ).second ) // f encounters twice
6848 if ( face[ iSide ] ) {
6849 MESSAGE( "2 faces per link " );
6850 aResult = iSide ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES;
6854 faceSet->erase( f );
6855 // get face nodes and find ones of a link
6860 fnodes1.resize(f->NbNodes()+1);
6861 notLinkNodes1.resize(f->NbNodes()-2);
6864 fnodes2.resize(f->NbNodes()+1);
6865 notLinkNodes2.resize(f->NbNodes()-2);
6868 if(!f->IsQuadratic()) {
6869 SMDS_ElemIteratorPtr nIt = f->nodesIterator();
6870 while ( nIt->more() ) {
6871 const SMDS_MeshNode* n =
6872 static_cast<const SMDS_MeshNode*>( nIt->next() );
6874 iLinkNode[ iSide ][ 0 ] = iNode;
6876 else if ( n == n2 ) {
6877 iLinkNode[ iSide ][ 1 ] = iNode;
6879 //else if ( notLinkNodes[ iSide ][ 0 ] )
6880 // notLinkNodes[ iSide ][ 1 ] = n;
6882 // notLinkNodes[ iSide ][ 0 ] = n;
6886 notLinkNodes1[nbl] = n;
6887 //notLinkNodes1.push_back(n);
6889 notLinkNodes2[nbl] = n;
6890 //notLinkNodes2.push_back(n);
6892 //faceNodes[ iSide ][ iNode++ ] = n;
6894 fnodes1[iNode++] = n;
6897 fnodes2[iNode++] = n;
6901 else { // f->IsQuadratic()
6902 const SMDS_QuadraticFaceOfNodes* F =
6903 static_cast<const SMDS_QuadraticFaceOfNodes*>(f);
6904 // use special nodes iterator
6905 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
6906 while ( anIter->more() ) {
6907 const SMDS_MeshNode* n =
6908 static_cast<const SMDS_MeshNode*>( anIter->next() );
6910 iLinkNode[ iSide ][ 0 ] = iNode;
6912 else if ( n == n2 ) {
6913 iLinkNode[ iSide ][ 1 ] = iNode;
6918 notLinkNodes1[nbl] = n;
6921 notLinkNodes2[nbl] = n;
6925 fnodes1[iNode++] = n;
6928 fnodes2[iNode++] = n;
6932 //faceNodes[ iSide ][ iNode ] = faceNodes[ iSide ][ 0 ];
6934 fnodes1[iNode] = fnodes1[0];
6937 fnodes2[iNode] = fnodes1[0];
6944 // check similarity of elements of the sides
6945 if (aResult == SEW_OK && ( face[0] && !face[1] ) || ( !face[0] && face[1] )) {
6946 MESSAGE("Correspondent face not found on side " << ( face[0] ? 1 : 0 ));
6947 if ( nReplaceMap.size() == 2 ) { // faces on input nodes not found
6948 aResult = ( face[0] ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES );
6951 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
6953 break; // do not return because it s necessary to remove tmp faces
6956 // set nodes to merge
6957 // -------------------
6959 if ( face[0] && face[1] ) {
6960 int nbNodes = face[0]->NbNodes();
6961 if ( nbNodes != face[1]->NbNodes() ) {
6962 MESSAGE("Diff nb of face nodes");
6963 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
6964 break; // do not return because it s necessary to remove tmp faces
6966 bool reverse[] = { false, false }; // order of notLinkNodes of quadrangle
6967 if ( nbNodes == 3 ) {
6968 //nReplaceMap.insert( TNodeNodeMap::value_type
6969 // ( notLinkNodes[0][0], notLinkNodes[1][0] ));
6970 nReplaceMap.insert( TNodeNodeMap::value_type
6971 ( notLinkNodes1[0], notLinkNodes2[0] ));
6974 for ( iSide = 0; iSide < 2; iSide++ ) { // loop on 2 sides
6975 // analyse link orientation in faces
6976 int i1 = iLinkNode[ iSide ][ 0 ];
6977 int i2 = iLinkNode[ iSide ][ 1 ];
6978 reverse[ iSide ] = Abs( i1 - i2 ) == 1 ? i1 > i2 : i2 > i1;
6979 // if notLinkNodes are the first and the last ones, then
6980 // their order does not correspond to the link orientation
6981 if (( i1 == 1 && i2 == 2 ) ||
6982 ( i1 == 2 && i2 == 1 ))
6983 reverse[ iSide ] = !reverse[ iSide ];
6985 if ( reverse[0] == reverse[1] ) {
6986 //nReplaceMap.insert( TNodeNodeMap::value_type
6987 // ( notLinkNodes[0][0], notLinkNodes[1][0] ));
6988 //nReplaceMap.insert( TNodeNodeMap::value_type
6989 // ( notLinkNodes[0][1], notLinkNodes[1][1] ));
6990 for(int nn=0; nn<nbNodes-2; nn++) {
6991 nReplaceMap.insert( TNodeNodeMap::value_type
6992 ( notLinkNodes1[nn], notLinkNodes2[nn] ));
6996 //nReplaceMap.insert( TNodeNodeMap::value_type
6997 // ( notLinkNodes[0][0], notLinkNodes[1][1] ));
6998 //nReplaceMap.insert( TNodeNodeMap::value_type
6999 // ( notLinkNodes[0][1], notLinkNodes[1][0] ));
7000 for(int nn=0; nn<nbNodes-2; nn++) {
7001 nReplaceMap.insert( TNodeNodeMap::value_type
7002 ( notLinkNodes1[nn], notLinkNodes2[nbNodes-3-nn] ));
7007 // add other links of the faces to linkList
7008 // -----------------------------------------
7010 //const SMDS_MeshNode** nodes = faceNodes[ 0 ];
7011 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
7012 //linkID = aLinkID_Gen.GetLinkID( nodes[iNode], nodes[iNode+1] );
7013 linkID = aLinkID_Gen.GetLinkID( fnodes1[iNode], fnodes1[iNode+1] );
7014 pair< set<long>::iterator, bool > iter_isnew = linkIdSet.insert( linkID );
7015 if ( !iter_isnew.second ) { // already in a set: no need to process
7016 linkIdSet.erase( iter_isnew.first );
7018 else // new in set == encountered for the first time: add
7020 //const SMDS_MeshNode* n1 = nodes[ iNode ];
7021 //const SMDS_MeshNode* n2 = nodes[ iNode + 1];
7022 const SMDS_MeshNode* n1 = fnodes1[ iNode ];
7023 const SMDS_MeshNode* n2 = fnodes1[ iNode + 1];
7024 linkList[0].push_back ( NLink( n1, n2 ));
7025 linkList[1].push_back ( NLink( nReplaceMap[n1], nReplaceMap[n2] ));
7029 } // loop on link lists
7031 if ( aResult == SEW_OK &&
7032 ( linkIt[0] != linkList[0].end() ||
7033 !faceSetPtr[0]->empty() || !faceSetPtr[1]->empty() )) {
7034 MESSAGE( (linkIt[0] != linkList[0].end()) <<" "<< (faceSetPtr[0]->empty()) <<
7035 " " << (faceSetPtr[1]->empty()));
7036 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
7039 // ====================================================================
7040 // 3. Replace nodes in elements of the side 1 and remove replaced nodes
7041 // ====================================================================
7043 // delete temporary faces: they are in reverseElements of actual nodes
7044 SMDS_FaceIteratorPtr tmpFaceIt = aTmpFacesMesh.facesIterator();
7045 while ( tmpFaceIt->more() )
7046 aTmpFacesMesh.RemoveElement( tmpFaceIt->next() );
7048 if ( aResult != SEW_OK)
7051 list< int > nodeIDsToRemove/*, elemIDsToRemove*/;
7052 // loop on nodes replacement map
7053 TNodeNodeMap::iterator nReplaceMapIt = nReplaceMap.begin(), nnIt;
7054 for ( ; nReplaceMapIt != nReplaceMap.end(); nReplaceMapIt++ )
7055 if ( (*nReplaceMapIt).first != (*nReplaceMapIt).second ) {
7056 const SMDS_MeshNode* nToRemove = (*nReplaceMapIt).first;
7057 nodeIDsToRemove.push_back( nToRemove->GetID() );
7058 // loop on elements sharing nToRemove
7059 SMDS_ElemIteratorPtr invElemIt = nToRemove->GetInverseElementIterator();
7060 while ( invElemIt->more() ) {
7061 const SMDS_MeshElement* e = invElemIt->next();
7062 // get a new suite of nodes: make replacement
7063 int nbReplaced = 0, i = 0, nbNodes = e->NbNodes();
7064 vector< const SMDS_MeshNode*> nodes( nbNodes );
7065 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
7066 while ( nIt->more() ) {
7067 const SMDS_MeshNode* n =
7068 static_cast<const SMDS_MeshNode*>( nIt->next() );
7069 nnIt = nReplaceMap.find( n );
7070 if ( nnIt != nReplaceMap.end() ) {
7076 // if ( nbReplaced == nbNodes && e->GetType() == SMDSAbs_Face )
7077 // elemIDsToRemove.push_back( e->GetID() );
7080 aMesh->ChangeElementNodes( e, & nodes[0], nbNodes );
7084 Remove( nodeIDsToRemove, true );
7090 * \brief A sorted pair of nodes
7092 struct TLink: public NLink
7094 TLink(const SMDS_MeshNode* n1, const SMDS_MeshNode* n2 ):NLink( n1, n2 )
7095 { if ( n1 < n2 ) std::swap( first, second ); }
7096 TLink(const NLink& link ):NLink( link )
7097 { if ( first < second ) std::swap( first, second ); }
7100 //================================================================================
7102 * \brief Find corresponding nodes in two sets of faces
7103 * \param theSide1 - first face set
7104 * \param theSide2 - second first face
7105 * \param theFirstNode1 - a boundary node of set 1
7106 * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
7107 * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
7108 * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
7109 * \param nReplaceMap - output map of corresponding nodes
7110 * \retval bool - is a success or not
7112 //================================================================================
7114 //#define DEBUG_MATCHING_NODES
7116 SMESH_MeshEditor::Sew_Error
7117 SMESH_MeshEditor::FindMatchingNodes(set<const SMDS_MeshElement*>& theSide1,
7118 set<const SMDS_MeshElement*>& theSide2,
7119 const SMDS_MeshNode* theFirstNode1,
7120 const SMDS_MeshNode* theFirstNode2,
7121 const SMDS_MeshNode* theSecondNode1,
7122 const SMDS_MeshNode* theSecondNode2,
7123 TNodeNodeMap & nReplaceMap)
7125 set<const SMDS_MeshElement*> * faceSetPtr[] = { &theSide1, &theSide2 };
7127 nReplaceMap.clear();
7128 if ( theFirstNode1 != theFirstNode2 )
7129 nReplaceMap.insert( make_pair( theFirstNode1, theFirstNode2 ));
7130 if ( theSecondNode1 != theSecondNode2 )
7131 nReplaceMap.insert( make_pair( theSecondNode1, theSecondNode2 ));
7133 set< TLink > linkSet; // set of nodes where order of nodes is ignored
7134 linkSet.insert( TLink( theFirstNode1, theSecondNode1 ));
7136 list< NLink > linkList[2];
7137 linkList[0].push_back( NLink( theFirstNode1, theSecondNode1 ));
7138 linkList[1].push_back( NLink( theFirstNode2, theSecondNode2 ));
7140 // loop on links in linkList; find faces by links and append links
7141 // of the found faces to linkList
7142 list< NLink >::iterator linkIt[] = { linkList[0].begin(), linkList[1].begin() } ;
7143 for ( ; linkIt[0] != linkList[0].end(); linkIt[0]++, linkIt[1]++ ) {
7144 NLink link[] = { *linkIt[0], *linkIt[1] };
7145 if ( linkSet.find( link[0] ) == linkSet.end() )
7148 // by links, find faces in the face sets,
7149 // and find indices of link nodes in the found faces;
7150 // in a face set, there is only one or no face sharing a link
7151 // ---------------------------------------------------------------
7153 const SMDS_MeshElement* face[] = { 0, 0 };
7154 list<const SMDS_MeshNode*> notLinkNodes[2];
7155 //bool reverse[] = { false, false }; // order of notLinkNodes
7157 for ( int iSide = 0; iSide < 2; iSide++ ) // loop on 2 sides
7159 const SMDS_MeshNode* n1 = link[iSide].first;
7160 const SMDS_MeshNode* n2 = link[iSide].second;
7161 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
7162 set< const SMDS_MeshElement* > facesOfNode1;
7163 for ( int iNode = 0; iNode < 2; iNode++ ) // loop on 2 nodes of a link
7165 // during a loop of the first node, we find all faces around n1,
7166 // during a loop of the second node, we find one face sharing both n1 and n2
7167 const SMDS_MeshNode* n = iNode ? n1 : n2; // a node of a link
7168 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
7169 while ( fIt->more() ) { // loop on faces sharing a node
7170 const SMDS_MeshElement* f = fIt->next();
7171 if (faceSet->find( f ) != faceSet->end() && // f is in face set
7172 ! facesOfNode1.insert( f ).second ) // f encounters twice
7174 if ( face[ iSide ] ) {
7175 MESSAGE( "2 faces per link " );
7176 return ( iSide ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES );
7179 faceSet->erase( f );
7181 // get not link nodes
7182 int nbN = f->NbNodes();
7183 if ( f->IsQuadratic() )
7185 nbNodes[ iSide ] = nbN;
7186 list< const SMDS_MeshNode* > & nodes = notLinkNodes[ iSide ];
7187 int i1 = f->GetNodeIndex( n1 );
7188 int i2 = f->GetNodeIndex( n2 );
7189 int iEnd = nbN, iBeg = -1, iDelta = 1;
7190 bool reverse = ( Abs( i1 - i2 ) == 1 ? i1 > i2 : i2 > i1 );
7192 std::swap( iEnd, iBeg ); iDelta = -1;
7197 if ( i == iEnd ) i = iBeg + iDelta;
7198 if ( i == i1 ) break;
7199 nodes.push_back ( f->GetNode( i ) );
7205 // check similarity of elements of the sides
7206 if (( face[0] && !face[1] ) || ( !face[0] && face[1] )) {
7207 MESSAGE("Correspondent face not found on side " << ( face[0] ? 1 : 0 ));
7208 if ( nReplaceMap.size() == 2 ) { // faces on input nodes not found
7209 return ( face[0] ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES );
7212 return SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
7216 // set nodes to merge
7217 // -------------------
7219 if ( face[0] && face[1] ) {
7220 if ( nbNodes[0] != nbNodes[1] ) {
7221 MESSAGE("Diff nb of face nodes");
7222 return SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
7224 #ifdef DEBUG_MATCHING_NODES
7225 cout << " Link 1: " << link[0].first->GetID() <<" "<< link[0].second->GetID()
7226 << " F 1: " << face[0];
7227 cout << "| Link 2: " << link[1].first->GetID() <<" "<< link[1].second->GetID()
7228 << " F 2: " << face[1] << " | Bind: "<<endl ;
7230 int nbN = nbNodes[0];
7232 list<const SMDS_MeshNode*>::iterator n1 = notLinkNodes[0].begin();
7233 list<const SMDS_MeshNode*>::iterator n2 = notLinkNodes[1].begin();
7234 for ( int i = 0 ; i < nbN - 2; ++i ) {
7235 #ifdef DEBUG_MATCHING_NODES
7236 cout << (*n1)->GetID() << " to " << (*n2)->GetID() << endl;
7238 nReplaceMap.insert( make_pair( *(n1++), *(n2++) ));
7242 // add other links of the face 1 to linkList
7243 // -----------------------------------------
7245 const SMDS_MeshElement* f0 = face[0];
7246 const SMDS_MeshNode* n1 = f0->GetNode( nbN - 1 );
7247 for ( int i = 0; i < nbN; i++ )
7249 const SMDS_MeshNode* n2 = f0->GetNode( i );
7250 pair< set< TLink >::iterator, bool > iter_isnew =
7251 linkSet.insert( TLink( n1, n2 ));
7252 if ( !iter_isnew.second ) { // already in a set: no need to process
7253 linkSet.erase( iter_isnew.first );
7255 else // new in set == encountered for the first time: add
7257 #ifdef DEBUG_MATCHING_NODES
7258 cout << "Add link 1: " << n1->GetID() << " " << n2->GetID() << " ";
7259 cout << " | link 2: " << nReplaceMap[n1]->GetID() << " " << nReplaceMap[n2]->GetID() << " " << endl;
7261 linkList[0].push_back ( NLink( n1, n2 ));
7262 linkList[1].push_back ( NLink( nReplaceMap[n1], nReplaceMap[n2] ));
7267 } // loop on link lists