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>
75 #define cast2Node(elem) static_cast<const SMDS_MeshNode*>( elem )
78 using namespace SMESH::Controls;
80 typedef map<const SMDS_MeshElement*, list<const SMDS_MeshNode*> > TElemOfNodeListMap;
81 typedef map<const SMDS_MeshElement*, list<const SMDS_MeshElement*> > TElemOfElemListMap;
82 typedef map<const SMDS_MeshNode*, list<const SMDS_MeshNode*> > TNodeOfNodeListMap;
83 typedef TNodeOfNodeListMap::iterator TNodeOfNodeListMapItr;
84 //typedef map<const SMDS_MeshNode*, vector<const SMDS_MeshNode*> > TNodeOfNodeVecMap;
85 //typedef TNodeOfNodeVecMap::iterator TNodeOfNodeVecMapItr;
86 typedef map<const SMDS_MeshElement*, vector<TNodeOfNodeListMapItr> > TElemOfVecOfNnlmiMap;
87 //typedef map<const SMDS_MeshElement*, vector<TNodeOfNodeVecMapItr> > TElemOfVecOfMapNodesMap;
89 struct TNodeXYZ : public gp_XYZ {
90 TNodeXYZ( const SMDS_MeshNode* n ):gp_XYZ( n->X(), n->Y(), n->Z() ) {}
93 typedef pair< const SMDS_MeshNode*, const SMDS_MeshNode* > NLink;
96 * \brief A sorted pair of nodes
98 struct TLink: public NLink
100 TLink(const SMDS_MeshNode* n1, const SMDS_MeshNode* n2 ):NLink( n1, n2 )
101 { if ( n1->GetID() < n2->GetID() ) std::swap( first, second ); }
102 TLink(const NLink& link ):NLink( link )
103 { if ( first->GetID() < second->GetID() ) std::swap( first, second ); }
106 //=======================================================================
107 //function : SMESH_MeshEditor
109 //=======================================================================
111 SMESH_MeshEditor::SMESH_MeshEditor( SMESH_Mesh* theMesh ):
116 //=======================================================================
120 //=======================================================================
123 SMESH_MeshEditor::AddElement(const vector<const SMDS_MeshNode*> & node,
124 const SMDSAbs_ElementType type,
128 SMDS_MeshElement* e = 0;
129 int nbnode = node.size();
130 SMESHDS_Mesh* mesh = GetMeshDS();
134 if ( ID ) e = mesh->AddEdgeWithID(node[0], node[1], ID);
135 else e = mesh->AddEdge (node[0], node[1] );
136 else if ( nbnode == 3 )
137 if ( ID ) e = mesh->AddEdgeWithID(node[0], node[1], node[2], ID);
138 else e = mesh->AddEdge (node[0], node[1], node[2] );
143 if ( ID ) e = mesh->AddFaceWithID(node[0], node[1], node[2], ID);
144 else e = mesh->AddFace (node[0], node[1], node[2] );
145 else if (nbnode == 4)
146 if ( ID ) e = mesh->AddFaceWithID(node[0], node[1], node[2], node[3], ID);
147 else e = mesh->AddFace (node[0], node[1], node[2], node[3] );
148 else if (nbnode == 6)
149 if ( ID ) e = mesh->AddFaceWithID(node[0], node[1], node[2], node[3],
150 node[4], node[5], ID);
151 else e = mesh->AddFace (node[0], node[1], node[2], node[3],
153 else if (nbnode == 8)
154 if ( ID ) e = mesh->AddFaceWithID(node[0], node[1], node[2], node[3],
155 node[4], node[5], node[6], node[7], ID);
156 else e = mesh->AddFace (node[0], node[1], node[2], node[3],
157 node[4], node[5], node[6], node[7] );
159 if ( ID ) e = mesh->AddPolygonalFaceWithID(node, ID);
160 else e = mesh->AddPolygonalFace (node );
166 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3], ID);
167 else e = mesh->AddVolume (node[0], node[1], node[2], node[3] );
168 else if (nbnode == 5)
169 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
171 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
173 else if (nbnode == 6)
174 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
175 node[4], node[5], ID);
176 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
178 else if (nbnode == 8)
179 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
180 node[4], node[5], node[6], node[7], ID);
181 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
182 node[4], node[5], node[6], node[7] );
183 else if (nbnode == 10)
184 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
185 node[4], node[5], node[6], node[7],
186 node[8], node[9], ID);
187 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
188 node[4], node[5], node[6], node[7],
190 else if (nbnode == 13)
191 if ( ID ) e = mesh->AddVolumeWithID(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],
195 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
196 node[4], node[5], node[6], node[7],
197 node[8], node[9], node[10],node[11],
199 else if (nbnode == 15)
200 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
201 node[4], node[5], node[6], node[7],
202 node[8], node[9], node[10],node[11],
203 node[12],node[13],node[14],ID);
204 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
205 node[4], node[5], node[6], node[7],
206 node[8], node[9], node[10],node[11],
207 node[12],node[13],node[14] );
208 else if (nbnode == 20)
209 if ( ID ) e = mesh->AddVolumeWithID(node[0], node[1], node[2], node[3],
210 node[4], node[5], node[6], node[7],
211 node[8], node[9], node[10],node[11],
212 node[12],node[13],node[14],node[15],
213 node[16],node[17],node[18],node[19],ID);
214 else e = mesh->AddVolume (node[0], node[1], node[2], node[3],
215 node[4], node[5], node[6], node[7],
216 node[8], node[9], node[10],node[11],
217 node[12],node[13],node[14],node[15],
218 node[16],node[17],node[18],node[19] );
224 //=======================================================================
228 //=======================================================================
230 SMDS_MeshElement* SMESH_MeshEditor::AddElement(const vector<int> & nodeIDs,
231 const SMDSAbs_ElementType type,
235 vector<const SMDS_MeshNode*> nodes;
236 nodes.reserve( nodeIDs.size() );
237 vector<int>::const_iterator id = nodeIDs.begin();
238 while ( id != nodeIDs.end() ) {
239 if ( const SMDS_MeshNode* node = GetMeshDS()->FindNode( *id++ ))
240 nodes.push_back( node );
244 return AddElement( nodes, type, isPoly, ID );
247 //=======================================================================
249 //purpose : Remove a node or an element.
250 // Modify a compute state of sub-meshes which become empty
251 //=======================================================================
253 bool SMESH_MeshEditor::Remove (const list< int >& theIDs,
256 myLastCreatedElems.Clear();
257 myLastCreatedNodes.Clear();
259 SMESHDS_Mesh* aMesh = GetMeshDS();
260 set< SMESH_subMesh *> smmap;
262 list<int>::const_iterator it = theIDs.begin();
263 for ( ; it != theIDs.end(); it++ ) {
264 const SMDS_MeshElement * elem;
266 elem = aMesh->FindNode( *it );
268 elem = aMesh->FindElement( *it );
272 // Find sub-meshes to notify about modification
273 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
274 while ( nodeIt->more() ) {
275 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
276 const SMDS_PositionPtr& aPosition = node->GetPosition();
277 if ( aPosition.get() ) {
278 if ( int aShapeID = aPosition->GetShapeId() ) {
279 if ( SMESH_subMesh * sm = GetMesh()->GetSubMeshContaining( aShapeID ) )
287 aMesh->RemoveNode( static_cast< const SMDS_MeshNode* >( elem ));
289 aMesh->RemoveElement( elem );
292 // Notify sub-meshes about modification
293 if ( !smmap.empty() ) {
294 set< SMESH_subMesh *>::iterator smIt;
295 for ( smIt = smmap.begin(); smIt != smmap.end(); smIt++ )
296 (*smIt)->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
299 // Check if the whole mesh becomes empty
300 if ( SMESH_subMesh * sm = GetMesh()->GetSubMeshContaining( 1 ) )
301 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
306 //=======================================================================
307 //function : FindShape
308 //purpose : Return an index of the shape theElem is on
309 // or zero if a shape not found
310 //=======================================================================
312 int SMESH_MeshEditor::FindShape (const SMDS_MeshElement * theElem)
314 myLastCreatedElems.Clear();
315 myLastCreatedNodes.Clear();
317 SMESHDS_Mesh * aMesh = GetMeshDS();
318 if ( aMesh->ShapeToMesh().IsNull() )
321 if ( theElem->GetType() == SMDSAbs_Node ) {
322 const SMDS_PositionPtr& aPosition =
323 static_cast<const SMDS_MeshNode*>( theElem )->GetPosition();
324 if ( aPosition.get() )
325 return aPosition->GetShapeId();
330 TopoDS_Shape aShape; // the shape a node is on
331 SMDS_ElemIteratorPtr nodeIt = theElem->nodesIterator();
332 while ( nodeIt->more() ) {
333 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
334 const SMDS_PositionPtr& aPosition = node->GetPosition();
335 if ( aPosition.get() ) {
336 int aShapeID = aPosition->GetShapeId();
337 SMESHDS_SubMesh * sm = aMesh->MeshElements( aShapeID );
339 if ( sm->Contains( theElem ))
341 if ( aShape.IsNull() )
342 aShape = aMesh->IndexToShape( aShapeID );
345 //MESSAGE ( "::FindShape() No SubShape for aShapeID " << aShapeID );
350 // None of nodes is on a proper shape,
351 // find the shape among ancestors of aShape on which a node is
352 if ( aShape.IsNull() ) {
353 //MESSAGE ("::FindShape() - NONE node is on shape")
356 TopTools_ListIteratorOfListOfShape ancIt( GetMesh()->GetAncestors( aShape ));
357 for ( ; ancIt.More(); ancIt.Next() ) {
358 SMESHDS_SubMesh * sm = aMesh->MeshElements( ancIt.Value() );
359 if ( sm && sm->Contains( theElem ))
360 return aMesh->ShapeToIndex( ancIt.Value() );
363 //MESSAGE ("::FindShape() - SHAPE NOT FOUND")
367 //=======================================================================
368 //function : IsMedium
370 //=======================================================================
372 bool SMESH_MeshEditor::IsMedium(const SMDS_MeshNode* node,
373 const SMDSAbs_ElementType typeToCheck)
375 bool isMedium = false;
376 SMDS_ElemIteratorPtr it = node->GetInverseElementIterator(typeToCheck);
377 while (it->more() && !isMedium ) {
378 const SMDS_MeshElement* elem = it->next();
379 isMedium = elem->IsMediumNode(node);
384 //=======================================================================
385 //function : ShiftNodesQuadTria
387 // Shift nodes in the array corresponded to quadratic triangle
388 // example: (0,1,2,3,4,5) -> (1,2,0,4,5,3)
389 //=======================================================================
390 static void ShiftNodesQuadTria(const SMDS_MeshNode* aNodes[])
392 const SMDS_MeshNode* nd1 = aNodes[0];
393 aNodes[0] = aNodes[1];
394 aNodes[1] = aNodes[2];
396 const SMDS_MeshNode* nd2 = aNodes[3];
397 aNodes[3] = aNodes[4];
398 aNodes[4] = aNodes[5];
402 //=======================================================================
403 //function : GetNodesFromTwoTria
405 // Shift nodes in the array corresponded to quadratic triangle
406 // example: (0,1,2,3,4,5) -> (1,2,0,4,5,3)
407 //=======================================================================
408 static bool GetNodesFromTwoTria(const SMDS_MeshElement * theTria1,
409 const SMDS_MeshElement * theTria2,
410 const SMDS_MeshNode* N1[],
411 const SMDS_MeshNode* N2[])
413 SMDS_ElemIteratorPtr it = theTria1->nodesIterator();
416 N1[i] = static_cast<const SMDS_MeshNode*>( it->next() );
419 if(it->more()) return false;
420 it = theTria2->nodesIterator();
423 N2[i] = static_cast<const SMDS_MeshNode*>( it->next() );
426 if(it->more()) return false;
428 int sames[3] = {-1,-1,-1};
440 if(nbsames!=2) return false;
442 ShiftNodesQuadTria(N1);
444 ShiftNodesQuadTria(N1);
447 i = sames[0] + sames[1] + sames[2];
449 ShiftNodesQuadTria(N2);
451 // now we receive following N1 and N2 (using numeration as above image)
452 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
453 // i.e. first nodes from both arrays determ new diagonal
457 //=======================================================================
458 //function : InverseDiag
459 //purpose : Replace two neighbour triangles with ones built on the same 4 nodes
460 // but having other common link.
461 // Return False if args are improper
462 //=======================================================================
464 bool SMESH_MeshEditor::InverseDiag (const SMDS_MeshElement * theTria1,
465 const SMDS_MeshElement * theTria2 )
467 myLastCreatedElems.Clear();
468 myLastCreatedNodes.Clear();
470 if (!theTria1 || !theTria2)
473 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( theTria1 );
474 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( theTria2 );
477 // 1 +--+ A theTria1: ( 1 A B ) A->2 ( 1 2 B ) 1 +--+ A
478 // | /| theTria2: ( B A 2 ) B->1 ( 1 A 2 ) |\ |
482 // put nodes in array and find out indices of the same ones
483 const SMDS_MeshNode* aNodes [6];
484 int sameInd [] = { 0, 0, 0, 0, 0, 0 };
486 SMDS_ElemIteratorPtr it = theTria1->nodesIterator();
487 while ( it->more() ) {
488 aNodes[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
490 if ( i > 2 ) // theTria2
491 // find same node of theTria1
492 for ( int j = 0; j < 3; j++ )
493 if ( aNodes[ i ] == aNodes[ j ]) {
502 return false; // theTria1 is not a triangle
503 it = theTria2->nodesIterator();
505 if ( i == 6 && it->more() )
506 return false; // theTria2 is not a triangle
509 // find indices of 1,2 and of A,B in theTria1
510 int iA = 0, iB = 0, i1 = 0, i2 = 0;
511 for ( i = 0; i < 6; i++ ) {
512 if ( sameInd [ i ] == 0 )
519 // nodes 1 and 2 should not be the same
520 if ( aNodes[ i1 ] == aNodes[ i2 ] )
524 aNodes[ iA ] = aNodes[ i2 ];
526 aNodes[ sameInd[ iB ]] = aNodes[ i1 ];
528 //MESSAGE( theTria1 << theTria2 );
530 GetMeshDS()->ChangeElementNodes( theTria1, aNodes, 3 );
531 GetMeshDS()->ChangeElementNodes( theTria2, &aNodes[ 3 ], 3 );
533 //MESSAGE( theTria1 << theTria2 );
537 } // end if(F1 && F2)
539 // check case of quadratic faces
540 const SMDS_QuadraticFaceOfNodes* QF1 =
541 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (theTria1);
542 if(!QF1) return false;
543 const SMDS_QuadraticFaceOfNodes* QF2 =
544 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (theTria2);
545 if(!QF2) return false;
548 // 1 +--+--+ 2 theTria1: (1 2 4 5 9 7) or (2 4 1 9 7 5) or (4 1 2 7 5 9)
549 // | /| theTria2: (2 3 4 6 8 9) or (3 4 2 8 9 6) or (4 2 3 9 6 8)
557 const SMDS_MeshNode* N1 [6];
558 const SMDS_MeshNode* N2 [6];
559 if(!GetNodesFromTwoTria(theTria1,theTria2,N1,N2))
561 // now we receive following N1 and N2 (using numeration as above image)
562 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
563 // i.e. first nodes from both arrays determ new diagonal
565 const SMDS_MeshNode* N1new [6];
566 const SMDS_MeshNode* N2new [6];
579 // replaces nodes in faces
580 GetMeshDS()->ChangeElementNodes( theTria1, N1new, 6 );
581 GetMeshDS()->ChangeElementNodes( theTria2, N2new, 6 );
586 //=======================================================================
587 //function : findTriangles
588 //purpose : find triangles sharing theNode1-theNode2 link
589 //=======================================================================
591 static bool findTriangles(const SMDS_MeshNode * theNode1,
592 const SMDS_MeshNode * theNode2,
593 const SMDS_MeshElement*& theTria1,
594 const SMDS_MeshElement*& theTria2)
596 if ( !theNode1 || !theNode2 ) return false;
598 theTria1 = theTria2 = 0;
600 set< const SMDS_MeshElement* > emap;
601 SMDS_ElemIteratorPtr it = theNode1->GetInverseElementIterator(SMDSAbs_Face);
603 const SMDS_MeshElement* elem = it->next();
604 if ( elem->NbNodes() == 3 )
607 it = theNode2->GetInverseElementIterator(SMDSAbs_Face);
609 const SMDS_MeshElement* elem = it->next();
610 if ( emap.find( elem ) != emap.end() )
612 // theTria1 must be element with minimum ID
613 if( theTria1->GetID() < elem->GetID() ) {
626 return ( theTria1 && theTria2 );
629 //=======================================================================
630 //function : InverseDiag
631 //purpose : Replace two neighbour triangles sharing theNode1-theNode2 link
632 // with ones built on the same 4 nodes but having other common link.
633 // Return false if proper faces not found
634 //=======================================================================
636 bool SMESH_MeshEditor::InverseDiag (const SMDS_MeshNode * theNode1,
637 const SMDS_MeshNode * theNode2)
639 myLastCreatedElems.Clear();
640 myLastCreatedNodes.Clear();
642 MESSAGE( "::InverseDiag()" );
644 const SMDS_MeshElement *tr1, *tr2;
645 if ( !findTriangles( theNode1, theNode2, tr1, tr2 ))
648 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( tr1 );
649 //if (!F1) return false;
650 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( tr2 );
651 //if (!F2) return false;
654 // 1 +--+ A tr1: ( 1 A B ) A->2 ( 1 2 B ) 1 +--+ A
655 // | /| tr2: ( B A 2 ) B->1 ( 1 A 2 ) |\ |
659 // put nodes in array
660 // and find indices of 1,2 and of A in tr1 and of B in tr2
661 int i, iA1 = 0, i1 = 0;
662 const SMDS_MeshNode* aNodes1 [3];
663 SMDS_ElemIteratorPtr it;
664 for (i = 0, it = tr1->nodesIterator(); it->more(); i++ ) {
665 aNodes1[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
666 if ( aNodes1[ i ] == theNode1 )
667 iA1 = i; // node A in tr1
668 else if ( aNodes1[ i ] != theNode2 )
672 const SMDS_MeshNode* aNodes2 [3];
673 for (i = 0, it = tr2->nodesIterator(); it->more(); i++ ) {
674 aNodes2[ i ] = static_cast<const SMDS_MeshNode*>( it->next() );
675 if ( aNodes2[ i ] == theNode2 )
676 iB2 = i; // node B in tr2
677 else if ( aNodes2[ i ] != theNode1 )
681 // nodes 1 and 2 should not be the same
682 if ( aNodes1[ i1 ] == aNodes2[ i2 ] )
686 aNodes1[ iA1 ] = aNodes2[ i2 ];
688 aNodes2[ iB2 ] = aNodes1[ i1 ];
690 //MESSAGE( tr1 << tr2 );
692 GetMeshDS()->ChangeElementNodes( tr1, aNodes1, 3 );
693 GetMeshDS()->ChangeElementNodes( tr2, aNodes2, 3 );
695 //MESSAGE( tr1 << tr2 );
700 // check case of quadratic faces
701 const SMDS_QuadraticFaceOfNodes* QF1 =
702 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (tr1);
703 if(!QF1) return false;
704 const SMDS_QuadraticFaceOfNodes* QF2 =
705 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (tr2);
706 if(!QF2) return false;
707 return InverseDiag(tr1,tr2);
710 //=======================================================================
711 //function : getQuadrangleNodes
712 //purpose : fill theQuadNodes - nodes of a quadrangle resulting from
713 // fusion of triangles tr1 and tr2 having shared link on
714 // theNode1 and theNode2
715 //=======================================================================
717 bool getQuadrangleNodes(const SMDS_MeshNode * theQuadNodes [],
718 const SMDS_MeshNode * theNode1,
719 const SMDS_MeshNode * theNode2,
720 const SMDS_MeshElement * tr1,
721 const SMDS_MeshElement * tr2 )
723 if( tr1->NbNodes() != tr2->NbNodes() )
725 // find the 4-th node to insert into tr1
726 const SMDS_MeshNode* n4 = 0;
727 SMDS_ElemIteratorPtr it = tr2->nodesIterator();
729 while ( !n4 && i<3 ) {
730 const SMDS_MeshNode * n = cast2Node( it->next() );
732 bool isDiag = ( n == theNode1 || n == theNode2 );
736 // Make an array of nodes to be in a quadrangle
737 int iNode = 0, iFirstDiag = -1;
738 it = tr1->nodesIterator();
741 const SMDS_MeshNode * n = cast2Node( it->next() );
743 bool isDiag = ( n == theNode1 || n == theNode2 );
745 if ( iFirstDiag < 0 )
747 else if ( iNode - iFirstDiag == 1 )
748 theQuadNodes[ iNode++ ] = n4; // insert the 4-th node between diagonal nodes
750 else if ( n == n4 ) {
751 return false; // tr1 and tr2 should not have all the same nodes
753 theQuadNodes[ iNode++ ] = n;
755 if ( iNode == 3 ) // diagonal nodes have 0 and 2 indices
756 theQuadNodes[ iNode ] = n4;
761 //=======================================================================
762 //function : DeleteDiag
763 //purpose : Replace two neighbour triangles sharing theNode1-theNode2 link
764 // with a quadrangle built on the same 4 nodes.
765 // Return false if proper faces not found
766 //=======================================================================
768 bool SMESH_MeshEditor::DeleteDiag (const SMDS_MeshNode * theNode1,
769 const SMDS_MeshNode * theNode2)
771 myLastCreatedElems.Clear();
772 myLastCreatedNodes.Clear();
774 MESSAGE( "::DeleteDiag()" );
776 const SMDS_MeshElement *tr1, *tr2;
777 if ( !findTriangles( theNode1, theNode2, tr1, tr2 ))
780 const SMDS_FaceOfNodes* F1 = dynamic_cast<const SMDS_FaceOfNodes*>( tr1 );
781 //if (!F1) return false;
782 const SMDS_FaceOfNodes* F2 = dynamic_cast<const SMDS_FaceOfNodes*>( tr2 );
783 //if (!F2) return false;
786 const SMDS_MeshNode* aNodes [ 4 ];
787 if ( ! getQuadrangleNodes( aNodes, theNode1, theNode2, tr1, tr2 ))
790 //MESSAGE( endl << tr1 << tr2 );
792 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 4 );
793 myLastCreatedElems.Append(tr1);
794 GetMeshDS()->RemoveElement( tr2 );
796 //MESSAGE( endl << tr1 );
801 // check case of quadratic faces
802 const SMDS_QuadraticFaceOfNodes* QF1 =
803 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (tr1);
804 if(!QF1) return false;
805 const SMDS_QuadraticFaceOfNodes* QF2 =
806 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (tr2);
807 if(!QF2) return false;
810 // 1 +--+--+ 2 tr1: (1 2 4 5 9 7) or (2 4 1 9 7 5) or (4 1 2 7 5 9)
811 // | /| tr2: (2 3 4 6 8 9) or (3 4 2 8 9 6) or (4 2 3 9 6 8)
819 const SMDS_MeshNode* N1 [6];
820 const SMDS_MeshNode* N2 [6];
821 if(!GetNodesFromTwoTria(tr1,tr2,N1,N2))
823 // now we receive following N1 and N2 (using numeration as above image)
824 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
825 // i.e. first nodes from both arrays determ new diagonal
827 const SMDS_MeshNode* aNodes[8];
837 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 8 );
838 myLastCreatedElems.Append(tr1);
839 GetMeshDS()->RemoveElement( tr2 );
841 // remove middle node (9)
842 GetMeshDS()->RemoveNode( N1[4] );
847 //=======================================================================
848 //function : Reorient
849 //purpose : Reverse theElement orientation
850 //=======================================================================
852 bool SMESH_MeshEditor::Reorient (const SMDS_MeshElement * theElem)
854 myLastCreatedElems.Clear();
855 myLastCreatedNodes.Clear();
859 SMDS_ElemIteratorPtr it = theElem->nodesIterator();
860 if ( !it || !it->more() )
863 switch ( theElem->GetType() ) {
867 if(!theElem->IsQuadratic()) {
868 int i = theElem->NbNodes();
869 vector<const SMDS_MeshNode*> aNodes( i );
871 aNodes[ --i ]= static_cast<const SMDS_MeshNode*>( it->next() );
872 return GetMeshDS()->ChangeElementNodes( theElem, &aNodes[0], theElem->NbNodes() );
875 // quadratic elements
876 if(theElem->GetType()==SMDSAbs_Edge) {
877 vector<const SMDS_MeshNode*> aNodes(3);
878 aNodes[1]= static_cast<const SMDS_MeshNode*>( it->next() );
879 aNodes[0]= static_cast<const SMDS_MeshNode*>( it->next() );
880 aNodes[2]= static_cast<const SMDS_MeshNode*>( it->next() );
881 return GetMeshDS()->ChangeElementNodes( theElem, &aNodes[0], 3 );
884 int nbn = theElem->NbNodes();
885 vector<const SMDS_MeshNode*> aNodes(nbn);
886 aNodes[0]= static_cast<const SMDS_MeshNode*>( it->next() );
888 for(; i<nbn/2; i++) {
889 aNodes[nbn/2-i]= static_cast<const SMDS_MeshNode*>( it->next() );
891 for(i=0; i<nbn/2; i++) {
892 aNodes[nbn-i-1]= static_cast<const SMDS_MeshNode*>( it->next() );
894 return GetMeshDS()->ChangeElementNodes( theElem, &aNodes[0], nbn );
898 case SMDSAbs_Volume: {
899 if (theElem->IsPoly()) {
900 const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
901 static_cast<const SMDS_PolyhedralVolumeOfNodes*>( theElem );
903 MESSAGE("Warning: bad volumic element");
907 int nbFaces = aPolyedre->NbFaces();
908 vector<const SMDS_MeshNode *> poly_nodes;
909 vector<int> quantities (nbFaces);
911 // reverse each face of the polyedre
912 for (int iface = 1; iface <= nbFaces; iface++) {
913 int inode, nbFaceNodes = aPolyedre->NbFaceNodes(iface);
914 quantities[iface - 1] = nbFaceNodes;
916 for (inode = nbFaceNodes; inode >= 1; inode--) {
917 const SMDS_MeshNode* curNode = aPolyedre->GetFaceNode(iface, inode);
918 poly_nodes.push_back(curNode);
922 return GetMeshDS()->ChangePolyhedronNodes( theElem, poly_nodes, quantities );
926 SMDS_VolumeTool vTool;
927 if ( !vTool.Set( theElem ))
930 return GetMeshDS()->ChangeElementNodes( theElem, vTool.GetNodes(), vTool.NbNodes() );
939 //=======================================================================
940 //function : getBadRate
942 //=======================================================================
944 static double getBadRate (const SMDS_MeshElement* theElem,
945 SMESH::Controls::NumericalFunctorPtr& theCrit)
947 SMESH::Controls::TSequenceOfXYZ P;
948 if ( !theElem || !theCrit->GetPoints( theElem, P ))
950 return theCrit->GetBadRate( theCrit->GetValue( P ), theElem->NbNodes() );
951 //return theCrit->GetBadRate( theCrit->GetValue( theElem->GetID() ), theElem->NbNodes() );
954 //=======================================================================
955 //function : QuadToTri
956 //purpose : Cut quadrangles into triangles.
957 // theCrit is used to select a diagonal to cut
958 //=======================================================================
960 bool SMESH_MeshEditor::QuadToTri (TIDSortedElemSet & theElems,
961 SMESH::Controls::NumericalFunctorPtr theCrit)
963 myLastCreatedElems.Clear();
964 myLastCreatedNodes.Clear();
966 MESSAGE( "::QuadToTri()" );
968 if ( !theCrit.get() )
971 SMESHDS_Mesh * aMesh = GetMeshDS();
973 Handle(Geom_Surface) surface;
974 SMESH_MesherHelper helper( *GetMesh() );
976 TIDSortedElemSet::iterator itElem;
977 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
978 const SMDS_MeshElement* elem = *itElem;
979 if ( !elem || elem->GetType() != SMDSAbs_Face )
981 if ( elem->NbNodes() != ( elem->IsQuadratic() ? 8 : 4 ))
984 // retrieve element nodes
985 const SMDS_MeshNode* aNodes [8];
986 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
988 while ( itN->more() )
989 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
991 // compare two sets of possible triangles
992 double aBadRate1, aBadRate2; // to what extent a set is bad
993 SMDS_FaceOfNodes tr1 ( aNodes[0], aNodes[1], aNodes[2] );
994 SMDS_FaceOfNodes tr2 ( aNodes[2], aNodes[3], aNodes[0] );
995 aBadRate1 = getBadRate( &tr1, theCrit ) + getBadRate( &tr2, theCrit );
997 SMDS_FaceOfNodes tr3 ( aNodes[1], aNodes[2], aNodes[3] );
998 SMDS_FaceOfNodes tr4 ( aNodes[3], aNodes[0], aNodes[1] );
999 aBadRate2 = getBadRate( &tr3, theCrit ) + getBadRate( &tr4, theCrit );
1001 int aShapeId = FindShape( elem );
1002 const SMDS_MeshElement* newElem = 0;
1004 if( !elem->IsQuadratic() ) {
1006 // split liner quadrangle
1008 if ( aBadRate1 <= aBadRate2 ) {
1009 // tr1 + tr2 is better
1010 aMesh->ChangeElementNodes( elem, aNodes, 3 );
1011 newElem = aMesh->AddFace( aNodes[2], aNodes[3], aNodes[0] );
1014 // tr3 + tr4 is better
1015 aMesh->ChangeElementNodes( elem, &aNodes[1], 3 );
1016 newElem = aMesh->AddFace( aNodes[3], aNodes[0], aNodes[1] );
1021 // split quadratic quadrangle
1023 // get surface elem is on
1024 if ( aShapeId != helper.GetSubShapeID() ) {
1028 shape = aMesh->IndexToShape( aShapeId );
1029 if ( !shape.IsNull() && shape.ShapeType() == TopAbs_FACE ) {
1030 TopoDS_Face face = TopoDS::Face( shape );
1031 surface = BRep_Tool::Surface( face );
1032 if ( !surface.IsNull() )
1033 helper.SetSubShape( shape );
1037 const SMDS_MeshNode* aNodes [8];
1038 const SMDS_MeshNode* inFaceNode = 0;
1039 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1041 while ( itN->more() ) {
1042 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1043 if ( !inFaceNode && helper.GetNodeUVneedInFaceNode() &&
1044 aNodes[ i-1 ]->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE )
1046 inFaceNode = aNodes[ i-1 ];
1049 // find middle point for (0,1,2,3)
1050 // and create a node in this point;
1052 if ( surface.IsNull() ) {
1054 p += gp_XYZ(aNodes[i]->X(), aNodes[i]->Y(), aNodes[i]->Z() );
1058 TopoDS_Face face = TopoDS::Face( helper.GetSubShape() );
1061 uv += helper.GetNodeUV( face, aNodes[i], inFaceNode );
1063 p = surface->Value( uv.X(), uv.Y() ).XYZ();
1065 const SMDS_MeshNode* newN = aMesh->AddNode( p.X(), p.Y(), p.Z() );
1066 myLastCreatedNodes.Append(newN);
1068 // create a new element
1069 const SMDS_MeshNode* N[6];
1070 if ( aBadRate1 <= aBadRate2 ) {
1077 newElem = aMesh->AddFace(aNodes[2], aNodes[3], aNodes[0],
1078 aNodes[6], aNodes[7], newN );
1087 newElem = aMesh->AddFace(aNodes[3], aNodes[0], aNodes[1],
1088 aNodes[7], aNodes[4], newN );
1090 aMesh->ChangeElementNodes( elem, N, 6 );
1094 // care of a new element
1096 myLastCreatedElems.Append(newElem);
1097 AddToSameGroups( newElem, elem, aMesh );
1099 // put a new triangle on the same shape
1101 aMesh->SetMeshElementOnShape( newElem, aShapeId );
1106 //=======================================================================
1107 //function : BestSplit
1108 //purpose : Find better diagonal for cutting.
1109 //=======================================================================
1110 int SMESH_MeshEditor::BestSplit (const SMDS_MeshElement* theQuad,
1111 SMESH::Controls::NumericalFunctorPtr theCrit)
1113 myLastCreatedElems.Clear();
1114 myLastCreatedNodes.Clear();
1119 if (!theQuad || theQuad->GetType() != SMDSAbs_Face )
1122 if( theQuad->NbNodes()==4 ||
1123 (theQuad->NbNodes()==8 && theQuad->IsQuadratic()) ) {
1125 // retrieve element nodes
1126 const SMDS_MeshNode* aNodes [4];
1127 SMDS_ElemIteratorPtr itN = theQuad->nodesIterator();
1129 //while (itN->more())
1131 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1133 // compare two sets of possible triangles
1134 double aBadRate1, aBadRate2; // to what extent a set is bad
1135 SMDS_FaceOfNodes tr1 ( aNodes[0], aNodes[1], aNodes[2] );
1136 SMDS_FaceOfNodes tr2 ( aNodes[2], aNodes[3], aNodes[0] );
1137 aBadRate1 = getBadRate( &tr1, theCrit ) + getBadRate( &tr2, theCrit );
1139 SMDS_FaceOfNodes tr3 ( aNodes[1], aNodes[2], aNodes[3] );
1140 SMDS_FaceOfNodes tr4 ( aNodes[3], aNodes[0], aNodes[1] );
1141 aBadRate2 = getBadRate( &tr3, theCrit ) + getBadRate( &tr4, theCrit );
1143 if (aBadRate1 <= aBadRate2) // tr1 + tr2 is better
1144 return 1; // diagonal 1-3
1146 return 2; // diagonal 2-4
1151 //=======================================================================
1152 //function : AddToSameGroups
1153 //purpose : add elemToAdd to the groups the elemInGroups belongs to
1154 //=======================================================================
1156 void SMESH_MeshEditor::AddToSameGroups (const SMDS_MeshElement* elemToAdd,
1157 const SMDS_MeshElement* elemInGroups,
1158 SMESHDS_Mesh * aMesh)
1160 const set<SMESHDS_GroupBase*>& groups = aMesh->GetGroups();
1161 set<SMESHDS_GroupBase*>::const_iterator grIt = groups.begin();
1162 for ( ; grIt != groups.end(); grIt++ ) {
1163 SMESHDS_Group* group = dynamic_cast<SMESHDS_Group*>( *grIt );
1164 if ( group && group->SMDSGroup().Contains( elemInGroups ))
1165 group->SMDSGroup().Add( elemToAdd );
1170 //=======================================================================
1171 //function : RemoveElemFromGroups
1172 //purpose : Remove removeelem to the groups the elemInGroups belongs to
1173 //=======================================================================
1174 void SMESH_MeshEditor::RemoveElemFromGroups (const SMDS_MeshElement* removeelem,
1175 SMESHDS_Mesh * aMesh)
1177 const set<SMESHDS_GroupBase*>& groups = aMesh->GetGroups();
1178 if (!groups.empty())
1180 set<SMESHDS_GroupBase*>::const_iterator GrIt = groups.begin();
1181 for (; GrIt != groups.end(); GrIt++)
1183 SMESHDS_Group* grp = dynamic_cast<SMESHDS_Group*>(*GrIt);
1184 if (!grp || grp->IsEmpty()) continue;
1185 grp->SMDSGroup().Remove(removeelem);
1191 //=======================================================================
1192 //function : QuadToTri
1193 //purpose : Cut quadrangles into triangles.
1194 // theCrit is used to select a diagonal to cut
1195 //=======================================================================
1197 bool SMESH_MeshEditor::QuadToTri (TIDSortedElemSet & theElems,
1198 const bool the13Diag)
1200 myLastCreatedElems.Clear();
1201 myLastCreatedNodes.Clear();
1203 MESSAGE( "::QuadToTri()" );
1205 SMESHDS_Mesh * aMesh = GetMeshDS();
1207 Handle(Geom_Surface) surface;
1208 SMESH_MesherHelper helper( *GetMesh() );
1210 TIDSortedElemSet::iterator itElem;
1211 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
1212 const SMDS_MeshElement* elem = *itElem;
1213 if ( !elem || elem->GetType() != SMDSAbs_Face )
1215 bool isquad = elem->NbNodes()==4 || elem->NbNodes()==8;
1216 if(!isquad) continue;
1218 if(elem->NbNodes()==4) {
1219 // retrieve element nodes
1220 const SMDS_MeshNode* aNodes [4];
1221 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1223 while ( itN->more() )
1224 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1226 int aShapeId = FindShape( elem );
1227 const SMDS_MeshElement* newElem = 0;
1229 aMesh->ChangeElementNodes( elem, aNodes, 3 );
1230 newElem = aMesh->AddFace( aNodes[2], aNodes[3], aNodes[0] );
1233 aMesh->ChangeElementNodes( elem, &aNodes[1], 3 );
1234 newElem = aMesh->AddFace( aNodes[3], aNodes[0], aNodes[1] );
1236 myLastCreatedElems.Append(newElem);
1237 // put a new triangle on the same shape and add to the same groups
1239 aMesh->SetMeshElementOnShape( newElem, aShapeId );
1240 AddToSameGroups( newElem, elem, aMesh );
1243 // Quadratic quadrangle
1245 if( elem->NbNodes()==8 && elem->IsQuadratic() ) {
1247 // get surface elem is on
1248 int aShapeId = FindShape( elem );
1249 if ( aShapeId != helper.GetSubShapeID() ) {
1253 shape = aMesh->IndexToShape( aShapeId );
1254 if ( !shape.IsNull() && shape.ShapeType() == TopAbs_FACE ) {
1255 TopoDS_Face face = TopoDS::Face( shape );
1256 surface = BRep_Tool::Surface( face );
1257 if ( !surface.IsNull() )
1258 helper.SetSubShape( shape );
1262 const SMDS_MeshNode* aNodes [8];
1263 const SMDS_MeshNode* inFaceNode = 0;
1264 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1266 while ( itN->more() ) {
1267 aNodes[ i++ ] = static_cast<const SMDS_MeshNode*>( itN->next() );
1268 if ( !inFaceNode && helper.GetNodeUVneedInFaceNode() &&
1269 aNodes[ i-1 ]->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE )
1271 inFaceNode = aNodes[ i-1 ];
1275 // find middle point for (0,1,2,3)
1276 // and create a node in this point;
1278 if ( surface.IsNull() ) {
1280 p += gp_XYZ(aNodes[i]->X(), aNodes[i]->Y(), aNodes[i]->Z() );
1284 TopoDS_Face geomFace = TopoDS::Face( helper.GetSubShape() );
1287 uv += helper.GetNodeUV( geomFace, aNodes[i], inFaceNode );
1289 p = surface->Value( uv.X(), uv.Y() ).XYZ();
1291 const SMDS_MeshNode* newN = aMesh->AddNode( p.X(), p.Y(), p.Z() );
1292 myLastCreatedNodes.Append(newN);
1294 // create a new element
1295 const SMDS_MeshElement* newElem = 0;
1296 const SMDS_MeshNode* N[6];
1304 newElem = aMesh->AddFace(aNodes[2], aNodes[3], aNodes[0],
1305 aNodes[6], aNodes[7], newN );
1314 newElem = aMesh->AddFace(aNodes[3], aNodes[0], aNodes[1],
1315 aNodes[7], aNodes[4], newN );
1317 myLastCreatedElems.Append(newElem);
1318 aMesh->ChangeElementNodes( elem, N, 6 );
1319 // put a new triangle on the same shape and add to the same groups
1321 aMesh->SetMeshElementOnShape( newElem, aShapeId );
1322 AddToSameGroups( newElem, elem, aMesh );
1329 //=======================================================================
1330 //function : getAngle
1332 //=======================================================================
1334 double getAngle(const SMDS_MeshElement * tr1,
1335 const SMDS_MeshElement * tr2,
1336 const SMDS_MeshNode * n1,
1337 const SMDS_MeshNode * n2)
1339 double angle = 2*PI; // bad angle
1342 SMESH::Controls::TSequenceOfXYZ P1, P2;
1343 if ( !SMESH::Controls::NumericalFunctor::GetPoints( tr1, P1 ) ||
1344 !SMESH::Controls::NumericalFunctor::GetPoints( tr2, P2 ))
1347 if(!tr1->IsQuadratic())
1348 N1 = gp_Vec( P1(2) - P1(1) ) ^ gp_Vec( P1(3) - P1(1) );
1350 N1 = gp_Vec( P1(3) - P1(1) ) ^ gp_Vec( P1(5) - P1(1) );
1351 if ( N1.SquareMagnitude() <= gp::Resolution() )
1353 if(!tr2->IsQuadratic())
1354 N2 = gp_Vec( P2(2) - P2(1) ) ^ gp_Vec( P2(3) - P2(1) );
1356 N2 = gp_Vec( P2(3) - P2(1) ) ^ gp_Vec( P2(5) - P2(1) );
1357 if ( N2.SquareMagnitude() <= gp::Resolution() )
1360 // find the first diagonal node n1 in the triangles:
1361 // take in account a diagonal link orientation
1362 const SMDS_MeshElement *nFirst[2], *tr[] = { tr1, tr2 };
1363 for ( int t = 0; t < 2; t++ ) {
1364 SMDS_ElemIteratorPtr it = tr[ t ]->nodesIterator();
1365 int i = 0, iDiag = -1;
1366 while ( it->more()) {
1367 const SMDS_MeshElement *n = it->next();
1368 if ( n == n1 || n == n2 )
1372 if ( i - iDiag == 1 )
1373 nFirst[ t ] = ( n == n1 ? n2 : n1 );
1381 if ( nFirst[ 0 ] == nFirst[ 1 ] )
1384 angle = N1.Angle( N2 );
1389 // =================================================
1390 // class generating a unique ID for a pair of nodes
1391 // and able to return nodes by that ID
1392 // =================================================
1396 LinkID_Gen( const SMESHDS_Mesh* theMesh )
1397 :myMesh( theMesh ), myMaxID( theMesh->MaxNodeID() + 1)
1400 long GetLinkID (const SMDS_MeshNode * n1,
1401 const SMDS_MeshNode * n2) const
1403 return ( Min(n1->GetID(),n2->GetID()) * myMaxID + Max(n1->GetID(),n2->GetID()));
1406 bool GetNodes (const long theLinkID,
1407 const SMDS_MeshNode* & theNode1,
1408 const SMDS_MeshNode* & theNode2) const
1410 theNode1 = myMesh->FindNode( theLinkID / myMaxID );
1411 if ( !theNode1 ) return false;
1412 theNode2 = myMesh->FindNode( theLinkID % myMaxID );
1413 if ( !theNode2 ) return false;
1419 const SMESHDS_Mesh* myMesh;
1424 //=======================================================================
1425 //function : TriToQuad
1426 //purpose : Fuse neighbour triangles into quadrangles.
1427 // theCrit is used to select a neighbour to fuse with.
1428 // theMaxAngle is a max angle between element normals at which
1429 // fusion is still performed.
1430 //=======================================================================
1432 bool SMESH_MeshEditor::TriToQuad (TIDSortedElemSet & theElems,
1433 SMESH::Controls::NumericalFunctorPtr theCrit,
1434 const double theMaxAngle)
1436 myLastCreatedElems.Clear();
1437 myLastCreatedNodes.Clear();
1439 MESSAGE( "::TriToQuad()" );
1441 if ( !theCrit.get() )
1444 SMESHDS_Mesh * aMesh = GetMeshDS();
1446 // Prepare data for algo: build
1447 // 1. map of elements with their linkIDs
1448 // 2. map of linkIDs with their elements
1450 map< TLink, list< const SMDS_MeshElement* > > mapLi_listEl;
1451 map< TLink, list< const SMDS_MeshElement* > >::iterator itLE;
1452 map< const SMDS_MeshElement*, set< TLink > > mapEl_setLi;
1453 map< const SMDS_MeshElement*, set< TLink > >::iterator itEL;
1455 TIDSortedElemSet::iterator itElem;
1456 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
1457 const SMDS_MeshElement* elem = *itElem;
1458 if(!elem || elem->GetType() != SMDSAbs_Face ) continue;
1459 bool IsTria = elem->NbNodes()==3 || (elem->NbNodes()==6 && elem->IsQuadratic());
1460 if(!IsTria) continue;
1462 // retrieve element nodes
1463 const SMDS_MeshNode* aNodes [4];
1464 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
1467 aNodes[ i++ ] = cast2Node( itN->next() );
1468 aNodes[ 3 ] = aNodes[ 0 ];
1471 for ( i = 0; i < 3; i++ ) {
1472 TLink link( aNodes[i], aNodes[i+1] );
1473 // check if elements sharing a link can be fused
1474 itLE = mapLi_listEl.find( link );
1475 if ( itLE != mapLi_listEl.end() ) {
1476 if ((*itLE).second.size() > 1 ) // consider only 2 elems adjacent by a link
1478 const SMDS_MeshElement* elem2 = (*itLE).second.front();
1479 //if ( FindShape( elem ) != FindShape( elem2 ))
1480 // continue; // do not fuse triangles laying on different shapes
1481 if ( getAngle( elem, elem2, aNodes[i], aNodes[i+1] ) > theMaxAngle )
1482 continue; // avoid making badly shaped quads
1483 (*itLE).second.push_back( elem );
1486 mapLi_listEl[ link ].push_back( elem );
1488 mapEl_setLi [ elem ].insert( link );
1491 // Clean the maps from the links shared by a sole element, ie
1492 // links to which only one element is bound in mapLi_listEl
1494 for ( itLE = mapLi_listEl.begin(); itLE != mapLi_listEl.end(); itLE++ ) {
1495 int nbElems = (*itLE).second.size();
1496 if ( nbElems < 2 ) {
1497 const SMDS_MeshElement* elem = (*itLE).second.front();
1498 TLink link = (*itLE).first;
1499 mapEl_setLi[ elem ].erase( link );
1500 if ( mapEl_setLi[ elem ].empty() )
1501 mapEl_setLi.erase( elem );
1505 // Algo: fuse triangles into quadrangles
1507 while ( ! mapEl_setLi.empty() ) {
1508 // Look for the start element:
1509 // the element having the least nb of shared links
1510 const SMDS_MeshElement* startElem = 0;
1512 for ( itEL = mapEl_setLi.begin(); itEL != mapEl_setLi.end(); itEL++ ) {
1513 int nbLinks = (*itEL).second.size();
1514 if ( nbLinks < minNbLinks ) {
1515 startElem = (*itEL).first;
1516 minNbLinks = nbLinks;
1517 if ( minNbLinks == 1 )
1522 // search elements to fuse starting from startElem or links of elements
1523 // fused earlyer - startLinks
1524 list< TLink > startLinks;
1525 while ( startElem || !startLinks.empty() ) {
1526 while ( !startElem && !startLinks.empty() ) {
1527 // Get an element to start, by a link
1528 TLink linkId = startLinks.front();
1529 startLinks.pop_front();
1530 itLE = mapLi_listEl.find( linkId );
1531 if ( itLE != mapLi_listEl.end() ) {
1532 list< const SMDS_MeshElement* > & listElem = (*itLE).second;
1533 list< const SMDS_MeshElement* >::iterator itE = listElem.begin();
1534 for ( ; itE != listElem.end() ; itE++ )
1535 if ( mapEl_setLi.find( (*itE) ) != mapEl_setLi.end() )
1537 mapLi_listEl.erase( itLE );
1542 // Get candidates to be fused
1543 const SMDS_MeshElement *tr1 = startElem, *tr2 = 0, *tr3 = 0;
1544 const TLink *link12, *link13;
1546 ASSERT( mapEl_setLi.find( tr1 ) != mapEl_setLi.end() );
1547 set< TLink >& setLi = mapEl_setLi[ tr1 ];
1548 ASSERT( !setLi.empty() );
1549 set< TLink >::iterator itLi;
1550 for ( itLi = setLi.begin(); itLi != setLi.end(); itLi++ )
1552 const TLink & link = (*itLi);
1553 itLE = mapLi_listEl.find( link );
1554 if ( itLE == mapLi_listEl.end() )
1557 const SMDS_MeshElement* elem = (*itLE).second.front();
1559 elem = (*itLE).second.back();
1560 mapLi_listEl.erase( itLE );
1561 if ( mapEl_setLi.find( elem ) == mapEl_setLi.end())
1572 // add other links of elem to list of links to re-start from
1573 set< TLink >& links = mapEl_setLi[ elem ];
1574 set< TLink >::iterator it;
1575 for ( it = links.begin(); it != links.end(); it++ ) {
1576 const TLink& link2 = (*it);
1577 if ( link2 != link )
1578 startLinks.push_back( link2 );
1582 // Get nodes of possible quadrangles
1583 const SMDS_MeshNode *n12 [4], *n13 [4];
1584 bool Ok12 = false, Ok13 = false;
1585 const SMDS_MeshNode *linkNode1, *linkNode2;
1587 linkNode1 = link12->first;
1588 linkNode2 = link12->second;
1589 if ( tr2 && getQuadrangleNodes( n12, linkNode1, linkNode2, tr1, tr2 ))
1593 linkNode1 = link13->first;
1594 linkNode2 = link13->second;
1595 if ( tr3 && getQuadrangleNodes( n13, linkNode1, linkNode2, tr1, tr3 ))
1599 // Choose a pair to fuse
1600 if ( Ok12 && Ok13 ) {
1601 SMDS_FaceOfNodes quad12 ( n12[ 0 ], n12[ 1 ], n12[ 2 ], n12[ 3 ] );
1602 SMDS_FaceOfNodes quad13 ( n13[ 0 ], n13[ 1 ], n13[ 2 ], n13[ 3 ] );
1603 double aBadRate12 = getBadRate( &quad12, theCrit );
1604 double aBadRate13 = getBadRate( &quad13, theCrit );
1605 if ( aBadRate13 < aBadRate12 )
1612 // and remove fused elems and removed links from the maps
1613 mapEl_setLi.erase( tr1 );
1615 mapEl_setLi.erase( tr2 );
1616 mapLi_listEl.erase( *link12 );
1617 if(tr1->NbNodes()==3) {
1618 if( tr1->GetID() < tr2->GetID() ) {
1619 aMesh->ChangeElementNodes( tr1, n12, 4 );
1620 myLastCreatedElems.Append(tr1);
1621 aMesh->RemoveElement( tr2 );
1624 aMesh->ChangeElementNodes( tr2, n12, 4 );
1625 myLastCreatedElems.Append(tr2);
1626 aMesh->RemoveElement( tr1);
1630 const SMDS_MeshNode* N1 [6];
1631 const SMDS_MeshNode* N2 [6];
1632 GetNodesFromTwoTria(tr1,tr2,N1,N2);
1633 // now we receive following N1 and N2 (using numeration as above image)
1634 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
1635 // i.e. first nodes from both arrays determ new diagonal
1636 const SMDS_MeshNode* aNodes[8];
1645 if( tr1->GetID() < tr2->GetID() ) {
1646 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 8 );
1647 myLastCreatedElems.Append(tr1);
1648 GetMeshDS()->RemoveElement( tr2 );
1651 GetMeshDS()->ChangeElementNodes( tr2, aNodes, 8 );
1652 myLastCreatedElems.Append(tr2);
1653 GetMeshDS()->RemoveElement( tr1 );
1655 // remove middle node (9)
1656 GetMeshDS()->RemoveNode( N1[4] );
1660 mapEl_setLi.erase( tr3 );
1661 mapLi_listEl.erase( *link13 );
1662 if(tr1->NbNodes()==3) {
1663 if( tr1->GetID() < tr2->GetID() ) {
1664 aMesh->ChangeElementNodes( tr1, n13, 4 );
1665 myLastCreatedElems.Append(tr1);
1666 aMesh->RemoveElement( tr3 );
1669 aMesh->ChangeElementNodes( tr3, n13, 4 );
1670 myLastCreatedElems.Append(tr3);
1671 aMesh->RemoveElement( tr1 );
1675 const SMDS_MeshNode* N1 [6];
1676 const SMDS_MeshNode* N2 [6];
1677 GetNodesFromTwoTria(tr1,tr3,N1,N2);
1678 // now we receive following N1 and N2 (using numeration as above image)
1679 // tria1 : (1 2 4 5 9 7) and tria2 : (3 4 2 8 9 6)
1680 // i.e. first nodes from both arrays determ new diagonal
1681 const SMDS_MeshNode* aNodes[8];
1690 if( tr1->GetID() < tr2->GetID() ) {
1691 GetMeshDS()->ChangeElementNodes( tr1, aNodes, 8 );
1692 myLastCreatedElems.Append(tr1);
1693 GetMeshDS()->RemoveElement( tr3 );
1696 GetMeshDS()->ChangeElementNodes( tr3, aNodes, 8 );
1697 myLastCreatedElems.Append(tr3);
1698 GetMeshDS()->RemoveElement( tr1 );
1700 // remove middle node (9)
1701 GetMeshDS()->RemoveNode( N1[4] );
1705 // Next element to fuse: the rejected one
1707 startElem = Ok12 ? tr3 : tr2;
1709 } // if ( startElem )
1710 } // while ( startElem || !startLinks.empty() )
1711 } // while ( ! mapEl_setLi.empty() )
1717 /*#define DUMPSO(txt) \
1718 // cout << txt << endl;
1719 //=============================================================================
1723 //=============================================================================
1724 static void swap( int i1, int i2, int idNodes[], gp_Pnt P[] )
1728 int tmp = idNodes[ i1 ];
1729 idNodes[ i1 ] = idNodes[ i2 ];
1730 idNodes[ i2 ] = tmp;
1731 gp_Pnt Ptmp = P[ i1 ];
1734 DUMPSO( i1 << "(" << idNodes[ i2 ] << ") <-> " << i2 << "(" << idNodes[ i1 ] << ")");
1737 //=======================================================================
1738 //function : SortQuadNodes
1739 //purpose : Set 4 nodes of a quadrangle face in a good order.
1740 // Swap 1<->2 or 2<->3 nodes and correspondingly return
1742 //=======================================================================
1744 int SMESH_MeshEditor::SortQuadNodes (const SMDS_Mesh * theMesh,
1749 for ( i = 0; i < 4; i++ ) {
1750 const SMDS_MeshNode *n = theMesh->FindNode( idNodes[i] );
1752 P[ i ].SetCoord( n->X(), n->Y(), n->Z() );
1755 gp_Vec V1(P[0], P[1]);
1756 gp_Vec V2(P[0], P[2]);
1757 gp_Vec V3(P[0], P[3]);
1759 gp_Vec Cross1 = V1 ^ V2;
1760 gp_Vec Cross2 = V2 ^ V3;
1763 if (Cross1.Dot(Cross2) < 0)
1768 if (Cross1.Dot(Cross2) < 0)
1772 swap ( i, i + 1, idNodes, P );
1774 // for ( int ii = 0; ii < 4; ii++ ) {
1775 // const SMDS_MeshNode *n = theMesh->FindNode( idNodes[ii] );
1776 // DUMPSO( ii << "(" << idNodes[ii] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1782 //=======================================================================
1783 //function : SortHexaNodes
1784 //purpose : Set 8 nodes of a hexahedron in a good order.
1785 // Return success status
1786 //=======================================================================
1788 bool SMESH_MeshEditor::SortHexaNodes (const SMDS_Mesh * theMesh,
1793 DUMPSO( "INPUT: ========================================");
1794 for ( i = 0; i < 8; i++ ) {
1795 const SMDS_MeshNode *n = theMesh->FindNode( idNodes[i] );
1796 if ( !n ) return false;
1797 P[ i ].SetCoord( n->X(), n->Y(), n->Z() );
1798 DUMPSO( i << "(" << idNodes[i] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1800 DUMPSO( "========================================");
1803 set<int> faceNodes; // ids of bottom face nodes, to be found
1804 set<int> checkedId1; // ids of tried 2-nd nodes
1805 Standard_Real leastDist = DBL_MAX; // dist of the 4-th node from 123 plane
1806 const Standard_Real tol = 1.e-6; // tolerance to find nodes in plane
1807 int iMin, iLoop1 = 0;
1809 // Loop to try the 2-nd nodes
1811 while ( leastDist > DBL_MIN && ++iLoop1 < 8 )
1813 // Find not checked 2-nd node
1814 for ( i = 1; i < 8; i++ )
1815 if ( checkedId1.find( idNodes[i] ) == checkedId1.end() ) {
1816 int id1 = idNodes[i];
1817 swap ( 1, i, idNodes, P );
1818 checkedId1.insert ( id1 );
1822 // Find the 3-d node so that 1-2-3 triangle to be on a hexa face,
1823 // ie that all but meybe one (id3 which is on the same face) nodes
1824 // lay on the same side from the triangle plane.
1826 bool manyInPlane = false; // more than 4 nodes lay in plane
1828 while ( ++iLoop2 < 6 ) {
1830 // get 1-2-3 plane coeffs
1831 Standard_Real A, B, C, D;
1832 gp_Vec N = gp_Vec (P[0], P[1]).Crossed( gp_Vec (P[0], P[2]) );
1833 if ( N.SquareMagnitude() > gp::Resolution() )
1835 gp_Pln pln ( P[0], N );
1836 pln.Coefficients( A, B, C, D );
1838 // find the node (iMin) closest to pln
1839 Standard_Real dist[ 8 ], minDist = DBL_MAX;
1841 for ( i = 3; i < 8; i++ ) {
1842 dist[i] = A * P[i].X() + B * P[i].Y() + C * P[i].Z() + D;
1843 if ( fabs( dist[i] ) < minDist ) {
1844 minDist = fabs( dist[i] );
1847 if ( fabs( dist[i] ) <= tol )
1848 idInPln.insert( idNodes[i] );
1851 // there should not be more than 4 nodes in bottom plane
1852 if ( idInPln.size() > 1 )
1854 DUMPSO( "### idInPln.size() = " << idInPln.size());
1855 // idInPlane does not contain the first 3 nodes
1856 if ( manyInPlane || idInPln.size() == 5)
1857 return false; // all nodes in one plane
1860 // set the 1-st node to be not in plane
1861 for ( i = 3; i < 8; i++ ) {
1862 if ( idInPln.find( idNodes[ i ] ) == idInPln.end() ) {
1863 DUMPSO( "### Reset 0-th node");
1864 swap( 0, i, idNodes, P );
1869 // reset to re-check second nodes
1870 leastDist = DBL_MAX;
1874 break; // from iLoop2;
1877 // check that the other 4 nodes are on the same side
1878 bool sameSide = true;
1879 bool isNeg = dist[ iMin == 3 ? 4 : 3 ] <= 0.;
1880 for ( i = 3; sameSide && i < 8; i++ ) {
1882 sameSide = ( isNeg == dist[i] <= 0.);
1885 // keep best solution
1886 if ( sameSide && minDist < leastDist ) {
1887 leastDist = minDist;
1889 faceNodes.insert( idNodes[ 1 ] );
1890 faceNodes.insert( idNodes[ 2 ] );
1891 faceNodes.insert( idNodes[ iMin ] );
1892 DUMPSO( "loop " << iLoop2 << " id2 " << idNodes[ 1 ] << " id3 " << idNodes[ 2 ]
1893 << " leastDist = " << leastDist);
1894 if ( leastDist <= DBL_MIN )
1899 // set next 3-d node to check
1900 int iNext = 2 + iLoop2;
1902 DUMPSO( "Try 2-nd");
1903 swap ( 2, iNext, idNodes, P );
1905 } // while ( iLoop2 < 6 )
1908 if ( faceNodes.empty() ) return false;
1910 // Put the faceNodes in proper places
1911 for ( i = 4; i < 8; i++ ) {
1912 if ( faceNodes.find( idNodes[ i ] ) != faceNodes.end() ) {
1913 // find a place to put
1915 while ( faceNodes.find( idNodes[ iTo ] ) != faceNodes.end() )
1917 DUMPSO( "Set faceNodes");
1918 swap ( iTo, i, idNodes, P );
1923 // Set nodes of the found bottom face in good order
1924 DUMPSO( " Found bottom face: ");
1925 i = SortQuadNodes( theMesh, idNodes );
1927 gp_Pnt Ptmp = P[ i ];
1932 // for ( int ii = 0; ii < 4; ii++ ) {
1933 // const SMDS_MeshNode *n = theMesh->FindNode( idNodes[ii] );
1934 // DUMPSO( ii << "(" << idNodes[ii] <<") : "<<n->X()<<" "<<n->Y()<<" "<<n->Z());
1937 // Gravity center of the top and bottom faces
1938 gp_Pnt aGCb = ( P[0].XYZ() + P[1].XYZ() + P[2].XYZ() + P[3].XYZ() ) / 4.;
1939 gp_Pnt aGCt = ( P[4].XYZ() + P[5].XYZ() + P[6].XYZ() + P[7].XYZ() ) / 4.;
1941 // Get direction from the bottom to the top face
1942 gp_Vec upDir ( aGCb, aGCt );
1943 Standard_Real upDirSize = upDir.Magnitude();
1944 if ( upDirSize <= gp::Resolution() ) return false;
1947 // Assure that the bottom face normal points up
1948 gp_Vec Nb = gp_Vec (P[0], P[1]).Crossed( gp_Vec (P[0], P[2]) );
1949 Nb += gp_Vec (P[0], P[2]).Crossed( gp_Vec (P[0], P[3]) );
1950 if ( Nb.Dot( upDir ) < 0 ) {
1951 DUMPSO( "Reverse bottom face");
1952 swap( 1, 3, idNodes, P );
1955 // Find 5-th node - the one closest to the 1-st among the last 4 nodes.
1956 Standard_Real minDist = DBL_MAX;
1957 for ( i = 4; i < 8; i++ ) {
1958 // projection of P[i] to the plane defined by P[0] and upDir
1959 gp_Pnt Pp = P[i].Translated( upDir * ( upDir.Dot( gp_Vec( P[i], P[0] ))));
1960 Standard_Real sqDist = P[0].SquareDistance( Pp );
1961 if ( sqDist < minDist ) {
1966 DUMPSO( "Set 4-th");
1967 swap ( 4, iMin, idNodes, P );
1969 // Set nodes of the top face in good order
1970 DUMPSO( "Sort top face");
1971 i = SortQuadNodes( theMesh, &idNodes[4] );
1974 gp_Pnt Ptmp = P[ i ];
1979 // Assure that direction of the top face normal is from the bottom face
1980 gp_Vec Nt = gp_Vec (P[4], P[5]).Crossed( gp_Vec (P[4], P[6]) );
1981 Nt += gp_Vec (P[4], P[6]).Crossed( gp_Vec (P[4], P[7]) );
1982 if ( Nt.Dot( upDir ) < 0 ) {
1983 DUMPSO( "Reverse top face");
1984 swap( 5, 7, idNodes, P );
1987 // DUMPSO( "OUTPUT: ========================================");
1988 // for ( i = 0; i < 8; i++ ) {
1989 // float *p = ugrid->GetPoint(idNodes[i]);
1990 // DUMPSO( i << "(" << idNodes[i] << ") : " << p[0] << " " << p[1] << " " << p[2]);
1996 //================================================================================
1998 * \brief Return nodes linked to the given one
1999 * \param theNode - the node
2000 * \param linkedNodes - the found nodes
2001 * \param type - the type of elements to check
2003 * Medium nodes are ignored
2005 //================================================================================
2007 void SMESH_MeshEditor::GetLinkedNodes( const SMDS_MeshNode* theNode,
2008 TIDSortedElemSet & linkedNodes,
2009 SMDSAbs_ElementType type )
2011 SMDS_ElemIteratorPtr elemIt = theNode->GetInverseElementIterator(type);
2012 while ( elemIt->more() )
2014 const SMDS_MeshElement* elem = elemIt->next();
2015 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
2016 if ( elem->GetType() == SMDSAbs_Volume )
2018 SMDS_VolumeTool vol( elem );
2019 while ( nodeIt->more() ) {
2020 const SMDS_MeshNode* n = cast2Node( nodeIt->next() );
2021 if ( theNode != n && vol.IsLinked( theNode, n ))
2022 linkedNodes.insert( n );
2027 for ( int i = 0; nodeIt->more(); ++i ) {
2028 const SMDS_MeshNode* n = cast2Node( nodeIt->next() );
2029 if ( n == theNode ) {
2030 int iBefore = i - 1;
2032 if ( elem->IsQuadratic() ) {
2033 int nb = elem->NbNodes() / 2;
2034 iAfter = SMESH_MesherHelper::WrapIndex( iAfter, nb );
2035 iBefore = SMESH_MesherHelper::WrapIndex( iBefore, nb );
2037 linkedNodes.insert( elem->GetNode( iAfter ));
2038 linkedNodes.insert( elem->GetNode( iBefore ));
2045 //=======================================================================
2046 //function : laplacianSmooth
2047 //purpose : pulls theNode toward the center of surrounding nodes directly
2048 // connected to that node along an element edge
2049 //=======================================================================
2051 void laplacianSmooth(const SMDS_MeshNode* theNode,
2052 const Handle(Geom_Surface)& theSurface,
2053 map< const SMDS_MeshNode*, gp_XY* >& theUVMap)
2055 // find surrounding nodes
2057 TIDSortedElemSet nodeSet;
2058 SMESH_MeshEditor::GetLinkedNodes( theNode, nodeSet, SMDSAbs_Face );
2060 // compute new coodrs
2062 double coord[] = { 0., 0., 0. };
2063 TIDSortedElemSet::iterator nodeSetIt = nodeSet.begin();
2064 for ( ; nodeSetIt != nodeSet.end(); nodeSetIt++ ) {
2065 const SMDS_MeshNode* node = cast2Node(*nodeSetIt);
2066 if ( theSurface.IsNull() ) { // smooth in 3D
2067 coord[0] += node->X();
2068 coord[1] += node->Y();
2069 coord[2] += node->Z();
2071 else { // smooth in 2D
2072 ASSERT( theUVMap.find( node ) != theUVMap.end() );
2073 gp_XY* uv = theUVMap[ node ];
2074 coord[0] += uv->X();
2075 coord[1] += uv->Y();
2078 int nbNodes = nodeSet.size();
2081 coord[0] /= nbNodes;
2082 coord[1] /= nbNodes;
2084 if ( !theSurface.IsNull() ) {
2085 ASSERT( theUVMap.find( theNode ) != theUVMap.end() );
2086 theUVMap[ theNode ]->SetCoord( coord[0], coord[1] );
2087 gp_Pnt p3d = theSurface->Value( coord[0], coord[1] );
2093 coord[2] /= nbNodes;
2097 const_cast< SMDS_MeshNode* >( theNode )->setXYZ(coord[0],coord[1],coord[2]);
2100 //=======================================================================
2101 //function : centroidalSmooth
2102 //purpose : pulls theNode toward the element-area-weighted centroid of the
2103 // surrounding elements
2104 //=======================================================================
2106 void centroidalSmooth(const SMDS_MeshNode* theNode,
2107 const Handle(Geom_Surface)& theSurface,
2108 map< const SMDS_MeshNode*, gp_XY* >& theUVMap)
2110 gp_XYZ aNewXYZ(0.,0.,0.);
2111 SMESH::Controls::Area anAreaFunc;
2112 double totalArea = 0.;
2117 SMDS_ElemIteratorPtr elemIt = theNode->GetInverseElementIterator(SMDSAbs_Face);
2118 while ( elemIt->more() )
2120 const SMDS_MeshElement* elem = elemIt->next();
2123 gp_XYZ elemCenter(0.,0.,0.);
2124 SMESH::Controls::TSequenceOfXYZ aNodePoints;
2125 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2126 int nn = elem->NbNodes();
2127 if(elem->IsQuadratic()) nn = nn/2;
2129 //while ( itN->more() ) {
2131 const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>( itN->next() );
2133 gp_XYZ aP( aNode->X(), aNode->Y(), aNode->Z() );
2134 aNodePoints.push_back( aP );
2135 if ( !theSurface.IsNull() ) { // smooth in 2D
2136 ASSERT( theUVMap.find( aNode ) != theUVMap.end() );
2137 gp_XY* uv = theUVMap[ aNode ];
2138 aP.SetCoord( uv->X(), uv->Y(), 0. );
2142 double elemArea = anAreaFunc.GetValue( aNodePoints );
2143 totalArea += elemArea;
2145 aNewXYZ += elemCenter * elemArea;
2147 aNewXYZ /= totalArea;
2148 if ( !theSurface.IsNull() ) {
2149 theUVMap[ theNode ]->SetCoord( aNewXYZ.X(), aNewXYZ.Y() );
2150 aNewXYZ = theSurface->Value( aNewXYZ.X(), aNewXYZ.Y() ).XYZ();
2155 const_cast< SMDS_MeshNode* >( theNode )->setXYZ(aNewXYZ.X(),aNewXYZ.Y(),aNewXYZ.Z());
2158 //=======================================================================
2159 //function : getClosestUV
2160 //purpose : return UV of closest projection
2161 //=======================================================================
2163 static bool getClosestUV (Extrema_GenExtPS& projector,
2164 const gp_Pnt& point,
2167 projector.Perform( point );
2168 if ( projector.IsDone() ) {
2169 double u, v, minVal = DBL_MAX;
2170 for ( int i = projector.NbExt(); i > 0; i-- )
2171 if ( projector.Value( i ) < minVal ) {
2172 minVal = projector.Value( i );
2173 projector.Point( i ).Parameter( u, v );
2175 result.SetCoord( u, v );
2181 //=======================================================================
2183 //purpose : Smooth theElements during theNbIterations or until a worst
2184 // element has aspect ratio <= theTgtAspectRatio.
2185 // Aspect Ratio varies in range [1.0, inf].
2186 // If theElements is empty, the whole mesh is smoothed.
2187 // theFixedNodes contains additionally fixed nodes. Nodes built
2188 // on edges and boundary nodes are always fixed.
2189 //=======================================================================
2191 void SMESH_MeshEditor::Smooth (TIDSortedElemSet & theElems,
2192 set<const SMDS_MeshNode*> & theFixedNodes,
2193 const SmoothMethod theSmoothMethod,
2194 const int theNbIterations,
2195 double theTgtAspectRatio,
2198 myLastCreatedElems.Clear();
2199 myLastCreatedNodes.Clear();
2201 MESSAGE((theSmoothMethod==LAPLACIAN ? "LAPLACIAN" : "CENTROIDAL") << "--::Smooth()");
2203 if ( theTgtAspectRatio < 1.0 )
2204 theTgtAspectRatio = 1.0;
2206 const double disttol = 1.e-16;
2208 SMESH::Controls::AspectRatio aQualityFunc;
2210 SMESHDS_Mesh* aMesh = GetMeshDS();
2212 if ( theElems.empty() ) {
2213 // add all faces to theElems
2214 SMDS_FaceIteratorPtr fIt = aMesh->facesIterator();
2215 while ( fIt->more() ) {
2216 const SMDS_MeshElement* face = fIt->next();
2217 theElems.insert( face );
2220 // get all face ids theElems are on
2221 set< int > faceIdSet;
2222 TIDSortedElemSet::iterator itElem;
2224 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
2225 int fId = FindShape( *itElem );
2226 // check that corresponding submesh exists and a shape is face
2228 faceIdSet.find( fId ) == faceIdSet.end() &&
2229 aMesh->MeshElements( fId )) {
2230 TopoDS_Shape F = aMesh->IndexToShape( fId );
2231 if ( !F.IsNull() && F.ShapeType() == TopAbs_FACE )
2232 faceIdSet.insert( fId );
2235 faceIdSet.insert( 0 ); // to smooth elements that are not on any TopoDS_Face
2237 // ===============================================
2238 // smooth elements on each TopoDS_Face separately
2239 // ===============================================
2241 set< int >::reverse_iterator fId = faceIdSet.rbegin(); // treate 0 fId at the end
2242 for ( ; fId != faceIdSet.rend(); ++fId ) {
2243 // get face surface and submesh
2244 Handle(Geom_Surface) surface;
2245 SMESHDS_SubMesh* faceSubMesh = 0;
2247 double fToler2 = 0, vPeriod = 0., uPeriod = 0., f,l;
2248 double u1 = 0, u2 = 0, v1 = 0, v2 = 0;
2249 bool isUPeriodic = false, isVPeriodic = false;
2251 face = TopoDS::Face( aMesh->IndexToShape( *fId ));
2252 surface = BRep_Tool::Surface( face );
2253 faceSubMesh = aMesh->MeshElements( *fId );
2254 fToler2 = BRep_Tool::Tolerance( face );
2255 fToler2 *= fToler2 * 10.;
2256 isUPeriodic = surface->IsUPeriodic();
2258 vPeriod = surface->UPeriod();
2259 isVPeriodic = surface->IsVPeriodic();
2261 uPeriod = surface->VPeriod();
2262 surface->Bounds( u1, u2, v1, v2 );
2264 // ---------------------------------------------------------
2265 // for elements on a face, find movable and fixed nodes and
2266 // compute UV for them
2267 // ---------------------------------------------------------
2268 bool checkBoundaryNodes = false;
2269 bool isQuadratic = false;
2270 set<const SMDS_MeshNode*> setMovableNodes;
2271 map< const SMDS_MeshNode*, gp_XY* > uvMap, uvMap2;
2272 list< gp_XY > listUV; // uvs the 2 uvMaps refer to
2273 list< const SMDS_MeshElement* > elemsOnFace;
2275 Extrema_GenExtPS projector;
2276 GeomAdaptor_Surface surfAdaptor;
2277 if ( !surface.IsNull() ) {
2278 surfAdaptor.Load( surface );
2279 projector.Initialize( surfAdaptor, 20,20, 1e-5,1e-5 );
2281 int nbElemOnFace = 0;
2282 itElem = theElems.begin();
2283 // loop on not yet smoothed elements: look for elems on a face
2284 while ( itElem != theElems.end() ) {
2285 if ( faceSubMesh && nbElemOnFace == faceSubMesh->NbElements() )
2286 break; // all elements found
2288 const SMDS_MeshElement* elem = *itElem;
2289 if ( !elem || elem->GetType() != SMDSAbs_Face || elem->NbNodes() < 3 ||
2290 ( faceSubMesh && !faceSubMesh->Contains( elem ))) {
2294 elemsOnFace.push_back( elem );
2295 theElems.erase( itElem++ );
2299 isQuadratic = elem->IsQuadratic();
2301 // get movable nodes of elem
2302 const SMDS_MeshNode* node;
2303 SMDS_TypeOfPosition posType;
2304 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
2305 int nn = 0, nbn = elem->NbNodes();
2306 if(elem->IsQuadratic())
2308 while ( nn++ < nbn ) {
2309 node = static_cast<const SMDS_MeshNode*>( itN->next() );
2310 const SMDS_PositionPtr& pos = node->GetPosition();
2311 posType = pos.get() ? pos->GetTypeOfPosition() : SMDS_TOP_3DSPACE;
2312 if (posType != SMDS_TOP_EDGE &&
2313 posType != SMDS_TOP_VERTEX &&
2314 theFixedNodes.find( node ) == theFixedNodes.end())
2316 // check if all faces around the node are on faceSubMesh
2317 // because a node on edge may be bound to face
2318 SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator(SMDSAbs_Face);
2320 if ( faceSubMesh ) {
2321 while ( eIt->more() && all ) {
2322 const SMDS_MeshElement* e = eIt->next();
2323 all = faceSubMesh->Contains( e );
2327 setMovableNodes.insert( node );
2329 checkBoundaryNodes = true;
2331 if ( posType == SMDS_TOP_3DSPACE )
2332 checkBoundaryNodes = true;
2335 if ( surface.IsNull() )
2338 // get nodes to check UV
2339 list< const SMDS_MeshNode* > uvCheckNodes;
2340 itN = elem->nodesIterator();
2341 nn = 0; nbn = elem->NbNodes();
2342 if(elem->IsQuadratic())
2344 while ( nn++ < nbn ) {
2345 node = static_cast<const SMDS_MeshNode*>( itN->next() );
2346 if ( uvMap.find( node ) == uvMap.end() )
2347 uvCheckNodes.push_back( node );
2348 // add nodes of elems sharing node
2349 // SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator(SMDSAbs_Face);
2350 // while ( eIt->more() ) {
2351 // const SMDS_MeshElement* e = eIt->next();
2352 // if ( e != elem ) {
2353 // SMDS_ElemIteratorPtr nIt = e->nodesIterator();
2354 // while ( nIt->more() ) {
2355 // const SMDS_MeshNode* n =
2356 // static_cast<const SMDS_MeshNode*>( nIt->next() );
2357 // if ( uvMap.find( n ) == uvMap.end() )
2358 // uvCheckNodes.push_back( n );
2364 list< const SMDS_MeshNode* >::iterator n = uvCheckNodes.begin();
2365 for ( ; n != uvCheckNodes.end(); ++n ) {
2368 const SMDS_PositionPtr& pos = node->GetPosition();
2369 posType = pos.get() ? pos->GetTypeOfPosition() : SMDS_TOP_3DSPACE;
2371 switch ( posType ) {
2372 case SMDS_TOP_FACE: {
2373 SMDS_FacePosition* fPos = ( SMDS_FacePosition* ) pos.get();
2374 uv.SetCoord( fPos->GetUParameter(), fPos->GetVParameter() );
2377 case SMDS_TOP_EDGE: {
2378 TopoDS_Shape S = aMesh->IndexToShape( pos->GetShapeId() );
2379 Handle(Geom2d_Curve) pcurve;
2380 if ( !S.IsNull() && S.ShapeType() == TopAbs_EDGE )
2381 pcurve = BRep_Tool::CurveOnSurface( TopoDS::Edge( S ), face, f,l );
2382 if ( !pcurve.IsNull() ) {
2383 double u = (( SMDS_EdgePosition* ) pos.get() )->GetUParameter();
2384 uv = pcurve->Value( u ).XY();
2388 case SMDS_TOP_VERTEX: {
2389 TopoDS_Shape S = aMesh->IndexToShape( pos->GetShapeId() );
2390 if ( !S.IsNull() && S.ShapeType() == TopAbs_VERTEX )
2391 uv = BRep_Tool::Parameters( TopoDS::Vertex( S ), face ).XY();
2396 // check existing UV
2397 bool project = true;
2398 gp_Pnt pNode ( node->X(), node->Y(), node->Z() );
2399 double dist1 = DBL_MAX, dist2 = 0;
2400 if ( posType != SMDS_TOP_3DSPACE ) {
2401 dist1 = pNode.SquareDistance( surface->Value( uv.X(), uv.Y() ));
2402 project = dist1 > fToler2;
2404 if ( project ) { // compute new UV
2406 if ( !getClosestUV( projector, pNode, newUV )) {
2407 MESSAGE("Node Projection Failed " << node);
2411 newUV.SetX( ElCLib::InPeriod( newUV.X(), u1, u2 ));
2413 newUV.SetY( ElCLib::InPeriod( newUV.Y(), v1, v2 ));
2415 if ( posType != SMDS_TOP_3DSPACE )
2416 dist2 = pNode.SquareDistance( surface->Value( newUV.X(), newUV.Y() ));
2417 if ( dist2 < dist1 )
2421 // store UV in the map
2422 listUV.push_back( uv );
2423 uvMap.insert( make_pair( node, &listUV.back() ));
2425 } // loop on not yet smoothed elements
2427 if ( !faceSubMesh || nbElemOnFace != faceSubMesh->NbElements() )
2428 checkBoundaryNodes = true;
2430 // fix nodes on mesh boundary
2432 if ( checkBoundaryNodes ) {
2433 typedef pair<const SMDS_MeshNode*, const SMDS_MeshNode*> TLink;
2434 map< TLink, int > linkNbMap; // how many times a link encounters in elemsOnFace
2435 map< TLink, int >::iterator link_nb;
2436 // put all elements links to linkNbMap
2437 list< const SMDS_MeshElement* >::iterator elemIt = elemsOnFace.begin();
2438 for ( ; elemIt != elemsOnFace.end(); ++elemIt ) {
2439 const SMDS_MeshElement* elem = (*elemIt);
2440 int nbn = elem->NbNodes();
2441 if(elem->IsQuadratic())
2443 // loop on elem links: insert them in linkNbMap
2444 const SMDS_MeshNode* curNode, *prevNode = elem->GetNode( nbn );
2445 for ( int iN = 0; iN < nbn; ++iN ) {
2446 curNode = elem->GetNode( iN );
2448 if ( curNode < prevNode ) link = make_pair( curNode , prevNode );
2449 else link = make_pair( prevNode , curNode );
2451 link_nb = linkNbMap.find( link );
2452 if ( link_nb == linkNbMap.end() )
2453 linkNbMap.insert( make_pair ( link, 1 ));
2458 // remove nodes that are in links encountered only once from setMovableNodes
2459 for ( link_nb = linkNbMap.begin(); link_nb != linkNbMap.end(); ++link_nb ) {
2460 if ( link_nb->second == 1 ) {
2461 setMovableNodes.erase( link_nb->first.first );
2462 setMovableNodes.erase( link_nb->first.second );
2467 // -----------------------------------------------------
2468 // for nodes on seam edge, compute one more UV ( uvMap2 );
2469 // find movable nodes linked to nodes on seam and which
2470 // are to be smoothed using the second UV ( uvMap2 )
2471 // -----------------------------------------------------
2473 set<const SMDS_MeshNode*> nodesNearSeam; // to smooth using uvMap2
2474 if ( !surface.IsNull() ) {
2475 TopExp_Explorer eExp( face, TopAbs_EDGE );
2476 for ( ; eExp.More(); eExp.Next() ) {
2477 TopoDS_Edge edge = TopoDS::Edge( eExp.Current() );
2478 if ( !BRep_Tool::IsClosed( edge, face ))
2480 SMESHDS_SubMesh* sm = aMesh->MeshElements( edge );
2481 if ( !sm ) continue;
2482 // find out which parameter varies for a node on seam
2485 Handle(Geom2d_Curve) pcurve = BRep_Tool::CurveOnSurface( edge, face, f, l );
2486 if ( pcurve.IsNull() ) continue;
2487 uv1 = pcurve->Value( f );
2489 pcurve = BRep_Tool::CurveOnSurface( edge, face, f, l );
2490 if ( pcurve.IsNull() ) continue;
2491 uv2 = pcurve->Value( f );
2492 int iPar = Abs( uv1.X() - uv2.X() ) > Abs( uv1.Y() - uv2.Y() ) ? 1 : 2;
2494 if ( uv1.Coord( iPar ) > uv2.Coord( iPar )) {
2495 gp_Pnt2d tmp = uv1; uv1 = uv2; uv2 = tmp;
2497 // get nodes on seam and its vertices
2498 list< const SMDS_MeshNode* > seamNodes;
2499 SMDS_NodeIteratorPtr nSeamIt = sm->GetNodes();
2500 while ( nSeamIt->more() ) {
2501 const SMDS_MeshNode* node = nSeamIt->next();
2502 if ( !isQuadratic || !IsMedium( node ))
2503 seamNodes.push_back( node );
2505 TopExp_Explorer vExp( edge, TopAbs_VERTEX );
2506 for ( ; vExp.More(); vExp.Next() ) {
2507 sm = aMesh->MeshElements( vExp.Current() );
2509 nSeamIt = sm->GetNodes();
2510 while ( nSeamIt->more() )
2511 seamNodes.push_back( nSeamIt->next() );
2514 // loop on nodes on seam
2515 list< const SMDS_MeshNode* >::iterator noSeIt = seamNodes.begin();
2516 for ( ; noSeIt != seamNodes.end(); ++noSeIt ) {
2517 const SMDS_MeshNode* nSeam = *noSeIt;
2518 map< const SMDS_MeshNode*, gp_XY* >::iterator n_uv = uvMap.find( nSeam );
2519 if ( n_uv == uvMap.end() )
2522 n_uv->second->SetCoord( iPar, uv1.Coord( iPar ));
2523 // set the second UV
2524 listUV.push_back( *n_uv->second );
2525 listUV.back().SetCoord( iPar, uv2.Coord( iPar ));
2526 if ( uvMap2.empty() )
2527 uvMap2 = uvMap; // copy the uvMap contents
2528 uvMap2[ nSeam ] = &listUV.back();
2530 // collect movable nodes linked to ones on seam in nodesNearSeam
2531 SMDS_ElemIteratorPtr eIt = nSeam->GetInverseElementIterator(SMDSAbs_Face);
2532 while ( eIt->more() ) {
2533 const SMDS_MeshElement* e = eIt->next();
2534 int nbUseMap1 = 0, nbUseMap2 = 0;
2535 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
2536 int nn = 0, nbn = e->NbNodes();
2537 if(e->IsQuadratic()) nbn = nbn/2;
2538 while ( nn++ < nbn )
2540 const SMDS_MeshNode* n =
2541 static_cast<const SMDS_MeshNode*>( nIt->next() );
2543 setMovableNodes.find( n ) == setMovableNodes.end() )
2545 // add only nodes being closer to uv2 than to uv1
2546 gp_Pnt pMid (0.5 * ( n->X() + nSeam->X() ),
2547 0.5 * ( n->Y() + nSeam->Y() ),
2548 0.5 * ( n->Z() + nSeam->Z() ));
2550 getClosestUV( projector, pMid, uv );
2551 if ( uv.Coord( iPar ) > uvMap[ n ]->Coord( iPar ) ) {
2552 nodesNearSeam.insert( n );
2558 // for centroidalSmooth all element nodes must
2559 // be on one side of a seam
2560 if ( theSmoothMethod == CENTROIDAL && nbUseMap1 && nbUseMap2 ) {
2561 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
2563 while ( nn++ < nbn ) {
2564 const SMDS_MeshNode* n =
2565 static_cast<const SMDS_MeshNode*>( nIt->next() );
2566 setMovableNodes.erase( n );
2570 } // loop on nodes on seam
2571 } // loop on edge of a face
2572 } // if ( !face.IsNull() )
2574 if ( setMovableNodes.empty() ) {
2575 MESSAGE( "Face id : " << *fId << " - NO SMOOTHING: no nodes to move!!!");
2576 continue; // goto next face
2584 double maxRatio = -1., maxDisplacement = -1.;
2585 set<const SMDS_MeshNode*>::iterator nodeToMove;
2586 for ( it = 0; it < theNbIterations; it++ ) {
2587 maxDisplacement = 0.;
2588 nodeToMove = setMovableNodes.begin();
2589 for ( ; nodeToMove != setMovableNodes.end(); nodeToMove++ ) {
2590 const SMDS_MeshNode* node = (*nodeToMove);
2591 gp_XYZ aPrevPos ( node->X(), node->Y(), node->Z() );
2594 bool map2 = ( nodesNearSeam.find( node ) != nodesNearSeam.end() );
2595 if ( theSmoothMethod == LAPLACIAN )
2596 laplacianSmooth( node, surface, map2 ? uvMap2 : uvMap );
2598 centroidalSmooth( node, surface, map2 ? uvMap2 : uvMap );
2600 // node displacement
2601 gp_XYZ aNewPos ( node->X(), node->Y(), node->Z() );
2602 Standard_Real aDispl = (aPrevPos - aNewPos).SquareModulus();
2603 if ( aDispl > maxDisplacement )
2604 maxDisplacement = aDispl;
2606 // no node movement => exit
2607 //if ( maxDisplacement < 1.e-16 ) {
2608 if ( maxDisplacement < disttol ) {
2609 MESSAGE("-- no node movement --");
2613 // check elements quality
2615 list< const SMDS_MeshElement* >::iterator elemIt = elemsOnFace.begin();
2616 for ( ; elemIt != elemsOnFace.end(); ++elemIt ) {
2617 const SMDS_MeshElement* elem = (*elemIt);
2618 if ( !elem || elem->GetType() != SMDSAbs_Face )
2620 SMESH::Controls::TSequenceOfXYZ aPoints;
2621 if ( aQualityFunc.GetPoints( elem, aPoints )) {
2622 double aValue = aQualityFunc.GetValue( aPoints );
2623 if ( aValue > maxRatio )
2627 if ( maxRatio <= theTgtAspectRatio ) {
2628 MESSAGE("-- quality achived --");
2631 if (it+1 == theNbIterations) {
2632 MESSAGE("-- Iteration limit exceeded --");
2634 } // smoothing iterations
2636 MESSAGE(" Face id: " << *fId <<
2637 " Nb iterstions: " << it <<
2638 " Displacement: " << maxDisplacement <<
2639 " Aspect Ratio " << maxRatio);
2641 // ---------------------------------------
2642 // new nodes positions are computed,
2643 // record movement in DS and set new UV
2644 // ---------------------------------------
2645 nodeToMove = setMovableNodes.begin();
2646 for ( ; nodeToMove != setMovableNodes.end(); nodeToMove++ ) {
2647 SMDS_MeshNode* node = const_cast< SMDS_MeshNode* > (*nodeToMove);
2648 aMesh->MoveNode( node, node->X(), node->Y(), node->Z() );
2649 map< const SMDS_MeshNode*, gp_XY* >::iterator node_uv = uvMap.find( node );
2650 if ( node_uv != uvMap.end() ) {
2651 gp_XY* uv = node_uv->second;
2653 ( SMDS_PositionPtr( new SMDS_FacePosition( *fId, uv->X(), uv->Y() )));
2657 // move medium nodes of quadratic elements
2660 SMESH_MesherHelper helper( *GetMesh() );
2661 if ( !face.IsNull() )
2662 helper.SetSubShape( face );
2663 list< const SMDS_MeshElement* >::iterator elemIt = elemsOnFace.begin();
2664 for ( ; elemIt != elemsOnFace.end(); ++elemIt ) {
2665 const SMDS_QuadraticFaceOfNodes* QF =
2666 dynamic_cast<const SMDS_QuadraticFaceOfNodes*> (*elemIt);
2668 vector<const SMDS_MeshNode*> Ns;
2669 Ns.reserve(QF->NbNodes()+1);
2670 SMDS_NodeIteratorPtr anIter = QF->interlacedNodesIterator();
2671 while ( anIter->more() )
2672 Ns.push_back( anIter->next() );
2673 Ns.push_back( Ns[0] );
2675 for(int i=0; i<QF->NbNodes(); i=i+2) {
2676 if ( !surface.IsNull() ) {
2677 gp_XY uv1 = helper.GetNodeUV( face, Ns[i], Ns[i+2] );
2678 gp_XY uv2 = helper.GetNodeUV( face, Ns[i+2], Ns[i] );
2679 gp_XY uv = ( uv1 + uv2 ) / 2.;
2680 gp_Pnt xyz = surface->Value( uv.X(), uv.Y() );
2681 x = xyz.X(); y = xyz.Y(); z = xyz.Z();
2684 x = (Ns[i]->X() + Ns[i+2]->X())/2;
2685 y = (Ns[i]->Y() + Ns[i+2]->Y())/2;
2686 z = (Ns[i]->Z() + Ns[i+2]->Z())/2;
2688 if( fabs( Ns[i+1]->X() - x ) > disttol ||
2689 fabs( Ns[i+1]->Y() - y ) > disttol ||
2690 fabs( Ns[i+1]->Z() - z ) > disttol ) {
2691 // we have to move i+1 node
2692 aMesh->MoveNode( Ns[i+1], x, y, z );
2699 } // loop on face ids
2703 //=======================================================================
2704 //function : isReverse
2705 //purpose : Return true if normal of prevNodes is not co-directied with
2706 // gp_Vec(prevNodes[iNotSame],nextNodes[iNotSame]).
2707 // iNotSame is where prevNodes and nextNodes are different
2708 //=======================================================================
2710 static bool isReverse(const SMDS_MeshNode* prevNodes[],
2711 const SMDS_MeshNode* nextNodes[],
2715 int iBeforeNotSame = ( iNotSame == 0 ? nbNodes - 1 : iNotSame - 1 );
2716 int iAfterNotSame = ( iNotSame + 1 == nbNodes ? 0 : iNotSame + 1 );
2718 const SMDS_MeshNode* nB = prevNodes[ iBeforeNotSame ];
2719 const SMDS_MeshNode* nA = prevNodes[ iAfterNotSame ];
2720 const SMDS_MeshNode* nP = prevNodes[ iNotSame ];
2721 const SMDS_MeshNode* nN = nextNodes[ iNotSame ];
2723 gp_Pnt pB ( nB->X(), nB->Y(), nB->Z() );
2724 gp_Pnt pA ( nA->X(), nA->Y(), nA->Z() );
2725 gp_Pnt pP ( nP->X(), nP->Y(), nP->Z() );
2726 gp_Pnt pN ( nN->X(), nN->Y(), nN->Z() );
2728 gp_Vec vB ( pP, pB ), vA ( pP, pA ), vN ( pP, pN );
2730 return (vA ^ vB) * vN < 0.0;
2733 //=======================================================================
2734 //function : sweepElement
2736 //=======================================================================
2738 static void sweepElement(SMESHDS_Mesh* aMesh,
2739 const SMDS_MeshElement* elem,
2740 const vector<TNodeOfNodeListMapItr> & newNodesItVec,
2741 list<const SMDS_MeshElement*>& newElems,
2743 SMESH_SequenceOfElemPtr& myLastCreatedElems)
2745 // Loop on elem nodes:
2746 // find new nodes and detect same nodes indices
2747 int nbNodes = elem->NbNodes();
2749 //list<const SMDS_MeshNode*>::const_iterator itNN[ nbNodes ];
2750 vector < list< const SMDS_MeshNode* >::const_iterator > itNN;
2751 itNN.reserve(nbNodes);
2753 const SMDS_MeshNode* prevNod[ nbNodes ], *nextNod[ nbNodes ], *midlNod[ nbNodes ];
2754 int iNode, nbSame = 0, iNotSameNode = 0, iSameNode = 0;
2755 vector<int> sames(nbNodes);
2757 bool issimple[nbNodes];
2759 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
2760 TNodeOfNodeListMapItr nnIt = newNodesItVec[ iNode ];
2761 const SMDS_MeshNode* node = nnIt->first;
2762 const list< const SMDS_MeshNode* > & listNewNodes = nnIt->second;
2763 if ( listNewNodes.empty() )
2766 if(listNewNodes.size()==nbSteps) {
2767 issimple[iNode] = true;
2770 issimple[iNode] = false;
2773 itNN[ iNode ] = listNewNodes.begin();
2774 prevNod[ iNode ] = node;
2775 nextNod[ iNode ] = listNewNodes.front();
2776 //cout<<"iNode="<<iNode<<endl;
2777 //cout<<" prevNod[iNode]="<< prevNod[iNode]<<" nextNod[iNode]="<< nextNod[iNode]<<endl;
2778 if ( prevNod[ iNode ] != nextNod [ iNode ])
2779 iNotSameNode = iNode;
2783 sames[nbSame++] = iNode;
2786 //cout<<"1 nbSame="<<nbSame<<endl;
2787 if ( nbSame == nbNodes || nbSame > 2) {
2788 MESSAGE( " Too many same nodes of element " << elem->GetID() );
2792 // if( elem->IsQuadratic() && nbSame>0 ) {
2793 // MESSAGE( "Can not rotate quadratic element " << elem->GetID() );
2797 int iBeforeSame = 0, iAfterSame = 0, iOpposSame = 0;
2799 iBeforeSame = ( iSameNode == 0 ? nbNodes - 1 : iSameNode - 1 );
2800 iAfterSame = ( iSameNode + 1 == nbNodes ? 0 : iSameNode + 1 );
2801 iOpposSame = ( iSameNode - 2 < 0 ? iSameNode + 2 : iSameNode - 2 );
2805 //cout<<" prevNod[0]="<< prevNod[0]<<" prevNod[1]="<< prevNod[1]
2806 // <<" prevNod[2]="<< prevNod[2]<<" prevNod[3]="<< prevNod[4]
2807 // <<" prevNod[4]="<< prevNod[4]<<" prevNod[5]="<< prevNod[5]
2808 // <<" prevNod[6]="<< prevNod[6]<<" prevNod[7]="<< prevNod[7]<<endl;
2810 // check element orientation
2812 if ( nbNodes > 2 && !isReverse( prevNod, nextNod, nbNodes, iNotSameNode )) {
2813 //MESSAGE("Reversed elem " << elem );
2817 int iAB = iAfterSame + iBeforeSame;
2818 iBeforeSame = iAB - iBeforeSame;
2819 iAfterSame = iAB - iAfterSame;
2823 // make new elements
2824 int iStep;//, nbSteps = newNodesItVec[ 0 ]->second.size();
2825 for (iStep = 0; iStep < nbSteps; iStep++ ) {
2827 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
2828 if(issimple[iNode]) {
2829 nextNod[ iNode ] = *itNN[ iNode ];
2833 if( elem->GetType()==SMDSAbs_Node ) {
2834 // we have to use two nodes
2835 midlNod[ iNode ] = *itNN[ iNode ];
2837 nextNod[ iNode ] = *itNN[ iNode ];
2840 else if(!elem->IsQuadratic() ||
2841 elem->IsQuadratic() && elem->IsMediumNode(prevNod[iNode]) ) {
2842 // we have to use each second node
2844 nextNod[ iNode ] = *itNN[ iNode ];
2848 // we have to use two nodes
2849 midlNod[ iNode ] = *itNN[ iNode ];
2851 nextNod[ iNode ] = *itNN[ iNode ];
2856 SMDS_MeshElement* aNewElem = 0;
2857 if(!elem->IsPoly()) {
2858 switch ( nbNodes ) {
2862 if ( nbSame == 0 ) {
2864 aNewElem = aMesh->AddEdge( prevNod[ 0 ], nextNod[ 0 ] );
2866 aNewElem = aMesh->AddEdge( prevNod[ 0 ], nextNod[ 0 ], midlNod[ 0 ] );
2872 aNewElem = aMesh->AddFace(prevNod[ 0 ], prevNod[ 1 ],
2873 nextNod[ 1 ], nextNod[ 0 ] );
2875 aNewElem = aMesh->AddFace(prevNod[ 0 ], prevNod[ 1 ],
2876 nextNod[ iNotSameNode ] );
2880 case 3: { // TRIANGLE or quadratic edge
2881 if(elem->GetType() == SMDSAbs_Face) { // TRIANGLE
2883 if ( nbSame == 0 ) // --- pentahedron
2884 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ],
2885 nextNod[ i0 ], nextNod[ 1 ], nextNod[ i2 ] );
2887 else if ( nbSame == 1 ) // --- pyramid
2888 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iAfterSame ],
2889 nextNod[ iAfterSame ], nextNod[ iBeforeSame ],
2890 nextNod[ iSameNode ]);
2892 else // 2 same nodes: --- tetrahedron
2893 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ],
2894 nextNod[ iNotSameNode ]);
2896 else { // quadratic edge
2897 if(nbSame==0) { // quadratic quadrangle
2898 aNewElem = aMesh->AddFace(prevNod[0], nextNod[0], nextNod[1], prevNod[1],
2899 midlNod[0], nextNod[2], midlNod[1], prevNod[2]);
2901 else if(nbSame==1) { // quadratic triangle
2903 return; // medium node on axis
2904 else if(sames[0]==0) {
2905 aNewElem = aMesh->AddFace(prevNod[0], nextNod[1], prevNod[1],
2906 nextNod[2], midlNod[1], prevNod[2]);
2908 else { // sames[0]==1
2909 aNewElem = aMesh->AddFace(prevNod[0], nextNod[0], prevNod[1],
2910 midlNod[0], nextNod[2], prevNod[2]);
2918 case 4: { // QUADRANGLE
2920 if ( nbSame == 0 ) // --- hexahedron
2921 aNewElem = aMesh->AddVolume (prevNod[ i0 ], prevNod[ 1 ], prevNod[ i2 ], prevNod[ 3 ],
2922 nextNod[ i0 ], nextNod[ 1 ], nextNod[ i2 ], nextNod[ 3 ]);
2924 else if ( nbSame == 1 ) { // --- pyramid + pentahedron
2925 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iAfterSame ],
2926 nextNod[ iAfterSame ], nextNod[ iBeforeSame ],
2927 nextNod[ iSameNode ]);
2928 newElems.push_back( aNewElem );
2929 aNewElem = aMesh->AddVolume (prevNod[ iAfterSame ], prevNod[ iOpposSame ],
2930 prevNod[ iBeforeSame ], nextNod[ iAfterSame ],
2931 nextNod[ iOpposSame ], nextNod[ iBeforeSame ] );
2933 else if ( nbSame == 2 ) { // pentahedron
2934 if ( prevNod[ iBeforeSame ] == nextNod[ iBeforeSame ] )
2935 // iBeforeSame is same too
2936 aNewElem = aMesh->AddVolume (prevNod[ iBeforeSame ], prevNod[ iOpposSame ],
2937 nextNod[ iOpposSame ], prevNod[ iSameNode ],
2938 prevNod[ iAfterSame ], nextNod[ iAfterSame ]);
2940 // iAfterSame is same too
2941 aNewElem = aMesh->AddVolume (prevNod[ iSameNode ], prevNod[ iBeforeSame ],
2942 nextNod[ iBeforeSame ], prevNod[ iAfterSame ],
2943 prevNod[ iOpposSame ], nextNod[ iOpposSame ]);
2947 case 6: { // quadratic triangle
2948 // create pentahedron with 15 nodes
2949 if(i0>0) { // reversed case
2950 aNewElem = aMesh->AddVolume (prevNod[0], prevNod[2], prevNod[1],
2951 nextNod[0], nextNod[2], nextNod[1],
2952 prevNod[5], prevNod[4], prevNod[3],
2953 nextNod[5], nextNod[4], nextNod[3],
2954 midlNod[0], midlNod[2], midlNod[1]);
2956 else { // not reversed case
2957 aNewElem = aMesh->AddVolume (prevNod[0], prevNod[1], prevNod[2],
2958 nextNod[0], nextNod[1], nextNod[2],
2959 prevNod[3], prevNod[4], prevNod[5],
2960 nextNod[3], nextNod[4], nextNod[5],
2961 midlNod[0], midlNod[1], midlNod[2]);
2965 case 8: { // quadratic quadrangle
2966 // create hexahedron with 20 nodes
2967 if(i0>0) { // reversed case
2968 aNewElem = aMesh->AddVolume (prevNod[0], prevNod[3], prevNod[2], prevNod[1],
2969 nextNod[0], nextNod[3], nextNod[2], nextNod[1],
2970 prevNod[7], prevNod[6], prevNod[5], prevNod[4],
2971 nextNod[7], nextNod[6], nextNod[5], nextNod[4],
2972 midlNod[0], midlNod[3], midlNod[2], midlNod[1]);
2974 else { // not reversed case
2975 aNewElem = aMesh->AddVolume (prevNod[0], prevNod[1], prevNod[2], prevNod[3],
2976 nextNod[0], nextNod[1], nextNod[2], nextNod[3],
2977 prevNod[4], prevNod[5], prevNod[6], prevNod[7],
2978 nextNod[4], nextNod[5], nextNod[6], nextNod[7],
2979 midlNod[0], midlNod[1], midlNod[2], midlNod[3]);
2984 // realized for extrusion only
2985 //vector<const SMDS_MeshNode*> polyedre_nodes (nbNodes*2 + 4*nbNodes);
2986 //vector<int> quantities (nbNodes + 2);
2988 //quantities[0] = nbNodes; // bottom of prism
2989 //for (int inode = 0; inode < nbNodes; inode++) {
2990 // polyedre_nodes[inode] = prevNod[inode];
2993 //quantities[1] = nbNodes; // top of prism
2994 //for (int inode = 0; inode < nbNodes; inode++) {
2995 // polyedre_nodes[nbNodes + inode] = nextNod[inode];
2998 //for (int iface = 0; iface < nbNodes; iface++) {
2999 // quantities[iface + 2] = 4;
3000 // int inextface = (iface == nbNodes - 1) ? 0 : iface + 1;
3001 // polyedre_nodes[2*nbNodes + 4*iface + 0] = prevNod[iface];
3002 // polyedre_nodes[2*nbNodes + 4*iface + 1] = prevNod[inextface];
3003 // polyedre_nodes[2*nbNodes + 4*iface + 2] = nextNod[inextface];
3004 // polyedre_nodes[2*nbNodes + 4*iface + 3] = nextNod[iface];
3006 //aNewElem = aMesh->AddPolyhedralVolume (polyedre_nodes, quantities);
3013 // realized for extrusion only
3014 vector<const SMDS_MeshNode*> polyedre_nodes (nbNodes*2 + 4*nbNodes);
3015 vector<int> quantities (nbNodes + 2);
3017 quantities[0] = nbNodes; // bottom of prism
3018 for (int inode = 0; inode < nbNodes; inode++) {
3019 polyedre_nodes[inode] = prevNod[inode];
3022 quantities[1] = nbNodes; // top of prism
3023 for (int inode = 0; inode < nbNodes; inode++) {
3024 polyedre_nodes[nbNodes + inode] = nextNod[inode];
3027 for (int iface = 0; iface < nbNodes; iface++) {
3028 quantities[iface + 2] = 4;
3029 int inextface = (iface == nbNodes - 1) ? 0 : iface + 1;
3030 polyedre_nodes[2*nbNodes + 4*iface + 0] = prevNod[iface];
3031 polyedre_nodes[2*nbNodes + 4*iface + 1] = prevNod[inextface];
3032 polyedre_nodes[2*nbNodes + 4*iface + 2] = nextNod[inextface];
3033 polyedre_nodes[2*nbNodes + 4*iface + 3] = nextNod[iface];
3035 aNewElem = aMesh->AddPolyhedralVolume (polyedre_nodes, quantities);
3039 newElems.push_back( aNewElem );
3040 myLastCreatedElems.Append(aNewElem);
3043 // set new prev nodes
3044 for ( iNode = 0; iNode < nbNodes; iNode++ )
3045 prevNod[ iNode ] = nextNod[ iNode ];
3050 //=======================================================================
3051 //function : makeWalls
3052 //purpose : create 1D and 2D elements around swept elements
3053 //=======================================================================
3055 static void makeWalls (SMESHDS_Mesh* aMesh,
3056 TNodeOfNodeListMap & mapNewNodes,
3057 TElemOfElemListMap & newElemsMap,
3058 TElemOfVecOfNnlmiMap & elemNewNodesMap,
3059 TIDSortedElemSet& elemSet,
3061 SMESH_SequenceOfElemPtr& myLastCreatedElems)
3063 ASSERT( newElemsMap.size() == elemNewNodesMap.size() );
3065 // Find nodes belonging to only one initial element - sweep them to get edges.
3067 TNodeOfNodeListMapItr nList = mapNewNodes.begin();
3068 for ( ; nList != mapNewNodes.end(); nList++ ) {
3069 const SMDS_MeshNode* node =
3070 static_cast<const SMDS_MeshNode*>( nList->first );
3071 SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator();
3072 int nbInitElems = 0;
3073 const SMDS_MeshElement* el = 0;
3074 SMDSAbs_ElementType highType = SMDSAbs_Edge; // count most complex elements only
3075 while ( eIt->more() && nbInitElems < 2 ) {
3077 SMDSAbs_ElementType type = el->GetType();
3078 if ( type == SMDSAbs_Volume || type < highType ) continue;
3079 if ( type > highType ) {
3083 if ( elemSet.find(el) != elemSet.end() )
3086 if ( nbInitElems < 2 ) {
3087 bool NotCreateEdge = el && el->IsQuadratic() && el->IsMediumNode(node);
3088 if(!NotCreateEdge) {
3089 vector<TNodeOfNodeListMapItr> newNodesItVec( 1, nList );
3090 list<const SMDS_MeshElement*> newEdges;
3091 sweepElement( aMesh, node, newNodesItVec, newEdges, nbSteps, myLastCreatedElems );
3096 // Make a ceiling for each element ie an equal element of last new nodes.
3097 // Find free links of faces - make edges and sweep them into faces.
3099 TElemOfElemListMap::iterator itElem = newElemsMap.begin();
3100 TElemOfVecOfNnlmiMap::iterator itElemNodes = elemNewNodesMap.begin();
3101 for ( ; itElem != newElemsMap.end(); itElem++, itElemNodes++ ) {
3102 const SMDS_MeshElement* elem = itElem->first;
3103 vector<TNodeOfNodeListMapItr>& vecNewNodes = itElemNodes->second;
3105 if ( elem->GetType() == SMDSAbs_Edge ) {
3106 // create a ceiling edge
3107 if (!elem->IsQuadratic()) {
3108 if ( !aMesh->FindEdge( vecNewNodes[ 0 ]->second.back(),
3109 vecNewNodes[ 1 ]->second.back()))
3110 myLastCreatedElems.Append(aMesh->AddEdge(vecNewNodes[ 0 ]->second.back(),
3111 vecNewNodes[ 1 ]->second.back()));
3114 if ( !aMesh->FindEdge( vecNewNodes[ 0 ]->second.back(),
3115 vecNewNodes[ 1 ]->second.back(),
3116 vecNewNodes[ 2 ]->second.back()))
3117 myLastCreatedElems.Append(aMesh->AddEdge(vecNewNodes[ 0 ]->second.back(),
3118 vecNewNodes[ 1 ]->second.back(),
3119 vecNewNodes[ 2 ]->second.back()));
3122 if ( elem->GetType() != SMDSAbs_Face )
3125 if(itElem->second.size()==0) continue;
3127 bool hasFreeLinks = false;
3129 TIDSortedElemSet avoidSet;
3130 avoidSet.insert( elem );
3132 set<const SMDS_MeshNode*> aFaceLastNodes;
3133 int iNode, nbNodes = vecNewNodes.size();
3134 if(!elem->IsQuadratic()) {
3135 // loop on the face nodes
3136 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
3137 aFaceLastNodes.insert( vecNewNodes[ iNode ]->second.back() );
3138 // look for free links of the face
3139 int iNext = ( iNode + 1 == nbNodes ) ? 0 : iNode + 1;
3140 const SMDS_MeshNode* n1 = vecNewNodes[ iNode ]->first;
3141 const SMDS_MeshNode* n2 = vecNewNodes[ iNext ]->first;
3142 // check if a link is free
3143 if ( ! SMESH_MeshEditor::FindFaceInSet ( n1, n2, elemSet, avoidSet )) {
3144 hasFreeLinks = true;
3145 // make an edge and a ceiling for a new edge
3146 if ( !aMesh->FindEdge( n1, n2 )) {
3147 myLastCreatedElems.Append(aMesh->AddEdge( n1, n2 ));
3149 n1 = vecNewNodes[ iNode ]->second.back();
3150 n2 = vecNewNodes[ iNext ]->second.back();
3151 if ( !aMesh->FindEdge( n1, n2 )) {
3152 myLastCreatedElems.Append(aMesh->AddEdge( n1, n2 ));
3157 else { // elem is quadratic face
3158 int nbn = nbNodes/2;
3159 for ( iNode = 0; iNode < nbn; iNode++ ) {
3160 aFaceLastNodes.insert( vecNewNodes[ iNode ]->second.back() );
3161 int iNext = ( iNode + 1 == nbn ) ? 0 : iNode + 1;
3162 const SMDS_MeshNode* n1 = vecNewNodes[ iNode ]->first;
3163 const SMDS_MeshNode* n2 = vecNewNodes[ iNext ]->first;
3164 // check if a link is free
3165 if ( ! SMESH_MeshEditor::FindFaceInSet ( n1, n2, elemSet, avoidSet )) {
3166 hasFreeLinks = true;
3167 // make an edge and a ceiling for a new edge
3169 const SMDS_MeshNode* n3 = vecNewNodes[ iNode+nbn ]->first;
3170 if ( !aMesh->FindEdge( n1, n2, n3 )) {
3171 myLastCreatedElems.Append(aMesh->AddEdge( n1, n2, n3 ));
3173 n1 = vecNewNodes[ iNode ]->second.back();
3174 n2 = vecNewNodes[ iNext ]->second.back();
3175 n3 = vecNewNodes[ iNode+nbn ]->second.back();
3176 if ( !aMesh->FindEdge( n1, n2, n3 )) {
3177 myLastCreatedElems.Append(aMesh->AddEdge( n1, n2, n3 ));
3181 for ( iNode = nbn; iNode < 2*nbn; iNode++ ) {
3182 aFaceLastNodes.insert( vecNewNodes[ iNode ]->second.back() );
3186 // sweep free links into faces
3188 if ( hasFreeLinks ) {
3189 list<const SMDS_MeshElement*> & newVolumes = itElem->second;
3190 int iStep; //, nbSteps = vecNewNodes[0]->second.size();
3191 int iVol, volNb, nbVolumesByStep = newVolumes.size() / nbSteps;
3193 set<const SMDS_MeshNode*> initNodeSet, faceNodeSet;
3194 for ( iNode = 0; iNode < nbNodes; iNode++ )
3195 initNodeSet.insert( vecNewNodes[ iNode ]->first );
3197 for ( volNb = 0; volNb < nbVolumesByStep; volNb++ ) {
3198 list<const SMDS_MeshElement*>::iterator v = newVolumes.begin();
3200 while ( iVol++ < volNb ) v++;
3201 // find indices of free faces of a volume
3203 SMDS_VolumeTool vTool( *v );
3204 int iF, nbF = vTool.NbFaces();
3205 for ( iF = 0; iF < nbF; iF ++ ) {
3206 if (vTool.IsFreeFace( iF ) &&
3207 vTool.GetFaceNodes( iF, faceNodeSet ) &&
3208 initNodeSet != faceNodeSet) // except an initial face
3209 fInd.push_back( iF );
3214 // create faces for all steps
3215 // if such a face has been already created by sweep of edge, assure that its orientation is OK
3216 for ( iStep = 0; iStep < nbSteps; iStep++ ) {
3218 vTool.SetExternalNormal();
3219 list< int >::iterator ind = fInd.begin();
3220 for ( ; ind != fInd.end(); ind++ ) {
3221 const SMDS_MeshNode** nodes = vTool.GetFaceNodes( *ind );
3222 int nbn = vTool.NbFaceNodes( *ind );
3224 case 3: { ///// triangle
3225 const SMDS_MeshFace * f = aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ]);
3227 myLastCreatedElems.Append(aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] ));
3228 else if ( nodes[ 1 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3229 aMesh->ChangeElementNodes( f, nodes, nbn );
3232 case 4: { ///// quadrangle
3233 const SMDS_MeshFace * f = aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ]);
3235 myLastCreatedElems.Append(aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ] ));
3236 else if ( nodes[ 1 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3237 aMesh->ChangeElementNodes( f, nodes, nbn );
3241 if( (*v)->IsQuadratic() ) {
3242 if(nbn==6) { /////// quadratic triangle
3243 const SMDS_MeshFace * f = aMesh->FindFace( nodes[0], nodes[2], nodes[4],
3244 nodes[1], nodes[3], nodes[5] );
3246 myLastCreatedElems.Append(aMesh->AddFace(nodes[0], nodes[2], nodes[4],
3247 nodes[1], nodes[3], nodes[5]));
3248 else if ( nodes[ 2 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3249 aMesh->ChangeElementNodes( f, nodes, nbn );
3251 else { /////// quadratic quadrangle
3252 const SMDS_MeshFace * f = aMesh->FindFace( nodes[0], nodes[2], nodes[4], nodes[6],
3253 nodes[1], nodes[3], nodes[5], nodes[7] );
3255 myLastCreatedElems.Append(aMesh->AddFace(nodes[0], nodes[2], nodes[4], nodes[6],
3256 nodes[1], nodes[3], nodes[5], nodes[7]));
3257 else if ( nodes[ 2 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3258 aMesh->ChangeElementNodes( f, nodes, nbn );
3261 else { //////// polygon
3262 vector<const SMDS_MeshNode*> polygon_nodes ( nodes, &nodes[nbn] );
3263 const SMDS_MeshFace * f = aMesh->FindFace( polygon_nodes );
3265 myLastCreatedElems.Append(aMesh->AddPolygonalFace(polygon_nodes));
3266 else if ( nodes[ 1 ] != f->GetNode( f->GetNodeIndex( nodes[ 0 ] ) + 1 ))
3267 aMesh->ChangeElementNodes( f, nodes, nbn );
3271 // go to the next volume
3273 while ( iVol++ < nbVolumesByStep ) v++;
3276 } // sweep free links into faces
3278 // make a ceiling face with a normal external to a volume
3280 SMDS_VolumeTool lastVol( itElem->second.back() );
3282 int iF = lastVol.GetFaceIndex( aFaceLastNodes );
3284 lastVol.SetExternalNormal();
3285 const SMDS_MeshNode** nodes = lastVol.GetFaceNodes( iF );
3286 int nbn = lastVol.NbFaceNodes( iF );
3289 if (!hasFreeLinks ||
3290 !aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ]))
3291 myLastCreatedElems.Append(aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ] ));
3294 if (!hasFreeLinks ||
3295 !aMesh->FindFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ]))
3296 myLastCreatedElems.Append(aMesh->AddFace( nodes[ 0 ], nodes[ 1 ], nodes[ 2 ], nodes[ 3 ] ));
3299 if(itElem->second.back()->IsQuadratic()) {
3301 if (!hasFreeLinks ||
3302 !aMesh->FindFace(nodes[0], nodes[2], nodes[4],
3303 nodes[1], nodes[3], nodes[5]) ) {
3304 myLastCreatedElems.Append(aMesh->AddFace(nodes[0], nodes[2], nodes[4],
3305 nodes[1], nodes[3], nodes[5]));
3309 if (!hasFreeLinks ||
3310 !aMesh->FindFace(nodes[0], nodes[2], nodes[4], nodes[6],
3311 nodes[1], nodes[3], nodes[5], nodes[7]) )
3312 myLastCreatedElems.Append(aMesh->AddFace(nodes[0], nodes[2], nodes[4], nodes[6],
3313 nodes[1], nodes[3], nodes[5], nodes[7]));
3317 vector<const SMDS_MeshNode*> polygon_nodes ( nodes, &nodes[nbn] );
3318 if (!hasFreeLinks || !aMesh->FindFace(polygon_nodes))
3319 myLastCreatedElems.Append(aMesh->AddPolygonalFace(polygon_nodes));
3323 } // loop on swept elements
3326 //=======================================================================
3327 //function : RotationSweep
3329 //=======================================================================
3331 void SMESH_MeshEditor::RotationSweep(TIDSortedElemSet & theElems,
3332 const gp_Ax1& theAxis,
3333 const double theAngle,
3334 const int theNbSteps,
3335 const double theTol,
3336 const bool theMakeWalls)
3338 myLastCreatedElems.Clear();
3339 myLastCreatedNodes.Clear();
3341 MESSAGE( "RotationSweep()");
3343 aTrsf.SetRotation( theAxis, theAngle );
3345 aTrsf2.SetRotation( theAxis, theAngle/2. );
3347 gp_Lin aLine( theAxis );
3348 double aSqTol = theTol * theTol;
3350 SMESHDS_Mesh* aMesh = GetMeshDS();
3352 TNodeOfNodeListMap mapNewNodes;
3353 TElemOfVecOfNnlmiMap mapElemNewNodes;
3354 TElemOfElemListMap newElemsMap;
3357 TIDSortedElemSet::iterator itElem;
3358 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
3359 const SMDS_MeshElement* elem = *itElem;
3360 if ( !elem || elem->GetType() == SMDSAbs_Volume )
3362 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
3363 newNodesItVec.reserve( elem->NbNodes() );
3365 // loop on elem nodes
3366 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
3367 while ( itN->more() ) {
3369 // check if a node has been already sweeped
3370 const SMDS_MeshNode* node =
3371 static_cast<const SMDS_MeshNode*>( itN->next() );
3372 TNodeOfNodeListMapItr nIt = mapNewNodes.find( node );
3373 if ( nIt == mapNewNodes.end() ) {
3374 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
3375 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
3378 gp_XYZ aXYZ( node->X(), node->Y(), node->Z() );
3380 aXYZ.Coord( coord[0], coord[1], coord[2] );
3381 bool isOnAxis = ( aLine.SquareDistance( aXYZ ) <= aSqTol );
3382 const SMDS_MeshNode * newNode = node;
3383 for ( int i = 0; i < theNbSteps; i++ ) {
3385 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3387 aTrsf2.Transforms( coord[0], coord[1], coord[2] );
3388 //aTrsf.Transforms( coord[0], coord[1], coord[2] );
3389 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3390 myLastCreatedNodes.Append(newNode);
3391 listNewNodes.push_back( newNode );
3392 aTrsf2.Transforms( coord[0], coord[1], coord[2] );
3393 //aTrsf.Transforms( coord[0], coord[1], coord[2] );
3396 aTrsf.Transforms( coord[0], coord[1], coord[2] );
3398 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3399 myLastCreatedNodes.Append(newNode);
3401 listNewNodes.push_back( newNode );
3405 // if current elem is quadratic and current node is not medium
3406 // we have to check - may be it is needed to insert additional nodes
3407 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3408 list< const SMDS_MeshNode* > & listNewNodes = nIt->second;
3409 if(listNewNodes.size()==theNbSteps) {
3410 listNewNodes.clear();
3412 gp_XYZ aXYZ( node->X(), node->Y(), node->Z() );
3414 aXYZ.Coord( coord[0], coord[1], coord[2] );
3415 const SMDS_MeshNode * newNode = node;
3416 for(int i = 0; i<theNbSteps; i++) {
3417 aTrsf2.Transforms( coord[0], coord[1], coord[2] );
3418 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3419 myLastCreatedNodes.Append(newNode);
3420 listNewNodes.push_back( newNode );
3421 aTrsf2.Transforms( coord[0], coord[1], coord[2] );
3422 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3423 myLastCreatedNodes.Append(newNode);
3424 listNewNodes.push_back( newNode );
3429 newNodesItVec.push_back( nIt );
3431 // make new elements
3432 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem], theNbSteps, myLastCreatedElems );
3436 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes,
3437 theElems, theNbSteps, myLastCreatedElems );
3441 //=======================================================================
3442 //function : CreateNode
3444 //=======================================================================
3445 const SMDS_MeshNode* SMESH_MeshEditor::CreateNode(const double x,
3448 const double tolnode,
3449 SMESH_SequenceOfNode& aNodes)
3451 myLastCreatedElems.Clear();
3452 myLastCreatedNodes.Clear();
3455 SMESHDS_Mesh * aMesh = myMesh->GetMeshDS();
3457 // try to search in sequence of existing nodes
3458 // if aNodes.Length()>0 we 'nave to use given sequence
3459 // else - use all nodes of mesh
3460 if(aNodes.Length()>0) {
3462 for(i=1; i<=aNodes.Length(); i++) {
3463 gp_Pnt P2(aNodes.Value(i)->X(),aNodes.Value(i)->Y(),aNodes.Value(i)->Z());
3464 if(P1.Distance(P2)<tolnode)
3465 return aNodes.Value(i);
3469 SMDS_NodeIteratorPtr itn = aMesh->nodesIterator();
3470 while(itn->more()) {
3471 const SMDS_MeshNode* aN = static_cast<const SMDS_MeshNode*> (itn->next());
3472 gp_Pnt P2(aN->X(),aN->Y(),aN->Z());
3473 if(P1.Distance(P2)<tolnode)
3478 // create new node and return it
3479 const SMDS_MeshNode* NewNode = aMesh->AddNode(x,y,z);
3480 myLastCreatedNodes.Append(NewNode);
3485 //=======================================================================
3486 //function : ExtrusionSweep
3488 //=======================================================================
3490 void SMESH_MeshEditor::ExtrusionSweep (TIDSortedElemSet & theElems,
3491 const gp_Vec& theStep,
3492 const int theNbSteps,
3493 TElemOfElemListMap& newElemsMap,
3495 const double theTolerance)
3497 ExtrusParam aParams;
3498 aParams.myDir = gp_Dir(theStep);
3499 aParams.myNodes.Clear();
3500 aParams.mySteps = new TColStd_HSequenceOfReal;
3502 for(i=1; i<=theNbSteps; i++)
3503 aParams.mySteps->Append(theStep.Magnitude());
3505 ExtrusionSweep(theElems,aParams,newElemsMap,theFlags,theTolerance);
3510 //=======================================================================
3511 //function : ExtrusionSweep
3513 //=======================================================================
3515 void SMESH_MeshEditor::ExtrusionSweep (TIDSortedElemSet & theElems,
3516 ExtrusParam& theParams,
3517 TElemOfElemListMap& newElemsMap,
3519 const double theTolerance)
3521 myLastCreatedElems.Clear();
3522 myLastCreatedNodes.Clear();
3524 SMESHDS_Mesh* aMesh = GetMeshDS();
3526 int nbsteps = theParams.mySteps->Length();
3528 TNodeOfNodeListMap mapNewNodes;
3529 //TNodeOfNodeVecMap mapNewNodes;
3530 TElemOfVecOfNnlmiMap mapElemNewNodes;
3531 //TElemOfVecOfMapNodesMap mapElemNewNodes;
3534 TIDSortedElemSet::iterator itElem;
3535 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
3536 // check element type
3537 const SMDS_MeshElement* elem = *itElem;
3538 if ( !elem || elem->GetType() == SMDSAbs_Volume )
3541 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
3542 //vector<TNodeOfNodeVecMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
3543 newNodesItVec.reserve( elem->NbNodes() );
3545 // loop on elem nodes
3546 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
3547 while ( itN->more() ) {
3549 // check if a node has been already sweeped
3550 const SMDS_MeshNode* node =
3551 static_cast<const SMDS_MeshNode*>( itN->next() );
3552 TNodeOfNodeListMap::iterator nIt = mapNewNodes.find( node );
3553 //TNodeOfNodeVecMap::iterator nIt = mapNewNodes.find( node );
3554 if ( nIt == mapNewNodes.end() ) {
3555 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
3556 //nIt = mapNewNodes.insert( make_pair( node, vector<const SMDS_MeshNode*>() )).first;
3557 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
3558 //vector<const SMDS_MeshNode*>& vecNewNodes = nIt->second;
3559 //vecNewNodes.reserve(nbsteps);
3562 double coord[] = { node->X(), node->Y(), node->Z() };
3563 //int nbsteps = theParams.mySteps->Length();
3564 for ( int i = 0; i < nbsteps; i++ ) {
3565 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3566 // create additional node
3567 double x = coord[0] + theParams.myDir.X()*theParams.mySteps->Value(i+1)/2.;
3568 double y = coord[1] + theParams.myDir.Y()*theParams.mySteps->Value(i+1)/2.;
3569 double z = coord[2] + theParams.myDir.Z()*theParams.mySteps->Value(i+1)/2.;
3570 if( theFlags & EXTRUSION_FLAG_SEW ) {
3571 const SMDS_MeshNode * newNode = CreateNode(x, y, z,
3572 theTolerance, theParams.myNodes);
3573 listNewNodes.push_back( newNode );
3576 const SMDS_MeshNode * newNode = aMesh->AddNode(x, y, z);
3577 myLastCreatedNodes.Append(newNode);
3578 listNewNodes.push_back( newNode );
3581 //aTrsf.Transforms( coord[0], coord[1], coord[2] );
3582 coord[0] = coord[0] + theParams.myDir.X()*theParams.mySteps->Value(i+1);
3583 coord[1] = coord[1] + theParams.myDir.Y()*theParams.mySteps->Value(i+1);
3584 coord[2] = coord[2] + theParams.myDir.Z()*theParams.mySteps->Value(i+1);
3585 if( theFlags & EXTRUSION_FLAG_SEW ) {
3586 const SMDS_MeshNode * newNode = CreateNode(coord[0], coord[1], coord[2],
3587 theTolerance, theParams.myNodes);
3588 listNewNodes.push_back( newNode );
3589 //vecNewNodes[i]=newNode;
3592 const SMDS_MeshNode * newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3593 myLastCreatedNodes.Append(newNode);
3594 listNewNodes.push_back( newNode );
3595 //vecNewNodes[i]=newNode;
3600 // if current elem is quadratic and current node is not medium
3601 // we have to check - may be it is needed to insert additional nodes
3602 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3603 list< const SMDS_MeshNode* > & listNewNodes = nIt->second;
3604 if(listNewNodes.size()==nbsteps) {
3605 listNewNodes.clear();
3606 double coord[] = { node->X(), node->Y(), node->Z() };
3607 for ( int i = 0; i < nbsteps; i++ ) {
3608 double x = coord[0] + theParams.myDir.X()*theParams.mySteps->Value(i+1);
3609 double y = coord[1] + theParams.myDir.Y()*theParams.mySteps->Value(i+1);
3610 double z = coord[2] + theParams.myDir.Z()*theParams.mySteps->Value(i+1);
3611 if( theFlags & EXTRUSION_FLAG_SEW ) {
3612 const SMDS_MeshNode * newNode = CreateNode(x, y, z,
3613 theTolerance, theParams.myNodes);
3614 listNewNodes.push_back( newNode );
3617 const SMDS_MeshNode * newNode = aMesh->AddNode(x, y, z);
3618 myLastCreatedNodes.Append(newNode);
3619 listNewNodes.push_back( newNode );
3621 coord[0] = coord[0] + theParams.myDir.X()*theParams.mySteps->Value(i+1);
3622 coord[1] = coord[1] + theParams.myDir.Y()*theParams.mySteps->Value(i+1);
3623 coord[2] = coord[2] + theParams.myDir.Z()*theParams.mySteps->Value(i+1);
3624 if( theFlags & EXTRUSION_FLAG_SEW ) {
3625 const SMDS_MeshNode * newNode = CreateNode(coord[0], coord[1], coord[2],
3626 theTolerance, theParams.myNodes);
3627 listNewNodes.push_back( newNode );
3630 const SMDS_MeshNode * newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
3631 myLastCreatedNodes.Append(newNode);
3632 listNewNodes.push_back( newNode );
3638 newNodesItVec.push_back( nIt );
3640 // make new elements
3641 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem], nbsteps, myLastCreatedElems );
3644 if( theFlags & EXTRUSION_FLAG_BOUNDARY ) {
3645 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElems, nbsteps, myLastCreatedElems );
3650 //=======================================================================
3651 //class : SMESH_MeshEditor_PathPoint
3652 //purpose : auxiliary class
3653 //=======================================================================
3654 class SMESH_MeshEditor_PathPoint {
3656 SMESH_MeshEditor_PathPoint() {
3657 myPnt.SetCoord(99., 99., 99.);
3658 myTgt.SetCoord(1.,0.,0.);
3662 void SetPnt(const gp_Pnt& aP3D){
3665 void SetTangent(const gp_Dir& aTgt){
3668 void SetAngle(const double& aBeta){
3671 void SetParameter(const double& aPrm){
3674 const gp_Pnt& Pnt()const{
3677 const gp_Dir& Tangent()const{
3680 double Angle()const{
3683 double Parameter()const{
3694 //=======================================================================
3695 //function : ExtrusionAlongTrack
3697 //=======================================================================
3698 SMESH_MeshEditor::Extrusion_Error
3699 SMESH_MeshEditor::ExtrusionAlongTrack (TIDSortedElemSet & theElements,
3700 SMESH_subMesh* theTrack,
3701 const SMDS_MeshNode* theN1,
3702 const bool theHasAngles,
3703 list<double>& theAngles,
3704 const bool theHasRefPoint,
3705 const gp_Pnt& theRefPoint)
3707 myLastCreatedElems.Clear();
3708 myLastCreatedNodes.Clear();
3710 MESSAGE("SMESH_MeshEditor::ExtrusionAlongTrack")
3711 int j, aNbTP, aNbE, aNb;
3712 double aT1, aT2, aT, aAngle, aX, aY, aZ;
3713 std::list<double> aPrms;
3714 std::list<double>::iterator aItD;
3715 TIDSortedElemSet::iterator itElem;
3717 Standard_Real aTx1, aTx2, aL2, aTolVec, aTolVec2;
3721 Handle(Geom_Curve) aC3D;
3722 TopoDS_Edge aTrackEdge;
3723 TopoDS_Vertex aV1, aV2;
3725 SMDS_ElemIteratorPtr aItE;
3726 SMDS_NodeIteratorPtr aItN;
3727 SMDSAbs_ElementType aTypeE;
3729 TNodeOfNodeListMap mapNewNodes;
3730 TElemOfVecOfNnlmiMap mapElemNewNodes;
3731 TElemOfElemListMap newElemsMap;
3734 aTolVec2=aTolVec*aTolVec;
3737 aNbE = theElements.size();
3740 return EXTR_NO_ELEMENTS;
3742 // 1.1 Track Pattern
3745 SMESHDS_SubMesh* pSubMeshDS=theTrack->GetSubMeshDS();
3747 aItE = pSubMeshDS->GetElements();
3748 while ( aItE->more() ) {
3749 const SMDS_MeshElement* pE = aItE->next();
3750 aTypeE = pE->GetType();
3751 // Pattern must contain links only
3752 if ( aTypeE != SMDSAbs_Edge )
3753 return EXTR_PATH_NOT_EDGE;
3756 const TopoDS_Shape& aS = theTrack->GetSubShape();
3757 // Sub shape for the Pattern must be an Edge
3758 if ( aS.ShapeType() != TopAbs_EDGE )
3759 return EXTR_BAD_PATH_SHAPE;
3761 aTrackEdge = TopoDS::Edge( aS );
3762 // the Edge must not be degenerated
3763 if ( BRep_Tool::Degenerated( aTrackEdge ) )
3764 return EXTR_BAD_PATH_SHAPE;
3766 TopExp::Vertices( aTrackEdge, aV1, aV2 );
3767 aT1=BRep_Tool::Parameter( aV1, aTrackEdge );
3768 aT2=BRep_Tool::Parameter( aV2, aTrackEdge );
3770 aItN = theTrack->GetFather()->GetSubMesh( aV1 )->GetSubMeshDS()->GetNodes();
3771 const SMDS_MeshNode* aN1 = aItN->next();
3773 aItN = theTrack->GetFather()->GetSubMesh( aV2 )->GetSubMeshDS()->GetNodes();
3774 const SMDS_MeshNode* aN2 = aItN->next();
3776 // starting node must be aN1 or aN2
3777 if ( !( aN1 == theN1 || aN2 == theN1 ) )
3778 return EXTR_BAD_STARTING_NODE;
3780 aNbTP = pSubMeshDS->NbNodes() + 2;
3783 vector<double> aAngles( aNbTP );
3785 for ( j=0; j < aNbTP; ++j ) {
3789 if ( theHasAngles ) {
3790 aItD = theAngles.begin();
3791 for ( j=1; (aItD != theAngles.end()) && (j<aNbTP); ++aItD, ++j ) {
3793 aAngles[j] = aAngle;
3797 // 2. Collect parameters on the track edge
3798 aPrms.push_back( aT1 );
3799 aPrms.push_back( aT2 );
3801 aItN = pSubMeshDS->GetNodes();
3802 while ( aItN->more() ) {
3803 const SMDS_MeshNode* pNode = aItN->next();
3804 const SMDS_EdgePosition* pEPos =
3805 static_cast<const SMDS_EdgePosition*>( pNode->GetPosition().get() );
3806 aT = pEPos->GetUParameter();
3807 aPrms.push_back( aT );
3812 if ( aN1 == theN1 ) {
3824 SMESH_MeshEditor_PathPoint aPP;
3825 vector<SMESH_MeshEditor_PathPoint> aPPs( aNbTP );
3827 aC3D = BRep_Tool::Curve( aTrackEdge, aTx1, aTx2 );
3829 aItD = aPrms.begin();
3830 for ( j=0; aItD != aPrms.end(); ++aItD, ++j ) {
3832 aC3D->D1( aT, aP3D, aVec );
3833 aL2 = aVec.SquareMagnitude();
3834 if ( aL2 < aTolVec2 )
3835 return EXTR_CANT_GET_TANGENT;
3837 gp_Dir aTgt( aVec );
3838 aAngle = aAngles[j];
3841 aPP.SetTangent( aTgt );
3842 aPP.SetAngle( aAngle );
3843 aPP.SetParameter( aT );
3847 // 3. Center of rotation aV0
3849 if ( !theHasRefPoint ) {
3851 aGC.SetCoord( 0.,0.,0. );
3853 itElem = theElements.begin();
3854 for ( ; itElem != theElements.end(); itElem++ ) {
3855 const SMDS_MeshElement* elem = *itElem;
3857 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
3858 while ( itN->more() ) {
3859 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( itN->next() );
3864 if ( mapNewNodes.find( node ) == mapNewNodes.end() ) {
3865 list<const SMDS_MeshNode*> aLNx;
3866 mapNewNodes[node] = aLNx;
3868 gp_XYZ aXYZ( aX, aY, aZ );
3876 } // if (!theHasRefPoint) {
3877 mapNewNodes.clear();
3879 // 4. Processing the elements
3880 SMESHDS_Mesh* aMesh = GetMeshDS();
3882 for ( itElem = theElements.begin(); itElem != theElements.end(); itElem++ ) {
3883 // check element type
3884 const SMDS_MeshElement* elem = *itElem;
3885 aTypeE = elem->GetType();
3886 if ( !elem || ( aTypeE != SMDSAbs_Face && aTypeE != SMDSAbs_Edge ) )
3889 vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
3890 newNodesItVec.reserve( elem->NbNodes() );
3892 // loop on elem nodes
3893 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
3894 while ( itN->more() ) {
3896 // check if a node has been already processed
3897 const SMDS_MeshNode* node =
3898 static_cast<const SMDS_MeshNode*>( itN->next() );
3899 TNodeOfNodeListMap::iterator nIt = mapNewNodes.find( node );
3900 if ( nIt == mapNewNodes.end() ) {
3901 nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
3902 list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
3905 aX = node->X(); aY = node->Y(); aZ = node->Z();
3907 Standard_Real aAngle1x, aAngleT1T0, aTolAng;
3908 gp_Pnt aP0x, aP1x, aPN0, aPN1, aV0x, aV1x;
3909 gp_Ax1 anAx1, anAxT1T0;
3910 gp_Dir aDT1x, aDT0x, aDT1T0;
3915 aPN0.SetCoord(aX, aY, aZ);
3917 const SMESH_MeshEditor_PathPoint& aPP0 = aPPs[0];
3919 aDT0x= aPP0.Tangent();
3921 for ( j = 1; j < aNbTP; ++j ) {
3922 const SMESH_MeshEditor_PathPoint& aPP1 = aPPs[j];
3924 aDT1x = aPP1.Tangent();
3925 aAngle1x = aPP1.Angle();
3927 gp_Trsf aTrsf, aTrsfRot, aTrsfRotT1T0;
3929 gp_Vec aV01x( aP0x, aP1x );
3930 aTrsf.SetTranslation( aV01x );
3933 aV1x = aV0x.Transformed( aTrsf );
3934 aPN1 = aPN0.Transformed( aTrsf );
3936 // rotation 1 [ T1,T0 ]
3937 aAngleT1T0=-aDT1x.Angle( aDT0x );
3938 if (fabs(aAngleT1T0) > aTolAng) {
3940 anAxT1T0.SetLocation( aV1x );
3941 anAxT1T0.SetDirection( aDT1T0 );
3942 aTrsfRotT1T0.SetRotation( anAxT1T0, aAngleT1T0 );
3944 aPN1 = aPN1.Transformed( aTrsfRotT1T0 );
3948 if ( theHasAngles ) {
3949 anAx1.SetLocation( aV1x );
3950 anAx1.SetDirection( aDT1x );
3951 aTrsfRot.SetRotation( anAx1, aAngle1x );
3953 aPN1 = aPN1.Transformed( aTrsfRot );
3957 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3958 // create additional node
3959 double x = ( aPN1.X() + aPN0.X() )/2.;
3960 double y = ( aPN1.Y() + aPN0.Y() )/2.;
3961 double z = ( aPN1.Z() + aPN0.Z() )/2.;
3962 const SMDS_MeshNode* newNode = aMesh->AddNode(x,y,z);
3963 myLastCreatedNodes.Append(newNode);
3964 listNewNodes.push_back( newNode );
3969 const SMDS_MeshNode* newNode = aMesh->AddNode( aX, aY, aZ );
3970 myLastCreatedNodes.Append(newNode);
3971 listNewNodes.push_back( newNode );
3981 // if current elem is quadratic and current node is not medium
3982 // we have to check - may be it is needed to insert additional nodes
3983 if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
3984 list< const SMDS_MeshNode* > & listNewNodes = nIt->second;
3985 if(listNewNodes.size()==aNbTP-1) {
3986 vector<const SMDS_MeshNode*> aNodes(2*(aNbTP-1));
3987 gp_XYZ P(node->X(), node->Y(), node->Z());
3988 list< const SMDS_MeshNode* >::iterator it = listNewNodes.begin();
3990 for(i=0; i<aNbTP-1; i++) {
3991 const SMDS_MeshNode* N = *it;
3992 double x = ( N->X() + P.X() )/2.;
3993 double y = ( N->Y() + P.Y() )/2.;
3994 double z = ( N->Z() + P.Z() )/2.;
3995 const SMDS_MeshNode* newN = aMesh->AddNode(x,y,z);
3996 myLastCreatedNodes.Append(newN);
3999 P = gp_XYZ(N->X(),N->Y(),N->Z());
4001 listNewNodes.clear();
4002 for(i=0; i<2*(aNbTP-1); i++) {
4003 listNewNodes.push_back(aNodes[i]);
4009 newNodesItVec.push_back( nIt );
4011 // make new elements
4012 //sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem],
4013 // newNodesItVec[0]->second.size(), myLastCreatedElems );
4014 sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem],
4015 aNbTP-1, myLastCreatedElems );
4018 makeWalls( aMesh, mapNewNodes, newElemsMap, mapElemNewNodes, theElements,
4019 aNbTP-1, myLastCreatedElems );
4024 //=======================================================================
4025 //function : Transform
4027 //=======================================================================
4029 void SMESH_MeshEditor::Transform (TIDSortedElemSet & theElems,
4030 const gp_Trsf& theTrsf,
4033 myLastCreatedElems.Clear();
4034 myLastCreatedNodes.Clear();
4037 switch ( theTrsf.Form() ) {
4043 needReverse = false;
4046 SMESHDS_Mesh* aMesh = GetMeshDS();
4048 // map old node to new one
4049 TNodeNodeMap nodeMap;
4051 // elements sharing moved nodes; those of them which have all
4052 // nodes mirrored but are not in theElems are to be reversed
4053 TIDSortedElemSet inverseElemSet;
4056 TIDSortedElemSet::iterator itElem;
4057 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
4058 const SMDS_MeshElement* elem = *itElem;
4062 // loop on elem nodes
4063 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
4064 while ( itN->more() ) {
4066 // check if a node has been already transformed
4067 const SMDS_MeshNode* node =
4068 static_cast<const SMDS_MeshNode*>( itN->next() );
4069 if (nodeMap.find( node ) != nodeMap.end() )
4073 coord[0] = node->X();
4074 coord[1] = node->Y();
4075 coord[2] = node->Z();
4076 theTrsf.Transforms( coord[0], coord[1], coord[2] );
4077 const SMDS_MeshNode * newNode = node;
4079 newNode = aMesh->AddNode( coord[0], coord[1], coord[2] );
4080 myLastCreatedNodes.Append(newNode);
4083 aMesh->MoveNode( node, coord[0], coord[1], coord[2] );
4084 // node position on shape becomes invalid
4085 const_cast< SMDS_MeshNode* > ( node )->SetPosition
4086 ( SMDS_SpacePosition::originSpacePosition() );
4088 nodeMap.insert( TNodeNodeMap::value_type( node, newNode ));
4090 // keep inverse elements
4091 if ( !theCopy && needReverse ) {
4092 SMDS_ElemIteratorPtr invElemIt = node->GetInverseElementIterator();
4093 while ( invElemIt->more() ) {
4094 const SMDS_MeshElement* iel = invElemIt->next();
4095 inverseElemSet.insert( iel );
4101 // either new elements are to be created
4102 // or a mirrored element are to be reversed
4103 if ( !theCopy && !needReverse)
4106 if ( !inverseElemSet.empty()) {
4107 TIDSortedElemSet::iterator invElemIt = inverseElemSet.begin();
4108 for ( ; invElemIt != inverseElemSet.end(); invElemIt++ )
4109 theElems.insert( *invElemIt );
4112 // replicate or reverse elements
4115 REV_TETRA = 0, // = nbNodes - 4
4116 REV_PYRAMID = 1, // = nbNodes - 4
4117 REV_PENTA = 2, // = nbNodes - 4
4119 REV_HEXA = 4, // = nbNodes - 4
4123 { 2, 1, 0, 3, 4, 0, 0, 0 }, // REV_TETRA
4124 { 2, 1, 0, 3, 4, 0, 0, 0 }, // REV_PYRAMID
4125 { 2, 1, 0, 5, 4, 3, 0, 0 }, // REV_PENTA
4126 { 2, 1, 0, 3, 0, 0, 0, 0 }, // REV_FACE
4127 { 2, 1, 0, 3, 6, 5, 4, 7 }, // REV_HEXA
4128 { 0, 1, 2, 3, 4, 5, 6, 7 } // FORWARD
4131 for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ ) {
4132 const SMDS_MeshElement* elem = *itElem;
4133 if ( !elem || elem->GetType() == SMDSAbs_Node )
4136 int nbNodes = elem->NbNodes();
4137 int elemType = elem->GetType();
4139 if (elem->IsPoly()) {
4140 // Polygon or Polyhedral Volume
4141 switch ( elemType ) {
4144 vector<const SMDS_MeshNode*> poly_nodes (nbNodes);
4146 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
4147 while (itN->more()) {
4148 const SMDS_MeshNode* node =
4149 static_cast<const SMDS_MeshNode*>(itN->next());
4150 TNodeNodeMap::iterator nodeMapIt = nodeMap.find(node);
4151 if (nodeMapIt == nodeMap.end())
4152 break; // not all nodes transformed
4154 // reverse mirrored faces and volumes
4155 poly_nodes[nbNodes - iNode - 1] = (*nodeMapIt).second;
4157 poly_nodes[iNode] = (*nodeMapIt).second;
4161 if ( iNode != nbNodes )
4162 continue; // not all nodes transformed
4165 myLastCreatedElems.Append(aMesh->AddPolygonalFace(poly_nodes));
4168 aMesh->ChangePolygonNodes(elem, poly_nodes);
4172 case SMDSAbs_Volume:
4174 // ATTENTION: Reversing is not yet done!!!
4175 const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
4176 (const SMDS_PolyhedralVolumeOfNodes*) elem;
4178 MESSAGE("Warning: bad volumic element");
4182 vector<const SMDS_MeshNode*> poly_nodes;
4183 vector<int> quantities;
4185 bool allTransformed = true;
4186 int nbFaces = aPolyedre->NbFaces();
4187 for (int iface = 1; iface <= nbFaces && allTransformed; iface++) {
4188 int nbFaceNodes = aPolyedre->NbFaceNodes(iface);
4189 for (int inode = 1; inode <= nbFaceNodes && allTransformed; inode++) {
4190 const SMDS_MeshNode* node = aPolyedre->GetFaceNode(iface, inode);
4191 TNodeNodeMap::iterator nodeMapIt = nodeMap.find(node);
4192 if (nodeMapIt == nodeMap.end()) {
4193 allTransformed = false; // not all nodes transformed
4195 poly_nodes.push_back((*nodeMapIt).second);
4198 quantities.push_back(nbFaceNodes);
4200 if ( !allTransformed )
4201 continue; // not all nodes transformed
4204 myLastCreatedElems.Append(aMesh->AddPolyhedralVolume(poly_nodes, quantities));
4207 aMesh->ChangePolyhedronNodes(elem, poly_nodes, quantities);
4217 int* i = index[ FORWARD ];
4218 if ( needReverse && nbNodes > 2) // reverse mirrored faces and volumes
4219 if ( elemType == SMDSAbs_Face )
4220 i = index[ REV_FACE ];
4222 i = index[ nbNodes - 4 ];
4224 if(elem->IsQuadratic()) {
4225 static int anIds[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
4228 if(nbNodes==3) { // quadratic edge
4229 static int anIds[] = {1,0,2};
4232 else if(nbNodes==6) { // quadratic triangle
4233 static int anIds[] = {0,2,1,5,4,3};
4236 else if(nbNodes==8) { // quadratic quadrangle
4237 static int anIds[] = {0,3,2,1,7,6,5,4};
4240 else if(nbNodes==10) { // quadratic tetrahedron of 10 nodes
4241 static int anIds[] = {0,2,1,3,6,5,4,7,9,8};
4244 else if(nbNodes==13) { // quadratic pyramid of 13 nodes
4245 static int anIds[] = {0,3,2,1,4,8,7,6,5,9,12,11,10};
4248 else if(nbNodes==15) { // quadratic pentahedron with 15 nodes
4249 static int anIds[] = {0,2,1,3,5,4,8,7,6,11,10,9,12,14,13};
4252 else { // nbNodes==20 - quadratic hexahedron with 20 nodes
4253 static int anIds[] = {0,3,2,1,4,7,6,5,11,10,9,8,15,14,13,12,16,19,18,17};
4259 // find transformed nodes
4260 vector<const SMDS_MeshNode*> nodes(nbNodes);
4262 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
4263 while ( itN->more() ) {
4264 const SMDS_MeshNode* node =
4265 static_cast<const SMDS_MeshNode*>( itN->next() );
4266 TNodeNodeMap::iterator nodeMapIt = nodeMap.find( node );
4267 if ( nodeMapIt == nodeMap.end() )
4268 break; // not all nodes transformed
4269 nodes[ i [ iNode++ ]] = (*nodeMapIt).second;
4271 if ( iNode != nbNodes )
4272 continue; // not all nodes transformed
4275 if ( SMDS_MeshElement* copy = AddElement( nodes, elem->GetType(), elem->IsPoly() ))
4276 myLastCreatedElems.Append( copy );
4280 // reverse element as it was reversed by transformation
4282 aMesh->ChangeElementNodes( elem, &nodes[0], nbNodes );
4287 //=======================================================================
4288 //function : FindCoincidentNodes
4289 //purpose : Return list of group of nodes close to each other within theTolerance
4290 // Search among theNodes or in the whole mesh if theNodes is empty using
4291 // an Octree algorithm
4292 //=======================================================================
4294 void SMESH_MeshEditor::FindCoincidentNodes (set<const SMDS_MeshNode*> & theNodes,
4295 const double theTolerance,
4296 TListOfListOfNodes & theGroupsOfNodes)
4298 myLastCreatedElems.Clear();
4299 myLastCreatedNodes.Clear();
4301 set<const SMDS_MeshNode*> nodes;
4302 if ( theNodes.empty() )
4303 { // get all nodes in the mesh
4304 SMDS_NodeIteratorPtr nIt = GetMeshDS()->nodesIterator();
4305 while ( nIt->more() )
4306 nodes.insert( nodes.end(),nIt->next());
4310 SMESH_OctreeNode::FindCoincidentNodes ( nodes, &theGroupsOfNodes, theTolerance);
4314 //=======================================================================
4316 * \brief Implementation of search for the node closest to point
4318 //=======================================================================
4320 struct SMESH_NodeSearcherImpl: public SMESH_NodeSearcher
4323 * \brief Constructor
4325 SMESH_NodeSearcherImpl( const SMESHDS_Mesh* theMesh )
4327 set<const SMDS_MeshNode*> nodes;
4329 SMDS_NodeIteratorPtr nIt = theMesh->nodesIterator();
4330 while ( nIt->more() )
4331 nodes.insert( nodes.end(), nIt->next() );
4333 myOctreeNode = new SMESH_OctreeNode(nodes) ;
4336 * \brief Do it's job
4338 const SMDS_MeshNode* FindClosestTo( const gp_Pnt& thePnt )
4340 SMDS_MeshNode tgtNode( thePnt.X(), thePnt.Y(), thePnt.Z() );
4341 list<const SMDS_MeshNode*> nodes;
4342 const double precision = 1e-6;
4343 myOctreeNode->NodesAround( &tgtNode, &nodes, precision );
4345 double minSqDist = DBL_MAX;
4347 if ( nodes.empty() ) // get all nodes of OctreeNode's closest to thePnt
4349 // sort leafs by their distance from thePnt
4350 typedef map< double, SMESH_OctreeNode* > TDistTreeMap;
4351 TDistTreeMap treeMap;
4352 list< SMESH_OctreeNode* > treeList;
4353 list< SMESH_OctreeNode* >::iterator trIt;
4354 treeList.push_back( myOctreeNode );
4355 for ( trIt = treeList.begin(); trIt != treeList.end(); ++trIt)
4357 SMESH_OctreeNode* tree = *trIt;
4358 if ( !tree->isLeaf() ) { // put children to the queue
4359 SMESH_OctreeNodeIteratorPtr cIt = tree->GetChildrenIterator();
4360 while ( cIt->more() )
4361 treeList.push_back( cIt->next() );
4363 else if ( tree->NbNodes() ) { // put tree to treeMap
4364 tree->getBox( box );
4365 double sqDist = thePnt.SquareDistance( 0.5 * ( box.CornerMin() + box.CornerMax() ));
4366 pair<TDistTreeMap::iterator,bool> it_in = treeMap.insert( make_pair( sqDist, tree ));
4367 if ( !it_in.second ) // not unique distance to box center
4368 treeMap.insert( it_in.first, make_pair( sqDist - 1e-13*treeMap.size(), tree ));
4371 // find distance after which there is no sense to check tree's
4372 double sqLimit = DBL_MAX;
4373 TDistTreeMap::iterator sqDist_tree = treeMap.begin();
4374 if ( treeMap.size() > 5 ) {
4375 SMESH_OctreeNode* closestTree = sqDist_tree->second;
4376 closestTree->getBox( box );
4377 double limit = sqrt( sqDist_tree->first ) + sqrt ( box.SquareExtent() );
4378 sqLimit = limit * limit;
4380 // get all nodes from trees
4381 for ( ; sqDist_tree != treeMap.end(); ++sqDist_tree) {
4382 if ( sqDist_tree->first > sqLimit )
4384 SMESH_OctreeNode* tree = sqDist_tree->second;
4385 tree->NodesAround( tree->GetNodeIterator()->next(), &nodes );
4388 // find closest among nodes
4389 minSqDist = DBL_MAX;
4390 const SMDS_MeshNode* closestNode = 0;
4391 list<const SMDS_MeshNode*>::iterator nIt = nodes.begin();
4392 for ( ; nIt != nodes.end(); ++nIt ) {
4393 double sqDist = thePnt.SquareDistance( TNodeXYZ( *nIt ) );
4394 if ( minSqDist > sqDist ) {
4404 ~SMESH_NodeSearcherImpl() { delete myOctreeNode; }
4406 SMESH_OctreeNode* myOctreeNode;
4409 //=======================================================================
4411 * \brief Return SMESH_NodeSearcher
4413 //=======================================================================
4415 SMESH_NodeSearcher* SMESH_MeshEditor::GetNodeSearcher()
4417 return new SMESH_NodeSearcherImpl( GetMeshDS() );
4420 //=======================================================================
4421 //function : SimplifyFace
4423 //=======================================================================
4424 int SMESH_MeshEditor::SimplifyFace (const vector<const SMDS_MeshNode *> faceNodes,
4425 vector<const SMDS_MeshNode *>& poly_nodes,
4426 vector<int>& quantities) const
4428 int nbNodes = faceNodes.size();
4433 set<const SMDS_MeshNode*> nodeSet;
4435 // get simple seq of nodes
4436 const SMDS_MeshNode* simpleNodes[ nbNodes ];
4437 int iSimple = 0, nbUnique = 0;
4439 simpleNodes[iSimple++] = faceNodes[0];
4441 for (int iCur = 1; iCur < nbNodes; iCur++) {
4442 if (faceNodes[iCur] != simpleNodes[iSimple - 1]) {
4443 simpleNodes[iSimple++] = faceNodes[iCur];
4444 if (nodeSet.insert( faceNodes[iCur] ).second)
4448 int nbSimple = iSimple;
4449 if (simpleNodes[nbSimple - 1] == simpleNodes[0]) {
4459 bool foundLoop = (nbSimple > nbUnique);
4462 set<const SMDS_MeshNode*> loopSet;
4463 for (iSimple = 0; iSimple < nbSimple && !foundLoop; iSimple++) {
4464 const SMDS_MeshNode* n = simpleNodes[iSimple];
4465 if (!loopSet.insert( n ).second) {
4469 int iC = 0, curLast = iSimple;
4470 for (; iC < curLast; iC++) {
4471 if (simpleNodes[iC] == n) break;
4473 int loopLen = curLast - iC;
4475 // create sub-element
4477 quantities.push_back(loopLen);
4478 for (; iC < curLast; iC++) {
4479 poly_nodes.push_back(simpleNodes[iC]);
4482 // shift the rest nodes (place from the first loop position)
4483 for (iC = curLast + 1; iC < nbSimple; iC++) {
4484 simpleNodes[iC - loopLen] = simpleNodes[iC];
4486 nbSimple -= loopLen;
4489 } // for (iSimple = 0; iSimple < nbSimple; iSimple++)
4490 } // while (foundLoop)
4494 quantities.push_back(iSimple);
4495 for (int i = 0; i < iSimple; i++)
4496 poly_nodes.push_back(simpleNodes[i]);
4502 //=======================================================================
4503 //function : MergeNodes
4504 //purpose : In each group, the cdr of nodes are substituted by the first one
4506 //=======================================================================
4508 void SMESH_MeshEditor::MergeNodes (TListOfListOfNodes & theGroupsOfNodes)
4510 myLastCreatedElems.Clear();
4511 myLastCreatedNodes.Clear();
4513 SMESHDS_Mesh* aMesh = GetMeshDS();
4515 TNodeNodeMap nodeNodeMap; // node to replace - new node
4516 set<const SMDS_MeshElement*> elems; // all elements with changed nodes
4517 list< int > rmElemIds, rmNodeIds;
4519 // Fill nodeNodeMap and elems
4521 TListOfListOfNodes::iterator grIt = theGroupsOfNodes.begin();
4522 for ( ; grIt != theGroupsOfNodes.end(); grIt++ ) {
4523 list<const SMDS_MeshNode*>& nodes = *grIt;
4524 list<const SMDS_MeshNode*>::iterator nIt = nodes.begin();
4525 const SMDS_MeshNode* nToKeep = *nIt;
4526 for ( ++nIt; nIt != nodes.end(); nIt++ ) {
4527 const SMDS_MeshNode* nToRemove = *nIt;
4528 nodeNodeMap.insert( TNodeNodeMap::value_type( nToRemove, nToKeep ));
4529 if ( nToRemove != nToKeep ) {
4530 rmNodeIds.push_back( nToRemove->GetID() );
4531 AddToSameGroups( nToKeep, nToRemove, aMesh );
4534 SMDS_ElemIteratorPtr invElemIt = nToRemove->GetInverseElementIterator();
4535 while ( invElemIt->more() ) {
4536 const SMDS_MeshElement* elem = invElemIt->next();
4541 // Change element nodes or remove an element
4543 set<const SMDS_MeshElement*>::iterator eIt = elems.begin();
4544 for ( ; eIt != elems.end(); eIt++ ) {
4545 const SMDS_MeshElement* elem = *eIt;
4546 int nbNodes = elem->NbNodes();
4547 int aShapeId = FindShape( elem );
4549 set<const SMDS_MeshNode*> nodeSet;
4550 const SMDS_MeshNode* curNodes[ nbNodes ], *uniqueNodes[ nbNodes ];
4551 int iUnique = 0, iCur = 0, nbRepl = 0, iRepl [ nbNodes ];
4553 // get new seq of nodes
4554 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
4555 while ( itN->more() ) {
4556 const SMDS_MeshNode* n =
4557 static_cast<const SMDS_MeshNode*>( itN->next() );
4559 TNodeNodeMap::iterator nnIt = nodeNodeMap.find( n );
4560 if ( nnIt != nodeNodeMap.end() ) { // n sticks
4562 iRepl[ nbRepl++ ] = iCur;
4564 curNodes[ iCur ] = n;
4565 bool isUnique = nodeSet.insert( n ).second;
4567 uniqueNodes[ iUnique++ ] = n;
4571 // Analyse element topology after replacement
4574 int nbUniqueNodes = nodeSet.size();
4575 if ( nbNodes != nbUniqueNodes ) { // some nodes stick
4576 // Polygons and Polyhedral volumes
4577 if (elem->IsPoly()) {
4579 if (elem->GetType() == SMDSAbs_Face) {
4581 vector<const SMDS_MeshNode *> face_nodes (nbNodes);
4583 for (; inode < nbNodes; inode++) {
4584 face_nodes[inode] = curNodes[inode];
4587 vector<const SMDS_MeshNode *> polygons_nodes;
4588 vector<int> quantities;
4589 int nbNew = SimplifyFace(face_nodes, polygons_nodes, quantities);
4593 for (int iface = 0; iface < nbNew - 1; iface++) {
4594 int nbNodes = quantities[iface];
4595 vector<const SMDS_MeshNode *> poly_nodes (nbNodes);
4596 for (int ii = 0; ii < nbNodes; ii++, inode++) {
4597 poly_nodes[ii] = polygons_nodes[inode];
4599 SMDS_MeshElement* newElem = aMesh->AddPolygonalFace(poly_nodes);
4600 myLastCreatedElems.Append(newElem);
4602 aMesh->SetMeshElementOnShape(newElem, aShapeId);
4604 aMesh->ChangeElementNodes(elem, &polygons_nodes[inode], quantities[nbNew - 1]);
4607 rmElemIds.push_back(elem->GetID());
4611 else if (elem->GetType() == SMDSAbs_Volume) {
4612 // Polyhedral volume
4613 if (nbUniqueNodes < 4) {
4614 rmElemIds.push_back(elem->GetID());
4617 // each face has to be analized in order to check volume validity
4618 const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
4619 static_cast<const SMDS_PolyhedralVolumeOfNodes*>( elem );
4621 int nbFaces = aPolyedre->NbFaces();
4623 vector<const SMDS_MeshNode *> poly_nodes;
4624 vector<int> quantities;
4626 for (int iface = 1; iface <= nbFaces; iface++) {
4627 int nbFaceNodes = aPolyedre->NbFaceNodes(iface);
4628 vector<const SMDS_MeshNode *> faceNodes (nbFaceNodes);
4630 for (int inode = 1; inode <= nbFaceNodes; inode++) {
4631 const SMDS_MeshNode * faceNode = aPolyedre->GetFaceNode(iface, inode);
4632 TNodeNodeMap::iterator nnIt = nodeNodeMap.find(faceNode);
4633 if (nnIt != nodeNodeMap.end()) { // faceNode sticks
4634 faceNode = (*nnIt).second;
4636 faceNodes[inode - 1] = faceNode;
4639 SimplifyFace(faceNodes, poly_nodes, quantities);
4642 if (quantities.size() > 3) {
4643 // to be done: remove coincident faces
4646 if (quantities.size() > 3)
4647 aMesh->ChangePolyhedronNodes(elem, poly_nodes, quantities);
4649 rmElemIds.push_back(elem->GetID());
4653 rmElemIds.push_back(elem->GetID());
4664 switch ( nbNodes ) {
4665 case 2: ///////////////////////////////////// EDGE
4666 isOk = false; break;
4667 case 3: ///////////////////////////////////// TRIANGLE
4668 isOk = false; break;
4670 if ( elem->GetType() == SMDSAbs_Volume ) // TETRAHEDRON
4672 else { //////////////////////////////////// QUADRANGLE
4673 if ( nbUniqueNodes < 3 )
4675 else if ( nbRepl == 2 && iRepl[ 1 ] - iRepl[ 0 ] == 2 )
4676 isOk = false; // opposite nodes stick
4679 case 6: ///////////////////////////////////// PENTAHEDRON
4680 if ( nbUniqueNodes == 4 ) {
4681 // ---------------------------------> tetrahedron
4683 iRepl[ 0 ] > 2 && iRepl[ 1 ] > 2 && iRepl[ 2 ] > 2 ) {
4684 // all top nodes stick: reverse a bottom
4685 uniqueNodes[ 0 ] = curNodes [ 1 ];
4686 uniqueNodes[ 1 ] = curNodes [ 0 ];
4688 else if (nbRepl == 3 &&
4689 iRepl[ 0 ] < 3 && iRepl[ 1 ] < 3 && iRepl[ 2 ] < 3 ) {
4690 // all bottom nodes stick: set a top before
4691 uniqueNodes[ 3 ] = uniqueNodes [ 0 ];
4692 uniqueNodes[ 0 ] = curNodes [ 3 ];
4693 uniqueNodes[ 1 ] = curNodes [ 4 ];
4694 uniqueNodes[ 2 ] = curNodes [ 5 ];
4696 else if (nbRepl == 4 &&
4697 iRepl[ 2 ] - iRepl [ 0 ] == 3 && iRepl[ 3 ] - iRepl [ 1 ] == 3 ) {
4698 // a lateral face turns into a line: reverse a bottom
4699 uniqueNodes[ 0 ] = curNodes [ 1 ];
4700 uniqueNodes[ 1 ] = curNodes [ 0 ];
4705 else if ( nbUniqueNodes == 5 ) {
4706 // PENTAHEDRON --------------------> 2 tetrahedrons
4707 if ( nbRepl == 2 && iRepl[ 1 ] - iRepl [ 0 ] == 3 ) {
4708 // a bottom node sticks with a linked top one
4710 SMDS_MeshElement* newElem =
4711 aMesh->AddVolume(curNodes[ 3 ],
4714 curNodes[ iRepl[ 0 ] == 2 ? 1 : 2 ]);
4715 myLastCreatedElems.Append(newElem);
4717 aMesh->SetMeshElementOnShape( newElem, aShapeId );
4718 // 2. : reverse a bottom
4719 uniqueNodes[ 0 ] = curNodes [ 1 ];
4720 uniqueNodes[ 1 ] = curNodes [ 0 ];
4730 if(elem->IsQuadratic()) { // Quadratic quadrangle
4743 if( iRepl[0]==0 && iRepl[1]==1 && iRepl[2]==4 ) {
4744 uniqueNodes[0] = curNodes[0];
4745 uniqueNodes[1] = curNodes[2];
4746 uniqueNodes[2] = curNodes[3];
4747 uniqueNodes[3] = curNodes[5];
4748 uniqueNodes[4] = curNodes[6];
4749 uniqueNodes[5] = curNodes[7];
4752 if( iRepl[0]==0 && iRepl[1]==3 && iRepl[2]==7 ) {
4753 uniqueNodes[0] = curNodes[0];
4754 uniqueNodes[1] = curNodes[1];
4755 uniqueNodes[2] = curNodes[2];
4756 uniqueNodes[3] = curNodes[4];
4757 uniqueNodes[4] = curNodes[5];
4758 uniqueNodes[5] = curNodes[6];
4761 if( iRepl[0]==0 && iRepl[1]==4 && iRepl[2]==7 ) {
4762 uniqueNodes[0] = curNodes[1];
4763 uniqueNodes[1] = curNodes[2];
4764 uniqueNodes[2] = curNodes[3];
4765 uniqueNodes[3] = curNodes[5];
4766 uniqueNodes[4] = curNodes[6];
4767 uniqueNodes[5] = curNodes[0];
4770 if( iRepl[0]==1 && iRepl[1]==2 && iRepl[2]==5 ) {
4771 uniqueNodes[0] = curNodes[0];
4772 uniqueNodes[1] = curNodes[1];
4773 uniqueNodes[2] = curNodes[3];
4774 uniqueNodes[3] = curNodes[4];
4775 uniqueNodes[4] = curNodes[6];
4776 uniqueNodes[5] = curNodes[7];
4779 if( iRepl[0]==1 && iRepl[1]==4 && iRepl[2]==5 ) {
4780 uniqueNodes[0] = curNodes[0];
4781 uniqueNodes[1] = curNodes[2];
4782 uniqueNodes[2] = curNodes[3];
4783 uniqueNodes[3] = curNodes[1];
4784 uniqueNodes[4] = curNodes[6];
4785 uniqueNodes[5] = curNodes[7];
4788 if( iRepl[0]==2 && iRepl[1]==3 && iRepl[2]==6 ) {
4789 uniqueNodes[0] = curNodes[0];
4790 uniqueNodes[1] = curNodes[1];
4791 uniqueNodes[2] = curNodes[2];
4792 uniqueNodes[3] = curNodes[4];
4793 uniqueNodes[4] = curNodes[5];
4794 uniqueNodes[5] = curNodes[7];
4797 if( iRepl[0]==2 && iRepl[1]==5 && iRepl[2]==6 ) {
4798 uniqueNodes[0] = curNodes[0];
4799 uniqueNodes[1] = curNodes[1];
4800 uniqueNodes[2] = curNodes[3];
4801 uniqueNodes[3] = curNodes[4];
4802 uniqueNodes[4] = curNodes[2];
4803 uniqueNodes[5] = curNodes[7];
4806 if( iRepl[0]==3 && iRepl[1]==6 && iRepl[2]==7 ) {
4807 uniqueNodes[0] = curNodes[0];
4808 uniqueNodes[1] = curNodes[1];
4809 uniqueNodes[2] = curNodes[2];
4810 uniqueNodes[3] = curNodes[4];
4811 uniqueNodes[4] = curNodes[5];
4812 uniqueNodes[5] = curNodes[3];
4818 //////////////////////////////////// HEXAHEDRON
4820 SMDS_VolumeTool hexa (elem);
4821 hexa.SetExternalNormal();
4822 if ( nbUniqueNodes == 4 && nbRepl == 6 ) {
4823 //////////////////////// ---> tetrahedron
4824 for ( int iFace = 0; iFace < 6; iFace++ ) {
4825 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
4826 if (curNodes[ind[ 0 ]] == curNodes[ind[ 1 ]] &&
4827 curNodes[ind[ 0 ]] == curNodes[ind[ 2 ]] &&
4828 curNodes[ind[ 0 ]] == curNodes[ind[ 3 ]] ) {
4829 // one face turns into a point ...
4830 int iOppFace = hexa.GetOppFaceIndex( iFace );
4831 ind = hexa.GetFaceNodesIndices( iOppFace );
4833 iUnique = 2; // reverse a tetrahedron bottom
4834 for ( iCur = 0; iCur < 4 && nbStick < 2; iCur++ ) {
4835 if ( curNodes[ind[ iCur ]] == curNodes[ind[ iCur + 1 ]] )
4837 else if ( iUnique >= 0 )
4838 uniqueNodes[ iUnique-- ] = curNodes[ind[ iCur ]];
4840 if ( nbStick == 1 ) {
4841 // ... and the opposite one - into a triangle.
4843 ind = hexa.GetFaceNodesIndices( iFace );
4844 uniqueNodes[ 3 ] = curNodes[ind[ 0 ]];
4851 else if (nbUniqueNodes == 5 && nbRepl == 4 ) {
4852 //////////////////// HEXAHEDRON ---> 2 tetrahedrons
4853 for ( int iFace = 0; iFace < 6; iFace++ ) {
4854 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
4855 if (curNodes[ind[ 0 ]] == curNodes[ind[ 1 ]] &&
4856 curNodes[ind[ 0 ]] == curNodes[ind[ 2 ]] &&
4857 curNodes[ind[ 0 ]] == curNodes[ind[ 3 ]] ) {
4858 // one face turns into a point ...
4859 int iOppFace = hexa.GetOppFaceIndex( iFace );
4860 ind = hexa.GetFaceNodesIndices( iOppFace );
4862 iUnique = 2; // reverse a tetrahedron 1 bottom
4863 for ( iCur = 0; iCur < 4 && nbStick == 0; iCur++ ) {
4864 if ( curNodes[ind[ iCur ]] == curNodes[ind[ iCur + 1 ]] )
4866 else if ( iUnique >= 0 )
4867 uniqueNodes[ iUnique-- ] = curNodes[ind[ iCur ]];
4869 if ( nbStick == 0 ) {
4870 // ... and the opposite one is a quadrangle
4872 const int* indTop = hexa.GetFaceNodesIndices( iFace );
4873 uniqueNodes[ 3 ] = curNodes[indTop[ 0 ]];
4876 SMDS_MeshElement* newElem =
4877 aMesh->AddVolume(curNodes[ind[ 0 ]],
4880 curNodes[indTop[ 0 ]]);
4881 myLastCreatedElems.Append(newElem);
4883 aMesh->SetMeshElementOnShape( newElem, aShapeId );
4890 else if ( nbUniqueNodes == 6 && nbRepl == 4 ) {
4891 ////////////////// HEXAHEDRON ---> 2 tetrahedrons or 1 prism
4892 // find indices of quad and tri faces
4893 int iQuadFace[ 6 ], iTriFace[ 6 ], nbQuad = 0, nbTri = 0, iFace;
4894 for ( iFace = 0; iFace < 6; iFace++ ) {
4895 const int *ind = hexa.GetFaceNodesIndices( iFace ); // indices of face nodes
4897 for ( iCur = 0; iCur < 4; iCur++ )
4898 nodeSet.insert( curNodes[ind[ iCur ]] );
4899 nbUniqueNodes = nodeSet.size();
4900 if ( nbUniqueNodes == 3 )
4901 iTriFace[ nbTri++ ] = iFace;
4902 else if ( nbUniqueNodes == 4 )
4903 iQuadFace[ nbQuad++ ] = iFace;
4905 if (nbQuad == 2 && nbTri == 4 &&
4906 hexa.GetOppFaceIndex( iQuadFace[ 0 ] ) == iQuadFace[ 1 ]) {
4907 // 2 opposite quadrangles stuck with a diagonal;
4908 // sample groups of merged indices: (0-4)(2-6)
4909 // --------------------------------------------> 2 tetrahedrons
4910 const int *ind1 = hexa.GetFaceNodesIndices( iQuadFace[ 0 ]); // indices of quad1 nodes
4911 const int *ind2 = hexa.GetFaceNodesIndices( iQuadFace[ 1 ]);
4912 int i0, i1d, i2, i3d, i0t, i2t; // d-daigonal, t-top
4913 if (curNodes[ind1[ 0 ]] == curNodes[ind2[ 0 ]] &&
4914 curNodes[ind1[ 2 ]] == curNodes[ind2[ 2 ]]) {
4915 // stuck with 0-2 diagonal
4923 else if (curNodes[ind1[ 1 ]] == curNodes[ind2[ 3 ]] &&
4924 curNodes[ind1[ 3 ]] == curNodes[ind2[ 1 ]]) {
4925 // stuck with 1-3 diagonal
4937 uniqueNodes[ 0 ] = curNodes [ i0 ];
4938 uniqueNodes[ 1 ] = curNodes [ i1d ];
4939 uniqueNodes[ 2 ] = curNodes [ i3d ];
4940 uniqueNodes[ 3 ] = curNodes [ i0t ];
4943 SMDS_MeshElement* newElem = aMesh->AddVolume(curNodes[ i1d ],
4947 myLastCreatedElems.Append(newElem);
4949 aMesh->SetMeshElementOnShape( newElem, aShapeId );
4952 else if (( nbTri == 2 && nbQuad == 3 ) || // merged (0-4)(1-5)
4953 ( nbTri == 4 && nbQuad == 2 )) { // merged (7-4)(1-5)
4954 // --------------------------------------------> prism
4955 // find 2 opposite triangles
4957 for ( iFace = 0; iFace + 1 < nbTri; iFace++ ) {
4958 if ( hexa.GetOppFaceIndex( iTriFace[ iFace ] ) == iTriFace[ iFace + 1 ]) {
4959 // find indices of kept and replaced nodes
4960 // and fill unique nodes of 2 opposite triangles
4961 const int *ind1 = hexa.GetFaceNodesIndices( iTriFace[ iFace ]);
4962 const int *ind2 = hexa.GetFaceNodesIndices( iTriFace[ iFace + 1 ]);
4963 const SMDS_MeshNode** hexanodes = hexa.GetNodes();
4964 // fill unique nodes
4967 for ( iCur = 0; iCur < 4 && isOk; iCur++ ) {
4968 const SMDS_MeshNode* n = curNodes[ind1[ iCur ]];
4969 const SMDS_MeshNode* nInit = hexanodes[ind1[ iCur ]];
4971 // iCur of a linked node of the opposite face (make normals co-directed):
4972 int iCurOpp = ( iCur == 1 || iCur == 3 ) ? 4 - iCur : iCur;
4973 // check that correspondent corners of triangles are linked
4974 if ( !hexa.IsLinked( ind1[ iCur ], ind2[ iCurOpp ] ))
4977 uniqueNodes[ iUnique ] = n;
4978 uniqueNodes[ iUnique + 3 ] = curNodes[ind2[ iCurOpp ]];
4987 } // if ( nbUniqueNodes == 6 && nbRepl == 4 )
4993 } // switch ( nbNodes )
4995 } // if ( nbNodes != nbUniqueNodes ) // some nodes stick
4998 if (elem->IsPoly() && elem->GetType() == SMDSAbs_Volume) {
4999 // Change nodes of polyedre
5000 const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
5001 static_cast<const SMDS_PolyhedralVolumeOfNodes*>( elem );
5003 int nbFaces = aPolyedre->NbFaces();
5005 vector<const SMDS_MeshNode *> poly_nodes;
5006 vector<int> quantities (nbFaces);
5008 for (int iface = 1; iface <= nbFaces; iface++) {
5009 int inode, nbFaceNodes = aPolyedre->NbFaceNodes(iface);
5010 quantities[iface - 1] = nbFaceNodes;
5012 for (inode = 1; inode <= nbFaceNodes; inode++) {
5013 const SMDS_MeshNode* curNode = aPolyedre->GetFaceNode(iface, inode);
5015 TNodeNodeMap::iterator nnIt = nodeNodeMap.find( curNode );
5016 if (nnIt != nodeNodeMap.end()) { // curNode sticks
5017 curNode = (*nnIt).second;
5019 poly_nodes.push_back(curNode);
5022 aMesh->ChangePolyhedronNodes( elem, poly_nodes, quantities );
5026 // Change regular element or polygon
5027 aMesh->ChangeElementNodes( elem, uniqueNodes, nbUniqueNodes );
5031 // Remove invalid regular element or invalid polygon
5032 rmElemIds.push_back( elem->GetID() );
5035 } // loop on elements
5037 // Remove equal nodes and bad elements
5039 Remove( rmNodeIds, true );
5040 Remove( rmElemIds, false );
5045 // ========================================================
5046 // class : SortableElement
5047 // purpose : allow sorting elements basing on their nodes
5048 // ========================================================
5049 class SortableElement : public set <const SMDS_MeshElement*>
5053 SortableElement( const SMDS_MeshElement* theElem )
5056 SMDS_ElemIteratorPtr nodeIt = theElem->nodesIterator();
5057 while ( nodeIt->more() )
5058 this->insert( nodeIt->next() );
5061 const SMDS_MeshElement* Get() const
5064 void Set(const SMDS_MeshElement* e) const
5069 mutable const SMDS_MeshElement* myElem;
5072 //=======================================================================
5073 //function : FindEqualElements
5074 //purpose : Return list of group of elements built on the same nodes.
5075 // Search among theElements or in the whole mesh if theElements is empty
5076 //=======================================================================
5077 void SMESH_MeshEditor::FindEqualElements(set<const SMDS_MeshElement*> & theElements,
5078 TListOfListOfElementsID & theGroupsOfElementsID)
5080 myLastCreatedElems.Clear();
5081 myLastCreatedNodes.Clear();
5083 typedef set<const SMDS_MeshElement*> TElemsSet;
5084 typedef map< SortableElement, int > TMapOfNodeSet;
5085 typedef list<int> TGroupOfElems;
5088 if ( theElements.empty() )
5089 { // get all elements in the mesh
5090 SMDS_ElemIteratorPtr eIt = GetMeshDS()->elementsIterator();
5091 while ( eIt->more() )
5092 elems.insert( elems.end(), eIt->next());
5095 elems = theElements;
5097 vector< TGroupOfElems > arrayOfGroups;
5098 TGroupOfElems groupOfElems;
5099 TMapOfNodeSet mapOfNodeSet;
5101 TElemsSet::iterator elemIt = elems.begin();
5102 for ( int i = 0, j=0; elemIt != elems.end(); ++elemIt, ++j ) {
5103 const SMDS_MeshElement* curElem = *elemIt;
5104 SortableElement SE(curElem);
5107 pair< TMapOfNodeSet::iterator, bool> pp = mapOfNodeSet.insert(make_pair(SE, i));
5108 if( !(pp.second) ) {
5109 TMapOfNodeSet::iterator& itSE = pp.first;
5110 ind = (*itSE).second;
5111 arrayOfGroups[ind].push_back(curElem->GetID());
5114 groupOfElems.clear();
5115 groupOfElems.push_back(curElem->GetID());
5116 arrayOfGroups.push_back(groupOfElems);
5121 vector< TGroupOfElems >::iterator groupIt = arrayOfGroups.begin();
5122 for ( ; groupIt != arrayOfGroups.end(); ++groupIt ) {
5123 groupOfElems = *groupIt;
5124 if ( groupOfElems.size() > 1 ) {
5125 groupOfElems.sort();
5126 theGroupsOfElementsID.push_back(groupOfElems);
5131 //=======================================================================
5132 //function : MergeElements
5133 //purpose : In each given group, substitute all elements by the first one.
5134 //=======================================================================
5136 void SMESH_MeshEditor::MergeElements(TListOfListOfElementsID & theGroupsOfElementsID)
5138 myLastCreatedElems.Clear();
5139 myLastCreatedNodes.Clear();
5141 typedef list<int> TListOfIDs;
5142 TListOfIDs rmElemIds; // IDs of elems to remove
5144 SMESHDS_Mesh* aMesh = GetMeshDS();
5146 TListOfListOfElementsID::iterator groupsIt = theGroupsOfElementsID.begin();
5147 while ( groupsIt != theGroupsOfElementsID.end() ) {
5148 TListOfIDs& aGroupOfElemID = *groupsIt;
5149 aGroupOfElemID.sort();
5150 int elemIDToKeep = aGroupOfElemID.front();
5151 const SMDS_MeshElement* elemToKeep = aMesh->FindElement(elemIDToKeep);
5152 aGroupOfElemID.pop_front();
5153 TListOfIDs::iterator idIt = aGroupOfElemID.begin();
5154 while ( idIt != aGroupOfElemID.end() ) {
5155 int elemIDToRemove = *idIt;
5156 const SMDS_MeshElement* elemToRemove = aMesh->FindElement(elemIDToRemove);
5157 // add the kept element in groups of removed one (PAL15188)
5158 AddToSameGroups( elemToKeep, elemToRemove, aMesh );
5159 rmElemIds.push_back( elemIDToRemove );
5165 Remove( rmElemIds, false );
5168 //=======================================================================
5169 //function : MergeEqualElements
5170 //purpose : Remove all but one of elements built on the same nodes.
5171 //=======================================================================
5173 void SMESH_MeshEditor::MergeEqualElements()
5175 set<const SMDS_MeshElement*> aMeshElements; /* empty input -
5176 to merge equal elements in the whole mesh */
5177 TListOfListOfElementsID aGroupsOfElementsID;
5178 FindEqualElements(aMeshElements, aGroupsOfElementsID);
5179 MergeElements(aGroupsOfElementsID);
5182 //=======================================================================
5183 //function : FindFaceInSet
5184 //purpose : Return a face having linked nodes n1 and n2 and which is
5185 // - not in avoidSet,
5186 // - in elemSet provided that !elemSet.empty()
5187 //=======================================================================
5189 const SMDS_MeshElement*
5190 SMESH_MeshEditor::FindFaceInSet(const SMDS_MeshNode* n1,
5191 const SMDS_MeshNode* n2,
5192 const TIDSortedElemSet& elemSet,
5193 const TIDSortedElemSet& avoidSet)
5196 SMDS_ElemIteratorPtr invElemIt = n1->GetInverseElementIterator(SMDSAbs_Face);
5197 while ( invElemIt->more() ) { // loop on inverse elements of n1
5198 const SMDS_MeshElement* elem = invElemIt->next();
5199 if (avoidSet.find( elem ) != avoidSet.end() )
5201 if ( !elemSet.empty() && elemSet.find( elem ) == elemSet.end())
5203 // get face nodes and find index of n1
5204 int i1, nbN = elem->NbNodes(), iNode = 0;
5205 const SMDS_MeshNode* faceNodes[ nbN ], *n;
5206 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
5207 while ( nIt->more() ) {
5208 faceNodes[ iNode ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
5209 if ( faceNodes[ iNode++ ] == n1 )
5212 // find a n2 linked to n1
5213 if(!elem->IsQuadratic()) {
5214 for ( iNode = 0; iNode < 2; iNode++ ) {
5215 if ( iNode ) // node before n1
5216 n = faceNodes[ i1 == 0 ? nbN - 1 : i1 - 1 ];
5217 else // node after n1
5218 n = faceNodes[ i1 + 1 == nbN ? 0 : i1 + 1 ];
5223 else { // analysis for quadratic elements
5224 bool IsFind = false;
5225 // check using only corner nodes
5226 for ( iNode = 0; iNode < 2; iNode++ ) {
5227 if ( iNode ) // node before n1
5228 n = faceNodes[ i1 == 0 ? nbN/2 - 1 : i1 - 1 ];
5229 else // node after n1
5230 n = faceNodes[ i1 + 1 == nbN/2 ? 0 : i1 + 1 ];
5238 // check using all nodes
5239 const SMDS_QuadraticFaceOfNodes* F =
5240 static_cast<const SMDS_QuadraticFaceOfNodes*>(elem);
5241 // use special nodes iterator
5243 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5244 while ( anIter->more() ) {
5245 faceNodes[iNode] = static_cast<const SMDS_MeshNode*>(anIter->next());
5246 if ( faceNodes[ iNode++ ] == n1 )
5249 for ( iNode = 0; iNode < 2; iNode++ ) {
5250 if ( iNode ) // node before n1
5251 n = faceNodes[ i1 == 0 ? nbN - 1 : i1 - 1 ];
5252 else // node after n1
5253 n = faceNodes[ i1 + 1 == nbN ? 0 : i1 + 1 ];
5259 } // end analysis for quadratic elements
5264 //=======================================================================
5265 //function : findAdjacentFace
5267 //=======================================================================
5269 static const SMDS_MeshElement* findAdjacentFace(const SMDS_MeshNode* n1,
5270 const SMDS_MeshNode* n2,
5271 const SMDS_MeshElement* elem)
5273 TIDSortedElemSet elemSet, avoidSet;
5275 avoidSet.insert ( elem );
5276 return SMESH_MeshEditor::FindFaceInSet( n1, n2, elemSet, avoidSet );
5279 //=======================================================================
5280 //function : FindFreeBorder
5282 //=======================================================================
5284 #define ControlFreeBorder SMESH::Controls::FreeEdges::IsFreeEdge
5286 bool SMESH_MeshEditor::FindFreeBorder (const SMDS_MeshNode* theFirstNode,
5287 const SMDS_MeshNode* theSecondNode,
5288 const SMDS_MeshNode* theLastNode,
5289 list< const SMDS_MeshNode* > & theNodes,
5290 list< const SMDS_MeshElement* >& theFaces)
5292 if ( !theFirstNode || !theSecondNode )
5294 // find border face between theFirstNode and theSecondNode
5295 const SMDS_MeshElement* curElem = findAdjacentFace( theFirstNode, theSecondNode, 0 );
5299 theFaces.push_back( curElem );
5300 theNodes.push_back( theFirstNode );
5301 theNodes.push_back( theSecondNode );
5303 //vector<const SMDS_MeshNode*> nodes;
5304 const SMDS_MeshNode *nIgnore = theFirstNode, *nStart = theSecondNode;
5305 set < const SMDS_MeshElement* > foundElems;
5306 bool needTheLast = ( theLastNode != 0 );
5308 while ( nStart != theLastNode ) {
5309 if ( nStart == theFirstNode )
5310 return !needTheLast;
5312 // find all free border faces sharing form nStart
5314 list< const SMDS_MeshElement* > curElemList;
5315 list< const SMDS_MeshNode* > nStartList;
5316 SMDS_ElemIteratorPtr invElemIt = nStart->GetInverseElementIterator(SMDSAbs_Face);
5317 while ( invElemIt->more() ) {
5318 const SMDS_MeshElement* e = invElemIt->next();
5319 if ( e == curElem || foundElems.insert( e ).second ) {
5321 int iNode = 0, nbNodes = e->NbNodes();
5322 const SMDS_MeshNode* nodes[nbNodes+1];
5323 if(e->IsQuadratic()) {
5324 const SMDS_QuadraticFaceOfNodes* F =
5325 static_cast<const SMDS_QuadraticFaceOfNodes*>(e);
5326 // use special nodes iterator
5327 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5328 while( anIter->more() ) {
5329 nodes[ iNode++ ] = anIter->next();
5333 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
5334 while ( nIt->more() )
5335 nodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
5337 nodes[ iNode ] = nodes[ 0 ];
5339 for ( iNode = 0; iNode < nbNodes; iNode++ )
5340 if (((nodes[ iNode ] == nStart && nodes[ iNode + 1] != nIgnore ) ||
5341 (nodes[ iNode + 1] == nStart && nodes[ iNode ] != nIgnore )) &&
5342 ControlFreeBorder( &nodes[ iNode ], e->GetID() ))
5344 nStartList.push_back( nodes[ iNode + ( nodes[ iNode ] == nStart ? 1 : 0 )]);
5345 curElemList.push_back( e );
5349 // analyse the found
5351 int nbNewBorders = curElemList.size();
5352 if ( nbNewBorders == 0 ) {
5353 // no free border furthermore
5354 return !needTheLast;
5356 else if ( nbNewBorders == 1 ) {
5357 // one more element found
5359 nStart = nStartList.front();
5360 curElem = curElemList.front();
5361 theFaces.push_back( curElem );
5362 theNodes.push_back( nStart );
5365 // several continuations found
5366 list< const SMDS_MeshElement* >::iterator curElemIt;
5367 list< const SMDS_MeshNode* >::iterator nStartIt;
5368 // check if one of them reached the last node
5369 if ( needTheLast ) {
5370 for (curElemIt = curElemList.begin(), nStartIt = nStartList.begin();
5371 curElemIt!= curElemList.end();
5372 curElemIt++, nStartIt++ )
5373 if ( *nStartIt == theLastNode ) {
5374 theFaces.push_back( *curElemIt );
5375 theNodes.push_back( *nStartIt );
5379 // find the best free border by the continuations
5380 list<const SMDS_MeshNode*> contNodes[ 2 ], *cNL;
5381 list<const SMDS_MeshElement*> contFaces[ 2 ], *cFL;
5382 for (curElemIt = curElemList.begin(), nStartIt = nStartList.begin();
5383 curElemIt!= curElemList.end();
5384 curElemIt++, nStartIt++ )
5386 cNL = & contNodes[ contNodes[0].empty() ? 0 : 1 ];
5387 cFL = & contFaces[ contFaces[0].empty() ? 0 : 1 ];
5388 // find one more free border
5389 if ( ! FindFreeBorder( nStart, *nStartIt, theLastNode, *cNL, *cFL )) {
5393 else if ( !contNodes[0].empty() && !contNodes[1].empty() ) {
5394 // choice: clear a worse one
5395 int iLongest = ( contNodes[0].size() < contNodes[1].size() ? 1 : 0 );
5396 int iWorse = ( needTheLast ? 1 - iLongest : iLongest );
5397 contNodes[ iWorse ].clear();
5398 contFaces[ iWorse ].clear();
5401 if ( contNodes[0].empty() && contNodes[1].empty() )
5404 // append the best free border
5405 cNL = & contNodes[ contNodes[0].empty() ? 1 : 0 ];
5406 cFL = & contFaces[ contFaces[0].empty() ? 1 : 0 ];
5407 theNodes.pop_back(); // remove nIgnore
5408 theNodes.pop_back(); // remove nStart
5409 theFaces.pop_back(); // remove curElem
5410 list< const SMDS_MeshNode* >::iterator nIt = cNL->begin();
5411 list< const SMDS_MeshElement* >::iterator fIt = cFL->begin();
5412 for ( ; nIt != cNL->end(); nIt++ ) theNodes.push_back( *nIt );
5413 for ( ; fIt != cFL->end(); fIt++ ) theFaces.push_back( *fIt );
5416 } // several continuations found
5417 } // while ( nStart != theLastNode )
5422 //=======================================================================
5423 //function : CheckFreeBorderNodes
5424 //purpose : Return true if the tree nodes are on a free border
5425 //=======================================================================
5427 bool SMESH_MeshEditor::CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
5428 const SMDS_MeshNode* theNode2,
5429 const SMDS_MeshNode* theNode3)
5431 list< const SMDS_MeshNode* > nodes;
5432 list< const SMDS_MeshElement* > faces;
5433 return FindFreeBorder( theNode1, theNode2, theNode3, nodes, faces);
5436 //=======================================================================
5437 //function : SewFreeBorder
5439 //=======================================================================
5441 SMESH_MeshEditor::Sew_Error
5442 SMESH_MeshEditor::SewFreeBorder (const SMDS_MeshNode* theBordFirstNode,
5443 const SMDS_MeshNode* theBordSecondNode,
5444 const SMDS_MeshNode* theBordLastNode,
5445 const SMDS_MeshNode* theSideFirstNode,
5446 const SMDS_MeshNode* theSideSecondNode,
5447 const SMDS_MeshNode* theSideThirdNode,
5448 const bool theSideIsFreeBorder,
5449 const bool toCreatePolygons,
5450 const bool toCreatePolyedrs)
5452 myLastCreatedElems.Clear();
5453 myLastCreatedNodes.Clear();
5455 MESSAGE("::SewFreeBorder()");
5456 Sew_Error aResult = SEW_OK;
5458 // ====================================
5459 // find side nodes and elements
5460 // ====================================
5462 list< const SMDS_MeshNode* > nSide[ 2 ];
5463 list< const SMDS_MeshElement* > eSide[ 2 ];
5464 list< const SMDS_MeshNode* >::iterator nIt[ 2 ];
5465 list< const SMDS_MeshElement* >::iterator eIt[ 2 ];
5469 if (!FindFreeBorder(theBordFirstNode,theBordSecondNode,theBordLastNode,
5470 nSide[0], eSide[0])) {
5471 MESSAGE(" Free Border 1 not found " );
5472 aResult = SEW_BORDER1_NOT_FOUND;
5474 if (theSideIsFreeBorder) {
5477 if (!FindFreeBorder(theSideFirstNode, theSideSecondNode, theSideThirdNode,
5478 nSide[1], eSide[1])) {
5479 MESSAGE(" Free Border 2 not found " );
5480 aResult = ( aResult != SEW_OK ? SEW_BOTH_BORDERS_NOT_FOUND : SEW_BORDER2_NOT_FOUND );
5483 if ( aResult != SEW_OK )
5486 if (!theSideIsFreeBorder) {
5490 // -------------------------------------------------------------------------
5492 // 1. If nodes to merge are not coincident, move nodes of the free border
5493 // from the coord sys defined by the direction from the first to last
5494 // nodes of the border to the correspondent sys of the side 2
5495 // 2. On the side 2, find the links most co-directed with the correspondent
5496 // links of the free border
5497 // -------------------------------------------------------------------------
5499 // 1. Since sewing may brake if there are volumes to split on the side 2,
5500 // we wont move nodes but just compute new coordinates for them
5501 typedef map<const SMDS_MeshNode*, gp_XYZ> TNodeXYZMap;
5502 TNodeXYZMap nBordXYZ;
5503 list< const SMDS_MeshNode* >& bordNodes = nSide[ 0 ];
5504 list< const SMDS_MeshNode* >::iterator nBordIt;
5506 gp_XYZ Pb1( theBordFirstNode->X(), theBordFirstNode->Y(), theBordFirstNode->Z() );
5507 gp_XYZ Pb2( theBordLastNode->X(), theBordLastNode->Y(), theBordLastNode->Z() );
5508 gp_XYZ Ps1( theSideFirstNode->X(), theSideFirstNode->Y(), theSideFirstNode->Z() );
5509 gp_XYZ Ps2( theSideSecondNode->X(), theSideSecondNode->Y(), theSideSecondNode->Z() );
5510 double tol2 = 1.e-8;
5511 gp_Vec Vbs1( Pb1 - Ps1 ),Vbs2( Pb2 - Ps2 );
5512 if ( Vbs1.SquareMagnitude() > tol2 || Vbs2.SquareMagnitude() > tol2 ) {
5513 // Need node movement.
5515 // find X and Z axes to create trsf
5516 gp_Vec Zb( Pb1 - Pb2 ), Zs( Ps1 - Ps2 );
5518 if ( X.SquareMagnitude() <= gp::Resolution() * gp::Resolution() )
5520 X = gp_Ax2( gp::Origin(), Zb ).XDirection();
5523 gp_Ax3 toBordAx( Pb1, Zb, X );
5524 gp_Ax3 fromSideAx( Ps1, Zs, X );
5525 gp_Ax3 toGlobalAx( gp::Origin(), gp::DZ(), gp::DX() );
5527 gp_Trsf toBordSys, fromSide2Sys;
5528 toBordSys.SetTransformation( toBordAx );
5529 fromSide2Sys.SetTransformation( fromSideAx, toGlobalAx );
5530 fromSide2Sys.SetScaleFactor( Zs.Magnitude() / Zb.Magnitude() );
5533 for ( nBordIt = bordNodes.begin(); nBordIt != bordNodes.end(); nBordIt++ ) {
5534 const SMDS_MeshNode* n = *nBordIt;
5535 gp_XYZ xyz( n->X(),n->Y(),n->Z() );
5536 toBordSys.Transforms( xyz );
5537 fromSide2Sys.Transforms( xyz );
5538 nBordXYZ.insert( TNodeXYZMap::value_type( n, xyz ));
5542 // just insert nodes XYZ in the nBordXYZ map
5543 for ( nBordIt = bordNodes.begin(); nBordIt != bordNodes.end(); nBordIt++ ) {
5544 const SMDS_MeshNode* n = *nBordIt;
5545 nBordXYZ.insert( TNodeXYZMap::value_type( n, gp_XYZ( n->X(),n->Y(),n->Z() )));
5549 // 2. On the side 2, find the links most co-directed with the correspondent
5550 // links of the free border
5552 list< const SMDS_MeshElement* >& sideElems = eSide[ 1 ];
5553 list< const SMDS_MeshNode* >& sideNodes = nSide[ 1 ];
5554 sideNodes.push_back( theSideFirstNode );
5556 bool hasVolumes = false;
5557 LinkID_Gen aLinkID_Gen( GetMeshDS() );
5558 set<long> foundSideLinkIDs, checkedLinkIDs;
5559 SMDS_VolumeTool volume;
5560 //const SMDS_MeshNode* faceNodes[ 4 ];
5562 const SMDS_MeshNode* sideNode;
5563 const SMDS_MeshElement* sideElem;
5564 const SMDS_MeshNode* prevSideNode = theSideFirstNode;
5565 const SMDS_MeshNode* prevBordNode = theBordFirstNode;
5566 nBordIt = bordNodes.begin();
5568 // border node position and border link direction to compare with
5569 gp_XYZ bordPos = nBordXYZ[ *nBordIt ];
5570 gp_XYZ bordDir = bordPos - nBordXYZ[ prevBordNode ];
5571 // choose next side node by link direction or by closeness to
5572 // the current border node:
5573 bool searchByDir = ( *nBordIt != theBordLastNode );
5575 // find the next node on the Side 2
5577 double maxDot = -DBL_MAX, minDist = DBL_MAX;
5579 checkedLinkIDs.clear();
5580 gp_XYZ prevXYZ( prevSideNode->X(), prevSideNode->Y(), prevSideNode->Z() );
5582 // loop on inverse elements of current node (prevSideNode) on the Side 2
5583 SMDS_ElemIteratorPtr invElemIt = prevSideNode->GetInverseElementIterator();
5584 while ( invElemIt->more() )
5586 const SMDS_MeshElement* elem = invElemIt->next();
5587 // prepare data for a loop on links coming to prevSideNode, of a face or a volume
5588 int iPrevNode, iNode = 0, nbNodes = elem->NbNodes();
5589 const SMDS_MeshNode* faceNodes[ nbNodes ];
5590 bool isVolume = volume.Set( elem );
5591 const SMDS_MeshNode** nodes = isVolume ? volume.GetNodes() : faceNodes;
5592 if ( isVolume ) // --volume
5594 //else if ( nbNodes > 2 ) { // --face
5595 else if ( elem->GetType()==SMDSAbs_Face ) { // --face
5596 // retrieve all face nodes and find iPrevNode - an index of the prevSideNode
5597 if(elem->IsQuadratic()) {
5598 const SMDS_QuadraticFaceOfNodes* F =
5599 static_cast<const SMDS_QuadraticFaceOfNodes*>(elem);
5600 // use special nodes iterator
5601 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5602 while( anIter->more() ) {
5603 nodes[ iNode ] = anIter->next();
5604 if ( nodes[ iNode++ ] == prevSideNode )
5605 iPrevNode = iNode - 1;
5609 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
5610 while ( nIt->more() ) {
5611 nodes[ iNode ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
5612 if ( nodes[ iNode++ ] == prevSideNode )
5613 iPrevNode = iNode - 1;
5616 // there are 2 links to check
5621 // loop on links, to be precise, on the second node of links
5622 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
5623 const SMDS_MeshNode* n = nodes[ iNode ];
5625 if ( !volume.IsLinked( n, prevSideNode ))
5629 if ( iNode ) // a node before prevSideNode
5630 n = nodes[ iPrevNode == 0 ? elem->NbNodes() - 1 : iPrevNode - 1 ];
5631 else // a node after prevSideNode
5632 n = nodes[ iPrevNode + 1 == elem->NbNodes() ? 0 : iPrevNode + 1 ];
5634 // check if this link was already used
5635 long iLink = aLinkID_Gen.GetLinkID( prevSideNode, n );
5636 bool isJustChecked = !checkedLinkIDs.insert( iLink ).second;
5637 if (!isJustChecked &&
5638 foundSideLinkIDs.find( iLink ) == foundSideLinkIDs.end() )
5640 // test a link geometrically
5641 gp_XYZ nextXYZ ( n->X(), n->Y(), n->Z() );
5642 bool linkIsBetter = false;
5644 if ( searchByDir ) { // choose most co-directed link
5645 dot = bordDir * ( nextXYZ - prevXYZ ).Normalized();
5646 linkIsBetter = ( dot > maxDot );
5648 else { // choose link with the node closest to bordPos
5649 dist = ( nextXYZ - bordPos ).SquareModulus();
5650 linkIsBetter = ( dist < minDist );
5652 if ( linkIsBetter ) {
5661 } // loop on inverse elements of prevSideNode
5664 MESSAGE(" Cant find path by links of the Side 2 ");
5665 return SEW_BAD_SIDE_NODES;
5667 sideNodes.push_back( sideNode );
5668 sideElems.push_back( sideElem );
5669 foundSideLinkIDs.insert ( linkID );
5670 prevSideNode = sideNode;
5672 if ( *nBordIt == theBordLastNode )
5673 searchByDir = false;
5675 // find the next border link to compare with
5676 gp_XYZ sidePos( sideNode->X(), sideNode->Y(), sideNode->Z() );
5677 searchByDir = ( bordDir * ( sidePos - bordPos ) <= 0 );
5678 // move to next border node if sideNode is before forward border node (bordPos)
5679 while ( *nBordIt != theBordLastNode && !searchByDir ) {
5680 prevBordNode = *nBordIt;
5682 bordPos = nBordXYZ[ *nBordIt ];
5683 bordDir = bordPos - nBordXYZ[ prevBordNode ];
5684 searchByDir = ( bordDir * ( sidePos - bordPos ) <= 0 );
5688 while ( sideNode != theSideSecondNode );
5690 if ( hasVolumes && sideNodes.size () != bordNodes.size() && !toCreatePolyedrs) {
5691 MESSAGE("VOLUME SPLITTING IS FORBIDDEN");
5692 return SEW_VOLUMES_TO_SPLIT; // volume splitting is forbidden
5694 } // end nodes search on the side 2
5696 // ============================
5697 // sew the border to the side 2
5698 // ============================
5700 int nbNodes[] = { nSide[0].size(), nSide[1].size() };
5701 int maxNbNodes = Max( nbNodes[0], nbNodes[1] );
5703 TListOfListOfNodes nodeGroupsToMerge;
5704 if ( nbNodes[0] == nbNodes[1] ||
5705 ( theSideIsFreeBorder && !theSideThirdNode)) {
5707 // all nodes are to be merged
5709 for (nIt[0] = nSide[0].begin(), nIt[1] = nSide[1].begin();
5710 nIt[0] != nSide[0].end() && nIt[1] != nSide[1].end();
5711 nIt[0]++, nIt[1]++ )
5713 nodeGroupsToMerge.push_back( list<const SMDS_MeshNode*>() );
5714 nodeGroupsToMerge.back().push_back( *nIt[1] ); // to keep
5715 nodeGroupsToMerge.back().push_back( *nIt[0] ); // tp remove
5720 // insert new nodes into the border and the side to get equal nb of segments
5722 // get normalized parameters of nodes on the borders
5723 double param[ 2 ][ maxNbNodes ];
5725 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
5726 list< const SMDS_MeshNode* >& nodes = nSide[ iBord ];
5727 list< const SMDS_MeshNode* >::iterator nIt = nodes.begin();
5728 const SMDS_MeshNode* nPrev = *nIt;
5729 double bordLength = 0;
5730 for ( iNode = 0; nIt != nodes.end(); nIt++, iNode++ ) { // loop on border nodes
5731 const SMDS_MeshNode* nCur = *nIt;
5732 gp_XYZ segment (nCur->X() - nPrev->X(),
5733 nCur->Y() - nPrev->Y(),
5734 nCur->Z() - nPrev->Z());
5735 double segmentLen = segment.Modulus();
5736 bordLength += segmentLen;
5737 param[ iBord ][ iNode ] = bordLength;
5740 // normalize within [0,1]
5741 for ( iNode = 0; iNode < nbNodes[ iBord ]; iNode++ ) {
5742 param[ iBord ][ iNode ] /= bordLength;
5746 // loop on border segments
5747 const SMDS_MeshNode *nPrev[ 2 ] = { 0, 0 };
5748 int i[ 2 ] = { 0, 0 };
5749 nIt[0] = nSide[0].begin(); eIt[0] = eSide[0].begin();
5750 nIt[1] = nSide[1].begin(); eIt[1] = eSide[1].begin();
5752 TElemOfNodeListMap insertMap;
5753 TElemOfNodeListMap::iterator insertMapIt;
5755 // key: elem to insert nodes into
5756 // value: 2 nodes to insert between + nodes to be inserted
5758 bool next[ 2 ] = { false, false };
5760 // find min adjacent segment length after sewing
5761 double nextParam = 10., prevParam = 0;
5762 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
5763 if ( i[ iBord ] + 1 < nbNodes[ iBord ])
5764 nextParam = Min( nextParam, param[iBord][ i[iBord] + 1 ]);
5765 if ( i[ iBord ] > 0 )
5766 prevParam = Max( prevParam, param[iBord][ i[iBord] - 1 ]);
5768 double minParam = Min( param[ 0 ][ i[0] ], param[ 1 ][ i[1] ]);
5769 double maxParam = Max( param[ 0 ][ i[0] ], param[ 1 ][ i[1] ]);
5770 double minSegLen = Min( nextParam - minParam, maxParam - prevParam );
5772 // choose to insert or to merge nodes
5773 double du = param[ 1 ][ i[1] ] - param[ 0 ][ i[0] ];
5774 if ( Abs( du ) <= minSegLen * 0.2 ) {
5777 nodeGroupsToMerge.push_back( list<const SMDS_MeshNode*>() );
5778 const SMDS_MeshNode* n0 = *nIt[0];
5779 const SMDS_MeshNode* n1 = *nIt[1];
5780 nodeGroupsToMerge.back().push_back( n1 );
5781 nodeGroupsToMerge.back().push_back( n0 );
5782 // position of node of the border changes due to merge
5783 param[ 0 ][ i[0] ] += du;
5784 // move n1 for the sake of elem shape evaluation during insertion.
5785 // n1 will be removed by MergeNodes() anyway
5786 const_cast<SMDS_MeshNode*>( n0 )->setXYZ( n1->X(), n1->Y(), n1->Z() );
5787 next[0] = next[1] = true;
5792 int intoBord = ( du < 0 ) ? 0 : 1;
5793 const SMDS_MeshElement* elem = *eIt[ intoBord ];
5794 const SMDS_MeshNode* n1 = nPrev[ intoBord ];
5795 const SMDS_MeshNode* n2 = *nIt[ intoBord ];
5796 const SMDS_MeshNode* nIns = *nIt[ 1 - intoBord ];
5797 if ( intoBord == 1 ) {
5798 // move node of the border to be on a link of elem of the side
5799 gp_XYZ p1 (n1->X(), n1->Y(), n1->Z());
5800 gp_XYZ p2 (n2->X(), n2->Y(), n2->Z());
5801 double ratio = du / ( param[ 1 ][ i[1] ] - param[ 1 ][ i[1]-1 ]);
5802 gp_XYZ p = p2 * ( 1 - ratio ) + p1 * ratio;
5803 GetMeshDS()->MoveNode( nIns, p.X(), p.Y(), p.Z() );
5805 insertMapIt = insertMap.find( elem );
5806 bool notFound = ( insertMapIt == insertMap.end() );
5807 bool otherLink = ( !notFound && (*insertMapIt).second.front() != n1 );
5809 // insert into another link of the same element:
5810 // 1. perform insertion into the other link of the elem
5811 list<const SMDS_MeshNode*> & nodeList = (*insertMapIt).second;
5812 const SMDS_MeshNode* n12 = nodeList.front(); nodeList.pop_front();
5813 const SMDS_MeshNode* n22 = nodeList.front(); nodeList.pop_front();
5814 InsertNodesIntoLink( elem, n12, n22, nodeList, toCreatePolygons );
5815 // 2. perform insertion into the link of adjacent faces
5817 const SMDS_MeshElement* adjElem = findAdjacentFace( n12, n22, elem );
5819 InsertNodesIntoLink( adjElem, n12, n22, nodeList, toCreatePolygons );
5823 if (toCreatePolyedrs) {
5824 // perform insertion into the links of adjacent volumes
5825 UpdateVolumes(n12, n22, nodeList);
5827 // 3. find an element appeared on n1 and n2 after the insertion
5828 insertMap.erase( elem );
5829 elem = findAdjacentFace( n1, n2, 0 );
5831 if ( notFound || otherLink ) {
5832 // add element and nodes of the side into the insertMap
5833 insertMapIt = insertMap.insert
5834 ( TElemOfNodeListMap::value_type( elem, list<const SMDS_MeshNode*>() )).first;
5835 (*insertMapIt).second.push_back( n1 );
5836 (*insertMapIt).second.push_back( n2 );
5838 // add node to be inserted into elem
5839 (*insertMapIt).second.push_back( nIns );
5840 next[ 1 - intoBord ] = true;
5843 // go to the next segment
5844 for ( iBord = 0; iBord < 2; iBord++ ) { // loop on 2 borders
5845 if ( next[ iBord ] ) {
5846 if ( i[ iBord ] != 0 && eIt[ iBord ] != eSide[ iBord ].end())
5848 nPrev[ iBord ] = *nIt[ iBord ];
5849 nIt[ iBord ]++; i[ iBord ]++;
5853 while ( nIt[0] != nSide[0].end() && nIt[1] != nSide[1].end());
5855 // perform insertion of nodes into elements
5857 for (insertMapIt = insertMap.begin();
5858 insertMapIt != insertMap.end();
5861 const SMDS_MeshElement* elem = (*insertMapIt).first;
5862 list<const SMDS_MeshNode*> & nodeList = (*insertMapIt).second;
5863 const SMDS_MeshNode* n1 = nodeList.front(); nodeList.pop_front();
5864 const SMDS_MeshNode* n2 = nodeList.front(); nodeList.pop_front();
5866 InsertNodesIntoLink( elem, n1, n2, nodeList, toCreatePolygons );
5868 if ( !theSideIsFreeBorder ) {
5869 // look for and insert nodes into the faces adjacent to elem
5871 const SMDS_MeshElement* adjElem = findAdjacentFace( n1, n2, elem );
5873 InsertNodesIntoLink( adjElem, n1, n2, nodeList, toCreatePolygons );
5878 if (toCreatePolyedrs) {
5879 // perform insertion into the links of adjacent volumes
5880 UpdateVolumes(n1, n2, nodeList);
5884 } // end: insert new nodes
5886 MergeNodes ( nodeGroupsToMerge );
5891 //=======================================================================
5892 //function : InsertNodesIntoLink
5893 //purpose : insert theNodesToInsert into theFace between theBetweenNode1
5894 // and theBetweenNode2 and split theElement
5895 //=======================================================================
5897 void SMESH_MeshEditor::InsertNodesIntoLink(const SMDS_MeshElement* theFace,
5898 const SMDS_MeshNode* theBetweenNode1,
5899 const SMDS_MeshNode* theBetweenNode2,
5900 list<const SMDS_MeshNode*>& theNodesToInsert,
5901 const bool toCreatePoly)
5903 if ( theFace->GetType() != SMDSAbs_Face ) return;
5905 // find indices of 2 link nodes and of the rest nodes
5906 int iNode = 0, il1, il2, i3, i4;
5907 il1 = il2 = i3 = i4 = -1;
5908 const SMDS_MeshNode* nodes[ theFace->NbNodes() ];
5910 if(theFace->IsQuadratic()) {
5911 const SMDS_QuadraticFaceOfNodes* F =
5912 static_cast<const SMDS_QuadraticFaceOfNodes*>(theFace);
5913 // use special nodes iterator
5914 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5915 while( anIter->more() ) {
5916 const SMDS_MeshNode* n = anIter->next();
5917 if ( n == theBetweenNode1 )
5919 else if ( n == theBetweenNode2 )
5925 nodes[ iNode++ ] = n;
5929 SMDS_ElemIteratorPtr nodeIt = theFace->nodesIterator();
5930 while ( nodeIt->more() ) {
5931 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
5932 if ( n == theBetweenNode1 )
5934 else if ( n == theBetweenNode2 )
5940 nodes[ iNode++ ] = n;
5943 if ( il1 < 0 || il2 < 0 || i3 < 0 )
5946 // arrange link nodes to go one after another regarding the face orientation
5947 bool reverse = ( Abs( il2 - il1 ) == 1 ? il2 < il1 : il1 < il2 );
5948 list<const SMDS_MeshNode *> aNodesToInsert = theNodesToInsert;
5953 aNodesToInsert.reverse();
5955 // check that not link nodes of a quadrangles are in good order
5956 int nbFaceNodes = theFace->NbNodes();
5957 if ( nbFaceNodes == 4 && i4 - i3 != 1 ) {
5963 if (toCreatePoly || theFace->IsPoly()) {
5966 vector<const SMDS_MeshNode *> poly_nodes (nbFaceNodes + aNodesToInsert.size());
5968 // add nodes of face up to first node of link
5971 if(theFace->IsQuadratic()) {
5972 const SMDS_QuadraticFaceOfNodes* F =
5973 static_cast<const SMDS_QuadraticFaceOfNodes*>(theFace);
5974 // use special nodes iterator
5975 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
5976 while( anIter->more() && !isFLN ) {
5977 const SMDS_MeshNode* n = anIter->next();
5978 poly_nodes[iNode++] = n;
5979 if (n == nodes[il1]) {
5983 // add nodes to insert
5984 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
5985 for (; nIt != aNodesToInsert.end(); nIt++) {
5986 poly_nodes[iNode++] = *nIt;
5988 // add nodes of face starting from last node of link
5989 while ( anIter->more() ) {
5990 poly_nodes[iNode++] = anIter->next();
5994 SMDS_ElemIteratorPtr nodeIt = theFace->nodesIterator();
5995 while ( nodeIt->more() && !isFLN ) {
5996 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
5997 poly_nodes[iNode++] = n;
5998 if (n == nodes[il1]) {
6002 // add nodes to insert
6003 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
6004 for (; nIt != aNodesToInsert.end(); nIt++) {
6005 poly_nodes[iNode++] = *nIt;
6007 // add nodes of face starting from last node of link
6008 while ( nodeIt->more() ) {
6009 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6010 poly_nodes[iNode++] = n;
6014 // edit or replace the face
6015 SMESHDS_Mesh *aMesh = GetMeshDS();
6017 if (theFace->IsPoly()) {
6018 aMesh->ChangePolygonNodes(theFace, poly_nodes);
6021 int aShapeId = FindShape( theFace );
6023 SMDS_MeshElement* newElem = aMesh->AddPolygonalFace(poly_nodes);
6024 myLastCreatedElems.Append(newElem);
6025 if ( aShapeId && newElem )
6026 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6028 aMesh->RemoveElement(theFace);
6033 if( !theFace->IsQuadratic() ) {
6035 // put aNodesToInsert between theBetweenNode1 and theBetweenNode2
6036 int nbLinkNodes = 2 + aNodesToInsert.size();
6037 const SMDS_MeshNode* linkNodes[ nbLinkNodes ];
6038 linkNodes[ 0 ] = nodes[ il1 ];
6039 linkNodes[ nbLinkNodes - 1 ] = nodes[ il2 ];
6040 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
6041 for ( iNode = 1; nIt != aNodesToInsert.end(); nIt++ ) {
6042 linkNodes[ iNode++ ] = *nIt;
6044 // decide how to split a quadrangle: compare possible variants
6045 // and choose which of splits to be a quadrangle
6046 int i1, i2, iSplit, nbSplits = nbLinkNodes - 1, iBestQuad;
6047 if ( nbFaceNodes == 3 ) {
6048 iBestQuad = nbSplits;
6051 else if ( nbFaceNodes == 4 ) {
6052 SMESH::Controls::NumericalFunctorPtr aCrit( new SMESH::Controls::AspectRatio);
6053 double aBestRate = DBL_MAX;
6054 for ( int iQuad = 0; iQuad < nbSplits; iQuad++ ) {
6056 double aBadRate = 0;
6057 // evaluate elements quality
6058 for ( iSplit = 0; iSplit < nbSplits; iSplit++ ) {
6059 if ( iSplit == iQuad ) {
6060 SMDS_FaceOfNodes quad (linkNodes[ i1++ ],
6064 aBadRate += getBadRate( &quad, aCrit );
6067 SMDS_FaceOfNodes tria (linkNodes[ i1++ ],
6069 nodes[ iSplit < iQuad ? i4 : i3 ]);
6070 aBadRate += getBadRate( &tria, aCrit );
6074 if ( aBadRate < aBestRate ) {
6076 aBestRate = aBadRate;
6081 // create new elements
6082 SMESHDS_Mesh *aMesh = GetMeshDS();
6083 int aShapeId = FindShape( theFace );
6086 for ( iSplit = 0; iSplit < nbSplits - 1; iSplit++ ) {
6087 SMDS_MeshElement* newElem = 0;
6088 if ( iSplit == iBestQuad )
6089 newElem = aMesh->AddFace (linkNodes[ i1++ ],
6094 newElem = aMesh->AddFace (linkNodes[ i1++ ],
6096 nodes[ iSplit < iBestQuad ? i4 : i3 ]);
6097 myLastCreatedElems.Append(newElem);
6098 if ( aShapeId && newElem )
6099 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6102 // change nodes of theFace
6103 const SMDS_MeshNode* newNodes[ 4 ];
6104 newNodes[ 0 ] = linkNodes[ i1 ];
6105 newNodes[ 1 ] = linkNodes[ i2 ];
6106 newNodes[ 2 ] = nodes[ iSplit >= iBestQuad ? i3 : i4 ];
6107 newNodes[ 3 ] = nodes[ i4 ];
6108 aMesh->ChangeElementNodes( theFace, newNodes, iSplit == iBestQuad ? 4 : 3 );
6109 } // end if(!theFace->IsQuadratic())
6110 else { // theFace is quadratic
6111 // we have to split theFace on simple triangles and one simple quadrangle
6113 int nbshift = tmp*2;
6114 // shift nodes in nodes[] by nbshift
6116 for(i=0; i<nbshift; i++) {
6117 const SMDS_MeshNode* n = nodes[0];
6118 for(j=0; j<nbFaceNodes-1; j++) {
6119 nodes[j] = nodes[j+1];
6121 nodes[nbFaceNodes-1] = n;
6123 il1 = il1 - nbshift;
6124 // now have to insert nodes between n0 and n1 or n1 and n2 (see below)
6125 // n0 n1 n2 n0 n1 n2
6126 // +-----+-----+ +-----+-----+
6135 // create new elements
6136 SMESHDS_Mesh *aMesh = GetMeshDS();
6137 int aShapeId = FindShape( theFace );
6140 if(nbFaceNodes==6) { // quadratic triangle
6141 SMDS_MeshElement* newElem =
6142 aMesh->AddFace(nodes[3],nodes[4],nodes[5]);
6143 myLastCreatedElems.Append(newElem);
6144 if ( aShapeId && newElem )
6145 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6146 if(theFace->IsMediumNode(nodes[il1])) {
6147 // create quadrangle
6148 newElem = aMesh->AddFace(nodes[0],nodes[1],nodes[3],nodes[5]);
6149 myLastCreatedElems.Append(newElem);
6150 if ( aShapeId && newElem )
6151 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6157 // create quadrangle
6158 newElem = aMesh->AddFace(nodes[1],nodes[2],nodes[3],nodes[5]);
6159 myLastCreatedElems.Append(newElem);
6160 if ( aShapeId && newElem )
6161 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6167 else { // nbFaceNodes==8 - quadratic quadrangle
6168 SMDS_MeshElement* newElem =
6169 aMesh->AddFace(nodes[3],nodes[4],nodes[5]);
6170 myLastCreatedElems.Append(newElem);
6171 if ( aShapeId && newElem )
6172 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6173 newElem = aMesh->AddFace(nodes[5],nodes[6],nodes[7]);
6174 myLastCreatedElems.Append(newElem);
6175 if ( aShapeId && newElem )
6176 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6177 newElem = aMesh->AddFace(nodes[5],nodes[7],nodes[3]);
6178 myLastCreatedElems.Append(newElem);
6179 if ( aShapeId && newElem )
6180 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6181 if(theFace->IsMediumNode(nodes[il1])) {
6182 // create quadrangle
6183 newElem = aMesh->AddFace(nodes[0],nodes[1],nodes[3],nodes[7]);
6184 myLastCreatedElems.Append(newElem);
6185 if ( aShapeId && newElem )
6186 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6192 // create quadrangle
6193 newElem = aMesh->AddFace(nodes[1],nodes[2],nodes[3],nodes[7]);
6194 myLastCreatedElems.Append(newElem);
6195 if ( aShapeId && newElem )
6196 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6202 // create needed triangles using n1,n2,n3 and inserted nodes
6203 int nbn = 2 + aNodesToInsert.size();
6204 const SMDS_MeshNode* aNodes[nbn];
6205 aNodes[0] = nodes[n1];
6206 aNodes[nbn-1] = nodes[n2];
6207 list<const SMDS_MeshNode*>::iterator nIt = aNodesToInsert.begin();
6208 for ( iNode = 1; nIt != aNodesToInsert.end(); nIt++ ) {
6209 aNodes[iNode++] = *nIt;
6211 for(i=1; i<nbn; i++) {
6212 SMDS_MeshElement* newElem =
6213 aMesh->AddFace(aNodes[i-1],aNodes[i],nodes[n3]);
6214 myLastCreatedElems.Append(newElem);
6215 if ( aShapeId && newElem )
6216 aMesh->SetMeshElementOnShape( newElem, aShapeId );
6218 // remove old quadratic face
6219 aMesh->RemoveElement(theFace);
6223 //=======================================================================
6224 //function : UpdateVolumes
6226 //=======================================================================
6227 void SMESH_MeshEditor::UpdateVolumes (const SMDS_MeshNode* theBetweenNode1,
6228 const SMDS_MeshNode* theBetweenNode2,
6229 list<const SMDS_MeshNode*>& theNodesToInsert)
6231 myLastCreatedElems.Clear();
6232 myLastCreatedNodes.Clear();
6234 SMDS_ElemIteratorPtr invElemIt = theBetweenNode1->GetInverseElementIterator(SMDSAbs_Volume);
6235 while (invElemIt->more()) { // loop on inverse elements of theBetweenNode1
6236 const SMDS_MeshElement* elem = invElemIt->next();
6238 // check, if current volume has link theBetweenNode1 - theBetweenNode2
6239 SMDS_VolumeTool aVolume (elem);
6240 if (!aVolume.IsLinked(theBetweenNode1, theBetweenNode2))
6243 // insert new nodes in all faces of the volume, sharing link theBetweenNode1 - theBetweenNode2
6244 int iface, nbFaces = aVolume.NbFaces();
6245 vector<const SMDS_MeshNode *> poly_nodes;
6246 vector<int> quantities (nbFaces);
6248 for (iface = 0; iface < nbFaces; iface++) {
6249 int nbFaceNodes = aVolume.NbFaceNodes(iface), nbInserted = 0;
6250 // faceNodes will contain (nbFaceNodes + 1) nodes, last = first
6251 const SMDS_MeshNode** faceNodes = aVolume.GetFaceNodes(iface);
6253 for (int inode = 0; inode < nbFaceNodes; inode++) {
6254 poly_nodes.push_back(faceNodes[inode]);
6256 if (nbInserted == 0) {
6257 if (faceNodes[inode] == theBetweenNode1) {
6258 if (faceNodes[inode + 1] == theBetweenNode2) {
6259 nbInserted = theNodesToInsert.size();
6261 // add nodes to insert
6262 list<const SMDS_MeshNode*>::iterator nIt = theNodesToInsert.begin();
6263 for (; nIt != theNodesToInsert.end(); nIt++) {
6264 poly_nodes.push_back(*nIt);
6268 else if (faceNodes[inode] == theBetweenNode2) {
6269 if (faceNodes[inode + 1] == theBetweenNode1) {
6270 nbInserted = theNodesToInsert.size();
6272 // add nodes to insert in reversed order
6273 list<const SMDS_MeshNode*>::iterator nIt = theNodesToInsert.end();
6275 for (; nIt != theNodesToInsert.begin(); nIt--) {
6276 poly_nodes.push_back(*nIt);
6278 poly_nodes.push_back(*nIt);
6285 quantities[iface] = nbFaceNodes + nbInserted;
6288 // Replace or update the volume
6289 SMESHDS_Mesh *aMesh = GetMeshDS();
6291 if (elem->IsPoly()) {
6292 aMesh->ChangePolyhedronNodes(elem, poly_nodes, quantities);
6296 int aShapeId = FindShape( elem );
6298 SMDS_MeshElement* newElem =
6299 aMesh->AddPolyhedralVolume(poly_nodes, quantities);
6300 myLastCreatedElems.Append(newElem);
6301 if (aShapeId && newElem)
6302 aMesh->SetMeshElementOnShape(newElem, aShapeId);
6304 aMesh->RemoveElement(elem);
6309 //=======================================================================
6311 * \brief Convert elements contained in a submesh to quadratic
6312 * \retval int - nb of checked elements
6314 //=======================================================================
6316 int SMESH_MeshEditor::ConvertElemToQuadratic(SMESHDS_SubMesh * theSm,
6317 SMESH_MesherHelper& theHelper,
6318 const bool theForce3d)
6321 if( !theSm ) return nbElem;
6322 SMDS_ElemIteratorPtr ElemItr = theSm->GetElements();
6323 while(ElemItr->more())
6326 const SMDS_MeshElement* elem = ElemItr->next();
6327 if( !elem || elem->IsQuadratic() ) continue;
6329 int id = elem->GetID();
6330 int nbNodes = elem->NbNodes();
6331 vector<const SMDS_MeshNode *> aNds (nbNodes);
6333 for(int i = 0; i < nbNodes; i++)
6335 aNds[i] = elem->GetNode(i);
6337 SMDSAbs_ElementType aType = elem->GetType();
6339 theSm->RemoveElement(elem);
6340 GetMeshDS()->SMDS_Mesh::RemoveFreeElement(elem);
6342 const SMDS_MeshElement* NewElem = 0;
6348 NewElem = theHelper.AddEdge(aNds[0], aNds[1], id, theForce3d);
6356 NewElem = theHelper.AddFace(aNds[0], aNds[1], aNds[2], id, theForce3d);
6359 NewElem = theHelper.AddFace(aNds[0], aNds[1], aNds[2], aNds[3], id, theForce3d);
6366 case SMDSAbs_Volume :
6371 NewElem = theHelper.AddVolume(aNds[0], aNds[1], aNds[2], aNds[3], id, true);
6374 NewElem = theHelper.AddVolume(aNds[0], aNds[1], aNds[2], aNds[3], aNds[4], aNds[5], id, true);
6377 NewElem = theHelper.AddVolume(aNds[0], aNds[1], aNds[2], aNds[3],
6378 aNds[4], aNds[5], aNds[6], aNds[7], id, true);
6390 AddToSameGroups( NewElem, elem, GetMeshDS());
6391 theSm->AddElement( NewElem );
6393 if ( NewElem != elem )
6394 RemoveElemFromGroups (elem, GetMeshDS());
6399 //=======================================================================
6400 //function : ConvertToQuadratic
6402 //=======================================================================
6403 void SMESH_MeshEditor::ConvertToQuadratic(const bool theForce3d)
6405 SMESHDS_Mesh* meshDS = GetMeshDS();
6407 SMESH_MesherHelper aHelper(*myMesh);
6408 aHelper.SetIsQuadratic( true );
6410 int nbCheckedElems = 0;
6411 if ( myMesh->HasShapeToMesh() )
6413 if ( SMESH_subMesh *aSubMesh = myMesh->GetSubMeshContaining(myMesh->GetShapeToMesh()))
6415 SMESH_subMeshIteratorPtr smIt = aSubMesh->getDependsOnIterator(true,false);
6416 while ( smIt->more() ) {
6417 SMESH_subMesh* sm = smIt->next();
6418 if ( SMESHDS_SubMesh *smDS = sm->GetSubMeshDS() ) {
6419 aHelper.SetSubShape( sm->GetSubShape() );
6420 nbCheckedElems += ConvertElemToQuadratic(smDS, aHelper, theForce3d);
6425 int totalNbElems = meshDS->NbEdges() + meshDS->NbFaces() + meshDS->NbVolumes();
6426 if ( nbCheckedElems < totalNbElems ) // not all elements in submeshes
6428 SMDS_EdgeIteratorPtr aEdgeItr = meshDS->edgesIterator();
6429 while(aEdgeItr->more())
6431 const SMDS_MeshEdge* edge = aEdgeItr->next();
6432 if(edge && !edge->IsQuadratic())
6434 int id = edge->GetID();
6435 const SMDS_MeshNode* n1 = edge->GetNode(0);
6436 const SMDS_MeshNode* n2 = edge->GetNode(1);
6438 meshDS->SMDS_Mesh::RemoveFreeElement(edge);
6440 const SMDS_MeshEdge* NewEdge = aHelper.AddEdge(n1, n2, id, theForce3d);
6442 AddToSameGroups(NewEdge, edge, meshDS);
6443 if ( NewEdge != edge )
6444 RemoveElemFromGroups (edge, meshDS);
6447 SMDS_FaceIteratorPtr aFaceItr = meshDS->facesIterator();
6448 while(aFaceItr->more())
6450 const SMDS_MeshFace* face = aFaceItr->next();
6451 if(!face || face->IsQuadratic() ) continue;
6453 int id = face->GetID();
6454 int nbNodes = face->NbNodes();
6455 vector<const SMDS_MeshNode *> aNds (nbNodes);
6457 for(int i = 0; i < nbNodes; i++)
6459 aNds[i] = face->GetNode(i);
6462 meshDS->SMDS_Mesh::RemoveFreeElement(face);
6464 SMDS_MeshFace * NewFace = 0;
6468 NewFace = aHelper.AddFace(aNds[0], aNds[1], aNds[2], id, theForce3d);
6471 NewFace = aHelper.AddFace(aNds[0], aNds[1], aNds[2], aNds[3], id, theForce3d);
6477 AddToSameGroups(NewFace, face, meshDS);
6478 if ( NewFace != face )
6479 RemoveElemFromGroups (face, meshDS);
6481 SMDS_VolumeIteratorPtr aVolumeItr = meshDS->volumesIterator();
6482 while(aVolumeItr->more())
6484 const SMDS_MeshVolume* volume = aVolumeItr->next();
6485 if(!volume || volume->IsQuadratic() ) continue;
6487 int id = volume->GetID();
6488 int nbNodes = volume->NbNodes();
6489 vector<const SMDS_MeshNode *> aNds (nbNodes);
6491 for(int i = 0; i < nbNodes; i++)
6493 aNds[i] = volume->GetNode(i);
6496 meshDS->SMDS_Mesh::RemoveFreeElement(volume);
6498 SMDS_MeshVolume * NewVolume = 0;
6502 NewVolume = aHelper.AddVolume(aNds[0], aNds[1], aNds[2],
6503 aNds[3], id, true );
6506 NewVolume = aHelper.AddVolume(aNds[0], aNds[1], aNds[2],
6507 aNds[3], aNds[4], aNds[5], id, true);
6510 NewVolume = aHelper.AddVolume(aNds[0], aNds[1], aNds[2], aNds[3],
6511 aNds[4], aNds[5], aNds[6], aNds[7], id, true);
6517 AddToSameGroups(NewVolume, volume, meshDS);
6518 if ( NewVolume != volume )
6519 RemoveElemFromGroups (volume, meshDS);
6524 //=======================================================================
6526 * \brief Convert quadratic elements to linear ones and remove quadratic nodes
6527 * \retval int - nb of checked elements
6529 //=======================================================================
6531 int SMESH_MeshEditor::RemoveQuadElem(SMESHDS_SubMesh * theSm,
6532 SMDS_ElemIteratorPtr theItr,
6533 const int theShapeID)
6536 SMESHDS_Mesh* meshDS = GetMeshDS();
6537 while( theItr->more() )
6539 const SMDS_MeshElement* elem = theItr->next();
6541 if( elem && elem->IsQuadratic())
6543 int id = elem->GetID();
6544 int nbNodes = elem->NbNodes();
6545 vector<const SMDS_MeshNode *> aNds, mediumNodes;
6546 aNds.reserve( nbNodes );
6547 mediumNodes.reserve( nbNodes );
6549 for(int i = 0; i < nbNodes; i++)
6551 const SMDS_MeshNode* n = elem->GetNode(i);
6553 if( elem->IsMediumNode( n ) )
6554 mediumNodes.push_back( n );
6556 aNds.push_back( n );
6558 if( aNds.empty() ) continue;
6559 SMDSAbs_ElementType aType = elem->GetType();
6561 //remove old quadratic element
6562 meshDS->SMDS_Mesh::RemoveFreeElement( elem );
6564 theSm->RemoveElement( elem );
6566 SMDS_MeshElement * NewElem = AddElement( aNds, aType, false, id );
6568 AddToSameGroups(NewElem, elem, meshDS);
6569 if ( NewElem != elem )
6570 RemoveElemFromGroups (elem, meshDS);
6571 if( theSm && NewElem )
6572 theSm->AddElement( NewElem );
6574 // remove medium nodes
6575 vector<const SMDS_MeshNode*>::iterator nIt = mediumNodes.begin();
6576 for ( ; nIt != mediumNodes.end(); ++nIt ) {
6577 const SMDS_MeshNode* n = *nIt;
6578 if ( n->NbInverseNodes() == 0 ) {
6579 if ( n->GetPosition()->GetShapeId() != theShapeID )
6580 meshDS->RemoveFreeNode( n, meshDS->MeshElements
6581 ( n->GetPosition()->GetShapeId() ));
6583 meshDS->RemoveFreeNode( n, theSm );
6591 //=======================================================================
6592 //function : ConvertFromQuadratic
6594 //=======================================================================
6595 bool SMESH_MeshEditor::ConvertFromQuadratic()
6597 int nbCheckedElems = 0;
6598 if ( myMesh->HasShapeToMesh() )
6600 if ( SMESH_subMesh *aSubMesh = myMesh->GetSubMeshContaining(myMesh->GetShapeToMesh()))
6602 SMESH_subMeshIteratorPtr smIt = aSubMesh->getDependsOnIterator(true,false);
6603 while ( smIt->more() ) {
6604 SMESH_subMesh* sm = smIt->next();
6605 if ( SMESHDS_SubMesh *smDS = sm->GetSubMeshDS() )
6606 nbCheckedElems += RemoveQuadElem( smDS, smDS->GetElements(), sm->GetId() );
6612 GetMeshDS()->NbEdges() + GetMeshDS()->NbFaces() + GetMeshDS()->NbVolumes();
6613 if ( nbCheckedElems < totalNbElems ) // not all elements in submeshes
6615 SMESHDS_SubMesh *aSM = 0;
6616 RemoveQuadElem( aSM, GetMeshDS()->elementsIterator(), 0 );
6622 //=======================================================================
6623 //function : SewSideElements
6625 //=======================================================================
6627 SMESH_MeshEditor::Sew_Error
6628 SMESH_MeshEditor::SewSideElements (TIDSortedElemSet& theSide1,
6629 TIDSortedElemSet& theSide2,
6630 const SMDS_MeshNode* theFirstNode1,
6631 const SMDS_MeshNode* theFirstNode2,
6632 const SMDS_MeshNode* theSecondNode1,
6633 const SMDS_MeshNode* theSecondNode2)
6635 myLastCreatedElems.Clear();
6636 myLastCreatedNodes.Clear();
6638 MESSAGE ("::::SewSideElements()");
6639 if ( theSide1.size() != theSide2.size() )
6640 return SEW_DIFF_NB_OF_ELEMENTS;
6642 Sew_Error aResult = SEW_OK;
6644 // 1. Build set of faces representing each side
6645 // 2. Find which nodes of the side 1 to merge with ones on the side 2
6646 // 3. Replace nodes in elements of the side 1 and remove replaced nodes
6648 // =======================================================================
6649 // 1. Build set of faces representing each side:
6650 // =======================================================================
6651 // a. build set of nodes belonging to faces
6652 // b. complete set of faces: find missing fices whose nodes are in set of nodes
6653 // c. create temporary faces representing side of volumes if correspondent
6654 // face does not exist
6656 SMESHDS_Mesh* aMesh = GetMeshDS();
6657 SMDS_Mesh aTmpFacesMesh;
6658 set<const SMDS_MeshElement*> faceSet1, faceSet2;
6659 set<const SMDS_MeshElement*> volSet1, volSet2;
6660 set<const SMDS_MeshNode*> nodeSet1, nodeSet2;
6661 set<const SMDS_MeshElement*> * faceSetPtr[] = { &faceSet1, &faceSet2 };
6662 set<const SMDS_MeshElement*> * volSetPtr[] = { &volSet1, &volSet2 };
6663 set<const SMDS_MeshNode*> * nodeSetPtr[] = { &nodeSet1, &nodeSet2 };
6664 TIDSortedElemSet * elemSetPtr[] = { &theSide1, &theSide2 };
6665 int iSide, iFace, iNode;
6667 for ( iSide = 0; iSide < 2; iSide++ ) {
6668 set<const SMDS_MeshNode*> * nodeSet = nodeSetPtr[ iSide ];
6669 TIDSortedElemSet * elemSet = elemSetPtr[ iSide ];
6670 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
6671 set<const SMDS_MeshElement*> * volSet = volSetPtr [ iSide ];
6672 set<const SMDS_MeshElement*>::iterator vIt;
6673 TIDSortedElemSet::iterator eIt;
6674 set<const SMDS_MeshNode*>::iterator nIt;
6676 // check that given nodes belong to given elements
6677 const SMDS_MeshNode* n1 = ( iSide == 0 ) ? theFirstNode1 : theFirstNode2;
6678 const SMDS_MeshNode* n2 = ( iSide == 0 ) ? theSecondNode1 : theSecondNode2;
6679 int firstIndex = -1, secondIndex = -1;
6680 for (eIt = elemSet->begin(); eIt != elemSet->end(); eIt++ ) {
6681 const SMDS_MeshElement* elem = *eIt;
6682 if ( firstIndex < 0 ) firstIndex = elem->GetNodeIndex( n1 );
6683 if ( secondIndex < 0 ) secondIndex = elem->GetNodeIndex( n2 );
6684 if ( firstIndex > -1 && secondIndex > -1 ) break;
6686 if ( firstIndex < 0 || secondIndex < 0 ) {
6687 // we can simply return until temporary faces created
6688 return (iSide == 0 ) ? SEW_BAD_SIDE1_NODES : SEW_BAD_SIDE2_NODES;
6691 // -----------------------------------------------------------
6692 // 1a. Collect nodes of existing faces
6693 // and build set of face nodes in order to detect missing
6694 // faces corresponing to sides of volumes
6695 // -----------------------------------------------------------
6697 set< set <const SMDS_MeshNode*> > setOfFaceNodeSet;
6699 // loop on the given element of a side
6700 for (eIt = elemSet->begin(); eIt != elemSet->end(); eIt++ ) {
6701 //const SMDS_MeshElement* elem = *eIt;
6702 const SMDS_MeshElement* elem = *eIt;
6703 if ( elem->GetType() == SMDSAbs_Face ) {
6704 faceSet->insert( elem );
6705 set <const SMDS_MeshNode*> faceNodeSet;
6706 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
6707 while ( nodeIt->more() ) {
6708 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6709 nodeSet->insert( n );
6710 faceNodeSet.insert( n );
6712 setOfFaceNodeSet.insert( faceNodeSet );
6714 else if ( elem->GetType() == SMDSAbs_Volume )
6715 volSet->insert( elem );
6717 // ------------------------------------------------------------------------------
6718 // 1b. Complete set of faces: find missing fices whose nodes are in set of nodes
6719 // ------------------------------------------------------------------------------
6721 for ( nIt = nodeSet->begin(); nIt != nodeSet->end(); nIt++ ) { // loop on nodes of iSide
6722 SMDS_ElemIteratorPtr fIt = (*nIt)->GetInverseElementIterator(SMDSAbs_Face);
6723 while ( fIt->more() ) { // loop on faces sharing a node
6724 const SMDS_MeshElement* f = fIt->next();
6725 if ( faceSet->find( f ) == faceSet->end() ) {
6726 // check if all nodes are in nodeSet and
6727 // complete setOfFaceNodeSet if they are
6728 set <const SMDS_MeshNode*> faceNodeSet;
6729 SMDS_ElemIteratorPtr nodeIt = f->nodesIterator();
6730 bool allInSet = true;
6731 while ( nodeIt->more() && allInSet ) { // loop on nodes of a face
6732 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6733 if ( nodeSet->find( n ) == nodeSet->end() )
6736 faceNodeSet.insert( n );
6739 faceSet->insert( f );
6740 setOfFaceNodeSet.insert( faceNodeSet );
6746 // -------------------------------------------------------------------------
6747 // 1c. Create temporary faces representing sides of volumes if correspondent
6748 // face does not exist
6749 // -------------------------------------------------------------------------
6751 if ( !volSet->empty() ) {
6752 //int nodeSetSize = nodeSet->size();
6754 // loop on given volumes
6755 for ( vIt = volSet->begin(); vIt != volSet->end(); vIt++ ) {
6756 SMDS_VolumeTool vol (*vIt);
6757 // loop on volume faces: find free faces
6758 // --------------------------------------
6759 list<const SMDS_MeshElement* > freeFaceList;
6760 for ( iFace = 0; iFace < vol.NbFaces(); iFace++ ) {
6761 if ( !vol.IsFreeFace( iFace ))
6763 // check if there is already a face with same nodes in a face set
6764 const SMDS_MeshElement* aFreeFace = 0;
6765 const SMDS_MeshNode** fNodes = vol.GetFaceNodes( iFace );
6766 int nbNodes = vol.NbFaceNodes( iFace );
6767 set <const SMDS_MeshNode*> faceNodeSet;
6768 vol.GetFaceNodes( iFace, faceNodeSet );
6769 bool isNewFace = setOfFaceNodeSet.insert( faceNodeSet ).second;
6771 // no such a face is given but it still can exist, check it
6772 if ( nbNodes == 3 ) {
6773 aFreeFace = aMesh->FindFace( fNodes[0],fNodes[1],fNodes[2] );
6775 else if ( nbNodes == 4 ) {
6776 aFreeFace = aMesh->FindFace( fNodes[0],fNodes[1],fNodes[2],fNodes[3] );
6779 vector<const SMDS_MeshNode *> poly_nodes ( fNodes, & fNodes[nbNodes]);
6780 aFreeFace = aMesh->FindFace(poly_nodes);
6784 // create a temporary face
6785 if ( nbNodes == 3 ) {
6786 aFreeFace = aTmpFacesMesh.AddFace( fNodes[0],fNodes[1],fNodes[2] );
6788 else if ( nbNodes == 4 ) {
6789 aFreeFace = aTmpFacesMesh.AddFace( fNodes[0],fNodes[1],fNodes[2],fNodes[3] );
6792 vector<const SMDS_MeshNode *> poly_nodes ( fNodes, & fNodes[nbNodes]);
6793 aFreeFace = aTmpFacesMesh.AddPolygonalFace(poly_nodes);
6797 freeFaceList.push_back( aFreeFace );
6799 } // loop on faces of a volume
6801 // choose one of several free faces
6802 // --------------------------------------
6803 if ( freeFaceList.size() > 1 ) {
6804 // choose a face having max nb of nodes shared by other elems of a side
6805 int maxNbNodes = -1/*, nbExcludedFaces = 0*/;
6806 list<const SMDS_MeshElement* >::iterator fIt = freeFaceList.begin();
6807 while ( fIt != freeFaceList.end() ) { // loop on free faces
6808 int nbSharedNodes = 0;
6809 SMDS_ElemIteratorPtr nodeIt = (*fIt)->nodesIterator();
6810 while ( nodeIt->more() ) { // loop on free face nodes
6811 const SMDS_MeshNode* n =
6812 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6813 SMDS_ElemIteratorPtr invElemIt = n->GetInverseElementIterator();
6814 while ( invElemIt->more() ) {
6815 const SMDS_MeshElement* e = invElemIt->next();
6816 if ( faceSet->find( e ) != faceSet->end() )
6818 if ( elemSet->find( e ) != elemSet->end() )
6822 if ( nbSharedNodes >= maxNbNodes ) {
6823 maxNbNodes = nbSharedNodes;
6827 freeFaceList.erase( fIt++ ); // here fIt++ occures before erase
6829 if ( freeFaceList.size() > 1 )
6831 // could not choose one face, use another way
6832 // choose a face most close to the bary center of the opposite side
6833 gp_XYZ aBC( 0., 0., 0. );
6834 set <const SMDS_MeshNode*> addedNodes;
6835 TIDSortedElemSet * elemSet2 = elemSetPtr[ 1 - iSide ];
6836 eIt = elemSet2->begin();
6837 for ( eIt = elemSet2->begin(); eIt != elemSet2->end(); eIt++ ) {
6838 SMDS_ElemIteratorPtr nodeIt = (*eIt)->nodesIterator();
6839 while ( nodeIt->more() ) { // loop on free face nodes
6840 const SMDS_MeshNode* n =
6841 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6842 if ( addedNodes.insert( n ).second )
6843 aBC += gp_XYZ( n->X(),n->Y(),n->Z() );
6846 aBC /= addedNodes.size();
6847 double minDist = DBL_MAX;
6848 fIt = freeFaceList.begin();
6849 while ( fIt != freeFaceList.end() ) { // loop on free faces
6851 SMDS_ElemIteratorPtr nodeIt = (*fIt)->nodesIterator();
6852 while ( nodeIt->more() ) { // loop on free face nodes
6853 const SMDS_MeshNode* n =
6854 static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6855 gp_XYZ p( n->X(),n->Y(),n->Z() );
6856 dist += ( aBC - p ).SquareModulus();
6858 if ( dist < minDist ) {
6860 freeFaceList.erase( freeFaceList.begin(), fIt++ );
6863 fIt = freeFaceList.erase( fIt++ );
6866 } // choose one of several free faces of a volume
6868 if ( freeFaceList.size() == 1 ) {
6869 const SMDS_MeshElement* aFreeFace = freeFaceList.front();
6870 faceSet->insert( aFreeFace );
6871 // complete a node set with nodes of a found free face
6872 // for ( iNode = 0; iNode < ; iNode++ )
6873 // nodeSet->insert( fNodes[ iNode ] );
6876 } // loop on volumes of a side
6878 // // complete a set of faces if new nodes in a nodeSet appeared
6879 // // ----------------------------------------------------------
6880 // if ( nodeSetSize != nodeSet->size() ) {
6881 // for ( ; nIt != nodeSet->end(); nIt++ ) { // loop on nodes of iSide
6882 // SMDS_ElemIteratorPtr fIt = (*nIt)->GetInverseElementIterator(SMDSAbs_Face);
6883 // while ( fIt->more() ) { // loop on faces sharing a node
6884 // const SMDS_MeshElement* f = fIt->next();
6885 // if ( faceSet->find( f ) == faceSet->end() ) {
6886 // // check if all nodes are in nodeSet and
6887 // // complete setOfFaceNodeSet if they are
6888 // set <const SMDS_MeshNode*> faceNodeSet;
6889 // SMDS_ElemIteratorPtr nodeIt = f->nodesIterator();
6890 // bool allInSet = true;
6891 // while ( nodeIt->more() && allInSet ) { // loop on nodes of a face
6892 // const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
6893 // if ( nodeSet->find( n ) == nodeSet->end() )
6894 // allInSet = false;
6896 // faceNodeSet.insert( n );
6898 // if ( allInSet ) {
6899 // faceSet->insert( f );
6900 // setOfFaceNodeSet.insert( faceNodeSet );
6906 } // Create temporary faces, if there are volumes given
6909 if ( faceSet1.size() != faceSet2.size() ) {
6910 // delete temporary faces: they are in reverseElements of actual nodes
6911 SMDS_FaceIteratorPtr tmpFaceIt = aTmpFacesMesh.facesIterator();
6912 while ( tmpFaceIt->more() )
6913 aTmpFacesMesh.RemoveElement( tmpFaceIt->next() );
6914 MESSAGE("Diff nb of faces");
6915 return SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
6918 // ============================================================
6919 // 2. Find nodes to merge:
6920 // bind a node to remove to a node to put instead
6921 // ============================================================
6923 TNodeNodeMap nReplaceMap; // bind a node to remove to a node to put instead
6924 if ( theFirstNode1 != theFirstNode2 )
6925 nReplaceMap.insert( TNodeNodeMap::value_type( theFirstNode1, theFirstNode2 ));
6926 if ( theSecondNode1 != theSecondNode2 )
6927 nReplaceMap.insert( TNodeNodeMap::value_type( theSecondNode1, theSecondNode2 ));
6929 LinkID_Gen aLinkID_Gen( GetMeshDS() );
6930 set< long > linkIdSet; // links to process
6931 linkIdSet.insert( aLinkID_Gen.GetLinkID( theFirstNode1, theSecondNode1 ));
6933 typedef pair< const SMDS_MeshNode*, const SMDS_MeshNode* > NLink;
6934 list< NLink > linkList[2];
6935 linkList[0].push_back( NLink( theFirstNode1, theSecondNode1 ));
6936 linkList[1].push_back( NLink( theFirstNode2, theSecondNode2 ));
6937 // loop on links in linkList; find faces by links and append links
6938 // of the found faces to linkList
6939 list< NLink >::iterator linkIt[] = { linkList[0].begin(), linkList[1].begin() } ;
6940 for ( ; linkIt[0] != linkList[0].end(); linkIt[0]++, linkIt[1]++ ) {
6941 NLink link[] = { *linkIt[0], *linkIt[1] };
6942 long linkID = aLinkID_Gen.GetLinkID( link[0].first, link[0].second );
6943 if ( linkIdSet.find( linkID ) == linkIdSet.end() )
6946 // by links, find faces in the face sets,
6947 // and find indices of link nodes in the found faces;
6948 // in a face set, there is only one or no face sharing a link
6949 // ---------------------------------------------------------------
6951 const SMDS_MeshElement* face[] = { 0, 0 };
6952 //const SMDS_MeshNode* faceNodes[ 2 ][ 5 ];
6953 vector<const SMDS_MeshNode*> fnodes1(9);
6954 vector<const SMDS_MeshNode*> fnodes2(9);
6955 //const SMDS_MeshNode* notLinkNodes[ 2 ][ 2 ] = {{ 0, 0 },{ 0, 0 }} ;
6956 vector<const SMDS_MeshNode*> notLinkNodes1(6);
6957 vector<const SMDS_MeshNode*> notLinkNodes2(6);
6958 int iLinkNode[2][2];
6959 for ( iSide = 0; iSide < 2; iSide++ ) { // loop on 2 sides
6960 const SMDS_MeshNode* n1 = link[iSide].first;
6961 const SMDS_MeshNode* n2 = link[iSide].second;
6962 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
6963 set< const SMDS_MeshElement* > fMap;
6964 for ( int i = 0; i < 2; i++ ) { // loop on 2 nodes of a link
6965 const SMDS_MeshNode* n = i ? n1 : n2; // a node of a link
6966 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
6967 while ( fIt->more() ) { // loop on faces sharing a node
6968 const SMDS_MeshElement* f = fIt->next();
6969 if (faceSet->find( f ) != faceSet->end() && // f is in face set
6970 ! fMap.insert( f ).second ) // f encounters twice
6972 if ( face[ iSide ] ) {
6973 MESSAGE( "2 faces per link " );
6974 aResult = iSide ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES;
6978 faceSet->erase( f );
6979 // get face nodes and find ones of a link
6984 fnodes1.resize(f->NbNodes()+1);
6985 notLinkNodes1.resize(f->NbNodes()-2);
6988 fnodes2.resize(f->NbNodes()+1);
6989 notLinkNodes2.resize(f->NbNodes()-2);
6992 if(!f->IsQuadratic()) {
6993 SMDS_ElemIteratorPtr nIt = f->nodesIterator();
6994 while ( nIt->more() ) {
6995 const SMDS_MeshNode* n =
6996 static_cast<const SMDS_MeshNode*>( nIt->next() );
6998 iLinkNode[ iSide ][ 0 ] = iNode;
7000 else if ( n == n2 ) {
7001 iLinkNode[ iSide ][ 1 ] = iNode;
7003 //else if ( notLinkNodes[ iSide ][ 0 ] )
7004 // notLinkNodes[ iSide ][ 1 ] = n;
7006 // notLinkNodes[ iSide ][ 0 ] = n;
7010 notLinkNodes1[nbl] = n;
7011 //notLinkNodes1.push_back(n);
7013 notLinkNodes2[nbl] = n;
7014 //notLinkNodes2.push_back(n);
7016 //faceNodes[ iSide ][ iNode++ ] = n;
7018 fnodes1[iNode++] = n;
7021 fnodes2[iNode++] = n;
7025 else { // f->IsQuadratic()
7026 const SMDS_QuadraticFaceOfNodes* F =
7027 static_cast<const SMDS_QuadraticFaceOfNodes*>(f);
7028 // use special nodes iterator
7029 SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
7030 while ( anIter->more() ) {
7031 const SMDS_MeshNode* n =
7032 static_cast<const SMDS_MeshNode*>( anIter->next() );
7034 iLinkNode[ iSide ][ 0 ] = iNode;
7036 else if ( n == n2 ) {
7037 iLinkNode[ iSide ][ 1 ] = iNode;
7042 notLinkNodes1[nbl] = n;
7045 notLinkNodes2[nbl] = n;
7049 fnodes1[iNode++] = n;
7052 fnodes2[iNode++] = n;
7056 //faceNodes[ iSide ][ iNode ] = faceNodes[ iSide ][ 0 ];
7058 fnodes1[iNode] = fnodes1[0];
7061 fnodes2[iNode] = fnodes1[0];
7068 // check similarity of elements of the sides
7069 if (aResult == SEW_OK && ( face[0] && !face[1] ) || ( !face[0] && face[1] )) {
7070 MESSAGE("Correspondent face not found on side " << ( face[0] ? 1 : 0 ));
7071 if ( nReplaceMap.size() == 2 ) { // faces on input nodes not found
7072 aResult = ( face[0] ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES );
7075 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
7077 break; // do not return because it s necessary to remove tmp faces
7080 // set nodes to merge
7081 // -------------------
7083 if ( face[0] && face[1] ) {
7084 int nbNodes = face[0]->NbNodes();
7085 if ( nbNodes != face[1]->NbNodes() ) {
7086 MESSAGE("Diff nb of face nodes");
7087 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
7088 break; // do not return because it s necessary to remove tmp faces
7090 bool reverse[] = { false, false }; // order of notLinkNodes of quadrangle
7091 if ( nbNodes == 3 ) {
7092 //nReplaceMap.insert( TNodeNodeMap::value_type
7093 // ( notLinkNodes[0][0], notLinkNodes[1][0] ));
7094 nReplaceMap.insert( TNodeNodeMap::value_type
7095 ( notLinkNodes1[0], notLinkNodes2[0] ));
7098 for ( iSide = 0; iSide < 2; iSide++ ) { // loop on 2 sides
7099 // analyse link orientation in faces
7100 int i1 = iLinkNode[ iSide ][ 0 ];
7101 int i2 = iLinkNode[ iSide ][ 1 ];
7102 reverse[ iSide ] = Abs( i1 - i2 ) == 1 ? i1 > i2 : i2 > i1;
7103 // if notLinkNodes are the first and the last ones, then
7104 // their order does not correspond to the link orientation
7105 if (( i1 == 1 && i2 == 2 ) ||
7106 ( i1 == 2 && i2 == 1 ))
7107 reverse[ iSide ] = !reverse[ iSide ];
7109 if ( reverse[0] == reverse[1] ) {
7110 //nReplaceMap.insert( TNodeNodeMap::value_type
7111 // ( notLinkNodes[0][0], notLinkNodes[1][0] ));
7112 //nReplaceMap.insert( TNodeNodeMap::value_type
7113 // ( notLinkNodes[0][1], notLinkNodes[1][1] ));
7114 for(int nn=0; nn<nbNodes-2; nn++) {
7115 nReplaceMap.insert( TNodeNodeMap::value_type
7116 ( notLinkNodes1[nn], notLinkNodes2[nn] ));
7120 //nReplaceMap.insert( TNodeNodeMap::value_type
7121 // ( notLinkNodes[0][0], notLinkNodes[1][1] ));
7122 //nReplaceMap.insert( TNodeNodeMap::value_type
7123 // ( notLinkNodes[0][1], notLinkNodes[1][0] ));
7124 for(int nn=0; nn<nbNodes-2; nn++) {
7125 nReplaceMap.insert( TNodeNodeMap::value_type
7126 ( notLinkNodes1[nn], notLinkNodes2[nbNodes-3-nn] ));
7131 // add other links of the faces to linkList
7132 // -----------------------------------------
7134 //const SMDS_MeshNode** nodes = faceNodes[ 0 ];
7135 for ( iNode = 0; iNode < nbNodes; iNode++ ) {
7136 //linkID = aLinkID_Gen.GetLinkID( nodes[iNode], nodes[iNode+1] );
7137 linkID = aLinkID_Gen.GetLinkID( fnodes1[iNode], fnodes1[iNode+1] );
7138 pair< set<long>::iterator, bool > iter_isnew = linkIdSet.insert( linkID );
7139 if ( !iter_isnew.second ) { // already in a set: no need to process
7140 linkIdSet.erase( iter_isnew.first );
7142 else // new in set == encountered for the first time: add
7144 //const SMDS_MeshNode* n1 = nodes[ iNode ];
7145 //const SMDS_MeshNode* n2 = nodes[ iNode + 1];
7146 const SMDS_MeshNode* n1 = fnodes1[ iNode ];
7147 const SMDS_MeshNode* n2 = fnodes1[ iNode + 1];
7148 linkList[0].push_back ( NLink( n1, n2 ));
7149 linkList[1].push_back ( NLink( nReplaceMap[n1], nReplaceMap[n2] ));
7153 } // loop on link lists
7155 if ( aResult == SEW_OK &&
7156 ( linkIt[0] != linkList[0].end() ||
7157 !faceSetPtr[0]->empty() || !faceSetPtr[1]->empty() )) {
7158 MESSAGE( (linkIt[0] != linkList[0].end()) <<" "<< (faceSetPtr[0]->empty()) <<
7159 " " << (faceSetPtr[1]->empty()));
7160 aResult = SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
7163 // ====================================================================
7164 // 3. Replace nodes in elements of the side 1 and remove replaced nodes
7165 // ====================================================================
7167 // delete temporary faces: they are in reverseElements of actual nodes
7168 SMDS_FaceIteratorPtr tmpFaceIt = aTmpFacesMesh.facesIterator();
7169 while ( tmpFaceIt->more() )
7170 aTmpFacesMesh.RemoveElement( tmpFaceIt->next() );
7172 if ( aResult != SEW_OK)
7175 list< int > nodeIDsToRemove/*, elemIDsToRemove*/;
7176 // loop on nodes replacement map
7177 TNodeNodeMap::iterator nReplaceMapIt = nReplaceMap.begin(), nnIt;
7178 for ( ; nReplaceMapIt != nReplaceMap.end(); nReplaceMapIt++ )
7179 if ( (*nReplaceMapIt).first != (*nReplaceMapIt).second ) {
7180 const SMDS_MeshNode* nToRemove = (*nReplaceMapIt).first;
7181 nodeIDsToRemove.push_back( nToRemove->GetID() );
7182 // loop on elements sharing nToRemove
7183 SMDS_ElemIteratorPtr invElemIt = nToRemove->GetInverseElementIterator();
7184 while ( invElemIt->more() ) {
7185 const SMDS_MeshElement* e = invElemIt->next();
7186 // get a new suite of nodes: make replacement
7187 int nbReplaced = 0, i = 0, nbNodes = e->NbNodes();
7188 vector< const SMDS_MeshNode*> nodes( nbNodes );
7189 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
7190 while ( nIt->more() ) {
7191 const SMDS_MeshNode* n =
7192 static_cast<const SMDS_MeshNode*>( nIt->next() );
7193 nnIt = nReplaceMap.find( n );
7194 if ( nnIt != nReplaceMap.end() ) {
7200 // if ( nbReplaced == nbNodes && e->GetType() == SMDSAbs_Face )
7201 // elemIDsToRemove.push_back( e->GetID() );
7204 aMesh->ChangeElementNodes( e, & nodes[0], nbNodes );
7208 Remove( nodeIDsToRemove, true );
7213 //================================================================================
7215 * \brief Find corresponding nodes in two sets of faces
7216 * \param theSide1 - first face set
7217 * \param theSide2 - second first face
7218 * \param theFirstNode1 - a boundary node of set 1
7219 * \param theFirstNode2 - a node of set 2 corresponding to theFirstNode1
7220 * \param theSecondNode1 - a boundary node of set 1 linked with theFirstNode1
7221 * \param theSecondNode2 - a node of set 2 corresponding to theSecondNode1
7222 * \param nReplaceMap - output map of corresponding nodes
7223 * \retval bool - is a success or not
7225 //================================================================================
7227 //#define DEBUG_MATCHING_NODES
7229 SMESH_MeshEditor::Sew_Error
7230 SMESH_MeshEditor::FindMatchingNodes(set<const SMDS_MeshElement*>& theSide1,
7231 set<const SMDS_MeshElement*>& theSide2,
7232 const SMDS_MeshNode* theFirstNode1,
7233 const SMDS_MeshNode* theFirstNode2,
7234 const SMDS_MeshNode* theSecondNode1,
7235 const SMDS_MeshNode* theSecondNode2,
7236 TNodeNodeMap & nReplaceMap)
7238 set<const SMDS_MeshElement*> * faceSetPtr[] = { &theSide1, &theSide2 };
7240 nReplaceMap.clear();
7241 if ( theFirstNode1 != theFirstNode2 )
7242 nReplaceMap.insert( make_pair( theFirstNode1, theFirstNode2 ));
7243 if ( theSecondNode1 != theSecondNode2 )
7244 nReplaceMap.insert( make_pair( theSecondNode1, theSecondNode2 ));
7246 set< TLink > linkSet; // set of nodes where order of nodes is ignored
7247 linkSet.insert( TLink( theFirstNode1, theSecondNode1 ));
7249 list< NLink > linkList[2];
7250 linkList[0].push_back( NLink( theFirstNode1, theSecondNode1 ));
7251 linkList[1].push_back( NLink( theFirstNode2, theSecondNode2 ));
7253 // loop on links in linkList; find faces by links and append links
7254 // of the found faces to linkList
7255 list< NLink >::iterator linkIt[] = { linkList[0].begin(), linkList[1].begin() } ;
7256 for ( ; linkIt[0] != linkList[0].end(); linkIt[0]++, linkIt[1]++ ) {
7257 NLink link[] = { *linkIt[0], *linkIt[1] };
7258 if ( linkSet.find( link[0] ) == linkSet.end() )
7261 // by links, find faces in the face sets,
7262 // and find indices of link nodes in the found faces;
7263 // in a face set, there is only one or no face sharing a link
7264 // ---------------------------------------------------------------
7266 const SMDS_MeshElement* face[] = { 0, 0 };
7267 list<const SMDS_MeshNode*> notLinkNodes[2];
7268 //bool reverse[] = { false, false }; // order of notLinkNodes
7270 for ( int iSide = 0; iSide < 2; iSide++ ) // loop on 2 sides
7272 const SMDS_MeshNode* n1 = link[iSide].first;
7273 const SMDS_MeshNode* n2 = link[iSide].second;
7274 set<const SMDS_MeshElement*> * faceSet = faceSetPtr[ iSide ];
7275 set< const SMDS_MeshElement* > facesOfNode1;
7276 for ( int iNode = 0; iNode < 2; iNode++ ) // loop on 2 nodes of a link
7278 // during a loop of the first node, we find all faces around n1,
7279 // during a loop of the second node, we find one face sharing both n1 and n2
7280 const SMDS_MeshNode* n = iNode ? n1 : n2; // a node of a link
7281 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
7282 while ( fIt->more() ) { // loop on faces sharing a node
7283 const SMDS_MeshElement* f = fIt->next();
7284 if (faceSet->find( f ) != faceSet->end() && // f is in face set
7285 ! facesOfNode1.insert( f ).second ) // f encounters twice
7287 if ( face[ iSide ] ) {
7288 MESSAGE( "2 faces per link " );
7289 return ( iSide ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES );
7292 faceSet->erase( f );
7294 // get not link nodes
7295 int nbN = f->NbNodes();
7296 if ( f->IsQuadratic() )
7298 nbNodes[ iSide ] = nbN;
7299 list< const SMDS_MeshNode* > & nodes = notLinkNodes[ iSide ];
7300 int i1 = f->GetNodeIndex( n1 );
7301 int i2 = f->GetNodeIndex( n2 );
7302 int iEnd = nbN, iBeg = -1, iDelta = 1;
7303 bool reverse = ( Abs( i1 - i2 ) == 1 ? i1 > i2 : i2 > i1 );
7305 std::swap( iEnd, iBeg ); iDelta = -1;
7310 if ( i == iEnd ) i = iBeg + iDelta;
7311 if ( i == i1 ) break;
7312 nodes.push_back ( f->GetNode( i ) );
7318 // check similarity of elements of the sides
7319 if (( face[0] && !face[1] ) || ( !face[0] && face[1] )) {
7320 MESSAGE("Correspondent face not found on side " << ( face[0] ? 1 : 0 ));
7321 if ( nReplaceMap.size() == 2 ) { // faces on input nodes not found
7322 return ( face[0] ? SEW_BAD_SIDE2_NODES : SEW_BAD_SIDE1_NODES );
7325 return SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
7329 // set nodes to merge
7330 // -------------------
7332 if ( face[0] && face[1] ) {
7333 if ( nbNodes[0] != nbNodes[1] ) {
7334 MESSAGE("Diff nb of face nodes");
7335 return SEW_TOPO_DIFF_SETS_OF_ELEMENTS;
7337 #ifdef DEBUG_MATCHING_NODES
7338 cout << " Link 1: " << link[0].first->GetID() <<" "<< link[0].second->GetID()
7339 << " F 1: " << face[0];
7340 cout << "| Link 2: " << link[1].first->GetID() <<" "<< link[1].second->GetID()
7341 << " F 2: " << face[1] << " | Bind: "<<endl ;
7343 int nbN = nbNodes[0];
7345 list<const SMDS_MeshNode*>::iterator n1 = notLinkNodes[0].begin();
7346 list<const SMDS_MeshNode*>::iterator n2 = notLinkNodes[1].begin();
7347 for ( int i = 0 ; i < nbN - 2; ++i ) {
7348 #ifdef DEBUG_MATCHING_NODES
7349 cout << (*n1)->GetID() << " to " << (*n2)->GetID() << endl;
7351 nReplaceMap.insert( make_pair( *(n1++), *(n2++) ));
7355 // add other links of the face 1 to linkList
7356 // -----------------------------------------
7358 const SMDS_MeshElement* f0 = face[0];
7359 const SMDS_MeshNode* n1 = f0->GetNode( nbN - 1 );
7360 for ( int i = 0; i < nbN; i++ )
7362 const SMDS_MeshNode* n2 = f0->GetNode( i );
7363 pair< set< TLink >::iterator, bool > iter_isnew =
7364 linkSet.insert( TLink( n1, n2 ));
7365 if ( !iter_isnew.second ) { // already in a set: no need to process
7366 linkSet.erase( iter_isnew.first );
7368 else // new in set == encountered for the first time: add
7370 #ifdef DEBUG_MATCHING_NODES
7371 cout << "Add link 1: " << n1->GetID() << " " << n2->GetID() << " ";
7372 cout << " | link 2: " << nReplaceMap[n1]->GetID() << " " << nReplaceMap[n2]->GetID() << " " << endl;
7374 linkList[0].push_back ( NLink( n1, n2 ));
7375 linkList[1].push_back ( NLink( nReplaceMap[n1], nReplaceMap[n2] ));
7380 } // loop on link lists