Back to index

salome-smesh  6.5.0
StdMeshers_Import_1D2D.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SMESH SMESH : implementaion of SMESH idl descriptions
00024 //  File   : StdMeshers_Import_1D2D.cxx
00025 //  Module : SMESH
00026 //
00027 #include "StdMeshers_Import_1D2D.hxx"
00028 
00029 #include "StdMeshers_Import_1D.hxx"
00030 #include "StdMeshers_ImportSource.hxx"
00031 
00032 #include "SMDS_MeshElement.hxx"
00033 #include "SMDS_MeshNode.hxx"
00034 #include "SMESHDS_Group.hxx"
00035 #include "SMESHDS_Mesh.hxx"
00036 #include "SMESH_Comment.hxx"
00037 #include "SMESH_Gen.hxx"
00038 #include "SMESH_Group.hxx"
00039 #include "SMESH_Mesh.hxx"
00040 #include "SMESH_MesherHelper.hxx"
00041 #include "SMESH_subMesh.hxx"
00042 
00043 #include "Utils_SALOME_Exception.hxx"
00044 #include "utilities.h"
00045 
00046 #include <BRep_Builder.hxx>
00047 #include <BRep_Tool.hxx>
00048 #include <TopExp.hxx>
00049 #include <TopExp_Explorer.hxx>
00050 #include <TopoDS.hxx>
00051 #include <TopoDS_Compound.hxx>
00052 #include <TopoDS_Edge.hxx>
00053 #include <TopoDS_Vertex.hxx>
00054 #include <Precision.hxx>
00055 
00056 #include <numeric>
00057 
00058 using namespace std;
00059 
00060 namespace
00061 {
00062   double getMinElemSize2( const SMESHDS_GroupBase* srcGroup )
00063   {
00064     double minSize2 = 1e100;
00065     SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
00066     while ( srcElems->more() ) // loop on group contents
00067     {
00068       const SMDS_MeshElement* face = srcElems->next();
00069       int nbN = face->NbCornerNodes();
00070 
00071       SMESH_TNodeXYZ prevN( face->GetNode( nbN-1 ));
00072       for ( int i = 0; i < nbN; ++i )
00073       {
00074         SMESH_TNodeXYZ n( face->GetNode( i ) );
00075         double size2 = ( n - prevN ).SquareModulus();
00076         minSize2 = std::min( minSize2, size2 );
00077         prevN = n;
00078       }
00079     }
00080     return minSize2;
00081   }
00082 }
00083 
00084 //=============================================================================
00088 //=============================================================================
00089 
00090 StdMeshers_Import_1D2D::StdMeshers_Import_1D2D(int hypId, int studyId, SMESH_Gen * gen)
00091   :SMESH_2D_Algo(hypId, studyId, gen), _sourceHyp(0)
00092 {
00093   MESSAGE("StdMeshers_Import_1D2D::StdMeshers_Import_1D2D");
00094   _name = "Import_1D2D";
00095   _shapeType = (1 << TopAbs_FACE);
00096 
00097   _compatibleHypothesis.push_back("ImportSource2D");
00098   _requireDiscreteBoundary = false;
00099 }
00100 
00101 //=============================================================================
00105 //=============================================================================
00106 
00107 bool StdMeshers_Import_1D2D::CheckHypothesis
00108                          (SMESH_Mesh&                          aMesh,
00109                           const TopoDS_Shape&                  aShape,
00110                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
00111 {
00112   _sourceHyp = 0;
00113 
00114   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
00115   if ( hyps.size() == 0 )
00116   {
00117     aStatus = SMESH_Hypothesis::HYP_MISSING;
00118     return false;  // can't work with no hypothesis
00119   }
00120 
00121   if ( hyps.size() > 1 )
00122   {
00123     aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
00124     return false;
00125   }
00126 
00127   const SMESHDS_Hypothesis *theHyp = hyps.front();
00128 
00129   string hypName = theHyp->GetName();
00130 
00131   if (hypName == _compatibleHypothesis.front())
00132   {
00133     _sourceHyp = (StdMeshers_ImportSource1D *)theHyp;
00134     aStatus = SMESH_Hypothesis::HYP_OK;
00135     return true;
00136   }
00137 
00138   aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
00139   return true;
00140 }
00141 
00142 namespace
00143 {
00147   struct TLink : public SMESH_OrientedLink
00148   {
00149     const SMDS_MeshNode* _medium;
00150     TLink( const SMDS_MeshNode* n1,
00151            const SMDS_MeshNode* n2,
00152            const SMDS_MeshNode* medium=0)
00153       : SMESH_OrientedLink( n1,n2 ), _medium( medium ) {}
00154   };
00155 }
00156 
00157 //=============================================================================
00161 //=============================================================================
00162 
00163 bool StdMeshers_Import_1D2D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
00164 {
00165   if ( !_sourceHyp ) return false;
00166 
00167   const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
00168   if ( srcGroups.empty() )
00169     return error("Invalid source groups");
00170 
00171   bool allGroupsEmpty = true;
00172   for ( size_t iG = 0; iG < srcGroups.size() && allGroupsEmpty; ++iG )
00173     allGroupsEmpty = srcGroups[iG]->GetGroupDS()->IsEmpty();
00174   if ( allGroupsEmpty )
00175     return error("No faces in source groups");
00176 
00177   SMESH_MesherHelper helper(theMesh);
00178   helper.SetSubShape(theShape);
00179   SMESHDS_Mesh* tgtMesh = theMesh.GetMeshDS();
00180 
00181   const TopoDS_Face& geomFace = TopoDS::Face( theShape );
00182   const double faceTol = helper.MaxTolerance( geomFace );
00183   const int shapeID = tgtMesh->ShapeToIndex( geomFace );
00184   const bool toCheckOri = (helper.NbAncestors( geomFace, theMesh, TopAbs_SOLID ) == 1 );
00185 
00186   Handle(Geom_Surface) surface = BRep_Tool::Surface( geomFace );
00187   const bool reverse = 
00188     ( helper.GetSubShapeOri( tgtMesh->ShapeToMesh(), geomFace) == TopAbs_REVERSED );
00189   gp_Pnt p; gp_Vec du, dv;
00190 
00191   set<int> subShapeIDs;
00192   subShapeIDs.insert( shapeID );
00193 
00194   // get nodes on vertices
00195   list < SMESH_TNodeXYZ > vertexNodes;
00196   list < SMESH_TNodeXYZ >::iterator vNIt;
00197   TopExp_Explorer exp( theShape, TopAbs_VERTEX );
00198   for ( ; exp.More(); exp.Next() )
00199   {
00200     const TopoDS_Vertex& v = TopoDS::Vertex( exp.Current() );
00201     if ( !subShapeIDs.insert( tgtMesh->ShapeToIndex( v )).second )
00202       continue;
00203     const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, tgtMesh );
00204     if ( !n )
00205     {
00206       _gen->Compute(theMesh,v,/*anUpward=*/true);
00207       n = SMESH_Algo::VertexNode( v, tgtMesh );
00208       if ( !n ) return false; // very strange
00209     }
00210     vertexNodes.push_back( SMESH_TNodeXYZ( n ));
00211   }
00212 
00213   // to count now many times a link between nodes encounters
00214   map<TLink, int> linkCount;
00215   map<TLink, int>::iterator link2Nb;
00216   double minGroupTol = Precision::Infinite();
00217 
00218   // =========================
00219   // Import faces from groups
00220   // =========================
00221 
00222   StdMeshers_Import_1D::TNodeNodeMap* n2n;
00223   StdMeshers_Import_1D::TElemElemMap* e2e;
00224   vector<const SMDS_MeshNode*> newNodes;
00225   for ( size_t iG = 0; iG < srcGroups.size(); ++iG )
00226   {
00227     const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
00228 
00229     const int meshID = srcGroup->GetMesh()->GetPersistentId();
00230     const SMESH_Mesh* srcMesh = GetMeshByPersistentID( meshID );
00231     if ( !srcMesh ) continue;
00232     StdMeshers_Import_1D::getMaps( srcMesh, &theMesh, n2n, e2e );
00233 
00234     const double groupTol = 0.5 * sqrt( getMinElemSize2( srcGroup ));
00235     minGroupTol = std::min( groupTol, minGroupTol );
00236 
00237     SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
00238     SMDS_MeshNode *tmpNode = helper.AddNode(0,0,0);
00239     gp_XY uv( Precision::Infinite(), Precision::Infinite() );
00240     while ( srcElems->more() ) // loop on group contents
00241     {
00242       const SMDS_MeshElement* face = srcElems->next();
00243       // find or create nodes of a new face
00244       newNodes.resize( face->NbNodes() );
00245       newNodes.back() = 0;
00246       int nbCreatedNodes = 0;
00247       SMDS_MeshElement::iterator node = face->begin_nodes();
00248       for ( size_t i = 0; i < newNodes.size(); ++i, ++node )
00249       {
00250         StdMeshers_Import_1D::TNodeNodeMap::iterator n2nIt = n2n->insert( make_pair( *node, (SMDS_MeshNode*)0 )).first;
00251         if ( n2nIt->second )
00252         {
00253           if ( !subShapeIDs.count( n2nIt->second->getshapeId() ))
00254             break;
00255         }
00256         else
00257         {
00258           // find an existing vertex node
00259           for ( vNIt = vertexNodes.begin(); vNIt != vertexNodes.end(); ++vNIt)
00260             if ( vNIt->SquareDistance( *node ) < groupTol * groupTol)
00261             {
00262               (*n2nIt).second = vNIt->_node;
00263               vertexNodes.erase( vNIt );
00264               break;
00265             }
00266         }
00267         if ( !n2nIt->second )
00268         {
00269           // find out if node lies on theShape
00270           tmpNode->setXYZ( (*node)->X(), (*node)->Y(), (*node)->Z());
00271           uv.SetCoord( Precision::Infinite(), Precision::Infinite() );
00272           if ( helper.CheckNodeUV( geomFace, tmpNode, uv, groupTol, /*force=*/true ))
00273           {
00274             SMDS_MeshNode* newNode = tgtMesh->AddNode( (*node)->X(), (*node)->Y(), (*node)->Z());
00275             n2nIt->second = newNode;
00276             tgtMesh->SetNodeOnFace( newNode, shapeID, uv.X(), uv.Y() );
00277             nbCreatedNodes++;
00278           }
00279         }
00280         if ( !(newNodes[i] = n2nIt->second ))
00281           break;
00282       }
00283       if ( !newNodes.back() )
00284         continue; // not all nodes of the face lie on theShape
00285 
00286       // try to find already created face
00287       SMDS_MeshElement * newFace = 0;
00288       if ( nbCreatedNodes == 0 &&
00289            tgtMesh->FindElement(newNodes, SMDSAbs_Face, /*noMedium=*/false))
00290         continue; // repeated face in source groups already created 
00291 
00292       // check future face orientation
00293       if ( toCheckOri )
00294       {
00295         int iNode = -1;
00296         gp_Vec geomNorm;
00297         do
00298         {
00299           uv = helper.GetNodeUV( geomFace, newNodes[++iNode] );
00300           surface->D1( uv.X(),uv.Y(), p, du,dv );
00301           geomNorm = reverse ? dv^du : du^dv;
00302         }
00303         while ( geomNorm.SquareMagnitude() < 1e-6 && iNode+1 < face->NbCornerNodes());
00304 
00305         int iNext = helper.WrapIndex( iNode+1, face->NbCornerNodes() );
00306         int iPrev = helper.WrapIndex( iNode-1, face->NbCornerNodes() );
00307 
00308         SMESH_TNodeXYZ prevNode( newNodes[iPrev] );
00309         SMESH_TNodeXYZ curNode ( newNodes[iNode] );
00310         SMESH_TNodeXYZ nextNode( newNodes[iNext] );
00311         gp_Vec n1n0( prevNode - curNode);
00312         gp_Vec n1n2( nextNode - curNode );
00313         gp_Vec meshNorm = n1n2 ^ n1n0;
00314 
00315         if ( geomNorm * meshNorm < 0 )
00316           std::reverse( newNodes.begin(), newNodes.end() );
00317       }
00318 
00319       // make a new face
00320       switch ( newNodes.size() )
00321       {
00322       case 3:
00323         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2] );
00324         break;
00325       case 4:
00326         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3] );
00327         break;
00328       case 6:
00329         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2],
00330                                     newNodes[3], newNodes[4], newNodes[5]);
00331         break;
00332       case 8:
00333         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3],
00334                                     newNodes[4], newNodes[5], newNodes[6], newNodes[7]);
00335         break;
00336       default: continue;
00337       }
00338       tgtMesh->SetMeshElementOnShape( newFace, shapeID );
00339       e2e->insert( make_pair( face, newFace ));
00340 
00341       // collect links
00342       int nbNodes = face->NbCornerNodes();
00343       const SMDS_MeshNode* medium = 0;
00344       for ( int i = 0; i < nbNodes; ++i )
00345       {
00346         const SMDS_MeshNode* n1 = newNodes[i];
00347         const SMDS_MeshNode* n2 = newNodes[ (i+1)%nbNodes ];
00348         if ( newFace->IsQuadratic() )
00349           medium = newNodes[i+nbNodes];
00350         link2Nb = linkCount.insert( make_pair( TLink( n1, n2, medium ), 0)).first;
00351         ++link2Nb->second;
00352         // if ( link2Nb->second == 1 )
00353         // {
00354         //   // measure link length
00355         //   double len2 = SMESH_TNodeXYZ( n1 ).SquareDistance( n2 );
00356         //   if ( len2 < minGroupTol )
00357         //     minGroupTol = len2;
00358         // }
00359       }
00360     }
00361     helper.GetMeshDS()->RemoveNode(tmpNode);
00362   }
00363 
00364   // ==========================================================
00365   // Put nodes on geom edges and create edges on them;
00366   // check if the whole geom face is covered by imported faces
00367   // ==========================================================
00368 
00369   vector< TopoDS_Edge > edges;
00370   for ( exp.Init( theShape, TopAbs_EDGE ); exp.More(); exp.Next() )
00371     if ( subShapeIDs.insert( tgtMesh->ShapeToIndex( exp.Current() )).second )
00372       edges.push_back( TopoDS::Edge( exp.Current() ));
00373 
00374   // use large tolerance for projection of nodes to edges because of
00375   // BLSURF mesher specifics (issue 0020918, Study2.hdf)
00376   const double projTol = minGroupTol;
00377 
00378   bool isFaceMeshed = false;
00379   SMESHDS_SubMesh* tgtFaceSM = tgtMesh->MeshElements( theShape );
00380   if ( tgtFaceSM )
00381   {
00382     // the imported mesh is valid if all external links (encountered once)
00383     // lie on geom edges
00384     subShapeIDs.erase( shapeID ); // to contain edges and vertices only
00385     double u, f, l;
00386     for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); ++link2Nb)
00387     {
00388       const TLink& link = (*link2Nb).first;
00389       int nbFaces = link2Nb->second;
00390       if ( nbFaces == 1 )
00391       {
00392         // check if a not shared link lies on face boundary
00393         bool nodesOnBoundary = true;
00394         list< TopoDS_Shape > bndShapes;
00395         for ( int is1stN = 0; is1stN < 2 && nodesOnBoundary; ++is1stN )
00396         {
00397           const SMDS_MeshNode* n = is1stN ? link.node1() : link.node2();
00398           if ( !subShapeIDs.count( n->getshapeId() ))
00399           {
00400             for ( size_t iE = 0; iE < edges.size(); ++iE )
00401               if ( helper.CheckNodeU( edges[iE], n, u=0, projTol, /*force=*/true ))
00402               {
00403                 BRep_Tool::Range(edges[iE],f,l);
00404                 if ( Abs(u-f) < 2 * faceTol || Abs(u-l) < 2 * faceTol )
00405                   // duplicated node on vertex
00406                   return error("Source elements overlap one another");
00407                 tgtFaceSM->RemoveNode( n, /*isNodeDeleted=*/false );
00408                 tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)n, edges[iE], u );
00409                 break;
00410               }
00411             nodesOnBoundary = subShapeIDs.count( n->getshapeId());
00412           }
00413           if ( nodesOnBoundary )
00414           {
00415             TopoDS_Shape s = helper.GetSubShapeByNode( n, tgtMesh );
00416             if ( s.ShapeType() == TopAbs_VERTEX )
00417               bndShapes.push_front( s ); // vertex first
00418             else
00419               bndShapes.push_back( s ); // edges last
00420           }
00421         }
00422         if ( !nodesOnBoundary )
00423         {
00424           error("free internal link"); // just for an easier debug
00425           break;
00426         }
00427         if ( bndShapes.front().ShapeType() == TopAbs_EDGE &&
00428              bndShapes.front() != bndShapes.back() )
00429           // link nodes on different geom edges
00430           return error(COMPERR_BAD_INPUT_MESH, "Source nodes mismatch target vertices");
00431 
00432         // find geom edge the link is on
00433         if ( bndShapes.back().ShapeType() != TopAbs_EDGE )
00434         {
00435           // find geom edge by two vertices
00436           TopoDS_Shape geomEdge = helper.GetCommonAncestor( bndShapes.back(),
00437                                                             bndShapes.front(),
00438                                                             theMesh, TopAbs_EDGE );
00439           if ( geomEdge.IsNull() )
00440           {
00441             error("free internal link");
00442             break; // vertices belong to different edges
00443           }
00444           bndShapes.push_back( geomEdge );
00445         }
00446 
00447         // create an edge if not yet exists
00448         newNodes.resize(2);
00449         newNodes[0] = link.node1(), newNodes[1] = link.node2();
00450         const SMDS_MeshElement* edge = tgtMesh->FindElement( newNodes, SMDSAbs_Edge );
00451         if ( edge ) continue;
00452 
00453         if ( link._reversed ) std::swap( newNodes[0], newNodes[1] );
00454         if ( link._medium )
00455         {
00456           newNodes.push_back( link._medium );
00457           edge = tgtMesh->AddEdge( newNodes[0], newNodes[1], newNodes[2] );
00458 
00459           TopoDS_Edge geomEdge = TopoDS::Edge(bndShapes.back());
00460           helper.CheckNodeU( geomEdge, link._medium, u, projTol, /*force=*/true );
00461           tgtFaceSM->RemoveNode( link._medium, /*isNodeDeleted=*/false );
00462           tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)link._medium, geomEdge, u );
00463         }
00464         else
00465         {
00466           edge = tgtMesh->AddEdge( newNodes[0], newNodes[1]);
00467         }
00468         if ( !edge )
00469           return false;
00470 
00471         tgtMesh->SetMeshElementOnShape( edge, bndShapes.back() );
00472       }
00473       else if ( nbFaces > 2 )
00474       {
00475         return error( COMPERR_BAD_INPUT_MESH, "Non-manifold source mesh");
00476       }
00477     }
00478     isFaceMeshed = ( link2Nb == linkCount.end() && !linkCount.empty());
00479     if ( isFaceMeshed )
00480     {
00481       // check that source faces do not overlap:
00482       // there must be only two edges sharing each vertex and bound to sub-edges of theShape
00483       SMESH_MeshEditor editor( &theMesh );
00484       set<int>::iterator subID = subShapeIDs.begin();
00485       for ( ; subID != subShapeIDs.end(); ++subID )
00486       {
00487         const TopoDS_Shape& s = tgtMesh->IndexToShape( *subID );
00488         if ( s.ShapeType() != TopAbs_VERTEX ) continue;
00489         const SMDS_MeshNode* n = SMESH_Algo::VertexNode( TopoDS::Vertex(s), tgtMesh );
00490         SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator(SMDSAbs_Edge);
00491         int nbEdges = 0;
00492         while ( eIt->more() )
00493         {
00494           const SMDS_MeshElement* edge = eIt->next();
00495           int sId = editor.FindShape( edge );
00496           nbEdges += subShapeIDs.count( sId );
00497         }
00498         if ( nbEdges < 2 )
00499           return false; // weird
00500         if ( nbEdges > 2 )
00501           return error( COMPERR_BAD_INPUT_MESH, "Source elements overlap one another");
00502       }
00503     }
00504   }
00505   if ( !isFaceMeshed )
00506     return error( COMPERR_BAD_INPUT_MESH,
00507                   "Source elements don't cover totally the geometrical face" );
00508 
00509   if ( helper.HasSeam() )
00510   {
00511     // links on seam edges are shared by two faces, so no edges were created on them
00512     // by the previous detection of 2D mesh boundary
00513     for ( size_t iE = 0; iE < edges.size(); ++iE )
00514     {
00515       if ( !helper.IsRealSeam( edges[iE] )) continue;
00516       const TopoDS_Edge& seamEdge = edges[iE];
00517       // to find nodes lying on the seamEdge we check nodes of mesh faces sharing a node on one
00518       // of its vertices; after finding another node on seamEdge we continue the same way
00519       // until finding all nodes.
00520       TopoDS_Vertex      seamVertex = helper.IthVertex( 0, seamEdge );
00521       const SMDS_MeshNode* vertNode = SMESH_Algo::VertexNode( seamVertex, tgtMesh );
00522       set< const SMDS_MeshNode* > checkedNodes; checkedNodes.insert( vertNode );
00523       set< const SMDS_MeshElement* > checkedFaces;
00524       // as a face can have more than one node on the seamEdge, there is a difficulty in selecting
00525       // one of those nodes to treat next; so we simply find all nodes on the seamEdge and
00526       // then sort them by U on edge
00527       typedef list< pair< double, const SMDS_MeshNode* > > TUNodeList;
00528       TUNodeList nodesOnSeam;
00529       double u = helper.GetNodeU( seamEdge, vertNode );
00530       nodesOnSeam.push_back( make_pair( u, vertNode ));
00531       TUNodeList::iterator u2nIt = nodesOnSeam.begin();
00532       for ( ; u2nIt != nodesOnSeam.end(); ++u2nIt )
00533       {
00534         const SMDS_MeshNode* startNode = (*u2nIt).second;
00535         SMDS_ElemIteratorPtr faceIt = startNode->GetInverseElementIterator( SMDSAbs_Face );
00536         while ( faceIt->more() )
00537         {
00538           const SMDS_MeshElement* face = faceIt->next();
00539           if ( !checkedFaces.insert( face ).second ) continue;
00540           for ( int i = 0, nbNodes = face->NbCornerNodes(); i < nbNodes; ++i )
00541           {
00542             const SMDS_MeshNode* n = face->GetNode( i );
00543             if ( n == startNode || !checkedNodes.insert( n ).second ) continue;
00544             if ( helper.CheckNodeU( seamEdge, n, u=0, projTol, /*force=*/true ))
00545               nodesOnSeam.push_back( make_pair( u, n ));
00546           }
00547         }
00548       }
00549       // sort the found nodes by U on the seamEdge; most probably they are in a good order,
00550       // so we can use the hint to spead-up map filling
00551       map< double, const SMDS_MeshNode* > u2nodeMap;
00552       for ( u2nIt = nodesOnSeam.begin(); u2nIt != nodesOnSeam.end(); ++u2nIt )
00553         u2nodeMap.insert( u2nodeMap.end(), *u2nIt );
00554 
00555       // create edges
00556       {
00557         SMESH_MesherHelper seamHelper( theMesh );
00558         seamHelper.SetSubShape( edges[ iE ]);
00559         seamHelper.SetElementsOnShape( true );
00560 
00561         if ( (*checkedFaces.begin())->IsQuadratic() )
00562           for ( set< const SMDS_MeshElement* >::iterator fIt = checkedFaces.begin();
00563                 fIt != checkedFaces.end(); ++fIt )
00564             seamHelper.AddTLinks( static_cast<const SMDS_MeshFace*>( *fIt ));
00565 
00566         map< double, const SMDS_MeshNode* >::iterator n1, n2, u2nEnd = u2nodeMap.end();
00567         for ( n2 = u2nodeMap.begin(), n1 = n2++; n2 != u2nEnd; ++n1, ++n2 )
00568         {
00569           const SMDS_MeshNode* node1 = n1->second;
00570           const SMDS_MeshNode* node2 = n2->second;
00571           seamHelper.AddEdge( node1, node2 );
00572           if ( node2->getshapeId() == helper.GetSubShapeID() )
00573           {
00574             tgtFaceSM->RemoveNode( node2, /*isNodeDeleted=*/false );
00575             tgtMesh->SetNodeOnEdge( const_cast<SMDS_MeshNode*>( node2 ), seamEdge, n2->first );
00576           }
00577         }
00578       }
00579     } // loop on edges to find seam ones
00580   } // if ( helper.HasSeam() )
00581 
00582   // notify sub-meshes of edges on computation
00583   for ( size_t iE = 0; iE < edges.size(); ++iE )
00584   {
00585     SMESH_subMesh * sm = theMesh.GetSubMesh( edges[iE] );
00586     if ( BRep_Tool::Degenerated( edges[iE] ))
00587       sm->SetIsAlwaysComputed( true );
00588     sm->ComputeStateEngine(SMESH_subMesh::CHECK_COMPUTE_STATE);
00589     if ( sm->GetComputeState() != SMESH_subMesh::COMPUTE_OK )
00590       return error(SMESH_Comment("Failed to create segments on the edge ")
00591                    << tgtMesh->ShapeToIndex( edges[iE ]));
00592   }
00593 
00594   // ============
00595   // Copy meshes
00596   // ============
00597 
00598   vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
00599   for ( size_t i = 0; i < srcMeshes.size(); ++i )
00600     StdMeshers_Import_1D::importMesh( srcMeshes[i], theMesh, _sourceHyp, theShape );
00601 
00602   return true;
00603 }
00604 
00605 //=============================================================================
00611 //=============================================================================
00612 
00613 void StdMeshers_Import_1D2D::SetEventListener(SMESH_subMesh* subMesh)
00614 {
00615   if ( !_sourceHyp )
00616   {
00617     const TopoDS_Shape& tgtShape = subMesh->GetSubShape();
00618     SMESH_Mesh*         tgtMesh  = subMesh->GetFather();
00619     Hypothesis_Status aStatus;
00620     CheckHypothesis( *tgtMesh, tgtShape, aStatus );
00621   }
00622   StdMeshers_Import_1D::setEventListener( subMesh, _sourceHyp );
00623 }
00624 void StdMeshers_Import_1D2D::SubmeshRestored(SMESH_subMesh* subMesh)
00625 {
00626   SetEventListener(subMesh);
00627 }
00628 
00629 //=============================================================================
00633 //=============================================================================
00634 
00635 bool StdMeshers_Import_1D2D::Evaluate(SMESH_Mesh &         theMesh,
00636                                       const TopoDS_Shape & theShape,
00637                                       MapShapeNbElems&     aResMap)
00638 {
00639   if ( !_sourceHyp ) return false;
00640 
00641   const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
00642   if ( srcGroups.empty() )
00643     return error("Invalid source groups");
00644 
00645   vector<int> aVec(SMDSEntity_Last,0);
00646 
00647   bool toCopyMesh, toCopyGroups;
00648   _sourceHyp->GetCopySourceMesh(toCopyMesh, toCopyGroups);
00649   if ( toCopyMesh ) // the whole mesh is copied
00650   {
00651     vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
00652     for ( unsigned i = 0; i < srcMeshes.size(); ++i )
00653     {
00654       SMESH_subMesh* sm = StdMeshers_Import_1D::getSubMeshOfCopiedMesh( theMesh, *srcMeshes[i]);
00655       if ( !sm || aResMap.count( sm )) continue; // already counted
00656       const SMDS_MeshInfo& aMeshInfo = srcMeshes[i]->GetMeshDS()->GetMeshInfo();
00657       for (int i = 0; i < SMDSEntity_Last; i++)
00658         aVec[i] = aMeshInfo.NbEntities((SMDSAbs_EntityType)i);
00659     }
00660   }
00661   else
00662   {
00663     // std-like iterator used to get coordinates of nodes of mesh element
00664     typedef SMDS_StdIterator< SMESH_TNodeXYZ, SMDS_ElemIteratorPtr > TXyzIterator;
00665 
00666     SMESH_MesherHelper helper(theMesh);
00667     helper.SetSubShape(theShape);
00668 
00669     const TopoDS_Face& geomFace = TopoDS::Face( theShape );
00670 
00671     // take into account nodes on vertices
00672     TopExp_Explorer exp( theShape, TopAbs_VERTEX );
00673     for ( ; exp.More(); exp.Next() )
00674       theMesh.GetSubMesh( exp.Current())->Evaluate( aResMap );
00675 
00676     // to count now many times a link between nodes encounters,
00677     // negative nb additionally means that a link is quadratic
00678     map<SMESH_TLink, int> linkCount;
00679     map<SMESH_TLink, int>::iterator link2Nb;
00680 
00681     // count faces and nodes imported from groups
00682     set<const SMDS_MeshNode* > allNodes;
00683     gp_XY uv;
00684     double minGroupTol = 1e100;
00685     for ( int iG = 0; iG < srcGroups.size(); ++iG )
00686     {
00687       const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
00688       const double groupTol = 0.5 * sqrt( getMinElemSize2( srcGroup ));
00689       minGroupTol = std::min( groupTol, minGroupTol );
00690       SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
00691       SMDS_MeshNode *tmpNode =helper.AddNode(0,0,0);
00692       while ( srcElems->more() ) // loop on group contents
00693       {
00694         const SMDS_MeshElement* face = srcElems->next();
00695         // find out if face is located on geomEdge by projecting
00696         // a gravity center of face to geomFace
00697         gp_XYZ gc(0,0,0);
00698         gc = accumulate( TXyzIterator(face->nodesIterator()), TXyzIterator(), gc)/face->NbNodes();
00699         tmpNode->setXYZ( gc.X(), gc.Y(), gc.Z());
00700         if ( helper.CheckNodeUV( geomFace, tmpNode, uv, groupTol, /*force=*/true ))
00701         {
00702           ++aVec[ face->GetEntityType() ];
00703 
00704           // collect links
00705           int nbConers = face->NbCornerNodes();
00706           for ( int i = 0; i < face->NbNodes(); ++i )
00707           {
00708             const SMDS_MeshNode* n1 = face->GetNode(i);
00709             allNodes.insert( n1 );
00710             if ( i < nbConers )
00711             {
00712               const SMDS_MeshNode* n2 = face->GetNode( (i+1)%nbConers );
00713               link2Nb = linkCount.insert( make_pair( SMESH_TLink( n1, n2 ), 0)).first;
00714               if ( (*link2Nb).second )
00715                 link2Nb->second += (link2Nb->second < 0 ) ? -1 : 1;
00716               else
00717                 link2Nb->second += ( face->IsQuadratic() ) ? -1 : 1;
00718             }
00719           }
00720         }
00721       }
00722       helper.GetMeshDS()->RemoveNode(tmpNode);
00723     }
00724 
00725     int nbNodes = allNodes.size();
00726     allNodes.clear();
00727 
00728     // count nodes and edges on geom edges
00729 
00730     double u;
00731     for ( exp.Init(theShape, TopAbs_EDGE); exp.More(); exp.Next() )
00732     {
00733       TopoDS_Edge geomEdge = TopoDS::Edge( exp.Current() );
00734       SMESH_subMesh* sm = theMesh.GetSubMesh( geomEdge );
00735       vector<int>& edgeVec = aResMap[sm];
00736       if ( edgeVec.empty() )
00737       {
00738         edgeVec.resize(SMDSEntity_Last,0);
00739         for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); )
00740         {
00741           const SMESH_TLink& link = (*link2Nb).first;
00742           int nbFacesOfLink = Abs( link2Nb->second );
00743           bool eraseLink = ( nbFacesOfLink != 1 );
00744           if ( nbFacesOfLink == 1 )
00745           {
00746             if ( helper.CheckNodeU( geomEdge, link.node1(), u, minGroupTol, /*force=*/true )&&
00747                  helper.CheckNodeU( geomEdge, link.node2(), u, minGroupTol, /*force=*/true ))
00748             {
00749               bool isQuadratic = ( link2Nb->second < 0 );
00750               ++edgeVec[ isQuadratic ? SMDSEntity_Quad_Edge : SMDSEntity_Edge ];
00751               ++edgeVec[ SMDSEntity_Node ];
00752               --nbNodes;
00753               eraseLink = true;
00754             }
00755           }
00756           if ( eraseLink )
00757             linkCount.erase(link2Nb++);
00758           else
00759             link2Nb++;
00760         }
00761         if ( edgeVec[ SMDSEntity_Node] > 0 )
00762           --edgeVec[ SMDSEntity_Node ]; // for one node on vertex
00763       }
00764       else if ( !helper.IsSeamShape( geomEdge ) ||
00765                 geomEdge.Orientation() == TopAbs_FORWARD )
00766       {
00767         nbNodes -= 1+edgeVec[ SMDSEntity_Node ];
00768       }
00769     }
00770 
00771     aVec[SMDSEntity_Node] = nbNodes;
00772   }
00773 
00774   SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
00775   aResMap.insert(make_pair(sm,aVec));
00776 
00777   return true;
00778 }