Salome HOME
Merge from PHASE_25_BR 14/11/2010
[modules/smesh.git] / src / StdMeshers / StdMeshers_Import_1D2D.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 //  File   : StdMeshers_Import_1D2D.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_Import_1D2D.hxx"
28
29 #include "StdMeshers_Import_1D.hxx"
30 #include "StdMeshers_ImportSource.hxx"
31
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"
42
43 #include "Utils_SALOME_Exception.hxx"
44 #include "utilities.h"
45
46 #include <BRep_Builder.hxx>
47 #include <BRep_Tool.hxx>
48 #include <TopExp.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Compound.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Vertex.hxx>
54
55 #include <numeric>
56
57 using namespace std;
58
59 //=============================================================================
60 /*!
61  * Creates StdMeshers_Import_1D2D
62  */
63 //=============================================================================
64
65 StdMeshers_Import_1D2D::StdMeshers_Import_1D2D(int hypId, int studyId, SMESH_Gen * gen)
66   :SMESH_2D_Algo(hypId, studyId, gen), _sourceHyp(0)
67 {
68   MESSAGE("StdMeshers_Import_1D2D::StdMeshers_Import_1D2D");
69   _name = "Import_1D2D";
70   _shapeType = (1 << TopAbs_FACE);
71
72   _compatibleHypothesis.push_back("ImportSource2D");
73   _requireDescretBoundary = false;
74 }
75
76 //=============================================================================
77 /*!
78  * Check presence of a hypothesis
79  */
80 //=============================================================================
81
82 bool StdMeshers_Import_1D2D::CheckHypothesis
83                          (SMESH_Mesh&                          aMesh,
84                           const TopoDS_Shape&                  aShape,
85                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
86 {
87   _sourceHyp = 0;
88
89   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
90   if ( hyps.size() == 0 )
91   {
92     aStatus = SMESH_Hypothesis::HYP_MISSING;
93     return false;  // can't work with no hypothesis
94   }
95
96   if ( hyps.size() > 1 )
97   {
98     aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
99     return false;
100   }
101
102   const SMESHDS_Hypothesis *theHyp = hyps.front();
103
104   string hypName = theHyp->GetName();
105
106   if (hypName == _compatibleHypothesis.front())
107   {
108     _sourceHyp = (StdMeshers_ImportSource1D *)theHyp;
109     aStatus = SMESH_Hypothesis::HYP_OK;
110     return true;
111   }
112
113   aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
114   return true;
115 }
116
117 namespace
118 {
119   /*!
120    * \brief OrientedLink additionally storing a medium node
121    */
122   struct TLink : public SMESH_OrientedLink
123   {
124     const SMDS_MeshNode* _medium;
125     TLink( const SMDS_MeshNode* n1,
126            const SMDS_MeshNode* n2,
127            const SMDS_MeshNode* medium=0)
128       : SMESH_OrientedLink( n1,n2 ), _medium( medium ) {}
129   };
130 }
131
132 //=============================================================================
133 /*!
134  * Import elements from the other mesh 
135  */
136 //=============================================================================
137
138 bool StdMeshers_Import_1D2D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
139 {
140   if ( !_sourceHyp ) return false;
141
142   const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
143   if ( srcGroups.empty() )
144     return error("Invalid source groups");
145
146   SMESH_MesherHelper helper(theMesh);
147   helper.SetSubShape(theShape);
148   SMESHDS_Mesh* tgtMesh = theMesh.GetMeshDS();
149
150   const TopoDS_Face& geomFace = TopoDS::Face( theShape );
151   const double faceTol = helper.MaxTolerance( geomFace );
152   const int shapeID = tgtMesh->ShapeToIndex( geomFace );
153   const bool toCheckOri = (helper.NbAncestors( geomFace, theMesh, TopAbs_SOLID ) == 1 );
154
155   Handle(Geom_Surface) surface = BRep_Tool::Surface( geomFace );
156   if ( helper.GetSubShapeOri( tgtMesh->ShapeToMesh(), geomFace) == TopAbs_REVERSED )
157     surface->UReverse();
158   gp_Pnt p; gp_Vec du, dv;
159
160   set<int> subShapeIDs;
161   subShapeIDs.insert( shapeID );
162
163   // get nodes on vertices
164   list < SMESH_MeshEditor::TNodeXYZ > vertexNodes;
165   list < SMESH_MeshEditor::TNodeXYZ >::iterator vNIt;
166   TopExp_Explorer exp( theShape, TopAbs_VERTEX );
167   for ( ; exp.More(); exp.Next() )
168   {
169     const TopoDS_Vertex& v = TopoDS::Vertex( exp.Current() );
170     if ( !subShapeIDs.insert( tgtMesh->ShapeToIndex( v )).second )
171       continue;
172     const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, tgtMesh );
173     if ( !n )
174     {
175       _gen->Compute(theMesh,v,/*anUpward=*/true);
176       n = SMESH_Algo::VertexNode( v, tgtMesh );
177       if ( !n ) return false; // very strange
178     }
179     vertexNodes.push_back( SMESH_MeshEditor::TNodeXYZ( n ));
180   }
181
182   // to count now many times a link between nodes encounters
183   map<TLink, int> linkCount;
184   map<TLink, int>::iterator link2Nb;
185
186   // =========================
187   // Import faces from groups
188   // =========================
189
190   StdMeshers_Import_1D::TNodeNodeMap* n2n;
191   StdMeshers_Import_1D::TElemElemMap* e2e;
192   vector<const SMDS_MeshNode*> newNodes;
193   for ( int iG = 0; iG < srcGroups.size(); ++iG )
194   {
195     const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
196
197     const int meshID = srcGroup->GetMesh()->GetPersistentId();
198     const SMESH_Mesh* srcMesh = GetMeshByPersistentID( meshID );
199     if ( !srcMesh ) continue;
200     StdMeshers_Import_1D::getMaps( srcMesh, &theMesh, n2n, e2e );
201
202     SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
203     SMDS_MeshNode tmpNode(0,0,0);
204     gp_XY uv;
205     while ( srcElems->more() ) // loop on group contents
206     {
207       const SMDS_MeshElement* face = srcElems->next();
208       // find or create nodes of a new face
209       newNodes.resize( face->NbNodes() );
210       newNodes.back() = 0;
211       int nbCreatedNodes = 0;
212       SMDS_MeshElement::iterator node = face->begin_nodes();
213       for ( unsigned i = 0; i < newNodes.size(); ++i, ++node )
214       {
215         TNodeNodeMap::iterator n2nIt = n2n->insert( make_pair( *node, (SMDS_MeshNode*)0 )).first;
216         if ( n2nIt->second )
217         {
218           if ( !subShapeIDs.count( n2nIt->second->GetPosition()->GetShapeId() ))
219             break;
220         }
221         else
222         {
223           // find an existing vertex node
224           for ( vNIt = vertexNodes.begin(); vNIt != vertexNodes.end(); ++vNIt)
225             if ( vNIt->SquareDistance( *node ) < 10 * faceTol * faceTol)
226             {
227               (*n2nIt).second = vNIt->_node;
228               vertexNodes.erase( vNIt );
229               break;
230             }
231         }
232         if ( !n2nIt->second )
233         {
234           // find out if node lies on theShape
235           tmpNode.setXYZ( (*node)->X(), (*node)->Y(), (*node)->Z());
236           if ( helper.CheckNodeUV( geomFace, &tmpNode, uv, 10 * faceTol, /*force=*/true ))
237           {
238             SMDS_MeshNode* newNode = tgtMesh->AddNode( (*node)->X(), (*node)->Y(), (*node)->Z());
239             n2nIt->second = newNode;
240             tgtMesh->SetNodeOnFace( newNode, shapeID, uv.X(), uv.Y() );
241             nbCreatedNodes++;
242           }
243         }
244         if ( !(newNodes[i] = n2nIt->second ))
245           break;
246       }
247       if ( !newNodes.back() )
248         continue; // not all nodes of the face lie on theShape
249
250       // try to find already created face
251       SMDS_MeshElement * newFace = 0;
252       if ( nbCreatedNodes == 0 &&
253            tgtMesh->FindElement(newNodes, SMDSAbs_Face, /*noMedium=*/false))
254         continue; // repeated face in source groups already created 
255
256       // check future face orientation
257       if ( toCheckOri )
258       {
259         int iNode = -1;
260         gp_Vec geomNorm;
261         do
262         {
263           uv = helper.GetNodeUV( geomFace, newNodes[++iNode] );
264           surface->D1( uv.X(),uv.Y(), p, du,dv );
265           geomNorm = du ^ dv;
266         }
267         while ( geomNorm.SquareMagnitude() < 1e-6 && iNode+1 < face->NbCornerNodes());
268
269         int iNext = helper.WrapIndex( iNode+1, face->NbCornerNodes() );
270         int iPrev = helper.WrapIndex( iNode-1, face->NbCornerNodes() );
271
272         gp_Vec n1n0( SMESH_MeshEditor::TNodeXYZ( newNodes[iPrev] ) -
273                      SMESH_MeshEditor::TNodeXYZ( newNodes[iNode] ));
274         gp_Vec n1n2( SMESH_MeshEditor::TNodeXYZ( newNodes[iNext] ) -
275                      SMESH_MeshEditor::TNodeXYZ( newNodes[iNode] ));
276         gp_Vec meshNorm = n1n2 ^ n1n0;
277
278         if ( geomNorm * meshNorm < 0 )
279           std::reverse( newNodes.begin(), newNodes.end() );
280       }
281
282       // make a new face
283       switch ( newNodes.size() )
284       {
285       case 3:
286         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2] );
287         break;
288       case 4:
289         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3] );
290         break;
291       case 6:
292         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2],
293                                     newNodes[3], newNodes[4], newNodes[5]);
294         break;
295       case 8:
296         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3],
297                                     newNodes[4], newNodes[5], newNodes[6], newNodes[7]);
298         break;
299       default: continue;
300       }
301       tgtMesh->SetMeshElementOnShape( newFace, shapeID );
302       e2e->insert( make_pair( face, newFace ));
303
304       // collect links
305       int nbNodes = face->NbCornerNodes();
306       const SMDS_MeshNode* medium = 0;
307       for ( int i = 0; i < nbNodes; ++i )
308       {
309         const SMDS_MeshNode* n1 = newNodes[i];
310         const SMDS_MeshNode* n2 = newNodes[ (i+1)%nbNodes ];
311         if ( newFace->IsQuadratic() )
312           medium = newNodes[i+nbNodes];
313         link2Nb = linkCount.insert( make_pair( TLink( n1, n2, medium ), 0)).first;
314         ++link2Nb->second;
315       }
316     }
317   }
318
319   // ==========================================================
320   // Put nodes on geom edges and create edges on them;
321   // check if the whole geom face is covered by imported faces
322   // ==========================================================
323
324   vector< TopoDS_Edge > edges;
325   for ( exp.Init( theShape, TopAbs_EDGE ); exp.More(); exp.Next() )
326     if ( subShapeIDs.insert( tgtMesh->ShapeToIndex( exp.Current() )).second )
327       edges.push_back( TopoDS::Edge( exp.Current() ));
328
329   bool isFaceMeshed = false;
330   if ( SMESHDS_SubMesh* tgtSM = tgtMesh->MeshElements( theShape ))
331   {
332     // the imported mesh is valid if all external links (encountered once)
333     // lie on geom edges
334     subShapeIDs.erase( shapeID ); // to contain edges and vertices only
335     double u, f, l;
336     for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); ++link2Nb)
337     {
338       const TLink& link = (*link2Nb).first;
339       int nbFaces = link2Nb->second;
340       if ( nbFaces == 1 )
341       {
342         // check if the link lie on face boundary
343         bool nodesOnBoundary = true;
344         list< TopoDS_Shape > bndShapes;
345         for ( int is1stN = 0; is1stN < 2 && nodesOnBoundary; ++is1stN )
346         {
347           const SMDS_MeshNode* n = is1stN ? link.node1() : link.node2();
348           if ( !subShapeIDs.count( n->GetPosition()->GetShapeId() ))
349           {
350             for ( unsigned iE = 0; iE < edges.size(); ++iE )
351               if ( helper.CheckNodeU( edges[iE], n, u, 10 * faceTol, /*force=*/true ))
352               {
353                 BRep_Tool::Range(edges[iE],f,l);
354                 if ( Abs(u-f) < 2 * faceTol || Abs(u-l) < 2 * faceTol )
355                   // duplicated node on vertex
356                   return error("Source elements overlap one another");
357                 tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)n, edges[iE], u );
358                 break;
359               }
360             nodesOnBoundary = subShapeIDs.count( n->GetPosition()->GetShapeId());
361           }
362           if ( nodesOnBoundary )
363           {
364             TopoDS_Shape s = helper.GetSubShapeByNode( n, tgtMesh );
365             if ( s.ShapeType() == TopAbs_VERTEX )
366               bndShapes.push_front( s ); // vertex first
367             else
368               bndShapes.push_back( s ); // edges last
369           }
370         }
371         if ( !nodesOnBoundary )
372           break; // free internal link
373         if ( bndShapes.front().ShapeType() == TopAbs_EDGE &&
374              bndShapes.front() != bndShapes.back() )
375           break; // link nodes on different geom edges
376
377         // find geom edge the link is on
378         if ( bndShapes.back().ShapeType() != TopAbs_EDGE )
379         {
380           // find geom edge by two vertices
381           TopoDS_Shape geomEdge;
382           PShapeIteratorPtr edgeIt = helper.GetAncestors( bndShapes.back(), theMesh, TopAbs_EDGE );
383           while ( edgeIt->more() )
384           {
385             geomEdge = *(edgeIt->next());
386             if ( !helper.IsSubShape( bndShapes.front(), geomEdge ))
387               geomEdge.Nullify();
388           }
389           if ( geomEdge.IsNull() )
390             break; // vertices belong to different edges -> free internal link
391           bndShapes.push_back( geomEdge );
392         }
393
394         // create an edge if not yet exists
395         newNodes.resize(2);
396         newNodes[0] = link.node1(), newNodes[1] = link.node2();
397         const SMDS_MeshElement* edge = tgtMesh->FindElement( newNodes, SMDSAbs_Edge );
398         if ( edge ) continue;
399
400         if ( link._reversed ) std::swap( newNodes[0], newNodes[1] );
401         if ( link._medium )
402         {
403           newNodes.push_back( link._medium );
404           edge = tgtMesh->AddEdge( newNodes[0], newNodes[1], newNodes[2] );
405
406           TopoDS_Edge geomEdge = TopoDS::Edge(bndShapes.back());
407           helper.CheckNodeU( geomEdge, link._medium, u, 10*faceTol, /*force=*/true );
408           tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)link._medium, geomEdge, u );
409         }
410         else
411         {
412           edge = tgtMesh->AddEdge( newNodes[0], newNodes[1]);
413         }
414         // remove nodes from submesh of theShape
415         for ( unsigned i = 0; i < newNodes.size(); ++i )
416           tgtSM->RemoveNode( newNodes[i], /*isNodeDeleted=*/false );
417         if ( !edge )
418           return false;
419
420         tgtMesh->SetMeshElementOnShape( edge, bndShapes.back() );
421       }
422       else if ( nbFaces > 2 )
423       {
424         return error( "Non-manifold source mesh");
425       }
426     }
427     isFaceMeshed = ( link2Nb == linkCount.end() && !linkCount.empty());
428     if ( isFaceMeshed )
429     {
430       // check that source faces do not overlap:
431       // there must be only two edges sharing each vertex and bound to sub-edges of theShape
432       SMESH_MeshEditor editor( &theMesh );
433       set<int>::iterator subID = subShapeIDs.begin();
434       for ( ; subID != subShapeIDs.end(); ++subID )
435       {
436         const TopoDS_Shape& s = tgtMesh->IndexToShape( *subID );
437         if ( s.ShapeType() != TopAbs_VERTEX ) continue;
438         const SMDS_MeshNode* n = SMESH_Algo::VertexNode( TopoDS::Vertex(s), tgtMesh );
439         SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator(SMDSAbs_Edge);
440         int nbEdges = 0;
441         while ( eIt->more() )
442         {
443           const SMDS_MeshElement* edge = eIt->next();
444           int sId = editor.FindShape( edge );
445           nbEdges += subShapeIDs.count( sId );
446         }
447         if ( nbEdges < 2 )
448           return false; // weird
449         if ( nbEdges > 2 )
450           return error( "Source elements overlap one another");
451       }
452     }
453   }
454   if ( !isFaceMeshed )
455     return error( "Source elements don't cover totally the geometrical face" );
456
457   // notify sub-meshes of edges on computation
458   for ( unsigned iE = 0; iE < edges.size(); ++iE )
459     theMesh.GetSubMesh( edges[iE] )->ComputeStateEngine(SMESH_subMesh::CHECK_COMPUTE_STATE);
460
461   // ============
462   // Copy meshes
463   // ============
464
465   vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
466   for ( unsigned i = 0; i < srcMeshes.size(); ++i )
467     StdMeshers_Import_1D::importMesh( srcMeshes[i], theMesh, _sourceHyp, theShape );
468
469   return true;
470 }
471
472 //=============================================================================
473 /*!
474  * \brief Set needed event listeners and create a submesh for a copied mesh
475  *
476  * This method is called only if a submesh has HYP_OK algo_state.
477  */
478 //=============================================================================
479
480 void StdMeshers_Import_1D2D::SetEventListener(SMESH_subMesh* subMesh)
481 {
482   if ( !_sourceHyp )
483   {
484     const TopoDS_Shape& tgtShape = subMesh->GetSubShape();
485     SMESH_Mesh*         tgtMesh  = subMesh->GetFather();
486     Hypothesis_Status aStatus;
487     CheckHypothesis( *tgtMesh, tgtShape, aStatus );
488   }
489   StdMeshers_Import_1D::setEventListener( subMesh, _sourceHyp );
490 }
491 void StdMeshers_Import_1D2D::SubmeshRestored(SMESH_subMesh* subMesh)
492 {
493   SetEventListener(subMesh);
494 }
495
496 //=============================================================================
497 /*!
498  * Predict nb of mesh entities created by Compute()
499  */
500 //=============================================================================
501
502 bool StdMeshers_Import_1D2D::Evaluate(SMESH_Mesh &         theMesh,
503                                       const TopoDS_Shape & theShape,
504                                       MapShapeNbElems&     aResMap)
505 {
506   if ( !_sourceHyp ) return false;
507
508   const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
509   if ( srcGroups.empty() )
510     return error("Invalid source groups");
511
512   vector<int> aVec(SMDSEntity_Last,0);
513
514   bool toCopyMesh, toCopyGroups;
515   _sourceHyp->GetCopySourceMesh(toCopyMesh, toCopyGroups);
516   if ( toCopyMesh ) // the whole mesh is copied
517   {
518     vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
519     for ( unsigned i = 0; i < srcMeshes.size(); ++i )
520     {
521       SMESH_subMesh* sm = StdMeshers_Import_1D::getSubMeshOfCopiedMesh( theMesh, *srcMeshes[i]);
522       if ( !sm || aResMap.count( sm )) continue; // already counted
523       const SMDS_MeshInfo& aMeshInfo = srcMeshes[i]->GetMeshDS()->GetMeshInfo();
524       for (int i = 0; i < SMDSEntity_Last; i++)
525         aVec[i] = aMeshInfo.NbEntities((SMDSAbs_EntityType)i);
526     }
527   }
528   else
529   {
530     // std-like iterator used to get coordinates of nodes of mesh element
531     typedef SMDS_StdIterator< SMESH_MeshEditor::TNodeXYZ, SMDS_ElemIteratorPtr > TXyzIterator;
532
533     SMESH_MesherHelper helper(theMesh);
534     helper.SetSubShape(theShape);
535
536     const TopoDS_Face& geomFace = TopoDS::Face( theShape );
537     const double faceTol = helper.MaxTolerance( geomFace );
538
539     // take into account nodes on vertices
540     TopExp_Explorer exp( theShape, TopAbs_VERTEX );
541     for ( ; exp.More(); exp.Next() )
542       theMesh.GetSubMesh( exp.Current())->Evaluate( aResMap );
543
544     // to count now many times a link between nodes encounters,
545     // negative nb additionally means that a link is quadratic
546     map<SMESH_TLink, int> linkCount;
547     map<SMESH_TLink, int>::iterator link2Nb;
548
549     // count faces and nodes imported from groups
550     set<const SMDS_MeshNode* > allNodes;
551     gp_XY uv;
552     for ( int iG = 0; iG < srcGroups.size(); ++iG )
553     {
554       const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
555       SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
556       SMDS_MeshNode tmpNode(0,0,0);
557       while ( srcElems->more() ) // loop on group contents
558       {
559         const SMDS_MeshElement* face = srcElems->next();
560         // find out if face is located on geomEdge by projecting
561         // a gravity center of face to geomFace
562         gp_XYZ gc(0,0,0);
563         gc = accumulate( TXyzIterator(face->nodesIterator()), TXyzIterator(), gc)/face->NbNodes();
564         tmpNode.setXYZ( gc.X(), gc.Y(), gc.Z());
565         if ( helper.CheckNodeUV( geomFace, &tmpNode, uv, 10 * faceTol, /*force=*/true ))
566         {
567           ++aVec[ face->GetEntityType() ];
568
569           // collect links
570           int nbConers = face->NbCornerNodes();
571           for ( int i = 0; i < face->NbNodes(); ++i )
572           {
573             const SMDS_MeshNode* n1 = face->GetNode(i);
574             allNodes.insert( n1 );
575             if ( i < nbConers )
576             {
577               const SMDS_MeshNode* n2 = face->GetNode( (i+1)%nbConers );
578               link2Nb = linkCount.insert( make_pair( SMESH_TLink( n1, n2 ), 0)).first;
579               if ( (*link2Nb).second )
580                 link2Nb->second += (link2Nb->second < 0 ) ? -1 : 1;
581               else
582                 link2Nb->second += ( face->IsQuadratic() ) ? -1 : 1;
583             }
584           }
585         }
586       }
587     }
588
589     int nbNodes = allNodes.size();
590     allNodes.clear();
591
592     // count nodes and edges on geom edges
593
594     double u;
595     for ( exp.Init(theShape, TopAbs_EDGE); exp.More(); exp.Next() )
596     {
597       TopoDS_Edge geomEdge = TopoDS::Edge( exp.Current() );
598       SMESH_subMesh* sm = theMesh.GetSubMesh( geomEdge );
599       vector<int>& edgeVec = aResMap[sm];
600       if ( edgeVec.empty() )
601       {
602         edgeVec.resize(SMDSEntity_Last,0);
603         for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); )
604         {
605           const SMESH_TLink& link = (*link2Nb).first;
606           int nbFacesOfLink = Abs( link2Nb->second );
607           bool eraseLink = ( nbFacesOfLink != 1 );
608           if ( nbFacesOfLink == 1 )
609           {
610             if ( helper.CheckNodeU( geomEdge, link.node1(), u, 10*faceTol, /*force=*/true )&&
611                  helper.CheckNodeU( geomEdge, link.node2(), u, 10*faceTol, /*force=*/true ))
612             {
613               bool isQuadratic = ( link2Nb->second < 0 );
614               ++edgeVec[ isQuadratic ? SMDSEntity_Quad_Edge : SMDSEntity_Edge ];
615               ++edgeVec[ SMDSEntity_Node ];
616               --nbNodes;
617               eraseLink = true;
618             }
619           }
620           if ( eraseLink )
621             linkCount.erase(link2Nb++);
622           else
623             link2Nb++;
624         }
625         if ( edgeVec[ SMDSEntity_Node] > 0 )
626           --edgeVec[ SMDSEntity_Node ]; // for one node on vertex
627       }
628       else if ( !helper.IsSeamShape( geomEdge ) ||
629                 geomEdge.Orientation() == TopAbs_FORWARD )
630       {
631         nbNodes -= 1+edgeVec[ SMDSEntity_Node ];
632       }
633     }
634
635     aVec[SMDSEntity_Node] = nbNodes;
636   }
637
638   SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
639   aResMap.insert(make_pair(sm,aVec));
640
641   return true;
642 }