1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : StdMeshers_Import_1D2D.cxx
27 #include "StdMeshers_Import_1D2D.hxx"
29 #include "StdMeshers_Import_1D.hxx"
30 #include "StdMeshers_ImportSource.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMESHDS_Group.hxx"
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESH_Comment.hxx"
37 #include "SMESH_Gen.hxx"
38 #include "SMESH_Group.hxx"
39 #include "SMESH_Mesh.hxx"
40 #include "SMESH_MesherHelper.hxx"
41 #include "SMESH_subMesh.hxx"
43 #include "Utils_SALOME_Exception.hxx"
44 #include "utilities.h"
46 #include <BRep_Builder.hxx>
47 #include <BRep_Tool.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <TopoDS_Compound.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <Precision.hxx>
62 double getMinElemSize2( const SMESHDS_GroupBase* srcGroup )
64 double minSize2 = 1e100;
65 SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
66 while ( srcElems->more() ) // loop on group contents
68 const SMDS_MeshElement* face = srcElems->next();
69 int nbN = face->NbCornerNodes();
71 SMESH_TNodeXYZ prevN( face->GetNode( nbN-1 ));
72 for ( int i = 0; i < nbN; ++i )
74 SMESH_TNodeXYZ n( face->GetNode( i ) );
75 double size2 = ( n - prevN ).SquareModulus();
76 minSize2 = std::min( minSize2, size2 );
84 //=============================================================================
86 * Creates StdMeshers_Import_1D2D
88 //=============================================================================
90 StdMeshers_Import_1D2D::StdMeshers_Import_1D2D(int hypId, int studyId, SMESH_Gen * gen)
91 :SMESH_2D_Algo(hypId, studyId, gen), _sourceHyp(0)
93 MESSAGE("StdMeshers_Import_1D2D::StdMeshers_Import_1D2D");
94 _name = "Import_1D2D";
95 _shapeType = (1 << TopAbs_FACE);
97 _compatibleHypothesis.push_back("ImportSource2D");
98 _requireDiscreteBoundary = false;
101 //=============================================================================
103 * Check presence of a hypothesis
105 //=============================================================================
107 bool StdMeshers_Import_1D2D::CheckHypothesis
109 const TopoDS_Shape& aShape,
110 SMESH_Hypothesis::Hypothesis_Status& aStatus)
114 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
115 if ( hyps.size() == 0 )
117 aStatus = SMESH_Hypothesis::HYP_MISSING;
118 return false; // can't work with no hypothesis
121 if ( hyps.size() > 1 )
123 aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
127 const SMESHDS_Hypothesis *theHyp = hyps.front();
129 string hypName = theHyp->GetName();
131 if (hypName == _compatibleHypothesis.front())
133 _sourceHyp = (StdMeshers_ImportSource1D *)theHyp;
134 aStatus = SMESH_Hypothesis::HYP_OK;
138 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
145 * \brief OrientedLink additionally storing a medium node
147 struct TLink : public SMESH_OrientedLink
149 const SMDS_MeshNode* _medium;
150 TLink( const SMDS_MeshNode* n1,
151 const SMDS_MeshNode* n2,
152 const SMDS_MeshNode* medium=0)
153 : SMESH_OrientedLink( n1,n2 ), _medium( medium ) {}
157 //=============================================================================
159 * Import elements from the other mesh
161 //=============================================================================
163 bool StdMeshers_Import_1D2D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
165 if ( !_sourceHyp ) return false;
167 const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups(/*loaded=*/true);
168 if ( srcGroups.empty() )
169 return error("Invalid source groups");
171 bool allGroupsEmpty = true;
172 for ( size_t iG = 0; iG < srcGroups.size() && allGroupsEmpty; ++iG )
173 allGroupsEmpty = srcGroups[iG]->GetGroupDS()->IsEmpty();
174 if ( allGroupsEmpty )
175 return error("No faces in source groups");
177 SMESH_MesherHelper helper(theMesh);
178 helper.SetSubShape(theShape);
179 SMESHDS_Mesh* tgtMesh = theMesh.GetMeshDS();
181 const TopoDS_Face& geomFace = TopoDS::Face( theShape );
182 const double faceTol = helper.MaxTolerance( geomFace );
183 const int shapeID = tgtMesh->ShapeToIndex( geomFace );
184 const bool toCheckOri = (helper.NbAncestors( geomFace, theMesh, TopAbs_SOLID ) == 1 );
186 Handle(Geom_Surface) surface = BRep_Tool::Surface( geomFace );
188 ( helper.GetSubShapeOri( tgtMesh->ShapeToMesh(), geomFace) == TopAbs_REVERSED );
189 gp_Pnt p; gp_Vec du, dv;
191 set<int> subShapeIDs;
192 subShapeIDs.insert( shapeID );
194 // get nodes on vertices
195 list < SMESH_TNodeXYZ > vertexNodes;
196 list < SMESH_TNodeXYZ >::iterator vNIt;
197 TopExp_Explorer exp( theShape, TopAbs_VERTEX );
198 for ( ; exp.More(); exp.Next() )
200 const TopoDS_Vertex& v = TopoDS::Vertex( exp.Current() );
201 if ( !subShapeIDs.insert( tgtMesh->ShapeToIndex( v )).second )
203 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, tgtMesh );
206 _gen->Compute(theMesh,v,/*anUpward=*/true);
207 n = SMESH_Algo::VertexNode( v, tgtMesh );
208 if ( !n ) return false; // very strange
210 vertexNodes.push_back( SMESH_TNodeXYZ( n ));
213 // get EDGESs and their ids
214 vector< TopoDS_Edge > edges;
215 for ( exp.Init( theShape, TopAbs_EDGE ); exp.More(); exp.Next() )
216 if ( subShapeIDs.insert( tgtMesh->ShapeToIndex( exp.Current() )).second )
217 edges.push_back( TopoDS::Edge( exp.Current() ));
219 // to count now many times a link between nodes encounters
220 map<TLink, int> linkCount;
221 map<TLink, int>::iterator link2Nb;
222 double minGroupTol = Precision::Infinite();
224 // =========================
225 // Import faces from groups
226 // =========================
228 StdMeshers_Import_1D::TNodeNodeMap* n2n;
229 StdMeshers_Import_1D::TElemElemMap* e2e;
230 vector<const SMDS_MeshNode*> newNodes;
231 for ( size_t iG = 0; iG < srcGroups.size(); ++iG )
233 const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
235 const int meshID = srcGroup->GetMesh()->GetPersistentId();
236 const SMESH_Mesh* srcMesh = GetMeshByPersistentID( meshID );
237 if ( !srcMesh ) continue;
238 StdMeshers_Import_1D::getMaps( srcMesh, &theMesh, n2n, e2e );
240 const double groupTol = 0.5 * sqrt( getMinElemSize2( srcGroup ));
241 minGroupTol = std::min( groupTol, minGroupTol );
243 SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
244 SMDS_MeshNode *tmpNode = helper.AddNode(0,0,0);
245 gp_XY uv( Precision::Infinite(), Precision::Infinite() );
246 while ( srcElems->more() ) // loop on group contents
248 const SMDS_MeshElement* face = srcElems->next();
249 // find or create nodes of a new face
250 newNodes.resize( face->NbNodes() );
252 int nbCreatedNodes = 0;
253 SMDS_MeshElement::iterator node = face->begin_nodes();
254 for ( size_t i = 0; i < newNodes.size(); ++i, ++node )
256 StdMeshers_Import_1D::TNodeNodeMap::iterator n2nIt =
257 n2n->insert( make_pair( *node, (SMDS_MeshNode*)0 )).first;
260 if ( !subShapeIDs.count( n2nIt->second->getshapeId() )) // node already on an EDGE
265 // find an existing vertex node
266 for ( vNIt = vertexNodes.begin(); vNIt != vertexNodes.end(); ++vNIt)
267 if ( vNIt->SquareDistance( *node ) < groupTol * groupTol)
269 (*n2nIt).second = vNIt->_node;
270 vertexNodes.erase( vNIt );
274 if ( !n2nIt->second )
276 // find out if node lies on theShape
277 tmpNode->setXYZ( (*node)->X(), (*node)->Y(), (*node)->Z());
278 uv.SetCoord( Precision::Infinite(), Precision::Infinite() );
279 if ( helper.CheckNodeUV( geomFace, tmpNode, uv, groupTol, /*force=*/true ))
281 SMDS_MeshNode* newNode = tgtMesh->AddNode( (*node)->X(), (*node)->Y(), (*node)->Z());
282 n2nIt->second = newNode;
283 tgtMesh->SetNodeOnFace( newNode, shapeID, uv.X(), uv.Y() );
287 if ( !(newNodes[i] = n2nIt->second ))
290 if ( !newNodes.back() )
291 continue; // not all nodes of the face lie on theShape
293 // try to find already created face
294 SMDS_MeshElement * newFace = 0;
295 if ( nbCreatedNodes == 0 &&
296 tgtMesh->FindElement(newNodes, SMDSAbs_Face, /*noMedium=*/false))
297 continue; // repeated face in source groups already created
299 // check future face orientation
300 const int nbCorners = face->NbCornerNodes();
301 const bool isQuad = ( nbCorners != (int) newNodes.size() );
308 uv = helper.GetNodeUV( geomFace, newNodes[++iNode] );
309 surface->D1( uv.X(),uv.Y(), p, du,dv );
310 geomNorm = reverse ? dv^du : du^dv;
312 while ( geomNorm.SquareMagnitude() < 1e-6 && iNode+1 < nbCorners );
314 int iNext = helper.WrapIndex( iNode+1, nbCorners );
315 int iPrev = helper.WrapIndex( iNode-1, nbCorners );
317 SMESH_TNodeXYZ prevNode( newNodes[iPrev] );
318 SMESH_TNodeXYZ curNode ( newNodes[iNode] );
319 SMESH_TNodeXYZ nextNode( newNodes[iNext] );
320 gp_Vec n1n0( prevNode - curNode);
321 gp_Vec n1n2( nextNode - curNode );
322 gp_Vec meshNorm = n1n2 ^ n1n0;
324 if ( geomNorm * meshNorm < 0 )
325 SMDS_MeshCell::applyInterlace
326 ( SMDS_MeshCell::reverseSmdsOrder( face->GetEntityType() ), newNodes );
330 if ( face->IsPoly() )
331 newFace = tgtMesh->AddPolygonalFace( newNodes );
333 switch ( newNodes.size() )
336 newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2] );
339 newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3] );
342 newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2],
343 newNodes[3], newNodes[4], newNodes[5]);
346 newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3],
347 newNodes[4], newNodes[5], newNodes[6], newNodes[7]);
351 tgtMesh->SetMeshElementOnShape( newFace, shapeID );
352 e2e->insert( make_pair( face, newFace ));
355 const SMDS_MeshNode* medium = 0;
356 for ( int i = 0; i < nbCorners; ++i )
358 const SMDS_MeshNode* n1 = newNodes[i];
359 const SMDS_MeshNode* n2 = newNodes[ (i+1)%nbCorners ];
360 if ( isQuad ) // quadratic face
361 medium = newNodes[i+nbCorners];
362 link2Nb = linkCount.insert( make_pair( TLink( n1, n2, medium ), 0)).first;
364 // if ( link2Nb->second == 1 )
366 // // measure link length
367 // double len2 = SMESH_TNodeXYZ( n1 ).SquareDistance( n2 );
368 // if ( len2 < minGroupTol )
369 // minGroupTol = len2;
373 helper.GetMeshDS()->RemoveNode(tmpNode);
376 // ==========================================================
377 // Put nodes on geom edges and create edges on them;
378 // check if the whole geom face is covered by imported faces
379 // ==========================================================
381 // use large tolerance for projection of nodes to edges because of
382 // BLSURF mesher specifics (issue 0020918, Study2.hdf)
383 const double projTol = minGroupTol;
385 bool isFaceMeshed = false;
386 SMESHDS_SubMesh* tgtFaceSM = tgtMesh->MeshElements( theShape );
389 // the imported mesh is valid if all external links (encountered once)
391 subShapeIDs.erase( shapeID ); // to contain edges and vertices only
393 for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); ++link2Nb)
395 const TLink& link = (*link2Nb).first;
396 int nbFaces = link2Nb->second;
399 // check if a not shared link lies on face boundary
400 bool nodesOnBoundary = true;
401 list< TopoDS_Shape > bndShapes;
402 for ( int is1stN = 0; is1stN < 2 && nodesOnBoundary; ++is1stN )
404 const SMDS_MeshNode* n = is1stN ? link.node1() : link.node2();
405 if ( !subShapeIDs.count( n->getshapeId() )) // n is assigned to FACE
407 for ( size_t iE = 0; iE < edges.size(); ++iE )
408 if ( helper.CheckNodeU( edges[iE], n, u=0, projTol, /*force=*/true ))
410 BRep_Tool::Range(edges[iE],f,l);
411 if ( Abs(u-f) < 2 * faceTol || Abs(u-l) < 2 * faceTol )
412 // duplicated node on vertex
413 return error("Source elements overlap one another");
414 tgtFaceSM->RemoveNode( n, /*isNodeDeleted=*/false );
415 tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)n, edges[iE], u );
418 nodesOnBoundary = subShapeIDs.count( n->getshapeId());
420 if ( nodesOnBoundary )
422 TopoDS_Shape s = helper.GetSubShapeByNode( n, tgtMesh );
423 if ( s.ShapeType() == TopAbs_VERTEX )
424 bndShapes.push_front( s ); // vertex first
426 bndShapes.push_back( s ); // edges last
429 if ( !nodesOnBoundary )
431 error("free internal link"); // just for an easier debug
434 if ( bndShapes.front().ShapeType() == TopAbs_EDGE && // all link nodes are on EDGEs
435 bndShapes.front() != bndShapes.back() )
436 // link nodes on different geom edges
437 return error(COMPERR_BAD_INPUT_MESH, "Source nodes mismatch target vertices");
439 // find geom edge the link is on
440 if ( bndShapes.back().ShapeType() != TopAbs_EDGE ) // all link nodes are on VERTEXes
442 // find geom edge by two vertices
443 TopoDS_Shape geomEdge = helper.GetCommonAncestor( bndShapes.back(),
445 theMesh, TopAbs_EDGE );
446 if ( geomEdge.IsNull() )
448 error("free internal link");
449 break; // vertices belong to different edges
451 bndShapes.push_back( geomEdge );
454 // create an edge if not yet exists
456 newNodes[0] = link.node1(), newNodes[1] = link.node2();
457 const SMDS_MeshElement* edge = tgtMesh->FindElement( newNodes, SMDSAbs_Edge );
458 if ( edge ) continue;
460 if ( link._reversed ) std::swap( newNodes[0], newNodes[1] );
463 edge = tgtMesh->AddEdge( newNodes[0], newNodes[1], link._medium );
465 TopoDS_Edge geomEdge = TopoDS::Edge(bndShapes.back());
466 helper.CheckNodeU( geomEdge, link._medium, u, projTol, /*force=*/true );
467 tgtFaceSM->RemoveNode( link._medium, /*isNodeDeleted=*/false );
468 tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)link._medium, geomEdge, u );
472 edge = tgtMesh->AddEdge( newNodes[0], newNodes[1]);
477 tgtMesh->SetMeshElementOnShape( edge, bndShapes.back() );
479 else if ( nbFaces > 2 )
481 return error( COMPERR_BAD_INPUT_MESH, "Non-manifold source mesh");
484 isFaceMeshed = ( link2Nb == linkCount.end() && !linkCount.empty());
487 // check that source faces do not overlap:
488 // there must be only two edges sharing each vertex and bound to sub-edges of theShape
489 SMESH_MeshEditor editor( &theMesh );
490 set<int>::iterator subID = subShapeIDs.begin();
491 for ( ; subID != subShapeIDs.end(); ++subID )
493 const TopoDS_Shape& s = tgtMesh->IndexToShape( *subID );
494 if ( s.ShapeType() != TopAbs_VERTEX ) continue;
495 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( TopoDS::Vertex(s), tgtMesh );
496 SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator(SMDSAbs_Edge);
498 while ( eIt->more() )
500 const SMDS_MeshElement* edge = eIt->next();
501 int sId = editor.FindShape( edge );
502 nbEdges += subShapeIDs.count( sId );
505 return false; // weird
507 return error( COMPERR_BAD_INPUT_MESH, "Source elements overlap one another");
512 return error( COMPERR_BAD_INPUT_MESH,
513 "Source elements don't cover totally the geometrical face" );
515 if ( helper.HasSeam() )
517 // links on seam edges are shared by two faces, so no edges were created on them
518 // by the previous detection of 2D mesh boundary
519 for ( size_t iE = 0; iE < edges.size(); ++iE )
521 if ( !helper.IsRealSeam( edges[iE] )) continue;
522 const TopoDS_Edge& seamEdge = edges[iE];
523 // to find nodes lying on the seamEdge we check nodes of mesh faces sharing a node on one
524 // of its vertices; after finding another node on seamEdge we continue the same way
525 // until finding all nodes.
526 TopoDS_Vertex seamVertex = helper.IthVertex( 0, seamEdge );
527 const SMDS_MeshNode* vertNode = SMESH_Algo::VertexNode( seamVertex, tgtMesh );
528 set< const SMDS_MeshNode* > checkedNodes; checkedNodes.insert( vertNode );
529 set< const SMDS_MeshElement* > checkedFaces;
530 // as a face can have more than one node on the seamEdge, there is a difficulty in selecting
531 // one of those nodes to treat next; so we simply find all nodes on the seamEdge and
532 // then sort them by U on edge
533 typedef list< pair< double, const SMDS_MeshNode* > > TUNodeList;
534 TUNodeList nodesOnSeam;
535 double u = helper.GetNodeU( seamEdge, vertNode );
536 nodesOnSeam.push_back( make_pair( u, vertNode ));
537 TUNodeList::iterator u2nIt = nodesOnSeam.begin();
538 for ( ; u2nIt != nodesOnSeam.end(); ++u2nIt )
540 const SMDS_MeshNode* startNode = (*u2nIt).second;
541 SMDS_ElemIteratorPtr faceIt = startNode->GetInverseElementIterator( SMDSAbs_Face );
542 while ( faceIt->more() )
544 const SMDS_MeshElement* face = faceIt->next();
545 if ( !checkedFaces.insert( face ).second ) continue;
546 for ( int i = 0, nbNodes = face->NbCornerNodes(); i < nbNodes; ++i )
548 const SMDS_MeshNode* n = face->GetNode( i );
549 if ( n == startNode || !checkedNodes.insert( n ).second ) continue;
550 if ( helper.CheckNodeU( seamEdge, n, u=0, projTol, /*force=*/true ))
551 nodesOnSeam.push_back( make_pair( u, n ));
555 // sort the found nodes by U on the seamEdge; most probably they are in a good order,
556 // so we can use the hint to spead-up map filling
557 map< double, const SMDS_MeshNode* > u2nodeMap;
558 for ( u2nIt = nodesOnSeam.begin(); u2nIt != nodesOnSeam.end(); ++u2nIt )
559 u2nodeMap.insert( u2nodeMap.end(), *u2nIt );
563 SMESH_MesherHelper seamHelper( theMesh );
564 seamHelper.SetSubShape( edges[ iE ]);
565 seamHelper.SetElementsOnShape( true );
567 if ( (*checkedFaces.begin())->IsQuadratic() )
568 for ( set< const SMDS_MeshElement* >::iterator fIt = checkedFaces.begin();
569 fIt != checkedFaces.end(); ++fIt )
570 seamHelper.AddTLinks( static_cast<const SMDS_MeshFace*>( *fIt ));
572 map< double, const SMDS_MeshNode* >::iterator n1, n2, u2nEnd = u2nodeMap.end();
573 for ( n2 = u2nodeMap.begin(), n1 = n2++; n2 != u2nEnd; ++n1, ++n2 )
575 const SMDS_MeshNode* node1 = n1->second;
576 const SMDS_MeshNode* node2 = n2->second;
577 seamHelper.AddEdge( node1, node2 );
578 if ( node2->getshapeId() == helper.GetSubShapeID() )
580 tgtFaceSM->RemoveNode( node2, /*isNodeDeleted=*/false );
581 tgtMesh->SetNodeOnEdge( const_cast<SMDS_MeshNode*>( node2 ), seamEdge, n2->first );
585 } // loop on edges to find seam ones
586 } // if ( helper.HasSeam() )
588 // notify sub-meshes of edges on computation
589 for ( size_t iE = 0; iE < edges.size(); ++iE )
591 SMESH_subMesh * sm = theMesh.GetSubMesh( edges[iE] );
592 if ( BRep_Tool::Degenerated( edges[iE] ))
593 sm->SetIsAlwaysComputed( true );
594 sm->ComputeStateEngine(SMESH_subMesh::CHECK_COMPUTE_STATE);
595 if ( sm->GetComputeState() != SMESH_subMesh::COMPUTE_OK )
596 return error(SMESH_Comment("Failed to create segments on the edge ")
597 << tgtMesh->ShapeToIndex( edges[iE ]));
604 vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
605 for ( size_t i = 0; i < srcMeshes.size(); ++i )
606 StdMeshers_Import_1D::importMesh( srcMeshes[i], theMesh, _sourceHyp, theShape );
611 //=============================================================================
613 * \brief Set needed event listeners and create a submesh for a copied mesh
615 * This method is called only if a submesh has HYP_OK algo_state.
617 //=============================================================================
619 void StdMeshers_Import_1D2D::SetEventListener(SMESH_subMesh* subMesh)
623 const TopoDS_Shape& tgtShape = subMesh->GetSubShape();
624 SMESH_Mesh* tgtMesh = subMesh->GetFather();
625 Hypothesis_Status aStatus;
626 CheckHypothesis( *tgtMesh, tgtShape, aStatus );
628 StdMeshers_Import_1D::setEventListener( subMesh, _sourceHyp );
630 void StdMeshers_Import_1D2D::SubmeshRestored(SMESH_subMesh* subMesh)
632 SetEventListener(subMesh);
635 //=============================================================================
637 * Predict nb of mesh entities created by Compute()
639 //=============================================================================
641 bool StdMeshers_Import_1D2D::Evaluate(SMESH_Mesh & theMesh,
642 const TopoDS_Shape & theShape,
643 MapShapeNbElems& aResMap)
645 if ( !_sourceHyp ) return false;
647 const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
648 if ( srcGroups.empty() )
649 return error("Invalid source groups");
651 vector<int> aVec(SMDSEntity_Last,0);
653 bool toCopyMesh, toCopyGroups;
654 _sourceHyp->GetCopySourceMesh(toCopyMesh, toCopyGroups);
655 if ( toCopyMesh ) // the whole mesh is copied
657 vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
658 for ( unsigned i = 0; i < srcMeshes.size(); ++i )
660 SMESH_subMesh* sm = StdMeshers_Import_1D::getSubMeshOfCopiedMesh( theMesh, *srcMeshes[i]);
661 if ( !sm || aResMap.count( sm )) continue; // already counted
662 const SMDS_MeshInfo& aMeshInfo = srcMeshes[i]->GetMeshDS()->GetMeshInfo();
663 for (int i = 0; i < SMDSEntity_Last; i++)
664 aVec[i] = aMeshInfo.NbEntities((SMDSAbs_EntityType)i);
669 // std-like iterator used to get coordinates of nodes of mesh element
670 typedef SMDS_StdIterator< SMESH_TNodeXYZ, SMDS_ElemIteratorPtr > TXyzIterator;
672 SMESH_MesherHelper helper(theMesh);
673 helper.SetSubShape(theShape);
675 const TopoDS_Face& geomFace = TopoDS::Face( theShape );
677 // take into account nodes on vertices
678 TopExp_Explorer exp( theShape, TopAbs_VERTEX );
679 for ( ; exp.More(); exp.Next() )
680 theMesh.GetSubMesh( exp.Current())->Evaluate( aResMap );
682 // to count now many times a link between nodes encounters,
683 // negative nb additionally means that a link is quadratic
684 map<SMESH_TLink, int> linkCount;
685 map<SMESH_TLink, int>::iterator link2Nb;
687 // count faces and nodes imported from groups
688 set<const SMDS_MeshNode* > allNodes;
690 double minGroupTol = 1e100;
691 for ( int iG = 0; iG < srcGroups.size(); ++iG )
693 const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
694 const double groupTol = 0.5 * sqrt( getMinElemSize2( srcGroup ));
695 minGroupTol = std::min( groupTol, minGroupTol );
696 SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
697 SMDS_MeshNode *tmpNode =helper.AddNode(0,0,0);
698 while ( srcElems->more() ) // loop on group contents
700 const SMDS_MeshElement* face = srcElems->next();
701 // find out if face is located on geomEdge by projecting
702 // a gravity center of face to geomFace
704 gc = accumulate( TXyzIterator(face->nodesIterator()), TXyzIterator(), gc)/face->NbNodes();
705 tmpNode->setXYZ( gc.X(), gc.Y(), gc.Z());
706 if ( helper.CheckNodeUV( geomFace, tmpNode, uv, groupTol, /*force=*/true ))
708 ++aVec[ face->GetEntityType() ];
711 int nbConers = face->NbCornerNodes();
712 for ( int i = 0; i < face->NbNodes(); ++i )
714 const SMDS_MeshNode* n1 = face->GetNode(i);
715 allNodes.insert( n1 );
718 const SMDS_MeshNode* n2 = face->GetNode( (i+1)%nbConers );
719 link2Nb = linkCount.insert( make_pair( SMESH_TLink( n1, n2 ), 0)).first;
720 if ( (*link2Nb).second )
721 link2Nb->second += (link2Nb->second < 0 ) ? -1 : 1;
723 link2Nb->second += ( face->IsQuadratic() ) ? -1 : 1;
728 helper.GetMeshDS()->RemoveNode(tmpNode);
731 int nbNodes = allNodes.size();
734 // count nodes and edges on geom edges
737 for ( exp.Init(theShape, TopAbs_EDGE); exp.More(); exp.Next() )
739 TopoDS_Edge geomEdge = TopoDS::Edge( exp.Current() );
740 SMESH_subMesh* sm = theMesh.GetSubMesh( geomEdge );
741 vector<int>& edgeVec = aResMap[sm];
742 if ( edgeVec.empty() )
744 edgeVec.resize(SMDSEntity_Last,0);
745 for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); )
747 const SMESH_TLink& link = (*link2Nb).first;
748 int nbFacesOfLink = Abs( link2Nb->second );
749 bool eraseLink = ( nbFacesOfLink != 1 );
750 if ( nbFacesOfLink == 1 )
752 if ( helper.CheckNodeU( geomEdge, link.node1(), u, minGroupTol, /*force=*/true )&&
753 helper.CheckNodeU( geomEdge, link.node2(), u, minGroupTol, /*force=*/true ))
755 bool isQuadratic = ( link2Nb->second < 0 );
756 ++edgeVec[ isQuadratic ? SMDSEntity_Quad_Edge : SMDSEntity_Edge ];
757 ++edgeVec[ SMDSEntity_Node ];
763 linkCount.erase(link2Nb++);
767 if ( edgeVec[ SMDSEntity_Node] > 0 )
768 --edgeVec[ SMDSEntity_Node ]; // for one node on vertex
770 else if ( !helper.IsSeamShape( geomEdge ) ||
771 geomEdge.Orientation() == TopAbs_FORWARD )
773 nbNodes -= 1+edgeVec[ SMDSEntity_Node ];
777 aVec[SMDSEntity_Node] = nbNodes;
780 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
781 aResMap.insert(make_pair(sm,aVec));